Typo.
[emacs.git] / src / xterm.c
blob635fe3f28e4a3e6fea0bd440a6084cf283ee0e35
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
25 #include <config.h>
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
31 #include <stdio.h>
33 #ifdef HAVE_X_WINDOWS
35 #include "lisp.h"
36 #include "blockinput.h"
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
57 #include "systty.h"
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 #define ARROW_SCROLLBAR
134 #include <X11/Xaw3d/ScrollbarP.h>
135 #else /* !HAVE_XAW3D */
136 #include <X11/Xaw/Simple.h>
137 #include <X11/Xaw/Scrollbar.h>
138 #endif /* !HAVE_XAW3D */
139 #ifndef XtNpickTop
140 #define XtNpickTop "pickTop"
141 #endif /* !XtNpickTop */
142 #endif /* !USE_MOTIF */
143 #endif /* USE_TOOLKIT_SCROLL_BARS */
145 #endif /* USE_X_TOOLKIT */
147 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
148 #define x_any_window_to_frame x_window_to_frame
149 #define x_top_window_to_frame x_window_to_frame
150 #endif
152 #ifdef USE_X_TOOLKIT
153 #include "widget.h"
154 #ifndef XtNinitialState
155 #define XtNinitialState "initialState"
156 #endif
157 #endif
159 #define abs(x) ((x) < 0 ? -(x) : (x))
163 /* Non-nil means Emacs uses toolkit scroll bars. */
165 Lisp_Object Vx_toolkit_scroll_bars;
167 /* Non-zero means that a HELP_EVENT has been generated since Emacs
168 start. */
170 static int any_help_event_p;
172 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
173 static Lisp_Object last_window;
175 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
177 int x_use_underline_position_properties;
179 /* This is a chain of structures for all the X displays currently in
180 use. */
182 struct x_display_info *x_display_list;
184 /* This is a list of cons cells, each of the form (NAME
185 . FONT-LIST-CACHE), one for each element of x_display_list and in
186 the same order. NAME is the name of the frame. FONT-LIST-CACHE
187 records previous values returned by x-list-fonts. */
189 Lisp_Object x_display_name_list;
191 /* Frame being updated by update_frame. This is declared in term.c.
192 This is set by update_begin and looked at by all the XT functions.
193 It is zero while not inside an update. In that case, the XT
194 functions assume that `selected_frame' is the frame to apply to. */
196 extern struct frame *updating_frame;
198 /* This is a frame waiting to be auto-raised, within XTread_socket. */
200 struct frame *pending_autoraise_frame;
202 #ifdef USE_X_TOOLKIT
203 /* The application context for Xt use. */
204 XtAppContext Xt_app_con;
205 static String Xt_default_resources[] = {0};
206 #endif /* USE_X_TOOLKIT */
208 /* Non-zero means user is interacting with a toolkit scroll bar. */
210 static int toolkit_scroll_bar_interaction;
212 /* Mouse movement.
214 Formerly, we used PointerMotionHintMask (in standard_event_mask)
215 so that we would have to call XQueryPointer after each MotionNotify
216 event to ask for another such event. However, this made mouse tracking
217 slow, and there was a bug that made it eventually stop.
219 Simply asking for MotionNotify all the time seems to work better.
221 In order to avoid asking for motion events and then throwing most
222 of them away or busy-polling the server for mouse positions, we ask
223 the server for pointer motion hints. This means that we get only
224 one event per group of mouse movements. "Groups" are delimited by
225 other kinds of events (focus changes and button clicks, for
226 example), or by XQueryPointer calls; when one of these happens, we
227 get another MotionNotify event the next time the mouse moves. This
228 is at least as efficient as getting motion events when mouse
229 tracking is on, and I suspect only negligibly worse when tracking
230 is off. */
232 /* Where the mouse was last time we reported a mouse event. */
234 static XRectangle last_mouse_glyph;
235 static Lisp_Object last_mouse_press_frame;
237 /* The scroll bar in which the last X motion event occurred.
239 If the last X motion event occurred in a scroll bar, we set this so
240 XTmouse_position can know whether to report a scroll bar motion or
241 an ordinary motion.
243 If the last X motion event didn't occur in a scroll bar, we set
244 this to Qnil, to tell XTmouse_position to return an ordinary motion
245 event. */
247 static Lisp_Object last_mouse_scroll_bar;
249 /* This is a hack. We would really prefer that XTmouse_position would
250 return the time associated with the position it returns, but there
251 doesn't seem to be any way to wrest the time-stamp from the server
252 along with the position query. So, we just keep track of the time
253 of the last movement we received, and return that in hopes that
254 it's somewhat accurate. */
256 static Time last_mouse_movement_time;
258 /* Incremented by XTread_socket whenever it really tries to read
259 events. */
261 #ifdef __STDC__
262 static int volatile input_signal_count;
263 #else
264 static int input_signal_count;
265 #endif
267 /* Used locally within XTread_socket. */
269 static int x_noop_count;
271 /* Initial values of argv and argc. */
273 extern char **initial_argv;
274 extern int initial_argc;
276 extern Lisp_Object Vcommand_line_args, Vsystem_name;
278 /* Tells if a window manager is present or not. */
280 extern Lisp_Object Vx_no_window_manager;
282 extern Lisp_Object Qface, Qmouse_face, Qeql;
284 extern int errno;
286 /* A mask of extra modifier bits to put into every keyboard char. */
288 extern EMACS_INT extra_keyboard_modifiers;
290 /* The keysyms to use for the various modifiers. */
292 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
293 Lisp_Object Vx_keysym_table;
294 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
296 static Lisp_Object Qvendor_specific_keysyms;
297 static Lisp_Object Qlatin_1, Qutf_8;
299 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
302 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
303 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
304 static const XColor *x_color_cells P_ ((Display *, int *));
305 static void x_update_window_end P_ ((struct window *, int, int));
306 void x_delete_display P_ ((struct x_display_info *));
307 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
308 unsigned));
309 static int x_io_error_quitter P_ ((Display *));
310 int x_catch_errors P_ ((Display *));
311 void x_uncatch_errors P_ ((Display *, int));
312 void x_lower_frame P_ ((struct frame *));
313 void x_scroll_bar_clear P_ ((struct frame *));
314 int x_had_errors_p P_ ((Display *));
315 void x_wm_set_size_hint P_ ((struct frame *, long, int));
316 void x_raise_frame P_ ((struct frame *));
317 void x_set_window_size P_ ((struct frame *, int, int, int));
318 void x_wm_set_window_state P_ ((struct frame *, int));
319 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
320 void x_initialize P_ ((void));
321 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
322 static int x_compute_min_glyph_bounds P_ ((struct frame *));
323 static void x_update_end P_ ((struct frame *));
324 static void XTframe_up_to_date P_ ((struct frame *));
325 static void XTset_terminal_modes P_ ((void));
326 static void XTreset_terminal_modes P_ ((void));
327 static void x_clear_frame P_ ((void));
328 static void frame_highlight P_ ((struct frame *));
329 static void frame_unhighlight P_ ((struct frame *));
330 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
331 static int x_focus_changed P_ ((int,
332 int,
333 struct x_display_info *,
334 struct frame *,
335 struct input_event *,
336 int));
337 static int x_detect_focus_change P_ ((struct x_display_info *,
338 XEvent *,
339 struct input_event *,
340 int));
341 static void XTframe_rehighlight P_ ((struct frame *));
342 static void x_frame_rehighlight P_ ((struct x_display_info *));
343 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
344 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
345 enum text_cursor_kinds));
347 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
348 GC, int));
349 static void x_flush P_ ((struct frame *f));
350 static void x_update_begin P_ ((struct frame *));
351 static void x_update_window_begin P_ ((struct window *));
352 static void x_after_update_window_line P_ ((struct glyph_row *));
353 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
354 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
355 enum scroll_bar_part *,
356 Lisp_Object *, Lisp_Object *,
357 unsigned long *));
358 static void x_check_fullscreen P_ ((struct frame *));
359 static void x_check_fullscreen_move P_ ((struct frame *));
360 static int handle_one_xevent P_ ((struct x_display_info *,
361 XEvent *,
362 struct input_event **,
363 int *,
364 int *));
367 /* Flush display of frame F, or of all frames if F is null. */
369 static void
370 x_flush (f)
371 struct frame *f;
373 BLOCK_INPUT;
374 if (f == NULL)
376 Lisp_Object rest, frame;
377 FOR_EACH_FRAME (rest, frame)
378 x_flush (XFRAME (frame));
380 else if (FRAME_X_P (f))
381 XFlush (FRAME_X_DISPLAY (f));
382 UNBLOCK_INPUT;
386 /* Remove calls to XFlush by defining XFlush to an empty replacement.
387 Calls to XFlush should be unnecessary because the X output buffer
388 is flushed automatically as needed by calls to XPending,
389 XNextEvent, or XWindowEvent according to the XFlush man page.
390 XTread_socket calls XPending. Removing XFlush improves
391 performance. */
393 #define XFlush(DISPLAY) (void) 0
396 /***********************************************************************
397 Debugging
398 ***********************************************************************/
400 #if 0
402 /* This is a function useful for recording debugging information about
403 the sequence of occurrences in this file. */
405 struct record
407 char *locus;
408 int type;
411 struct record event_record[100];
413 int event_record_index;
415 record_event (locus, type)
416 char *locus;
417 int type;
419 if (event_record_index == sizeof (event_record) / sizeof (struct record))
420 event_record_index = 0;
422 event_record[event_record_index].locus = locus;
423 event_record[event_record_index].type = type;
424 event_record_index++;
427 #endif /* 0 */
431 /* Return the struct x_display_info corresponding to DPY. */
433 struct x_display_info *
434 x_display_info_for_display (dpy)
435 Display *dpy;
437 struct x_display_info *dpyinfo;
439 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
440 if (dpyinfo->display == dpy)
441 return dpyinfo;
443 return 0;
448 /***********************************************************************
449 Starting and ending an update
450 ***********************************************************************/
452 /* Start an update of frame F. This function is installed as a hook
453 for update_begin, i.e. it is called when update_begin is called.
454 This function is called prior to calls to x_update_window_begin for
455 each window being updated. Currently, there is nothing to do here
456 because all interesting stuff is done on a window basis. */
458 static void
459 x_update_begin (f)
460 struct frame *f;
462 /* Nothing to do. */
466 /* Start update of window W. Set the global variable updated_window
467 to the window being updated and set output_cursor to the cursor
468 position of W. */
470 static void
471 x_update_window_begin (w)
472 struct window *w;
474 struct frame *f = XFRAME (WINDOW_FRAME (w));
475 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
477 updated_window = w;
478 set_output_cursor (&w->cursor);
480 BLOCK_INPUT;
482 if (f == display_info->mouse_face_mouse_frame)
484 /* Don't do highlighting for mouse motion during the update. */
485 display_info->mouse_face_defer = 1;
487 /* If F needs to be redrawn, simply forget about any prior mouse
488 highlighting. */
489 if (FRAME_GARBAGED_P (f))
490 display_info->mouse_face_window = Qnil;
492 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
493 their mouse_face_p flag set, which means that they are always
494 unequal to rows in a desired matrix which never have that
495 flag set. So, rows containing mouse-face glyphs are never
496 scrolled, and we don't have to switch the mouse highlight off
497 here to prevent it from being scrolled. */
499 /* Can we tell that this update does not affect the window
500 where the mouse highlight is? If so, no need to turn off.
501 Likewise, don't do anything if the frame is garbaged;
502 in that case, the frame's current matrix that we would use
503 is all wrong, and we will redisplay that line anyway. */
504 if (!NILP (display_info->mouse_face_window)
505 && w == XWINDOW (display_info->mouse_face_window))
507 int i;
509 for (i = 0; i < w->desired_matrix->nrows; ++i)
510 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
511 break;
513 if (i < w->desired_matrix->nrows)
514 clear_mouse_face (display_info);
516 #endif /* 0 */
519 UNBLOCK_INPUT;
523 /* Draw a vertical window border from (x,y0) to (x,y1) */
525 static void
526 x_draw_vertical_window_border (w, x, y0, y1)
527 struct window *w;
528 int x, y0, y1;
530 struct frame *f = XFRAME (WINDOW_FRAME (w));
532 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
533 f->output_data.x->normal_gc, x, y0, x, y1);
536 /* End update of window W (which is equal to updated_window).
538 Draw vertical borders between horizontally adjacent windows, and
539 display W's cursor if CURSOR_ON_P is non-zero.
541 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
542 glyphs in mouse-face were overwritten. In that case we have to
543 make sure that the mouse-highlight is properly redrawn.
545 W may be a menu bar pseudo-window in case we don't have X toolkit
546 support. Such windows don't have a cursor, so don't display it
547 here. */
549 static void
550 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
551 struct window *w;
552 int cursor_on_p, mouse_face_overwritten_p;
554 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
556 if (!w->pseudo_window_p)
558 BLOCK_INPUT;
560 if (cursor_on_p)
561 display_and_set_cursor (w, 1, output_cursor.hpos,
562 output_cursor.vpos,
563 output_cursor.x, output_cursor.y);
565 x_draw_vertical_border (w);
566 UNBLOCK_INPUT;
569 /* If a row with mouse-face was overwritten, arrange for
570 XTframe_up_to_date to redisplay the mouse highlight. */
571 if (mouse_face_overwritten_p)
573 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
574 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
575 dpyinfo->mouse_face_window = Qnil;
578 updated_window = NULL;
582 /* End update of frame F. This function is installed as a hook in
583 update_end. */
585 static void
586 x_update_end (f)
587 struct frame *f;
589 /* Mouse highlight may be displayed again. */
590 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
592 #ifndef XFlush
593 BLOCK_INPUT;
594 XFlush (FRAME_X_DISPLAY (f));
595 UNBLOCK_INPUT;
596 #endif
600 /* This function is called from various places in xdisp.c whenever a
601 complete update has been performed. The global variable
602 updated_window is not available here. */
604 static void
605 XTframe_up_to_date (f)
606 struct frame *f;
608 if (FRAME_X_P (f))
610 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
612 if (dpyinfo->mouse_face_deferred_gc
613 || f == dpyinfo->mouse_face_mouse_frame)
615 BLOCK_INPUT;
616 if (dpyinfo->mouse_face_mouse_frame)
617 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
618 dpyinfo->mouse_face_mouse_x,
619 dpyinfo->mouse_face_mouse_y);
620 dpyinfo->mouse_face_deferred_gc = 0;
621 UNBLOCK_INPUT;
627 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
628 arrow bitmaps, or clear the fringes if no bitmaps are required
629 before DESIRED_ROW is made current. The window being updated is
630 found in updated_window. This function It is called from
631 update_window_line only if it is known that there are differences
632 between bitmaps to be drawn between current row and DESIRED_ROW. */
634 static void
635 x_after_update_window_line (desired_row)
636 struct glyph_row *desired_row;
638 struct window *w = updated_window;
639 struct frame *f;
640 int width, height;
642 xassert (w);
644 if (!desired_row->mode_line_p && !w->pseudo_window_p)
646 BLOCK_INPUT;
647 draw_row_fringe_bitmaps (w, desired_row);
648 UNBLOCK_INPUT;
651 /* When a window has disappeared, make sure that no rest of
652 full-width rows stays visible in the internal border. Could
653 check here if updated_window is the leftmost/rightmost window,
654 but I guess it's not worth doing since vertically split windows
655 are almost never used, internal border is rarely set, and the
656 overhead is very small. */
657 if (windows_or_buffers_changed
658 && desired_row->full_width_p
659 && (f = XFRAME (w->frame),
660 width = FRAME_INTERNAL_BORDER_WIDTH (f),
661 width != 0)
662 && (height = desired_row->visible_height,
663 height > 0))
665 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
667 /* Internal border is drawn below the tool bar. */
668 if (WINDOWP (f->tool_bar_window)
669 && w == XWINDOW (f->tool_bar_window))
670 y -= width;
672 BLOCK_INPUT;
673 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
674 0, y, width, height, False);
675 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
676 f->output_data.x->pixel_width - width,
677 y, width, height, False);
678 UNBLOCK_INPUT;
682 static void
683 x_draw_fringe_bitmap (w, row, p)
684 struct window *w;
685 struct glyph_row *row;
686 struct draw_fringe_bitmap_params *p;
688 struct frame *f = XFRAME (WINDOW_FRAME (w));
689 Display *display = FRAME_X_DISPLAY (f);
690 Window window = FRAME_X_WINDOW (f);
691 GC gc = f->output_data.x->normal_gc;
692 struct face *face = p->face;
694 /* Must clip because of partially visible lines. */
695 x_clip_to_row (w, row, gc, 1);
697 if (p->bx >= 0)
699 /* In case the same realized face is used for fringes and
700 for something displayed in the text (e.g. face `region' on
701 mono-displays, the fill style may have been changed to
702 FillSolid in x_draw_glyph_string_background. */
703 if (face->stipple)
704 XSetFillStyle (display, face->gc, FillOpaqueStippled);
705 else
706 XSetForeground (display, face->gc, face->background);
708 XFillRectangle (display, window, face->gc,
709 p->bx, p->by, p->nx, p->ny);
711 if (!face->stipple)
712 XSetForeground (display, face->gc, face->foreground);
715 if (p->which != NO_FRINGE_BITMAP)
717 unsigned char *bits = fringe_bitmaps[p->which].bits + p->dh;
718 Pixmap pixmap;
719 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
721 /* Draw the bitmap. I believe these small pixmaps can be cached
722 by the server. */
723 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
724 face->foreground,
725 face->background, depth);
726 XCopyArea (display, pixmap, window, gc, 0, 0,
727 p->wd, p->h, p->x, p->y);
728 XFreePixmap (display, pixmap);
731 XSetClipMask (display, gc, None);
736 /* This is called when starting Emacs and when restarting after
737 suspend. When starting Emacs, no X window is mapped. And nothing
738 must be done to Emacs's own window if it is suspended (though that
739 rarely happens). */
741 static void
742 XTset_terminal_modes ()
746 /* This is called when exiting or suspending Emacs. Exiting will make
747 the X-windows go away, and suspending requires no action. */
749 static void
750 XTreset_terminal_modes ()
756 /***********************************************************************
757 Display Iterator
758 ***********************************************************************/
760 /* Function prototypes of this page. */
762 static int x_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
765 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
766 is not contained in the font. */
768 static XCharStruct *
769 x_per_char_metric (font, char2b, font_type)
770 XFontStruct *font;
771 XChar2b *char2b;
772 int font_type; /* unused on X */
774 /* The result metric information. */
775 XCharStruct *pcm = NULL;
777 xassert (font && char2b);
779 if (font->per_char != NULL)
781 if (font->min_byte1 == 0 && font->max_byte1 == 0)
783 /* min_char_or_byte2 specifies the linear character index
784 corresponding to the first element of the per_char array,
785 max_char_or_byte2 is the index of the last character. A
786 character with non-zero CHAR2B->byte1 is not in the font.
787 A character with byte2 less than min_char_or_byte2 or
788 greater max_char_or_byte2 is not in the font. */
789 if (char2b->byte1 == 0
790 && char2b->byte2 >= font->min_char_or_byte2
791 && char2b->byte2 <= font->max_char_or_byte2)
792 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
794 else
796 /* If either min_byte1 or max_byte1 are nonzero, both
797 min_char_or_byte2 and max_char_or_byte2 are less than
798 256, and the 2-byte character index values corresponding
799 to the per_char array element N (counting from 0) are:
801 byte1 = N/D + min_byte1
802 byte2 = N\D + min_char_or_byte2
804 where:
806 D = max_char_or_byte2 - min_char_or_byte2 + 1
807 / = integer division
808 \ = integer modulus */
809 if (char2b->byte1 >= font->min_byte1
810 && char2b->byte1 <= font->max_byte1
811 && char2b->byte2 >= font->min_char_or_byte2
812 && char2b->byte2 <= font->max_char_or_byte2)
814 pcm = (font->per_char
815 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
816 * (char2b->byte1 - font->min_byte1))
817 + (char2b->byte2 - font->min_char_or_byte2));
821 else
823 /* If the per_char pointer is null, all glyphs between the first
824 and last character indexes inclusive have the same
825 information, as given by both min_bounds and max_bounds. */
826 if (char2b->byte2 >= font->min_char_or_byte2
827 && char2b->byte2 <= font->max_char_or_byte2)
828 pcm = &font->max_bounds;
831 return ((pcm == NULL
832 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
833 ? NULL : pcm);
837 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
838 the two-byte form of C. Encoding is returned in *CHAR2B. */
840 static int
841 x_encode_char (c, char2b, font_info, two_byte_p)
842 int c;
843 XChar2b *char2b;
844 struct font_info *font_info;
845 int *two_byte_p;
847 int charset = CHAR_CHARSET (c);
848 XFontStruct *font = font_info->font;
850 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
851 This may be either a program in a special encoder language or a
852 fixed encoding. */
853 if (font_info->font_encoder)
855 /* It's a program. */
856 struct ccl_program *ccl = font_info->font_encoder;
858 if (CHARSET_DIMENSION (charset) == 1)
860 ccl->reg[0] = charset;
861 ccl->reg[1] = char2b->byte2;
862 ccl->reg[2] = -1;
864 else
866 ccl->reg[0] = charset;
867 ccl->reg[1] = char2b->byte1;
868 ccl->reg[2] = char2b->byte2;
871 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
873 /* We assume that MSBs are appropriately set/reset by CCL
874 program. */
875 if (font->max_byte1 == 0) /* 1-byte font */
876 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
877 else
878 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
880 else if (font_info->encoding[charset])
882 /* Fixed encoding scheme. See fontset.h for the meaning of the
883 encoding numbers. */
884 int enc = font_info->encoding[charset];
886 if ((enc == 1 || enc == 2)
887 && CHARSET_DIMENSION (charset) == 2)
888 char2b->byte1 |= 0x80;
890 if (enc == 1 || enc == 3)
891 char2b->byte2 |= 0x80;
894 if (two_byte_p)
895 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
897 return FONT_TYPE_UNKNOWN;
902 /***********************************************************************
903 Glyph display
904 ***********************************************************************/
908 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
909 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
910 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
911 int));
912 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
913 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
914 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
915 static void x_draw_glyph_string P_ ((struct glyph_string *));
916 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
917 static void x_set_cursor_gc P_ ((struct glyph_string *));
918 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
919 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
920 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
921 unsigned long *, double, int));
922 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
923 double, int, unsigned long));
924 static void x_setup_relief_colors P_ ((struct glyph_string *));
925 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
926 static void x_draw_image_relief P_ ((struct glyph_string *));
927 static void x_draw_image_foreground P_ ((struct glyph_string *));
928 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
929 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
930 int, int, int));
931 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
932 int, int, int, int, XRectangle *));
933 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
934 int, int, int, XRectangle *));
936 #if GLYPH_DEBUG
937 static void x_check_font P_ ((struct frame *, XFontStruct *));
938 #endif
941 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
942 face. */
944 static void
945 x_set_cursor_gc (s)
946 struct glyph_string *s;
948 if (s->font == FRAME_FONT (s->f)
949 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
950 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
951 && !s->cmp)
952 s->gc = s->f->output_data.x->cursor_gc;
953 else
955 /* Cursor on non-default face: must merge. */
956 XGCValues xgcv;
957 unsigned long mask;
959 xgcv.background = s->f->output_data.x->cursor_pixel;
960 xgcv.foreground = s->face->background;
962 /* If the glyph would be invisible, try a different foreground. */
963 if (xgcv.foreground == xgcv.background)
964 xgcv.foreground = s->face->foreground;
965 if (xgcv.foreground == xgcv.background)
966 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
967 if (xgcv.foreground == xgcv.background)
968 xgcv.foreground = s->face->foreground;
970 /* Make sure the cursor is distinct from text in this face. */
971 if (xgcv.background == s->face->background
972 && xgcv.foreground == s->face->foreground)
974 xgcv.background = s->face->foreground;
975 xgcv.foreground = s->face->background;
978 IF_DEBUG (x_check_font (s->f, s->font));
979 xgcv.font = s->font->fid;
980 xgcv.graphics_exposures = False;
981 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
983 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
984 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
985 mask, &xgcv);
986 else
987 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
988 = XCreateGC (s->display, s->window, mask, &xgcv);
990 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
995 /* Set up S->gc of glyph string S for drawing text in mouse face. */
997 static void
998 x_set_mouse_face_gc (s)
999 struct glyph_string *s;
1001 int face_id;
1002 struct face *face;
1004 /* What face has to be used last for the mouse face? */
1005 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1006 face = FACE_FROM_ID (s->f, face_id);
1007 if (face == NULL)
1008 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1010 if (s->first_glyph->type == CHAR_GLYPH)
1011 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
1012 else
1013 face_id = FACE_FOR_CHAR (s->f, face, 0);
1014 s->face = FACE_FROM_ID (s->f, face_id);
1015 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1017 /* If font in this face is same as S->font, use it. */
1018 if (s->font == s->face->font)
1019 s->gc = s->face->gc;
1020 else
1022 /* Otherwise construct scratch_cursor_gc with values from FACE
1023 but font FONT. */
1024 XGCValues xgcv;
1025 unsigned long mask;
1027 xgcv.background = s->face->background;
1028 xgcv.foreground = s->face->foreground;
1029 IF_DEBUG (x_check_font (s->f, s->font));
1030 xgcv.font = s->font->fid;
1031 xgcv.graphics_exposures = False;
1032 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1034 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1035 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1036 mask, &xgcv);
1037 else
1038 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1039 = XCreateGC (s->display, s->window, mask, &xgcv);
1041 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1044 xassert (s->gc != 0);
1048 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1049 Faces to use in the mode line have already been computed when the
1050 matrix was built, so there isn't much to do, here. */
1052 static INLINE void
1053 x_set_mode_line_face_gc (s)
1054 struct glyph_string *s;
1056 s->gc = s->face->gc;
1060 /* Set S->gc of glyph string S for drawing that glyph string. Set
1061 S->stippled_p to a non-zero value if the face of S has a stipple
1062 pattern. */
1064 static INLINE void
1065 x_set_glyph_string_gc (s)
1066 struct glyph_string *s;
1068 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1070 if (s->hl == DRAW_NORMAL_TEXT)
1072 s->gc = s->face->gc;
1073 s->stippled_p = s->face->stipple != 0;
1075 else if (s->hl == DRAW_INVERSE_VIDEO)
1077 x_set_mode_line_face_gc (s);
1078 s->stippled_p = s->face->stipple != 0;
1080 else if (s->hl == DRAW_CURSOR)
1082 x_set_cursor_gc (s);
1083 s->stippled_p = 0;
1085 else if (s->hl == DRAW_MOUSE_FACE)
1087 x_set_mouse_face_gc (s);
1088 s->stippled_p = s->face->stipple != 0;
1090 else if (s->hl == DRAW_IMAGE_RAISED
1091 || s->hl == DRAW_IMAGE_SUNKEN)
1093 s->gc = s->face->gc;
1094 s->stippled_p = s->face->stipple != 0;
1096 else
1098 s->gc = s->face->gc;
1099 s->stippled_p = s->face->stipple != 0;
1102 /* GC must have been set. */
1103 xassert (s->gc != 0);
1107 /* Set clipping for output of glyph string S. S may be part of a mode
1108 line or menu if we don't have X toolkit support. */
1110 static INLINE void
1111 x_set_glyph_string_clipping (s)
1112 struct glyph_string *s;
1114 XRectangle r;
1115 get_glyph_string_clip_rect (s, &r);
1116 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
1120 /* RIF:
1121 Compute left and right overhang of glyph string S. If S is a glyph
1122 string for a composition, assume overhangs don't exist. */
1124 static void
1125 x_compute_glyph_string_overhangs (s)
1126 struct glyph_string *s;
1128 if (s->cmp == NULL
1129 && s->first_glyph->type == CHAR_GLYPH)
1131 XCharStruct cs;
1132 int direction, font_ascent, font_descent;
1133 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1134 &font_ascent, &font_descent, &cs);
1135 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1136 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1141 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1143 static INLINE void
1144 x_clear_glyph_string_rect (s, x, y, w, h)
1145 struct glyph_string *s;
1146 int x, y, w, h;
1148 XGCValues xgcv;
1149 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1150 XSetForeground (s->display, s->gc, xgcv.background);
1151 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1152 XSetForeground (s->display, s->gc, xgcv.foreground);
1156 /* Draw the background of glyph_string S. If S->background_filled_p
1157 is non-zero don't draw it. FORCE_P non-zero means draw the
1158 background even if it wouldn't be drawn normally. This is used
1159 when a string preceding S draws into the background of S, or S
1160 contains the first component of a composition. */
1162 static void
1163 x_draw_glyph_string_background (s, force_p)
1164 struct glyph_string *s;
1165 int force_p;
1167 /* Nothing to do if background has already been drawn or if it
1168 shouldn't be drawn in the first place. */
1169 if (!s->background_filled_p)
1171 int box_line_width = max (s->face->box_line_width, 0);
1173 if (s->stippled_p)
1175 /* Fill background with a stipple pattern. */
1176 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1177 XFillRectangle (s->display, s->window, s->gc, s->x,
1178 s->y + box_line_width,
1179 s->background_width,
1180 s->height - 2 * box_line_width);
1181 XSetFillStyle (s->display, s->gc, FillSolid);
1182 s->background_filled_p = 1;
1184 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1185 || s->font_not_found_p
1186 || s->extends_to_end_of_line_p
1187 || force_p)
1189 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1190 s->background_width,
1191 s->height - 2 * box_line_width);
1192 s->background_filled_p = 1;
1198 /* Draw the foreground of glyph string S. */
1200 static void
1201 x_draw_glyph_string_foreground (s)
1202 struct glyph_string *s;
1204 int i, x;
1206 /* If first glyph of S has a left box line, start drawing the text
1207 of S to the right of that box line. */
1208 if (s->face->box != FACE_NO_BOX
1209 && s->first_glyph->left_box_line_p)
1210 x = s->x + abs (s->face->box_line_width);
1211 else
1212 x = s->x;
1214 /* Draw characters of S as rectangles if S's font could not be
1215 loaded. */
1216 if (s->font_not_found_p)
1218 for (i = 0; i < s->nchars; ++i)
1220 struct glyph *g = s->first_glyph + i;
1221 XDrawRectangle (s->display, s->window,
1222 s->gc, x, s->y, g->pixel_width - 1,
1223 s->height - 1);
1224 x += g->pixel_width;
1227 else
1229 char *char1b = (char *) s->char2b;
1230 int boff = s->font_info->baseline_offset;
1232 if (s->font_info->vertical_centering)
1233 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1235 /* If we can use 8-bit functions, condense S->char2b. */
1236 if (!s->two_byte_p)
1237 for (i = 0; i < s->nchars; ++i)
1238 char1b[i] = s->char2b[i].byte2;
1240 /* Draw text with XDrawString if background has already been
1241 filled. Otherwise, use XDrawImageString. (Note that
1242 XDrawImageString is usually faster than XDrawString.) Always
1243 use XDrawImageString when drawing the cursor so that there is
1244 no chance that characters under a box cursor are invisible. */
1245 if (s->for_overlaps_p
1246 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1248 /* Draw characters with 16-bit or 8-bit functions. */
1249 if (s->two_byte_p)
1250 XDrawString16 (s->display, s->window, s->gc, x,
1251 s->ybase - boff, s->char2b, s->nchars);
1252 else
1253 XDrawString (s->display, s->window, s->gc, x,
1254 s->ybase - boff, char1b, s->nchars);
1256 else
1258 if (s->two_byte_p)
1259 XDrawImageString16 (s->display, s->window, s->gc, x,
1260 s->ybase - boff, s->char2b, s->nchars);
1261 else
1262 XDrawImageString (s->display, s->window, s->gc, x,
1263 s->ybase - boff, char1b, s->nchars);
1266 if (s->face->overstrike)
1268 /* For overstriking (to simulate bold-face), draw the
1269 characters again shifted to the right by one pixel. */
1270 if (s->two_byte_p)
1271 XDrawString16 (s->display, s->window, s->gc, x + 1,
1272 s->ybase - boff, s->char2b, s->nchars);
1273 else
1274 XDrawString (s->display, s->window, s->gc, x + 1,
1275 s->ybase - boff, char1b, s->nchars);
1280 /* Draw the foreground of composite glyph string S. */
1282 static void
1283 x_draw_composite_glyph_string_foreground (s)
1284 struct glyph_string *s;
1286 int i, x;
1288 /* If first glyph of S has a left box line, start drawing the text
1289 of S to the right of that box line. */
1290 if (s->face->box != FACE_NO_BOX
1291 && s->first_glyph->left_box_line_p)
1292 x = s->x + abs (s->face->box_line_width);
1293 else
1294 x = s->x;
1296 /* S is a glyph string for a composition. S->gidx is the index of
1297 the first character drawn for glyphs of this composition.
1298 S->gidx == 0 means we are drawing the very first character of
1299 this composition. */
1301 /* Draw a rectangle for the composition if the font for the very
1302 first character of the composition could not be loaded. */
1303 if (s->font_not_found_p)
1305 if (s->gidx == 0)
1306 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1307 s->width - 1, s->height - 1);
1309 else
1311 for (i = 0; i < s->nchars; i++, ++s->gidx)
1313 XDrawString16 (s->display, s->window, s->gc,
1314 x + s->cmp->offsets[s->gidx * 2],
1315 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1316 s->char2b + i, 1);
1317 if (s->face->overstrike)
1318 XDrawString16 (s->display, s->window, s->gc,
1319 x + s->cmp->offsets[s->gidx * 2] + 1,
1320 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1321 s->char2b + i, 1);
1327 #ifdef USE_X_TOOLKIT
1329 static struct frame *x_frame_of_widget P_ ((Widget));
1330 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1331 XrmValue *, XrmValue *, XtPointer *));
1332 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1333 XrmValue *, Cardinal *));
1336 /* Return the frame on which widget WIDGET is used.. Abort if frame
1337 cannot be determined. */
1339 static struct frame *
1340 x_frame_of_widget (widget)
1341 Widget widget;
1343 struct x_display_info *dpyinfo;
1344 Lisp_Object tail;
1345 struct frame *f;
1347 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1349 /* Find the top-level shell of the widget. Note that this function
1350 can be called when the widget is not yet realized, so XtWindow
1351 (widget) == 0. That's the reason we can't simply use
1352 x_any_window_to_frame. */
1353 while (!XtIsTopLevelShell (widget))
1354 widget = XtParent (widget);
1356 /* Look for a frame with that top-level widget. Allocate the color
1357 on that frame to get the right gamma correction value. */
1358 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1359 if (GC_FRAMEP (XCAR (tail))
1360 && (f = XFRAME (XCAR (tail)),
1361 (f->output_data.nothing != 1
1362 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1363 && f->output_data.x->widget == widget)
1364 return f;
1366 abort ();
1370 /* Allocate the color COLOR->pixel on the screen and display of
1371 widget WIDGET in colormap CMAP. If an exact match cannot be
1372 allocated, try the nearest color available. Value is non-zero
1373 if successful. This is called from lwlib. */
1376 x_alloc_nearest_color_for_widget (widget, cmap, color)
1377 Widget widget;
1378 Colormap cmap;
1379 XColor *color;
1381 struct frame *f = x_frame_of_widget (widget);
1382 return x_alloc_nearest_color (f, cmap, color);
1386 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1387 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1388 If this produces the same color as PIXEL, try a color where all RGB
1389 values have DELTA added. Return the allocated color in *PIXEL.
1390 DISPLAY is the X display, CMAP is the colormap to operate on.
1391 Value is non-zero if successful. */
1394 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1395 Widget widget;
1396 Display *display;
1397 Colormap cmap;
1398 unsigned long *pixel;
1399 double factor;
1400 int delta;
1402 struct frame *f = x_frame_of_widget (widget);
1403 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1407 /* Structure specifying which arguments should be passed by Xt to
1408 cvt_string_to_pixel. We want the widget's screen and colormap. */
1410 static XtConvertArgRec cvt_string_to_pixel_args[] =
1412 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1413 sizeof (Screen *)},
1414 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1415 sizeof (Colormap)}
1419 /* The address of this variable is returned by
1420 cvt_string_to_pixel. */
1422 static Pixel cvt_string_to_pixel_value;
1425 /* Convert a color name to a pixel color.
1427 DPY is the display we are working on.
1429 ARGS is an array of *NARGS XrmValue structures holding additional
1430 information about the widget for which the conversion takes place.
1431 The contents of this array are determined by the specification
1432 in cvt_string_to_pixel_args.
1434 FROM is a pointer to an XrmValue which points to the color name to
1435 convert. TO is an XrmValue in which to return the pixel color.
1437 CLOSURE_RET is a pointer to user-data, in which we record if
1438 we allocated the color or not.
1440 Value is True if successful, False otherwise. */
1442 static Boolean
1443 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1444 Display *dpy;
1445 XrmValue *args;
1446 Cardinal *nargs;
1447 XrmValue *from, *to;
1448 XtPointer *closure_ret;
1450 Screen *screen;
1451 Colormap cmap;
1452 Pixel pixel;
1453 String color_name;
1454 XColor color;
1456 if (*nargs != 2)
1458 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1459 "wrongParameters", "cvt_string_to_pixel",
1460 "XtToolkitError",
1461 "Screen and colormap args required", NULL, NULL);
1462 return False;
1465 screen = *(Screen **) args[0].addr;
1466 cmap = *(Colormap *) args[1].addr;
1467 color_name = (String) from->addr;
1469 if (strcmp (color_name, XtDefaultBackground) == 0)
1471 *closure_ret = (XtPointer) False;
1472 pixel = WhitePixelOfScreen (screen);
1474 else if (strcmp (color_name, XtDefaultForeground) == 0)
1476 *closure_ret = (XtPointer) False;
1477 pixel = BlackPixelOfScreen (screen);
1479 else if (XParseColor (dpy, cmap, color_name, &color)
1480 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1482 pixel = color.pixel;
1483 *closure_ret = (XtPointer) True;
1485 else
1487 String params[1];
1488 Cardinal nparams = 1;
1490 params[0] = color_name;
1491 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1492 "badValue", "cvt_string_to_pixel",
1493 "XtToolkitError", "Invalid color `%s'",
1494 params, &nparams);
1495 return False;
1498 if (to->addr != NULL)
1500 if (to->size < sizeof (Pixel))
1502 to->size = sizeof (Pixel);
1503 return False;
1506 *(Pixel *) to->addr = pixel;
1508 else
1510 cvt_string_to_pixel_value = pixel;
1511 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1514 to->size = sizeof (Pixel);
1515 return True;
1519 /* Free a pixel color which was previously allocated via
1520 cvt_string_to_pixel. This is registered as the destructor
1521 for this type of resource via XtSetTypeConverter.
1523 APP is the application context in which we work.
1525 TO is a pointer to an XrmValue holding the color to free.
1526 CLOSURE is the value we stored in CLOSURE_RET for this color
1527 in cvt_string_to_pixel.
1529 ARGS and NARGS are like for cvt_string_to_pixel. */
1531 static void
1532 cvt_pixel_dtor (app, to, closure, args, nargs)
1533 XtAppContext app;
1534 XrmValuePtr to;
1535 XtPointer closure;
1536 XrmValuePtr args;
1537 Cardinal *nargs;
1539 if (*nargs != 2)
1541 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1542 "XtToolkitError",
1543 "Screen and colormap arguments required",
1544 NULL, NULL);
1546 else if (closure != NULL)
1548 /* We did allocate the pixel, so free it. */
1549 Screen *screen = *(Screen **) args[0].addr;
1550 Colormap cmap = *(Colormap *) args[1].addr;
1551 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1552 (Pixel *) to->addr, 1);
1557 #endif /* USE_X_TOOLKIT */
1560 /* Value is an array of XColor structures for the contents of the
1561 color map of display DPY. Set *NCELLS to the size of the array.
1562 Note that this probably shouldn't be called for large color maps,
1563 say a 24-bit TrueColor map. */
1565 static const XColor *
1566 x_color_cells (dpy, ncells)
1567 Display *dpy;
1568 int *ncells;
1570 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1572 if (dpyinfo->color_cells == NULL)
1574 Screen *screen = dpyinfo->screen;
1575 int i;
1577 dpyinfo->ncolor_cells
1578 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1579 dpyinfo->color_cells
1580 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1581 * sizeof *dpyinfo->color_cells);
1583 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1584 dpyinfo->color_cells[i].pixel = i;
1586 XQueryColors (dpy, dpyinfo->cmap,
1587 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1590 *ncells = dpyinfo->ncolor_cells;
1591 return dpyinfo->color_cells;
1595 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1596 colors in COLORS. Use cached information, if available. */
1598 void
1599 x_query_colors (f, colors, ncolors)
1600 struct frame *f;
1601 XColor *colors;
1602 int ncolors;
1604 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1606 if (dpyinfo->color_cells)
1608 int i;
1609 for (i = 0; i < ncolors; ++i)
1611 unsigned long pixel = colors[i].pixel;
1612 xassert (pixel < dpyinfo->ncolor_cells);
1613 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1614 colors[i] = dpyinfo->color_cells[pixel];
1617 else
1618 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1622 /* On frame F, translate pixel color to RGB values for the color in
1623 COLOR. Use cached information, if available. */
1625 void
1626 x_query_color (f, color)
1627 struct frame *f;
1628 XColor *color;
1630 x_query_colors (f, color, 1);
1634 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1635 exact match can't be allocated, try the nearest color available.
1636 Value is non-zero if successful. Set *COLOR to the color
1637 allocated. */
1639 static int
1640 x_alloc_nearest_color_1 (dpy, cmap, color)
1641 Display *dpy;
1642 Colormap cmap;
1643 XColor *color;
1645 int rc;
1647 rc = XAllocColor (dpy, cmap, color);
1648 if (rc == 0)
1650 /* If we got to this point, the colormap is full, so we're going
1651 to try to get the next closest color. The algorithm used is
1652 a least-squares matching, which is what X uses for closest
1653 color matching with StaticColor visuals. */
1654 int nearest, i;
1655 unsigned long nearest_delta = ~0;
1656 int ncells;
1657 const XColor *cells = x_color_cells (dpy, &ncells);
1659 for (nearest = i = 0; i < ncells; ++i)
1661 long dred = (color->red >> 8) - (cells[i].red >> 8);
1662 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1663 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1664 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1666 if (delta < nearest_delta)
1668 nearest = i;
1669 nearest_delta = delta;
1673 color->red = cells[nearest].red;
1674 color->green = cells[nearest].green;
1675 color->blue = cells[nearest].blue;
1676 rc = XAllocColor (dpy, cmap, color);
1678 else
1680 /* If allocation succeeded, and the allocated pixel color is not
1681 equal to a cached pixel color recorded earlier, there was a
1682 change in the colormap, so clear the color cache. */
1683 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1684 XColor *cached_color;
1686 if (dpyinfo->color_cells
1687 && (cached_color = &dpyinfo->color_cells[color->pixel],
1688 (cached_color->red != color->red
1689 || cached_color->blue != color->blue
1690 || cached_color->green != color->green)))
1692 xfree (dpyinfo->color_cells);
1693 dpyinfo->color_cells = NULL;
1694 dpyinfo->ncolor_cells = 0;
1698 #ifdef DEBUG_X_COLORS
1699 if (rc)
1700 register_color (color->pixel);
1701 #endif /* DEBUG_X_COLORS */
1703 return rc;
1707 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1708 exact match can't be allocated, try the nearest color available.
1709 Value is non-zero if successful. Set *COLOR to the color
1710 allocated. */
1713 x_alloc_nearest_color (f, cmap, color)
1714 struct frame *f;
1715 Colormap cmap;
1716 XColor *color;
1718 gamma_correct (f, color);
1719 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1723 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1724 It's necessary to do this instead of just using PIXEL directly to
1725 get color reference counts right. */
1727 unsigned long
1728 x_copy_color (f, pixel)
1729 struct frame *f;
1730 unsigned long pixel;
1732 XColor color;
1734 color.pixel = pixel;
1735 BLOCK_INPUT;
1736 x_query_color (f, &color);
1737 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1738 UNBLOCK_INPUT;
1739 #ifdef DEBUG_X_COLORS
1740 register_color (pixel);
1741 #endif
1742 return color.pixel;
1746 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1747 It's necessary to do this instead of just using PIXEL directly to
1748 get color reference counts right. */
1750 unsigned long
1751 x_copy_dpy_color (dpy, cmap, pixel)
1752 Display *dpy;
1753 Colormap cmap;
1754 unsigned long pixel;
1756 XColor color;
1758 color.pixel = pixel;
1759 BLOCK_INPUT;
1760 XQueryColor (dpy, cmap, &color);
1761 XAllocColor (dpy, cmap, &color);
1762 UNBLOCK_INPUT;
1763 #ifdef DEBUG_X_COLORS
1764 register_color (pixel);
1765 #endif
1766 return color.pixel;
1770 /* Brightness beyond which a color won't have its highlight brightness
1771 boosted.
1773 Nominally, highlight colors for `3d' faces are calculated by
1774 brightening an object's color by a constant scale factor, but this
1775 doesn't yield good results for dark colors, so for colors who's
1776 brightness is less than this value (on a scale of 0-65535) have an
1777 use an additional additive factor.
1779 The value here is set so that the default menu-bar/mode-line color
1780 (grey75) will not have its highlights changed at all. */
1781 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1784 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1785 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1786 If this produces the same color as PIXEL, try a color where all RGB
1787 values have DELTA added. Return the allocated color in *PIXEL.
1788 DISPLAY is the X display, CMAP is the colormap to operate on.
1789 Value is non-zero if successful. */
1791 static int
1792 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1793 struct frame *f;
1794 Display *display;
1795 Colormap cmap;
1796 unsigned long *pixel;
1797 double factor;
1798 int delta;
1800 XColor color, new;
1801 long bright;
1802 int success_p;
1804 /* Get RGB color values. */
1805 color.pixel = *pixel;
1806 x_query_color (f, &color);
1808 /* Change RGB values by specified FACTOR. Avoid overflow! */
1809 xassert (factor >= 0);
1810 new.red = min (0xffff, factor * color.red);
1811 new.green = min (0xffff, factor * color.green);
1812 new.blue = min (0xffff, factor * color.blue);
1814 /* Calculate brightness of COLOR. */
1815 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1817 /* We only boost colors that are darker than
1818 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1819 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1820 /* Make an additive adjustment to NEW, because it's dark enough so
1821 that scaling by FACTOR alone isn't enough. */
1823 /* How far below the limit this color is (0 - 1, 1 being darker). */
1824 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1825 /* The additive adjustment. */
1826 int min_delta = delta * dimness * factor / 2;
1828 if (factor < 1)
1830 new.red = max (0, new.red - min_delta);
1831 new.green = max (0, new.green - min_delta);
1832 new.blue = max (0, new.blue - min_delta);
1834 else
1836 new.red = min (0xffff, min_delta + new.red);
1837 new.green = min (0xffff, min_delta + new.green);
1838 new.blue = min (0xffff, min_delta + new.blue);
1842 /* Try to allocate the color. */
1843 success_p = x_alloc_nearest_color (f, cmap, &new);
1844 if (success_p)
1846 if (new.pixel == *pixel)
1848 /* If we end up with the same color as before, try adding
1849 delta to the RGB values. */
1850 x_free_colors (f, &new.pixel, 1);
1852 new.red = min (0xffff, delta + color.red);
1853 new.green = min (0xffff, delta + color.green);
1854 new.blue = min (0xffff, delta + color.blue);
1855 success_p = x_alloc_nearest_color (f, cmap, &new);
1857 else
1858 success_p = 1;
1859 *pixel = new.pixel;
1862 return success_p;
1866 /* Set up the foreground color for drawing relief lines of glyph
1867 string S. RELIEF is a pointer to a struct relief containing the GC
1868 with which lines will be drawn. Use a color that is FACTOR or
1869 DELTA lighter or darker than the relief's background which is found
1870 in S->f->output_data.x->relief_background. If such a color cannot
1871 be allocated, use DEFAULT_PIXEL, instead. */
1873 static void
1874 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1875 struct frame *f;
1876 struct relief *relief;
1877 double factor;
1878 int delta;
1879 unsigned long default_pixel;
1881 XGCValues xgcv;
1882 struct x_output *di = f->output_data.x;
1883 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1884 unsigned long pixel;
1885 unsigned long background = di->relief_background;
1886 Colormap cmap = FRAME_X_COLORMAP (f);
1887 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1888 Display *dpy = FRAME_X_DISPLAY (f);
1890 xgcv.graphics_exposures = False;
1891 xgcv.line_width = 1;
1893 /* Free previously allocated color. The color cell will be reused
1894 when it has been freed as many times as it was allocated, so this
1895 doesn't affect faces using the same colors. */
1896 if (relief->gc
1897 && relief->allocated_p)
1899 x_free_colors (f, &relief->pixel, 1);
1900 relief->allocated_p = 0;
1903 /* Allocate new color. */
1904 xgcv.foreground = default_pixel;
1905 pixel = background;
1906 if (dpyinfo->n_planes != 1
1907 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1909 relief->allocated_p = 1;
1910 xgcv.foreground = relief->pixel = pixel;
1913 if (relief->gc == 0)
1915 xgcv.stipple = dpyinfo->gray;
1916 mask |= GCStipple;
1917 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1919 else
1920 XChangeGC (dpy, relief->gc, mask, &xgcv);
1924 /* Set up colors for the relief lines around glyph string S. */
1926 static void
1927 x_setup_relief_colors (s)
1928 struct glyph_string *s;
1930 struct x_output *di = s->f->output_data.x;
1931 unsigned long color;
1933 if (s->face->use_box_color_for_shadows_p)
1934 color = s->face->box_color;
1935 else if (s->first_glyph->type == IMAGE_GLYPH
1936 && s->img->pixmap
1937 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1938 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1939 else
1941 XGCValues xgcv;
1943 /* Get the background color of the face. */
1944 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
1945 color = xgcv.background;
1948 if (di->white_relief.gc == 0
1949 || color != di->relief_background)
1951 di->relief_background = color;
1952 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1953 WHITE_PIX_DEFAULT (s->f));
1954 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1955 BLACK_PIX_DEFAULT (s->f));
1960 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1961 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1962 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1963 relief. LEFT_P non-zero means draw a relief on the left side of
1964 the rectangle. RIGHT_P non-zero means draw a relief on the right
1965 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1966 when drawing. */
1968 static void
1969 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
1970 raised_p, left_p, right_p, clip_rect)
1971 struct frame *f;
1972 int left_x, top_y, right_x, bottom_y, width, left_p, right_p, raised_p;
1973 XRectangle *clip_rect;
1975 Display *dpy = FRAME_X_DISPLAY (f);
1976 Window window = FRAME_X_WINDOW (f);
1977 int i;
1978 GC gc;
1980 if (raised_p)
1981 gc = f->output_data.x->white_relief.gc;
1982 else
1983 gc = f->output_data.x->black_relief.gc;
1984 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1986 /* Top. */
1987 for (i = 0; i < width; ++i)
1988 XDrawLine (dpy, window, gc,
1989 left_x + i * left_p, top_y + i,
1990 right_x + 1 - i * right_p, top_y + i);
1992 /* Left. */
1993 if (left_p)
1994 for (i = 0; i < width; ++i)
1995 XDrawLine (dpy, window, gc,
1996 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
1998 XSetClipMask (dpy, gc, None);
1999 if (raised_p)
2000 gc = f->output_data.x->black_relief.gc;
2001 else
2002 gc = f->output_data.x->white_relief.gc;
2003 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2005 /* Bottom. */
2006 for (i = 0; i < width; ++i)
2007 XDrawLine (dpy, window, gc,
2008 left_x + i * left_p, bottom_y - i,
2009 right_x + 1 - i * right_p, bottom_y - i);
2011 /* Right. */
2012 if (right_p)
2013 for (i = 0; i < width; ++i)
2014 XDrawLine (dpy, window, gc,
2015 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2017 XSetClipMask (dpy, gc, None);
2021 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2022 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2023 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2024 left side of the rectangle. RIGHT_P non-zero means draw a line
2025 on the right side of the rectangle. CLIP_RECT is the clipping
2026 rectangle to use when drawing. */
2028 static void
2029 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2030 left_p, right_p, clip_rect)
2031 struct glyph_string *s;
2032 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2033 XRectangle *clip_rect;
2035 XGCValues xgcv;
2037 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2038 XSetForeground (s->display, s->gc, s->face->box_color);
2039 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2041 /* Top. */
2042 XFillRectangle (s->display, s->window, s->gc,
2043 left_x, top_y, right_x - left_x + 1, width);
2045 /* Left. */
2046 if (left_p)
2047 XFillRectangle (s->display, s->window, s->gc,
2048 left_x, top_y, width, bottom_y - top_y + 1);
2050 /* Bottom. */
2051 XFillRectangle (s->display, s->window, s->gc,
2052 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2054 /* Right. */
2055 if (right_p)
2056 XFillRectangle (s->display, s->window, s->gc,
2057 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2059 XSetForeground (s->display, s->gc, xgcv.foreground);
2060 XSetClipMask (s->display, s->gc, None);
2064 /* Draw a box around glyph string S. */
2066 static void
2067 x_draw_glyph_string_box (s)
2068 struct glyph_string *s;
2070 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2071 int left_p, right_p;
2072 struct glyph *last_glyph;
2073 XRectangle clip_rect;
2075 last_x = window_box_right (s->w, s->area);
2076 if (s->row->full_width_p
2077 && !s->w->pseudo_window_p)
2079 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
2080 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
2081 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
2084 /* The glyph that may have a right box line. */
2085 last_glyph = (s->cmp || s->img
2086 ? s->first_glyph
2087 : s->first_glyph + s->nchars - 1);
2089 width = abs (s->face->box_line_width);
2090 raised_p = s->face->box == FACE_RAISED_BOX;
2091 left_x = s->x;
2092 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2093 ? last_x - 1
2094 : min (last_x, s->x + s->background_width) - 1);
2095 top_y = s->y;
2096 bottom_y = top_y + s->height - 1;
2098 left_p = (s->first_glyph->left_box_line_p
2099 || (s->hl == DRAW_MOUSE_FACE
2100 && (s->prev == NULL
2101 || s->prev->hl != s->hl)));
2102 right_p = (last_glyph->right_box_line_p
2103 || (s->hl == DRAW_MOUSE_FACE
2104 && (s->next == NULL
2105 || s->next->hl != s->hl)));
2107 get_glyph_string_clip_rect (s, &clip_rect);
2109 if (s->face->box == FACE_SIMPLE_BOX)
2110 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2111 left_p, right_p, &clip_rect);
2112 else
2114 x_setup_relief_colors (s);
2115 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2116 width, raised_p, left_p, right_p, &clip_rect);
2121 /* Draw foreground of image glyph string S. */
2123 static void
2124 x_draw_image_foreground (s)
2125 struct glyph_string *s;
2127 int x;
2128 int y = s->ybase - image_ascent (s->img, s->face);
2130 /* If first glyph of S has a left box line, start drawing it to the
2131 right of that line. */
2132 if (s->face->box != FACE_NO_BOX
2133 && s->first_glyph->left_box_line_p)
2134 x = s->x + abs (s->face->box_line_width);
2135 else
2136 x = s->x;
2138 /* If there is a margin around the image, adjust x- and y-position
2139 by that margin. */
2140 x += s->img->hmargin;
2141 y += s->img->vmargin;
2143 if (s->img->pixmap)
2145 if (s->img->mask)
2147 /* We can't set both a clip mask and use XSetClipRectangles
2148 because the latter also sets a clip mask. We also can't
2149 trust on the shape extension to be available
2150 (XShapeCombineRegion). So, compute the rectangle to draw
2151 manually. */
2152 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2153 | GCFunction);
2154 XGCValues xgcv;
2155 XRectangle clip_rect, image_rect, r;
2157 xgcv.clip_mask = s->img->mask;
2158 xgcv.clip_x_origin = x;
2159 xgcv.clip_y_origin = y;
2160 xgcv.function = GXcopy;
2161 XChangeGC (s->display, s->gc, mask, &xgcv);
2163 get_glyph_string_clip_rect (s, &clip_rect);
2164 image_rect.x = x;
2165 image_rect.y = y;
2166 image_rect.width = s->img->width;
2167 image_rect.height = s->img->height;
2168 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2169 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2170 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
2172 else
2174 XRectangle clip_rect, image_rect, r;
2176 get_glyph_string_clip_rect (s, &clip_rect);
2177 image_rect.x = x;
2178 image_rect.y = y;
2179 image_rect.width = s->img->width;
2180 image_rect.height = s->img->height;
2181 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2182 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2183 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
2185 /* When the image has a mask, we can expect that at
2186 least part of a mouse highlight or a block cursor will
2187 be visible. If the image doesn't have a mask, make
2188 a block cursor visible by drawing a rectangle around
2189 the image. I believe it's looking better if we do
2190 nothing here for mouse-face. */
2191 if (s->hl == DRAW_CURSOR)
2193 int r = s->img->relief;
2194 if (r < 0) r = -r;
2195 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2196 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
2200 else
2201 /* Draw a rectangle if image could not be loaded. */
2202 XDrawRectangle (s->display, s->window, s->gc, x, y,
2203 s->img->width - 1, s->img->height - 1);
2207 /* Draw a relief around the image glyph string S. */
2209 static void
2210 x_draw_image_relief (s)
2211 struct glyph_string *s;
2213 int x0, y0, x1, y1, thick, raised_p;
2214 XRectangle r;
2215 int x;
2216 int y = s->ybase - image_ascent (s->img, s->face);
2218 /* If first glyph of S has a left box line, start drawing it to the
2219 right of that line. */
2220 if (s->face->box != FACE_NO_BOX
2221 && s->first_glyph->left_box_line_p)
2222 x = s->x + abs (s->face->box_line_width);
2223 else
2224 x = s->x;
2226 /* If there is a margin around the image, adjust x- and y-position
2227 by that margin. */
2228 x += s->img->hmargin;
2229 y += s->img->vmargin;
2231 if (s->hl == DRAW_IMAGE_SUNKEN
2232 || s->hl == DRAW_IMAGE_RAISED)
2234 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2235 raised_p = s->hl == DRAW_IMAGE_RAISED;
2237 else
2239 thick = abs (s->img->relief);
2240 raised_p = s->img->relief > 0;
2243 x0 = x - thick;
2244 y0 = y - thick;
2245 x1 = x + s->img->width + thick - 1;
2246 y1 = y + s->img->height + thick - 1;
2248 x_setup_relief_colors (s);
2249 get_glyph_string_clip_rect (s, &r);
2250 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
2254 /* Draw the foreground of image glyph string S to PIXMAP. */
2256 static void
2257 x_draw_image_foreground_1 (s, pixmap)
2258 struct glyph_string *s;
2259 Pixmap pixmap;
2261 int x;
2262 int y = s->ybase - s->y - image_ascent (s->img, s->face);
2264 /* If first glyph of S has a left box line, start drawing it to the
2265 right of that line. */
2266 if (s->face->box != FACE_NO_BOX
2267 && s->first_glyph->left_box_line_p)
2268 x = abs (s->face->box_line_width);
2269 else
2270 x = 0;
2272 /* If there is a margin around the image, adjust x- and y-position
2273 by that margin. */
2274 x += s->img->hmargin;
2275 y += s->img->vmargin;
2277 if (s->img->pixmap)
2279 if (s->img->mask)
2281 /* We can't set both a clip mask and use XSetClipRectangles
2282 because the latter also sets a clip mask. We also can't
2283 trust on the shape extension to be available
2284 (XShapeCombineRegion). So, compute the rectangle to draw
2285 manually. */
2286 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2287 | GCFunction);
2288 XGCValues xgcv;
2290 xgcv.clip_mask = s->img->mask;
2291 xgcv.clip_x_origin = x;
2292 xgcv.clip_y_origin = y;
2293 xgcv.function = GXcopy;
2294 XChangeGC (s->display, s->gc, mask, &xgcv);
2296 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2297 0, 0, s->img->width, s->img->height, x, y);
2298 XSetClipMask (s->display, s->gc, None);
2300 else
2302 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2303 0, 0, s->img->width, s->img->height, x, y);
2305 /* When the image has a mask, we can expect that at
2306 least part of a mouse highlight or a block cursor will
2307 be visible. If the image doesn't have a mask, make
2308 a block cursor visible by drawing a rectangle around
2309 the image. I believe it's looking better if we do
2310 nothing here for mouse-face. */
2311 if (s->hl == DRAW_CURSOR)
2313 int r = s->img->relief;
2314 if (r < 0) r = -r;
2315 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2316 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
2320 else
2321 /* Draw a rectangle if image could not be loaded. */
2322 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2323 s->img->width - 1, s->img->height - 1);
2327 /* Draw part of the background of glyph string S. X, Y, W, and H
2328 give the rectangle to draw. */
2330 static void
2331 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2332 struct glyph_string *s;
2333 int x, y, w, h;
2335 if (s->stippled_p)
2337 /* Fill background with a stipple pattern. */
2338 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2339 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2340 XSetFillStyle (s->display, s->gc, FillSolid);
2342 else
2343 x_clear_glyph_string_rect (s, x, y, w, h);
2347 /* Draw image glyph string S.
2349 s->y
2350 s->x +-------------------------
2351 | s->face->box
2353 | +-------------------------
2354 | | s->img->margin
2356 | | +-------------------
2357 | | | the image
2361 static void
2362 x_draw_image_glyph_string (s)
2363 struct glyph_string *s;
2365 int x, y;
2366 int box_line_hwidth = abs (s->face->box_line_width);
2367 int box_line_vwidth = max (s->face->box_line_width, 0);
2368 int height;
2369 Pixmap pixmap = None;
2371 height = s->height - 2 * box_line_vwidth;
2374 /* Fill background with face under the image. Do it only if row is
2375 taller than image or if image has a clip mask to reduce
2376 flickering. */
2377 s->stippled_p = s->face->stipple != 0;
2378 if (height > s->img->height
2379 || s->img->hmargin
2380 || s->img->vmargin
2381 || s->img->mask
2382 || s->img->pixmap == 0
2383 || s->width != s->background_width)
2385 if (box_line_hwidth && s->first_glyph->left_box_line_p)
2386 x = s->x + box_line_hwidth;
2387 else
2388 x = s->x;
2390 y = s->y + box_line_vwidth;
2392 if (s->img->mask)
2394 /* Create a pixmap as large as the glyph string. Fill it
2395 with the background color. Copy the image to it, using
2396 its mask. Copy the temporary pixmap to the display. */
2397 Screen *screen = FRAME_X_SCREEN (s->f);
2398 int depth = DefaultDepthOfScreen (screen);
2400 /* Create a pixmap as large as the glyph string. */
2401 pixmap = XCreatePixmap (s->display, s->window,
2402 s->background_width,
2403 s->height, depth);
2405 /* Don't clip in the following because we're working on the
2406 pixmap. */
2407 XSetClipMask (s->display, s->gc, None);
2409 /* Fill the pixmap with the background color/stipple. */
2410 if (s->stippled_p)
2412 /* Fill background with a stipple pattern. */
2413 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2414 XFillRectangle (s->display, pixmap, s->gc,
2415 0, 0, s->background_width, s->height);
2416 XSetFillStyle (s->display, s->gc, FillSolid);
2418 else
2420 XGCValues xgcv;
2421 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2422 &xgcv);
2423 XSetForeground (s->display, s->gc, xgcv.background);
2424 XFillRectangle (s->display, pixmap, s->gc,
2425 0, 0, s->background_width, s->height);
2426 XSetForeground (s->display, s->gc, xgcv.foreground);
2429 else
2430 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2432 s->background_filled_p = 1;
2435 /* Draw the foreground. */
2436 if (pixmap != None)
2438 x_draw_image_foreground_1 (s, pixmap);
2439 x_set_glyph_string_clipping (s);
2440 XCopyArea (s->display, pixmap, s->window, s->gc,
2441 0, 0, s->background_width, s->height, s->x, s->y);
2442 XFreePixmap (s->display, pixmap);
2444 else
2445 x_draw_image_foreground (s);
2447 /* If we must draw a relief around the image, do it. */
2448 if (s->img->relief
2449 || s->hl == DRAW_IMAGE_RAISED
2450 || s->hl == DRAW_IMAGE_SUNKEN)
2451 x_draw_image_relief (s);
2455 /* Draw stretch glyph string S. */
2457 static void
2458 x_draw_stretch_glyph_string (s)
2459 struct glyph_string *s;
2461 xassert (s->first_glyph->type == STRETCH_GLYPH);
2462 s->stippled_p = s->face->stipple != 0;
2464 if (s->hl == DRAW_CURSOR
2465 && !x_stretch_cursor_p)
2467 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2468 as wide as the stretch glyph. */
2469 int width = min (CANON_X_UNIT (s->f), s->background_width);
2471 /* Draw cursor. */
2472 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
2474 /* Clear rest using the GC of the original non-cursor face. */
2475 if (width < s->background_width)
2477 int x = s->x + width, y = s->y;
2478 int w = s->background_width - width, h = s->height;
2479 XRectangle r;
2480 GC gc;
2482 if (s->row->mouse_face_p
2483 && cursor_in_mouse_face_p (s->w))
2485 x_set_mouse_face_gc (s);
2486 gc = s->gc;
2488 else
2489 gc = s->face->gc;
2491 get_glyph_string_clip_rect (s, &r);
2492 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2494 if (s->face->stipple)
2496 /* Fill background with a stipple pattern. */
2497 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2498 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2499 XSetFillStyle (s->display, gc, FillSolid);
2501 else
2503 XGCValues xgcv;
2504 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2505 XSetForeground (s->display, gc, xgcv.background);
2506 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2507 XSetForeground (s->display, gc, xgcv.foreground);
2511 else if (!s->background_filled_p)
2512 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
2513 s->height);
2515 s->background_filled_p = 1;
2519 /* Draw glyph string S. */
2521 static void
2522 x_draw_glyph_string (s)
2523 struct glyph_string *s;
2525 int relief_drawn_p = 0;
2527 /* If S draws into the background of its successor, draw the
2528 background of the successor first so that S can draw into it.
2529 This makes S->next use XDrawString instead of XDrawImageString. */
2530 if (s->next && s->right_overhang && !s->for_overlaps_p)
2532 xassert (s->next->img == NULL);
2533 x_set_glyph_string_gc (s->next);
2534 x_set_glyph_string_clipping (s->next);
2535 x_draw_glyph_string_background (s->next, 1);
2538 /* Set up S->gc, set clipping and draw S. */
2539 x_set_glyph_string_gc (s);
2541 /* Draw relief (if any) in advance for char/composition so that the
2542 glyph string can be drawn over it. */
2543 if (!s->for_overlaps_p
2544 && s->face->box != FACE_NO_BOX
2545 && (s->first_glyph->type == CHAR_GLYPH
2546 || s->first_glyph->type == COMPOSITE_GLYPH))
2549 x_set_glyph_string_clipping (s);
2550 x_draw_glyph_string_background (s, 1);
2551 x_draw_glyph_string_box (s);
2552 x_set_glyph_string_clipping (s);
2553 relief_drawn_p = 1;
2555 else
2556 x_set_glyph_string_clipping (s);
2558 switch (s->first_glyph->type)
2560 case IMAGE_GLYPH:
2561 x_draw_image_glyph_string (s);
2562 break;
2564 case STRETCH_GLYPH:
2565 x_draw_stretch_glyph_string (s);
2566 break;
2568 case CHAR_GLYPH:
2569 if (s->for_overlaps_p)
2570 s->background_filled_p = 1;
2571 else
2572 x_draw_glyph_string_background (s, 0);
2573 x_draw_glyph_string_foreground (s);
2574 break;
2576 case COMPOSITE_GLYPH:
2577 if (s->for_overlaps_p || s->gidx > 0)
2578 s->background_filled_p = 1;
2579 else
2580 x_draw_glyph_string_background (s, 1);
2581 x_draw_composite_glyph_string_foreground (s);
2582 break;
2584 default:
2585 abort ();
2588 if (!s->for_overlaps_p)
2590 /* Draw underline. */
2591 if (s->face->underline_p)
2593 unsigned long tem, h;
2594 int y;
2596 /* Get the underline thickness. Default is 1 pixel. */
2597 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
2598 h = 1;
2600 /* Get the underline position. This is the recommended
2601 vertical offset in pixels from the baseline to the top of
2602 the underline. This is a signed value according to the
2603 specs, and its default is
2605 ROUND ((maximum descent) / 2), with
2606 ROUND(x) = floor (x + 0.5) */
2608 if (x_use_underline_position_properties
2609 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
2610 y = s->ybase + (long) tem;
2611 else if (s->face->font)
2612 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2613 else
2614 y = s->y + s->height - h;
2616 if (s->face->underline_defaulted_p)
2617 XFillRectangle (s->display, s->window, s->gc,
2618 s->x, y, s->width, h);
2619 else
2621 XGCValues xgcv;
2622 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2623 XSetForeground (s->display, s->gc, s->face->underline_color);
2624 XFillRectangle (s->display, s->window, s->gc,
2625 s->x, y, s->width, h);
2626 XSetForeground (s->display, s->gc, xgcv.foreground);
2630 /* Draw overline. */
2631 if (s->face->overline_p)
2633 unsigned long dy = 0, h = 1;
2635 if (s->face->overline_color_defaulted_p)
2636 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2637 s->width, h);
2638 else
2640 XGCValues xgcv;
2641 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2642 XSetForeground (s->display, s->gc, s->face->overline_color);
2643 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2644 s->width, h);
2645 XSetForeground (s->display, s->gc, xgcv.foreground);
2649 /* Draw strike-through. */
2650 if (s->face->strike_through_p)
2652 unsigned long h = 1;
2653 unsigned long dy = (s->height - h) / 2;
2655 if (s->face->strike_through_color_defaulted_p)
2656 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2657 s->width, h);
2658 else
2660 XGCValues xgcv;
2661 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2662 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2663 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2664 s->width, h);
2665 XSetForeground (s->display, s->gc, xgcv.foreground);
2669 /* Draw relief if not yet drawn. */
2670 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2671 x_draw_glyph_string_box (s);
2674 /* Reset clipping. */
2675 XSetClipMask (s->display, s->gc, None);
2678 /* Shift display to make room for inserted glyphs. */
2680 void
2681 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2682 struct frame *f;
2683 int x, y, width, height, shift_by;
2685 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2686 f->output_data.x->normal_gc,
2687 x, y, width, height,
2688 x + shift_by, y);
2691 /* Delete N glyphs at the nominal cursor position. Not implemented
2692 for X frames. */
2694 static void
2695 x_delete_glyphs (n)
2696 register int n;
2698 abort ();
2702 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2703 If they are <= 0, this is probably an error. */
2705 void
2706 x_clear_area (dpy, window, x, y, width, height, exposures)
2707 Display *dpy;
2708 Window window;
2709 int x, y;
2710 int width, height;
2711 int exposures;
2713 xassert (width > 0 && height > 0);
2714 XClearArea (dpy, window, x, y, width, height, exposures);
2718 /* Clear entire frame. If updating_frame is non-null, clear that
2719 frame. Otherwise clear the selected frame. */
2721 static void
2722 x_clear_frame ()
2724 struct frame *f;
2726 if (updating_frame)
2727 f = updating_frame;
2728 else
2729 f = SELECTED_FRAME ();
2731 /* Clearing the frame will erase any cursor, so mark them all as no
2732 longer visible. */
2733 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2734 output_cursor.hpos = output_cursor.vpos = 0;
2735 output_cursor.x = -1;
2737 /* We don't set the output cursor here because there will always
2738 follow an explicit cursor_to. */
2739 BLOCK_INPUT;
2740 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2742 /* We have to clear the scroll bars, too. If we have changed
2743 colors or something like that, then they should be notified. */
2744 x_scroll_bar_clear (f);
2746 XFlush (FRAME_X_DISPLAY (f));
2748 #ifdef USE_GTK
2749 xg_frame_cleared (f);
2750 #endif
2752 UNBLOCK_INPUT;
2757 /* Invert the middle quarter of the frame for .15 sec. */
2759 /* We use the select system call to do the waiting, so we have to make
2760 sure it's available. If it isn't, we just won't do visual bells. */
2762 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2765 /* Subtract the `struct timeval' values X and Y, storing the result in
2766 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2768 static int
2769 timeval_subtract (result, x, y)
2770 struct timeval *result, x, y;
2772 /* Perform the carry for the later subtraction by updating y. This
2773 is safer because on some systems the tv_sec member is unsigned. */
2774 if (x.tv_usec < y.tv_usec)
2776 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2777 y.tv_usec -= 1000000 * nsec;
2778 y.tv_sec += nsec;
2781 if (x.tv_usec - y.tv_usec > 1000000)
2783 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2784 y.tv_usec += 1000000 * nsec;
2785 y.tv_sec -= nsec;
2788 /* Compute the time remaining to wait. tv_usec is certainly
2789 positive. */
2790 result->tv_sec = x.tv_sec - y.tv_sec;
2791 result->tv_usec = x.tv_usec - y.tv_usec;
2793 /* Return indication of whether the result should be considered
2794 negative. */
2795 return x.tv_sec < y.tv_sec;
2798 void
2799 XTflash (f)
2800 struct frame *f;
2802 BLOCK_INPUT;
2805 GC gc;
2807 /* Create a GC that will use the GXxor function to flip foreground
2808 pixels into background pixels. */
2810 XGCValues values;
2812 values.function = GXxor;
2813 values.foreground = (f->output_data.x->foreground_pixel
2814 ^ f->output_data.x->background_pixel);
2816 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2817 GCFunction | GCForeground, &values);
2821 /* Get the height not including a menu bar widget. */
2822 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
2823 /* Height of each line to flash. */
2824 int flash_height = FRAME_LINE_HEIGHT (f);
2825 /* These will be the left and right margins of the rectangles. */
2826 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
2827 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
2829 int width;
2831 /* Don't flash the area between a scroll bar and the frame
2832 edge it is next to. */
2833 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
2835 case vertical_scroll_bar_left:
2836 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2837 break;
2839 case vertical_scroll_bar_right:
2840 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2841 break;
2843 default:
2844 break;
2847 width = flash_right - flash_left;
2849 /* If window is tall, flash top and bottom line. */
2850 if (height > 3 * FRAME_LINE_HEIGHT (f))
2852 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2853 flash_left,
2854 (FRAME_INTERNAL_BORDER_WIDTH (f)
2855 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
2856 width, flash_height);
2857 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2858 flash_left,
2859 (height - flash_height
2860 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2861 width, flash_height);
2863 else
2864 /* If it is short, flash it all. */
2865 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2866 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2867 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2869 x_flush (f);
2872 struct timeval wakeup;
2874 EMACS_GET_TIME (wakeup);
2876 /* Compute time to wait until, propagating carry from usecs. */
2877 wakeup.tv_usec += 150000;
2878 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
2879 wakeup.tv_usec %= 1000000;
2881 /* Keep waiting until past the time wakeup or any input gets
2882 available. */
2883 while (! detect_input_pending ())
2885 struct timeval current;
2886 struct timeval timeout;
2888 EMACS_GET_TIME (current);
2890 /* Break if result would be negative. */
2891 if (timeval_subtract (&current, wakeup, current))
2892 break;
2894 /* How long `select' should wait. */
2895 timeout.tv_sec = 0;
2896 timeout.tv_usec = 10000;
2898 /* Try to wait that long--but we might wake up sooner. */
2899 select (0, NULL, NULL, NULL, &timeout);
2903 /* If window is tall, flash top and bottom line. */
2904 if (height > 3 * FRAME_LINE_HEIGHT (f))
2906 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2907 flash_left,
2908 (FRAME_INTERNAL_BORDER_WIDTH (f)
2909 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
2910 width, flash_height);
2911 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2912 flash_left,
2913 (height - flash_height
2914 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2915 width, flash_height);
2917 else
2918 /* If it is short, flash it all. */
2919 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2920 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2921 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2923 XFreeGC (FRAME_X_DISPLAY (f), gc);
2924 x_flush (f);
2928 UNBLOCK_INPUT;
2931 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
2934 /* Make audible bell. */
2936 void
2937 XTring_bell ()
2939 struct frame *f = SELECTED_FRAME ();
2941 if (FRAME_X_DISPLAY (f))
2943 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2944 if (visible_bell)
2945 XTflash (f);
2946 else
2947 #endif
2949 BLOCK_INPUT;
2950 XBell (FRAME_X_DISPLAY (f), 0);
2951 XFlush (FRAME_X_DISPLAY (f));
2952 UNBLOCK_INPUT;
2958 /* Specify how many text lines, from the top of the window,
2959 should be affected by insert-lines and delete-lines operations.
2960 This, and those operations, are used only within an update
2961 that is bounded by calls to x_update_begin and x_update_end. */
2963 static void
2964 XTset_terminal_window (n)
2965 register int n;
2967 /* This function intentionally left blank. */
2972 /***********************************************************************
2973 Line Dance
2974 ***********************************************************************/
2976 /* Perform an insert-lines or delete-lines operation, inserting N
2977 lines or deleting -N lines at vertical position VPOS. */
2979 static void
2980 x_ins_del_lines (vpos, n)
2981 int vpos, n;
2983 abort ();
2987 /* Scroll part of the display as described by RUN. */
2989 static void
2990 x_scroll_run (w, run)
2991 struct window *w;
2992 struct run *run;
2994 struct frame *f = XFRAME (w->frame);
2995 int x, y, width, height, from_y, to_y, bottom_y;
2997 /* Get frame-relative bounding box of the text display area of W,
2998 without mode lines. Include in this box the left and right
2999 fringe of W. */
3000 window_box (w, -1, &x, &y, &width, &height);
3001 width += FRAME_X_FRINGE_WIDTH (f);
3002 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
3004 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3005 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3006 bottom_y = y + height;
3008 if (to_y < from_y)
3010 /* Scrolling up. Make sure we don't copy part of the mode
3011 line at the bottom. */
3012 if (from_y + run->height > bottom_y)
3013 height = bottom_y - from_y;
3014 else
3015 height = run->height;
3017 else
3019 /* Scolling down. Make sure we don't copy over the mode line.
3020 at the bottom. */
3021 if (to_y + run->height > bottom_y)
3022 height = bottom_y - to_y;
3023 else
3024 height = run->height;
3027 BLOCK_INPUT;
3029 /* Cursor off. Will be switched on again in x_update_window_end. */
3030 updated_window = w;
3031 x_clear_cursor (w);
3033 XCopyArea (FRAME_X_DISPLAY (f),
3034 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3035 f->output_data.x->normal_gc,
3036 x, from_y,
3037 width, height,
3038 x, to_y);
3040 UNBLOCK_INPUT;
3045 /***********************************************************************
3046 Exposure Events
3047 ***********************************************************************/
3050 static void
3051 frame_highlight (f)
3052 struct frame *f;
3054 /* We used to only do this if Vx_no_window_manager was non-nil, but
3055 the ICCCM (section 4.1.6) says that the window's border pixmap
3056 and border pixel are window attributes which are "private to the
3057 client", so we can always change it to whatever we want. */
3058 BLOCK_INPUT;
3059 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3060 f->output_data.x->border_pixel);
3061 UNBLOCK_INPUT;
3062 x_update_cursor (f, 1);
3065 static void
3066 frame_unhighlight (f)
3067 struct frame *f;
3069 /* We used to only do this if Vx_no_window_manager was non-nil, but
3070 the ICCCM (section 4.1.6) says that the window's border pixmap
3071 and border pixel are window attributes which are "private to the
3072 client", so we can always change it to whatever we want. */
3073 BLOCK_INPUT;
3074 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3075 f->output_data.x->border_tile);
3076 UNBLOCK_INPUT;
3077 x_update_cursor (f, 1);
3080 /* The focus has changed. Update the frames as necessary to reflect
3081 the new situation. Note that we can't change the selected frame
3082 here, because the Lisp code we are interrupting might become confused.
3083 Each event gets marked with the frame in which it occurred, so the
3084 Lisp code can tell when the switch took place by examining the events. */
3086 static void
3087 x_new_focus_frame (dpyinfo, frame)
3088 struct x_display_info *dpyinfo;
3089 struct frame *frame;
3091 struct frame *old_focus = dpyinfo->x_focus_frame;
3093 if (frame != dpyinfo->x_focus_frame)
3095 /* Set this before calling other routines, so that they see
3096 the correct value of x_focus_frame. */
3097 dpyinfo->x_focus_frame = frame;
3099 if (old_focus && old_focus->auto_lower)
3100 x_lower_frame (old_focus);
3102 #if 0
3103 selected_frame = frame;
3104 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3105 selected_frame);
3106 Fselect_window (selected_frame->selected_window);
3107 choose_minibuf_frame ();
3108 #endif /* ! 0 */
3110 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3111 pending_autoraise_frame = dpyinfo->x_focus_frame;
3112 else
3113 pending_autoraise_frame = 0;
3116 x_frame_rehighlight (dpyinfo);
3119 /* Handle FocusIn and FocusOut state changes for FRAME.
3120 If FRAME has focus and there exists more than one frame, puts
3121 a FOCUS_IN_EVENT into BUFP.
3122 Returns number of events inserted into BUFP. */
3124 static int
3125 x_focus_changed (type, state, dpyinfo, frame, bufp, numchars)
3126 int type;
3127 int state;
3128 struct x_display_info *dpyinfo;
3129 struct frame *frame;
3130 struct input_event *bufp;
3131 int numchars;
3133 int nr_events = 0;
3135 if (type == FocusIn)
3137 if (dpyinfo->x_focus_event_frame != frame)
3139 x_new_focus_frame (dpyinfo, frame);
3140 dpyinfo->x_focus_event_frame = frame;
3142 /* Don't stop displaying the initial startup message
3143 for a switch-frame event we don't need. */
3144 if (numchars > 0
3145 && GC_NILP (Vterminal_frame)
3146 && GC_CONSP (Vframe_list)
3147 && !GC_NILP (XCDR (Vframe_list)))
3149 bufp->kind = FOCUS_IN_EVENT;
3150 XSETFRAME (bufp->frame_or_window, frame);
3151 bufp->arg = Qnil;
3152 ++bufp;
3153 numchars--;
3154 ++nr_events;
3158 frame->output_data.x->focus_state |= state;
3160 #ifdef HAVE_X_I18N
3161 if (FRAME_XIC (frame))
3162 XSetICFocus (FRAME_XIC (frame));
3163 #endif
3165 else if (type == FocusOut)
3167 frame->output_data.x->focus_state &= ~state;
3169 if (dpyinfo->x_focus_event_frame == frame)
3171 dpyinfo->x_focus_event_frame = 0;
3172 x_new_focus_frame (dpyinfo, 0);
3175 #ifdef HAVE_X_I18N
3176 if (FRAME_XIC (frame))
3177 XUnsetICFocus (FRAME_XIC (frame));
3178 #endif
3181 return nr_events;
3184 /* The focus may have changed. Figure out if it is a real focus change,
3185 by checking both FocusIn/Out and Enter/LeaveNotify events.
3187 Returns number of events inserted into BUFP. */
3189 static int
3190 x_detect_focus_change (dpyinfo, event, bufp, numchars)
3191 struct x_display_info *dpyinfo;
3192 XEvent *event;
3193 struct input_event *bufp;
3194 int numchars;
3196 struct frame *frame;
3197 int nr_events = 0;
3199 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3200 if (! frame) return nr_events;
3202 switch (event->type)
3204 case EnterNotify:
3205 case LeaveNotify:
3206 if (event->xcrossing.detail != NotifyInferior
3207 && event->xcrossing.focus
3208 && ! (frame->output_data.x->focus_state & FOCUS_EXPLICIT))
3209 nr_events = x_focus_changed ((event->type == EnterNotify
3210 ? FocusIn : FocusOut),
3211 FOCUS_IMPLICIT,
3212 dpyinfo,
3213 frame,
3214 bufp,
3215 numchars);
3216 break;
3218 case FocusIn:
3219 case FocusOut:
3220 nr_events = x_focus_changed (event->type,
3221 (event->xfocus.detail == NotifyPointer
3222 ? FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3223 dpyinfo,
3224 frame,
3225 bufp,
3226 numchars);
3227 break;
3230 return nr_events;
3234 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3236 void
3237 x_mouse_leave (dpyinfo)
3238 struct x_display_info *dpyinfo;
3240 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3243 /* The focus has changed, or we have redirected a frame's focus to
3244 another frame (this happens when a frame uses a surrogate
3245 mini-buffer frame). Shift the highlight as appropriate.
3247 The FRAME argument doesn't necessarily have anything to do with which
3248 frame is being highlighted or un-highlighted; we only use it to find
3249 the appropriate X display info. */
3251 static void
3252 XTframe_rehighlight (frame)
3253 struct frame *frame;
3255 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3258 static void
3259 x_frame_rehighlight (dpyinfo)
3260 struct x_display_info *dpyinfo;
3262 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3264 if (dpyinfo->x_focus_frame)
3266 dpyinfo->x_highlight_frame
3267 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3268 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3269 : dpyinfo->x_focus_frame);
3270 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3272 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3273 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3276 else
3277 dpyinfo->x_highlight_frame = 0;
3279 if (dpyinfo->x_highlight_frame != old_highlight)
3281 if (old_highlight)
3282 frame_unhighlight (old_highlight);
3283 if (dpyinfo->x_highlight_frame)
3284 frame_highlight (dpyinfo->x_highlight_frame);
3290 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3292 /* Initialize mode_switch_bit and modifier_meaning. */
3293 static void
3294 x_find_modifier_meanings (dpyinfo)
3295 struct x_display_info *dpyinfo;
3297 int min_code, max_code;
3298 KeySym *syms;
3299 int syms_per_code;
3300 XModifierKeymap *mods;
3302 dpyinfo->meta_mod_mask = 0;
3303 dpyinfo->shift_lock_mask = 0;
3304 dpyinfo->alt_mod_mask = 0;
3305 dpyinfo->super_mod_mask = 0;
3306 dpyinfo->hyper_mod_mask = 0;
3308 #ifdef HAVE_X11R4
3309 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3310 #else
3311 min_code = dpyinfo->display->min_keycode;
3312 max_code = dpyinfo->display->max_keycode;
3313 #endif
3315 syms = XGetKeyboardMapping (dpyinfo->display,
3316 min_code, max_code - min_code + 1,
3317 &syms_per_code);
3318 mods = XGetModifierMapping (dpyinfo->display);
3320 /* Scan the modifier table to see which modifier bits the Meta and
3321 Alt keysyms are on. */
3323 int row, col; /* The row and column in the modifier table. */
3325 for (row = 3; row < 8; row++)
3326 for (col = 0; col < mods->max_keypermod; col++)
3328 KeyCode code
3329 = mods->modifiermap[(row * mods->max_keypermod) + col];
3331 /* Zeroes are used for filler. Skip them. */
3332 if (code == 0)
3333 continue;
3335 /* Are any of this keycode's keysyms a meta key? */
3337 int code_col;
3339 for (code_col = 0; code_col < syms_per_code; code_col++)
3341 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3343 switch (sym)
3345 case XK_Meta_L:
3346 case XK_Meta_R:
3347 dpyinfo->meta_mod_mask |= (1 << row);
3348 break;
3350 case XK_Alt_L:
3351 case XK_Alt_R:
3352 dpyinfo->alt_mod_mask |= (1 << row);
3353 break;
3355 case XK_Hyper_L:
3356 case XK_Hyper_R:
3357 dpyinfo->hyper_mod_mask |= (1 << row);
3358 break;
3360 case XK_Super_L:
3361 case XK_Super_R:
3362 dpyinfo->super_mod_mask |= (1 << row);
3363 break;
3365 case XK_Shift_Lock:
3366 /* Ignore this if it's not on the lock modifier. */
3367 if ((1 << row) == LockMask)
3368 dpyinfo->shift_lock_mask = LockMask;
3369 break;
3376 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3377 if (! dpyinfo->meta_mod_mask)
3379 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3380 dpyinfo->alt_mod_mask = 0;
3383 /* If some keys are both alt and meta,
3384 make them just meta, not alt. */
3385 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3387 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3390 XFree ((char *) syms);
3391 XFreeModifiermap (mods);
3394 /* Convert between the modifier bits X uses and the modifier bits
3395 Emacs uses. */
3397 static unsigned int
3398 x_x_to_emacs_modifiers (dpyinfo, state)
3399 struct x_display_info *dpyinfo;
3400 unsigned int state;
3402 EMACS_UINT mod_meta = meta_modifier;
3403 EMACS_UINT mod_alt = alt_modifier;
3404 EMACS_UINT mod_hyper = hyper_modifier;
3405 EMACS_UINT mod_super = super_modifier;
3406 Lisp_Object tem;
3408 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3409 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3410 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3411 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3412 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3413 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3414 tem = Fget (Vx_super_keysym, Qmodifier_value);
3415 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3418 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3419 | ((state & ControlMask) ? ctrl_modifier : 0)
3420 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3421 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3422 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3423 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3426 static unsigned int
3427 x_emacs_to_x_modifiers (dpyinfo, state)
3428 struct x_display_info *dpyinfo;
3429 unsigned int state;
3431 EMACS_UINT mod_meta = meta_modifier;
3432 EMACS_UINT mod_alt = alt_modifier;
3433 EMACS_UINT mod_hyper = hyper_modifier;
3434 EMACS_UINT mod_super = super_modifier;
3436 Lisp_Object tem;
3438 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3439 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3440 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3441 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3442 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3443 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3444 tem = Fget (Vx_super_keysym, Qmodifier_value);
3445 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3448 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3449 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3450 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3451 | ((state & shift_modifier) ? ShiftMask : 0)
3452 | ((state & ctrl_modifier) ? ControlMask : 0)
3453 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3456 /* Convert a keysym to its name. */
3458 char *
3459 x_get_keysym_name (keysym)
3460 KeySym keysym;
3462 char *value;
3464 BLOCK_INPUT;
3465 value = XKeysymToString (keysym);
3466 UNBLOCK_INPUT;
3468 return value;
3473 /* Mouse clicks and mouse movement. Rah. */
3475 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3477 If the event is a button press, then note that we have grabbed
3478 the mouse. */
3480 static Lisp_Object
3481 construct_mouse_click (result, event, f)
3482 struct input_event *result;
3483 XButtonEvent *event;
3484 struct frame *f;
3486 /* Make the event type NO_EVENT; we'll change that when we decide
3487 otherwise. */
3488 result->kind = MOUSE_CLICK_EVENT;
3489 result->code = event->button - Button1;
3490 result->timestamp = event->time;
3491 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3492 event->state)
3493 | (event->type == ButtonRelease
3494 ? up_modifier
3495 : down_modifier));
3497 XSETINT (result->x, event->x);
3498 XSETINT (result->y, event->y);
3499 XSETFRAME (result->frame_or_window, f);
3500 result->arg = Qnil;
3501 return Qnil;
3505 /* Function to report a mouse movement to the mainstream Emacs code.
3506 The input handler calls this.
3508 We have received a mouse movement event, which is given in *event.
3509 If the mouse is over a different glyph than it was last time, tell
3510 the mainstream emacs code by setting mouse_moved. If not, ask for
3511 another motion event, so we can check again the next time it moves. */
3513 static XMotionEvent last_mouse_motion_event;
3514 static Lisp_Object last_mouse_motion_frame;
3516 static void
3517 note_mouse_movement (frame, event)
3518 FRAME_PTR frame;
3519 XMotionEvent *event;
3521 last_mouse_movement_time = event->time;
3522 last_mouse_motion_event = *event;
3523 XSETFRAME (last_mouse_motion_frame, frame);
3525 if (event->window != FRAME_X_WINDOW (frame))
3527 frame->mouse_moved = 1;
3528 last_mouse_scroll_bar = Qnil;
3529 note_mouse_highlight (frame, -1, -1);
3532 /* Has the mouse moved off the glyph it was on at the last sighting? */
3533 else if (event->x < last_mouse_glyph.x
3534 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3535 || event->y < last_mouse_glyph.y
3536 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3538 frame->mouse_moved = 1;
3539 last_mouse_scroll_bar = Qnil;
3540 note_mouse_highlight (frame, event->x, event->y);
3545 /************************************************************************
3546 Mouse Face
3547 ************************************************************************/
3549 static void
3550 redo_mouse_highlight ()
3552 if (!NILP (last_mouse_motion_frame)
3553 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3554 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3555 last_mouse_motion_event.x,
3556 last_mouse_motion_event.y);
3560 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
3563 /* Try to determine frame pixel position and size of the glyph under
3564 frame pixel coordinates X/Y on frame F . Return the position and
3565 size in *RECT. Value is non-zero if we could compute these
3566 values. */
3568 static int
3569 glyph_rect (f, x, y, rect)
3570 struct frame *f;
3571 int x, y;
3572 XRectangle *rect;
3574 Lisp_Object window;
3575 int found = 0;
3577 window = window_from_coordinates (f, x, y, 0, 0);
3578 if (!NILP (window))
3580 struct window *w = XWINDOW (window);
3581 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
3582 struct glyph_row *end = r + w->current_matrix->nrows - 1;
3584 frame_to_window_pixel_xy (w, &x, &y);
3586 for (; !found && r < end && r->enabled_p; ++r)
3587 if (r->y >= y)
3589 struct glyph *g = r->glyphs[TEXT_AREA];
3590 struct glyph *end = g + r->used[TEXT_AREA];
3591 int gx;
3593 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
3594 if (gx >= x)
3596 rect->width = g->pixel_width;
3597 rect->height = r->height;
3598 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
3599 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
3600 found = 1;
3605 return found;
3609 /* Return the current position of the mouse.
3610 *FP should be a frame which indicates which display to ask about.
3612 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3613 and *PART to the frame, window, and scroll bar part that the mouse
3614 is over. Set *X and *Y to the portion and whole of the mouse's
3615 position on the scroll bar.
3617 If the mouse movement started elsewhere, set *FP to the frame the
3618 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3619 the mouse is over.
3621 Set *TIME to the server time-stamp for the time at which the mouse
3622 was at this position.
3624 Don't store anything if we don't have a valid set of values to report.
3626 This clears the mouse_moved flag, so we can wait for the next mouse
3627 movement. */
3629 static void
3630 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3631 FRAME_PTR *fp;
3632 int insist;
3633 Lisp_Object *bar_window;
3634 enum scroll_bar_part *part;
3635 Lisp_Object *x, *y;
3636 unsigned long *time;
3638 FRAME_PTR f1;
3640 BLOCK_INPUT;
3642 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3643 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3644 else
3646 Window root;
3647 int root_x, root_y;
3649 Window dummy_window;
3650 int dummy;
3652 Lisp_Object frame, tail;
3654 /* Clear the mouse-moved flag for every frame on this display. */
3655 FOR_EACH_FRAME (tail, frame)
3656 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3657 XFRAME (frame)->mouse_moved = 0;
3659 last_mouse_scroll_bar = Qnil;
3661 /* Figure out which root window we're on. */
3662 XQueryPointer (FRAME_X_DISPLAY (*fp),
3663 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3665 /* The root window which contains the pointer. */
3666 &root,
3668 /* Trash which we can't trust if the pointer is on
3669 a different screen. */
3670 &dummy_window,
3672 /* The position on that root window. */
3673 &root_x, &root_y,
3675 /* More trash we can't trust. */
3676 &dummy, &dummy,
3678 /* Modifier keys and pointer buttons, about which
3679 we don't care. */
3680 (unsigned int *) &dummy);
3682 /* Now we have a position on the root; find the innermost window
3683 containing the pointer. */
3685 Window win, child;
3686 int win_x, win_y;
3687 int parent_x = 0, parent_y = 0;
3688 int count;
3690 win = root;
3692 /* XTranslateCoordinates can get errors if the window
3693 structure is changing at the same time this function
3694 is running. So at least we must not crash from them. */
3696 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
3698 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3699 && FRAME_LIVE_P (last_mouse_frame))
3701 /* If mouse was grabbed on a frame, give coords for that frame
3702 even if the mouse is now outside it. */
3703 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3705 /* From-window, to-window. */
3706 root, FRAME_X_WINDOW (last_mouse_frame),
3708 /* From-position, to-position. */
3709 root_x, root_y, &win_x, &win_y,
3711 /* Child of win. */
3712 &child);
3713 f1 = last_mouse_frame;
3715 else
3717 while (1)
3719 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3721 /* From-window, to-window. */
3722 root, win,
3724 /* From-position, to-position. */
3725 root_x, root_y, &win_x, &win_y,
3727 /* Child of win. */
3728 &child);
3730 if (child == None || child == win)
3731 break;
3733 win = child;
3734 parent_x = win_x;
3735 parent_y = win_y;
3738 /* Now we know that:
3739 win is the innermost window containing the pointer
3740 (XTC says it has no child containing the pointer),
3741 win_x and win_y are the pointer's position in it
3742 (XTC did this the last time through), and
3743 parent_x and parent_y are the pointer's position in win's parent.
3744 (They are what win_x and win_y were when win was child.
3745 If win is the root window, it has no parent, and
3746 parent_{x,y} are invalid, but that's okay, because we'll
3747 never use them in that case.) */
3749 /* Is win one of our frames? */
3750 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3752 #ifdef USE_X_TOOLKIT
3753 /* If we end up with the menu bar window, say it's not
3754 on the frame. */
3755 if (f1 != NULL
3756 && f1->output_data.x->menubar_widget
3757 && win == XtWindow (f1->output_data.x->menubar_widget))
3758 f1 = NULL;
3759 #endif /* USE_X_TOOLKIT */
3762 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3763 f1 = 0;
3765 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
3767 /* If not, is it one of our scroll bars? */
3768 if (! f1)
3770 struct scroll_bar *bar = x_window_to_scroll_bar (win);
3772 if (bar)
3774 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3775 win_x = parent_x;
3776 win_y = parent_y;
3780 if (f1 == 0 && insist > 0)
3781 f1 = SELECTED_FRAME ();
3783 if (f1)
3785 /* Ok, we found a frame. Store all the values.
3786 last_mouse_glyph is a rectangle used to reduce the
3787 generation of mouse events. To not miss any motion
3788 events, we must divide the frame into rectangles of the
3789 size of the smallest character that could be displayed
3790 on it, i.e. into the same rectangles that matrices on
3791 the frame are divided into. */
3793 int width, height, gx, gy;
3794 XRectangle rect;
3796 if (glyph_rect (f1, win_x, win_y, &rect))
3797 last_mouse_glyph = rect;
3798 else
3800 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
3801 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
3802 gx = win_x;
3803 gy = win_y;
3805 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
3806 round down even for negative values. */
3807 if (gx < 0)
3808 gx -= width - 1;
3809 if (gy < 0)
3810 gy -= height - 1;
3811 gx = (gx + width - 1) / width * width;
3812 gy = (gy + height - 1) / height * height;
3814 last_mouse_glyph.width = width;
3815 last_mouse_glyph.height = height;
3816 last_mouse_glyph.x = gx;
3817 last_mouse_glyph.y = gy;
3820 *bar_window = Qnil;
3821 *part = 0;
3822 *fp = f1;
3823 XSETINT (*x, win_x);
3824 XSETINT (*y, win_y);
3825 *time = last_mouse_movement_time;
3830 UNBLOCK_INPUT;
3835 /***********************************************************************
3836 Scroll bars
3837 ***********************************************************************/
3839 /* Scroll bar support. */
3841 /* Given an X window ID, find the struct scroll_bar which manages it.
3842 This can be called in GC, so we have to make sure to strip off mark
3843 bits. */
3845 static struct scroll_bar *
3846 x_window_to_scroll_bar (window_id)
3847 Window window_id;
3849 Lisp_Object tail;
3851 #ifdef USE_GTK
3852 window_id = (Window) xg_get_scroll_id_for_window (window_id);
3853 #endif /* USE_GTK */
3855 for (tail = Vframe_list;
3856 XGCTYPE (tail) == Lisp_Cons;
3857 tail = XCDR (tail))
3859 Lisp_Object frame, bar, condemned;
3861 frame = XCAR (tail);
3862 /* All elements of Vframe_list should be frames. */
3863 if (! GC_FRAMEP (frame))
3864 abort ();
3866 /* Scan this frame's scroll bar list for a scroll bar with the
3867 right window ID. */
3868 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3869 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3870 /* This trick allows us to search both the ordinary and
3871 condemned scroll bar lists with one loop. */
3872 ! GC_NILP (bar) || (bar = condemned,
3873 condemned = Qnil,
3874 ! GC_NILP (bar));
3875 bar = XSCROLL_BAR (bar)->next)
3876 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
3877 return XSCROLL_BAR (bar);
3880 return 0;
3884 #if defined USE_LUCID
3886 /* Return the Lucid menu bar WINDOW is part of. Return null
3887 if WINDOW is not part of a menu bar. */
3889 static Widget
3890 x_window_to_menu_bar (window)
3891 Window window;
3893 Lisp_Object tail;
3895 for (tail = Vframe_list;
3896 XGCTYPE (tail) == Lisp_Cons;
3897 tail = XCDR (tail))
3899 Lisp_Object frame = XCAR (tail);
3900 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
3902 if (menu_bar && xlwmenu_window_p (menu_bar, window))
3903 return menu_bar;
3906 return NULL;
3909 #endif /* USE_LUCID */
3912 /************************************************************************
3913 Toolkit scroll bars
3914 ************************************************************************/
3916 #ifdef USE_TOOLKIT_SCROLL_BARS
3918 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
3919 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
3920 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
3921 struct scroll_bar *));
3922 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
3923 int, int, int));
3926 /* Lisp window being scrolled. Set when starting to interact with
3927 a toolkit scroll bar, reset to nil when ending the interaction. */
3929 static Lisp_Object window_being_scrolled;
3931 /* Last scroll bar part sent in xm_scroll_callback. */
3933 static int last_scroll_bar_part;
3935 /* Whether this is an Xaw with arrow-scrollbars. This should imply
3936 that movements of 1/20 of the screen size are mapped to up/down. */
3938 #ifndef USE_GTK
3939 /* Id of action hook installed for scroll bars. */
3941 static XtActionHookId action_hook_id;
3943 static Boolean xaw3d_arrow_scroll;
3945 /* Whether the drag scrolling maintains the mouse at the top of the
3946 thumb. If not, resizing the thumb needs to be done more carefully
3947 to avoid jerkyness. */
3949 static Boolean xaw3d_pick_top;
3951 extern void set_vertical_scroll_bar P_ ((struct window *));
3953 /* Action hook installed via XtAppAddActionHook when toolkit scroll
3954 bars are used.. The hook is responsible for detecting when
3955 the user ends an interaction with the scroll bar, and generates
3956 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
3958 static void
3959 xt_action_hook (widget, client_data, action_name, event, params,
3960 num_params)
3961 Widget widget;
3962 XtPointer client_data;
3963 String action_name;
3964 XEvent *event;
3965 String *params;
3966 Cardinal *num_params;
3968 int scroll_bar_p;
3969 char *end_action;
3971 #ifdef USE_MOTIF
3972 scroll_bar_p = XmIsScrollBar (widget);
3973 end_action = "Release";
3974 #else /* !USE_MOTIF i.e. use Xaw */
3975 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
3976 end_action = "EndScroll";
3977 #endif /* USE_MOTIF */
3979 if (scroll_bar_p
3980 && strcmp (action_name, end_action) == 0
3981 && WINDOWP (window_being_scrolled))
3983 struct window *w;
3985 x_send_scroll_bar_event (window_being_scrolled,
3986 scroll_bar_end_scroll, 0, 0);
3987 w = XWINDOW (window_being_scrolled);
3989 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
3991 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
3992 /* The thumb size is incorrect while dragging: fix it. */
3993 set_vertical_scroll_bar (w);
3995 window_being_scrolled = Qnil;
3996 last_scroll_bar_part = -1;
3998 /* Xt timeouts no longer needed. */
3999 toolkit_scroll_bar_interaction = 0;
4002 #endif /* not USE_GTK */
4004 /* A vector of windows used for communication between
4005 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4007 static struct window **scroll_bar_windows;
4008 static int scroll_bar_windows_size;
4011 /* Send a client message with message type Xatom_Scrollbar for a
4012 scroll action to the frame of WINDOW. PART is a value identifying
4013 the part of the scroll bar that was clicked on. PORTION is the
4014 amount to scroll of a whole of WHOLE. */
4016 static void
4017 x_send_scroll_bar_event (window, part, portion, whole)
4018 Lisp_Object window;
4019 int part, portion, whole;
4021 XEvent event;
4022 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4023 struct window *w = XWINDOW (window);
4024 struct frame *f = XFRAME (w->frame);
4025 int i;
4027 BLOCK_INPUT;
4029 /* Construct a ClientMessage event to send to the frame. */
4030 ev->type = ClientMessage;
4031 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4032 ev->display = FRAME_X_DISPLAY (f);
4033 ev->window = FRAME_X_WINDOW (f);
4034 ev->format = 32;
4036 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4037 not enough to store a pointer or Lisp_Object on a 64 bit system.
4038 So, store the window in scroll_bar_windows and pass the index
4039 into that array in the event. */
4040 for (i = 0; i < scroll_bar_windows_size; ++i)
4041 if (scroll_bar_windows[i] == NULL)
4042 break;
4044 if (i == scroll_bar_windows_size)
4046 int new_size = max (10, 2 * scroll_bar_windows_size);
4047 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4048 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4050 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4051 nbytes);
4052 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4053 scroll_bar_windows_size = new_size;
4056 scroll_bar_windows[i] = w;
4057 ev->data.l[0] = (long) i;
4058 ev->data.l[1] = (long) part;
4059 ev->data.l[2] = (long) 0;
4060 ev->data.l[3] = (long) portion;
4061 ev->data.l[4] = (long) whole;
4063 /* Make Xt timeouts work while the scroll bar is active. */
4064 toolkit_scroll_bar_interaction = 1;
4066 /* Setting the event mask to zero means that the message will
4067 be sent to the client that created the window, and if that
4068 window no longer exists, no event will be sent. */
4069 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4070 UNBLOCK_INPUT;
4074 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4075 in *IEVENT. */
4077 static void
4078 x_scroll_bar_to_input_event (event, ievent)
4079 XEvent *event;
4080 struct input_event *ievent;
4082 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4083 Lisp_Object window;
4084 struct frame *f;
4085 struct window *w;
4087 w = scroll_bar_windows[ev->data.l[0]];
4088 scroll_bar_windows[ev->data.l[0]] = NULL;
4090 XSETWINDOW (window, w);
4091 f = XFRAME (w->frame);
4093 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4094 ievent->frame_or_window = window;
4095 ievent->arg = Qnil;
4096 #ifdef USE_GTK
4097 ievent->timestamp = CurrentTime;
4098 #else
4099 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4100 #endif
4101 ievent->part = ev->data.l[1];
4102 ievent->code = ev->data.l[2];
4103 ievent->x = make_number ((int) ev->data.l[3]);
4104 ievent->y = make_number ((int) ev->data.l[4]);
4105 ievent->modifiers = 0;
4109 #ifdef USE_MOTIF
4111 /* Minimum and maximum values used for Motif scroll bars. */
4113 #define XM_SB_MAX 10000000
4116 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4117 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4118 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4120 static void
4121 xm_scroll_callback (widget, client_data, call_data)
4122 Widget widget;
4123 XtPointer client_data, call_data;
4125 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4126 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4127 int part = -1, whole = 0, portion = 0;
4129 switch (cs->reason)
4131 case XmCR_DECREMENT:
4132 bar->dragging = Qnil;
4133 part = scroll_bar_up_arrow;
4134 break;
4136 case XmCR_INCREMENT:
4137 bar->dragging = Qnil;
4138 part = scroll_bar_down_arrow;
4139 break;
4141 case XmCR_PAGE_DECREMENT:
4142 bar->dragging = Qnil;
4143 part = scroll_bar_above_handle;
4144 break;
4146 case XmCR_PAGE_INCREMENT:
4147 bar->dragging = Qnil;
4148 part = scroll_bar_below_handle;
4149 break;
4151 case XmCR_TO_TOP:
4152 bar->dragging = Qnil;
4153 part = scroll_bar_to_top;
4154 break;
4156 case XmCR_TO_BOTTOM:
4157 bar->dragging = Qnil;
4158 part = scroll_bar_to_bottom;
4159 break;
4161 case XmCR_DRAG:
4163 int slider_size;
4165 /* Get the slider size. */
4166 BLOCK_INPUT;
4167 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4168 UNBLOCK_INPUT;
4170 whole = XM_SB_MAX - slider_size;
4171 portion = min (cs->value, whole);
4172 part = scroll_bar_handle;
4173 bar->dragging = make_number (cs->value);
4175 break;
4177 case XmCR_VALUE_CHANGED:
4178 break;
4181 if (part >= 0)
4183 window_being_scrolled = bar->window;
4184 last_scroll_bar_part = part;
4185 x_send_scroll_bar_event (bar->window, part, portion, whole);
4190 #else /* !USE_MOTIF, i.e. Xaw or GTK */
4191 #ifdef USE_GTK
4192 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4193 bar widget. DATA is a pointer to the scroll_bar structure. */
4195 static void
4196 xg_scroll_callback (widget, data)
4197 GtkRange *widget;
4198 gpointer data;
4200 struct scroll_bar *bar = (struct scroll_bar *) data;
4201 gdouble previous;
4202 gdouble position;
4203 gdouble *p;
4204 int diff;
4206 int part = -1, whole = 0, portion = 0;
4207 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4209 if (xg_ignore_gtk_scrollbar) return;
4211 position = gtk_adjustment_get_value (adj);
4213 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4214 if (! p)
4216 p = (gdouble*) xmalloc (sizeof (gdouble));
4217 *p = XG_SB_MIN;
4218 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4221 previous = *p;
4222 *p = position;
4224 diff = (int) (position - previous);
4226 if (diff == (int) adj->step_increment)
4228 part = scroll_bar_down_arrow;
4229 bar->dragging = Qnil;
4231 else if (-diff == (int) adj->step_increment)
4233 part = scroll_bar_up_arrow;
4234 bar->dragging = Qnil;
4236 else if (diff == (int) adj->page_increment)
4238 part = scroll_bar_below_handle;
4239 bar->dragging = Qnil;
4241 else if (-diff == (int) adj->page_increment)
4243 part = scroll_bar_above_handle;
4244 bar->dragging = Qnil;
4246 else
4248 part = scroll_bar_handle;
4249 whole = adj->upper - adj->page_size;
4250 portion = min ((int)position, whole);
4251 bar->dragging = make_number ((int)portion);
4254 if (part >= 0)
4256 window_being_scrolled = bar->window;
4257 last_scroll_bar_part = part;
4258 x_send_scroll_bar_event (bar->window, part, portion, whole);
4262 #else /* not USE_GTK */
4264 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4265 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4266 scroll bar struct. CALL_DATA is a pointer to a float saying where
4267 the thumb is. */
4269 static void
4270 xaw_jump_callback (widget, client_data, call_data)
4271 Widget widget;
4272 XtPointer client_data, call_data;
4274 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4275 float top = *(float *) call_data;
4276 float shown;
4277 int whole, portion, height;
4278 int part;
4280 /* Get the size of the thumb, a value between 0 and 1. */
4281 BLOCK_INPUT;
4282 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4283 UNBLOCK_INPUT;
4285 whole = 10000000;
4286 portion = shown < 1 ? top * whole : 0;
4288 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
4289 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4290 the bottom, so we force the scrolling whenever we see that we're
4291 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4292 we try to ensure that we always stay two pixels away from the
4293 bottom). */
4294 part = scroll_bar_down_arrow;
4295 else
4296 part = scroll_bar_handle;
4298 window_being_scrolled = bar->window;
4299 bar->dragging = make_number (portion);
4300 last_scroll_bar_part = part;
4301 x_send_scroll_bar_event (bar->window, part, portion, whole);
4305 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4306 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4307 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4308 the scroll bar. CALL_DATA is an integer specifying the action that
4309 has taken place. Its magnitude is in the range 0..height of the
4310 scroll bar. Negative values mean scroll towards buffer start.
4311 Values < height of scroll bar mean line-wise movement. */
4313 static void
4314 xaw_scroll_callback (widget, client_data, call_data)
4315 Widget widget;
4316 XtPointer client_data, call_data;
4318 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4319 /* The position really is stored cast to a pointer. */
4320 int position = (long) call_data;
4321 Dimension height;
4322 int part;
4324 /* Get the height of the scroll bar. */
4325 BLOCK_INPUT;
4326 XtVaGetValues (widget, XtNheight, &height, NULL);
4327 UNBLOCK_INPUT;
4329 if (abs (position) >= height)
4330 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4332 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4333 it maps line-movement to call_data = max(5, height/20). */
4334 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
4335 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4336 else
4337 part = scroll_bar_move_ratio;
4339 window_being_scrolled = bar->window;
4340 bar->dragging = Qnil;
4341 last_scroll_bar_part = part;
4342 x_send_scroll_bar_event (bar->window, part, position, height);
4345 #endif /* not USE_GTK */
4346 #endif /* not USE_MOTIF */
4348 #define SCROLL_BAR_NAME "verticalScrollBar"
4350 /* Create the widget for scroll bar BAR on frame F. Record the widget
4351 and X window of the scroll bar in BAR. */
4353 #ifdef USE_GTK
4354 static void
4355 x_create_toolkit_scroll_bar (f, bar)
4356 struct frame *f;
4357 struct scroll_bar *bar;
4359 char *scroll_bar_name = SCROLL_BAR_NAME;
4361 BLOCK_INPUT;
4362 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4363 scroll_bar_name);
4364 UNBLOCK_INPUT;
4367 #else /* not USE_GTK */
4369 static void
4370 x_create_toolkit_scroll_bar (f, bar)
4371 struct frame *f;
4372 struct scroll_bar *bar;
4374 Window xwindow;
4375 Widget widget;
4376 Arg av[20];
4377 int ac = 0;
4378 char *scroll_bar_name = SCROLL_BAR_NAME;
4379 unsigned long pixel;
4381 BLOCK_INPUT;
4383 #ifdef USE_MOTIF
4384 /* Set resources. Create the widget. */
4385 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4386 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4387 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4388 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4389 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4390 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4391 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4393 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4394 if (pixel != -1)
4396 XtSetArg (av[ac], XmNforeground, pixel);
4397 ++ac;
4400 pixel = f->output_data.x->scroll_bar_background_pixel;
4401 if (pixel != -1)
4403 XtSetArg (av[ac], XmNbackground, pixel);
4404 ++ac;
4407 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4408 scroll_bar_name, av, ac);
4410 /* Add one callback for everything that can happen. */
4411 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4412 (XtPointer) bar);
4413 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4414 (XtPointer) bar);
4415 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4416 (XtPointer) bar);
4417 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4418 (XtPointer) bar);
4419 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4420 (XtPointer) bar);
4421 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4422 (XtPointer) bar);
4423 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4424 (XtPointer) bar);
4426 /* Realize the widget. Only after that is the X window created. */
4427 XtRealizeWidget (widget);
4429 /* Set the cursor to an arrow. I didn't find a resource to do that.
4430 And I'm wondering why it hasn't an arrow cursor by default. */
4431 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4432 f->output_data.x->nontext_cursor);
4434 #else /* !USE_MOTIF i.e. use Xaw */
4436 /* Set resources. Create the widget. The background of the
4437 Xaw3d scroll bar widget is a little bit light for my taste.
4438 We don't alter it here to let users change it according
4439 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4440 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4441 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4442 /* For smoother scrolling with Xaw3d -sm */
4443 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4445 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4446 if (pixel != -1)
4448 XtSetArg (av[ac], XtNforeground, pixel);
4449 ++ac;
4452 pixel = f->output_data.x->scroll_bar_background_pixel;
4453 if (pixel != -1)
4455 XtSetArg (av[ac], XtNbackground, pixel);
4456 ++ac;
4459 /* Top/bottom shadow colors. */
4461 /* Allocate them, if necessary. */
4462 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4464 pixel = f->output_data.x->scroll_bar_background_pixel;
4465 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4466 &pixel, 1.2, 0x8000))
4467 pixel = -1;
4468 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4470 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4472 pixel = f->output_data.x->scroll_bar_background_pixel;
4473 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4474 &pixel, 0.6, 0x4000))
4475 pixel = -1;
4476 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4479 /* Tell the toolkit about them. */
4480 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4481 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4482 /* We tried to allocate a color for the top/bottom shadow, and
4483 failed, so tell Xaw3d to use dithering instead. */
4485 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4486 ++ac;
4488 else
4489 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4490 be more consistent with other emacs 3d colors, and since Xaw3d is
4491 not good at dealing with allocation failure. */
4493 /* This tells Xaw3d to use real colors instead of dithering for
4494 the shadows. */
4495 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4496 ++ac;
4498 /* Specify the colors. */
4499 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4500 if (pixel != -1)
4502 XtSetArg (av[ac], "topShadowPixel", pixel);
4503 ++ac;
4505 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4506 if (pixel != -1)
4508 XtSetArg (av[ac], "bottomShadowPixel", pixel);
4509 ++ac;
4513 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4514 f->output_data.x->edit_widget, av, ac);
4517 char *initial = "";
4518 char *val = initial;
4519 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4520 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4521 if (val == initial)
4522 { /* ARROW_SCROLL */
4523 xaw3d_arrow_scroll = True;
4524 /* Isn't that just a personal preference ? -sm */
4525 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4529 /* Define callbacks. */
4530 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4531 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4532 (XtPointer) bar);
4534 /* Realize the widget. Only after that is the X window created. */
4535 XtRealizeWidget (widget);
4537 #endif /* !USE_MOTIF */
4539 /* Install an action hook that lets us detect when the user
4540 finishes interacting with a scroll bar. */
4541 if (action_hook_id == 0)
4542 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4544 /* Remember X window and widget in the scroll bar vector. */
4545 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4546 xwindow = XtWindow (widget);
4547 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
4549 UNBLOCK_INPUT;
4551 #endif /* not USE_GTK */
4554 /* Set the thumb size and position of scroll bar BAR. We are currently
4555 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4557 #ifdef USE_GTK
4558 static void
4559 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4560 struct scroll_bar *bar;
4561 int portion, position, whole;
4563 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4566 #else /* not USE_GTK */
4567 static void
4568 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4569 struct scroll_bar *bar;
4570 int portion, position, whole;
4572 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4573 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4574 float top, shown;
4576 BLOCK_INPUT;
4578 #ifdef USE_MOTIF
4580 /* We use an estimate of 30 chars per line rather than the real
4581 `portion' value. This has the disadvantage that the thumb size
4582 is not very representative, but it makes our life a lot easier.
4583 Otherwise, we have to constantly adjust the thumb size, which
4584 we can't always do quickly enough: while dragging, the size of
4585 the thumb might prevent the user from dragging the thumb all the
4586 way to the end. but Motif and some versions of Xaw3d don't allow
4587 updating the thumb size while dragging. Also, even if we can update
4588 its size, the update will often happen too late.
4589 If you don't believe it, check out revision 1.650 of xterm.c to see
4590 what hoops we were going through and the still poor behavior we got. */
4591 portion = XFASTINT (XWINDOW (bar->window)->height) * 30;
4592 /* When the thumb is at the bottom, position == whole.
4593 So we need to increase `whole' to make space for the thumb. */
4594 whole += portion;
4596 if (whole <= 0)
4597 top = 0, shown = 1;
4598 else
4600 top = (float) position / whole;
4601 shown = (float) portion / whole;
4604 if (NILP (bar->dragging))
4606 int size, value;
4608 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4609 is the scroll bar's maximum and MIN is the scroll bar's minimum
4610 value. */
4611 size = shown * XM_SB_MAX;
4612 size = min (size, XM_SB_MAX);
4613 size = max (size, 1);
4615 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4616 value = top * XM_SB_MAX;
4617 value = min (value, XM_SB_MAX - size);
4619 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4621 #else /* !USE_MOTIF i.e. use Xaw */
4623 if (whole == 0)
4624 top = 0, shown = 1;
4625 else
4627 top = (float) position / whole;
4628 shown = (float) portion / whole;
4632 float old_top, old_shown;
4633 Dimension height;
4634 XtVaGetValues (widget,
4635 XtNtopOfThumb, &old_top,
4636 XtNshown, &old_shown,
4637 XtNheight, &height,
4638 NULL);
4640 /* Massage the top+shown values. */
4641 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4642 top = max (0, min (1, top));
4643 else
4644 top = old_top;
4645 /* Keep two pixels available for moving the thumb down. */
4646 shown = max (0, min (1 - top - (2.0 / height), shown));
4648 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4649 check that your system's configuration file contains a define
4650 for `NARROWPROTO'. See s/freebsd.h for an example. */
4651 if (top != old_top || shown != old_shown)
4653 if (NILP (bar->dragging))
4654 XawScrollbarSetThumb (widget, top, shown);
4655 else
4657 #ifdef HAVE_XAW3D
4658 ScrollbarWidget sb = (ScrollbarWidget) widget;
4659 int scroll_mode = 0;
4661 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
4662 if (xaw3d_arrow_scroll)
4664 /* Xaw3d stupidly ignores resize requests while dragging
4665 so we have to make it believe it's not in dragging mode. */
4666 scroll_mode = sb->scrollbar.scroll_mode;
4667 if (scroll_mode == 2)
4668 sb->scrollbar.scroll_mode = 0;
4670 #endif
4671 /* Try to make the scrolling a tad smoother. */
4672 if (!xaw3d_pick_top)
4673 shown = min (shown, old_shown);
4675 XawScrollbarSetThumb (widget, top, shown);
4677 #ifdef HAVE_XAW3D
4678 if (xaw3d_arrow_scroll && scroll_mode == 2)
4679 sb->scrollbar.scroll_mode = scroll_mode;
4680 #endif
4684 #endif /* !USE_MOTIF */
4686 UNBLOCK_INPUT;
4688 #endif /* not USE_GTK */
4690 #endif /* USE_TOOLKIT_SCROLL_BARS */
4694 /************************************************************************
4695 Scroll bars, general
4696 ************************************************************************/
4698 /* Create a scroll bar and return the scroll bar vector for it. W is
4699 the Emacs window on which to create the scroll bar. TOP, LEFT,
4700 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4701 scroll bar. */
4703 static struct scroll_bar *
4704 x_scroll_bar_create (w, top, left, width, height)
4705 struct window *w;
4706 int top, left, width, height;
4708 struct frame *f = XFRAME (w->frame);
4709 struct scroll_bar *bar
4710 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
4712 BLOCK_INPUT;
4714 #ifdef USE_TOOLKIT_SCROLL_BARS
4715 x_create_toolkit_scroll_bar (f, bar);
4716 #else /* not USE_TOOLKIT_SCROLL_BARS */
4718 XSetWindowAttributes a;
4719 unsigned long mask;
4720 Window window;
4722 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4723 if (a.background_pixel == -1)
4724 a.background_pixel = f->output_data.x->background_pixel;
4726 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4727 | ButtonMotionMask | PointerMotionHintMask
4728 | ExposureMask);
4729 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4731 mask = (CWBackPixel | CWEventMask | CWCursor);
4733 /* Clear the area of W that will serve as a scroll bar. This is
4734 for the case that a window has been split horizontally. In
4735 this case, no clear_frame is generated to reduce flickering. */
4736 if (width > 0 && height > 0)
4737 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4738 left, top, width,
4739 window_box_height (w), False);
4741 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4742 /* Position and size of scroll bar. */
4743 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4744 top,
4745 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4746 height,
4747 /* Border width, depth, class, and visual. */
4749 CopyFromParent,
4750 CopyFromParent,
4751 CopyFromParent,
4752 /* Attributes. */
4753 mask, &a);
4754 SET_SCROLL_BAR_X_WINDOW (bar, window);
4756 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4758 XSETWINDOW (bar->window, w);
4759 XSETINT (bar->top, top);
4760 XSETINT (bar->left, left);
4761 XSETINT (bar->width, width);
4762 XSETINT (bar->height, height);
4763 XSETINT (bar->start, 0);
4764 XSETINT (bar->end, 0);
4765 bar->dragging = Qnil;
4767 /* Add bar to its frame's list of scroll bars. */
4768 bar->next = FRAME_SCROLL_BARS (f);
4769 bar->prev = Qnil;
4770 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4771 if (!NILP (bar->next))
4772 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4774 /* Map the window/widget. */
4775 #ifdef USE_TOOLKIT_SCROLL_BARS
4777 #ifdef USE_GTK
4778 xg_update_scrollbar_pos (f,
4779 SCROLL_BAR_X_WINDOW (bar),
4780 top,
4781 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4782 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4783 max (height, 1),
4784 left,
4785 width);
4786 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
4787 #else /* not USE_GTK */
4788 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4789 XtConfigureWidget (scroll_bar,
4790 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4791 top,
4792 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4793 max (height, 1), 0);
4794 XtMapWidget (scroll_bar);
4795 #endif /* not USE_GTK */
4797 #else /* not USE_TOOLKIT_SCROLL_BARS */
4798 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4799 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4801 UNBLOCK_INPUT;
4802 return bar;
4806 /* Draw BAR's handle in the proper position.
4808 If the handle is already drawn from START to END, don't bother
4809 redrawing it, unless REBUILD is non-zero; in that case, always
4810 redraw it. (REBUILD is handy for drawing the handle after expose
4811 events.)
4813 Normally, we want to constrain the start and end of the handle to
4814 fit inside its rectangle, but if the user is dragging the scroll
4815 bar handle, we want to let them drag it down all the way, so that
4816 the bar's top is as far down as it goes; otherwise, there's no way
4817 to move to the very end of the buffer. */
4819 #ifndef USE_TOOLKIT_SCROLL_BARS
4821 static void
4822 x_scroll_bar_set_handle (bar, start, end, rebuild)
4823 struct scroll_bar *bar;
4824 int start, end;
4825 int rebuild;
4827 int dragging = ! NILP (bar->dragging);
4828 Window w = SCROLL_BAR_X_WINDOW (bar);
4829 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4830 GC gc = f->output_data.x->normal_gc;
4832 /* If the display is already accurate, do nothing. */
4833 if (! rebuild
4834 && start == XINT (bar->start)
4835 && end == XINT (bar->end))
4836 return;
4838 BLOCK_INPUT;
4841 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
4842 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
4843 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4845 /* Make sure the values are reasonable, and try to preserve
4846 the distance between start and end. */
4848 int length = end - start;
4850 if (start < 0)
4851 start = 0;
4852 else if (start > top_range)
4853 start = top_range;
4854 end = start + length;
4856 if (end < start)
4857 end = start;
4858 else if (end > top_range && ! dragging)
4859 end = top_range;
4862 /* Store the adjusted setting in the scroll bar. */
4863 XSETINT (bar->start, start);
4864 XSETINT (bar->end, end);
4866 /* Clip the end position, just for display. */
4867 if (end > top_range)
4868 end = top_range;
4870 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
4871 below top positions, to make sure the handle is always at least
4872 that many pixels tall. */
4873 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
4875 /* Draw the empty space above the handle. Note that we can't clear
4876 zero-height areas; that means "clear to end of window." */
4877 if (0 < start)
4878 x_clear_area (FRAME_X_DISPLAY (f), w,
4879 /* x, y, width, height, and exposures. */
4880 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4881 VERTICAL_SCROLL_BAR_TOP_BORDER,
4882 inside_width, start,
4883 False);
4885 /* Change to proper foreground color if one is specified. */
4886 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4887 XSetForeground (FRAME_X_DISPLAY (f), gc,
4888 f->output_data.x->scroll_bar_foreground_pixel);
4890 /* Draw the handle itself. */
4891 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
4892 /* x, y, width, height */
4893 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4894 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
4895 inside_width, end - start);
4897 /* Restore the foreground color of the GC if we changed it above. */
4898 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4899 XSetForeground (FRAME_X_DISPLAY (f), gc,
4900 f->output_data.x->foreground_pixel);
4902 /* Draw the empty space below the handle. Note that we can't
4903 clear zero-height areas; that means "clear to end of window." */
4904 if (end < inside_height)
4905 x_clear_area (FRAME_X_DISPLAY (f), w,
4906 /* x, y, width, height, and exposures. */
4907 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4908 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
4909 inside_width, inside_height - end,
4910 False);
4914 UNBLOCK_INPUT;
4917 #endif /* !USE_TOOLKIT_SCROLL_BARS */
4919 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
4920 nil. */
4922 static void
4923 x_scroll_bar_remove (bar)
4924 struct scroll_bar *bar;
4926 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4927 BLOCK_INPUT;
4929 #ifdef USE_TOOLKIT_SCROLL_BARS
4930 #ifdef USE_GTK
4931 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
4932 #else /* not USE_GTK */
4933 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
4934 #endif /* not USE_GTK */
4935 #else
4936 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4937 #endif
4939 /* Disassociate this scroll bar from its window. */
4940 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
4942 UNBLOCK_INPUT;
4946 /* Set the handle of the vertical scroll bar for WINDOW to indicate
4947 that we are displaying PORTION characters out of a total of WHOLE
4948 characters, starting at POSITION. If WINDOW has no scroll bar,
4949 create one. */
4951 static void
4952 XTset_vertical_scroll_bar (w, portion, whole, position)
4953 struct window *w;
4954 int portion, whole, position;
4956 struct frame *f = XFRAME (w->frame);
4957 struct scroll_bar *bar;
4958 int top, height, left, sb_left, width, sb_width;
4959 int window_x, window_y, window_width, window_height;
4961 /* Get window dimensions. */
4962 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
4963 top = window_y;
4964 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
4965 height = window_height;
4967 /* Compute the left edge of the scroll bar area. */
4968 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4969 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
4970 else
4971 left = XFASTINT (w->left);
4972 left *= CANON_X_UNIT (f);
4973 left += FRAME_INTERNAL_BORDER_WIDTH (f);
4975 /* Compute the width of the scroll bar which might be less than
4976 the width of the area reserved for the scroll bar. */
4977 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
4978 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
4979 else
4980 sb_width = width;
4982 /* Compute the left edge of the scroll bar. */
4983 #ifdef USE_TOOLKIT_SCROLL_BARS
4984 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4985 sb_left = left + width - sb_width - (width - sb_width) / 2;
4986 else
4987 sb_left = left + (width - sb_width) / 2;
4988 #else
4989 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4990 sb_left = left + width - sb_width;
4991 else
4992 sb_left = left;
4993 #endif
4995 /* Does the scroll bar exist yet? */
4996 if (NILP (w->vertical_scroll_bar))
4998 if (width > 0 && height > 0)
5000 BLOCK_INPUT;
5001 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5002 left, top, width, height, False);
5003 UNBLOCK_INPUT;
5006 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5008 else
5010 /* It may just need to be moved and resized. */
5011 unsigned int mask = 0;
5013 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5015 BLOCK_INPUT;
5017 if (sb_left != XINT (bar->left))
5018 mask |= CWX;
5019 if (top != XINT (bar->top))
5020 mask |= CWY;
5021 if (sb_width != XINT (bar->width))
5022 mask |= CWWidth;
5023 if (height != XINT (bar->height))
5024 mask |= CWHeight;
5026 #ifdef USE_TOOLKIT_SCROLL_BARS
5028 #ifdef USE_GTK
5029 if (mask)
5030 xg_update_scrollbar_pos (f,
5031 SCROLL_BAR_X_WINDOW (bar),
5032 top,
5033 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5034 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5035 max (height, 1),
5036 left,
5037 width);
5038 #else /* not USE_GTK */
5040 /* Since toolkit scroll bars are smaller than the space reserved
5041 for them on the frame, we have to clear "under" them. */
5042 if (width > 0 && height > 0)
5043 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5044 left, top, width, height, False);
5045 /* Move/size the scroll bar widget. */
5046 if (mask)
5047 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5048 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5049 top,
5050 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5051 max (height, 1), 0);
5053 #endif /* not USE_GTK */
5054 #else /* not USE_TOOLKIT_SCROLL_BARS */
5056 /* Clear areas not covered by the scroll bar because of
5057 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5058 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5060 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5061 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5062 height, False);
5063 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5064 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5065 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5066 height, False);
5069 /* Clear areas not covered by the scroll bar because it's not as
5070 wide as the area reserved for it. This makes sure a
5071 previous mode line display is cleared after C-x 2 C-x 1, for
5072 example. */
5074 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
5075 int rest = area_width - sb_width;
5076 if (rest > 0 && height > 0)
5078 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
5079 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5080 left + area_width - rest, top,
5081 rest, height, False);
5082 else
5083 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5084 left, top, rest, height, False);
5088 /* Move/size the scroll bar window. */
5089 if (mask)
5091 XWindowChanges wc;
5093 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5094 wc.y = top;
5095 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5096 wc.height = height;
5097 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
5098 mask, &wc);
5101 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5103 /* Remember new settings. */
5104 XSETINT (bar->left, sb_left);
5105 XSETINT (bar->top, top);
5106 XSETINT (bar->width, sb_width);
5107 XSETINT (bar->height, height);
5109 UNBLOCK_INPUT;
5112 #ifdef USE_TOOLKIT_SCROLL_BARS
5113 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5114 #else /* not USE_TOOLKIT_SCROLL_BARS */
5115 /* Set the scroll bar's current state, unless we're currently being
5116 dragged. */
5117 if (NILP (bar->dragging))
5119 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5121 if (whole == 0)
5122 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5123 else
5125 int start = ((double) position * top_range) / whole;
5126 int end = ((double) (position + portion) * top_range) / whole;
5127 x_scroll_bar_set_handle (bar, start, end, 0);
5130 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5132 XSETVECTOR (w->vertical_scroll_bar, bar);
5136 /* The following three hooks are used when we're doing a thorough
5137 redisplay of the frame. We don't explicitly know which scroll bars
5138 are going to be deleted, because keeping track of when windows go
5139 away is a real pain - "Can you say set-window-configuration, boys
5140 and girls?" Instead, we just assert at the beginning of redisplay
5141 that *all* scroll bars are to be removed, and then save a scroll bar
5142 from the fiery pit when we actually redisplay its window. */
5144 /* Arrange for all scroll bars on FRAME to be removed at the next call
5145 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5146 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5148 static void
5149 XTcondemn_scroll_bars (frame)
5150 FRAME_PTR frame;
5152 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5153 while (! NILP (FRAME_SCROLL_BARS (frame)))
5155 Lisp_Object bar;
5156 bar = FRAME_SCROLL_BARS (frame);
5157 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5158 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5159 XSCROLL_BAR (bar)->prev = Qnil;
5160 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5161 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5162 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5167 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5168 Note that WINDOW isn't necessarily condemned at all. */
5170 static void
5171 XTredeem_scroll_bar (window)
5172 struct window *window;
5174 struct scroll_bar *bar;
5175 struct frame *f;
5177 /* We can't redeem this window's scroll bar if it doesn't have one. */
5178 if (NILP (window->vertical_scroll_bar))
5179 abort ();
5181 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5183 /* Unlink it from the condemned list. */
5184 f = XFRAME (WINDOW_FRAME (window));
5185 if (NILP (bar->prev))
5187 /* If the prev pointer is nil, it must be the first in one of
5188 the lists. */
5189 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5190 /* It's not condemned. Everything's fine. */
5191 return;
5192 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5193 window->vertical_scroll_bar))
5194 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5195 else
5196 /* If its prev pointer is nil, it must be at the front of
5197 one or the other! */
5198 abort ();
5200 else
5201 XSCROLL_BAR (bar->prev)->next = bar->next;
5203 if (! NILP (bar->next))
5204 XSCROLL_BAR (bar->next)->prev = bar->prev;
5206 bar->next = FRAME_SCROLL_BARS (f);
5207 bar->prev = Qnil;
5208 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5209 if (! NILP (bar->next))
5210 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5213 /* Remove all scroll bars on FRAME that haven't been saved since the
5214 last call to `*condemn_scroll_bars_hook'. */
5216 static void
5217 XTjudge_scroll_bars (f)
5218 FRAME_PTR f;
5220 Lisp_Object bar, next;
5222 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5224 /* Clear out the condemned list now so we won't try to process any
5225 more events on the hapless scroll bars. */
5226 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5228 for (; ! NILP (bar); bar = next)
5230 struct scroll_bar *b = XSCROLL_BAR (bar);
5232 x_scroll_bar_remove (b);
5234 next = b->next;
5235 b->next = b->prev = Qnil;
5238 /* Now there should be no references to the condemned scroll bars,
5239 and they should get garbage-collected. */
5243 #ifndef USE_TOOLKIT_SCROLL_BARS
5244 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5245 is a no-op when using toolkit scroll bars.
5247 This may be called from a signal handler, so we have to ignore GC
5248 mark bits. */
5250 static void
5251 x_scroll_bar_expose (bar, event)
5252 struct scroll_bar *bar;
5253 XEvent *event;
5255 Window w = SCROLL_BAR_X_WINDOW (bar);
5256 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5257 GC gc = f->output_data.x->normal_gc;
5258 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5260 BLOCK_INPUT;
5262 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
5264 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5265 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5267 /* x, y, width, height */
5268 0, 0,
5269 XINT (bar->width) - 1 - width_trim - width_trim,
5270 XINT (bar->height) - 1);
5272 UNBLOCK_INPUT;
5275 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5277 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5278 is set to something other than NO_EVENT, it is enqueued.
5280 This may be called from a signal handler, so we have to ignore GC
5281 mark bits. */
5284 static void
5285 x_scroll_bar_handle_click (bar, event, emacs_event)
5286 struct scroll_bar *bar;
5287 XEvent *event;
5288 struct input_event *emacs_event;
5290 if (! GC_WINDOWP (bar->window))
5291 abort ();
5293 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5294 emacs_event->code = event->xbutton.button - Button1;
5295 emacs_event->modifiers
5296 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5297 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5298 event->xbutton.state)
5299 | (event->type == ButtonRelease
5300 ? up_modifier
5301 : down_modifier));
5302 emacs_event->frame_or_window = bar->window;
5303 emacs_event->arg = Qnil;
5304 emacs_event->timestamp = event->xbutton.time;
5306 #if 0
5307 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5308 int internal_height
5309 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5310 #endif
5311 int top_range
5312 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5313 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5315 if (y < 0) y = 0;
5316 if (y > top_range) y = top_range;
5318 if (y < XINT (bar->start))
5319 emacs_event->part = scroll_bar_above_handle;
5320 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5321 emacs_event->part = scroll_bar_handle;
5322 else
5323 emacs_event->part = scroll_bar_below_handle;
5325 /* Just because the user has clicked on the handle doesn't mean
5326 they want to drag it. Lisp code needs to be able to decide
5327 whether or not we're dragging. */
5328 #if 0
5329 /* If the user has just clicked on the handle, record where they're
5330 holding it. */
5331 if (event->type == ButtonPress
5332 && emacs_event->part == scroll_bar_handle)
5333 XSETINT (bar->dragging, y - XINT (bar->start));
5334 #endif
5336 #ifndef USE_TOOLKIT_SCROLL_BARS
5337 /* If the user has released the handle, set it to its final position. */
5338 if (event->type == ButtonRelease
5339 && ! NILP (bar->dragging))
5341 int new_start = y - XINT (bar->dragging);
5342 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5344 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5345 bar->dragging = Qnil;
5347 #endif
5349 /* Same deal here as the other #if 0. */
5350 #if 0
5351 /* Clicks on the handle are always reported as occurring at the top of
5352 the handle. */
5353 if (emacs_event->part == scroll_bar_handle)
5354 emacs_event->x = bar->start;
5355 else
5356 XSETINT (emacs_event->x, y);
5357 #else
5358 XSETINT (emacs_event->x, y);
5359 #endif
5361 XSETINT (emacs_event->y, top_range);
5365 #ifndef USE_TOOLKIT_SCROLL_BARS
5367 /* Handle some mouse motion while someone is dragging the scroll bar.
5369 This may be called from a signal handler, so we have to ignore GC
5370 mark bits. */
5372 static void
5373 x_scroll_bar_note_movement (bar, event)
5374 struct scroll_bar *bar;
5375 XEvent *event;
5377 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5379 last_mouse_movement_time = event->xmotion.time;
5381 f->mouse_moved = 1;
5382 XSETVECTOR (last_mouse_scroll_bar, bar);
5384 /* If we're dragging the bar, display it. */
5385 if (! GC_NILP (bar->dragging))
5387 /* Where should the handle be now? */
5388 int new_start = event->xmotion.y - XINT (bar->dragging);
5390 if (new_start != XINT (bar->start))
5392 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5394 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5399 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5401 /* Return information to the user about the current position of the mouse
5402 on the scroll bar. */
5404 static void
5405 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5406 FRAME_PTR *fp;
5407 Lisp_Object *bar_window;
5408 enum scroll_bar_part *part;
5409 Lisp_Object *x, *y;
5410 unsigned long *time;
5412 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5413 Window w = SCROLL_BAR_X_WINDOW (bar);
5414 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5415 int win_x, win_y;
5416 Window dummy_window;
5417 int dummy_coord;
5418 unsigned int dummy_mask;
5420 BLOCK_INPUT;
5422 /* Get the mouse's position relative to the scroll bar window, and
5423 report that. */
5424 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5426 /* Root, child, root x and root y. */
5427 &dummy_window, &dummy_window,
5428 &dummy_coord, &dummy_coord,
5430 /* Position relative to scroll bar. */
5431 &win_x, &win_y,
5433 /* Mouse buttons and modifier keys. */
5434 &dummy_mask))
5436 else
5438 #if 0
5439 int inside_height
5440 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5441 #endif
5442 int top_range
5443 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5445 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5447 if (! NILP (bar->dragging))
5448 win_y -= XINT (bar->dragging);
5450 if (win_y < 0)
5451 win_y = 0;
5452 if (win_y > top_range)
5453 win_y = top_range;
5455 *fp = f;
5456 *bar_window = bar->window;
5458 if (! NILP (bar->dragging))
5459 *part = scroll_bar_handle;
5460 else if (win_y < XINT (bar->start))
5461 *part = scroll_bar_above_handle;
5462 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5463 *part = scroll_bar_handle;
5464 else
5465 *part = scroll_bar_below_handle;
5467 XSETINT (*x, win_y);
5468 XSETINT (*y, top_range);
5470 f->mouse_moved = 0;
5471 last_mouse_scroll_bar = Qnil;
5474 *time = last_mouse_movement_time;
5476 UNBLOCK_INPUT;
5480 /* The screen has been cleared so we may have changed foreground or
5481 background colors, and the scroll bars may need to be redrawn.
5482 Clear out the scroll bars, and ask for expose events, so we can
5483 redraw them. */
5485 void
5486 x_scroll_bar_clear (f)
5487 FRAME_PTR f;
5489 #ifndef USE_TOOLKIT_SCROLL_BARS
5490 Lisp_Object bar;
5492 /* We can have scroll bars even if this is 0,
5493 if we just turned off scroll bar mode.
5494 But in that case we should not clear them. */
5495 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5496 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5497 bar = XSCROLL_BAR (bar)->next)
5498 XClearArea (FRAME_X_DISPLAY (f),
5499 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
5500 0, 0, 0, 0, True);
5501 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5505 /* Define a queue to save up SelectionRequest events for later handling. */
5507 struct selection_event_queue
5509 XEvent event;
5510 struct selection_event_queue *next;
5513 static struct selection_event_queue *queue;
5515 /* Nonzero means queue up certain events--don't process them yet. */
5517 static int x_queue_selection_requests;
5519 /* Queue up an X event *EVENT, to be processed later. */
5521 static void
5522 x_queue_event (f, event)
5523 FRAME_PTR f;
5524 XEvent *event;
5526 struct selection_event_queue *queue_tmp
5527 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
5529 if (queue_tmp != NULL)
5531 queue_tmp->event = *event;
5532 queue_tmp->next = queue;
5533 queue = queue_tmp;
5537 /* Take all the queued events and put them back
5538 so that they get processed afresh. */
5540 static void
5541 x_unqueue_events (display)
5542 Display *display;
5544 while (queue != NULL)
5546 struct selection_event_queue *queue_tmp = queue;
5547 XPutBackEvent (display, &queue_tmp->event);
5548 queue = queue_tmp->next;
5549 xfree ((char *)queue_tmp);
5553 /* Start queuing SelectionRequest events. */
5555 void
5556 x_start_queuing_selection_requests (display)
5557 Display *display;
5559 x_queue_selection_requests++;
5562 /* Stop queuing SelectionRequest events. */
5564 void
5565 x_stop_queuing_selection_requests (display)
5566 Display *display;
5568 x_queue_selection_requests--;
5569 x_unqueue_events (display);
5572 /* The main X event-reading loop - XTread_socket. */
5574 #if 0
5575 /* Time stamp of enter window event. This is only used by XTread_socket,
5576 but we have to put it out here, since static variables within functions
5577 sometimes don't work. */
5579 static Time enter_timestamp;
5580 #endif
5582 /* This holds the state XLookupString needs to implement dead keys
5583 and other tricks known as "compose processing". _X Window System_
5584 says that a portable program can't use this, but Stephen Gildea assures
5585 me that letting the compiler initialize it to zeros will work okay.
5587 This must be defined outside of XTread_socket, for the same reasons
5588 given for enter_timestamp, above. */
5590 static XComposeStatus compose_status;
5592 /* Record the last 100 characters stored
5593 to help debug the loss-of-chars-during-GC problem. */
5595 static int temp_index;
5596 static short temp_buffer[100];
5598 /* Set this to nonzero to fake an "X I/O error"
5599 on a particular display. */
5601 struct x_display_info *XTread_socket_fake_io_error;
5603 /* When we find no input here, we occasionally do a no-op command
5604 to verify that the X server is still running and we can still talk with it.
5605 We try all the open displays, one by one.
5606 This variable is used for cycling thru the displays. */
5608 static struct x_display_info *next_noop_dpyinfo;
5610 #define SET_SAVED_MENU_EVENT(size) \
5611 do \
5613 if (f->output_data.x->saved_menu_event == 0) \
5614 f->output_data.x->saved_menu_event \
5615 = (XEvent *) xmalloc (sizeof (XEvent)); \
5616 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5617 if (numchars >= 1) \
5619 bufp->kind = MENU_BAR_ACTIVATE_EVENT; \
5620 XSETFRAME (bufp->frame_or_window, f); \
5621 bufp->arg = Qnil; \
5622 bufp++; \
5623 count++; \
5624 numchars--; \
5627 while (0)
5629 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5630 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5633 enum
5635 X_EVENT_NORMAL,
5636 X_EVENT_GOTO_OUT,
5637 X_EVENT_DROP
5640 /* Filter events for the current X input method.
5641 DPYINFO is the display this event is for.
5642 EVENT is the X event to filter.
5644 Returns non-zero if the event was filtered, caller shall not process
5645 this event further.
5646 Returns zero if event is wasn't filtered. */
5648 #ifdef HAVE_X_I18N
5649 static int
5650 x_filter_event (dpyinfo, event)
5651 struct x_display_info *dpyinfo;
5652 XEvent *event;
5654 /* XFilterEvent returns non-zero if the input method has
5655 consumed the event. We pass the frame's X window to
5656 XFilterEvent because that's the one for which the IC
5657 was created. */
5659 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5660 event->xclient.window);
5662 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5664 #endif
5666 #ifdef USE_GTK
5667 static struct x_display_info *current_dpyinfo;
5668 static struct input_event **current_bufp;
5669 static int *current_numcharsp;
5670 static int current_count;
5671 static int current_finish;
5673 /* This is the filter function invoked by the GTK event loop.
5674 It is invoked before the XEvent is translated to a GdkEvent,
5675 so we have a chanse to act on the event before GTK. */
5676 static GdkFilterReturn
5677 event_handler_gdk (gxev, ev, data)
5678 GdkXEvent *gxev;
5679 GdkEvent *ev;
5680 gpointer data;
5682 XEvent *xev = (XEvent*)gxev;
5684 if (current_numcharsp)
5686 #ifdef HAVE_X_I18N
5687 /* Filter events for the current X input method.
5688 GTK calls XFilterEvent but not for key press and release,
5689 so we do it here. */
5690 if (xev->type == KeyPress || xev->type == KeyRelease)
5691 if (x_filter_event (current_dpyinfo, xev))
5692 return GDK_FILTER_REMOVE;
5693 #endif
5694 current_count += handle_one_xevent (current_dpyinfo,
5695 xev,
5696 current_bufp,
5697 current_numcharsp,
5698 &current_finish);
5700 else
5701 current_finish = x_dispatch_event (xev, GDK_DISPLAY ());
5703 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5704 return GDK_FILTER_REMOVE;
5706 return GDK_FILTER_CONTINUE;
5708 #endif /* USE_GTK */
5711 /* Handles the XEvent EVENT on display DPYINFO.
5713 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5714 *FINISH is zero if caller should continue reading events.
5715 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5717 Events representing keys are stored in buffer *BUFP_R,
5718 which can hold up to *NUMCHARSP characters.
5719 We return the number of characters stored into the buffer. */
5721 static int
5722 handle_one_xevent (dpyinfo, eventp, bufp_r, numcharsp, finish)
5723 struct x_display_info *dpyinfo;
5724 XEvent *eventp;
5725 /* register */ struct input_event **bufp_r;
5726 /* register */ int *numcharsp;
5727 int *finish;
5729 int count = 0;
5730 int nbytes = 0;
5731 struct frame *f;
5732 struct coding_system coding;
5733 struct input_event *bufp = *bufp_r;
5734 int numchars = *numcharsp;
5735 XEvent event = *eventp;
5737 *finish = X_EVENT_NORMAL;
5739 switch (event.type)
5741 case ClientMessage:
5743 if (event.xclient.message_type
5744 == dpyinfo->Xatom_wm_protocols
5745 && event.xclient.format == 32)
5747 if (event.xclient.data.l[0]
5748 == dpyinfo->Xatom_wm_take_focus)
5750 /* Use x_any_window_to_frame because this
5751 could be the shell widget window
5752 if the frame has no title bar. */
5753 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5754 #ifdef HAVE_X_I18N
5755 /* Not quite sure this is needed -pd */
5756 if (f && FRAME_XIC (f))
5757 XSetICFocus (FRAME_XIC (f));
5758 #endif
5759 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5760 instructs the WM to set the input focus automatically for
5761 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5762 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5763 it has set the focus. So, XSetInputFocus below is not
5764 needed.
5766 The call to XSetInputFocus below has also caused trouble. In
5767 cases where the XSetInputFocus done by the WM and the one
5768 below are temporally close (on a fast machine), the call
5769 below can generate additional FocusIn events which confuse
5770 Emacs. */
5772 /* Since we set WM_TAKE_FOCUS, we must call
5773 XSetInputFocus explicitly. But not if f is null,
5774 since that might be an event for a deleted frame. */
5775 if (f)
5777 Display *d = event.xclient.display;
5778 /* Catch and ignore errors, in case window has been
5779 iconified by a window manager such as GWM. */
5780 int count = x_catch_errors (d);
5781 XSetInputFocus (d, event.xclient.window,
5782 /* The ICCCM says this is
5783 the only valid choice. */
5784 RevertToParent,
5785 event.xclient.data.l[1]);
5786 /* This is needed to detect the error
5787 if there is an error. */
5788 XSync (d, False);
5789 x_uncatch_errors (d, count);
5791 /* Not certain about handling scroll bars here */
5792 #endif /* 0 */
5794 else if (event.xclient.data.l[0]
5795 == dpyinfo->Xatom_wm_save_yourself)
5797 /* Save state modify the WM_COMMAND property to
5798 something which can reinstate us. This notifies
5799 the session manager, who's looking for such a
5800 PropertyNotify. Can restart processing when
5801 a keyboard or mouse event arrives. */
5802 /* If we have a session manager, don't set this.
5803 KDE will then start two Emacsen, one for the
5804 session manager and one for this. */
5805 if (numchars > 0
5806 #ifdef HAVE_X_SM
5807 && ! x_session_have_connection ()
5808 #endif
5811 f = x_top_window_to_frame (dpyinfo,
5812 event.xclient.window);
5813 /* This is just so we only give real data once
5814 for a single Emacs process. */
5815 if (f == SELECTED_FRAME ())
5816 XSetCommand (FRAME_X_DISPLAY (f),
5817 event.xclient.window,
5818 initial_argv, initial_argc);
5819 else if (f)
5820 XSetCommand (FRAME_X_DISPLAY (f),
5821 event.xclient.window,
5822 0, 0);
5825 else if (event.xclient.data.l[0]
5826 == dpyinfo->Xatom_wm_delete_window)
5828 struct frame *f
5829 = x_any_window_to_frame (dpyinfo,
5830 event.xclient.window);
5832 if (f)
5834 if (numchars == 0)
5835 abort ();
5837 bufp->kind = DELETE_WINDOW_EVENT;
5838 XSETFRAME (bufp->frame_or_window, f);
5839 bufp->arg = Qnil;
5840 bufp++;
5842 count += 1;
5843 numchars -= 1;
5845 else
5846 goto OTHER; /* May be a dialog that is to be removed */
5849 else if (event.xclient.message_type
5850 == dpyinfo->Xatom_wm_configure_denied)
5853 else if (event.xclient.message_type
5854 == dpyinfo->Xatom_wm_window_moved)
5856 int new_x, new_y;
5857 struct frame *f
5858 = x_window_to_frame (dpyinfo, event.xclient.window);
5860 new_x = event.xclient.data.s[0];
5861 new_y = event.xclient.data.s[1];
5863 if (f)
5865 f->output_data.x->left_pos = new_x;
5866 f->output_data.x->top_pos = new_y;
5869 #ifdef HACK_EDITRES
5870 else if (event.xclient.message_type
5871 == dpyinfo->Xatom_editres)
5873 struct frame *f
5874 = x_any_window_to_frame (dpyinfo, event.xclient.window);
5875 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5876 &event, NULL);
5878 #endif /* HACK_EDITRES */
5879 else if ((event.xclient.message_type
5880 == dpyinfo->Xatom_DONE)
5881 || (event.xclient.message_type
5882 == dpyinfo->Xatom_PAGE))
5884 /* Ghostview job completed. Kill it. We could
5885 reply with "Next" if we received "Page", but we
5886 currently never do because we are interested in
5887 images, only, which should have 1 page. */
5888 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5889 struct frame *f
5890 = x_window_to_frame (dpyinfo, event.xclient.window);
5891 x_kill_gs_process (pixmap, f);
5892 expose_frame (f, 0, 0, 0, 0);
5894 #ifdef USE_TOOLKIT_SCROLL_BARS
5895 /* Scroll bar callbacks send a ClientMessage from which
5896 we construct an input_event. */
5897 else if (event.xclient.message_type
5898 == dpyinfo->Xatom_Scrollbar)
5900 x_scroll_bar_to_input_event (&event, bufp);
5901 ++bufp, ++count, --numchars;
5902 goto out;
5904 #endif /* USE_TOOLKIT_SCROLL_BARS */
5905 else
5906 goto OTHER;
5908 break;
5910 case SelectionNotify:
5911 #ifdef USE_X_TOOLKIT
5912 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
5913 goto OTHER;
5914 #endif /* not USE_X_TOOLKIT */
5915 x_handle_selection_notify (&event.xselection);
5916 break;
5918 case SelectionClear: /* Someone has grabbed ownership. */
5919 #ifdef USE_X_TOOLKIT
5920 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
5921 goto OTHER;
5922 #endif /* USE_X_TOOLKIT */
5924 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
5926 if (numchars == 0)
5927 abort ();
5929 bufp->kind = SELECTION_CLEAR_EVENT;
5930 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
5931 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
5932 SELECTION_EVENT_TIME (bufp) = eventp->time;
5933 bufp->frame_or_window = Qnil;
5934 bufp->arg = Qnil;
5935 bufp++;
5937 count += 1;
5938 numchars -= 1;
5940 break;
5942 case SelectionRequest: /* Someone wants our selection. */
5943 #ifdef USE_X_TOOLKIT
5944 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
5945 goto OTHER;
5946 #endif /* USE_X_TOOLKIT */
5947 if (x_queue_selection_requests)
5948 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
5949 &event);
5950 else
5952 XSelectionRequestEvent *eventp
5953 = (XSelectionRequestEvent *) &event;
5955 if (numchars == 0)
5956 abort ();
5958 bufp->kind = SELECTION_REQUEST_EVENT;
5959 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
5960 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
5961 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
5962 SELECTION_EVENT_TARGET (bufp) = eventp->target;
5963 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
5964 SELECTION_EVENT_TIME (bufp) = eventp->time;
5965 bufp->frame_or_window = Qnil;
5966 bufp->arg = Qnil;
5967 bufp++;
5969 count += 1;
5970 numchars -= 1;
5972 break;
5974 case PropertyNotify:
5975 #if 0 /* This is plain wrong. In the case that we are waiting for a
5976 PropertyNotify used as an ACK in incremental selection
5977 transfer, the property will be on the receiver's window. */
5978 #if defined USE_X_TOOLKIT
5979 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
5980 goto OTHER;
5981 #endif
5982 #endif
5983 x_handle_property_notify (&event.xproperty);
5984 goto OTHER;
5986 case ReparentNotify:
5987 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
5988 if (f)
5990 int x, y;
5991 f->output_data.x->parent_desc = event.xreparent.parent;
5992 x_real_positions (f, &x, &y);
5993 f->output_data.x->left_pos = x;
5994 f->output_data.x->top_pos = y;
5996 goto OTHER;
5997 break;
5999 case Expose:
6000 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6001 if (f)
6003 x_check_fullscreen (f);
6005 if (f->async_visible == 0)
6007 f->async_visible = 1;
6008 f->async_iconified = 0;
6009 f->output_data.x->has_been_visible = 1;
6010 SET_FRAME_GARBAGED (f);
6012 else
6013 expose_frame (x_window_to_frame (dpyinfo,
6014 event.xexpose.window),
6015 event.xexpose.x, event.xexpose.y,
6016 event.xexpose.width, event.xexpose.height);
6018 else
6020 #ifndef USE_TOOLKIT_SCROLL_BARS
6021 struct scroll_bar *bar;
6022 #endif
6023 #if defined USE_LUCID
6024 /* Submenus of the Lucid menu bar aren't widgets
6025 themselves, so there's no way to dispatch events
6026 to them. Recognize this case separately. */
6028 Widget widget
6029 = x_window_to_menu_bar (event.xexpose.window);
6030 if (widget)
6031 xlwmenu_redisplay (widget);
6033 #endif /* USE_LUCID */
6035 #ifdef USE_TOOLKIT_SCROLL_BARS
6036 /* Dispatch event to the widget. */
6037 goto OTHER;
6038 #else /* not USE_TOOLKIT_SCROLL_BARS */
6039 bar = x_window_to_scroll_bar (event.xexpose.window);
6041 if (bar)
6042 x_scroll_bar_expose (bar, &event);
6043 #ifdef USE_X_TOOLKIT
6044 else
6045 goto OTHER;
6046 #endif /* USE_X_TOOLKIT */
6047 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6049 break;
6051 case GraphicsExpose: /* This occurs when an XCopyArea's
6052 source area was obscured or not
6053 available. */
6054 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6055 if (f)
6057 expose_frame (f,
6058 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6059 event.xgraphicsexpose.width,
6060 event.xgraphicsexpose.height);
6062 #ifdef USE_X_TOOLKIT
6063 else
6064 goto OTHER;
6065 #endif /* USE_X_TOOLKIT */
6066 break;
6068 case NoExpose: /* This occurs when an XCopyArea's
6069 source area was completely
6070 available. */
6071 break;
6073 case UnmapNotify:
6074 /* Redo the mouse-highlight after the tooltip has gone. */
6075 if (event.xmap.window == tip_window)
6077 tip_window = 0;
6078 redo_mouse_highlight ();
6081 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6082 if (f) /* F may no longer exist if
6083 the frame was deleted. */
6085 /* While a frame is unmapped, display generation is
6086 disabled; you don't want to spend time updating a
6087 display that won't ever be seen. */
6088 f->async_visible = 0;
6089 /* We can't distinguish, from the event, whether the window
6090 has become iconified or invisible. So assume, if it
6091 was previously visible, than now it is iconified.
6092 But x_make_frame_invisible clears both
6093 the visible flag and the iconified flag;
6094 and that way, we know the window is not iconified now. */
6095 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6097 f->async_iconified = 1;
6099 bufp->kind = ICONIFY_EVENT;
6100 XSETFRAME (bufp->frame_or_window, f);
6101 bufp->arg = Qnil;
6102 bufp++;
6103 count++;
6104 numchars--;
6107 goto OTHER;
6109 case MapNotify:
6110 if (event.xmap.window == tip_window)
6111 /* The tooltip has been drawn already. Avoid
6112 the SET_FRAME_GARBAGED below. */
6113 goto OTHER;
6115 /* We use x_top_window_to_frame because map events can
6116 come for sub-windows and they don't mean that the
6117 frame is visible. */
6118 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6119 if (f)
6121 /* wait_reading_process_input will notice this and update
6122 the frame's display structures.
6123 If we where iconified, we should not set garbaged,
6124 because that stops redrawing on Expose events. This looks
6125 bad if we are called from a recursive event loop
6126 (x_dispatch_event), for example when a dialog is up. */
6127 if (! f->async_iconified)
6128 SET_FRAME_GARBAGED (f);
6130 f->async_visible = 1;
6131 f->async_iconified = 0;
6132 f->output_data.x->has_been_visible = 1;
6134 if (f->iconified)
6136 bufp->kind = DEICONIFY_EVENT;
6137 XSETFRAME (bufp->frame_or_window, f);
6138 bufp->arg = Qnil;
6139 bufp++;
6140 count++;
6141 numchars--;
6143 else if (! NILP (Vframe_list)
6144 && ! NILP (XCDR (Vframe_list)))
6145 /* Force a redisplay sooner or later
6146 to update the frame titles
6147 in case this is the second frame. */
6148 record_asynch_buffer_change ();
6150 goto OTHER;
6152 case KeyPress:
6154 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6155 /* Dispatch KeyPress events when in menu. */
6156 if (popup_activated ())
6157 goto OTHER;
6158 #endif
6160 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6162 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
6164 dpyinfo->mouse_face_hidden = 1;
6165 clear_mouse_face (dpyinfo);
6168 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6169 if (f == 0)
6171 /* Scroll bars consume key events, but we want
6172 the keys to go to the scroll bar's frame. */
6173 Widget widget = XtWindowToWidget (dpyinfo->display,
6174 event.xkey.window);
6175 if (widget && XmIsScrollBar (widget))
6177 widget = XtParent (widget);
6178 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6181 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6183 if (f != 0)
6185 KeySym keysym, orig_keysym;
6186 /* al%imercury@uunet.uu.net says that making this 81
6187 instead of 80 fixed a bug whereby meta chars made
6188 his Emacs hang.
6190 It seems that some version of XmbLookupString has
6191 a bug of not returning XBufferOverflow in
6192 status_return even if the input is too long to
6193 fit in 81 bytes. So, we must prepare sufficient
6194 bytes for copy_buffer. 513 bytes (256 chars for
6195 two-byte character set) seems to be a fairly good
6196 approximation. -- 2000.8.10 handa@etl.go.jp */
6197 unsigned char copy_buffer[513];
6198 unsigned char *copy_bufptr = copy_buffer;
6199 int copy_bufsiz = sizeof (copy_buffer);
6200 int modifiers;
6201 Lisp_Object coding_system = Qlatin_1;
6203 event.xkey.state
6204 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6205 extra_keyboard_modifiers);
6206 modifiers = event.xkey.state;
6208 /* This will have to go some day... */
6210 /* make_lispy_event turns chars into control chars.
6211 Don't do it here because XLookupString is too eager. */
6212 event.xkey.state &= ~ControlMask;
6213 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6214 | dpyinfo->super_mod_mask
6215 | dpyinfo->hyper_mod_mask
6216 | dpyinfo->alt_mod_mask);
6218 /* In case Meta is ComposeCharacter,
6219 clear its status. According to Markus Ehrnsperger
6220 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6221 this enables ComposeCharacter to work whether or
6222 not it is combined with Meta. */
6223 if (modifiers & dpyinfo->meta_mod_mask)
6224 bzero (&compose_status, sizeof (compose_status));
6226 #ifdef HAVE_X_I18N
6227 if (FRAME_XIC (f))
6229 Status status_return;
6231 coding_system = Vlocale_coding_system;
6232 nbytes = XmbLookupString (FRAME_XIC (f),
6233 &event.xkey, copy_bufptr,
6234 copy_bufsiz, &keysym,
6235 &status_return);
6236 if (status_return == XBufferOverflow)
6238 copy_bufsiz = nbytes + 1;
6239 copy_bufptr = (char *) alloca (copy_bufsiz);
6240 nbytes = XmbLookupString (FRAME_XIC (f),
6241 &event.xkey, copy_bufptr,
6242 copy_bufsiz, &keysym,
6243 &status_return);
6245 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6246 #if 0 && defined X_HAVE_UTF8_STRING
6247 else if (status_return == XLookupKeySym)
6248 { /* Try again but with utf-8. */
6249 coding_system = Qutf_8;
6250 nbytes = Xutf8LookupString (FRAME_XIC (f),
6251 &event.xkey, copy_bufptr,
6252 copy_bufsiz, &keysym,
6253 &status_return);
6254 if (status_return == XBufferOverflow)
6256 copy_bufsiz = nbytes + 1;
6257 copy_bufptr = (char *) alloca (copy_bufsiz);
6258 nbytes = Xutf8LookupString (FRAME_XIC (f),
6259 &event.xkey,
6260 copy_bufptr,
6261 copy_bufsiz, &keysym,
6262 &status_return);
6265 #endif
6267 if (status_return == XLookupNone)
6268 break;
6269 else if (status_return == XLookupChars)
6271 keysym = NoSymbol;
6272 modifiers = 0;
6274 else if (status_return != XLookupKeySym
6275 && status_return != XLookupBoth)
6276 abort ();
6278 else
6279 nbytes = XLookupString (&event.xkey, copy_bufptr,
6280 copy_bufsiz, &keysym,
6281 &compose_status);
6282 #else
6283 nbytes = XLookupString (&event.xkey, copy_bufptr,
6284 copy_bufsiz, &keysym,
6285 &compose_status);
6286 #endif
6288 orig_keysym = keysym;
6290 if (numchars > 1)
6292 Lisp_Object c;
6294 /* First deal with keysyms which have defined
6295 translations to characters. */
6296 if (keysym >= 32 && keysym < 128)
6297 /* Avoid explicitly decoding each ASCII character. */
6299 bufp->kind = ASCII_KEYSTROKE_EVENT;
6300 bufp->code = keysym;
6301 XSETFRAME (bufp->frame_or_window, f);
6302 bufp->arg = Qnil;
6303 bufp->modifiers
6304 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6305 modifiers);
6306 bufp->timestamp = event.xkey.time;
6307 bufp++;
6308 count++;
6309 numchars--;
6311 /* Now non-ASCII. */
6312 else if (HASH_TABLE_P (Vx_keysym_table)
6313 && (NATNUMP (c = Fgethash (make_number (keysym),
6314 Vx_keysym_table,
6315 Qnil))))
6317 bufp->kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6318 ? ASCII_KEYSTROKE_EVENT
6319 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6320 bufp->code = XFASTINT (c);
6321 XSETFRAME (bufp->frame_or_window, f);
6322 bufp->arg = Qnil;
6323 bufp->modifiers
6324 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6325 modifiers);
6326 bufp->timestamp = event.xkey.time;
6327 bufp++;
6328 count++;
6329 numchars--;
6331 /* Random non-modifier sorts of keysyms. */
6332 else if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6333 || keysym == XK_Delete
6334 #ifdef XK_ISO_Left_Tab
6335 || (keysym >= XK_ISO_Left_Tab
6336 && keysym <= XK_ISO_Enter)
6337 #endif
6338 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6339 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6340 #ifdef HPUX
6341 /* This recognizes the "extended function
6342 keys". It seems there's no cleaner way.
6343 Test IsModifierKey to avoid handling
6344 mode_switch incorrectly. */
6345 || ((unsigned) (keysym) >= XK_Select
6346 && (unsigned)(keysym) < XK_KP_Space)
6347 #endif
6348 #ifdef XK_dead_circumflex
6349 || orig_keysym == XK_dead_circumflex
6350 #endif
6351 #ifdef XK_dead_grave
6352 || orig_keysym == XK_dead_grave
6353 #endif
6354 #ifdef XK_dead_tilde
6355 || orig_keysym == XK_dead_tilde
6356 #endif
6357 #ifdef XK_dead_diaeresis
6358 || orig_keysym == XK_dead_diaeresis
6359 #endif
6360 #ifdef XK_dead_macron
6361 || orig_keysym == XK_dead_macron
6362 #endif
6363 #ifdef XK_dead_degree
6364 || orig_keysym == XK_dead_degree
6365 #endif
6366 #ifdef XK_dead_acute
6367 || orig_keysym == XK_dead_acute
6368 #endif
6369 #ifdef XK_dead_cedilla
6370 || orig_keysym == XK_dead_cedilla
6371 #endif
6372 #ifdef XK_dead_breve
6373 || orig_keysym == XK_dead_breve
6374 #endif
6375 #ifdef XK_dead_ogonek
6376 || orig_keysym == XK_dead_ogonek
6377 #endif
6378 #ifdef XK_dead_caron
6379 || orig_keysym == XK_dead_caron
6380 #endif
6381 #ifdef XK_dead_doubleacute
6382 || orig_keysym == XK_dead_doubleacute
6383 #endif
6384 #ifdef XK_dead_abovedot
6385 || orig_keysym == XK_dead_abovedot
6386 #endif
6387 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6388 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6389 /* Any "vendor-specific" key is ok. */
6390 || (orig_keysym & (1 << 28))
6391 || (keysym != NoSymbol && nbytes == 0))
6392 && ! (IsModifierKey (orig_keysym)
6393 #ifndef HAVE_X11R5
6394 #ifdef XK_Mode_switch
6395 || ((unsigned)(orig_keysym) == XK_Mode_switch)
6396 #endif
6397 #ifdef XK_Num_Lock
6398 || ((unsigned)(orig_keysym) == XK_Num_Lock)
6399 #endif
6400 #endif /* not HAVE_X11R5 */
6401 /* The symbols from XK_ISO_Lock
6402 to XK_ISO_Last_Group_Lock
6403 don't have real modifiers but
6404 should be treated similarly to
6405 Mode_switch by Emacs. */
6406 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6407 || ((unsigned)(orig_keysym)
6408 >= XK_ISO_Lock
6409 && (unsigned)(orig_keysym)
6410 <= XK_ISO_Last_Group_Lock)
6411 #endif
6414 if (temp_index == sizeof temp_buffer / sizeof (short))
6415 temp_index = 0;
6416 temp_buffer[temp_index++] = keysym;
6417 /* make_lispy_event will convert this to a symbolic
6418 key. */
6419 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
6420 bufp->code = keysym;
6421 XSETFRAME (bufp->frame_or_window, f);
6422 bufp->arg = Qnil;
6423 bufp->modifiers
6424 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6425 modifiers);
6426 bufp->timestamp = event.xkey.time;
6427 bufp++;
6428 count++;
6429 numchars--;
6431 else if (numchars > nbytes)
6432 { /* Raw bytes, not keysym. */
6433 register int i;
6434 register int c;
6435 int nchars, len;
6437 /* The input should be decoded with `coding_system'
6438 which depends on which X*LookupString function
6439 we used just above and the locale. */
6440 setup_coding_system (coding_system, &coding);
6441 coding.src_multibyte = 0;
6442 coding.dst_multibyte = 1;
6443 /* The input is converted to events, thus we can't
6444 handle composition. Anyway, there's no XIM that
6445 gives us composition information. */
6446 coding.composing = COMPOSITION_DISABLED;
6448 for (i = 0; i < nbytes; i++)
6450 if (temp_index == (sizeof temp_buffer
6451 / sizeof (short)))
6452 temp_index = 0;
6453 temp_buffer[temp_index++] = copy_bufptr[i];
6457 /* Decode the input data. */
6458 int require;
6459 unsigned char *p;
6461 require = decoding_buffer_size (&coding, nbytes);
6462 p = (unsigned char *) alloca (require);
6463 coding.mode |= CODING_MODE_LAST_BLOCK;
6464 /* We explicitely disable composition
6465 handling because key data should
6466 not contain any composition
6467 sequence. */
6468 coding.composing = COMPOSITION_DISABLED;
6469 decode_coding (&coding, copy_bufptr, p,
6470 nbytes, require);
6471 nbytes = coding.produced;
6472 nchars = coding.produced_char;
6473 copy_bufptr = p;
6476 /* Convert the input data to a sequence of
6477 character events. */
6478 for (i = 0; i < nbytes; i += len)
6480 if (nchars == nbytes)
6481 c = copy_bufptr[i], len = 1;
6482 else
6483 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6484 nbytes - i, len);
6486 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
6487 ? ASCII_KEYSTROKE_EVENT
6488 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6489 bufp->code = c;
6490 XSETFRAME (bufp->frame_or_window, f);
6491 bufp->arg = Qnil;
6492 bufp->modifiers
6493 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6494 modifiers);
6495 bufp->timestamp = event.xkey.time;
6496 bufp++;
6499 count += nchars;
6500 numchars -= nchars;
6502 if (keysym == NoSymbol)
6503 break;
6505 else
6506 abort ();
6508 else
6509 abort ();
6511 #ifdef HAVE_X_I18N
6512 /* Don't dispatch this event since XtDispatchEvent calls
6513 XFilterEvent, and two calls in a row may freeze the
6514 client. */
6515 break;
6516 #else
6517 goto OTHER;
6518 #endif
6520 case KeyRelease:
6521 #ifdef HAVE_X_I18N
6522 /* Don't dispatch this event since XtDispatchEvent calls
6523 XFilterEvent, and two calls in a row may freeze the
6524 client. */
6525 break;
6526 #else
6527 goto OTHER;
6528 #endif
6530 case EnterNotify:
6532 int n;
6534 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6535 if (n > 0)
6537 bufp += n, count += n, numchars -= n;
6540 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6542 #if 0
6543 if (event.xcrossing.focus)
6545 /* Avoid nasty pop/raise loops. */
6546 if (f && (!(f->auto_raise)
6547 || !(f->auto_lower)
6548 || (event.xcrossing.time - enter_timestamp) > 500))
6550 x_new_focus_frame (dpyinfo, f);
6551 enter_timestamp = event.xcrossing.time;
6554 else if (f == dpyinfo->x_focus_frame)
6555 x_new_focus_frame (dpyinfo, 0);
6556 #endif
6558 /* EnterNotify counts as mouse movement,
6559 so update things that depend on mouse position. */
6560 if (f && !f->output_data.x->hourglass_p)
6561 note_mouse_movement (f, &event.xmotion);
6562 goto OTHER;
6565 case FocusIn:
6567 int n;
6569 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6570 if (n > 0)
6572 bufp += n, count += n, numchars -= n;
6576 goto OTHER;
6578 case LeaveNotify:
6580 int n;
6582 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6583 if (n > 0)
6585 bufp += n, count += n, numchars -= n;
6589 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6590 if (f)
6592 if (f == dpyinfo->mouse_face_mouse_frame)
6594 /* If we move outside the frame, then we're
6595 certainly no longer on any text in the frame. */
6596 clear_mouse_face (dpyinfo);
6597 dpyinfo->mouse_face_mouse_frame = 0;
6600 /* Generate a nil HELP_EVENT to cancel a help-echo.
6601 Do it only if there's something to cancel.
6602 Otherwise, the startup message is cleared when
6603 the mouse leaves the frame. */
6604 if (any_help_event_p)
6606 Lisp_Object frame;
6607 int n;
6609 XSETFRAME (frame, f);
6610 help_echo_string = Qnil;
6611 n = gen_help_event (bufp, numchars,
6612 Qnil, frame, Qnil, Qnil, 0);
6613 bufp += n, count += n, numchars -= n;
6617 goto OTHER;
6619 case FocusOut:
6621 int n;
6623 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6624 if (n > 0)
6626 bufp += n, count += n, numchars -= n;
6630 goto OTHER;
6632 case MotionNotify:
6634 previous_help_echo_string = help_echo_string;
6635 help_echo_string = help_echo_object = help_echo_window = Qnil;
6636 help_echo_pos = -1;
6638 if (dpyinfo->grabbed && last_mouse_frame
6639 && FRAME_LIVE_P (last_mouse_frame))
6640 f = last_mouse_frame;
6641 else
6642 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6644 if (dpyinfo->mouse_face_hidden)
6646 dpyinfo->mouse_face_hidden = 0;
6647 clear_mouse_face (dpyinfo);
6650 if (f)
6653 /* Generate SELECT_WINDOW_EVENTs when needed. */
6654 if (mouse_autoselect_window)
6656 Lisp_Object window;
6658 window = window_from_coordinates (f,
6659 event.xmotion.x, event.xmotion.y,
6660 0, 0);
6662 /* Window will be selected only when it is not selected now and
6663 last mouse movement event was not in it. Minibuffer window
6664 will be selected iff it is active. */
6665 if (WINDOWP(window)
6666 && !EQ (window, last_window)
6667 && !EQ (window, selected_window)
6668 && numchars > 0)
6670 bufp->kind = SELECT_WINDOW_EVENT;
6671 bufp->frame_or_window = window;
6672 bufp->arg = Qnil;
6673 ++bufp, ++count, --numchars;
6676 last_window=window;
6678 note_mouse_movement (f, &event.xmotion);
6680 else
6682 #ifndef USE_TOOLKIT_SCROLL_BARS
6683 struct scroll_bar *bar
6684 = x_window_to_scroll_bar (event.xmotion.window);
6686 if (bar)
6687 x_scroll_bar_note_movement (bar, &event);
6688 #endif /* USE_TOOLKIT_SCROLL_BARS */
6690 /* If we move outside the frame, then we're
6691 certainly no longer on any text in the frame. */
6692 clear_mouse_face (dpyinfo);
6695 /* If the contents of the global variable help_echo_string
6696 has changed, generate a HELP_EVENT. */
6697 if (!NILP (help_echo_string)
6698 || !NILP (previous_help_echo_string))
6700 Lisp_Object frame;
6701 int n;
6703 if (f)
6704 XSETFRAME (frame, f);
6705 else
6706 frame = Qnil;
6708 any_help_event_p = 1;
6709 n = gen_help_event (bufp, numchars, help_echo_string, frame,
6710 help_echo_window, help_echo_object,
6711 help_echo_pos);
6712 bufp += n, count += n, numchars -= n;
6715 goto OTHER;
6718 case ConfigureNotify:
6719 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6720 if (f)
6722 #ifndef USE_X_TOOLKIT
6723 #ifdef USE_GTK
6724 xg_resize_widgets (f, event.xconfigure.width,
6725 event.xconfigure.height);
6726 #else /* not USE_GTK */
6727 /* If there is a pending resize for fullscreen, don't
6728 do this one, the right one will come later.
6729 The toolkit version doesn't seem to need this, but we
6730 need to reset it below. */
6731 int dont_resize =
6732 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
6733 && FRAME_NEW_WIDTH (f) != 0);
6734 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
6735 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
6736 if (dont_resize)
6737 goto OTHER;
6739 /* In the toolkit version, change_frame_size
6740 is called by the code that handles resizing
6741 of the EmacsFrame widget. */
6743 /* Even if the number of character rows and columns has
6744 not changed, the font size may have changed, so we need
6745 to check the pixel dimensions as well. */
6746 if (columns != f->width
6747 || rows != f->height
6748 || event.xconfigure.width != f->output_data.x->pixel_width
6749 || event.xconfigure.height != f->output_data.x->pixel_height)
6751 change_frame_size (f, rows, columns, 0, 1, 0);
6752 SET_FRAME_GARBAGED (f);
6753 cancel_mouse_face (f);
6755 #endif /* not USE_GTK */
6756 #endif
6758 f->output_data.x->pixel_width = event.xconfigure.width;
6759 f->output_data.x->pixel_height = event.xconfigure.height;
6761 #ifdef USE_GTK
6762 /* GTK creates windows but doesn't map them.
6763 Only get real positions and check fullscreen when mapped. */
6764 if (FRAME_GTK_OUTER_WIDGET (f)
6765 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6767 #endif
6768 /* What we have now is the position of Emacs's own window.
6769 Convert that to the position of the window manager window. */
6770 x_real_positions (f, &f->output_data.x->left_pos,
6771 &f->output_data.x->top_pos);
6773 x_check_fullscreen_move (f);
6774 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
6775 f->output_data.x->want_fullscreen &=
6776 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6777 #ifdef USE_GTK
6779 #endif
6780 #ifdef HAVE_X_I18N
6781 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6782 xic_set_statusarea (f);
6783 #endif
6785 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6787 /* Since the WM decorations come below top_pos now,
6788 we must put them below top_pos in the future. */
6789 f->output_data.x->win_gravity = NorthWestGravity;
6790 x_wm_set_size_hint (f, (long) 0, 0);
6793 goto OTHER;
6795 case ButtonRelease:
6796 case ButtonPress:
6798 /* If we decide we want to generate an event to be seen
6799 by the rest of Emacs, we put it here. */
6800 struct input_event emacs_event;
6801 int tool_bar_p = 0;
6803 emacs_event.kind = NO_EVENT;
6804 bzero (&compose_status, sizeof (compose_status));
6806 if (dpyinfo->grabbed
6807 && last_mouse_frame
6808 && FRAME_LIVE_P (last_mouse_frame))
6809 f = last_mouse_frame;
6810 else
6811 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6813 if (f)
6815 /* Is this in the tool-bar? */
6816 if (WINDOWP (f->tool_bar_window)
6817 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
6819 Lisp_Object window;
6820 int x = event.xbutton.x;
6821 int y = event.xbutton.y;
6823 window = window_from_coordinates (f, x, y, 0, 1);
6824 if (EQ (window, f->tool_bar_window))
6826 if (event.xbutton.type == ButtonPress)
6827 handle_tool_bar_click (f, x, y, 1, 0);
6828 else
6829 handle_tool_bar_click (f, x, y, 0,
6830 x_x_to_emacs_modifiers (dpyinfo,
6831 event.xbutton.state));
6832 tool_bar_p = 1;
6836 if (!tool_bar_p)
6837 if (!dpyinfo->x_focus_frame
6838 || f == dpyinfo->x_focus_frame)
6840 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6841 if (! popup_activated ())
6842 #endif
6843 construct_mouse_click (&emacs_event, &event, f);
6846 else
6848 struct scroll_bar *bar
6849 = x_window_to_scroll_bar (event.xbutton.window);
6851 #ifdef USE_TOOLKIT_SCROLL_BARS
6852 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6853 scroll bars. */
6854 if (bar && event.xbutton.state & ControlMask)
6856 x_scroll_bar_handle_click (bar, &event, &emacs_event);
6857 *finish = X_EVENT_DROP;
6859 #else /* not USE_TOOLKIT_SCROLL_BARS */
6860 if (bar)
6861 x_scroll_bar_handle_click (bar, &event, &emacs_event);
6862 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6865 if (event.type == ButtonPress)
6867 dpyinfo->grabbed |= (1 << event.xbutton.button);
6868 last_mouse_frame = f;
6869 /* Ignore any mouse motion that happened
6870 before this event; any subsequent mouse-movement
6871 Emacs events should reflect only motion after
6872 the ButtonPress. */
6873 if (f != 0)
6874 f->mouse_moved = 0;
6876 if (!tool_bar_p)
6877 last_tool_bar_item = -1;
6879 else
6880 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6882 if (numchars >= 1 && emacs_event.kind != NO_EVENT)
6884 bcopy (&emacs_event, bufp, sizeof (struct input_event));
6885 bufp++;
6886 count++;
6887 numchars--;
6890 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6891 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6892 /* For a down-event in the menu bar,
6893 don't pass it to Xt right now.
6894 Instead, save it away
6895 and we will pass it to Xt from kbd_buffer_get_event.
6896 That way, we can run some Lisp code first. */
6897 if (
6898 #ifdef USE_GTK
6899 ! popup_activated ()
6901 #endif
6902 f && event.type == ButtonPress
6903 /* Verify the event is really within the menu bar
6904 and not just sent to it due to grabbing. */
6905 && event.xbutton.x >= 0
6906 && event.xbutton.x < f->output_data.x->pixel_width
6907 && event.xbutton.y >= 0
6908 && event.xbutton.y < f->output_data.x->menubar_height
6909 && event.xbutton.same_screen)
6911 SET_SAVED_BUTTON_EVENT;
6912 XSETFRAME (last_mouse_press_frame, f);
6913 #ifdef USE_GTK
6914 *finish = X_EVENT_DROP;
6915 #endif
6917 else if (event.type == ButtonPress)
6919 last_mouse_press_frame = Qnil;
6920 goto OTHER;
6923 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6924 but I am trying to be cautious. */
6925 else if (event.type == ButtonRelease)
6927 if (!NILP (last_mouse_press_frame))
6929 f = XFRAME (last_mouse_press_frame);
6930 if (f->output_data.x)
6931 SET_SAVED_BUTTON_EVENT;
6933 else
6934 goto OTHER;
6936 #endif /* USE_MOTIF */
6937 else
6938 goto OTHER;
6939 #endif /* USE_X_TOOLKIT || USE_GTK */
6941 break;
6943 case CirculateNotify:
6944 goto OTHER;
6946 case CirculateRequest:
6947 goto OTHER;
6949 case VisibilityNotify:
6950 goto OTHER;
6952 case MappingNotify:
6953 /* Someone has changed the keyboard mapping - update the
6954 local cache. */
6955 switch (event.xmapping.request)
6957 case MappingModifier:
6958 x_find_modifier_meanings (dpyinfo);
6959 /* This is meant to fall through. */
6960 case MappingKeyboard:
6961 XRefreshKeyboardMapping (&event.xmapping);
6963 goto OTHER;
6965 default:
6966 OTHER:
6967 #ifdef USE_X_TOOLKIT
6968 BLOCK_INPUT;
6969 if (*finish != X_EVENT_DROP)
6970 XtDispatchEvent (&event);
6971 UNBLOCK_INPUT;
6972 #endif /* USE_X_TOOLKIT */
6973 break;
6976 goto ret;
6978 out:
6979 *finish = X_EVENT_GOTO_OUT;
6981 ret:
6982 *bufp_r = bufp;
6983 *numcharsp = numchars;
6984 *eventp = event;
6986 return count;
6990 /* Handles the XEvent EVENT on display DISPLAY.
6991 This is used for event loops outside the normal event handling,
6992 i.e. looping while a popup menu or a dialog is posted.
6994 Returns the value handle_one_xevent sets in the finish argument. */
6996 x_dispatch_event (event, display)
6997 XEvent *event;
6998 Display *display;
7000 struct x_display_info *dpyinfo;
7001 struct input_event bufp[10];
7002 struct input_event *bufpp = bufp;
7003 int numchars = 10;
7004 int finish = X_EVENT_NORMAL;
7006 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7007 if (dpyinfo->display == display)
7008 break;
7010 if (dpyinfo)
7012 int i, events;
7013 events = handle_one_xevent (dpyinfo,
7014 event,
7015 &bufpp,
7016 &numchars,
7017 &finish);
7018 for (i = 0; i < events; ++i)
7019 kbd_buffer_store_event (&bufp[i]);
7022 return finish;
7026 /* Read events coming from the X server.
7027 This routine is called by the SIGIO handler.
7028 We return as soon as there are no more events to be read.
7030 Events representing keys are stored in buffer BUFP,
7031 which can hold up to NUMCHARS characters.
7032 We return the number of characters stored into the buffer,
7033 thus pretending to be `read'.
7035 EXPECTED is nonzero if the caller knows input is available. */
7037 static int
7038 XTread_socket (sd, bufp, numchars, expected)
7039 register int sd;
7040 /* register */ struct input_event *bufp;
7041 /* register */ int numchars;
7042 int expected;
7044 int count = 0;
7045 XEvent event;
7046 int event_found = 0;
7047 struct x_display_info *dpyinfo;
7049 if (interrupt_input_blocked)
7051 interrupt_input_pending = 1;
7052 return -1;
7055 interrupt_input_pending = 0;
7056 BLOCK_INPUT;
7058 /* So people can tell when we have read the available input. */
7059 input_signal_count++;
7061 if (numchars <= 0)
7062 abort (); /* Don't think this happens. */
7064 ++handling_signal;
7066 /* Find the display we are supposed to read input for.
7067 It's the one communicating on descriptor SD. */
7068 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7070 #if 0 /* This ought to be unnecessary; let's verify it. */
7071 #ifdef FIOSNBIO
7072 /* If available, Xlib uses FIOSNBIO to make the socket
7073 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7074 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7075 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7076 fcntl (dpyinfo->connection, F_SETFL, 0);
7077 #endif /* ! defined (FIOSNBIO) */
7078 #endif
7080 #if 0 /* This code can't be made to work, with multiple displays,
7081 and appears not to be used on any system any more.
7082 Also keyboard.c doesn't turn O_NDELAY on and off
7083 for X connections. */
7084 #ifndef SIGIO
7085 #ifndef HAVE_SELECT
7086 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7088 extern int read_alarm_should_throw;
7089 read_alarm_should_throw = 1;
7090 XPeekEvent (dpyinfo->display, &event);
7091 read_alarm_should_throw = 0;
7093 #endif /* HAVE_SELECT */
7094 #endif /* SIGIO */
7095 #endif
7097 /* For debugging, this gives a way to fake an I/O error. */
7098 if (dpyinfo == XTread_socket_fake_io_error)
7100 XTread_socket_fake_io_error = 0;
7101 x_io_error_quitter (dpyinfo->display);
7104 #ifdef HAVE_X_SM
7105 BLOCK_INPUT;
7106 count += x_session_check_input (bufp, &numchars);
7107 UNBLOCK_INPUT;
7108 #endif
7110 #ifdef USE_GTK
7111 /* For GTK we must use the GTK event loop. But XEvents gets passed
7112 to our filter function above, and then to the big event switch.
7113 We use a bunch of globals to communicate with our filter function,
7114 that is kind of ugly, but it works. */
7115 current_dpyinfo = dpyinfo;
7117 while (gtk_events_pending ())
7119 static int nr = 0;
7120 current_count = count;
7121 current_numcharsp = &numchars;
7122 current_bufp = &bufp;
7124 gtk_main_iteration ();
7126 count = current_count;
7127 current_bufp = 0;
7128 current_numcharsp = 0;
7130 if (current_finish == X_EVENT_GOTO_OUT)
7131 goto out;
7134 #else /* not USE_GTK */
7135 while (XPending (dpyinfo->display))
7137 int finish;
7139 XNextEvent (dpyinfo->display, &event);
7141 #ifdef HAVE_X_I18N
7142 /* Filter events for the current X input method. */
7143 if (x_filter_event (dpyinfo, &event))
7144 break;
7145 #endif
7146 event_found = 1;
7148 count += handle_one_xevent (dpyinfo,
7149 &event,
7150 &bufp,
7151 &numchars,
7152 &finish);
7154 if (finish == X_EVENT_GOTO_OUT)
7155 goto out;
7157 #endif /* USE_GTK */
7160 out:;
7162 /* On some systems, an X bug causes Emacs to get no more events
7163 when the window is destroyed. Detect that. (1994.) */
7164 if (! event_found)
7166 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7167 One XNOOP in 100 loops will make Emacs terminate.
7168 B. Bretthauer, 1994 */
7169 x_noop_count++;
7170 if (x_noop_count >= 100)
7172 x_noop_count=0;
7174 if (next_noop_dpyinfo == 0)
7175 next_noop_dpyinfo = x_display_list;
7177 XNoOp (next_noop_dpyinfo->display);
7179 /* Each time we get here, cycle through the displays now open. */
7180 next_noop_dpyinfo = next_noop_dpyinfo->next;
7184 /* If the focus was just given to an auto-raising frame,
7185 raise it now. */
7186 /* ??? This ought to be able to handle more than one such frame. */
7187 if (pending_autoraise_frame)
7189 x_raise_frame (pending_autoraise_frame);
7190 pending_autoraise_frame = 0;
7193 UNBLOCK_INPUT;
7194 --handling_signal;
7195 return count;
7201 /***********************************************************************
7202 Text Cursor
7203 ***********************************************************************/
7205 /* Set clipping for output in glyph row ROW. W is the window in which
7206 we operate. GC is the graphics context to set clipping in.
7207 WHOLE_LINE_P non-zero means include the areas used for truncation
7208 mark display and alike in the clipping rectangle.
7210 ROW may be a text row or, e.g., a mode line. Text rows must be
7211 clipped to the interior of the window dedicated to text display,
7212 mode lines must be clipped to the whole window. */
7214 static void
7215 x_clip_to_row (w, row, gc, whole_line_p)
7216 struct window *w;
7217 struct glyph_row *row;
7218 GC gc;
7219 int whole_line_p;
7221 struct frame *f = XFRAME (WINDOW_FRAME (w));
7222 XRectangle clip_rect;
7223 int window_x, window_y, window_width, window_height;
7225 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
7227 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
7228 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
7229 clip_rect.y = max (clip_rect.y, window_y);
7230 clip_rect.width = window_width;
7231 clip_rect.height = row->visible_height;
7233 /* If clipping to the whole line, including trunc marks, extend
7234 the rectangle to the left and increase its width. */
7235 if (whole_line_p)
7237 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
7238 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
7241 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7245 /* Draw a hollow box cursor on window W in glyph row ROW. */
7247 static void
7248 x_draw_hollow_cursor (w, row)
7249 struct window *w;
7250 struct glyph_row *row;
7252 struct frame *f = XFRAME (WINDOW_FRAME (w));
7253 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7254 Display *dpy = FRAME_X_DISPLAY (f);
7255 int x, y, wd, h;
7256 XGCValues xgcv;
7257 struct glyph *cursor_glyph;
7258 GC gc;
7260 /* Compute frame-relative coordinates from window-relative
7261 coordinates. */
7262 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7263 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
7264 + row->ascent - w->phys_cursor_ascent);
7265 h = row->height - 1;
7267 /* Get the glyph the cursor is on. If we can't tell because
7268 the current matrix is invalid or such, give up. */
7269 cursor_glyph = get_phys_cursor_glyph (w);
7270 if (cursor_glyph == NULL)
7271 return;
7273 /* Compute the width of the rectangle to draw. If on a stretch
7274 glyph, and `x-stretch-block-cursor' is nil, don't draw a
7275 rectangle as wide as the glyph, but use a canonical character
7276 width instead. */
7277 wd = cursor_glyph->pixel_width - 1;
7278 if (cursor_glyph->type == STRETCH_GLYPH
7279 && !x_stretch_cursor_p)
7280 wd = min (CANON_X_UNIT (f), wd);
7281 w->phys_cursor_width = wd;
7283 /* The foreground of cursor_gc is typically the same as the normal
7284 background color, which can cause the cursor box to be invisible. */
7285 xgcv.foreground = f->output_data.x->cursor_pixel;
7286 if (dpyinfo->scratch_cursor_gc)
7287 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7288 else
7289 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7290 GCForeground, &xgcv);
7291 gc = dpyinfo->scratch_cursor_gc;
7293 /* Set clipping, draw the rectangle, and reset clipping again. */
7294 x_clip_to_row (w, row, gc, 0);
7295 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
7296 XSetClipMask (dpy, gc, None);
7300 /* Draw a bar cursor on window W in glyph row ROW.
7302 Implementation note: One would like to draw a bar cursor with an
7303 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7304 Unfortunately, I didn't find a font yet that has this property set.
7305 --gerd. */
7307 static void
7308 x_draw_bar_cursor (w, row, width, kind)
7309 struct window *w;
7310 struct glyph_row *row;
7311 int width;
7312 enum text_cursor_kinds kind;
7314 struct frame *f = XFRAME (w->frame);
7315 struct glyph *cursor_glyph;
7317 /* If cursor is out of bounds, don't draw garbage. This can happen
7318 in mini-buffer windows when switching between echo area glyphs
7319 and mini-buffer. */
7320 cursor_glyph = get_phys_cursor_glyph (w);
7321 if (cursor_glyph == NULL)
7322 return;
7324 /* If on an image, draw like a normal cursor. That's usually better
7325 visible than drawing a bar, esp. if the image is large so that
7326 the bar might not be in the window. */
7327 if (cursor_glyph->type == IMAGE_GLYPH)
7329 struct glyph_row *row;
7330 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7331 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7333 else
7335 Display *dpy = FRAME_X_DISPLAY (f);
7336 Window window = FRAME_X_WINDOW (f);
7337 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7338 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7339 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7340 XGCValues xgcv;
7342 /* If the glyph's background equals the color we normally draw
7343 the bar cursor in, the bar cursor in its normal color is
7344 invisible. Use the glyph's foreground color instead in this
7345 case, on the assumption that the glyph's colors are chosen so
7346 that the glyph is legible. */
7347 if (face->background == f->output_data.x->cursor_pixel)
7348 xgcv.background = xgcv.foreground = face->foreground;
7349 else
7350 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7351 xgcv.graphics_exposures = 0;
7353 if (gc)
7354 XChangeGC (dpy, gc, mask, &xgcv);
7355 else
7357 gc = XCreateGC (dpy, window, mask, &xgcv);
7358 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7361 if (width < 0)
7362 width = FRAME_CURSOR_WIDTH (f);
7363 width = min (cursor_glyph->pixel_width, width);
7365 w->phys_cursor_width = width;
7366 x_clip_to_row (w, row, gc, 0);
7368 if (kind == BAR_CURSOR)
7369 XFillRectangle (dpy, window, gc,
7370 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7371 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7372 width, row->height);
7373 else
7374 XFillRectangle (dpy, window, gc,
7375 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7376 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7377 row->height - width),
7378 cursor_glyph->pixel_width,
7379 width);
7381 XSetClipMask (dpy, gc, None);
7386 /* RIF: Define cursor CURSOR on frame F. */
7388 static void
7389 x_define_frame_cursor (f, cursor)
7390 struct frame *f;
7391 Cursor cursor;
7393 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7397 /* RIF: Clear area on frame F. */
7399 static void
7400 x_clear_frame_area (f, x, y, width, height)
7401 struct frame *f;
7402 int x, y, width, height;
7404 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7405 x, y, width, height, False);
7409 /* RIF: Draw cursor on window W. */
7411 static void
7412 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7413 struct window *w;
7414 struct glyph_row *glyph_row;
7415 int x, y;
7416 int cursor_type, cursor_width;
7417 int on_p, active_p;
7419 struct frame *f = XFRAME (WINDOW_FRAME (w));
7421 if (on_p)
7423 w->phys_cursor_type = cursor_type;
7424 w->phys_cursor_on_p = 1;
7426 switch (cursor_type)
7428 case HOLLOW_BOX_CURSOR:
7429 x_draw_hollow_cursor (w, glyph_row);
7430 break;
7432 case FILLED_BOX_CURSOR:
7433 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7434 break;
7436 case BAR_CURSOR:
7437 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7438 break;
7440 case HBAR_CURSOR:
7441 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7442 break;
7444 case NO_CURSOR:
7445 w->phys_cursor_width = 0;
7446 break;
7448 default:
7449 abort ();
7452 #ifdef HAVE_X_I18N
7453 if (w == XWINDOW (f->selected_window))
7454 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7455 xic_set_preeditarea (w, x, y);
7456 #endif
7459 #ifndef XFlush
7460 if (updating_frame != f)
7461 XFlush (FRAME_X_DISPLAY (f));
7462 #endif
7466 /* Icons. */
7468 /* Make the x-window of frame F use the gnu icon bitmap. */
7471 x_bitmap_icon (f, file)
7472 struct frame *f;
7473 Lisp_Object file;
7475 int bitmap_id;
7477 if (FRAME_X_WINDOW (f) == 0)
7478 return 1;
7480 /* Free up our existing icon bitmap if any. */
7481 if (f->output_data.x->icon_bitmap > 0)
7482 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7483 f->output_data.x->icon_bitmap = 0;
7485 if (STRINGP (file))
7486 bitmap_id = x_create_bitmap_from_file (f, file);
7487 else
7489 /* Create the GNU bitmap if necessary. */
7490 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7491 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
7492 = x_create_bitmap_from_data (f, gnu_bits,
7493 gnu_width, gnu_height);
7495 /* The first time we create the GNU bitmap,
7496 this increments the ref-count one extra time.
7497 As a result, the GNU bitmap is never freed.
7498 That way, we don't have to worry about allocating it again. */
7499 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7501 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7504 x_wm_set_icon_pixmap (f, bitmap_id);
7505 f->output_data.x->icon_bitmap = bitmap_id;
7507 return 0;
7511 /* Make the x-window of frame F use a rectangle with text.
7512 Use ICON_NAME as the text. */
7515 x_text_icon (f, icon_name)
7516 struct frame *f;
7517 char *icon_name;
7519 if (FRAME_X_WINDOW (f) == 0)
7520 return 1;
7522 #ifdef HAVE_X11R4
7524 XTextProperty text;
7525 text.value = (unsigned char *) icon_name;
7526 text.encoding = XA_STRING;
7527 text.format = 8;
7528 text.nitems = strlen (icon_name);
7529 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7531 #else /* not HAVE_X11R4 */
7532 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
7533 #endif /* not HAVE_X11R4 */
7535 if (f->output_data.x->icon_bitmap > 0)
7536 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7537 f->output_data.x->icon_bitmap = 0;
7538 x_wm_set_icon_pixmap (f, 0);
7540 return 0;
7543 #define X_ERROR_MESSAGE_SIZE 200
7545 /* If non-nil, this should be a string.
7546 It means catch X errors and store the error message in this string. */
7548 static Lisp_Object x_error_message_string;
7550 /* An X error handler which stores the error message in
7551 x_error_message_string. This is called from x_error_handler if
7552 x_catch_errors is in effect. */
7554 static void
7555 x_error_catcher (display, error)
7556 Display *display;
7557 XErrorEvent *error;
7559 XGetErrorText (display, error->error_code,
7560 SDATA (x_error_message_string),
7561 X_ERROR_MESSAGE_SIZE);
7564 /* Begin trapping X errors for display DPY. Actually we trap X errors
7565 for all displays, but DPY should be the display you are actually
7566 operating on.
7568 After calling this function, X protocol errors no longer cause
7569 Emacs to exit; instead, they are recorded in the string
7570 stored in x_error_message_string.
7572 Calling x_check_errors signals an Emacs error if an X error has
7573 occurred since the last call to x_catch_errors or x_check_errors.
7575 Calling x_uncatch_errors resumes the normal error handling. */
7577 void x_check_errors ();
7578 static Lisp_Object x_catch_errors_unwind ();
7581 x_catch_errors (dpy)
7582 Display *dpy;
7584 int count = SPECPDL_INDEX ();
7586 /* Make sure any errors from previous requests have been dealt with. */
7587 XSync (dpy, False);
7589 record_unwind_protect (x_catch_errors_unwind,
7590 Fcons (make_save_value (dpy, 0),
7591 x_error_message_string));
7593 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
7594 SSET (x_error_message_string, 0, 0);
7596 return count;
7599 /* Unbind the binding that we made to check for X errors. */
7601 static Lisp_Object
7602 x_catch_errors_unwind (old_val)
7603 Lisp_Object old_val;
7605 Lisp_Object first;
7607 first = XCAR (old_val);
7609 XSync (XSAVE_VALUE (first)->pointer, False);
7611 x_error_message_string = XCDR (old_val);
7612 return Qnil;
7615 /* If any X protocol errors have arrived since the last call to
7616 x_catch_errors or x_check_errors, signal an Emacs error using
7617 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7619 void
7620 x_check_errors (dpy, format)
7621 Display *dpy;
7622 char *format;
7624 /* Make sure to catch any errors incurred so far. */
7625 XSync (dpy, False);
7627 if (SREF (x_error_message_string, 0))
7628 error (format, SDATA (x_error_message_string));
7631 /* Nonzero if we had any X protocol errors
7632 since we did x_catch_errors on DPY. */
7635 x_had_errors_p (dpy)
7636 Display *dpy;
7638 /* Make sure to catch any errors incurred so far. */
7639 XSync (dpy, False);
7641 return SREF (x_error_message_string, 0) != 0;
7644 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7646 void
7647 x_clear_errors (dpy)
7648 Display *dpy;
7650 SSET (x_error_message_string, 0, 0);
7653 /* Stop catching X protocol errors and let them make Emacs die.
7654 DPY should be the display that was passed to x_catch_errors.
7655 COUNT should be the value that was returned by
7656 the corresponding call to x_catch_errors. */
7658 void
7659 x_uncatch_errors (dpy, count)
7660 Display *dpy;
7661 int count;
7663 unbind_to (count, Qnil);
7666 #if 0
7667 static unsigned int x_wire_count;
7668 x_trace_wire ()
7670 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7672 #endif /* ! 0 */
7675 /* Handle SIGPIPE, which can happen when the connection to a server
7676 simply goes away. SIGPIPE is handled by x_connection_signal.
7677 Don't need to do anything, because the write which caused the
7678 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7679 which will do the appropriate cleanup for us. */
7681 static SIGTYPE
7682 x_connection_signal (signalnum) /* If we don't have an argument, */
7683 int signalnum; /* some compilers complain in signal calls. */
7685 #ifdef USG
7686 /* USG systems forget handlers when they are used;
7687 must reestablish each time */
7688 signal (signalnum, x_connection_signal);
7689 #endif /* USG */
7693 /************************************************************************
7694 Handling X errors
7695 ************************************************************************/
7697 /* Error message passed to x_connection_closed. */
7699 static char *error_msg;
7701 /* Function installed as fatal_error_signal_hook in
7702 x_connection_closed. Print the X error message, and exit normally,
7703 instead of dumping core when XtCloseDisplay fails. */
7705 static void
7706 x_fatal_error_signal ()
7708 fprintf (stderr, "%s\n", error_msg);
7709 exit (70);
7712 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7713 the text of an error message that lead to the connection loss. */
7715 static SIGTYPE
7716 x_connection_closed (dpy, error_message)
7717 Display *dpy;
7718 char *error_message;
7720 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7721 Lisp_Object frame, tail;
7722 int count;
7724 error_msg = (char *) alloca (strlen (error_message) + 1);
7725 strcpy (error_msg, error_message);
7726 handling_signal = 0;
7728 /* Prevent being called recursively because of an error condition
7729 below. Otherwise, we might end up with printing ``can't find per
7730 display information'' in the recursive call instead of printing
7731 the original message here. */
7732 count = x_catch_errors (dpy);
7734 /* We have to close the display to inform Xt that it doesn't
7735 exist anymore. If we don't, Xt will continue to wait for
7736 events from the display. As a consequence, a sequence of
7738 M-x make-frame-on-display RET :1 RET
7739 ...kill the new frame, so that we get an IO error...
7740 M-x make-frame-on-display RET :1 RET
7742 will indefinitely wait in Xt for events for display `:1', opened
7743 in the first class to make-frame-on-display.
7745 Closing the display is reported to lead to a bus error on
7746 OpenWindows in certain situations. I suspect that is a bug
7747 in OpenWindows. I don't know how to cicumvent it here. */
7749 #ifdef USE_X_TOOLKIT
7750 /* If DPYINFO is null, this means we didn't open the display
7751 in the first place, so don't try to close it. */
7752 if (dpyinfo)
7754 extern void (*fatal_error_signal_hook) P_ ((void));
7755 fatal_error_signal_hook = x_fatal_error_signal;
7756 XtCloseDisplay (dpy);
7757 fatal_error_signal_hook = NULL;
7759 #endif
7761 /* Indicate that this display is dead. */
7762 if (dpyinfo)
7763 dpyinfo->display = 0;
7765 /* First delete frames whose mini-buffers are on frames
7766 that are on the dead display. */
7767 FOR_EACH_FRAME (tail, frame)
7769 Lisp_Object minibuf_frame;
7770 minibuf_frame
7771 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7772 if (FRAME_X_P (XFRAME (frame))
7773 && FRAME_X_P (XFRAME (minibuf_frame))
7774 && ! EQ (frame, minibuf_frame)
7775 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7776 Fdelete_frame (frame, Qt);
7779 /* Now delete all remaining frames on the dead display.
7780 We are now sure none of these is used as the mini-buffer
7781 for another frame that we need to delete. */
7782 FOR_EACH_FRAME (tail, frame)
7783 if (FRAME_X_P (XFRAME (frame))
7784 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7786 /* Set this to t so that Fdelete_frame won't get confused
7787 trying to find a replacement. */
7788 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7789 Fdelete_frame (frame, Qt);
7792 if (dpyinfo)
7793 x_delete_display (dpyinfo);
7795 x_uncatch_errors (dpy, count);
7797 if (x_display_list == 0)
7799 fprintf (stderr, "%s\n", error_msg);
7800 shut_down_emacs (0, 0, Qnil);
7801 exit (70);
7804 /* Ordinary stack unwind doesn't deal with these. */
7805 #ifdef SIGIO
7806 sigunblock (sigmask (SIGIO));
7807 #endif
7808 sigunblock (sigmask (SIGALRM));
7809 TOTALLY_UNBLOCK_INPUT;
7811 clear_waiting_for_input ();
7812 error ("%s", error_msg);
7816 /* This is the usual handler for X protocol errors.
7817 It kills all frames on the display that we got the error for.
7818 If that was the only one, it prints an error message and kills Emacs. */
7820 static void
7821 x_error_quitter (display, error)
7822 Display *display;
7823 XErrorEvent *error;
7825 char buf[256], buf1[356];
7827 /* Note that there is no real way portable across R3/R4 to get the
7828 original error handler. */
7830 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7831 sprintf (buf1, "X protocol error: %s on protocol request %d",
7832 buf, error->request_code);
7833 x_connection_closed (display, buf1);
7837 /* This is the first-level handler for X protocol errors.
7838 It calls x_error_quitter or x_error_catcher. */
7840 static int
7841 x_error_handler (display, error)
7842 Display *display;
7843 XErrorEvent *error;
7845 if (! NILP (x_error_message_string))
7846 x_error_catcher (display, error);
7847 else
7848 x_error_quitter (display, error);
7849 return 0;
7852 /* This is the handler for X IO errors, always.
7853 It kills all frames on the display that we lost touch with.
7854 If that was the only one, it prints an error message and kills Emacs. */
7856 static int
7857 x_io_error_quitter (display)
7858 Display *display;
7860 char buf[256];
7862 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7863 x_connection_closed (display, buf);
7864 return 0;
7867 /* Changing the font of the frame. */
7869 /* Give frame F the font named FONTNAME as its default font, and
7870 return the full name of that font. FONTNAME may be a wildcard
7871 pattern; in that case, we choose some font that fits the pattern.
7872 The return value shows which font we chose. */
7874 Lisp_Object
7875 x_new_font (f, fontname)
7876 struct frame *f;
7877 register char *fontname;
7879 struct font_info *fontp
7880 = FS_LOAD_FONT (f, 0, fontname, -1);
7882 if (!fontp)
7883 return Qnil;
7885 f->output_data.x->font = (XFontStruct *) (fontp->font);
7886 f->output_data.x->baseline_offset = fontp->baseline_offset;
7887 f->output_data.x->fontset = -1;
7889 compute_fringe_widths (f, 1);
7891 /* Compute the scroll bar width in character columns. */
7892 if (f->scroll_bar_pixel_width > 0)
7894 int wid = FONT_WIDTH (f->output_data.x->font);
7895 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
7897 else
7899 int wid = FONT_WIDTH (f->output_data.x->font);
7900 f->scroll_bar_cols = (14 + wid - 1) / wid;
7903 /* Now make the frame display the given font. */
7904 if (FRAME_X_WINDOW (f) != 0)
7906 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
7907 f->output_data.x->font->fid);
7908 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
7909 f->output_data.x->font->fid);
7910 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
7911 f->output_data.x->font->fid);
7913 frame_update_line_height (f);
7915 /* Don't change the size of a tip frame; there's no point in
7916 doing it because it's done in Fx_show_tip, and it leads to
7917 problems because the tip frame has no widget. */
7918 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
7919 x_set_window_size (f, 0, f->width, f->height);
7921 else
7922 /* If we are setting a new frame's font for the first time,
7923 there are no faces yet, so this font's height is the line height. */
7924 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
7926 return build_string (fontp->full_name);
7929 /* Give frame F the fontset named FONTSETNAME as its default font, and
7930 return the full name of that fontset. FONTSETNAME may be a wildcard
7931 pattern; in that case, we choose some fontset that fits the pattern.
7932 The return value shows which fontset we chose. */
7934 Lisp_Object
7935 x_new_fontset (f, fontsetname)
7936 struct frame *f;
7937 char *fontsetname;
7939 int fontset = fs_query_fontset (build_string (fontsetname), 0);
7940 Lisp_Object result;
7942 if (fontset < 0)
7943 return Qnil;
7945 if (f->output_data.x->fontset == fontset)
7946 /* This fontset is already set in frame F. There's nothing more
7947 to do. */
7948 return fontset_name (fontset);
7950 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
7952 if (!STRINGP (result))
7953 /* Can't load ASCII font. */
7954 return Qnil;
7956 /* Since x_new_font doesn't update any fontset information, do it now. */
7957 f->output_data.x->fontset = fontset;
7959 #ifdef HAVE_X_I18N
7960 if (FRAME_XIC (f)
7961 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
7962 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
7963 #endif
7965 return build_string (fontsetname);
7969 /***********************************************************************
7970 X Input Methods
7971 ***********************************************************************/
7973 #ifdef HAVE_X_I18N
7975 #ifdef HAVE_X11R6
7977 /* XIM destroy callback function, which is called whenever the
7978 connection to input method XIM dies. CLIENT_DATA contains a
7979 pointer to the x_display_info structure corresponding to XIM. */
7981 static void
7982 xim_destroy_callback (xim, client_data, call_data)
7983 XIM xim;
7984 XPointer client_data;
7985 XPointer call_data;
7987 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
7988 Lisp_Object frame, tail;
7990 BLOCK_INPUT;
7992 /* No need to call XDestroyIC.. */
7993 FOR_EACH_FRAME (tail, frame)
7995 struct frame *f = XFRAME (frame);
7996 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
7998 FRAME_XIC (f) = NULL;
7999 if (FRAME_XIC_FONTSET (f))
8001 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
8002 FRAME_XIC_FONTSET (f) = NULL;
8007 /* No need to call XCloseIM. */
8008 dpyinfo->xim = NULL;
8009 XFree (dpyinfo->xim_styles);
8010 UNBLOCK_INPUT;
8013 #endif /* HAVE_X11R6 */
8015 #ifdef HAVE_X11R6
8016 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8017 extern char *XSetIMValues P_ ((XIM, ...));
8018 #endif
8020 /* Open the connection to the XIM server on display DPYINFO.
8021 RESOURCE_NAME is the resource name Emacs uses. */
8023 static void
8024 xim_open_dpy (dpyinfo, resource_name)
8025 struct x_display_info *dpyinfo;
8026 char *resource_name;
8028 #ifdef USE_XIM
8029 XIM xim;
8031 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
8032 dpyinfo->xim = xim;
8034 if (xim)
8036 #ifdef HAVE_X11R6
8037 XIMCallback destroy;
8038 #endif
8040 /* Get supported styles and XIM values. */
8041 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8043 #ifdef HAVE_X11R6
8044 destroy.callback = xim_destroy_callback;
8045 destroy.client_data = (XPointer)dpyinfo;
8046 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8047 #endif
8050 #else /* not USE_XIM */
8051 dpyinfo->xim = NULL;
8052 #endif /* not USE_XIM */
8056 #ifdef HAVE_X11R6_XIM
8058 struct xim_inst_t
8060 struct x_display_info *dpyinfo;
8061 char *resource_name;
8064 /* XIM instantiate callback function, which is called whenever an XIM
8065 server is available. DISPLAY is the display of the XIM.
8066 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8067 when the callback was registered. */
8069 static void
8070 xim_instantiate_callback (display, client_data, call_data)
8071 Display *display;
8072 XPointer client_data;
8073 XPointer call_data;
8075 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8076 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8078 /* We don't support multiple XIM connections. */
8079 if (dpyinfo->xim)
8080 return;
8082 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8084 /* Create XIC for the existing frames on the same display, as long
8085 as they have no XIC. */
8086 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8088 Lisp_Object tail, frame;
8090 BLOCK_INPUT;
8091 FOR_EACH_FRAME (tail, frame)
8093 struct frame *f = XFRAME (frame);
8095 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8096 if (FRAME_XIC (f) == NULL)
8098 create_frame_xic (f);
8099 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8100 xic_set_statusarea (f);
8101 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8103 struct window *w = XWINDOW (f->selected_window);
8104 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8109 UNBLOCK_INPUT;
8113 #endif /* HAVE_X11R6_XIM */
8116 /* Open a connection to the XIM server on display DPYINFO.
8117 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8118 connection only at the first time. On X11R6, open the connection
8119 in the XIM instantiate callback function. */
8121 static void
8122 xim_initialize (dpyinfo, resource_name)
8123 struct x_display_info *dpyinfo;
8124 char *resource_name;
8126 #ifdef USE_XIM
8127 #ifdef HAVE_X11R6_XIM
8128 struct xim_inst_t *xim_inst;
8129 int len;
8131 dpyinfo->xim = NULL;
8132 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8133 xim_inst->dpyinfo = dpyinfo;
8134 len = strlen (resource_name);
8135 xim_inst->resource_name = (char *) xmalloc (len + 1);
8136 bcopy (resource_name, xim_inst->resource_name, len + 1);
8137 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8138 resource_name, EMACS_CLASS,
8139 xim_instantiate_callback,
8140 /* Fixme: This is XPointer in
8141 XFree86 but (XPointer *) on
8142 Tru64, at least. */
8143 (XPointer) xim_inst);
8144 #else /* not HAVE_X11R6_XIM */
8145 dpyinfo->xim = NULL;
8146 xim_open_dpy (dpyinfo, resource_name);
8147 #endif /* not HAVE_X11R6_XIM */
8149 #else /* not USE_XIM */
8150 dpyinfo->xim = NULL;
8151 #endif /* not USE_XIM */
8155 /* Close the connection to the XIM server on display DPYINFO. */
8157 static void
8158 xim_close_dpy (dpyinfo)
8159 struct x_display_info *dpyinfo;
8161 #ifdef USE_XIM
8162 #ifdef HAVE_X11R6_XIM
8163 if (dpyinfo->display)
8164 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8165 NULL, EMACS_CLASS,
8166 xim_instantiate_callback, NULL);
8167 #endif /* not HAVE_X11R6_XIM */
8168 if (dpyinfo->display)
8169 XCloseIM (dpyinfo->xim);
8170 dpyinfo->xim = NULL;
8171 XFree (dpyinfo->xim_styles);
8172 #endif /* USE_XIM */
8175 #endif /* not HAVE_X11R6_XIM */
8179 /* Calculate the absolute position in frame F
8180 from its current recorded position values and gravity. */
8182 void
8183 x_calc_absolute_position (f)
8184 struct frame *f;
8186 Window child;
8187 int win_x = 0, win_y = 0;
8188 int flags = f->output_data.x->size_hint_flags;
8189 int this_window;
8191 /* We have nothing to do if the current position
8192 is already for the top-left corner. */
8193 if (! ((flags & XNegative) || (flags & YNegative)))
8194 return;
8196 this_window = FRAME_OUTER_WINDOW (f);
8198 /* Find the position of the outside upper-left corner of
8199 the inner window, with respect to the outer window.
8200 But do this only if we will need the results. */
8201 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8203 int count;
8205 BLOCK_INPUT;
8206 count = x_catch_errors (FRAME_X_DISPLAY (f));
8207 while (1)
8209 x_clear_errors (FRAME_X_DISPLAY (f));
8210 XTranslateCoordinates (FRAME_X_DISPLAY (f),
8212 /* From-window, to-window. */
8213 this_window,
8214 f->output_data.x->parent_desc,
8216 /* From-position, to-position. */
8217 0, 0, &win_x, &win_y,
8219 /* Child of win. */
8220 &child);
8221 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
8223 Window newroot, newparent = 0xdeadbeef;
8224 Window *newchildren;
8225 unsigned int nchildren;
8227 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
8228 &newparent, &newchildren, &nchildren))
8229 break;
8231 XFree ((char *) newchildren);
8233 f->output_data.x->parent_desc = newparent;
8235 else
8236 break;
8239 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
8240 UNBLOCK_INPUT;
8243 /* Treat negative positions as relative to the leftmost bottommost
8244 position that fits on the screen. */
8245 if (flags & XNegative)
8246 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8247 - 2 * f->output_data.x->border_width - win_x
8248 - PIXEL_WIDTH (f)
8249 + f->output_data.x->left_pos);
8252 int height = PIXEL_HEIGHT (f);
8254 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8255 /* Something is fishy here. When using Motif, starting Emacs with
8256 `-g -0-0', the frame appears too low by a few pixels.
8258 This seems to be so because initially, while Emacs is starting,
8259 the column widget's height and the frame's pixel height are
8260 different. The column widget's height is the right one. In
8261 later invocations, when Emacs is up, the frame's pixel height
8262 is right, though.
8264 It's not obvious where the initial small difference comes from.
8265 2000-12-01, gerd. */
8267 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8268 #endif
8270 if (flags & YNegative)
8271 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
8272 - 2 * f->output_data.x->border_width
8273 - win_y
8274 - height
8275 + f->output_data.x->top_pos);
8278 /* The left_pos and top_pos
8279 are now relative to the top and left screen edges,
8280 so the flags should correspond. */
8281 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
8284 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8285 to really change the position, and 0 when calling from
8286 x_make_frame_visible (in that case, XOFF and YOFF are the current
8287 position values). It is -1 when calling from x_set_frame_parameters,
8288 which means, do adjust for borders but don't change the gravity. */
8290 void
8291 x_set_offset (f, xoff, yoff, change_gravity)
8292 struct frame *f;
8293 register int xoff, yoff;
8294 int change_gravity;
8296 int modified_top, modified_left;
8298 if (change_gravity > 0)
8300 f->output_data.x->top_pos = yoff;
8301 f->output_data.x->left_pos = xoff;
8302 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
8303 if (xoff < 0)
8304 f->output_data.x->size_hint_flags |= XNegative;
8305 if (yoff < 0)
8306 f->output_data.x->size_hint_flags |= YNegative;
8307 f->output_data.x->win_gravity = NorthWestGravity;
8309 x_calc_absolute_position (f);
8311 BLOCK_INPUT;
8312 x_wm_set_size_hint (f, (long) 0, 0);
8314 modified_left = f->output_data.x->left_pos;
8315 modified_top = f->output_data.x->top_pos;
8316 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
8317 this seems to be unnecessary and incorrect. rms, 4/17/97. */
8318 /* It is a mystery why we need to add the border_width here
8319 when the frame is already visible, but experiment says we do. */
8320 if (change_gravity != 0)
8322 modified_left += f->output_data.x->border_width;
8323 modified_top += f->output_data.x->border_width;
8325 #endif
8327 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8328 modified_left, modified_top);
8329 UNBLOCK_INPUT;
8332 /* Check if we need to resize the frame due to a fullscreen request.
8333 If so needed, resize the frame. */
8334 static void
8335 x_check_fullscreen (f)
8336 struct frame *f;
8338 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
8340 int width, height, ign;
8342 x_real_positions (f, &f->output_data.x->left_pos,
8343 &f->output_data.x->top_pos);
8345 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8347 /* We do not need to move the window, it shall be taken care of
8348 when setting WM manager hints.
8349 If the frame is visible already, the position is checked by
8350 x_check_fullscreen_move. */
8351 if (f->width != width || f->height != height)
8353 change_frame_size (f, height, width, 0, 1, 0);
8354 SET_FRAME_GARBAGED (f);
8355 cancel_mouse_face (f);
8357 /* Wait for the change of frame size to occur */
8358 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
8363 /* If frame parameters are set after the frame is mapped, we need to move
8364 the window. This is done in xfns.c.
8365 Some window managers moves the window to the right position, some
8366 moves the outer window manager window to the specified position.
8367 Here we check that we are in the right spot. If not, make a second
8368 move, assuming we are dealing with the second kind of window manager. */
8369 static void
8370 x_check_fullscreen_move (f)
8371 struct frame *f;
8373 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
8375 int expect_top = f->output_data.x->top_pos;
8376 int expect_left = f->output_data.x->left_pos;
8378 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
8379 expect_top = 0;
8380 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
8381 expect_left = 0;
8383 if (expect_top != f->output_data.x->top_pos
8384 || expect_left != f->output_data.x->left_pos)
8385 x_set_offset (f, expect_left, expect_top, 1);
8387 /* Just do this once */
8388 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
8393 /* Change the size of frame F's X window to COLS/ROWS in the case F
8394 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8395 top-left-corner window gravity for this size change and subsequent
8396 size changes. Otherwise we leave the window gravity unchanged. */
8398 static void
8399 x_set_window_size_1 (f, change_gravity, cols, rows)
8400 struct frame *f;
8401 int change_gravity;
8402 int cols, rows;
8404 int pixelwidth, pixelheight;
8406 check_frame_size (f, &rows, &cols);
8407 f->output_data.x->vertical_scroll_bar_extra
8408 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8410 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
8411 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
8412 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
8414 compute_fringe_widths (f, 0);
8416 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
8417 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
8419 f->output_data.x->win_gravity = NorthWestGravity;
8420 x_wm_set_size_hint (f, (long) 0, 0);
8422 XSync (FRAME_X_DISPLAY (f), False);
8423 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8424 pixelwidth, pixelheight);
8426 /* Now, strictly speaking, we can't be sure that this is accurate,
8427 but the window manager will get around to dealing with the size
8428 change request eventually, and we'll hear how it went when the
8429 ConfigureNotify event gets here.
8431 We could just not bother storing any of this information here,
8432 and let the ConfigureNotify event set everything up, but that
8433 might be kind of confusing to the Lisp code, since size changes
8434 wouldn't be reported in the frame parameters until some random
8435 point in the future when the ConfigureNotify event arrives.
8437 We pass 1 for DELAY since we can't run Lisp code inside of
8438 a BLOCK_INPUT. */
8439 change_frame_size (f, rows, cols, 0, 1, 0);
8440 PIXEL_WIDTH (f) = pixelwidth;
8441 PIXEL_HEIGHT (f) = pixelheight;
8443 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8444 receive in the ConfigureNotify event; if we get what we asked
8445 for, then the event won't cause the screen to become garbaged, so
8446 we have to make sure to do it here. */
8447 SET_FRAME_GARBAGED (f);
8449 XFlush (FRAME_X_DISPLAY (f));
8453 /* Call this to change the size of frame F's x-window.
8454 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8455 for this size change and subsequent size changes.
8456 Otherwise we leave the window gravity unchanged. */
8458 void
8459 x_set_window_size (f, change_gravity, cols, rows)
8460 struct frame *f;
8461 int change_gravity;
8462 int cols, rows;
8464 BLOCK_INPUT;
8466 #ifdef USE_GTK
8467 if (FRAME_GTK_WIDGET (f))
8468 xg_frame_set_char_size (f, cols, rows);
8469 else
8470 x_set_window_size_1 (f, change_gravity, cols, rows);
8471 #elif USE_X_TOOLKIT
8473 if (f->output_data.x->widget != NULL)
8475 /* The x and y position of the widget is clobbered by the
8476 call to XtSetValues within EmacsFrameSetCharSize.
8477 This is a real kludge, but I don't understand Xt so I can't
8478 figure out a correct fix. Can anyone else tell me? -- rms. */
8479 int xpos = f->output_data.x->widget->core.x;
8480 int ypos = f->output_data.x->widget->core.y;
8481 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8482 f->output_data.x->widget->core.x = xpos;
8483 f->output_data.x->widget->core.y = ypos;
8485 else
8486 x_set_window_size_1 (f, change_gravity, cols, rows);
8488 #else /* not USE_X_TOOLKIT */
8490 x_set_window_size_1 (f, change_gravity, cols, rows);
8492 #endif /* not USE_X_TOOLKIT */
8494 /* If cursor was outside the new size, mark it as off. */
8495 mark_window_cursors_off (XWINDOW (f->root_window));
8497 /* Clear out any recollection of where the mouse highlighting was,
8498 since it might be in a place that's outside the new frame size.
8499 Actually checking whether it is outside is a pain in the neck,
8500 so don't try--just let the highlighting be done afresh with new size. */
8501 cancel_mouse_face (f);
8503 UNBLOCK_INPUT;
8506 /* Mouse warping. */
8508 void
8509 x_set_mouse_position (f, x, y)
8510 struct frame *f;
8511 int x, y;
8513 int pix_x, pix_y;
8515 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
8516 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
8518 if (pix_x < 0) pix_x = 0;
8519 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
8521 if (pix_y < 0) pix_y = 0;
8522 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
8524 BLOCK_INPUT;
8526 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8527 0, 0, 0, 0, pix_x, pix_y);
8528 UNBLOCK_INPUT;
8531 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8533 void
8534 x_set_mouse_pixel_position (f, pix_x, pix_y)
8535 struct frame *f;
8536 int pix_x, pix_y;
8538 BLOCK_INPUT;
8540 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8541 0, 0, 0, 0, pix_x, pix_y);
8542 UNBLOCK_INPUT;
8545 /* focus shifting, raising and lowering. */
8547 void
8548 x_focus_on_frame (f)
8549 struct frame *f;
8551 #if 0 /* This proves to be unpleasant. */
8552 x_raise_frame (f);
8553 #endif
8554 #if 0
8555 /* I don't think that the ICCCM allows programs to do things like this
8556 without the interaction of the window manager. Whatever you end up
8557 doing with this code, do it to x_unfocus_frame too. */
8558 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8559 RevertToPointerRoot, CurrentTime);
8560 #endif /* ! 0 */
8563 void
8564 x_unfocus_frame (f)
8565 struct frame *f;
8567 #if 0
8568 /* Look at the remarks in x_focus_on_frame. */
8569 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8570 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8571 RevertToPointerRoot, CurrentTime);
8572 #endif /* ! 0 */
8575 /* Raise frame F. */
8577 void
8578 x_raise_frame (f)
8579 struct frame *f;
8581 if (f->async_visible)
8583 BLOCK_INPUT;
8584 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8585 XFlush (FRAME_X_DISPLAY (f));
8586 UNBLOCK_INPUT;
8590 /* Lower frame F. */
8592 void
8593 x_lower_frame (f)
8594 struct frame *f;
8596 if (f->async_visible)
8598 BLOCK_INPUT;
8599 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8600 XFlush (FRAME_X_DISPLAY (f));
8601 UNBLOCK_INPUT;
8605 static void
8606 XTframe_raise_lower (f, raise_flag)
8607 FRAME_PTR f;
8608 int raise_flag;
8610 if (raise_flag)
8611 x_raise_frame (f);
8612 else
8613 x_lower_frame (f);
8616 /* Change of visibility. */
8618 /* This tries to wait until the frame is really visible.
8619 However, if the window manager asks the user where to position
8620 the frame, this will return before the user finishes doing that.
8621 The frame will not actually be visible at that time,
8622 but it will become visible later when the window manager
8623 finishes with it. */
8625 void
8626 x_make_frame_visible (f)
8627 struct frame *f;
8629 Lisp_Object type;
8630 int original_top, original_left;
8631 int retry_count = 2;
8633 retry:
8635 BLOCK_INPUT;
8637 type = x_icon_type (f);
8638 if (!NILP (type))
8639 x_bitmap_icon (f, type);
8641 if (! FRAME_VISIBLE_P (f))
8643 /* We test FRAME_GARBAGED_P here to make sure we don't
8644 call x_set_offset a second time
8645 if we get to x_make_frame_visible a second time
8646 before the window gets really visible. */
8647 if (! FRAME_ICONIFIED_P (f)
8648 && ! f->output_data.x->asked_for_visible)
8649 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
8651 f->output_data.x->asked_for_visible = 1;
8653 if (! EQ (Vx_no_window_manager, Qt))
8654 x_wm_set_window_state (f, NormalState);
8655 #ifdef USE_X_TOOLKIT
8656 /* This was XtPopup, but that did nothing for an iconified frame. */
8657 XtMapWidget (f->output_data.x->widget);
8658 #else /* not USE_X_TOOLKIT */
8659 #ifdef USE_GTK
8660 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8661 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8662 #else
8663 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8664 #endif /* not USE_GTK */
8665 #endif /* not USE_X_TOOLKIT */
8666 #if 0 /* This seems to bring back scroll bars in the wrong places
8667 if the window configuration has changed. They seem
8668 to come back ok without this. */
8669 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8670 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8671 #endif
8674 XFlush (FRAME_X_DISPLAY (f));
8676 /* Synchronize to ensure Emacs knows the frame is visible
8677 before we do anything else. We do this loop with input not blocked
8678 so that incoming events are handled. */
8680 Lisp_Object frame;
8681 int count;
8682 /* This must be before UNBLOCK_INPUT
8683 since events that arrive in response to the actions above
8684 will set it when they are handled. */
8685 int previously_visible = f->output_data.x->has_been_visible;
8687 original_left = f->output_data.x->left_pos;
8688 original_top = f->output_data.x->top_pos;
8690 /* This must come after we set COUNT. */
8691 UNBLOCK_INPUT;
8693 /* We unblock here so that arriving X events are processed. */
8695 /* Now move the window back to where it was "supposed to be".
8696 But don't do it if the gravity is negative.
8697 When the gravity is negative, this uses a position
8698 that is 3 pixels too low. Perhaps that's really the border width.
8700 Don't do this if the window has never been visible before,
8701 because the window manager may choose the position
8702 and we don't want to override it. */
8704 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
8705 && f->output_data.x->win_gravity == NorthWestGravity
8706 && previously_visible)
8708 Drawable rootw;
8709 int x, y;
8710 unsigned int width, height, border, depth;
8712 BLOCK_INPUT;
8714 /* On some window managers (such as FVWM) moving an existing
8715 window, even to the same place, causes the window manager
8716 to introduce an offset. This can cause the window to move
8717 to an unexpected location. Check the geometry (a little
8718 slow here) and then verify that the window is in the right
8719 place. If the window is not in the right place, move it
8720 there, and take the potential window manager hit. */
8721 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8722 &rootw, &x, &y, &width, &height, &border, &depth);
8724 if (original_left != x || original_top != y)
8725 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8726 original_left, original_top);
8728 UNBLOCK_INPUT;
8731 XSETFRAME (frame, f);
8733 /* Wait until the frame is visible. Process X events until a
8734 MapNotify event has been seen, or until we think we won't get a
8735 MapNotify at all.. */
8736 for (count = input_signal_count + 10;
8737 input_signal_count < count && !FRAME_VISIBLE_P (f);)
8739 /* Force processing of queued events. */
8740 x_sync (f);
8742 /* Machines that do polling rather than SIGIO have been
8743 observed to go into a busy-wait here. So we'll fake an
8744 alarm signal to let the handler know that there's something
8745 to be read. We used to raise a real alarm, but it seems
8746 that the handler isn't always enabled here. This is
8747 probably a bug. */
8748 if (input_polling_used ())
8750 /* It could be confusing if a real alarm arrives while
8751 processing the fake one. Turn it off and let the
8752 handler reset it. */
8753 extern void poll_for_input_1 P_ ((void));
8754 int old_poll_suppress_count = poll_suppress_count;
8755 poll_suppress_count = 1;
8756 poll_for_input_1 ();
8757 poll_suppress_count = old_poll_suppress_count;
8760 /* See if a MapNotify event has been processed. */
8761 FRAME_SAMPLE_VISIBILITY (f);
8764 /* 2000-09-28: In
8766 (let ((f (selected-frame)))
8767 (iconify-frame f)
8768 (raise-frame f))
8770 the frame is not raised with various window managers on
8771 FreeBSD, Linux and Solaris. It turns out that, for some
8772 unknown reason, the call to XtMapWidget is completely ignored.
8773 Mapping the widget a second time works. */
8775 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
8776 goto retry;
8780 /* Change from mapped state to withdrawn state. */
8782 /* Make the frame visible (mapped and not iconified). */
8784 void
8785 x_make_frame_invisible (f)
8786 struct frame *f;
8788 Window window;
8790 /* Use the frame's outermost window, not the one we normally draw on. */
8791 window = FRAME_OUTER_WINDOW (f);
8793 /* Don't keep the highlight on an invisible frame. */
8794 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8795 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
8797 #if 0/* This might add unreliability; I don't trust it -- rms. */
8798 if (! f->async_visible && ! f->async_iconified)
8799 return;
8800 #endif
8802 BLOCK_INPUT;
8804 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
8805 that the current position of the window is user-specified, rather than
8806 program-specified, so that when the window is mapped again, it will be
8807 placed at the same location, without forcing the user to position it
8808 by hand again (they have already done that once for this window.) */
8809 x_wm_set_size_hint (f, (long) 0, 1);
8811 #ifdef USE_GTK
8812 if (FRAME_GTK_OUTER_WIDGET (f))
8813 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
8814 else
8815 #endif
8817 #ifdef HAVE_X11R4
8819 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
8820 DefaultScreen (FRAME_X_DISPLAY (f))))
8822 UNBLOCK_INPUT_RESIGNAL;
8823 error ("Can't notify window manager of window withdrawal");
8825 #else /* ! defined (HAVE_X11R4) */
8827 /* Tell the window manager what we're going to do. */
8828 if (! EQ (Vx_no_window_manager, Qt))
8830 XEvent unmap;
8832 unmap.xunmap.type = UnmapNotify;
8833 unmap.xunmap.window = window;
8834 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
8835 unmap.xunmap.from_configure = False;
8836 if (! XSendEvent (FRAME_X_DISPLAY (f),
8837 DefaultRootWindow (FRAME_X_DISPLAY (f)),
8838 False,
8839 SubstructureRedirectMaskSubstructureNotifyMask,
8840 &unmap))
8842 UNBLOCK_INPUT_RESIGNAL;
8843 error ("Can't notify window manager of withdrawal");
8847 /* Unmap the window ourselves. Cheeky! */
8848 XUnmapWindow (FRAME_X_DISPLAY (f), window);
8849 #endif /* ! defined (HAVE_X11R4) */
8852 /* We can't distinguish this from iconification
8853 just by the event that we get from the server.
8854 So we can't win using the usual strategy of letting
8855 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
8856 and synchronize with the server to make sure we agree. */
8857 f->visible = 0;
8858 FRAME_ICONIFIED_P (f) = 0;
8859 f->async_visible = 0;
8860 f->async_iconified = 0;
8862 x_sync (f);
8864 UNBLOCK_INPUT;
8867 /* Change window state from mapped to iconified. */
8869 void
8870 x_iconify_frame (f)
8871 struct frame *f;
8873 int result;
8874 Lisp_Object type;
8876 /* Don't keep the highlight on an invisible frame. */
8877 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8878 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
8880 if (f->async_iconified)
8881 return;
8883 BLOCK_INPUT;
8885 FRAME_SAMPLE_VISIBILITY (f);
8887 type = x_icon_type (f);
8888 if (!NILP (type))
8889 x_bitmap_icon (f, type);
8891 #ifdef USE_GTK
8892 if (FRAME_GTK_OUTER_WIDGET (f))
8894 if (! FRAME_VISIBLE_P (f))
8895 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8897 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8898 f->iconified = 1;
8899 f->visible = 1;
8900 f->async_iconified = 1;
8901 f->async_visible = 0;
8902 UNBLOCK_INPUT;
8903 return;
8905 #endif
8907 #ifdef USE_X_TOOLKIT
8909 if (! FRAME_VISIBLE_P (f))
8911 if (! EQ (Vx_no_window_manager, Qt))
8912 x_wm_set_window_state (f, IconicState);
8913 /* This was XtPopup, but that did nothing for an iconified frame. */
8914 XtMapWidget (f->output_data.x->widget);
8915 /* The server won't give us any event to indicate
8916 that an invisible frame was changed to an icon,
8917 so we have to record it here. */
8918 f->iconified = 1;
8919 f->visible = 1;
8920 f->async_iconified = 1;
8921 f->async_visible = 0;
8922 UNBLOCK_INPUT;
8923 return;
8926 result = XIconifyWindow (FRAME_X_DISPLAY (f),
8927 XtWindow (f->output_data.x->widget),
8928 DefaultScreen (FRAME_X_DISPLAY (f)));
8929 UNBLOCK_INPUT;
8931 if (!result)
8932 error ("Can't notify window manager of iconification");
8934 f->async_iconified = 1;
8935 f->async_visible = 0;
8938 BLOCK_INPUT;
8939 XFlush (FRAME_X_DISPLAY (f));
8940 UNBLOCK_INPUT;
8941 #else /* not USE_X_TOOLKIT */
8943 /* Make sure the X server knows where the window should be positioned,
8944 in case the user deiconifies with the window manager. */
8945 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
8946 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
8948 /* Since we don't know which revision of X we're running, we'll use both
8949 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
8951 /* X11R4: send a ClientMessage to the window manager using the
8952 WM_CHANGE_STATE type. */
8954 XEvent message;
8956 message.xclient.window = FRAME_X_WINDOW (f);
8957 message.xclient.type = ClientMessage;
8958 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
8959 message.xclient.format = 32;
8960 message.xclient.data.l[0] = IconicState;
8962 if (! XSendEvent (FRAME_X_DISPLAY (f),
8963 DefaultRootWindow (FRAME_X_DISPLAY (f)),
8964 False,
8965 SubstructureRedirectMask | SubstructureNotifyMask,
8966 &message))
8968 UNBLOCK_INPUT_RESIGNAL;
8969 error ("Can't notify window manager of iconification");
8973 /* X11R3: set the initial_state field of the window manager hints to
8974 IconicState. */
8975 x_wm_set_window_state (f, IconicState);
8977 if (!FRAME_VISIBLE_P (f))
8979 /* If the frame was withdrawn, before, we must map it. */
8980 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8983 f->async_iconified = 1;
8984 f->async_visible = 0;
8986 XFlush (FRAME_X_DISPLAY (f));
8987 UNBLOCK_INPUT;
8988 #endif /* not USE_X_TOOLKIT */
8992 /* Free X resources of frame F. */
8994 void
8995 x_free_frame_resources (f)
8996 struct frame *f;
8998 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8999 Lisp_Object bar;
9000 struct scroll_bar *b;
9002 BLOCK_INPUT;
9004 /* If a display connection is dead, don't try sending more
9005 commands to the X server. */
9006 if (dpyinfo->display)
9008 if (f->output_data.x->icon_desc)
9009 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9011 #ifdef USE_X_TOOLKIT
9012 /* Explicitly destroy the scroll bars of the frame. Without
9013 this, we get "BadDrawable" errors from the toolkit later on,
9014 presumably from expose events generated for the disappearing
9015 toolkit scroll bars. */
9016 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9018 b = XSCROLL_BAR (bar);
9019 x_scroll_bar_remove (b);
9021 #endif
9023 #ifdef HAVE_X_I18N
9024 if (FRAME_XIC (f))
9025 free_frame_xic (f);
9026 #endif
9028 #ifdef USE_X_TOOLKIT
9029 if (f->output_data.x->widget)
9031 XtDestroyWidget (f->output_data.x->widget);
9032 f->output_data.x->widget = NULL;
9034 /* Tooltips don't have widgets, only a simple X window, even if
9035 we are using a toolkit. */
9036 else if (FRAME_X_WINDOW (f))
9037 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9039 free_frame_menubar (f);
9040 #else /* !USE_X_TOOLKIT */
9042 #ifdef USE_GTK
9043 /* In the GTK version, tooltips are normal X
9044 frames. We must check and free both types. */
9045 if (FRAME_GTK_OUTER_WIDGET (f))
9047 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9048 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9049 FRAME_GTK_OUTER_WIDGET (f) = 0;
9051 #endif /* USE_GTK */
9053 if (FRAME_X_WINDOW (f))
9054 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9055 #endif /* !USE_X_TOOLKIT */
9057 unload_color (f, f->output_data.x->foreground_pixel);
9058 unload_color (f, f->output_data.x->background_pixel);
9059 unload_color (f, f->output_data.x->cursor_pixel);
9060 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9061 unload_color (f, f->output_data.x->border_pixel);
9062 unload_color (f, f->output_data.x->mouse_pixel);
9064 if (f->output_data.x->scroll_bar_background_pixel != -1)
9065 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9066 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9067 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9068 #ifdef USE_TOOLKIT_SCROLL_BARS
9069 /* Scrollbar shadow colors. */
9070 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9071 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9072 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9073 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9074 #endif /* USE_TOOLKIT_SCROLL_BARS */
9075 if (f->output_data.x->white_relief.allocated_p)
9076 unload_color (f, f->output_data.x->white_relief.pixel);
9077 if (f->output_data.x->black_relief.allocated_p)
9078 unload_color (f, f->output_data.x->black_relief.pixel);
9080 if (FRAME_FACE_CACHE (f))
9081 free_frame_faces (f);
9083 x_free_gcs (f);
9084 XFlush (FRAME_X_DISPLAY (f));
9087 if (f->output_data.x->saved_menu_event)
9088 xfree (f->output_data.x->saved_menu_event);
9090 xfree (f->output_data.x);
9091 f->output_data.x = NULL;
9093 if (f == dpyinfo->x_focus_frame)
9094 dpyinfo->x_focus_frame = 0;
9095 if (f == dpyinfo->x_focus_event_frame)
9096 dpyinfo->x_focus_event_frame = 0;
9097 if (f == dpyinfo->x_highlight_frame)
9098 dpyinfo->x_highlight_frame = 0;
9100 if (f == dpyinfo->mouse_face_mouse_frame)
9102 dpyinfo->mouse_face_beg_row
9103 = dpyinfo->mouse_face_beg_col = -1;
9104 dpyinfo->mouse_face_end_row
9105 = dpyinfo->mouse_face_end_col = -1;
9106 dpyinfo->mouse_face_window = Qnil;
9107 dpyinfo->mouse_face_deferred_gc = 0;
9108 dpyinfo->mouse_face_mouse_frame = 0;
9111 UNBLOCK_INPUT;
9115 /* Destroy the X window of frame F. */
9117 void
9118 x_destroy_window (f)
9119 struct frame *f;
9121 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9123 /* If a display connection is dead, don't try sending more
9124 commands to the X server. */
9125 if (dpyinfo->display != 0)
9126 x_free_frame_resources (f);
9128 dpyinfo->reference_count--;
9132 /* Setting window manager hints. */
9134 /* Set the normal size hints for the window manager, for frame F.
9135 FLAGS is the flags word to use--or 0 meaning preserve the flags
9136 that the window now has.
9137 If USER_POSITION is nonzero, we set the USPosition
9138 flag (this is useful when FLAGS is 0).
9139 The GTK version is in gtkutils.c */
9141 #ifndef USE_GTK
9142 void
9143 x_wm_set_size_hint (f, flags, user_position)
9144 struct frame *f;
9145 long flags;
9146 int user_position;
9148 XSizeHints size_hints;
9150 #ifdef USE_X_TOOLKIT
9151 Arg al[2];
9152 int ac = 0;
9153 Dimension widget_width, widget_height;
9154 #endif
9156 Window window = FRAME_OUTER_WINDOW (f);
9158 /* Setting PMaxSize caused various problems. */
9159 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9161 size_hints.x = f->output_data.x->left_pos;
9162 size_hints.y = f->output_data.x->top_pos;
9164 #ifdef USE_X_TOOLKIT
9165 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9166 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9167 XtGetValues (f->output_data.x->widget, al, ac);
9168 size_hints.height = widget_height;
9169 size_hints.width = widget_width;
9170 #else /* not USE_X_TOOLKIT */
9171 size_hints.height = PIXEL_HEIGHT (f);
9172 size_hints.width = PIXEL_WIDTH (f);
9173 #endif /* not USE_X_TOOLKIT */
9175 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
9176 size_hints.height_inc = f->output_data.x->line_height;
9177 size_hints.max_width
9178 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
9179 size_hints.max_height
9180 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
9182 /* Calculate the base and minimum sizes.
9184 (When we use the X toolkit, we don't do it here.
9185 Instead we copy the values that the widgets are using, below.) */
9186 #ifndef USE_X_TOOLKIT
9188 int base_width, base_height;
9189 int min_rows = 0, min_cols = 0;
9191 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
9192 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
9194 check_frame_size (f, &min_rows, &min_cols);
9196 /* The window manager uses the base width hints to calculate the
9197 current number of rows and columns in the frame while
9198 resizing; min_width and min_height aren't useful for this
9199 purpose, since they might not give the dimensions for a
9200 zero-row, zero-column frame.
9202 We use the base_width and base_height members if we have
9203 them; otherwise, we set the min_width and min_height members
9204 to the size for a zero x zero frame. */
9206 #ifdef HAVE_X11R4
9207 size_hints.flags |= PBaseSize;
9208 size_hints.base_width = base_width;
9209 size_hints.base_height = base_height;
9210 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9211 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9212 #else
9213 size_hints.min_width = base_width;
9214 size_hints.min_height = base_height;
9215 #endif
9218 /* If we don't need the old flags, we don't need the old hint at all. */
9219 if (flags)
9221 size_hints.flags |= flags;
9222 goto no_read;
9224 #endif /* not USE_X_TOOLKIT */
9227 XSizeHints hints; /* Sometimes I hate X Windows... */
9228 long supplied_return;
9229 int value;
9231 #ifdef HAVE_X11R4
9232 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9233 &supplied_return);
9234 #else
9235 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
9236 #endif
9238 #ifdef USE_X_TOOLKIT
9239 size_hints.base_height = hints.base_height;
9240 size_hints.base_width = hints.base_width;
9241 size_hints.min_height = hints.min_height;
9242 size_hints.min_width = hints.min_width;
9243 #endif
9245 if (flags)
9246 size_hints.flags |= flags;
9247 else
9249 if (value == 0)
9250 hints.flags = 0;
9251 if (hints.flags & PSize)
9252 size_hints.flags |= PSize;
9253 if (hints.flags & PPosition)
9254 size_hints.flags |= PPosition;
9255 if (hints.flags & USPosition)
9256 size_hints.flags |= USPosition;
9257 if (hints.flags & USSize)
9258 size_hints.flags |= USSize;
9262 #ifndef USE_X_TOOLKIT
9263 no_read:
9264 #endif
9266 #ifdef PWinGravity
9267 size_hints.win_gravity = f->output_data.x->win_gravity;
9268 size_hints.flags |= PWinGravity;
9270 if (user_position)
9272 size_hints.flags &= ~ PPosition;
9273 size_hints.flags |= USPosition;
9275 #endif /* PWinGravity */
9277 #ifdef HAVE_X11R4
9278 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9279 #else
9280 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9281 #endif
9283 #endif /* not USE_GTK */
9285 /* Used for IconicState or NormalState */
9287 void
9288 x_wm_set_window_state (f, state)
9289 struct frame *f;
9290 int state;
9292 #ifdef USE_X_TOOLKIT
9293 Arg al[1];
9295 XtSetArg (al[0], XtNinitialState, state);
9296 XtSetValues (f->output_data.x->widget, al, 1);
9297 #else /* not USE_X_TOOLKIT */
9298 Window window = FRAME_X_WINDOW (f);
9300 f->output_data.x->wm_hints.flags |= StateHint;
9301 f->output_data.x->wm_hints.initial_state = state;
9303 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9304 #endif /* not USE_X_TOOLKIT */
9307 void
9308 x_wm_set_icon_pixmap (f, pixmap_id)
9309 struct frame *f;
9310 int pixmap_id;
9312 Pixmap icon_pixmap;
9314 #ifndef USE_X_TOOLKIT
9315 Window window = FRAME_OUTER_WINDOW (f);
9316 #endif
9318 if (pixmap_id > 0)
9320 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9321 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9323 else
9325 /* It seems there is no way to turn off use of an icon pixmap.
9326 The following line does it, only if no icon has yet been created,
9327 for some window managers. But with mwm it crashes.
9328 Some people say it should clear the IconPixmapHint bit in this case,
9329 but that doesn't work, and the X consortium said it isn't the
9330 right thing at all. Since there is no way to win,
9331 best to explicitly give up. */
9332 #if 0
9333 f->output_data.x->wm_hints.icon_pixmap = None;
9334 #else
9335 return;
9336 #endif
9339 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
9342 Arg al[1];
9343 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9344 XtSetValues (f->output_data.x->widget, al, 1);
9347 #else /* not USE_X_TOOLKIT */
9349 f->output_data.x->wm_hints.flags |= IconPixmapHint;
9350 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9352 #endif /* not USE_X_TOOLKIT */
9355 void
9356 x_wm_set_icon_position (f, icon_x, icon_y)
9357 struct frame *f;
9358 int icon_x, icon_y;
9360 Window window = FRAME_OUTER_WINDOW (f);
9362 f->output_data.x->wm_hints.flags |= IconPositionHint;
9363 f->output_data.x->wm_hints.icon_x = icon_x;
9364 f->output_data.x->wm_hints.icon_y = icon_y;
9366 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9370 /***********************************************************************
9371 Fonts
9372 ***********************************************************************/
9374 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
9376 struct font_info *
9377 x_get_font_info (f, font_idx)
9378 FRAME_PTR f;
9379 int font_idx;
9381 return (FRAME_X_FONT_TABLE (f) + font_idx);
9385 /* Return a list of names of available fonts matching PATTERN on frame F.
9387 If SIZE is > 0, it is the size (maximum bounds width) of fonts
9388 to be listed.
9390 SIZE < 0 means include scalable fonts.
9392 Frame F null means we have not yet created any frame on X, and
9393 consult the first display in x_display_list. MAXNAMES sets a limit
9394 on how many fonts to match. */
9396 Lisp_Object
9397 x_list_fonts (f, pattern, size, maxnames)
9398 struct frame *f;
9399 Lisp_Object pattern;
9400 int size;
9401 int maxnames;
9403 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
9404 Lisp_Object tem, second_best;
9405 struct x_display_info *dpyinfo
9406 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
9407 Display *dpy = dpyinfo->display;
9408 int try_XLoadQueryFont = 0;
9409 int count;
9410 int allow_scalable_fonts_p = 0;
9412 if (size < 0)
9414 allow_scalable_fonts_p = 1;
9415 size = 0;
9418 patterns = Fassoc (pattern, Valternate_fontname_alist);
9419 if (NILP (patterns))
9420 patterns = Fcons (pattern, Qnil);
9422 if (maxnames == 1 && !size)
9423 /* We can return any single font matching PATTERN. */
9424 try_XLoadQueryFont = 1;
9426 for (; CONSP (patterns); patterns = XCDR (patterns))
9428 int num_fonts;
9429 char **names = NULL;
9431 pattern = XCAR (patterns);
9432 /* See if we cached the result for this particular query.
9433 The cache is an alist of the form:
9434 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
9435 tem = XCDR (dpyinfo->name_list_element);
9436 key = Fcons (Fcons (pattern, make_number (maxnames)),
9437 allow_scalable_fonts_p ? Qt : Qnil);
9438 list = Fassoc (key, tem);
9439 if (!NILP (list))
9441 list = Fcdr_safe (list);
9442 /* We have a cashed list. Don't have to get the list again. */
9443 goto label_cached;
9446 /* At first, put PATTERN in the cache. */
9448 BLOCK_INPUT;
9449 count = x_catch_errors (dpy);
9451 if (try_XLoadQueryFont)
9453 XFontStruct *font;
9454 unsigned long value;
9456 font = XLoadQueryFont (dpy, SDATA (pattern));
9457 if (x_had_errors_p (dpy))
9459 /* This error is perhaps due to insufficient memory on X
9460 server. Let's just ignore it. */
9461 font = NULL;
9462 x_clear_errors (dpy);
9465 if (font
9466 && XGetFontProperty (font, XA_FONT, &value))
9468 char *name = (char *) XGetAtomName (dpy, (Atom) value);
9469 int len = strlen (name);
9470 char *tmp;
9472 /* If DXPC (a Differential X Protocol Compressor)
9473 Ver.3.7 is running, XGetAtomName will return null
9474 string. We must avoid such a name. */
9475 if (len == 0)
9476 try_XLoadQueryFont = 0;
9477 else
9479 num_fonts = 1;
9480 names = (char **) alloca (sizeof (char *));
9481 /* Some systems only allow alloca assigned to a
9482 simple var. */
9483 tmp = (char *) alloca (len + 1); names[0] = tmp;
9484 bcopy (name, names[0], len + 1);
9485 XFree (name);
9488 else
9489 try_XLoadQueryFont = 0;
9491 if (font)
9492 XFreeFont (dpy, font);
9495 if (!try_XLoadQueryFont)
9497 /* We try at least 10 fonts because XListFonts will return
9498 auto-scaled fonts at the head. */
9499 if (maxnames < 0)
9501 int limit;
9503 for (limit = 500;;)
9505 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
9506 if (num_fonts == limit)
9508 BLOCK_INPUT;
9509 XFreeFontNames (names);
9510 UNBLOCK_INPUT;
9511 limit *= 2;
9513 else
9514 break;
9517 else
9518 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
9519 &num_fonts);
9521 if (x_had_errors_p (dpy))
9523 /* This error is perhaps due to insufficient memory on X
9524 server. Let's just ignore it. */
9525 names = NULL;
9526 x_clear_errors (dpy);
9530 x_uncatch_errors (dpy, count);
9531 UNBLOCK_INPUT;
9533 if (names)
9535 int i;
9537 /* Make a list of all the fonts we got back.
9538 Store that in the font cache for the display. */
9539 for (i = 0; i < num_fonts; i++)
9541 int width = 0;
9542 char *p = names[i];
9543 int average_width = -1, dashes = 0;
9545 /* Count the number of dashes in NAMES[I]. If there are
9546 14 dashes, and the field value following 12th dash
9547 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
9548 is usually too ugly to be used for editing. Let's
9549 ignore it. */
9550 while (*p)
9551 if (*p++ == '-')
9553 dashes++;
9554 if (dashes == 7) /* PIXEL_SIZE field */
9555 width = atoi (p);
9556 else if (dashes == 12) /* AVERAGE_WIDTH field */
9557 average_width = atoi (p);
9560 if (allow_scalable_fonts_p
9561 || dashes < 14 || average_width != 0)
9563 tem = build_string (names[i]);
9564 if (NILP (Fassoc (tem, list)))
9566 if (STRINGP (Vx_pixel_size_width_font_regexp)
9567 && ((fast_c_string_match_ignore_case
9568 (Vx_pixel_size_width_font_regexp, names[i]))
9569 >= 0))
9570 /* We can set the value of PIXEL_SIZE to the
9571 width of this font. */
9572 list = Fcons (Fcons (tem, make_number (width)), list);
9573 else
9574 /* For the moment, width is not known. */
9575 list = Fcons (Fcons (tem, Qnil), list);
9580 if (!try_XLoadQueryFont)
9582 BLOCK_INPUT;
9583 XFreeFontNames (names);
9584 UNBLOCK_INPUT;
9588 /* Now store the result in the cache. */
9589 XSETCDR (dpyinfo->name_list_element,
9590 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
9592 label_cached:
9593 if (NILP (list)) continue; /* Try the remaining alternatives. */
9595 newlist = second_best = Qnil;
9596 /* Make a list of the fonts that have the right width. */
9597 for (; CONSP (list); list = XCDR (list))
9599 int found_size;
9601 tem = XCAR (list);
9603 if (!CONSP (tem) || NILP (XCAR (tem)))
9604 continue;
9605 if (!size)
9607 newlist = Fcons (XCAR (tem), newlist);
9608 continue;
9611 if (!INTEGERP (XCDR (tem)))
9613 /* Since we have not yet known the size of this font, we
9614 must try slow function call XLoadQueryFont. */
9615 XFontStruct *thisinfo;
9617 BLOCK_INPUT;
9618 count = x_catch_errors (dpy);
9619 thisinfo = XLoadQueryFont (dpy,
9620 SDATA (XCAR (tem)));
9621 if (x_had_errors_p (dpy))
9623 /* This error is perhaps due to insufficient memory on X
9624 server. Let's just ignore it. */
9625 thisinfo = NULL;
9626 x_clear_errors (dpy);
9628 x_uncatch_errors (dpy, count);
9629 UNBLOCK_INPUT;
9631 if (thisinfo)
9633 XSETCDR (tem,
9634 (thisinfo->min_bounds.width == 0
9635 ? make_number (0)
9636 : make_number (thisinfo->max_bounds.width)));
9637 BLOCK_INPUT;
9638 XFreeFont (dpy, thisinfo);
9639 UNBLOCK_INPUT;
9641 else
9642 /* For unknown reason, the previous call of XListFont had
9643 returned a font which can't be opened. Record the size
9644 as 0 not to try to open it again. */
9645 XSETCDR (tem, make_number (0));
9648 found_size = XINT (XCDR (tem));
9649 if (found_size == size)
9650 newlist = Fcons (XCAR (tem), newlist);
9651 else if (found_size > 0)
9653 if (NILP (second_best))
9654 second_best = tem;
9655 else if (found_size < size)
9657 if (XINT (XCDR (second_best)) > size
9658 || XINT (XCDR (second_best)) < found_size)
9659 second_best = tem;
9661 else
9663 if (XINT (XCDR (second_best)) > size
9664 && XINT (XCDR (second_best)) > found_size)
9665 second_best = tem;
9669 if (!NILP (newlist))
9670 break;
9671 else if (!NILP (second_best))
9673 newlist = Fcons (XCAR (second_best), Qnil);
9674 break;
9678 return newlist;
9682 #if GLYPH_DEBUG
9684 /* Check that FONT is valid on frame F. It is if it can be found in F's
9685 font table. */
9687 static void
9688 x_check_font (f, font)
9689 struct frame *f;
9690 XFontStruct *font;
9692 int i;
9693 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9695 xassert (font != NULL);
9697 for (i = 0; i < dpyinfo->n_fonts; i++)
9698 if (dpyinfo->font_table[i].name
9699 && font == dpyinfo->font_table[i].font)
9700 break;
9702 xassert (i < dpyinfo->n_fonts);
9705 #endif /* GLYPH_DEBUG != 0 */
9707 /* Set *W to the minimum width, *H to the minimum font height of FONT.
9708 Note: There are (broken) X fonts out there with invalid XFontStruct
9709 min_bounds contents. For example, handa@etl.go.jp reports that
9710 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
9711 have font->min_bounds.width == 0. */
9713 static INLINE void
9714 x_font_min_bounds (font, w, h)
9715 XFontStruct *font;
9716 int *w, *h;
9718 *h = FONT_HEIGHT (font);
9719 *w = font->min_bounds.width;
9721 /* Try to handle the case where FONT->min_bounds has invalid
9722 contents. Since the only font known to have invalid min_bounds
9723 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
9724 if (*w <= 0)
9725 *w = font->max_bounds.width;
9729 /* Compute the smallest character width and smallest font height over
9730 all fonts available on frame F. Set the members smallest_char_width
9731 and smallest_font_height in F's x_display_info structure to
9732 the values computed. Value is non-zero if smallest_font_height or
9733 smallest_char_width become smaller than they were before. */
9735 static int
9736 x_compute_min_glyph_bounds (f)
9737 struct frame *f;
9739 int i;
9740 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9741 XFontStruct *font;
9742 int old_width = dpyinfo->smallest_char_width;
9743 int old_height = dpyinfo->smallest_font_height;
9745 dpyinfo->smallest_font_height = 100000;
9746 dpyinfo->smallest_char_width = 100000;
9748 for (i = 0; i < dpyinfo->n_fonts; ++i)
9749 if (dpyinfo->font_table[i].name)
9751 struct font_info *fontp = dpyinfo->font_table + i;
9752 int w, h;
9754 font = (XFontStruct *) fontp->font;
9755 xassert (font != (XFontStruct *) ~0);
9756 x_font_min_bounds (font, &w, &h);
9758 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
9759 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
9762 xassert (dpyinfo->smallest_char_width > 0
9763 && dpyinfo->smallest_font_height > 0);
9765 return (dpyinfo->n_fonts == 1
9766 || dpyinfo->smallest_char_width < old_width
9767 || dpyinfo->smallest_font_height < old_height);
9771 /* Load font named FONTNAME of the size SIZE for frame F, and return a
9772 pointer to the structure font_info while allocating it dynamically.
9773 If SIZE is 0, load any size of font.
9774 If loading is failed, return NULL. */
9776 struct font_info *
9777 x_load_font (f, fontname, size)
9778 struct frame *f;
9779 register char *fontname;
9780 int size;
9782 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9783 Lisp_Object font_names;
9784 int count;
9786 /* Get a list of all the fonts that match this name. Once we
9787 have a list of matching fonts, we compare them against the fonts
9788 we already have by comparing names. */
9789 font_names = x_list_fonts (f, build_string (fontname), size, 1);
9791 if (!NILP (font_names))
9793 Lisp_Object tail;
9794 int i;
9796 for (i = 0; i < dpyinfo->n_fonts; i++)
9797 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
9798 if (dpyinfo->font_table[i].name
9799 && (!strcmp (dpyinfo->font_table[i].name,
9800 SDATA (XCAR (tail)))
9801 || !strcmp (dpyinfo->font_table[i].full_name,
9802 SDATA (XCAR (tail)))))
9803 return (dpyinfo->font_table + i);
9806 /* Load the font and add it to the table. */
9808 char *full_name;
9809 XFontStruct *font;
9810 struct font_info *fontp;
9811 unsigned long value;
9812 int i;
9814 /* If we have found fonts by x_list_font, load one of them. If
9815 not, we still try to load a font by the name given as FONTNAME
9816 because XListFonts (called in x_list_font) of some X server has
9817 a bug of not finding a font even if the font surely exists and
9818 is loadable by XLoadQueryFont. */
9819 if (size > 0 && !NILP (font_names))
9820 fontname = (char *) SDATA (XCAR (font_names));
9822 BLOCK_INPUT;
9823 count = x_catch_errors (FRAME_X_DISPLAY (f));
9824 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
9825 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
9827 /* This error is perhaps due to insufficient memory on X
9828 server. Let's just ignore it. */
9829 font = NULL;
9830 x_clear_errors (FRAME_X_DISPLAY (f));
9832 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
9833 UNBLOCK_INPUT;
9834 if (!font)
9835 return NULL;
9837 /* Find a free slot in the font table. */
9838 for (i = 0; i < dpyinfo->n_fonts; ++i)
9839 if (dpyinfo->font_table[i].name == NULL)
9840 break;
9842 /* If no free slot found, maybe enlarge the font table. */
9843 if (i == dpyinfo->n_fonts
9844 && dpyinfo->n_fonts == dpyinfo->font_table_size)
9846 int sz;
9847 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
9848 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
9849 dpyinfo->font_table
9850 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
9853 fontp = dpyinfo->font_table + i;
9854 if (i == dpyinfo->n_fonts)
9855 ++dpyinfo->n_fonts;
9857 /* Now fill in the slots of *FONTP. */
9858 BLOCK_INPUT;
9859 fontp->font = font;
9860 fontp->font_idx = i;
9861 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
9862 bcopy (fontname, fontp->name, strlen (fontname) + 1);
9864 /* Try to get the full name of FONT. Put it in FULL_NAME. */
9865 full_name = 0;
9866 if (XGetFontProperty (font, XA_FONT, &value))
9868 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
9869 char *p = name;
9870 int dashes = 0;
9872 /* Count the number of dashes in the "full name".
9873 If it is too few, this isn't really the font's full name,
9874 so don't use it.
9875 In X11R4, the fonts did not come with their canonical names
9876 stored in them. */
9877 while (*p)
9879 if (*p == '-')
9880 dashes++;
9881 p++;
9884 if (dashes >= 13)
9886 full_name = (char *) xmalloc (p - name + 1);
9887 bcopy (name, full_name, p - name + 1);
9890 XFree (name);
9893 if (full_name != 0)
9894 fontp->full_name = full_name;
9895 else
9896 fontp->full_name = fontp->name;
9898 fontp->size = font->max_bounds.width;
9899 fontp->height = FONT_HEIGHT (font);
9901 if (NILP (font_names))
9903 /* We come here because of a bug of XListFonts mentioned at
9904 the head of this block. Let's store this information in
9905 the cache for x_list_fonts. */
9906 Lisp_Object lispy_name = build_string (fontname);
9907 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9908 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
9909 Qnil);
9911 XSETCDR (dpyinfo->name_list_element,
9912 Fcons (Fcons (key,
9913 Fcons (Fcons (lispy_full_name,
9914 make_number (fontp->size)),
9915 Qnil)),
9916 XCDR (dpyinfo->name_list_element)));
9917 if (full_name)
9919 key = Fcons (Fcons (lispy_full_name, make_number (256)),
9920 Qnil);
9921 XSETCDR (dpyinfo->name_list_element,
9922 Fcons (Fcons (key,
9923 Fcons (Fcons (lispy_full_name,
9924 make_number (fontp->size)),
9925 Qnil)),
9926 XCDR (dpyinfo->name_list_element)));
9930 /* The slot `encoding' specifies how to map a character
9931 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
9932 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
9933 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
9934 2:0xA020..0xFF7F). For the moment, we don't know which charset
9935 uses this font. So, we set information in fontp->encoding[1]
9936 which is never used by any charset. If mapping can't be
9937 decided, set FONT_ENCODING_NOT_DECIDED. */
9938 fontp->encoding[1]
9939 = (font->max_byte1 == 0
9940 /* 1-byte font */
9941 ? (font->min_char_or_byte2 < 0x80
9942 ? (font->max_char_or_byte2 < 0x80
9943 ? 0 /* 0x20..0x7F */
9944 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
9945 : 1) /* 0xA0..0xFF */
9946 /* 2-byte font */
9947 : (font->min_byte1 < 0x80
9948 ? (font->max_byte1 < 0x80
9949 ? (font->min_char_or_byte2 < 0x80
9950 ? (font->max_char_or_byte2 < 0x80
9951 ? 0 /* 0x2020..0x7F7F */
9952 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
9953 : 3) /* 0x20A0..0x7FFF */
9954 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
9955 : (font->min_char_or_byte2 < 0x80
9956 ? (font->max_char_or_byte2 < 0x80
9957 ? 2 /* 0xA020..0xFF7F */
9958 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
9959 : 1))); /* 0xA0A0..0xFFFF */
9961 fontp->baseline_offset
9962 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
9963 ? (long) value : 0);
9964 fontp->relative_compose
9965 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
9966 ? (long) value : 0);
9967 fontp->default_ascent
9968 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
9969 ? (long) value : 0);
9971 /* Set global flag fonts_changed_p to non-zero if the font loaded
9972 has a character with a smaller width than any other character
9973 before, or if the font loaded has a smaller height than any
9974 other font loaded before. If this happens, it will make a
9975 glyph matrix reallocation necessary. */
9976 fonts_changed_p |= x_compute_min_glyph_bounds (f);
9977 UNBLOCK_INPUT;
9978 return fontp;
9983 /* Return a pointer to struct font_info of a font named FONTNAME for
9984 frame F. If no such font is loaded, return NULL. */
9986 struct font_info *
9987 x_query_font (f, fontname)
9988 struct frame *f;
9989 register char *fontname;
9991 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9992 int i;
9994 for (i = 0; i < dpyinfo->n_fonts; i++)
9995 if (dpyinfo->font_table[i].name
9996 && (!strcmp (dpyinfo->font_table[i].name, fontname)
9997 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
9998 return (dpyinfo->font_table + i);
9999 return NULL;
10003 /* Find a CCL program for a font specified by FONTP, and set the member
10004 `encoder' of the structure. */
10006 void
10007 x_find_ccl_program (fontp)
10008 struct font_info *fontp;
10010 Lisp_Object list, elt;
10012 elt = Qnil;
10013 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
10015 elt = XCAR (list);
10016 if (CONSP (elt)
10017 && STRINGP (XCAR (elt))
10018 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10019 >= 0)
10020 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10021 >= 0)))
10022 break;
10025 if (! NILP (list))
10027 struct ccl_program *ccl
10028 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
10030 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
10031 xfree (ccl);
10032 else
10033 fontp->font_encoder = ccl;
10039 /***********************************************************************
10040 Initialization
10041 ***********************************************************************/
10043 #ifdef USE_X_TOOLKIT
10044 static XrmOptionDescRec emacs_options[] = {
10045 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10046 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10048 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10049 XrmoptionSepArg, NULL},
10050 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10052 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10053 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10054 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10055 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10056 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10057 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10058 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10060 #endif /* USE_X_TOOLKIT */
10062 static int x_initialized;
10064 #ifdef MULTI_KBOARD
10065 /* Test whether two display-name strings agree up to the dot that separates
10066 the screen number from the server number. */
10067 static int
10068 same_x_server (name1, name2)
10069 const char *name1, *name2;
10071 int seen_colon = 0;
10072 const unsigned char *system_name = SDATA (Vsystem_name);
10073 int system_name_length = strlen (system_name);
10074 int length_until_period = 0;
10076 while (system_name[length_until_period] != 0
10077 && system_name[length_until_period] != '.')
10078 length_until_period++;
10080 /* Treat `unix' like an empty host name. */
10081 if (! strncmp (name1, "unix:", 5))
10082 name1 += 4;
10083 if (! strncmp (name2, "unix:", 5))
10084 name2 += 4;
10085 /* Treat this host's name like an empty host name. */
10086 if (! strncmp (name1, system_name, system_name_length)
10087 && name1[system_name_length] == ':')
10088 name1 += system_name_length;
10089 if (! strncmp (name2, system_name, system_name_length)
10090 && name2[system_name_length] == ':')
10091 name2 += system_name_length;
10092 /* Treat this host's domainless name like an empty host name. */
10093 if (! strncmp (name1, system_name, length_until_period)
10094 && name1[length_until_period] == ':')
10095 name1 += length_until_period;
10096 if (! strncmp (name2, system_name, length_until_period)
10097 && name2[length_until_period] == ':')
10098 name2 += length_until_period;
10100 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10102 if (*name1 == ':')
10103 seen_colon++;
10104 if (seen_colon && *name1 == '.')
10105 return 1;
10107 return (seen_colon
10108 && (*name1 == '.' || *name1 == '\0')
10109 && (*name2 == '.' || *name2 == '\0'));
10111 #endif
10113 struct x_display_info *
10114 x_term_init (display_name, xrm_option, resource_name)
10115 Lisp_Object display_name;
10116 char *xrm_option;
10117 char *resource_name;
10119 int connection;
10120 Display *dpy;
10121 struct x_display_info *dpyinfo;
10122 XrmDatabase xrdb;
10124 BLOCK_INPUT;
10126 if (!x_initialized)
10128 x_initialize ();
10129 x_initialized = 1;
10132 #ifdef USE_GTK
10134 #define NUM_ARGV 10
10135 int argc;
10136 char *argv[NUM_ARGV];
10137 char **argv2 = argv;
10138 GdkAtom atom;
10140 /* GTK 2.0 can only handle one display, GTK 2.2 can handle more
10141 than one, but this remains to be implemented. */
10142 if (x_initialized > 1)
10143 return 0;
10145 x_initialized++;
10147 for (argc = 0; argc < NUM_ARGV; ++argc)
10148 argv[argc] = 0;
10150 argc = 0;
10151 argv[argc++] = initial_argv[0];
10153 if (! NILP (display_name))
10155 argv[argc++] = "--display";
10156 argv[argc++] = SDATA (display_name);
10159 argv[argc++] = "--name";
10160 argv[argc++] = resource_name;
10162 #ifdef HAVE_X11R5
10163 XSetLocaleModifiers ("");
10164 #endif
10166 gtk_init (&argc, &argv2);
10168 /* gtk_init does set_locale. We must fix locale after calling it. */
10169 fixup_locale ();
10170 xg_initialize ();
10172 dpy = GDK_DISPLAY ();
10174 /* NULL window -> events for all windows go to our function */
10175 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10177 /* Load our own gtkrc if it exists. */
10179 struct gcpro gcpro1, gcpro2;
10180 char *file = "~/.emacs.d/gtkrc";
10181 Lisp_Object s, abs_file;
10183 GCPRO2 (s, abs_file);
10184 s = make_string (file, strlen (file));
10185 abs_file = Fexpand_file_name(s, Qnil);
10187 if (! NILP (abs_file) && Ffile_readable_p (abs_file))
10188 gtk_rc_parse (SDATA (abs_file));
10190 UNGCPRO;
10193 XSetErrorHandler (x_error_handler);
10194 XSetIOErrorHandler (x_io_error_quitter);
10196 #else /* not USE_GTK */
10197 #ifdef USE_X_TOOLKIT
10198 /* weiner@footloose.sps.mot.com reports that this causes
10199 errors with X11R5:
10200 X protocol error: BadAtom (invalid Atom parameter)
10201 on protocol request 18skiloaf.
10202 So let's not use it until R6. */
10203 #ifdef HAVE_X11XTR6
10204 XtSetLanguageProc (NULL, NULL, NULL);
10205 #endif
10208 int argc = 0;
10209 char *argv[3];
10211 argv[0] = "";
10212 argc = 1;
10213 if (xrm_option)
10215 argv[argc++] = "-xrm";
10216 argv[argc++] = xrm_option;
10218 turn_on_atimers (0);
10219 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10220 resource_name, EMACS_CLASS,
10221 emacs_options, XtNumber (emacs_options),
10222 &argc, argv);
10223 turn_on_atimers (1);
10225 #ifdef HAVE_X11XTR6
10226 /* I think this is to compensate for XtSetLanguageProc. */
10227 fixup_locale ();
10228 #endif
10231 #else /* not USE_X_TOOLKIT */
10232 #ifdef HAVE_X11R5
10233 XSetLocaleModifiers ("");
10234 #endif
10235 dpy = XOpenDisplay (SDATA (display_name));
10236 #endif /* not USE_X_TOOLKIT */
10237 #endif /* not USE_GTK*/
10239 /* Detect failure. */
10240 if (dpy == 0)
10242 UNBLOCK_INPUT;
10243 return 0;
10246 /* We have definitely succeeded. Record the new connection. */
10248 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10249 bzero (dpyinfo, sizeof *dpyinfo);
10251 #ifdef MULTI_KBOARD
10253 struct x_display_info *share;
10254 Lisp_Object tail;
10256 for (share = x_display_list, tail = x_display_name_list; share;
10257 share = share->next, tail = XCDR (tail))
10258 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10259 SDATA (display_name)))
10260 break;
10261 if (share)
10262 dpyinfo->kboard = share->kboard;
10263 else
10265 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10266 init_kboard (dpyinfo->kboard);
10267 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10269 char *vendor = ServerVendor (dpy);
10270 UNBLOCK_INPUT;
10271 dpyinfo->kboard->Vsystem_key_alist
10272 = call1 (Qvendor_specific_keysyms,
10273 build_string (vendor ? vendor : ""));
10274 BLOCK_INPUT;
10277 dpyinfo->kboard->next_kboard = all_kboards;
10278 all_kboards = dpyinfo->kboard;
10279 /* Don't let the initial kboard remain current longer than necessary.
10280 That would cause problems if a file loaded on startup tries to
10281 prompt in the mini-buffer. */
10282 if (current_kboard == initial_kboard)
10283 current_kboard = dpyinfo->kboard;
10285 dpyinfo->kboard->reference_count++;
10287 #endif
10289 /* Put this display on the chain. */
10290 dpyinfo->next = x_display_list;
10291 x_display_list = dpyinfo;
10293 /* Put it on x_display_name_list as well, to keep them parallel. */
10294 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10295 x_display_name_list);
10296 dpyinfo->name_list_element = XCAR (x_display_name_list);
10298 dpyinfo->display = dpy;
10300 #if 0
10301 XSetAfterFunction (x_current_display, x_trace_wire);
10302 #endif /* ! 0 */
10304 dpyinfo->x_id_name
10305 = (char *) xmalloc (SBYTES (Vinvocation_name)
10306 + SBYTES (Vsystem_name)
10307 + 2);
10308 sprintf (dpyinfo->x_id_name, "%s@%s",
10309 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10311 /* Figure out which modifier bits mean what. */
10312 x_find_modifier_meanings (dpyinfo);
10314 /* Get the scroll bar cursor. */
10315 dpyinfo->vertical_scroll_bar_cursor
10316 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10318 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10319 resource_name, EMACS_CLASS);
10320 #ifdef HAVE_XRMSETDATABASE
10321 XrmSetDatabase (dpyinfo->display, xrdb);
10322 #else
10323 dpyinfo->display->db = xrdb;
10324 #endif
10325 /* Put the rdb where we can find it in a way that works on
10326 all versions. */
10327 dpyinfo->xrdb = xrdb;
10329 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10330 DefaultScreen (dpyinfo->display));
10331 select_visual (dpyinfo);
10332 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10333 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
10334 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
10335 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10336 dpyinfo->grabbed = 0;
10337 dpyinfo->reference_count = 0;
10338 dpyinfo->icon_bitmap_id = -1;
10339 dpyinfo->font_table = NULL;
10340 dpyinfo->n_fonts = 0;
10341 dpyinfo->font_table_size = 0;
10342 dpyinfo->bitmaps = 0;
10343 dpyinfo->bitmaps_size = 0;
10344 dpyinfo->bitmaps_last = 0;
10345 dpyinfo->scratch_cursor_gc = 0;
10346 dpyinfo->mouse_face_mouse_frame = 0;
10347 dpyinfo->mouse_face_deferred_gc = 0;
10348 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10349 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10350 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10351 dpyinfo->mouse_face_window = Qnil;
10352 dpyinfo->mouse_face_overlay = Qnil;
10353 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10354 dpyinfo->mouse_face_defer = 0;
10355 dpyinfo->mouse_face_hidden = 0;
10356 dpyinfo->x_focus_frame = 0;
10357 dpyinfo->x_focus_event_frame = 0;
10358 dpyinfo->x_highlight_frame = 0;
10359 dpyinfo->image_cache = make_image_cache ();
10361 /* See if a private colormap is requested. */
10362 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10364 if (dpyinfo->visual->class == PseudoColor)
10366 Lisp_Object value;
10367 value = display_x_get_resource (dpyinfo,
10368 build_string ("privateColormap"),
10369 build_string ("PrivateColormap"),
10370 Qnil, Qnil);
10371 if (STRINGP (value)
10372 && (!strcmp (SDATA (value), "true")
10373 || !strcmp (SDATA (value), "on")))
10374 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10377 else
10378 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10379 dpyinfo->visual, AllocNone);
10382 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10383 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10384 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10385 dpyinfo->resy = pixels * 25.4 / mm;
10386 pixels = DisplayWidth (dpyinfo->display, screen_number);
10387 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10388 dpyinfo->resx = pixels * 25.4 / mm;
10391 dpyinfo->Xatom_wm_protocols
10392 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10393 dpyinfo->Xatom_wm_take_focus
10394 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10395 dpyinfo->Xatom_wm_save_yourself
10396 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10397 dpyinfo->Xatom_wm_delete_window
10398 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10399 dpyinfo->Xatom_wm_change_state
10400 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10401 dpyinfo->Xatom_wm_configure_denied
10402 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10403 dpyinfo->Xatom_wm_window_moved
10404 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10405 dpyinfo->Xatom_editres
10406 = XInternAtom (dpyinfo->display, "Editres", False);
10407 dpyinfo->Xatom_CLIPBOARD
10408 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10409 dpyinfo->Xatom_TIMESTAMP
10410 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10411 dpyinfo->Xatom_TEXT
10412 = XInternAtom (dpyinfo->display, "TEXT", False);
10413 dpyinfo->Xatom_COMPOUND_TEXT
10414 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10415 dpyinfo->Xatom_UTF8_STRING
10416 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10417 dpyinfo->Xatom_DELETE
10418 = XInternAtom (dpyinfo->display, "DELETE", False);
10419 dpyinfo->Xatom_MULTIPLE
10420 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10421 dpyinfo->Xatom_INCR
10422 = XInternAtom (dpyinfo->display, "INCR", False);
10423 dpyinfo->Xatom_EMACS_TMP
10424 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10425 dpyinfo->Xatom_TARGETS
10426 = XInternAtom (dpyinfo->display, "TARGETS", False);
10427 dpyinfo->Xatom_NULL
10428 = XInternAtom (dpyinfo->display, "NULL", False);
10429 dpyinfo->Xatom_ATOM_PAIR
10430 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10431 /* For properties of font. */
10432 dpyinfo->Xatom_PIXEL_SIZE
10433 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10434 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10435 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10436 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10437 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10438 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10439 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10441 /* Ghostscript support. */
10442 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10443 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10445 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10446 False);
10448 dpyinfo->cut_buffers_initialized = 0;
10450 connection = ConnectionNumber (dpyinfo->display);
10451 dpyinfo->connection = connection;
10454 char null_bits[1];
10456 null_bits[0] = 0x00;
10458 dpyinfo->null_pixel
10459 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10460 null_bits, 1, 1, (long) 0, (long) 0,
10465 extern int gray_bitmap_width, gray_bitmap_height;
10466 extern char *gray_bitmap_bits;
10467 dpyinfo->gray
10468 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10469 gray_bitmap_bits,
10470 gray_bitmap_width, gray_bitmap_height,
10471 (unsigned long) 1, (unsigned long) 0, 1);
10474 #ifdef HAVE_X_I18N
10475 xim_initialize (dpyinfo, resource_name);
10476 #endif
10478 #ifdef subprocesses
10479 /* This is only needed for distinguishing keyboard and process input. */
10480 if (connection != 0)
10481 add_keyboard_wait_descriptor (connection);
10482 #endif
10484 #ifndef F_SETOWN_BUG
10485 #ifdef F_SETOWN
10486 #ifdef F_SETOWN_SOCK_NEG
10487 /* stdin is a socket here */
10488 fcntl (connection, F_SETOWN, -getpid ());
10489 #else /* ! defined (F_SETOWN_SOCK_NEG) */
10490 fcntl (connection, F_SETOWN, getpid ());
10491 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
10492 #endif /* ! defined (F_SETOWN) */
10493 #endif /* F_SETOWN_BUG */
10495 #ifdef SIGIO
10496 if (interrupt_input)
10497 init_sigio (connection);
10498 #endif /* ! defined (SIGIO) */
10500 #ifdef USE_LUCID
10501 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
10502 /* Make sure that we have a valid font for dialog boxes
10503 so that Xt does not crash. */
10505 Display *dpy = dpyinfo->display;
10506 XrmValue d, fr, to;
10507 Font font;
10508 int count;
10510 d.addr = (XPointer)&dpy;
10511 d.size = sizeof (Display *);
10512 fr.addr = XtDefaultFont;
10513 fr.size = sizeof (XtDefaultFont);
10514 to.size = sizeof (Font *);
10515 to.addr = (XPointer)&font;
10516 count = x_catch_errors (dpy);
10517 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10518 abort ();
10519 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10520 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10521 x_uncatch_errors (dpy, count);
10523 #endif
10524 #endif
10526 /* See if we should run in synchronous mode. This is useful
10527 for debugging X code. */
10529 Lisp_Object value;
10530 value = display_x_get_resource (dpyinfo,
10531 build_string ("synchronous"),
10532 build_string ("Synchronous"),
10533 Qnil, Qnil);
10534 if (STRINGP (value)
10535 && (!strcmp (SDATA (value), "true")
10536 || !strcmp (SDATA (value), "on")))
10537 XSynchronize (dpyinfo->display, True);
10540 UNBLOCK_INPUT;
10542 return dpyinfo;
10545 /* Get rid of display DPYINFO, assuming all frames are already gone,
10546 and without sending any more commands to the X server. */
10548 void
10549 x_delete_display (dpyinfo)
10550 struct x_display_info *dpyinfo;
10552 delete_keyboard_wait_descriptor (dpyinfo->connection);
10554 /* Discard this display from x_display_name_list and x_display_list.
10555 We can't use Fdelq because that can quit. */
10556 if (! NILP (x_display_name_list)
10557 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10558 x_display_name_list = XCDR (x_display_name_list);
10559 else
10561 Lisp_Object tail;
10563 tail = x_display_name_list;
10564 while (CONSP (tail) && CONSP (XCDR (tail)))
10566 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10568 XSETCDR (tail, XCDR (XCDR (tail)));
10569 break;
10571 tail = XCDR (tail);
10575 if (next_noop_dpyinfo == dpyinfo)
10576 next_noop_dpyinfo = dpyinfo->next;
10578 if (x_display_list == dpyinfo)
10579 x_display_list = dpyinfo->next;
10580 else
10582 struct x_display_info *tail;
10584 for (tail = x_display_list; tail; tail = tail->next)
10585 if (tail->next == dpyinfo)
10586 tail->next = tail->next->next;
10589 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
10590 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
10591 XrmDestroyDatabase (dpyinfo->xrdb);
10592 #endif
10593 #endif
10594 #ifdef MULTI_KBOARD
10595 if (--dpyinfo->kboard->reference_count == 0)
10596 delete_kboard (dpyinfo->kboard);
10597 #endif
10598 #ifdef HAVE_X_I18N
10599 if (dpyinfo->xim)
10600 xim_close_dpy (dpyinfo);
10601 #endif
10603 xfree (dpyinfo->font_table);
10604 xfree (dpyinfo->x_id_name);
10605 xfree (dpyinfo->color_cells);
10606 xfree (dpyinfo);
10609 #ifdef USE_X_TOOLKIT
10611 /* Atimer callback function for TIMER. Called every 0.1s to process
10612 Xt timeouts, if needed. We must avoid calling XtAppPending as
10613 much as possible because that function does an implicit XFlush
10614 that slows us down. */
10616 static void
10617 x_process_timeouts (timer)
10618 struct atimer *timer;
10620 if (toolkit_scroll_bar_interaction || popup_activated ())
10622 BLOCK_INPUT;
10623 while (XtAppPending (Xt_app_con) & XtIMTimer)
10624 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10625 UNBLOCK_INPUT;
10629 #endif /* USE_X_TOOLKIT */
10632 /* Set up use of X before we make the first connection. */
10634 extern frame_parm_handler x_frame_parm_handlers[];
10636 static struct redisplay_interface x_redisplay_interface =
10638 x_frame_parm_handlers,
10639 x_produce_glyphs,
10640 x_write_glyphs,
10641 x_insert_glyphs,
10642 x_clear_end_of_line,
10643 x_scroll_run,
10644 x_after_update_window_line,
10645 x_update_window_begin,
10646 x_update_window_end,
10647 x_cursor_to,
10648 x_flush,
10649 #ifndef XFlush
10650 x_flush,
10651 #else
10652 0, /* flush_display_optional */
10653 #endif
10654 x_clear_window_mouse_face,
10655 x_get_glyph_overhangs,
10656 x_fix_overlapping_area,
10657 x_draw_fringe_bitmap,
10658 x_per_char_metric,
10659 x_encode_char,
10660 x_compute_glyph_string_overhangs,
10661 x_draw_glyph_string,
10662 x_define_frame_cursor,
10663 x_clear_frame_area,
10664 x_draw_window_cursor,
10665 x_draw_vertical_window_border,
10666 x_shift_glyphs_for_insert
10669 void
10670 x_initialize ()
10672 rif = &x_redisplay_interface;
10674 clear_frame_hook = x_clear_frame;
10675 ins_del_lines_hook = x_ins_del_lines;
10676 delete_glyphs_hook = x_delete_glyphs;
10677 ring_bell_hook = XTring_bell;
10678 reset_terminal_modes_hook = XTreset_terminal_modes;
10679 set_terminal_modes_hook = XTset_terminal_modes;
10680 update_begin_hook = x_update_begin;
10681 update_end_hook = x_update_end;
10682 set_terminal_window_hook = XTset_terminal_window;
10683 read_socket_hook = XTread_socket;
10684 frame_up_to_date_hook = XTframe_up_to_date;
10685 mouse_position_hook = XTmouse_position;
10686 frame_rehighlight_hook = XTframe_rehighlight;
10687 frame_raise_lower_hook = XTframe_raise_lower;
10688 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10689 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10690 redeem_scroll_bar_hook = XTredeem_scroll_bar;
10691 judge_scroll_bars_hook = XTjudge_scroll_bars;
10693 scroll_region_ok = 1; /* we'll scroll partial frames */
10694 char_ins_del_ok = 1;
10695 line_ins_del_ok = 1; /* we'll just blt 'em */
10696 fast_clear_end_of_line = 1; /* X does this well */
10697 memory_below_frame = 0; /* we don't remember what scrolls
10698 off the bottom */
10699 baud_rate = 19200;
10701 x_noop_count = 0;
10702 last_tool_bar_item = -1;
10703 any_help_event_p = 0;
10705 /* Try to use interrupt input; if we can't, then start polling. */
10706 Fset_input_mode (Qt, Qnil, Qt, Qnil);
10708 #ifdef USE_X_TOOLKIT
10709 XtToolkitInitialize ();
10711 Xt_app_con = XtCreateApplicationContext ();
10713 /* Register a converter from strings to pixels, which uses
10714 Emacs' color allocation infrastructure. */
10715 XtAppSetTypeConverter (Xt_app_con,
10716 XtRString, XtRPixel, cvt_string_to_pixel,
10717 cvt_string_to_pixel_args,
10718 XtNumber (cvt_string_to_pixel_args),
10719 XtCacheByDisplay, cvt_pixel_dtor);
10721 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10723 /* Install an asynchronous timer that processes Xt timeout events
10724 every 0.1s. This is necessary because some widget sets use
10725 timeouts internally, for example the LessTif menu bar, or the
10726 Xaw3d scroll bar. When Xt timouts aren't processed, these
10727 widgets don't behave normally. */
10729 EMACS_TIME interval;
10730 EMACS_SET_SECS_USECS (interval, 0, 100000);
10731 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
10733 #endif
10735 #ifdef USE_TOOLKIT_SCROLL_BARS
10736 #ifndef USE_GTK
10737 xaw3d_arrow_scroll = False;
10738 xaw3d_pick_top = True;
10739 #endif
10740 #endif
10742 /* Note that there is no real way portable across R3/R4 to get the
10743 original error handler. */
10744 XSetErrorHandler (x_error_handler);
10745 XSetIOErrorHandler (x_io_error_quitter);
10747 /* Disable Window Change signals; they are handled by X events. */
10748 #ifdef SIGWINCH
10749 signal (SIGWINCH, SIG_DFL);
10750 #endif /* SIGWINCH */
10752 signal (SIGPIPE, x_connection_signal);
10754 #ifdef HAVE_X_SM
10755 x_session_initialize ();
10756 #endif
10760 void
10761 syms_of_xterm ()
10763 staticpro (&x_error_message_string);
10764 x_error_message_string = Qnil;
10766 staticpro (&x_display_name_list);
10767 x_display_name_list = Qnil;
10769 staticpro (&last_mouse_scroll_bar);
10770 last_mouse_scroll_bar = Qnil;
10772 staticpro (&Qvendor_specific_keysyms);
10773 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10775 staticpro (&Qutf_8);
10776 Qutf_8 = intern ("utf-8");
10777 staticpro (&Qlatin_1);
10778 Qlatin_1 = intern ("latin-1");
10780 staticpro (&last_mouse_press_frame);
10781 last_mouse_press_frame = Qnil;
10783 DEFVAR_BOOL ("x-use-underline-position-properties",
10784 &x_use_underline_position_properties,
10785 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10786 nil means ignore them. If you encounter fonts with bogus
10787 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10788 to 4.1, set this to nil. */);
10789 x_use_underline_position_properties = 1;
10791 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10792 doc: /* What X toolkit scroll bars Emacs uses.
10793 A value of nil means Emacs doesn't use X toolkit scroll bars.
10794 Otherwise, value is a symbol describing the X toolkit. */);
10795 #ifdef USE_TOOLKIT_SCROLL_BARS
10796 #ifdef USE_MOTIF
10797 Vx_toolkit_scroll_bars = intern ("motif");
10798 #elif defined HAVE_XAW3D
10799 Vx_toolkit_scroll_bars = intern ("xaw3d");
10800 #elif USE_GTK
10801 Vx_toolkit_scroll_bars = intern ("gtk");
10802 #else
10803 Vx_toolkit_scroll_bars = intern ("xaw");
10804 #endif
10805 #else
10806 Vx_toolkit_scroll_bars = Qnil;
10807 #endif
10809 staticpro (&last_mouse_motion_frame);
10810 last_mouse_motion_frame = Qnil;
10812 Qmodifier_value = intern ("modifier-value");
10813 Qalt = intern ("alt");
10814 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10815 Qhyper = intern ("hyper");
10816 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10817 Qmeta = intern ("meta");
10818 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10819 Qsuper = intern ("super");
10820 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10822 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10823 doc: /* Which keys Emacs uses for the alt modifier.
10824 This should be one of the symbols `alt', `hyper', `meta', `super'.
10825 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10826 is nil, which is the same as `alt'. */);
10827 Vx_alt_keysym = Qnil;
10829 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
10830 doc: /* Which keys Emacs uses for the hyper modifier.
10831 This should be one of the symbols `alt', `hyper', `meta', `super'.
10832 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10833 default is nil, which is the same as `hyper'. */);
10834 Vx_hyper_keysym = Qnil;
10836 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
10837 doc: /* Which keys Emacs uses for the meta modifier.
10838 This should be one of the symbols `alt', `hyper', `meta', `super'.
10839 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10840 default is nil, which is the same as `meta'. */);
10841 Vx_meta_keysym = Qnil;
10843 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
10844 doc: /* Which keys Emacs uses for the super modifier.
10845 This should be one of the symbols `alt', `hyper', `meta', `super'.
10846 For example, `super' means use the Super_L and Super_R keysyms. The
10847 default is nil, which is the same as `super'. */);
10848 Vx_super_keysym = Qnil;
10850 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
10851 doc: /* Hash table of character codes indexed by X keysym codes. */);
10852 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
10853 make_float (DEFAULT_REHASH_SIZE),
10854 make_float (DEFAULT_REHASH_THRESHOLD),
10855 Qnil, Qnil, Qnil);
10858 #endif /* HAVE_X_WINDOWS */