Fixed X support, preliminary support for X-tty combo sessions.
[emacs.git] / src / xterm.c
blob01b500f19158cfd371347bfae4dbd68d94750538
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 97, 98, 1999, 2000, 01, 02, 2003
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 #define XAW_ARROW_SCROLLBARS
135 #include <X11/Xaw3d/ScrollbarP.h>
136 #else /* !HAVE_XAW3D */
137 #include <X11/Xaw/Simple.h>
138 #include <X11/Xaw/Scrollbar.h>
139 #endif /* !HAVE_XAW3D */
140 #ifndef XtNpickTop
141 #define XtNpickTop "pickTop"
142 #endif /* !XtNpickTop */
143 #endif /* !USE_MOTIF */
144 #endif /* USE_TOOLKIT_SCROLL_BARS */
146 #endif /* USE_X_TOOLKIT */
148 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
149 #define x_any_window_to_frame x_window_to_frame
150 #define x_top_window_to_frame x_window_to_frame
151 #endif
153 #ifdef USE_X_TOOLKIT
154 #include "widget.h"
155 #ifndef XtNinitialState
156 #define XtNinitialState "initialState"
157 #endif
158 #endif
160 #define abs(x) ((x) < 0 ? -(x) : (x))
162 /* Default to using XIM if available. */
163 #ifdef USE_XIM
164 int use_xim = 1;
165 #else
166 int use_xim = 0; /* configure --without-xim */
167 #endif
171 /* Non-nil means Emacs uses toolkit scroll bars. */
173 Lisp_Object Vx_toolkit_scroll_bars;
175 /* Non-zero means that a HELP_EVENT has been generated since Emacs
176 start. */
178 static int any_help_event_p;
180 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
181 static Lisp_Object last_window;
183 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
185 int x_use_underline_position_properties;
187 /* Generic display parameters for X displays. */
189 struct display_method x_display_method;
191 /* This is a chain of structures for all the X displays currently in
192 use. */
194 struct x_display_info *x_display_list;
196 /* This is a list of cons cells, each of the form (NAME
197 . FONT-LIST-CACHE), one for each element of x_display_list and in
198 the same order. NAME is the name of the frame. FONT-LIST-CACHE
199 records previous values returned by x-list-fonts. */
201 Lisp_Object x_display_name_list;
203 /* Frame being updated by update_frame. This is declared in term.c.
204 This is set by update_begin and looked at by all the XT functions.
205 It is zero while not inside an update. In that case, the XT
206 functions assume that `selected_frame' is the frame to apply to. */
208 extern struct frame *updating_frame;
210 /* This is a frame waiting to be auto-raised, within XTread_socket. */
212 struct frame *pending_autoraise_frame;
214 #ifdef USE_X_TOOLKIT
215 /* The application context for Xt use. */
216 XtAppContext Xt_app_con;
217 static String Xt_default_resources[] = {0};
218 #endif /* USE_X_TOOLKIT */
220 /* Non-zero means user is interacting with a toolkit scroll bar. */
222 static int toolkit_scroll_bar_interaction;
224 /* Mouse movement.
226 Formerly, we used PointerMotionHintMask (in standard_event_mask)
227 so that we would have to call XQueryPointer after each MotionNotify
228 event to ask for another such event. However, this made mouse tracking
229 slow, and there was a bug that made it eventually stop.
231 Simply asking for MotionNotify all the time seems to work better.
233 In order to avoid asking for motion events and then throwing most
234 of them away or busy-polling the server for mouse positions, we ask
235 the server for pointer motion hints. This means that we get only
236 one event per group of mouse movements. "Groups" are delimited by
237 other kinds of events (focus changes and button clicks, for
238 example), or by XQueryPointer calls; when one of these happens, we
239 get another MotionNotify event the next time the mouse moves. This
240 is at least as efficient as getting motion events when mouse
241 tracking is on, and I suspect only negligibly worse when tracking
242 is off. */
244 /* Where the mouse was last time we reported a mouse event. */
246 static XRectangle last_mouse_glyph;
247 static Lisp_Object last_mouse_press_frame;
249 /* The scroll bar in which the last X motion event occurred.
251 If the last X motion event occurred in a scroll bar, we set this so
252 XTmouse_position can know whether to report a scroll bar motion or
253 an ordinary motion.
255 If the last X motion event didn't occur in a scroll bar, we set
256 this to Qnil, to tell XTmouse_position to return an ordinary motion
257 event. */
259 static Lisp_Object last_mouse_scroll_bar;
261 /* This is a hack. We would really prefer that XTmouse_position would
262 return the time associated with the position it returns, but there
263 doesn't seem to be any way to wrest the time-stamp from the server
264 along with the position query. So, we just keep track of the time
265 of the last movement we received, and return that in hopes that
266 it's somewhat accurate. */
268 static Time last_mouse_movement_time;
270 /* Incremented by XTread_socket whenever it really tries to read
271 events. */
273 #ifdef __STDC__
274 static int volatile input_signal_count;
275 #else
276 static int input_signal_count;
277 #endif
279 /* Used locally within XTread_socket. */
281 static int x_noop_count;
283 /* Initial values of argv and argc. */
285 extern char **initial_argv;
286 extern int initial_argc;
288 extern Lisp_Object Vcommand_line_args, Vsystem_name;
290 /* Tells if a window manager is present or not. */
292 extern Lisp_Object Vx_no_window_manager;
294 extern Lisp_Object Qeql;
296 extern int errno;
298 /* A mask of extra modifier bits to put into every keyboard char. */
300 extern EMACS_INT extra_keyboard_modifiers;
302 /* The keysyms to use for the various modifiers. */
304 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
305 Lisp_Object Vx_keysym_table;
306 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
308 static Lisp_Object Qvendor_specific_keysyms;
309 static Lisp_Object Qlatin_1;
311 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
312 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
314 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
315 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
316 static const XColor *x_color_cells P_ ((Display *, int *));
317 static void x_update_window_end P_ ((struct window *, int, int));
318 void x_delete_display P_ ((struct x_display_info *));
319 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
320 unsigned));
321 static int x_io_error_quitter P_ ((Display *));
322 int x_catch_errors P_ ((Display *));
323 void x_uncatch_errors P_ ((Display *, int));
324 void x_lower_frame P_ ((struct frame *));
325 void x_scroll_bar_clear P_ ((struct frame *));
326 int x_had_errors_p P_ ((Display *));
327 void x_wm_set_size_hint P_ ((struct frame *, long, int));
328 void x_raise_frame P_ ((struct frame *));
329 void x_set_window_size P_ ((struct frame *, int, int, int));
330 void x_wm_set_window_state P_ ((struct frame *, int));
331 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
332 void x_initialize P_ ((void));
333 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
334 static int x_compute_min_glyph_bounds P_ ((struct frame *));
335 static void x_update_end P_ ((struct frame *));
336 static void XTframe_up_to_date P_ ((struct frame *));
337 static void XTset_terminal_modes P_ ((void));
338 static void XTreset_terminal_modes P_ ((void));
339 static void x_clear_frame P_ ((void));
340 static void frame_highlight P_ ((struct frame *));
341 static void frame_unhighlight P_ ((struct frame *));
342 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
343 static int x_focus_changed P_ ((int,
344 int,
345 struct x_display_info *,
346 struct frame *,
347 struct input_event *,
348 int));
349 static int x_detect_focus_change P_ ((struct x_display_info *,
350 XEvent *,
351 struct input_event *,
352 int));
353 static void XTframe_rehighlight P_ ((struct frame *));
354 static void x_frame_rehighlight P_ ((struct x_display_info *));
355 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
356 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
357 enum text_cursor_kinds));
359 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, GC));
360 static void x_flush P_ ((struct frame *f));
361 static void x_update_begin P_ ((struct frame *));
362 static void x_update_window_begin P_ ((struct window *));
363 static void x_after_update_window_line P_ ((struct glyph_row *));
364 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
365 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
366 enum scroll_bar_part *,
367 Lisp_Object *, Lisp_Object *,
368 unsigned long *));
369 static void x_check_fullscreen P_ ((struct frame *));
370 static void x_check_expected_move P_ ((struct frame *));
371 static int handle_one_xevent P_ ((struct x_display_info *,
372 XEvent *,
373 struct input_event **,
374 int *,
375 int *));
378 /* Flush display of frame F, or of all frames if F is null. */
380 static void
381 x_flush (f)
382 struct frame *f;
384 BLOCK_INPUT;
385 if (f == NULL)
387 Lisp_Object rest, frame;
388 FOR_EACH_FRAME (rest, frame)
389 if (FRAME_X_P (XFRAME (frame)))
390 x_flush (XFRAME (frame));
392 else if (FRAME_X_P (f))
393 XFlush (FRAME_X_DISPLAY (f));
394 UNBLOCK_INPUT;
398 /* Remove calls to XFlush by defining XFlush to an empty replacement.
399 Calls to XFlush should be unnecessary because the X output buffer
400 is flushed automatically as needed by calls to XPending,
401 XNextEvent, or XWindowEvent according to the XFlush man page.
402 XTread_socket calls XPending. Removing XFlush improves
403 performance. */
405 #define XFlush(DISPLAY) (void) 0
408 /***********************************************************************
409 Debugging
410 ***********************************************************************/
412 #if 0
414 /* This is a function useful for recording debugging information about
415 the sequence of occurrences in this file. */
417 struct record
419 char *locus;
420 int type;
423 struct record event_record[100];
425 int event_record_index;
427 record_event (locus, type)
428 char *locus;
429 int type;
431 if (event_record_index == sizeof (event_record) / sizeof (struct record))
432 event_record_index = 0;
434 event_record[event_record_index].locus = locus;
435 event_record[event_record_index].type = type;
436 event_record_index++;
439 #endif /* 0 */
443 /* Return the struct x_display_info corresponding to DPY. */
445 struct x_display_info *
446 x_display_info_for_display (dpy)
447 Display *dpy;
449 struct x_display_info *dpyinfo;
451 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
452 if (dpyinfo->display == dpy)
453 return dpyinfo;
455 return 0;
460 /***********************************************************************
461 Starting and ending an update
462 ***********************************************************************/
464 /* Start an update of frame F. This function is installed as a hook
465 for update_begin, i.e. it is called when update_begin is called.
466 This function is called prior to calls to x_update_window_begin for
467 each window being updated. Currently, there is nothing to do here
468 because all interesting stuff is done on a window basis. */
470 static void
471 x_update_begin (f)
472 struct frame *f;
474 /* Nothing to do. */
477 /* Start update of window W. Set the global variable updated_window
478 to the window being updated and set output_cursor to the cursor
479 position of W. */
481 static void
482 x_update_window_begin (w)
483 struct window *w;
485 struct frame *f = XFRAME (WINDOW_FRAME (w));
486 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
488 updated_window = w;
489 set_output_cursor (&w->cursor);
491 BLOCK_INPUT;
493 if (f == display_info->mouse_face_mouse_frame)
495 /* Don't do highlighting for mouse motion during the update. */
496 display_info->mouse_face_defer = 1;
498 /* If F needs to be redrawn, simply forget about any prior mouse
499 highlighting. */
500 if (FRAME_GARBAGED_P (f))
501 display_info->mouse_face_window = Qnil;
503 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
504 their mouse_face_p flag set, which means that they are always
505 unequal to rows in a desired matrix which never have that
506 flag set. So, rows containing mouse-face glyphs are never
507 scrolled, and we don't have to switch the mouse highlight off
508 here to prevent it from being scrolled. */
510 /* Can we tell that this update does not affect the window
511 where the mouse highlight is? If so, no need to turn off.
512 Likewise, don't do anything if the frame is garbaged;
513 in that case, the frame's current matrix that we would use
514 is all wrong, and we will redisplay that line anyway. */
515 if (!NILP (display_info->mouse_face_window)
516 && w == XWINDOW (display_info->mouse_face_window))
518 int i;
520 for (i = 0; i < w->desired_matrix->nrows; ++i)
521 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
522 break;
524 if (i < w->desired_matrix->nrows)
525 clear_mouse_face (display_info);
527 #endif /* 0 */
530 UNBLOCK_INPUT;
534 /* Draw a vertical window border from (x,y0) to (x,y1) */
536 static void
537 x_draw_vertical_window_border (w, x, y0, y1)
538 struct window *w;
539 int x, y0, y1;
541 struct frame *f = XFRAME (WINDOW_FRAME (w));
543 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
544 f->output_data.x->normal_gc, x, y0, x, y1);
547 /* End update of window W (which is equal to updated_window).
549 Draw vertical borders between horizontally adjacent windows, and
550 display W's cursor if CURSOR_ON_P is non-zero.
552 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
553 glyphs in mouse-face were overwritten. In that case we have to
554 make sure that the mouse-highlight is properly redrawn.
556 W may be a menu bar pseudo-window in case we don't have X toolkit
557 support. Such windows don't have a cursor, so don't display it
558 here. */
560 static void
561 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
562 struct window *w;
563 int cursor_on_p, mouse_face_overwritten_p;
565 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
567 if (!w->pseudo_window_p)
569 BLOCK_INPUT;
571 if (cursor_on_p)
572 display_and_set_cursor (w, 1, output_cursor.hpos,
573 output_cursor.vpos,
574 output_cursor.x, output_cursor.y);
576 x_draw_vertical_border (w);
577 UNBLOCK_INPUT;
580 /* If a row with mouse-face was overwritten, arrange for
581 XTframe_up_to_date to redisplay the mouse highlight. */
582 if (mouse_face_overwritten_p)
584 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
585 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
586 dpyinfo->mouse_face_window = Qnil;
589 updated_window = NULL;
593 /* End update of frame F. This function is installed as a hook in
594 update_end. */
596 static void
597 x_update_end (f)
598 struct frame *f;
600 /* Mouse highlight may be displayed again. */
601 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
603 #ifndef XFlush
604 BLOCK_INPUT;
605 XFlush (FRAME_X_DISPLAY (f));
606 UNBLOCK_INPUT;
607 #endif
611 /* This function is called from various places in xdisp.c whenever a
612 complete update has been performed. The global variable
613 updated_window is not available here. */
615 static void
616 XTframe_up_to_date (f)
617 struct frame *f;
619 if (FRAME_X_P (f))
621 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
623 if (dpyinfo->mouse_face_deferred_gc
624 || f == dpyinfo->mouse_face_mouse_frame)
626 BLOCK_INPUT;
627 if (dpyinfo->mouse_face_mouse_frame)
628 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
629 dpyinfo->mouse_face_mouse_x,
630 dpyinfo->mouse_face_mouse_y);
631 dpyinfo->mouse_face_deferred_gc = 0;
632 UNBLOCK_INPUT;
638 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
639 arrow bitmaps, or clear the fringes if no bitmaps are required
640 before DESIRED_ROW is made current. The window being updated is
641 found in updated_window. This function It is called from
642 update_window_line only if it is known that there are differences
643 between bitmaps to be drawn between current row and DESIRED_ROW. */
645 static void
646 x_after_update_window_line (desired_row)
647 struct glyph_row *desired_row;
649 struct window *w = updated_window;
650 struct frame *f;
651 int width, height;
653 xassert (w);
655 if (!desired_row->mode_line_p && !w->pseudo_window_p)
657 BLOCK_INPUT;
658 draw_row_fringe_bitmaps (w, desired_row);
659 UNBLOCK_INPUT;
662 /* When a window has disappeared, make sure that no rest of
663 full-width rows stays visible in the internal border. Could
664 check here if updated_window is the leftmost/rightmost window,
665 but I guess it's not worth doing since vertically split windows
666 are almost never used, internal border is rarely set, and the
667 overhead is very small. */
668 if (windows_or_buffers_changed
669 && desired_row->full_width_p
670 && (f = XFRAME (w->frame),
671 width = FRAME_INTERNAL_BORDER_WIDTH (f),
672 width != 0)
673 && (height = desired_row->visible_height,
674 height > 0))
676 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
678 /* Internal border is drawn below the tool bar. */
679 if (WINDOWP (f->tool_bar_window)
680 && w == XWINDOW (f->tool_bar_window))
681 y -= width;
683 BLOCK_INPUT;
684 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
685 0, y, width, height, False);
686 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
687 FRAME_PIXEL_WIDTH (f) - width,
688 y, width, height, False);
689 UNBLOCK_INPUT;
693 static void
694 x_draw_fringe_bitmap (w, row, p)
695 struct window *w;
696 struct glyph_row *row;
697 struct draw_fringe_bitmap_params *p;
699 struct frame *f = XFRAME (WINDOW_FRAME (w));
700 Display *display = FRAME_X_DISPLAY (f);
701 Window window = FRAME_X_WINDOW (f);
702 GC gc = f->output_data.x->normal_gc;
703 struct face *face = p->face;
705 /* Must clip because of partially visible lines. */
706 x_clip_to_row (w, row, gc);
708 if (p->bx >= 0)
710 /* In case the same realized face is used for fringes and
711 for something displayed in the text (e.g. face `region' on
712 mono-displays, the fill style may have been changed to
713 FillSolid in x_draw_glyph_string_background. */
714 if (face->stipple)
715 XSetFillStyle (display, face->gc, FillOpaqueStippled);
716 else
717 XSetForeground (display, face->gc, face->background);
719 XFillRectangle (display, window, face->gc,
720 p->bx, p->by, p->nx, p->ny);
722 if (!face->stipple)
723 XSetForeground (display, face->gc, face->foreground);
726 if (p->which != NO_FRINGE_BITMAP)
728 unsigned char *bits = fringe_bitmaps[p->which].bits + p->dh;
729 Pixmap pixmap;
730 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
732 /* Draw the bitmap. I believe these small pixmaps can be cached
733 by the server. */
734 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
735 face->foreground,
736 face->background, depth);
737 XCopyArea (display, pixmap, window, gc, 0, 0,
738 p->wd, p->h, p->x, p->y);
739 XFreePixmap (display, pixmap);
742 XSetClipMask (display, gc, None);
747 /* This is called when starting Emacs and when restarting after
748 suspend. When starting Emacs, no X window is mapped. And nothing
749 must be done to Emacs's own window if it is suspended (though that
750 rarely happens). */
752 static void
753 XTset_terminal_modes ()
757 /* This is called when exiting or suspending Emacs. Exiting will make
758 the X-windows go away, and suspending requires no action. */
760 static void
761 XTreset_terminal_modes ()
767 /***********************************************************************
768 Display Iterator
769 ***********************************************************************/
771 /* Function prototypes of this page. */
773 static int x_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
776 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
777 is not contained in the font. */
779 static XCharStruct *
780 x_per_char_metric (font, char2b, font_type)
781 XFontStruct *font;
782 XChar2b *char2b;
783 int font_type; /* unused on X */
785 /* The result metric information. */
786 XCharStruct *pcm = NULL;
788 xassert (font && char2b);
790 if (font->per_char != NULL)
792 if (font->min_byte1 == 0 && font->max_byte1 == 0)
794 /* min_char_or_byte2 specifies the linear character index
795 corresponding to the first element of the per_char array,
796 max_char_or_byte2 is the index of the last character. A
797 character with non-zero CHAR2B->byte1 is not in the font.
798 A character with byte2 less than min_char_or_byte2 or
799 greater max_char_or_byte2 is not in the font. */
800 if (char2b->byte1 == 0
801 && char2b->byte2 >= font->min_char_or_byte2
802 && char2b->byte2 <= font->max_char_or_byte2)
803 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
805 else
807 /* If either min_byte1 or max_byte1 are nonzero, both
808 min_char_or_byte2 and max_char_or_byte2 are less than
809 256, and the 2-byte character index values corresponding
810 to the per_char array element N (counting from 0) are:
812 byte1 = N/D + min_byte1
813 byte2 = N\D + min_char_or_byte2
815 where:
817 D = max_char_or_byte2 - min_char_or_byte2 + 1
818 / = integer division
819 \ = integer modulus */
820 if (char2b->byte1 >= font->min_byte1
821 && char2b->byte1 <= font->max_byte1
822 && char2b->byte2 >= font->min_char_or_byte2
823 && char2b->byte2 <= font->max_char_or_byte2)
825 pcm = (font->per_char
826 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
827 * (char2b->byte1 - font->min_byte1))
828 + (char2b->byte2 - font->min_char_or_byte2));
832 else
834 /* If the per_char pointer is null, all glyphs between the first
835 and last character indexes inclusive have the same
836 information, as given by both min_bounds and max_bounds. */
837 if (char2b->byte2 >= font->min_char_or_byte2
838 && char2b->byte2 <= font->max_char_or_byte2)
839 pcm = &font->max_bounds;
842 return ((pcm == NULL
843 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
844 ? NULL : pcm);
848 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
849 the two-byte form of C. Encoding is returned in *CHAR2B. */
851 static int
852 x_encode_char (c, char2b, font_info, two_byte_p)
853 int c;
854 XChar2b *char2b;
855 struct font_info *font_info;
856 int *two_byte_p;
858 int charset = CHAR_CHARSET (c);
859 XFontStruct *font = font_info->font;
861 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
862 This may be either a program in a special encoder language or a
863 fixed encoding. */
864 if (font_info->font_encoder)
866 /* It's a program. */
867 struct ccl_program *ccl = font_info->font_encoder;
869 if (CHARSET_DIMENSION (charset) == 1)
871 ccl->reg[0] = charset;
872 ccl->reg[1] = char2b->byte2;
873 ccl->reg[2] = -1;
875 else
877 ccl->reg[0] = charset;
878 ccl->reg[1] = char2b->byte1;
879 ccl->reg[2] = char2b->byte2;
882 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
884 /* We assume that MSBs are appropriately set/reset by CCL
885 program. */
886 if (font->max_byte1 == 0) /* 1-byte font */
887 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
888 else
889 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
891 else if (font_info->encoding[charset])
893 /* Fixed encoding scheme. See fontset.h for the meaning of the
894 encoding numbers. */
895 int enc = font_info->encoding[charset];
897 if ((enc == 1 || enc == 2)
898 && CHARSET_DIMENSION (charset) == 2)
899 char2b->byte1 |= 0x80;
901 if (enc == 1 || enc == 3)
902 char2b->byte2 |= 0x80;
905 if (two_byte_p)
906 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
908 return FONT_TYPE_UNKNOWN;
913 /***********************************************************************
914 Glyph display
915 ***********************************************************************/
919 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
920 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
921 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
922 int));
923 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
924 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
925 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
926 static void x_draw_glyph_string P_ ((struct glyph_string *));
927 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
928 static void x_set_cursor_gc P_ ((struct glyph_string *));
929 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
930 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
931 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
932 unsigned long *, double, int));
933 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
934 double, int, unsigned long));
935 static void x_setup_relief_colors P_ ((struct glyph_string *));
936 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
937 static void x_draw_image_relief P_ ((struct glyph_string *));
938 static void x_draw_image_foreground P_ ((struct glyph_string *));
939 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
940 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
941 int, int, int));
942 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
943 int, int, int, int, XRectangle *));
944 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
945 int, int, int, XRectangle *));
947 #if GLYPH_DEBUG
948 static void x_check_font P_ ((struct frame *, XFontStruct *));
949 #endif
952 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
953 face. */
955 static void
956 x_set_cursor_gc (s)
957 struct glyph_string *s;
959 if (s->font == FRAME_FONT (s->f)
960 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
961 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
962 && !s->cmp)
963 s->gc = s->f->output_data.x->cursor_gc;
964 else
966 /* Cursor on non-default face: must merge. */
967 XGCValues xgcv;
968 unsigned long mask;
970 xgcv.background = s->f->output_data.x->cursor_pixel;
971 xgcv.foreground = s->face->background;
973 /* If the glyph would be invisible, try a different foreground. */
974 if (xgcv.foreground == xgcv.background)
975 xgcv.foreground = s->face->foreground;
976 if (xgcv.foreground == xgcv.background)
977 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
978 if (xgcv.foreground == xgcv.background)
979 xgcv.foreground = s->face->foreground;
981 /* Make sure the cursor is distinct from text in this face. */
982 if (xgcv.background == s->face->background
983 && xgcv.foreground == s->face->foreground)
985 xgcv.background = s->face->foreground;
986 xgcv.foreground = s->face->background;
989 IF_DEBUG (x_check_font (s->f, s->font));
990 xgcv.font = s->font->fid;
991 xgcv.graphics_exposures = False;
992 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
994 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
995 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
996 mask, &xgcv);
997 else
998 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
999 = XCreateGC (s->display, s->window, mask, &xgcv);
1001 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1006 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1008 static void
1009 x_set_mouse_face_gc (s)
1010 struct glyph_string *s;
1012 int face_id;
1013 struct face *face;
1015 /* What face has to be used last for the mouse face? */
1016 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1017 face = FACE_FROM_ID (s->f, face_id);
1018 if (face == NULL)
1019 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1021 if (s->first_glyph->type == CHAR_GLYPH)
1022 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
1023 else
1024 face_id = FACE_FOR_CHAR (s->f, face, 0);
1025 s->face = FACE_FROM_ID (s->f, face_id);
1026 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1028 /* If font in this face is same as S->font, use it. */
1029 if (s->font == s->face->font)
1030 s->gc = s->face->gc;
1031 else
1033 /* Otherwise construct scratch_cursor_gc with values from FACE
1034 but font FONT. */
1035 XGCValues xgcv;
1036 unsigned long mask;
1038 xgcv.background = s->face->background;
1039 xgcv.foreground = s->face->foreground;
1040 IF_DEBUG (x_check_font (s->f, s->font));
1041 xgcv.font = s->font->fid;
1042 xgcv.graphics_exposures = False;
1043 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1045 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1046 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1047 mask, &xgcv);
1048 else
1049 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1050 = XCreateGC (s->display, s->window, mask, &xgcv);
1052 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1055 xassert (s->gc != 0);
1059 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1060 Faces to use in the mode line have already been computed when the
1061 matrix was built, so there isn't much to do, here. */
1063 static INLINE void
1064 x_set_mode_line_face_gc (s)
1065 struct glyph_string *s;
1067 s->gc = s->face->gc;
1071 /* Set S->gc of glyph string S for drawing that glyph string. Set
1072 S->stippled_p to a non-zero value if the face of S has a stipple
1073 pattern. */
1075 static INLINE void
1076 x_set_glyph_string_gc (s)
1077 struct glyph_string *s;
1079 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1081 if (s->hl == DRAW_NORMAL_TEXT)
1083 s->gc = s->face->gc;
1084 s->stippled_p = s->face->stipple != 0;
1086 else if (s->hl == DRAW_INVERSE_VIDEO)
1088 x_set_mode_line_face_gc (s);
1089 s->stippled_p = s->face->stipple != 0;
1091 else if (s->hl == DRAW_CURSOR)
1093 x_set_cursor_gc (s);
1094 s->stippled_p = 0;
1096 else if (s->hl == DRAW_MOUSE_FACE)
1098 x_set_mouse_face_gc (s);
1099 s->stippled_p = s->face->stipple != 0;
1101 else if (s->hl == DRAW_IMAGE_RAISED
1102 || s->hl == DRAW_IMAGE_SUNKEN)
1104 s->gc = s->face->gc;
1105 s->stippled_p = s->face->stipple != 0;
1107 else
1109 s->gc = s->face->gc;
1110 s->stippled_p = s->face->stipple != 0;
1113 /* GC must have been set. */
1114 xassert (s->gc != 0);
1118 /* Set clipping for output of glyph string S. S may be part of a mode
1119 line or menu if we don't have X toolkit support. */
1121 static INLINE void
1122 x_set_glyph_string_clipping (s)
1123 struct glyph_string *s;
1125 XRectangle r;
1126 get_glyph_string_clip_rect (s, &r);
1127 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
1131 /* RIF:
1132 Compute left and right overhang of glyph string S. If S is a glyph
1133 string for a composition, assume overhangs don't exist. */
1135 static void
1136 x_compute_glyph_string_overhangs (s)
1137 struct glyph_string *s;
1139 if (s->cmp == NULL
1140 && s->first_glyph->type == CHAR_GLYPH)
1142 XCharStruct cs;
1143 int direction, font_ascent, font_descent;
1144 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1145 &font_ascent, &font_descent, &cs);
1146 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1147 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1152 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1154 static INLINE void
1155 x_clear_glyph_string_rect (s, x, y, w, h)
1156 struct glyph_string *s;
1157 int x, y, w, h;
1159 XGCValues xgcv;
1160 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1161 XSetForeground (s->display, s->gc, xgcv.background);
1162 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1163 XSetForeground (s->display, s->gc, xgcv.foreground);
1167 /* Draw the background of glyph_string S. If S->background_filled_p
1168 is non-zero don't draw it. FORCE_P non-zero means draw the
1169 background even if it wouldn't be drawn normally. This is used
1170 when a string preceding S draws into the background of S, or S
1171 contains the first component of a composition. */
1173 static void
1174 x_draw_glyph_string_background (s, force_p)
1175 struct glyph_string *s;
1176 int force_p;
1178 /* Nothing to do if background has already been drawn or if it
1179 shouldn't be drawn in the first place. */
1180 if (!s->background_filled_p)
1182 int box_line_width = max (s->face->box_line_width, 0);
1184 if (s->stippled_p)
1186 /* Fill background with a stipple pattern. */
1187 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1188 XFillRectangle (s->display, s->window, s->gc, s->x,
1189 s->y + box_line_width,
1190 s->background_width,
1191 s->height - 2 * box_line_width);
1192 XSetFillStyle (s->display, s->gc, FillSolid);
1193 s->background_filled_p = 1;
1195 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1196 || s->font_not_found_p
1197 || s->extends_to_end_of_line_p
1198 || force_p)
1200 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1201 s->background_width,
1202 s->height - 2 * box_line_width);
1203 s->background_filled_p = 1;
1209 /* Draw the foreground of glyph string S. */
1211 static void
1212 x_draw_glyph_string_foreground (s)
1213 struct glyph_string *s;
1215 int i, x;
1217 /* If first glyph of S has a left box line, start drawing the text
1218 of S to the right of that box line. */
1219 if (s->face->box != FACE_NO_BOX
1220 && s->first_glyph->left_box_line_p)
1221 x = s->x + abs (s->face->box_line_width);
1222 else
1223 x = s->x;
1225 /* Draw characters of S as rectangles if S's font could not be
1226 loaded. */
1227 if (s->font_not_found_p)
1229 for (i = 0; i < s->nchars; ++i)
1231 struct glyph *g = s->first_glyph + i;
1232 XDrawRectangle (s->display, s->window,
1233 s->gc, x, s->y, g->pixel_width - 1,
1234 s->height - 1);
1235 x += g->pixel_width;
1238 else
1240 char *char1b = (char *) s->char2b;
1241 int boff = s->font_info->baseline_offset;
1243 if (s->font_info->vertical_centering)
1244 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1246 /* If we can use 8-bit functions, condense S->char2b. */
1247 if (!s->two_byte_p)
1248 for (i = 0; i < s->nchars; ++i)
1249 char1b[i] = s->char2b[i].byte2;
1251 /* Draw text with XDrawString if background has already been
1252 filled. Otherwise, use XDrawImageString. (Note that
1253 XDrawImageString is usually faster than XDrawString.) Always
1254 use XDrawImageString when drawing the cursor so that there is
1255 no chance that characters under a box cursor are invisible. */
1256 if (s->for_overlaps_p
1257 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1259 /* Draw characters with 16-bit or 8-bit functions. */
1260 if (s->two_byte_p)
1261 XDrawString16 (s->display, s->window, s->gc, x,
1262 s->ybase - boff, s->char2b, s->nchars);
1263 else
1264 XDrawString (s->display, s->window, s->gc, x,
1265 s->ybase - boff, char1b, s->nchars);
1267 else
1269 if (s->two_byte_p)
1270 XDrawImageString16 (s->display, s->window, s->gc, x,
1271 s->ybase - boff, s->char2b, s->nchars);
1272 else
1273 XDrawImageString (s->display, s->window, s->gc, x,
1274 s->ybase - boff, char1b, s->nchars);
1277 if (s->face->overstrike)
1279 /* For overstriking (to simulate bold-face), draw the
1280 characters again shifted to the right by one pixel. */
1281 if (s->two_byte_p)
1282 XDrawString16 (s->display, s->window, s->gc, x + 1,
1283 s->ybase - boff, s->char2b, s->nchars);
1284 else
1285 XDrawString (s->display, s->window, s->gc, x + 1,
1286 s->ybase - boff, char1b, s->nchars);
1291 /* Draw the foreground of composite glyph string S. */
1293 static void
1294 x_draw_composite_glyph_string_foreground (s)
1295 struct glyph_string *s;
1297 int i, x;
1299 /* If first glyph of S has a left box line, start drawing the text
1300 of S to the right of that box line. */
1301 if (s->face->box != FACE_NO_BOX
1302 && s->first_glyph->left_box_line_p)
1303 x = s->x + abs (s->face->box_line_width);
1304 else
1305 x = s->x;
1307 /* S is a glyph string for a composition. S->gidx is the index of
1308 the first character drawn for glyphs of this composition.
1309 S->gidx == 0 means we are drawing the very first character of
1310 this composition. */
1312 /* Draw a rectangle for the composition if the font for the very
1313 first character of the composition could not be loaded. */
1314 if (s->font_not_found_p)
1316 if (s->gidx == 0)
1317 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1318 s->width - 1, s->height - 1);
1320 else
1322 for (i = 0; i < s->nchars; i++, ++s->gidx)
1324 XDrawString16 (s->display, s->window, s->gc,
1325 x + s->cmp->offsets[s->gidx * 2],
1326 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1327 s->char2b + i, 1);
1328 if (s->face->overstrike)
1329 XDrawString16 (s->display, s->window, s->gc,
1330 x + s->cmp->offsets[s->gidx * 2] + 1,
1331 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1332 s->char2b + i, 1);
1338 #ifdef USE_X_TOOLKIT
1340 static struct frame *x_frame_of_widget P_ ((Widget));
1341 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1342 XrmValue *, XrmValue *, XtPointer *));
1343 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1344 XrmValue *, Cardinal *));
1347 /* Return the frame on which widget WIDGET is used.. Abort if frame
1348 cannot be determined. */
1350 static struct frame *
1351 x_frame_of_widget (widget)
1352 Widget widget;
1354 struct x_display_info *dpyinfo;
1355 Lisp_Object tail;
1356 struct frame *f;
1358 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1360 /* Find the top-level shell of the widget. Note that this function
1361 can be called when the widget is not yet realized, so XtWindow
1362 (widget) == 0. That's the reason we can't simply use
1363 x_any_window_to_frame. */
1364 while (!XtIsTopLevelShell (widget))
1365 widget = XtParent (widget);
1367 /* Look for a frame with that top-level widget. Allocate the color
1368 on that frame to get the right gamma correction value. */
1369 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1370 if (GC_FRAMEP (XCAR (tail))
1371 && (f = XFRAME (XCAR (tail)),
1372 (FRAME_X_P (f)
1373 && f->output_data.nothing != 1
1374 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1375 && f->output_data.x->widget == widget)
1376 return f;
1378 abort ();
1382 /* Allocate the color COLOR->pixel on the screen and display of
1383 widget WIDGET in colormap CMAP. If an exact match cannot be
1384 allocated, try the nearest color available. Value is non-zero
1385 if successful. This is called from lwlib. */
1388 x_alloc_nearest_color_for_widget (widget, cmap, color)
1389 Widget widget;
1390 Colormap cmap;
1391 XColor *color;
1393 struct frame *f = x_frame_of_widget (widget);
1394 return x_alloc_nearest_color (f, cmap, color);
1398 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1399 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1400 If this produces the same color as PIXEL, try a color where all RGB
1401 values have DELTA added. Return the allocated color in *PIXEL.
1402 DISPLAY is the X display, CMAP is the colormap to operate on.
1403 Value is non-zero if successful. */
1406 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1407 Widget widget;
1408 Display *display;
1409 Colormap cmap;
1410 unsigned long *pixel;
1411 double factor;
1412 int delta;
1414 struct frame *f = x_frame_of_widget (widget);
1415 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1419 /* Structure specifying which arguments should be passed by Xt to
1420 cvt_string_to_pixel. We want the widget's screen and colormap. */
1422 static XtConvertArgRec cvt_string_to_pixel_args[] =
1424 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1425 sizeof (Screen *)},
1426 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1427 sizeof (Colormap)}
1431 /* The address of this variable is returned by
1432 cvt_string_to_pixel. */
1434 static Pixel cvt_string_to_pixel_value;
1437 /* Convert a color name to a pixel color.
1439 DPY is the display we are working on.
1441 ARGS is an array of *NARGS XrmValue structures holding additional
1442 information about the widget for which the conversion takes place.
1443 The contents of this array are determined by the specification
1444 in cvt_string_to_pixel_args.
1446 FROM is a pointer to an XrmValue which points to the color name to
1447 convert. TO is an XrmValue in which to return the pixel color.
1449 CLOSURE_RET is a pointer to user-data, in which we record if
1450 we allocated the color or not.
1452 Value is True if successful, False otherwise. */
1454 static Boolean
1455 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1456 Display *dpy;
1457 XrmValue *args;
1458 Cardinal *nargs;
1459 XrmValue *from, *to;
1460 XtPointer *closure_ret;
1462 Screen *screen;
1463 Colormap cmap;
1464 Pixel pixel;
1465 String color_name;
1466 XColor color;
1468 if (*nargs != 2)
1470 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1471 "wrongParameters", "cvt_string_to_pixel",
1472 "XtToolkitError",
1473 "Screen and colormap args required", NULL, NULL);
1474 return False;
1477 screen = *(Screen **) args[0].addr;
1478 cmap = *(Colormap *) args[1].addr;
1479 color_name = (String) from->addr;
1481 if (strcmp (color_name, XtDefaultBackground) == 0)
1483 *closure_ret = (XtPointer) False;
1484 pixel = WhitePixelOfScreen (screen);
1486 else if (strcmp (color_name, XtDefaultForeground) == 0)
1488 *closure_ret = (XtPointer) False;
1489 pixel = BlackPixelOfScreen (screen);
1491 else if (XParseColor (dpy, cmap, color_name, &color)
1492 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1494 pixel = color.pixel;
1495 *closure_ret = (XtPointer) True;
1497 else
1499 String params[1];
1500 Cardinal nparams = 1;
1502 params[0] = color_name;
1503 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1504 "badValue", "cvt_string_to_pixel",
1505 "XtToolkitError", "Invalid color `%s'",
1506 params, &nparams);
1507 return False;
1510 if (to->addr != NULL)
1512 if (to->size < sizeof (Pixel))
1514 to->size = sizeof (Pixel);
1515 return False;
1518 *(Pixel *) to->addr = pixel;
1520 else
1522 cvt_string_to_pixel_value = pixel;
1523 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1526 to->size = sizeof (Pixel);
1527 return True;
1531 /* Free a pixel color which was previously allocated via
1532 cvt_string_to_pixel. This is registered as the destructor
1533 for this type of resource via XtSetTypeConverter.
1535 APP is the application context in which we work.
1537 TO is a pointer to an XrmValue holding the color to free.
1538 CLOSURE is the value we stored in CLOSURE_RET for this color
1539 in cvt_string_to_pixel.
1541 ARGS and NARGS are like for cvt_string_to_pixel. */
1543 static void
1544 cvt_pixel_dtor (app, to, closure, args, nargs)
1545 XtAppContext app;
1546 XrmValuePtr to;
1547 XtPointer closure;
1548 XrmValuePtr args;
1549 Cardinal *nargs;
1551 if (*nargs != 2)
1553 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1554 "XtToolkitError",
1555 "Screen and colormap arguments required",
1556 NULL, NULL);
1558 else if (closure != NULL)
1560 /* We did allocate the pixel, so free it. */
1561 Screen *screen = *(Screen **) args[0].addr;
1562 Colormap cmap = *(Colormap *) args[1].addr;
1563 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1564 (Pixel *) to->addr, 1);
1569 #endif /* USE_X_TOOLKIT */
1572 /* Value is an array of XColor structures for the contents of the
1573 color map of display DPY. Set *NCELLS to the size of the array.
1574 Note that this probably shouldn't be called for large color maps,
1575 say a 24-bit TrueColor map. */
1577 static const XColor *
1578 x_color_cells (dpy, ncells)
1579 Display *dpy;
1580 int *ncells;
1582 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1584 if (dpyinfo->color_cells == NULL)
1586 Screen *screen = dpyinfo->screen;
1587 int i;
1589 dpyinfo->ncolor_cells
1590 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1591 dpyinfo->color_cells
1592 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1593 * sizeof *dpyinfo->color_cells);
1595 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1596 dpyinfo->color_cells[i].pixel = i;
1598 XQueryColors (dpy, dpyinfo->cmap,
1599 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1602 *ncells = dpyinfo->ncolor_cells;
1603 return dpyinfo->color_cells;
1607 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1608 colors in COLORS. Use cached information, if available. */
1610 void
1611 x_query_colors (f, colors, ncolors)
1612 struct frame *f;
1613 XColor *colors;
1614 int ncolors;
1616 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1618 if (dpyinfo->color_cells)
1620 int i;
1621 for (i = 0; i < ncolors; ++i)
1623 unsigned long pixel = colors[i].pixel;
1624 xassert (pixel < dpyinfo->ncolor_cells);
1625 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1626 colors[i] = dpyinfo->color_cells[pixel];
1629 else
1630 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1634 /* On frame F, translate pixel color to RGB values for the color in
1635 COLOR. Use cached information, if available. */
1637 void
1638 x_query_color (f, color)
1639 struct frame *f;
1640 XColor *color;
1642 x_query_colors (f, color, 1);
1646 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1647 exact match can't be allocated, try the nearest color available.
1648 Value is non-zero if successful. Set *COLOR to the color
1649 allocated. */
1651 static int
1652 x_alloc_nearest_color_1 (dpy, cmap, color)
1653 Display *dpy;
1654 Colormap cmap;
1655 XColor *color;
1657 int rc;
1659 rc = XAllocColor (dpy, cmap, color);
1660 if (rc == 0)
1662 /* If we got to this point, the colormap is full, so we're going
1663 to try to get the next closest color. The algorithm used is
1664 a least-squares matching, which is what X uses for closest
1665 color matching with StaticColor visuals. */
1666 int nearest, i;
1667 unsigned long nearest_delta = ~0;
1668 int ncells;
1669 const XColor *cells = x_color_cells (dpy, &ncells);
1671 for (nearest = i = 0; i < ncells; ++i)
1673 long dred = (color->red >> 8) - (cells[i].red >> 8);
1674 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1675 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1676 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1678 if (delta < nearest_delta)
1680 nearest = i;
1681 nearest_delta = delta;
1685 color->red = cells[nearest].red;
1686 color->green = cells[nearest].green;
1687 color->blue = cells[nearest].blue;
1688 rc = XAllocColor (dpy, cmap, color);
1690 else
1692 /* If allocation succeeded, and the allocated pixel color is not
1693 equal to a cached pixel color recorded earlier, there was a
1694 change in the colormap, so clear the color cache. */
1695 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1696 XColor *cached_color;
1698 if (dpyinfo->color_cells
1699 && (cached_color = &dpyinfo->color_cells[color->pixel],
1700 (cached_color->red != color->red
1701 || cached_color->blue != color->blue
1702 || cached_color->green != color->green)))
1704 xfree (dpyinfo->color_cells);
1705 dpyinfo->color_cells = NULL;
1706 dpyinfo->ncolor_cells = 0;
1710 #ifdef DEBUG_X_COLORS
1711 if (rc)
1712 register_color (color->pixel);
1713 #endif /* DEBUG_X_COLORS */
1715 return rc;
1719 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1720 exact match can't be allocated, try the nearest color available.
1721 Value is non-zero if successful. Set *COLOR to the color
1722 allocated. */
1725 x_alloc_nearest_color (f, cmap, color)
1726 struct frame *f;
1727 Colormap cmap;
1728 XColor *color;
1730 gamma_correct (f, color);
1731 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1735 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1736 It's necessary to do this instead of just using PIXEL directly to
1737 get color reference counts right. */
1739 unsigned long
1740 x_copy_color (f, pixel)
1741 struct frame *f;
1742 unsigned long pixel;
1744 XColor color;
1746 color.pixel = pixel;
1747 BLOCK_INPUT;
1748 x_query_color (f, &color);
1749 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1750 UNBLOCK_INPUT;
1751 #ifdef DEBUG_X_COLORS
1752 register_color (pixel);
1753 #endif
1754 return color.pixel;
1758 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1759 It's necessary to do this instead of just using PIXEL directly to
1760 get color reference counts right. */
1762 unsigned long
1763 x_copy_dpy_color (dpy, cmap, pixel)
1764 Display *dpy;
1765 Colormap cmap;
1766 unsigned long pixel;
1768 XColor color;
1770 color.pixel = pixel;
1771 BLOCK_INPUT;
1772 XQueryColor (dpy, cmap, &color);
1773 XAllocColor (dpy, cmap, &color);
1774 UNBLOCK_INPUT;
1775 #ifdef DEBUG_X_COLORS
1776 register_color (pixel);
1777 #endif
1778 return color.pixel;
1782 /* Brightness beyond which a color won't have its highlight brightness
1783 boosted.
1785 Nominally, highlight colors for `3d' faces are calculated by
1786 brightening an object's color by a constant scale factor, but this
1787 doesn't yield good results for dark colors, so for colors who's
1788 brightness is less than this value (on a scale of 0-65535) have an
1789 use an additional additive factor.
1791 The value here is set so that the default menu-bar/mode-line color
1792 (grey75) will not have its highlights changed at all. */
1793 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1796 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1797 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1798 If this produces the same color as PIXEL, try a color where all RGB
1799 values have DELTA added. Return the allocated color in *PIXEL.
1800 DISPLAY is the X display, CMAP is the colormap to operate on.
1801 Value is non-zero if successful. */
1803 static int
1804 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1805 struct frame *f;
1806 Display *display;
1807 Colormap cmap;
1808 unsigned long *pixel;
1809 double factor;
1810 int delta;
1812 XColor color, new;
1813 long bright;
1814 int success_p;
1816 /* Get RGB color values. */
1817 color.pixel = *pixel;
1818 x_query_color (f, &color);
1820 /* Change RGB values by specified FACTOR. Avoid overflow! */
1821 xassert (factor >= 0);
1822 new.red = min (0xffff, factor * color.red);
1823 new.green = min (0xffff, factor * color.green);
1824 new.blue = min (0xffff, factor * color.blue);
1826 /* Calculate brightness of COLOR. */
1827 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1829 /* We only boost colors that are darker than
1830 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1831 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1832 /* Make an additive adjustment to NEW, because it's dark enough so
1833 that scaling by FACTOR alone isn't enough. */
1835 /* How far below the limit this color is (0 - 1, 1 being darker). */
1836 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1837 /* The additive adjustment. */
1838 int min_delta = delta * dimness * factor / 2;
1840 if (factor < 1)
1842 new.red = max (0, new.red - min_delta);
1843 new.green = max (0, new.green - min_delta);
1844 new.blue = max (0, new.blue - min_delta);
1846 else
1848 new.red = min (0xffff, min_delta + new.red);
1849 new.green = min (0xffff, min_delta + new.green);
1850 new.blue = min (0xffff, min_delta + new.blue);
1854 /* Try to allocate the color. */
1855 success_p = x_alloc_nearest_color (f, cmap, &new);
1856 if (success_p)
1858 if (new.pixel == *pixel)
1860 /* If we end up with the same color as before, try adding
1861 delta to the RGB values. */
1862 x_free_colors (f, &new.pixel, 1);
1864 new.red = min (0xffff, delta + color.red);
1865 new.green = min (0xffff, delta + color.green);
1866 new.blue = min (0xffff, delta + color.blue);
1867 success_p = x_alloc_nearest_color (f, cmap, &new);
1869 else
1870 success_p = 1;
1871 *pixel = new.pixel;
1874 return success_p;
1878 /* Set up the foreground color for drawing relief lines of glyph
1879 string S. RELIEF is a pointer to a struct relief containing the GC
1880 with which lines will be drawn. Use a color that is FACTOR or
1881 DELTA lighter or darker than the relief's background which is found
1882 in S->f->output_data.x->relief_background. If such a color cannot
1883 be allocated, use DEFAULT_PIXEL, instead. */
1885 static void
1886 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1887 struct frame *f;
1888 struct relief *relief;
1889 double factor;
1890 int delta;
1891 unsigned long default_pixel;
1893 XGCValues xgcv;
1894 struct x_output *di = f->output_data.x;
1895 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1896 unsigned long pixel;
1897 unsigned long background = di->relief_background;
1898 Colormap cmap = FRAME_X_COLORMAP (f);
1899 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1900 Display *dpy = FRAME_X_DISPLAY (f);
1902 xgcv.graphics_exposures = False;
1903 xgcv.line_width = 1;
1905 /* Free previously allocated color. The color cell will be reused
1906 when it has been freed as many times as it was allocated, so this
1907 doesn't affect faces using the same colors. */
1908 if (relief->gc
1909 && relief->allocated_p)
1911 x_free_colors (f, &relief->pixel, 1);
1912 relief->allocated_p = 0;
1915 /* Allocate new color. */
1916 xgcv.foreground = default_pixel;
1917 pixel = background;
1918 if (dpyinfo->n_planes != 1
1919 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1921 relief->allocated_p = 1;
1922 xgcv.foreground = relief->pixel = pixel;
1925 if (relief->gc == 0)
1927 xgcv.stipple = dpyinfo->gray;
1928 mask |= GCStipple;
1929 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1931 else
1932 XChangeGC (dpy, relief->gc, mask, &xgcv);
1936 /* Set up colors for the relief lines around glyph string S. */
1938 static void
1939 x_setup_relief_colors (s)
1940 struct glyph_string *s;
1942 struct x_output *di = s->f->output_data.x;
1943 unsigned long color;
1945 if (s->face->use_box_color_for_shadows_p)
1946 color = s->face->box_color;
1947 else if (s->first_glyph->type == IMAGE_GLYPH
1948 && s->img->pixmap
1949 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1950 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1951 else
1953 XGCValues xgcv;
1955 /* Get the background color of the face. */
1956 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
1957 color = xgcv.background;
1960 if (di->white_relief.gc == 0
1961 || color != di->relief_background)
1963 di->relief_background = color;
1964 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1965 WHITE_PIX_DEFAULT (s->f));
1966 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1967 BLACK_PIX_DEFAULT (s->f));
1972 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1973 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1974 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1975 relief. LEFT_P non-zero means draw a relief on the left side of
1976 the rectangle. RIGHT_P non-zero means draw a relief on the right
1977 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1978 when drawing. */
1980 static void
1981 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
1982 raised_p, left_p, right_p, clip_rect)
1983 struct frame *f;
1984 int left_x, top_y, right_x, bottom_y, width, left_p, right_p, raised_p;
1985 XRectangle *clip_rect;
1987 Display *dpy = FRAME_X_DISPLAY (f);
1988 Window window = FRAME_X_WINDOW (f);
1989 int i;
1990 GC gc;
1992 if (raised_p)
1993 gc = f->output_data.x->white_relief.gc;
1994 else
1995 gc = f->output_data.x->black_relief.gc;
1996 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1998 /* Top. */
1999 for (i = 0; i < width; ++i)
2000 XDrawLine (dpy, window, gc,
2001 left_x + i * left_p, top_y + i,
2002 right_x + 1 - i * right_p, top_y + i);
2004 /* Left. */
2005 if (left_p)
2006 for (i = 0; i < width; ++i)
2007 XDrawLine (dpy, window, gc,
2008 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2010 XSetClipMask (dpy, gc, None);
2011 if (raised_p)
2012 gc = f->output_data.x->black_relief.gc;
2013 else
2014 gc = f->output_data.x->white_relief.gc;
2015 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2017 /* Bottom. */
2018 for (i = 0; i < width; ++i)
2019 XDrawLine (dpy, window, gc,
2020 left_x + i * left_p, bottom_y - i,
2021 right_x + 1 - i * right_p, bottom_y - i);
2023 /* Right. */
2024 if (right_p)
2025 for (i = 0; i < width; ++i)
2026 XDrawLine (dpy, window, gc,
2027 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2029 XSetClipMask (dpy, gc, None);
2033 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2034 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2035 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2036 left side of the rectangle. RIGHT_P non-zero means draw a line
2037 on the right side of the rectangle. CLIP_RECT is the clipping
2038 rectangle to use when drawing. */
2040 static void
2041 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2042 left_p, right_p, clip_rect)
2043 struct glyph_string *s;
2044 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2045 XRectangle *clip_rect;
2047 XGCValues xgcv;
2049 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2050 XSetForeground (s->display, s->gc, s->face->box_color);
2051 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2053 /* Top. */
2054 XFillRectangle (s->display, s->window, s->gc,
2055 left_x, top_y, right_x - left_x + 1, width);
2057 /* Left. */
2058 if (left_p)
2059 XFillRectangle (s->display, s->window, s->gc,
2060 left_x, top_y, width, bottom_y - top_y + 1);
2062 /* Bottom. */
2063 XFillRectangle (s->display, s->window, s->gc,
2064 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2066 /* Right. */
2067 if (right_p)
2068 XFillRectangle (s->display, s->window, s->gc,
2069 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2071 XSetForeground (s->display, s->gc, xgcv.foreground);
2072 XSetClipMask (s->display, s->gc, None);
2076 /* Draw a box around glyph string S. */
2078 static void
2079 x_draw_glyph_string_box (s)
2080 struct glyph_string *s;
2082 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2083 int left_p, right_p;
2084 struct glyph *last_glyph;
2085 XRectangle clip_rect;
2087 last_x = window_box_right (s->w, s->area);
2088 if (s->row->full_width_p
2089 && !s->w->pseudo_window_p)
2091 last_x += WINDOW_RIGHT_SCROLL_BAR_AREA_WIDTH (s->w);
2092 if (s->area != RIGHT_MARGIN_AREA
2093 || WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (s->w))
2094 last_x += WINDOW_RIGHT_FRINGE_WIDTH (s->w);
2097 /* The glyph that may have a right box line. */
2098 last_glyph = (s->cmp || s->img
2099 ? s->first_glyph
2100 : s->first_glyph + s->nchars - 1);
2102 width = abs (s->face->box_line_width);
2103 raised_p = s->face->box == FACE_RAISED_BOX;
2104 left_x = s->x;
2105 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2106 ? last_x - 1
2107 : min (last_x, s->x + s->background_width) - 1);
2108 top_y = s->y;
2109 bottom_y = top_y + s->height - 1;
2111 left_p = (s->first_glyph->left_box_line_p
2112 || (s->hl == DRAW_MOUSE_FACE
2113 && (s->prev == NULL
2114 || s->prev->hl != s->hl)));
2115 right_p = (last_glyph->right_box_line_p
2116 || (s->hl == DRAW_MOUSE_FACE
2117 && (s->next == NULL
2118 || s->next->hl != s->hl)));
2120 get_glyph_string_clip_rect (s, &clip_rect);
2122 if (s->face->box == FACE_SIMPLE_BOX)
2123 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2124 left_p, right_p, &clip_rect);
2125 else
2127 x_setup_relief_colors (s);
2128 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2129 width, raised_p, left_p, right_p, &clip_rect);
2134 /* Draw foreground of image glyph string S. */
2136 static void
2137 x_draw_image_foreground (s)
2138 struct glyph_string *s;
2140 int x;
2141 int y = s->ybase - image_ascent (s->img, s->face);
2143 /* If first glyph of S has a left box line, start drawing it to the
2144 right of that line. */
2145 if (s->face->box != FACE_NO_BOX
2146 && s->first_glyph->left_box_line_p)
2147 x = s->x + abs (s->face->box_line_width);
2148 else
2149 x = s->x;
2151 /* If there is a margin around the image, adjust x- and y-position
2152 by that margin. */
2153 x += s->img->hmargin;
2154 y += s->img->vmargin;
2156 if (s->img->pixmap)
2158 if (s->img->mask)
2160 /* We can't set both a clip mask and use XSetClipRectangles
2161 because the latter also sets a clip mask. We also can't
2162 trust on the shape extension to be available
2163 (XShapeCombineRegion). So, compute the rectangle to draw
2164 manually. */
2165 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2166 | GCFunction);
2167 XGCValues xgcv;
2168 XRectangle clip_rect, image_rect, r;
2170 xgcv.clip_mask = s->img->mask;
2171 xgcv.clip_x_origin = x;
2172 xgcv.clip_y_origin = y;
2173 xgcv.function = GXcopy;
2174 XChangeGC (s->display, s->gc, mask, &xgcv);
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 else
2187 XRectangle clip_rect, image_rect, r;
2189 get_glyph_string_clip_rect (s, &clip_rect);
2190 image_rect.x = x;
2191 image_rect.y = y;
2192 image_rect.width = s->img->width;
2193 image_rect.height = s->img->height;
2194 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2195 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2196 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
2198 /* When the image has a mask, we can expect that at
2199 least part of a mouse highlight or a block cursor will
2200 be visible. If the image doesn't have a mask, make
2201 a block cursor visible by drawing a rectangle around
2202 the image. I believe it's looking better if we do
2203 nothing here for mouse-face. */
2204 if (s->hl == DRAW_CURSOR)
2206 int r = s->img->relief;
2207 if (r < 0) r = -r;
2208 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2209 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
2213 else
2214 /* Draw a rectangle if image could not be loaded. */
2215 XDrawRectangle (s->display, s->window, s->gc, x, y,
2216 s->img->width - 1, s->img->height - 1);
2220 /* Draw a relief around the image glyph string S. */
2222 static void
2223 x_draw_image_relief (s)
2224 struct glyph_string *s;
2226 int x0, y0, x1, y1, thick, raised_p;
2227 XRectangle r;
2228 int x;
2229 int y = s->ybase - image_ascent (s->img, s->face);
2231 /* If first glyph of S has a left box line, start drawing it to the
2232 right of that line. */
2233 if (s->face->box != FACE_NO_BOX
2234 && s->first_glyph->left_box_line_p)
2235 x = s->x + abs (s->face->box_line_width);
2236 else
2237 x = s->x;
2239 /* If there is a margin around the image, adjust x- and y-position
2240 by that margin. */
2241 x += s->img->hmargin;
2242 y += s->img->vmargin;
2244 if (s->hl == DRAW_IMAGE_SUNKEN
2245 || s->hl == DRAW_IMAGE_RAISED)
2247 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2248 raised_p = s->hl == DRAW_IMAGE_RAISED;
2250 else
2252 thick = abs (s->img->relief);
2253 raised_p = s->img->relief > 0;
2256 x0 = x - thick;
2257 y0 = y - thick;
2258 x1 = x + s->img->width + thick - 1;
2259 y1 = y + s->img->height + thick - 1;
2261 x_setup_relief_colors (s);
2262 get_glyph_string_clip_rect (s, &r);
2263 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
2267 /* Draw the foreground of image glyph string S to PIXMAP. */
2269 static void
2270 x_draw_image_foreground_1 (s, pixmap)
2271 struct glyph_string *s;
2272 Pixmap pixmap;
2274 int x;
2275 int y = s->ybase - s->y - image_ascent (s->img, s->face);
2277 /* If first glyph of S has a left box line, start drawing it to the
2278 right of that line. */
2279 if (s->face->box != FACE_NO_BOX
2280 && s->first_glyph->left_box_line_p)
2281 x = abs (s->face->box_line_width);
2282 else
2283 x = 0;
2285 /* If there is a margin around the image, adjust x- and y-position
2286 by that margin. */
2287 x += s->img->hmargin;
2288 y += s->img->vmargin;
2290 if (s->img->pixmap)
2292 if (s->img->mask)
2294 /* We can't set both a clip mask and use XSetClipRectangles
2295 because the latter also sets a clip mask. We also can't
2296 trust on the shape extension to be available
2297 (XShapeCombineRegion). So, compute the rectangle to draw
2298 manually. */
2299 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2300 | GCFunction);
2301 XGCValues xgcv;
2303 xgcv.clip_mask = s->img->mask;
2304 xgcv.clip_x_origin = x;
2305 xgcv.clip_y_origin = y;
2306 xgcv.function = GXcopy;
2307 XChangeGC (s->display, s->gc, mask, &xgcv);
2309 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2310 0, 0, s->img->width, s->img->height, x, y);
2311 XSetClipMask (s->display, s->gc, None);
2313 else
2315 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2316 0, 0, s->img->width, s->img->height, x, y);
2318 /* When the image has a mask, we can expect that at
2319 least part of a mouse highlight or a block cursor will
2320 be visible. If the image doesn't have a mask, make
2321 a block cursor visible by drawing a rectangle around
2322 the image. I believe it's looking better if we do
2323 nothing here for mouse-face. */
2324 if (s->hl == DRAW_CURSOR)
2326 int r = s->img->relief;
2327 if (r < 0) r = -r;
2328 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2329 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
2333 else
2334 /* Draw a rectangle if image could not be loaded. */
2335 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2336 s->img->width - 1, s->img->height - 1);
2340 /* Draw part of the background of glyph string S. X, Y, W, and H
2341 give the rectangle to draw. */
2343 static void
2344 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2345 struct glyph_string *s;
2346 int x, y, w, h;
2348 if (s->stippled_p)
2350 /* Fill background with a stipple pattern. */
2351 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2352 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2353 XSetFillStyle (s->display, s->gc, FillSolid);
2355 else
2356 x_clear_glyph_string_rect (s, x, y, w, h);
2360 /* Draw image glyph string S.
2362 s->y
2363 s->x +-------------------------
2364 | s->face->box
2366 | +-------------------------
2367 | | s->img->margin
2369 | | +-------------------
2370 | | | the image
2374 static void
2375 x_draw_image_glyph_string (s)
2376 struct glyph_string *s;
2378 int x, y;
2379 int box_line_hwidth = abs (s->face->box_line_width);
2380 int box_line_vwidth = max (s->face->box_line_width, 0);
2381 int height;
2382 Pixmap pixmap = None;
2384 height = s->height - 2 * box_line_vwidth;
2387 /* Fill background with face under the image. Do it only if row is
2388 taller than image or if image has a clip mask to reduce
2389 flickering. */
2390 s->stippled_p = s->face->stipple != 0;
2391 if (height > s->img->height
2392 || s->img->hmargin
2393 || s->img->vmargin
2394 || s->img->mask
2395 || s->img->pixmap == 0
2396 || s->width != s->background_width)
2398 if (box_line_hwidth && s->first_glyph->left_box_line_p)
2399 x = s->x + box_line_hwidth;
2400 else
2401 x = s->x;
2403 y = s->y + box_line_vwidth;
2405 if (s->img->mask)
2407 /* Create a pixmap as large as the glyph string. Fill it
2408 with the background color. Copy the image to it, using
2409 its mask. Copy the temporary pixmap to the display. */
2410 Screen *screen = FRAME_X_SCREEN (s->f);
2411 int depth = DefaultDepthOfScreen (screen);
2413 /* Create a pixmap as large as the glyph string. */
2414 pixmap = XCreatePixmap (s->display, s->window,
2415 s->background_width,
2416 s->height, depth);
2418 /* Don't clip in the following because we're working on the
2419 pixmap. */
2420 XSetClipMask (s->display, s->gc, None);
2422 /* Fill the pixmap with the background color/stipple. */
2423 if (s->stippled_p)
2425 /* Fill background with a stipple pattern. */
2426 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2427 XFillRectangle (s->display, pixmap, s->gc,
2428 0, 0, s->background_width, s->height);
2429 XSetFillStyle (s->display, s->gc, FillSolid);
2431 else
2433 XGCValues xgcv;
2434 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2435 &xgcv);
2436 XSetForeground (s->display, s->gc, xgcv.background);
2437 XFillRectangle (s->display, pixmap, s->gc,
2438 0, 0, s->background_width, s->height);
2439 XSetForeground (s->display, s->gc, xgcv.foreground);
2442 else
2443 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2445 s->background_filled_p = 1;
2448 /* Draw the foreground. */
2449 if (pixmap != None)
2451 x_draw_image_foreground_1 (s, pixmap);
2452 x_set_glyph_string_clipping (s);
2453 XCopyArea (s->display, pixmap, s->window, s->gc,
2454 0, 0, s->background_width, s->height, s->x, s->y);
2455 XFreePixmap (s->display, pixmap);
2457 else
2458 x_draw_image_foreground (s);
2460 /* If we must draw a relief around the image, do it. */
2461 if (s->img->relief
2462 || s->hl == DRAW_IMAGE_RAISED
2463 || s->hl == DRAW_IMAGE_SUNKEN)
2464 x_draw_image_relief (s);
2468 /* Draw stretch glyph string S. */
2470 static void
2471 x_draw_stretch_glyph_string (s)
2472 struct glyph_string *s;
2474 xassert (s->first_glyph->type == STRETCH_GLYPH);
2475 s->stippled_p = s->face->stipple != 0;
2477 if (s->hl == DRAW_CURSOR
2478 && !x_stretch_cursor_p)
2480 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2481 as wide as the stretch glyph. */
2482 int width = min (FRAME_COLUMN_WIDTH (s->f), s->background_width);
2484 /* Draw cursor. */
2485 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
2487 /* Clear rest using the GC of the original non-cursor face. */
2488 if (width < s->background_width)
2490 int x = s->x + width, y = s->y;
2491 int w = s->background_width - width, h = s->height;
2492 XRectangle r;
2493 GC gc;
2495 if (s->row->mouse_face_p
2496 && cursor_in_mouse_face_p (s->w))
2498 x_set_mouse_face_gc (s);
2499 gc = s->gc;
2501 else
2502 gc = s->face->gc;
2504 get_glyph_string_clip_rect (s, &r);
2505 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2507 if (s->face->stipple)
2509 /* Fill background with a stipple pattern. */
2510 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2511 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2512 XSetFillStyle (s->display, gc, FillSolid);
2514 else
2516 XGCValues xgcv;
2517 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2518 XSetForeground (s->display, gc, xgcv.background);
2519 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2520 XSetForeground (s->display, gc, xgcv.foreground);
2524 else if (!s->background_filled_p)
2525 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
2526 s->height);
2528 s->background_filled_p = 1;
2532 /* Draw glyph string S. */
2534 static void
2535 x_draw_glyph_string (s)
2536 struct glyph_string *s;
2538 int relief_drawn_p = 0;
2540 /* If S draws into the background of its successor, draw the
2541 background of the successor first so that S can draw into it.
2542 This makes S->next use XDrawString instead of XDrawImageString. */
2543 if (s->next && s->right_overhang && !s->for_overlaps_p)
2545 xassert (s->next->img == NULL);
2546 x_set_glyph_string_gc (s->next);
2547 x_set_glyph_string_clipping (s->next);
2548 x_draw_glyph_string_background (s->next, 1);
2551 /* Set up S->gc, set clipping and draw S. */
2552 x_set_glyph_string_gc (s);
2554 /* Draw relief (if any) in advance for char/composition so that the
2555 glyph string can be drawn over it. */
2556 if (!s->for_overlaps_p
2557 && s->face->box != FACE_NO_BOX
2558 && (s->first_glyph->type == CHAR_GLYPH
2559 || s->first_glyph->type == COMPOSITE_GLYPH))
2562 x_set_glyph_string_clipping (s);
2563 x_draw_glyph_string_background (s, 1);
2564 x_draw_glyph_string_box (s);
2565 x_set_glyph_string_clipping (s);
2566 relief_drawn_p = 1;
2568 else
2569 x_set_glyph_string_clipping (s);
2571 switch (s->first_glyph->type)
2573 case IMAGE_GLYPH:
2574 x_draw_image_glyph_string (s);
2575 break;
2577 case STRETCH_GLYPH:
2578 x_draw_stretch_glyph_string (s);
2579 break;
2581 case CHAR_GLYPH:
2582 if (s->for_overlaps_p)
2583 s->background_filled_p = 1;
2584 else
2585 x_draw_glyph_string_background (s, 0);
2586 x_draw_glyph_string_foreground (s);
2587 break;
2589 case COMPOSITE_GLYPH:
2590 if (s->for_overlaps_p || s->gidx > 0)
2591 s->background_filled_p = 1;
2592 else
2593 x_draw_glyph_string_background (s, 1);
2594 x_draw_composite_glyph_string_foreground (s);
2595 break;
2597 default:
2598 abort ();
2601 if (!s->for_overlaps_p)
2603 /* Draw underline. */
2604 if (s->face->underline_p)
2606 unsigned long tem, h;
2607 int y;
2609 /* Get the underline thickness. Default is 1 pixel. */
2610 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
2611 h = 1;
2613 /* Get the underline position. This is the recommended
2614 vertical offset in pixels from the baseline to the top of
2615 the underline. This is a signed value according to the
2616 specs, and its default is
2618 ROUND ((maximum descent) / 2), with
2619 ROUND(x) = floor (x + 0.5) */
2621 if (x_use_underline_position_properties
2622 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
2623 y = s->ybase + (long) tem;
2624 else if (s->face->font)
2625 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2626 else
2627 y = s->y + s->height - h;
2629 if (s->face->underline_defaulted_p)
2630 XFillRectangle (s->display, s->window, s->gc,
2631 s->x, y, s->width, h);
2632 else
2634 XGCValues xgcv;
2635 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2636 XSetForeground (s->display, s->gc, s->face->underline_color);
2637 XFillRectangle (s->display, s->window, s->gc,
2638 s->x, y, s->width, h);
2639 XSetForeground (s->display, s->gc, xgcv.foreground);
2643 /* Draw overline. */
2644 if (s->face->overline_p)
2646 unsigned long dy = 0, h = 1;
2648 if (s->face->overline_color_defaulted_p)
2649 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2650 s->width, h);
2651 else
2653 XGCValues xgcv;
2654 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2655 XSetForeground (s->display, s->gc, s->face->overline_color);
2656 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2657 s->width, h);
2658 XSetForeground (s->display, s->gc, xgcv.foreground);
2662 /* Draw strike-through. */
2663 if (s->face->strike_through_p)
2665 unsigned long h = 1;
2666 unsigned long dy = (s->height - h) / 2;
2668 if (s->face->strike_through_color_defaulted_p)
2669 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2670 s->width, h);
2671 else
2673 XGCValues xgcv;
2674 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2675 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2676 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2677 s->width, h);
2678 XSetForeground (s->display, s->gc, xgcv.foreground);
2682 /* Draw relief if not yet drawn. */
2683 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2684 x_draw_glyph_string_box (s);
2687 /* Reset clipping. */
2688 XSetClipMask (s->display, s->gc, None);
2691 /* Shift display to make room for inserted glyphs. */
2693 void
2694 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2695 struct frame *f;
2696 int x, y, width, height, shift_by;
2698 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2699 f->output_data.x->normal_gc,
2700 x, y, width, height,
2701 x + shift_by, y);
2704 /* Delete N glyphs at the nominal cursor position. Not implemented
2705 for X frames. */
2707 static void
2708 x_delete_glyphs (n)
2709 register int n;
2711 abort ();
2715 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2716 If they are <= 0, this is probably an error. */
2718 void
2719 x_clear_area (dpy, window, x, y, width, height, exposures)
2720 Display *dpy;
2721 Window window;
2722 int x, y;
2723 int width, height;
2724 int exposures;
2726 xassert (width > 0 && height > 0);
2727 XClearArea (dpy, window, x, y, width, height, exposures);
2731 /* Clear entire frame. If updating_frame is non-null, clear that
2732 frame. Otherwise clear the selected frame. */
2734 static void
2735 x_clear_frame ()
2737 struct frame *f;
2739 if (updating_frame)
2740 f = updating_frame;
2741 else
2742 f = SELECTED_FRAME ();
2744 /* Clearing the frame will erase any cursor, so mark them all as no
2745 longer visible. */
2746 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2747 output_cursor.hpos = output_cursor.vpos = 0;
2748 output_cursor.x = -1;
2750 /* We don't set the output cursor here because there will always
2751 follow an explicit cursor_to. */
2752 BLOCK_INPUT;
2753 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2755 /* We have to clear the scroll bars, too. If we have changed
2756 colors or something like that, then they should be notified. */
2757 x_scroll_bar_clear (f);
2759 XFlush (FRAME_X_DISPLAY (f));
2761 #ifdef USE_GTK
2762 xg_frame_cleared (f);
2763 #endif
2765 UNBLOCK_INPUT;
2770 /* Invert the middle quarter of the frame for .15 sec. */
2772 /* We use the select system call to do the waiting, so we have to make
2773 sure it's available. If it isn't, we just won't do visual bells. */
2775 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2778 /* Subtract the `struct timeval' values X and Y, storing the result in
2779 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2781 static int
2782 timeval_subtract (result, x, y)
2783 struct timeval *result, x, y;
2785 /* Perform the carry for the later subtraction by updating y. This
2786 is safer because on some systems the tv_sec member is unsigned. */
2787 if (x.tv_usec < y.tv_usec)
2789 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2790 y.tv_usec -= 1000000 * nsec;
2791 y.tv_sec += nsec;
2794 if (x.tv_usec - y.tv_usec > 1000000)
2796 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2797 y.tv_usec += 1000000 * nsec;
2798 y.tv_sec -= nsec;
2801 /* Compute the time remaining to wait. tv_usec is certainly
2802 positive. */
2803 result->tv_sec = x.tv_sec - y.tv_sec;
2804 result->tv_usec = x.tv_usec - y.tv_usec;
2806 /* Return indication of whether the result should be considered
2807 negative. */
2808 return x.tv_sec < y.tv_sec;
2811 void
2812 XTflash (f)
2813 struct frame *f;
2815 BLOCK_INPUT;
2818 GC gc;
2820 /* Create a GC that will use the GXxor function to flip foreground
2821 pixels into background pixels. */
2823 XGCValues values;
2825 values.function = GXxor;
2826 values.foreground = (f->output_data.x->foreground_pixel
2827 ^ f->output_data.x->background_pixel);
2829 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2830 GCFunction | GCForeground, &values);
2834 /* Get the height not including a menu bar widget. */
2835 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
2836 /* Height of each line to flash. */
2837 int flash_height = FRAME_LINE_HEIGHT (f);
2838 /* These will be the left and right margins of the rectangles. */
2839 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
2840 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
2842 int width;
2844 /* Don't flash the area between a scroll bar and the frame
2845 edge it is next to. */
2846 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
2848 case vertical_scroll_bar_left:
2849 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2850 break;
2852 case vertical_scroll_bar_right:
2853 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2854 break;
2856 default:
2857 break;
2860 width = flash_right - flash_left;
2862 /* If window is tall, flash top and bottom line. */
2863 if (height > 3 * FRAME_LINE_HEIGHT (f))
2865 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2866 flash_left,
2867 (FRAME_INTERNAL_BORDER_WIDTH (f)
2868 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
2869 width, flash_height);
2870 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2871 flash_left,
2872 (height - flash_height
2873 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2874 width, flash_height);
2876 else
2877 /* If it is short, flash it all. */
2878 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2879 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2880 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2882 x_flush (f);
2885 struct timeval wakeup;
2887 EMACS_GET_TIME (wakeup);
2889 /* Compute time to wait until, propagating carry from usecs. */
2890 wakeup.tv_usec += 150000;
2891 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
2892 wakeup.tv_usec %= 1000000;
2894 /* Keep waiting until past the time wakeup or any input gets
2895 available. */
2896 while (! detect_input_pending ())
2898 struct timeval current;
2899 struct timeval timeout;
2901 EMACS_GET_TIME (current);
2903 /* Break if result would be negative. */
2904 if (timeval_subtract (&current, wakeup, current))
2905 break;
2907 /* How long `select' should wait. */
2908 timeout.tv_sec = 0;
2909 timeout.tv_usec = 10000;
2911 /* Try to wait that long--but we might wake up sooner. */
2912 select (0, NULL, NULL, NULL, &timeout);
2916 /* If window is tall, flash top and bottom line. */
2917 if (height > 3 * FRAME_LINE_HEIGHT (f))
2919 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2920 flash_left,
2921 (FRAME_INTERNAL_BORDER_WIDTH (f)
2922 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
2923 width, flash_height);
2924 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2925 flash_left,
2926 (height - flash_height
2927 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2928 width, flash_height);
2930 else
2931 /* If it is short, flash it all. */
2932 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2933 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2934 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2936 XFreeGC (FRAME_X_DISPLAY (f), gc);
2937 x_flush (f);
2941 UNBLOCK_INPUT;
2944 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
2947 /* Make audible bell. */
2949 void
2950 XTring_bell ()
2952 struct frame *f = SELECTED_FRAME ();
2954 if (FRAME_X_DISPLAY (f))
2956 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2957 if (visible_bell)
2958 XTflash (f);
2959 else
2960 #endif
2962 BLOCK_INPUT;
2963 XBell (FRAME_X_DISPLAY (f), 0);
2964 XFlush (FRAME_X_DISPLAY (f));
2965 UNBLOCK_INPUT;
2971 /* Specify how many text lines, from the top of the window,
2972 should be affected by insert-lines and delete-lines operations.
2973 This, and those operations, are used only within an update
2974 that is bounded by calls to x_update_begin and x_update_end. */
2976 static void
2977 XTset_terminal_window (n)
2978 register int n;
2980 /* This function intentionally left blank. */
2985 /***********************************************************************
2986 Line Dance
2987 ***********************************************************************/
2989 /* Perform an insert-lines or delete-lines operation, inserting N
2990 lines or deleting -N lines at vertical position VPOS. */
2992 static void
2993 x_ins_del_lines (vpos, n)
2994 int vpos, n;
2996 abort ();
3000 /* Scroll part of the display as described by RUN. */
3002 static void
3003 x_scroll_run (w, run)
3004 struct window *w;
3005 struct run *run;
3007 struct frame *f = XFRAME (w->frame);
3008 int x, y, width, height, from_y, to_y, bottom_y;
3010 /* Get frame-relative bounding box of the text display area of W,
3011 without mode lines. Include in this box the left and right
3012 fringe of W. */
3013 window_box (w, -1, &x, &y, &width, &height);
3015 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3016 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3017 bottom_y = y + height;
3019 if (to_y < from_y)
3021 /* Scrolling up. Make sure we don't copy part of the mode
3022 line at the bottom. */
3023 if (from_y + run->height > bottom_y)
3024 height = bottom_y - from_y;
3025 else
3026 height = run->height;
3028 else
3030 /* Scolling down. Make sure we don't copy over the mode line.
3031 at the bottom. */
3032 if (to_y + run->height > bottom_y)
3033 height = bottom_y - to_y;
3034 else
3035 height = run->height;
3038 BLOCK_INPUT;
3040 /* Cursor off. Will be switched on again in x_update_window_end. */
3041 updated_window = w;
3042 x_clear_cursor (w);
3044 XCopyArea (FRAME_X_DISPLAY (f),
3045 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3046 f->output_data.x->normal_gc,
3047 x, from_y,
3048 width, height,
3049 x, to_y);
3051 UNBLOCK_INPUT;
3056 /***********************************************************************
3057 Exposure Events
3058 ***********************************************************************/
3061 static void
3062 frame_highlight (f)
3063 struct frame *f;
3065 /* XXX hack: make sure rif is right. */
3066 rif = f->display_method->rif;
3068 /* We used to only do this if Vx_no_window_manager was non-nil, but
3069 the ICCCM (section 4.1.6) says that the window's border pixmap
3070 and border pixel are window attributes which are "private to the
3071 client", so we can always change it to whatever we want. */
3072 BLOCK_INPUT;
3073 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3074 f->output_data.x->border_pixel);
3075 UNBLOCK_INPUT;
3076 x_update_cursor (f, 1);
3079 static void
3080 frame_unhighlight (f)
3081 struct frame *f;
3083 /* XXX hack: make sure rif is right. */
3084 rif = f->display_method->rif;
3086 /* We used to only do this if Vx_no_window_manager was non-nil, but
3087 the ICCCM (section 4.1.6) says that the window's border pixmap
3088 and border pixel are window attributes which are "private to the
3089 client", so we can always change it to whatever we want. */
3090 BLOCK_INPUT;
3091 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3092 f->output_data.x->border_tile);
3093 UNBLOCK_INPUT;
3094 x_update_cursor (f, 1);
3097 /* The focus has changed. Update the frames as necessary to reflect
3098 the new situation. Note that we can't change the selected frame
3099 here, because the Lisp code we are interrupting might become confused.
3100 Each event gets marked with the frame in which it occurred, so the
3101 Lisp code can tell when the switch took place by examining the events. */
3103 static void
3104 x_new_focus_frame (dpyinfo, frame)
3105 struct x_display_info *dpyinfo;
3106 struct frame *frame;
3108 struct frame *old_focus = dpyinfo->x_focus_frame;
3110 if (frame != dpyinfo->x_focus_frame)
3112 /* Set this before calling other routines, so that they see
3113 the correct value of x_focus_frame. */
3114 dpyinfo->x_focus_frame = frame;
3116 if (old_focus && old_focus->auto_lower)
3117 x_lower_frame (old_focus);
3119 #if 0
3120 selected_frame = frame;
3121 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3122 selected_frame);
3123 Fselect_window (selected_frame->selected_window, Qnil);
3124 choose_minibuf_frame ();
3125 #endif /* ! 0 */
3127 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3128 pending_autoraise_frame = dpyinfo->x_focus_frame;
3129 else
3130 pending_autoraise_frame = 0;
3133 x_frame_rehighlight (dpyinfo);
3136 /* Handle FocusIn and FocusOut state changes for FRAME.
3137 If FRAME has focus and there exists more than one frame, puts
3138 a FOCUS_IN_EVENT into BUFP.
3139 Returns number of events inserted into BUFP. */
3141 static int
3142 x_focus_changed (type, state, dpyinfo, frame, bufp, numchars)
3143 int type;
3144 int state;
3145 struct x_display_info *dpyinfo;
3146 struct frame *frame;
3147 struct input_event *bufp;
3148 int numchars;
3150 int nr_events = 0;
3152 if (type == FocusIn)
3154 if (dpyinfo->x_focus_event_frame != frame)
3156 x_new_focus_frame (dpyinfo, frame);
3157 dpyinfo->x_focus_event_frame = frame;
3159 /* Don't stop displaying the initial startup message
3160 for a switch-frame event we don't need. */
3161 if (numchars > 0
3162 && GC_NILP (Vterminal_frame)
3163 && GC_CONSP (Vframe_list)
3164 && !GC_NILP (XCDR (Vframe_list)))
3166 bufp->kind = FOCUS_IN_EVENT;
3167 XSETFRAME (bufp->frame_or_window, frame);
3168 bufp->arg = Qnil;
3169 ++bufp;
3170 numchars--;
3171 ++nr_events;
3175 frame->output_data.x->focus_state |= state;
3177 #ifdef HAVE_X_I18N
3178 if (FRAME_XIC (frame))
3179 XSetICFocus (FRAME_XIC (frame));
3180 #endif
3182 else if (type == FocusOut)
3184 frame->output_data.x->focus_state &= ~state;
3186 if (dpyinfo->x_focus_event_frame == frame)
3188 dpyinfo->x_focus_event_frame = 0;
3189 x_new_focus_frame (dpyinfo, 0);
3192 #ifdef HAVE_X_I18N
3193 if (FRAME_XIC (frame))
3194 XUnsetICFocus (FRAME_XIC (frame));
3195 #endif
3198 return nr_events;
3201 /* The focus may have changed. Figure out if it is a real focus change,
3202 by checking both FocusIn/Out and Enter/LeaveNotify events.
3204 Returns number of events inserted into BUFP. */
3206 static int
3207 x_detect_focus_change (dpyinfo, event, bufp, numchars)
3208 struct x_display_info *dpyinfo;
3209 XEvent *event;
3210 struct input_event *bufp;
3211 int numchars;
3213 struct frame *frame;
3214 int nr_events = 0;
3216 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3217 if (! frame) return nr_events;
3219 switch (event->type)
3221 case EnterNotify:
3222 case LeaveNotify:
3224 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3225 int focus_state
3226 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3228 if (event->xcrossing.detail != NotifyInferior
3229 && event->xcrossing.focus
3230 && ! (focus_state & FOCUS_EXPLICIT))
3231 nr_events = x_focus_changed ((event->type == EnterNotify
3232 ? FocusIn : FocusOut),
3233 FOCUS_IMPLICIT,
3234 dpyinfo,
3235 frame,
3236 bufp,
3237 numchars);
3239 break;
3241 case FocusIn:
3242 case FocusOut:
3243 nr_events = x_focus_changed (event->type,
3244 (event->xfocus.detail == NotifyPointer
3245 ? FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3246 dpyinfo,
3247 frame,
3248 bufp,
3249 numchars);
3250 break;
3253 return nr_events;
3257 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3259 void
3260 x_mouse_leave (dpyinfo)
3261 struct x_display_info *dpyinfo;
3263 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3266 /* The focus has changed, or we have redirected a frame's focus to
3267 another frame (this happens when a frame uses a surrogate
3268 mini-buffer frame). Shift the highlight as appropriate.
3270 The FRAME argument doesn't necessarily have anything to do with which
3271 frame is being highlighted or un-highlighted; we only use it to find
3272 the appropriate X display info. */
3274 static void
3275 XTframe_rehighlight (frame)
3276 struct frame *frame;
3278 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3281 static void
3282 x_frame_rehighlight (dpyinfo)
3283 struct x_display_info *dpyinfo;
3285 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3287 if (dpyinfo->x_focus_frame)
3289 dpyinfo->x_highlight_frame
3290 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3291 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3292 : dpyinfo->x_focus_frame);
3293 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3295 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3296 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3299 else
3300 dpyinfo->x_highlight_frame = 0;
3302 if (dpyinfo->x_highlight_frame != old_highlight)
3304 if (old_highlight)
3305 frame_unhighlight (old_highlight);
3306 if (dpyinfo->x_highlight_frame)
3307 frame_highlight (dpyinfo->x_highlight_frame);
3313 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3315 /* Initialize mode_switch_bit and modifier_meaning. */
3316 static void
3317 x_find_modifier_meanings (dpyinfo)
3318 struct x_display_info *dpyinfo;
3320 int min_code, max_code;
3321 KeySym *syms;
3322 int syms_per_code;
3323 XModifierKeymap *mods;
3325 dpyinfo->meta_mod_mask = 0;
3326 dpyinfo->shift_lock_mask = 0;
3327 dpyinfo->alt_mod_mask = 0;
3328 dpyinfo->super_mod_mask = 0;
3329 dpyinfo->hyper_mod_mask = 0;
3331 #ifdef HAVE_X11R4
3332 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3333 #else
3334 min_code = dpyinfo->display->min_keycode;
3335 max_code = dpyinfo->display->max_keycode;
3336 #endif
3338 syms = XGetKeyboardMapping (dpyinfo->display,
3339 min_code, max_code - min_code + 1,
3340 &syms_per_code);
3341 mods = XGetModifierMapping (dpyinfo->display);
3343 /* Scan the modifier table to see which modifier bits the Meta and
3344 Alt keysyms are on. */
3346 int row, col; /* The row and column in the modifier table. */
3348 for (row = 3; row < 8; row++)
3349 for (col = 0; col < mods->max_keypermod; col++)
3351 KeyCode code
3352 = mods->modifiermap[(row * mods->max_keypermod) + col];
3354 /* Zeroes are used for filler. Skip them. */
3355 if (code == 0)
3356 continue;
3358 /* Are any of this keycode's keysyms a meta key? */
3360 int code_col;
3362 for (code_col = 0; code_col < syms_per_code; code_col++)
3364 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3366 switch (sym)
3368 case XK_Meta_L:
3369 case XK_Meta_R:
3370 dpyinfo->meta_mod_mask |= (1 << row);
3371 break;
3373 case XK_Alt_L:
3374 case XK_Alt_R:
3375 dpyinfo->alt_mod_mask |= (1 << row);
3376 break;
3378 case XK_Hyper_L:
3379 case XK_Hyper_R:
3380 dpyinfo->hyper_mod_mask |= (1 << row);
3381 break;
3383 case XK_Super_L:
3384 case XK_Super_R:
3385 dpyinfo->super_mod_mask |= (1 << row);
3386 break;
3388 case XK_Shift_Lock:
3389 /* Ignore this if it's not on the lock modifier. */
3390 if ((1 << row) == LockMask)
3391 dpyinfo->shift_lock_mask = LockMask;
3392 break;
3399 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3400 if (! dpyinfo->meta_mod_mask)
3402 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3403 dpyinfo->alt_mod_mask = 0;
3406 /* If some keys are both alt and meta,
3407 make them just meta, not alt. */
3408 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3410 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3413 XFree ((char *) syms);
3414 XFreeModifiermap (mods);
3417 /* Convert between the modifier bits X uses and the modifier bits
3418 Emacs uses. */
3420 static unsigned int
3421 x_x_to_emacs_modifiers (dpyinfo, state)
3422 struct x_display_info *dpyinfo;
3423 unsigned int state;
3425 EMACS_UINT mod_meta = meta_modifier;
3426 EMACS_UINT mod_alt = alt_modifier;
3427 EMACS_UINT mod_hyper = hyper_modifier;
3428 EMACS_UINT mod_super = super_modifier;
3429 Lisp_Object tem;
3431 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3432 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3433 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3434 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3435 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3436 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3437 tem = Fget (Vx_super_keysym, Qmodifier_value);
3438 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3441 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3442 | ((state & ControlMask) ? ctrl_modifier : 0)
3443 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3444 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3445 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3446 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3449 static unsigned int
3450 x_emacs_to_x_modifiers (dpyinfo, state)
3451 struct x_display_info *dpyinfo;
3452 unsigned int state;
3454 EMACS_UINT mod_meta = meta_modifier;
3455 EMACS_UINT mod_alt = alt_modifier;
3456 EMACS_UINT mod_hyper = hyper_modifier;
3457 EMACS_UINT mod_super = super_modifier;
3459 Lisp_Object tem;
3461 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3462 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3463 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3464 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3465 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3466 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3467 tem = Fget (Vx_super_keysym, Qmodifier_value);
3468 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3471 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3472 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3473 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3474 | ((state & shift_modifier) ? ShiftMask : 0)
3475 | ((state & ctrl_modifier) ? ControlMask : 0)
3476 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3479 /* Convert a keysym to its name. */
3481 char *
3482 x_get_keysym_name (keysym)
3483 KeySym keysym;
3485 char *value;
3487 BLOCK_INPUT;
3488 value = XKeysymToString (keysym);
3489 UNBLOCK_INPUT;
3491 return value;
3496 /* Mouse clicks and mouse movement. Rah. */
3498 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3500 If the event is a button press, then note that we have grabbed
3501 the mouse. */
3503 static Lisp_Object
3504 construct_mouse_click (result, event, f)
3505 struct input_event *result;
3506 XButtonEvent *event;
3507 struct frame *f;
3509 /* Make the event type NO_EVENT; we'll change that when we decide
3510 otherwise. */
3511 result->kind = MOUSE_CLICK_EVENT;
3512 result->code = event->button - Button1;
3513 result->timestamp = event->time;
3514 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3515 event->state)
3516 | (event->type == ButtonRelease
3517 ? up_modifier
3518 : down_modifier));
3520 XSETINT (result->x, event->x);
3521 XSETINT (result->y, event->y);
3522 XSETFRAME (result->frame_or_window, f);
3523 result->arg = Qnil;
3524 return Qnil;
3528 /* Function to report a mouse movement to the mainstream Emacs code.
3529 The input handler calls this.
3531 We have received a mouse movement event, which is given in *event.
3532 If the mouse is over a different glyph than it was last time, tell
3533 the mainstream emacs code by setting mouse_moved. If not, ask for
3534 another motion event, so we can check again the next time it moves. */
3536 static XMotionEvent last_mouse_motion_event;
3537 static Lisp_Object last_mouse_motion_frame;
3539 static void
3540 note_mouse_movement (frame, event)
3541 FRAME_PTR frame;
3542 XMotionEvent *event;
3544 last_mouse_movement_time = event->time;
3545 last_mouse_motion_event = *event;
3546 XSETFRAME (last_mouse_motion_frame, frame);
3548 if (event->window != FRAME_X_WINDOW (frame))
3550 frame->mouse_moved = 1;
3551 last_mouse_scroll_bar = Qnil;
3552 note_mouse_highlight (frame, -1, -1);
3555 /* Has the mouse moved off the glyph it was on at the last sighting? */
3556 else if (event->x < last_mouse_glyph.x
3557 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3558 || event->y < last_mouse_glyph.y
3559 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3561 frame->mouse_moved = 1;
3562 last_mouse_scroll_bar = Qnil;
3563 note_mouse_highlight (frame, event->x, event->y);
3568 /************************************************************************
3569 Mouse Face
3570 ************************************************************************/
3572 static void
3573 redo_mouse_highlight ()
3575 if (!NILP (last_mouse_motion_frame)
3576 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3577 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3578 last_mouse_motion_event.x,
3579 last_mouse_motion_event.y);
3583 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
3586 /* Try to determine frame pixel position and size of the glyph under
3587 frame pixel coordinates X/Y on frame F . Return the position and
3588 size in *RECT. Value is non-zero if we could compute these
3589 values. */
3591 static int
3592 glyph_rect (f, x, y, rect)
3593 struct frame *f;
3594 int x, y;
3595 XRectangle *rect;
3597 Lisp_Object window;
3598 int found = 0;
3600 window = window_from_coordinates (f, x, y, 0, &x, &y, 0);
3601 if (!NILP (window))
3603 struct window *w = XWINDOW (window);
3604 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
3605 struct glyph_row *end = r + w->current_matrix->nrows - 1;
3607 for (; !found && r < end && r->enabled_p; ++r)
3608 if (r->y >= y)
3610 struct glyph *g = r->glyphs[TEXT_AREA];
3611 struct glyph *end = g + r->used[TEXT_AREA];
3612 int gx;
3614 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
3615 if (gx >= x)
3617 rect->width = g->pixel_width;
3618 rect->height = r->height;
3619 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
3620 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
3621 found = 1;
3626 return found;
3630 /* Return the current position of the mouse.
3631 *FP should be a frame which indicates which display to ask about.
3633 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3634 and *PART to the frame, window, and scroll bar part that the mouse
3635 is over. Set *X and *Y to the portion and whole of the mouse's
3636 position on the scroll bar.
3638 If the mouse movement started elsewhere, set *FP to the frame the
3639 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3640 the mouse is over.
3642 Set *TIME to the server time-stamp for the time at which the mouse
3643 was at this position.
3645 Don't store anything if we don't have a valid set of values to report.
3647 This clears the mouse_moved flag, so we can wait for the next mouse
3648 movement. */
3650 static void
3651 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3652 FRAME_PTR *fp;
3653 int insist;
3654 Lisp_Object *bar_window;
3655 enum scroll_bar_part *part;
3656 Lisp_Object *x, *y;
3657 unsigned long *time;
3659 FRAME_PTR f1;
3661 BLOCK_INPUT;
3663 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3664 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3665 else
3667 Window root;
3668 int root_x, root_y;
3670 Window dummy_window;
3671 int dummy;
3673 Lisp_Object frame, tail;
3675 /* Clear the mouse-moved flag for every frame on this display. */
3676 FOR_EACH_FRAME (tail, frame)
3677 if (FRAME_X_P (XFRAME (frame))
3678 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3679 XFRAME (frame)->mouse_moved = 0;
3681 last_mouse_scroll_bar = Qnil;
3683 /* Figure out which root window we're on. */
3684 XQueryPointer (FRAME_X_DISPLAY (*fp),
3685 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3687 /* The root window which contains the pointer. */
3688 &root,
3690 /* Trash which we can't trust if the pointer is on
3691 a different screen. */
3692 &dummy_window,
3694 /* The position on that root window. */
3695 &root_x, &root_y,
3697 /* More trash we can't trust. */
3698 &dummy, &dummy,
3700 /* Modifier keys and pointer buttons, about which
3701 we don't care. */
3702 (unsigned int *) &dummy);
3704 /* Now we have a position on the root; find the innermost window
3705 containing the pointer. */
3707 Window win, child;
3708 int win_x, win_y;
3709 int parent_x = 0, parent_y = 0;
3710 int count;
3712 win = root;
3714 /* XTranslateCoordinates can get errors if the window
3715 structure is changing at the same time this function
3716 is running. So at least we must not crash from them. */
3718 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
3720 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3721 && FRAME_LIVE_P (last_mouse_frame))
3723 /* If mouse was grabbed on a frame, give coords for that frame
3724 even if the mouse is now outside it. */
3725 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3727 /* From-window, to-window. */
3728 root, FRAME_X_WINDOW (last_mouse_frame),
3730 /* From-position, to-position. */
3731 root_x, root_y, &win_x, &win_y,
3733 /* Child of win. */
3734 &child);
3735 f1 = last_mouse_frame;
3737 else
3739 while (1)
3741 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3743 /* From-window, to-window. */
3744 root, win,
3746 /* From-position, to-position. */
3747 root_x, root_y, &win_x, &win_y,
3749 /* Child of win. */
3750 &child);
3752 if (child == None || child == win)
3753 break;
3755 win = child;
3756 parent_x = win_x;
3757 parent_y = win_y;
3760 /* Now we know that:
3761 win is the innermost window containing the pointer
3762 (XTC says it has no child containing the pointer),
3763 win_x and win_y are the pointer's position in it
3764 (XTC did this the last time through), and
3765 parent_x and parent_y are the pointer's position in win's parent.
3766 (They are what win_x and win_y were when win was child.
3767 If win is the root window, it has no parent, and
3768 parent_{x,y} are invalid, but that's okay, because we'll
3769 never use them in that case.) */
3771 /* Is win one of our frames? */
3772 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3774 #ifdef USE_X_TOOLKIT
3775 /* If we end up with the menu bar window, say it's not
3776 on the frame. */
3777 if (f1 != NULL
3778 && f1->output_data.x->menubar_widget
3779 && win == XtWindow (f1->output_data.x->menubar_widget))
3780 f1 = NULL;
3781 #endif /* USE_X_TOOLKIT */
3784 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3785 f1 = 0;
3787 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
3789 /* If not, is it one of our scroll bars? */
3790 if (! f1)
3792 struct scroll_bar *bar;
3794 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3796 if (bar)
3798 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3799 win_x = parent_x;
3800 win_y = parent_y;
3804 if (f1 == 0 && insist > 0)
3805 f1 = SELECTED_FRAME ();
3807 if (f1)
3809 /* Ok, we found a frame. Store all the values.
3810 last_mouse_glyph is a rectangle used to reduce the
3811 generation of mouse events. To not miss any motion
3812 events, we must divide the frame into rectangles of the
3813 size of the smallest character that could be displayed
3814 on it, i.e. into the same rectangles that matrices on
3815 the frame are divided into. */
3817 int width, height, gx, gy;
3818 XRectangle rect;
3820 if (glyph_rect (f1, win_x, win_y, &rect))
3821 last_mouse_glyph = rect;
3822 else
3824 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
3825 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
3826 gx = win_x;
3827 gy = win_y;
3829 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
3830 round down even for negative values. */
3831 if (gx < 0)
3832 gx -= width - 1;
3833 if (gy < 0)
3834 gy -= height - 1;
3835 gx = (gx + width - 1) / width * width;
3836 gy = (gy + height - 1) / height * height;
3838 last_mouse_glyph.width = width;
3839 last_mouse_glyph.height = height;
3840 last_mouse_glyph.x = gx;
3841 last_mouse_glyph.y = gy;
3844 *bar_window = Qnil;
3845 *part = 0;
3846 *fp = f1;
3847 XSETINT (*x, win_x);
3848 XSETINT (*y, win_y);
3849 *time = last_mouse_movement_time;
3854 UNBLOCK_INPUT;
3859 /***********************************************************************
3860 Scroll bars
3861 ***********************************************************************/
3863 /* Scroll bar support. */
3865 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
3866 manages it.
3867 This can be called in GC, so we have to make sure to strip off mark
3868 bits. */
3870 static struct scroll_bar *
3871 x_window_to_scroll_bar (display, window_id)
3872 Display *display;
3873 Window window_id;
3875 Lisp_Object tail;
3877 #ifdef USE_GTK
3878 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
3879 #endif /* USE_GTK */
3881 for (tail = Vframe_list;
3882 XGCTYPE (tail) == Lisp_Cons;
3883 tail = XCDR (tail))
3885 Lisp_Object frame, bar, condemned;
3887 frame = XCAR (tail);
3888 /* All elements of Vframe_list should be frames. */
3889 if (! GC_FRAMEP (frame))
3890 abort ();
3892 if (! FRAME_X_P (XFRAME (frame)))
3893 continue;
3895 /* Scan this frame's scroll bar list for a scroll bar with the
3896 right window ID. */
3897 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3898 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3899 /* This trick allows us to search both the ordinary and
3900 condemned scroll bar lists with one loop. */
3901 ! GC_NILP (bar) || (bar = condemned,
3902 condemned = Qnil,
3903 ! GC_NILP (bar));
3904 bar = XSCROLL_BAR (bar)->next)
3905 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id &&
3906 FRAME_X_DISPLAY (XFRAME (frame)) == display)
3907 return XSCROLL_BAR (bar);
3910 return 0;
3914 #if defined USE_LUCID
3916 /* Return the Lucid menu bar WINDOW is part of. Return null
3917 if WINDOW is not part of a menu bar. */
3919 static Widget
3920 x_window_to_menu_bar (window)
3921 Window window;
3923 Lisp_Object tail;
3925 for (tail = Vframe_list;
3926 XGCTYPE (tail) == Lisp_Cons;
3927 tail = XCDR (tail))
3929 if (FRAME_X_P (XFRAME (XCAR (tail))))
3931 Lisp_Object frame = XCAR (tail);
3932 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
3934 if (menu_bar && xlwmenu_window_p (menu_bar, window))
3935 return menu_bar;
3939 return NULL;
3942 #endif /* USE_LUCID */
3945 /************************************************************************
3946 Toolkit scroll bars
3947 ************************************************************************/
3949 #ifdef USE_TOOLKIT_SCROLL_BARS
3951 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
3952 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
3953 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
3954 struct scroll_bar *));
3955 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
3956 int, int, int));
3959 /* Lisp window being scrolled. Set when starting to interact with
3960 a toolkit scroll bar, reset to nil when ending the interaction. */
3962 static Lisp_Object window_being_scrolled;
3964 /* Last scroll bar part sent in xm_scroll_callback. */
3966 static int last_scroll_bar_part;
3968 /* Whether this is an Xaw with arrow-scrollbars. This should imply
3969 that movements of 1/20 of the screen size are mapped to up/down. */
3971 #ifndef USE_GTK
3972 /* Id of action hook installed for scroll bars. */
3974 static XtActionHookId action_hook_id;
3976 static Boolean xaw3d_arrow_scroll;
3978 /* Whether the drag scrolling maintains the mouse at the top of the
3979 thumb. If not, resizing the thumb needs to be done more carefully
3980 to avoid jerkyness. */
3982 static Boolean xaw3d_pick_top;
3984 extern void set_vertical_scroll_bar P_ ((struct window *));
3986 /* Action hook installed via XtAppAddActionHook when toolkit scroll
3987 bars are used.. The hook is responsible for detecting when
3988 the user ends an interaction with the scroll bar, and generates
3989 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
3991 static void
3992 xt_action_hook (widget, client_data, action_name, event, params,
3993 num_params)
3994 Widget widget;
3995 XtPointer client_data;
3996 String action_name;
3997 XEvent *event;
3998 String *params;
3999 Cardinal *num_params;
4001 int scroll_bar_p;
4002 char *end_action;
4004 #ifdef USE_MOTIF
4005 scroll_bar_p = XmIsScrollBar (widget);
4006 end_action = "Release";
4007 #else /* !USE_MOTIF i.e. use Xaw */
4008 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4009 end_action = "EndScroll";
4010 #endif /* USE_MOTIF */
4012 if (scroll_bar_p
4013 && strcmp (action_name, end_action) == 0
4014 && WINDOWP (window_being_scrolled))
4016 struct window *w;
4018 x_send_scroll_bar_event (window_being_scrolled,
4019 scroll_bar_end_scroll, 0, 0);
4020 w = XWINDOW (window_being_scrolled);
4022 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4024 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4025 /* The thumb size is incorrect while dragging: fix it. */
4026 set_vertical_scroll_bar (w);
4028 window_being_scrolled = Qnil;
4029 last_scroll_bar_part = -1;
4031 /* Xt timeouts no longer needed. */
4032 toolkit_scroll_bar_interaction = 0;
4035 #endif /* not USE_GTK */
4037 /* A vector of windows used for communication between
4038 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4040 static struct window **scroll_bar_windows;
4041 static int scroll_bar_windows_size;
4044 /* Send a client message with message type Xatom_Scrollbar for a
4045 scroll action to the frame of WINDOW. PART is a value identifying
4046 the part of the scroll bar that was clicked on. PORTION is the
4047 amount to scroll of a whole of WHOLE. */
4049 static void
4050 x_send_scroll_bar_event (window, part, portion, whole)
4051 Lisp_Object window;
4052 int part, portion, whole;
4054 XEvent event;
4055 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4056 struct window *w = XWINDOW (window);
4057 struct frame *f = XFRAME (w->frame);
4058 int i;
4060 BLOCK_INPUT;
4062 /* Construct a ClientMessage event to send to the frame. */
4063 ev->type = ClientMessage;
4064 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4065 ev->display = FRAME_X_DISPLAY (f);
4066 ev->window = FRAME_X_WINDOW (f);
4067 ev->format = 32;
4069 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4070 not enough to store a pointer or Lisp_Object on a 64 bit system.
4071 So, store the window in scroll_bar_windows and pass the index
4072 into that array in the event. */
4073 for (i = 0; i < scroll_bar_windows_size; ++i)
4074 if (scroll_bar_windows[i] == NULL)
4075 break;
4077 if (i == scroll_bar_windows_size)
4079 int new_size = max (10, 2 * scroll_bar_windows_size);
4080 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4081 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4083 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4084 nbytes);
4085 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4086 scroll_bar_windows_size = new_size;
4089 scroll_bar_windows[i] = w;
4090 ev->data.l[0] = (long) i;
4091 ev->data.l[1] = (long) part;
4092 ev->data.l[2] = (long) 0;
4093 ev->data.l[3] = (long) portion;
4094 ev->data.l[4] = (long) whole;
4096 /* Make Xt timeouts work while the scroll bar is active. */
4097 toolkit_scroll_bar_interaction = 1;
4099 /* Setting the event mask to zero means that the message will
4100 be sent to the client that created the window, and if that
4101 window no longer exists, no event will be sent. */
4102 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4103 UNBLOCK_INPUT;
4107 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4108 in *IEVENT. */
4110 static void
4111 x_scroll_bar_to_input_event (event, ievent)
4112 XEvent *event;
4113 struct input_event *ievent;
4115 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4116 Lisp_Object window;
4117 struct frame *f;
4118 struct window *w;
4120 w = scroll_bar_windows[ev->data.l[0]];
4121 scroll_bar_windows[ev->data.l[0]] = NULL;
4123 XSETWINDOW (window, w);
4124 f = XFRAME (w->frame);
4126 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4127 ievent->frame_or_window = window;
4128 ievent->arg = Qnil;
4129 #ifdef USE_GTK
4130 ievent->timestamp = CurrentTime;
4131 #else
4132 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4133 #endif
4134 ievent->part = ev->data.l[1];
4135 ievent->code = ev->data.l[2];
4136 ievent->x = make_number ((int) ev->data.l[3]);
4137 ievent->y = make_number ((int) ev->data.l[4]);
4138 ievent->modifiers = 0;
4142 #ifdef USE_MOTIF
4144 /* Minimum and maximum values used for Motif scroll bars. */
4146 #define XM_SB_MAX 10000000
4149 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4150 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4151 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4153 static void
4154 xm_scroll_callback (widget, client_data, call_data)
4155 Widget widget;
4156 XtPointer client_data, call_data;
4158 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4159 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4160 int part = -1, whole = 0, portion = 0;
4162 switch (cs->reason)
4164 case XmCR_DECREMENT:
4165 bar->dragging = Qnil;
4166 part = scroll_bar_up_arrow;
4167 break;
4169 case XmCR_INCREMENT:
4170 bar->dragging = Qnil;
4171 part = scroll_bar_down_arrow;
4172 break;
4174 case XmCR_PAGE_DECREMENT:
4175 bar->dragging = Qnil;
4176 part = scroll_bar_above_handle;
4177 break;
4179 case XmCR_PAGE_INCREMENT:
4180 bar->dragging = Qnil;
4181 part = scroll_bar_below_handle;
4182 break;
4184 case XmCR_TO_TOP:
4185 bar->dragging = Qnil;
4186 part = scroll_bar_to_top;
4187 break;
4189 case XmCR_TO_BOTTOM:
4190 bar->dragging = Qnil;
4191 part = scroll_bar_to_bottom;
4192 break;
4194 case XmCR_DRAG:
4196 int slider_size;
4198 /* Get the slider size. */
4199 BLOCK_INPUT;
4200 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4201 UNBLOCK_INPUT;
4203 whole = XM_SB_MAX - slider_size;
4204 portion = min (cs->value, whole);
4205 part = scroll_bar_handle;
4206 bar->dragging = make_number (cs->value);
4208 break;
4210 case XmCR_VALUE_CHANGED:
4211 break;
4214 if (part >= 0)
4216 window_being_scrolled = bar->window;
4217 last_scroll_bar_part = part;
4218 x_send_scroll_bar_event (bar->window, part, portion, whole);
4223 #else /* !USE_MOTIF, i.e. Xaw or GTK */
4224 #ifdef USE_GTK
4225 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4226 bar widget. DATA is a pointer to the scroll_bar structure. */
4228 static void
4229 xg_scroll_callback (widget, data)
4230 GtkRange *widget;
4231 gpointer data;
4233 struct scroll_bar *bar = (struct scroll_bar *) data;
4234 gdouble previous;
4235 gdouble position;
4236 gdouble *p;
4237 int diff;
4239 int part = -1, whole = 0, portion = 0;
4240 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4242 if (xg_ignore_gtk_scrollbar) return;
4244 position = gtk_adjustment_get_value (adj);
4246 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4247 if (! p)
4249 p = (gdouble*) xmalloc (sizeof (gdouble));
4250 *p = XG_SB_MIN;
4251 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4254 previous = *p;
4255 *p = position;
4257 diff = (int) (position - previous);
4259 if (diff == (int) adj->step_increment)
4261 part = scroll_bar_down_arrow;
4262 bar->dragging = Qnil;
4264 else if (-diff == (int) adj->step_increment)
4266 part = scroll_bar_up_arrow;
4267 bar->dragging = Qnil;
4269 else if (diff == (int) adj->page_increment)
4271 part = scroll_bar_below_handle;
4272 bar->dragging = Qnil;
4274 else if (-diff == (int) adj->page_increment)
4276 part = scroll_bar_above_handle;
4277 bar->dragging = Qnil;
4279 else
4281 part = scroll_bar_handle;
4282 whole = adj->upper - adj->page_size;
4283 portion = min ((int)position, whole);
4284 bar->dragging = make_number ((int)portion);
4287 if (part >= 0)
4289 window_being_scrolled = bar->window;
4290 last_scroll_bar_part = part;
4291 x_send_scroll_bar_event (bar->window, part, portion, whole);
4295 #else /* not USE_GTK */
4297 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4298 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4299 scroll bar struct. CALL_DATA is a pointer to a float saying where
4300 the thumb is. */
4302 static void
4303 xaw_jump_callback (widget, client_data, call_data)
4304 Widget widget;
4305 XtPointer client_data, call_data;
4307 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4308 float top = *(float *) call_data;
4309 float shown;
4310 int whole, portion, height;
4311 int part;
4313 /* Get the size of the thumb, a value between 0 and 1. */
4314 BLOCK_INPUT;
4315 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4316 UNBLOCK_INPUT;
4318 whole = 10000000;
4319 portion = shown < 1 ? top * whole : 0;
4321 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
4322 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4323 the bottom, so we force the scrolling whenever we see that we're
4324 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4325 we try to ensure that we always stay two pixels away from the
4326 bottom). */
4327 part = scroll_bar_down_arrow;
4328 else
4329 part = scroll_bar_handle;
4331 window_being_scrolled = bar->window;
4332 bar->dragging = make_number (portion);
4333 last_scroll_bar_part = part;
4334 x_send_scroll_bar_event (bar->window, part, portion, whole);
4338 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4339 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4340 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4341 the scroll bar. CALL_DATA is an integer specifying the action that
4342 has taken place. Its magnitude is in the range 0..height of the
4343 scroll bar. Negative values mean scroll towards buffer start.
4344 Values < height of scroll bar mean line-wise movement. */
4346 static void
4347 xaw_scroll_callback (widget, client_data, call_data)
4348 Widget widget;
4349 XtPointer client_data, call_data;
4351 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4352 /* The position really is stored cast to a pointer. */
4353 int position = (long) call_data;
4354 Dimension height;
4355 int part;
4357 /* Get the height of the scroll bar. */
4358 BLOCK_INPUT;
4359 XtVaGetValues (widget, XtNheight, &height, NULL);
4360 UNBLOCK_INPUT;
4362 if (abs (position) >= height)
4363 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4365 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4366 it maps line-movement to call_data = max(5, height/20). */
4367 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
4368 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4369 else
4370 part = scroll_bar_move_ratio;
4372 window_being_scrolled = bar->window;
4373 bar->dragging = Qnil;
4374 last_scroll_bar_part = part;
4375 x_send_scroll_bar_event (bar->window, part, position, height);
4378 #endif /* not USE_GTK */
4379 #endif /* not USE_MOTIF */
4381 #define SCROLL_BAR_NAME "verticalScrollBar"
4383 /* Create the widget for scroll bar BAR on frame F. Record the widget
4384 and X window of the scroll bar in BAR. */
4386 #ifdef USE_GTK
4387 static void
4388 x_create_toolkit_scroll_bar (f, bar)
4389 struct frame *f;
4390 struct scroll_bar *bar;
4392 char *scroll_bar_name = SCROLL_BAR_NAME;
4394 BLOCK_INPUT;
4395 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4396 scroll_bar_name);
4397 UNBLOCK_INPUT;
4400 #else /* not USE_GTK */
4402 static void
4403 x_create_toolkit_scroll_bar (f, bar)
4404 struct frame *f;
4405 struct scroll_bar *bar;
4407 Window xwindow;
4408 Widget widget;
4409 Arg av[20];
4410 int ac = 0;
4411 char *scroll_bar_name = SCROLL_BAR_NAME;
4412 unsigned long pixel;
4414 BLOCK_INPUT;
4416 #ifdef USE_MOTIF
4417 /* Set resources. Create the widget. */
4418 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4419 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4420 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4421 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4422 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4423 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4424 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4426 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4427 if (pixel != -1)
4429 XtSetArg (av[ac], XmNforeground, pixel);
4430 ++ac;
4433 pixel = f->output_data.x->scroll_bar_background_pixel;
4434 if (pixel != -1)
4436 XtSetArg (av[ac], XmNbackground, pixel);
4437 ++ac;
4440 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4441 scroll_bar_name, av, ac);
4443 /* Add one callback for everything that can happen. */
4444 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4445 (XtPointer) bar);
4446 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4447 (XtPointer) bar);
4448 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4449 (XtPointer) bar);
4450 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4451 (XtPointer) bar);
4452 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4453 (XtPointer) bar);
4454 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4455 (XtPointer) bar);
4456 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4457 (XtPointer) bar);
4459 /* Realize the widget. Only after that is the X window created. */
4460 XtRealizeWidget (widget);
4462 /* Set the cursor to an arrow. I didn't find a resource to do that.
4463 And I'm wondering why it hasn't an arrow cursor by default. */
4464 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4465 f->output_data.x->nontext_cursor);
4467 #else /* !USE_MOTIF i.e. use Xaw */
4469 /* Set resources. Create the widget. The background of the
4470 Xaw3d scroll bar widget is a little bit light for my taste.
4471 We don't alter it here to let users change it according
4472 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4473 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4474 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4475 /* For smoother scrolling with Xaw3d -sm */
4476 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4478 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4479 if (pixel != -1)
4481 XtSetArg (av[ac], XtNforeground, pixel);
4482 ++ac;
4485 pixel = f->output_data.x->scroll_bar_background_pixel;
4486 if (pixel != -1)
4488 XtSetArg (av[ac], XtNbackground, pixel);
4489 ++ac;
4492 /* Top/bottom shadow colors. */
4494 /* Allocate them, if necessary. */
4495 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4497 pixel = f->output_data.x->scroll_bar_background_pixel;
4498 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4499 &pixel, 1.2, 0x8000))
4500 pixel = -1;
4501 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4503 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4505 pixel = f->output_data.x->scroll_bar_background_pixel;
4506 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4507 &pixel, 0.6, 0x4000))
4508 pixel = -1;
4509 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4512 /* Tell the toolkit about them. */
4513 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4514 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4515 /* We tried to allocate a color for the top/bottom shadow, and
4516 failed, so tell Xaw3d to use dithering instead. */
4518 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4519 ++ac;
4521 else
4522 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4523 be more consistent with other emacs 3d colors, and since Xaw3d is
4524 not good at dealing with allocation failure. */
4526 /* This tells Xaw3d to use real colors instead of dithering for
4527 the shadows. */
4528 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4529 ++ac;
4531 /* Specify the colors. */
4532 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4533 if (pixel != -1)
4535 XtSetArg (av[ac], "topShadowPixel", pixel);
4536 ++ac;
4538 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4539 if (pixel != -1)
4541 XtSetArg (av[ac], "bottomShadowPixel", pixel);
4542 ++ac;
4546 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4547 f->output_data.x->edit_widget, av, ac);
4550 char *initial = "";
4551 char *val = initial;
4552 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4553 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4554 if (val == initial)
4555 { /* ARROW_SCROLL */
4556 xaw3d_arrow_scroll = True;
4557 /* Isn't that just a personal preference ? -sm */
4558 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4562 /* Define callbacks. */
4563 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4564 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4565 (XtPointer) bar);
4567 /* Realize the widget. Only after that is the X window created. */
4568 XtRealizeWidget (widget);
4570 #endif /* !USE_MOTIF */
4572 /* Install an action hook that lets us detect when the user
4573 finishes interacting with a scroll bar. */
4574 if (action_hook_id == 0)
4575 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4577 /* Remember X window and widget in the scroll bar vector. */
4578 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4579 xwindow = XtWindow (widget);
4580 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
4582 UNBLOCK_INPUT;
4584 #endif /* not USE_GTK */
4587 /* Set the thumb size and position of scroll bar BAR. We are currently
4588 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4590 #ifdef USE_GTK
4591 static void
4592 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4593 struct scroll_bar *bar;
4594 int portion, position, whole;
4596 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4599 #else /* not USE_GTK */
4600 static void
4601 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4602 struct scroll_bar *bar;
4603 int portion, position, whole;
4605 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4606 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4607 float top, shown;
4609 BLOCK_INPUT;
4611 #ifdef USE_MOTIF
4613 /* We use an estimate of 30 chars per line rather than the real
4614 `portion' value. This has the disadvantage that the thumb size
4615 is not very representative, but it makes our life a lot easier.
4616 Otherwise, we have to constantly adjust the thumb size, which
4617 we can't always do quickly enough: while dragging, the size of
4618 the thumb might prevent the user from dragging the thumb all the
4619 way to the end. but Motif and some versions of Xaw3d don't allow
4620 updating the thumb size while dragging. Also, even if we can update
4621 its size, the update will often happen too late.
4622 If you don't believe it, check out revision 1.650 of xterm.c to see
4623 what hoops we were going through and the still poor behavior we got. */
4624 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4625 /* When the thumb is at the bottom, position == whole.
4626 So we need to increase `whole' to make space for the thumb. */
4627 whole += portion;
4629 if (whole <= 0)
4630 top = 0, shown = 1;
4631 else
4633 top = (float) position / whole;
4634 shown = (float) portion / whole;
4637 if (NILP (bar->dragging))
4639 int size, value;
4641 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4642 is the scroll bar's maximum and MIN is the scroll bar's minimum
4643 value. */
4644 size = shown * XM_SB_MAX;
4645 size = min (size, XM_SB_MAX);
4646 size = max (size, 1);
4648 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4649 value = top * XM_SB_MAX;
4650 value = min (value, XM_SB_MAX - size);
4652 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4654 #else /* !USE_MOTIF i.e. use Xaw */
4656 if (whole == 0)
4657 top = 0, shown = 1;
4658 else
4660 top = (float) position / whole;
4661 shown = (float) portion / whole;
4665 float old_top, old_shown;
4666 Dimension height;
4667 XtVaGetValues (widget,
4668 XtNtopOfThumb, &old_top,
4669 XtNshown, &old_shown,
4670 XtNheight, &height,
4671 NULL);
4673 /* Massage the top+shown values. */
4674 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4675 top = max (0, min (1, top));
4676 else
4677 top = old_top;
4678 /* Keep two pixels available for moving the thumb down. */
4679 shown = max (0, min (1 - top - (2.0 / height), shown));
4681 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4682 check that your system's configuration file contains a define
4683 for `NARROWPROTO'. See s/freebsd.h for an example. */
4684 if (top != old_top || shown != old_shown)
4686 if (NILP (bar->dragging))
4687 XawScrollbarSetThumb (widget, top, shown);
4688 else
4690 #ifdef HAVE_XAW3D
4691 ScrollbarWidget sb = (ScrollbarWidget) widget;
4692 int scroll_mode = 0;
4694 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
4695 if (xaw3d_arrow_scroll)
4697 /* Xaw3d stupidly ignores resize requests while dragging
4698 so we have to make it believe it's not in dragging mode. */
4699 scroll_mode = sb->scrollbar.scroll_mode;
4700 if (scroll_mode == 2)
4701 sb->scrollbar.scroll_mode = 0;
4703 #endif
4704 /* Try to make the scrolling a tad smoother. */
4705 if (!xaw3d_pick_top)
4706 shown = min (shown, old_shown);
4708 XawScrollbarSetThumb (widget, top, shown);
4710 #ifdef HAVE_XAW3D
4711 if (xaw3d_arrow_scroll && scroll_mode == 2)
4712 sb->scrollbar.scroll_mode = scroll_mode;
4713 #endif
4717 #endif /* !USE_MOTIF */
4719 UNBLOCK_INPUT;
4721 #endif /* not USE_GTK */
4723 #endif /* USE_TOOLKIT_SCROLL_BARS */
4727 /************************************************************************
4728 Scroll bars, general
4729 ************************************************************************/
4731 /* Create a scroll bar and return the scroll bar vector for it. W is
4732 the Emacs window on which to create the scroll bar. TOP, LEFT,
4733 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4734 scroll bar. */
4736 static struct scroll_bar *
4737 x_scroll_bar_create (w, top, left, width, height)
4738 struct window *w;
4739 int top, left, width, height;
4741 struct frame *f = XFRAME (w->frame);
4742 struct scroll_bar *bar
4743 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
4745 BLOCK_INPUT;
4747 #ifdef USE_TOOLKIT_SCROLL_BARS
4748 x_create_toolkit_scroll_bar (f, bar);
4749 #else /* not USE_TOOLKIT_SCROLL_BARS */
4751 XSetWindowAttributes a;
4752 unsigned long mask;
4753 Window window;
4755 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4756 if (a.background_pixel == -1)
4757 a.background_pixel = f->output_data.x->background_pixel;
4759 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4760 | ButtonMotionMask | PointerMotionHintMask
4761 | ExposureMask);
4762 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4764 mask = (CWBackPixel | CWEventMask | CWCursor);
4766 /* Clear the area of W that will serve as a scroll bar. This is
4767 for the case that a window has been split horizontally. In
4768 this case, no clear_frame is generated to reduce flickering. */
4769 if (width > 0 && height > 0)
4770 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4771 left, top, width,
4772 window_box_height (w), False);
4774 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4775 /* Position and size of scroll bar. */
4776 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4777 top,
4778 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4779 height,
4780 /* Border width, depth, class, and visual. */
4782 CopyFromParent,
4783 CopyFromParent,
4784 CopyFromParent,
4785 /* Attributes. */
4786 mask, &a);
4787 SET_SCROLL_BAR_X_WINDOW (bar, window);
4789 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4791 XSETWINDOW (bar->window, w);
4792 XSETINT (bar->top, top);
4793 XSETINT (bar->left, left);
4794 XSETINT (bar->width, width);
4795 XSETINT (bar->height, height);
4796 XSETINT (bar->start, 0);
4797 XSETINT (bar->end, 0);
4798 bar->dragging = Qnil;
4800 /* Add bar to its frame's list of scroll bars. */
4801 bar->next = FRAME_SCROLL_BARS (f);
4802 bar->prev = Qnil;
4803 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4804 if (!NILP (bar->next))
4805 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4807 /* Map the window/widget. */
4808 #ifdef USE_TOOLKIT_SCROLL_BARS
4810 #ifdef USE_GTK
4811 xg_update_scrollbar_pos (f,
4812 SCROLL_BAR_X_WINDOW (bar),
4813 top,
4814 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4815 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4816 max (height, 1),
4817 left,
4818 width);
4819 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
4820 #else /* not USE_GTK */
4821 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4822 XtConfigureWidget (scroll_bar,
4823 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4824 top,
4825 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4826 max (height, 1), 0);
4827 XtMapWidget (scroll_bar);
4828 #endif /* not USE_GTK */
4830 #else /* not USE_TOOLKIT_SCROLL_BARS */
4831 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4832 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4834 UNBLOCK_INPUT;
4835 return bar;
4839 /* Draw BAR's handle in the proper position.
4841 If the handle is already drawn from START to END, don't bother
4842 redrawing it, unless REBUILD is non-zero; in that case, always
4843 redraw it. (REBUILD is handy for drawing the handle after expose
4844 events.)
4846 Normally, we want to constrain the start and end of the handle to
4847 fit inside its rectangle, but if the user is dragging the scroll
4848 bar handle, we want to let them drag it down all the way, so that
4849 the bar's top is as far down as it goes; otherwise, there's no way
4850 to move to the very end of the buffer. */
4852 #ifndef USE_TOOLKIT_SCROLL_BARS
4854 static void
4855 x_scroll_bar_set_handle (bar, start, end, rebuild)
4856 struct scroll_bar *bar;
4857 int start, end;
4858 int rebuild;
4860 int dragging = ! NILP (bar->dragging);
4861 Window w = SCROLL_BAR_X_WINDOW (bar);
4862 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4863 GC gc = f->output_data.x->normal_gc;
4865 /* If the display is already accurate, do nothing. */
4866 if (! rebuild
4867 && start == XINT (bar->start)
4868 && end == XINT (bar->end))
4869 return;
4871 BLOCK_INPUT;
4874 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
4875 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
4876 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4878 /* Make sure the values are reasonable, and try to preserve
4879 the distance between start and end. */
4881 int length = end - start;
4883 if (start < 0)
4884 start = 0;
4885 else if (start > top_range)
4886 start = top_range;
4887 end = start + length;
4889 if (end < start)
4890 end = start;
4891 else if (end > top_range && ! dragging)
4892 end = top_range;
4895 /* Store the adjusted setting in the scroll bar. */
4896 XSETINT (bar->start, start);
4897 XSETINT (bar->end, end);
4899 /* Clip the end position, just for display. */
4900 if (end > top_range)
4901 end = top_range;
4903 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
4904 below top positions, to make sure the handle is always at least
4905 that many pixels tall. */
4906 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
4908 /* Draw the empty space above the handle. Note that we can't clear
4909 zero-height areas; that means "clear to end of window." */
4910 if (0 < start)
4911 x_clear_area (FRAME_X_DISPLAY (f), w,
4912 /* x, y, width, height, and exposures. */
4913 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4914 VERTICAL_SCROLL_BAR_TOP_BORDER,
4915 inside_width, start,
4916 False);
4918 /* Change to proper foreground color if one is specified. */
4919 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4920 XSetForeground (FRAME_X_DISPLAY (f), gc,
4921 f->output_data.x->scroll_bar_foreground_pixel);
4923 /* Draw the handle itself. */
4924 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
4925 /* x, y, width, height */
4926 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4927 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
4928 inside_width, end - start);
4930 /* Restore the foreground color of the GC if we changed it above. */
4931 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4932 XSetForeground (FRAME_X_DISPLAY (f), gc,
4933 f->output_data.x->foreground_pixel);
4935 /* Draw the empty space below the handle. Note that we can't
4936 clear zero-height areas; that means "clear to end of window." */
4937 if (end < inside_height)
4938 x_clear_area (FRAME_X_DISPLAY (f), w,
4939 /* x, y, width, height, and exposures. */
4940 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4941 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
4942 inside_width, inside_height - end,
4943 False);
4947 UNBLOCK_INPUT;
4950 #endif /* !USE_TOOLKIT_SCROLL_BARS */
4952 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
4953 nil. */
4955 static void
4956 x_scroll_bar_remove (bar)
4957 struct scroll_bar *bar;
4959 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4960 BLOCK_INPUT;
4962 #ifdef USE_TOOLKIT_SCROLL_BARS
4963 #ifdef USE_GTK
4964 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
4965 #else /* not USE_GTK */
4966 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
4967 #endif /* not USE_GTK */
4968 #else
4969 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4970 #endif
4972 /* Disassociate this scroll bar from its window. */
4973 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
4975 UNBLOCK_INPUT;
4979 /* Set the handle of the vertical scroll bar for WINDOW to indicate
4980 that we are displaying PORTION characters out of a total of WHOLE
4981 characters, starting at POSITION. If WINDOW has no scroll bar,
4982 create one. */
4984 static void
4985 XTset_vertical_scroll_bar (w, portion, whole, position)
4986 struct window *w;
4987 int portion, whole, position;
4989 struct frame *f = XFRAME (w->frame);
4990 struct scroll_bar *bar;
4991 int top, height, left, sb_left, width, sb_width;
4992 int window_y, window_height;
4994 /* Get window dimensions. */
4995 window_box (w, -1, 0, &window_y, 0, &window_height);
4996 top = window_y;
4997 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
4998 height = window_height;
5000 /* Compute the left edge of the scroll bar area. */
5001 left = WINDOW_SCROLL_BAR_AREA_X (w);
5003 /* Compute the width of the scroll bar which might be less than
5004 the width of the area reserved for the scroll bar. */
5005 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5006 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5007 else
5008 sb_width = width;
5010 /* Compute the left edge of the scroll bar. */
5011 #ifdef USE_TOOLKIT_SCROLL_BARS
5012 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5013 sb_left = left + width - sb_width - (width - sb_width) / 2;
5014 else
5015 sb_left = left + (width - sb_width) / 2;
5016 #else
5017 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5018 sb_left = left + width - sb_width;
5019 else
5020 sb_left = left;
5021 #endif
5023 /* Does the scroll bar exist yet? */
5024 if (NILP (w->vertical_scroll_bar))
5026 if (width > 0 && height > 0)
5028 BLOCK_INPUT;
5029 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5030 left, top, width, height, False);
5031 UNBLOCK_INPUT;
5034 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5036 else
5038 /* It may just need to be moved and resized. */
5039 unsigned int mask = 0;
5041 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5043 BLOCK_INPUT;
5045 if (sb_left != XINT (bar->left))
5046 mask |= CWX;
5047 if (top != XINT (bar->top))
5048 mask |= CWY;
5049 if (sb_width != XINT (bar->width))
5050 mask |= CWWidth;
5051 if (height != XINT (bar->height))
5052 mask |= CWHeight;
5054 #ifdef USE_TOOLKIT_SCROLL_BARS
5056 #ifdef USE_GTK
5057 if (mask)
5058 xg_update_scrollbar_pos (f,
5059 SCROLL_BAR_X_WINDOW (bar),
5060 top,
5061 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5062 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5063 max (height, 1),
5064 left,
5065 width);
5066 #else /* not USE_GTK */
5068 /* Since toolkit scroll bars are smaller than the space reserved
5069 for them on the frame, we have to clear "under" them. */
5070 if (width > 0 && height > 0)
5071 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5072 left, top, width, height, False);
5073 /* Move/size the scroll bar widget. */
5074 if (mask)
5075 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5076 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5077 top,
5078 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5079 max (height, 1), 0);
5081 #endif /* not USE_GTK */
5082 #else /* not USE_TOOLKIT_SCROLL_BARS */
5084 /* Clear areas not covered by the scroll bar because of
5085 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5086 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5088 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5089 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5090 height, False);
5091 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5092 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5093 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5094 height, False);
5097 /* Clear areas not covered by the scroll bar because it's not as
5098 wide as the area reserved for it. This makes sure a
5099 previous mode line display is cleared after C-x 2 C-x 1, for
5100 example. */
5102 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5103 int rest = area_width - sb_width;
5104 if (rest > 0 && height > 0)
5106 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5107 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5108 left + area_width - rest, top,
5109 rest, height, False);
5110 else
5111 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5112 left, top, rest, height, False);
5116 /* Move/size the scroll bar window. */
5117 if (mask)
5119 XWindowChanges wc;
5121 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5122 wc.y = top;
5123 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5124 wc.height = height;
5125 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
5126 mask, &wc);
5129 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5131 /* Remember new settings. */
5132 XSETINT (bar->left, sb_left);
5133 XSETINT (bar->top, top);
5134 XSETINT (bar->width, sb_width);
5135 XSETINT (bar->height, height);
5137 UNBLOCK_INPUT;
5140 #ifdef USE_TOOLKIT_SCROLL_BARS
5141 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5142 #else /* not USE_TOOLKIT_SCROLL_BARS */
5143 /* Set the scroll bar's current state, unless we're currently being
5144 dragged. */
5145 if (NILP (bar->dragging))
5147 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5149 if (whole == 0)
5150 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5151 else
5153 int start = ((double) position * top_range) / whole;
5154 int end = ((double) (position + portion) * top_range) / whole;
5155 x_scroll_bar_set_handle (bar, start, end, 0);
5158 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5160 XSETVECTOR (w->vertical_scroll_bar, bar);
5164 /* The following three hooks are used when we're doing a thorough
5165 redisplay of the frame. We don't explicitly know which scroll bars
5166 are going to be deleted, because keeping track of when windows go
5167 away is a real pain - "Can you say set-window-configuration, boys
5168 and girls?" Instead, we just assert at the beginning of redisplay
5169 that *all* scroll bars are to be removed, and then save a scroll bar
5170 from the fiery pit when we actually redisplay its window. */
5172 /* Arrange for all scroll bars on FRAME to be removed at the next call
5173 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5174 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5176 static void
5177 XTcondemn_scroll_bars (frame)
5178 FRAME_PTR frame;
5180 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5181 while (! NILP (FRAME_SCROLL_BARS (frame)))
5183 Lisp_Object bar;
5184 bar = FRAME_SCROLL_BARS (frame);
5185 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5186 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5187 XSCROLL_BAR (bar)->prev = Qnil;
5188 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5189 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5190 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5195 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5196 Note that WINDOW isn't necessarily condemned at all. */
5198 static void
5199 XTredeem_scroll_bar (window)
5200 struct window *window;
5202 struct scroll_bar *bar;
5203 struct frame *f;
5205 /* We can't redeem this window's scroll bar if it doesn't have one. */
5206 if (NILP (window->vertical_scroll_bar))
5207 abort ();
5209 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5211 /* Unlink it from the condemned list. */
5212 f = XFRAME (WINDOW_FRAME (window));
5213 if (NILP (bar->prev))
5215 /* If the prev pointer is nil, it must be the first in one of
5216 the lists. */
5217 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5218 /* It's not condemned. Everything's fine. */
5219 return;
5220 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5221 window->vertical_scroll_bar))
5222 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5223 else
5224 /* If its prev pointer is nil, it must be at the front of
5225 one or the other! */
5226 abort ();
5228 else
5229 XSCROLL_BAR (bar->prev)->next = bar->next;
5231 if (! NILP (bar->next))
5232 XSCROLL_BAR (bar->next)->prev = bar->prev;
5234 bar->next = FRAME_SCROLL_BARS (f);
5235 bar->prev = Qnil;
5236 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5237 if (! NILP (bar->next))
5238 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5241 /* Remove all scroll bars on FRAME that haven't been saved since the
5242 last call to `*condemn_scroll_bars_hook'. */
5244 static void
5245 XTjudge_scroll_bars (f)
5246 FRAME_PTR f;
5248 Lisp_Object bar, next;
5250 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5252 /* Clear out the condemned list now so we won't try to process any
5253 more events on the hapless scroll bars. */
5254 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5256 for (; ! NILP (bar); bar = next)
5258 struct scroll_bar *b = XSCROLL_BAR (bar);
5260 x_scroll_bar_remove (b);
5262 next = b->next;
5263 b->next = b->prev = Qnil;
5266 /* Now there should be no references to the condemned scroll bars,
5267 and they should get garbage-collected. */
5271 #ifndef USE_TOOLKIT_SCROLL_BARS
5272 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5273 is a no-op when using toolkit scroll bars.
5275 This may be called from a signal handler, so we have to ignore GC
5276 mark bits. */
5278 static void
5279 x_scroll_bar_expose (bar, event)
5280 struct scroll_bar *bar;
5281 XEvent *event;
5283 Window w = SCROLL_BAR_X_WINDOW (bar);
5284 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5285 GC gc = f->output_data.x->normal_gc;
5286 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5288 BLOCK_INPUT;
5290 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
5292 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5293 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5295 /* x, y, width, height */
5296 0, 0,
5297 XINT (bar->width) - 1 - width_trim - width_trim,
5298 XINT (bar->height) - 1);
5300 UNBLOCK_INPUT;
5303 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5305 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5306 is set to something other than NO_EVENT, it is enqueued.
5308 This may be called from a signal handler, so we have to ignore GC
5309 mark bits. */
5312 static void
5313 x_scroll_bar_handle_click (bar, event, emacs_event)
5314 struct scroll_bar *bar;
5315 XEvent *event;
5316 struct input_event *emacs_event;
5318 if (! GC_WINDOWP (bar->window))
5319 abort ();
5321 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5322 emacs_event->code = event->xbutton.button - Button1;
5323 emacs_event->modifiers
5324 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5325 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5326 event->xbutton.state)
5327 | (event->type == ButtonRelease
5328 ? up_modifier
5329 : down_modifier));
5330 emacs_event->frame_or_window = bar->window;
5331 emacs_event->arg = Qnil;
5332 emacs_event->timestamp = event->xbutton.time;
5334 #if 0
5335 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5336 int internal_height
5337 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5338 #endif
5339 int top_range
5340 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5341 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5343 if (y < 0) y = 0;
5344 if (y > top_range) y = top_range;
5346 if (y < XINT (bar->start))
5347 emacs_event->part = scroll_bar_above_handle;
5348 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5349 emacs_event->part = scroll_bar_handle;
5350 else
5351 emacs_event->part = scroll_bar_below_handle;
5353 /* Just because the user has clicked on the handle doesn't mean
5354 they want to drag it. Lisp code needs to be able to decide
5355 whether or not we're dragging. */
5356 #if 0
5357 /* If the user has just clicked on the handle, record where they're
5358 holding it. */
5359 if (event->type == ButtonPress
5360 && emacs_event->part == scroll_bar_handle)
5361 XSETINT (bar->dragging, y - XINT (bar->start));
5362 #endif
5364 #ifndef USE_TOOLKIT_SCROLL_BARS
5365 /* If the user has released the handle, set it to its final position. */
5366 if (event->type == ButtonRelease
5367 && ! NILP (bar->dragging))
5369 int new_start = y - XINT (bar->dragging);
5370 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5372 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5373 bar->dragging = Qnil;
5375 #endif
5377 /* Same deal here as the other #if 0. */
5378 #if 0
5379 /* Clicks on the handle are always reported as occurring at the top of
5380 the handle. */
5381 if (emacs_event->part == scroll_bar_handle)
5382 emacs_event->x = bar->start;
5383 else
5384 XSETINT (emacs_event->x, y);
5385 #else
5386 XSETINT (emacs_event->x, y);
5387 #endif
5389 XSETINT (emacs_event->y, top_range);
5393 #ifndef USE_TOOLKIT_SCROLL_BARS
5395 /* Handle some mouse motion while someone is dragging the scroll bar.
5397 This may be called from a signal handler, so we have to ignore GC
5398 mark bits. */
5400 static void
5401 x_scroll_bar_note_movement (bar, event)
5402 struct scroll_bar *bar;
5403 XEvent *event;
5405 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5407 last_mouse_movement_time = event->xmotion.time;
5409 f->mouse_moved = 1;
5410 XSETVECTOR (last_mouse_scroll_bar, bar);
5412 /* If we're dragging the bar, display it. */
5413 if (! GC_NILP (bar->dragging))
5415 /* Where should the handle be now? */
5416 int new_start = event->xmotion.y - XINT (bar->dragging);
5418 if (new_start != XINT (bar->start))
5420 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5422 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5427 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5429 /* Return information to the user about the current position of the mouse
5430 on the scroll bar. */
5432 static void
5433 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5434 FRAME_PTR *fp;
5435 Lisp_Object *bar_window;
5436 enum scroll_bar_part *part;
5437 Lisp_Object *x, *y;
5438 unsigned long *time;
5440 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5441 Window w = SCROLL_BAR_X_WINDOW (bar);
5442 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5443 int win_x, win_y;
5444 Window dummy_window;
5445 int dummy_coord;
5446 unsigned int dummy_mask;
5448 BLOCK_INPUT;
5450 /* Get the mouse's position relative to the scroll bar window, and
5451 report that. */
5452 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5454 /* Root, child, root x and root y. */
5455 &dummy_window, &dummy_window,
5456 &dummy_coord, &dummy_coord,
5458 /* Position relative to scroll bar. */
5459 &win_x, &win_y,
5461 /* Mouse buttons and modifier keys. */
5462 &dummy_mask))
5464 else
5466 #if 0
5467 int inside_height
5468 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5469 #endif
5470 int top_range
5471 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5473 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5475 if (! NILP (bar->dragging))
5476 win_y -= XINT (bar->dragging);
5478 if (win_y < 0)
5479 win_y = 0;
5480 if (win_y > top_range)
5481 win_y = top_range;
5483 *fp = f;
5484 *bar_window = bar->window;
5486 if (! NILP (bar->dragging))
5487 *part = scroll_bar_handle;
5488 else if (win_y < XINT (bar->start))
5489 *part = scroll_bar_above_handle;
5490 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5491 *part = scroll_bar_handle;
5492 else
5493 *part = scroll_bar_below_handle;
5495 XSETINT (*x, win_y);
5496 XSETINT (*y, top_range);
5498 f->mouse_moved = 0;
5499 last_mouse_scroll_bar = Qnil;
5502 *time = last_mouse_movement_time;
5504 UNBLOCK_INPUT;
5508 /* The screen has been cleared so we may have changed foreground or
5509 background colors, and the scroll bars may need to be redrawn.
5510 Clear out the scroll bars, and ask for expose events, so we can
5511 redraw them. */
5513 void
5514 x_scroll_bar_clear (f)
5515 FRAME_PTR f;
5517 #ifndef USE_TOOLKIT_SCROLL_BARS
5518 Lisp_Object bar;
5520 /* We can have scroll bars even if this is 0,
5521 if we just turned off scroll bar mode.
5522 But in that case we should not clear them. */
5523 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5524 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5525 bar = XSCROLL_BAR (bar)->next)
5526 XClearArea (FRAME_X_DISPLAY (f),
5527 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
5528 0, 0, 0, 0, True);
5529 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5533 /* Define a queue to save up SelectionRequest events for later handling. */
5535 struct selection_event_queue
5537 XEvent event;
5538 struct selection_event_queue *next;
5541 static struct selection_event_queue *queue;
5543 /* Nonzero means queue up certain events--don't process them yet. */
5545 static int x_queue_selection_requests;
5547 /* Queue up an X event *EVENT, to be processed later. */
5549 static void
5550 x_queue_event (f, event)
5551 FRAME_PTR f;
5552 XEvent *event;
5554 struct selection_event_queue *queue_tmp
5555 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
5557 if (queue_tmp != NULL)
5559 queue_tmp->event = *event;
5560 queue_tmp->next = queue;
5561 queue = queue_tmp;
5565 /* Take all the queued events and put them back
5566 so that they get processed afresh. */
5568 static void
5569 x_unqueue_events (display)
5570 Display *display;
5572 while (queue != NULL)
5574 struct selection_event_queue *queue_tmp = queue;
5575 XPutBackEvent (display, &queue_tmp->event);
5576 queue = queue_tmp->next;
5577 xfree ((char *)queue_tmp);
5581 /* Start queuing SelectionRequest events. */
5583 void
5584 x_start_queuing_selection_requests (display)
5585 Display *display;
5587 x_queue_selection_requests++;
5590 /* Stop queuing SelectionRequest events. */
5592 void
5593 x_stop_queuing_selection_requests (display)
5594 Display *display;
5596 x_queue_selection_requests--;
5597 x_unqueue_events (display);
5600 /* The main X event-reading loop - XTread_socket. */
5602 #if 0
5603 /* Time stamp of enter window event. This is only used by XTread_socket,
5604 but we have to put it out here, since static variables within functions
5605 sometimes don't work. */
5607 static Time enter_timestamp;
5608 #endif
5610 /* This holds the state XLookupString needs to implement dead keys
5611 and other tricks known as "compose processing". _X Window System_
5612 says that a portable program can't use this, but Stephen Gildea assures
5613 me that letting the compiler initialize it to zeros will work okay.
5615 This must be defined outside of XTread_socket, for the same reasons
5616 given for enter_timestamp, above. */
5618 static XComposeStatus compose_status;
5620 /* Record the last 100 characters stored
5621 to help debug the loss-of-chars-during-GC problem. */
5623 static int temp_index;
5624 static short temp_buffer[100];
5626 /* Set this to nonzero to fake an "X I/O error"
5627 on a particular display. */
5629 struct x_display_info *XTread_socket_fake_io_error;
5631 /* When we find no input here, we occasionally do a no-op command
5632 to verify that the X server is still running and we can still talk with it.
5633 We try all the open displays, one by one.
5634 This variable is used for cycling thru the displays. */
5636 static struct x_display_info *next_noop_dpyinfo;
5638 #define SET_SAVED_MENU_EVENT(size) \
5639 do \
5641 if (f->output_data.x->saved_menu_event == 0) \
5642 f->output_data.x->saved_menu_event \
5643 = (XEvent *) xmalloc (sizeof (XEvent)); \
5644 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5645 if (numchars >= 1) \
5647 bufp->kind = MENU_BAR_ACTIVATE_EVENT; \
5648 XSETFRAME (bufp->frame_or_window, f); \
5649 bufp->arg = Qnil; \
5650 bufp++; \
5651 count++; \
5652 numchars--; \
5655 while (0)
5657 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5658 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5661 enum
5663 X_EVENT_NORMAL,
5664 X_EVENT_GOTO_OUT,
5665 X_EVENT_DROP
5668 /* Filter events for the current X input method.
5669 DPYINFO is the display this event is for.
5670 EVENT is the X event to filter.
5672 Returns non-zero if the event was filtered, caller shall not process
5673 this event further.
5674 Returns zero if event is wasn't filtered. */
5676 #ifdef HAVE_X_I18N
5677 static int
5678 x_filter_event (dpyinfo, event)
5679 struct x_display_info *dpyinfo;
5680 XEvent *event;
5682 /* XFilterEvent returns non-zero if the input method has
5683 consumed the event. We pass the frame's X window to
5684 XFilterEvent because that's the one for which the IC
5685 was created. */
5687 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5688 event->xclient.window);
5690 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5692 #endif
5694 #ifdef USE_GTK
5695 static struct input_event **current_bufp;
5696 static int *current_numcharsp;
5697 static int current_count;
5698 static int current_finish;
5700 /* This is the filter function invoked by the GTK event loop.
5701 It is invoked before the XEvent is translated to a GdkEvent,
5702 so we have a chanse to act on the event before GTK. */
5703 static GdkFilterReturn
5704 event_handler_gdk (gxev, ev, data)
5705 GdkXEvent *gxev;
5706 GdkEvent *ev;
5707 gpointer data;
5709 XEvent *xev = (XEvent *) gxev;
5711 if (current_numcharsp)
5713 struct x_display_info *dpyinfo;
5715 dpyinfo = x_display_info_for_display (xev->xany.display);
5717 #ifdef HAVE_X_I18N
5718 /* Filter events for the current X input method.
5719 GTK calls XFilterEvent but not for key press and release,
5720 so we do it here. */
5721 if (xev->type == KeyPress || xev->type == KeyRelease)
5722 if (dpyinfo && x_filter_event (dpyinfo, xev))
5723 return GDK_FILTER_REMOVE;
5724 #endif
5726 if (! dpyinfo)
5727 current_finish = X_EVENT_NORMAL;
5728 else
5729 current_count += handle_one_xevent (dpyinfo,
5730 xev,
5731 current_bufp,
5732 current_numcharsp,
5733 &current_finish);
5735 else
5736 current_finish = x_dispatch_event (xev, xev->xany.display);
5738 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5739 return GDK_FILTER_REMOVE;
5741 return GDK_FILTER_CONTINUE;
5743 #endif /* USE_GTK */
5746 /* Handles the XEvent EVENT on display DPYINFO.
5748 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5749 *FINISH is zero if caller should continue reading events.
5750 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5752 Events representing keys are stored in buffer *BUFP_R,
5753 which can hold up to *NUMCHARSP characters.
5754 We return the number of characters stored into the buffer. */
5756 static int
5757 handle_one_xevent (dpyinfo, eventp, bufp_r, numcharsp, finish)
5758 struct x_display_info *dpyinfo;
5759 XEvent *eventp;
5760 /* register */ struct input_event **bufp_r;
5761 /* register */ int *numcharsp;
5762 int *finish;
5764 int count = 0;
5765 int nbytes = 0;
5766 struct frame *f;
5767 struct coding_system coding;
5768 struct input_event *bufp = *bufp_r;
5769 int numchars = *numcharsp;
5770 XEvent event = *eventp;
5772 *finish = X_EVENT_NORMAL;
5774 switch (event.type)
5776 case ClientMessage:
5778 if (event.xclient.message_type
5779 == dpyinfo->Xatom_wm_protocols
5780 && event.xclient.format == 32)
5782 if (event.xclient.data.l[0]
5783 == dpyinfo->Xatom_wm_take_focus)
5785 /* Use x_any_window_to_frame because this
5786 could be the shell widget window
5787 if the frame has no title bar. */
5788 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5789 #ifdef HAVE_X_I18N
5790 /* Not quite sure this is needed -pd */
5791 if (f && FRAME_XIC (f))
5792 XSetICFocus (FRAME_XIC (f));
5793 #endif
5794 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5795 instructs the WM to set the input focus automatically for
5796 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5797 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5798 it has set the focus. So, XSetInputFocus below is not
5799 needed.
5801 The call to XSetInputFocus below has also caused trouble. In
5802 cases where the XSetInputFocus done by the WM and the one
5803 below are temporally close (on a fast machine), the call
5804 below can generate additional FocusIn events which confuse
5805 Emacs. */
5807 /* Since we set WM_TAKE_FOCUS, we must call
5808 XSetInputFocus explicitly. But not if f is null,
5809 since that might be an event for a deleted frame. */
5810 if (f)
5812 Display *d = event.xclient.display;
5813 /* Catch and ignore errors, in case window has been
5814 iconified by a window manager such as GWM. */
5815 int count = x_catch_errors (d);
5816 XSetInputFocus (d, event.xclient.window,
5817 /* The ICCCM says this is
5818 the only valid choice. */
5819 RevertToParent,
5820 event.xclient.data.l[1]);
5821 /* This is needed to detect the error
5822 if there is an error. */
5823 XSync (d, False);
5824 x_uncatch_errors (d, count);
5826 /* Not certain about handling scroll bars here */
5827 #endif /* 0 */
5829 else if (event.xclient.data.l[0]
5830 == dpyinfo->Xatom_wm_save_yourself)
5832 /* Save state modify the WM_COMMAND property to
5833 something which can reinstate us. This notifies
5834 the session manager, who's looking for such a
5835 PropertyNotify. Can restart processing when
5836 a keyboard or mouse event arrives. */
5837 /* If we have a session manager, don't set this.
5838 KDE will then start two Emacsen, one for the
5839 session manager and one for this. */
5840 if (numchars > 0
5841 #ifdef HAVE_X_SM
5842 && ! x_session_have_connection ()
5843 #endif
5846 f = x_top_window_to_frame (dpyinfo,
5847 event.xclient.window);
5848 /* This is just so we only give real data once
5849 for a single Emacs process. */
5850 if (f == SELECTED_FRAME ())
5851 XSetCommand (FRAME_X_DISPLAY (f),
5852 event.xclient.window,
5853 initial_argv, initial_argc);
5854 else if (f)
5855 XSetCommand (FRAME_X_DISPLAY (f),
5856 event.xclient.window,
5857 0, 0);
5860 else if (event.xclient.data.l[0]
5861 == dpyinfo->Xatom_wm_delete_window)
5863 struct frame *f
5864 = x_any_window_to_frame (dpyinfo,
5865 event.xclient.window);
5867 if (f)
5869 if (numchars == 0)
5870 abort ();
5872 bufp->kind = DELETE_WINDOW_EVENT;
5873 XSETFRAME (bufp->frame_or_window, f);
5874 bufp->arg = Qnil;
5875 bufp++;
5877 count += 1;
5878 numchars -= 1;
5880 else
5881 goto OTHER; /* May be a dialog that is to be removed */
5884 else if (event.xclient.message_type
5885 == dpyinfo->Xatom_wm_configure_denied)
5888 else if (event.xclient.message_type
5889 == dpyinfo->Xatom_wm_window_moved)
5891 int new_x, new_y;
5892 struct frame *f
5893 = x_window_to_frame (dpyinfo, event.xclient.window);
5895 new_x = event.xclient.data.s[0];
5896 new_y = event.xclient.data.s[1];
5898 if (f)
5900 f->left_pos = new_x;
5901 f->top_pos = new_y;
5904 #ifdef HACK_EDITRES
5905 else if (event.xclient.message_type
5906 == dpyinfo->Xatom_editres)
5908 struct frame *f
5909 = x_any_window_to_frame (dpyinfo, event.xclient.window);
5910 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5911 &event, NULL);
5913 #endif /* HACK_EDITRES */
5914 else if ((event.xclient.message_type
5915 == dpyinfo->Xatom_DONE)
5916 || (event.xclient.message_type
5917 == dpyinfo->Xatom_PAGE))
5919 /* Ghostview job completed. Kill it. We could
5920 reply with "Next" if we received "Page", but we
5921 currently never do because we are interested in
5922 images, only, which should have 1 page. */
5923 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5924 struct frame *f
5925 = x_window_to_frame (dpyinfo, event.xclient.window);
5926 x_kill_gs_process (pixmap, f);
5927 expose_frame (f, 0, 0, 0, 0);
5929 #ifdef USE_TOOLKIT_SCROLL_BARS
5930 /* Scroll bar callbacks send a ClientMessage from which
5931 we construct an input_event. */
5932 else if (event.xclient.message_type
5933 == dpyinfo->Xatom_Scrollbar)
5935 x_scroll_bar_to_input_event (&event, bufp);
5936 ++bufp, ++count, --numchars;
5937 goto out;
5939 #endif /* USE_TOOLKIT_SCROLL_BARS */
5940 else
5941 goto OTHER;
5943 break;
5945 case SelectionNotify:
5946 #ifdef USE_X_TOOLKIT
5947 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
5948 goto OTHER;
5949 #endif /* not USE_X_TOOLKIT */
5950 x_handle_selection_notify (&event.xselection);
5951 break;
5953 case SelectionClear: /* Someone has grabbed ownership. */
5954 #ifdef USE_X_TOOLKIT
5955 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
5956 goto OTHER;
5957 #endif /* USE_X_TOOLKIT */
5959 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
5961 if (numchars == 0)
5962 abort ();
5964 bufp->kind = SELECTION_CLEAR_EVENT;
5965 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
5966 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
5967 SELECTION_EVENT_TIME (bufp) = eventp->time;
5968 bufp->frame_or_window = Qnil;
5969 bufp->arg = Qnil;
5970 bufp++;
5972 count += 1;
5973 numchars -= 1;
5975 break;
5977 case SelectionRequest: /* Someone wants our selection. */
5978 #ifdef USE_X_TOOLKIT
5979 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
5980 goto OTHER;
5981 #endif /* USE_X_TOOLKIT */
5982 if (x_queue_selection_requests)
5983 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
5984 &event);
5985 else
5987 XSelectionRequestEvent *eventp
5988 = (XSelectionRequestEvent *) &event;
5990 if (numchars == 0)
5991 abort ();
5993 bufp->kind = SELECTION_REQUEST_EVENT;
5994 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
5995 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
5996 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
5997 SELECTION_EVENT_TARGET (bufp) = eventp->target;
5998 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
5999 SELECTION_EVENT_TIME (bufp) = eventp->time;
6000 bufp->frame_or_window = Qnil;
6001 bufp->arg = Qnil;
6002 bufp++;
6004 count += 1;
6005 numchars -= 1;
6007 break;
6009 case PropertyNotify:
6010 #if 0 /* This is plain wrong. In the case that we are waiting for a
6011 PropertyNotify used as an ACK in incremental selection
6012 transfer, the property will be on the receiver's window. */
6013 #if defined USE_X_TOOLKIT
6014 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
6015 goto OTHER;
6016 #endif
6017 #endif
6018 x_handle_property_notify (&event.xproperty);
6019 goto OTHER;
6021 case ReparentNotify:
6022 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6023 if (f)
6025 int x, y;
6026 f->output_data.x->parent_desc = event.xreparent.parent;
6027 x_real_positions (f, &x, &y);
6028 f->left_pos = x;
6029 f->top_pos = y;
6031 /* Perhaps reparented due to a WM restart. Reset this. */
6032 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6034 goto OTHER;
6035 break;
6037 case Expose:
6038 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6039 if (f)
6041 x_check_fullscreen (f);
6043 if (f->async_visible == 0)
6045 f->async_visible = 1;
6046 f->async_iconified = 0;
6047 f->output_data.x->has_been_visible = 1;
6048 SET_FRAME_GARBAGED (f);
6050 else
6051 expose_frame (f,
6052 event.xexpose.x, event.xexpose.y,
6053 event.xexpose.width, event.xexpose.height);
6055 else
6057 #ifndef USE_TOOLKIT_SCROLL_BARS
6058 struct scroll_bar *bar;
6059 #endif
6060 #if defined USE_LUCID
6061 /* Submenus of the Lucid menu bar aren't widgets
6062 themselves, so there's no way to dispatch events
6063 to them. Recognize this case separately. */
6065 Widget widget
6066 = x_window_to_menu_bar (event.xexpose.window);
6067 if (widget)
6068 xlwmenu_redisplay (widget);
6070 #endif /* USE_LUCID */
6072 #ifdef USE_TOOLKIT_SCROLL_BARS
6073 /* Dispatch event to the widget. */
6074 goto OTHER;
6075 #else /* not USE_TOOLKIT_SCROLL_BARS */
6076 bar = x_window_to_scroll_bar (event.xexpose.display,
6077 event.xexpose.window);
6079 if (bar)
6080 x_scroll_bar_expose (bar, &event);
6081 #ifdef USE_X_TOOLKIT
6082 else
6083 goto OTHER;
6084 #endif /* USE_X_TOOLKIT */
6085 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6087 break;
6089 case GraphicsExpose: /* This occurs when an XCopyArea's
6090 source area was obscured or not
6091 available. */
6092 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6093 if (f)
6095 expose_frame (f,
6096 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6097 event.xgraphicsexpose.width,
6098 event.xgraphicsexpose.height);
6100 #ifdef USE_X_TOOLKIT
6101 else
6102 goto OTHER;
6103 #endif /* USE_X_TOOLKIT */
6104 break;
6106 case NoExpose: /* This occurs when an XCopyArea's
6107 source area was completely
6108 available. */
6109 break;
6111 case UnmapNotify:
6112 /* Redo the mouse-highlight after the tooltip has gone. */
6113 if (event.xmap.window == tip_window)
6115 tip_window = 0;
6116 redo_mouse_highlight ();
6119 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6120 if (f) /* F may no longer exist if
6121 the frame was deleted. */
6123 /* While a frame is unmapped, display generation is
6124 disabled; you don't want to spend time updating a
6125 display that won't ever be seen. */
6126 f->async_visible = 0;
6127 /* We can't distinguish, from the event, whether the window
6128 has become iconified or invisible. So assume, if it
6129 was previously visible, than now it is iconified.
6130 But x_make_frame_invisible clears both
6131 the visible flag and the iconified flag;
6132 and that way, we know the window is not iconified now. */
6133 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6135 f->async_iconified = 1;
6137 bufp->kind = ICONIFY_EVENT;
6138 XSETFRAME (bufp->frame_or_window, f);
6139 bufp->arg = Qnil;
6140 bufp++;
6141 count++;
6142 numchars--;
6145 goto OTHER;
6147 case MapNotify:
6148 if (event.xmap.window == tip_window)
6149 /* The tooltip has been drawn already. Avoid
6150 the SET_FRAME_GARBAGED below. */
6151 goto OTHER;
6153 /* We use x_top_window_to_frame because map events can
6154 come for sub-windows and they don't mean that the
6155 frame is visible. */
6156 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6157 if (f)
6159 /* wait_reading_process_input will notice this and update
6160 the frame's display structures.
6161 If we where iconified, we should not set garbaged,
6162 because that stops redrawing on Expose events. This looks
6163 bad if we are called from a recursive event loop
6164 (x_dispatch_event), for example when a dialog is up. */
6165 if (! f->async_iconified)
6166 SET_FRAME_GARBAGED (f);
6168 f->async_visible = 1;
6169 f->async_iconified = 0;
6170 f->output_data.x->has_been_visible = 1;
6172 if (f->iconified)
6174 bufp->kind = DEICONIFY_EVENT;
6175 XSETFRAME (bufp->frame_or_window, f);
6176 bufp->arg = Qnil;
6177 bufp++;
6178 count++;
6179 numchars--;
6181 else if (! NILP (Vframe_list)
6182 && ! NILP (XCDR (Vframe_list)))
6183 /* Force a redisplay sooner or later
6184 to update the frame titles
6185 in case this is the second frame. */
6186 record_asynch_buffer_change ();
6188 goto OTHER;
6190 case KeyPress:
6192 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6193 /* Dispatch KeyPress events when in menu. */
6194 if (popup_activated ())
6195 goto OTHER;
6196 #endif
6198 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6200 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
6202 dpyinfo->mouse_face_hidden = 1;
6203 clear_mouse_face (dpyinfo);
6206 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6207 if (f == 0)
6209 /* Scroll bars consume key events, but we want
6210 the keys to go to the scroll bar's frame. */
6211 Widget widget = XtWindowToWidget (dpyinfo->display,
6212 event.xkey.window);
6213 if (widget && XmIsScrollBar (widget))
6215 widget = XtParent (widget);
6216 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6219 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6221 if (f != 0)
6223 KeySym keysym, orig_keysym;
6224 /* al%imercury@uunet.uu.net says that making this 81
6225 instead of 80 fixed a bug whereby meta chars made
6226 his Emacs hang.
6228 It seems that some version of XmbLookupString has
6229 a bug of not returning XBufferOverflow in
6230 status_return even if the input is too long to
6231 fit in 81 bytes. So, we must prepare sufficient
6232 bytes for copy_buffer. 513 bytes (256 chars for
6233 two-byte character set) seems to be a fairly good
6234 approximation. -- 2000.8.10 handa@etl.go.jp */
6235 unsigned char copy_buffer[513];
6236 unsigned char *copy_bufptr = copy_buffer;
6237 int copy_bufsiz = sizeof (copy_buffer);
6238 int modifiers;
6239 Lisp_Object coding_system = Qlatin_1;
6241 event.xkey.state
6242 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6243 extra_keyboard_modifiers);
6244 modifiers = event.xkey.state;
6246 /* This will have to go some day... */
6248 /* make_lispy_event turns chars into control chars.
6249 Don't do it here because XLookupString is too eager. */
6250 event.xkey.state &= ~ControlMask;
6251 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6252 | dpyinfo->super_mod_mask
6253 | dpyinfo->hyper_mod_mask
6254 | dpyinfo->alt_mod_mask);
6256 /* In case Meta is ComposeCharacter,
6257 clear its status. According to Markus Ehrnsperger
6258 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6259 this enables ComposeCharacter to work whether or
6260 not it is combined with Meta. */
6261 if (modifiers & dpyinfo->meta_mod_mask)
6262 bzero (&compose_status, sizeof (compose_status));
6264 #ifdef HAVE_X_I18N
6265 if (FRAME_XIC (f))
6267 Status status_return;
6269 coding_system = Vlocale_coding_system;
6270 nbytes = XmbLookupString (FRAME_XIC (f),
6271 &event.xkey, copy_bufptr,
6272 copy_bufsiz, &keysym,
6273 &status_return);
6274 if (status_return == XBufferOverflow)
6276 copy_bufsiz = nbytes + 1;
6277 copy_bufptr = (char *) alloca (copy_bufsiz);
6278 nbytes = XmbLookupString (FRAME_XIC (f),
6279 &event.xkey, copy_bufptr,
6280 copy_bufsiz, &keysym,
6281 &status_return);
6283 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6284 #if 0 && defined X_HAVE_UTF8_STRING
6285 else if (status_return == XLookupKeySym)
6286 { /* Try again but with utf-8. */
6287 coding_system = Qutf_8;
6288 nbytes = Xutf8LookupString (FRAME_XIC (f),
6289 &event.xkey, copy_bufptr,
6290 copy_bufsiz, &keysym,
6291 &status_return);
6292 if (status_return == XBufferOverflow)
6294 copy_bufsiz = nbytes + 1;
6295 copy_bufptr = (char *) alloca (copy_bufsiz);
6296 nbytes = Xutf8LookupString (FRAME_XIC (f),
6297 &event.xkey,
6298 copy_bufptr,
6299 copy_bufsiz, &keysym,
6300 &status_return);
6303 #endif
6305 if (status_return == XLookupNone)
6306 break;
6307 else if (status_return == XLookupChars)
6309 keysym = NoSymbol;
6310 modifiers = 0;
6312 else if (status_return != XLookupKeySym
6313 && status_return != XLookupBoth)
6314 abort ();
6316 else
6317 nbytes = XLookupString (&event.xkey, copy_bufptr,
6318 copy_bufsiz, &keysym,
6319 &compose_status);
6320 #else
6321 nbytes = XLookupString (&event.xkey, copy_bufptr,
6322 copy_bufsiz, &keysym,
6323 &compose_status);
6324 #endif
6326 orig_keysym = keysym;
6328 if (numchars > 1)
6330 Lisp_Object c;
6332 /* First deal with keysyms which have defined
6333 translations to characters. */
6334 if (keysym >= 32 && keysym < 128)
6335 /* Avoid explicitly decoding each ASCII character. */
6337 bufp->kind = ASCII_KEYSTROKE_EVENT;
6338 bufp->code = keysym;
6339 XSETFRAME (bufp->frame_or_window, f);
6340 bufp->arg = Qnil;
6341 bufp->modifiers
6342 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6343 modifiers);
6344 bufp->timestamp = event.xkey.time;
6345 bufp++;
6346 count++;
6347 numchars--;
6349 /* Now non-ASCII. */
6350 else if (HASH_TABLE_P (Vx_keysym_table)
6351 && (NATNUMP (c = Fgethash (make_number (keysym),
6352 Vx_keysym_table,
6353 Qnil))))
6355 bufp->kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6356 ? ASCII_KEYSTROKE_EVENT
6357 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6358 bufp->code = XFASTINT (c);
6359 XSETFRAME (bufp->frame_or_window, f);
6360 bufp->arg = Qnil;
6361 bufp->modifiers
6362 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6363 modifiers);
6364 bufp->timestamp = event.xkey.time;
6365 bufp++;
6366 count++;
6367 numchars--;
6369 /* Random non-modifier sorts of keysyms. */
6370 else if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6371 || keysym == XK_Delete
6372 #ifdef XK_ISO_Left_Tab
6373 || (keysym >= XK_ISO_Left_Tab
6374 && keysym <= XK_ISO_Enter)
6375 #endif
6376 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6377 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6378 #ifdef HPUX
6379 /* This recognizes the "extended function
6380 keys". It seems there's no cleaner way.
6381 Test IsModifierKey to avoid handling
6382 mode_switch incorrectly. */
6383 || ((unsigned) (keysym) >= XK_Select
6384 && (unsigned)(keysym) < XK_KP_Space)
6385 #endif
6386 #ifdef XK_dead_circumflex
6387 || orig_keysym == XK_dead_circumflex
6388 #endif
6389 #ifdef XK_dead_grave
6390 || orig_keysym == XK_dead_grave
6391 #endif
6392 #ifdef XK_dead_tilde
6393 || orig_keysym == XK_dead_tilde
6394 #endif
6395 #ifdef XK_dead_diaeresis
6396 || orig_keysym == XK_dead_diaeresis
6397 #endif
6398 #ifdef XK_dead_macron
6399 || orig_keysym == XK_dead_macron
6400 #endif
6401 #ifdef XK_dead_degree
6402 || orig_keysym == XK_dead_degree
6403 #endif
6404 #ifdef XK_dead_acute
6405 || orig_keysym == XK_dead_acute
6406 #endif
6407 #ifdef XK_dead_cedilla
6408 || orig_keysym == XK_dead_cedilla
6409 #endif
6410 #ifdef XK_dead_breve
6411 || orig_keysym == XK_dead_breve
6412 #endif
6413 #ifdef XK_dead_ogonek
6414 || orig_keysym == XK_dead_ogonek
6415 #endif
6416 #ifdef XK_dead_caron
6417 || orig_keysym == XK_dead_caron
6418 #endif
6419 #ifdef XK_dead_doubleacute
6420 || orig_keysym == XK_dead_doubleacute
6421 #endif
6422 #ifdef XK_dead_abovedot
6423 || orig_keysym == XK_dead_abovedot
6424 #endif
6425 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6426 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6427 /* Any "vendor-specific" key is ok. */
6428 || (orig_keysym & (1 << 28))
6429 || (keysym != NoSymbol && nbytes == 0))
6430 && ! (IsModifierKey (orig_keysym)
6431 #ifndef HAVE_X11R5
6432 #ifdef XK_Mode_switch
6433 || ((unsigned)(orig_keysym) == XK_Mode_switch)
6434 #endif
6435 #ifdef XK_Num_Lock
6436 || ((unsigned)(orig_keysym) == XK_Num_Lock)
6437 #endif
6438 #endif /* not HAVE_X11R5 */
6439 /* The symbols from XK_ISO_Lock
6440 to XK_ISO_Last_Group_Lock
6441 don't have real modifiers but
6442 should be treated similarly to
6443 Mode_switch by Emacs. */
6444 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6445 || ((unsigned)(orig_keysym)
6446 >= XK_ISO_Lock
6447 && (unsigned)(orig_keysym)
6448 <= XK_ISO_Last_Group_Lock)
6449 #endif
6452 if (temp_index == sizeof temp_buffer / sizeof (short))
6453 temp_index = 0;
6454 temp_buffer[temp_index++] = keysym;
6455 /* make_lispy_event will convert this to a symbolic
6456 key. */
6457 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
6458 bufp->code = keysym;
6459 XSETFRAME (bufp->frame_or_window, f);
6460 bufp->arg = Qnil;
6461 bufp->modifiers
6462 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6463 modifiers);
6464 bufp->timestamp = event.xkey.time;
6465 bufp++;
6466 count++;
6467 numchars--;
6469 else if (numchars > nbytes)
6470 { /* Raw bytes, not keysym. */
6471 register int i;
6472 register int c;
6473 int nchars, len;
6475 /* The input should be decoded with `coding_system'
6476 which depends on which X*LookupString function
6477 we used just above and the locale. */
6478 setup_coding_system (coding_system, &coding);
6479 coding.src_multibyte = 0;
6480 coding.dst_multibyte = 1;
6481 /* The input is converted to events, thus we can't
6482 handle composition. Anyway, there's no XIM that
6483 gives us composition information. */
6484 coding.composing = COMPOSITION_DISABLED;
6486 for (i = 0; i < nbytes; i++)
6488 if (temp_index == (sizeof temp_buffer
6489 / sizeof (short)))
6490 temp_index = 0;
6491 temp_buffer[temp_index++] = copy_bufptr[i];
6495 /* Decode the input data. */
6496 int require;
6497 unsigned char *p;
6499 require = decoding_buffer_size (&coding, nbytes);
6500 p = (unsigned char *) alloca (require);
6501 coding.mode |= CODING_MODE_LAST_BLOCK;
6502 /* We explicitly disable composition
6503 handling because key data should
6504 not contain any composition
6505 sequence. */
6506 coding.composing = COMPOSITION_DISABLED;
6507 decode_coding (&coding, copy_bufptr, p,
6508 nbytes, require);
6509 nbytes = coding.produced;
6510 nchars = coding.produced_char;
6511 copy_bufptr = p;
6514 /* Convert the input data to a sequence of
6515 character events. */
6516 for (i = 0; i < nbytes; i += len)
6518 if (nchars == nbytes)
6519 c = copy_bufptr[i], len = 1;
6520 else
6521 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6522 nbytes - i, len);
6524 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
6525 ? ASCII_KEYSTROKE_EVENT
6526 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6527 bufp->code = c;
6528 XSETFRAME (bufp->frame_or_window, f);
6529 bufp->arg = Qnil;
6530 bufp->modifiers
6531 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6532 modifiers);
6533 bufp->timestamp = event.xkey.time;
6534 bufp++;
6537 count += nchars;
6538 numchars -= nchars;
6540 if (keysym == NoSymbol)
6541 break;
6543 else
6544 abort ();
6546 else
6547 abort ();
6549 #ifdef HAVE_X_I18N
6550 /* Don't dispatch this event since XtDispatchEvent calls
6551 XFilterEvent, and two calls in a row may freeze the
6552 client. */
6553 break;
6554 #else
6555 goto OTHER;
6556 #endif
6558 case KeyRelease:
6559 #ifdef HAVE_X_I18N
6560 /* Don't dispatch this event since XtDispatchEvent calls
6561 XFilterEvent, and two calls in a row may freeze the
6562 client. */
6563 break;
6564 #else
6565 goto OTHER;
6566 #endif
6568 case EnterNotify:
6570 int n;
6572 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6573 if (n > 0)
6575 bufp += n, count += n, numchars -= n;
6578 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6580 #if 0
6581 if (event.xcrossing.focus)
6583 /* Avoid nasty pop/raise loops. */
6584 if (f && (!(f->auto_raise)
6585 || !(f->auto_lower)
6586 || (event.xcrossing.time - enter_timestamp) > 500))
6588 x_new_focus_frame (dpyinfo, f);
6589 enter_timestamp = event.xcrossing.time;
6592 else if (f == dpyinfo->x_focus_frame)
6593 x_new_focus_frame (dpyinfo, 0);
6594 #endif
6596 /* EnterNotify counts as mouse movement,
6597 so update things that depend on mouse position. */
6598 if (f && !f->output_data.x->hourglass_p)
6599 note_mouse_movement (f, &event.xmotion);
6600 goto OTHER;
6603 case FocusIn:
6605 int n;
6607 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6608 if (n > 0)
6610 bufp += n, count += n, numchars -= n;
6614 goto OTHER;
6616 case LeaveNotify:
6618 int n;
6620 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6621 if (n > 0)
6623 bufp += n, count += n, numchars -= n;
6627 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6628 if (f)
6630 if (f == dpyinfo->mouse_face_mouse_frame)
6632 /* If we move outside the frame, then we're
6633 certainly no longer on any text in the frame. */
6634 clear_mouse_face (dpyinfo);
6635 dpyinfo->mouse_face_mouse_frame = 0;
6638 /* Generate a nil HELP_EVENT to cancel a help-echo.
6639 Do it only if there's something to cancel.
6640 Otherwise, the startup message is cleared when
6641 the mouse leaves the frame. */
6642 if (any_help_event_p)
6644 Lisp_Object frame;
6645 int n;
6647 XSETFRAME (frame, f);
6648 help_echo_string = Qnil;
6649 n = gen_help_event (bufp, numchars,
6650 Qnil, frame, Qnil, Qnil, 0);
6651 bufp += n, count += n, numchars -= n;
6655 goto OTHER;
6657 case FocusOut:
6659 int n;
6661 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6662 if (n > 0)
6664 bufp += n, count += n, numchars -= n;
6668 goto OTHER;
6670 case MotionNotify:
6672 previous_help_echo_string = help_echo_string;
6673 help_echo_string = help_echo_object = help_echo_window = Qnil;
6674 help_echo_pos = -1;
6676 if (dpyinfo->grabbed && last_mouse_frame
6677 && FRAME_LIVE_P (last_mouse_frame))
6678 f = last_mouse_frame;
6679 else
6680 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6682 if (dpyinfo->mouse_face_hidden)
6684 dpyinfo->mouse_face_hidden = 0;
6685 clear_mouse_face (dpyinfo);
6688 if (f)
6691 /* Generate SELECT_WINDOW_EVENTs when needed. */
6692 if (mouse_autoselect_window)
6694 Lisp_Object window;
6696 window = window_from_coordinates (f,
6697 event.xmotion.x, event.xmotion.y,
6698 0, 0, 0, 0);
6700 /* Window will be selected only when it is not selected now and
6701 last mouse movement event was not in it. Minibuffer window
6702 will be selected iff it is active. */
6703 if (WINDOWP (window)
6704 && !EQ (window, last_window)
6705 && !EQ (window, selected_window)
6706 && numchars > 0)
6708 bufp->kind = SELECT_WINDOW_EVENT;
6709 bufp->frame_or_window = window;
6710 bufp->arg = Qnil;
6711 ++bufp, ++count, --numchars;
6714 last_window=window;
6716 note_mouse_movement (f, &event.xmotion);
6718 else
6720 #ifndef USE_TOOLKIT_SCROLL_BARS
6721 struct scroll_bar *bar
6722 = x_window_to_scroll_bar (event.xmotion.display,
6723 event.xmotion.window);
6725 if (bar)
6726 x_scroll_bar_note_movement (bar, &event);
6727 #endif /* USE_TOOLKIT_SCROLL_BARS */
6729 /* If we move outside the frame, then we're
6730 certainly no longer on any text in the frame. */
6731 clear_mouse_face (dpyinfo);
6734 /* If the contents of the global variable help_echo_string
6735 has changed, generate a HELP_EVENT. */
6736 if (!NILP (help_echo_string)
6737 || !NILP (previous_help_echo_string))
6739 Lisp_Object frame;
6740 int n;
6742 if (f)
6743 XSETFRAME (frame, f);
6744 else
6745 frame = Qnil;
6747 any_help_event_p = 1;
6748 n = gen_help_event (bufp, numchars, help_echo_string, frame,
6749 help_echo_window, help_echo_object,
6750 help_echo_pos);
6751 bufp += n, count += n, numchars -= n;
6754 goto OTHER;
6757 case ConfigureNotify:
6758 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6759 if (f)
6761 #ifndef USE_X_TOOLKIT
6762 #ifdef USE_GTK
6763 xg_resize_widgets (f, event.xconfigure.width,
6764 event.xconfigure.height);
6765 #else /* not USE_GTK */
6766 /* If there is a pending resize for fullscreen, don't
6767 do this one, the right one will come later.
6768 The toolkit version doesn't seem to need this, but we
6769 need to reset it below. */
6770 int dont_resize
6771 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6772 && f->new_text_cols != 0);
6773 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6774 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6776 if (dont_resize)
6777 goto OTHER;
6779 /* In the toolkit version, change_frame_size
6780 is called by the code that handles resizing
6781 of the EmacsFrame widget. */
6783 /* Even if the number of character rows and columns has
6784 not changed, the font size may have changed, so we need
6785 to check the pixel dimensions as well. */
6786 if (columns != FRAME_COLS (f)
6787 || rows != FRAME_LINES (f)
6788 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6789 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6791 change_frame_size (f, rows, columns, 0, 1, 0);
6792 SET_FRAME_GARBAGED (f);
6793 cancel_mouse_face (f);
6795 #endif /* not USE_GTK */
6796 #endif
6798 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6799 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6801 #ifdef USE_GTK
6802 /* GTK creates windows but doesn't map them.
6803 Only get real positions and check fullscreen when mapped. */
6804 if (FRAME_GTK_OUTER_WIDGET (f)
6805 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6806 #endif
6808 /* What we have now is the position of Emacs's own window.
6809 Convert that to the position of the window manager window. */
6810 x_real_positions (f, &f->left_pos, &f->top_pos);
6812 x_check_expected_move (f);
6813 if (f->want_fullscreen & FULLSCREEN_WAIT)
6814 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6817 #ifdef HAVE_X_I18N
6818 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6819 xic_set_statusarea (f);
6820 #endif
6822 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6824 /* Since the WM decorations come below top_pos now,
6825 we must put them below top_pos in the future. */
6826 f->win_gravity = NorthWestGravity;
6827 x_wm_set_size_hint (f, (long) 0, 0);
6830 goto OTHER;
6832 case ButtonRelease:
6833 case ButtonPress:
6835 /* If we decide we want to generate an event to be seen
6836 by the rest of Emacs, we put it here. */
6837 struct input_event emacs_event;
6838 int tool_bar_p = 0;
6840 emacs_event.kind = NO_EVENT;
6841 bzero (&compose_status, sizeof (compose_status));
6843 if (dpyinfo->grabbed
6844 && last_mouse_frame
6845 && FRAME_LIVE_P (last_mouse_frame))
6846 f = last_mouse_frame;
6847 else
6848 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6850 if (f)
6852 /* Is this in the tool-bar? */
6853 if (WINDOWP (f->tool_bar_window)
6854 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6856 Lisp_Object window;
6857 int x = event.xbutton.x;
6858 int y = event.xbutton.y;
6860 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6861 if (EQ (window, f->tool_bar_window))
6863 if (event.xbutton.type == ButtonPress)
6864 handle_tool_bar_click (f, x, y, 1, 0);
6865 else
6866 handle_tool_bar_click (f, x, y, 0,
6867 x_x_to_emacs_modifiers (dpyinfo,
6868 event.xbutton.state));
6869 tool_bar_p = 1;
6873 if (!tool_bar_p)
6874 if (!dpyinfo->x_focus_frame
6875 || f == dpyinfo->x_focus_frame)
6877 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6878 if (! popup_activated ())
6879 #endif
6880 construct_mouse_click (&emacs_event, &event, f);
6883 else
6885 struct scroll_bar *bar
6886 = x_window_to_scroll_bar (event.xbutton.display,
6887 event.xbutton.window);
6889 #ifdef USE_TOOLKIT_SCROLL_BARS
6890 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6891 scroll bars. */
6892 if (bar && event.xbutton.state & ControlMask)
6894 x_scroll_bar_handle_click (bar, &event, &emacs_event);
6895 *finish = X_EVENT_DROP;
6897 #else /* not USE_TOOLKIT_SCROLL_BARS */
6898 if (bar)
6899 x_scroll_bar_handle_click (bar, &event, &emacs_event);
6900 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6903 if (event.type == ButtonPress)
6905 dpyinfo->grabbed |= (1 << event.xbutton.button);
6906 last_mouse_frame = f;
6907 /* Ignore any mouse motion that happened
6908 before this event; any subsequent mouse-movement
6909 Emacs events should reflect only motion after
6910 the ButtonPress. */
6911 if (f != 0)
6912 f->mouse_moved = 0;
6914 if (!tool_bar_p)
6915 last_tool_bar_item = -1;
6917 else
6918 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6920 if (numchars >= 1 && emacs_event.kind != NO_EVENT)
6922 bcopy (&emacs_event, bufp, sizeof (struct input_event));
6923 bufp++;
6924 count++;
6925 numchars--;
6928 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6929 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6930 /* For a down-event in the menu bar,
6931 don't pass it to Xt right now.
6932 Instead, save it away
6933 and we will pass it to Xt from kbd_buffer_get_event.
6934 That way, we can run some Lisp code first. */
6935 if (
6936 #ifdef USE_GTK
6937 ! popup_activated ()
6939 #endif
6940 f && event.type == ButtonPress
6941 /* Verify the event is really within the menu bar
6942 and not just sent to it due to grabbing. */
6943 && event.xbutton.x >= 0
6944 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6945 && event.xbutton.y >= 0
6946 && event.xbutton.y < f->output_data.x->menubar_height
6947 && event.xbutton.same_screen)
6949 SET_SAVED_BUTTON_EVENT;
6950 XSETFRAME (last_mouse_press_frame, f);
6951 #ifdef USE_GTK
6952 *finish = X_EVENT_DROP;
6953 #endif
6955 else if (event.type == ButtonPress)
6957 last_mouse_press_frame = Qnil;
6958 goto OTHER;
6961 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6962 but I am trying to be cautious. */
6963 else if (event.type == ButtonRelease)
6965 if (!NILP (last_mouse_press_frame))
6967 f = XFRAME (last_mouse_press_frame);
6968 if (f->output_data.x)
6969 SET_SAVED_BUTTON_EVENT;
6971 else
6972 goto OTHER;
6974 #endif /* USE_MOTIF */
6975 else
6976 goto OTHER;
6977 #endif /* USE_X_TOOLKIT || USE_GTK */
6979 break;
6981 case CirculateNotify:
6982 goto OTHER;
6984 case CirculateRequest:
6985 goto OTHER;
6987 case VisibilityNotify:
6988 goto OTHER;
6990 case MappingNotify:
6991 /* Someone has changed the keyboard mapping - update the
6992 local cache. */
6993 switch (event.xmapping.request)
6995 case MappingModifier:
6996 x_find_modifier_meanings (dpyinfo);
6997 /* This is meant to fall through. */
6998 case MappingKeyboard:
6999 XRefreshKeyboardMapping (&event.xmapping);
7001 goto OTHER;
7003 default:
7004 OTHER:
7005 #ifdef USE_X_TOOLKIT
7006 BLOCK_INPUT;
7007 if (*finish != X_EVENT_DROP)
7008 XtDispatchEvent (&event);
7009 UNBLOCK_INPUT;
7010 #endif /* USE_X_TOOLKIT */
7011 break;
7014 goto ret;
7016 out:
7017 *finish = X_EVENT_GOTO_OUT;
7019 ret:
7020 *bufp_r = bufp;
7021 *numcharsp = numchars;
7022 *eventp = event;
7024 return count;
7028 /* Handles the XEvent EVENT on display DISPLAY.
7029 This is used for event loops outside the normal event handling,
7030 i.e. looping while a popup menu or a dialog is posted.
7032 Returns the value handle_one_xevent sets in the finish argument. */
7034 x_dispatch_event (event, display)
7035 XEvent *event;
7036 Display *display;
7038 struct x_display_info *dpyinfo;
7039 struct input_event bufp[10];
7040 struct input_event *bufpp;
7041 int numchars = 10;
7042 int finish = X_EVENT_NORMAL;
7044 for (bufpp = bufp; bufpp != bufp + 10; bufpp++)
7045 EVENT_INIT (*bufpp);
7046 bufpp = bufp;
7048 dpyinfo = x_display_info_for_display (display);
7050 if (dpyinfo)
7052 int i, events;
7053 events = handle_one_xevent (dpyinfo,
7054 event,
7055 &bufpp,
7056 &numchars,
7057 &finish);
7058 for (i = 0; i < events; ++i)
7059 kbd_buffer_store_event (&bufp[i]);
7062 return finish;
7066 /* Read events coming from the X server.
7067 This routine is called by the SIGIO handler.
7068 We return as soon as there are no more events to be read.
7070 Events representing keys are stored in buffer BUFP,
7071 which can hold up to NUMCHARS characters.
7072 We return the number of characters stored into the buffer,
7073 thus pretending to be `read'.
7075 EXPECTED is nonzero if the caller knows input is available. */
7077 static int
7078 XTread_socket (bufp, numchars, expected)
7079 /* register */ struct input_event *bufp;
7080 /* register */ int numchars;
7081 int expected;
7083 int count = 0;
7084 XEvent event;
7085 int event_found = 0;
7086 struct x_display_info *dpyinfo;
7088 if (interrupt_input_blocked)
7090 interrupt_input_pending = 1;
7091 return -1;
7094 interrupt_input_pending = 0;
7095 BLOCK_INPUT;
7097 /* So people can tell when we have read the available input. */
7098 input_signal_count++;
7100 if (numchars <= 0)
7101 abort (); /* Don't think this happens. */
7103 ++handling_signal;
7105 /* Find the display we are supposed to read input for.
7106 It's the one communicating on descriptor SD. */
7107 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7109 #if 0 /* This ought to be unnecessary; let's verify it. */
7110 #ifdef FIOSNBIO
7111 /* If available, Xlib uses FIOSNBIO to make the socket
7112 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7113 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7114 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7115 fcntl (dpyinfo->connection, F_SETFL, 0);
7116 #endif /* ! defined (FIOSNBIO) */
7117 #endif
7119 #if 0 /* This code can't be made to work, with multiple displays,
7120 and appears not to be used on any system any more.
7121 Also keyboard.c doesn't turn O_NDELAY on and off
7122 for X connections. */
7123 #ifndef SIGIO
7124 #ifndef HAVE_SELECT
7125 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7127 extern int read_alarm_should_throw;
7128 read_alarm_should_throw = 1;
7129 XPeekEvent (dpyinfo->display, &event);
7130 read_alarm_should_throw = 0;
7132 #endif /* HAVE_SELECT */
7133 #endif /* SIGIO */
7134 #endif
7136 /* For debugging, this gives a way to fake an I/O error. */
7137 if (dpyinfo == XTread_socket_fake_io_error)
7139 XTread_socket_fake_io_error = 0;
7140 x_io_error_quitter (dpyinfo->display);
7143 #ifdef HAVE_X_SM
7144 BLOCK_INPUT;
7145 count += x_session_check_input (bufp, &numchars);
7146 UNBLOCK_INPUT;
7147 #endif
7149 #ifndef USE_GTK
7150 while (XPending (dpyinfo->display))
7152 int finish;
7154 XNextEvent (dpyinfo->display, &event);
7156 #ifdef HAVE_X_I18N
7157 /* Filter events for the current X input method. */
7158 if (x_filter_event (dpyinfo, &event))
7159 break;
7160 #endif
7161 event_found = 1;
7163 count += handle_one_xevent (dpyinfo,
7164 &event,
7165 &bufp,
7166 &numchars,
7167 &finish);
7169 if (finish == X_EVENT_GOTO_OUT)
7170 goto out;
7172 #endif /* not USE_GTK */
7175 #ifdef USE_GTK
7177 /* For GTK we must use the GTK event loop. But XEvents gets passed
7178 to our filter function above, and then to the big event switch.
7179 We use a bunch of globals to communicate with our filter function,
7180 that is kind of ugly, but it works.
7182 There is no way to do one display at the time, GTK just does events
7183 from all displays. */
7185 while (gtk_events_pending ())
7187 current_count = count;
7188 current_numcharsp = &numchars;
7189 current_bufp = &bufp;
7191 gtk_main_iteration ();
7193 count = current_count;
7194 current_bufp = 0;
7195 current_numcharsp = 0;
7197 if (current_finish == X_EVENT_GOTO_OUT)
7198 break;
7200 #endif /* USE_GTK */
7202 out:;
7204 /* On some systems, an X bug causes Emacs to get no more events
7205 when the window is destroyed. Detect that. (1994.) */
7206 if (! event_found)
7208 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7209 One XNOOP in 100 loops will make Emacs terminate.
7210 B. Bretthauer, 1994 */
7211 x_noop_count++;
7212 if (x_noop_count >= 100)
7214 x_noop_count=0;
7216 if (next_noop_dpyinfo == 0)
7217 next_noop_dpyinfo = x_display_list;
7219 XNoOp (next_noop_dpyinfo->display);
7221 /* Each time we get here, cycle through the displays now open. */
7222 next_noop_dpyinfo = next_noop_dpyinfo->next;
7226 /* If the focus was just given to an auto-raising frame,
7227 raise it now. */
7228 /* ??? This ought to be able to handle more than one such frame. */
7229 if (pending_autoraise_frame)
7231 x_raise_frame (pending_autoraise_frame);
7232 pending_autoraise_frame = 0;
7235 UNBLOCK_INPUT;
7236 --handling_signal;
7237 return count;
7243 /***********************************************************************
7244 Text Cursor
7245 ***********************************************************************/
7247 /* Set clipping for output in glyph row ROW. W is the window in which
7248 we operate. GC is the graphics context to set clipping in.
7250 ROW may be a text row or, e.g., a mode line. Text rows must be
7251 clipped to the interior of the window dedicated to text display,
7252 mode lines must be clipped to the whole window. */
7254 static void
7255 x_clip_to_row (w, row, gc)
7256 struct window *w;
7257 struct glyph_row *row;
7258 GC gc;
7260 struct frame *f = XFRAME (WINDOW_FRAME (w));
7261 XRectangle clip_rect;
7262 int window_y, window_width;
7264 window_box (w, -1, 0, &window_y, &window_width, 0);
7266 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
7267 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
7268 clip_rect.y = max (clip_rect.y, window_y);
7269 clip_rect.width = window_width;
7270 clip_rect.height = row->visible_height;
7272 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7276 /* Draw a hollow box cursor on window W in glyph row ROW. */
7278 static void
7279 x_draw_hollow_cursor (w, row)
7280 struct window *w;
7281 struct glyph_row *row;
7283 struct frame *f = XFRAME (WINDOW_FRAME (w));
7284 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7285 Display *dpy = FRAME_X_DISPLAY (f);
7286 int x, y, wd, h;
7287 XGCValues xgcv;
7288 struct glyph *cursor_glyph;
7289 GC gc;
7291 /* Get the glyph the cursor is on. If we can't tell because
7292 the current matrix is invalid or such, give up. */
7293 cursor_glyph = get_phys_cursor_glyph (w);
7294 if (cursor_glyph == NULL)
7295 return;
7297 /* Compute the width of the rectangle to draw. If on a stretch
7298 glyph, and `x-stretch-block-cursor' is nil, don't draw a
7299 rectangle as wide as the glyph, but use a canonical character
7300 width instead. */
7301 wd = cursor_glyph->pixel_width - 1;
7302 if (cursor_glyph->type == STRETCH_GLYPH
7303 && !x_stretch_cursor_p)
7304 wd = min (FRAME_COLUMN_WIDTH (f), wd);
7305 w->phys_cursor_width = wd;
7307 /* Compute frame-relative coordinates from window-relative
7308 coordinates. */
7309 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7310 y = WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y);
7312 /* Compute the proper height and ascent of the rectangle, based
7313 on the actual glyph. Using the full height of the row looks
7314 bad when there are tall images on that row. */
7315 h = max (FRAME_LINE_HEIGHT (f), cursor_glyph->ascent + cursor_glyph->descent);
7316 if (h < row->height)
7317 y += row->ascent /* - w->phys_cursor_ascent */ + cursor_glyph->descent - h;
7318 h--;
7320 /* The foreground of cursor_gc is typically the same as the normal
7321 background color, which can cause the cursor box to be invisible. */
7322 xgcv.foreground = f->output_data.x->cursor_pixel;
7323 if (dpyinfo->scratch_cursor_gc)
7324 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7325 else
7326 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7327 GCForeground, &xgcv);
7328 gc = dpyinfo->scratch_cursor_gc;
7330 /* Set clipping, draw the rectangle, and reset clipping again. */
7331 x_clip_to_row (w, row, gc);
7332 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
7333 XSetClipMask (dpy, gc, None);
7337 /* Draw a bar cursor on window W in glyph row ROW.
7339 Implementation note: One would like to draw a bar cursor with an
7340 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7341 Unfortunately, I didn't find a font yet that has this property set.
7342 --gerd. */
7344 static void
7345 x_draw_bar_cursor (w, row, width, kind)
7346 struct window *w;
7347 struct glyph_row *row;
7348 int width;
7349 enum text_cursor_kinds kind;
7351 struct frame *f = XFRAME (w->frame);
7352 struct glyph *cursor_glyph;
7354 /* If cursor is out of bounds, don't draw garbage. This can happen
7355 in mini-buffer windows when switching between echo area glyphs
7356 and mini-buffer. */
7357 cursor_glyph = get_phys_cursor_glyph (w);
7358 if (cursor_glyph == NULL)
7359 return;
7361 /* If on an image, draw like a normal cursor. That's usually better
7362 visible than drawing a bar, esp. if the image is large so that
7363 the bar might not be in the window. */
7364 if (cursor_glyph->type == IMAGE_GLYPH)
7366 struct glyph_row *row;
7367 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7368 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7370 else
7372 Display *dpy = FRAME_X_DISPLAY (f);
7373 Window window = FRAME_X_WINDOW (f);
7374 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7375 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7376 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7377 XGCValues xgcv;
7379 /* If the glyph's background equals the color we normally draw
7380 the bar cursor in, the bar cursor in its normal color is
7381 invisible. Use the glyph's foreground color instead in this
7382 case, on the assumption that the glyph's colors are chosen so
7383 that the glyph is legible. */
7384 if (face->background == f->output_data.x->cursor_pixel)
7385 xgcv.background = xgcv.foreground = face->foreground;
7386 else
7387 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7388 xgcv.graphics_exposures = 0;
7390 if (gc)
7391 XChangeGC (dpy, gc, mask, &xgcv);
7392 else
7394 gc = XCreateGC (dpy, window, mask, &xgcv);
7395 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7398 if (width < 0)
7399 width = FRAME_CURSOR_WIDTH (f);
7400 width = min (cursor_glyph->pixel_width, width);
7402 w->phys_cursor_width = width;
7403 x_clip_to_row (w, row, gc);
7405 if (kind == BAR_CURSOR)
7406 XFillRectangle (dpy, window, gc,
7407 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7408 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7409 width, row->height);
7410 else
7411 XFillRectangle (dpy, window, gc,
7412 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7413 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7414 row->height - width),
7415 cursor_glyph->pixel_width,
7416 width);
7418 XSetClipMask (dpy, gc, None);
7423 /* RIF: Define cursor CURSOR on frame F. */
7425 static void
7426 x_define_frame_cursor (f, cursor)
7427 struct frame *f;
7428 Cursor cursor;
7430 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7434 /* RIF: Clear area on frame F. */
7436 static void
7437 x_clear_frame_area (f, x, y, width, height)
7438 struct frame *f;
7439 int x, y, width, height;
7441 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7442 x, y, width, height, False);
7446 /* RIF: Draw cursor on window W. */
7448 static void
7449 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7450 struct window *w;
7451 struct glyph_row *glyph_row;
7452 int x, y;
7453 int cursor_type, cursor_width;
7454 int on_p, active_p;
7456 struct frame *f = XFRAME (WINDOW_FRAME (w));
7458 if (on_p)
7460 w->phys_cursor_type = cursor_type;
7461 w->phys_cursor_on_p = 1;
7463 switch (cursor_type)
7465 case HOLLOW_BOX_CURSOR:
7466 x_draw_hollow_cursor (w, glyph_row);
7467 break;
7469 case FILLED_BOX_CURSOR:
7470 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7471 break;
7473 case BAR_CURSOR:
7474 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7475 break;
7477 case HBAR_CURSOR:
7478 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7479 break;
7481 case NO_CURSOR:
7482 w->phys_cursor_width = 0;
7483 break;
7485 default:
7486 abort ();
7489 #ifdef HAVE_X_I18N
7490 if (w == XWINDOW (f->selected_window))
7491 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7492 xic_set_preeditarea (w, x, y);
7493 #endif
7496 #ifndef XFlush
7497 if (updating_frame != f)
7498 XFlush (FRAME_X_DISPLAY (f));
7499 #endif
7503 /* Icons. */
7505 /* Make the x-window of frame F use the gnu icon bitmap. */
7508 x_bitmap_icon (f, file)
7509 struct frame *f;
7510 Lisp_Object file;
7512 int bitmap_id;
7514 if (FRAME_X_WINDOW (f) == 0)
7515 return 1;
7517 /* Free up our existing icon bitmap and mask if any. */
7518 if (f->output_data.x->icon_bitmap > 0)
7519 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7520 f->output_data.x->icon_bitmap = 0;
7522 if (STRINGP (file))
7524 #ifdef USE_GTK
7525 /* Use gtk_window_set_icon_from_file () if available,
7526 It's not restricted to bitmaps */
7527 if (xg_set_icon (f, file))
7528 return 0;
7529 #endif /* USE_GTK */
7530 bitmap_id = x_create_bitmap_from_file (f, file);
7531 x_create_bitmap_mask (f, bitmap_id);
7533 else
7535 /* Create the GNU bitmap and mask if necessary. */
7536 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7538 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
7539 = x_create_bitmap_from_data (f, gnu_bits,
7540 gnu_width, gnu_height);
7541 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7544 /* The first time we create the GNU bitmap and mask,
7545 this increments the ref-count one extra time.
7546 As a result, the GNU bitmap and mask are never freed.
7547 That way, we don't have to worry about allocating it again. */
7548 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7550 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7553 x_wm_set_icon_pixmap (f, bitmap_id);
7554 f->output_data.x->icon_bitmap = bitmap_id;
7556 return 0;
7560 /* Make the x-window of frame F use a rectangle with text.
7561 Use ICON_NAME as the text. */
7564 x_text_icon (f, icon_name)
7565 struct frame *f;
7566 char *icon_name;
7568 if (FRAME_X_WINDOW (f) == 0)
7569 return 1;
7571 #ifdef HAVE_X11R4
7573 XTextProperty text;
7574 text.value = (unsigned char *) icon_name;
7575 text.encoding = XA_STRING;
7576 text.format = 8;
7577 text.nitems = strlen (icon_name);
7578 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7580 #else /* not HAVE_X11R4 */
7581 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
7582 #endif /* not HAVE_X11R4 */
7584 if (f->output_data.x->icon_bitmap > 0)
7585 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7586 f->output_data.x->icon_bitmap = 0;
7587 x_wm_set_icon_pixmap (f, 0);
7589 return 0;
7592 #define X_ERROR_MESSAGE_SIZE 200
7594 /* If non-nil, this should be a string.
7595 It means catch X errors and store the error message in this string. */
7597 static Lisp_Object x_error_message_string;
7599 /* An X error handler which stores the error message in
7600 x_error_message_string. This is called from x_error_handler if
7601 x_catch_errors is in effect. */
7603 static void
7604 x_error_catcher (display, error)
7605 Display *display;
7606 XErrorEvent *error;
7608 XGetErrorText (display, error->error_code,
7609 SDATA (x_error_message_string),
7610 X_ERROR_MESSAGE_SIZE);
7613 /* Begin trapping X errors for display DPY. Actually we trap X errors
7614 for all displays, but DPY should be the display you are actually
7615 operating on.
7617 After calling this function, X protocol errors no longer cause
7618 Emacs to exit; instead, they are recorded in the string
7619 stored in x_error_message_string.
7621 Calling x_check_errors signals an Emacs error if an X error has
7622 occurred since the last call to x_catch_errors or x_check_errors.
7624 Calling x_uncatch_errors resumes the normal error handling. */
7626 void x_check_errors ();
7627 static Lisp_Object x_catch_errors_unwind ();
7630 x_catch_errors (dpy)
7631 Display *dpy;
7633 int count = SPECPDL_INDEX ();
7635 /* Make sure any errors from previous requests have been dealt with. */
7636 XSync (dpy, False);
7638 record_unwind_protect (x_catch_errors_unwind,
7639 Fcons (make_save_value (dpy, 0),
7640 x_error_message_string));
7642 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
7643 SSET (x_error_message_string, 0, 0);
7645 return count;
7648 /* Unbind the binding that we made to check for X errors. */
7650 static Lisp_Object
7651 x_catch_errors_unwind (old_val)
7652 Lisp_Object old_val;
7654 Lisp_Object first;
7656 first = XCAR (old_val);
7658 XSync (XSAVE_VALUE (first)->pointer, False);
7660 x_error_message_string = XCDR (old_val);
7661 return Qnil;
7664 /* If any X protocol errors have arrived since the last call to
7665 x_catch_errors or x_check_errors, signal an Emacs error using
7666 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7668 void
7669 x_check_errors (dpy, format)
7670 Display *dpy;
7671 char *format;
7673 /* Make sure to catch any errors incurred so far. */
7674 XSync (dpy, False);
7676 if (SREF (x_error_message_string, 0))
7677 error (format, SDATA (x_error_message_string));
7680 /* Nonzero if we had any X protocol errors
7681 since we did x_catch_errors on DPY. */
7684 x_had_errors_p (dpy)
7685 Display *dpy;
7687 /* Make sure to catch any errors incurred so far. */
7688 XSync (dpy, False);
7690 return SREF (x_error_message_string, 0) != 0;
7693 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7695 void
7696 x_clear_errors (dpy)
7697 Display *dpy;
7699 SSET (x_error_message_string, 0, 0);
7702 /* Stop catching X protocol errors and let them make Emacs die.
7703 DPY should be the display that was passed to x_catch_errors.
7704 COUNT should be the value that was returned by
7705 the corresponding call to x_catch_errors. */
7707 void
7708 x_uncatch_errors (dpy, count)
7709 Display *dpy;
7710 int count;
7712 unbind_to (count, Qnil);
7715 #if 0
7716 static unsigned int x_wire_count;
7717 x_trace_wire ()
7719 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7721 #endif /* ! 0 */
7724 /* Handle SIGPIPE, which can happen when the connection to a server
7725 simply goes away. SIGPIPE is handled by x_connection_signal.
7726 Don't need to do anything, because the write which caused the
7727 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7728 which will do the appropriate cleanup for us. */
7730 static SIGTYPE
7731 x_connection_signal (signalnum) /* If we don't have an argument, */
7732 int signalnum; /* some compilers complain in signal calls. */
7734 #ifdef USG
7735 /* USG systems forget handlers when they are used;
7736 must reestablish each time */
7737 signal (signalnum, x_connection_signal);
7738 #endif /* USG */
7742 /************************************************************************
7743 Handling X errors
7744 ************************************************************************/
7746 /* Error message passed to x_connection_closed. */
7748 static char *error_msg;
7750 /* Function installed as fatal_error_signal_hook in
7751 x_connection_closed. Print the X error message, and exit normally,
7752 instead of dumping core when XtCloseDisplay fails. */
7754 static void
7755 x_fatal_error_signal ()
7757 fprintf (stderr, "%s\n", error_msg);
7758 exit (70);
7761 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7762 the text of an error message that lead to the connection loss. */
7764 static SIGTYPE
7765 x_connection_closed (dpy, error_message)
7766 Display *dpy;
7767 char *error_message;
7769 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7770 Lisp_Object frame, tail;
7771 int count;
7773 error_msg = (char *) alloca (strlen (error_message) + 1);
7774 strcpy (error_msg, error_message);
7775 handling_signal = 0;
7777 /* Prevent being called recursively because of an error condition
7778 below. Otherwise, we might end up with printing ``can't find per
7779 display information'' in the recursive call instead of printing
7780 the original message here. */
7781 count = x_catch_errors (dpy);
7783 /* We have to close the display to inform Xt that it doesn't
7784 exist anymore. If we don't, Xt will continue to wait for
7785 events from the display. As a consequence, a sequence of
7787 M-x make-frame-on-display RET :1 RET
7788 ...kill the new frame, so that we get an IO error...
7789 M-x make-frame-on-display RET :1 RET
7791 will indefinitely wait in Xt for events for display `:1', opened
7792 in the first class to make-frame-on-display.
7794 Closing the display is reported to lead to a bus error on
7795 OpenWindows in certain situations. I suspect that is a bug
7796 in OpenWindows. I don't know how to cicumvent it here. */
7798 #ifdef USE_X_TOOLKIT
7799 /* If DPYINFO is null, this means we didn't open the display
7800 in the first place, so don't try to close it. */
7801 if (dpyinfo)
7803 extern void (*fatal_error_signal_hook) P_ ((void));
7804 fatal_error_signal_hook = x_fatal_error_signal;
7805 XtCloseDisplay (dpy);
7806 fatal_error_signal_hook = NULL;
7808 #endif
7810 #ifdef USE_GTK
7811 if (dpyinfo)
7812 xg_display_close (dpyinfo->display);
7813 #endif
7815 /* Indicate that this display is dead. */
7816 if (dpyinfo)
7817 dpyinfo->display = 0;
7819 /* First delete frames whose mini-buffers are on frames
7820 that are on the dead display. */
7821 FOR_EACH_FRAME (tail, frame)
7823 Lisp_Object minibuf_frame;
7824 minibuf_frame
7825 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7826 if (FRAME_X_P (XFRAME (frame))
7827 && FRAME_X_P (XFRAME (minibuf_frame))
7828 && ! EQ (frame, minibuf_frame)
7829 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7830 Fdelete_frame (frame, Qt);
7833 /* Now delete all remaining frames on the dead display.
7834 We are now sure none of these is used as the mini-buffer
7835 for another frame that we need to delete. */
7836 FOR_EACH_FRAME (tail, frame)
7837 if (FRAME_X_P (XFRAME (frame))
7838 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7840 /* Set this to t so that Fdelete_frame won't get confused
7841 trying to find a replacement. */
7842 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7843 Fdelete_frame (frame, Qt);
7846 if (dpyinfo)
7847 x_delete_display (dpyinfo);
7849 x_uncatch_errors (dpy, count);
7851 if (x_display_list == 0)
7853 fprintf (stderr, "%s\n", error_msg);
7854 shut_down_emacs (0, 0, Qnil);
7855 exit (70);
7858 /* Ordinary stack unwind doesn't deal with these. */
7859 #ifdef SIGIO
7860 sigunblock (sigmask (SIGIO));
7861 #endif
7862 sigunblock (sigmask (SIGALRM));
7863 TOTALLY_UNBLOCK_INPUT;
7865 clear_waiting_for_input ();
7866 error ("%s", error_msg);
7870 /* This is the usual handler for X protocol errors.
7871 It kills all frames on the display that we got the error for.
7872 If that was the only one, it prints an error message and kills Emacs. */
7874 static void
7875 x_error_quitter (display, error)
7876 Display *display;
7877 XErrorEvent *error;
7879 char buf[256], buf1[356];
7881 /* Note that there is no real way portable across R3/R4 to get the
7882 original error handler. */
7884 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7885 sprintf (buf1, "X protocol error: %s on protocol request %d",
7886 buf, error->request_code);
7887 x_connection_closed (display, buf1);
7891 /* This is the first-level handler for X protocol errors.
7892 It calls x_error_quitter or x_error_catcher. */
7894 static int
7895 x_error_handler (display, error)
7896 Display *display;
7897 XErrorEvent *error;
7899 if (! NILP (x_error_message_string))
7900 x_error_catcher (display, error);
7901 else
7902 x_error_quitter (display, error);
7903 return 0;
7906 /* This is the handler for X IO errors, always.
7907 It kills all frames on the display that we lost touch with.
7908 If that was the only one, it prints an error message and kills Emacs. */
7910 static int
7911 x_io_error_quitter (display)
7912 Display *display;
7914 char buf[256];
7916 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7917 x_connection_closed (display, buf);
7918 return 0;
7921 /* Changing the font of the frame. */
7923 /* Give frame F the font named FONTNAME as its default font, and
7924 return the full name of that font. FONTNAME may be a wildcard
7925 pattern; in that case, we choose some font that fits the pattern.
7926 The return value shows which font we chose. */
7928 Lisp_Object
7929 x_new_font (f, fontname)
7930 struct frame *f;
7931 register char *fontname;
7933 struct font_info *fontp
7934 = FS_LOAD_FONT (f, 0, fontname, -1);
7936 if (!fontp)
7937 return Qnil;
7939 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
7940 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
7941 FRAME_FONTSET (f) = -1;
7943 FRAME_COLUMN_WIDTH (f) = FONT_WIDTH (FRAME_FONT (f));
7944 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
7946 compute_fringe_widths (f, 1);
7948 /* Compute the scroll bar width in character columns. */
7949 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
7951 int wid = FRAME_COLUMN_WIDTH (f);
7952 FRAME_CONFIG_SCROLL_BAR_COLS (f)
7953 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
7955 else
7957 int wid = FRAME_COLUMN_WIDTH (f);
7958 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
7961 /* Now make the frame display the given font. */
7962 if (FRAME_X_WINDOW (f) != 0)
7964 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
7965 FRAME_FONT (f)->fid);
7966 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
7967 FRAME_FONT (f)->fid);
7968 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
7969 FRAME_FONT (f)->fid);
7971 /* Don't change the size of a tip frame; there's no point in
7972 doing it because it's done in Fx_show_tip, and it leads to
7973 problems because the tip frame has no widget. */
7974 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
7975 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
7978 return build_string (fontp->full_name);
7981 /* Give frame F the fontset named FONTSETNAME as its default font, and
7982 return the full name of that fontset. FONTSETNAME may be a wildcard
7983 pattern; in that case, we choose some fontset that fits the pattern.
7984 The return value shows which fontset we chose. */
7986 Lisp_Object
7987 x_new_fontset (f, fontsetname)
7988 struct frame *f;
7989 char *fontsetname;
7991 int fontset = fs_query_fontset (build_string (fontsetname), 0);
7992 Lisp_Object result;
7994 if (fontset < 0)
7995 return Qnil;
7997 if (FRAME_FONTSET (f) == fontset)
7998 /* This fontset is already set in frame F. There's nothing more
7999 to do. */
8000 return fontset_name (fontset);
8002 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
8004 if (!STRINGP (result))
8005 /* Can't load ASCII font. */
8006 return Qnil;
8008 /* Since x_new_font doesn't update any fontset information, do it now. */
8009 FRAME_FONTSET (f) = fontset;
8011 #ifdef HAVE_X_I18N
8012 if (FRAME_XIC (f)
8013 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8014 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8015 #endif
8017 return build_string (fontsetname);
8021 /***********************************************************************
8022 X Input Methods
8023 ***********************************************************************/
8025 #ifdef HAVE_X_I18N
8027 #ifdef HAVE_X11R6
8029 /* XIM destroy callback function, which is called whenever the
8030 connection to input method XIM dies. CLIENT_DATA contains a
8031 pointer to the x_display_info structure corresponding to XIM. */
8033 static void
8034 xim_destroy_callback (xim, client_data, call_data)
8035 XIM xim;
8036 XPointer client_data;
8037 XPointer call_data;
8039 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8040 Lisp_Object frame, tail;
8042 BLOCK_INPUT;
8044 /* No need to call XDestroyIC.. */
8045 FOR_EACH_FRAME (tail, frame)
8047 struct frame *f = XFRAME (frame);
8048 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8050 FRAME_XIC (f) = NULL;
8051 if (FRAME_XIC_FONTSET (f))
8053 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
8054 FRAME_XIC_FONTSET (f) = NULL;
8059 /* No need to call XCloseIM. */
8060 dpyinfo->xim = NULL;
8061 XFree (dpyinfo->xim_styles);
8062 UNBLOCK_INPUT;
8065 #endif /* HAVE_X11R6 */
8067 #ifdef HAVE_X11R6
8068 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8069 extern char *XSetIMValues P_ ((XIM, ...));
8070 #endif
8072 /* Open the connection to the XIM server on display DPYINFO.
8073 RESOURCE_NAME is the resource name Emacs uses. */
8075 static void
8076 xim_open_dpy (dpyinfo, resource_name)
8077 struct x_display_info *dpyinfo;
8078 char *resource_name;
8080 XIM xim;
8082 #ifdef HAVE_XIM
8083 if (use_xim)
8085 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8086 EMACS_CLASS);
8087 dpyinfo->xim = xim;
8089 if (xim)
8091 #ifdef HAVE_X11R6
8092 XIMCallback destroy;
8093 #endif
8095 /* Get supported styles and XIM values. */
8096 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8098 #ifdef HAVE_X11R6
8099 destroy.callback = xim_destroy_callback;
8100 destroy.client_data = (XPointer)dpyinfo;
8101 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8102 #endif
8106 else
8107 #endif /* HAVE_XIM */
8108 dpyinfo->xim = NULL;
8112 #ifdef HAVE_X11R6_XIM
8114 struct xim_inst_t
8116 struct x_display_info *dpyinfo;
8117 char *resource_name;
8120 /* XIM instantiate callback function, which is called whenever an XIM
8121 server is available. DISPLAY is the display of the XIM.
8122 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8123 when the callback was registered. */
8125 static void
8126 xim_instantiate_callback (display, client_data, call_data)
8127 Display *display;
8128 XPointer client_data;
8129 XPointer call_data;
8131 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8132 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8134 /* We don't support multiple XIM connections. */
8135 if (dpyinfo->xim)
8136 return;
8138 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8140 /* Create XIC for the existing frames on the same display, as long
8141 as they have no XIC. */
8142 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8144 Lisp_Object tail, frame;
8146 BLOCK_INPUT;
8147 FOR_EACH_FRAME (tail, frame)
8149 struct frame *f = XFRAME (frame);
8151 if (FRAME_X_P (f)
8152 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8153 if (FRAME_XIC (f) == NULL)
8155 create_frame_xic (f);
8156 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8157 xic_set_statusarea (f);
8158 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8160 struct window *w = XWINDOW (f->selected_window);
8161 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8166 UNBLOCK_INPUT;
8170 #endif /* HAVE_X11R6_XIM */
8173 /* Open a connection to the XIM server on display DPYINFO.
8174 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8175 connection only at the first time. On X11R6, open the connection
8176 in the XIM instantiate callback function. */
8178 static void
8179 xim_initialize (dpyinfo, resource_name)
8180 struct x_display_info *dpyinfo;
8181 char *resource_name;
8183 #ifdef HAVE_XIM
8184 if (use_xim)
8186 #ifdef HAVE_X11R6_XIM
8187 struct xim_inst_t *xim_inst;
8188 int len;
8190 dpyinfo->xim = NULL;
8191 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8192 xim_inst->dpyinfo = dpyinfo;
8193 len = strlen (resource_name);
8194 xim_inst->resource_name = (char *) xmalloc (len + 1);
8195 bcopy (resource_name, xim_inst->resource_name, len + 1);
8196 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8197 resource_name, EMACS_CLASS,
8198 xim_instantiate_callback,
8199 /* This is XPointer in XFree86
8200 but (XPointer *) on Tru64, at
8201 least, hence the configure test. */
8202 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8203 #else /* not HAVE_X11R6_XIM */
8204 dpyinfo->xim = NULL;
8205 xim_open_dpy (dpyinfo, resource_name);
8206 #endif /* not HAVE_X11R6_XIM */
8209 else
8210 #endif /* HAVE_XIM */
8211 dpyinfo->xim = NULL;
8215 /* Close the connection to the XIM server on display DPYINFO. */
8217 static void
8218 xim_close_dpy (dpyinfo)
8219 struct x_display_info *dpyinfo;
8221 #ifdef HAVE_XIM
8222 if (use_xim)
8224 #ifdef HAVE_X11R6_XIM
8225 if (dpyinfo->display)
8226 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8227 NULL, EMACS_CLASS,
8228 xim_instantiate_callback, NULL);
8229 #endif /* not HAVE_X11R6_XIM */
8230 if (dpyinfo->display)
8231 XCloseIM (dpyinfo->xim);
8232 dpyinfo->xim = NULL;
8233 XFree (dpyinfo->xim_styles);
8235 #endif /* HAVE_XIM */
8238 #endif /* not HAVE_X11R6_XIM */
8242 /* Calculate the absolute position in frame F
8243 from its current recorded position values and gravity. */
8245 void
8246 x_calc_absolute_position (f)
8247 struct frame *f;
8249 Window child;
8250 int win_x = 0, win_y = 0;
8251 int flags = f->size_hint_flags;
8252 int this_window;
8254 /* We have nothing to do if the current position
8255 is already for the top-left corner. */
8256 if (! ((flags & XNegative) || (flags & YNegative)))
8257 return;
8259 this_window = FRAME_OUTER_WINDOW (f);
8261 /* Find the position of the outside upper-left corner of
8262 the inner window, with respect to the outer window.
8263 But do this only if we will need the results. */
8264 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8266 int count;
8268 BLOCK_INPUT;
8269 count = x_catch_errors (FRAME_X_DISPLAY (f));
8270 while (1)
8272 x_clear_errors (FRAME_X_DISPLAY (f));
8273 XTranslateCoordinates (FRAME_X_DISPLAY (f),
8275 /* From-window, to-window. */
8276 this_window,
8277 f->output_data.x->parent_desc,
8279 /* From-position, to-position. */
8280 0, 0, &win_x, &win_y,
8282 /* Child of win. */
8283 &child);
8284 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
8286 Window newroot, newparent = 0xdeadbeef;
8287 Window *newchildren;
8288 unsigned int nchildren;
8290 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
8291 &newparent, &newchildren, &nchildren))
8292 break;
8294 XFree ((char *) newchildren);
8296 f->output_data.x->parent_desc = newparent;
8298 else
8299 break;
8302 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
8303 UNBLOCK_INPUT;
8306 /* Treat negative positions as relative to the leftmost bottommost
8307 position that fits on the screen. */
8308 if (flags & XNegative)
8309 f->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8310 - 2 * f->border_width - win_x
8311 - FRAME_PIXEL_WIDTH (f)
8312 + f->left_pos);
8315 int height = FRAME_PIXEL_HEIGHT (f);
8317 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8318 /* Something is fishy here. When using Motif, starting Emacs with
8319 `-g -0-0', the frame appears too low by a few pixels.
8321 This seems to be so because initially, while Emacs is starting,
8322 the column widget's height and the frame's pixel height are
8323 different. The column widget's height is the right one. In
8324 later invocations, when Emacs is up, the frame's pixel height
8325 is right, though.
8327 It's not obvious where the initial small difference comes from.
8328 2000-12-01, gerd. */
8330 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8331 #endif
8333 if (flags & YNegative)
8334 f->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
8335 - 2 * f->border_width
8336 - win_y
8337 - height
8338 + f->top_pos);
8341 /* The left_pos and top_pos
8342 are now relative to the top and left screen edges,
8343 so the flags should correspond. */
8344 f->size_hint_flags &= ~ (XNegative | YNegative);
8347 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8348 to really change the position, and 0 when calling from
8349 x_make_frame_visible (in that case, XOFF and YOFF are the current
8350 position values). It is -1 when calling from x_set_frame_parameters,
8351 which means, do adjust for borders but don't change the gravity. */
8353 void
8354 x_set_offset (f, xoff, yoff, change_gravity)
8355 struct frame *f;
8356 register int xoff, yoff;
8357 int change_gravity;
8359 int modified_top, modified_left;
8361 if (change_gravity > 0)
8363 f->top_pos = yoff;
8364 f->left_pos = xoff;
8365 f->size_hint_flags &= ~ (XNegative | YNegative);
8366 if (xoff < 0)
8367 f->size_hint_flags |= XNegative;
8368 if (yoff < 0)
8369 f->size_hint_flags |= YNegative;
8370 f->win_gravity = NorthWestGravity;
8372 x_calc_absolute_position (f);
8374 BLOCK_INPUT;
8375 x_wm_set_size_hint (f, (long) 0, 0);
8377 modified_left = f->left_pos;
8378 modified_top = f->top_pos;
8380 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
8381 this seems to be unnecessary and incorrect. rms, 4/17/97. */
8382 /* It is a mystery why we need to add the border_width here
8383 when the frame is already visible, but experiment says we do. */
8384 if (change_gravity != 0)
8386 modified_left += f->border_width;
8387 modified_top += f->border_width;
8389 #endif
8391 if (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8393 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8394 than the WM decorations. So we use the calculated offset instead
8395 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8396 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8397 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8400 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8401 modified_left, modified_top);
8403 if (FRAME_VISIBLE_P (f)
8404 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN)
8406 FRAME_X_OUTPUT (f)->check_expected_move = 1;
8407 FRAME_X_OUTPUT (f)->expected_top = f->top_pos;
8408 FRAME_X_OUTPUT (f)->expected_left = f->left_pos;
8411 UNBLOCK_INPUT;
8414 /* Check if we need to resize the frame due to a fullscreen request.
8415 If so needed, resize the frame. */
8416 static void
8417 x_check_fullscreen (f)
8418 struct frame *f;
8420 if (f->want_fullscreen & FULLSCREEN_BOTH)
8422 int width, height, ign;
8424 x_real_positions (f, &f->left_pos, &f->top_pos);
8426 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8428 /* We do not need to move the window, it shall be taken care of
8429 when setting WM manager hints.
8430 If the frame is visible already, the position is checked by
8431 x_check_expected_move. */
8432 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8434 change_frame_size (f, height, width, 0, 1, 0);
8435 SET_FRAME_GARBAGED (f);
8436 cancel_mouse_face (f);
8438 /* Wait for the change of frame size to occur */
8439 f->want_fullscreen |= FULLSCREEN_WAIT;
8444 /* If frame parameters are set after the frame is mapped, we need to move
8445 the window.
8446 Some window managers moves the window to the right position, some
8447 moves the outer window manager window to the specified position.
8448 Here we check that we are in the right spot. If not, make a second
8449 move, assuming we are dealing with the second kind of window manager. */
8450 static void
8451 x_check_expected_move (f)
8452 struct frame *f;
8454 if (FRAME_X_OUTPUT (f)->check_expected_move)
8456 int expect_top = FRAME_X_OUTPUT (f)->expected_top;
8457 int expect_left = FRAME_X_OUTPUT (f)->expected_left;
8459 if (expect_top != f->top_pos || expect_left != f->left_pos)
8461 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8462 FRAME_X_OUTPUT (f)->move_offset_left = expect_left - f->left_pos;
8463 FRAME_X_OUTPUT (f)->move_offset_top = expect_top - f->top_pos;
8465 x_set_offset (f, expect_left, expect_top, 1);
8467 else if (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN)
8468 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8470 /* Just do this once */
8471 FRAME_X_OUTPUT (f)->check_expected_move = 0;
8476 /* Change the size of frame F's X window to COLS/ROWS in the case F
8477 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8478 top-left-corner window gravity for this size change and subsequent
8479 size changes. Otherwise we leave the window gravity unchanged. */
8481 static void
8482 x_set_window_size_1 (f, change_gravity, cols, rows)
8483 struct frame *f;
8484 int change_gravity;
8485 int cols, rows;
8487 int pixelwidth, pixelheight;
8489 check_frame_size (f, &rows, &cols);
8490 f->scroll_bar_actual_width
8491 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8493 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8494 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8495 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8497 compute_fringe_widths (f, 0);
8499 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8500 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8502 f->win_gravity = NorthWestGravity;
8503 x_wm_set_size_hint (f, (long) 0, 0);
8505 XSync (FRAME_X_DISPLAY (f), False);
8506 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8507 pixelwidth, pixelheight);
8509 /* Now, strictly speaking, we can't be sure that this is accurate,
8510 but the window manager will get around to dealing with the size
8511 change request eventually, and we'll hear how it went when the
8512 ConfigureNotify event gets here.
8514 We could just not bother storing any of this information here,
8515 and let the ConfigureNotify event set everything up, but that
8516 might be kind of confusing to the Lisp code, since size changes
8517 wouldn't be reported in the frame parameters until some random
8518 point in the future when the ConfigureNotify event arrives.
8520 We pass 1 for DELAY since we can't run Lisp code inside of
8521 a BLOCK_INPUT. */
8522 change_frame_size (f, rows, cols, 0, 1, 0);
8523 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8524 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8526 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8527 receive in the ConfigureNotify event; if we get what we asked
8528 for, then the event won't cause the screen to become garbaged, so
8529 we have to make sure to do it here. */
8530 SET_FRAME_GARBAGED (f);
8532 XFlush (FRAME_X_DISPLAY (f));
8536 /* Call this to change the size of frame F's x-window.
8537 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8538 for this size change and subsequent size changes.
8539 Otherwise we leave the window gravity unchanged. */
8541 void
8542 x_set_window_size (f, change_gravity, cols, rows)
8543 struct frame *f;
8544 int change_gravity;
8545 int cols, rows;
8547 BLOCK_INPUT;
8549 #ifdef USE_GTK
8550 if (FRAME_GTK_WIDGET (f))
8551 xg_frame_set_char_size (f, cols, rows);
8552 else
8553 x_set_window_size_1 (f, change_gravity, cols, rows);
8554 #elif USE_X_TOOLKIT
8556 if (f->output_data.x->widget != NULL)
8558 /* The x and y position of the widget is clobbered by the
8559 call to XtSetValues within EmacsFrameSetCharSize.
8560 This is a real kludge, but I don't understand Xt so I can't
8561 figure out a correct fix. Can anyone else tell me? -- rms. */
8562 int xpos = f->output_data.x->widget->core.x;
8563 int ypos = f->output_data.x->widget->core.y;
8564 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8565 f->output_data.x->widget->core.x = xpos;
8566 f->output_data.x->widget->core.y = ypos;
8568 else
8569 x_set_window_size_1 (f, change_gravity, cols, rows);
8571 #else /* not USE_X_TOOLKIT */
8573 x_set_window_size_1 (f, change_gravity, cols, rows);
8575 #endif /* not USE_X_TOOLKIT */
8577 /* If cursor was outside the new size, mark it as off. */
8578 mark_window_cursors_off (XWINDOW (f->root_window));
8580 /* Clear out any recollection of where the mouse highlighting was,
8581 since it might be in a place that's outside the new frame size.
8582 Actually checking whether it is outside is a pain in the neck,
8583 so don't try--just let the highlighting be done afresh with new size. */
8584 cancel_mouse_face (f);
8586 UNBLOCK_INPUT;
8589 /* Mouse warping. */
8591 void
8592 x_set_mouse_position (f, x, y)
8593 struct frame *f;
8594 int x, y;
8596 int pix_x, pix_y;
8598 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8599 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8601 if (pix_x < 0) pix_x = 0;
8602 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8604 if (pix_y < 0) pix_y = 0;
8605 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8607 BLOCK_INPUT;
8609 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8610 0, 0, 0, 0, pix_x, pix_y);
8611 UNBLOCK_INPUT;
8614 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8616 void
8617 x_set_mouse_pixel_position (f, pix_x, pix_y)
8618 struct frame *f;
8619 int pix_x, pix_y;
8621 BLOCK_INPUT;
8623 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8624 0, 0, 0, 0, pix_x, pix_y);
8625 UNBLOCK_INPUT;
8628 /* focus shifting, raising and lowering. */
8630 void
8631 x_focus_on_frame (f)
8632 struct frame *f;
8634 #if 0 /* This proves to be unpleasant. */
8635 x_raise_frame (f);
8636 #endif
8637 #if 0
8638 /* I don't think that the ICCCM allows programs to do things like this
8639 without the interaction of the window manager. Whatever you end up
8640 doing with this code, do it to x_unfocus_frame too. */
8641 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8642 RevertToPointerRoot, CurrentTime);
8643 #endif /* ! 0 */
8646 void
8647 x_unfocus_frame (f)
8648 struct frame *f;
8650 #if 0
8651 /* Look at the remarks in x_focus_on_frame. */
8652 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8653 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8654 RevertToPointerRoot, CurrentTime);
8655 #endif /* ! 0 */
8658 /* Raise frame F. */
8660 void
8661 x_raise_frame (f)
8662 struct frame *f;
8664 if (f->async_visible)
8666 BLOCK_INPUT;
8667 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8668 XFlush (FRAME_X_DISPLAY (f));
8669 UNBLOCK_INPUT;
8673 /* Lower frame F. */
8675 void
8676 x_lower_frame (f)
8677 struct frame *f;
8679 if (f->async_visible)
8681 BLOCK_INPUT;
8682 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8683 XFlush (FRAME_X_DISPLAY (f));
8684 UNBLOCK_INPUT;
8688 static void
8689 XTframe_raise_lower (f, raise_flag)
8690 FRAME_PTR f;
8691 int raise_flag;
8693 if (raise_flag)
8694 x_raise_frame (f);
8695 else
8696 x_lower_frame (f);
8699 /* Change of visibility. */
8701 /* This tries to wait until the frame is really visible.
8702 However, if the window manager asks the user where to position
8703 the frame, this will return before the user finishes doing that.
8704 The frame will not actually be visible at that time,
8705 but it will become visible later when the window manager
8706 finishes with it. */
8708 void
8709 x_make_frame_visible (f)
8710 struct frame *f;
8712 Lisp_Object type;
8713 int original_top, original_left;
8714 int retry_count = 2;
8716 retry:
8718 BLOCK_INPUT;
8720 type = x_icon_type (f);
8721 if (!NILP (type))
8722 x_bitmap_icon (f, type);
8724 if (! FRAME_VISIBLE_P (f))
8726 /* We test FRAME_GARBAGED_P here to make sure we don't
8727 call x_set_offset a second time
8728 if we get to x_make_frame_visible a second time
8729 before the window gets really visible. */
8730 if (! FRAME_ICONIFIED_P (f)
8731 && ! f->output_data.x->asked_for_visible)
8732 x_set_offset (f, f->left_pos, f->top_pos, 0);
8734 f->output_data.x->asked_for_visible = 1;
8736 if (! EQ (Vx_no_window_manager, Qt))
8737 x_wm_set_window_state (f, NormalState);
8738 #ifdef USE_X_TOOLKIT
8739 /* This was XtPopup, but that did nothing for an iconified frame. */
8740 XtMapWidget (f->output_data.x->widget);
8741 #else /* not USE_X_TOOLKIT */
8742 #ifdef USE_GTK
8743 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8744 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8745 #else
8746 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8747 #endif /* not USE_GTK */
8748 #endif /* not USE_X_TOOLKIT */
8749 #if 0 /* This seems to bring back scroll bars in the wrong places
8750 if the window configuration has changed. They seem
8751 to come back ok without this. */
8752 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8753 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8754 #endif
8757 XFlush (FRAME_X_DISPLAY (f));
8759 /* Synchronize to ensure Emacs knows the frame is visible
8760 before we do anything else. We do this loop with input not blocked
8761 so that incoming events are handled. */
8763 Lisp_Object frame;
8764 int count;
8765 /* This must be before UNBLOCK_INPUT
8766 since events that arrive in response to the actions above
8767 will set it when they are handled. */
8768 int previously_visible = f->output_data.x->has_been_visible;
8770 original_left = f->left_pos;
8771 original_top = f->top_pos;
8773 /* This must come after we set COUNT. */
8774 UNBLOCK_INPUT;
8776 /* We unblock here so that arriving X events are processed. */
8778 /* Now move the window back to where it was "supposed to be".
8779 But don't do it if the gravity is negative.
8780 When the gravity is negative, this uses a position
8781 that is 3 pixels too low. Perhaps that's really the border width.
8783 Don't do this if the window has never been visible before,
8784 because the window manager may choose the position
8785 and we don't want to override it. */
8787 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
8788 && f->win_gravity == NorthWestGravity
8789 && previously_visible)
8791 Drawable rootw;
8792 int x, y;
8793 unsigned int width, height, border, depth;
8795 BLOCK_INPUT;
8797 /* On some window managers (such as FVWM) moving an existing
8798 window, even to the same place, causes the window manager
8799 to introduce an offset. This can cause the window to move
8800 to an unexpected location. Check the geometry (a little
8801 slow here) and then verify that the window is in the right
8802 place. If the window is not in the right place, move it
8803 there, and take the potential window manager hit. */
8804 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8805 &rootw, &x, &y, &width, &height, &border, &depth);
8807 if (original_left != x || original_top != y)
8808 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8809 original_left, original_top);
8811 UNBLOCK_INPUT;
8814 XSETFRAME (frame, f);
8816 /* Wait until the frame is visible. Process X events until a
8817 MapNotify event has been seen, or until we think we won't get a
8818 MapNotify at all.. */
8819 for (count = input_signal_count + 10;
8820 input_signal_count < count && !FRAME_VISIBLE_P (f);)
8822 /* Force processing of queued events. */
8823 x_sync (f);
8825 /* Machines that do polling rather than SIGIO have been
8826 observed to go into a busy-wait here. So we'll fake an
8827 alarm signal to let the handler know that there's something
8828 to be read. We used to raise a real alarm, but it seems
8829 that the handler isn't always enabled here. This is
8830 probably a bug. */
8831 if (input_polling_used ())
8833 /* It could be confusing if a real alarm arrives while
8834 processing the fake one. Turn it off and let the
8835 handler reset it. */
8836 extern void poll_for_input_1 P_ ((void));
8837 int old_poll_suppress_count = poll_suppress_count;
8838 poll_suppress_count = 1;
8839 poll_for_input_1 ();
8840 poll_suppress_count = old_poll_suppress_count;
8843 /* See if a MapNotify event has been processed. */
8844 FRAME_SAMPLE_VISIBILITY (f);
8847 /* 2000-09-28: In
8849 (let ((f (selected-frame)))
8850 (iconify-frame f)
8851 (raise-frame f))
8853 the frame is not raised with various window managers on
8854 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
8855 unknown reason, the call to XtMapWidget is completely ignored.
8856 Mapping the widget a second time works. */
8858 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
8859 goto retry;
8863 /* Change from mapped state to withdrawn state. */
8865 /* Make the frame visible (mapped and not iconified). */
8867 void
8868 x_make_frame_invisible (f)
8869 struct frame *f;
8871 Window window;
8873 /* Use the frame's outermost window, not the one we normally draw on. */
8874 window = FRAME_OUTER_WINDOW (f);
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 0/* This might add unreliability; I don't trust it -- rms. */
8881 if (! f->async_visible && ! f->async_iconified)
8882 return;
8883 #endif
8885 BLOCK_INPUT;
8887 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
8888 that the current position of the window is user-specified, rather than
8889 program-specified, so that when the window is mapped again, it will be
8890 placed at the same location, without forcing the user to position it
8891 by hand again (they have already done that once for this window.) */
8892 x_wm_set_size_hint (f, (long) 0, 1);
8894 #ifdef USE_GTK
8895 if (FRAME_GTK_OUTER_WIDGET (f))
8896 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
8897 else
8898 #endif
8900 #ifdef HAVE_X11R4
8902 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
8903 DefaultScreen (FRAME_X_DISPLAY (f))))
8905 UNBLOCK_INPUT_RESIGNAL;
8906 error ("Can't notify window manager of window withdrawal");
8908 #else /* ! defined (HAVE_X11R4) */
8910 /* Tell the window manager what we're going to do. */
8911 if (! EQ (Vx_no_window_manager, Qt))
8913 XEvent unmap;
8915 unmap.xunmap.type = UnmapNotify;
8916 unmap.xunmap.window = window;
8917 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
8918 unmap.xunmap.from_configure = False;
8919 if (! XSendEvent (FRAME_X_DISPLAY (f),
8920 DefaultRootWindow (FRAME_X_DISPLAY (f)),
8921 False,
8922 SubstructureRedirectMaskSubstructureNotifyMask,
8923 &unmap))
8925 UNBLOCK_INPUT_RESIGNAL;
8926 error ("Can't notify window manager of withdrawal");
8930 /* Unmap the window ourselves. Cheeky! */
8931 XUnmapWindow (FRAME_X_DISPLAY (f), window);
8932 #endif /* ! defined (HAVE_X11R4) */
8935 /* We can't distinguish this from iconification
8936 just by the event that we get from the server.
8937 So we can't win using the usual strategy of letting
8938 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
8939 and synchronize with the server to make sure we agree. */
8940 f->visible = 0;
8941 FRAME_ICONIFIED_P (f) = 0;
8942 f->async_visible = 0;
8943 f->async_iconified = 0;
8945 x_sync (f);
8947 UNBLOCK_INPUT;
8950 /* Change window state from mapped to iconified. */
8952 void
8953 x_iconify_frame (f)
8954 struct frame *f;
8956 int result;
8957 Lisp_Object type;
8959 /* Don't keep the highlight on an invisible frame. */
8960 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8961 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
8963 if (f->async_iconified)
8964 return;
8966 BLOCK_INPUT;
8968 FRAME_SAMPLE_VISIBILITY (f);
8970 type = x_icon_type (f);
8971 if (!NILP (type))
8972 x_bitmap_icon (f, type);
8974 #ifdef USE_GTK
8975 if (FRAME_GTK_OUTER_WIDGET (f))
8977 if (! FRAME_VISIBLE_P (f))
8978 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8980 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8981 f->iconified = 1;
8982 f->visible = 1;
8983 f->async_iconified = 1;
8984 f->async_visible = 0;
8985 UNBLOCK_INPUT;
8986 return;
8988 #endif
8990 #ifdef USE_X_TOOLKIT
8992 if (! FRAME_VISIBLE_P (f))
8994 if (! EQ (Vx_no_window_manager, Qt))
8995 x_wm_set_window_state (f, IconicState);
8996 /* This was XtPopup, but that did nothing for an iconified frame. */
8997 XtMapWidget (f->output_data.x->widget);
8998 /* The server won't give us any event to indicate
8999 that an invisible frame was changed to an icon,
9000 so we have to record it here. */
9001 f->iconified = 1;
9002 f->visible = 1;
9003 f->async_iconified = 1;
9004 f->async_visible = 0;
9005 UNBLOCK_INPUT;
9006 return;
9009 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9010 XtWindow (f->output_data.x->widget),
9011 DefaultScreen (FRAME_X_DISPLAY (f)));
9012 UNBLOCK_INPUT;
9014 if (!result)
9015 error ("Can't notify window manager of iconification");
9017 f->async_iconified = 1;
9018 f->async_visible = 0;
9021 BLOCK_INPUT;
9022 XFlush (FRAME_X_DISPLAY (f));
9023 UNBLOCK_INPUT;
9024 #else /* not USE_X_TOOLKIT */
9026 /* Make sure the X server knows where the window should be positioned,
9027 in case the user deiconifies with the window manager. */
9028 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
9029 x_set_offset (f, f->left_pos, f->top_pos, 0);
9031 /* Since we don't know which revision of X we're running, we'll use both
9032 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9034 /* X11R4: send a ClientMessage to the window manager using the
9035 WM_CHANGE_STATE type. */
9037 XEvent message;
9039 message.xclient.window = FRAME_X_WINDOW (f);
9040 message.xclient.type = ClientMessage;
9041 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9042 message.xclient.format = 32;
9043 message.xclient.data.l[0] = IconicState;
9045 if (! XSendEvent (FRAME_X_DISPLAY (f),
9046 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9047 False,
9048 SubstructureRedirectMask | SubstructureNotifyMask,
9049 &message))
9051 UNBLOCK_INPUT_RESIGNAL;
9052 error ("Can't notify window manager of iconification");
9056 /* X11R3: set the initial_state field of the window manager hints to
9057 IconicState. */
9058 x_wm_set_window_state (f, IconicState);
9060 if (!FRAME_VISIBLE_P (f))
9062 /* If the frame was withdrawn, before, we must map it. */
9063 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9066 f->async_iconified = 1;
9067 f->async_visible = 0;
9069 XFlush (FRAME_X_DISPLAY (f));
9070 UNBLOCK_INPUT;
9071 #endif /* not USE_X_TOOLKIT */
9075 /* Free X resources of frame F. */
9077 void
9078 x_free_frame_resources (f)
9079 struct frame *f;
9081 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9082 Lisp_Object bar;
9083 struct scroll_bar *b;
9085 BLOCK_INPUT;
9087 /* If a display connection is dead, don't try sending more
9088 commands to the X server. */
9089 if (dpyinfo->display)
9091 if (f->output_data.x->icon_desc)
9092 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9094 #ifdef USE_X_TOOLKIT
9095 /* Explicitly destroy the scroll bars of the frame. Without
9096 this, we get "BadDrawable" errors from the toolkit later on,
9097 presumably from expose events generated for the disappearing
9098 toolkit scroll bars. */
9099 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9101 b = XSCROLL_BAR (bar);
9102 x_scroll_bar_remove (b);
9104 #endif
9106 #ifdef HAVE_X_I18N
9107 if (FRAME_XIC (f))
9108 free_frame_xic (f);
9109 #endif
9111 #ifdef USE_X_TOOLKIT
9112 if (f->output_data.x->widget)
9114 XtDestroyWidget (f->output_data.x->widget);
9115 f->output_data.x->widget = NULL;
9117 /* Tooltips don't have widgets, only a simple X window, even if
9118 we are using a toolkit. */
9119 else if (FRAME_X_WINDOW (f))
9120 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9122 free_frame_menubar (f);
9123 #else /* !USE_X_TOOLKIT */
9125 #ifdef USE_GTK
9126 /* In the GTK version, tooltips are normal X
9127 frames. We must check and free both types. */
9128 if (FRAME_GTK_OUTER_WIDGET (f))
9130 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9131 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9132 FRAME_GTK_OUTER_WIDGET (f) = 0;
9134 #endif /* USE_GTK */
9136 if (FRAME_X_WINDOW (f))
9137 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9138 #endif /* !USE_X_TOOLKIT */
9140 unload_color (f, f->output_data.x->foreground_pixel);
9141 unload_color (f, f->output_data.x->background_pixel);
9142 unload_color (f, f->output_data.x->cursor_pixel);
9143 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9144 unload_color (f, f->output_data.x->border_pixel);
9145 unload_color (f, f->output_data.x->mouse_pixel);
9147 if (f->output_data.x->scroll_bar_background_pixel != -1)
9148 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9149 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9150 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9151 #ifdef USE_TOOLKIT_SCROLL_BARS
9152 /* Scrollbar shadow colors. */
9153 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9154 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9155 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9156 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9157 #endif /* USE_TOOLKIT_SCROLL_BARS */
9158 if (f->output_data.x->white_relief.allocated_p)
9159 unload_color (f, f->output_data.x->white_relief.pixel);
9160 if (f->output_data.x->black_relief.allocated_p)
9161 unload_color (f, f->output_data.x->black_relief.pixel);
9163 if (FRAME_FACE_CACHE (f))
9164 free_frame_faces (f);
9166 x_free_gcs (f);
9167 XFlush (FRAME_X_DISPLAY (f));
9170 if (f->output_data.x->saved_menu_event)
9171 xfree (f->output_data.x->saved_menu_event);
9173 xfree (f->output_data.x);
9174 f->output_data.x = NULL;
9176 if (f == dpyinfo->x_focus_frame)
9177 dpyinfo->x_focus_frame = 0;
9178 if (f == dpyinfo->x_focus_event_frame)
9179 dpyinfo->x_focus_event_frame = 0;
9180 if (f == dpyinfo->x_highlight_frame)
9181 dpyinfo->x_highlight_frame = 0;
9183 if (f == dpyinfo->mouse_face_mouse_frame)
9185 dpyinfo->mouse_face_beg_row
9186 = dpyinfo->mouse_face_beg_col = -1;
9187 dpyinfo->mouse_face_end_row
9188 = dpyinfo->mouse_face_end_col = -1;
9189 dpyinfo->mouse_face_window = Qnil;
9190 dpyinfo->mouse_face_deferred_gc = 0;
9191 dpyinfo->mouse_face_mouse_frame = 0;
9194 UNBLOCK_INPUT;
9198 /* Destroy the X window of frame F. */
9200 void
9201 x_destroy_window (f)
9202 struct frame *f;
9204 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9206 /* If a display connection is dead, don't try sending more
9207 commands to the X server. */
9208 if (dpyinfo->display != 0)
9209 x_free_frame_resources (f);
9211 dpyinfo->reference_count--;
9215 /* Setting window manager hints. */
9217 /* Set the normal size hints for the window manager, for frame F.
9218 FLAGS is the flags word to use--or 0 meaning preserve the flags
9219 that the window now has.
9220 If USER_POSITION is nonzero, we set the USPosition
9221 flag (this is useful when FLAGS is 0).
9222 The GTK version is in gtkutils.c */
9224 #ifndef USE_GTK
9225 void
9226 x_wm_set_size_hint (f, flags, user_position)
9227 struct frame *f;
9228 long flags;
9229 int user_position;
9231 XSizeHints size_hints;
9233 #ifdef USE_X_TOOLKIT
9234 Arg al[2];
9235 int ac = 0;
9236 Dimension widget_width, widget_height;
9237 #endif
9239 Window window = FRAME_OUTER_WINDOW (f);
9241 /* Setting PMaxSize caused various problems. */
9242 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9244 size_hints.x = f->left_pos;
9245 size_hints.y = f->top_pos;
9247 #ifdef USE_X_TOOLKIT
9248 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9249 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9250 XtGetValues (f->output_data.x->widget, al, ac);
9251 size_hints.height = widget_height;
9252 size_hints.width = widget_width;
9253 #else /* not USE_X_TOOLKIT */
9254 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9255 size_hints.width = FRAME_PIXEL_WIDTH (f);
9256 #endif /* not USE_X_TOOLKIT */
9258 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9259 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9260 size_hints.max_width
9261 = FRAME_X_DISPLAY_INFO (f)->width - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9262 size_hints.max_height
9263 = FRAME_X_DISPLAY_INFO (f)->height - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9265 /* Calculate the base and minimum sizes.
9267 (When we use the X toolkit, we don't do it here.
9268 Instead we copy the values that the widgets are using, below.) */
9269 #ifndef USE_X_TOOLKIT
9271 int base_width, base_height;
9272 int min_rows = 0, min_cols = 0;
9274 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9275 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9277 check_frame_size (f, &min_rows, &min_cols);
9279 /* The window manager uses the base width hints to calculate the
9280 current number of rows and columns in the frame while
9281 resizing; min_width and min_height aren't useful for this
9282 purpose, since they might not give the dimensions for a
9283 zero-row, zero-column frame.
9285 We use the base_width and base_height members if we have
9286 them; otherwise, we set the min_width and min_height members
9287 to the size for a zero x zero frame. */
9289 #ifdef HAVE_X11R4
9290 size_hints.flags |= PBaseSize;
9291 size_hints.base_width = base_width;
9292 size_hints.base_height = base_height;
9293 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9294 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9295 #else
9296 size_hints.min_width = base_width;
9297 size_hints.min_height = base_height;
9298 #endif
9301 /* If we don't need the old flags, we don't need the old hint at all. */
9302 if (flags)
9304 size_hints.flags |= flags;
9305 goto no_read;
9307 #endif /* not USE_X_TOOLKIT */
9310 XSizeHints hints; /* Sometimes I hate X Windows... */
9311 long supplied_return;
9312 int value;
9314 #ifdef HAVE_X11R4
9315 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9316 &supplied_return);
9317 #else
9318 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
9319 #endif
9321 #ifdef USE_X_TOOLKIT
9322 size_hints.base_height = hints.base_height;
9323 size_hints.base_width = hints.base_width;
9324 size_hints.min_height = hints.min_height;
9325 size_hints.min_width = hints.min_width;
9326 #endif
9328 if (flags)
9329 size_hints.flags |= flags;
9330 else
9332 if (value == 0)
9333 hints.flags = 0;
9334 if (hints.flags & PSize)
9335 size_hints.flags |= PSize;
9336 if (hints.flags & PPosition)
9337 size_hints.flags |= PPosition;
9338 if (hints.flags & USPosition)
9339 size_hints.flags |= USPosition;
9340 if (hints.flags & USSize)
9341 size_hints.flags |= USSize;
9345 #ifndef USE_X_TOOLKIT
9346 no_read:
9347 #endif
9349 #ifdef PWinGravity
9350 size_hints.win_gravity = f->win_gravity;
9351 size_hints.flags |= PWinGravity;
9353 if (user_position)
9355 size_hints.flags &= ~ PPosition;
9356 size_hints.flags |= USPosition;
9358 #endif /* PWinGravity */
9360 #ifdef HAVE_X11R4
9361 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9362 #else
9363 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9364 #endif
9366 #endif /* not USE_GTK */
9368 /* Used for IconicState or NormalState */
9370 void
9371 x_wm_set_window_state (f, state)
9372 struct frame *f;
9373 int state;
9375 #ifdef USE_X_TOOLKIT
9376 Arg al[1];
9378 XtSetArg (al[0], XtNinitialState, state);
9379 XtSetValues (f->output_data.x->widget, al, 1);
9380 #else /* not USE_X_TOOLKIT */
9381 Window window = FRAME_X_WINDOW (f);
9383 f->output_data.x->wm_hints.flags |= StateHint;
9384 f->output_data.x->wm_hints.initial_state = state;
9386 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9387 #endif /* not USE_X_TOOLKIT */
9390 void
9391 x_wm_set_icon_pixmap (f, pixmap_id)
9392 struct frame *f;
9393 int pixmap_id;
9395 Pixmap icon_pixmap, icon_mask;
9397 #ifndef USE_X_TOOLKIT
9398 Window window = FRAME_OUTER_WINDOW (f);
9399 #endif
9401 if (pixmap_id > 0)
9403 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9404 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9405 icon_mask = x_bitmap_mask (f, pixmap_id);
9406 f->output_data.x->wm_hints.icon_mask = icon_mask;
9408 else
9410 /* It seems there is no way to turn off use of an icon pixmap.
9411 The following line does it, only if no icon has yet been created,
9412 for some window managers. But with mwm it crashes.
9413 Some people say it should clear the IconPixmapHint bit in this case,
9414 but that doesn't work, and the X consortium said it isn't the
9415 right thing at all. Since there is no way to win,
9416 best to explicitly give up. */
9417 #if 0
9418 f->output_data.x->wm_hints.icon_pixmap = None;
9419 f->output_data.x->wm_hints.icon_mask = None;
9420 #else
9421 return;
9422 #endif
9425 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
9428 Arg al[1];
9429 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9430 XtSetValues (f->output_data.x->widget, al, 1);
9431 XtSetArg (al[0], XtNiconMask, icon_mask);
9432 XtSetValues (f->output_data.x->widget, al, 1);
9435 #else /* not USE_X_TOOLKIT */
9437 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9438 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9440 #endif /* not USE_X_TOOLKIT */
9443 void
9444 x_wm_set_icon_position (f, icon_x, icon_y)
9445 struct frame *f;
9446 int icon_x, icon_y;
9448 Window window = FRAME_OUTER_WINDOW (f);
9450 f->output_data.x->wm_hints.flags |= IconPositionHint;
9451 f->output_data.x->wm_hints.icon_x = icon_x;
9452 f->output_data.x->wm_hints.icon_y = icon_y;
9454 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9458 /***********************************************************************
9459 Fonts
9460 ***********************************************************************/
9462 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
9464 struct font_info *
9465 x_get_font_info (f, font_idx)
9466 FRAME_PTR f;
9467 int font_idx;
9469 return (FRAME_X_FONT_TABLE (f) + font_idx);
9473 /* Return a list of names of available fonts matching PATTERN on frame F.
9475 If SIZE is > 0, it is the size (maximum bounds width) of fonts
9476 to be listed.
9478 SIZE < 0 means include scalable fonts.
9480 Frame F null means we have not yet created any frame on X, and
9481 consult the first display in x_display_list. MAXNAMES sets a limit
9482 on how many fonts to match. */
9484 Lisp_Object
9485 x_list_fonts (f, pattern, size, maxnames)
9486 struct frame *f;
9487 Lisp_Object pattern;
9488 int size;
9489 int maxnames;
9491 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
9492 Lisp_Object tem, second_best;
9493 struct x_display_info *dpyinfo
9494 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
9495 Display *dpy = dpyinfo->display;
9496 int try_XLoadQueryFont = 0;
9497 int count;
9498 int allow_auto_scaled_font = 0;
9500 if (size < 0)
9502 allow_auto_scaled_font = 1;
9503 size = 0;
9506 patterns = Fassoc (pattern, Valternate_fontname_alist);
9507 if (NILP (patterns))
9508 patterns = Fcons (pattern, Qnil);
9510 if (maxnames == 1 && !size)
9511 /* We can return any single font matching PATTERN. */
9512 try_XLoadQueryFont = 1;
9514 for (; CONSP (patterns); patterns = XCDR (patterns))
9516 int num_fonts;
9517 char **names = NULL;
9519 pattern = XCAR (patterns);
9520 /* See if we cached the result for this particular query.
9521 The cache is an alist of the form:
9522 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
9523 tem = XCDR (dpyinfo->name_list_element);
9524 key = Fcons (Fcons (pattern, make_number (maxnames)),
9525 allow_auto_scaled_font ? Qt : Qnil);
9526 list = Fassoc (key, tem);
9527 if (!NILP (list))
9529 list = Fcdr_safe (list);
9530 /* We have a cashed list. Don't have to get the list again. */
9531 goto label_cached;
9534 /* At first, put PATTERN in the cache. */
9536 BLOCK_INPUT;
9537 count = x_catch_errors (dpy);
9539 if (try_XLoadQueryFont)
9541 XFontStruct *font;
9542 unsigned long value;
9544 font = XLoadQueryFont (dpy, SDATA (pattern));
9545 if (x_had_errors_p (dpy))
9547 /* This error is perhaps due to insufficient memory on X
9548 server. Let's just ignore it. */
9549 font = NULL;
9550 x_clear_errors (dpy);
9553 if (font
9554 && XGetFontProperty (font, XA_FONT, &value))
9556 char *name = (char *) XGetAtomName (dpy, (Atom) value);
9557 int len = strlen (name);
9558 char *tmp;
9560 /* If DXPC (a Differential X Protocol Compressor)
9561 Ver.3.7 is running, XGetAtomName will return null
9562 string. We must avoid such a name. */
9563 if (len == 0)
9564 try_XLoadQueryFont = 0;
9565 else
9567 num_fonts = 1;
9568 names = (char **) alloca (sizeof (char *));
9569 /* Some systems only allow alloca assigned to a
9570 simple var. */
9571 tmp = (char *) alloca (len + 1); names[0] = tmp;
9572 bcopy (name, names[0], len + 1);
9573 XFree (name);
9576 else
9577 try_XLoadQueryFont = 0;
9579 if (font)
9580 XFreeFont (dpy, font);
9583 if (!try_XLoadQueryFont)
9585 /* We try at least 10 fonts because XListFonts will return
9586 auto-scaled fonts at the head. */
9587 if (maxnames < 0)
9589 int limit;
9591 for (limit = 500;;)
9593 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
9594 if (num_fonts == limit)
9596 BLOCK_INPUT;
9597 XFreeFontNames (names);
9598 UNBLOCK_INPUT;
9599 limit *= 2;
9601 else
9602 break;
9605 else
9606 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
9607 &num_fonts);
9609 if (x_had_errors_p (dpy))
9611 /* This error is perhaps due to insufficient memory on X
9612 server. Let's just ignore it. */
9613 names = NULL;
9614 x_clear_errors (dpy);
9618 x_uncatch_errors (dpy, count);
9619 UNBLOCK_INPUT;
9621 if (names)
9623 int i;
9625 /* Make a list of all the fonts we got back.
9626 Store that in the font cache for the display. */
9627 for (i = 0; i < num_fonts; i++)
9629 int width = 0;
9630 char *p = names[i];
9631 int average_width = -1, resx = 0, dashes = 0;
9633 /* Count the number of dashes in NAMES[I]. If there are
9634 14 dashes, the field value following 9th dash
9635 (RESOLUTION_X) is nonzero, and the field value
9636 following 12th dash (AVERAGE_WIDTH) is 0, this is a
9637 auto-scaled font which is usually too ugly to be used
9638 for editing. Let's ignore it. */
9639 while (*p)
9640 if (*p++ == '-')
9642 dashes++;
9643 if (dashes == 7) /* PIXEL_SIZE field */
9644 width = atoi (p);
9645 else if (dashes == 9)
9646 resx = atoi (p);
9647 else if (dashes == 12) /* AVERAGE_WIDTH field */
9648 average_width = atoi (p);
9651 if (allow_auto_scaled_font
9652 || dashes < 14 || average_width != 0 || resx == 0)
9654 tem = build_string (names[i]);
9655 if (NILP (Fassoc (tem, list)))
9657 if (STRINGP (Vx_pixel_size_width_font_regexp)
9658 && ((fast_c_string_match_ignore_case
9659 (Vx_pixel_size_width_font_regexp, names[i]))
9660 >= 0))
9661 /* We can set the value of PIXEL_SIZE to the
9662 width of this font. */
9663 list = Fcons (Fcons (tem, make_number (width)), list);
9664 else
9665 /* For the moment, width is not known. */
9666 list = Fcons (Fcons (tem, Qnil), list);
9671 if (!try_XLoadQueryFont)
9673 BLOCK_INPUT;
9674 XFreeFontNames (names);
9675 UNBLOCK_INPUT;
9679 /* Now store the result in the cache. */
9680 XSETCDR (dpyinfo->name_list_element,
9681 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
9683 label_cached:
9684 if (NILP (list)) continue; /* Try the remaining alternatives. */
9686 newlist = second_best = Qnil;
9687 /* Make a list of the fonts that have the right width. */
9688 for (; CONSP (list); list = XCDR (list))
9690 int found_size;
9692 tem = XCAR (list);
9694 if (!CONSP (tem) || NILP (XCAR (tem)))
9695 continue;
9696 if (!size)
9698 newlist = Fcons (XCAR (tem), newlist);
9699 continue;
9702 if (!INTEGERP (XCDR (tem)))
9704 /* Since we have not yet known the size of this font, we
9705 must try slow function call XLoadQueryFont. */
9706 XFontStruct *thisinfo;
9708 BLOCK_INPUT;
9709 count = x_catch_errors (dpy);
9710 thisinfo = XLoadQueryFont (dpy,
9711 SDATA (XCAR (tem)));
9712 if (x_had_errors_p (dpy))
9714 /* This error is perhaps due to insufficient memory on X
9715 server. Let's just ignore it. */
9716 thisinfo = NULL;
9717 x_clear_errors (dpy);
9719 x_uncatch_errors (dpy, count);
9720 UNBLOCK_INPUT;
9722 if (thisinfo)
9724 XSETCDR (tem,
9725 (thisinfo->min_bounds.width == 0
9726 ? make_number (0)
9727 : make_number (thisinfo->max_bounds.width)));
9728 BLOCK_INPUT;
9729 XFreeFont (dpy, thisinfo);
9730 UNBLOCK_INPUT;
9732 else
9733 /* For unknown reason, the previous call of XListFont had
9734 returned a font which can't be opened. Record the size
9735 as 0 not to try to open it again. */
9736 XSETCDR (tem, make_number (0));
9739 found_size = XINT (XCDR (tem));
9740 if (found_size == size)
9741 newlist = Fcons (XCAR (tem), newlist);
9742 else if (found_size > 0)
9744 if (NILP (second_best))
9745 second_best = tem;
9746 else if (found_size < size)
9748 if (XINT (XCDR (second_best)) > size
9749 || XINT (XCDR (second_best)) < found_size)
9750 second_best = tem;
9752 else
9754 if (XINT (XCDR (second_best)) > size
9755 && XINT (XCDR (second_best)) > found_size)
9756 second_best = tem;
9760 if (!NILP (newlist))
9761 break;
9762 else if (!NILP (second_best))
9764 newlist = Fcons (XCAR (second_best), Qnil);
9765 break;
9769 return newlist;
9773 #if GLYPH_DEBUG
9775 /* Check that FONT is valid on frame F. It is if it can be found in F's
9776 font table. */
9778 static void
9779 x_check_font (f, font)
9780 struct frame *f;
9781 XFontStruct *font;
9783 int i;
9784 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9786 xassert (font != NULL);
9788 for (i = 0; i < dpyinfo->n_fonts; i++)
9789 if (dpyinfo->font_table[i].name
9790 && font == dpyinfo->font_table[i].font)
9791 break;
9793 xassert (i < dpyinfo->n_fonts);
9796 #endif /* GLYPH_DEBUG != 0 */
9798 /* Set *W to the minimum width, *H to the minimum font height of FONT.
9799 Note: There are (broken) X fonts out there with invalid XFontStruct
9800 min_bounds contents. For example, handa@etl.go.jp reports that
9801 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
9802 have font->min_bounds.width == 0. */
9804 static INLINE void
9805 x_font_min_bounds (font, w, h)
9806 XFontStruct *font;
9807 int *w, *h;
9809 *h = FONT_HEIGHT (font);
9810 *w = font->min_bounds.width;
9812 /* Try to handle the case where FONT->min_bounds has invalid
9813 contents. Since the only font known to have invalid min_bounds
9814 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
9815 if (*w <= 0)
9816 *w = font->max_bounds.width;
9820 /* Compute the smallest character width and smallest font height over
9821 all fonts available on frame F. Set the members smallest_char_width
9822 and smallest_font_height in F's x_display_info structure to
9823 the values computed. Value is non-zero if smallest_font_height or
9824 smallest_char_width become smaller than they were before. */
9826 static int
9827 x_compute_min_glyph_bounds (f)
9828 struct frame *f;
9830 int i;
9831 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9832 XFontStruct *font;
9833 int old_width = dpyinfo->smallest_char_width;
9834 int old_height = dpyinfo->smallest_font_height;
9836 dpyinfo->smallest_font_height = 100000;
9837 dpyinfo->smallest_char_width = 100000;
9839 for (i = 0; i < dpyinfo->n_fonts; ++i)
9840 if (dpyinfo->font_table[i].name)
9842 struct font_info *fontp = dpyinfo->font_table + i;
9843 int w, h;
9845 font = (XFontStruct *) fontp->font;
9846 xassert (font != (XFontStruct *) ~0);
9847 x_font_min_bounds (font, &w, &h);
9849 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
9850 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
9853 xassert (dpyinfo->smallest_char_width > 0
9854 && dpyinfo->smallest_font_height > 0);
9856 return (dpyinfo->n_fonts == 1
9857 || dpyinfo->smallest_char_width < old_width
9858 || dpyinfo->smallest_font_height < old_height);
9862 /* Load font named FONTNAME of the size SIZE for frame F, and return a
9863 pointer to the structure font_info while allocating it dynamically.
9864 If SIZE is 0, load any size of font.
9865 If loading is failed, return NULL. */
9867 struct font_info *
9868 x_load_font (f, fontname, size)
9869 struct frame *f;
9870 register char *fontname;
9871 int size;
9873 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9874 Lisp_Object font_names;
9875 int count;
9877 /* Get a list of all the fonts that match this name. Once we
9878 have a list of matching fonts, we compare them against the fonts
9879 we already have by comparing names. */
9880 font_names = x_list_fonts (f, build_string (fontname), size, 1);
9882 if (!NILP (font_names))
9884 Lisp_Object tail;
9885 int i;
9887 for (i = 0; i < dpyinfo->n_fonts; i++)
9888 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
9889 if (dpyinfo->font_table[i].name
9890 && (!strcmp (dpyinfo->font_table[i].name,
9891 SDATA (XCAR (tail)))
9892 || !strcmp (dpyinfo->font_table[i].full_name,
9893 SDATA (XCAR (tail)))))
9894 return (dpyinfo->font_table + i);
9897 /* Load the font and add it to the table. */
9899 char *full_name;
9900 XFontStruct *font;
9901 struct font_info *fontp;
9902 unsigned long value;
9903 int i;
9905 /* If we have found fonts by x_list_font, load one of them. If
9906 not, we still try to load a font by the name given as FONTNAME
9907 because XListFonts (called in x_list_font) of some X server has
9908 a bug of not finding a font even if the font surely exists and
9909 is loadable by XLoadQueryFont. */
9910 if (size > 0 && !NILP (font_names))
9911 fontname = (char *) SDATA (XCAR (font_names));
9913 BLOCK_INPUT;
9914 count = x_catch_errors (FRAME_X_DISPLAY (f));
9915 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
9916 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
9918 /* This error is perhaps due to insufficient memory on X
9919 server. Let's just ignore it. */
9920 font = NULL;
9921 x_clear_errors (FRAME_X_DISPLAY (f));
9923 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
9924 UNBLOCK_INPUT;
9925 if (!font)
9926 return NULL;
9928 /* Find a free slot in the font table. */
9929 for (i = 0; i < dpyinfo->n_fonts; ++i)
9930 if (dpyinfo->font_table[i].name == NULL)
9931 break;
9933 /* If no free slot found, maybe enlarge the font table. */
9934 if (i == dpyinfo->n_fonts
9935 && dpyinfo->n_fonts == dpyinfo->font_table_size)
9937 int sz;
9938 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
9939 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
9940 dpyinfo->font_table
9941 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
9944 fontp = dpyinfo->font_table + i;
9945 if (i == dpyinfo->n_fonts)
9946 ++dpyinfo->n_fonts;
9948 /* Now fill in the slots of *FONTP. */
9949 BLOCK_INPUT;
9950 bzero (fontp, sizeof (*fontp));
9951 fontp->font = font;
9952 fontp->font_idx = i;
9953 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
9954 bcopy (fontname, fontp->name, strlen (fontname) + 1);
9956 /* Try to get the full name of FONT. Put it in FULL_NAME. */
9957 full_name = 0;
9958 if (XGetFontProperty (font, XA_FONT, &value))
9960 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
9961 char *p = name;
9962 int dashes = 0;
9964 /* Count the number of dashes in the "full name".
9965 If it is too few, this isn't really the font's full name,
9966 so don't use it.
9967 In X11R4, the fonts did not come with their canonical names
9968 stored in them. */
9969 while (*p)
9971 if (*p == '-')
9972 dashes++;
9973 p++;
9976 if (dashes >= 13)
9978 full_name = (char *) xmalloc (p - name + 1);
9979 bcopy (name, full_name, p - name + 1);
9982 XFree (name);
9985 if (full_name != 0)
9986 fontp->full_name = full_name;
9987 else
9988 fontp->full_name = fontp->name;
9990 fontp->size = font->max_bounds.width;
9991 fontp->height = FONT_HEIGHT (font);
9993 if (NILP (font_names))
9995 /* We come here because of a bug of XListFonts mentioned at
9996 the head of this block. Let's store this information in
9997 the cache for x_list_fonts. */
9998 Lisp_Object lispy_name = build_string (fontname);
9999 Lisp_Object lispy_full_name = build_string (fontp->full_name);
10000 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
10001 Qnil);
10003 XSETCDR (dpyinfo->name_list_element,
10004 Fcons (Fcons (key,
10005 Fcons (Fcons (lispy_full_name,
10006 make_number (fontp->size)),
10007 Qnil)),
10008 XCDR (dpyinfo->name_list_element)));
10009 if (full_name)
10011 key = Fcons (Fcons (lispy_full_name, make_number (256)),
10012 Qnil);
10013 XSETCDR (dpyinfo->name_list_element,
10014 Fcons (Fcons (key,
10015 Fcons (Fcons (lispy_full_name,
10016 make_number (fontp->size)),
10017 Qnil)),
10018 XCDR (dpyinfo->name_list_element)));
10022 /* The slot `encoding' specifies how to map a character
10023 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
10024 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
10025 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
10026 2:0xA020..0xFF7F). For the moment, we don't know which charset
10027 uses this font. So, we set information in fontp->encoding[1]
10028 which is never used by any charset. If mapping can't be
10029 decided, set FONT_ENCODING_NOT_DECIDED. */
10030 fontp->encoding[1]
10031 = (font->max_byte1 == 0
10032 /* 1-byte font */
10033 ? (font->min_char_or_byte2 < 0x80
10034 ? (font->max_char_or_byte2 < 0x80
10035 ? 0 /* 0x20..0x7F */
10036 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
10037 : 1) /* 0xA0..0xFF */
10038 /* 2-byte font */
10039 : (font->min_byte1 < 0x80
10040 ? (font->max_byte1 < 0x80
10041 ? (font->min_char_or_byte2 < 0x80
10042 ? (font->max_char_or_byte2 < 0x80
10043 ? 0 /* 0x2020..0x7F7F */
10044 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
10045 : 3) /* 0x20A0..0x7FFF */
10046 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
10047 : (font->min_char_or_byte2 < 0x80
10048 ? (font->max_char_or_byte2 < 0x80
10049 ? 2 /* 0xA020..0xFF7F */
10050 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
10051 : 1))); /* 0xA0A0..0xFFFF */
10053 fontp->baseline_offset
10054 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
10055 ? (long) value : 0);
10056 fontp->relative_compose
10057 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
10058 ? (long) value : 0);
10059 fontp->default_ascent
10060 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
10061 ? (long) value : 0);
10063 /* Set global flag fonts_changed_p to non-zero if the font loaded
10064 has a character with a smaller width than any other character
10065 before, or if the font loaded has a smaller height than any
10066 other font loaded before. If this happens, it will make a
10067 glyph matrix reallocation necessary. */
10068 fonts_changed_p |= x_compute_min_glyph_bounds (f);
10069 UNBLOCK_INPUT;
10070 return fontp;
10075 /* Return a pointer to struct font_info of a font named FONTNAME for
10076 frame F. If no such font is loaded, return NULL. */
10078 struct font_info *
10079 x_query_font (f, fontname)
10080 struct frame *f;
10081 register char *fontname;
10083 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10084 int i;
10086 for (i = 0; i < dpyinfo->n_fonts; i++)
10087 if (dpyinfo->font_table[i].name
10088 && (!strcmp (dpyinfo->font_table[i].name, fontname)
10089 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
10090 return (dpyinfo->font_table + i);
10091 return NULL;
10095 /* Find a CCL program for a font specified by FONTP, and set the member
10096 `encoder' of the structure. */
10098 void
10099 x_find_ccl_program (fontp)
10100 struct font_info *fontp;
10102 Lisp_Object list, elt;
10104 elt = Qnil;
10105 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
10107 elt = XCAR (list);
10108 if (CONSP (elt)
10109 && STRINGP (XCAR (elt))
10110 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10111 >= 0)
10112 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10113 >= 0)))
10114 break;
10117 if (! NILP (list))
10119 struct ccl_program *ccl
10120 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
10122 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
10123 xfree (ccl);
10124 else
10125 fontp->font_encoder = ccl;
10131 /***********************************************************************
10132 Initialization
10133 ***********************************************************************/
10135 #ifdef USE_X_TOOLKIT
10136 static XrmOptionDescRec emacs_options[] = {
10137 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10138 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10140 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10141 XrmoptionSepArg, NULL},
10142 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10144 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10145 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10146 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10147 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10148 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10149 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10150 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10152 #endif /* USE_X_TOOLKIT */
10154 static int x_initialized;
10156 #ifdef MULTI_KBOARD
10157 /* Test whether two display-name strings agree up to the dot that separates
10158 the screen number from the server number. */
10159 static int
10160 same_x_server (name1, name2)
10161 const char *name1, *name2;
10163 int seen_colon = 0;
10164 const unsigned char *system_name = SDATA (Vsystem_name);
10165 int system_name_length = strlen (system_name);
10166 int length_until_period = 0;
10168 while (system_name[length_until_period] != 0
10169 && system_name[length_until_period] != '.')
10170 length_until_period++;
10172 /* Treat `unix' like an empty host name. */
10173 if (! strncmp (name1, "unix:", 5))
10174 name1 += 4;
10175 if (! strncmp (name2, "unix:", 5))
10176 name2 += 4;
10177 /* Treat this host's name like an empty host name. */
10178 if (! strncmp (name1, system_name, system_name_length)
10179 && name1[system_name_length] == ':')
10180 name1 += system_name_length;
10181 if (! strncmp (name2, system_name, system_name_length)
10182 && name2[system_name_length] == ':')
10183 name2 += system_name_length;
10184 /* Treat this host's domainless name like an empty host name. */
10185 if (! strncmp (name1, system_name, length_until_period)
10186 && name1[length_until_period] == ':')
10187 name1 += length_until_period;
10188 if (! strncmp (name2, system_name, length_until_period)
10189 && name2[length_until_period] == ':')
10190 name2 += length_until_period;
10192 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10194 if (*name1 == ':')
10195 seen_colon++;
10196 if (seen_colon && *name1 == '.')
10197 return 1;
10199 return (seen_colon
10200 && (*name1 == '.' || *name1 == '\0')
10201 && (*name2 == '.' || *name2 == '\0'));
10203 #endif
10205 /* Count number of set bits in mask and number of bits to shift to
10206 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10207 to 5. */
10208 static void
10209 get_bits_and_offset (mask, bits, offset)
10210 unsigned long mask;
10211 int *bits;
10212 int *offset;
10214 int nr = 0;
10215 int off = 0;
10217 while (!(mask & 1))
10219 off++;
10220 mask >>= 1;
10223 while (mask & 1)
10225 nr++;
10226 mask >>= 1;
10229 *offset = off;
10230 *bits = nr;
10233 struct x_display_info *
10234 x_term_init (display_name, xrm_option, resource_name)
10235 Lisp_Object display_name;
10236 char *xrm_option;
10237 char *resource_name;
10239 int connection;
10240 Display *dpy;
10241 struct x_display_info *dpyinfo;
10242 XrmDatabase xrdb;
10244 BLOCK_INPUT;
10246 if (!x_initialized)
10248 x_initialize ();
10249 ++x_initialized;
10252 #ifdef USE_GTK
10254 #define NUM_ARGV 10
10255 int argc;
10256 char *argv[NUM_ARGV];
10257 char **argv2 = argv;
10258 GdkAtom atom;
10260 if (x_initialized++ > 1)
10262 /* Opening another display. If xg_display_open returns less
10263 than zero, we are probably on GTK 2.0, which can only handle
10264 one display. GTK 2.2 or later can handle more than one. */
10265 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10266 error ("Sorry, this version of GTK can only handle one display");
10268 else
10270 for (argc = 0; argc < NUM_ARGV; ++argc)
10271 argv[argc] = 0;
10273 argc = 0;
10274 argv[argc++] = initial_argv[0];
10276 if (! NILP (display_name))
10278 argv[argc++] = "--display";
10279 argv[argc++] = SDATA (display_name);
10282 argv[argc++] = "--name";
10283 argv[argc++] = resource_name;
10285 #ifdef HAVE_X11R5
10286 XSetLocaleModifiers ("");
10287 #endif
10289 gtk_init (&argc, &argv2);
10291 /* gtk_init does set_locale. We must fix locale after calling it. */
10292 fixup_locale ();
10293 xg_initialize ();
10295 dpy = GDK_DISPLAY ();
10297 /* NULL window -> events for all windows go to our function */
10298 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10300 /* Load our own gtkrc if it exists. */
10302 struct gcpro gcpro1, gcpro2;
10303 char *file = "~/.emacs.d/gtkrc";
10304 Lisp_Object s, abs_file;
10306 GCPRO2 (s, abs_file);
10307 s = make_string (file, strlen (file));
10308 abs_file = Fexpand_file_name (s, Qnil);
10310 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10311 gtk_rc_parse (SDATA (abs_file));
10313 UNGCPRO;
10316 XSetErrorHandler (x_error_handler);
10317 XSetIOErrorHandler (x_io_error_quitter);
10320 #else /* not USE_GTK */
10321 #ifdef USE_X_TOOLKIT
10322 /* weiner@footloose.sps.mot.com reports that this causes
10323 errors with X11R5:
10324 X protocol error: BadAtom (invalid Atom parameter)
10325 on protocol request 18skiloaf.
10326 So let's not use it until R6. */
10327 #ifdef HAVE_X11XTR6
10328 XtSetLanguageProc (NULL, NULL, NULL);
10329 #endif
10332 int argc = 0;
10333 char *argv[3];
10335 argv[0] = "";
10336 argc = 1;
10337 if (xrm_option)
10339 argv[argc++] = "-xrm";
10340 argv[argc++] = xrm_option;
10342 turn_on_atimers (0);
10343 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10344 resource_name, EMACS_CLASS,
10345 emacs_options, XtNumber (emacs_options),
10346 &argc, argv);
10347 turn_on_atimers (1);
10349 #ifdef HAVE_X11XTR6
10350 /* I think this is to compensate for XtSetLanguageProc. */
10351 fixup_locale ();
10352 #endif
10355 #else /* not USE_X_TOOLKIT */
10356 #ifdef HAVE_X11R5
10357 XSetLocaleModifiers ("");
10358 #endif
10359 dpy = XOpenDisplay (SDATA (display_name));
10360 #endif /* not USE_X_TOOLKIT */
10361 #endif /* not USE_GTK*/
10363 /* Detect failure. */
10364 if (dpy == 0)
10366 UNBLOCK_INPUT;
10367 return 0;
10370 /* We have definitely succeeded. Record the new connection. */
10372 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10373 bzero (dpyinfo, sizeof *dpyinfo);
10375 #ifdef MULTI_KBOARD
10377 struct x_display_info *share;
10378 Lisp_Object tail;
10380 for (share = x_display_list, tail = x_display_name_list; share;
10381 share = share->next, tail = XCDR (tail))
10382 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10383 SDATA (display_name)))
10384 break;
10385 if (share)
10386 dpyinfo->kboard = share->kboard;
10387 else
10389 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10390 init_kboard (dpyinfo->kboard);
10391 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10393 char *vendor = ServerVendor (dpy);
10394 UNBLOCK_INPUT;
10395 dpyinfo->kboard->Vsystem_key_alist
10396 = call1 (Qvendor_specific_keysyms,
10397 build_string (vendor ? vendor : ""));
10398 BLOCK_INPUT;
10401 dpyinfo->kboard->next_kboard = all_kboards;
10402 all_kboards = dpyinfo->kboard;
10403 /* Don't let the initial kboard remain current longer than necessary.
10404 That would cause problems if a file loaded on startup tries to
10405 prompt in the mini-buffer. */
10406 if (current_kboard == initial_kboard)
10407 current_kboard = dpyinfo->kboard;
10409 dpyinfo->kboard->reference_count++;
10411 #endif
10413 /* Put this display on the chain. */
10414 dpyinfo->next = x_display_list;
10415 x_display_list = dpyinfo;
10417 /* Put it on x_display_name_list as well, to keep them parallel. */
10418 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10419 x_display_name_list);
10420 dpyinfo->name_list_element = XCAR (x_display_name_list);
10422 dpyinfo->display = dpy;
10424 #if 0
10425 XSetAfterFunction (x_current_display, x_trace_wire);
10426 #endif /* ! 0 */
10428 dpyinfo->x_id_name
10429 = (char *) xmalloc (SBYTES (Vinvocation_name)
10430 + SBYTES (Vsystem_name)
10431 + 2);
10432 sprintf (dpyinfo->x_id_name, "%s@%s",
10433 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10435 /* Figure out which modifier bits mean what. */
10436 x_find_modifier_meanings (dpyinfo);
10438 /* Get the scroll bar cursor. */
10439 #ifdef USE_GTK
10440 /* We must create a GTK cursor, it is required for GTK widgets. */
10441 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10442 #endif /* USE_GTK */
10444 dpyinfo->vertical_scroll_bar_cursor
10445 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10447 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10448 resource_name, EMACS_CLASS);
10449 #ifdef HAVE_XRMSETDATABASE
10450 XrmSetDatabase (dpyinfo->display, xrdb);
10451 #else
10452 dpyinfo->display->db = xrdb;
10453 #endif
10454 /* Put the rdb where we can find it in a way that works on
10455 all versions. */
10456 dpyinfo->xrdb = xrdb;
10458 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10459 DefaultScreen (dpyinfo->display));
10460 select_visual (dpyinfo);
10461 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10462 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
10463 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
10464 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10465 dpyinfo->client_leader_window = 0;
10466 dpyinfo->grabbed = 0;
10467 dpyinfo->reference_count = 0;
10468 dpyinfo->icon_bitmap_id = -1;
10469 dpyinfo->font_table = NULL;
10470 dpyinfo->n_fonts = 0;
10471 dpyinfo->font_table_size = 0;
10472 dpyinfo->bitmaps = 0;
10473 dpyinfo->bitmaps_size = 0;
10474 dpyinfo->bitmaps_last = 0;
10475 dpyinfo->scratch_cursor_gc = 0;
10476 dpyinfo->mouse_face_mouse_frame = 0;
10477 dpyinfo->mouse_face_deferred_gc = 0;
10478 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10479 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10480 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10481 dpyinfo->mouse_face_window = Qnil;
10482 dpyinfo->mouse_face_overlay = Qnil;
10483 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10484 dpyinfo->mouse_face_defer = 0;
10485 dpyinfo->mouse_face_hidden = 0;
10486 dpyinfo->x_focus_frame = 0;
10487 dpyinfo->x_focus_event_frame = 0;
10488 dpyinfo->x_highlight_frame = 0;
10489 dpyinfo->image_cache = make_image_cache ();
10490 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10492 /* See if we can construct pixel values from RGB values. */
10493 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10494 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10496 if (dpyinfo->visual->class == TrueColor)
10498 get_bits_and_offset (dpyinfo->visual->red_mask,
10499 &dpyinfo->red_bits, &dpyinfo->red_offset);
10500 get_bits_and_offset (dpyinfo->visual->blue_mask,
10501 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10502 get_bits_and_offset (dpyinfo->visual->green_mask,
10503 &dpyinfo->green_bits, &dpyinfo->green_offset);
10506 /* See if a private colormap is requested. */
10507 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10509 if (dpyinfo->visual->class == PseudoColor)
10511 Lisp_Object value;
10512 value = display_x_get_resource (dpyinfo,
10513 build_string ("privateColormap"),
10514 build_string ("PrivateColormap"),
10515 Qnil, Qnil);
10516 if (STRINGP (value)
10517 && (!strcmp (SDATA (value), "true")
10518 || !strcmp (SDATA (value), "on")))
10519 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10522 else
10523 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10524 dpyinfo->visual, AllocNone);
10527 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10528 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10529 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10530 dpyinfo->resy = pixels * 25.4 / mm;
10531 pixels = DisplayWidth (dpyinfo->display, screen_number);
10532 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10533 dpyinfo->resx = pixels * 25.4 / mm;
10536 dpyinfo->Xatom_wm_protocols
10537 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10538 dpyinfo->Xatom_wm_take_focus
10539 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10540 dpyinfo->Xatom_wm_save_yourself
10541 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10542 dpyinfo->Xatom_wm_delete_window
10543 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10544 dpyinfo->Xatom_wm_change_state
10545 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10546 dpyinfo->Xatom_wm_configure_denied
10547 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10548 dpyinfo->Xatom_wm_window_moved
10549 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10550 dpyinfo->Xatom_wm_client_leader
10551 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10552 dpyinfo->Xatom_editres
10553 = XInternAtom (dpyinfo->display, "Editres", False);
10554 dpyinfo->Xatom_CLIPBOARD
10555 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10556 dpyinfo->Xatom_TIMESTAMP
10557 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10558 dpyinfo->Xatom_TEXT
10559 = XInternAtom (dpyinfo->display, "TEXT", False);
10560 dpyinfo->Xatom_COMPOUND_TEXT
10561 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10562 dpyinfo->Xatom_UTF8_STRING
10563 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10564 dpyinfo->Xatom_DELETE
10565 = XInternAtom (dpyinfo->display, "DELETE", False);
10566 dpyinfo->Xatom_MULTIPLE
10567 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10568 dpyinfo->Xatom_INCR
10569 = XInternAtom (dpyinfo->display, "INCR", False);
10570 dpyinfo->Xatom_EMACS_TMP
10571 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10572 dpyinfo->Xatom_TARGETS
10573 = XInternAtom (dpyinfo->display, "TARGETS", False);
10574 dpyinfo->Xatom_NULL
10575 = XInternAtom (dpyinfo->display, "NULL", False);
10576 dpyinfo->Xatom_ATOM_PAIR
10577 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10578 /* For properties of font. */
10579 dpyinfo->Xatom_PIXEL_SIZE
10580 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10581 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10582 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10583 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10584 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10585 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10586 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10588 /* Ghostscript support. */
10589 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10590 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10592 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10593 False);
10595 dpyinfo->cut_buffers_initialized = 0;
10597 connection = ConnectionNumber (dpyinfo->display);
10598 dpyinfo->connection = connection;
10601 char null_bits[1];
10603 null_bits[0] = 0x00;
10605 dpyinfo->null_pixel
10606 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10607 null_bits, 1, 1, (long) 0, (long) 0,
10612 extern int gray_bitmap_width, gray_bitmap_height;
10613 extern char *gray_bitmap_bits;
10614 dpyinfo->gray
10615 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10616 gray_bitmap_bits,
10617 gray_bitmap_width, gray_bitmap_height,
10618 (unsigned long) 1, (unsigned long) 0, 1);
10621 #ifdef HAVE_X_I18N
10622 xim_initialize (dpyinfo, resource_name);
10623 #endif
10625 #ifdef subprocesses
10626 /* This is only needed for distinguishing keyboard and process input. */
10627 if (connection != 0)
10628 add_keyboard_wait_descriptor (connection);
10629 #endif
10631 #ifndef F_SETOWN_BUG
10632 #ifdef F_SETOWN
10633 #ifdef F_SETOWN_SOCK_NEG
10634 /* stdin is a socket here */
10635 fcntl (connection, F_SETOWN, -getpid ());
10636 #else /* ! defined (F_SETOWN_SOCK_NEG) */
10637 fcntl (connection, F_SETOWN, getpid ());
10638 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
10639 #endif /* ! defined (F_SETOWN) */
10640 #endif /* F_SETOWN_BUG */
10642 #ifdef SIGIO
10643 if (interrupt_input)
10644 init_sigio (connection);
10645 #endif /* ! defined (SIGIO) */
10647 #ifdef USE_LUCID
10648 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
10649 /* Make sure that we have a valid font for dialog boxes
10650 so that Xt does not crash. */
10652 Display *dpy = dpyinfo->display;
10653 XrmValue d, fr, to;
10654 Font font;
10655 int count;
10657 d.addr = (XPointer)&dpy;
10658 d.size = sizeof (Display *);
10659 fr.addr = XtDefaultFont;
10660 fr.size = sizeof (XtDefaultFont);
10661 to.size = sizeof (Font *);
10662 to.addr = (XPointer)&font;
10663 count = x_catch_errors (dpy);
10664 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10665 abort ();
10666 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10667 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10668 x_uncatch_errors (dpy, count);
10670 #endif
10671 #endif
10673 /* See if we should run in synchronous mode. This is useful
10674 for debugging X code. */
10676 Lisp_Object value;
10677 value = display_x_get_resource (dpyinfo,
10678 build_string ("synchronous"),
10679 build_string ("Synchronous"),
10680 Qnil, Qnil);
10681 if (STRINGP (value)
10682 && (!strcmp (SDATA (value), "true")
10683 || !strcmp (SDATA (value), "on")))
10684 XSynchronize (dpyinfo->display, True);
10688 Lisp_Object value;
10689 value = display_x_get_resource (dpyinfo,
10690 build_string ("useXIM"),
10691 build_string ("UseXIM"),
10692 Qnil, Qnil);
10693 #ifdef USE_XIM
10694 if (STRINGP (value)
10695 && (!strcmp (XSTRING (value)->data, "false")
10696 || !strcmp (XSTRING (value)->data, "off")))
10697 use_xim = 0;
10698 #else
10699 if (STRINGP (value)
10700 && (!strcmp (XSTRING (value)->data, "true")
10701 || !strcmp (XSTRING (value)->data, "on")))
10702 use_xim = 1;
10703 #endif
10706 #ifdef HAVE_X_SM
10707 /* Only do this for the first display. */
10708 if (x_initialized == 1)
10709 x_session_initialize (dpyinfo);
10710 #endif
10712 UNBLOCK_INPUT;
10714 return dpyinfo;
10717 /* Get rid of display DPYINFO, assuming all frames are already gone,
10718 and without sending any more commands to the X server. */
10720 void
10721 x_delete_display (dpyinfo)
10722 struct x_display_info *dpyinfo;
10724 int i;
10726 delete_keyboard_wait_descriptor (dpyinfo->connection);
10728 /* Discard this display from x_display_name_list and x_display_list.
10729 We can't use Fdelq because that can quit. */
10730 if (! NILP (x_display_name_list)
10731 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10732 x_display_name_list = XCDR (x_display_name_list);
10733 else
10735 Lisp_Object tail;
10737 tail = x_display_name_list;
10738 while (CONSP (tail) && CONSP (XCDR (tail)))
10740 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10742 XSETCDR (tail, XCDR (XCDR (tail)));
10743 break;
10745 tail = XCDR (tail);
10749 if (next_noop_dpyinfo == dpyinfo)
10750 next_noop_dpyinfo = dpyinfo->next;
10752 if (x_display_list == dpyinfo)
10753 x_display_list = dpyinfo->next;
10754 else
10756 struct x_display_info *tail;
10758 for (tail = x_display_list; tail; tail = tail->next)
10759 if (tail->next == dpyinfo)
10760 tail->next = tail->next->next;
10763 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
10764 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
10765 XrmDestroyDatabase (dpyinfo->xrdb);
10766 #endif
10767 #endif
10768 #ifdef MULTI_KBOARD
10769 if (--dpyinfo->kboard->reference_count == 0)
10770 delete_kboard (dpyinfo->kboard);
10771 #endif
10772 #ifdef HAVE_X_I18N
10773 if (dpyinfo->xim)
10774 xim_close_dpy (dpyinfo);
10775 #endif
10777 /* Free the font names in the font table. */
10778 for (i = 0; i < dpyinfo->n_fonts; i++)
10779 if (dpyinfo->font_table[i].name)
10781 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
10782 xfree (dpyinfo->font_table[i].full_name);
10783 xfree (dpyinfo->font_table[i].name);
10786 if (dpyinfo->font_table->font_encoder)
10787 xfree (dpyinfo->font_table->font_encoder);
10789 xfree (dpyinfo->font_table);
10790 xfree (dpyinfo->x_id_name);
10791 xfree (dpyinfo->color_cells);
10792 xfree (dpyinfo);
10795 #ifdef USE_X_TOOLKIT
10797 /* Atimer callback function for TIMER. Called every 0.1s to process
10798 Xt timeouts, if needed. We must avoid calling XtAppPending as
10799 much as possible because that function does an implicit XFlush
10800 that slows us down. */
10802 static void
10803 x_process_timeouts (timer)
10804 struct atimer *timer;
10806 if (toolkit_scroll_bar_interaction || popup_activated ())
10808 BLOCK_INPUT;
10809 while (XtAppPending (Xt_app_con) & XtIMTimer)
10810 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10811 UNBLOCK_INPUT;
10815 #endif /* USE_X_TOOLKIT */
10818 /* Set up use of X before we make the first connection. */
10820 extern frame_parm_handler x_frame_parm_handlers[];
10822 static struct redisplay_interface x_redisplay_interface =
10824 x_frame_parm_handlers,
10825 x_produce_glyphs,
10826 x_write_glyphs,
10827 x_insert_glyphs,
10828 x_clear_end_of_line,
10829 x_scroll_run,
10830 x_after_update_window_line,
10831 x_update_window_begin,
10832 x_update_window_end,
10833 x_cursor_to,
10834 x_flush,
10835 #ifndef XFlush
10836 x_flush,
10837 #else
10838 0, /* flush_display_optional */
10839 #endif
10840 x_clear_window_mouse_face,
10841 x_get_glyph_overhangs,
10842 x_fix_overlapping_area,
10843 x_draw_fringe_bitmap,
10844 x_per_char_metric,
10845 x_encode_char,
10846 x_compute_glyph_string_overhangs,
10847 x_draw_glyph_string,
10848 x_define_frame_cursor,
10849 x_clear_frame_area,
10850 x_draw_window_cursor,
10851 x_draw_vertical_window_border,
10852 x_shift_glyphs_for_insert
10855 void
10856 x_initialize ()
10858 clear_frame_hook = x_clear_frame;
10859 ins_del_lines_hook = x_ins_del_lines;
10860 delete_glyphs_hook = x_delete_glyphs;
10861 ring_bell_hook = XTring_bell;
10862 reset_terminal_modes_hook = XTreset_terminal_modes;
10863 set_terminal_modes_hook = XTset_terminal_modes;
10864 update_begin_hook = x_update_begin;
10865 update_end_hook = x_update_end;
10866 set_terminal_window_hook = XTset_terminal_window;
10867 read_socket_hook = XTread_socket;
10868 frame_up_to_date_hook = XTframe_up_to_date;
10869 mouse_position_hook = XTmouse_position;
10870 frame_rehighlight_hook = XTframe_rehighlight;
10871 frame_raise_lower_hook = XTframe_raise_lower;
10872 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10873 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10874 redeem_scroll_bar_hook = XTredeem_scroll_bar;
10875 judge_scroll_bars_hook = XTjudge_scroll_bars;
10877 x_display_method.rif = &x_redisplay_interface;
10878 x_display_method.scroll_region_ok = 1; /* We'll scroll partial frames. */
10879 x_display_method.char_ins_del_ok = 1;
10880 x_display_method.line_ins_del_ok = 1; /* We'll just blt 'em. */
10881 x_display_method.fast_clear_end_of_line = 1; /* X does this well. */
10882 x_display_method.memory_below_frame = 0; /* We don't remember what scrolls off the
10883 bottom. */
10884 baud_rate = 19200;
10886 x_noop_count = 0;
10887 last_tool_bar_item = -1;
10888 any_help_event_p = 0;
10890 /* Try to use interrupt input; if we can't, then start polling. */
10891 Fset_input_mode (Qt, Qnil, Qt, Qnil);
10893 #ifdef USE_X_TOOLKIT
10894 XtToolkitInitialize ();
10896 Xt_app_con = XtCreateApplicationContext ();
10898 /* Register a converter from strings to pixels, which uses
10899 Emacs' color allocation infrastructure. */
10900 XtAppSetTypeConverter (Xt_app_con,
10901 XtRString, XtRPixel, cvt_string_to_pixel,
10902 cvt_string_to_pixel_args,
10903 XtNumber (cvt_string_to_pixel_args),
10904 XtCacheByDisplay, cvt_pixel_dtor);
10906 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10908 /* Install an asynchronous timer that processes Xt timeout events
10909 every 0.1s. This is necessary because some widget sets use
10910 timeouts internally, for example the LessTif menu bar, or the
10911 Xaw3d scroll bar. When Xt timouts aren't processed, these
10912 widgets don't behave normally. */
10914 EMACS_TIME interval;
10915 EMACS_SET_SECS_USECS (interval, 0, 100000);
10916 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
10918 #endif
10920 #ifdef USE_TOOLKIT_SCROLL_BARS
10921 #ifndef USE_GTK
10922 xaw3d_arrow_scroll = False;
10923 xaw3d_pick_top = True;
10924 #endif
10925 #endif
10927 /* Note that there is no real way portable across R3/R4 to get the
10928 original error handler. */
10929 XSetErrorHandler (x_error_handler);
10930 XSetIOErrorHandler (x_io_error_quitter);
10932 /* Disable Window Change signals; they are handled by X events. */
10933 #ifdef SIGWINCH
10934 signal (SIGWINCH, SIG_DFL);
10935 #endif /* SIGWINCH */
10937 signal (SIGPIPE, x_connection_signal);
10941 void
10942 syms_of_xterm ()
10944 staticpro (&x_error_message_string);
10945 x_error_message_string = Qnil;
10947 staticpro (&x_display_name_list);
10948 x_display_name_list = Qnil;
10950 staticpro (&last_mouse_scroll_bar);
10951 last_mouse_scroll_bar = Qnil;
10953 staticpro (&Qvendor_specific_keysyms);
10954 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10956 staticpro (&Qutf_8);
10957 Qutf_8 = intern ("utf-8");
10958 staticpro (&Qlatin_1);
10959 Qlatin_1 = intern ("latin-1");
10961 staticpro (&last_mouse_press_frame);
10962 last_mouse_press_frame = Qnil;
10964 DEFVAR_BOOL ("x-use-underline-position-properties",
10965 &x_use_underline_position_properties,
10966 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10967 nil means ignore them. If you encounter fonts with bogus
10968 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10969 to 4.1, set this to nil. */);
10970 x_use_underline_position_properties = 1;
10972 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10973 doc: /* What X toolkit scroll bars Emacs uses.
10974 A value of nil means Emacs doesn't use X toolkit scroll bars.
10975 Otherwise, value is a symbol describing the X toolkit. */);
10976 #ifdef USE_TOOLKIT_SCROLL_BARS
10977 #ifdef USE_MOTIF
10978 Vx_toolkit_scroll_bars = intern ("motif");
10979 #elif defined HAVE_XAW3D
10980 Vx_toolkit_scroll_bars = intern ("xaw3d");
10981 #elif USE_GTK
10982 Vx_toolkit_scroll_bars = intern ("gtk");
10983 #else
10984 Vx_toolkit_scroll_bars = intern ("xaw");
10985 #endif
10986 #else
10987 Vx_toolkit_scroll_bars = Qnil;
10988 #endif
10990 staticpro (&last_mouse_motion_frame);
10991 last_mouse_motion_frame = Qnil;
10993 Qmodifier_value = intern ("modifier-value");
10994 Qalt = intern ("alt");
10995 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10996 Qhyper = intern ("hyper");
10997 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10998 Qmeta = intern ("meta");
10999 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11000 Qsuper = intern ("super");
11001 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11003 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
11004 doc: /* Which keys Emacs uses for the alt modifier.
11005 This should be one of the symbols `alt', `hyper', `meta', `super'.
11006 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11007 is nil, which is the same as `alt'. */);
11008 Vx_alt_keysym = Qnil;
11010 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
11011 doc: /* Which keys Emacs uses for the hyper modifier.
11012 This should be one of the symbols `alt', `hyper', `meta', `super'.
11013 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11014 default is nil, which is the same as `hyper'. */);
11015 Vx_hyper_keysym = Qnil;
11017 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
11018 doc: /* Which keys Emacs uses for the meta modifier.
11019 This should be one of the symbols `alt', `hyper', `meta', `super'.
11020 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11021 default is nil, which is the same as `meta'. */);
11022 Vx_meta_keysym = Qnil;
11024 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
11025 doc: /* Which keys Emacs uses for the super modifier.
11026 This should be one of the symbols `alt', `hyper', `meta', `super'.
11027 For example, `super' means use the Super_L and Super_R keysyms. The
11028 default is nil, which is the same as `super'. */);
11029 Vx_super_keysym = Qnil;
11031 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
11032 doc: /* Hash table of character codes indexed by X keysym codes. */);
11033 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
11034 make_float (DEFAULT_REHASH_SIZE),
11035 make_float (DEFAULT_REHASH_THRESHOLD),
11036 Qnil, Qnil, Qnil);
11039 #endif /* HAVE_X_WINDOWS */
11041 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
11042 (do not change this comment) */