(PC-do-completion): Do not forget to use `pred' as the default-directory
[emacs.git] / src / xterm.c
blobc686824a00353e83badb2bad4f8e2999b73656c4
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 #include <X11/Xaw3d/ScrollbarP.h>
135 #else /* !HAVE_XAW3D */
136 #include <X11/Xaw/Simple.h>
137 #include <X11/Xaw/Scrollbar.h>
138 #endif /* !HAVE_XAW3D */
139 #ifndef XtNpickTop
140 #define XtNpickTop "pickTop"
141 #endif /* !XtNpickTop */
142 #endif /* !USE_MOTIF */
143 #endif /* USE_TOOLKIT_SCROLL_BARS */
145 #endif /* USE_X_TOOLKIT */
147 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
148 #define x_any_window_to_frame x_window_to_frame
149 #define x_top_window_to_frame x_window_to_frame
150 #endif
152 #ifdef USE_X_TOOLKIT
153 #include "widget.h"
154 #ifndef XtNinitialState
155 #define XtNinitialState "initialState"
156 #endif
157 #endif
159 #define abs(x) ((x) < 0 ? -(x) : (x))
161 /* Default to using XIM if available. */
162 #ifdef USE_XIM
163 int use_xim = 1;
164 #else
165 int use_xim = 0; /* configure --without-xim */
166 #endif
170 /* Non-nil means Emacs uses toolkit scroll bars. */
172 Lisp_Object Vx_toolkit_scroll_bars;
174 /* Non-zero means that a HELP_EVENT has been generated since Emacs
175 start. */
177 static int any_help_event_p;
179 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
180 static Lisp_Object last_window;
182 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
184 int x_use_underline_position_properties;
186 /* This is a chain of structures for all the X displays currently in
187 use. */
189 struct x_display_info *x_display_list;
191 /* This is a list of cons cells, each of the form (NAME
192 . FONT-LIST-CACHE), one for each element of x_display_list and in
193 the same order. NAME is the name of the frame. FONT-LIST-CACHE
194 records previous values returned by x-list-fonts. */
196 Lisp_Object x_display_name_list;
198 /* Frame being updated by update_frame. This is declared in term.c.
199 This is set by update_begin and looked at by all the XT functions.
200 It is zero while not inside an update. In that case, the XT
201 functions assume that `selected_frame' is the frame to apply to. */
203 extern struct frame *updating_frame;
205 /* This is a frame waiting to be auto-raised, within XTread_socket. */
207 struct frame *pending_autoraise_frame;
209 #ifdef USE_X_TOOLKIT
210 /* The application context for Xt use. */
211 XtAppContext Xt_app_con;
212 static String Xt_default_resources[] = {0};
213 #endif /* USE_X_TOOLKIT */
215 /* Non-zero means user is interacting with a toolkit scroll bar. */
217 static int toolkit_scroll_bar_interaction;
219 /* Mouse movement.
221 Formerly, we used PointerMotionHintMask (in standard_event_mask)
222 so that we would have to call XQueryPointer after each MotionNotify
223 event to ask for another such event. However, this made mouse tracking
224 slow, and there was a bug that made it eventually stop.
226 Simply asking for MotionNotify all the time seems to work better.
228 In order to avoid asking for motion events and then throwing most
229 of them away or busy-polling the server for mouse positions, we ask
230 the server for pointer motion hints. This means that we get only
231 one event per group of mouse movements. "Groups" are delimited by
232 other kinds of events (focus changes and button clicks, for
233 example), or by XQueryPointer calls; when one of these happens, we
234 get another MotionNotify event the next time the mouse moves. This
235 is at least as efficient as getting motion events when mouse
236 tracking is on, and I suspect only negligibly worse when tracking
237 is off. */
239 /* Where the mouse was last time we reported a mouse event. */
241 static XRectangle last_mouse_glyph;
242 static Lisp_Object last_mouse_press_frame;
244 /* The scroll bar in which the last X motion event occurred.
246 If the last X motion event occurred in a scroll bar, we set this so
247 XTmouse_position can know whether to report a scroll bar motion or
248 an ordinary motion.
250 If the last X motion event didn't occur in a scroll bar, we set
251 this to Qnil, to tell XTmouse_position to return an ordinary motion
252 event. */
254 static Lisp_Object last_mouse_scroll_bar;
256 /* This is a hack. We would really prefer that XTmouse_position would
257 return the time associated with the position it returns, but there
258 doesn't seem to be any way to wrest the time-stamp from the server
259 along with the position query. So, we just keep track of the time
260 of the last movement we received, and return that in hopes that
261 it's somewhat accurate. */
263 static Time last_mouse_movement_time;
265 /* Incremented by XTread_socket whenever it really tries to read
266 events. */
268 #ifdef __STDC__
269 static int volatile input_signal_count;
270 #else
271 static int input_signal_count;
272 #endif
274 /* Used locally within XTread_socket. */
276 static int x_noop_count;
278 /* Initial values of argv and argc. */
280 extern char **initial_argv;
281 extern int initial_argc;
283 extern Lisp_Object Vcommand_line_args, Vsystem_name;
285 /* Tells if a window manager is present or not. */
287 extern Lisp_Object Vx_no_window_manager;
289 extern Lisp_Object Qface, Qmouse_face, Qeql;
291 extern int errno;
293 /* A mask of extra modifier bits to put into every keyboard char. */
295 extern EMACS_INT extra_keyboard_modifiers;
297 /* The keysyms to use for the various modifiers. */
299 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
300 Lisp_Object Vx_keysym_table;
301 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
303 static Lisp_Object Qvendor_specific_keysyms;
304 static Lisp_Object Qlatin_1;
306 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
307 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
309 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
310 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
311 static const XColor *x_color_cells P_ ((Display *, int *));
312 static void x_update_window_end P_ ((struct window *, int, int));
313 void x_delete_display P_ ((struct x_display_info *));
314 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
315 unsigned));
316 static int x_io_error_quitter P_ ((Display *));
317 int x_catch_errors P_ ((Display *));
318 void x_uncatch_errors P_ ((Display *, int));
319 void x_lower_frame P_ ((struct frame *));
320 void x_scroll_bar_clear P_ ((struct frame *));
321 int x_had_errors_p P_ ((Display *));
322 void x_wm_set_size_hint P_ ((struct frame *, long, int));
323 void x_raise_frame P_ ((struct frame *));
324 void x_set_window_size P_ ((struct frame *, int, int, int));
325 void x_wm_set_window_state P_ ((struct frame *, int));
326 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
327 void x_initialize P_ ((void));
328 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
329 static int x_compute_min_glyph_bounds P_ ((struct frame *));
330 static void x_update_end P_ ((struct frame *));
331 static void XTframe_up_to_date P_ ((struct frame *));
332 static void XTset_terminal_modes P_ ((void));
333 static void XTreset_terminal_modes P_ ((void));
334 static void x_clear_frame P_ ((void));
335 static void frame_highlight P_ ((struct frame *));
336 static void frame_unhighlight P_ ((struct frame *));
337 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
338 static int x_focus_changed P_ ((int,
339 int,
340 struct x_display_info *,
341 struct frame *,
342 struct input_event *,
343 int));
344 static int x_detect_focus_change P_ ((struct x_display_info *,
345 XEvent *,
346 struct input_event *,
347 int));
348 static void XTframe_rehighlight P_ ((struct frame *));
349 static void x_frame_rehighlight P_ ((struct x_display_info *));
350 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
351 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
352 enum text_cursor_kinds));
354 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, GC));
355 static void x_flush P_ ((struct frame *f));
356 static void x_update_begin P_ ((struct frame *));
357 static void x_update_window_begin P_ ((struct window *));
358 static void x_after_update_window_line P_ ((struct glyph_row *));
359 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
360 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
361 enum scroll_bar_part *,
362 Lisp_Object *, Lisp_Object *,
363 unsigned long *));
364 static void x_check_fullscreen P_ ((struct frame *));
365 static void x_check_expected_move P_ ((struct frame *));
366 static int handle_one_xevent P_ ((struct x_display_info *,
367 XEvent *,
368 struct input_event **,
369 int *,
370 int *));
373 /* Flush display of frame F, or of all frames if F is null. */
375 static void
376 x_flush (f)
377 struct frame *f;
379 BLOCK_INPUT;
380 if (f == NULL)
382 Lisp_Object rest, frame;
383 FOR_EACH_FRAME (rest, frame)
384 x_flush (XFRAME (frame));
386 else if (FRAME_X_P (f))
387 XFlush (FRAME_X_DISPLAY (f));
388 UNBLOCK_INPUT;
392 /* Remove calls to XFlush by defining XFlush to an empty replacement.
393 Calls to XFlush should be unnecessary because the X output buffer
394 is flushed automatically as needed by calls to XPending,
395 XNextEvent, or XWindowEvent according to the XFlush man page.
396 XTread_socket calls XPending. Removing XFlush improves
397 performance. */
399 #define XFlush(DISPLAY) (void) 0
402 /***********************************************************************
403 Debugging
404 ***********************************************************************/
406 #if 0
408 /* This is a function useful for recording debugging information about
409 the sequence of occurrences in this file. */
411 struct record
413 char *locus;
414 int type;
417 struct record event_record[100];
419 int event_record_index;
421 record_event (locus, type)
422 char *locus;
423 int type;
425 if (event_record_index == sizeof (event_record) / sizeof (struct record))
426 event_record_index = 0;
428 event_record[event_record_index].locus = locus;
429 event_record[event_record_index].type = type;
430 event_record_index++;
433 #endif /* 0 */
437 /* Return the struct x_display_info corresponding to DPY. */
439 struct x_display_info *
440 x_display_info_for_display (dpy)
441 Display *dpy;
443 struct x_display_info *dpyinfo;
445 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
446 if (dpyinfo->display == dpy)
447 return dpyinfo;
449 return 0;
454 /***********************************************************************
455 Starting and ending an update
456 ***********************************************************************/
458 /* Start an update of frame F. This function is installed as a hook
459 for update_begin, i.e. it is called when update_begin is called.
460 This function is called prior to calls to x_update_window_begin for
461 each window being updated. Currently, there is nothing to do here
462 because all interesting stuff is done on a window basis. */
464 static void
465 x_update_begin (f)
466 struct frame *f;
468 /* Nothing to do. */
472 /* Start update of window W. Set the global variable updated_window
473 to the window being updated and set output_cursor to the cursor
474 position of W. */
476 static void
477 x_update_window_begin (w)
478 struct window *w;
480 struct frame *f = XFRAME (WINDOW_FRAME (w));
481 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
483 updated_window = w;
484 set_output_cursor (&w->cursor);
486 BLOCK_INPUT;
488 if (f == display_info->mouse_face_mouse_frame)
490 /* Don't do highlighting for mouse motion during the update. */
491 display_info->mouse_face_defer = 1;
493 /* If F needs to be redrawn, simply forget about any prior mouse
494 highlighting. */
495 if (FRAME_GARBAGED_P (f))
496 display_info->mouse_face_window = Qnil;
498 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
499 their mouse_face_p flag set, which means that they are always
500 unequal to rows in a desired matrix which never have that
501 flag set. So, rows containing mouse-face glyphs are never
502 scrolled, and we don't have to switch the mouse highlight off
503 here to prevent it from being scrolled. */
505 /* Can we tell that this update does not affect the window
506 where the mouse highlight is? If so, no need to turn off.
507 Likewise, don't do anything if the frame is garbaged;
508 in that case, the frame's current matrix that we would use
509 is all wrong, and we will redisplay that line anyway. */
510 if (!NILP (display_info->mouse_face_window)
511 && w == XWINDOW (display_info->mouse_face_window))
513 int i;
515 for (i = 0; i < w->desired_matrix->nrows; ++i)
516 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
517 break;
519 if (i < w->desired_matrix->nrows)
520 clear_mouse_face (display_info);
522 #endif /* 0 */
525 UNBLOCK_INPUT;
529 /* Draw a vertical window border from (x,y0) to (x,y1) */
531 static void
532 x_draw_vertical_window_border (w, x, y0, y1)
533 struct window *w;
534 int x, y0, y1;
536 struct frame *f = XFRAME (WINDOW_FRAME (w));
538 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
539 f->output_data.x->normal_gc, x, y0, x, y1);
542 /* End update of window W (which is equal to updated_window).
544 Draw vertical borders between horizontally adjacent windows, and
545 display W's cursor if CURSOR_ON_P is non-zero.
547 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
548 glyphs in mouse-face were overwritten. In that case we have to
549 make sure that the mouse-highlight is properly redrawn.
551 W may be a menu bar pseudo-window in case we don't have X toolkit
552 support. Such windows don't have a cursor, so don't display it
553 here. */
555 static void
556 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
557 struct window *w;
558 int cursor_on_p, mouse_face_overwritten_p;
560 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
562 if (!w->pseudo_window_p)
564 BLOCK_INPUT;
566 if (cursor_on_p)
567 display_and_set_cursor (w, 1, output_cursor.hpos,
568 output_cursor.vpos,
569 output_cursor.x, output_cursor.y);
571 x_draw_vertical_border (w);
572 UNBLOCK_INPUT;
575 /* If a row with mouse-face was overwritten, arrange for
576 XTframe_up_to_date to redisplay the mouse highlight. */
577 if (mouse_face_overwritten_p)
579 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
580 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
581 dpyinfo->mouse_face_window = Qnil;
584 updated_window = NULL;
588 /* End update of frame F. This function is installed as a hook in
589 update_end. */
591 static void
592 x_update_end (f)
593 struct frame *f;
595 /* Mouse highlight may be displayed again. */
596 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
598 #ifndef XFlush
599 BLOCK_INPUT;
600 XFlush (FRAME_X_DISPLAY (f));
601 UNBLOCK_INPUT;
602 #endif
606 /* This function is called from various places in xdisp.c whenever a
607 complete update has been performed. The global variable
608 updated_window is not available here. */
610 static void
611 XTframe_up_to_date (f)
612 struct frame *f;
614 if (FRAME_X_P (f))
616 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
618 if (dpyinfo->mouse_face_deferred_gc
619 || f == dpyinfo->mouse_face_mouse_frame)
621 BLOCK_INPUT;
622 if (dpyinfo->mouse_face_mouse_frame)
623 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
624 dpyinfo->mouse_face_mouse_x,
625 dpyinfo->mouse_face_mouse_y);
626 dpyinfo->mouse_face_deferred_gc = 0;
627 UNBLOCK_INPUT;
633 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
634 arrow bitmaps, or clear the fringes if no bitmaps are required
635 before DESIRED_ROW is made current. The window being updated is
636 found in updated_window. This function It is called from
637 update_window_line only if it is known that there are differences
638 between bitmaps to be drawn between current row and DESIRED_ROW. */
640 static void
641 x_after_update_window_line (desired_row)
642 struct glyph_row *desired_row;
644 struct window *w = updated_window;
645 struct frame *f;
646 int width, height;
648 xassert (w);
650 if (!desired_row->mode_line_p && !w->pseudo_window_p)
652 BLOCK_INPUT;
653 draw_row_fringe_bitmaps (w, desired_row);
654 UNBLOCK_INPUT;
657 /* When a window has disappeared, make sure that no rest of
658 full-width rows stays visible in the internal border. Could
659 check here if updated_window is the leftmost/rightmost window,
660 but I guess it's not worth doing since vertically split windows
661 are almost never used, internal border is rarely set, and the
662 overhead is very small. */
663 if (windows_or_buffers_changed
664 && desired_row->full_width_p
665 && (f = XFRAME (w->frame),
666 width = FRAME_INTERNAL_BORDER_WIDTH (f),
667 width != 0)
668 && (height = desired_row->visible_height,
669 height > 0))
671 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
673 /* Internal border is drawn below the tool bar. */
674 if (WINDOWP (f->tool_bar_window)
675 && w == XWINDOW (f->tool_bar_window))
676 y -= width;
678 BLOCK_INPUT;
679 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
680 0, y, width, height, False);
681 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
682 FRAME_PIXEL_WIDTH (f) - width,
683 y, width, height, False);
684 UNBLOCK_INPUT;
688 static void
689 x_draw_fringe_bitmap (w, row, p)
690 struct window *w;
691 struct glyph_row *row;
692 struct draw_fringe_bitmap_params *p;
694 struct frame *f = XFRAME (WINDOW_FRAME (w));
695 Display *display = FRAME_X_DISPLAY (f);
696 Window window = FRAME_X_WINDOW (f);
697 GC gc = f->output_data.x->normal_gc;
698 struct face *face = p->face;
700 /* Must clip because of partially visible lines. */
701 x_clip_to_row (w, row, gc);
703 if (p->bx >= 0)
705 /* In case the same realized face is used for fringes and
706 for something displayed in the text (e.g. face `region' on
707 mono-displays, the fill style may have been changed to
708 FillSolid in x_draw_glyph_string_background. */
709 if (face->stipple)
710 XSetFillStyle (display, face->gc, FillOpaqueStippled);
711 else
712 XSetForeground (display, face->gc, face->background);
714 XFillRectangle (display, window, face->gc,
715 p->bx, p->by, p->nx, p->ny);
717 if (!face->stipple)
718 XSetForeground (display, face->gc, face->foreground);
721 if (p->which != NO_FRINGE_BITMAP)
723 unsigned char *bits = fringe_bitmaps[p->which].bits + p->dh;
724 Pixmap pixmap;
725 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
727 /* Draw the bitmap. I believe these small pixmaps can be cached
728 by the server. */
729 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
730 face->foreground,
731 face->background, depth);
732 XCopyArea (display, pixmap, window, gc, 0, 0,
733 p->wd, p->h, p->x, p->y);
734 XFreePixmap (display, pixmap);
737 XSetClipMask (display, gc, None);
742 /* This is called when starting Emacs and when restarting after
743 suspend. When starting Emacs, no X window is mapped. And nothing
744 must be done to Emacs's own window if it is suspended (though that
745 rarely happens). */
747 static void
748 XTset_terminal_modes ()
752 /* This is called when exiting or suspending Emacs. Exiting will make
753 the X-windows go away, and suspending requires no action. */
755 static void
756 XTreset_terminal_modes ()
762 /***********************************************************************
763 Display Iterator
764 ***********************************************************************/
766 /* Function prototypes of this page. */
768 static int x_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
771 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
772 is not contained in the font. */
774 static XCharStruct *
775 x_per_char_metric (font, char2b, font_type)
776 XFontStruct *font;
777 XChar2b *char2b;
778 int font_type; /* unused on X */
780 /* The result metric information. */
781 XCharStruct *pcm = NULL;
783 xassert (font && char2b);
785 if (font->per_char != NULL)
787 if (font->min_byte1 == 0 && font->max_byte1 == 0)
789 /* min_char_or_byte2 specifies the linear character index
790 corresponding to the first element of the per_char array,
791 max_char_or_byte2 is the index of the last character. A
792 character with non-zero CHAR2B->byte1 is not in the font.
793 A character with byte2 less than min_char_or_byte2 or
794 greater max_char_or_byte2 is not in the font. */
795 if (char2b->byte1 == 0
796 && char2b->byte2 >= font->min_char_or_byte2
797 && char2b->byte2 <= font->max_char_or_byte2)
798 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
800 else
802 /* If either min_byte1 or max_byte1 are nonzero, both
803 min_char_or_byte2 and max_char_or_byte2 are less than
804 256, and the 2-byte character index values corresponding
805 to the per_char array element N (counting from 0) are:
807 byte1 = N/D + min_byte1
808 byte2 = N\D + min_char_or_byte2
810 where:
812 D = max_char_or_byte2 - min_char_or_byte2 + 1
813 / = integer division
814 \ = integer modulus */
815 if (char2b->byte1 >= font->min_byte1
816 && char2b->byte1 <= font->max_byte1
817 && char2b->byte2 >= font->min_char_or_byte2
818 && char2b->byte2 <= font->max_char_or_byte2)
820 pcm = (font->per_char
821 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
822 * (char2b->byte1 - font->min_byte1))
823 + (char2b->byte2 - font->min_char_or_byte2));
827 else
829 /* If the per_char pointer is null, all glyphs between the first
830 and last character indexes inclusive have the same
831 information, as given by both min_bounds and max_bounds. */
832 if (char2b->byte2 >= font->min_char_or_byte2
833 && char2b->byte2 <= font->max_char_or_byte2)
834 pcm = &font->max_bounds;
837 return ((pcm == NULL
838 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
839 ? NULL : pcm);
843 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
844 the two-byte form of C. Encoding is returned in *CHAR2B. */
846 static int
847 x_encode_char (c, char2b, font_info, two_byte_p)
848 int c;
849 XChar2b *char2b;
850 struct font_info *font_info;
851 int *two_byte_p;
853 int charset = CHAR_CHARSET (c);
854 XFontStruct *font = font_info->font;
856 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
857 This may be either a program in a special encoder language or a
858 fixed encoding. */
859 if (font_info->font_encoder)
861 /* It's a program. */
862 struct ccl_program *ccl = font_info->font_encoder;
864 if (CHARSET_DIMENSION (charset) == 1)
866 ccl->reg[0] = charset;
867 ccl->reg[1] = char2b->byte2;
868 ccl->reg[2] = -1;
870 else
872 ccl->reg[0] = charset;
873 ccl->reg[1] = char2b->byte1;
874 ccl->reg[2] = char2b->byte2;
877 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
879 /* We assume that MSBs are appropriately set/reset by CCL
880 program. */
881 if (font->max_byte1 == 0) /* 1-byte font */
882 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
883 else
884 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
886 else if (font_info->encoding[charset])
888 /* Fixed encoding scheme. See fontset.h for the meaning of the
889 encoding numbers. */
890 int enc = font_info->encoding[charset];
892 if ((enc == 1 || enc == 2)
893 && CHARSET_DIMENSION (charset) == 2)
894 char2b->byte1 |= 0x80;
896 if (enc == 1 || enc == 3)
897 char2b->byte2 |= 0x80;
900 if (two_byte_p)
901 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
903 return FONT_TYPE_UNKNOWN;
908 /***********************************************************************
909 Glyph display
910 ***********************************************************************/
914 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
915 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
916 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
917 int));
918 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
919 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
920 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
921 static void x_draw_glyph_string P_ ((struct glyph_string *));
922 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
923 static void x_set_cursor_gc P_ ((struct glyph_string *));
924 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
925 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
926 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
927 unsigned long *, double, int));
928 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
929 double, int, unsigned long));
930 static void x_setup_relief_colors P_ ((struct glyph_string *));
931 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
932 static void x_draw_image_relief P_ ((struct glyph_string *));
933 static void x_draw_image_foreground P_ ((struct glyph_string *));
934 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
935 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
936 int, int, int));
937 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
938 int, int, int, int, XRectangle *));
939 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
940 int, int, int, XRectangle *));
942 #if GLYPH_DEBUG
943 static void x_check_font P_ ((struct frame *, XFontStruct *));
944 #endif
947 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
948 face. */
950 static void
951 x_set_cursor_gc (s)
952 struct glyph_string *s;
954 if (s->font == FRAME_FONT (s->f)
955 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
956 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
957 && !s->cmp)
958 s->gc = s->f->output_data.x->cursor_gc;
959 else
961 /* Cursor on non-default face: must merge. */
962 XGCValues xgcv;
963 unsigned long mask;
965 xgcv.background = s->f->output_data.x->cursor_pixel;
966 xgcv.foreground = s->face->background;
968 /* If the glyph would be invisible, try a different foreground. */
969 if (xgcv.foreground == xgcv.background)
970 xgcv.foreground = s->face->foreground;
971 if (xgcv.foreground == xgcv.background)
972 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
973 if (xgcv.foreground == xgcv.background)
974 xgcv.foreground = s->face->foreground;
976 /* Make sure the cursor is distinct from text in this face. */
977 if (xgcv.background == s->face->background
978 && xgcv.foreground == s->face->foreground)
980 xgcv.background = s->face->foreground;
981 xgcv.foreground = s->face->background;
984 IF_DEBUG (x_check_font (s->f, s->font));
985 xgcv.font = s->font->fid;
986 xgcv.graphics_exposures = False;
987 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
989 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
990 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
991 mask, &xgcv);
992 else
993 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
994 = XCreateGC (s->display, s->window, mask, &xgcv);
996 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1001 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1003 static void
1004 x_set_mouse_face_gc (s)
1005 struct glyph_string *s;
1007 int face_id;
1008 struct face *face;
1010 /* What face has to be used last for the mouse face? */
1011 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1012 face = FACE_FROM_ID (s->f, face_id);
1013 if (face == NULL)
1014 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1016 if (s->first_glyph->type == CHAR_GLYPH)
1017 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
1018 else
1019 face_id = FACE_FOR_CHAR (s->f, face, 0);
1020 s->face = FACE_FROM_ID (s->f, face_id);
1021 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1023 /* If font in this face is same as S->font, use it. */
1024 if (s->font == s->face->font)
1025 s->gc = s->face->gc;
1026 else
1028 /* Otherwise construct scratch_cursor_gc with values from FACE
1029 but font FONT. */
1030 XGCValues xgcv;
1031 unsigned long mask;
1033 xgcv.background = s->face->background;
1034 xgcv.foreground = s->face->foreground;
1035 IF_DEBUG (x_check_font (s->f, s->font));
1036 xgcv.font = s->font->fid;
1037 xgcv.graphics_exposures = False;
1038 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1040 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1041 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1042 mask, &xgcv);
1043 else
1044 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1045 = XCreateGC (s->display, s->window, mask, &xgcv);
1047 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1050 xassert (s->gc != 0);
1054 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1055 Faces to use in the mode line have already been computed when the
1056 matrix was built, so there isn't much to do, here. */
1058 static INLINE void
1059 x_set_mode_line_face_gc (s)
1060 struct glyph_string *s;
1062 s->gc = s->face->gc;
1066 /* Set S->gc of glyph string S for drawing that glyph string. Set
1067 S->stippled_p to a non-zero value if the face of S has a stipple
1068 pattern. */
1070 static INLINE void
1071 x_set_glyph_string_gc (s)
1072 struct glyph_string *s;
1074 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1076 if (s->hl == DRAW_NORMAL_TEXT)
1078 s->gc = s->face->gc;
1079 s->stippled_p = s->face->stipple != 0;
1081 else if (s->hl == DRAW_INVERSE_VIDEO)
1083 x_set_mode_line_face_gc (s);
1084 s->stippled_p = s->face->stipple != 0;
1086 else if (s->hl == DRAW_CURSOR)
1088 x_set_cursor_gc (s);
1089 s->stippled_p = 0;
1091 else if (s->hl == DRAW_MOUSE_FACE)
1093 x_set_mouse_face_gc (s);
1094 s->stippled_p = s->face->stipple != 0;
1096 else if (s->hl == DRAW_IMAGE_RAISED
1097 || s->hl == DRAW_IMAGE_SUNKEN)
1099 s->gc = s->face->gc;
1100 s->stippled_p = s->face->stipple != 0;
1102 else
1104 s->gc = s->face->gc;
1105 s->stippled_p = s->face->stipple != 0;
1108 /* GC must have been set. */
1109 xassert (s->gc != 0);
1113 /* Set clipping for output of glyph string S. S may be part of a mode
1114 line or menu if we don't have X toolkit support. */
1116 static INLINE void
1117 x_set_glyph_string_clipping (s)
1118 struct glyph_string *s;
1120 XRectangle r;
1121 get_glyph_string_clip_rect (s, &r);
1122 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
1126 /* RIF:
1127 Compute left and right overhang of glyph string S. If S is a glyph
1128 string for a composition, assume overhangs don't exist. */
1130 static void
1131 x_compute_glyph_string_overhangs (s)
1132 struct glyph_string *s;
1134 if (s->cmp == NULL
1135 && s->first_glyph->type == CHAR_GLYPH)
1137 XCharStruct cs;
1138 int direction, font_ascent, font_descent;
1139 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1140 &font_ascent, &font_descent, &cs);
1141 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1142 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1147 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1149 static INLINE void
1150 x_clear_glyph_string_rect (s, x, y, w, h)
1151 struct glyph_string *s;
1152 int x, y, w, h;
1154 XGCValues xgcv;
1155 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1156 XSetForeground (s->display, s->gc, xgcv.background);
1157 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1158 XSetForeground (s->display, s->gc, xgcv.foreground);
1162 /* Draw the background of glyph_string S. If S->background_filled_p
1163 is non-zero don't draw it. FORCE_P non-zero means draw the
1164 background even if it wouldn't be drawn normally. This is used
1165 when a string preceding S draws into the background of S, or S
1166 contains the first component of a composition. */
1168 static void
1169 x_draw_glyph_string_background (s, force_p)
1170 struct glyph_string *s;
1171 int force_p;
1173 /* Nothing to do if background has already been drawn or if it
1174 shouldn't be drawn in the first place. */
1175 if (!s->background_filled_p)
1177 int box_line_width = max (s->face->box_line_width, 0);
1179 if (s->stippled_p)
1181 /* Fill background with a stipple pattern. */
1182 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1183 XFillRectangle (s->display, s->window, s->gc, s->x,
1184 s->y + box_line_width,
1185 s->background_width,
1186 s->height - 2 * box_line_width);
1187 XSetFillStyle (s->display, s->gc, FillSolid);
1188 s->background_filled_p = 1;
1190 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1191 || s->font_not_found_p
1192 || s->extends_to_end_of_line_p
1193 || force_p)
1195 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1196 s->background_width,
1197 s->height - 2 * box_line_width);
1198 s->background_filled_p = 1;
1204 /* Draw the foreground of glyph string S. */
1206 static void
1207 x_draw_glyph_string_foreground (s)
1208 struct glyph_string *s;
1210 int i, x;
1212 /* If first glyph of S has a left box line, start drawing the text
1213 of S to the right of that box line. */
1214 if (s->face->box != FACE_NO_BOX
1215 && s->first_glyph->left_box_line_p)
1216 x = s->x + abs (s->face->box_line_width);
1217 else
1218 x = s->x;
1220 /* Draw characters of S as rectangles if S's font could not be
1221 loaded. */
1222 if (s->font_not_found_p)
1224 for (i = 0; i < s->nchars; ++i)
1226 struct glyph *g = s->first_glyph + i;
1227 XDrawRectangle (s->display, s->window,
1228 s->gc, x, s->y, g->pixel_width - 1,
1229 s->height - 1);
1230 x += g->pixel_width;
1233 else
1235 char *char1b = (char *) s->char2b;
1236 int boff = s->font_info->baseline_offset;
1238 if (s->font_info->vertical_centering)
1239 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1241 /* If we can use 8-bit functions, condense S->char2b. */
1242 if (!s->two_byte_p)
1243 for (i = 0; i < s->nchars; ++i)
1244 char1b[i] = s->char2b[i].byte2;
1246 /* Draw text with XDrawString if background has already been
1247 filled. Otherwise, use XDrawImageString. (Note that
1248 XDrawImageString is usually faster than XDrawString.) Always
1249 use XDrawImageString when drawing the cursor so that there is
1250 no chance that characters under a box cursor are invisible. */
1251 if (s->for_overlaps_p
1252 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1254 /* Draw characters with 16-bit or 8-bit functions. */
1255 if (s->two_byte_p)
1256 XDrawString16 (s->display, s->window, s->gc, x,
1257 s->ybase - boff, s->char2b, s->nchars);
1258 else
1259 XDrawString (s->display, s->window, s->gc, x,
1260 s->ybase - boff, char1b, s->nchars);
1262 else
1264 if (s->two_byte_p)
1265 XDrawImageString16 (s->display, s->window, s->gc, x,
1266 s->ybase - boff, s->char2b, s->nchars);
1267 else
1268 XDrawImageString (s->display, s->window, s->gc, x,
1269 s->ybase - boff, char1b, s->nchars);
1272 if (s->face->overstrike)
1274 /* For overstriking (to simulate bold-face), draw the
1275 characters again shifted to the right by one pixel. */
1276 if (s->two_byte_p)
1277 XDrawString16 (s->display, s->window, s->gc, x + 1,
1278 s->ybase - boff, s->char2b, s->nchars);
1279 else
1280 XDrawString (s->display, s->window, s->gc, x + 1,
1281 s->ybase - boff, char1b, s->nchars);
1286 /* Draw the foreground of composite glyph string S. */
1288 static void
1289 x_draw_composite_glyph_string_foreground (s)
1290 struct glyph_string *s;
1292 int i, x;
1294 /* If first glyph of S has a left box line, start drawing the text
1295 of S to the right of that box line. */
1296 if (s->face->box != FACE_NO_BOX
1297 && s->first_glyph->left_box_line_p)
1298 x = s->x + abs (s->face->box_line_width);
1299 else
1300 x = s->x;
1302 /* S is a glyph string for a composition. S->gidx is the index of
1303 the first character drawn for glyphs of this composition.
1304 S->gidx == 0 means we are drawing the very first character of
1305 this composition. */
1307 /* Draw a rectangle for the composition if the font for the very
1308 first character of the composition could not be loaded. */
1309 if (s->font_not_found_p)
1311 if (s->gidx == 0)
1312 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1313 s->width - 1, s->height - 1);
1315 else
1317 for (i = 0; i < s->nchars; i++, ++s->gidx)
1319 XDrawString16 (s->display, s->window, s->gc,
1320 x + s->cmp->offsets[s->gidx * 2],
1321 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1322 s->char2b + i, 1);
1323 if (s->face->overstrike)
1324 XDrawString16 (s->display, s->window, s->gc,
1325 x + s->cmp->offsets[s->gidx * 2] + 1,
1326 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1327 s->char2b + i, 1);
1333 #ifdef USE_X_TOOLKIT
1335 static struct frame *x_frame_of_widget P_ ((Widget));
1336 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1337 XrmValue *, XrmValue *, XtPointer *));
1338 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1339 XrmValue *, Cardinal *));
1342 /* Return the frame on which widget WIDGET is used.. Abort if frame
1343 cannot be determined. */
1345 static struct frame *
1346 x_frame_of_widget (widget)
1347 Widget widget;
1349 struct x_display_info *dpyinfo;
1350 Lisp_Object tail;
1351 struct frame *f;
1353 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1355 /* Find the top-level shell of the widget. Note that this function
1356 can be called when the widget is not yet realized, so XtWindow
1357 (widget) == 0. That's the reason we can't simply use
1358 x_any_window_to_frame. */
1359 while (!XtIsTopLevelShell (widget))
1360 widget = XtParent (widget);
1362 /* Look for a frame with that top-level widget. Allocate the color
1363 on that frame to get the right gamma correction value. */
1364 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1365 if (GC_FRAMEP (XCAR (tail))
1366 && (f = XFRAME (XCAR (tail)),
1367 (f->output_data.nothing != 1
1368 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1369 && f->output_data.x->widget == widget)
1370 return f;
1372 abort ();
1376 /* Allocate the color COLOR->pixel on the screen and display of
1377 widget WIDGET in colormap CMAP. If an exact match cannot be
1378 allocated, try the nearest color available. Value is non-zero
1379 if successful. This is called from lwlib. */
1382 x_alloc_nearest_color_for_widget (widget, cmap, color)
1383 Widget widget;
1384 Colormap cmap;
1385 XColor *color;
1387 struct frame *f = x_frame_of_widget (widget);
1388 return x_alloc_nearest_color (f, cmap, color);
1392 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1393 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1394 If this produces the same color as PIXEL, try a color where all RGB
1395 values have DELTA added. Return the allocated color in *PIXEL.
1396 DISPLAY is the X display, CMAP is the colormap to operate on.
1397 Value is non-zero if successful. */
1400 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1401 Widget widget;
1402 Display *display;
1403 Colormap cmap;
1404 unsigned long *pixel;
1405 double factor;
1406 int delta;
1408 struct frame *f = x_frame_of_widget (widget);
1409 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1413 /* Structure specifying which arguments should be passed by Xt to
1414 cvt_string_to_pixel. We want the widget's screen and colormap. */
1416 static XtConvertArgRec cvt_string_to_pixel_args[] =
1418 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1419 sizeof (Screen *)},
1420 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1421 sizeof (Colormap)}
1425 /* The address of this variable is returned by
1426 cvt_string_to_pixel. */
1428 static Pixel cvt_string_to_pixel_value;
1431 /* Convert a color name to a pixel color.
1433 DPY is the display we are working on.
1435 ARGS is an array of *NARGS XrmValue structures holding additional
1436 information about the widget for which the conversion takes place.
1437 The contents of this array are determined by the specification
1438 in cvt_string_to_pixel_args.
1440 FROM is a pointer to an XrmValue which points to the color name to
1441 convert. TO is an XrmValue in which to return the pixel color.
1443 CLOSURE_RET is a pointer to user-data, in which we record if
1444 we allocated the color or not.
1446 Value is True if successful, False otherwise. */
1448 static Boolean
1449 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1450 Display *dpy;
1451 XrmValue *args;
1452 Cardinal *nargs;
1453 XrmValue *from, *to;
1454 XtPointer *closure_ret;
1456 Screen *screen;
1457 Colormap cmap;
1458 Pixel pixel;
1459 String color_name;
1460 XColor color;
1462 if (*nargs != 2)
1464 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1465 "wrongParameters", "cvt_string_to_pixel",
1466 "XtToolkitError",
1467 "Screen and colormap args required", NULL, NULL);
1468 return False;
1471 screen = *(Screen **) args[0].addr;
1472 cmap = *(Colormap *) args[1].addr;
1473 color_name = (String) from->addr;
1475 if (strcmp (color_name, XtDefaultBackground) == 0)
1477 *closure_ret = (XtPointer) False;
1478 pixel = WhitePixelOfScreen (screen);
1480 else if (strcmp (color_name, XtDefaultForeground) == 0)
1482 *closure_ret = (XtPointer) False;
1483 pixel = BlackPixelOfScreen (screen);
1485 else if (XParseColor (dpy, cmap, color_name, &color)
1486 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1488 pixel = color.pixel;
1489 *closure_ret = (XtPointer) True;
1491 else
1493 String params[1];
1494 Cardinal nparams = 1;
1496 params[0] = color_name;
1497 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1498 "badValue", "cvt_string_to_pixel",
1499 "XtToolkitError", "Invalid color `%s'",
1500 params, &nparams);
1501 return False;
1504 if (to->addr != NULL)
1506 if (to->size < sizeof (Pixel))
1508 to->size = sizeof (Pixel);
1509 return False;
1512 *(Pixel *) to->addr = pixel;
1514 else
1516 cvt_string_to_pixel_value = pixel;
1517 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1520 to->size = sizeof (Pixel);
1521 return True;
1525 /* Free a pixel color which was previously allocated via
1526 cvt_string_to_pixel. This is registered as the destructor
1527 for this type of resource via XtSetTypeConverter.
1529 APP is the application context in which we work.
1531 TO is a pointer to an XrmValue holding the color to free.
1532 CLOSURE is the value we stored in CLOSURE_RET for this color
1533 in cvt_string_to_pixel.
1535 ARGS and NARGS are like for cvt_string_to_pixel. */
1537 static void
1538 cvt_pixel_dtor (app, to, closure, args, nargs)
1539 XtAppContext app;
1540 XrmValuePtr to;
1541 XtPointer closure;
1542 XrmValuePtr args;
1543 Cardinal *nargs;
1545 if (*nargs != 2)
1547 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1548 "XtToolkitError",
1549 "Screen and colormap arguments required",
1550 NULL, NULL);
1552 else if (closure != NULL)
1554 /* We did allocate the pixel, so free it. */
1555 Screen *screen = *(Screen **) args[0].addr;
1556 Colormap cmap = *(Colormap *) args[1].addr;
1557 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1558 (Pixel *) to->addr, 1);
1563 #endif /* USE_X_TOOLKIT */
1566 /* Value is an array of XColor structures for the contents of the
1567 color map of display DPY. Set *NCELLS to the size of the array.
1568 Note that this probably shouldn't be called for large color maps,
1569 say a 24-bit TrueColor map. */
1571 static const XColor *
1572 x_color_cells (dpy, ncells)
1573 Display *dpy;
1574 int *ncells;
1576 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1578 if (dpyinfo->color_cells == NULL)
1580 Screen *screen = dpyinfo->screen;
1581 int i;
1583 dpyinfo->ncolor_cells
1584 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1585 dpyinfo->color_cells
1586 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1587 * sizeof *dpyinfo->color_cells);
1589 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1590 dpyinfo->color_cells[i].pixel = i;
1592 XQueryColors (dpy, dpyinfo->cmap,
1593 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1596 *ncells = dpyinfo->ncolor_cells;
1597 return dpyinfo->color_cells;
1601 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1602 colors in COLORS. Use cached information, if available. */
1604 void
1605 x_query_colors (f, colors, ncolors)
1606 struct frame *f;
1607 XColor *colors;
1608 int ncolors;
1610 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1612 if (dpyinfo->color_cells)
1614 int i;
1615 for (i = 0; i < ncolors; ++i)
1617 unsigned long pixel = colors[i].pixel;
1618 xassert (pixel < dpyinfo->ncolor_cells);
1619 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1620 colors[i] = dpyinfo->color_cells[pixel];
1623 else
1624 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1628 /* On frame F, translate pixel color to RGB values for the color in
1629 COLOR. Use cached information, if available. */
1631 void
1632 x_query_color (f, color)
1633 struct frame *f;
1634 XColor *color;
1636 x_query_colors (f, color, 1);
1640 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1641 exact match can't be allocated, try the nearest color available.
1642 Value is non-zero if successful. Set *COLOR to the color
1643 allocated. */
1645 static int
1646 x_alloc_nearest_color_1 (dpy, cmap, color)
1647 Display *dpy;
1648 Colormap cmap;
1649 XColor *color;
1651 int rc;
1653 rc = XAllocColor (dpy, cmap, color);
1654 if (rc == 0)
1656 /* If we got to this point, the colormap is full, so we're going
1657 to try to get the next closest color. The algorithm used is
1658 a least-squares matching, which is what X uses for closest
1659 color matching with StaticColor visuals. */
1660 int nearest, i;
1661 unsigned long nearest_delta = ~0;
1662 int ncells;
1663 const XColor *cells = x_color_cells (dpy, &ncells);
1665 for (nearest = i = 0; i < ncells; ++i)
1667 long dred = (color->red >> 8) - (cells[i].red >> 8);
1668 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1669 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1670 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1672 if (delta < nearest_delta)
1674 nearest = i;
1675 nearest_delta = delta;
1679 color->red = cells[nearest].red;
1680 color->green = cells[nearest].green;
1681 color->blue = cells[nearest].blue;
1682 rc = XAllocColor (dpy, cmap, color);
1684 else
1686 /* If allocation succeeded, and the allocated pixel color is not
1687 equal to a cached pixel color recorded earlier, there was a
1688 change in the colormap, so clear the color cache. */
1689 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1690 XColor *cached_color;
1692 if (dpyinfo->color_cells
1693 && (cached_color = &dpyinfo->color_cells[color->pixel],
1694 (cached_color->red != color->red
1695 || cached_color->blue != color->blue
1696 || cached_color->green != color->green)))
1698 xfree (dpyinfo->color_cells);
1699 dpyinfo->color_cells = NULL;
1700 dpyinfo->ncolor_cells = 0;
1704 #ifdef DEBUG_X_COLORS
1705 if (rc)
1706 register_color (color->pixel);
1707 #endif /* DEBUG_X_COLORS */
1709 return rc;
1713 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1714 exact match can't be allocated, try the nearest color available.
1715 Value is non-zero if successful. Set *COLOR to the color
1716 allocated. */
1719 x_alloc_nearest_color (f, cmap, color)
1720 struct frame *f;
1721 Colormap cmap;
1722 XColor *color;
1724 gamma_correct (f, color);
1725 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1729 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1730 It's necessary to do this instead of just using PIXEL directly to
1731 get color reference counts right. */
1733 unsigned long
1734 x_copy_color (f, pixel)
1735 struct frame *f;
1736 unsigned long pixel;
1738 XColor color;
1740 color.pixel = pixel;
1741 BLOCK_INPUT;
1742 x_query_color (f, &color);
1743 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1744 UNBLOCK_INPUT;
1745 #ifdef DEBUG_X_COLORS
1746 register_color (pixel);
1747 #endif
1748 return color.pixel;
1752 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1753 It's necessary to do this instead of just using PIXEL directly to
1754 get color reference counts right. */
1756 unsigned long
1757 x_copy_dpy_color (dpy, cmap, pixel)
1758 Display *dpy;
1759 Colormap cmap;
1760 unsigned long pixel;
1762 XColor color;
1764 color.pixel = pixel;
1765 BLOCK_INPUT;
1766 XQueryColor (dpy, cmap, &color);
1767 XAllocColor (dpy, cmap, &color);
1768 UNBLOCK_INPUT;
1769 #ifdef DEBUG_X_COLORS
1770 register_color (pixel);
1771 #endif
1772 return color.pixel;
1776 /* Brightness beyond which a color won't have its highlight brightness
1777 boosted.
1779 Nominally, highlight colors for `3d' faces are calculated by
1780 brightening an object's color by a constant scale factor, but this
1781 doesn't yield good results for dark colors, so for colors who's
1782 brightness is less than this value (on a scale of 0-65535) have an
1783 use an additional additive factor.
1785 The value here is set so that the default menu-bar/mode-line color
1786 (grey75) will not have its highlights changed at all. */
1787 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1790 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1791 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1792 If this produces the same color as PIXEL, try a color where all RGB
1793 values have DELTA added. Return the allocated color in *PIXEL.
1794 DISPLAY is the X display, CMAP is the colormap to operate on.
1795 Value is non-zero if successful. */
1797 static int
1798 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1799 struct frame *f;
1800 Display *display;
1801 Colormap cmap;
1802 unsigned long *pixel;
1803 double factor;
1804 int delta;
1806 XColor color, new;
1807 long bright;
1808 int success_p;
1810 /* Get RGB color values. */
1811 color.pixel = *pixel;
1812 x_query_color (f, &color);
1814 /* Change RGB values by specified FACTOR. Avoid overflow! */
1815 xassert (factor >= 0);
1816 new.red = min (0xffff, factor * color.red);
1817 new.green = min (0xffff, factor * color.green);
1818 new.blue = min (0xffff, factor * color.blue);
1820 /* Calculate brightness of COLOR. */
1821 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1823 /* We only boost colors that are darker than
1824 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1825 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1826 /* Make an additive adjustment to NEW, because it's dark enough so
1827 that scaling by FACTOR alone isn't enough. */
1829 /* How far below the limit this color is (0 - 1, 1 being darker). */
1830 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1831 /* The additive adjustment. */
1832 int min_delta = delta * dimness * factor / 2;
1834 if (factor < 1)
1836 new.red = max (0, new.red - min_delta);
1837 new.green = max (0, new.green - min_delta);
1838 new.blue = max (0, new.blue - min_delta);
1840 else
1842 new.red = min (0xffff, min_delta + new.red);
1843 new.green = min (0xffff, min_delta + new.green);
1844 new.blue = min (0xffff, min_delta + new.blue);
1848 /* Try to allocate the color. */
1849 success_p = x_alloc_nearest_color (f, cmap, &new);
1850 if (success_p)
1852 if (new.pixel == *pixel)
1854 /* If we end up with the same color as before, try adding
1855 delta to the RGB values. */
1856 x_free_colors (f, &new.pixel, 1);
1858 new.red = min (0xffff, delta + color.red);
1859 new.green = min (0xffff, delta + color.green);
1860 new.blue = min (0xffff, delta + color.blue);
1861 success_p = x_alloc_nearest_color (f, cmap, &new);
1863 else
1864 success_p = 1;
1865 *pixel = new.pixel;
1868 return success_p;
1872 /* Set up the foreground color for drawing relief lines of glyph
1873 string S. RELIEF is a pointer to a struct relief containing the GC
1874 with which lines will be drawn. Use a color that is FACTOR or
1875 DELTA lighter or darker than the relief's background which is found
1876 in S->f->output_data.x->relief_background. If such a color cannot
1877 be allocated, use DEFAULT_PIXEL, instead. */
1879 static void
1880 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1881 struct frame *f;
1882 struct relief *relief;
1883 double factor;
1884 int delta;
1885 unsigned long default_pixel;
1887 XGCValues xgcv;
1888 struct x_output *di = f->output_data.x;
1889 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1890 unsigned long pixel;
1891 unsigned long background = di->relief_background;
1892 Colormap cmap = FRAME_X_COLORMAP (f);
1893 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1894 Display *dpy = FRAME_X_DISPLAY (f);
1896 xgcv.graphics_exposures = False;
1897 xgcv.line_width = 1;
1899 /* Free previously allocated color. The color cell will be reused
1900 when it has been freed as many times as it was allocated, so this
1901 doesn't affect faces using the same colors. */
1902 if (relief->gc
1903 && relief->allocated_p)
1905 x_free_colors (f, &relief->pixel, 1);
1906 relief->allocated_p = 0;
1909 /* Allocate new color. */
1910 xgcv.foreground = default_pixel;
1911 pixel = background;
1912 if (dpyinfo->n_planes != 1
1913 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1915 relief->allocated_p = 1;
1916 xgcv.foreground = relief->pixel = pixel;
1919 if (relief->gc == 0)
1921 xgcv.stipple = dpyinfo->gray;
1922 mask |= GCStipple;
1923 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1925 else
1926 XChangeGC (dpy, relief->gc, mask, &xgcv);
1930 /* Set up colors for the relief lines around glyph string S. */
1932 static void
1933 x_setup_relief_colors (s)
1934 struct glyph_string *s;
1936 struct x_output *di = s->f->output_data.x;
1937 unsigned long color;
1939 if (s->face->use_box_color_for_shadows_p)
1940 color = s->face->box_color;
1941 else if (s->first_glyph->type == IMAGE_GLYPH
1942 && s->img->pixmap
1943 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1944 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1945 else
1947 XGCValues xgcv;
1949 /* Get the background color of the face. */
1950 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
1951 color = xgcv.background;
1954 if (di->white_relief.gc == 0
1955 || color != di->relief_background)
1957 di->relief_background = color;
1958 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1959 WHITE_PIX_DEFAULT (s->f));
1960 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1961 BLACK_PIX_DEFAULT (s->f));
1966 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1967 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1968 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1969 relief. LEFT_P non-zero means draw a relief on the left side of
1970 the rectangle. RIGHT_P non-zero means draw a relief on the right
1971 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1972 when drawing. */
1974 static void
1975 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
1976 raised_p, left_p, right_p, clip_rect)
1977 struct frame *f;
1978 int left_x, top_y, right_x, bottom_y, width, left_p, right_p, raised_p;
1979 XRectangle *clip_rect;
1981 Display *dpy = FRAME_X_DISPLAY (f);
1982 Window window = FRAME_X_WINDOW (f);
1983 int i;
1984 GC gc;
1986 if (raised_p)
1987 gc = f->output_data.x->white_relief.gc;
1988 else
1989 gc = f->output_data.x->black_relief.gc;
1990 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1992 /* Top. */
1993 for (i = 0; i < width; ++i)
1994 XDrawLine (dpy, window, gc,
1995 left_x + i * left_p, top_y + i,
1996 right_x + 1 - i * right_p, top_y + i);
1998 /* Left. */
1999 if (left_p)
2000 for (i = 0; i < width; ++i)
2001 XDrawLine (dpy, window, gc,
2002 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2004 XSetClipMask (dpy, gc, None);
2005 if (raised_p)
2006 gc = f->output_data.x->black_relief.gc;
2007 else
2008 gc = f->output_data.x->white_relief.gc;
2009 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2011 /* Bottom. */
2012 for (i = 0; i < width; ++i)
2013 XDrawLine (dpy, window, gc,
2014 left_x + i * left_p, bottom_y - i,
2015 right_x + 1 - i * right_p, bottom_y - i);
2017 /* Right. */
2018 if (right_p)
2019 for (i = 0; i < width; ++i)
2020 XDrawLine (dpy, window, gc,
2021 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2023 XSetClipMask (dpy, gc, None);
2027 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2028 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2029 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2030 left side of the rectangle. RIGHT_P non-zero means draw a line
2031 on the right side of the rectangle. CLIP_RECT is the clipping
2032 rectangle to use when drawing. */
2034 static void
2035 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2036 left_p, right_p, clip_rect)
2037 struct glyph_string *s;
2038 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2039 XRectangle *clip_rect;
2041 XGCValues xgcv;
2043 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2044 XSetForeground (s->display, s->gc, s->face->box_color);
2045 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2047 /* Top. */
2048 XFillRectangle (s->display, s->window, s->gc,
2049 left_x, top_y, right_x - left_x + 1, width);
2051 /* Left. */
2052 if (left_p)
2053 XFillRectangle (s->display, s->window, s->gc,
2054 left_x, top_y, width, bottom_y - top_y + 1);
2056 /* Bottom. */
2057 XFillRectangle (s->display, s->window, s->gc,
2058 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2060 /* Right. */
2061 if (right_p)
2062 XFillRectangle (s->display, s->window, s->gc,
2063 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2065 XSetForeground (s->display, s->gc, xgcv.foreground);
2066 XSetClipMask (s->display, s->gc, None);
2070 /* Draw a box around glyph string S. */
2072 static void
2073 x_draw_glyph_string_box (s)
2074 struct glyph_string *s;
2076 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2077 int left_p, right_p;
2078 struct glyph *last_glyph;
2079 XRectangle clip_rect;
2081 last_x = window_box_right (s->w, s->area);
2082 if (s->row->full_width_p
2083 && !s->w->pseudo_window_p)
2085 last_x += WINDOW_RIGHT_SCROLL_BAR_AREA_WIDTH (s->w);
2086 if (s->area != RIGHT_MARGIN_AREA
2087 || WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (s->w))
2088 last_x += WINDOW_RIGHT_FRINGE_WIDTH (s->w);
2091 /* The glyph that may have a right box line. */
2092 last_glyph = (s->cmp || s->img
2093 ? s->first_glyph
2094 : s->first_glyph + s->nchars - 1);
2096 width = abs (s->face->box_line_width);
2097 raised_p = s->face->box == FACE_RAISED_BOX;
2098 left_x = s->x;
2099 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2100 ? last_x - 1
2101 : min (last_x, s->x + s->background_width) - 1);
2102 top_y = s->y;
2103 bottom_y = top_y + s->height - 1;
2105 left_p = (s->first_glyph->left_box_line_p
2106 || (s->hl == DRAW_MOUSE_FACE
2107 && (s->prev == NULL
2108 || s->prev->hl != s->hl)));
2109 right_p = (last_glyph->right_box_line_p
2110 || (s->hl == DRAW_MOUSE_FACE
2111 && (s->next == NULL
2112 || s->next->hl != s->hl)));
2114 get_glyph_string_clip_rect (s, &clip_rect);
2116 if (s->face->box == FACE_SIMPLE_BOX)
2117 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2118 left_p, right_p, &clip_rect);
2119 else
2121 x_setup_relief_colors (s);
2122 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2123 width, raised_p, left_p, right_p, &clip_rect);
2128 /* Draw foreground of image glyph string S. */
2130 static void
2131 x_draw_image_foreground (s)
2132 struct glyph_string *s;
2134 int x;
2135 int y = s->ybase - image_ascent (s->img, s->face);
2137 /* If first glyph of S has a left box line, start drawing it to the
2138 right of that line. */
2139 if (s->face->box != FACE_NO_BOX
2140 && s->first_glyph->left_box_line_p)
2141 x = s->x + abs (s->face->box_line_width);
2142 else
2143 x = s->x;
2145 /* If there is a margin around the image, adjust x- and y-position
2146 by that margin. */
2147 x += s->img->hmargin;
2148 y += s->img->vmargin;
2150 if (s->img->pixmap)
2152 if (s->img->mask)
2154 /* We can't set both a clip mask and use XSetClipRectangles
2155 because the latter also sets a clip mask. We also can't
2156 trust on the shape extension to be available
2157 (XShapeCombineRegion). So, compute the rectangle to draw
2158 manually. */
2159 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2160 | GCFunction);
2161 XGCValues xgcv;
2162 XRectangle clip_rect, image_rect, r;
2164 xgcv.clip_mask = s->img->mask;
2165 xgcv.clip_x_origin = x;
2166 xgcv.clip_y_origin = y;
2167 xgcv.function = GXcopy;
2168 XChangeGC (s->display, s->gc, mask, &xgcv);
2170 get_glyph_string_clip_rect (s, &clip_rect);
2171 image_rect.x = x;
2172 image_rect.y = y;
2173 image_rect.width = s->img->width;
2174 image_rect.height = s->img->height;
2175 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2176 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2177 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
2179 else
2181 XRectangle clip_rect, image_rect, r;
2183 get_glyph_string_clip_rect (s, &clip_rect);
2184 image_rect.x = x;
2185 image_rect.y = y;
2186 image_rect.width = s->img->width;
2187 image_rect.height = s->img->height;
2188 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2189 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2190 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
2192 /* When the image has a mask, we can expect that at
2193 least part of a mouse highlight or a block cursor will
2194 be visible. If the image doesn't have a mask, make
2195 a block cursor visible by drawing a rectangle around
2196 the image. I believe it's looking better if we do
2197 nothing here for mouse-face. */
2198 if (s->hl == DRAW_CURSOR)
2200 int r = s->img->relief;
2201 if (r < 0) r = -r;
2202 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2203 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
2207 else
2208 /* Draw a rectangle if image could not be loaded. */
2209 XDrawRectangle (s->display, s->window, s->gc, x, y,
2210 s->img->width - 1, s->img->height - 1);
2214 /* Draw a relief around the image glyph string S. */
2216 static void
2217 x_draw_image_relief (s)
2218 struct glyph_string *s;
2220 int x0, y0, x1, y1, thick, raised_p;
2221 XRectangle r;
2222 int x;
2223 int y = s->ybase - image_ascent (s->img, s->face);
2225 /* If first glyph of S has a left box line, start drawing it to the
2226 right of that line. */
2227 if (s->face->box != FACE_NO_BOX
2228 && s->first_glyph->left_box_line_p)
2229 x = s->x + abs (s->face->box_line_width);
2230 else
2231 x = s->x;
2233 /* If there is a margin around the image, adjust x- and y-position
2234 by that margin. */
2235 x += s->img->hmargin;
2236 y += s->img->vmargin;
2238 if (s->hl == DRAW_IMAGE_SUNKEN
2239 || s->hl == DRAW_IMAGE_RAISED)
2241 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2242 raised_p = s->hl == DRAW_IMAGE_RAISED;
2244 else
2246 thick = abs (s->img->relief);
2247 raised_p = s->img->relief > 0;
2250 x0 = x - thick;
2251 y0 = y - thick;
2252 x1 = x + s->img->width + thick - 1;
2253 y1 = y + s->img->height + thick - 1;
2255 x_setup_relief_colors (s);
2256 get_glyph_string_clip_rect (s, &r);
2257 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
2261 /* Draw the foreground of image glyph string S to PIXMAP. */
2263 static void
2264 x_draw_image_foreground_1 (s, pixmap)
2265 struct glyph_string *s;
2266 Pixmap pixmap;
2268 int x;
2269 int y = s->ybase - s->y - image_ascent (s->img, s->face);
2271 /* If first glyph of S has a left box line, start drawing it to the
2272 right of that line. */
2273 if (s->face->box != FACE_NO_BOX
2274 && s->first_glyph->left_box_line_p)
2275 x = abs (s->face->box_line_width);
2276 else
2277 x = 0;
2279 /* If there is a margin around the image, adjust x- and y-position
2280 by that margin. */
2281 x += s->img->hmargin;
2282 y += s->img->vmargin;
2284 if (s->img->pixmap)
2286 if (s->img->mask)
2288 /* We can't set both a clip mask and use XSetClipRectangles
2289 because the latter also sets a clip mask. We also can't
2290 trust on the shape extension to be available
2291 (XShapeCombineRegion). So, compute the rectangle to draw
2292 manually. */
2293 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2294 | GCFunction);
2295 XGCValues xgcv;
2297 xgcv.clip_mask = s->img->mask;
2298 xgcv.clip_x_origin = x;
2299 xgcv.clip_y_origin = y;
2300 xgcv.function = GXcopy;
2301 XChangeGC (s->display, s->gc, mask, &xgcv);
2303 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2304 0, 0, s->img->width, s->img->height, x, y);
2305 XSetClipMask (s->display, s->gc, None);
2307 else
2309 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2310 0, 0, s->img->width, s->img->height, x, y);
2312 /* When the image has a mask, we can expect that at
2313 least part of a mouse highlight or a block cursor will
2314 be visible. If the image doesn't have a mask, make
2315 a block cursor visible by drawing a rectangle around
2316 the image. I believe it's looking better if we do
2317 nothing here for mouse-face. */
2318 if (s->hl == DRAW_CURSOR)
2320 int r = s->img->relief;
2321 if (r < 0) r = -r;
2322 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2323 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
2327 else
2328 /* Draw a rectangle if image could not be loaded. */
2329 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2330 s->img->width - 1, s->img->height - 1);
2334 /* Draw part of the background of glyph string S. X, Y, W, and H
2335 give the rectangle to draw. */
2337 static void
2338 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2339 struct glyph_string *s;
2340 int x, y, w, h;
2342 if (s->stippled_p)
2344 /* Fill background with a stipple pattern. */
2345 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2346 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2347 XSetFillStyle (s->display, s->gc, FillSolid);
2349 else
2350 x_clear_glyph_string_rect (s, x, y, w, h);
2354 /* Draw image glyph string S.
2356 s->y
2357 s->x +-------------------------
2358 | s->face->box
2360 | +-------------------------
2361 | | s->img->margin
2363 | | +-------------------
2364 | | | the image
2368 static void
2369 x_draw_image_glyph_string (s)
2370 struct glyph_string *s;
2372 int x, y;
2373 int box_line_hwidth = abs (s->face->box_line_width);
2374 int box_line_vwidth = max (s->face->box_line_width, 0);
2375 int height;
2376 Pixmap pixmap = None;
2378 height = s->height - 2 * box_line_vwidth;
2381 /* Fill background with face under the image. Do it only if row is
2382 taller than image or if image has a clip mask to reduce
2383 flickering. */
2384 s->stippled_p = s->face->stipple != 0;
2385 if (height > s->img->height
2386 || s->img->hmargin
2387 || s->img->vmargin
2388 || s->img->mask
2389 || s->img->pixmap == 0
2390 || s->width != s->background_width)
2392 if (box_line_hwidth && s->first_glyph->left_box_line_p)
2393 x = s->x + box_line_hwidth;
2394 else
2395 x = s->x;
2397 y = s->y + box_line_vwidth;
2399 if (s->img->mask)
2401 /* Create a pixmap as large as the glyph string. Fill it
2402 with the background color. Copy the image to it, using
2403 its mask. Copy the temporary pixmap to the display. */
2404 Screen *screen = FRAME_X_SCREEN (s->f);
2405 int depth = DefaultDepthOfScreen (screen);
2407 /* Create a pixmap as large as the glyph string. */
2408 pixmap = XCreatePixmap (s->display, s->window,
2409 s->background_width,
2410 s->height, depth);
2412 /* Don't clip in the following because we're working on the
2413 pixmap. */
2414 XSetClipMask (s->display, s->gc, None);
2416 /* Fill the pixmap with the background color/stipple. */
2417 if (s->stippled_p)
2419 /* Fill background with a stipple pattern. */
2420 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2421 XFillRectangle (s->display, pixmap, s->gc,
2422 0, 0, s->background_width, s->height);
2423 XSetFillStyle (s->display, s->gc, FillSolid);
2425 else
2427 XGCValues xgcv;
2428 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2429 &xgcv);
2430 XSetForeground (s->display, s->gc, xgcv.background);
2431 XFillRectangle (s->display, pixmap, s->gc,
2432 0, 0, s->background_width, s->height);
2433 XSetForeground (s->display, s->gc, xgcv.foreground);
2436 else
2437 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2439 s->background_filled_p = 1;
2442 /* Draw the foreground. */
2443 if (pixmap != None)
2445 x_draw_image_foreground_1 (s, pixmap);
2446 x_set_glyph_string_clipping (s);
2447 XCopyArea (s->display, pixmap, s->window, s->gc,
2448 0, 0, s->background_width, s->height, s->x, s->y);
2449 XFreePixmap (s->display, pixmap);
2451 else
2452 x_draw_image_foreground (s);
2454 /* If we must draw a relief around the image, do it. */
2455 if (s->img->relief
2456 || s->hl == DRAW_IMAGE_RAISED
2457 || s->hl == DRAW_IMAGE_SUNKEN)
2458 x_draw_image_relief (s);
2462 /* Draw stretch glyph string S. */
2464 static void
2465 x_draw_stretch_glyph_string (s)
2466 struct glyph_string *s;
2468 xassert (s->first_glyph->type == STRETCH_GLYPH);
2469 s->stippled_p = s->face->stipple != 0;
2471 if (s->hl == DRAW_CURSOR
2472 && !x_stretch_cursor_p)
2474 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2475 as wide as the stretch glyph. */
2476 int width = min (FRAME_COLUMN_WIDTH (s->f), s->background_width);
2478 /* Draw cursor. */
2479 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
2481 /* Clear rest using the GC of the original non-cursor face. */
2482 if (width < s->background_width)
2484 int x = s->x + width, y = s->y;
2485 int w = s->background_width - width, h = s->height;
2486 XRectangle r;
2487 GC gc;
2489 if (s->row->mouse_face_p
2490 && cursor_in_mouse_face_p (s->w))
2492 x_set_mouse_face_gc (s);
2493 gc = s->gc;
2495 else
2496 gc = s->face->gc;
2498 get_glyph_string_clip_rect (s, &r);
2499 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2501 if (s->face->stipple)
2503 /* Fill background with a stipple pattern. */
2504 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2505 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2506 XSetFillStyle (s->display, gc, FillSolid);
2508 else
2510 XGCValues xgcv;
2511 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2512 XSetForeground (s->display, gc, xgcv.background);
2513 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2514 XSetForeground (s->display, gc, xgcv.foreground);
2518 else if (!s->background_filled_p)
2519 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
2520 s->height);
2522 s->background_filled_p = 1;
2526 /* Draw glyph string S. */
2528 static void
2529 x_draw_glyph_string (s)
2530 struct glyph_string *s;
2532 int relief_drawn_p = 0;
2534 /* If S draws into the background of its successor, draw the
2535 background of the successor first so that S can draw into it.
2536 This makes S->next use XDrawString instead of XDrawImageString. */
2537 if (s->next && s->right_overhang && !s->for_overlaps_p)
2539 xassert (s->next->img == NULL);
2540 x_set_glyph_string_gc (s->next);
2541 x_set_glyph_string_clipping (s->next);
2542 x_draw_glyph_string_background (s->next, 1);
2545 /* Set up S->gc, set clipping and draw S. */
2546 x_set_glyph_string_gc (s);
2548 /* Draw relief (if any) in advance for char/composition so that the
2549 glyph string can be drawn over it. */
2550 if (!s->for_overlaps_p
2551 && s->face->box != FACE_NO_BOX
2552 && (s->first_glyph->type == CHAR_GLYPH
2553 || s->first_glyph->type == COMPOSITE_GLYPH))
2556 x_set_glyph_string_clipping (s);
2557 x_draw_glyph_string_background (s, 1);
2558 x_draw_glyph_string_box (s);
2559 x_set_glyph_string_clipping (s);
2560 relief_drawn_p = 1;
2562 else
2563 x_set_glyph_string_clipping (s);
2565 switch (s->first_glyph->type)
2567 case IMAGE_GLYPH:
2568 x_draw_image_glyph_string (s);
2569 break;
2571 case STRETCH_GLYPH:
2572 x_draw_stretch_glyph_string (s);
2573 break;
2575 case CHAR_GLYPH:
2576 if (s->for_overlaps_p)
2577 s->background_filled_p = 1;
2578 else
2579 x_draw_glyph_string_background (s, 0);
2580 x_draw_glyph_string_foreground (s);
2581 break;
2583 case COMPOSITE_GLYPH:
2584 if (s->for_overlaps_p || s->gidx > 0)
2585 s->background_filled_p = 1;
2586 else
2587 x_draw_glyph_string_background (s, 1);
2588 x_draw_composite_glyph_string_foreground (s);
2589 break;
2591 default:
2592 abort ();
2595 if (!s->for_overlaps_p)
2597 /* Draw underline. */
2598 if (s->face->underline_p)
2600 unsigned long tem, h;
2601 int y;
2603 /* Get the underline thickness. Default is 1 pixel. */
2604 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
2605 h = 1;
2607 /* Get the underline position. This is the recommended
2608 vertical offset in pixels from the baseline to the top of
2609 the underline. This is a signed value according to the
2610 specs, and its default is
2612 ROUND ((maximum descent) / 2), with
2613 ROUND(x) = floor (x + 0.5) */
2615 if (x_use_underline_position_properties
2616 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
2617 y = s->ybase + (long) tem;
2618 else if (s->face->font)
2619 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2620 else
2621 y = s->y + s->height - h;
2623 if (s->face->underline_defaulted_p)
2624 XFillRectangle (s->display, s->window, s->gc,
2625 s->x, y, s->width, h);
2626 else
2628 XGCValues xgcv;
2629 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2630 XSetForeground (s->display, s->gc, s->face->underline_color);
2631 XFillRectangle (s->display, s->window, s->gc,
2632 s->x, y, s->width, h);
2633 XSetForeground (s->display, s->gc, xgcv.foreground);
2637 /* Draw overline. */
2638 if (s->face->overline_p)
2640 unsigned long dy = 0, h = 1;
2642 if (s->face->overline_color_defaulted_p)
2643 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2644 s->width, h);
2645 else
2647 XGCValues xgcv;
2648 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2649 XSetForeground (s->display, s->gc, s->face->overline_color);
2650 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2651 s->width, h);
2652 XSetForeground (s->display, s->gc, xgcv.foreground);
2656 /* Draw strike-through. */
2657 if (s->face->strike_through_p)
2659 unsigned long h = 1;
2660 unsigned long dy = (s->height - h) / 2;
2662 if (s->face->strike_through_color_defaulted_p)
2663 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2664 s->width, h);
2665 else
2667 XGCValues xgcv;
2668 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2669 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2670 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2671 s->width, h);
2672 XSetForeground (s->display, s->gc, xgcv.foreground);
2676 /* Draw relief if not yet drawn. */
2677 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2678 x_draw_glyph_string_box (s);
2681 /* Reset clipping. */
2682 XSetClipMask (s->display, s->gc, None);
2685 /* Shift display to make room for inserted glyphs. */
2687 void
2688 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2689 struct frame *f;
2690 int x, y, width, height, shift_by;
2692 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2693 f->output_data.x->normal_gc,
2694 x, y, width, height,
2695 x + shift_by, y);
2698 /* Delete N glyphs at the nominal cursor position. Not implemented
2699 for X frames. */
2701 static void
2702 x_delete_glyphs (n)
2703 register int n;
2705 abort ();
2709 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2710 If they are <= 0, this is probably an error. */
2712 void
2713 x_clear_area (dpy, window, x, y, width, height, exposures)
2714 Display *dpy;
2715 Window window;
2716 int x, y;
2717 int width, height;
2718 int exposures;
2720 xassert (width > 0 && height > 0);
2721 XClearArea (dpy, window, x, y, width, height, exposures);
2725 /* Clear entire frame. If updating_frame is non-null, clear that
2726 frame. Otherwise clear the selected frame. */
2728 static void
2729 x_clear_frame ()
2731 struct frame *f;
2733 if (updating_frame)
2734 f = updating_frame;
2735 else
2736 f = SELECTED_FRAME ();
2738 /* Clearing the frame will erase any cursor, so mark them all as no
2739 longer visible. */
2740 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2741 output_cursor.hpos = output_cursor.vpos = 0;
2742 output_cursor.x = -1;
2744 /* We don't set the output cursor here because there will always
2745 follow an explicit cursor_to. */
2746 BLOCK_INPUT;
2747 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2749 /* We have to clear the scroll bars, too. If we have changed
2750 colors or something like that, then they should be notified. */
2751 x_scroll_bar_clear (f);
2753 XFlush (FRAME_X_DISPLAY (f));
2755 #ifdef USE_GTK
2756 xg_frame_cleared (f);
2757 #endif
2759 UNBLOCK_INPUT;
2764 /* Invert the middle quarter of the frame for .15 sec. */
2766 /* We use the select system call to do the waiting, so we have to make
2767 sure it's available. If it isn't, we just won't do visual bells. */
2769 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2772 /* Subtract the `struct timeval' values X and Y, storing the result in
2773 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2775 static int
2776 timeval_subtract (result, x, y)
2777 struct timeval *result, x, y;
2779 /* Perform the carry for the later subtraction by updating y. This
2780 is safer because on some systems the tv_sec member is unsigned. */
2781 if (x.tv_usec < y.tv_usec)
2783 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2784 y.tv_usec -= 1000000 * nsec;
2785 y.tv_sec += nsec;
2788 if (x.tv_usec - y.tv_usec > 1000000)
2790 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2791 y.tv_usec += 1000000 * nsec;
2792 y.tv_sec -= nsec;
2795 /* Compute the time remaining to wait. tv_usec is certainly
2796 positive. */
2797 result->tv_sec = x.tv_sec - y.tv_sec;
2798 result->tv_usec = x.tv_usec - y.tv_usec;
2800 /* Return indication of whether the result should be considered
2801 negative. */
2802 return x.tv_sec < y.tv_sec;
2805 void
2806 XTflash (f)
2807 struct frame *f;
2809 BLOCK_INPUT;
2812 GC gc;
2814 /* Create a GC that will use the GXxor function to flip foreground
2815 pixels into background pixels. */
2817 XGCValues values;
2819 values.function = GXxor;
2820 values.foreground = (f->output_data.x->foreground_pixel
2821 ^ f->output_data.x->background_pixel);
2823 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2824 GCFunction | GCForeground, &values);
2828 /* Get the height not including a menu bar widget. */
2829 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
2830 /* Height of each line to flash. */
2831 int flash_height = FRAME_LINE_HEIGHT (f);
2832 /* These will be the left and right margins of the rectangles. */
2833 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
2834 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
2836 int width;
2838 /* Don't flash the area between a scroll bar and the frame
2839 edge it is next to. */
2840 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
2842 case vertical_scroll_bar_left:
2843 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2844 break;
2846 case vertical_scroll_bar_right:
2847 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2848 break;
2850 default:
2851 break;
2854 width = flash_right - flash_left;
2856 /* If window is tall, flash top and bottom line. */
2857 if (height > 3 * FRAME_LINE_HEIGHT (f))
2859 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2860 flash_left,
2861 (FRAME_INTERNAL_BORDER_WIDTH (f)
2862 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
2863 width, flash_height);
2864 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2865 flash_left,
2866 (height - flash_height
2867 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2868 width, flash_height);
2870 else
2871 /* If it is short, flash it all. */
2872 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2873 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2874 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2876 x_flush (f);
2879 struct timeval wakeup;
2881 EMACS_GET_TIME (wakeup);
2883 /* Compute time to wait until, propagating carry from usecs. */
2884 wakeup.tv_usec += 150000;
2885 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
2886 wakeup.tv_usec %= 1000000;
2888 /* Keep waiting until past the time wakeup or any input gets
2889 available. */
2890 while (! detect_input_pending ())
2892 struct timeval current;
2893 struct timeval timeout;
2895 EMACS_GET_TIME (current);
2897 /* Break if result would be negative. */
2898 if (timeval_subtract (&current, wakeup, current))
2899 break;
2901 /* How long `select' should wait. */
2902 timeout.tv_sec = 0;
2903 timeout.tv_usec = 10000;
2905 /* Try to wait that long--but we might wake up sooner. */
2906 select (0, NULL, NULL, NULL, &timeout);
2910 /* If window is tall, flash top and bottom line. */
2911 if (height > 3 * FRAME_LINE_HEIGHT (f))
2913 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2914 flash_left,
2915 (FRAME_INTERNAL_BORDER_WIDTH (f)
2916 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
2917 width, flash_height);
2918 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2919 flash_left,
2920 (height - flash_height
2921 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2922 width, flash_height);
2924 else
2925 /* If it is short, flash it all. */
2926 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2927 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2928 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2930 XFreeGC (FRAME_X_DISPLAY (f), gc);
2931 x_flush (f);
2935 UNBLOCK_INPUT;
2938 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
2941 /* Make audible bell. */
2943 void
2944 XTring_bell ()
2946 struct frame *f = SELECTED_FRAME ();
2948 if (FRAME_X_DISPLAY (f))
2950 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2951 if (visible_bell)
2952 XTflash (f);
2953 else
2954 #endif
2956 BLOCK_INPUT;
2957 XBell (FRAME_X_DISPLAY (f), 0);
2958 XFlush (FRAME_X_DISPLAY (f));
2959 UNBLOCK_INPUT;
2965 /* Specify how many text lines, from the top of the window,
2966 should be affected by insert-lines and delete-lines operations.
2967 This, and those operations, are used only within an update
2968 that is bounded by calls to x_update_begin and x_update_end. */
2970 static void
2971 XTset_terminal_window (n)
2972 register int n;
2974 /* This function intentionally left blank. */
2979 /***********************************************************************
2980 Line Dance
2981 ***********************************************************************/
2983 /* Perform an insert-lines or delete-lines operation, inserting N
2984 lines or deleting -N lines at vertical position VPOS. */
2986 static void
2987 x_ins_del_lines (vpos, n)
2988 int vpos, n;
2990 abort ();
2994 /* Scroll part of the display as described by RUN. */
2996 static void
2997 x_scroll_run (w, run)
2998 struct window *w;
2999 struct run *run;
3001 struct frame *f = XFRAME (w->frame);
3002 int x, y, width, height, from_y, to_y, bottom_y;
3004 /* Get frame-relative bounding box of the text display area of W,
3005 without mode lines. Include in this box the left and right
3006 fringe of W. */
3007 window_box (w, -1, &x, &y, &width, &height);
3009 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3010 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3011 bottom_y = y + height;
3013 if (to_y < from_y)
3015 /* Scrolling up. Make sure we don't copy part of the mode
3016 line at the bottom. */
3017 if (from_y + run->height > bottom_y)
3018 height = bottom_y - from_y;
3019 else
3020 height = run->height;
3022 else
3024 /* Scolling down. Make sure we don't copy over the mode line.
3025 at the bottom. */
3026 if (to_y + run->height > bottom_y)
3027 height = bottom_y - to_y;
3028 else
3029 height = run->height;
3032 BLOCK_INPUT;
3034 /* Cursor off. Will be switched on again in x_update_window_end. */
3035 updated_window = w;
3036 x_clear_cursor (w);
3038 XCopyArea (FRAME_X_DISPLAY (f),
3039 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3040 f->output_data.x->normal_gc,
3041 x, from_y,
3042 width, height,
3043 x, to_y);
3045 UNBLOCK_INPUT;
3050 /***********************************************************************
3051 Exposure Events
3052 ***********************************************************************/
3055 static void
3056 frame_highlight (f)
3057 struct frame *f;
3059 /* We used to only do this if Vx_no_window_manager was non-nil, but
3060 the ICCCM (section 4.1.6) says that the window's border pixmap
3061 and border pixel are window attributes which are "private to the
3062 client", so we can always change it to whatever we want. */
3063 BLOCK_INPUT;
3064 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3065 f->output_data.x->border_pixel);
3066 UNBLOCK_INPUT;
3067 x_update_cursor (f, 1);
3070 static void
3071 frame_unhighlight (f)
3072 struct frame *f;
3074 /* We used to only do this if Vx_no_window_manager was non-nil, but
3075 the ICCCM (section 4.1.6) says that the window's border pixmap
3076 and border pixel are window attributes which are "private to the
3077 client", so we can always change it to whatever we want. */
3078 BLOCK_INPUT;
3079 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3080 f->output_data.x->border_tile);
3081 UNBLOCK_INPUT;
3082 x_update_cursor (f, 1);
3085 /* The focus has changed. Update the frames as necessary to reflect
3086 the new situation. Note that we can't change the selected frame
3087 here, because the Lisp code we are interrupting might become confused.
3088 Each event gets marked with the frame in which it occurred, so the
3089 Lisp code can tell when the switch took place by examining the events. */
3091 static void
3092 x_new_focus_frame (dpyinfo, frame)
3093 struct x_display_info *dpyinfo;
3094 struct frame *frame;
3096 struct frame *old_focus = dpyinfo->x_focus_frame;
3098 if (frame != dpyinfo->x_focus_frame)
3100 /* Set this before calling other routines, so that they see
3101 the correct value of x_focus_frame. */
3102 dpyinfo->x_focus_frame = frame;
3104 if (old_focus && old_focus->auto_lower)
3105 x_lower_frame (old_focus);
3107 #if 0
3108 selected_frame = frame;
3109 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3110 selected_frame);
3111 Fselect_window (selected_frame->selected_window, Qnil);
3112 choose_minibuf_frame ();
3113 #endif /* ! 0 */
3115 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3116 pending_autoraise_frame = dpyinfo->x_focus_frame;
3117 else
3118 pending_autoraise_frame = 0;
3121 x_frame_rehighlight (dpyinfo);
3124 /* Handle FocusIn and FocusOut state changes for FRAME.
3125 If FRAME has focus and there exists more than one frame, puts
3126 a FOCUS_IN_EVENT into BUFP.
3127 Returns number of events inserted into BUFP. */
3129 static int
3130 x_focus_changed (type, state, dpyinfo, frame, bufp, numchars)
3131 int type;
3132 int state;
3133 struct x_display_info *dpyinfo;
3134 struct frame *frame;
3135 struct input_event *bufp;
3136 int numchars;
3138 int nr_events = 0;
3140 if (type == FocusIn)
3142 if (dpyinfo->x_focus_event_frame != frame)
3144 x_new_focus_frame (dpyinfo, frame);
3145 dpyinfo->x_focus_event_frame = frame;
3147 /* Don't stop displaying the initial startup message
3148 for a switch-frame event we don't need. */
3149 if (numchars > 0
3150 && GC_NILP (Vterminal_frame)
3151 && GC_CONSP (Vframe_list)
3152 && !GC_NILP (XCDR (Vframe_list)))
3154 bufp->kind = FOCUS_IN_EVENT;
3155 XSETFRAME (bufp->frame_or_window, frame);
3156 bufp->arg = Qnil;
3157 ++bufp;
3158 numchars--;
3159 ++nr_events;
3163 frame->output_data.x->focus_state |= state;
3165 #ifdef HAVE_X_I18N
3166 if (FRAME_XIC (frame))
3167 XSetICFocus (FRAME_XIC (frame));
3168 #endif
3170 else if (type == FocusOut)
3172 frame->output_data.x->focus_state &= ~state;
3174 if (dpyinfo->x_focus_event_frame == frame)
3176 dpyinfo->x_focus_event_frame = 0;
3177 x_new_focus_frame (dpyinfo, 0);
3180 #ifdef HAVE_X_I18N
3181 if (FRAME_XIC (frame))
3182 XUnsetICFocus (FRAME_XIC (frame));
3183 #endif
3186 return nr_events;
3189 /* The focus may have changed. Figure out if it is a real focus change,
3190 by checking both FocusIn/Out and Enter/LeaveNotify events.
3192 Returns number of events inserted into BUFP. */
3194 static int
3195 x_detect_focus_change (dpyinfo, event, bufp, numchars)
3196 struct x_display_info *dpyinfo;
3197 XEvent *event;
3198 struct input_event *bufp;
3199 int numchars;
3201 struct frame *frame;
3202 int nr_events = 0;
3204 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3205 if (! frame) return nr_events;
3207 switch (event->type)
3209 case EnterNotify:
3210 case LeaveNotify:
3211 if (event->xcrossing.detail != NotifyInferior
3212 && event->xcrossing.focus
3213 && ! (frame->output_data.x->focus_state & FOCUS_EXPLICIT))
3214 nr_events = x_focus_changed ((event->type == EnterNotify
3215 ? FocusIn : FocusOut),
3216 FOCUS_IMPLICIT,
3217 dpyinfo,
3218 frame,
3219 bufp,
3220 numchars);
3221 break;
3223 case FocusIn:
3224 case FocusOut:
3225 nr_events = x_focus_changed (event->type,
3226 (event->xfocus.detail == NotifyPointer
3227 ? FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3228 dpyinfo,
3229 frame,
3230 bufp,
3231 numchars);
3232 break;
3235 return nr_events;
3239 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3241 void
3242 x_mouse_leave (dpyinfo)
3243 struct x_display_info *dpyinfo;
3245 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3248 /* The focus has changed, or we have redirected a frame's focus to
3249 another frame (this happens when a frame uses a surrogate
3250 mini-buffer frame). Shift the highlight as appropriate.
3252 The FRAME argument doesn't necessarily have anything to do with which
3253 frame is being highlighted or un-highlighted; we only use it to find
3254 the appropriate X display info. */
3256 static void
3257 XTframe_rehighlight (frame)
3258 struct frame *frame;
3260 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3263 static void
3264 x_frame_rehighlight (dpyinfo)
3265 struct x_display_info *dpyinfo;
3267 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3269 if (dpyinfo->x_focus_frame)
3271 dpyinfo->x_highlight_frame
3272 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3273 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3274 : dpyinfo->x_focus_frame);
3275 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3277 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3278 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3281 else
3282 dpyinfo->x_highlight_frame = 0;
3284 if (dpyinfo->x_highlight_frame != old_highlight)
3286 if (old_highlight)
3287 frame_unhighlight (old_highlight);
3288 if (dpyinfo->x_highlight_frame)
3289 frame_highlight (dpyinfo->x_highlight_frame);
3295 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3297 /* Initialize mode_switch_bit and modifier_meaning. */
3298 static void
3299 x_find_modifier_meanings (dpyinfo)
3300 struct x_display_info *dpyinfo;
3302 int min_code, max_code;
3303 KeySym *syms;
3304 int syms_per_code;
3305 XModifierKeymap *mods;
3307 dpyinfo->meta_mod_mask = 0;
3308 dpyinfo->shift_lock_mask = 0;
3309 dpyinfo->alt_mod_mask = 0;
3310 dpyinfo->super_mod_mask = 0;
3311 dpyinfo->hyper_mod_mask = 0;
3313 #ifdef HAVE_X11R4
3314 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3315 #else
3316 min_code = dpyinfo->display->min_keycode;
3317 max_code = dpyinfo->display->max_keycode;
3318 #endif
3320 syms = XGetKeyboardMapping (dpyinfo->display,
3321 min_code, max_code - min_code + 1,
3322 &syms_per_code);
3323 mods = XGetModifierMapping (dpyinfo->display);
3325 /* Scan the modifier table to see which modifier bits the Meta and
3326 Alt keysyms are on. */
3328 int row, col; /* The row and column in the modifier table. */
3330 for (row = 3; row < 8; row++)
3331 for (col = 0; col < mods->max_keypermod; col++)
3333 KeyCode code
3334 = mods->modifiermap[(row * mods->max_keypermod) + col];
3336 /* Zeroes are used for filler. Skip them. */
3337 if (code == 0)
3338 continue;
3340 /* Are any of this keycode's keysyms a meta key? */
3342 int code_col;
3344 for (code_col = 0; code_col < syms_per_code; code_col++)
3346 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3348 switch (sym)
3350 case XK_Meta_L:
3351 case XK_Meta_R:
3352 dpyinfo->meta_mod_mask |= (1 << row);
3353 break;
3355 case XK_Alt_L:
3356 case XK_Alt_R:
3357 dpyinfo->alt_mod_mask |= (1 << row);
3358 break;
3360 case XK_Hyper_L:
3361 case XK_Hyper_R:
3362 dpyinfo->hyper_mod_mask |= (1 << row);
3363 break;
3365 case XK_Super_L:
3366 case XK_Super_R:
3367 dpyinfo->super_mod_mask |= (1 << row);
3368 break;
3370 case XK_Shift_Lock:
3371 /* Ignore this if it's not on the lock modifier. */
3372 if ((1 << row) == LockMask)
3373 dpyinfo->shift_lock_mask = LockMask;
3374 break;
3381 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3382 if (! dpyinfo->meta_mod_mask)
3384 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3385 dpyinfo->alt_mod_mask = 0;
3388 /* If some keys are both alt and meta,
3389 make them just meta, not alt. */
3390 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3392 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3395 XFree ((char *) syms);
3396 XFreeModifiermap (mods);
3399 /* Convert between the modifier bits X uses and the modifier bits
3400 Emacs uses. */
3402 static unsigned int
3403 x_x_to_emacs_modifiers (dpyinfo, state)
3404 struct x_display_info *dpyinfo;
3405 unsigned int state;
3407 EMACS_UINT mod_meta = meta_modifier;
3408 EMACS_UINT mod_alt = alt_modifier;
3409 EMACS_UINT mod_hyper = hyper_modifier;
3410 EMACS_UINT mod_super = super_modifier;
3411 Lisp_Object tem;
3413 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3414 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3415 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3416 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3417 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3418 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3419 tem = Fget (Vx_super_keysym, Qmodifier_value);
3420 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3423 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3424 | ((state & ControlMask) ? ctrl_modifier : 0)
3425 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3426 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3427 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3428 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3431 static unsigned int
3432 x_emacs_to_x_modifiers (dpyinfo, state)
3433 struct x_display_info *dpyinfo;
3434 unsigned int state;
3436 EMACS_UINT mod_meta = meta_modifier;
3437 EMACS_UINT mod_alt = alt_modifier;
3438 EMACS_UINT mod_hyper = hyper_modifier;
3439 EMACS_UINT mod_super = super_modifier;
3441 Lisp_Object tem;
3443 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3444 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3445 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3446 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3447 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3448 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3449 tem = Fget (Vx_super_keysym, Qmodifier_value);
3450 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3453 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3454 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3455 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3456 | ((state & shift_modifier) ? ShiftMask : 0)
3457 | ((state & ctrl_modifier) ? ControlMask : 0)
3458 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3461 /* Convert a keysym to its name. */
3463 char *
3464 x_get_keysym_name (keysym)
3465 KeySym keysym;
3467 char *value;
3469 BLOCK_INPUT;
3470 value = XKeysymToString (keysym);
3471 UNBLOCK_INPUT;
3473 return value;
3478 /* Mouse clicks and mouse movement. Rah. */
3480 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3482 If the event is a button press, then note that we have grabbed
3483 the mouse. */
3485 static Lisp_Object
3486 construct_mouse_click (result, event, f)
3487 struct input_event *result;
3488 XButtonEvent *event;
3489 struct frame *f;
3491 /* Make the event type NO_EVENT; we'll change that when we decide
3492 otherwise. */
3493 result->kind = MOUSE_CLICK_EVENT;
3494 result->code = event->button - Button1;
3495 result->timestamp = event->time;
3496 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3497 event->state)
3498 | (event->type == ButtonRelease
3499 ? up_modifier
3500 : down_modifier));
3502 XSETINT (result->x, event->x);
3503 XSETINT (result->y, event->y);
3504 XSETFRAME (result->frame_or_window, f);
3505 result->arg = Qnil;
3506 return Qnil;
3510 /* Function to report a mouse movement to the mainstream Emacs code.
3511 The input handler calls this.
3513 We have received a mouse movement event, which is given in *event.
3514 If the mouse is over a different glyph than it was last time, tell
3515 the mainstream emacs code by setting mouse_moved. If not, ask for
3516 another motion event, so we can check again the next time it moves. */
3518 static XMotionEvent last_mouse_motion_event;
3519 static Lisp_Object last_mouse_motion_frame;
3521 static void
3522 note_mouse_movement (frame, event)
3523 FRAME_PTR frame;
3524 XMotionEvent *event;
3526 last_mouse_movement_time = event->time;
3527 last_mouse_motion_event = *event;
3528 XSETFRAME (last_mouse_motion_frame, frame);
3530 if (event->window != FRAME_X_WINDOW (frame))
3532 frame->mouse_moved = 1;
3533 last_mouse_scroll_bar = Qnil;
3534 note_mouse_highlight (frame, -1, -1);
3537 /* Has the mouse moved off the glyph it was on at the last sighting? */
3538 else if (event->x < last_mouse_glyph.x
3539 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3540 || event->y < last_mouse_glyph.y
3541 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3543 frame->mouse_moved = 1;
3544 last_mouse_scroll_bar = Qnil;
3545 note_mouse_highlight (frame, event->x, event->y);
3550 /************************************************************************
3551 Mouse Face
3552 ************************************************************************/
3554 static void
3555 redo_mouse_highlight ()
3557 if (!NILP (last_mouse_motion_frame)
3558 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3559 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3560 last_mouse_motion_event.x,
3561 last_mouse_motion_event.y);
3565 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
3568 /* Try to determine frame pixel position and size of the glyph under
3569 frame pixel coordinates X/Y on frame F . Return the position and
3570 size in *RECT. Value is non-zero if we could compute these
3571 values. */
3573 static int
3574 glyph_rect (f, x, y, rect)
3575 struct frame *f;
3576 int x, y;
3577 XRectangle *rect;
3579 Lisp_Object window;
3580 int found = 0;
3582 window = window_from_coordinates (f, x, y, 0, &x, &y, 0);
3583 if (!NILP (window))
3585 struct window *w = XWINDOW (window);
3586 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
3587 struct glyph_row *end = r + w->current_matrix->nrows - 1;
3589 for (; !found && r < end && r->enabled_p; ++r)
3590 if (r->y >= y)
3592 struct glyph *g = r->glyphs[TEXT_AREA];
3593 struct glyph *end = g + r->used[TEXT_AREA];
3594 int gx;
3596 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
3597 if (gx >= x)
3599 rect->width = g->pixel_width;
3600 rect->height = r->height;
3601 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
3602 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
3603 found = 1;
3608 return found;
3612 /* Return the current position of the mouse.
3613 *FP should be a frame which indicates which display to ask about.
3615 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3616 and *PART to the frame, window, and scroll bar part that the mouse
3617 is over. Set *X and *Y to the portion and whole of the mouse's
3618 position on the scroll bar.
3620 If the mouse movement started elsewhere, set *FP to the frame the
3621 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3622 the mouse is over.
3624 Set *TIME to the server time-stamp for the time at which the mouse
3625 was at this position.
3627 Don't store anything if we don't have a valid set of values to report.
3629 This clears the mouse_moved flag, so we can wait for the next mouse
3630 movement. */
3632 static void
3633 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3634 FRAME_PTR *fp;
3635 int insist;
3636 Lisp_Object *bar_window;
3637 enum scroll_bar_part *part;
3638 Lisp_Object *x, *y;
3639 unsigned long *time;
3641 FRAME_PTR f1;
3643 BLOCK_INPUT;
3645 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3646 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3647 else
3649 Window root;
3650 int root_x, root_y;
3652 Window dummy_window;
3653 int dummy;
3655 Lisp_Object frame, tail;
3657 /* Clear the mouse-moved flag for every frame on this display. */
3658 FOR_EACH_FRAME (tail, frame)
3659 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3660 XFRAME (frame)->mouse_moved = 0;
3662 last_mouse_scroll_bar = Qnil;
3664 /* Figure out which root window we're on. */
3665 XQueryPointer (FRAME_X_DISPLAY (*fp),
3666 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3668 /* The root window which contains the pointer. */
3669 &root,
3671 /* Trash which we can't trust if the pointer is on
3672 a different screen. */
3673 &dummy_window,
3675 /* The position on that root window. */
3676 &root_x, &root_y,
3678 /* More trash we can't trust. */
3679 &dummy, &dummy,
3681 /* Modifier keys and pointer buttons, about which
3682 we don't care. */
3683 (unsigned int *) &dummy);
3685 /* Now we have a position on the root; find the innermost window
3686 containing the pointer. */
3688 Window win, child;
3689 int win_x, win_y;
3690 int parent_x = 0, parent_y = 0;
3691 int count;
3693 win = root;
3695 /* XTranslateCoordinates can get errors if the window
3696 structure is changing at the same time this function
3697 is running. So at least we must not crash from them. */
3699 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
3701 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3702 && FRAME_LIVE_P (last_mouse_frame))
3704 /* If mouse was grabbed on a frame, give coords for that frame
3705 even if the mouse is now outside it. */
3706 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3708 /* From-window, to-window. */
3709 root, FRAME_X_WINDOW (last_mouse_frame),
3711 /* From-position, to-position. */
3712 root_x, root_y, &win_x, &win_y,
3714 /* Child of win. */
3715 &child);
3716 f1 = last_mouse_frame;
3718 else
3720 while (1)
3722 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3724 /* From-window, to-window. */
3725 root, win,
3727 /* From-position, to-position. */
3728 root_x, root_y, &win_x, &win_y,
3730 /* Child of win. */
3731 &child);
3733 if (child == None || child == win)
3734 break;
3736 win = child;
3737 parent_x = win_x;
3738 parent_y = win_y;
3741 /* Now we know that:
3742 win is the innermost window containing the pointer
3743 (XTC says it has no child containing the pointer),
3744 win_x and win_y are the pointer's position in it
3745 (XTC did this the last time through), and
3746 parent_x and parent_y are the pointer's position in win's parent.
3747 (They are what win_x and win_y were when win was child.
3748 If win is the root window, it has no parent, and
3749 parent_{x,y} are invalid, but that's okay, because we'll
3750 never use them in that case.) */
3752 /* Is win one of our frames? */
3753 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3755 #ifdef USE_X_TOOLKIT
3756 /* If we end up with the menu bar window, say it's not
3757 on the frame. */
3758 if (f1 != NULL
3759 && f1->output_data.x->menubar_widget
3760 && win == XtWindow (f1->output_data.x->menubar_widget))
3761 f1 = NULL;
3762 #endif /* USE_X_TOOLKIT */
3765 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3766 f1 = 0;
3768 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
3770 /* If not, is it one of our scroll bars? */
3771 if (! f1)
3773 struct scroll_bar *bar = x_window_to_scroll_bar (win);
3775 if (bar)
3777 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3778 win_x = parent_x;
3779 win_y = parent_y;
3783 if (f1 == 0 && insist > 0)
3784 f1 = SELECTED_FRAME ();
3786 if (f1)
3788 /* Ok, we found a frame. Store all the values.
3789 last_mouse_glyph is a rectangle used to reduce the
3790 generation of mouse events. To not miss any motion
3791 events, we must divide the frame into rectangles of the
3792 size of the smallest character that could be displayed
3793 on it, i.e. into the same rectangles that matrices on
3794 the frame are divided into. */
3796 int width, height, gx, gy;
3797 XRectangle rect;
3799 if (glyph_rect (f1, win_x, win_y, &rect))
3800 last_mouse_glyph = rect;
3801 else
3803 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
3804 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
3805 gx = win_x;
3806 gy = win_y;
3808 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
3809 round down even for negative values. */
3810 if (gx < 0)
3811 gx -= width - 1;
3812 if (gy < 0)
3813 gy -= height - 1;
3814 gx = (gx + width - 1) / width * width;
3815 gy = (gy + height - 1) / height * height;
3817 last_mouse_glyph.width = width;
3818 last_mouse_glyph.height = height;
3819 last_mouse_glyph.x = gx;
3820 last_mouse_glyph.y = gy;
3823 *bar_window = Qnil;
3824 *part = 0;
3825 *fp = f1;
3826 XSETINT (*x, win_x);
3827 XSETINT (*y, win_y);
3828 *time = last_mouse_movement_time;
3833 UNBLOCK_INPUT;
3838 /***********************************************************************
3839 Scroll bars
3840 ***********************************************************************/
3842 /* Scroll bar support. */
3844 /* Given an X window ID, find the struct scroll_bar which manages it.
3845 This can be called in GC, so we have to make sure to strip off mark
3846 bits. */
3848 static struct scroll_bar *
3849 x_window_to_scroll_bar (window_id)
3850 Window window_id;
3852 Lisp_Object tail;
3854 #ifdef USE_GTK
3855 window_id = (Window) xg_get_scroll_id_for_window (window_id);
3856 #endif /* USE_GTK */
3858 for (tail = Vframe_list;
3859 XGCTYPE (tail) == Lisp_Cons;
3860 tail = XCDR (tail))
3862 Lisp_Object frame, bar, condemned;
3864 frame = XCAR (tail);
3865 /* All elements of Vframe_list should be frames. */
3866 if (! GC_FRAMEP (frame))
3867 abort ();
3869 /* Scan this frame's scroll bar list for a scroll bar with the
3870 right window ID. */
3871 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3872 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3873 /* This trick allows us to search both the ordinary and
3874 condemned scroll bar lists with one loop. */
3875 ! GC_NILP (bar) || (bar = condemned,
3876 condemned = Qnil,
3877 ! GC_NILP (bar));
3878 bar = XSCROLL_BAR (bar)->next)
3879 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
3880 return XSCROLL_BAR (bar);
3883 return 0;
3887 #if defined USE_LUCID
3889 /* Return the Lucid menu bar WINDOW is part of. Return null
3890 if WINDOW is not part of a menu bar. */
3892 static Widget
3893 x_window_to_menu_bar (window)
3894 Window window;
3896 Lisp_Object tail;
3898 for (tail = Vframe_list;
3899 XGCTYPE (tail) == Lisp_Cons;
3900 tail = XCDR (tail))
3902 Lisp_Object frame = XCAR (tail);
3903 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
3905 if (menu_bar && xlwmenu_window_p (menu_bar, window))
3906 return menu_bar;
3909 return NULL;
3912 #endif /* USE_LUCID */
3915 /************************************************************************
3916 Toolkit scroll bars
3917 ************************************************************************/
3919 #ifdef USE_TOOLKIT_SCROLL_BARS
3921 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
3922 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
3923 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
3924 struct scroll_bar *));
3925 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
3926 int, int, int));
3929 /* Lisp window being scrolled. Set when starting to interact with
3930 a toolkit scroll bar, reset to nil when ending the interaction. */
3932 static Lisp_Object window_being_scrolled;
3934 /* Last scroll bar part sent in xm_scroll_callback. */
3936 static int last_scroll_bar_part;
3938 /* Whether this is an Xaw with arrow-scrollbars. This should imply
3939 that movements of 1/20 of the screen size are mapped to up/down. */
3941 #ifndef USE_GTK
3942 /* Id of action hook installed for scroll bars. */
3944 static XtActionHookId action_hook_id;
3946 static Boolean xaw3d_arrow_scroll;
3948 /* Whether the drag scrolling maintains the mouse at the top of the
3949 thumb. If not, resizing the thumb needs to be done more carefully
3950 to avoid jerkyness. */
3952 static Boolean xaw3d_pick_top;
3954 extern void set_vertical_scroll_bar P_ ((struct window *));
3956 /* Action hook installed via XtAppAddActionHook when toolkit scroll
3957 bars are used.. The hook is responsible for detecting when
3958 the user ends an interaction with the scroll bar, and generates
3959 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
3961 static void
3962 xt_action_hook (widget, client_data, action_name, event, params,
3963 num_params)
3964 Widget widget;
3965 XtPointer client_data;
3966 String action_name;
3967 XEvent *event;
3968 String *params;
3969 Cardinal *num_params;
3971 int scroll_bar_p;
3972 char *end_action;
3974 #ifdef USE_MOTIF
3975 scroll_bar_p = XmIsScrollBar (widget);
3976 end_action = "Release";
3977 #else /* !USE_MOTIF i.e. use Xaw */
3978 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
3979 end_action = "EndScroll";
3980 #endif /* USE_MOTIF */
3982 if (scroll_bar_p
3983 && strcmp (action_name, end_action) == 0
3984 && WINDOWP (window_being_scrolled))
3986 struct window *w;
3988 x_send_scroll_bar_event (window_being_scrolled,
3989 scroll_bar_end_scroll, 0, 0);
3990 w = XWINDOW (window_being_scrolled);
3992 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
3994 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
3995 /* The thumb size is incorrect while dragging: fix it. */
3996 set_vertical_scroll_bar (w);
3998 window_being_scrolled = Qnil;
3999 last_scroll_bar_part = -1;
4001 /* Xt timeouts no longer needed. */
4002 toolkit_scroll_bar_interaction = 0;
4005 #endif /* not USE_GTK */
4007 /* A vector of windows used for communication between
4008 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4010 static struct window **scroll_bar_windows;
4011 static int scroll_bar_windows_size;
4014 /* Send a client message with message type Xatom_Scrollbar for a
4015 scroll action to the frame of WINDOW. PART is a value identifying
4016 the part of the scroll bar that was clicked on. PORTION is the
4017 amount to scroll of a whole of WHOLE. */
4019 static void
4020 x_send_scroll_bar_event (window, part, portion, whole)
4021 Lisp_Object window;
4022 int part, portion, whole;
4024 XEvent event;
4025 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4026 struct window *w = XWINDOW (window);
4027 struct frame *f = XFRAME (w->frame);
4028 int i;
4030 BLOCK_INPUT;
4032 /* Construct a ClientMessage event to send to the frame. */
4033 ev->type = ClientMessage;
4034 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4035 ev->display = FRAME_X_DISPLAY (f);
4036 ev->window = FRAME_X_WINDOW (f);
4037 ev->format = 32;
4039 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4040 not enough to store a pointer or Lisp_Object on a 64 bit system.
4041 So, store the window in scroll_bar_windows and pass the index
4042 into that array in the event. */
4043 for (i = 0; i < scroll_bar_windows_size; ++i)
4044 if (scroll_bar_windows[i] == NULL)
4045 break;
4047 if (i == scroll_bar_windows_size)
4049 int new_size = max (10, 2 * scroll_bar_windows_size);
4050 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4051 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4053 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4054 nbytes);
4055 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4056 scroll_bar_windows_size = new_size;
4059 scroll_bar_windows[i] = w;
4060 ev->data.l[0] = (long) i;
4061 ev->data.l[1] = (long) part;
4062 ev->data.l[2] = (long) 0;
4063 ev->data.l[3] = (long) portion;
4064 ev->data.l[4] = (long) whole;
4066 /* Make Xt timeouts work while the scroll bar is active. */
4067 toolkit_scroll_bar_interaction = 1;
4069 /* Setting the event mask to zero means that the message will
4070 be sent to the client that created the window, and if that
4071 window no longer exists, no event will be sent. */
4072 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4073 UNBLOCK_INPUT;
4077 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4078 in *IEVENT. */
4080 static void
4081 x_scroll_bar_to_input_event (event, ievent)
4082 XEvent *event;
4083 struct input_event *ievent;
4085 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4086 Lisp_Object window;
4087 struct frame *f;
4088 struct window *w;
4090 w = scroll_bar_windows[ev->data.l[0]];
4091 scroll_bar_windows[ev->data.l[0]] = NULL;
4093 XSETWINDOW (window, w);
4094 f = XFRAME (w->frame);
4096 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4097 ievent->frame_or_window = window;
4098 ievent->arg = Qnil;
4099 #ifdef USE_GTK
4100 ievent->timestamp = CurrentTime;
4101 #else
4102 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4103 #endif
4104 ievent->part = ev->data.l[1];
4105 ievent->code = ev->data.l[2];
4106 ievent->x = make_number ((int) ev->data.l[3]);
4107 ievent->y = make_number ((int) ev->data.l[4]);
4108 ievent->modifiers = 0;
4112 #ifdef USE_MOTIF
4114 /* Minimum and maximum values used for Motif scroll bars. */
4116 #define XM_SB_MAX 10000000
4119 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4120 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4121 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4123 static void
4124 xm_scroll_callback (widget, client_data, call_data)
4125 Widget widget;
4126 XtPointer client_data, call_data;
4128 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4129 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4130 int part = -1, whole = 0, portion = 0;
4132 switch (cs->reason)
4134 case XmCR_DECREMENT:
4135 bar->dragging = Qnil;
4136 part = scroll_bar_up_arrow;
4137 break;
4139 case XmCR_INCREMENT:
4140 bar->dragging = Qnil;
4141 part = scroll_bar_down_arrow;
4142 break;
4144 case XmCR_PAGE_DECREMENT:
4145 bar->dragging = Qnil;
4146 part = scroll_bar_above_handle;
4147 break;
4149 case XmCR_PAGE_INCREMENT:
4150 bar->dragging = Qnil;
4151 part = scroll_bar_below_handle;
4152 break;
4154 case XmCR_TO_TOP:
4155 bar->dragging = Qnil;
4156 part = scroll_bar_to_top;
4157 break;
4159 case XmCR_TO_BOTTOM:
4160 bar->dragging = Qnil;
4161 part = scroll_bar_to_bottom;
4162 break;
4164 case XmCR_DRAG:
4166 int slider_size;
4168 /* Get the slider size. */
4169 BLOCK_INPUT;
4170 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4171 UNBLOCK_INPUT;
4173 whole = XM_SB_MAX - slider_size;
4174 portion = min (cs->value, whole);
4175 part = scroll_bar_handle;
4176 bar->dragging = make_number (cs->value);
4178 break;
4180 case XmCR_VALUE_CHANGED:
4181 break;
4184 if (part >= 0)
4186 window_being_scrolled = bar->window;
4187 last_scroll_bar_part = part;
4188 x_send_scroll_bar_event (bar->window, part, portion, whole);
4193 #else /* !USE_MOTIF, i.e. Xaw or GTK */
4194 #ifdef USE_GTK
4195 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4196 bar widget. DATA is a pointer to the scroll_bar structure. */
4198 static void
4199 xg_scroll_callback (widget, data)
4200 GtkRange *widget;
4201 gpointer data;
4203 struct scroll_bar *bar = (struct scroll_bar *) data;
4204 gdouble previous;
4205 gdouble position;
4206 gdouble *p;
4207 int diff;
4209 int part = -1, whole = 0, portion = 0;
4210 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4212 if (xg_ignore_gtk_scrollbar) return;
4214 position = gtk_adjustment_get_value (adj);
4216 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4217 if (! p)
4219 p = (gdouble*) xmalloc (sizeof (gdouble));
4220 *p = XG_SB_MIN;
4221 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4224 previous = *p;
4225 *p = position;
4227 diff = (int) (position - previous);
4229 if (diff == (int) adj->step_increment)
4231 part = scroll_bar_down_arrow;
4232 bar->dragging = Qnil;
4234 else if (-diff == (int) adj->step_increment)
4236 part = scroll_bar_up_arrow;
4237 bar->dragging = Qnil;
4239 else if (diff == (int) adj->page_increment)
4241 part = scroll_bar_below_handle;
4242 bar->dragging = Qnil;
4244 else if (-diff == (int) adj->page_increment)
4246 part = scroll_bar_above_handle;
4247 bar->dragging = Qnil;
4249 else
4251 part = scroll_bar_handle;
4252 whole = adj->upper - adj->page_size;
4253 portion = min ((int)position, whole);
4254 bar->dragging = make_number ((int)portion);
4257 if (part >= 0)
4259 window_being_scrolled = bar->window;
4260 last_scroll_bar_part = part;
4261 x_send_scroll_bar_event (bar->window, part, portion, whole);
4265 #else /* not USE_GTK */
4267 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4268 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4269 scroll bar struct. CALL_DATA is a pointer to a float saying where
4270 the thumb is. */
4272 static void
4273 xaw_jump_callback (widget, client_data, call_data)
4274 Widget widget;
4275 XtPointer client_data, call_data;
4277 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4278 float top = *(float *) call_data;
4279 float shown;
4280 int whole, portion, height;
4281 int part;
4283 /* Get the size of the thumb, a value between 0 and 1. */
4284 BLOCK_INPUT;
4285 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4286 UNBLOCK_INPUT;
4288 whole = 10000000;
4289 portion = shown < 1 ? top * whole : 0;
4291 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
4292 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4293 the bottom, so we force the scrolling whenever we see that we're
4294 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4295 we try to ensure that we always stay two pixels away from the
4296 bottom). */
4297 part = scroll_bar_down_arrow;
4298 else
4299 part = scroll_bar_handle;
4301 window_being_scrolled = bar->window;
4302 bar->dragging = make_number (portion);
4303 last_scroll_bar_part = part;
4304 x_send_scroll_bar_event (bar->window, part, portion, whole);
4308 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4309 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4310 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4311 the scroll bar. CALL_DATA is an integer specifying the action that
4312 has taken place. Its magnitude is in the range 0..height of the
4313 scroll bar. Negative values mean scroll towards buffer start.
4314 Values < height of scroll bar mean line-wise movement. */
4316 static void
4317 xaw_scroll_callback (widget, client_data, call_data)
4318 Widget widget;
4319 XtPointer client_data, call_data;
4321 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4322 /* The position really is stored cast to a pointer. */
4323 int position = (long) call_data;
4324 Dimension height;
4325 int part;
4327 /* Get the height of the scroll bar. */
4328 BLOCK_INPUT;
4329 XtVaGetValues (widget, XtNheight, &height, NULL);
4330 UNBLOCK_INPUT;
4332 if (abs (position) >= height)
4333 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4335 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4336 it maps line-movement to call_data = max(5, height/20). */
4337 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
4338 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4339 else
4340 part = scroll_bar_move_ratio;
4342 window_being_scrolled = bar->window;
4343 bar->dragging = Qnil;
4344 last_scroll_bar_part = part;
4345 x_send_scroll_bar_event (bar->window, part, position, height);
4348 #endif /* not USE_GTK */
4349 #endif /* not USE_MOTIF */
4351 #define SCROLL_BAR_NAME "verticalScrollBar"
4353 /* Create the widget for scroll bar BAR on frame F. Record the widget
4354 and X window of the scroll bar in BAR. */
4356 #ifdef USE_GTK
4357 static void
4358 x_create_toolkit_scroll_bar (f, bar)
4359 struct frame *f;
4360 struct scroll_bar *bar;
4362 char *scroll_bar_name = SCROLL_BAR_NAME;
4364 BLOCK_INPUT;
4365 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4366 scroll_bar_name);
4367 UNBLOCK_INPUT;
4370 #else /* not USE_GTK */
4372 static void
4373 x_create_toolkit_scroll_bar (f, bar)
4374 struct frame *f;
4375 struct scroll_bar *bar;
4377 Window xwindow;
4378 Widget widget;
4379 Arg av[20];
4380 int ac = 0;
4381 char *scroll_bar_name = SCROLL_BAR_NAME;
4382 unsigned long pixel;
4384 BLOCK_INPUT;
4386 #ifdef USE_MOTIF
4387 /* Set resources. Create the widget. */
4388 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4389 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4390 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4391 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4392 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4393 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4394 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4396 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4397 if (pixel != -1)
4399 XtSetArg (av[ac], XmNforeground, pixel);
4400 ++ac;
4403 pixel = f->output_data.x->scroll_bar_background_pixel;
4404 if (pixel != -1)
4406 XtSetArg (av[ac], XmNbackground, pixel);
4407 ++ac;
4410 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4411 scroll_bar_name, av, ac);
4413 /* Add one callback for everything that can happen. */
4414 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4415 (XtPointer) bar);
4416 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4417 (XtPointer) bar);
4418 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4419 (XtPointer) bar);
4420 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4421 (XtPointer) bar);
4422 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4423 (XtPointer) bar);
4424 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4425 (XtPointer) bar);
4426 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4427 (XtPointer) bar);
4429 /* Realize the widget. Only after that is the X window created. */
4430 XtRealizeWidget (widget);
4432 /* Set the cursor to an arrow. I didn't find a resource to do that.
4433 And I'm wondering why it hasn't an arrow cursor by default. */
4434 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4435 f->output_data.x->nontext_cursor);
4437 #else /* !USE_MOTIF i.e. use Xaw */
4439 /* Set resources. Create the widget. The background of the
4440 Xaw3d scroll bar widget is a little bit light for my taste.
4441 We don't alter it here to let users change it according
4442 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4443 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4444 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4445 /* For smoother scrolling with Xaw3d -sm */
4446 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4448 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4449 if (pixel != -1)
4451 XtSetArg (av[ac], XtNforeground, pixel);
4452 ++ac;
4455 pixel = f->output_data.x->scroll_bar_background_pixel;
4456 if (pixel != -1)
4458 XtSetArg (av[ac], XtNbackground, pixel);
4459 ++ac;
4462 /* Top/bottom shadow colors. */
4464 /* Allocate them, if necessary. */
4465 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4467 pixel = f->output_data.x->scroll_bar_background_pixel;
4468 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4469 &pixel, 1.2, 0x8000))
4470 pixel = -1;
4471 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4473 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4475 pixel = f->output_data.x->scroll_bar_background_pixel;
4476 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4477 &pixel, 0.6, 0x4000))
4478 pixel = -1;
4479 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4482 /* Tell the toolkit about them. */
4483 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4484 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4485 /* We tried to allocate a color for the top/bottom shadow, and
4486 failed, so tell Xaw3d to use dithering instead. */
4488 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4489 ++ac;
4491 else
4492 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4493 be more consistent with other emacs 3d colors, and since Xaw3d is
4494 not good at dealing with allocation failure. */
4496 /* This tells Xaw3d to use real colors instead of dithering for
4497 the shadows. */
4498 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4499 ++ac;
4501 /* Specify the colors. */
4502 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4503 if (pixel != -1)
4505 XtSetArg (av[ac], "topShadowPixel", pixel);
4506 ++ac;
4508 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4509 if (pixel != -1)
4511 XtSetArg (av[ac], "bottomShadowPixel", pixel);
4512 ++ac;
4516 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4517 f->output_data.x->edit_widget, av, ac);
4520 char *initial = "";
4521 char *val = initial;
4522 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4523 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4524 if (val == initial)
4525 { /* ARROW_SCROLL */
4526 xaw3d_arrow_scroll = True;
4527 /* Isn't that just a personal preference ? -sm */
4528 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4532 /* Define callbacks. */
4533 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4534 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4535 (XtPointer) bar);
4537 /* Realize the widget. Only after that is the X window created. */
4538 XtRealizeWidget (widget);
4540 #endif /* !USE_MOTIF */
4542 /* Install an action hook that lets us detect when the user
4543 finishes interacting with a scroll bar. */
4544 if (action_hook_id == 0)
4545 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4547 /* Remember X window and widget in the scroll bar vector. */
4548 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4549 xwindow = XtWindow (widget);
4550 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
4552 UNBLOCK_INPUT;
4554 #endif /* not USE_GTK */
4557 /* Set the thumb size and position of scroll bar BAR. We are currently
4558 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4560 #ifdef USE_GTK
4561 static void
4562 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4563 struct scroll_bar *bar;
4564 int portion, position, whole;
4566 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4569 #else /* not USE_GTK */
4570 static void
4571 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4572 struct scroll_bar *bar;
4573 int portion, position, whole;
4575 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4576 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4577 float top, shown;
4579 BLOCK_INPUT;
4581 #ifdef USE_MOTIF
4583 /* We use an estimate of 30 chars per line rather than the real
4584 `portion' value. This has the disadvantage that the thumb size
4585 is not very representative, but it makes our life a lot easier.
4586 Otherwise, we have to constantly adjust the thumb size, which
4587 we can't always do quickly enough: while dragging, the size of
4588 the thumb might prevent the user from dragging the thumb all the
4589 way to the end. but Motif and some versions of Xaw3d don't allow
4590 updating the thumb size while dragging. Also, even if we can update
4591 its size, the update will often happen too late.
4592 If you don't believe it, check out revision 1.650 of xterm.c to see
4593 what hoops we were going through and the still poor behavior we got. */
4594 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4595 /* When the thumb is at the bottom, position == whole.
4596 So we need to increase `whole' to make space for the thumb. */
4597 whole += portion;
4599 if (whole <= 0)
4600 top = 0, shown = 1;
4601 else
4603 top = (float) position / whole;
4604 shown = (float) portion / whole;
4607 if (NILP (bar->dragging))
4609 int size, value;
4611 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4612 is the scroll bar's maximum and MIN is the scroll bar's minimum
4613 value. */
4614 size = shown * XM_SB_MAX;
4615 size = min (size, XM_SB_MAX);
4616 size = max (size, 1);
4618 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4619 value = top * XM_SB_MAX;
4620 value = min (value, XM_SB_MAX - size);
4622 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4624 #else /* !USE_MOTIF i.e. use Xaw */
4626 if (whole == 0)
4627 top = 0, shown = 1;
4628 else
4630 top = (float) position / whole;
4631 shown = (float) portion / whole;
4635 float old_top, old_shown;
4636 Dimension height;
4637 XtVaGetValues (widget,
4638 XtNtopOfThumb, &old_top,
4639 XtNshown, &old_shown,
4640 XtNheight, &height,
4641 NULL);
4643 /* Massage the top+shown values. */
4644 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4645 top = max (0, min (1, top));
4646 else
4647 top = old_top;
4648 /* Keep two pixels available for moving the thumb down. */
4649 shown = max (0, min (1 - top - (2.0 / height), shown));
4651 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4652 check that your system's configuration file contains a define
4653 for `NARROWPROTO'. See s/freebsd.h for an example. */
4654 if (top != old_top || shown != old_shown)
4656 if (NILP (bar->dragging))
4657 XawScrollbarSetThumb (widget, top, shown);
4658 else
4660 #ifdef HAVE_XAW3D
4661 ScrollbarWidget sb = (ScrollbarWidget) widget;
4662 int scroll_mode = 0;
4664 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
4665 if (xaw3d_arrow_scroll)
4667 /* Xaw3d stupidly ignores resize requests while dragging
4668 so we have to make it believe it's not in dragging mode. */
4669 scroll_mode = sb->scrollbar.scroll_mode;
4670 if (scroll_mode == 2)
4671 sb->scrollbar.scroll_mode = 0;
4673 #endif
4674 /* Try to make the scrolling a tad smoother. */
4675 if (!xaw3d_pick_top)
4676 shown = min (shown, old_shown);
4678 XawScrollbarSetThumb (widget, top, shown);
4680 #ifdef HAVE_XAW3D
4681 if (xaw3d_arrow_scroll && scroll_mode == 2)
4682 sb->scrollbar.scroll_mode = scroll_mode;
4683 #endif
4687 #endif /* !USE_MOTIF */
4689 UNBLOCK_INPUT;
4691 #endif /* not USE_GTK */
4693 #endif /* USE_TOOLKIT_SCROLL_BARS */
4697 /************************************************************************
4698 Scroll bars, general
4699 ************************************************************************/
4701 /* Create a scroll bar and return the scroll bar vector for it. W is
4702 the Emacs window on which to create the scroll bar. TOP, LEFT,
4703 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4704 scroll bar. */
4706 static struct scroll_bar *
4707 x_scroll_bar_create (w, top, left, width, height)
4708 struct window *w;
4709 int top, left, width, height;
4711 struct frame *f = XFRAME (w->frame);
4712 struct scroll_bar *bar
4713 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
4715 BLOCK_INPUT;
4717 #ifdef USE_TOOLKIT_SCROLL_BARS
4718 x_create_toolkit_scroll_bar (f, bar);
4719 #else /* not USE_TOOLKIT_SCROLL_BARS */
4721 XSetWindowAttributes a;
4722 unsigned long mask;
4723 Window window;
4725 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4726 if (a.background_pixel == -1)
4727 a.background_pixel = f->output_data.x->background_pixel;
4729 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4730 | ButtonMotionMask | PointerMotionHintMask
4731 | ExposureMask);
4732 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4734 mask = (CWBackPixel | CWEventMask | CWCursor);
4736 /* Clear the area of W that will serve as a scroll bar. This is
4737 for the case that a window has been split horizontally. In
4738 this case, no clear_frame is generated to reduce flickering. */
4739 if (width > 0 && height > 0)
4740 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4741 left, top, width,
4742 window_box_height (w), False);
4744 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4745 /* Position and size of scroll bar. */
4746 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4747 top,
4748 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4749 height,
4750 /* Border width, depth, class, and visual. */
4752 CopyFromParent,
4753 CopyFromParent,
4754 CopyFromParent,
4755 /* Attributes. */
4756 mask, &a);
4757 SET_SCROLL_BAR_X_WINDOW (bar, window);
4759 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4761 XSETWINDOW (bar->window, w);
4762 XSETINT (bar->top, top);
4763 XSETINT (bar->left, left);
4764 XSETINT (bar->width, width);
4765 XSETINT (bar->height, height);
4766 XSETINT (bar->start, 0);
4767 XSETINT (bar->end, 0);
4768 bar->dragging = Qnil;
4770 /* Add bar to its frame's list of scroll bars. */
4771 bar->next = FRAME_SCROLL_BARS (f);
4772 bar->prev = Qnil;
4773 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4774 if (!NILP (bar->next))
4775 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4777 /* Map the window/widget. */
4778 #ifdef USE_TOOLKIT_SCROLL_BARS
4780 #ifdef USE_GTK
4781 xg_update_scrollbar_pos (f,
4782 SCROLL_BAR_X_WINDOW (bar),
4783 top,
4784 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4785 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4786 max (height, 1),
4787 left,
4788 width);
4789 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
4790 #else /* not USE_GTK */
4791 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4792 XtConfigureWidget (scroll_bar,
4793 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4794 top,
4795 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4796 max (height, 1), 0);
4797 XtMapWidget (scroll_bar);
4798 #endif /* not USE_GTK */
4800 #else /* not USE_TOOLKIT_SCROLL_BARS */
4801 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4802 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4804 UNBLOCK_INPUT;
4805 return bar;
4809 /* Draw BAR's handle in the proper position.
4811 If the handle is already drawn from START to END, don't bother
4812 redrawing it, unless REBUILD is non-zero; in that case, always
4813 redraw it. (REBUILD is handy for drawing the handle after expose
4814 events.)
4816 Normally, we want to constrain the start and end of the handle to
4817 fit inside its rectangle, but if the user is dragging the scroll
4818 bar handle, we want to let them drag it down all the way, so that
4819 the bar's top is as far down as it goes; otherwise, there's no way
4820 to move to the very end of the buffer. */
4822 #ifndef USE_TOOLKIT_SCROLL_BARS
4824 static void
4825 x_scroll_bar_set_handle (bar, start, end, rebuild)
4826 struct scroll_bar *bar;
4827 int start, end;
4828 int rebuild;
4830 int dragging = ! NILP (bar->dragging);
4831 Window w = SCROLL_BAR_X_WINDOW (bar);
4832 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4833 GC gc = f->output_data.x->normal_gc;
4835 /* If the display is already accurate, do nothing. */
4836 if (! rebuild
4837 && start == XINT (bar->start)
4838 && end == XINT (bar->end))
4839 return;
4841 BLOCK_INPUT;
4844 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
4845 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
4846 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4848 /* Make sure the values are reasonable, and try to preserve
4849 the distance between start and end. */
4851 int length = end - start;
4853 if (start < 0)
4854 start = 0;
4855 else if (start > top_range)
4856 start = top_range;
4857 end = start + length;
4859 if (end < start)
4860 end = start;
4861 else if (end > top_range && ! dragging)
4862 end = top_range;
4865 /* Store the adjusted setting in the scroll bar. */
4866 XSETINT (bar->start, start);
4867 XSETINT (bar->end, end);
4869 /* Clip the end position, just for display. */
4870 if (end > top_range)
4871 end = top_range;
4873 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
4874 below top positions, to make sure the handle is always at least
4875 that many pixels tall. */
4876 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
4878 /* Draw the empty space above the handle. Note that we can't clear
4879 zero-height areas; that means "clear to end of window." */
4880 if (0 < start)
4881 x_clear_area (FRAME_X_DISPLAY (f), w,
4882 /* x, y, width, height, and exposures. */
4883 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4884 VERTICAL_SCROLL_BAR_TOP_BORDER,
4885 inside_width, start,
4886 False);
4888 /* Change to proper foreground color if one is specified. */
4889 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4890 XSetForeground (FRAME_X_DISPLAY (f), gc,
4891 f->output_data.x->scroll_bar_foreground_pixel);
4893 /* Draw the handle itself. */
4894 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
4895 /* x, y, width, height */
4896 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4897 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
4898 inside_width, end - start);
4900 /* Restore the foreground color of the GC if we changed it above. */
4901 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4902 XSetForeground (FRAME_X_DISPLAY (f), gc,
4903 f->output_data.x->foreground_pixel);
4905 /* Draw the empty space below the handle. Note that we can't
4906 clear zero-height areas; that means "clear to end of window." */
4907 if (end < inside_height)
4908 x_clear_area (FRAME_X_DISPLAY (f), w,
4909 /* x, y, width, height, and exposures. */
4910 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4911 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
4912 inside_width, inside_height - end,
4913 False);
4917 UNBLOCK_INPUT;
4920 #endif /* !USE_TOOLKIT_SCROLL_BARS */
4922 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
4923 nil. */
4925 static void
4926 x_scroll_bar_remove (bar)
4927 struct scroll_bar *bar;
4929 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4930 BLOCK_INPUT;
4932 #ifdef USE_TOOLKIT_SCROLL_BARS
4933 #ifdef USE_GTK
4934 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
4935 #else /* not USE_GTK */
4936 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
4937 #endif /* not USE_GTK */
4938 #else
4939 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4940 #endif
4942 /* Disassociate this scroll bar from its window. */
4943 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
4945 UNBLOCK_INPUT;
4949 /* Set the handle of the vertical scroll bar for WINDOW to indicate
4950 that we are displaying PORTION characters out of a total of WHOLE
4951 characters, starting at POSITION. If WINDOW has no scroll bar,
4952 create one. */
4954 static void
4955 XTset_vertical_scroll_bar (w, portion, whole, position)
4956 struct window *w;
4957 int portion, whole, position;
4959 struct frame *f = XFRAME (w->frame);
4960 struct scroll_bar *bar;
4961 int top, height, left, sb_left, width, sb_width;
4962 int window_y, window_height;
4964 /* Get window dimensions. */
4965 window_box (w, -1, 0, &window_y, 0, &window_height);
4966 top = window_y;
4967 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
4968 height = window_height;
4970 /* Compute the left edge of the scroll bar area. */
4971 left = WINDOW_SCROLL_BAR_AREA_X (w);
4973 /* Compute the width of the scroll bar which might be less than
4974 the width of the area reserved for the scroll bar. */
4975 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
4976 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
4977 else
4978 sb_width = width;
4980 /* Compute the left edge of the scroll bar. */
4981 #ifdef USE_TOOLKIT_SCROLL_BARS
4982 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
4983 sb_left = left + width - sb_width - (width - sb_width) / 2;
4984 else
4985 sb_left = left + (width - sb_width) / 2;
4986 #else
4987 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
4988 sb_left = left + width - sb_width;
4989 else
4990 sb_left = left;
4991 #endif
4993 /* Does the scroll bar exist yet? */
4994 if (NILP (w->vertical_scroll_bar))
4996 if (width > 0 && height > 0)
4998 BLOCK_INPUT;
4999 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5000 left, top, width, height, False);
5001 UNBLOCK_INPUT;
5004 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5006 else
5008 /* It may just need to be moved and resized. */
5009 unsigned int mask = 0;
5011 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5013 BLOCK_INPUT;
5015 if (sb_left != XINT (bar->left))
5016 mask |= CWX;
5017 if (top != XINT (bar->top))
5018 mask |= CWY;
5019 if (sb_width != XINT (bar->width))
5020 mask |= CWWidth;
5021 if (height != XINT (bar->height))
5022 mask |= CWHeight;
5024 #ifdef USE_TOOLKIT_SCROLL_BARS
5026 #ifdef USE_GTK
5027 if (mask)
5028 xg_update_scrollbar_pos (f,
5029 SCROLL_BAR_X_WINDOW (bar),
5030 top,
5031 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5032 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5033 max (height, 1),
5034 left,
5035 width);
5036 #else /* not USE_GTK */
5038 /* Since toolkit scroll bars are smaller than the space reserved
5039 for them on the frame, we have to clear "under" them. */
5040 if (width > 0 && height > 0)
5041 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5042 left, top, width, height, False);
5043 /* Move/size the scroll bar widget. */
5044 if (mask)
5045 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5046 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5047 top,
5048 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5049 max (height, 1), 0);
5051 #endif /* not USE_GTK */
5052 #else /* not USE_TOOLKIT_SCROLL_BARS */
5054 /* Clear areas not covered by the scroll bar because of
5055 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5056 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5058 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5059 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5060 height, False);
5061 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5062 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5063 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5064 height, False);
5067 /* Clear areas not covered by the scroll bar because it's not as
5068 wide as the area reserved for it. This makes sure a
5069 previous mode line display is cleared after C-x 2 C-x 1, for
5070 example. */
5072 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5073 int rest = area_width - sb_width;
5074 if (rest > 0 && height > 0)
5076 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5077 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5078 left + area_width - rest, top,
5079 rest, height, False);
5080 else
5081 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5082 left, top, rest, height, False);
5086 /* Move/size the scroll bar window. */
5087 if (mask)
5089 XWindowChanges wc;
5091 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5092 wc.y = top;
5093 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5094 wc.height = height;
5095 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
5096 mask, &wc);
5099 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5101 /* Remember new settings. */
5102 XSETINT (bar->left, sb_left);
5103 XSETINT (bar->top, top);
5104 XSETINT (bar->width, sb_width);
5105 XSETINT (bar->height, height);
5107 UNBLOCK_INPUT;
5110 #ifdef USE_TOOLKIT_SCROLL_BARS
5111 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5112 #else /* not USE_TOOLKIT_SCROLL_BARS */
5113 /* Set the scroll bar's current state, unless we're currently being
5114 dragged. */
5115 if (NILP (bar->dragging))
5117 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5119 if (whole == 0)
5120 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5121 else
5123 int start = ((double) position * top_range) / whole;
5124 int end = ((double) (position + portion) * top_range) / whole;
5125 x_scroll_bar_set_handle (bar, start, end, 0);
5128 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5130 XSETVECTOR (w->vertical_scroll_bar, bar);
5134 /* The following three hooks are used when we're doing a thorough
5135 redisplay of the frame. We don't explicitly know which scroll bars
5136 are going to be deleted, because keeping track of when windows go
5137 away is a real pain - "Can you say set-window-configuration, boys
5138 and girls?" Instead, we just assert at the beginning of redisplay
5139 that *all* scroll bars are to be removed, and then save a scroll bar
5140 from the fiery pit when we actually redisplay its window. */
5142 /* Arrange for all scroll bars on FRAME to be removed at the next call
5143 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5144 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5146 static void
5147 XTcondemn_scroll_bars (frame)
5148 FRAME_PTR frame;
5150 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5151 while (! NILP (FRAME_SCROLL_BARS (frame)))
5153 Lisp_Object bar;
5154 bar = FRAME_SCROLL_BARS (frame);
5155 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5156 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5157 XSCROLL_BAR (bar)->prev = Qnil;
5158 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5159 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5160 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5165 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5166 Note that WINDOW isn't necessarily condemned at all. */
5168 static void
5169 XTredeem_scroll_bar (window)
5170 struct window *window;
5172 struct scroll_bar *bar;
5173 struct frame *f;
5175 /* We can't redeem this window's scroll bar if it doesn't have one. */
5176 if (NILP (window->vertical_scroll_bar))
5177 abort ();
5179 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5181 /* Unlink it from the condemned list. */
5182 f = XFRAME (WINDOW_FRAME (window));
5183 if (NILP (bar->prev))
5185 /* If the prev pointer is nil, it must be the first in one of
5186 the lists. */
5187 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5188 /* It's not condemned. Everything's fine. */
5189 return;
5190 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5191 window->vertical_scroll_bar))
5192 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5193 else
5194 /* If its prev pointer is nil, it must be at the front of
5195 one or the other! */
5196 abort ();
5198 else
5199 XSCROLL_BAR (bar->prev)->next = bar->next;
5201 if (! NILP (bar->next))
5202 XSCROLL_BAR (bar->next)->prev = bar->prev;
5204 bar->next = FRAME_SCROLL_BARS (f);
5205 bar->prev = Qnil;
5206 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5207 if (! NILP (bar->next))
5208 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5211 /* Remove all scroll bars on FRAME that haven't been saved since the
5212 last call to `*condemn_scroll_bars_hook'. */
5214 static void
5215 XTjudge_scroll_bars (f)
5216 FRAME_PTR f;
5218 Lisp_Object bar, next;
5220 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5222 /* Clear out the condemned list now so we won't try to process any
5223 more events on the hapless scroll bars. */
5224 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5226 for (; ! NILP (bar); bar = next)
5228 struct scroll_bar *b = XSCROLL_BAR (bar);
5230 x_scroll_bar_remove (b);
5232 next = b->next;
5233 b->next = b->prev = Qnil;
5236 /* Now there should be no references to the condemned scroll bars,
5237 and they should get garbage-collected. */
5241 #ifndef USE_TOOLKIT_SCROLL_BARS
5242 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5243 is a no-op when using toolkit scroll bars.
5245 This may be called from a signal handler, so we have to ignore GC
5246 mark bits. */
5248 static void
5249 x_scroll_bar_expose (bar, event)
5250 struct scroll_bar *bar;
5251 XEvent *event;
5253 Window w = SCROLL_BAR_X_WINDOW (bar);
5254 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5255 GC gc = f->output_data.x->normal_gc;
5256 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5258 BLOCK_INPUT;
5260 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
5262 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5263 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5265 /* x, y, width, height */
5266 0, 0,
5267 XINT (bar->width) - 1 - width_trim - width_trim,
5268 XINT (bar->height) - 1);
5270 UNBLOCK_INPUT;
5273 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5275 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5276 is set to something other than NO_EVENT, it is enqueued.
5278 This may be called from a signal handler, so we have to ignore GC
5279 mark bits. */
5282 static void
5283 x_scroll_bar_handle_click (bar, event, emacs_event)
5284 struct scroll_bar *bar;
5285 XEvent *event;
5286 struct input_event *emacs_event;
5288 if (! GC_WINDOWP (bar->window))
5289 abort ();
5291 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5292 emacs_event->code = event->xbutton.button - Button1;
5293 emacs_event->modifiers
5294 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5295 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5296 event->xbutton.state)
5297 | (event->type == ButtonRelease
5298 ? up_modifier
5299 : down_modifier));
5300 emacs_event->frame_or_window = bar->window;
5301 emacs_event->arg = Qnil;
5302 emacs_event->timestamp = event->xbutton.time;
5304 #if 0
5305 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5306 int internal_height
5307 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5308 #endif
5309 int top_range
5310 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5311 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5313 if (y < 0) y = 0;
5314 if (y > top_range) y = top_range;
5316 if (y < XINT (bar->start))
5317 emacs_event->part = scroll_bar_above_handle;
5318 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5319 emacs_event->part = scroll_bar_handle;
5320 else
5321 emacs_event->part = scroll_bar_below_handle;
5323 /* Just because the user has clicked on the handle doesn't mean
5324 they want to drag it. Lisp code needs to be able to decide
5325 whether or not we're dragging. */
5326 #if 0
5327 /* If the user has just clicked on the handle, record where they're
5328 holding it. */
5329 if (event->type == ButtonPress
5330 && emacs_event->part == scroll_bar_handle)
5331 XSETINT (bar->dragging, y - XINT (bar->start));
5332 #endif
5334 #ifndef USE_TOOLKIT_SCROLL_BARS
5335 /* If the user has released the handle, set it to its final position. */
5336 if (event->type == ButtonRelease
5337 && ! NILP (bar->dragging))
5339 int new_start = y - XINT (bar->dragging);
5340 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5342 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5343 bar->dragging = Qnil;
5345 #endif
5347 /* Same deal here as the other #if 0. */
5348 #if 0
5349 /* Clicks on the handle are always reported as occurring at the top of
5350 the handle. */
5351 if (emacs_event->part == scroll_bar_handle)
5352 emacs_event->x = bar->start;
5353 else
5354 XSETINT (emacs_event->x, y);
5355 #else
5356 XSETINT (emacs_event->x, y);
5357 #endif
5359 XSETINT (emacs_event->y, top_range);
5363 #ifndef USE_TOOLKIT_SCROLL_BARS
5365 /* Handle some mouse motion while someone is dragging the scroll bar.
5367 This may be called from a signal handler, so we have to ignore GC
5368 mark bits. */
5370 static void
5371 x_scroll_bar_note_movement (bar, event)
5372 struct scroll_bar *bar;
5373 XEvent *event;
5375 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5377 last_mouse_movement_time = event->xmotion.time;
5379 f->mouse_moved = 1;
5380 XSETVECTOR (last_mouse_scroll_bar, bar);
5382 /* If we're dragging the bar, display it. */
5383 if (! GC_NILP (bar->dragging))
5385 /* Where should the handle be now? */
5386 int new_start = event->xmotion.y - XINT (bar->dragging);
5388 if (new_start != XINT (bar->start))
5390 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5392 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5397 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5399 /* Return information to the user about the current position of the mouse
5400 on the scroll bar. */
5402 static void
5403 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5404 FRAME_PTR *fp;
5405 Lisp_Object *bar_window;
5406 enum scroll_bar_part *part;
5407 Lisp_Object *x, *y;
5408 unsigned long *time;
5410 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5411 Window w = SCROLL_BAR_X_WINDOW (bar);
5412 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5413 int win_x, win_y;
5414 Window dummy_window;
5415 int dummy_coord;
5416 unsigned int dummy_mask;
5418 BLOCK_INPUT;
5420 /* Get the mouse's position relative to the scroll bar window, and
5421 report that. */
5422 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5424 /* Root, child, root x and root y. */
5425 &dummy_window, &dummy_window,
5426 &dummy_coord, &dummy_coord,
5428 /* Position relative to scroll bar. */
5429 &win_x, &win_y,
5431 /* Mouse buttons and modifier keys. */
5432 &dummy_mask))
5434 else
5436 #if 0
5437 int inside_height
5438 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5439 #endif
5440 int top_range
5441 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5443 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5445 if (! NILP (bar->dragging))
5446 win_y -= XINT (bar->dragging);
5448 if (win_y < 0)
5449 win_y = 0;
5450 if (win_y > top_range)
5451 win_y = top_range;
5453 *fp = f;
5454 *bar_window = bar->window;
5456 if (! NILP (bar->dragging))
5457 *part = scroll_bar_handle;
5458 else if (win_y < XINT (bar->start))
5459 *part = scroll_bar_above_handle;
5460 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5461 *part = scroll_bar_handle;
5462 else
5463 *part = scroll_bar_below_handle;
5465 XSETINT (*x, win_y);
5466 XSETINT (*y, top_range);
5468 f->mouse_moved = 0;
5469 last_mouse_scroll_bar = Qnil;
5472 *time = last_mouse_movement_time;
5474 UNBLOCK_INPUT;
5478 /* The screen has been cleared so we may have changed foreground or
5479 background colors, and the scroll bars may need to be redrawn.
5480 Clear out the scroll bars, and ask for expose events, so we can
5481 redraw them. */
5483 void
5484 x_scroll_bar_clear (f)
5485 FRAME_PTR f;
5487 #ifndef USE_TOOLKIT_SCROLL_BARS
5488 Lisp_Object bar;
5490 /* We can have scroll bars even if this is 0,
5491 if we just turned off scroll bar mode.
5492 But in that case we should not clear them. */
5493 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5494 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5495 bar = XSCROLL_BAR (bar)->next)
5496 XClearArea (FRAME_X_DISPLAY (f),
5497 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
5498 0, 0, 0, 0, True);
5499 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5503 /* Define a queue to save up SelectionRequest events for later handling. */
5505 struct selection_event_queue
5507 XEvent event;
5508 struct selection_event_queue *next;
5511 static struct selection_event_queue *queue;
5513 /* Nonzero means queue up certain events--don't process them yet. */
5515 static int x_queue_selection_requests;
5517 /* Queue up an X event *EVENT, to be processed later. */
5519 static void
5520 x_queue_event (f, event)
5521 FRAME_PTR f;
5522 XEvent *event;
5524 struct selection_event_queue *queue_tmp
5525 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
5527 if (queue_tmp != NULL)
5529 queue_tmp->event = *event;
5530 queue_tmp->next = queue;
5531 queue = queue_tmp;
5535 /* Take all the queued events and put them back
5536 so that they get processed afresh. */
5538 static void
5539 x_unqueue_events (display)
5540 Display *display;
5542 while (queue != NULL)
5544 struct selection_event_queue *queue_tmp = queue;
5545 XPutBackEvent (display, &queue_tmp->event);
5546 queue = queue_tmp->next;
5547 xfree ((char *)queue_tmp);
5551 /* Start queuing SelectionRequest events. */
5553 void
5554 x_start_queuing_selection_requests (display)
5555 Display *display;
5557 x_queue_selection_requests++;
5560 /* Stop queuing SelectionRequest events. */
5562 void
5563 x_stop_queuing_selection_requests (display)
5564 Display *display;
5566 x_queue_selection_requests--;
5567 x_unqueue_events (display);
5570 /* The main X event-reading loop - XTread_socket. */
5572 #if 0
5573 /* Time stamp of enter window event. This is only used by XTread_socket,
5574 but we have to put it out here, since static variables within functions
5575 sometimes don't work. */
5577 static Time enter_timestamp;
5578 #endif
5580 /* This holds the state XLookupString needs to implement dead keys
5581 and other tricks known as "compose processing". _X Window System_
5582 says that a portable program can't use this, but Stephen Gildea assures
5583 me that letting the compiler initialize it to zeros will work okay.
5585 This must be defined outside of XTread_socket, for the same reasons
5586 given for enter_timestamp, above. */
5588 static XComposeStatus compose_status;
5590 /* Record the last 100 characters stored
5591 to help debug the loss-of-chars-during-GC problem. */
5593 static int temp_index;
5594 static short temp_buffer[100];
5596 /* Set this to nonzero to fake an "X I/O error"
5597 on a particular display. */
5599 struct x_display_info *XTread_socket_fake_io_error;
5601 /* When we find no input here, we occasionally do a no-op command
5602 to verify that the X server is still running and we can still talk with it.
5603 We try all the open displays, one by one.
5604 This variable is used for cycling thru the displays. */
5606 static struct x_display_info *next_noop_dpyinfo;
5608 #define SET_SAVED_MENU_EVENT(size) \
5609 do \
5611 if (f->output_data.x->saved_menu_event == 0) \
5612 f->output_data.x->saved_menu_event \
5613 = (XEvent *) xmalloc (sizeof (XEvent)); \
5614 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5615 if (numchars >= 1) \
5617 bufp->kind = MENU_BAR_ACTIVATE_EVENT; \
5618 XSETFRAME (bufp->frame_or_window, f); \
5619 bufp->arg = Qnil; \
5620 bufp++; \
5621 count++; \
5622 numchars--; \
5625 while (0)
5627 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5628 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5631 enum
5633 X_EVENT_NORMAL,
5634 X_EVENT_GOTO_OUT,
5635 X_EVENT_DROP
5638 /* Filter events for the current X input method.
5639 DPYINFO is the display this event is for.
5640 EVENT is the X event to filter.
5642 Returns non-zero if the event was filtered, caller shall not process
5643 this event further.
5644 Returns zero if event is wasn't filtered. */
5646 #ifdef HAVE_X_I18N
5647 static int
5648 x_filter_event (dpyinfo, event)
5649 struct x_display_info *dpyinfo;
5650 XEvent *event;
5652 /* XFilterEvent returns non-zero if the input method has
5653 consumed the event. We pass the frame's X window to
5654 XFilterEvent because that's the one for which the IC
5655 was created. */
5657 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5658 event->xclient.window);
5660 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5662 #endif
5664 #ifdef USE_GTK
5665 static struct x_display_info *current_dpyinfo;
5666 static struct input_event **current_bufp;
5667 static int *current_numcharsp;
5668 static int current_count;
5669 static int current_finish;
5671 /* This is the filter function invoked by the GTK event loop.
5672 It is invoked before the XEvent is translated to a GdkEvent,
5673 so we have a chanse to act on the event before GTK. */
5674 static GdkFilterReturn
5675 event_handler_gdk (gxev, ev, data)
5676 GdkXEvent *gxev;
5677 GdkEvent *ev;
5678 gpointer data;
5680 XEvent *xev = (XEvent*)gxev;
5682 if (current_numcharsp)
5684 #ifdef HAVE_X_I18N
5685 /* Filter events for the current X input method.
5686 GTK calls XFilterEvent but not for key press and release,
5687 so we do it here. */
5688 if (xev->type == KeyPress || xev->type == KeyRelease)
5689 if (x_filter_event (current_dpyinfo, xev))
5690 return GDK_FILTER_REMOVE;
5691 #endif
5692 current_count += handle_one_xevent (current_dpyinfo,
5693 xev,
5694 current_bufp,
5695 current_numcharsp,
5696 &current_finish);
5698 else
5699 current_finish = x_dispatch_event (xev, GDK_DISPLAY ());
5701 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5702 return GDK_FILTER_REMOVE;
5704 return GDK_FILTER_CONTINUE;
5706 #endif /* USE_GTK */
5709 /* Handles the XEvent EVENT on display DPYINFO.
5711 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5712 *FINISH is zero if caller should continue reading events.
5713 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5715 Events representing keys are stored in buffer *BUFP_R,
5716 which can hold up to *NUMCHARSP characters.
5717 We return the number of characters stored into the buffer. */
5719 static int
5720 handle_one_xevent (dpyinfo, eventp, bufp_r, numcharsp, finish)
5721 struct x_display_info *dpyinfo;
5722 XEvent *eventp;
5723 /* register */ struct input_event **bufp_r;
5724 /* register */ int *numcharsp;
5725 int *finish;
5727 int count = 0;
5728 int nbytes = 0;
5729 struct frame *f;
5730 struct coding_system coding;
5731 struct input_event *bufp = *bufp_r;
5732 int numchars = *numcharsp;
5733 XEvent event = *eventp;
5735 *finish = X_EVENT_NORMAL;
5737 switch (event.type)
5739 case ClientMessage:
5741 if (event.xclient.message_type
5742 == dpyinfo->Xatom_wm_protocols
5743 && event.xclient.format == 32)
5745 if (event.xclient.data.l[0]
5746 == dpyinfo->Xatom_wm_take_focus)
5748 /* Use x_any_window_to_frame because this
5749 could be the shell widget window
5750 if the frame has no title bar. */
5751 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5752 #ifdef HAVE_X_I18N
5753 /* Not quite sure this is needed -pd */
5754 if (f && FRAME_XIC (f))
5755 XSetICFocus (FRAME_XIC (f));
5756 #endif
5757 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5758 instructs the WM to set the input focus automatically for
5759 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5760 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5761 it has set the focus. So, XSetInputFocus below is not
5762 needed.
5764 The call to XSetInputFocus below has also caused trouble. In
5765 cases where the XSetInputFocus done by the WM and the one
5766 below are temporally close (on a fast machine), the call
5767 below can generate additional FocusIn events which confuse
5768 Emacs. */
5770 /* Since we set WM_TAKE_FOCUS, we must call
5771 XSetInputFocus explicitly. But not if f is null,
5772 since that might be an event for a deleted frame. */
5773 if (f)
5775 Display *d = event.xclient.display;
5776 /* Catch and ignore errors, in case window has been
5777 iconified by a window manager such as GWM. */
5778 int count = x_catch_errors (d);
5779 XSetInputFocus (d, event.xclient.window,
5780 /* The ICCCM says this is
5781 the only valid choice. */
5782 RevertToParent,
5783 event.xclient.data.l[1]);
5784 /* This is needed to detect the error
5785 if there is an error. */
5786 XSync (d, False);
5787 x_uncatch_errors (d, count);
5789 /* Not certain about handling scroll bars here */
5790 #endif /* 0 */
5792 else if (event.xclient.data.l[0]
5793 == dpyinfo->Xatom_wm_save_yourself)
5795 /* Save state modify the WM_COMMAND property to
5796 something which can reinstate us. This notifies
5797 the session manager, who's looking for such a
5798 PropertyNotify. Can restart processing when
5799 a keyboard or mouse event arrives. */
5800 /* If we have a session manager, don't set this.
5801 KDE will then start two Emacsen, one for the
5802 session manager and one for this. */
5803 if (numchars > 0
5804 #ifdef HAVE_X_SM
5805 && ! x_session_have_connection ()
5806 #endif
5809 f = x_top_window_to_frame (dpyinfo,
5810 event.xclient.window);
5811 /* This is just so we only give real data once
5812 for a single Emacs process. */
5813 if (f == SELECTED_FRAME ())
5814 XSetCommand (FRAME_X_DISPLAY (f),
5815 event.xclient.window,
5816 initial_argv, initial_argc);
5817 else if (f)
5818 XSetCommand (FRAME_X_DISPLAY (f),
5819 event.xclient.window,
5820 0, 0);
5823 else if (event.xclient.data.l[0]
5824 == dpyinfo->Xatom_wm_delete_window)
5826 struct frame *f
5827 = x_any_window_to_frame (dpyinfo,
5828 event.xclient.window);
5830 if (f)
5832 if (numchars == 0)
5833 abort ();
5835 bufp->kind = DELETE_WINDOW_EVENT;
5836 XSETFRAME (bufp->frame_or_window, f);
5837 bufp->arg = Qnil;
5838 bufp++;
5840 count += 1;
5841 numchars -= 1;
5843 else
5844 goto OTHER; /* May be a dialog that is to be removed */
5847 else if (event.xclient.message_type
5848 == dpyinfo->Xatom_wm_configure_denied)
5851 else if (event.xclient.message_type
5852 == dpyinfo->Xatom_wm_window_moved)
5854 int new_x, new_y;
5855 struct frame *f
5856 = x_window_to_frame (dpyinfo, event.xclient.window);
5858 new_x = event.xclient.data.s[0];
5859 new_y = event.xclient.data.s[1];
5861 if (f)
5863 f->left_pos = new_x;
5864 f->top_pos = new_y;
5867 #ifdef HACK_EDITRES
5868 else if (event.xclient.message_type
5869 == dpyinfo->Xatom_editres)
5871 struct frame *f
5872 = x_any_window_to_frame (dpyinfo, event.xclient.window);
5873 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5874 &event, NULL);
5876 #endif /* HACK_EDITRES */
5877 else if ((event.xclient.message_type
5878 == dpyinfo->Xatom_DONE)
5879 || (event.xclient.message_type
5880 == dpyinfo->Xatom_PAGE))
5882 /* Ghostview job completed. Kill it. We could
5883 reply with "Next" if we received "Page", but we
5884 currently never do because we are interested in
5885 images, only, which should have 1 page. */
5886 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5887 struct frame *f
5888 = x_window_to_frame (dpyinfo, event.xclient.window);
5889 x_kill_gs_process (pixmap, f);
5890 expose_frame (f, 0, 0, 0, 0);
5892 #ifdef USE_TOOLKIT_SCROLL_BARS
5893 /* Scroll bar callbacks send a ClientMessage from which
5894 we construct an input_event. */
5895 else if (event.xclient.message_type
5896 == dpyinfo->Xatom_Scrollbar)
5898 x_scroll_bar_to_input_event (&event, bufp);
5899 ++bufp, ++count, --numchars;
5900 goto out;
5902 #endif /* USE_TOOLKIT_SCROLL_BARS */
5903 else
5904 goto OTHER;
5906 break;
5908 case SelectionNotify:
5909 #ifdef USE_X_TOOLKIT
5910 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
5911 goto OTHER;
5912 #endif /* not USE_X_TOOLKIT */
5913 x_handle_selection_notify (&event.xselection);
5914 break;
5916 case SelectionClear: /* Someone has grabbed ownership. */
5917 #ifdef USE_X_TOOLKIT
5918 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
5919 goto OTHER;
5920 #endif /* USE_X_TOOLKIT */
5922 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
5924 if (numchars == 0)
5925 abort ();
5927 bufp->kind = SELECTION_CLEAR_EVENT;
5928 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
5929 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
5930 SELECTION_EVENT_TIME (bufp) = eventp->time;
5931 bufp->frame_or_window = Qnil;
5932 bufp->arg = Qnil;
5933 bufp++;
5935 count += 1;
5936 numchars -= 1;
5938 break;
5940 case SelectionRequest: /* Someone wants our selection. */
5941 #ifdef USE_X_TOOLKIT
5942 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
5943 goto OTHER;
5944 #endif /* USE_X_TOOLKIT */
5945 if (x_queue_selection_requests)
5946 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
5947 &event);
5948 else
5950 XSelectionRequestEvent *eventp
5951 = (XSelectionRequestEvent *) &event;
5953 if (numchars == 0)
5954 abort ();
5956 bufp->kind = SELECTION_REQUEST_EVENT;
5957 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
5958 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
5959 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
5960 SELECTION_EVENT_TARGET (bufp) = eventp->target;
5961 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
5962 SELECTION_EVENT_TIME (bufp) = eventp->time;
5963 bufp->frame_or_window = Qnil;
5964 bufp->arg = Qnil;
5965 bufp++;
5967 count += 1;
5968 numchars -= 1;
5970 break;
5972 case PropertyNotify:
5973 #if 0 /* This is plain wrong. In the case that we are waiting for a
5974 PropertyNotify used as an ACK in incremental selection
5975 transfer, the property will be on the receiver's window. */
5976 #if defined USE_X_TOOLKIT
5977 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
5978 goto OTHER;
5979 #endif
5980 #endif
5981 x_handle_property_notify (&event.xproperty);
5982 goto OTHER;
5984 case ReparentNotify:
5985 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
5986 if (f)
5988 int x, y;
5989 f->output_data.x->parent_desc = event.xreparent.parent;
5990 x_real_positions (f, &x, &y);
5991 f->left_pos = x;
5992 f->top_pos = y;
5994 /* Perhaps reparented due to a WM restart. Reset this. */
5995 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
5997 goto OTHER;
5998 break;
6000 case Expose:
6001 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6002 if (f)
6004 x_check_fullscreen (f);
6006 if (f->async_visible == 0)
6008 f->async_visible = 1;
6009 f->async_iconified = 0;
6010 f->output_data.x->has_been_visible = 1;
6011 SET_FRAME_GARBAGED (f);
6013 else
6014 expose_frame (f,
6015 event.xexpose.x, event.xexpose.y,
6016 event.xexpose.width, event.xexpose.height);
6018 else
6020 #ifndef USE_TOOLKIT_SCROLL_BARS
6021 struct scroll_bar *bar;
6022 #endif
6023 #if defined USE_LUCID
6024 /* Submenus of the Lucid menu bar aren't widgets
6025 themselves, so there's no way to dispatch events
6026 to them. Recognize this case separately. */
6028 Widget widget
6029 = x_window_to_menu_bar (event.xexpose.window);
6030 if (widget)
6031 xlwmenu_redisplay (widget);
6033 #endif /* USE_LUCID */
6035 #ifdef USE_TOOLKIT_SCROLL_BARS
6036 /* Dispatch event to the widget. */
6037 goto OTHER;
6038 #else /* not USE_TOOLKIT_SCROLL_BARS */
6039 bar = x_window_to_scroll_bar (event.xexpose.window);
6041 if (bar)
6042 x_scroll_bar_expose (bar, &event);
6043 #ifdef USE_X_TOOLKIT
6044 else
6045 goto OTHER;
6046 #endif /* USE_X_TOOLKIT */
6047 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6049 break;
6051 case GraphicsExpose: /* This occurs when an XCopyArea's
6052 source area was obscured or not
6053 available. */
6054 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6055 if (f)
6057 expose_frame (f,
6058 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6059 event.xgraphicsexpose.width,
6060 event.xgraphicsexpose.height);
6062 #ifdef USE_X_TOOLKIT
6063 else
6064 goto OTHER;
6065 #endif /* USE_X_TOOLKIT */
6066 break;
6068 case NoExpose: /* This occurs when an XCopyArea's
6069 source area was completely
6070 available. */
6071 break;
6073 case UnmapNotify:
6074 /* Redo the mouse-highlight after the tooltip has gone. */
6075 if (event.xmap.window == tip_window)
6077 tip_window = 0;
6078 redo_mouse_highlight ();
6081 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6082 if (f) /* F may no longer exist if
6083 the frame was deleted. */
6085 /* While a frame is unmapped, display generation is
6086 disabled; you don't want to spend time updating a
6087 display that won't ever be seen. */
6088 f->async_visible = 0;
6089 /* We can't distinguish, from the event, whether the window
6090 has become iconified or invisible. So assume, if it
6091 was previously visible, than now it is iconified.
6092 But x_make_frame_invisible clears both
6093 the visible flag and the iconified flag;
6094 and that way, we know the window is not iconified now. */
6095 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6097 f->async_iconified = 1;
6099 bufp->kind = ICONIFY_EVENT;
6100 XSETFRAME (bufp->frame_or_window, f);
6101 bufp->arg = Qnil;
6102 bufp++;
6103 count++;
6104 numchars--;
6107 goto OTHER;
6109 case MapNotify:
6110 if (event.xmap.window == tip_window)
6111 /* The tooltip has been drawn already. Avoid
6112 the SET_FRAME_GARBAGED below. */
6113 goto OTHER;
6115 /* We use x_top_window_to_frame because map events can
6116 come for sub-windows and they don't mean that the
6117 frame is visible. */
6118 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6119 if (f)
6121 /* wait_reading_process_input will notice this and update
6122 the frame's display structures.
6123 If we where iconified, we should not set garbaged,
6124 because that stops redrawing on Expose events. This looks
6125 bad if we are called from a recursive event loop
6126 (x_dispatch_event), for example when a dialog is up. */
6127 if (! f->async_iconified)
6128 SET_FRAME_GARBAGED (f);
6130 f->async_visible = 1;
6131 f->async_iconified = 0;
6132 f->output_data.x->has_been_visible = 1;
6134 if (f->iconified)
6136 bufp->kind = DEICONIFY_EVENT;
6137 XSETFRAME (bufp->frame_or_window, f);
6138 bufp->arg = Qnil;
6139 bufp++;
6140 count++;
6141 numchars--;
6143 else if (! NILP (Vframe_list)
6144 && ! NILP (XCDR (Vframe_list)))
6145 /* Force a redisplay sooner or later
6146 to update the frame titles
6147 in case this is the second frame. */
6148 record_asynch_buffer_change ();
6150 goto OTHER;
6152 case KeyPress:
6154 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6155 /* Dispatch KeyPress events when in menu. */
6156 if (popup_activated ())
6157 goto OTHER;
6158 #endif
6160 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6162 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
6164 dpyinfo->mouse_face_hidden = 1;
6165 clear_mouse_face (dpyinfo);
6168 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6169 if (f == 0)
6171 /* Scroll bars consume key events, but we want
6172 the keys to go to the scroll bar's frame. */
6173 Widget widget = XtWindowToWidget (dpyinfo->display,
6174 event.xkey.window);
6175 if (widget && XmIsScrollBar (widget))
6177 widget = XtParent (widget);
6178 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6181 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6183 if (f != 0)
6185 KeySym keysym, orig_keysym;
6186 /* al%imercury@uunet.uu.net says that making this 81
6187 instead of 80 fixed a bug whereby meta chars made
6188 his Emacs hang.
6190 It seems that some version of XmbLookupString has
6191 a bug of not returning XBufferOverflow in
6192 status_return even if the input is too long to
6193 fit in 81 bytes. So, we must prepare sufficient
6194 bytes for copy_buffer. 513 bytes (256 chars for
6195 two-byte character set) seems to be a fairly good
6196 approximation. -- 2000.8.10 handa@etl.go.jp */
6197 unsigned char copy_buffer[513];
6198 unsigned char *copy_bufptr = copy_buffer;
6199 int copy_bufsiz = sizeof (copy_buffer);
6200 int modifiers;
6201 Lisp_Object coding_system = Qlatin_1;
6203 event.xkey.state
6204 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6205 extra_keyboard_modifiers);
6206 modifiers = event.xkey.state;
6208 /* This will have to go some day... */
6210 /* make_lispy_event turns chars into control chars.
6211 Don't do it here because XLookupString is too eager. */
6212 event.xkey.state &= ~ControlMask;
6213 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6214 | dpyinfo->super_mod_mask
6215 | dpyinfo->hyper_mod_mask
6216 | dpyinfo->alt_mod_mask);
6218 /* In case Meta is ComposeCharacter,
6219 clear its status. According to Markus Ehrnsperger
6220 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6221 this enables ComposeCharacter to work whether or
6222 not it is combined with Meta. */
6223 if (modifiers & dpyinfo->meta_mod_mask)
6224 bzero (&compose_status, sizeof (compose_status));
6226 #ifdef HAVE_X_I18N
6227 if (FRAME_XIC (f))
6229 Status status_return;
6231 coding_system = Vlocale_coding_system;
6232 nbytes = XmbLookupString (FRAME_XIC (f),
6233 &event.xkey, copy_bufptr,
6234 copy_bufsiz, &keysym,
6235 &status_return);
6236 if (status_return == XBufferOverflow)
6238 copy_bufsiz = nbytes + 1;
6239 copy_bufptr = (char *) alloca (copy_bufsiz);
6240 nbytes = XmbLookupString (FRAME_XIC (f),
6241 &event.xkey, copy_bufptr,
6242 copy_bufsiz, &keysym,
6243 &status_return);
6245 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6246 #if 0 && defined X_HAVE_UTF8_STRING
6247 else if (status_return == XLookupKeySym)
6248 { /* Try again but with utf-8. */
6249 coding_system = Qutf_8;
6250 nbytes = Xutf8LookupString (FRAME_XIC (f),
6251 &event.xkey, copy_bufptr,
6252 copy_bufsiz, &keysym,
6253 &status_return);
6254 if (status_return == XBufferOverflow)
6256 copy_bufsiz = nbytes + 1;
6257 copy_bufptr = (char *) alloca (copy_bufsiz);
6258 nbytes = Xutf8LookupString (FRAME_XIC (f),
6259 &event.xkey,
6260 copy_bufptr,
6261 copy_bufsiz, &keysym,
6262 &status_return);
6265 #endif
6267 if (status_return == XLookupNone)
6268 break;
6269 else if (status_return == XLookupChars)
6271 keysym = NoSymbol;
6272 modifiers = 0;
6274 else if (status_return != XLookupKeySym
6275 && status_return != XLookupBoth)
6276 abort ();
6278 else
6279 nbytes = XLookupString (&event.xkey, copy_bufptr,
6280 copy_bufsiz, &keysym,
6281 &compose_status);
6282 #else
6283 nbytes = XLookupString (&event.xkey, copy_bufptr,
6284 copy_bufsiz, &keysym,
6285 &compose_status);
6286 #endif
6288 orig_keysym = keysym;
6290 if (numchars > 1)
6292 Lisp_Object c;
6294 /* First deal with keysyms which have defined
6295 translations to characters. */
6296 if (keysym >= 32 && keysym < 128)
6297 /* Avoid explicitly decoding each ASCII character. */
6299 bufp->kind = ASCII_KEYSTROKE_EVENT;
6300 bufp->code = keysym;
6301 XSETFRAME (bufp->frame_or_window, f);
6302 bufp->arg = Qnil;
6303 bufp->modifiers
6304 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6305 modifiers);
6306 bufp->timestamp = event.xkey.time;
6307 bufp++;
6308 count++;
6309 numchars--;
6311 /* Now non-ASCII. */
6312 else if (HASH_TABLE_P (Vx_keysym_table)
6313 && (NATNUMP (c = Fgethash (make_number (keysym),
6314 Vx_keysym_table,
6315 Qnil))))
6317 bufp->kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6318 ? ASCII_KEYSTROKE_EVENT
6319 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6320 bufp->code = XFASTINT (c);
6321 XSETFRAME (bufp->frame_or_window, f);
6322 bufp->arg = Qnil;
6323 bufp->modifiers
6324 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6325 modifiers);
6326 bufp->timestamp = event.xkey.time;
6327 bufp++;
6328 count++;
6329 numchars--;
6331 /* Random non-modifier sorts of keysyms. */
6332 else if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6333 || keysym == XK_Delete
6334 #ifdef XK_ISO_Left_Tab
6335 || (keysym >= XK_ISO_Left_Tab
6336 && keysym <= XK_ISO_Enter)
6337 #endif
6338 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6339 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6340 #ifdef HPUX
6341 /* This recognizes the "extended function
6342 keys". It seems there's no cleaner way.
6343 Test IsModifierKey to avoid handling
6344 mode_switch incorrectly. */
6345 || ((unsigned) (keysym) >= XK_Select
6346 && (unsigned)(keysym) < XK_KP_Space)
6347 #endif
6348 #ifdef XK_dead_circumflex
6349 || orig_keysym == XK_dead_circumflex
6350 #endif
6351 #ifdef XK_dead_grave
6352 || orig_keysym == XK_dead_grave
6353 #endif
6354 #ifdef XK_dead_tilde
6355 || orig_keysym == XK_dead_tilde
6356 #endif
6357 #ifdef XK_dead_diaeresis
6358 || orig_keysym == XK_dead_diaeresis
6359 #endif
6360 #ifdef XK_dead_macron
6361 || orig_keysym == XK_dead_macron
6362 #endif
6363 #ifdef XK_dead_degree
6364 || orig_keysym == XK_dead_degree
6365 #endif
6366 #ifdef XK_dead_acute
6367 || orig_keysym == XK_dead_acute
6368 #endif
6369 #ifdef XK_dead_cedilla
6370 || orig_keysym == XK_dead_cedilla
6371 #endif
6372 #ifdef XK_dead_breve
6373 || orig_keysym == XK_dead_breve
6374 #endif
6375 #ifdef XK_dead_ogonek
6376 || orig_keysym == XK_dead_ogonek
6377 #endif
6378 #ifdef XK_dead_caron
6379 || orig_keysym == XK_dead_caron
6380 #endif
6381 #ifdef XK_dead_doubleacute
6382 || orig_keysym == XK_dead_doubleacute
6383 #endif
6384 #ifdef XK_dead_abovedot
6385 || orig_keysym == XK_dead_abovedot
6386 #endif
6387 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6388 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6389 /* Any "vendor-specific" key is ok. */
6390 || (orig_keysym & (1 << 28))
6391 || (keysym != NoSymbol && nbytes == 0))
6392 && ! (IsModifierKey (orig_keysym)
6393 #ifndef HAVE_X11R5
6394 #ifdef XK_Mode_switch
6395 || ((unsigned)(orig_keysym) == XK_Mode_switch)
6396 #endif
6397 #ifdef XK_Num_Lock
6398 || ((unsigned)(orig_keysym) == XK_Num_Lock)
6399 #endif
6400 #endif /* not HAVE_X11R5 */
6401 /* The symbols from XK_ISO_Lock
6402 to XK_ISO_Last_Group_Lock
6403 don't have real modifiers but
6404 should be treated similarly to
6405 Mode_switch by Emacs. */
6406 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6407 || ((unsigned)(orig_keysym)
6408 >= XK_ISO_Lock
6409 && (unsigned)(orig_keysym)
6410 <= XK_ISO_Last_Group_Lock)
6411 #endif
6414 if (temp_index == sizeof temp_buffer / sizeof (short))
6415 temp_index = 0;
6416 temp_buffer[temp_index++] = keysym;
6417 /* make_lispy_event will convert this to a symbolic
6418 key. */
6419 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
6420 bufp->code = keysym;
6421 XSETFRAME (bufp->frame_or_window, f);
6422 bufp->arg = Qnil;
6423 bufp->modifiers
6424 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6425 modifiers);
6426 bufp->timestamp = event.xkey.time;
6427 bufp++;
6428 count++;
6429 numchars--;
6431 else if (numchars > nbytes)
6432 { /* Raw bytes, not keysym. */
6433 register int i;
6434 register int c;
6435 int nchars, len;
6437 /* The input should be decoded with `coding_system'
6438 which depends on which X*LookupString function
6439 we used just above and the locale. */
6440 setup_coding_system (coding_system, &coding);
6441 coding.src_multibyte = 0;
6442 coding.dst_multibyte = 1;
6443 /* The input is converted to events, thus we can't
6444 handle composition. Anyway, there's no XIM that
6445 gives us composition information. */
6446 coding.composing = COMPOSITION_DISABLED;
6448 for (i = 0; i < nbytes; i++)
6450 if (temp_index == (sizeof temp_buffer
6451 / sizeof (short)))
6452 temp_index = 0;
6453 temp_buffer[temp_index++] = copy_bufptr[i];
6457 /* Decode the input data. */
6458 int require;
6459 unsigned char *p;
6461 require = decoding_buffer_size (&coding, nbytes);
6462 p = (unsigned char *) alloca (require);
6463 coding.mode |= CODING_MODE_LAST_BLOCK;
6464 /* We explicitly disable composition
6465 handling because key data should
6466 not contain any composition
6467 sequence. */
6468 coding.composing = COMPOSITION_DISABLED;
6469 decode_coding (&coding, copy_bufptr, p,
6470 nbytes, require);
6471 nbytes = coding.produced;
6472 nchars = coding.produced_char;
6473 copy_bufptr = p;
6476 /* Convert the input data to a sequence of
6477 character events. */
6478 for (i = 0; i < nbytes; i += len)
6480 if (nchars == nbytes)
6481 c = copy_bufptr[i], len = 1;
6482 else
6483 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6484 nbytes - i, len);
6486 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
6487 ? ASCII_KEYSTROKE_EVENT
6488 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6489 bufp->code = c;
6490 XSETFRAME (bufp->frame_or_window, f);
6491 bufp->arg = Qnil;
6492 bufp->modifiers
6493 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6494 modifiers);
6495 bufp->timestamp = event.xkey.time;
6496 bufp++;
6499 count += nchars;
6500 numchars -= nchars;
6502 if (keysym == NoSymbol)
6503 break;
6505 else
6506 abort ();
6508 else
6509 abort ();
6511 #ifdef HAVE_X_I18N
6512 /* Don't dispatch this event since XtDispatchEvent calls
6513 XFilterEvent, and two calls in a row may freeze the
6514 client. */
6515 break;
6516 #else
6517 goto OTHER;
6518 #endif
6520 case KeyRelease:
6521 #ifdef HAVE_X_I18N
6522 /* Don't dispatch this event since XtDispatchEvent calls
6523 XFilterEvent, and two calls in a row may freeze the
6524 client. */
6525 break;
6526 #else
6527 goto OTHER;
6528 #endif
6530 case EnterNotify:
6532 int n;
6534 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6535 if (n > 0)
6537 bufp += n, count += n, numchars -= n;
6540 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6542 #if 0
6543 if (event.xcrossing.focus)
6545 /* Avoid nasty pop/raise loops. */
6546 if (f && (!(f->auto_raise)
6547 || !(f->auto_lower)
6548 || (event.xcrossing.time - enter_timestamp) > 500))
6550 x_new_focus_frame (dpyinfo, f);
6551 enter_timestamp = event.xcrossing.time;
6554 else if (f == dpyinfo->x_focus_frame)
6555 x_new_focus_frame (dpyinfo, 0);
6556 #endif
6558 /* EnterNotify counts as mouse movement,
6559 so update things that depend on mouse position. */
6560 if (f && !f->output_data.x->hourglass_p)
6561 note_mouse_movement (f, &event.xmotion);
6562 goto OTHER;
6565 case FocusIn:
6567 int n;
6569 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6570 if (n > 0)
6572 bufp += n, count += n, numchars -= n;
6576 goto OTHER;
6578 case LeaveNotify:
6580 int n;
6582 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6583 if (n > 0)
6585 bufp += n, count += n, numchars -= n;
6589 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6590 if (f)
6592 if (f == dpyinfo->mouse_face_mouse_frame)
6594 /* If we move outside the frame, then we're
6595 certainly no longer on any text in the frame. */
6596 clear_mouse_face (dpyinfo);
6597 dpyinfo->mouse_face_mouse_frame = 0;
6600 /* Generate a nil HELP_EVENT to cancel a help-echo.
6601 Do it only if there's something to cancel.
6602 Otherwise, the startup message is cleared when
6603 the mouse leaves the frame. */
6604 if (any_help_event_p)
6606 Lisp_Object frame;
6607 int n;
6609 XSETFRAME (frame, f);
6610 help_echo_string = Qnil;
6611 n = gen_help_event (bufp, numchars,
6612 Qnil, frame, Qnil, Qnil, 0);
6613 bufp += n, count += n, numchars -= n;
6617 goto OTHER;
6619 case FocusOut:
6621 int n;
6623 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6624 if (n > 0)
6626 bufp += n, count += n, numchars -= n;
6630 goto OTHER;
6632 case MotionNotify:
6634 previous_help_echo_string = help_echo_string;
6635 help_echo_string = help_echo_object = help_echo_window = Qnil;
6636 help_echo_pos = -1;
6638 if (dpyinfo->grabbed && last_mouse_frame
6639 && FRAME_LIVE_P (last_mouse_frame))
6640 f = last_mouse_frame;
6641 else
6642 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6644 if (dpyinfo->mouse_face_hidden)
6646 dpyinfo->mouse_face_hidden = 0;
6647 clear_mouse_face (dpyinfo);
6650 if (f)
6653 /* Generate SELECT_WINDOW_EVENTs when needed. */
6654 if (mouse_autoselect_window)
6656 Lisp_Object window;
6658 window = window_from_coordinates (f,
6659 event.xmotion.x, event.xmotion.y,
6660 0, 0, 0, 0);
6662 /* Window will be selected only when it is not selected now and
6663 last mouse movement event was not in it. Minibuffer window
6664 will be selected iff it is active. */
6665 if (WINDOWP(window)
6666 && !EQ (window, last_window)
6667 && !EQ (window, selected_window)
6668 && numchars > 0)
6670 bufp->kind = SELECT_WINDOW_EVENT;
6671 bufp->frame_or_window = window;
6672 bufp->arg = Qnil;
6673 ++bufp, ++count, --numchars;
6676 last_window=window;
6678 note_mouse_movement (f, &event.xmotion);
6680 else
6682 #ifndef USE_TOOLKIT_SCROLL_BARS
6683 struct scroll_bar *bar
6684 = x_window_to_scroll_bar (event.xmotion.window);
6686 if (bar)
6687 x_scroll_bar_note_movement (bar, &event);
6688 #endif /* USE_TOOLKIT_SCROLL_BARS */
6690 /* If we move outside the frame, then we're
6691 certainly no longer on any text in the frame. */
6692 clear_mouse_face (dpyinfo);
6695 /* If the contents of the global variable help_echo_string
6696 has changed, generate a HELP_EVENT. */
6697 if (!NILP (help_echo_string)
6698 || !NILP (previous_help_echo_string))
6700 Lisp_Object frame;
6701 int n;
6703 if (f)
6704 XSETFRAME (frame, f);
6705 else
6706 frame = Qnil;
6708 any_help_event_p = 1;
6709 n = gen_help_event (bufp, numchars, help_echo_string, frame,
6710 help_echo_window, help_echo_object,
6711 help_echo_pos);
6712 bufp += n, count += n, numchars -= n;
6715 goto OTHER;
6718 case ConfigureNotify:
6719 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6720 if (f)
6722 #ifndef USE_X_TOOLKIT
6723 #ifdef USE_GTK
6724 xg_resize_widgets (f, event.xconfigure.width,
6725 event.xconfigure.height);
6726 #else /* not USE_GTK */
6727 /* If there is a pending resize for fullscreen, don't
6728 do this one, the right one will come later.
6729 The toolkit version doesn't seem to need this, but we
6730 need to reset it below. */
6731 int dont_resize
6732 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6733 && f->new_text_cols != 0);
6734 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6735 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6737 if (dont_resize)
6738 goto OTHER;
6740 /* In the toolkit version, change_frame_size
6741 is called by the code that handles resizing
6742 of the EmacsFrame widget. */
6744 /* Even if the number of character rows and columns has
6745 not changed, the font size may have changed, so we need
6746 to check the pixel dimensions as well. */
6747 if (columns != FRAME_COLS (f)
6748 || rows != FRAME_LINES (f)
6749 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6750 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6752 change_frame_size (f, rows, columns, 0, 1, 0);
6753 SET_FRAME_GARBAGED (f);
6754 cancel_mouse_face (f);
6756 #endif /* not USE_GTK */
6757 #endif
6759 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6760 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6762 #ifdef USE_GTK
6763 /* GTK creates windows but doesn't map them.
6764 Only get real positions and check fullscreen when mapped. */
6765 if (FRAME_GTK_OUTER_WIDGET (f)
6766 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6767 #endif
6769 /* What we have now is the position of Emacs's own window.
6770 Convert that to the position of the window manager window. */
6771 x_real_positions (f, &f->left_pos, &f->top_pos);
6773 x_check_expected_move (f);
6774 if (f->want_fullscreen & FULLSCREEN_WAIT)
6775 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6778 #ifdef HAVE_X_I18N
6779 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6780 xic_set_statusarea (f);
6781 #endif
6783 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6785 /* Since the WM decorations come below top_pos now,
6786 we must put them below top_pos in the future. */
6787 f->win_gravity = NorthWestGravity;
6788 x_wm_set_size_hint (f, (long) 0, 0);
6791 goto OTHER;
6793 case ButtonRelease:
6794 case ButtonPress:
6796 /* If we decide we want to generate an event to be seen
6797 by the rest of Emacs, we put it here. */
6798 struct input_event emacs_event;
6799 int tool_bar_p = 0;
6801 emacs_event.kind = NO_EVENT;
6802 bzero (&compose_status, sizeof (compose_status));
6804 if (dpyinfo->grabbed
6805 && last_mouse_frame
6806 && FRAME_LIVE_P (last_mouse_frame))
6807 f = last_mouse_frame;
6808 else
6809 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6811 if (f)
6813 /* Is this in the tool-bar? */
6814 if (WINDOWP (f->tool_bar_window)
6815 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6817 Lisp_Object window;
6818 int x = event.xbutton.x;
6819 int y = event.xbutton.y;
6821 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6822 if (EQ (window, f->tool_bar_window))
6824 if (event.xbutton.type == ButtonPress)
6825 handle_tool_bar_click (f, x, y, 1, 0);
6826 else
6827 handle_tool_bar_click (f, x, y, 0,
6828 x_x_to_emacs_modifiers (dpyinfo,
6829 event.xbutton.state));
6830 tool_bar_p = 1;
6834 if (!tool_bar_p)
6835 if (!dpyinfo->x_focus_frame
6836 || f == dpyinfo->x_focus_frame)
6838 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6839 if (! popup_activated ())
6840 #endif
6841 construct_mouse_click (&emacs_event, &event, f);
6844 else
6846 struct scroll_bar *bar
6847 = x_window_to_scroll_bar (event.xbutton.window);
6849 #ifdef USE_TOOLKIT_SCROLL_BARS
6850 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6851 scroll bars. */
6852 if (bar && event.xbutton.state & ControlMask)
6854 x_scroll_bar_handle_click (bar, &event, &emacs_event);
6855 *finish = X_EVENT_DROP;
6857 #else /* not USE_TOOLKIT_SCROLL_BARS */
6858 if (bar)
6859 x_scroll_bar_handle_click (bar, &event, &emacs_event);
6860 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6863 if (event.type == ButtonPress)
6865 dpyinfo->grabbed |= (1 << event.xbutton.button);
6866 last_mouse_frame = f;
6867 /* Ignore any mouse motion that happened
6868 before this event; any subsequent mouse-movement
6869 Emacs events should reflect only motion after
6870 the ButtonPress. */
6871 if (f != 0)
6872 f->mouse_moved = 0;
6874 if (!tool_bar_p)
6875 last_tool_bar_item = -1;
6877 else
6878 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6880 if (numchars >= 1 && emacs_event.kind != NO_EVENT)
6882 bcopy (&emacs_event, bufp, sizeof (struct input_event));
6883 bufp++;
6884 count++;
6885 numchars--;
6888 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6889 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6890 /* For a down-event in the menu bar,
6891 don't pass it to Xt right now.
6892 Instead, save it away
6893 and we will pass it to Xt from kbd_buffer_get_event.
6894 That way, we can run some Lisp code first. */
6895 if (
6896 #ifdef USE_GTK
6897 ! popup_activated ()
6899 #endif
6900 f && event.type == ButtonPress
6901 /* Verify the event is really within the menu bar
6902 and not just sent to it due to grabbing. */
6903 && event.xbutton.x >= 0
6904 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6905 && event.xbutton.y >= 0
6906 && event.xbutton.y < f->output_data.x->menubar_height
6907 && event.xbutton.same_screen)
6909 SET_SAVED_BUTTON_EVENT;
6910 XSETFRAME (last_mouse_press_frame, f);
6911 #ifdef USE_GTK
6912 *finish = X_EVENT_DROP;
6913 #endif
6915 else if (event.type == ButtonPress)
6917 last_mouse_press_frame = Qnil;
6918 goto OTHER;
6921 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6922 but I am trying to be cautious. */
6923 else if (event.type == ButtonRelease)
6925 if (!NILP (last_mouse_press_frame))
6927 f = XFRAME (last_mouse_press_frame);
6928 if (f->output_data.x)
6929 SET_SAVED_BUTTON_EVENT;
6931 else
6932 goto OTHER;
6934 #endif /* USE_MOTIF */
6935 else
6936 goto OTHER;
6937 #endif /* USE_X_TOOLKIT || USE_GTK */
6939 break;
6941 case CirculateNotify:
6942 goto OTHER;
6944 case CirculateRequest:
6945 goto OTHER;
6947 case VisibilityNotify:
6948 goto OTHER;
6950 case MappingNotify:
6951 /* Someone has changed the keyboard mapping - update the
6952 local cache. */
6953 switch (event.xmapping.request)
6955 case MappingModifier:
6956 x_find_modifier_meanings (dpyinfo);
6957 /* This is meant to fall through. */
6958 case MappingKeyboard:
6959 XRefreshKeyboardMapping (&event.xmapping);
6961 goto OTHER;
6963 default:
6964 OTHER:
6965 #ifdef USE_X_TOOLKIT
6966 BLOCK_INPUT;
6967 if (*finish != X_EVENT_DROP)
6968 XtDispatchEvent (&event);
6969 UNBLOCK_INPUT;
6970 #endif /* USE_X_TOOLKIT */
6971 break;
6974 goto ret;
6976 out:
6977 *finish = X_EVENT_GOTO_OUT;
6979 ret:
6980 *bufp_r = bufp;
6981 *numcharsp = numchars;
6982 *eventp = event;
6984 return count;
6988 /* Handles the XEvent EVENT on display DISPLAY.
6989 This is used for event loops outside the normal event handling,
6990 i.e. looping while a popup menu or a dialog is posted.
6992 Returns the value handle_one_xevent sets in the finish argument. */
6994 x_dispatch_event (event, display)
6995 XEvent *event;
6996 Display *display;
6998 struct x_display_info *dpyinfo;
6999 struct input_event bufp[10];
7000 struct input_event *bufpp;
7001 int numchars = 10;
7002 int finish = X_EVENT_NORMAL;
7004 for (bufpp = bufp; bufpp != bufp + 10; bufpp++)
7005 EVENT_INIT (*bufpp);
7006 bufpp = bufp;
7008 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7009 if (dpyinfo->display == display)
7010 break;
7012 if (dpyinfo)
7014 int i, events;
7015 events = handle_one_xevent (dpyinfo,
7016 event,
7017 &bufpp,
7018 &numchars,
7019 &finish);
7020 for (i = 0; i < events; ++i)
7021 kbd_buffer_store_event (&bufp[i]);
7024 return finish;
7028 /* Read events coming from the X server.
7029 This routine is called by the SIGIO handler.
7030 We return as soon as there are no more events to be read.
7032 Events representing keys are stored in buffer BUFP,
7033 which can hold up to NUMCHARS characters.
7034 We return the number of characters stored into the buffer,
7035 thus pretending to be `read'.
7037 EXPECTED is nonzero if the caller knows input is available. */
7039 static int
7040 XTread_socket (sd, bufp, numchars, expected)
7041 register int sd;
7042 /* register */ struct input_event *bufp;
7043 /* register */ int numchars;
7044 int expected;
7046 int count = 0;
7047 XEvent event;
7048 int event_found = 0;
7049 struct x_display_info *dpyinfo;
7051 if (interrupt_input_blocked)
7053 interrupt_input_pending = 1;
7054 return -1;
7057 interrupt_input_pending = 0;
7058 BLOCK_INPUT;
7060 /* So people can tell when we have read the available input. */
7061 input_signal_count++;
7063 if (numchars <= 0)
7064 abort (); /* Don't think this happens. */
7066 ++handling_signal;
7068 /* Find the display we are supposed to read input for.
7069 It's the one communicating on descriptor SD. */
7070 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7072 #if 0 /* This ought to be unnecessary; let's verify it. */
7073 #ifdef FIOSNBIO
7074 /* If available, Xlib uses FIOSNBIO to make the socket
7075 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7076 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7077 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7078 fcntl (dpyinfo->connection, F_SETFL, 0);
7079 #endif /* ! defined (FIOSNBIO) */
7080 #endif
7082 #if 0 /* This code can't be made to work, with multiple displays,
7083 and appears not to be used on any system any more.
7084 Also keyboard.c doesn't turn O_NDELAY on and off
7085 for X connections. */
7086 #ifndef SIGIO
7087 #ifndef HAVE_SELECT
7088 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7090 extern int read_alarm_should_throw;
7091 read_alarm_should_throw = 1;
7092 XPeekEvent (dpyinfo->display, &event);
7093 read_alarm_should_throw = 0;
7095 #endif /* HAVE_SELECT */
7096 #endif /* SIGIO */
7097 #endif
7099 /* For debugging, this gives a way to fake an I/O error. */
7100 if (dpyinfo == XTread_socket_fake_io_error)
7102 XTread_socket_fake_io_error = 0;
7103 x_io_error_quitter (dpyinfo->display);
7106 #ifdef HAVE_X_SM
7107 BLOCK_INPUT;
7108 count += x_session_check_input (bufp, &numchars);
7109 UNBLOCK_INPUT;
7110 #endif
7112 #ifdef USE_GTK
7113 /* For GTK we must use the GTK event loop. But XEvents gets passed
7114 to our filter function above, and then to the big event switch.
7115 We use a bunch of globals to communicate with our filter function,
7116 that is kind of ugly, but it works. */
7117 current_dpyinfo = dpyinfo;
7119 while (gtk_events_pending ())
7121 current_count = count;
7122 current_numcharsp = &numchars;
7123 current_bufp = &bufp;
7125 gtk_main_iteration ();
7127 count = current_count;
7128 current_bufp = 0;
7129 current_numcharsp = 0;
7131 if (current_finish == X_EVENT_GOTO_OUT)
7132 goto out;
7135 #else /* not USE_GTK */
7136 while (XPending (dpyinfo->display))
7138 int finish;
7140 XNextEvent (dpyinfo->display, &event);
7142 #ifdef HAVE_X_I18N
7143 /* Filter events for the current X input method. */
7144 if (x_filter_event (dpyinfo, &event))
7145 break;
7146 #endif
7147 event_found = 1;
7149 count += handle_one_xevent (dpyinfo,
7150 &event,
7151 &bufp,
7152 &numchars,
7153 &finish);
7155 if (finish == X_EVENT_GOTO_OUT)
7156 goto out;
7158 #endif /* USE_GTK */
7161 out:;
7163 /* On some systems, an X bug causes Emacs to get no more events
7164 when the window is destroyed. Detect that. (1994.) */
7165 if (! event_found)
7167 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7168 One XNOOP in 100 loops will make Emacs terminate.
7169 B. Bretthauer, 1994 */
7170 x_noop_count++;
7171 if (x_noop_count >= 100)
7173 x_noop_count=0;
7175 if (next_noop_dpyinfo == 0)
7176 next_noop_dpyinfo = x_display_list;
7178 XNoOp (next_noop_dpyinfo->display);
7180 /* Each time we get here, cycle through the displays now open. */
7181 next_noop_dpyinfo = next_noop_dpyinfo->next;
7185 /* If the focus was just given to an auto-raising frame,
7186 raise it now. */
7187 /* ??? This ought to be able to handle more than one such frame. */
7188 if (pending_autoraise_frame)
7190 x_raise_frame (pending_autoraise_frame);
7191 pending_autoraise_frame = 0;
7194 UNBLOCK_INPUT;
7195 --handling_signal;
7196 return count;
7202 /***********************************************************************
7203 Text Cursor
7204 ***********************************************************************/
7206 /* Set clipping for output in glyph row ROW. W is the window in which
7207 we operate. GC is the graphics context to set clipping in.
7209 ROW may be a text row or, e.g., a mode line. Text rows must be
7210 clipped to the interior of the window dedicated to text display,
7211 mode lines must be clipped to the whole window. */
7213 static void
7214 x_clip_to_row (w, row, gc)
7215 struct window *w;
7216 struct glyph_row *row;
7217 GC gc;
7219 struct frame *f = XFRAME (WINDOW_FRAME (w));
7220 XRectangle clip_rect;
7221 int window_y, window_width;
7223 window_box (w, -1, 0, &window_y, &window_width, 0);
7225 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
7226 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
7227 clip_rect.y = max (clip_rect.y, window_y);
7228 clip_rect.width = window_width;
7229 clip_rect.height = row->visible_height;
7231 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7235 /* Draw a hollow box cursor on window W in glyph row ROW. */
7237 static void
7238 x_draw_hollow_cursor (w, row)
7239 struct window *w;
7240 struct glyph_row *row;
7242 struct frame *f = XFRAME (WINDOW_FRAME (w));
7243 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7244 Display *dpy = FRAME_X_DISPLAY (f);
7245 int x, y, wd, h;
7246 XGCValues xgcv;
7247 struct glyph *cursor_glyph;
7248 GC gc;
7250 /* Compute frame-relative coordinates from window-relative
7251 coordinates. */
7252 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7253 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
7254 + row->ascent - w->phys_cursor_ascent);
7255 h = row->height - 1;
7257 /* Get the glyph the cursor is on. If we can't tell because
7258 the current matrix is invalid or such, give up. */
7259 cursor_glyph = get_phys_cursor_glyph (w);
7260 if (cursor_glyph == NULL)
7261 return;
7263 /* Compute the width of the rectangle to draw. If on a stretch
7264 glyph, and `x-stretch-block-cursor' is nil, don't draw a
7265 rectangle as wide as the glyph, but use a canonical character
7266 width instead. */
7267 wd = cursor_glyph->pixel_width - 1;
7268 if (cursor_glyph->type == STRETCH_GLYPH
7269 && !x_stretch_cursor_p)
7270 wd = min (FRAME_COLUMN_WIDTH (f), wd);
7271 w->phys_cursor_width = wd;
7273 /* The foreground of cursor_gc is typically the same as the normal
7274 background color, which can cause the cursor box to be invisible. */
7275 xgcv.foreground = f->output_data.x->cursor_pixel;
7276 if (dpyinfo->scratch_cursor_gc)
7277 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7278 else
7279 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7280 GCForeground, &xgcv);
7281 gc = dpyinfo->scratch_cursor_gc;
7283 /* Set clipping, draw the rectangle, and reset clipping again. */
7284 x_clip_to_row (w, row, gc);
7285 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
7286 XSetClipMask (dpy, gc, None);
7290 /* Draw a bar cursor on window W in glyph row ROW.
7292 Implementation note: One would like to draw a bar cursor with an
7293 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7294 Unfortunately, I didn't find a font yet that has this property set.
7295 --gerd. */
7297 static void
7298 x_draw_bar_cursor (w, row, width, kind)
7299 struct window *w;
7300 struct glyph_row *row;
7301 int width;
7302 enum text_cursor_kinds kind;
7304 struct frame *f = XFRAME (w->frame);
7305 struct glyph *cursor_glyph;
7307 /* If cursor is out of bounds, don't draw garbage. This can happen
7308 in mini-buffer windows when switching between echo area glyphs
7309 and mini-buffer. */
7310 cursor_glyph = get_phys_cursor_glyph (w);
7311 if (cursor_glyph == NULL)
7312 return;
7314 /* If on an image, draw like a normal cursor. That's usually better
7315 visible than drawing a bar, esp. if the image is large so that
7316 the bar might not be in the window. */
7317 if (cursor_glyph->type == IMAGE_GLYPH)
7319 struct glyph_row *row;
7320 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7321 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7323 else
7325 Display *dpy = FRAME_X_DISPLAY (f);
7326 Window window = FRAME_X_WINDOW (f);
7327 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7328 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7329 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7330 XGCValues xgcv;
7332 /* If the glyph's background equals the color we normally draw
7333 the bar cursor in, the bar cursor in its normal color is
7334 invisible. Use the glyph's foreground color instead in this
7335 case, on the assumption that the glyph's colors are chosen so
7336 that the glyph is legible. */
7337 if (face->background == f->output_data.x->cursor_pixel)
7338 xgcv.background = xgcv.foreground = face->foreground;
7339 else
7340 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7341 xgcv.graphics_exposures = 0;
7343 if (gc)
7344 XChangeGC (dpy, gc, mask, &xgcv);
7345 else
7347 gc = XCreateGC (dpy, window, mask, &xgcv);
7348 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7351 if (width < 0)
7352 width = FRAME_CURSOR_WIDTH (f);
7353 width = min (cursor_glyph->pixel_width, width);
7355 w->phys_cursor_width = width;
7356 x_clip_to_row (w, row, gc);
7358 if (kind == BAR_CURSOR)
7359 XFillRectangle (dpy, window, gc,
7360 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7361 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7362 width, row->height);
7363 else
7364 XFillRectangle (dpy, window, gc,
7365 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7366 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7367 row->height - width),
7368 cursor_glyph->pixel_width,
7369 width);
7371 XSetClipMask (dpy, gc, None);
7376 /* RIF: Define cursor CURSOR on frame F. */
7378 static void
7379 x_define_frame_cursor (f, cursor)
7380 struct frame *f;
7381 Cursor cursor;
7383 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7387 /* RIF: Clear area on frame F. */
7389 static void
7390 x_clear_frame_area (f, x, y, width, height)
7391 struct frame *f;
7392 int x, y, width, height;
7394 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7395 x, y, width, height, False);
7399 /* RIF: Draw cursor on window W. */
7401 static void
7402 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7403 struct window *w;
7404 struct glyph_row *glyph_row;
7405 int x, y;
7406 int cursor_type, cursor_width;
7407 int on_p, active_p;
7409 struct frame *f = XFRAME (WINDOW_FRAME (w));
7411 if (on_p)
7413 w->phys_cursor_type = cursor_type;
7414 w->phys_cursor_on_p = 1;
7416 switch (cursor_type)
7418 case HOLLOW_BOX_CURSOR:
7419 x_draw_hollow_cursor (w, glyph_row);
7420 break;
7422 case FILLED_BOX_CURSOR:
7423 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7424 break;
7426 case BAR_CURSOR:
7427 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7428 break;
7430 case HBAR_CURSOR:
7431 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7432 break;
7434 case NO_CURSOR:
7435 w->phys_cursor_width = 0;
7436 break;
7438 default:
7439 abort ();
7442 #ifdef HAVE_X_I18N
7443 if (w == XWINDOW (f->selected_window))
7444 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7445 xic_set_preeditarea (w, x, y);
7446 #endif
7449 #ifndef XFlush
7450 if (updating_frame != f)
7451 XFlush (FRAME_X_DISPLAY (f));
7452 #endif
7456 /* Icons. */
7458 /* Make the x-window of frame F use the gnu icon bitmap. */
7461 x_bitmap_icon (f, file)
7462 struct frame *f;
7463 Lisp_Object file;
7465 int bitmap_id;
7467 if (FRAME_X_WINDOW (f) == 0)
7468 return 1;
7470 /* Free up our existing icon bitmap and mask if any. */
7471 if (f->output_data.x->icon_bitmap > 0)
7472 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7473 f->output_data.x->icon_bitmap = 0;
7475 if (STRINGP (file))
7477 #ifdef USE_GTK
7478 /* Use gtk_window_set_icon_from_file() if available,
7479 It's not restricted to bitmaps */
7480 if (xg_set_icon(f, file))
7481 return 0;
7482 #endif /* USE_GTK */
7483 bitmap_id = x_create_bitmap_from_file (f, file);
7484 x_create_bitmap_mask(f, bitmap_id);
7486 else
7488 /* Create the GNU bitmap and mask if necessary. */
7489 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7491 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
7492 = x_create_bitmap_from_data (f, gnu_bits,
7493 gnu_width, gnu_height);
7494 x_create_bitmap_mask(f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7497 /* The first time we create the GNU bitmap and mask,
7498 this increments the ref-count one extra time.
7499 As a result, the GNU bitmap and mask are never freed.
7500 That way, we don't have to worry about allocating it again. */
7501 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7503 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7506 x_wm_set_icon_pixmap (f, bitmap_id);
7507 f->output_data.x->icon_bitmap = bitmap_id;
7509 return 0;
7513 /* Make the x-window of frame F use a rectangle with text.
7514 Use ICON_NAME as the text. */
7517 x_text_icon (f, icon_name)
7518 struct frame *f;
7519 char *icon_name;
7521 if (FRAME_X_WINDOW (f) == 0)
7522 return 1;
7524 #ifdef HAVE_X11R4
7526 XTextProperty text;
7527 text.value = (unsigned char *) icon_name;
7528 text.encoding = XA_STRING;
7529 text.format = 8;
7530 text.nitems = strlen (icon_name);
7531 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7533 #else /* not HAVE_X11R4 */
7534 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
7535 #endif /* not HAVE_X11R4 */
7537 if (f->output_data.x->icon_bitmap > 0)
7538 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7539 f->output_data.x->icon_bitmap = 0;
7540 x_wm_set_icon_pixmap (f, 0);
7542 return 0;
7545 #define X_ERROR_MESSAGE_SIZE 200
7547 /* If non-nil, this should be a string.
7548 It means catch X errors and store the error message in this string. */
7550 static Lisp_Object x_error_message_string;
7552 /* An X error handler which stores the error message in
7553 x_error_message_string. This is called from x_error_handler if
7554 x_catch_errors is in effect. */
7556 static void
7557 x_error_catcher (display, error)
7558 Display *display;
7559 XErrorEvent *error;
7561 XGetErrorText (display, error->error_code,
7562 SDATA (x_error_message_string),
7563 X_ERROR_MESSAGE_SIZE);
7566 /* Begin trapping X errors for display DPY. Actually we trap X errors
7567 for all displays, but DPY should be the display you are actually
7568 operating on.
7570 After calling this function, X protocol errors no longer cause
7571 Emacs to exit; instead, they are recorded in the string
7572 stored in x_error_message_string.
7574 Calling x_check_errors signals an Emacs error if an X error has
7575 occurred since the last call to x_catch_errors or x_check_errors.
7577 Calling x_uncatch_errors resumes the normal error handling. */
7579 void x_check_errors ();
7580 static Lisp_Object x_catch_errors_unwind ();
7583 x_catch_errors (dpy)
7584 Display *dpy;
7586 int count = SPECPDL_INDEX ();
7588 /* Make sure any errors from previous requests have been dealt with. */
7589 XSync (dpy, False);
7591 record_unwind_protect (x_catch_errors_unwind,
7592 Fcons (make_save_value (dpy, 0),
7593 x_error_message_string));
7595 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
7596 SSET (x_error_message_string, 0, 0);
7598 return count;
7601 /* Unbind the binding that we made to check for X errors. */
7603 static Lisp_Object
7604 x_catch_errors_unwind (old_val)
7605 Lisp_Object old_val;
7607 Lisp_Object first;
7609 first = XCAR (old_val);
7611 XSync (XSAVE_VALUE (first)->pointer, False);
7613 x_error_message_string = XCDR (old_val);
7614 return Qnil;
7617 /* If any X protocol errors have arrived since the last call to
7618 x_catch_errors or x_check_errors, signal an Emacs error using
7619 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7621 void
7622 x_check_errors (dpy, format)
7623 Display *dpy;
7624 char *format;
7626 /* Make sure to catch any errors incurred so far. */
7627 XSync (dpy, False);
7629 if (SREF (x_error_message_string, 0))
7630 error (format, SDATA (x_error_message_string));
7633 /* Nonzero if we had any X protocol errors
7634 since we did x_catch_errors on DPY. */
7637 x_had_errors_p (dpy)
7638 Display *dpy;
7640 /* Make sure to catch any errors incurred so far. */
7641 XSync (dpy, False);
7643 return SREF (x_error_message_string, 0) != 0;
7646 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7648 void
7649 x_clear_errors (dpy)
7650 Display *dpy;
7652 SSET (x_error_message_string, 0, 0);
7655 /* Stop catching X protocol errors and let them make Emacs die.
7656 DPY should be the display that was passed to x_catch_errors.
7657 COUNT should be the value that was returned by
7658 the corresponding call to x_catch_errors. */
7660 void
7661 x_uncatch_errors (dpy, count)
7662 Display *dpy;
7663 int count;
7665 unbind_to (count, Qnil);
7668 #if 0
7669 static unsigned int x_wire_count;
7670 x_trace_wire ()
7672 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7674 #endif /* ! 0 */
7677 /* Handle SIGPIPE, which can happen when the connection to a server
7678 simply goes away. SIGPIPE is handled by x_connection_signal.
7679 Don't need to do anything, because the write which caused the
7680 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7681 which will do the appropriate cleanup for us. */
7683 static SIGTYPE
7684 x_connection_signal (signalnum) /* If we don't have an argument, */
7685 int signalnum; /* some compilers complain in signal calls. */
7687 #ifdef USG
7688 /* USG systems forget handlers when they are used;
7689 must reestablish each time */
7690 signal (signalnum, x_connection_signal);
7691 #endif /* USG */
7695 /************************************************************************
7696 Handling X errors
7697 ************************************************************************/
7699 /* Error message passed to x_connection_closed. */
7701 static char *error_msg;
7703 /* Function installed as fatal_error_signal_hook in
7704 x_connection_closed. Print the X error message, and exit normally,
7705 instead of dumping core when XtCloseDisplay fails. */
7707 static void
7708 x_fatal_error_signal ()
7710 fprintf (stderr, "%s\n", error_msg);
7711 exit (70);
7714 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7715 the text of an error message that lead to the connection loss. */
7717 static SIGTYPE
7718 x_connection_closed (dpy, error_message)
7719 Display *dpy;
7720 char *error_message;
7722 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7723 Lisp_Object frame, tail;
7724 int count;
7726 error_msg = (char *) alloca (strlen (error_message) + 1);
7727 strcpy (error_msg, error_message);
7728 handling_signal = 0;
7730 /* Prevent being called recursively because of an error condition
7731 below. Otherwise, we might end up with printing ``can't find per
7732 display information'' in the recursive call instead of printing
7733 the original message here. */
7734 count = x_catch_errors (dpy);
7736 /* We have to close the display to inform Xt that it doesn't
7737 exist anymore. If we don't, Xt will continue to wait for
7738 events from the display. As a consequence, a sequence of
7740 M-x make-frame-on-display RET :1 RET
7741 ...kill the new frame, so that we get an IO error...
7742 M-x make-frame-on-display RET :1 RET
7744 will indefinitely wait in Xt for events for display `:1', opened
7745 in the first class to make-frame-on-display.
7747 Closing the display is reported to lead to a bus error on
7748 OpenWindows in certain situations. I suspect that is a bug
7749 in OpenWindows. I don't know how to cicumvent it here. */
7751 #ifdef USE_X_TOOLKIT
7752 /* If DPYINFO is null, this means we didn't open the display
7753 in the first place, so don't try to close it. */
7754 if (dpyinfo)
7756 extern void (*fatal_error_signal_hook) P_ ((void));
7757 fatal_error_signal_hook = x_fatal_error_signal;
7758 XtCloseDisplay (dpy);
7759 fatal_error_signal_hook = NULL;
7761 #endif
7763 /* Indicate that this display is dead. */
7764 if (dpyinfo)
7765 dpyinfo->display = 0;
7767 /* First delete frames whose mini-buffers are on frames
7768 that are on the dead display. */
7769 FOR_EACH_FRAME (tail, frame)
7771 Lisp_Object minibuf_frame;
7772 minibuf_frame
7773 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7774 if (FRAME_X_P (XFRAME (frame))
7775 && FRAME_X_P (XFRAME (minibuf_frame))
7776 && ! EQ (frame, minibuf_frame)
7777 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7778 Fdelete_frame (frame, Qt);
7781 /* Now delete all remaining frames on the dead display.
7782 We are now sure none of these is used as the mini-buffer
7783 for another frame that we need to delete. */
7784 FOR_EACH_FRAME (tail, frame)
7785 if (FRAME_X_P (XFRAME (frame))
7786 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7788 /* Set this to t so that Fdelete_frame won't get confused
7789 trying to find a replacement. */
7790 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7791 Fdelete_frame (frame, Qt);
7794 if (dpyinfo)
7795 x_delete_display (dpyinfo);
7797 x_uncatch_errors (dpy, count);
7799 if (x_display_list == 0)
7801 fprintf (stderr, "%s\n", error_msg);
7802 shut_down_emacs (0, 0, Qnil);
7803 exit (70);
7806 /* Ordinary stack unwind doesn't deal with these. */
7807 #ifdef SIGIO
7808 sigunblock (sigmask (SIGIO));
7809 #endif
7810 sigunblock (sigmask (SIGALRM));
7811 TOTALLY_UNBLOCK_INPUT;
7813 clear_waiting_for_input ();
7814 error ("%s", error_msg);
7818 /* This is the usual handler for X protocol errors.
7819 It kills all frames on the display that we got the error for.
7820 If that was the only one, it prints an error message and kills Emacs. */
7822 static void
7823 x_error_quitter (display, error)
7824 Display *display;
7825 XErrorEvent *error;
7827 char buf[256], buf1[356];
7829 /* Note that there is no real way portable across R3/R4 to get the
7830 original error handler. */
7832 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7833 sprintf (buf1, "X protocol error: %s on protocol request %d",
7834 buf, error->request_code);
7835 x_connection_closed (display, buf1);
7839 /* This is the first-level handler for X protocol errors.
7840 It calls x_error_quitter or x_error_catcher. */
7842 static int
7843 x_error_handler (display, error)
7844 Display *display;
7845 XErrorEvent *error;
7847 if (! NILP (x_error_message_string))
7848 x_error_catcher (display, error);
7849 else
7850 x_error_quitter (display, error);
7851 return 0;
7854 /* This is the handler for X IO errors, always.
7855 It kills all frames on the display that we lost touch with.
7856 If that was the only one, it prints an error message and kills Emacs. */
7858 static int
7859 x_io_error_quitter (display)
7860 Display *display;
7862 char buf[256];
7864 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7865 x_connection_closed (display, buf);
7866 return 0;
7869 /* Changing the font of the frame. */
7871 /* Give frame F the font named FONTNAME as its default font, and
7872 return the full name of that font. FONTNAME may be a wildcard
7873 pattern; in that case, we choose some font that fits the pattern.
7874 The return value shows which font we chose. */
7876 Lisp_Object
7877 x_new_font (f, fontname)
7878 struct frame *f;
7879 register char *fontname;
7881 struct font_info *fontp
7882 = FS_LOAD_FONT (f, 0, fontname, -1);
7884 if (!fontp)
7885 return Qnil;
7887 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
7888 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
7889 FRAME_FONTSET (f) = -1;
7891 FRAME_COLUMN_WIDTH (f) = FONT_WIDTH (FRAME_FONT (f));
7892 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
7894 compute_fringe_widths (f, 1);
7896 /* Compute the scroll bar width in character columns. */
7897 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
7899 int wid = FRAME_COLUMN_WIDTH (f);
7900 FRAME_CONFIG_SCROLL_BAR_COLS (f)
7901 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
7903 else
7905 int wid = FRAME_COLUMN_WIDTH (f);
7906 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
7909 /* Now make the frame display the given font. */
7910 if (FRAME_X_WINDOW (f) != 0)
7912 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
7913 FRAME_FONT (f)->fid);
7914 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
7915 FRAME_FONT (f)->fid);
7916 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
7917 FRAME_FONT (f)->fid);
7919 /* Don't change the size of a tip frame; there's no point in
7920 doing it because it's done in Fx_show_tip, and it leads to
7921 problems because the tip frame has no widget. */
7922 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
7923 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
7926 return build_string (fontp->full_name);
7929 /* Give frame F the fontset named FONTSETNAME as its default font, and
7930 return the full name of that fontset. FONTSETNAME may be a wildcard
7931 pattern; in that case, we choose some fontset that fits the pattern.
7932 The return value shows which fontset we chose. */
7934 Lisp_Object
7935 x_new_fontset (f, fontsetname)
7936 struct frame *f;
7937 char *fontsetname;
7939 int fontset = fs_query_fontset (build_string (fontsetname), 0);
7940 Lisp_Object result;
7942 if (fontset < 0)
7943 return Qnil;
7945 if (FRAME_FONTSET (f) == fontset)
7946 /* This fontset is already set in frame F. There's nothing more
7947 to do. */
7948 return fontset_name (fontset);
7950 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
7952 if (!STRINGP (result))
7953 /* Can't load ASCII font. */
7954 return Qnil;
7956 /* Since x_new_font doesn't update any fontset information, do it now. */
7957 FRAME_FONTSET (f) = fontset;
7959 #ifdef HAVE_X_I18N
7960 if (FRAME_XIC (f)
7961 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
7962 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
7963 #endif
7965 return build_string (fontsetname);
7969 /***********************************************************************
7970 X Input Methods
7971 ***********************************************************************/
7973 #ifdef HAVE_X_I18N
7975 #ifdef HAVE_X11R6
7977 /* XIM destroy callback function, which is called whenever the
7978 connection to input method XIM dies. CLIENT_DATA contains a
7979 pointer to the x_display_info structure corresponding to XIM. */
7981 static void
7982 xim_destroy_callback (xim, client_data, call_data)
7983 XIM xim;
7984 XPointer client_data;
7985 XPointer call_data;
7987 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
7988 Lisp_Object frame, tail;
7990 BLOCK_INPUT;
7992 /* No need to call XDestroyIC.. */
7993 FOR_EACH_FRAME (tail, frame)
7995 struct frame *f = XFRAME (frame);
7996 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
7998 FRAME_XIC (f) = NULL;
7999 if (FRAME_XIC_FONTSET (f))
8001 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
8002 FRAME_XIC_FONTSET (f) = NULL;
8007 /* No need to call XCloseIM. */
8008 dpyinfo->xim = NULL;
8009 XFree (dpyinfo->xim_styles);
8010 UNBLOCK_INPUT;
8013 #endif /* HAVE_X11R6 */
8015 #ifdef HAVE_X11R6
8016 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8017 extern char *XSetIMValues P_ ((XIM, ...));
8018 #endif
8020 /* Open the connection to the XIM server on display DPYINFO.
8021 RESOURCE_NAME is the resource name Emacs uses. */
8023 static void
8024 xim_open_dpy (dpyinfo, resource_name)
8025 struct x_display_info *dpyinfo;
8026 char *resource_name;
8028 XIM xim;
8030 #ifdef HAVE_XIM
8031 if (use_xim)
8033 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8034 EMACS_CLASS);
8035 dpyinfo->xim = xim;
8037 if (xim)
8039 #ifdef HAVE_X11R6
8040 XIMCallback destroy;
8041 #endif
8043 /* Get supported styles and XIM values. */
8044 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8046 #ifdef HAVE_X11R6
8047 destroy.callback = xim_destroy_callback;
8048 destroy.client_data = (XPointer)dpyinfo;
8049 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8050 #endif
8054 else
8055 #endif /* HAVE_XIM */
8056 dpyinfo->xim = NULL;
8060 #ifdef HAVE_X11R6_XIM
8062 struct xim_inst_t
8064 struct x_display_info *dpyinfo;
8065 char *resource_name;
8068 /* XIM instantiate callback function, which is called whenever an XIM
8069 server is available. DISPLAY is the display of the XIM.
8070 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8071 when the callback was registered. */
8073 static void
8074 xim_instantiate_callback (display, client_data, call_data)
8075 Display *display;
8076 XPointer client_data;
8077 XPointer call_data;
8079 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8080 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8082 /* We don't support multiple XIM connections. */
8083 if (dpyinfo->xim)
8084 return;
8086 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8088 /* Create XIC for the existing frames on the same display, as long
8089 as they have no XIC. */
8090 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8092 Lisp_Object tail, frame;
8094 BLOCK_INPUT;
8095 FOR_EACH_FRAME (tail, frame)
8097 struct frame *f = XFRAME (frame);
8099 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8100 if (FRAME_XIC (f) == NULL)
8102 create_frame_xic (f);
8103 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8104 xic_set_statusarea (f);
8105 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8107 struct window *w = XWINDOW (f->selected_window);
8108 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8113 UNBLOCK_INPUT;
8117 #endif /* HAVE_X11R6_XIM */
8120 /* Open a connection to the XIM server on display DPYINFO.
8121 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8122 connection only at the first time. On X11R6, open the connection
8123 in the XIM instantiate callback function. */
8125 static void
8126 xim_initialize (dpyinfo, resource_name)
8127 struct x_display_info *dpyinfo;
8128 char *resource_name;
8130 #ifdef HAVE_XIM
8131 if (use_xim)
8133 #ifdef HAVE_X11R6_XIM
8134 struct xim_inst_t *xim_inst;
8135 int len;
8137 dpyinfo->xim = NULL;
8138 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8139 xim_inst->dpyinfo = dpyinfo;
8140 len = strlen (resource_name);
8141 xim_inst->resource_name = (char *) xmalloc (len + 1);
8142 bcopy (resource_name, xim_inst->resource_name, len + 1);
8143 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8144 resource_name, EMACS_CLASS,
8145 xim_instantiate_callback,
8146 /* This is XPointer in XFree86
8147 but (XPointer *) on Tru64, at
8148 least, hence the configure test. */
8149 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8150 #else /* not HAVE_X11R6_XIM */
8151 dpyinfo->xim = NULL;
8152 xim_open_dpy (dpyinfo, resource_name);
8153 #endif /* not HAVE_X11R6_XIM */
8156 else
8157 #endif /* HAVE_XIM */
8158 dpyinfo->xim = NULL;
8162 /* Close the connection to the XIM server on display DPYINFO. */
8164 static void
8165 xim_close_dpy (dpyinfo)
8166 struct x_display_info *dpyinfo;
8168 #ifdef HAVE_XIM
8169 if (use_xim)
8171 #ifdef HAVE_X11R6_XIM
8172 if (dpyinfo->display)
8173 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8174 NULL, EMACS_CLASS,
8175 xim_instantiate_callback, NULL);
8176 #endif /* not HAVE_X11R6_XIM */
8177 if (dpyinfo->display)
8178 XCloseIM (dpyinfo->xim);
8179 dpyinfo->xim = NULL;
8180 XFree (dpyinfo->xim_styles);
8182 #endif /* HAVE_XIM */
8185 #endif /* not HAVE_X11R6_XIM */
8189 /* Calculate the absolute position in frame F
8190 from its current recorded position values and gravity. */
8192 void
8193 x_calc_absolute_position (f)
8194 struct frame *f;
8196 Window child;
8197 int win_x = 0, win_y = 0;
8198 int flags = f->size_hint_flags;
8199 int this_window;
8201 /* We have nothing to do if the current position
8202 is already for the top-left corner. */
8203 if (! ((flags & XNegative) || (flags & YNegative)))
8204 return;
8206 this_window = FRAME_OUTER_WINDOW (f);
8208 /* Find the position of the outside upper-left corner of
8209 the inner window, with respect to the outer window.
8210 But do this only if we will need the results. */
8211 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8213 int count;
8215 BLOCK_INPUT;
8216 count = x_catch_errors (FRAME_X_DISPLAY (f));
8217 while (1)
8219 x_clear_errors (FRAME_X_DISPLAY (f));
8220 XTranslateCoordinates (FRAME_X_DISPLAY (f),
8222 /* From-window, to-window. */
8223 this_window,
8224 f->output_data.x->parent_desc,
8226 /* From-position, to-position. */
8227 0, 0, &win_x, &win_y,
8229 /* Child of win. */
8230 &child);
8231 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
8233 Window newroot, newparent = 0xdeadbeef;
8234 Window *newchildren;
8235 unsigned int nchildren;
8237 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
8238 &newparent, &newchildren, &nchildren))
8239 break;
8241 XFree ((char *) newchildren);
8243 f->output_data.x->parent_desc = newparent;
8245 else
8246 break;
8249 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
8250 UNBLOCK_INPUT;
8253 /* Treat negative positions as relative to the leftmost bottommost
8254 position that fits on the screen. */
8255 if (flags & XNegative)
8256 f->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8257 - 2 * f->border_width - win_x
8258 - FRAME_PIXEL_WIDTH (f)
8259 + f->left_pos);
8262 int height = FRAME_PIXEL_HEIGHT (f);
8264 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8265 /* Something is fishy here. When using Motif, starting Emacs with
8266 `-g -0-0', the frame appears too low by a few pixels.
8268 This seems to be so because initially, while Emacs is starting,
8269 the column widget's height and the frame's pixel height are
8270 different. The column widget's height is the right one. In
8271 later invocations, when Emacs is up, the frame's pixel height
8272 is right, though.
8274 It's not obvious where the initial small difference comes from.
8275 2000-12-01, gerd. */
8277 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8278 #endif
8280 if (flags & YNegative)
8281 f->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
8282 - 2 * f->border_width
8283 - win_y
8284 - height
8285 + f->top_pos);
8288 /* The left_pos and top_pos
8289 are now relative to the top and left screen edges,
8290 so the flags should correspond. */
8291 f->size_hint_flags &= ~ (XNegative | YNegative);
8294 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8295 to really change the position, and 0 when calling from
8296 x_make_frame_visible (in that case, XOFF and YOFF are the current
8297 position values). It is -1 when calling from x_set_frame_parameters,
8298 which means, do adjust for borders but don't change the gravity. */
8300 void
8301 x_set_offset (f, xoff, yoff, change_gravity)
8302 struct frame *f;
8303 register int xoff, yoff;
8304 int change_gravity;
8306 int modified_top, modified_left;
8308 if (change_gravity > 0)
8310 f->top_pos = yoff;
8311 f->left_pos = xoff;
8312 f->size_hint_flags &= ~ (XNegative | YNegative);
8313 if (xoff < 0)
8314 f->size_hint_flags |= XNegative;
8315 if (yoff < 0)
8316 f->size_hint_flags |= YNegative;
8317 f->win_gravity = NorthWestGravity;
8319 x_calc_absolute_position (f);
8321 BLOCK_INPUT;
8322 x_wm_set_size_hint (f, (long) 0, 0);
8324 modified_left = f->left_pos;
8325 modified_top = f->top_pos;
8327 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
8328 this seems to be unnecessary and incorrect. rms, 4/17/97. */
8329 /* It is a mystery why we need to add the border_width here
8330 when the frame is already visible, but experiment says we do. */
8331 if (change_gravity != 0)
8333 modified_left += f->border_width;
8334 modified_top += f->border_width;
8336 #endif
8338 if (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8340 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8341 than the WM decorations. So we use the calculated offset instead
8342 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8343 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8344 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8347 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8348 modified_left, modified_top);
8350 if (FRAME_VISIBLE_P (f)
8351 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN)
8353 FRAME_X_OUTPUT (f)->check_expected_move = 1;
8354 FRAME_X_OUTPUT (f)->expected_top = f->top_pos;
8355 FRAME_X_OUTPUT (f)->expected_left = f->left_pos;
8358 UNBLOCK_INPUT;
8361 /* Check if we need to resize the frame due to a fullscreen request.
8362 If so needed, resize the frame. */
8363 static void
8364 x_check_fullscreen (f)
8365 struct frame *f;
8367 if (f->want_fullscreen & FULLSCREEN_BOTH)
8369 int width, height, ign;
8371 x_real_positions (f, &f->left_pos, &f->top_pos);
8373 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8375 /* We do not need to move the window, it shall be taken care of
8376 when setting WM manager hints.
8377 If the frame is visible already, the position is checked by
8378 x_check_expected_move. */
8379 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8381 change_frame_size (f, height, width, 0, 1, 0);
8382 SET_FRAME_GARBAGED (f);
8383 cancel_mouse_face (f);
8385 /* Wait for the change of frame size to occur */
8386 f->want_fullscreen |= FULLSCREEN_WAIT;
8391 /* If frame parameters are set after the frame is mapped, we need to move
8392 the window.
8393 Some window managers moves the window to the right position, some
8394 moves the outer window manager window to the specified position.
8395 Here we check that we are in the right spot. If not, make a second
8396 move, assuming we are dealing with the second kind of window manager. */
8397 static void
8398 x_check_expected_move (f)
8399 struct frame *f;
8401 if (FRAME_X_OUTPUT (f)->check_expected_move)
8403 int expect_top = FRAME_X_OUTPUT (f)->expected_top;
8404 int expect_left = FRAME_X_OUTPUT (f)->expected_left;
8406 if (expect_top != f->top_pos || expect_left != f->left_pos)
8408 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8409 FRAME_X_OUTPUT (f)->move_offset_left = expect_left - f->left_pos;
8410 FRAME_X_OUTPUT (f)->move_offset_top = expect_top - f->top_pos;
8412 x_set_offset (f, expect_left, expect_top, 1);
8414 else if (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN)
8415 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8417 /* Just do this once */
8418 FRAME_X_OUTPUT (f)->check_expected_move = 0;
8423 /* Change the size of frame F's X window to COLS/ROWS in the case F
8424 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8425 top-left-corner window gravity for this size change and subsequent
8426 size changes. Otherwise we leave the window gravity unchanged. */
8428 static void
8429 x_set_window_size_1 (f, change_gravity, cols, rows)
8430 struct frame *f;
8431 int change_gravity;
8432 int cols, rows;
8434 int pixelwidth, pixelheight;
8436 check_frame_size (f, &rows, &cols);
8437 f->scroll_bar_actual_width
8438 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8440 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8441 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8442 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8444 compute_fringe_widths (f, 0);
8446 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8447 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8449 f->win_gravity = NorthWestGravity;
8450 x_wm_set_size_hint (f, (long) 0, 0);
8452 XSync (FRAME_X_DISPLAY (f), False);
8453 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8454 pixelwidth, pixelheight);
8456 /* Now, strictly speaking, we can't be sure that this is accurate,
8457 but the window manager will get around to dealing with the size
8458 change request eventually, and we'll hear how it went when the
8459 ConfigureNotify event gets here.
8461 We could just not bother storing any of this information here,
8462 and let the ConfigureNotify event set everything up, but that
8463 might be kind of confusing to the Lisp code, since size changes
8464 wouldn't be reported in the frame parameters until some random
8465 point in the future when the ConfigureNotify event arrives.
8467 We pass 1 for DELAY since we can't run Lisp code inside of
8468 a BLOCK_INPUT. */
8469 change_frame_size (f, rows, cols, 0, 1, 0);
8470 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8471 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8473 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8474 receive in the ConfigureNotify event; if we get what we asked
8475 for, then the event won't cause the screen to become garbaged, so
8476 we have to make sure to do it here. */
8477 SET_FRAME_GARBAGED (f);
8479 XFlush (FRAME_X_DISPLAY (f));
8483 /* Call this to change the size of frame F's x-window.
8484 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8485 for this size change and subsequent size changes.
8486 Otherwise we leave the window gravity unchanged. */
8488 void
8489 x_set_window_size (f, change_gravity, cols, rows)
8490 struct frame *f;
8491 int change_gravity;
8492 int cols, rows;
8494 BLOCK_INPUT;
8496 #ifdef USE_GTK
8497 if (FRAME_GTK_WIDGET (f))
8498 xg_frame_set_char_size (f, cols, rows);
8499 else
8500 x_set_window_size_1 (f, change_gravity, cols, rows);
8501 #elif USE_X_TOOLKIT
8503 if (f->output_data.x->widget != NULL)
8505 /* The x and y position of the widget is clobbered by the
8506 call to XtSetValues within EmacsFrameSetCharSize.
8507 This is a real kludge, but I don't understand Xt so I can't
8508 figure out a correct fix. Can anyone else tell me? -- rms. */
8509 int xpos = f->output_data.x->widget->core.x;
8510 int ypos = f->output_data.x->widget->core.y;
8511 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8512 f->output_data.x->widget->core.x = xpos;
8513 f->output_data.x->widget->core.y = ypos;
8515 else
8516 x_set_window_size_1 (f, change_gravity, cols, rows);
8518 #else /* not USE_X_TOOLKIT */
8520 x_set_window_size_1 (f, change_gravity, cols, rows);
8522 #endif /* not USE_X_TOOLKIT */
8524 /* If cursor was outside the new size, mark it as off. */
8525 mark_window_cursors_off (XWINDOW (f->root_window));
8527 /* Clear out any recollection of where the mouse highlighting was,
8528 since it might be in a place that's outside the new frame size.
8529 Actually checking whether it is outside is a pain in the neck,
8530 so don't try--just let the highlighting be done afresh with new size. */
8531 cancel_mouse_face (f);
8533 UNBLOCK_INPUT;
8536 /* Mouse warping. */
8538 void
8539 x_set_mouse_position (f, x, y)
8540 struct frame *f;
8541 int x, y;
8543 int pix_x, pix_y;
8545 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8546 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8548 if (pix_x < 0) pix_x = 0;
8549 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8551 if (pix_y < 0) pix_y = 0;
8552 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8554 BLOCK_INPUT;
8556 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8557 0, 0, 0, 0, pix_x, pix_y);
8558 UNBLOCK_INPUT;
8561 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8563 void
8564 x_set_mouse_pixel_position (f, pix_x, pix_y)
8565 struct frame *f;
8566 int pix_x, pix_y;
8568 BLOCK_INPUT;
8570 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8571 0, 0, 0, 0, pix_x, pix_y);
8572 UNBLOCK_INPUT;
8575 /* focus shifting, raising and lowering. */
8577 void
8578 x_focus_on_frame (f)
8579 struct frame *f;
8581 #if 0 /* This proves to be unpleasant. */
8582 x_raise_frame (f);
8583 #endif
8584 #if 0
8585 /* I don't think that the ICCCM allows programs to do things like this
8586 without the interaction of the window manager. Whatever you end up
8587 doing with this code, do it to x_unfocus_frame too. */
8588 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8589 RevertToPointerRoot, CurrentTime);
8590 #endif /* ! 0 */
8593 void
8594 x_unfocus_frame (f)
8595 struct frame *f;
8597 #if 0
8598 /* Look at the remarks in x_focus_on_frame. */
8599 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8600 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8601 RevertToPointerRoot, CurrentTime);
8602 #endif /* ! 0 */
8605 /* Raise frame F. */
8607 void
8608 x_raise_frame (f)
8609 struct frame *f;
8611 if (f->async_visible)
8613 BLOCK_INPUT;
8614 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8615 XFlush (FRAME_X_DISPLAY (f));
8616 UNBLOCK_INPUT;
8620 /* Lower frame F. */
8622 void
8623 x_lower_frame (f)
8624 struct frame *f;
8626 if (f->async_visible)
8628 BLOCK_INPUT;
8629 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8630 XFlush (FRAME_X_DISPLAY (f));
8631 UNBLOCK_INPUT;
8635 static void
8636 XTframe_raise_lower (f, raise_flag)
8637 FRAME_PTR f;
8638 int raise_flag;
8640 if (raise_flag)
8641 x_raise_frame (f);
8642 else
8643 x_lower_frame (f);
8646 /* Change of visibility. */
8648 /* This tries to wait until the frame is really visible.
8649 However, if the window manager asks the user where to position
8650 the frame, this will return before the user finishes doing that.
8651 The frame will not actually be visible at that time,
8652 but it will become visible later when the window manager
8653 finishes with it. */
8655 void
8656 x_make_frame_visible (f)
8657 struct frame *f;
8659 Lisp_Object type;
8660 int original_top, original_left;
8661 int retry_count = 2;
8663 retry:
8665 BLOCK_INPUT;
8667 type = x_icon_type (f);
8668 if (!NILP (type))
8669 x_bitmap_icon (f, type);
8671 if (! FRAME_VISIBLE_P (f))
8673 /* We test FRAME_GARBAGED_P here to make sure we don't
8674 call x_set_offset a second time
8675 if we get to x_make_frame_visible a second time
8676 before the window gets really visible. */
8677 if (! FRAME_ICONIFIED_P (f)
8678 && ! f->output_data.x->asked_for_visible)
8679 x_set_offset (f, f->left_pos, f->top_pos, 0);
8681 f->output_data.x->asked_for_visible = 1;
8683 if (! EQ (Vx_no_window_manager, Qt))
8684 x_wm_set_window_state (f, NormalState);
8685 #ifdef USE_X_TOOLKIT
8686 /* This was XtPopup, but that did nothing for an iconified frame. */
8687 XtMapWidget (f->output_data.x->widget);
8688 #else /* not USE_X_TOOLKIT */
8689 #ifdef USE_GTK
8690 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8691 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8692 #else
8693 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8694 #endif /* not USE_GTK */
8695 #endif /* not USE_X_TOOLKIT */
8696 #if 0 /* This seems to bring back scroll bars in the wrong places
8697 if the window configuration has changed. They seem
8698 to come back ok without this. */
8699 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8700 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8701 #endif
8704 XFlush (FRAME_X_DISPLAY (f));
8706 /* Synchronize to ensure Emacs knows the frame is visible
8707 before we do anything else. We do this loop with input not blocked
8708 so that incoming events are handled. */
8710 Lisp_Object frame;
8711 int count;
8712 /* This must be before UNBLOCK_INPUT
8713 since events that arrive in response to the actions above
8714 will set it when they are handled. */
8715 int previously_visible = f->output_data.x->has_been_visible;
8717 original_left = f->left_pos;
8718 original_top = f->top_pos;
8720 /* This must come after we set COUNT. */
8721 UNBLOCK_INPUT;
8723 /* We unblock here so that arriving X events are processed. */
8725 /* Now move the window back to where it was "supposed to be".
8726 But don't do it if the gravity is negative.
8727 When the gravity is negative, this uses a position
8728 that is 3 pixels too low. Perhaps that's really the border width.
8730 Don't do this if the window has never been visible before,
8731 because the window manager may choose the position
8732 and we don't want to override it. */
8734 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
8735 && f->win_gravity == NorthWestGravity
8736 && previously_visible)
8738 Drawable rootw;
8739 int x, y;
8740 unsigned int width, height, border, depth;
8742 BLOCK_INPUT;
8744 /* On some window managers (such as FVWM) moving an existing
8745 window, even to the same place, causes the window manager
8746 to introduce an offset. This can cause the window to move
8747 to an unexpected location. Check the geometry (a little
8748 slow here) and then verify that the window is in the right
8749 place. If the window is not in the right place, move it
8750 there, and take the potential window manager hit. */
8751 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8752 &rootw, &x, &y, &width, &height, &border, &depth);
8754 if (original_left != x || original_top != y)
8755 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8756 original_left, original_top);
8758 UNBLOCK_INPUT;
8761 XSETFRAME (frame, f);
8763 /* Wait until the frame is visible. Process X events until a
8764 MapNotify event has been seen, or until we think we won't get a
8765 MapNotify at all.. */
8766 for (count = input_signal_count + 10;
8767 input_signal_count < count && !FRAME_VISIBLE_P (f);)
8769 /* Force processing of queued events. */
8770 x_sync (f);
8772 /* Machines that do polling rather than SIGIO have been
8773 observed to go into a busy-wait here. So we'll fake an
8774 alarm signal to let the handler know that there's something
8775 to be read. We used to raise a real alarm, but it seems
8776 that the handler isn't always enabled here. This is
8777 probably a bug. */
8778 if (input_polling_used ())
8780 /* It could be confusing if a real alarm arrives while
8781 processing the fake one. Turn it off and let the
8782 handler reset it. */
8783 extern void poll_for_input_1 P_ ((void));
8784 int old_poll_suppress_count = poll_suppress_count;
8785 poll_suppress_count = 1;
8786 poll_for_input_1 ();
8787 poll_suppress_count = old_poll_suppress_count;
8790 /* See if a MapNotify event has been processed. */
8791 FRAME_SAMPLE_VISIBILITY (f);
8794 /* 2000-09-28: In
8796 (let ((f (selected-frame)))
8797 (iconify-frame f)
8798 (raise-frame f))
8800 the frame is not raised with various window managers on
8801 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
8802 unknown reason, the call to XtMapWidget is completely ignored.
8803 Mapping the widget a second time works. */
8805 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
8806 goto retry;
8810 /* Change from mapped state to withdrawn state. */
8812 /* Make the frame visible (mapped and not iconified). */
8814 void
8815 x_make_frame_invisible (f)
8816 struct frame *f;
8818 Window window;
8820 /* Use the frame's outermost window, not the one we normally draw on. */
8821 window = FRAME_OUTER_WINDOW (f);
8823 /* Don't keep the highlight on an invisible frame. */
8824 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8825 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
8827 #if 0/* This might add unreliability; I don't trust it -- rms. */
8828 if (! f->async_visible && ! f->async_iconified)
8829 return;
8830 #endif
8832 BLOCK_INPUT;
8834 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
8835 that the current position of the window is user-specified, rather than
8836 program-specified, so that when the window is mapped again, it will be
8837 placed at the same location, without forcing the user to position it
8838 by hand again (they have already done that once for this window.) */
8839 x_wm_set_size_hint (f, (long) 0, 1);
8841 #ifdef USE_GTK
8842 if (FRAME_GTK_OUTER_WIDGET (f))
8843 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
8844 else
8845 #endif
8847 #ifdef HAVE_X11R4
8849 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
8850 DefaultScreen (FRAME_X_DISPLAY (f))))
8852 UNBLOCK_INPUT_RESIGNAL;
8853 error ("Can't notify window manager of window withdrawal");
8855 #else /* ! defined (HAVE_X11R4) */
8857 /* Tell the window manager what we're going to do. */
8858 if (! EQ (Vx_no_window_manager, Qt))
8860 XEvent unmap;
8862 unmap.xunmap.type = UnmapNotify;
8863 unmap.xunmap.window = window;
8864 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
8865 unmap.xunmap.from_configure = False;
8866 if (! XSendEvent (FRAME_X_DISPLAY (f),
8867 DefaultRootWindow (FRAME_X_DISPLAY (f)),
8868 False,
8869 SubstructureRedirectMaskSubstructureNotifyMask,
8870 &unmap))
8872 UNBLOCK_INPUT_RESIGNAL;
8873 error ("Can't notify window manager of withdrawal");
8877 /* Unmap the window ourselves. Cheeky! */
8878 XUnmapWindow (FRAME_X_DISPLAY (f), window);
8879 #endif /* ! defined (HAVE_X11R4) */
8882 /* We can't distinguish this from iconification
8883 just by the event that we get from the server.
8884 So we can't win using the usual strategy of letting
8885 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
8886 and synchronize with the server to make sure we agree. */
8887 f->visible = 0;
8888 FRAME_ICONIFIED_P (f) = 0;
8889 f->async_visible = 0;
8890 f->async_iconified = 0;
8892 x_sync (f);
8894 UNBLOCK_INPUT;
8897 /* Change window state from mapped to iconified. */
8899 void
8900 x_iconify_frame (f)
8901 struct frame *f;
8903 int result;
8904 Lisp_Object type;
8906 /* Don't keep the highlight on an invisible frame. */
8907 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8908 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
8910 if (f->async_iconified)
8911 return;
8913 BLOCK_INPUT;
8915 FRAME_SAMPLE_VISIBILITY (f);
8917 type = x_icon_type (f);
8918 if (!NILP (type))
8919 x_bitmap_icon (f, type);
8921 #ifdef USE_GTK
8922 if (FRAME_GTK_OUTER_WIDGET (f))
8924 if (! FRAME_VISIBLE_P (f))
8925 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8927 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8928 f->iconified = 1;
8929 f->visible = 1;
8930 f->async_iconified = 1;
8931 f->async_visible = 0;
8932 UNBLOCK_INPUT;
8933 return;
8935 #endif
8937 #ifdef USE_X_TOOLKIT
8939 if (! FRAME_VISIBLE_P (f))
8941 if (! EQ (Vx_no_window_manager, Qt))
8942 x_wm_set_window_state (f, IconicState);
8943 /* This was XtPopup, but that did nothing for an iconified frame. */
8944 XtMapWidget (f->output_data.x->widget);
8945 /* The server won't give us any event to indicate
8946 that an invisible frame was changed to an icon,
8947 so we have to record it here. */
8948 f->iconified = 1;
8949 f->visible = 1;
8950 f->async_iconified = 1;
8951 f->async_visible = 0;
8952 UNBLOCK_INPUT;
8953 return;
8956 result = XIconifyWindow (FRAME_X_DISPLAY (f),
8957 XtWindow (f->output_data.x->widget),
8958 DefaultScreen (FRAME_X_DISPLAY (f)));
8959 UNBLOCK_INPUT;
8961 if (!result)
8962 error ("Can't notify window manager of iconification");
8964 f->async_iconified = 1;
8965 f->async_visible = 0;
8968 BLOCK_INPUT;
8969 XFlush (FRAME_X_DISPLAY (f));
8970 UNBLOCK_INPUT;
8971 #else /* not USE_X_TOOLKIT */
8973 /* Make sure the X server knows where the window should be positioned,
8974 in case the user deiconifies with the window manager. */
8975 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
8976 x_set_offset (f, f->left_pos, f->top_pos, 0);
8978 /* Since we don't know which revision of X we're running, we'll use both
8979 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
8981 /* X11R4: send a ClientMessage to the window manager using the
8982 WM_CHANGE_STATE type. */
8984 XEvent message;
8986 message.xclient.window = FRAME_X_WINDOW (f);
8987 message.xclient.type = ClientMessage;
8988 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
8989 message.xclient.format = 32;
8990 message.xclient.data.l[0] = IconicState;
8992 if (! XSendEvent (FRAME_X_DISPLAY (f),
8993 DefaultRootWindow (FRAME_X_DISPLAY (f)),
8994 False,
8995 SubstructureRedirectMask | SubstructureNotifyMask,
8996 &message))
8998 UNBLOCK_INPUT_RESIGNAL;
8999 error ("Can't notify window manager of iconification");
9003 /* X11R3: set the initial_state field of the window manager hints to
9004 IconicState. */
9005 x_wm_set_window_state (f, IconicState);
9007 if (!FRAME_VISIBLE_P (f))
9009 /* If the frame was withdrawn, before, we must map it. */
9010 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9013 f->async_iconified = 1;
9014 f->async_visible = 0;
9016 XFlush (FRAME_X_DISPLAY (f));
9017 UNBLOCK_INPUT;
9018 #endif /* not USE_X_TOOLKIT */
9022 /* Free X resources of frame F. */
9024 void
9025 x_free_frame_resources (f)
9026 struct frame *f;
9028 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9029 Lisp_Object bar;
9030 struct scroll_bar *b;
9032 BLOCK_INPUT;
9034 /* If a display connection is dead, don't try sending more
9035 commands to the X server. */
9036 if (dpyinfo->display)
9038 if (f->output_data.x->icon_desc)
9039 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9041 #ifdef USE_X_TOOLKIT
9042 /* Explicitly destroy the scroll bars of the frame. Without
9043 this, we get "BadDrawable" errors from the toolkit later on,
9044 presumably from expose events generated for the disappearing
9045 toolkit scroll bars. */
9046 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9048 b = XSCROLL_BAR (bar);
9049 x_scroll_bar_remove (b);
9051 #endif
9053 #ifdef HAVE_X_I18N
9054 if (FRAME_XIC (f))
9055 free_frame_xic (f);
9056 #endif
9058 #ifdef USE_X_TOOLKIT
9059 if (f->output_data.x->widget)
9061 XtDestroyWidget (f->output_data.x->widget);
9062 f->output_data.x->widget = NULL;
9064 /* Tooltips don't have widgets, only a simple X window, even if
9065 we are using a toolkit. */
9066 else if (FRAME_X_WINDOW (f))
9067 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9069 free_frame_menubar (f);
9070 #else /* !USE_X_TOOLKIT */
9072 #ifdef USE_GTK
9073 /* In the GTK version, tooltips are normal X
9074 frames. We must check and free both types. */
9075 if (FRAME_GTK_OUTER_WIDGET (f))
9077 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9078 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9079 FRAME_GTK_OUTER_WIDGET (f) = 0;
9081 #endif /* USE_GTK */
9083 if (FRAME_X_WINDOW (f))
9084 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9085 #endif /* !USE_X_TOOLKIT */
9087 unload_color (f, f->output_data.x->foreground_pixel);
9088 unload_color (f, f->output_data.x->background_pixel);
9089 unload_color (f, f->output_data.x->cursor_pixel);
9090 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9091 unload_color (f, f->output_data.x->border_pixel);
9092 unload_color (f, f->output_data.x->mouse_pixel);
9094 if (f->output_data.x->scroll_bar_background_pixel != -1)
9095 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9096 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9097 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9098 #ifdef USE_TOOLKIT_SCROLL_BARS
9099 /* Scrollbar shadow colors. */
9100 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9101 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9102 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9103 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9104 #endif /* USE_TOOLKIT_SCROLL_BARS */
9105 if (f->output_data.x->white_relief.allocated_p)
9106 unload_color (f, f->output_data.x->white_relief.pixel);
9107 if (f->output_data.x->black_relief.allocated_p)
9108 unload_color (f, f->output_data.x->black_relief.pixel);
9110 if (FRAME_FACE_CACHE (f))
9111 free_frame_faces (f);
9113 x_free_gcs (f);
9114 XFlush (FRAME_X_DISPLAY (f));
9117 if (f->output_data.x->saved_menu_event)
9118 xfree (f->output_data.x->saved_menu_event);
9120 xfree (f->output_data.x);
9121 f->output_data.x = NULL;
9123 if (f == dpyinfo->x_focus_frame)
9124 dpyinfo->x_focus_frame = 0;
9125 if (f == dpyinfo->x_focus_event_frame)
9126 dpyinfo->x_focus_event_frame = 0;
9127 if (f == dpyinfo->x_highlight_frame)
9128 dpyinfo->x_highlight_frame = 0;
9130 if (f == dpyinfo->mouse_face_mouse_frame)
9132 dpyinfo->mouse_face_beg_row
9133 = dpyinfo->mouse_face_beg_col = -1;
9134 dpyinfo->mouse_face_end_row
9135 = dpyinfo->mouse_face_end_col = -1;
9136 dpyinfo->mouse_face_window = Qnil;
9137 dpyinfo->mouse_face_deferred_gc = 0;
9138 dpyinfo->mouse_face_mouse_frame = 0;
9141 UNBLOCK_INPUT;
9145 /* Destroy the X window of frame F. */
9147 void
9148 x_destroy_window (f)
9149 struct frame *f;
9151 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9153 /* If a display connection is dead, don't try sending more
9154 commands to the X server. */
9155 if (dpyinfo->display != 0)
9156 x_free_frame_resources (f);
9158 dpyinfo->reference_count--;
9162 /* Setting window manager hints. */
9164 /* Set the normal size hints for the window manager, for frame F.
9165 FLAGS is the flags word to use--or 0 meaning preserve the flags
9166 that the window now has.
9167 If USER_POSITION is nonzero, we set the USPosition
9168 flag (this is useful when FLAGS is 0).
9169 The GTK version is in gtkutils.c */
9171 #ifndef USE_GTK
9172 void
9173 x_wm_set_size_hint (f, flags, user_position)
9174 struct frame *f;
9175 long flags;
9176 int user_position;
9178 XSizeHints size_hints;
9180 #ifdef USE_X_TOOLKIT
9181 Arg al[2];
9182 int ac = 0;
9183 Dimension widget_width, widget_height;
9184 #endif
9186 Window window = FRAME_OUTER_WINDOW (f);
9188 /* Setting PMaxSize caused various problems. */
9189 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9191 size_hints.x = f->left_pos;
9192 size_hints.y = f->top_pos;
9194 #ifdef USE_X_TOOLKIT
9195 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9196 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9197 XtGetValues (f->output_data.x->widget, al, ac);
9198 size_hints.height = widget_height;
9199 size_hints.width = widget_width;
9200 #else /* not USE_X_TOOLKIT */
9201 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9202 size_hints.width = FRAME_PIXEL_WIDTH (f);
9203 #endif /* not USE_X_TOOLKIT */
9205 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9206 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9207 size_hints.max_width
9208 = FRAME_X_DISPLAY_INFO (f)->width - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9209 size_hints.max_height
9210 = FRAME_X_DISPLAY_INFO (f)->height - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9212 /* Calculate the base and minimum sizes.
9214 (When we use the X toolkit, we don't do it here.
9215 Instead we copy the values that the widgets are using, below.) */
9216 #ifndef USE_X_TOOLKIT
9218 int base_width, base_height;
9219 int min_rows = 0, min_cols = 0;
9221 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9222 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9224 check_frame_size (f, &min_rows, &min_cols);
9226 /* The window manager uses the base width hints to calculate the
9227 current number of rows and columns in the frame while
9228 resizing; min_width and min_height aren't useful for this
9229 purpose, since they might not give the dimensions for a
9230 zero-row, zero-column frame.
9232 We use the base_width and base_height members if we have
9233 them; otherwise, we set the min_width and min_height members
9234 to the size for a zero x zero frame. */
9236 #ifdef HAVE_X11R4
9237 size_hints.flags |= PBaseSize;
9238 size_hints.base_width = base_width;
9239 size_hints.base_height = base_height;
9240 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9241 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9242 #else
9243 size_hints.min_width = base_width;
9244 size_hints.min_height = base_height;
9245 #endif
9248 /* If we don't need the old flags, we don't need the old hint at all. */
9249 if (flags)
9251 size_hints.flags |= flags;
9252 goto no_read;
9254 #endif /* not USE_X_TOOLKIT */
9257 XSizeHints hints; /* Sometimes I hate X Windows... */
9258 long supplied_return;
9259 int value;
9261 #ifdef HAVE_X11R4
9262 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9263 &supplied_return);
9264 #else
9265 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
9266 #endif
9268 #ifdef USE_X_TOOLKIT
9269 size_hints.base_height = hints.base_height;
9270 size_hints.base_width = hints.base_width;
9271 size_hints.min_height = hints.min_height;
9272 size_hints.min_width = hints.min_width;
9273 #endif
9275 if (flags)
9276 size_hints.flags |= flags;
9277 else
9279 if (value == 0)
9280 hints.flags = 0;
9281 if (hints.flags & PSize)
9282 size_hints.flags |= PSize;
9283 if (hints.flags & PPosition)
9284 size_hints.flags |= PPosition;
9285 if (hints.flags & USPosition)
9286 size_hints.flags |= USPosition;
9287 if (hints.flags & USSize)
9288 size_hints.flags |= USSize;
9292 #ifndef USE_X_TOOLKIT
9293 no_read:
9294 #endif
9296 #ifdef PWinGravity
9297 size_hints.win_gravity = f->win_gravity;
9298 size_hints.flags |= PWinGravity;
9300 if (user_position)
9302 size_hints.flags &= ~ PPosition;
9303 size_hints.flags |= USPosition;
9305 #endif /* PWinGravity */
9307 #ifdef HAVE_X11R4
9308 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9309 #else
9310 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9311 #endif
9313 #endif /* not USE_GTK */
9315 /* Used for IconicState or NormalState */
9317 void
9318 x_wm_set_window_state (f, state)
9319 struct frame *f;
9320 int state;
9322 #ifdef USE_X_TOOLKIT
9323 Arg al[1];
9325 XtSetArg (al[0], XtNinitialState, state);
9326 XtSetValues (f->output_data.x->widget, al, 1);
9327 #else /* not USE_X_TOOLKIT */
9328 Window window = FRAME_X_WINDOW (f);
9330 f->output_data.x->wm_hints.flags |= StateHint;
9331 f->output_data.x->wm_hints.initial_state = state;
9333 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9334 #endif /* not USE_X_TOOLKIT */
9337 void
9338 x_wm_set_icon_pixmap (f, pixmap_id)
9339 struct frame *f;
9340 int pixmap_id;
9342 Pixmap icon_pixmap, icon_mask;
9344 #ifndef USE_X_TOOLKIT
9345 Window window = FRAME_OUTER_WINDOW (f);
9346 #endif
9348 if (pixmap_id > 0)
9350 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9351 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9352 icon_mask = x_bitmap_mask (f, pixmap_id);
9353 f->output_data.x->wm_hints.icon_mask = icon_mask;
9355 else
9357 /* It seems there is no way to turn off use of an icon pixmap.
9358 The following line does it, only if no icon has yet been created,
9359 for some window managers. But with mwm it crashes.
9360 Some people say it should clear the IconPixmapHint bit in this case,
9361 but that doesn't work, and the X consortium said it isn't the
9362 right thing at all. Since there is no way to win,
9363 best to explicitly give up. */
9364 #if 0
9365 f->output_data.x->wm_hints.icon_pixmap = None;
9366 f->output_data.x->wm_hints.icon_mask = None;
9367 #else
9368 return;
9369 #endif
9372 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
9375 Arg al[1];
9376 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9377 XtSetValues (f->output_data.x->widget, al, 1);
9378 XtSetArg (al[0], XtNiconMask, icon_mask);
9379 XtSetValues (f->output_data.x->widget, al, 1);
9382 #else /* not USE_X_TOOLKIT */
9384 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9385 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_position (f, icon_x, icon_y)
9392 struct frame *f;
9393 int icon_x, icon_y;
9395 Window window = FRAME_OUTER_WINDOW (f);
9397 f->output_data.x->wm_hints.flags |= IconPositionHint;
9398 f->output_data.x->wm_hints.icon_x = icon_x;
9399 f->output_data.x->wm_hints.icon_y = icon_y;
9401 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9405 /***********************************************************************
9406 Fonts
9407 ***********************************************************************/
9409 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
9411 struct font_info *
9412 x_get_font_info (f, font_idx)
9413 FRAME_PTR f;
9414 int font_idx;
9416 return (FRAME_X_FONT_TABLE (f) + font_idx);
9420 /* Return a list of names of available fonts matching PATTERN on frame F.
9422 If SIZE is > 0, it is the size (maximum bounds width) of fonts
9423 to be listed.
9425 SIZE < 0 means include scalable fonts.
9427 Frame F null means we have not yet created any frame on X, and
9428 consult the first display in x_display_list. MAXNAMES sets a limit
9429 on how many fonts to match. */
9431 Lisp_Object
9432 x_list_fonts (f, pattern, size, maxnames)
9433 struct frame *f;
9434 Lisp_Object pattern;
9435 int size;
9436 int maxnames;
9438 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
9439 Lisp_Object tem, second_best;
9440 struct x_display_info *dpyinfo
9441 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
9442 Display *dpy = dpyinfo->display;
9443 int try_XLoadQueryFont = 0;
9444 int count;
9445 int allow_scalable_fonts_p = 0;
9447 if (size < 0)
9449 allow_scalable_fonts_p = 1;
9450 size = 0;
9453 patterns = Fassoc (pattern, Valternate_fontname_alist);
9454 if (NILP (patterns))
9455 patterns = Fcons (pattern, Qnil);
9457 if (maxnames == 1 && !size)
9458 /* We can return any single font matching PATTERN. */
9459 try_XLoadQueryFont = 1;
9461 for (; CONSP (patterns); patterns = XCDR (patterns))
9463 int num_fonts;
9464 char **names = NULL;
9466 pattern = XCAR (patterns);
9467 /* See if we cached the result for this particular query.
9468 The cache is an alist of the form:
9469 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
9470 tem = XCDR (dpyinfo->name_list_element);
9471 key = Fcons (Fcons (pattern, make_number (maxnames)),
9472 allow_scalable_fonts_p ? Qt : Qnil);
9473 list = Fassoc (key, tem);
9474 if (!NILP (list))
9476 list = Fcdr_safe (list);
9477 /* We have a cashed list. Don't have to get the list again. */
9478 goto label_cached;
9481 /* At first, put PATTERN in the cache. */
9483 BLOCK_INPUT;
9484 count = x_catch_errors (dpy);
9486 if (try_XLoadQueryFont)
9488 XFontStruct *font;
9489 unsigned long value;
9491 font = XLoadQueryFont (dpy, SDATA (pattern));
9492 if (x_had_errors_p (dpy))
9494 /* This error is perhaps due to insufficient memory on X
9495 server. Let's just ignore it. */
9496 font = NULL;
9497 x_clear_errors (dpy);
9500 if (font
9501 && XGetFontProperty (font, XA_FONT, &value))
9503 char *name = (char *) XGetAtomName (dpy, (Atom) value);
9504 int len = strlen (name);
9505 char *tmp;
9507 /* If DXPC (a Differential X Protocol Compressor)
9508 Ver.3.7 is running, XGetAtomName will return null
9509 string. We must avoid such a name. */
9510 if (len == 0)
9511 try_XLoadQueryFont = 0;
9512 else
9514 num_fonts = 1;
9515 names = (char **) alloca (sizeof (char *));
9516 /* Some systems only allow alloca assigned to a
9517 simple var. */
9518 tmp = (char *) alloca (len + 1); names[0] = tmp;
9519 bcopy (name, names[0], len + 1);
9520 XFree (name);
9523 else
9524 try_XLoadQueryFont = 0;
9526 if (font)
9527 XFreeFont (dpy, font);
9530 if (!try_XLoadQueryFont)
9532 /* We try at least 10 fonts because XListFonts will return
9533 auto-scaled fonts at the head. */
9534 if (maxnames < 0)
9536 int limit;
9538 for (limit = 500;;)
9540 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
9541 if (num_fonts == limit)
9543 BLOCK_INPUT;
9544 XFreeFontNames (names);
9545 UNBLOCK_INPUT;
9546 limit *= 2;
9548 else
9549 break;
9552 else
9553 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
9554 &num_fonts);
9556 if (x_had_errors_p (dpy))
9558 /* This error is perhaps due to insufficient memory on X
9559 server. Let's just ignore it. */
9560 names = NULL;
9561 x_clear_errors (dpy);
9565 x_uncatch_errors (dpy, count);
9566 UNBLOCK_INPUT;
9568 if (names)
9570 int i;
9572 /* Make a list of all the fonts we got back.
9573 Store that in the font cache for the display. */
9574 for (i = 0; i < num_fonts; i++)
9576 int width = 0;
9577 char *p = names[i];
9578 int average_width = -1, dashes = 0;
9580 /* Count the number of dashes in NAMES[I]. If there are
9581 14 dashes, and the field value following 12th dash
9582 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
9583 is usually too ugly to be used for editing. Let's
9584 ignore it. */
9585 while (*p)
9586 if (*p++ == '-')
9588 dashes++;
9589 if (dashes == 7) /* PIXEL_SIZE field */
9590 width = atoi (p);
9591 else if (dashes == 12) /* AVERAGE_WIDTH field */
9592 average_width = atoi (p);
9595 if (allow_scalable_fonts_p
9596 || dashes < 14 || average_width != 0)
9598 tem = build_string (names[i]);
9599 if (NILP (Fassoc (tem, list)))
9601 if (STRINGP (Vx_pixel_size_width_font_regexp)
9602 && ((fast_c_string_match_ignore_case
9603 (Vx_pixel_size_width_font_regexp, names[i]))
9604 >= 0))
9605 /* We can set the value of PIXEL_SIZE to the
9606 width of this font. */
9607 list = Fcons (Fcons (tem, make_number (width)), list);
9608 else
9609 /* For the moment, width is not known. */
9610 list = Fcons (Fcons (tem, Qnil), list);
9615 if (!try_XLoadQueryFont)
9617 BLOCK_INPUT;
9618 XFreeFontNames (names);
9619 UNBLOCK_INPUT;
9623 /* Now store the result in the cache. */
9624 XSETCDR (dpyinfo->name_list_element,
9625 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
9627 label_cached:
9628 if (NILP (list)) continue; /* Try the remaining alternatives. */
9630 newlist = second_best = Qnil;
9631 /* Make a list of the fonts that have the right width. */
9632 for (; CONSP (list); list = XCDR (list))
9634 int found_size;
9636 tem = XCAR (list);
9638 if (!CONSP (tem) || NILP (XCAR (tem)))
9639 continue;
9640 if (!size)
9642 newlist = Fcons (XCAR (tem), newlist);
9643 continue;
9646 if (!INTEGERP (XCDR (tem)))
9648 /* Since we have not yet known the size of this font, we
9649 must try slow function call XLoadQueryFont. */
9650 XFontStruct *thisinfo;
9652 BLOCK_INPUT;
9653 count = x_catch_errors (dpy);
9654 thisinfo = XLoadQueryFont (dpy,
9655 SDATA (XCAR (tem)));
9656 if (x_had_errors_p (dpy))
9658 /* This error is perhaps due to insufficient memory on X
9659 server. Let's just ignore it. */
9660 thisinfo = NULL;
9661 x_clear_errors (dpy);
9663 x_uncatch_errors (dpy, count);
9664 UNBLOCK_INPUT;
9666 if (thisinfo)
9668 XSETCDR (tem,
9669 (thisinfo->min_bounds.width == 0
9670 ? make_number (0)
9671 : make_number (thisinfo->max_bounds.width)));
9672 BLOCK_INPUT;
9673 XFreeFont (dpy, thisinfo);
9674 UNBLOCK_INPUT;
9676 else
9677 /* For unknown reason, the previous call of XListFont had
9678 returned a font which can't be opened. Record the size
9679 as 0 not to try to open it again. */
9680 XSETCDR (tem, make_number (0));
9683 found_size = XINT (XCDR (tem));
9684 if (found_size == size)
9685 newlist = Fcons (XCAR (tem), newlist);
9686 else if (found_size > 0)
9688 if (NILP (second_best))
9689 second_best = tem;
9690 else if (found_size < size)
9692 if (XINT (XCDR (second_best)) > size
9693 || XINT (XCDR (second_best)) < found_size)
9694 second_best = tem;
9696 else
9698 if (XINT (XCDR (second_best)) > size
9699 && XINT (XCDR (second_best)) > found_size)
9700 second_best = tem;
9704 if (!NILP (newlist))
9705 break;
9706 else if (!NILP (second_best))
9708 newlist = Fcons (XCAR (second_best), Qnil);
9709 break;
9713 return newlist;
9717 #if GLYPH_DEBUG
9719 /* Check that FONT is valid on frame F. It is if it can be found in F's
9720 font table. */
9722 static void
9723 x_check_font (f, font)
9724 struct frame *f;
9725 XFontStruct *font;
9727 int i;
9728 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9730 xassert (font != NULL);
9732 for (i = 0; i < dpyinfo->n_fonts; i++)
9733 if (dpyinfo->font_table[i].name
9734 && font == dpyinfo->font_table[i].font)
9735 break;
9737 xassert (i < dpyinfo->n_fonts);
9740 #endif /* GLYPH_DEBUG != 0 */
9742 /* Set *W to the minimum width, *H to the minimum font height of FONT.
9743 Note: There are (broken) X fonts out there with invalid XFontStruct
9744 min_bounds contents. For example, handa@etl.go.jp reports that
9745 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
9746 have font->min_bounds.width == 0. */
9748 static INLINE void
9749 x_font_min_bounds (font, w, h)
9750 XFontStruct *font;
9751 int *w, *h;
9753 *h = FONT_HEIGHT (font);
9754 *w = font->min_bounds.width;
9756 /* Try to handle the case where FONT->min_bounds has invalid
9757 contents. Since the only font known to have invalid min_bounds
9758 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
9759 if (*w <= 0)
9760 *w = font->max_bounds.width;
9764 /* Compute the smallest character width and smallest font height over
9765 all fonts available on frame F. Set the members smallest_char_width
9766 and smallest_font_height in F's x_display_info structure to
9767 the values computed. Value is non-zero if smallest_font_height or
9768 smallest_char_width become smaller than they were before. */
9770 static int
9771 x_compute_min_glyph_bounds (f)
9772 struct frame *f;
9774 int i;
9775 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9776 XFontStruct *font;
9777 int old_width = dpyinfo->smallest_char_width;
9778 int old_height = dpyinfo->smallest_font_height;
9780 dpyinfo->smallest_font_height = 100000;
9781 dpyinfo->smallest_char_width = 100000;
9783 for (i = 0; i < dpyinfo->n_fonts; ++i)
9784 if (dpyinfo->font_table[i].name)
9786 struct font_info *fontp = dpyinfo->font_table + i;
9787 int w, h;
9789 font = (XFontStruct *) fontp->font;
9790 xassert (font != (XFontStruct *) ~0);
9791 x_font_min_bounds (font, &w, &h);
9793 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
9794 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
9797 xassert (dpyinfo->smallest_char_width > 0
9798 && dpyinfo->smallest_font_height > 0);
9800 return (dpyinfo->n_fonts == 1
9801 || dpyinfo->smallest_char_width < old_width
9802 || dpyinfo->smallest_font_height < old_height);
9806 /* Load font named FONTNAME of the size SIZE for frame F, and return a
9807 pointer to the structure font_info while allocating it dynamically.
9808 If SIZE is 0, load any size of font.
9809 If loading is failed, return NULL. */
9811 struct font_info *
9812 x_load_font (f, fontname, size)
9813 struct frame *f;
9814 register char *fontname;
9815 int size;
9817 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9818 Lisp_Object font_names;
9819 int count;
9821 /* Get a list of all the fonts that match this name. Once we
9822 have a list of matching fonts, we compare them against the fonts
9823 we already have by comparing names. */
9824 font_names = x_list_fonts (f, build_string (fontname), size, 1);
9826 if (!NILP (font_names))
9828 Lisp_Object tail;
9829 int i;
9831 for (i = 0; i < dpyinfo->n_fonts; i++)
9832 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
9833 if (dpyinfo->font_table[i].name
9834 && (!strcmp (dpyinfo->font_table[i].name,
9835 SDATA (XCAR (tail)))
9836 || !strcmp (dpyinfo->font_table[i].full_name,
9837 SDATA (XCAR (tail)))))
9838 return (dpyinfo->font_table + i);
9841 /* Load the font and add it to the table. */
9843 char *full_name;
9844 XFontStruct *font;
9845 struct font_info *fontp;
9846 unsigned long value;
9847 int i;
9849 /* If we have found fonts by x_list_font, load one of them. If
9850 not, we still try to load a font by the name given as FONTNAME
9851 because XListFonts (called in x_list_font) of some X server has
9852 a bug of not finding a font even if the font surely exists and
9853 is loadable by XLoadQueryFont. */
9854 if (size > 0 && !NILP (font_names))
9855 fontname = (char *) SDATA (XCAR (font_names));
9857 BLOCK_INPUT;
9858 count = x_catch_errors (FRAME_X_DISPLAY (f));
9859 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
9860 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
9862 /* This error is perhaps due to insufficient memory on X
9863 server. Let's just ignore it. */
9864 font = NULL;
9865 x_clear_errors (FRAME_X_DISPLAY (f));
9867 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
9868 UNBLOCK_INPUT;
9869 if (!font)
9870 return NULL;
9872 /* Find a free slot in the font table. */
9873 for (i = 0; i < dpyinfo->n_fonts; ++i)
9874 if (dpyinfo->font_table[i].name == NULL)
9875 break;
9877 /* If no free slot found, maybe enlarge the font table. */
9878 if (i == dpyinfo->n_fonts
9879 && dpyinfo->n_fonts == dpyinfo->font_table_size)
9881 int sz;
9882 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
9883 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
9884 dpyinfo->font_table
9885 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
9888 fontp = dpyinfo->font_table + i;
9889 if (i == dpyinfo->n_fonts)
9890 ++dpyinfo->n_fonts;
9892 /* Now fill in the slots of *FONTP. */
9893 BLOCK_INPUT;
9894 bzero (fontp, sizeof (*fontp));
9895 fontp->font = font;
9896 fontp->font_idx = i;
9897 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
9898 bcopy (fontname, fontp->name, strlen (fontname) + 1);
9900 /* Try to get the full name of FONT. Put it in FULL_NAME. */
9901 full_name = 0;
9902 if (XGetFontProperty (font, XA_FONT, &value))
9904 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
9905 char *p = name;
9906 int dashes = 0;
9908 /* Count the number of dashes in the "full name".
9909 If it is too few, this isn't really the font's full name,
9910 so don't use it.
9911 In X11R4, the fonts did not come with their canonical names
9912 stored in them. */
9913 while (*p)
9915 if (*p == '-')
9916 dashes++;
9917 p++;
9920 if (dashes >= 13)
9922 full_name = (char *) xmalloc (p - name + 1);
9923 bcopy (name, full_name, p - name + 1);
9926 XFree (name);
9929 if (full_name != 0)
9930 fontp->full_name = full_name;
9931 else
9932 fontp->full_name = fontp->name;
9934 fontp->size = font->max_bounds.width;
9935 fontp->height = FONT_HEIGHT (font);
9937 if (NILP (font_names))
9939 /* We come here because of a bug of XListFonts mentioned at
9940 the head of this block. Let's store this information in
9941 the cache for x_list_fonts. */
9942 Lisp_Object lispy_name = build_string (fontname);
9943 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9944 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
9945 Qnil);
9947 XSETCDR (dpyinfo->name_list_element,
9948 Fcons (Fcons (key,
9949 Fcons (Fcons (lispy_full_name,
9950 make_number (fontp->size)),
9951 Qnil)),
9952 XCDR (dpyinfo->name_list_element)));
9953 if (full_name)
9955 key = Fcons (Fcons (lispy_full_name, make_number (256)),
9956 Qnil);
9957 XSETCDR (dpyinfo->name_list_element,
9958 Fcons (Fcons (key,
9959 Fcons (Fcons (lispy_full_name,
9960 make_number (fontp->size)),
9961 Qnil)),
9962 XCDR (dpyinfo->name_list_element)));
9966 /* The slot `encoding' specifies how to map a character
9967 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
9968 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
9969 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
9970 2:0xA020..0xFF7F). For the moment, we don't know which charset
9971 uses this font. So, we set information in fontp->encoding[1]
9972 which is never used by any charset. If mapping can't be
9973 decided, set FONT_ENCODING_NOT_DECIDED. */
9974 fontp->encoding[1]
9975 = (font->max_byte1 == 0
9976 /* 1-byte font */
9977 ? (font->min_char_or_byte2 < 0x80
9978 ? (font->max_char_or_byte2 < 0x80
9979 ? 0 /* 0x20..0x7F */
9980 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
9981 : 1) /* 0xA0..0xFF */
9982 /* 2-byte font */
9983 : (font->min_byte1 < 0x80
9984 ? (font->max_byte1 < 0x80
9985 ? (font->min_char_or_byte2 < 0x80
9986 ? (font->max_char_or_byte2 < 0x80
9987 ? 0 /* 0x2020..0x7F7F */
9988 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
9989 : 3) /* 0x20A0..0x7FFF */
9990 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
9991 : (font->min_char_or_byte2 < 0x80
9992 ? (font->max_char_or_byte2 < 0x80
9993 ? 2 /* 0xA020..0xFF7F */
9994 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
9995 : 1))); /* 0xA0A0..0xFFFF */
9997 fontp->baseline_offset
9998 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
9999 ? (long) value : 0);
10000 fontp->relative_compose
10001 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
10002 ? (long) value : 0);
10003 fontp->default_ascent
10004 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
10005 ? (long) value : 0);
10007 /* Set global flag fonts_changed_p to non-zero if the font loaded
10008 has a character with a smaller width than any other character
10009 before, or if the font loaded has a smaller height than any
10010 other font loaded before. If this happens, it will make a
10011 glyph matrix reallocation necessary. */
10012 fonts_changed_p |= x_compute_min_glyph_bounds (f);
10013 UNBLOCK_INPUT;
10014 return fontp;
10019 /* Return a pointer to struct font_info of a font named FONTNAME for
10020 frame F. If no such font is loaded, return NULL. */
10022 struct font_info *
10023 x_query_font (f, fontname)
10024 struct frame *f;
10025 register char *fontname;
10027 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10028 int i;
10030 for (i = 0; i < dpyinfo->n_fonts; i++)
10031 if (dpyinfo->font_table[i].name
10032 && (!strcmp (dpyinfo->font_table[i].name, fontname)
10033 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
10034 return (dpyinfo->font_table + i);
10035 return NULL;
10039 /* Find a CCL program for a font specified by FONTP, and set the member
10040 `encoder' of the structure. */
10042 void
10043 x_find_ccl_program (fontp)
10044 struct font_info *fontp;
10046 Lisp_Object list, elt;
10048 elt = Qnil;
10049 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
10051 elt = XCAR (list);
10052 if (CONSP (elt)
10053 && STRINGP (XCAR (elt))
10054 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10055 >= 0)
10056 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10057 >= 0)))
10058 break;
10061 if (! NILP (list))
10063 struct ccl_program *ccl
10064 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
10066 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
10067 xfree (ccl);
10068 else
10069 fontp->font_encoder = ccl;
10075 /***********************************************************************
10076 Initialization
10077 ***********************************************************************/
10079 #ifdef USE_X_TOOLKIT
10080 static XrmOptionDescRec emacs_options[] = {
10081 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10082 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10084 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10085 XrmoptionSepArg, NULL},
10086 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10088 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10089 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10090 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10091 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10092 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10093 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10094 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10096 #endif /* USE_X_TOOLKIT */
10098 static int x_initialized;
10100 #ifdef MULTI_KBOARD
10101 /* Test whether two display-name strings agree up to the dot that separates
10102 the screen number from the server number. */
10103 static int
10104 same_x_server (name1, name2)
10105 const char *name1, *name2;
10107 int seen_colon = 0;
10108 const unsigned char *system_name = SDATA (Vsystem_name);
10109 int system_name_length = strlen (system_name);
10110 int length_until_period = 0;
10112 while (system_name[length_until_period] != 0
10113 && system_name[length_until_period] != '.')
10114 length_until_period++;
10116 /* Treat `unix' like an empty host name. */
10117 if (! strncmp (name1, "unix:", 5))
10118 name1 += 4;
10119 if (! strncmp (name2, "unix:", 5))
10120 name2 += 4;
10121 /* Treat this host's name like an empty host name. */
10122 if (! strncmp (name1, system_name, system_name_length)
10123 && name1[system_name_length] == ':')
10124 name1 += system_name_length;
10125 if (! strncmp (name2, system_name, system_name_length)
10126 && name2[system_name_length] == ':')
10127 name2 += system_name_length;
10128 /* Treat this host's domainless name like an empty host name. */
10129 if (! strncmp (name1, system_name, length_until_period)
10130 && name1[length_until_period] == ':')
10131 name1 += length_until_period;
10132 if (! strncmp (name2, system_name, length_until_period)
10133 && name2[length_until_period] == ':')
10134 name2 += length_until_period;
10136 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10138 if (*name1 == ':')
10139 seen_colon++;
10140 if (seen_colon && *name1 == '.')
10141 return 1;
10143 return (seen_colon
10144 && (*name1 == '.' || *name1 == '\0')
10145 && (*name2 == '.' || *name2 == '\0'));
10147 #endif
10149 /* Count number of set bits in mask and number of bits to shift to
10150 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10151 to 5. */
10152 static void
10153 get_bits_and_offset (mask, bits, offset)
10154 unsigned long mask;
10155 int *bits;
10156 int *offset;
10158 int nr = 0;
10159 int off = 0;
10161 while (!(mask & 1))
10163 off++;
10164 mask >>= 1;
10167 while (mask & 1)
10169 nr++;
10170 mask >>= 1;
10173 *offset = off;
10174 *bits = nr;
10177 struct x_display_info *
10178 x_term_init (display_name, xrm_option, resource_name)
10179 Lisp_Object display_name;
10180 char *xrm_option;
10181 char *resource_name;
10183 int connection;
10184 Display *dpy;
10185 struct x_display_info *dpyinfo;
10186 XrmDatabase xrdb;
10188 BLOCK_INPUT;
10190 if (!x_initialized)
10192 x_initialize ();
10193 ++x_initialized;
10196 #ifdef USE_GTK
10198 #define NUM_ARGV 10
10199 int argc;
10200 char *argv[NUM_ARGV];
10201 char **argv2 = argv;
10202 GdkAtom atom;
10204 /* GTK 2.0 can only handle one display, GTK 2.2 can handle more
10205 than one, but this remains to be implemented. */
10206 if (x_initialized > 1)
10207 return 0;
10209 for (argc = 0; argc < NUM_ARGV; ++argc)
10210 argv[argc] = 0;
10212 argc = 0;
10213 argv[argc++] = initial_argv[0];
10215 if (! NILP (display_name))
10217 argv[argc++] = "--display";
10218 argv[argc++] = SDATA (display_name);
10221 argv[argc++] = "--name";
10222 argv[argc++] = resource_name;
10224 #ifdef HAVE_X11R5
10225 XSetLocaleModifiers ("");
10226 #endif
10228 gtk_init (&argc, &argv2);
10230 /* gtk_init does set_locale. We must fix locale after calling it. */
10231 fixup_locale ();
10232 xg_initialize ();
10234 dpy = GDK_DISPLAY ();
10236 /* NULL window -> events for all windows go to our function */
10237 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10239 /* Load our own gtkrc if it exists. */
10241 struct gcpro gcpro1, gcpro2;
10242 char *file = "~/.emacs.d/gtkrc";
10243 Lisp_Object s, abs_file;
10245 GCPRO2 (s, abs_file);
10246 s = make_string (file, strlen (file));
10247 abs_file = Fexpand_file_name(s, Qnil);
10249 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10250 gtk_rc_parse (SDATA (abs_file));
10252 UNGCPRO;
10255 XSetErrorHandler (x_error_handler);
10256 XSetIOErrorHandler (x_io_error_quitter);
10258 #else /* not USE_GTK */
10259 #ifdef USE_X_TOOLKIT
10260 /* weiner@footloose.sps.mot.com reports that this causes
10261 errors with X11R5:
10262 X protocol error: BadAtom (invalid Atom parameter)
10263 on protocol request 18skiloaf.
10264 So let's not use it until R6. */
10265 #ifdef HAVE_X11XTR6
10266 XtSetLanguageProc (NULL, NULL, NULL);
10267 #endif
10270 int argc = 0;
10271 char *argv[3];
10273 argv[0] = "";
10274 argc = 1;
10275 if (xrm_option)
10277 argv[argc++] = "-xrm";
10278 argv[argc++] = xrm_option;
10280 turn_on_atimers (0);
10281 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10282 resource_name, EMACS_CLASS,
10283 emacs_options, XtNumber (emacs_options),
10284 &argc, argv);
10285 turn_on_atimers (1);
10287 #ifdef HAVE_X11XTR6
10288 /* I think this is to compensate for XtSetLanguageProc. */
10289 fixup_locale ();
10290 #endif
10293 #else /* not USE_X_TOOLKIT */
10294 #ifdef HAVE_X11R5
10295 XSetLocaleModifiers ("");
10296 #endif
10297 dpy = XOpenDisplay (SDATA (display_name));
10298 #endif /* not USE_X_TOOLKIT */
10299 #endif /* not USE_GTK*/
10301 /* Detect failure. */
10302 if (dpy == 0)
10304 UNBLOCK_INPUT;
10305 return 0;
10308 /* We have definitely succeeded. Record the new connection. */
10310 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10311 bzero (dpyinfo, sizeof *dpyinfo);
10313 #ifdef MULTI_KBOARD
10315 struct x_display_info *share;
10316 Lisp_Object tail;
10318 for (share = x_display_list, tail = x_display_name_list; share;
10319 share = share->next, tail = XCDR (tail))
10320 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10321 SDATA (display_name)))
10322 break;
10323 if (share)
10324 dpyinfo->kboard = share->kboard;
10325 else
10327 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10328 init_kboard (dpyinfo->kboard);
10329 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10331 char *vendor = ServerVendor (dpy);
10332 UNBLOCK_INPUT;
10333 dpyinfo->kboard->Vsystem_key_alist
10334 = call1 (Qvendor_specific_keysyms,
10335 build_string (vendor ? vendor : ""));
10336 BLOCK_INPUT;
10339 dpyinfo->kboard->next_kboard = all_kboards;
10340 all_kboards = dpyinfo->kboard;
10341 /* Don't let the initial kboard remain current longer than necessary.
10342 That would cause problems if a file loaded on startup tries to
10343 prompt in the mini-buffer. */
10344 if (current_kboard == initial_kboard)
10345 current_kboard = dpyinfo->kboard;
10347 dpyinfo->kboard->reference_count++;
10349 #endif
10351 /* Put this display on the chain. */
10352 dpyinfo->next = x_display_list;
10353 x_display_list = dpyinfo;
10355 /* Put it on x_display_name_list as well, to keep them parallel. */
10356 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10357 x_display_name_list);
10358 dpyinfo->name_list_element = XCAR (x_display_name_list);
10360 dpyinfo->display = dpy;
10362 #if 0
10363 XSetAfterFunction (x_current_display, x_trace_wire);
10364 #endif /* ! 0 */
10366 dpyinfo->x_id_name
10367 = (char *) xmalloc (SBYTES (Vinvocation_name)
10368 + SBYTES (Vsystem_name)
10369 + 2);
10370 sprintf (dpyinfo->x_id_name, "%s@%s",
10371 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10373 /* Figure out which modifier bits mean what. */
10374 x_find_modifier_meanings (dpyinfo);
10376 /* Get the scroll bar cursor. */
10377 dpyinfo->vertical_scroll_bar_cursor
10378 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10380 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10381 resource_name, EMACS_CLASS);
10382 #ifdef HAVE_XRMSETDATABASE
10383 XrmSetDatabase (dpyinfo->display, xrdb);
10384 #else
10385 dpyinfo->display->db = xrdb;
10386 #endif
10387 /* Put the rdb where we can find it in a way that works on
10388 all versions. */
10389 dpyinfo->xrdb = xrdb;
10391 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10392 DefaultScreen (dpyinfo->display));
10393 select_visual (dpyinfo);
10394 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10395 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
10396 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
10397 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10398 dpyinfo->client_leader_window = 0;
10399 dpyinfo->grabbed = 0;
10400 dpyinfo->reference_count = 0;
10401 dpyinfo->icon_bitmap_id = -1;
10402 dpyinfo->font_table = NULL;
10403 dpyinfo->n_fonts = 0;
10404 dpyinfo->font_table_size = 0;
10405 dpyinfo->bitmaps = 0;
10406 dpyinfo->bitmaps_size = 0;
10407 dpyinfo->bitmaps_last = 0;
10408 dpyinfo->scratch_cursor_gc = 0;
10409 dpyinfo->mouse_face_mouse_frame = 0;
10410 dpyinfo->mouse_face_deferred_gc = 0;
10411 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10412 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10413 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10414 dpyinfo->mouse_face_window = Qnil;
10415 dpyinfo->mouse_face_overlay = Qnil;
10416 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10417 dpyinfo->mouse_face_defer = 0;
10418 dpyinfo->mouse_face_hidden = 0;
10419 dpyinfo->x_focus_frame = 0;
10420 dpyinfo->x_focus_event_frame = 0;
10421 dpyinfo->x_highlight_frame = 0;
10422 dpyinfo->image_cache = make_image_cache ();
10423 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10425 /* See if we can construct pixel values from RGB values. */
10426 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10427 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10429 if (dpyinfo->visual->class == TrueColor)
10431 get_bits_and_offset (dpyinfo->visual->red_mask,
10432 &dpyinfo->red_bits, &dpyinfo->red_offset);
10433 get_bits_and_offset (dpyinfo->visual->blue_mask,
10434 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10435 get_bits_and_offset (dpyinfo->visual->green_mask,
10436 &dpyinfo->green_bits, &dpyinfo->green_offset);
10439 /* See if a private colormap is requested. */
10440 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10442 if (dpyinfo->visual->class == PseudoColor)
10444 Lisp_Object value;
10445 value = display_x_get_resource (dpyinfo,
10446 build_string ("privateColormap"),
10447 build_string ("PrivateColormap"),
10448 Qnil, Qnil);
10449 if (STRINGP (value)
10450 && (!strcmp (SDATA (value), "true")
10451 || !strcmp (SDATA (value), "on")))
10452 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10455 else
10456 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10457 dpyinfo->visual, AllocNone);
10460 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10461 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10462 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10463 dpyinfo->resy = pixels * 25.4 / mm;
10464 pixels = DisplayWidth (dpyinfo->display, screen_number);
10465 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10466 dpyinfo->resx = pixels * 25.4 / mm;
10469 dpyinfo->Xatom_wm_protocols
10470 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10471 dpyinfo->Xatom_wm_take_focus
10472 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10473 dpyinfo->Xatom_wm_save_yourself
10474 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10475 dpyinfo->Xatom_wm_delete_window
10476 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10477 dpyinfo->Xatom_wm_change_state
10478 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10479 dpyinfo->Xatom_wm_configure_denied
10480 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10481 dpyinfo->Xatom_wm_window_moved
10482 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10483 dpyinfo->Xatom_wm_client_leader
10484 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10485 dpyinfo->Xatom_editres
10486 = XInternAtom (dpyinfo->display, "Editres", False);
10487 dpyinfo->Xatom_CLIPBOARD
10488 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10489 dpyinfo->Xatom_TIMESTAMP
10490 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10491 dpyinfo->Xatom_TEXT
10492 = XInternAtom (dpyinfo->display, "TEXT", False);
10493 dpyinfo->Xatom_COMPOUND_TEXT
10494 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10495 dpyinfo->Xatom_UTF8_STRING
10496 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10497 dpyinfo->Xatom_DELETE
10498 = XInternAtom (dpyinfo->display, "DELETE", False);
10499 dpyinfo->Xatom_MULTIPLE
10500 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10501 dpyinfo->Xatom_INCR
10502 = XInternAtom (dpyinfo->display, "INCR", False);
10503 dpyinfo->Xatom_EMACS_TMP
10504 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10505 dpyinfo->Xatom_TARGETS
10506 = XInternAtom (dpyinfo->display, "TARGETS", False);
10507 dpyinfo->Xatom_NULL
10508 = XInternAtom (dpyinfo->display, "NULL", False);
10509 dpyinfo->Xatom_ATOM_PAIR
10510 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10511 /* For properties of font. */
10512 dpyinfo->Xatom_PIXEL_SIZE
10513 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10514 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10515 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10516 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10517 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10518 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10519 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10521 /* Ghostscript support. */
10522 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10523 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10525 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10526 False);
10528 dpyinfo->cut_buffers_initialized = 0;
10530 connection = ConnectionNumber (dpyinfo->display);
10531 dpyinfo->connection = connection;
10534 char null_bits[1];
10536 null_bits[0] = 0x00;
10538 dpyinfo->null_pixel
10539 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10540 null_bits, 1, 1, (long) 0, (long) 0,
10545 extern int gray_bitmap_width, gray_bitmap_height;
10546 extern char *gray_bitmap_bits;
10547 dpyinfo->gray
10548 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10549 gray_bitmap_bits,
10550 gray_bitmap_width, gray_bitmap_height,
10551 (unsigned long) 1, (unsigned long) 0, 1);
10554 #ifdef HAVE_X_I18N
10555 xim_initialize (dpyinfo, resource_name);
10556 #endif
10558 #ifdef subprocesses
10559 /* This is only needed for distinguishing keyboard and process input. */
10560 if (connection != 0)
10561 add_keyboard_wait_descriptor (connection);
10562 #endif
10564 #ifndef F_SETOWN_BUG
10565 #ifdef F_SETOWN
10566 #ifdef F_SETOWN_SOCK_NEG
10567 /* stdin is a socket here */
10568 fcntl (connection, F_SETOWN, -getpid ());
10569 #else /* ! defined (F_SETOWN_SOCK_NEG) */
10570 fcntl (connection, F_SETOWN, getpid ());
10571 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
10572 #endif /* ! defined (F_SETOWN) */
10573 #endif /* F_SETOWN_BUG */
10575 #ifdef SIGIO
10576 if (interrupt_input)
10577 init_sigio (connection);
10578 #endif /* ! defined (SIGIO) */
10580 #ifdef USE_LUCID
10581 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
10582 /* Make sure that we have a valid font for dialog boxes
10583 so that Xt does not crash. */
10585 Display *dpy = dpyinfo->display;
10586 XrmValue d, fr, to;
10587 Font font;
10588 int count;
10590 d.addr = (XPointer)&dpy;
10591 d.size = sizeof (Display *);
10592 fr.addr = XtDefaultFont;
10593 fr.size = sizeof (XtDefaultFont);
10594 to.size = sizeof (Font *);
10595 to.addr = (XPointer)&font;
10596 count = x_catch_errors (dpy);
10597 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10598 abort ();
10599 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10600 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10601 x_uncatch_errors (dpy, count);
10603 #endif
10604 #endif
10606 /* See if we should run in synchronous mode. This is useful
10607 for debugging X code. */
10609 Lisp_Object value;
10610 value = display_x_get_resource (dpyinfo,
10611 build_string ("synchronous"),
10612 build_string ("Synchronous"),
10613 Qnil, Qnil);
10614 if (STRINGP (value)
10615 && (!strcmp (SDATA (value), "true")
10616 || !strcmp (SDATA (value), "on")))
10617 XSynchronize (dpyinfo->display, True);
10621 Lisp_Object value;
10622 value = display_x_get_resource (dpyinfo,
10623 build_string ("useXIM"),
10624 build_string ("UseXIM"),
10625 Qnil, Qnil);
10626 #ifdef USE_XIM
10627 if (STRINGP (value)
10628 && (!strcmp (XSTRING (value)->data, "false")
10629 || !strcmp (XSTRING (value)->data, "off")))
10630 use_xim = 0;
10631 #else
10632 if (STRINGP (value)
10633 && (!strcmp (XSTRING (value)->data, "true")
10634 || !strcmp (XSTRING (value)->data, "on")))
10635 use_xim = 1;
10636 #endif
10639 #ifdef HAVE_X_SM
10640 /* Only do this for the first display. */
10641 if (x_initialized == 1)
10642 x_session_initialize (dpyinfo);
10643 #endif
10645 UNBLOCK_INPUT;
10647 return dpyinfo;
10650 /* Get rid of display DPYINFO, assuming all frames are already gone,
10651 and without sending any more commands to the X server. */
10653 void
10654 x_delete_display (dpyinfo)
10655 struct x_display_info *dpyinfo;
10657 int i;
10659 delete_keyboard_wait_descriptor (dpyinfo->connection);
10661 /* Discard this display from x_display_name_list and x_display_list.
10662 We can't use Fdelq because that can quit. */
10663 if (! NILP (x_display_name_list)
10664 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10665 x_display_name_list = XCDR (x_display_name_list);
10666 else
10668 Lisp_Object tail;
10670 tail = x_display_name_list;
10671 while (CONSP (tail) && CONSP (XCDR (tail)))
10673 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10675 XSETCDR (tail, XCDR (XCDR (tail)));
10676 break;
10678 tail = XCDR (tail);
10682 if (next_noop_dpyinfo == dpyinfo)
10683 next_noop_dpyinfo = dpyinfo->next;
10685 if (x_display_list == dpyinfo)
10686 x_display_list = dpyinfo->next;
10687 else
10689 struct x_display_info *tail;
10691 for (tail = x_display_list; tail; tail = tail->next)
10692 if (tail->next == dpyinfo)
10693 tail->next = tail->next->next;
10696 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
10697 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
10698 XrmDestroyDatabase (dpyinfo->xrdb);
10699 #endif
10700 #endif
10701 #ifdef MULTI_KBOARD
10702 if (--dpyinfo->kboard->reference_count == 0)
10703 delete_kboard (dpyinfo->kboard);
10704 #endif
10705 #ifdef HAVE_X_I18N
10706 if (dpyinfo->xim)
10707 xim_close_dpy (dpyinfo);
10708 #endif
10710 /* Free the font names in the font table. */
10711 for (i = 0; i < dpyinfo->n_fonts; i++)
10712 if (dpyinfo->font_table[i].name)
10714 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
10715 xfree (dpyinfo->font_table[i].full_name);
10716 xfree (dpyinfo->font_table[i].name);
10719 if (dpyinfo->font_table->font_encoder)
10720 xfree (dpyinfo->font_table->font_encoder);
10722 xfree (dpyinfo->font_table);
10723 xfree (dpyinfo->x_id_name);
10724 xfree (dpyinfo->color_cells);
10725 xfree (dpyinfo);
10728 #ifdef USE_X_TOOLKIT
10730 /* Atimer callback function for TIMER. Called every 0.1s to process
10731 Xt timeouts, if needed. We must avoid calling XtAppPending as
10732 much as possible because that function does an implicit XFlush
10733 that slows us down. */
10735 static void
10736 x_process_timeouts (timer)
10737 struct atimer *timer;
10739 if (toolkit_scroll_bar_interaction || popup_activated ())
10741 BLOCK_INPUT;
10742 while (XtAppPending (Xt_app_con) & XtIMTimer)
10743 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10744 UNBLOCK_INPUT;
10748 #endif /* USE_X_TOOLKIT */
10751 /* Set up use of X before we make the first connection. */
10753 extern frame_parm_handler x_frame_parm_handlers[];
10755 static struct redisplay_interface x_redisplay_interface =
10757 x_frame_parm_handlers,
10758 x_produce_glyphs,
10759 x_write_glyphs,
10760 x_insert_glyphs,
10761 x_clear_end_of_line,
10762 x_scroll_run,
10763 x_after_update_window_line,
10764 x_update_window_begin,
10765 x_update_window_end,
10766 x_cursor_to,
10767 x_flush,
10768 #ifndef XFlush
10769 x_flush,
10770 #else
10771 0, /* flush_display_optional */
10772 #endif
10773 x_clear_window_mouse_face,
10774 x_get_glyph_overhangs,
10775 x_fix_overlapping_area,
10776 x_draw_fringe_bitmap,
10777 x_per_char_metric,
10778 x_encode_char,
10779 x_compute_glyph_string_overhangs,
10780 x_draw_glyph_string,
10781 x_define_frame_cursor,
10782 x_clear_frame_area,
10783 x_draw_window_cursor,
10784 x_draw_vertical_window_border,
10785 x_shift_glyphs_for_insert
10788 void
10789 x_initialize ()
10791 rif = &x_redisplay_interface;
10793 clear_frame_hook = x_clear_frame;
10794 ins_del_lines_hook = x_ins_del_lines;
10795 delete_glyphs_hook = x_delete_glyphs;
10796 ring_bell_hook = XTring_bell;
10797 reset_terminal_modes_hook = XTreset_terminal_modes;
10798 set_terminal_modes_hook = XTset_terminal_modes;
10799 update_begin_hook = x_update_begin;
10800 update_end_hook = x_update_end;
10801 set_terminal_window_hook = XTset_terminal_window;
10802 read_socket_hook = XTread_socket;
10803 frame_up_to_date_hook = XTframe_up_to_date;
10804 mouse_position_hook = XTmouse_position;
10805 frame_rehighlight_hook = XTframe_rehighlight;
10806 frame_raise_lower_hook = XTframe_raise_lower;
10807 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10808 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10809 redeem_scroll_bar_hook = XTredeem_scroll_bar;
10810 judge_scroll_bars_hook = XTjudge_scroll_bars;
10812 scroll_region_ok = 1; /* we'll scroll partial frames */
10813 char_ins_del_ok = 1;
10814 line_ins_del_ok = 1; /* we'll just blt 'em */
10815 fast_clear_end_of_line = 1; /* X does this well */
10816 memory_below_frame = 0; /* we don't remember what scrolls
10817 off the bottom */
10818 baud_rate = 19200;
10820 x_noop_count = 0;
10821 last_tool_bar_item = -1;
10822 any_help_event_p = 0;
10824 /* Try to use interrupt input; if we can't, then start polling. */
10825 Fset_input_mode (Qt, Qnil, Qt, Qnil);
10827 #ifdef USE_X_TOOLKIT
10828 XtToolkitInitialize ();
10830 Xt_app_con = XtCreateApplicationContext ();
10832 /* Register a converter from strings to pixels, which uses
10833 Emacs' color allocation infrastructure. */
10834 XtAppSetTypeConverter (Xt_app_con,
10835 XtRString, XtRPixel, cvt_string_to_pixel,
10836 cvt_string_to_pixel_args,
10837 XtNumber (cvt_string_to_pixel_args),
10838 XtCacheByDisplay, cvt_pixel_dtor);
10840 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10842 /* Install an asynchronous timer that processes Xt timeout events
10843 every 0.1s. This is necessary because some widget sets use
10844 timeouts internally, for example the LessTif menu bar, or the
10845 Xaw3d scroll bar. When Xt timouts aren't processed, these
10846 widgets don't behave normally. */
10848 EMACS_TIME interval;
10849 EMACS_SET_SECS_USECS (interval, 0, 100000);
10850 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
10852 #endif
10854 #ifdef USE_TOOLKIT_SCROLL_BARS
10855 #ifndef USE_GTK
10856 xaw3d_arrow_scroll = False;
10857 xaw3d_pick_top = True;
10858 #endif
10859 #endif
10861 /* Note that there is no real way portable across R3/R4 to get the
10862 original error handler. */
10863 XSetErrorHandler (x_error_handler);
10864 XSetIOErrorHandler (x_io_error_quitter);
10866 /* Disable Window Change signals; they are handled by X events. */
10867 #ifdef SIGWINCH
10868 signal (SIGWINCH, SIG_DFL);
10869 #endif /* SIGWINCH */
10871 signal (SIGPIPE, x_connection_signal);
10875 void
10876 syms_of_xterm ()
10878 staticpro (&x_error_message_string);
10879 x_error_message_string = Qnil;
10881 staticpro (&x_display_name_list);
10882 x_display_name_list = Qnil;
10884 staticpro (&last_mouse_scroll_bar);
10885 last_mouse_scroll_bar = Qnil;
10887 staticpro (&Qvendor_specific_keysyms);
10888 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10890 staticpro (&Qutf_8);
10891 Qutf_8 = intern ("utf-8");
10892 staticpro (&Qlatin_1);
10893 Qlatin_1 = intern ("latin-1");
10895 staticpro (&last_mouse_press_frame);
10896 last_mouse_press_frame = Qnil;
10898 DEFVAR_BOOL ("x-use-underline-position-properties",
10899 &x_use_underline_position_properties,
10900 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10901 nil means ignore them. If you encounter fonts with bogus
10902 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10903 to 4.1, set this to nil. */);
10904 x_use_underline_position_properties = 1;
10906 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10907 doc: /* What X toolkit scroll bars Emacs uses.
10908 A value of nil means Emacs doesn't use X toolkit scroll bars.
10909 Otherwise, value is a symbol describing the X toolkit. */);
10910 #ifdef USE_TOOLKIT_SCROLL_BARS
10911 #ifdef USE_MOTIF
10912 Vx_toolkit_scroll_bars = intern ("motif");
10913 #elif defined HAVE_XAW3D
10914 Vx_toolkit_scroll_bars = intern ("xaw3d");
10915 #elif USE_GTK
10916 Vx_toolkit_scroll_bars = intern ("gtk");
10917 #else
10918 Vx_toolkit_scroll_bars = intern ("xaw");
10919 #endif
10920 #else
10921 Vx_toolkit_scroll_bars = Qnil;
10922 #endif
10924 staticpro (&last_mouse_motion_frame);
10925 last_mouse_motion_frame = Qnil;
10927 Qmodifier_value = intern ("modifier-value");
10928 Qalt = intern ("alt");
10929 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10930 Qhyper = intern ("hyper");
10931 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10932 Qmeta = intern ("meta");
10933 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10934 Qsuper = intern ("super");
10935 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10937 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10938 doc: /* Which keys Emacs uses for the alt modifier.
10939 This should be one of the symbols `alt', `hyper', `meta', `super'.
10940 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10941 is nil, which is the same as `alt'. */);
10942 Vx_alt_keysym = Qnil;
10944 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
10945 doc: /* Which keys Emacs uses for the hyper modifier.
10946 This should be one of the symbols `alt', `hyper', `meta', `super'.
10947 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10948 default is nil, which is the same as `hyper'. */);
10949 Vx_hyper_keysym = Qnil;
10951 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
10952 doc: /* Which keys Emacs uses for the meta modifier.
10953 This should be one of the symbols `alt', `hyper', `meta', `super'.
10954 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10955 default is nil, which is the same as `meta'. */);
10956 Vx_meta_keysym = Qnil;
10958 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
10959 doc: /* Which keys Emacs uses for the super modifier.
10960 This should be one of the symbols `alt', `hyper', `meta', `super'.
10961 For example, `super' means use the Super_L and Super_R keysyms. The
10962 default is nil, which is the same as `super'. */);
10963 Vx_super_keysym = Qnil;
10965 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
10966 doc: /* Hash table of character codes indexed by X keysym codes. */);
10967 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
10968 make_float (DEFAULT_REHASH_SIZE),
10969 make_float (DEFAULT_REHASH_THRESHOLD),
10970 Qnil, Qnil, Qnil);
10973 #endif /* HAVE_X_WINDOWS */
10975 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
10976 (do not change this comment) */