(struct mac_output): Move members left_pos, top_pos,
[emacs.git] / src / xterm.c
blob1d28bb5b92a14da6587545273d1d04496bc3b8ca
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 int use_xim = 1;
165 /* Non-nil means Emacs uses toolkit scroll bars. */
167 Lisp_Object Vx_toolkit_scroll_bars;
169 /* Non-zero means that a HELP_EVENT has been generated since Emacs
170 start. */
172 static int any_help_event_p;
174 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
175 static Lisp_Object last_window;
177 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
179 int x_use_underline_position_properties;
181 /* This is a chain of structures for all the X displays currently in
182 use. */
184 struct x_display_info *x_display_list;
186 /* This is a list of cons cells, each of the form (NAME
187 . FONT-LIST-CACHE), one for each element of x_display_list and in
188 the same order. NAME is the name of the frame. FONT-LIST-CACHE
189 records previous values returned by x-list-fonts. */
191 Lisp_Object x_display_name_list;
193 /* Frame being updated by update_frame. This is declared in term.c.
194 This is set by update_begin and looked at by all the XT functions.
195 It is zero while not inside an update. In that case, the XT
196 functions assume that `selected_frame' is the frame to apply to. */
198 extern struct frame *updating_frame;
200 /* This is a frame waiting to be auto-raised, within XTread_socket. */
202 struct frame *pending_autoraise_frame;
204 #ifdef USE_X_TOOLKIT
205 /* The application context for Xt use. */
206 XtAppContext Xt_app_con;
207 static String Xt_default_resources[] = {0};
208 #endif /* USE_X_TOOLKIT */
210 /* Non-zero means user is interacting with a toolkit scroll bar. */
212 static int toolkit_scroll_bar_interaction;
214 /* Mouse movement.
216 Formerly, we used PointerMotionHintMask (in standard_event_mask)
217 so that we would have to call XQueryPointer after each MotionNotify
218 event to ask for another such event. However, this made mouse tracking
219 slow, and there was a bug that made it eventually stop.
221 Simply asking for MotionNotify all the time seems to work better.
223 In order to avoid asking for motion events and then throwing most
224 of them away or busy-polling the server for mouse positions, we ask
225 the server for pointer motion hints. This means that we get only
226 one event per group of mouse movements. "Groups" are delimited by
227 other kinds of events (focus changes and button clicks, for
228 example), or by XQueryPointer calls; when one of these happens, we
229 get another MotionNotify event the next time the mouse moves. This
230 is at least as efficient as getting motion events when mouse
231 tracking is on, and I suspect only negligibly worse when tracking
232 is off. */
234 /* Where the mouse was last time we reported a mouse event. */
236 static XRectangle last_mouse_glyph;
237 static Lisp_Object last_mouse_press_frame;
239 /* The scroll bar in which the last X motion event occurred.
241 If the last X motion event occurred in a scroll bar, we set this so
242 XTmouse_position can know whether to report a scroll bar motion or
243 an ordinary motion.
245 If the last X motion event didn't occur in a scroll bar, we set
246 this to Qnil, to tell XTmouse_position to return an ordinary motion
247 event. */
249 static Lisp_Object last_mouse_scroll_bar;
251 /* This is a hack. We would really prefer that XTmouse_position would
252 return the time associated with the position it returns, but there
253 doesn't seem to be any way to wrest the time-stamp from the server
254 along with the position query. So, we just keep track of the time
255 of the last movement we received, and return that in hopes that
256 it's somewhat accurate. */
258 static Time last_mouse_movement_time;
260 /* Incremented by XTread_socket whenever it really tries to read
261 events. */
263 #ifdef __STDC__
264 static int volatile input_signal_count;
265 #else
266 static int input_signal_count;
267 #endif
269 /* Used locally within XTread_socket. */
271 static int x_noop_count;
273 /* Initial values of argv and argc. */
275 extern char **initial_argv;
276 extern int initial_argc;
278 extern Lisp_Object Vcommand_line_args, Vsystem_name;
280 /* Tells if a window manager is present or not. */
282 extern Lisp_Object Vx_no_window_manager;
284 extern Lisp_Object Qface, Qmouse_face, Qeql;
286 extern int errno;
288 /* A mask of extra modifier bits to put into every keyboard char. */
290 extern EMACS_INT extra_keyboard_modifiers;
292 /* The keysyms to use for the various modifiers. */
294 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
295 Lisp_Object Vx_keysym_table;
296 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
298 static Lisp_Object Qvendor_specific_keysyms;
299 static Lisp_Object Qlatin_1, Qutf_8;
301 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
304 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
305 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
306 static const XColor *x_color_cells P_ ((Display *, int *));
307 static void x_update_window_end P_ ((struct window *, int, int));
308 void x_delete_display P_ ((struct x_display_info *));
309 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
310 unsigned));
311 static int x_io_error_quitter P_ ((Display *));
312 int x_catch_errors P_ ((Display *));
313 void x_uncatch_errors P_ ((Display *, int));
314 void x_lower_frame P_ ((struct frame *));
315 void x_scroll_bar_clear P_ ((struct frame *));
316 int x_had_errors_p P_ ((Display *));
317 void x_wm_set_size_hint P_ ((struct frame *, long, int));
318 void x_raise_frame P_ ((struct frame *));
319 void x_set_window_size P_ ((struct frame *, int, int, int));
320 void x_wm_set_window_state P_ ((struct frame *, int));
321 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
322 void x_initialize P_ ((void));
323 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
324 static int x_compute_min_glyph_bounds P_ ((struct frame *));
325 static void x_update_end P_ ((struct frame *));
326 static void XTframe_up_to_date P_ ((struct frame *));
327 static void XTset_terminal_modes P_ ((void));
328 static void XTreset_terminal_modes P_ ((void));
329 static void x_clear_frame P_ ((void));
330 static void frame_highlight P_ ((struct frame *));
331 static void frame_unhighlight P_ ((struct frame *));
332 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
333 static int x_focus_changed P_ ((int,
334 int,
335 struct x_display_info *,
336 struct frame *,
337 struct input_event *,
338 int));
339 static int x_detect_focus_change P_ ((struct x_display_info *,
340 XEvent *,
341 struct input_event *,
342 int));
343 static void XTframe_rehighlight P_ ((struct frame *));
344 static void x_frame_rehighlight P_ ((struct x_display_info *));
345 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
346 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
347 enum text_cursor_kinds));
349 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
350 GC, int));
351 static void x_flush P_ ((struct frame *f));
352 static void x_update_begin P_ ((struct frame *));
353 static void x_update_window_begin P_ ((struct window *));
354 static void x_after_update_window_line P_ ((struct glyph_row *));
355 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
356 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
357 enum scroll_bar_part *,
358 Lisp_Object *, Lisp_Object *,
359 unsigned long *));
360 static void x_check_fullscreen P_ ((struct frame *));
361 static void x_check_fullscreen_move P_ ((struct frame *));
362 static int handle_one_xevent P_ ((struct x_display_info *,
363 XEvent *,
364 struct input_event **,
365 int *,
366 int *));
369 /* Flush display of frame F, or of all frames if F is null. */
371 static void
372 x_flush (f)
373 struct frame *f;
375 BLOCK_INPUT;
376 if (f == NULL)
378 Lisp_Object rest, frame;
379 FOR_EACH_FRAME (rest, frame)
380 x_flush (XFRAME (frame));
382 else if (FRAME_X_P (f))
383 XFlush (FRAME_X_DISPLAY (f));
384 UNBLOCK_INPUT;
388 /* Remove calls to XFlush by defining XFlush to an empty replacement.
389 Calls to XFlush should be unnecessary because the X output buffer
390 is flushed automatically as needed by calls to XPending,
391 XNextEvent, or XWindowEvent according to the XFlush man page.
392 XTread_socket calls XPending. Removing XFlush improves
393 performance. */
395 #define XFlush(DISPLAY) (void) 0
398 /***********************************************************************
399 Debugging
400 ***********************************************************************/
402 #if 0
404 /* This is a function useful for recording debugging information about
405 the sequence of occurrences in this file. */
407 struct record
409 char *locus;
410 int type;
413 struct record event_record[100];
415 int event_record_index;
417 record_event (locus, type)
418 char *locus;
419 int type;
421 if (event_record_index == sizeof (event_record) / sizeof (struct record))
422 event_record_index = 0;
424 event_record[event_record_index].locus = locus;
425 event_record[event_record_index].type = type;
426 event_record_index++;
429 #endif /* 0 */
433 /* Return the struct x_display_info corresponding to DPY. */
435 struct x_display_info *
436 x_display_info_for_display (dpy)
437 Display *dpy;
439 struct x_display_info *dpyinfo;
441 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
442 if (dpyinfo->display == dpy)
443 return dpyinfo;
445 return 0;
450 /***********************************************************************
451 Starting and ending an update
452 ***********************************************************************/
454 /* Start an update of frame F. This function is installed as a hook
455 for update_begin, i.e. it is called when update_begin is called.
456 This function is called prior to calls to x_update_window_begin for
457 each window being updated. Currently, there is nothing to do here
458 because all interesting stuff is done on a window basis. */
460 static void
461 x_update_begin (f)
462 struct frame *f;
464 /* Nothing to do. */
468 /* Start update of window W. Set the global variable updated_window
469 to the window being updated and set output_cursor to the cursor
470 position of W. */
472 static void
473 x_update_window_begin (w)
474 struct window *w;
476 struct frame *f = XFRAME (WINDOW_FRAME (w));
477 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
479 updated_window = w;
480 set_output_cursor (&w->cursor);
482 BLOCK_INPUT;
484 if (f == display_info->mouse_face_mouse_frame)
486 /* Don't do highlighting for mouse motion during the update. */
487 display_info->mouse_face_defer = 1;
489 /* If F needs to be redrawn, simply forget about any prior mouse
490 highlighting. */
491 if (FRAME_GARBAGED_P (f))
492 display_info->mouse_face_window = Qnil;
494 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
495 their mouse_face_p flag set, which means that they are always
496 unequal to rows in a desired matrix which never have that
497 flag set. So, rows containing mouse-face glyphs are never
498 scrolled, and we don't have to switch the mouse highlight off
499 here to prevent it from being scrolled. */
501 /* Can we tell that this update does not affect the window
502 where the mouse highlight is? If so, no need to turn off.
503 Likewise, don't do anything if the frame is garbaged;
504 in that case, the frame's current matrix that we would use
505 is all wrong, and we will redisplay that line anyway. */
506 if (!NILP (display_info->mouse_face_window)
507 && w == XWINDOW (display_info->mouse_face_window))
509 int i;
511 for (i = 0; i < w->desired_matrix->nrows; ++i)
512 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
513 break;
515 if (i < w->desired_matrix->nrows)
516 clear_mouse_face (display_info);
518 #endif /* 0 */
521 UNBLOCK_INPUT;
525 /* Draw a vertical window border from (x,y0) to (x,y1) */
527 static void
528 x_draw_vertical_window_border (w, x, y0, y1)
529 struct window *w;
530 int x, y0, y1;
532 struct frame *f = XFRAME (WINDOW_FRAME (w));
534 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
535 f->output_data.x->normal_gc, x, y0, x, y1);
538 /* End update of window W (which is equal to updated_window).
540 Draw vertical borders between horizontally adjacent windows, and
541 display W's cursor if CURSOR_ON_P is non-zero.
543 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
544 glyphs in mouse-face were overwritten. In that case we have to
545 make sure that the mouse-highlight is properly redrawn.
547 W may be a menu bar pseudo-window in case we don't have X toolkit
548 support. Such windows don't have a cursor, so don't display it
549 here. */
551 static void
552 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
553 struct window *w;
554 int cursor_on_p, mouse_face_overwritten_p;
556 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
558 if (!w->pseudo_window_p)
560 BLOCK_INPUT;
562 if (cursor_on_p)
563 display_and_set_cursor (w, 1, output_cursor.hpos,
564 output_cursor.vpos,
565 output_cursor.x, output_cursor.y);
567 x_draw_vertical_border (w);
568 UNBLOCK_INPUT;
571 /* If a row with mouse-face was overwritten, arrange for
572 XTframe_up_to_date to redisplay the mouse highlight. */
573 if (mouse_face_overwritten_p)
575 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
576 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
577 dpyinfo->mouse_face_window = Qnil;
580 updated_window = NULL;
584 /* End update of frame F. This function is installed as a hook in
585 update_end. */
587 static void
588 x_update_end (f)
589 struct frame *f;
591 /* Mouse highlight may be displayed again. */
592 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
594 #ifndef XFlush
595 BLOCK_INPUT;
596 XFlush (FRAME_X_DISPLAY (f));
597 UNBLOCK_INPUT;
598 #endif
602 /* This function is called from various places in xdisp.c whenever a
603 complete update has been performed. The global variable
604 updated_window is not available here. */
606 static void
607 XTframe_up_to_date (f)
608 struct frame *f;
610 if (FRAME_X_P (f))
612 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
614 if (dpyinfo->mouse_face_deferred_gc
615 || f == dpyinfo->mouse_face_mouse_frame)
617 BLOCK_INPUT;
618 if (dpyinfo->mouse_face_mouse_frame)
619 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
620 dpyinfo->mouse_face_mouse_x,
621 dpyinfo->mouse_face_mouse_y);
622 dpyinfo->mouse_face_deferred_gc = 0;
623 UNBLOCK_INPUT;
629 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
630 arrow bitmaps, or clear the fringes if no bitmaps are required
631 before DESIRED_ROW is made current. The window being updated is
632 found in updated_window. This function It is called from
633 update_window_line only if it is known that there are differences
634 between bitmaps to be drawn between current row and DESIRED_ROW. */
636 static void
637 x_after_update_window_line (desired_row)
638 struct glyph_row *desired_row;
640 struct window *w = updated_window;
641 struct frame *f;
642 int width, height;
644 xassert (w);
646 if (!desired_row->mode_line_p && !w->pseudo_window_p)
648 BLOCK_INPUT;
649 draw_row_fringe_bitmaps (w, desired_row);
650 UNBLOCK_INPUT;
653 /* When a window has disappeared, make sure that no rest of
654 full-width rows stays visible in the internal border. Could
655 check here if updated_window is the leftmost/rightmost window,
656 but I guess it's not worth doing since vertically split windows
657 are almost never used, internal border is rarely set, and the
658 overhead is very small. */
659 if (windows_or_buffers_changed
660 && desired_row->full_width_p
661 && (f = XFRAME (w->frame),
662 width = FRAME_INTERNAL_BORDER_WIDTH (f),
663 width != 0)
664 && (height = desired_row->visible_height,
665 height > 0))
667 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
669 /* Internal border is drawn below the tool bar. */
670 if (WINDOWP (f->tool_bar_window)
671 && w == XWINDOW (f->tool_bar_window))
672 y -= width;
674 BLOCK_INPUT;
675 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
676 0, y, width, height, False);
677 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
678 f->output_data.x->pixel_width - width,
679 y, width, height, False);
680 UNBLOCK_INPUT;
684 static void
685 x_draw_fringe_bitmap (w, row, p)
686 struct window *w;
687 struct glyph_row *row;
688 struct draw_fringe_bitmap_params *p;
690 struct frame *f = XFRAME (WINDOW_FRAME (w));
691 Display *display = FRAME_X_DISPLAY (f);
692 Window window = FRAME_X_WINDOW (f);
693 GC gc = f->output_data.x->normal_gc;
694 struct face *face = p->face;
696 /* Must clip because of partially visible lines. */
697 x_clip_to_row (w, row, gc, 1);
699 if (p->bx >= 0)
701 /* In case the same realized face is used for fringes and
702 for something displayed in the text (e.g. face `region' on
703 mono-displays, the fill style may have been changed to
704 FillSolid in x_draw_glyph_string_background. */
705 if (face->stipple)
706 XSetFillStyle (display, face->gc, FillOpaqueStippled);
707 else
708 XSetForeground (display, face->gc, face->background);
710 XFillRectangle (display, window, face->gc,
711 p->bx, p->by, p->nx, p->ny);
713 if (!face->stipple)
714 XSetForeground (display, face->gc, face->foreground);
717 if (p->which != NO_FRINGE_BITMAP)
719 unsigned char *bits = fringe_bitmaps[p->which].bits + p->dh;
720 Pixmap pixmap;
721 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
723 /* Draw the bitmap. I believe these small pixmaps can be cached
724 by the server. */
725 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
726 face->foreground,
727 face->background, depth);
728 XCopyArea (display, pixmap, window, gc, 0, 0,
729 p->wd, p->h, p->x, p->y);
730 XFreePixmap (display, pixmap);
733 XSetClipMask (display, gc, None);
738 /* This is called when starting Emacs and when restarting after
739 suspend. When starting Emacs, no X window is mapped. And nothing
740 must be done to Emacs's own window if it is suspended (though that
741 rarely happens). */
743 static void
744 XTset_terminal_modes ()
748 /* This is called when exiting or suspending Emacs. Exiting will make
749 the X-windows go away, and suspending requires no action. */
751 static void
752 XTreset_terminal_modes ()
758 /***********************************************************************
759 Display Iterator
760 ***********************************************************************/
762 /* Function prototypes of this page. */
764 static int x_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
767 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
768 is not contained in the font. */
770 static XCharStruct *
771 x_per_char_metric (font, char2b, font_type)
772 XFontStruct *font;
773 XChar2b *char2b;
774 int font_type; /* unused on X */
776 /* The result metric information. */
777 XCharStruct *pcm = NULL;
779 xassert (font && char2b);
781 if (font->per_char != NULL)
783 if (font->min_byte1 == 0 && font->max_byte1 == 0)
785 /* min_char_or_byte2 specifies the linear character index
786 corresponding to the first element of the per_char array,
787 max_char_or_byte2 is the index of the last character. A
788 character with non-zero CHAR2B->byte1 is not in the font.
789 A character with byte2 less than min_char_or_byte2 or
790 greater max_char_or_byte2 is not in the font. */
791 if (char2b->byte1 == 0
792 && char2b->byte2 >= font->min_char_or_byte2
793 && char2b->byte2 <= font->max_char_or_byte2)
794 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
796 else
798 /* If either min_byte1 or max_byte1 are nonzero, both
799 min_char_or_byte2 and max_char_or_byte2 are less than
800 256, and the 2-byte character index values corresponding
801 to the per_char array element N (counting from 0) are:
803 byte1 = N/D + min_byte1
804 byte2 = N\D + min_char_or_byte2
806 where:
808 D = max_char_or_byte2 - min_char_or_byte2 + 1
809 / = integer division
810 \ = integer modulus */
811 if (char2b->byte1 >= font->min_byte1
812 && char2b->byte1 <= font->max_byte1
813 && char2b->byte2 >= font->min_char_or_byte2
814 && char2b->byte2 <= font->max_char_or_byte2)
816 pcm = (font->per_char
817 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
818 * (char2b->byte1 - font->min_byte1))
819 + (char2b->byte2 - font->min_char_or_byte2));
823 else
825 /* If the per_char pointer is null, all glyphs between the first
826 and last character indexes inclusive have the same
827 information, as given by both min_bounds and max_bounds. */
828 if (char2b->byte2 >= font->min_char_or_byte2
829 && char2b->byte2 <= font->max_char_or_byte2)
830 pcm = &font->max_bounds;
833 return ((pcm == NULL
834 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
835 ? NULL : pcm);
839 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
840 the two-byte form of C. Encoding is returned in *CHAR2B. */
842 static int
843 x_encode_char (c, char2b, font_info, two_byte_p)
844 int c;
845 XChar2b *char2b;
846 struct font_info *font_info;
847 int *two_byte_p;
849 int charset = CHAR_CHARSET (c);
850 XFontStruct *font = font_info->font;
852 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
853 This may be either a program in a special encoder language or a
854 fixed encoding. */
855 if (font_info->font_encoder)
857 /* It's a program. */
858 struct ccl_program *ccl = font_info->font_encoder;
860 if (CHARSET_DIMENSION (charset) == 1)
862 ccl->reg[0] = charset;
863 ccl->reg[1] = char2b->byte2;
864 ccl->reg[2] = -1;
866 else
868 ccl->reg[0] = charset;
869 ccl->reg[1] = char2b->byte1;
870 ccl->reg[2] = char2b->byte2;
873 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
875 /* We assume that MSBs are appropriately set/reset by CCL
876 program. */
877 if (font->max_byte1 == 0) /* 1-byte font */
878 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
879 else
880 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
882 else if (font_info->encoding[charset])
884 /* Fixed encoding scheme. See fontset.h for the meaning of the
885 encoding numbers. */
886 int enc = font_info->encoding[charset];
888 if ((enc == 1 || enc == 2)
889 && CHARSET_DIMENSION (charset) == 2)
890 char2b->byte1 |= 0x80;
892 if (enc == 1 || enc == 3)
893 char2b->byte2 |= 0x80;
896 if (two_byte_p)
897 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
899 return FONT_TYPE_UNKNOWN;
904 /***********************************************************************
905 Glyph display
906 ***********************************************************************/
910 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
911 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
912 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
913 int));
914 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
915 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
916 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
917 static void x_draw_glyph_string P_ ((struct glyph_string *));
918 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
919 static void x_set_cursor_gc P_ ((struct glyph_string *));
920 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
921 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
922 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
923 unsigned long *, double, int));
924 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
925 double, int, unsigned long));
926 static void x_setup_relief_colors P_ ((struct glyph_string *));
927 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
928 static void x_draw_image_relief P_ ((struct glyph_string *));
929 static void x_draw_image_foreground P_ ((struct glyph_string *));
930 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
931 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
932 int, int, int));
933 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
934 int, int, int, int, XRectangle *));
935 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
936 int, int, int, XRectangle *));
938 #if GLYPH_DEBUG
939 static void x_check_font P_ ((struct frame *, XFontStruct *));
940 #endif
943 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
944 face. */
946 static void
947 x_set_cursor_gc (s)
948 struct glyph_string *s;
950 if (s->font == FRAME_FONT (s->f)
951 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
952 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
953 && !s->cmp)
954 s->gc = s->f->output_data.x->cursor_gc;
955 else
957 /* Cursor on non-default face: must merge. */
958 XGCValues xgcv;
959 unsigned long mask;
961 xgcv.background = s->f->output_data.x->cursor_pixel;
962 xgcv.foreground = s->face->background;
964 /* If the glyph would be invisible, try a different foreground. */
965 if (xgcv.foreground == xgcv.background)
966 xgcv.foreground = s->face->foreground;
967 if (xgcv.foreground == xgcv.background)
968 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
969 if (xgcv.foreground == xgcv.background)
970 xgcv.foreground = s->face->foreground;
972 /* Make sure the cursor is distinct from text in this face. */
973 if (xgcv.background == s->face->background
974 && xgcv.foreground == s->face->foreground)
976 xgcv.background = s->face->foreground;
977 xgcv.foreground = s->face->background;
980 IF_DEBUG (x_check_font (s->f, s->font));
981 xgcv.font = s->font->fid;
982 xgcv.graphics_exposures = False;
983 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
985 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
986 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
987 mask, &xgcv);
988 else
989 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
990 = XCreateGC (s->display, s->window, mask, &xgcv);
992 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
997 /* Set up S->gc of glyph string S for drawing text in mouse face. */
999 static void
1000 x_set_mouse_face_gc (s)
1001 struct glyph_string *s;
1003 int face_id;
1004 struct face *face;
1006 /* What face has to be used last for the mouse face? */
1007 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1008 face = FACE_FROM_ID (s->f, face_id);
1009 if (face == NULL)
1010 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1012 if (s->first_glyph->type == CHAR_GLYPH)
1013 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
1014 else
1015 face_id = FACE_FOR_CHAR (s->f, face, 0);
1016 s->face = FACE_FROM_ID (s->f, face_id);
1017 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1019 /* If font in this face is same as S->font, use it. */
1020 if (s->font == s->face->font)
1021 s->gc = s->face->gc;
1022 else
1024 /* Otherwise construct scratch_cursor_gc with values from FACE
1025 but font FONT. */
1026 XGCValues xgcv;
1027 unsigned long mask;
1029 xgcv.background = s->face->background;
1030 xgcv.foreground = s->face->foreground;
1031 IF_DEBUG (x_check_font (s->f, s->font));
1032 xgcv.font = s->font->fid;
1033 xgcv.graphics_exposures = False;
1034 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1036 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1037 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1038 mask, &xgcv);
1039 else
1040 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1041 = XCreateGC (s->display, s->window, mask, &xgcv);
1043 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1046 xassert (s->gc != 0);
1050 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1051 Faces to use in the mode line have already been computed when the
1052 matrix was built, so there isn't much to do, here. */
1054 static INLINE void
1055 x_set_mode_line_face_gc (s)
1056 struct glyph_string *s;
1058 s->gc = s->face->gc;
1062 /* Set S->gc of glyph string S for drawing that glyph string. Set
1063 S->stippled_p to a non-zero value if the face of S has a stipple
1064 pattern. */
1066 static INLINE void
1067 x_set_glyph_string_gc (s)
1068 struct glyph_string *s;
1070 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1072 if (s->hl == DRAW_NORMAL_TEXT)
1074 s->gc = s->face->gc;
1075 s->stippled_p = s->face->stipple != 0;
1077 else if (s->hl == DRAW_INVERSE_VIDEO)
1079 x_set_mode_line_face_gc (s);
1080 s->stippled_p = s->face->stipple != 0;
1082 else if (s->hl == DRAW_CURSOR)
1084 x_set_cursor_gc (s);
1085 s->stippled_p = 0;
1087 else if (s->hl == DRAW_MOUSE_FACE)
1089 x_set_mouse_face_gc (s);
1090 s->stippled_p = s->face->stipple != 0;
1092 else if (s->hl == DRAW_IMAGE_RAISED
1093 || s->hl == DRAW_IMAGE_SUNKEN)
1095 s->gc = s->face->gc;
1096 s->stippled_p = s->face->stipple != 0;
1098 else
1100 s->gc = s->face->gc;
1101 s->stippled_p = s->face->stipple != 0;
1104 /* GC must have been set. */
1105 xassert (s->gc != 0);
1109 /* Set clipping for output of glyph string S. S may be part of a mode
1110 line or menu if we don't have X toolkit support. */
1112 static INLINE void
1113 x_set_glyph_string_clipping (s)
1114 struct glyph_string *s;
1116 XRectangle r;
1117 get_glyph_string_clip_rect (s, &r);
1118 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
1122 /* RIF:
1123 Compute left and right overhang of glyph string S. If S is a glyph
1124 string for a composition, assume overhangs don't exist. */
1126 static void
1127 x_compute_glyph_string_overhangs (s)
1128 struct glyph_string *s;
1130 if (s->cmp == NULL
1131 && s->first_glyph->type == CHAR_GLYPH)
1133 XCharStruct cs;
1134 int direction, font_ascent, font_descent;
1135 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1136 &font_ascent, &font_descent, &cs);
1137 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1138 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1143 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1145 static INLINE void
1146 x_clear_glyph_string_rect (s, x, y, w, h)
1147 struct glyph_string *s;
1148 int x, y, w, h;
1150 XGCValues xgcv;
1151 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1152 XSetForeground (s->display, s->gc, xgcv.background);
1153 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1154 XSetForeground (s->display, s->gc, xgcv.foreground);
1158 /* Draw the background of glyph_string S. If S->background_filled_p
1159 is non-zero don't draw it. FORCE_P non-zero means draw the
1160 background even if it wouldn't be drawn normally. This is used
1161 when a string preceding S draws into the background of S, or S
1162 contains the first component of a composition. */
1164 static void
1165 x_draw_glyph_string_background (s, force_p)
1166 struct glyph_string *s;
1167 int force_p;
1169 /* Nothing to do if background has already been drawn or if it
1170 shouldn't be drawn in the first place. */
1171 if (!s->background_filled_p)
1173 int box_line_width = max (s->face->box_line_width, 0);
1175 if (s->stippled_p)
1177 /* Fill background with a stipple pattern. */
1178 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1179 XFillRectangle (s->display, s->window, s->gc, s->x,
1180 s->y + box_line_width,
1181 s->background_width,
1182 s->height - 2 * box_line_width);
1183 XSetFillStyle (s->display, s->gc, FillSolid);
1184 s->background_filled_p = 1;
1186 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1187 || s->font_not_found_p
1188 || s->extends_to_end_of_line_p
1189 || force_p)
1191 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1192 s->background_width,
1193 s->height - 2 * box_line_width);
1194 s->background_filled_p = 1;
1200 /* Draw the foreground of glyph string S. */
1202 static void
1203 x_draw_glyph_string_foreground (s)
1204 struct glyph_string *s;
1206 int i, x;
1208 /* If first glyph of S has a left box line, start drawing the text
1209 of S to the right of that box line. */
1210 if (s->face->box != FACE_NO_BOX
1211 && s->first_glyph->left_box_line_p)
1212 x = s->x + abs (s->face->box_line_width);
1213 else
1214 x = s->x;
1216 /* Draw characters of S as rectangles if S's font could not be
1217 loaded. */
1218 if (s->font_not_found_p)
1220 for (i = 0; i < s->nchars; ++i)
1222 struct glyph *g = s->first_glyph + i;
1223 XDrawRectangle (s->display, s->window,
1224 s->gc, x, s->y, g->pixel_width - 1,
1225 s->height - 1);
1226 x += g->pixel_width;
1229 else
1231 char *char1b = (char *) s->char2b;
1232 int boff = s->font_info->baseline_offset;
1234 if (s->font_info->vertical_centering)
1235 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1237 /* If we can use 8-bit functions, condense S->char2b. */
1238 if (!s->two_byte_p)
1239 for (i = 0; i < s->nchars; ++i)
1240 char1b[i] = s->char2b[i].byte2;
1242 /* Draw text with XDrawString if background has already been
1243 filled. Otherwise, use XDrawImageString. (Note that
1244 XDrawImageString is usually faster than XDrawString.) Always
1245 use XDrawImageString when drawing the cursor so that there is
1246 no chance that characters under a box cursor are invisible. */
1247 if (s->for_overlaps_p
1248 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1250 /* Draw characters with 16-bit or 8-bit functions. */
1251 if (s->two_byte_p)
1252 XDrawString16 (s->display, s->window, s->gc, x,
1253 s->ybase - boff, s->char2b, s->nchars);
1254 else
1255 XDrawString (s->display, s->window, s->gc, x,
1256 s->ybase - boff, char1b, s->nchars);
1258 else
1260 if (s->two_byte_p)
1261 XDrawImageString16 (s->display, s->window, s->gc, x,
1262 s->ybase - boff, s->char2b, s->nchars);
1263 else
1264 XDrawImageString (s->display, s->window, s->gc, x,
1265 s->ybase - boff, char1b, s->nchars);
1268 if (s->face->overstrike)
1270 /* For overstriking (to simulate bold-face), draw the
1271 characters again shifted to the right by one pixel. */
1272 if (s->two_byte_p)
1273 XDrawString16 (s->display, s->window, s->gc, x + 1,
1274 s->ybase - boff, s->char2b, s->nchars);
1275 else
1276 XDrawString (s->display, s->window, s->gc, x + 1,
1277 s->ybase - boff, char1b, s->nchars);
1282 /* Draw the foreground of composite glyph string S. */
1284 static void
1285 x_draw_composite_glyph_string_foreground (s)
1286 struct glyph_string *s;
1288 int i, x;
1290 /* If first glyph of S has a left box line, start drawing the text
1291 of S to the right of that box line. */
1292 if (s->face->box != FACE_NO_BOX
1293 && s->first_glyph->left_box_line_p)
1294 x = s->x + abs (s->face->box_line_width);
1295 else
1296 x = s->x;
1298 /* S is a glyph string for a composition. S->gidx is the index of
1299 the first character drawn for glyphs of this composition.
1300 S->gidx == 0 means we are drawing the very first character of
1301 this composition. */
1303 /* Draw a rectangle for the composition if the font for the very
1304 first character of the composition could not be loaded. */
1305 if (s->font_not_found_p)
1307 if (s->gidx == 0)
1308 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1309 s->width - 1, s->height - 1);
1311 else
1313 for (i = 0; i < s->nchars; i++, ++s->gidx)
1315 XDrawString16 (s->display, s->window, s->gc,
1316 x + s->cmp->offsets[s->gidx * 2],
1317 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1318 s->char2b + i, 1);
1319 if (s->face->overstrike)
1320 XDrawString16 (s->display, s->window, s->gc,
1321 x + s->cmp->offsets[s->gidx * 2] + 1,
1322 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1323 s->char2b + i, 1);
1329 #ifdef USE_X_TOOLKIT
1331 static struct frame *x_frame_of_widget P_ ((Widget));
1332 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1333 XrmValue *, XrmValue *, XtPointer *));
1334 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1335 XrmValue *, Cardinal *));
1338 /* Return the frame on which widget WIDGET is used.. Abort if frame
1339 cannot be determined. */
1341 static struct frame *
1342 x_frame_of_widget (widget)
1343 Widget widget;
1345 struct x_display_info *dpyinfo;
1346 Lisp_Object tail;
1347 struct frame *f;
1349 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1351 /* Find the top-level shell of the widget. Note that this function
1352 can be called when the widget is not yet realized, so XtWindow
1353 (widget) == 0. That's the reason we can't simply use
1354 x_any_window_to_frame. */
1355 while (!XtIsTopLevelShell (widget))
1356 widget = XtParent (widget);
1358 /* Look for a frame with that top-level widget. Allocate the color
1359 on that frame to get the right gamma correction value. */
1360 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1361 if (GC_FRAMEP (XCAR (tail))
1362 && (f = XFRAME (XCAR (tail)),
1363 (f->output_data.nothing != 1
1364 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1365 && f->output_data.x->widget == widget)
1366 return f;
1368 abort ();
1372 /* Allocate the color COLOR->pixel on the screen and display of
1373 widget WIDGET in colormap CMAP. If an exact match cannot be
1374 allocated, try the nearest color available. Value is non-zero
1375 if successful. This is called from lwlib. */
1378 x_alloc_nearest_color_for_widget (widget, cmap, color)
1379 Widget widget;
1380 Colormap cmap;
1381 XColor *color;
1383 struct frame *f = x_frame_of_widget (widget);
1384 return x_alloc_nearest_color (f, cmap, color);
1388 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1389 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1390 If this produces the same color as PIXEL, try a color where all RGB
1391 values have DELTA added. Return the allocated color in *PIXEL.
1392 DISPLAY is the X display, CMAP is the colormap to operate on.
1393 Value is non-zero if successful. */
1396 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1397 Widget widget;
1398 Display *display;
1399 Colormap cmap;
1400 unsigned long *pixel;
1401 double factor;
1402 int delta;
1404 struct frame *f = x_frame_of_widget (widget);
1405 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1409 /* Structure specifying which arguments should be passed by Xt to
1410 cvt_string_to_pixel. We want the widget's screen and colormap. */
1412 static XtConvertArgRec cvt_string_to_pixel_args[] =
1414 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1415 sizeof (Screen *)},
1416 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1417 sizeof (Colormap)}
1421 /* The address of this variable is returned by
1422 cvt_string_to_pixel. */
1424 static Pixel cvt_string_to_pixel_value;
1427 /* Convert a color name to a pixel color.
1429 DPY is the display we are working on.
1431 ARGS is an array of *NARGS XrmValue structures holding additional
1432 information about the widget for which the conversion takes place.
1433 The contents of this array are determined by the specification
1434 in cvt_string_to_pixel_args.
1436 FROM is a pointer to an XrmValue which points to the color name to
1437 convert. TO is an XrmValue in which to return the pixel color.
1439 CLOSURE_RET is a pointer to user-data, in which we record if
1440 we allocated the color or not.
1442 Value is True if successful, False otherwise. */
1444 static Boolean
1445 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1446 Display *dpy;
1447 XrmValue *args;
1448 Cardinal *nargs;
1449 XrmValue *from, *to;
1450 XtPointer *closure_ret;
1452 Screen *screen;
1453 Colormap cmap;
1454 Pixel pixel;
1455 String color_name;
1456 XColor color;
1458 if (*nargs != 2)
1460 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1461 "wrongParameters", "cvt_string_to_pixel",
1462 "XtToolkitError",
1463 "Screen and colormap args required", NULL, NULL);
1464 return False;
1467 screen = *(Screen **) args[0].addr;
1468 cmap = *(Colormap *) args[1].addr;
1469 color_name = (String) from->addr;
1471 if (strcmp (color_name, XtDefaultBackground) == 0)
1473 *closure_ret = (XtPointer) False;
1474 pixel = WhitePixelOfScreen (screen);
1476 else if (strcmp (color_name, XtDefaultForeground) == 0)
1478 *closure_ret = (XtPointer) False;
1479 pixel = BlackPixelOfScreen (screen);
1481 else if (XParseColor (dpy, cmap, color_name, &color)
1482 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1484 pixel = color.pixel;
1485 *closure_ret = (XtPointer) True;
1487 else
1489 String params[1];
1490 Cardinal nparams = 1;
1492 params[0] = color_name;
1493 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1494 "badValue", "cvt_string_to_pixel",
1495 "XtToolkitError", "Invalid color `%s'",
1496 params, &nparams);
1497 return False;
1500 if (to->addr != NULL)
1502 if (to->size < sizeof (Pixel))
1504 to->size = sizeof (Pixel);
1505 return False;
1508 *(Pixel *) to->addr = pixel;
1510 else
1512 cvt_string_to_pixel_value = pixel;
1513 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1516 to->size = sizeof (Pixel);
1517 return True;
1521 /* Free a pixel color which was previously allocated via
1522 cvt_string_to_pixel. This is registered as the destructor
1523 for this type of resource via XtSetTypeConverter.
1525 APP is the application context in which we work.
1527 TO is a pointer to an XrmValue holding the color to free.
1528 CLOSURE is the value we stored in CLOSURE_RET for this color
1529 in cvt_string_to_pixel.
1531 ARGS and NARGS are like for cvt_string_to_pixel. */
1533 static void
1534 cvt_pixel_dtor (app, to, closure, args, nargs)
1535 XtAppContext app;
1536 XrmValuePtr to;
1537 XtPointer closure;
1538 XrmValuePtr args;
1539 Cardinal *nargs;
1541 if (*nargs != 2)
1543 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1544 "XtToolkitError",
1545 "Screen and colormap arguments required",
1546 NULL, NULL);
1548 else if (closure != NULL)
1550 /* We did allocate the pixel, so free it. */
1551 Screen *screen = *(Screen **) args[0].addr;
1552 Colormap cmap = *(Colormap *) args[1].addr;
1553 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1554 (Pixel *) to->addr, 1);
1559 #endif /* USE_X_TOOLKIT */
1562 /* Value is an array of XColor structures for the contents of the
1563 color map of display DPY. Set *NCELLS to the size of the array.
1564 Note that this probably shouldn't be called for large color maps,
1565 say a 24-bit TrueColor map. */
1567 static const XColor *
1568 x_color_cells (dpy, ncells)
1569 Display *dpy;
1570 int *ncells;
1572 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1574 if (dpyinfo->color_cells == NULL)
1576 Screen *screen = dpyinfo->screen;
1577 int i;
1579 dpyinfo->ncolor_cells
1580 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1581 dpyinfo->color_cells
1582 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1583 * sizeof *dpyinfo->color_cells);
1585 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1586 dpyinfo->color_cells[i].pixel = i;
1588 XQueryColors (dpy, dpyinfo->cmap,
1589 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1592 *ncells = dpyinfo->ncolor_cells;
1593 return dpyinfo->color_cells;
1597 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1598 colors in COLORS. Use cached information, if available. */
1600 void
1601 x_query_colors (f, colors, ncolors)
1602 struct frame *f;
1603 XColor *colors;
1604 int ncolors;
1606 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1608 if (dpyinfo->color_cells)
1610 int i;
1611 for (i = 0; i < ncolors; ++i)
1613 unsigned long pixel = colors[i].pixel;
1614 xassert (pixel < dpyinfo->ncolor_cells);
1615 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1616 colors[i] = dpyinfo->color_cells[pixel];
1619 else
1620 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1624 /* On frame F, translate pixel color to RGB values for the color in
1625 COLOR. Use cached information, if available. */
1627 void
1628 x_query_color (f, color)
1629 struct frame *f;
1630 XColor *color;
1632 x_query_colors (f, color, 1);
1636 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1637 exact match can't be allocated, try the nearest color available.
1638 Value is non-zero if successful. Set *COLOR to the color
1639 allocated. */
1641 static int
1642 x_alloc_nearest_color_1 (dpy, cmap, color)
1643 Display *dpy;
1644 Colormap cmap;
1645 XColor *color;
1647 int rc;
1649 rc = XAllocColor (dpy, cmap, color);
1650 if (rc == 0)
1652 /* If we got to this point, the colormap is full, so we're going
1653 to try to get the next closest color. The algorithm used is
1654 a least-squares matching, which is what X uses for closest
1655 color matching with StaticColor visuals. */
1656 int nearest, i;
1657 unsigned long nearest_delta = ~0;
1658 int ncells;
1659 const XColor *cells = x_color_cells (dpy, &ncells);
1661 for (nearest = i = 0; i < ncells; ++i)
1663 long dred = (color->red >> 8) - (cells[i].red >> 8);
1664 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1665 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1666 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1668 if (delta < nearest_delta)
1670 nearest = i;
1671 nearest_delta = delta;
1675 color->red = cells[nearest].red;
1676 color->green = cells[nearest].green;
1677 color->blue = cells[nearest].blue;
1678 rc = XAllocColor (dpy, cmap, color);
1680 else
1682 /* If allocation succeeded, and the allocated pixel color is not
1683 equal to a cached pixel color recorded earlier, there was a
1684 change in the colormap, so clear the color cache. */
1685 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1686 XColor *cached_color;
1688 if (dpyinfo->color_cells
1689 && (cached_color = &dpyinfo->color_cells[color->pixel],
1690 (cached_color->red != color->red
1691 || cached_color->blue != color->blue
1692 || cached_color->green != color->green)))
1694 xfree (dpyinfo->color_cells);
1695 dpyinfo->color_cells = NULL;
1696 dpyinfo->ncolor_cells = 0;
1700 #ifdef DEBUG_X_COLORS
1701 if (rc)
1702 register_color (color->pixel);
1703 #endif /* DEBUG_X_COLORS */
1705 return rc;
1709 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1710 exact match can't be allocated, try the nearest color available.
1711 Value is non-zero if successful. Set *COLOR to the color
1712 allocated. */
1715 x_alloc_nearest_color (f, cmap, color)
1716 struct frame *f;
1717 Colormap cmap;
1718 XColor *color;
1720 gamma_correct (f, color);
1721 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1725 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1726 It's necessary to do this instead of just using PIXEL directly to
1727 get color reference counts right. */
1729 unsigned long
1730 x_copy_color (f, pixel)
1731 struct frame *f;
1732 unsigned long pixel;
1734 XColor color;
1736 color.pixel = pixel;
1737 BLOCK_INPUT;
1738 x_query_color (f, &color);
1739 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1740 UNBLOCK_INPUT;
1741 #ifdef DEBUG_X_COLORS
1742 register_color (pixel);
1743 #endif
1744 return color.pixel;
1748 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1749 It's necessary to do this instead of just using PIXEL directly to
1750 get color reference counts right. */
1752 unsigned long
1753 x_copy_dpy_color (dpy, cmap, pixel)
1754 Display *dpy;
1755 Colormap cmap;
1756 unsigned long pixel;
1758 XColor color;
1760 color.pixel = pixel;
1761 BLOCK_INPUT;
1762 XQueryColor (dpy, cmap, &color);
1763 XAllocColor (dpy, cmap, &color);
1764 UNBLOCK_INPUT;
1765 #ifdef DEBUG_X_COLORS
1766 register_color (pixel);
1767 #endif
1768 return color.pixel;
1772 /* Brightness beyond which a color won't have its highlight brightness
1773 boosted.
1775 Nominally, highlight colors for `3d' faces are calculated by
1776 brightening an object's color by a constant scale factor, but this
1777 doesn't yield good results for dark colors, so for colors who's
1778 brightness is less than this value (on a scale of 0-65535) have an
1779 use an additional additive factor.
1781 The value here is set so that the default menu-bar/mode-line color
1782 (grey75) will not have its highlights changed at all. */
1783 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1786 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1787 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1788 If this produces the same color as PIXEL, try a color where all RGB
1789 values have DELTA added. Return the allocated color in *PIXEL.
1790 DISPLAY is the X display, CMAP is the colormap to operate on.
1791 Value is non-zero if successful. */
1793 static int
1794 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1795 struct frame *f;
1796 Display *display;
1797 Colormap cmap;
1798 unsigned long *pixel;
1799 double factor;
1800 int delta;
1802 XColor color, new;
1803 long bright;
1804 int success_p;
1806 /* Get RGB color values. */
1807 color.pixel = *pixel;
1808 x_query_color (f, &color);
1810 /* Change RGB values by specified FACTOR. Avoid overflow! */
1811 xassert (factor >= 0);
1812 new.red = min (0xffff, factor * color.red);
1813 new.green = min (0xffff, factor * color.green);
1814 new.blue = min (0xffff, factor * color.blue);
1816 /* Calculate brightness of COLOR. */
1817 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1819 /* We only boost colors that are darker than
1820 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1821 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1822 /* Make an additive adjustment to NEW, because it's dark enough so
1823 that scaling by FACTOR alone isn't enough. */
1825 /* How far below the limit this color is (0 - 1, 1 being darker). */
1826 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1827 /* The additive adjustment. */
1828 int min_delta = delta * dimness * factor / 2;
1830 if (factor < 1)
1832 new.red = max (0, new.red - min_delta);
1833 new.green = max (0, new.green - min_delta);
1834 new.blue = max (0, new.blue - min_delta);
1836 else
1838 new.red = min (0xffff, min_delta + new.red);
1839 new.green = min (0xffff, min_delta + new.green);
1840 new.blue = min (0xffff, min_delta + new.blue);
1844 /* Try to allocate the color. */
1845 success_p = x_alloc_nearest_color (f, cmap, &new);
1846 if (success_p)
1848 if (new.pixel == *pixel)
1850 /* If we end up with the same color as before, try adding
1851 delta to the RGB values. */
1852 x_free_colors (f, &new.pixel, 1);
1854 new.red = min (0xffff, delta + color.red);
1855 new.green = min (0xffff, delta + color.green);
1856 new.blue = min (0xffff, delta + color.blue);
1857 success_p = x_alloc_nearest_color (f, cmap, &new);
1859 else
1860 success_p = 1;
1861 *pixel = new.pixel;
1864 return success_p;
1868 /* Set up the foreground color for drawing relief lines of glyph
1869 string S. RELIEF is a pointer to a struct relief containing the GC
1870 with which lines will be drawn. Use a color that is FACTOR or
1871 DELTA lighter or darker than the relief's background which is found
1872 in S->f->output_data.x->relief_background. If such a color cannot
1873 be allocated, use DEFAULT_PIXEL, instead. */
1875 static void
1876 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1877 struct frame *f;
1878 struct relief *relief;
1879 double factor;
1880 int delta;
1881 unsigned long default_pixel;
1883 XGCValues xgcv;
1884 struct x_output *di = f->output_data.x;
1885 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1886 unsigned long pixel;
1887 unsigned long background = di->relief_background;
1888 Colormap cmap = FRAME_X_COLORMAP (f);
1889 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1890 Display *dpy = FRAME_X_DISPLAY (f);
1892 xgcv.graphics_exposures = False;
1893 xgcv.line_width = 1;
1895 /* Free previously allocated color. The color cell will be reused
1896 when it has been freed as many times as it was allocated, so this
1897 doesn't affect faces using the same colors. */
1898 if (relief->gc
1899 && relief->allocated_p)
1901 x_free_colors (f, &relief->pixel, 1);
1902 relief->allocated_p = 0;
1905 /* Allocate new color. */
1906 xgcv.foreground = default_pixel;
1907 pixel = background;
1908 if (dpyinfo->n_planes != 1
1909 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1911 relief->allocated_p = 1;
1912 xgcv.foreground = relief->pixel = pixel;
1915 if (relief->gc == 0)
1917 xgcv.stipple = dpyinfo->gray;
1918 mask |= GCStipple;
1919 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1921 else
1922 XChangeGC (dpy, relief->gc, mask, &xgcv);
1926 /* Set up colors for the relief lines around glyph string S. */
1928 static void
1929 x_setup_relief_colors (s)
1930 struct glyph_string *s;
1932 struct x_output *di = s->f->output_data.x;
1933 unsigned long color;
1935 if (s->face->use_box_color_for_shadows_p)
1936 color = s->face->box_color;
1937 else if (s->first_glyph->type == IMAGE_GLYPH
1938 && s->img->pixmap
1939 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1940 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1941 else
1943 XGCValues xgcv;
1945 /* Get the background color of the face. */
1946 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
1947 color = xgcv.background;
1950 if (di->white_relief.gc == 0
1951 || color != di->relief_background)
1953 di->relief_background = color;
1954 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1955 WHITE_PIX_DEFAULT (s->f));
1956 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1957 BLACK_PIX_DEFAULT (s->f));
1962 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1963 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1964 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1965 relief. LEFT_P non-zero means draw a relief on the left side of
1966 the rectangle. RIGHT_P non-zero means draw a relief on the right
1967 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1968 when drawing. */
1970 static void
1971 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
1972 raised_p, left_p, right_p, clip_rect)
1973 struct frame *f;
1974 int left_x, top_y, right_x, bottom_y, width, left_p, right_p, raised_p;
1975 XRectangle *clip_rect;
1977 Display *dpy = FRAME_X_DISPLAY (f);
1978 Window window = FRAME_X_WINDOW (f);
1979 int i;
1980 GC gc;
1982 if (raised_p)
1983 gc = f->output_data.x->white_relief.gc;
1984 else
1985 gc = f->output_data.x->black_relief.gc;
1986 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1988 /* Top. */
1989 for (i = 0; i < width; ++i)
1990 XDrawLine (dpy, window, gc,
1991 left_x + i * left_p, top_y + i,
1992 right_x + 1 - i * right_p, top_y + i);
1994 /* Left. */
1995 if (left_p)
1996 for (i = 0; i < width; ++i)
1997 XDrawLine (dpy, window, gc,
1998 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2000 XSetClipMask (dpy, gc, None);
2001 if (raised_p)
2002 gc = f->output_data.x->black_relief.gc;
2003 else
2004 gc = f->output_data.x->white_relief.gc;
2005 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2007 /* Bottom. */
2008 for (i = 0; i < width; ++i)
2009 XDrawLine (dpy, window, gc,
2010 left_x + i * left_p, bottom_y - i,
2011 right_x + 1 - i * right_p, bottom_y - i);
2013 /* Right. */
2014 if (right_p)
2015 for (i = 0; i < width; ++i)
2016 XDrawLine (dpy, window, gc,
2017 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2019 XSetClipMask (dpy, gc, None);
2023 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2024 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2025 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2026 left side of the rectangle. RIGHT_P non-zero means draw a line
2027 on the right side of the rectangle. CLIP_RECT is the clipping
2028 rectangle to use when drawing. */
2030 static void
2031 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2032 left_p, right_p, clip_rect)
2033 struct glyph_string *s;
2034 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2035 XRectangle *clip_rect;
2037 XGCValues xgcv;
2039 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2040 XSetForeground (s->display, s->gc, s->face->box_color);
2041 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2043 /* Top. */
2044 XFillRectangle (s->display, s->window, s->gc,
2045 left_x, top_y, right_x - left_x + 1, width);
2047 /* Left. */
2048 if (left_p)
2049 XFillRectangle (s->display, s->window, s->gc,
2050 left_x, top_y, width, bottom_y - top_y + 1);
2052 /* Bottom. */
2053 XFillRectangle (s->display, s->window, s->gc,
2054 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2056 /* Right. */
2057 if (right_p)
2058 XFillRectangle (s->display, s->window, s->gc,
2059 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2061 XSetForeground (s->display, s->gc, xgcv.foreground);
2062 XSetClipMask (s->display, s->gc, None);
2066 /* Draw a box around glyph string S. */
2068 static void
2069 x_draw_glyph_string_box (s)
2070 struct glyph_string *s;
2072 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2073 int left_p, right_p;
2074 struct glyph *last_glyph;
2075 XRectangle clip_rect;
2077 last_x = window_box_right (s->w, s->area);
2078 if (s->row->full_width_p
2079 && !s->w->pseudo_window_p)
2081 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
2082 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
2083 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
2086 /* The glyph that may have a right box line. */
2087 last_glyph = (s->cmp || s->img
2088 ? s->first_glyph
2089 : s->first_glyph + s->nchars - 1);
2091 width = abs (s->face->box_line_width);
2092 raised_p = s->face->box == FACE_RAISED_BOX;
2093 left_x = s->x;
2094 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2095 ? last_x - 1
2096 : min (last_x, s->x + s->background_width) - 1);
2097 top_y = s->y;
2098 bottom_y = top_y + s->height - 1;
2100 left_p = (s->first_glyph->left_box_line_p
2101 || (s->hl == DRAW_MOUSE_FACE
2102 && (s->prev == NULL
2103 || s->prev->hl != s->hl)));
2104 right_p = (last_glyph->right_box_line_p
2105 || (s->hl == DRAW_MOUSE_FACE
2106 && (s->next == NULL
2107 || s->next->hl != s->hl)));
2109 get_glyph_string_clip_rect (s, &clip_rect);
2111 if (s->face->box == FACE_SIMPLE_BOX)
2112 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2113 left_p, right_p, &clip_rect);
2114 else
2116 x_setup_relief_colors (s);
2117 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2118 width, raised_p, left_p, right_p, &clip_rect);
2123 /* Draw foreground of image glyph string S. */
2125 static void
2126 x_draw_image_foreground (s)
2127 struct glyph_string *s;
2129 int x;
2130 int y = s->ybase - image_ascent (s->img, s->face);
2132 /* If first glyph of S has a left box line, start drawing it to the
2133 right of that line. */
2134 if (s->face->box != FACE_NO_BOX
2135 && s->first_glyph->left_box_line_p)
2136 x = s->x + abs (s->face->box_line_width);
2137 else
2138 x = s->x;
2140 /* If there is a margin around the image, adjust x- and y-position
2141 by that margin. */
2142 x += s->img->hmargin;
2143 y += s->img->vmargin;
2145 if (s->img->pixmap)
2147 if (s->img->mask)
2149 /* We can't set both a clip mask and use XSetClipRectangles
2150 because the latter also sets a clip mask. We also can't
2151 trust on the shape extension to be available
2152 (XShapeCombineRegion). So, compute the rectangle to draw
2153 manually. */
2154 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2155 | GCFunction);
2156 XGCValues xgcv;
2157 XRectangle clip_rect, image_rect, r;
2159 xgcv.clip_mask = s->img->mask;
2160 xgcv.clip_x_origin = x;
2161 xgcv.clip_y_origin = y;
2162 xgcv.function = GXcopy;
2163 XChangeGC (s->display, s->gc, mask, &xgcv);
2165 get_glyph_string_clip_rect (s, &clip_rect);
2166 image_rect.x = x;
2167 image_rect.y = y;
2168 image_rect.width = s->img->width;
2169 image_rect.height = s->img->height;
2170 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2171 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2172 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
2174 else
2176 XRectangle clip_rect, image_rect, r;
2178 get_glyph_string_clip_rect (s, &clip_rect);
2179 image_rect.x = x;
2180 image_rect.y = y;
2181 image_rect.width = s->img->width;
2182 image_rect.height = s->img->height;
2183 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2184 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2185 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
2187 /* When the image has a mask, we can expect that at
2188 least part of a mouse highlight or a block cursor will
2189 be visible. If the image doesn't have a mask, make
2190 a block cursor visible by drawing a rectangle around
2191 the image. I believe it's looking better if we do
2192 nothing here for mouse-face. */
2193 if (s->hl == DRAW_CURSOR)
2195 int r = s->img->relief;
2196 if (r < 0) r = -r;
2197 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2198 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
2202 else
2203 /* Draw a rectangle if image could not be loaded. */
2204 XDrawRectangle (s->display, s->window, s->gc, x, y,
2205 s->img->width - 1, s->img->height - 1);
2209 /* Draw a relief around the image glyph string S. */
2211 static void
2212 x_draw_image_relief (s)
2213 struct glyph_string *s;
2215 int x0, y0, x1, y1, thick, raised_p;
2216 XRectangle r;
2217 int x;
2218 int y = s->ybase - image_ascent (s->img, s->face);
2220 /* If first glyph of S has a left box line, start drawing it to the
2221 right of that line. */
2222 if (s->face->box != FACE_NO_BOX
2223 && s->first_glyph->left_box_line_p)
2224 x = s->x + abs (s->face->box_line_width);
2225 else
2226 x = s->x;
2228 /* If there is a margin around the image, adjust x- and y-position
2229 by that margin. */
2230 x += s->img->hmargin;
2231 y += s->img->vmargin;
2233 if (s->hl == DRAW_IMAGE_SUNKEN
2234 || s->hl == DRAW_IMAGE_RAISED)
2236 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2237 raised_p = s->hl == DRAW_IMAGE_RAISED;
2239 else
2241 thick = abs (s->img->relief);
2242 raised_p = s->img->relief > 0;
2245 x0 = x - thick;
2246 y0 = y - thick;
2247 x1 = x + s->img->width + thick - 1;
2248 y1 = y + s->img->height + thick - 1;
2250 x_setup_relief_colors (s);
2251 get_glyph_string_clip_rect (s, &r);
2252 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
2256 /* Draw the foreground of image glyph string S to PIXMAP. */
2258 static void
2259 x_draw_image_foreground_1 (s, pixmap)
2260 struct glyph_string *s;
2261 Pixmap pixmap;
2263 int x;
2264 int y = s->ybase - s->y - image_ascent (s->img, s->face);
2266 /* If first glyph of S has a left box line, start drawing it to the
2267 right of that line. */
2268 if (s->face->box != FACE_NO_BOX
2269 && s->first_glyph->left_box_line_p)
2270 x = abs (s->face->box_line_width);
2271 else
2272 x = 0;
2274 /* If there is a margin around the image, adjust x- and y-position
2275 by that margin. */
2276 x += s->img->hmargin;
2277 y += s->img->vmargin;
2279 if (s->img->pixmap)
2281 if (s->img->mask)
2283 /* We can't set both a clip mask and use XSetClipRectangles
2284 because the latter also sets a clip mask. We also can't
2285 trust on the shape extension to be available
2286 (XShapeCombineRegion). So, compute the rectangle to draw
2287 manually. */
2288 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2289 | GCFunction);
2290 XGCValues xgcv;
2292 xgcv.clip_mask = s->img->mask;
2293 xgcv.clip_x_origin = x;
2294 xgcv.clip_y_origin = y;
2295 xgcv.function = GXcopy;
2296 XChangeGC (s->display, s->gc, mask, &xgcv);
2298 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2299 0, 0, s->img->width, s->img->height, x, y);
2300 XSetClipMask (s->display, s->gc, None);
2302 else
2304 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2305 0, 0, s->img->width, s->img->height, x, y);
2307 /* When the image has a mask, we can expect that at
2308 least part of a mouse highlight or a block cursor will
2309 be visible. If the image doesn't have a mask, make
2310 a block cursor visible by drawing a rectangle around
2311 the image. I believe it's looking better if we do
2312 nothing here for mouse-face. */
2313 if (s->hl == DRAW_CURSOR)
2315 int r = s->img->relief;
2316 if (r < 0) r = -r;
2317 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2318 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
2322 else
2323 /* Draw a rectangle if image could not be loaded. */
2324 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2325 s->img->width - 1, s->img->height - 1);
2329 /* Draw part of the background of glyph string S. X, Y, W, and H
2330 give the rectangle to draw. */
2332 static void
2333 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2334 struct glyph_string *s;
2335 int x, y, w, h;
2337 if (s->stippled_p)
2339 /* Fill background with a stipple pattern. */
2340 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2341 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2342 XSetFillStyle (s->display, s->gc, FillSolid);
2344 else
2345 x_clear_glyph_string_rect (s, x, y, w, h);
2349 /* Draw image glyph string S.
2351 s->y
2352 s->x +-------------------------
2353 | s->face->box
2355 | +-------------------------
2356 | | s->img->margin
2358 | | +-------------------
2359 | | | the image
2363 static void
2364 x_draw_image_glyph_string (s)
2365 struct glyph_string *s;
2367 int x, y;
2368 int box_line_hwidth = abs (s->face->box_line_width);
2369 int box_line_vwidth = max (s->face->box_line_width, 0);
2370 int height;
2371 Pixmap pixmap = None;
2373 height = s->height - 2 * box_line_vwidth;
2376 /* Fill background with face under the image. Do it only if row is
2377 taller than image or if image has a clip mask to reduce
2378 flickering. */
2379 s->stippled_p = s->face->stipple != 0;
2380 if (height > s->img->height
2381 || s->img->hmargin
2382 || s->img->vmargin
2383 || s->img->mask
2384 || s->img->pixmap == 0
2385 || s->width != s->background_width)
2387 if (box_line_hwidth && s->first_glyph->left_box_line_p)
2388 x = s->x + box_line_hwidth;
2389 else
2390 x = s->x;
2392 y = s->y + box_line_vwidth;
2394 if (s->img->mask)
2396 /* Create a pixmap as large as the glyph string. Fill it
2397 with the background color. Copy the image to it, using
2398 its mask. Copy the temporary pixmap to the display. */
2399 Screen *screen = FRAME_X_SCREEN (s->f);
2400 int depth = DefaultDepthOfScreen (screen);
2402 /* Create a pixmap as large as the glyph string. */
2403 pixmap = XCreatePixmap (s->display, s->window,
2404 s->background_width,
2405 s->height, depth);
2407 /* Don't clip in the following because we're working on the
2408 pixmap. */
2409 XSetClipMask (s->display, s->gc, None);
2411 /* Fill the pixmap with the background color/stipple. */
2412 if (s->stippled_p)
2414 /* Fill background with a stipple pattern. */
2415 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2416 XFillRectangle (s->display, pixmap, s->gc,
2417 0, 0, s->background_width, s->height);
2418 XSetFillStyle (s->display, s->gc, FillSolid);
2420 else
2422 XGCValues xgcv;
2423 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2424 &xgcv);
2425 XSetForeground (s->display, s->gc, xgcv.background);
2426 XFillRectangle (s->display, pixmap, s->gc,
2427 0, 0, s->background_width, s->height);
2428 XSetForeground (s->display, s->gc, xgcv.foreground);
2431 else
2432 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2434 s->background_filled_p = 1;
2437 /* Draw the foreground. */
2438 if (pixmap != None)
2440 x_draw_image_foreground_1 (s, pixmap);
2441 x_set_glyph_string_clipping (s);
2442 XCopyArea (s->display, pixmap, s->window, s->gc,
2443 0, 0, s->background_width, s->height, s->x, s->y);
2444 XFreePixmap (s->display, pixmap);
2446 else
2447 x_draw_image_foreground (s);
2449 /* If we must draw a relief around the image, do it. */
2450 if (s->img->relief
2451 || s->hl == DRAW_IMAGE_RAISED
2452 || s->hl == DRAW_IMAGE_SUNKEN)
2453 x_draw_image_relief (s);
2457 /* Draw stretch glyph string S. */
2459 static void
2460 x_draw_stretch_glyph_string (s)
2461 struct glyph_string *s;
2463 xassert (s->first_glyph->type == STRETCH_GLYPH);
2464 s->stippled_p = s->face->stipple != 0;
2466 if (s->hl == DRAW_CURSOR
2467 && !x_stretch_cursor_p)
2469 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2470 as wide as the stretch glyph. */
2471 int width = min (CANON_X_UNIT (s->f), s->background_width);
2473 /* Draw cursor. */
2474 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
2476 /* Clear rest using the GC of the original non-cursor face. */
2477 if (width < s->background_width)
2479 int x = s->x + width, y = s->y;
2480 int w = s->background_width - width, h = s->height;
2481 XRectangle r;
2482 GC gc;
2484 if (s->row->mouse_face_p
2485 && cursor_in_mouse_face_p (s->w))
2487 x_set_mouse_face_gc (s);
2488 gc = s->gc;
2490 else
2491 gc = s->face->gc;
2493 get_glyph_string_clip_rect (s, &r);
2494 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2496 if (s->face->stipple)
2498 /* Fill background with a stipple pattern. */
2499 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2500 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2501 XSetFillStyle (s->display, gc, FillSolid);
2503 else
2505 XGCValues xgcv;
2506 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2507 XSetForeground (s->display, gc, xgcv.background);
2508 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2509 XSetForeground (s->display, gc, xgcv.foreground);
2513 else if (!s->background_filled_p)
2514 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
2515 s->height);
2517 s->background_filled_p = 1;
2521 /* Draw glyph string S. */
2523 static void
2524 x_draw_glyph_string (s)
2525 struct glyph_string *s;
2527 int relief_drawn_p = 0;
2529 /* If S draws into the background of its successor, draw the
2530 background of the successor first so that S can draw into it.
2531 This makes S->next use XDrawString instead of XDrawImageString. */
2532 if (s->next && s->right_overhang && !s->for_overlaps_p)
2534 xassert (s->next->img == NULL);
2535 x_set_glyph_string_gc (s->next);
2536 x_set_glyph_string_clipping (s->next);
2537 x_draw_glyph_string_background (s->next, 1);
2540 /* Set up S->gc, set clipping and draw S. */
2541 x_set_glyph_string_gc (s);
2543 /* Draw relief (if any) in advance for char/composition so that the
2544 glyph string can be drawn over it. */
2545 if (!s->for_overlaps_p
2546 && s->face->box != FACE_NO_BOX
2547 && (s->first_glyph->type == CHAR_GLYPH
2548 || s->first_glyph->type == COMPOSITE_GLYPH))
2551 x_set_glyph_string_clipping (s);
2552 x_draw_glyph_string_background (s, 1);
2553 x_draw_glyph_string_box (s);
2554 x_set_glyph_string_clipping (s);
2555 relief_drawn_p = 1;
2557 else
2558 x_set_glyph_string_clipping (s);
2560 switch (s->first_glyph->type)
2562 case IMAGE_GLYPH:
2563 x_draw_image_glyph_string (s);
2564 break;
2566 case STRETCH_GLYPH:
2567 x_draw_stretch_glyph_string (s);
2568 break;
2570 case CHAR_GLYPH:
2571 if (s->for_overlaps_p)
2572 s->background_filled_p = 1;
2573 else
2574 x_draw_glyph_string_background (s, 0);
2575 x_draw_glyph_string_foreground (s);
2576 break;
2578 case COMPOSITE_GLYPH:
2579 if (s->for_overlaps_p || s->gidx > 0)
2580 s->background_filled_p = 1;
2581 else
2582 x_draw_glyph_string_background (s, 1);
2583 x_draw_composite_glyph_string_foreground (s);
2584 break;
2586 default:
2587 abort ();
2590 if (!s->for_overlaps_p)
2592 /* Draw underline. */
2593 if (s->face->underline_p)
2595 unsigned long tem, h;
2596 int y;
2598 /* Get the underline thickness. Default is 1 pixel. */
2599 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
2600 h = 1;
2602 /* Get the underline position. This is the recommended
2603 vertical offset in pixels from the baseline to the top of
2604 the underline. This is a signed value according to the
2605 specs, and its default is
2607 ROUND ((maximum descent) / 2), with
2608 ROUND(x) = floor (x + 0.5) */
2610 if (x_use_underline_position_properties
2611 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
2612 y = s->ybase + (long) tem;
2613 else if (s->face->font)
2614 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2615 else
2616 y = s->y + s->height - h;
2618 if (s->face->underline_defaulted_p)
2619 XFillRectangle (s->display, s->window, s->gc,
2620 s->x, y, s->width, h);
2621 else
2623 XGCValues xgcv;
2624 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2625 XSetForeground (s->display, s->gc, s->face->underline_color);
2626 XFillRectangle (s->display, s->window, s->gc,
2627 s->x, y, s->width, h);
2628 XSetForeground (s->display, s->gc, xgcv.foreground);
2632 /* Draw overline. */
2633 if (s->face->overline_p)
2635 unsigned long dy = 0, h = 1;
2637 if (s->face->overline_color_defaulted_p)
2638 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2639 s->width, h);
2640 else
2642 XGCValues xgcv;
2643 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2644 XSetForeground (s->display, s->gc, s->face->overline_color);
2645 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2646 s->width, h);
2647 XSetForeground (s->display, s->gc, xgcv.foreground);
2651 /* Draw strike-through. */
2652 if (s->face->strike_through_p)
2654 unsigned long h = 1;
2655 unsigned long dy = (s->height - h) / 2;
2657 if (s->face->strike_through_color_defaulted_p)
2658 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2659 s->width, h);
2660 else
2662 XGCValues xgcv;
2663 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2664 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2665 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2666 s->width, h);
2667 XSetForeground (s->display, s->gc, xgcv.foreground);
2671 /* Draw relief if not yet drawn. */
2672 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2673 x_draw_glyph_string_box (s);
2676 /* Reset clipping. */
2677 XSetClipMask (s->display, s->gc, None);
2680 /* Shift display to make room for inserted glyphs. */
2682 void
2683 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2684 struct frame *f;
2685 int x, y, width, height, shift_by;
2687 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2688 f->output_data.x->normal_gc,
2689 x, y, width, height,
2690 x + shift_by, y);
2693 /* Delete N glyphs at the nominal cursor position. Not implemented
2694 for X frames. */
2696 static void
2697 x_delete_glyphs (n)
2698 register int n;
2700 abort ();
2704 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2705 If they are <= 0, this is probably an error. */
2707 void
2708 x_clear_area (dpy, window, x, y, width, height, exposures)
2709 Display *dpy;
2710 Window window;
2711 int x, y;
2712 int width, height;
2713 int exposures;
2715 xassert (width > 0 && height > 0);
2716 XClearArea (dpy, window, x, y, width, height, exposures);
2720 /* Clear entire frame. If updating_frame is non-null, clear that
2721 frame. Otherwise clear the selected frame. */
2723 static void
2724 x_clear_frame ()
2726 struct frame *f;
2728 if (updating_frame)
2729 f = updating_frame;
2730 else
2731 f = SELECTED_FRAME ();
2733 /* Clearing the frame will erase any cursor, so mark them all as no
2734 longer visible. */
2735 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2736 output_cursor.hpos = output_cursor.vpos = 0;
2737 output_cursor.x = -1;
2739 /* We don't set the output cursor here because there will always
2740 follow an explicit cursor_to. */
2741 BLOCK_INPUT;
2742 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2744 /* We have to clear the scroll bars, too. If we have changed
2745 colors or something like that, then they should be notified. */
2746 x_scroll_bar_clear (f);
2748 XFlush (FRAME_X_DISPLAY (f));
2750 #ifdef USE_GTK
2751 xg_frame_cleared (f);
2752 #endif
2754 UNBLOCK_INPUT;
2759 /* Invert the middle quarter of the frame for .15 sec. */
2761 /* We use the select system call to do the waiting, so we have to make
2762 sure it's available. If it isn't, we just won't do visual bells. */
2764 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2767 /* Subtract the `struct timeval' values X and Y, storing the result in
2768 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2770 static int
2771 timeval_subtract (result, x, y)
2772 struct timeval *result, x, y;
2774 /* Perform the carry for the later subtraction by updating y. This
2775 is safer because on some systems the tv_sec member is unsigned. */
2776 if (x.tv_usec < y.tv_usec)
2778 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2779 y.tv_usec -= 1000000 * nsec;
2780 y.tv_sec += nsec;
2783 if (x.tv_usec - y.tv_usec > 1000000)
2785 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2786 y.tv_usec += 1000000 * nsec;
2787 y.tv_sec -= nsec;
2790 /* Compute the time remaining to wait. tv_usec is certainly
2791 positive. */
2792 result->tv_sec = x.tv_sec - y.tv_sec;
2793 result->tv_usec = x.tv_usec - y.tv_usec;
2795 /* Return indication of whether the result should be considered
2796 negative. */
2797 return x.tv_sec < y.tv_sec;
2800 void
2801 XTflash (f)
2802 struct frame *f;
2804 BLOCK_INPUT;
2807 GC gc;
2809 /* Create a GC that will use the GXxor function to flip foreground
2810 pixels into background pixels. */
2812 XGCValues values;
2814 values.function = GXxor;
2815 values.foreground = (f->output_data.x->foreground_pixel
2816 ^ f->output_data.x->background_pixel);
2818 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2819 GCFunction | GCForeground, &values);
2823 /* Get the height not including a menu bar widget. */
2824 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
2825 /* Height of each line to flash. */
2826 int flash_height = FRAME_LINE_HEIGHT (f);
2827 /* These will be the left and right margins of the rectangles. */
2828 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
2829 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
2831 int width;
2833 /* Don't flash the area between a scroll bar and the frame
2834 edge it is next to. */
2835 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
2837 case vertical_scroll_bar_left:
2838 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2839 break;
2841 case vertical_scroll_bar_right:
2842 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2843 break;
2845 default:
2846 break;
2849 width = flash_right - flash_left;
2851 /* If window is tall, flash top and bottom line. */
2852 if (height > 3 * FRAME_LINE_HEIGHT (f))
2854 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2855 flash_left,
2856 (FRAME_INTERNAL_BORDER_WIDTH (f)
2857 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
2858 width, flash_height);
2859 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2860 flash_left,
2861 (height - flash_height
2862 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2863 width, flash_height);
2865 else
2866 /* If it is short, flash it all. */
2867 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2868 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2869 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2871 x_flush (f);
2874 struct timeval wakeup;
2876 EMACS_GET_TIME (wakeup);
2878 /* Compute time to wait until, propagating carry from usecs. */
2879 wakeup.tv_usec += 150000;
2880 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
2881 wakeup.tv_usec %= 1000000;
2883 /* Keep waiting until past the time wakeup or any input gets
2884 available. */
2885 while (! detect_input_pending ())
2887 struct timeval current;
2888 struct timeval timeout;
2890 EMACS_GET_TIME (current);
2892 /* Break if result would be negative. */
2893 if (timeval_subtract (&current, wakeup, current))
2894 break;
2896 /* How long `select' should wait. */
2897 timeout.tv_sec = 0;
2898 timeout.tv_usec = 10000;
2900 /* Try to wait that long--but we might wake up sooner. */
2901 select (0, NULL, NULL, NULL, &timeout);
2905 /* If window is tall, flash top and bottom line. */
2906 if (height > 3 * FRAME_LINE_HEIGHT (f))
2908 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2909 flash_left,
2910 (FRAME_INTERNAL_BORDER_WIDTH (f)
2911 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
2912 width, flash_height);
2913 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2914 flash_left,
2915 (height - flash_height
2916 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2917 width, flash_height);
2919 else
2920 /* If it is short, flash it all. */
2921 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2922 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2923 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2925 XFreeGC (FRAME_X_DISPLAY (f), gc);
2926 x_flush (f);
2930 UNBLOCK_INPUT;
2933 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
2936 /* Make audible bell. */
2938 void
2939 XTring_bell ()
2941 struct frame *f = SELECTED_FRAME ();
2943 if (FRAME_X_DISPLAY (f))
2945 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2946 if (visible_bell)
2947 XTflash (f);
2948 else
2949 #endif
2951 BLOCK_INPUT;
2952 XBell (FRAME_X_DISPLAY (f), 0);
2953 XFlush (FRAME_X_DISPLAY (f));
2954 UNBLOCK_INPUT;
2960 /* Specify how many text lines, from the top of the window,
2961 should be affected by insert-lines and delete-lines operations.
2962 This, and those operations, are used only within an update
2963 that is bounded by calls to x_update_begin and x_update_end. */
2965 static void
2966 XTset_terminal_window (n)
2967 register int n;
2969 /* This function intentionally left blank. */
2974 /***********************************************************************
2975 Line Dance
2976 ***********************************************************************/
2978 /* Perform an insert-lines or delete-lines operation, inserting N
2979 lines or deleting -N lines at vertical position VPOS. */
2981 static void
2982 x_ins_del_lines (vpos, n)
2983 int vpos, n;
2985 abort ();
2989 /* Scroll part of the display as described by RUN. */
2991 static void
2992 x_scroll_run (w, run)
2993 struct window *w;
2994 struct run *run;
2996 struct frame *f = XFRAME (w->frame);
2997 int x, y, width, height, from_y, to_y, bottom_y;
2999 /* Get frame-relative bounding box of the text display area of W,
3000 without mode lines. Include in this box the left and right
3001 fringe of W. */
3002 window_box (w, -1, &x, &y, &width, &height);
3003 width += FRAME_X_FRINGE_WIDTH (f);
3004 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
3006 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3007 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3008 bottom_y = y + height;
3010 if (to_y < from_y)
3012 /* Scrolling up. Make sure we don't copy part of the mode
3013 line at the bottom. */
3014 if (from_y + run->height > bottom_y)
3015 height = bottom_y - from_y;
3016 else
3017 height = run->height;
3019 else
3021 /* Scolling down. Make sure we don't copy over the mode line.
3022 at the bottom. */
3023 if (to_y + run->height > bottom_y)
3024 height = bottom_y - to_y;
3025 else
3026 height = run->height;
3029 BLOCK_INPUT;
3031 /* Cursor off. Will be switched on again in x_update_window_end. */
3032 updated_window = w;
3033 x_clear_cursor (w);
3035 XCopyArea (FRAME_X_DISPLAY (f),
3036 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3037 f->output_data.x->normal_gc,
3038 x, from_y,
3039 width, height,
3040 x, to_y);
3042 UNBLOCK_INPUT;
3047 /***********************************************************************
3048 Exposure Events
3049 ***********************************************************************/
3052 static void
3053 frame_highlight (f)
3054 struct frame *f;
3056 /* We used to only do this if Vx_no_window_manager was non-nil, but
3057 the ICCCM (section 4.1.6) says that the window's border pixmap
3058 and border pixel are window attributes which are "private to the
3059 client", so we can always change it to whatever we want. */
3060 BLOCK_INPUT;
3061 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3062 f->output_data.x->border_pixel);
3063 UNBLOCK_INPUT;
3064 x_update_cursor (f, 1);
3067 static void
3068 frame_unhighlight (f)
3069 struct frame *f;
3071 /* We used to only do this if Vx_no_window_manager was non-nil, but
3072 the ICCCM (section 4.1.6) says that the window's border pixmap
3073 and border pixel are window attributes which are "private to the
3074 client", so we can always change it to whatever we want. */
3075 BLOCK_INPUT;
3076 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3077 f->output_data.x->border_tile);
3078 UNBLOCK_INPUT;
3079 x_update_cursor (f, 1);
3082 /* The focus has changed. Update the frames as necessary to reflect
3083 the new situation. Note that we can't change the selected frame
3084 here, because the Lisp code we are interrupting might become confused.
3085 Each event gets marked with the frame in which it occurred, so the
3086 Lisp code can tell when the switch took place by examining the events. */
3088 static void
3089 x_new_focus_frame (dpyinfo, frame)
3090 struct x_display_info *dpyinfo;
3091 struct frame *frame;
3093 struct frame *old_focus = dpyinfo->x_focus_frame;
3095 if (frame != dpyinfo->x_focus_frame)
3097 /* Set this before calling other routines, so that they see
3098 the correct value of x_focus_frame. */
3099 dpyinfo->x_focus_frame = frame;
3101 if (old_focus && old_focus->auto_lower)
3102 x_lower_frame (old_focus);
3104 #if 0
3105 selected_frame = frame;
3106 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3107 selected_frame);
3108 Fselect_window (selected_frame->selected_window, Qnil);
3109 choose_minibuf_frame ();
3110 #endif /* ! 0 */
3112 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3113 pending_autoraise_frame = dpyinfo->x_focus_frame;
3114 else
3115 pending_autoraise_frame = 0;
3118 x_frame_rehighlight (dpyinfo);
3121 /* Handle FocusIn and FocusOut state changes for FRAME.
3122 If FRAME has focus and there exists more than one frame, puts
3123 a FOCUS_IN_EVENT into BUFP.
3124 Returns number of events inserted into BUFP. */
3126 static int
3127 x_focus_changed (type, state, dpyinfo, frame, bufp, numchars)
3128 int type;
3129 int state;
3130 struct x_display_info *dpyinfo;
3131 struct frame *frame;
3132 struct input_event *bufp;
3133 int numchars;
3135 int nr_events = 0;
3137 if (type == FocusIn)
3139 if (dpyinfo->x_focus_event_frame != frame)
3141 x_new_focus_frame (dpyinfo, frame);
3142 dpyinfo->x_focus_event_frame = frame;
3144 /* Don't stop displaying the initial startup message
3145 for a switch-frame event we don't need. */
3146 if (numchars > 0
3147 && GC_NILP (Vterminal_frame)
3148 && GC_CONSP (Vframe_list)
3149 && !GC_NILP (XCDR (Vframe_list)))
3151 bufp->kind = FOCUS_IN_EVENT;
3152 XSETFRAME (bufp->frame_or_window, frame);
3153 bufp->arg = Qnil;
3154 ++bufp;
3155 numchars--;
3156 ++nr_events;
3160 frame->output_data.x->focus_state |= state;
3162 #ifdef HAVE_X_I18N
3163 if (FRAME_XIC (frame))
3164 XSetICFocus (FRAME_XIC (frame));
3165 #endif
3167 else if (type == FocusOut)
3169 frame->output_data.x->focus_state &= ~state;
3171 if (dpyinfo->x_focus_event_frame == frame)
3173 dpyinfo->x_focus_event_frame = 0;
3174 x_new_focus_frame (dpyinfo, 0);
3177 #ifdef HAVE_X_I18N
3178 if (FRAME_XIC (frame))
3179 XUnsetICFocus (FRAME_XIC (frame));
3180 #endif
3183 return nr_events;
3186 /* The focus may have changed. Figure out if it is a real focus change,
3187 by checking both FocusIn/Out and Enter/LeaveNotify events.
3189 Returns number of events inserted into BUFP. */
3191 static int
3192 x_detect_focus_change (dpyinfo, event, bufp, numchars)
3193 struct x_display_info *dpyinfo;
3194 XEvent *event;
3195 struct input_event *bufp;
3196 int numchars;
3198 struct frame *frame;
3199 int nr_events = 0;
3201 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3202 if (! frame) return nr_events;
3204 switch (event->type)
3206 case EnterNotify:
3207 case LeaveNotify:
3208 if (event->xcrossing.detail != NotifyInferior
3209 && event->xcrossing.focus
3210 && ! (frame->output_data.x->focus_state & FOCUS_EXPLICIT))
3211 nr_events = x_focus_changed ((event->type == EnterNotify
3212 ? FocusIn : FocusOut),
3213 FOCUS_IMPLICIT,
3214 dpyinfo,
3215 frame,
3216 bufp,
3217 numchars);
3218 break;
3220 case FocusIn:
3221 case FocusOut:
3222 nr_events = x_focus_changed (event->type,
3223 (event->xfocus.detail == NotifyPointer
3224 ? FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3225 dpyinfo,
3226 frame,
3227 bufp,
3228 numchars);
3229 break;
3232 return nr_events;
3236 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3238 void
3239 x_mouse_leave (dpyinfo)
3240 struct x_display_info *dpyinfo;
3242 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3245 /* The focus has changed, or we have redirected a frame's focus to
3246 another frame (this happens when a frame uses a surrogate
3247 mini-buffer frame). Shift the highlight as appropriate.
3249 The FRAME argument doesn't necessarily have anything to do with which
3250 frame is being highlighted or un-highlighted; we only use it to find
3251 the appropriate X display info. */
3253 static void
3254 XTframe_rehighlight (frame)
3255 struct frame *frame;
3257 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3260 static void
3261 x_frame_rehighlight (dpyinfo)
3262 struct x_display_info *dpyinfo;
3264 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3266 if (dpyinfo->x_focus_frame)
3268 dpyinfo->x_highlight_frame
3269 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3270 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3271 : dpyinfo->x_focus_frame);
3272 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3274 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3275 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3278 else
3279 dpyinfo->x_highlight_frame = 0;
3281 if (dpyinfo->x_highlight_frame != old_highlight)
3283 if (old_highlight)
3284 frame_unhighlight (old_highlight);
3285 if (dpyinfo->x_highlight_frame)
3286 frame_highlight (dpyinfo->x_highlight_frame);
3292 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3294 /* Initialize mode_switch_bit and modifier_meaning. */
3295 static void
3296 x_find_modifier_meanings (dpyinfo)
3297 struct x_display_info *dpyinfo;
3299 int min_code, max_code;
3300 KeySym *syms;
3301 int syms_per_code;
3302 XModifierKeymap *mods;
3304 dpyinfo->meta_mod_mask = 0;
3305 dpyinfo->shift_lock_mask = 0;
3306 dpyinfo->alt_mod_mask = 0;
3307 dpyinfo->super_mod_mask = 0;
3308 dpyinfo->hyper_mod_mask = 0;
3310 #ifdef HAVE_X11R4
3311 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3312 #else
3313 min_code = dpyinfo->display->min_keycode;
3314 max_code = dpyinfo->display->max_keycode;
3315 #endif
3317 syms = XGetKeyboardMapping (dpyinfo->display,
3318 min_code, max_code - min_code + 1,
3319 &syms_per_code);
3320 mods = XGetModifierMapping (dpyinfo->display);
3322 /* Scan the modifier table to see which modifier bits the Meta and
3323 Alt keysyms are on. */
3325 int row, col; /* The row and column in the modifier table. */
3327 for (row = 3; row < 8; row++)
3328 for (col = 0; col < mods->max_keypermod; col++)
3330 KeyCode code
3331 = mods->modifiermap[(row * mods->max_keypermod) + col];
3333 /* Zeroes are used for filler. Skip them. */
3334 if (code == 0)
3335 continue;
3337 /* Are any of this keycode's keysyms a meta key? */
3339 int code_col;
3341 for (code_col = 0; code_col < syms_per_code; code_col++)
3343 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3345 switch (sym)
3347 case XK_Meta_L:
3348 case XK_Meta_R:
3349 dpyinfo->meta_mod_mask |= (1 << row);
3350 break;
3352 case XK_Alt_L:
3353 case XK_Alt_R:
3354 dpyinfo->alt_mod_mask |= (1 << row);
3355 break;
3357 case XK_Hyper_L:
3358 case XK_Hyper_R:
3359 dpyinfo->hyper_mod_mask |= (1 << row);
3360 break;
3362 case XK_Super_L:
3363 case XK_Super_R:
3364 dpyinfo->super_mod_mask |= (1 << row);
3365 break;
3367 case XK_Shift_Lock:
3368 /* Ignore this if it's not on the lock modifier. */
3369 if ((1 << row) == LockMask)
3370 dpyinfo->shift_lock_mask = LockMask;
3371 break;
3378 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3379 if (! dpyinfo->meta_mod_mask)
3381 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3382 dpyinfo->alt_mod_mask = 0;
3385 /* If some keys are both alt and meta,
3386 make them just meta, not alt. */
3387 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3389 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3392 XFree ((char *) syms);
3393 XFreeModifiermap (mods);
3396 /* Convert between the modifier bits X uses and the modifier bits
3397 Emacs uses. */
3399 static unsigned int
3400 x_x_to_emacs_modifiers (dpyinfo, state)
3401 struct x_display_info *dpyinfo;
3402 unsigned int state;
3404 EMACS_UINT mod_meta = meta_modifier;
3405 EMACS_UINT mod_alt = alt_modifier;
3406 EMACS_UINT mod_hyper = hyper_modifier;
3407 EMACS_UINT mod_super = super_modifier;
3408 Lisp_Object tem;
3410 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3411 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3412 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3413 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3414 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3415 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3416 tem = Fget (Vx_super_keysym, Qmodifier_value);
3417 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3420 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3421 | ((state & ControlMask) ? ctrl_modifier : 0)
3422 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3423 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3424 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3425 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3428 static unsigned int
3429 x_emacs_to_x_modifiers (dpyinfo, state)
3430 struct x_display_info *dpyinfo;
3431 unsigned int state;
3433 EMACS_UINT mod_meta = meta_modifier;
3434 EMACS_UINT mod_alt = alt_modifier;
3435 EMACS_UINT mod_hyper = hyper_modifier;
3436 EMACS_UINT mod_super = super_modifier;
3438 Lisp_Object tem;
3440 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3441 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3442 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3443 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3444 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3445 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3446 tem = Fget (Vx_super_keysym, Qmodifier_value);
3447 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3450 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3451 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3452 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3453 | ((state & shift_modifier) ? ShiftMask : 0)
3454 | ((state & ctrl_modifier) ? ControlMask : 0)
3455 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3458 /* Convert a keysym to its name. */
3460 char *
3461 x_get_keysym_name (keysym)
3462 KeySym keysym;
3464 char *value;
3466 BLOCK_INPUT;
3467 value = XKeysymToString (keysym);
3468 UNBLOCK_INPUT;
3470 return value;
3475 /* Mouse clicks and mouse movement. Rah. */
3477 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3479 If the event is a button press, then note that we have grabbed
3480 the mouse. */
3482 static Lisp_Object
3483 construct_mouse_click (result, event, f)
3484 struct input_event *result;
3485 XButtonEvent *event;
3486 struct frame *f;
3488 /* Make the event type NO_EVENT; we'll change that when we decide
3489 otherwise. */
3490 result->kind = MOUSE_CLICK_EVENT;
3491 result->code = event->button - Button1;
3492 result->timestamp = event->time;
3493 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3494 event->state)
3495 | (event->type == ButtonRelease
3496 ? up_modifier
3497 : down_modifier));
3499 XSETINT (result->x, event->x);
3500 XSETINT (result->y, event->y);
3501 XSETFRAME (result->frame_or_window, f);
3502 result->arg = Qnil;
3503 return Qnil;
3507 /* Function to report a mouse movement to the mainstream Emacs code.
3508 The input handler calls this.
3510 We have received a mouse movement event, which is given in *event.
3511 If the mouse is over a different glyph than it was last time, tell
3512 the mainstream emacs code by setting mouse_moved. If not, ask for
3513 another motion event, so we can check again the next time it moves. */
3515 static XMotionEvent last_mouse_motion_event;
3516 static Lisp_Object last_mouse_motion_frame;
3518 static void
3519 note_mouse_movement (frame, event)
3520 FRAME_PTR frame;
3521 XMotionEvent *event;
3523 last_mouse_movement_time = event->time;
3524 last_mouse_motion_event = *event;
3525 XSETFRAME (last_mouse_motion_frame, frame);
3527 if (event->window != FRAME_X_WINDOW (frame))
3529 frame->mouse_moved = 1;
3530 last_mouse_scroll_bar = Qnil;
3531 note_mouse_highlight (frame, -1, -1);
3534 /* Has the mouse moved off the glyph it was on at the last sighting? */
3535 else if (event->x < last_mouse_glyph.x
3536 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3537 || event->y < last_mouse_glyph.y
3538 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3540 frame->mouse_moved = 1;
3541 last_mouse_scroll_bar = Qnil;
3542 note_mouse_highlight (frame, event->x, event->y);
3547 /************************************************************************
3548 Mouse Face
3549 ************************************************************************/
3551 static void
3552 redo_mouse_highlight ()
3554 if (!NILP (last_mouse_motion_frame)
3555 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3556 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3557 last_mouse_motion_event.x,
3558 last_mouse_motion_event.y);
3562 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
3565 /* Try to determine frame pixel position and size of the glyph under
3566 frame pixel coordinates X/Y on frame F . Return the position and
3567 size in *RECT. Value is non-zero if we could compute these
3568 values. */
3570 static int
3571 glyph_rect (f, x, y, rect)
3572 struct frame *f;
3573 int x, y;
3574 XRectangle *rect;
3576 Lisp_Object window;
3577 int found = 0;
3579 window = window_from_coordinates (f, x, y, 0, 0);
3580 if (!NILP (window))
3582 struct window *w = XWINDOW (window);
3583 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
3584 struct glyph_row *end = r + w->current_matrix->nrows - 1;
3586 frame_to_window_pixel_xy (w, &x, &y);
3588 for (; !found && r < end && r->enabled_p; ++r)
3589 if (r->y >= y)
3591 struct glyph *g = r->glyphs[TEXT_AREA];
3592 struct glyph *end = g + r->used[TEXT_AREA];
3593 int gx;
3595 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
3596 if (gx >= x)
3598 rect->width = g->pixel_width;
3599 rect->height = r->height;
3600 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
3601 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
3602 found = 1;
3607 return found;
3611 /* Return the current position of the mouse.
3612 *FP should be a frame which indicates which display to ask about.
3614 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3615 and *PART to the frame, window, and scroll bar part that the mouse
3616 is over. Set *X and *Y to the portion and whole of the mouse's
3617 position on the scroll bar.
3619 If the mouse movement started elsewhere, set *FP to the frame the
3620 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3621 the mouse is over.
3623 Set *TIME to the server time-stamp for the time at which the mouse
3624 was at this position.
3626 Don't store anything if we don't have a valid set of values to report.
3628 This clears the mouse_moved flag, so we can wait for the next mouse
3629 movement. */
3631 static void
3632 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3633 FRAME_PTR *fp;
3634 int insist;
3635 Lisp_Object *bar_window;
3636 enum scroll_bar_part *part;
3637 Lisp_Object *x, *y;
3638 unsigned long *time;
3640 FRAME_PTR f1;
3642 BLOCK_INPUT;
3644 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3645 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3646 else
3648 Window root;
3649 int root_x, root_y;
3651 Window dummy_window;
3652 int dummy;
3654 Lisp_Object frame, tail;
3656 /* Clear the mouse-moved flag for every frame on this display. */
3657 FOR_EACH_FRAME (tail, frame)
3658 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3659 XFRAME (frame)->mouse_moved = 0;
3661 last_mouse_scroll_bar = Qnil;
3663 /* Figure out which root window we're on. */
3664 XQueryPointer (FRAME_X_DISPLAY (*fp),
3665 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3667 /* The root window which contains the pointer. */
3668 &root,
3670 /* Trash which we can't trust if the pointer is on
3671 a different screen. */
3672 &dummy_window,
3674 /* The position on that root window. */
3675 &root_x, &root_y,
3677 /* More trash we can't trust. */
3678 &dummy, &dummy,
3680 /* Modifier keys and pointer buttons, about which
3681 we don't care. */
3682 (unsigned int *) &dummy);
3684 /* Now we have a position on the root; find the innermost window
3685 containing the pointer. */
3687 Window win, child;
3688 int win_x, win_y;
3689 int parent_x = 0, parent_y = 0;
3690 int count;
3692 win = root;
3694 /* XTranslateCoordinates can get errors if the window
3695 structure is changing at the same time this function
3696 is running. So at least we must not crash from them. */
3698 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
3700 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3701 && FRAME_LIVE_P (last_mouse_frame))
3703 /* If mouse was grabbed on a frame, give coords for that frame
3704 even if the mouse is now outside it. */
3705 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3707 /* From-window, to-window. */
3708 root, FRAME_X_WINDOW (last_mouse_frame),
3710 /* From-position, to-position. */
3711 root_x, root_y, &win_x, &win_y,
3713 /* Child of win. */
3714 &child);
3715 f1 = last_mouse_frame;
3717 else
3719 while (1)
3721 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3723 /* From-window, to-window. */
3724 root, win,
3726 /* From-position, to-position. */
3727 root_x, root_y, &win_x, &win_y,
3729 /* Child of win. */
3730 &child);
3732 if (child == None || child == win)
3733 break;
3735 win = child;
3736 parent_x = win_x;
3737 parent_y = win_y;
3740 /* Now we know that:
3741 win is the innermost window containing the pointer
3742 (XTC says it has no child containing the pointer),
3743 win_x and win_y are the pointer's position in it
3744 (XTC did this the last time through), and
3745 parent_x and parent_y are the pointer's position in win's parent.
3746 (They are what win_x and win_y were when win was child.
3747 If win is the root window, it has no parent, and
3748 parent_{x,y} are invalid, but that's okay, because we'll
3749 never use them in that case.) */
3751 /* Is win one of our frames? */
3752 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3754 #ifdef USE_X_TOOLKIT
3755 /* If we end up with the menu bar window, say it's not
3756 on the frame. */
3757 if (f1 != NULL
3758 && f1->output_data.x->menubar_widget
3759 && win == XtWindow (f1->output_data.x->menubar_widget))
3760 f1 = NULL;
3761 #endif /* USE_X_TOOLKIT */
3764 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3765 f1 = 0;
3767 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
3769 /* If not, is it one of our scroll bars? */
3770 if (! f1)
3772 struct scroll_bar *bar = x_window_to_scroll_bar (win);
3774 if (bar)
3776 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3777 win_x = parent_x;
3778 win_y = parent_y;
3782 if (f1 == 0 && insist > 0)
3783 f1 = SELECTED_FRAME ();
3785 if (f1)
3787 /* Ok, we found a frame. Store all the values.
3788 last_mouse_glyph is a rectangle used to reduce the
3789 generation of mouse events. To not miss any motion
3790 events, we must divide the frame into rectangles of the
3791 size of the smallest character that could be displayed
3792 on it, i.e. into the same rectangles that matrices on
3793 the frame are divided into. */
3795 int width, height, gx, gy;
3796 XRectangle rect;
3798 if (glyph_rect (f1, win_x, win_y, &rect))
3799 last_mouse_glyph = rect;
3800 else
3802 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
3803 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
3804 gx = win_x;
3805 gy = win_y;
3807 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
3808 round down even for negative values. */
3809 if (gx < 0)
3810 gx -= width - 1;
3811 if (gy < 0)
3812 gy -= height - 1;
3813 gx = (gx + width - 1) / width * width;
3814 gy = (gy + height - 1) / height * height;
3816 last_mouse_glyph.width = width;
3817 last_mouse_glyph.height = height;
3818 last_mouse_glyph.x = gx;
3819 last_mouse_glyph.y = gy;
3822 *bar_window = Qnil;
3823 *part = 0;
3824 *fp = f1;
3825 XSETINT (*x, win_x);
3826 XSETINT (*y, win_y);
3827 *time = last_mouse_movement_time;
3832 UNBLOCK_INPUT;
3837 /***********************************************************************
3838 Scroll bars
3839 ***********************************************************************/
3841 /* Scroll bar support. */
3843 /* Given an X window ID, find the struct scroll_bar which manages it.
3844 This can be called in GC, so we have to make sure to strip off mark
3845 bits. */
3847 static struct scroll_bar *
3848 x_window_to_scroll_bar (window_id)
3849 Window window_id;
3851 Lisp_Object tail;
3853 #ifdef USE_GTK
3854 window_id = (Window) xg_get_scroll_id_for_window (window_id);
3855 #endif /* USE_GTK */
3857 for (tail = Vframe_list;
3858 XGCTYPE (tail) == Lisp_Cons;
3859 tail = XCDR (tail))
3861 Lisp_Object frame, bar, condemned;
3863 frame = XCAR (tail);
3864 /* All elements of Vframe_list should be frames. */
3865 if (! GC_FRAMEP (frame))
3866 abort ();
3868 /* Scan this frame's scroll bar list for a scroll bar with the
3869 right window ID. */
3870 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3871 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3872 /* This trick allows us to search both the ordinary and
3873 condemned scroll bar lists with one loop. */
3874 ! GC_NILP (bar) || (bar = condemned,
3875 condemned = Qnil,
3876 ! GC_NILP (bar));
3877 bar = XSCROLL_BAR (bar)->next)
3878 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
3879 return XSCROLL_BAR (bar);
3882 return 0;
3886 #if defined USE_LUCID
3888 /* Return the Lucid menu bar WINDOW is part of. Return null
3889 if WINDOW is not part of a menu bar. */
3891 static Widget
3892 x_window_to_menu_bar (window)
3893 Window window;
3895 Lisp_Object tail;
3897 for (tail = Vframe_list;
3898 XGCTYPE (tail) == Lisp_Cons;
3899 tail = XCDR (tail))
3901 Lisp_Object frame = XCAR (tail);
3902 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
3904 if (menu_bar && xlwmenu_window_p (menu_bar, window))
3905 return menu_bar;
3908 return NULL;
3911 #endif /* USE_LUCID */
3914 /************************************************************************
3915 Toolkit scroll bars
3916 ************************************************************************/
3918 #ifdef USE_TOOLKIT_SCROLL_BARS
3920 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
3921 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
3922 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
3923 struct scroll_bar *));
3924 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
3925 int, int, int));
3928 /* Lisp window being scrolled. Set when starting to interact with
3929 a toolkit scroll bar, reset to nil when ending the interaction. */
3931 static Lisp_Object window_being_scrolled;
3933 /* Last scroll bar part sent in xm_scroll_callback. */
3935 static int last_scroll_bar_part;
3937 /* Whether this is an Xaw with arrow-scrollbars. This should imply
3938 that movements of 1/20 of the screen size are mapped to up/down. */
3940 #ifndef USE_GTK
3941 /* Id of action hook installed for scroll bars. */
3943 static XtActionHookId action_hook_id;
3945 static Boolean xaw3d_arrow_scroll;
3947 /* Whether the drag scrolling maintains the mouse at the top of the
3948 thumb. If not, resizing the thumb needs to be done more carefully
3949 to avoid jerkyness. */
3951 static Boolean xaw3d_pick_top;
3953 extern void set_vertical_scroll_bar P_ ((struct window *));
3955 /* Action hook installed via XtAppAddActionHook when toolkit scroll
3956 bars are used.. The hook is responsible for detecting when
3957 the user ends an interaction with the scroll bar, and generates
3958 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
3960 static void
3961 xt_action_hook (widget, client_data, action_name, event, params,
3962 num_params)
3963 Widget widget;
3964 XtPointer client_data;
3965 String action_name;
3966 XEvent *event;
3967 String *params;
3968 Cardinal *num_params;
3970 int scroll_bar_p;
3971 char *end_action;
3973 #ifdef USE_MOTIF
3974 scroll_bar_p = XmIsScrollBar (widget);
3975 end_action = "Release";
3976 #else /* !USE_MOTIF i.e. use Xaw */
3977 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
3978 end_action = "EndScroll";
3979 #endif /* USE_MOTIF */
3981 if (scroll_bar_p
3982 && strcmp (action_name, end_action) == 0
3983 && WINDOWP (window_being_scrolled))
3985 struct window *w;
3987 x_send_scroll_bar_event (window_being_scrolled,
3988 scroll_bar_end_scroll, 0, 0);
3989 w = XWINDOW (window_being_scrolled);
3991 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
3993 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
3994 /* The thumb size is incorrect while dragging: fix it. */
3995 set_vertical_scroll_bar (w);
3997 window_being_scrolled = Qnil;
3998 last_scroll_bar_part = -1;
4000 /* Xt timeouts no longer needed. */
4001 toolkit_scroll_bar_interaction = 0;
4004 #endif /* not USE_GTK */
4006 /* A vector of windows used for communication between
4007 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4009 static struct window **scroll_bar_windows;
4010 static int scroll_bar_windows_size;
4013 /* Send a client message with message type Xatom_Scrollbar for a
4014 scroll action to the frame of WINDOW. PART is a value identifying
4015 the part of the scroll bar that was clicked on. PORTION is the
4016 amount to scroll of a whole of WHOLE. */
4018 static void
4019 x_send_scroll_bar_event (window, part, portion, whole)
4020 Lisp_Object window;
4021 int part, portion, whole;
4023 XEvent event;
4024 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4025 struct window *w = XWINDOW (window);
4026 struct frame *f = XFRAME (w->frame);
4027 int i;
4029 BLOCK_INPUT;
4031 /* Construct a ClientMessage event to send to the frame. */
4032 ev->type = ClientMessage;
4033 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4034 ev->display = FRAME_X_DISPLAY (f);
4035 ev->window = FRAME_X_WINDOW (f);
4036 ev->format = 32;
4038 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4039 not enough to store a pointer or Lisp_Object on a 64 bit system.
4040 So, store the window in scroll_bar_windows and pass the index
4041 into that array in the event. */
4042 for (i = 0; i < scroll_bar_windows_size; ++i)
4043 if (scroll_bar_windows[i] == NULL)
4044 break;
4046 if (i == scroll_bar_windows_size)
4048 int new_size = max (10, 2 * scroll_bar_windows_size);
4049 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4050 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4052 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4053 nbytes);
4054 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4055 scroll_bar_windows_size = new_size;
4058 scroll_bar_windows[i] = w;
4059 ev->data.l[0] = (long) i;
4060 ev->data.l[1] = (long) part;
4061 ev->data.l[2] = (long) 0;
4062 ev->data.l[3] = (long) portion;
4063 ev->data.l[4] = (long) whole;
4065 /* Make Xt timeouts work while the scroll bar is active. */
4066 toolkit_scroll_bar_interaction = 1;
4068 /* Setting the event mask to zero means that the message will
4069 be sent to the client that created the window, and if that
4070 window no longer exists, no event will be sent. */
4071 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4072 UNBLOCK_INPUT;
4076 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4077 in *IEVENT. */
4079 static void
4080 x_scroll_bar_to_input_event (event, ievent)
4081 XEvent *event;
4082 struct input_event *ievent;
4084 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4085 Lisp_Object window;
4086 struct frame *f;
4087 struct window *w;
4089 w = scroll_bar_windows[ev->data.l[0]];
4090 scroll_bar_windows[ev->data.l[0]] = NULL;
4092 XSETWINDOW (window, w);
4093 f = XFRAME (w->frame);
4095 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4096 ievent->frame_or_window = window;
4097 ievent->arg = Qnil;
4098 #ifdef USE_GTK
4099 ievent->timestamp = CurrentTime;
4100 #else
4101 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4102 #endif
4103 ievent->part = ev->data.l[1];
4104 ievent->code = ev->data.l[2];
4105 ievent->x = make_number ((int) ev->data.l[3]);
4106 ievent->y = make_number ((int) ev->data.l[4]);
4107 ievent->modifiers = 0;
4111 #ifdef USE_MOTIF
4113 /* Minimum and maximum values used for Motif scroll bars. */
4115 #define XM_SB_MAX 10000000
4118 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4119 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4120 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4122 static void
4123 xm_scroll_callback (widget, client_data, call_data)
4124 Widget widget;
4125 XtPointer client_data, call_data;
4127 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4128 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4129 int part = -1, whole = 0, portion = 0;
4131 switch (cs->reason)
4133 case XmCR_DECREMENT:
4134 bar->dragging = Qnil;
4135 part = scroll_bar_up_arrow;
4136 break;
4138 case XmCR_INCREMENT:
4139 bar->dragging = Qnil;
4140 part = scroll_bar_down_arrow;
4141 break;
4143 case XmCR_PAGE_DECREMENT:
4144 bar->dragging = Qnil;
4145 part = scroll_bar_above_handle;
4146 break;
4148 case XmCR_PAGE_INCREMENT:
4149 bar->dragging = Qnil;
4150 part = scroll_bar_below_handle;
4151 break;
4153 case XmCR_TO_TOP:
4154 bar->dragging = Qnil;
4155 part = scroll_bar_to_top;
4156 break;
4158 case XmCR_TO_BOTTOM:
4159 bar->dragging = Qnil;
4160 part = scroll_bar_to_bottom;
4161 break;
4163 case XmCR_DRAG:
4165 int slider_size;
4167 /* Get the slider size. */
4168 BLOCK_INPUT;
4169 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4170 UNBLOCK_INPUT;
4172 whole = XM_SB_MAX - slider_size;
4173 portion = min (cs->value, whole);
4174 part = scroll_bar_handle;
4175 bar->dragging = make_number (cs->value);
4177 break;
4179 case XmCR_VALUE_CHANGED:
4180 break;
4183 if (part >= 0)
4185 window_being_scrolled = bar->window;
4186 last_scroll_bar_part = part;
4187 x_send_scroll_bar_event (bar->window, part, portion, whole);
4192 #else /* !USE_MOTIF, i.e. Xaw or GTK */
4193 #ifdef USE_GTK
4194 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4195 bar widget. DATA is a pointer to the scroll_bar structure. */
4197 static void
4198 xg_scroll_callback (widget, data)
4199 GtkRange *widget;
4200 gpointer data;
4202 struct scroll_bar *bar = (struct scroll_bar *) data;
4203 gdouble previous;
4204 gdouble position;
4205 gdouble *p;
4206 int diff;
4208 int part = -1, whole = 0, portion = 0;
4209 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4211 if (xg_ignore_gtk_scrollbar) return;
4213 position = gtk_adjustment_get_value (adj);
4215 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4216 if (! p)
4218 p = (gdouble*) xmalloc (sizeof (gdouble));
4219 *p = XG_SB_MIN;
4220 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4223 previous = *p;
4224 *p = position;
4226 diff = (int) (position - previous);
4228 if (diff == (int) adj->step_increment)
4230 part = scroll_bar_down_arrow;
4231 bar->dragging = Qnil;
4233 else if (-diff == (int) adj->step_increment)
4235 part = scroll_bar_up_arrow;
4236 bar->dragging = Qnil;
4238 else if (diff == (int) adj->page_increment)
4240 part = scroll_bar_below_handle;
4241 bar->dragging = Qnil;
4243 else if (-diff == (int) adj->page_increment)
4245 part = scroll_bar_above_handle;
4246 bar->dragging = Qnil;
4248 else
4250 part = scroll_bar_handle;
4251 whole = adj->upper - adj->page_size;
4252 portion = min ((int)position, whole);
4253 bar->dragging = make_number ((int)portion);
4256 if (part >= 0)
4258 window_being_scrolled = bar->window;
4259 last_scroll_bar_part = part;
4260 x_send_scroll_bar_event (bar->window, part, portion, whole);
4264 #else /* not USE_GTK */
4266 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4267 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4268 scroll bar struct. CALL_DATA is a pointer to a float saying where
4269 the thumb is. */
4271 static void
4272 xaw_jump_callback (widget, client_data, call_data)
4273 Widget widget;
4274 XtPointer client_data, call_data;
4276 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4277 float top = *(float *) call_data;
4278 float shown;
4279 int whole, portion, height;
4280 int part;
4282 /* Get the size of the thumb, a value between 0 and 1. */
4283 BLOCK_INPUT;
4284 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4285 UNBLOCK_INPUT;
4287 whole = 10000000;
4288 portion = shown < 1 ? top * whole : 0;
4290 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
4291 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4292 the bottom, so we force the scrolling whenever we see that we're
4293 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4294 we try to ensure that we always stay two pixels away from the
4295 bottom). */
4296 part = scroll_bar_down_arrow;
4297 else
4298 part = scroll_bar_handle;
4300 window_being_scrolled = bar->window;
4301 bar->dragging = make_number (portion);
4302 last_scroll_bar_part = part;
4303 x_send_scroll_bar_event (bar->window, part, portion, whole);
4307 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4308 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4309 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4310 the scroll bar. CALL_DATA is an integer specifying the action that
4311 has taken place. Its magnitude is in the range 0..height of the
4312 scroll bar. Negative values mean scroll towards buffer start.
4313 Values < height of scroll bar mean line-wise movement. */
4315 static void
4316 xaw_scroll_callback (widget, client_data, call_data)
4317 Widget widget;
4318 XtPointer client_data, call_data;
4320 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4321 /* The position really is stored cast to a pointer. */
4322 int position = (long) call_data;
4323 Dimension height;
4324 int part;
4326 /* Get the height of the scroll bar. */
4327 BLOCK_INPUT;
4328 XtVaGetValues (widget, XtNheight, &height, NULL);
4329 UNBLOCK_INPUT;
4331 if (abs (position) >= height)
4332 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4334 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4335 it maps line-movement to call_data = max(5, height/20). */
4336 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
4337 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4338 else
4339 part = scroll_bar_move_ratio;
4341 window_being_scrolled = bar->window;
4342 bar->dragging = Qnil;
4343 last_scroll_bar_part = part;
4344 x_send_scroll_bar_event (bar->window, part, position, height);
4347 #endif /* not USE_GTK */
4348 #endif /* not USE_MOTIF */
4350 #define SCROLL_BAR_NAME "verticalScrollBar"
4352 /* Create the widget for scroll bar BAR on frame F. Record the widget
4353 and X window of the scroll bar in BAR. */
4355 #ifdef USE_GTK
4356 static void
4357 x_create_toolkit_scroll_bar (f, bar)
4358 struct frame *f;
4359 struct scroll_bar *bar;
4361 char *scroll_bar_name = SCROLL_BAR_NAME;
4363 BLOCK_INPUT;
4364 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4365 scroll_bar_name);
4366 UNBLOCK_INPUT;
4369 #else /* not USE_GTK */
4371 static void
4372 x_create_toolkit_scroll_bar (f, bar)
4373 struct frame *f;
4374 struct scroll_bar *bar;
4376 Window xwindow;
4377 Widget widget;
4378 Arg av[20];
4379 int ac = 0;
4380 char *scroll_bar_name = SCROLL_BAR_NAME;
4381 unsigned long pixel;
4383 BLOCK_INPUT;
4385 #ifdef USE_MOTIF
4386 /* Set resources. Create the widget. */
4387 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4388 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4389 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4390 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4391 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4392 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4393 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4395 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4396 if (pixel != -1)
4398 XtSetArg (av[ac], XmNforeground, pixel);
4399 ++ac;
4402 pixel = f->output_data.x->scroll_bar_background_pixel;
4403 if (pixel != -1)
4405 XtSetArg (av[ac], XmNbackground, pixel);
4406 ++ac;
4409 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4410 scroll_bar_name, av, ac);
4412 /* Add one callback for everything that can happen. */
4413 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4414 (XtPointer) bar);
4415 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4416 (XtPointer) bar);
4417 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4418 (XtPointer) bar);
4419 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4420 (XtPointer) bar);
4421 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4422 (XtPointer) bar);
4423 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4424 (XtPointer) bar);
4425 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4426 (XtPointer) bar);
4428 /* Realize the widget. Only after that is the X window created. */
4429 XtRealizeWidget (widget);
4431 /* Set the cursor to an arrow. I didn't find a resource to do that.
4432 And I'm wondering why it hasn't an arrow cursor by default. */
4433 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4434 f->output_data.x->nontext_cursor);
4436 #else /* !USE_MOTIF i.e. use Xaw */
4438 /* Set resources. Create the widget. The background of the
4439 Xaw3d scroll bar widget is a little bit light for my taste.
4440 We don't alter it here to let users change it according
4441 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4442 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4443 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4444 /* For smoother scrolling with Xaw3d -sm */
4445 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4447 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4448 if (pixel != -1)
4450 XtSetArg (av[ac], XtNforeground, pixel);
4451 ++ac;
4454 pixel = f->output_data.x->scroll_bar_background_pixel;
4455 if (pixel != -1)
4457 XtSetArg (av[ac], XtNbackground, pixel);
4458 ++ac;
4461 /* Top/bottom shadow colors. */
4463 /* Allocate them, if necessary. */
4464 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4466 pixel = f->output_data.x->scroll_bar_background_pixel;
4467 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4468 &pixel, 1.2, 0x8000))
4469 pixel = -1;
4470 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4472 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4474 pixel = f->output_data.x->scroll_bar_background_pixel;
4475 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4476 &pixel, 0.6, 0x4000))
4477 pixel = -1;
4478 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4481 /* Tell the toolkit about them. */
4482 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4483 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4484 /* We tried to allocate a color for the top/bottom shadow, and
4485 failed, so tell Xaw3d to use dithering instead. */
4487 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4488 ++ac;
4490 else
4491 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4492 be more consistent with other emacs 3d colors, and since Xaw3d is
4493 not good at dealing with allocation failure. */
4495 /* This tells Xaw3d to use real colors instead of dithering for
4496 the shadows. */
4497 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4498 ++ac;
4500 /* Specify the colors. */
4501 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4502 if (pixel != -1)
4504 XtSetArg (av[ac], "topShadowPixel", pixel);
4505 ++ac;
4507 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4508 if (pixel != -1)
4510 XtSetArg (av[ac], "bottomShadowPixel", pixel);
4511 ++ac;
4515 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4516 f->output_data.x->edit_widget, av, ac);
4519 char *initial = "";
4520 char *val = initial;
4521 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4522 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4523 if (val == initial)
4524 { /* ARROW_SCROLL */
4525 xaw3d_arrow_scroll = True;
4526 /* Isn't that just a personal preference ? -sm */
4527 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4531 /* Define callbacks. */
4532 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4533 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4534 (XtPointer) bar);
4536 /* Realize the widget. Only after that is the X window created. */
4537 XtRealizeWidget (widget);
4539 #endif /* !USE_MOTIF */
4541 /* Install an action hook that lets us detect when the user
4542 finishes interacting with a scroll bar. */
4543 if (action_hook_id == 0)
4544 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4546 /* Remember X window and widget in the scroll bar vector. */
4547 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4548 xwindow = XtWindow (widget);
4549 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
4551 UNBLOCK_INPUT;
4553 #endif /* not USE_GTK */
4556 /* Set the thumb size and position of scroll bar BAR. We are currently
4557 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4559 #ifdef USE_GTK
4560 static void
4561 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4562 struct scroll_bar *bar;
4563 int portion, position, whole;
4565 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4568 #else /* not USE_GTK */
4569 static void
4570 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4571 struct scroll_bar *bar;
4572 int portion, position, whole;
4574 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4575 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4576 float top, shown;
4578 BLOCK_INPUT;
4580 #ifdef USE_MOTIF
4582 /* We use an estimate of 30 chars per line rather than the real
4583 `portion' value. This has the disadvantage that the thumb size
4584 is not very representative, but it makes our life a lot easier.
4585 Otherwise, we have to constantly adjust the thumb size, which
4586 we can't always do quickly enough: while dragging, the size of
4587 the thumb might prevent the user from dragging the thumb all the
4588 way to the end. but Motif and some versions of Xaw3d don't allow
4589 updating the thumb size while dragging. Also, even if we can update
4590 its size, the update will often happen too late.
4591 If you don't believe it, check out revision 1.650 of xterm.c to see
4592 what hoops we were going through and the still poor behavior we got. */
4593 portion = XFASTINT (XWINDOW (bar->window)->height) * 30;
4594 /* When the thumb is at the bottom, position == whole.
4595 So we need to increase `whole' to make space for the thumb. */
4596 whole += portion;
4598 if (whole <= 0)
4599 top = 0, shown = 1;
4600 else
4602 top = (float) position / whole;
4603 shown = (float) portion / whole;
4606 if (NILP (bar->dragging))
4608 int size, value;
4610 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4611 is the scroll bar's maximum and MIN is the scroll bar's minimum
4612 value. */
4613 size = shown * XM_SB_MAX;
4614 size = min (size, XM_SB_MAX);
4615 size = max (size, 1);
4617 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4618 value = top * XM_SB_MAX;
4619 value = min (value, XM_SB_MAX - size);
4621 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4623 #else /* !USE_MOTIF i.e. use Xaw */
4625 if (whole == 0)
4626 top = 0, shown = 1;
4627 else
4629 top = (float) position / whole;
4630 shown = (float) portion / whole;
4634 float old_top, old_shown;
4635 Dimension height;
4636 XtVaGetValues (widget,
4637 XtNtopOfThumb, &old_top,
4638 XtNshown, &old_shown,
4639 XtNheight, &height,
4640 NULL);
4642 /* Massage the top+shown values. */
4643 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4644 top = max (0, min (1, top));
4645 else
4646 top = old_top;
4647 /* Keep two pixels available for moving the thumb down. */
4648 shown = max (0, min (1 - top - (2.0 / height), shown));
4650 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4651 check that your system's configuration file contains a define
4652 for `NARROWPROTO'. See s/freebsd.h for an example. */
4653 if (top != old_top || shown != old_shown)
4655 if (NILP (bar->dragging))
4656 XawScrollbarSetThumb (widget, top, shown);
4657 else
4659 #ifdef HAVE_XAW3D
4660 ScrollbarWidget sb = (ScrollbarWidget) widget;
4661 int scroll_mode = 0;
4663 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
4664 if (xaw3d_arrow_scroll)
4666 /* Xaw3d stupidly ignores resize requests while dragging
4667 so we have to make it believe it's not in dragging mode. */
4668 scroll_mode = sb->scrollbar.scroll_mode;
4669 if (scroll_mode == 2)
4670 sb->scrollbar.scroll_mode = 0;
4672 #endif
4673 /* Try to make the scrolling a tad smoother. */
4674 if (!xaw3d_pick_top)
4675 shown = min (shown, old_shown);
4677 XawScrollbarSetThumb (widget, top, shown);
4679 #ifdef HAVE_XAW3D
4680 if (xaw3d_arrow_scroll && scroll_mode == 2)
4681 sb->scrollbar.scroll_mode = scroll_mode;
4682 #endif
4686 #endif /* !USE_MOTIF */
4688 UNBLOCK_INPUT;
4690 #endif /* not USE_GTK */
4692 #endif /* USE_TOOLKIT_SCROLL_BARS */
4696 /************************************************************************
4697 Scroll bars, general
4698 ************************************************************************/
4700 /* Create a scroll bar and return the scroll bar vector for it. W is
4701 the Emacs window on which to create the scroll bar. TOP, LEFT,
4702 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4703 scroll bar. */
4705 static struct scroll_bar *
4706 x_scroll_bar_create (w, top, left, width, height)
4707 struct window *w;
4708 int top, left, width, height;
4710 struct frame *f = XFRAME (w->frame);
4711 struct scroll_bar *bar
4712 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
4714 BLOCK_INPUT;
4716 #ifdef USE_TOOLKIT_SCROLL_BARS
4717 x_create_toolkit_scroll_bar (f, bar);
4718 #else /* not USE_TOOLKIT_SCROLL_BARS */
4720 XSetWindowAttributes a;
4721 unsigned long mask;
4722 Window window;
4724 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4725 if (a.background_pixel == -1)
4726 a.background_pixel = f->output_data.x->background_pixel;
4728 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4729 | ButtonMotionMask | PointerMotionHintMask
4730 | ExposureMask);
4731 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4733 mask = (CWBackPixel | CWEventMask | CWCursor);
4735 /* Clear the area of W that will serve as a scroll bar. This is
4736 for the case that a window has been split horizontally. In
4737 this case, no clear_frame is generated to reduce flickering. */
4738 if (width > 0 && height > 0)
4739 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4740 left, top, width,
4741 window_box_height (w), False);
4743 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4744 /* Position and size of scroll bar. */
4745 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4746 top,
4747 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4748 height,
4749 /* Border width, depth, class, and visual. */
4751 CopyFromParent,
4752 CopyFromParent,
4753 CopyFromParent,
4754 /* Attributes. */
4755 mask, &a);
4756 SET_SCROLL_BAR_X_WINDOW (bar, window);
4758 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4760 XSETWINDOW (bar->window, w);
4761 XSETINT (bar->top, top);
4762 XSETINT (bar->left, left);
4763 XSETINT (bar->width, width);
4764 XSETINT (bar->height, height);
4765 XSETINT (bar->start, 0);
4766 XSETINT (bar->end, 0);
4767 bar->dragging = Qnil;
4769 /* Add bar to its frame's list of scroll bars. */
4770 bar->next = FRAME_SCROLL_BARS (f);
4771 bar->prev = Qnil;
4772 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4773 if (!NILP (bar->next))
4774 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4776 /* Map the window/widget. */
4777 #ifdef USE_TOOLKIT_SCROLL_BARS
4779 #ifdef USE_GTK
4780 xg_update_scrollbar_pos (f,
4781 SCROLL_BAR_X_WINDOW (bar),
4782 top,
4783 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4784 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4785 max (height, 1),
4786 left,
4787 width);
4788 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
4789 #else /* not USE_GTK */
4790 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4791 XtConfigureWidget (scroll_bar,
4792 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4793 top,
4794 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4795 max (height, 1), 0);
4796 XtMapWidget (scroll_bar);
4797 #endif /* not USE_GTK */
4799 #else /* not USE_TOOLKIT_SCROLL_BARS */
4800 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4801 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4803 UNBLOCK_INPUT;
4804 return bar;
4808 /* Draw BAR's handle in the proper position.
4810 If the handle is already drawn from START to END, don't bother
4811 redrawing it, unless REBUILD is non-zero; in that case, always
4812 redraw it. (REBUILD is handy for drawing the handle after expose
4813 events.)
4815 Normally, we want to constrain the start and end of the handle to
4816 fit inside its rectangle, but if the user is dragging the scroll
4817 bar handle, we want to let them drag it down all the way, so that
4818 the bar's top is as far down as it goes; otherwise, there's no way
4819 to move to the very end of the buffer. */
4821 #ifndef USE_TOOLKIT_SCROLL_BARS
4823 static void
4824 x_scroll_bar_set_handle (bar, start, end, rebuild)
4825 struct scroll_bar *bar;
4826 int start, end;
4827 int rebuild;
4829 int dragging = ! NILP (bar->dragging);
4830 Window w = SCROLL_BAR_X_WINDOW (bar);
4831 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4832 GC gc = f->output_data.x->normal_gc;
4834 /* If the display is already accurate, do nothing. */
4835 if (! rebuild
4836 && start == XINT (bar->start)
4837 && end == XINT (bar->end))
4838 return;
4840 BLOCK_INPUT;
4843 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
4844 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
4845 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4847 /* Make sure the values are reasonable, and try to preserve
4848 the distance between start and end. */
4850 int length = end - start;
4852 if (start < 0)
4853 start = 0;
4854 else if (start > top_range)
4855 start = top_range;
4856 end = start + length;
4858 if (end < start)
4859 end = start;
4860 else if (end > top_range && ! dragging)
4861 end = top_range;
4864 /* Store the adjusted setting in the scroll bar. */
4865 XSETINT (bar->start, start);
4866 XSETINT (bar->end, end);
4868 /* Clip the end position, just for display. */
4869 if (end > top_range)
4870 end = top_range;
4872 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
4873 below top positions, to make sure the handle is always at least
4874 that many pixels tall. */
4875 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
4877 /* Draw the empty space above the handle. Note that we can't clear
4878 zero-height areas; that means "clear to end of window." */
4879 if (0 < start)
4880 x_clear_area (FRAME_X_DISPLAY (f), w,
4881 /* x, y, width, height, and exposures. */
4882 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4883 VERTICAL_SCROLL_BAR_TOP_BORDER,
4884 inside_width, start,
4885 False);
4887 /* Change to proper foreground color if one is specified. */
4888 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4889 XSetForeground (FRAME_X_DISPLAY (f), gc,
4890 f->output_data.x->scroll_bar_foreground_pixel);
4892 /* Draw the handle itself. */
4893 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
4894 /* x, y, width, height */
4895 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4896 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
4897 inside_width, end - start);
4899 /* Restore the foreground color of the GC if we changed it above. */
4900 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4901 XSetForeground (FRAME_X_DISPLAY (f), gc,
4902 f->output_data.x->foreground_pixel);
4904 /* Draw the empty space below the handle. Note that we can't
4905 clear zero-height areas; that means "clear to end of window." */
4906 if (end < inside_height)
4907 x_clear_area (FRAME_X_DISPLAY (f), w,
4908 /* x, y, width, height, and exposures. */
4909 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4910 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
4911 inside_width, inside_height - end,
4912 False);
4916 UNBLOCK_INPUT;
4919 #endif /* !USE_TOOLKIT_SCROLL_BARS */
4921 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
4922 nil. */
4924 static void
4925 x_scroll_bar_remove (bar)
4926 struct scroll_bar *bar;
4928 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4929 BLOCK_INPUT;
4931 #ifdef USE_TOOLKIT_SCROLL_BARS
4932 #ifdef USE_GTK
4933 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
4934 #else /* not USE_GTK */
4935 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
4936 #endif /* not USE_GTK */
4937 #else
4938 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4939 #endif
4941 /* Disassociate this scroll bar from its window. */
4942 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
4944 UNBLOCK_INPUT;
4948 /* Set the handle of the vertical scroll bar for WINDOW to indicate
4949 that we are displaying PORTION characters out of a total of WHOLE
4950 characters, starting at POSITION. If WINDOW has no scroll bar,
4951 create one. */
4953 static void
4954 XTset_vertical_scroll_bar (w, portion, whole, position)
4955 struct window *w;
4956 int portion, whole, position;
4958 struct frame *f = XFRAME (w->frame);
4959 struct scroll_bar *bar;
4960 int top, height, left, sb_left, width, sb_width;
4961 int window_x, window_y, window_width, window_height;
4963 /* Get window dimensions. */
4964 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
4965 top = window_y;
4966 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
4967 height = window_height;
4969 /* Compute the left edge of the scroll bar area. */
4970 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4971 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
4972 else
4973 left = XFASTINT (w->left);
4974 left *= CANON_X_UNIT (f);
4975 left += FRAME_INTERNAL_BORDER_WIDTH (f);
4977 /* Compute the width of the scroll bar which might be less than
4978 the width of the area reserved for the scroll bar. */
4979 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
4980 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
4981 else
4982 sb_width = width;
4984 /* Compute the left edge of the scroll bar. */
4985 #ifdef USE_TOOLKIT_SCROLL_BARS
4986 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4987 sb_left = left + width - sb_width - (width - sb_width) / 2;
4988 else
4989 sb_left = left + (width - sb_width) / 2;
4990 #else
4991 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4992 sb_left = left + width - sb_width;
4993 else
4994 sb_left = left;
4995 #endif
4997 /* Does the scroll bar exist yet? */
4998 if (NILP (w->vertical_scroll_bar))
5000 if (width > 0 && height > 0)
5002 BLOCK_INPUT;
5003 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5004 left, top, width, height, False);
5005 UNBLOCK_INPUT;
5008 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5010 else
5012 /* It may just need to be moved and resized. */
5013 unsigned int mask = 0;
5015 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5017 BLOCK_INPUT;
5019 if (sb_left != XINT (bar->left))
5020 mask |= CWX;
5021 if (top != XINT (bar->top))
5022 mask |= CWY;
5023 if (sb_width != XINT (bar->width))
5024 mask |= CWWidth;
5025 if (height != XINT (bar->height))
5026 mask |= CWHeight;
5028 #ifdef USE_TOOLKIT_SCROLL_BARS
5030 #ifdef USE_GTK
5031 if (mask)
5032 xg_update_scrollbar_pos (f,
5033 SCROLL_BAR_X_WINDOW (bar),
5034 top,
5035 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5036 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5037 max (height, 1),
5038 left,
5039 width);
5040 #else /* not USE_GTK */
5042 /* Since toolkit scroll bars are smaller than the space reserved
5043 for them on the frame, we have to clear "under" them. */
5044 if (width > 0 && height > 0)
5045 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5046 left, top, width, height, False);
5047 /* Move/size the scroll bar widget. */
5048 if (mask)
5049 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5050 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5051 top,
5052 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5053 max (height, 1), 0);
5055 #endif /* not USE_GTK */
5056 #else /* not USE_TOOLKIT_SCROLL_BARS */
5058 /* Clear areas not covered by the scroll bar because of
5059 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5060 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5062 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5063 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5064 height, False);
5065 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5066 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5067 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5068 height, False);
5071 /* Clear areas not covered by the scroll bar because it's not as
5072 wide as the area reserved for it. This makes sure a
5073 previous mode line display is cleared after C-x 2 C-x 1, for
5074 example. */
5076 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
5077 int rest = area_width - sb_width;
5078 if (rest > 0 && height > 0)
5080 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
5081 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5082 left + area_width - rest, top,
5083 rest, height, False);
5084 else
5085 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5086 left, top, rest, height, False);
5090 /* Move/size the scroll bar window. */
5091 if (mask)
5093 XWindowChanges wc;
5095 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5096 wc.y = top;
5097 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5098 wc.height = height;
5099 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
5100 mask, &wc);
5103 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5105 /* Remember new settings. */
5106 XSETINT (bar->left, sb_left);
5107 XSETINT (bar->top, top);
5108 XSETINT (bar->width, sb_width);
5109 XSETINT (bar->height, height);
5111 UNBLOCK_INPUT;
5114 #ifdef USE_TOOLKIT_SCROLL_BARS
5115 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5116 #else /* not USE_TOOLKIT_SCROLL_BARS */
5117 /* Set the scroll bar's current state, unless we're currently being
5118 dragged. */
5119 if (NILP (bar->dragging))
5121 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5123 if (whole == 0)
5124 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5125 else
5127 int start = ((double) position * top_range) / whole;
5128 int end = ((double) (position + portion) * top_range) / whole;
5129 x_scroll_bar_set_handle (bar, start, end, 0);
5132 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5134 XSETVECTOR (w->vertical_scroll_bar, bar);
5138 /* The following three hooks are used when we're doing a thorough
5139 redisplay of the frame. We don't explicitly know which scroll bars
5140 are going to be deleted, because keeping track of when windows go
5141 away is a real pain - "Can you say set-window-configuration, boys
5142 and girls?" Instead, we just assert at the beginning of redisplay
5143 that *all* scroll bars are to be removed, and then save a scroll bar
5144 from the fiery pit when we actually redisplay its window. */
5146 /* Arrange for all scroll bars on FRAME to be removed at the next call
5147 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5148 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5150 static void
5151 XTcondemn_scroll_bars (frame)
5152 FRAME_PTR frame;
5154 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5155 while (! NILP (FRAME_SCROLL_BARS (frame)))
5157 Lisp_Object bar;
5158 bar = FRAME_SCROLL_BARS (frame);
5159 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5160 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5161 XSCROLL_BAR (bar)->prev = Qnil;
5162 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5163 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5164 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5169 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5170 Note that WINDOW isn't necessarily condemned at all. */
5172 static void
5173 XTredeem_scroll_bar (window)
5174 struct window *window;
5176 struct scroll_bar *bar;
5177 struct frame *f;
5179 /* We can't redeem this window's scroll bar if it doesn't have one. */
5180 if (NILP (window->vertical_scroll_bar))
5181 abort ();
5183 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5185 /* Unlink it from the condemned list. */
5186 f = XFRAME (WINDOW_FRAME (window));
5187 if (NILP (bar->prev))
5189 /* If the prev pointer is nil, it must be the first in one of
5190 the lists. */
5191 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5192 /* It's not condemned. Everything's fine. */
5193 return;
5194 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5195 window->vertical_scroll_bar))
5196 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5197 else
5198 /* If its prev pointer is nil, it must be at the front of
5199 one or the other! */
5200 abort ();
5202 else
5203 XSCROLL_BAR (bar->prev)->next = bar->next;
5205 if (! NILP (bar->next))
5206 XSCROLL_BAR (bar->next)->prev = bar->prev;
5208 bar->next = FRAME_SCROLL_BARS (f);
5209 bar->prev = Qnil;
5210 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5211 if (! NILP (bar->next))
5212 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5215 /* Remove all scroll bars on FRAME that haven't been saved since the
5216 last call to `*condemn_scroll_bars_hook'. */
5218 static void
5219 XTjudge_scroll_bars (f)
5220 FRAME_PTR f;
5222 Lisp_Object bar, next;
5224 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5226 /* Clear out the condemned list now so we won't try to process any
5227 more events on the hapless scroll bars. */
5228 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5230 for (; ! NILP (bar); bar = next)
5232 struct scroll_bar *b = XSCROLL_BAR (bar);
5234 x_scroll_bar_remove (b);
5236 next = b->next;
5237 b->next = b->prev = Qnil;
5240 /* Now there should be no references to the condemned scroll bars,
5241 and they should get garbage-collected. */
5245 #ifndef USE_TOOLKIT_SCROLL_BARS
5246 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5247 is a no-op when using toolkit scroll bars.
5249 This may be called from a signal handler, so we have to ignore GC
5250 mark bits. */
5252 static void
5253 x_scroll_bar_expose (bar, event)
5254 struct scroll_bar *bar;
5255 XEvent *event;
5257 Window w = SCROLL_BAR_X_WINDOW (bar);
5258 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5259 GC gc = f->output_data.x->normal_gc;
5260 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5262 BLOCK_INPUT;
5264 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
5266 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5267 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5269 /* x, y, width, height */
5270 0, 0,
5271 XINT (bar->width) - 1 - width_trim - width_trim,
5272 XINT (bar->height) - 1);
5274 UNBLOCK_INPUT;
5277 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5279 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5280 is set to something other than NO_EVENT, it is enqueued.
5282 This may be called from a signal handler, so we have to ignore GC
5283 mark bits. */
5286 static void
5287 x_scroll_bar_handle_click (bar, event, emacs_event)
5288 struct scroll_bar *bar;
5289 XEvent *event;
5290 struct input_event *emacs_event;
5292 if (! GC_WINDOWP (bar->window))
5293 abort ();
5295 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5296 emacs_event->code = event->xbutton.button - Button1;
5297 emacs_event->modifiers
5298 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5299 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5300 event->xbutton.state)
5301 | (event->type == ButtonRelease
5302 ? up_modifier
5303 : down_modifier));
5304 emacs_event->frame_or_window = bar->window;
5305 emacs_event->arg = Qnil;
5306 emacs_event->timestamp = event->xbutton.time;
5308 #if 0
5309 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5310 int internal_height
5311 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5312 #endif
5313 int top_range
5314 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5315 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5317 if (y < 0) y = 0;
5318 if (y > top_range) y = top_range;
5320 if (y < XINT (bar->start))
5321 emacs_event->part = scroll_bar_above_handle;
5322 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5323 emacs_event->part = scroll_bar_handle;
5324 else
5325 emacs_event->part = scroll_bar_below_handle;
5327 /* Just because the user has clicked on the handle doesn't mean
5328 they want to drag it. Lisp code needs to be able to decide
5329 whether or not we're dragging. */
5330 #if 0
5331 /* If the user has just clicked on the handle, record where they're
5332 holding it. */
5333 if (event->type == ButtonPress
5334 && emacs_event->part == scroll_bar_handle)
5335 XSETINT (bar->dragging, y - XINT (bar->start));
5336 #endif
5338 #ifndef USE_TOOLKIT_SCROLL_BARS
5339 /* If the user has released the handle, set it to its final position. */
5340 if (event->type == ButtonRelease
5341 && ! NILP (bar->dragging))
5343 int new_start = y - XINT (bar->dragging);
5344 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5346 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5347 bar->dragging = Qnil;
5349 #endif
5351 /* Same deal here as the other #if 0. */
5352 #if 0
5353 /* Clicks on the handle are always reported as occurring at the top of
5354 the handle. */
5355 if (emacs_event->part == scroll_bar_handle)
5356 emacs_event->x = bar->start;
5357 else
5358 XSETINT (emacs_event->x, y);
5359 #else
5360 XSETINT (emacs_event->x, y);
5361 #endif
5363 XSETINT (emacs_event->y, top_range);
5367 #ifndef USE_TOOLKIT_SCROLL_BARS
5369 /* Handle some mouse motion while someone is dragging the scroll bar.
5371 This may be called from a signal handler, so we have to ignore GC
5372 mark bits. */
5374 static void
5375 x_scroll_bar_note_movement (bar, event)
5376 struct scroll_bar *bar;
5377 XEvent *event;
5379 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5381 last_mouse_movement_time = event->xmotion.time;
5383 f->mouse_moved = 1;
5384 XSETVECTOR (last_mouse_scroll_bar, bar);
5386 /* If we're dragging the bar, display it. */
5387 if (! GC_NILP (bar->dragging))
5389 /* Where should the handle be now? */
5390 int new_start = event->xmotion.y - XINT (bar->dragging);
5392 if (new_start != XINT (bar->start))
5394 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5396 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5401 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5403 /* Return information to the user about the current position of the mouse
5404 on the scroll bar. */
5406 static void
5407 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5408 FRAME_PTR *fp;
5409 Lisp_Object *bar_window;
5410 enum scroll_bar_part *part;
5411 Lisp_Object *x, *y;
5412 unsigned long *time;
5414 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5415 Window w = SCROLL_BAR_X_WINDOW (bar);
5416 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5417 int win_x, win_y;
5418 Window dummy_window;
5419 int dummy_coord;
5420 unsigned int dummy_mask;
5422 BLOCK_INPUT;
5424 /* Get the mouse's position relative to the scroll bar window, and
5425 report that. */
5426 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5428 /* Root, child, root x and root y. */
5429 &dummy_window, &dummy_window,
5430 &dummy_coord, &dummy_coord,
5432 /* Position relative to scroll bar. */
5433 &win_x, &win_y,
5435 /* Mouse buttons and modifier keys. */
5436 &dummy_mask))
5438 else
5440 #if 0
5441 int inside_height
5442 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5443 #endif
5444 int top_range
5445 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5447 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5449 if (! NILP (bar->dragging))
5450 win_y -= XINT (bar->dragging);
5452 if (win_y < 0)
5453 win_y = 0;
5454 if (win_y > top_range)
5455 win_y = top_range;
5457 *fp = f;
5458 *bar_window = bar->window;
5460 if (! NILP (bar->dragging))
5461 *part = scroll_bar_handle;
5462 else if (win_y < XINT (bar->start))
5463 *part = scroll_bar_above_handle;
5464 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5465 *part = scroll_bar_handle;
5466 else
5467 *part = scroll_bar_below_handle;
5469 XSETINT (*x, win_y);
5470 XSETINT (*y, top_range);
5472 f->mouse_moved = 0;
5473 last_mouse_scroll_bar = Qnil;
5476 *time = last_mouse_movement_time;
5478 UNBLOCK_INPUT;
5482 /* The screen has been cleared so we may have changed foreground or
5483 background colors, and the scroll bars may need to be redrawn.
5484 Clear out the scroll bars, and ask for expose events, so we can
5485 redraw them. */
5487 void
5488 x_scroll_bar_clear (f)
5489 FRAME_PTR f;
5491 #ifndef USE_TOOLKIT_SCROLL_BARS
5492 Lisp_Object bar;
5494 /* We can have scroll bars even if this is 0,
5495 if we just turned off scroll bar mode.
5496 But in that case we should not clear them. */
5497 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5498 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5499 bar = XSCROLL_BAR (bar)->next)
5500 XClearArea (FRAME_X_DISPLAY (f),
5501 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
5502 0, 0, 0, 0, True);
5503 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5507 /* Define a queue to save up SelectionRequest events for later handling. */
5509 struct selection_event_queue
5511 XEvent event;
5512 struct selection_event_queue *next;
5515 static struct selection_event_queue *queue;
5517 /* Nonzero means queue up certain events--don't process them yet. */
5519 static int x_queue_selection_requests;
5521 /* Queue up an X event *EVENT, to be processed later. */
5523 static void
5524 x_queue_event (f, event)
5525 FRAME_PTR f;
5526 XEvent *event;
5528 struct selection_event_queue *queue_tmp
5529 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
5531 if (queue_tmp != NULL)
5533 queue_tmp->event = *event;
5534 queue_tmp->next = queue;
5535 queue = queue_tmp;
5539 /* Take all the queued events and put them back
5540 so that they get processed afresh. */
5542 static void
5543 x_unqueue_events (display)
5544 Display *display;
5546 while (queue != NULL)
5548 struct selection_event_queue *queue_tmp = queue;
5549 XPutBackEvent (display, &queue_tmp->event);
5550 queue = queue_tmp->next;
5551 xfree ((char *)queue_tmp);
5555 /* Start queuing SelectionRequest events. */
5557 void
5558 x_start_queuing_selection_requests (display)
5559 Display *display;
5561 x_queue_selection_requests++;
5564 /* Stop queuing SelectionRequest events. */
5566 void
5567 x_stop_queuing_selection_requests (display)
5568 Display *display;
5570 x_queue_selection_requests--;
5571 x_unqueue_events (display);
5574 /* The main X event-reading loop - XTread_socket. */
5576 #if 0
5577 /* Time stamp of enter window event. This is only used by XTread_socket,
5578 but we have to put it out here, since static variables within functions
5579 sometimes don't work. */
5581 static Time enter_timestamp;
5582 #endif
5584 /* This holds the state XLookupString needs to implement dead keys
5585 and other tricks known as "compose processing". _X Window System_
5586 says that a portable program can't use this, but Stephen Gildea assures
5587 me that letting the compiler initialize it to zeros will work okay.
5589 This must be defined outside of XTread_socket, for the same reasons
5590 given for enter_timestamp, above. */
5592 static XComposeStatus compose_status;
5594 /* Record the last 100 characters stored
5595 to help debug the loss-of-chars-during-GC problem. */
5597 static int temp_index;
5598 static short temp_buffer[100];
5600 /* Set this to nonzero to fake an "X I/O error"
5601 on a particular display. */
5603 struct x_display_info *XTread_socket_fake_io_error;
5605 /* When we find no input here, we occasionally do a no-op command
5606 to verify that the X server is still running and we can still talk with it.
5607 We try all the open displays, one by one.
5608 This variable is used for cycling thru the displays. */
5610 static struct x_display_info *next_noop_dpyinfo;
5612 #define SET_SAVED_MENU_EVENT(size) \
5613 do \
5615 if (f->output_data.x->saved_menu_event == 0) \
5616 f->output_data.x->saved_menu_event \
5617 = (XEvent *) xmalloc (sizeof (XEvent)); \
5618 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5619 if (numchars >= 1) \
5621 bufp->kind = MENU_BAR_ACTIVATE_EVENT; \
5622 XSETFRAME (bufp->frame_or_window, f); \
5623 bufp->arg = Qnil; \
5624 bufp++; \
5625 count++; \
5626 numchars--; \
5629 while (0)
5631 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5632 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5635 enum
5637 X_EVENT_NORMAL,
5638 X_EVENT_GOTO_OUT,
5639 X_EVENT_DROP
5642 /* Filter events for the current X input method.
5643 DPYINFO is the display this event is for.
5644 EVENT is the X event to filter.
5646 Returns non-zero if the event was filtered, caller shall not process
5647 this event further.
5648 Returns zero if event is wasn't filtered. */
5650 #ifdef HAVE_X_I18N
5651 static int
5652 x_filter_event (dpyinfo, event)
5653 struct x_display_info *dpyinfo;
5654 XEvent *event;
5656 /* XFilterEvent returns non-zero if the input method has
5657 consumed the event. We pass the frame's X window to
5658 XFilterEvent because that's the one for which the IC
5659 was created. */
5661 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5662 event->xclient.window);
5664 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5666 #endif
5668 #ifdef USE_GTK
5669 static struct x_display_info *current_dpyinfo;
5670 static struct input_event **current_bufp;
5671 static int *current_numcharsp;
5672 static int current_count;
5673 static int current_finish;
5675 /* This is the filter function invoked by the GTK event loop.
5676 It is invoked before the XEvent is translated to a GdkEvent,
5677 so we have a chanse to act on the event before GTK. */
5678 static GdkFilterReturn
5679 event_handler_gdk (gxev, ev, data)
5680 GdkXEvent *gxev;
5681 GdkEvent *ev;
5682 gpointer data;
5684 XEvent *xev = (XEvent*)gxev;
5686 if (current_numcharsp)
5688 #ifdef HAVE_X_I18N
5689 /* Filter events for the current X input method.
5690 GTK calls XFilterEvent but not for key press and release,
5691 so we do it here. */
5692 if (xev->type == KeyPress || xev->type == KeyRelease)
5693 if (x_filter_event (current_dpyinfo, xev))
5694 return GDK_FILTER_REMOVE;
5695 #endif
5696 current_count += handle_one_xevent (current_dpyinfo,
5697 xev,
5698 current_bufp,
5699 current_numcharsp,
5700 &current_finish);
5702 else
5703 current_finish = x_dispatch_event (xev, GDK_DISPLAY ());
5705 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5706 return GDK_FILTER_REMOVE;
5708 return GDK_FILTER_CONTINUE;
5710 #endif /* USE_GTK */
5713 /* Handles the XEvent EVENT on display DPYINFO.
5715 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5716 *FINISH is zero if caller should continue reading events.
5717 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5719 Events representing keys are stored in buffer *BUFP_R,
5720 which can hold up to *NUMCHARSP characters.
5721 We return the number of characters stored into the buffer. */
5723 static int
5724 handle_one_xevent (dpyinfo, eventp, bufp_r, numcharsp, finish)
5725 struct x_display_info *dpyinfo;
5726 XEvent *eventp;
5727 /* register */ struct input_event **bufp_r;
5728 /* register */ int *numcharsp;
5729 int *finish;
5731 int count = 0;
5732 int nbytes = 0;
5733 struct frame *f;
5734 struct coding_system coding;
5735 struct input_event *bufp = *bufp_r;
5736 int numchars = *numcharsp;
5737 XEvent event = *eventp;
5739 *finish = X_EVENT_NORMAL;
5741 switch (event.type)
5743 case ClientMessage:
5745 if (event.xclient.message_type
5746 == dpyinfo->Xatom_wm_protocols
5747 && event.xclient.format == 32)
5749 if (event.xclient.data.l[0]
5750 == dpyinfo->Xatom_wm_take_focus)
5752 /* Use x_any_window_to_frame because this
5753 could be the shell widget window
5754 if the frame has no title bar. */
5755 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5756 #ifdef HAVE_X_I18N
5757 /* Not quite sure this is needed -pd */
5758 if (f && FRAME_XIC (f))
5759 XSetICFocus (FRAME_XIC (f));
5760 #endif
5761 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5762 instructs the WM to set the input focus automatically for
5763 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5764 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5765 it has set the focus. So, XSetInputFocus below is not
5766 needed.
5768 The call to XSetInputFocus below has also caused trouble. In
5769 cases where the XSetInputFocus done by the WM and the one
5770 below are temporally close (on a fast machine), the call
5771 below can generate additional FocusIn events which confuse
5772 Emacs. */
5774 /* Since we set WM_TAKE_FOCUS, we must call
5775 XSetInputFocus explicitly. But not if f is null,
5776 since that might be an event for a deleted frame. */
5777 if (f)
5779 Display *d = event.xclient.display;
5780 /* Catch and ignore errors, in case window has been
5781 iconified by a window manager such as GWM. */
5782 int count = x_catch_errors (d);
5783 XSetInputFocus (d, event.xclient.window,
5784 /* The ICCCM says this is
5785 the only valid choice. */
5786 RevertToParent,
5787 event.xclient.data.l[1]);
5788 /* This is needed to detect the error
5789 if there is an error. */
5790 XSync (d, False);
5791 x_uncatch_errors (d, count);
5793 /* Not certain about handling scroll bars here */
5794 #endif /* 0 */
5796 else if (event.xclient.data.l[0]
5797 == dpyinfo->Xatom_wm_save_yourself)
5799 /* Save state modify the WM_COMMAND property to
5800 something which can reinstate us. This notifies
5801 the session manager, who's looking for such a
5802 PropertyNotify. Can restart processing when
5803 a keyboard or mouse event arrives. */
5804 /* If we have a session manager, don't set this.
5805 KDE will then start two Emacsen, one for the
5806 session manager and one for this. */
5807 if (numchars > 0
5808 #ifdef HAVE_X_SM
5809 && ! x_session_have_connection ()
5810 #endif
5813 f = x_top_window_to_frame (dpyinfo,
5814 event.xclient.window);
5815 /* This is just so we only give real data once
5816 for a single Emacs process. */
5817 if (f == SELECTED_FRAME ())
5818 XSetCommand (FRAME_X_DISPLAY (f),
5819 event.xclient.window,
5820 initial_argv, initial_argc);
5821 else if (f)
5822 XSetCommand (FRAME_X_DISPLAY (f),
5823 event.xclient.window,
5824 0, 0);
5827 else if (event.xclient.data.l[0]
5828 == dpyinfo->Xatom_wm_delete_window)
5830 struct frame *f
5831 = x_any_window_to_frame (dpyinfo,
5832 event.xclient.window);
5834 if (f)
5836 if (numchars == 0)
5837 abort ();
5839 bufp->kind = DELETE_WINDOW_EVENT;
5840 XSETFRAME (bufp->frame_or_window, f);
5841 bufp->arg = Qnil;
5842 bufp++;
5844 count += 1;
5845 numchars -= 1;
5847 else
5848 goto OTHER; /* May be a dialog that is to be removed */
5851 else if (event.xclient.message_type
5852 == dpyinfo->Xatom_wm_configure_denied)
5855 else if (event.xclient.message_type
5856 == dpyinfo->Xatom_wm_window_moved)
5858 int new_x, new_y;
5859 struct frame *f
5860 = x_window_to_frame (dpyinfo, event.xclient.window);
5862 new_x = event.xclient.data.s[0];
5863 new_y = event.xclient.data.s[1];
5865 if (f)
5867 f->output_data.x->left_pos = new_x;
5868 f->output_data.x->top_pos = new_y;
5871 #ifdef HACK_EDITRES
5872 else if (event.xclient.message_type
5873 == dpyinfo->Xatom_editres)
5875 struct frame *f
5876 = x_any_window_to_frame (dpyinfo, event.xclient.window);
5877 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5878 &event, NULL);
5880 #endif /* HACK_EDITRES */
5881 else if ((event.xclient.message_type
5882 == dpyinfo->Xatom_DONE)
5883 || (event.xclient.message_type
5884 == dpyinfo->Xatom_PAGE))
5886 /* Ghostview job completed. Kill it. We could
5887 reply with "Next" if we received "Page", but we
5888 currently never do because we are interested in
5889 images, only, which should have 1 page. */
5890 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5891 struct frame *f
5892 = x_window_to_frame (dpyinfo, event.xclient.window);
5893 x_kill_gs_process (pixmap, f);
5894 expose_frame (f, 0, 0, 0, 0);
5896 #ifdef USE_TOOLKIT_SCROLL_BARS
5897 /* Scroll bar callbacks send a ClientMessage from which
5898 we construct an input_event. */
5899 else if (event.xclient.message_type
5900 == dpyinfo->Xatom_Scrollbar)
5902 x_scroll_bar_to_input_event (&event, bufp);
5903 ++bufp, ++count, --numchars;
5904 goto out;
5906 #endif /* USE_TOOLKIT_SCROLL_BARS */
5907 else
5908 goto OTHER;
5910 break;
5912 case SelectionNotify:
5913 #ifdef USE_X_TOOLKIT
5914 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
5915 goto OTHER;
5916 #endif /* not USE_X_TOOLKIT */
5917 x_handle_selection_notify (&event.xselection);
5918 break;
5920 case SelectionClear: /* Someone has grabbed ownership. */
5921 #ifdef USE_X_TOOLKIT
5922 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
5923 goto OTHER;
5924 #endif /* USE_X_TOOLKIT */
5926 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
5928 if (numchars == 0)
5929 abort ();
5931 bufp->kind = SELECTION_CLEAR_EVENT;
5932 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
5933 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
5934 SELECTION_EVENT_TIME (bufp) = eventp->time;
5935 bufp->frame_or_window = Qnil;
5936 bufp->arg = Qnil;
5937 bufp++;
5939 count += 1;
5940 numchars -= 1;
5942 break;
5944 case SelectionRequest: /* Someone wants our selection. */
5945 #ifdef USE_X_TOOLKIT
5946 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
5947 goto OTHER;
5948 #endif /* USE_X_TOOLKIT */
5949 if (x_queue_selection_requests)
5950 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
5951 &event);
5952 else
5954 XSelectionRequestEvent *eventp
5955 = (XSelectionRequestEvent *) &event;
5957 if (numchars == 0)
5958 abort ();
5960 bufp->kind = SELECTION_REQUEST_EVENT;
5961 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
5962 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
5963 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
5964 SELECTION_EVENT_TARGET (bufp) = eventp->target;
5965 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
5966 SELECTION_EVENT_TIME (bufp) = eventp->time;
5967 bufp->frame_or_window = Qnil;
5968 bufp->arg = Qnil;
5969 bufp++;
5971 count += 1;
5972 numchars -= 1;
5974 break;
5976 case PropertyNotify:
5977 #if 0 /* This is plain wrong. In the case that we are waiting for a
5978 PropertyNotify used as an ACK in incremental selection
5979 transfer, the property will be on the receiver's window. */
5980 #if defined USE_X_TOOLKIT
5981 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
5982 goto OTHER;
5983 #endif
5984 #endif
5985 x_handle_property_notify (&event.xproperty);
5986 goto OTHER;
5988 case ReparentNotify:
5989 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
5990 if (f)
5992 int x, y;
5993 f->output_data.x->parent_desc = event.xreparent.parent;
5994 x_real_positions (f, &x, &y);
5995 f->output_data.x->left_pos = x;
5996 f->output_data.x->top_pos = y;
5998 goto OTHER;
5999 break;
6001 case Expose:
6002 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6003 if (f)
6005 x_check_fullscreen (f);
6007 if (f->async_visible == 0)
6009 f->async_visible = 1;
6010 f->async_iconified = 0;
6011 f->output_data.x->has_been_visible = 1;
6012 SET_FRAME_GARBAGED (f);
6014 else
6015 expose_frame (x_window_to_frame (dpyinfo,
6016 event.xexpose.window),
6017 event.xexpose.x, event.xexpose.y,
6018 event.xexpose.width, event.xexpose.height);
6020 else
6022 #ifndef USE_TOOLKIT_SCROLL_BARS
6023 struct scroll_bar *bar;
6024 #endif
6025 #if defined USE_LUCID
6026 /* Submenus of the Lucid menu bar aren't widgets
6027 themselves, so there's no way to dispatch events
6028 to them. Recognize this case separately. */
6030 Widget widget
6031 = x_window_to_menu_bar (event.xexpose.window);
6032 if (widget)
6033 xlwmenu_redisplay (widget);
6035 #endif /* USE_LUCID */
6037 #ifdef USE_TOOLKIT_SCROLL_BARS
6038 /* Dispatch event to the widget. */
6039 goto OTHER;
6040 #else /* not USE_TOOLKIT_SCROLL_BARS */
6041 bar = x_window_to_scroll_bar (event.xexpose.window);
6043 if (bar)
6044 x_scroll_bar_expose (bar, &event);
6045 #ifdef USE_X_TOOLKIT
6046 else
6047 goto OTHER;
6048 #endif /* USE_X_TOOLKIT */
6049 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6051 break;
6053 case GraphicsExpose: /* This occurs when an XCopyArea's
6054 source area was obscured or not
6055 available. */
6056 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6057 if (f)
6059 expose_frame (f,
6060 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6061 event.xgraphicsexpose.width,
6062 event.xgraphicsexpose.height);
6064 #ifdef USE_X_TOOLKIT
6065 else
6066 goto OTHER;
6067 #endif /* USE_X_TOOLKIT */
6068 break;
6070 case NoExpose: /* This occurs when an XCopyArea's
6071 source area was completely
6072 available. */
6073 break;
6075 case UnmapNotify:
6076 /* Redo the mouse-highlight after the tooltip has gone. */
6077 if (event.xmap.window == tip_window)
6079 tip_window = 0;
6080 redo_mouse_highlight ();
6083 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6084 if (f) /* F may no longer exist if
6085 the frame was deleted. */
6087 /* While a frame is unmapped, display generation is
6088 disabled; you don't want to spend time updating a
6089 display that won't ever be seen. */
6090 f->async_visible = 0;
6091 /* We can't distinguish, from the event, whether the window
6092 has become iconified or invisible. So assume, if it
6093 was previously visible, than now it is iconified.
6094 But x_make_frame_invisible clears both
6095 the visible flag and the iconified flag;
6096 and that way, we know the window is not iconified now. */
6097 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6099 f->async_iconified = 1;
6101 bufp->kind = ICONIFY_EVENT;
6102 XSETFRAME (bufp->frame_or_window, f);
6103 bufp->arg = Qnil;
6104 bufp++;
6105 count++;
6106 numchars--;
6109 goto OTHER;
6111 case MapNotify:
6112 if (event.xmap.window == tip_window)
6113 /* The tooltip has been drawn already. Avoid
6114 the SET_FRAME_GARBAGED below. */
6115 goto OTHER;
6117 /* We use x_top_window_to_frame because map events can
6118 come for sub-windows and they don't mean that the
6119 frame is visible. */
6120 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6121 if (f)
6123 /* wait_reading_process_input will notice this and update
6124 the frame's display structures.
6125 If we where iconified, we should not set garbaged,
6126 because that stops redrawing on Expose events. This looks
6127 bad if we are called from a recursive event loop
6128 (x_dispatch_event), for example when a dialog is up. */
6129 if (! f->async_iconified)
6130 SET_FRAME_GARBAGED (f);
6132 f->async_visible = 1;
6133 f->async_iconified = 0;
6134 f->output_data.x->has_been_visible = 1;
6136 if (f->iconified)
6138 bufp->kind = DEICONIFY_EVENT;
6139 XSETFRAME (bufp->frame_or_window, f);
6140 bufp->arg = Qnil;
6141 bufp++;
6142 count++;
6143 numchars--;
6145 else if (! NILP (Vframe_list)
6146 && ! NILP (XCDR (Vframe_list)))
6147 /* Force a redisplay sooner or later
6148 to update the frame titles
6149 in case this is the second frame. */
6150 record_asynch_buffer_change ();
6152 goto OTHER;
6154 case KeyPress:
6156 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6157 /* Dispatch KeyPress events when in menu. */
6158 if (popup_activated ())
6159 goto OTHER;
6160 #endif
6162 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6164 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
6166 dpyinfo->mouse_face_hidden = 1;
6167 clear_mouse_face (dpyinfo);
6170 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6171 if (f == 0)
6173 /* Scroll bars consume key events, but we want
6174 the keys to go to the scroll bar's frame. */
6175 Widget widget = XtWindowToWidget (dpyinfo->display,
6176 event.xkey.window);
6177 if (widget && XmIsScrollBar (widget))
6179 widget = XtParent (widget);
6180 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6183 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6185 if (f != 0)
6187 KeySym keysym, orig_keysym;
6188 /* al%imercury@uunet.uu.net says that making this 81
6189 instead of 80 fixed a bug whereby meta chars made
6190 his Emacs hang.
6192 It seems that some version of XmbLookupString has
6193 a bug of not returning XBufferOverflow in
6194 status_return even if the input is too long to
6195 fit in 81 bytes. So, we must prepare sufficient
6196 bytes for copy_buffer. 513 bytes (256 chars for
6197 two-byte character set) seems to be a fairly good
6198 approximation. -- 2000.8.10 handa@etl.go.jp */
6199 unsigned char copy_buffer[513];
6200 unsigned char *copy_bufptr = copy_buffer;
6201 int copy_bufsiz = sizeof (copy_buffer);
6202 int modifiers;
6203 Lisp_Object coding_system = Qlatin_1;
6205 event.xkey.state
6206 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6207 extra_keyboard_modifiers);
6208 modifiers = event.xkey.state;
6210 /* This will have to go some day... */
6212 /* make_lispy_event turns chars into control chars.
6213 Don't do it here because XLookupString is too eager. */
6214 event.xkey.state &= ~ControlMask;
6215 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6216 | dpyinfo->super_mod_mask
6217 | dpyinfo->hyper_mod_mask
6218 | dpyinfo->alt_mod_mask);
6220 /* In case Meta is ComposeCharacter,
6221 clear its status. According to Markus Ehrnsperger
6222 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6223 this enables ComposeCharacter to work whether or
6224 not it is combined with Meta. */
6225 if (modifiers & dpyinfo->meta_mod_mask)
6226 bzero (&compose_status, sizeof (compose_status));
6228 #ifdef HAVE_X_I18N
6229 if (FRAME_XIC (f))
6231 Status status_return;
6233 coding_system = Vlocale_coding_system;
6234 nbytes = XmbLookupString (FRAME_XIC (f),
6235 &event.xkey, copy_bufptr,
6236 copy_bufsiz, &keysym,
6237 &status_return);
6238 if (status_return == XBufferOverflow)
6240 copy_bufsiz = nbytes + 1;
6241 copy_bufptr = (char *) alloca (copy_bufsiz);
6242 nbytes = XmbLookupString (FRAME_XIC (f),
6243 &event.xkey, copy_bufptr,
6244 copy_bufsiz, &keysym,
6245 &status_return);
6247 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6248 #if 0 && defined X_HAVE_UTF8_STRING
6249 else if (status_return == XLookupKeySym)
6250 { /* Try again but with utf-8. */
6251 coding_system = Qutf_8;
6252 nbytes = Xutf8LookupString (FRAME_XIC (f),
6253 &event.xkey, copy_bufptr,
6254 copy_bufsiz, &keysym,
6255 &status_return);
6256 if (status_return == XBufferOverflow)
6258 copy_bufsiz = nbytes + 1;
6259 copy_bufptr = (char *) alloca (copy_bufsiz);
6260 nbytes = Xutf8LookupString (FRAME_XIC (f),
6261 &event.xkey,
6262 copy_bufptr,
6263 copy_bufsiz, &keysym,
6264 &status_return);
6267 #endif
6269 if (status_return == XLookupNone)
6270 break;
6271 else if (status_return == XLookupChars)
6273 keysym = NoSymbol;
6274 modifiers = 0;
6276 else if (status_return != XLookupKeySym
6277 && status_return != XLookupBoth)
6278 abort ();
6280 else
6281 nbytes = XLookupString (&event.xkey, copy_bufptr,
6282 copy_bufsiz, &keysym,
6283 &compose_status);
6284 #else
6285 nbytes = XLookupString (&event.xkey, copy_bufptr,
6286 copy_bufsiz, &keysym,
6287 &compose_status);
6288 #endif
6290 orig_keysym = keysym;
6292 if (numchars > 1)
6294 Lisp_Object c;
6296 /* First deal with keysyms which have defined
6297 translations to characters. */
6298 if (keysym >= 32 && keysym < 128)
6299 /* Avoid explicitly decoding each ASCII character. */
6301 bufp->kind = ASCII_KEYSTROKE_EVENT;
6302 bufp->code = keysym;
6303 XSETFRAME (bufp->frame_or_window, f);
6304 bufp->arg = Qnil;
6305 bufp->modifiers
6306 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6307 modifiers);
6308 bufp->timestamp = event.xkey.time;
6309 bufp++;
6310 count++;
6311 numchars--;
6313 /* Now non-ASCII. */
6314 else if (HASH_TABLE_P (Vx_keysym_table)
6315 && (NATNUMP (c = Fgethash (make_number (keysym),
6316 Vx_keysym_table,
6317 Qnil))))
6319 bufp->kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6320 ? ASCII_KEYSTROKE_EVENT
6321 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6322 bufp->code = XFASTINT (c);
6323 XSETFRAME (bufp->frame_or_window, f);
6324 bufp->arg = Qnil;
6325 bufp->modifiers
6326 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6327 modifiers);
6328 bufp->timestamp = event.xkey.time;
6329 bufp++;
6330 count++;
6331 numchars--;
6333 /* Random non-modifier sorts of keysyms. */
6334 else if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6335 || keysym == XK_Delete
6336 #ifdef XK_ISO_Left_Tab
6337 || (keysym >= XK_ISO_Left_Tab
6338 && keysym <= XK_ISO_Enter)
6339 #endif
6340 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6341 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6342 #ifdef HPUX
6343 /* This recognizes the "extended function
6344 keys". It seems there's no cleaner way.
6345 Test IsModifierKey to avoid handling
6346 mode_switch incorrectly. */
6347 || ((unsigned) (keysym) >= XK_Select
6348 && (unsigned)(keysym) < XK_KP_Space)
6349 #endif
6350 #ifdef XK_dead_circumflex
6351 || orig_keysym == XK_dead_circumflex
6352 #endif
6353 #ifdef XK_dead_grave
6354 || orig_keysym == XK_dead_grave
6355 #endif
6356 #ifdef XK_dead_tilde
6357 || orig_keysym == XK_dead_tilde
6358 #endif
6359 #ifdef XK_dead_diaeresis
6360 || orig_keysym == XK_dead_diaeresis
6361 #endif
6362 #ifdef XK_dead_macron
6363 || orig_keysym == XK_dead_macron
6364 #endif
6365 #ifdef XK_dead_degree
6366 || orig_keysym == XK_dead_degree
6367 #endif
6368 #ifdef XK_dead_acute
6369 || orig_keysym == XK_dead_acute
6370 #endif
6371 #ifdef XK_dead_cedilla
6372 || orig_keysym == XK_dead_cedilla
6373 #endif
6374 #ifdef XK_dead_breve
6375 || orig_keysym == XK_dead_breve
6376 #endif
6377 #ifdef XK_dead_ogonek
6378 || orig_keysym == XK_dead_ogonek
6379 #endif
6380 #ifdef XK_dead_caron
6381 || orig_keysym == XK_dead_caron
6382 #endif
6383 #ifdef XK_dead_doubleacute
6384 || orig_keysym == XK_dead_doubleacute
6385 #endif
6386 #ifdef XK_dead_abovedot
6387 || orig_keysym == XK_dead_abovedot
6388 #endif
6389 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6390 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6391 /* Any "vendor-specific" key is ok. */
6392 || (orig_keysym & (1 << 28))
6393 || (keysym != NoSymbol && nbytes == 0))
6394 && ! (IsModifierKey (orig_keysym)
6395 #ifndef HAVE_X11R5
6396 #ifdef XK_Mode_switch
6397 || ((unsigned)(orig_keysym) == XK_Mode_switch)
6398 #endif
6399 #ifdef XK_Num_Lock
6400 || ((unsigned)(orig_keysym) == XK_Num_Lock)
6401 #endif
6402 #endif /* not HAVE_X11R5 */
6403 /* The symbols from XK_ISO_Lock
6404 to XK_ISO_Last_Group_Lock
6405 don't have real modifiers but
6406 should be treated similarly to
6407 Mode_switch by Emacs. */
6408 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6409 || ((unsigned)(orig_keysym)
6410 >= XK_ISO_Lock
6411 && (unsigned)(orig_keysym)
6412 <= XK_ISO_Last_Group_Lock)
6413 #endif
6416 if (temp_index == sizeof temp_buffer / sizeof (short))
6417 temp_index = 0;
6418 temp_buffer[temp_index++] = keysym;
6419 /* make_lispy_event will convert this to a symbolic
6420 key. */
6421 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
6422 bufp->code = keysym;
6423 XSETFRAME (bufp->frame_or_window, f);
6424 bufp->arg = Qnil;
6425 bufp->modifiers
6426 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6427 modifiers);
6428 bufp->timestamp = event.xkey.time;
6429 bufp++;
6430 count++;
6431 numchars--;
6433 else if (numchars > nbytes)
6434 { /* Raw bytes, not keysym. */
6435 register int i;
6436 register int c;
6437 int nchars, len;
6439 /* The input should be decoded with `coding_system'
6440 which depends on which X*LookupString function
6441 we used just above and the locale. */
6442 setup_coding_system (coding_system, &coding);
6443 coding.src_multibyte = 0;
6444 coding.dst_multibyte = 1;
6445 /* The input is converted to events, thus we can't
6446 handle composition. Anyway, there's no XIM that
6447 gives us composition information. */
6448 coding.composing = COMPOSITION_DISABLED;
6450 for (i = 0; i < nbytes; i++)
6452 if (temp_index == (sizeof temp_buffer
6453 / sizeof (short)))
6454 temp_index = 0;
6455 temp_buffer[temp_index++] = copy_bufptr[i];
6459 /* Decode the input data. */
6460 int require;
6461 unsigned char *p;
6463 require = decoding_buffer_size (&coding, nbytes);
6464 p = (unsigned char *) alloca (require);
6465 coding.mode |= CODING_MODE_LAST_BLOCK;
6466 /* We explicitly disable composition
6467 handling because key data should
6468 not contain any composition
6469 sequence. */
6470 coding.composing = COMPOSITION_DISABLED;
6471 decode_coding (&coding, copy_bufptr, p,
6472 nbytes, require);
6473 nbytes = coding.produced;
6474 nchars = coding.produced_char;
6475 copy_bufptr = p;
6478 /* Convert the input data to a sequence of
6479 character events. */
6480 for (i = 0; i < nbytes; i += len)
6482 if (nchars == nbytes)
6483 c = copy_bufptr[i], len = 1;
6484 else
6485 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6486 nbytes - i, len);
6488 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
6489 ? ASCII_KEYSTROKE_EVENT
6490 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6491 bufp->code = c;
6492 XSETFRAME (bufp->frame_or_window, f);
6493 bufp->arg = Qnil;
6494 bufp->modifiers
6495 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6496 modifiers);
6497 bufp->timestamp = event.xkey.time;
6498 bufp++;
6501 count += nchars;
6502 numchars -= nchars;
6504 if (keysym == NoSymbol)
6505 break;
6507 else
6508 abort ();
6510 else
6511 abort ();
6513 #ifdef HAVE_X_I18N
6514 /* Don't dispatch this event since XtDispatchEvent calls
6515 XFilterEvent, and two calls in a row may freeze the
6516 client. */
6517 break;
6518 #else
6519 goto OTHER;
6520 #endif
6522 case KeyRelease:
6523 #ifdef HAVE_X_I18N
6524 /* Don't dispatch this event since XtDispatchEvent calls
6525 XFilterEvent, and two calls in a row may freeze the
6526 client. */
6527 break;
6528 #else
6529 goto OTHER;
6530 #endif
6532 case EnterNotify:
6534 int n;
6536 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6537 if (n > 0)
6539 bufp += n, count += n, numchars -= n;
6542 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6544 #if 0
6545 if (event.xcrossing.focus)
6547 /* Avoid nasty pop/raise loops. */
6548 if (f && (!(f->auto_raise)
6549 || !(f->auto_lower)
6550 || (event.xcrossing.time - enter_timestamp) > 500))
6552 x_new_focus_frame (dpyinfo, f);
6553 enter_timestamp = event.xcrossing.time;
6556 else if (f == dpyinfo->x_focus_frame)
6557 x_new_focus_frame (dpyinfo, 0);
6558 #endif
6560 /* EnterNotify counts as mouse movement,
6561 so update things that depend on mouse position. */
6562 if (f && !f->output_data.x->hourglass_p)
6563 note_mouse_movement (f, &event.xmotion);
6564 goto OTHER;
6567 case FocusIn:
6569 int n;
6571 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6572 if (n > 0)
6574 bufp += n, count += n, numchars -= n;
6578 goto OTHER;
6580 case LeaveNotify:
6582 int n;
6584 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6585 if (n > 0)
6587 bufp += n, count += n, numchars -= n;
6591 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6592 if (f)
6594 if (f == dpyinfo->mouse_face_mouse_frame)
6596 /* If we move outside the frame, then we're
6597 certainly no longer on any text in the frame. */
6598 clear_mouse_face (dpyinfo);
6599 dpyinfo->mouse_face_mouse_frame = 0;
6602 /* Generate a nil HELP_EVENT to cancel a help-echo.
6603 Do it only if there's something to cancel.
6604 Otherwise, the startup message is cleared when
6605 the mouse leaves the frame. */
6606 if (any_help_event_p)
6608 Lisp_Object frame;
6609 int n;
6611 XSETFRAME (frame, f);
6612 help_echo_string = Qnil;
6613 n = gen_help_event (bufp, numchars,
6614 Qnil, frame, Qnil, Qnil, 0);
6615 bufp += n, count += n, numchars -= n;
6619 goto OTHER;
6621 case FocusOut:
6623 int n;
6625 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
6626 if (n > 0)
6628 bufp += n, count += n, numchars -= n;
6632 goto OTHER;
6634 case MotionNotify:
6636 previous_help_echo_string = help_echo_string;
6637 help_echo_string = help_echo_object = help_echo_window = Qnil;
6638 help_echo_pos = -1;
6640 if (dpyinfo->grabbed && last_mouse_frame
6641 && FRAME_LIVE_P (last_mouse_frame))
6642 f = last_mouse_frame;
6643 else
6644 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6646 if (dpyinfo->mouse_face_hidden)
6648 dpyinfo->mouse_face_hidden = 0;
6649 clear_mouse_face (dpyinfo);
6652 if (f)
6655 /* Generate SELECT_WINDOW_EVENTs when needed. */
6656 if (mouse_autoselect_window)
6658 Lisp_Object window;
6660 window = window_from_coordinates (f,
6661 event.xmotion.x, event.xmotion.y,
6662 0, 0);
6664 /* Window will be selected only when it is not selected now and
6665 last mouse movement event was not in it. Minibuffer window
6666 will be selected iff it is active. */
6667 if (WINDOWP(window)
6668 && !EQ (window, last_window)
6669 && !EQ (window, selected_window)
6670 && numchars > 0)
6672 bufp->kind = SELECT_WINDOW_EVENT;
6673 bufp->frame_or_window = window;
6674 bufp->arg = Qnil;
6675 ++bufp, ++count, --numchars;
6678 last_window=window;
6680 note_mouse_movement (f, &event.xmotion);
6682 else
6684 #ifndef USE_TOOLKIT_SCROLL_BARS
6685 struct scroll_bar *bar
6686 = x_window_to_scroll_bar (event.xmotion.window);
6688 if (bar)
6689 x_scroll_bar_note_movement (bar, &event);
6690 #endif /* USE_TOOLKIT_SCROLL_BARS */
6692 /* If we move outside the frame, then we're
6693 certainly no longer on any text in the frame. */
6694 clear_mouse_face (dpyinfo);
6697 /* If the contents of the global variable help_echo_string
6698 has changed, generate a HELP_EVENT. */
6699 if (!NILP (help_echo_string)
6700 || !NILP (previous_help_echo_string))
6702 Lisp_Object frame;
6703 int n;
6705 if (f)
6706 XSETFRAME (frame, f);
6707 else
6708 frame = Qnil;
6710 any_help_event_p = 1;
6711 n = gen_help_event (bufp, numchars, help_echo_string, frame,
6712 help_echo_window, help_echo_object,
6713 help_echo_pos);
6714 bufp += n, count += n, numchars -= n;
6717 goto OTHER;
6720 case ConfigureNotify:
6721 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6722 if (f)
6724 #ifndef USE_X_TOOLKIT
6725 #ifdef USE_GTK
6726 xg_resize_widgets (f, event.xconfigure.width,
6727 event.xconfigure.height);
6728 #else /* not USE_GTK */
6729 /* If there is a pending resize for fullscreen, don't
6730 do this one, the right one will come later.
6731 The toolkit version doesn't seem to need this, but we
6732 need to reset it below. */
6733 int dont_resize =
6734 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
6735 && FRAME_NEW_WIDTH (f) != 0);
6736 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
6737 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
6738 if (dont_resize)
6739 goto OTHER;
6741 /* In the toolkit version, change_frame_size
6742 is called by the code that handles resizing
6743 of the EmacsFrame widget. */
6745 /* Even if the number of character rows and columns has
6746 not changed, the font size may have changed, so we need
6747 to check the pixel dimensions as well. */
6748 if (columns != f->width
6749 || rows != f->height
6750 || event.xconfigure.width != f->output_data.x->pixel_width
6751 || event.xconfigure.height != f->output_data.x->pixel_height)
6753 change_frame_size (f, rows, columns, 0, 1, 0);
6754 SET_FRAME_GARBAGED (f);
6755 cancel_mouse_face (f);
6757 #endif /* not USE_GTK */
6758 #endif
6760 f->output_data.x->pixel_width = event.xconfigure.width;
6761 f->output_data.x->pixel_height = event.xconfigure.height;
6763 #ifdef USE_GTK
6764 /* GTK creates windows but doesn't map them.
6765 Only get real positions and check fullscreen when mapped. */
6766 if (FRAME_GTK_OUTER_WIDGET (f)
6767 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6769 #endif
6770 /* What we have now is the position of Emacs's own window.
6771 Convert that to the position of the window manager window. */
6772 x_real_positions (f, &f->output_data.x->left_pos,
6773 &f->output_data.x->top_pos);
6775 x_check_fullscreen_move (f);
6776 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
6777 f->output_data.x->want_fullscreen &=
6778 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6779 #ifdef USE_GTK
6781 #endif
6782 #ifdef HAVE_X_I18N
6783 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6784 xic_set_statusarea (f);
6785 #endif
6787 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6789 /* Since the WM decorations come below top_pos now,
6790 we must put them below top_pos in the future. */
6791 f->output_data.x->win_gravity = NorthWestGravity;
6792 x_wm_set_size_hint (f, (long) 0, 0);
6795 goto OTHER;
6797 case ButtonRelease:
6798 case ButtonPress:
6800 /* If we decide we want to generate an event to be seen
6801 by the rest of Emacs, we put it here. */
6802 struct input_event emacs_event;
6803 int tool_bar_p = 0;
6805 emacs_event.kind = NO_EVENT;
6806 bzero (&compose_status, sizeof (compose_status));
6808 if (dpyinfo->grabbed
6809 && last_mouse_frame
6810 && FRAME_LIVE_P (last_mouse_frame))
6811 f = last_mouse_frame;
6812 else
6813 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6815 if (f)
6817 /* Is this in the tool-bar? */
6818 if (WINDOWP (f->tool_bar_window)
6819 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
6821 Lisp_Object window;
6822 int x = event.xbutton.x;
6823 int y = event.xbutton.y;
6825 window = window_from_coordinates (f, x, y, 0, 1);
6826 if (EQ (window, f->tool_bar_window))
6828 if (event.xbutton.type == ButtonPress)
6829 handle_tool_bar_click (f, x, y, 1, 0);
6830 else
6831 handle_tool_bar_click (f, x, y, 0,
6832 x_x_to_emacs_modifiers (dpyinfo,
6833 event.xbutton.state));
6834 tool_bar_p = 1;
6838 if (!tool_bar_p)
6839 if (!dpyinfo->x_focus_frame
6840 || f == dpyinfo->x_focus_frame)
6842 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6843 if (! popup_activated ())
6844 #endif
6845 construct_mouse_click (&emacs_event, &event, f);
6848 else
6850 struct scroll_bar *bar
6851 = x_window_to_scroll_bar (event.xbutton.window);
6853 #ifdef USE_TOOLKIT_SCROLL_BARS
6854 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6855 scroll bars. */
6856 if (bar && event.xbutton.state & ControlMask)
6858 x_scroll_bar_handle_click (bar, &event, &emacs_event);
6859 *finish = X_EVENT_DROP;
6861 #else /* not USE_TOOLKIT_SCROLL_BARS */
6862 if (bar)
6863 x_scroll_bar_handle_click (bar, &event, &emacs_event);
6864 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6867 if (event.type == ButtonPress)
6869 dpyinfo->grabbed |= (1 << event.xbutton.button);
6870 last_mouse_frame = f;
6871 /* Ignore any mouse motion that happened
6872 before this event; any subsequent mouse-movement
6873 Emacs events should reflect only motion after
6874 the ButtonPress. */
6875 if (f != 0)
6876 f->mouse_moved = 0;
6878 if (!tool_bar_p)
6879 last_tool_bar_item = -1;
6881 else
6882 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6884 if (numchars >= 1 && emacs_event.kind != NO_EVENT)
6886 bcopy (&emacs_event, bufp, sizeof (struct input_event));
6887 bufp++;
6888 count++;
6889 numchars--;
6892 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6893 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6894 /* For a down-event in the menu bar,
6895 don't pass it to Xt right now.
6896 Instead, save it away
6897 and we will pass it to Xt from kbd_buffer_get_event.
6898 That way, we can run some Lisp code first. */
6899 if (
6900 #ifdef USE_GTK
6901 ! popup_activated ()
6903 #endif
6904 f && event.type == ButtonPress
6905 /* Verify the event is really within the menu bar
6906 and not just sent to it due to grabbing. */
6907 && event.xbutton.x >= 0
6908 && event.xbutton.x < f->output_data.x->pixel_width
6909 && event.xbutton.y >= 0
6910 && event.xbutton.y < f->output_data.x->menubar_height
6911 && event.xbutton.same_screen)
6913 SET_SAVED_BUTTON_EVENT;
6914 XSETFRAME (last_mouse_press_frame, f);
6915 #ifdef USE_GTK
6916 *finish = X_EVENT_DROP;
6917 #endif
6919 else if (event.type == ButtonPress)
6921 last_mouse_press_frame = Qnil;
6922 goto OTHER;
6925 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6926 but I am trying to be cautious. */
6927 else if (event.type == ButtonRelease)
6929 if (!NILP (last_mouse_press_frame))
6931 f = XFRAME (last_mouse_press_frame);
6932 if (f->output_data.x)
6933 SET_SAVED_BUTTON_EVENT;
6935 else
6936 goto OTHER;
6938 #endif /* USE_MOTIF */
6939 else
6940 goto OTHER;
6941 #endif /* USE_X_TOOLKIT || USE_GTK */
6943 break;
6945 case CirculateNotify:
6946 goto OTHER;
6948 case CirculateRequest:
6949 goto OTHER;
6951 case VisibilityNotify:
6952 goto OTHER;
6954 case MappingNotify:
6955 /* Someone has changed the keyboard mapping - update the
6956 local cache. */
6957 switch (event.xmapping.request)
6959 case MappingModifier:
6960 x_find_modifier_meanings (dpyinfo);
6961 /* This is meant to fall through. */
6962 case MappingKeyboard:
6963 XRefreshKeyboardMapping (&event.xmapping);
6965 goto OTHER;
6967 default:
6968 OTHER:
6969 #ifdef USE_X_TOOLKIT
6970 BLOCK_INPUT;
6971 if (*finish != X_EVENT_DROP)
6972 XtDispatchEvent (&event);
6973 UNBLOCK_INPUT;
6974 #endif /* USE_X_TOOLKIT */
6975 break;
6978 goto ret;
6980 out:
6981 *finish = X_EVENT_GOTO_OUT;
6983 ret:
6984 *bufp_r = bufp;
6985 *numcharsp = numchars;
6986 *eventp = event;
6988 return count;
6992 /* Handles the XEvent EVENT on display DISPLAY.
6993 This is used for event loops outside the normal event handling,
6994 i.e. looping while a popup menu or a dialog is posted.
6996 Returns the value handle_one_xevent sets in the finish argument. */
6998 x_dispatch_event (event, display)
6999 XEvent *event;
7000 Display *display;
7002 struct x_display_info *dpyinfo;
7003 struct input_event bufp[10];
7004 struct input_event *bufpp = bufp;
7005 int numchars = 10;
7006 int finish = X_EVENT_NORMAL;
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 static int nr = 0;
7122 current_count = count;
7123 current_numcharsp = &numchars;
7124 current_bufp = &bufp;
7126 gtk_main_iteration ();
7128 count = current_count;
7129 current_bufp = 0;
7130 current_numcharsp = 0;
7132 if (current_finish == X_EVENT_GOTO_OUT)
7133 goto out;
7136 #else /* not USE_GTK */
7137 while (XPending (dpyinfo->display))
7139 int finish;
7141 XNextEvent (dpyinfo->display, &event);
7143 #ifdef HAVE_X_I18N
7144 /* Filter events for the current X input method. */
7145 if (x_filter_event (dpyinfo, &event))
7146 break;
7147 #endif
7148 event_found = 1;
7150 count += handle_one_xevent (dpyinfo,
7151 &event,
7152 &bufp,
7153 &numchars,
7154 &finish);
7156 if (finish == X_EVENT_GOTO_OUT)
7157 goto out;
7159 #endif /* USE_GTK */
7162 out:;
7164 /* On some systems, an X bug causes Emacs to get no more events
7165 when the window is destroyed. Detect that. (1994.) */
7166 if (! event_found)
7168 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7169 One XNOOP in 100 loops will make Emacs terminate.
7170 B. Bretthauer, 1994 */
7171 x_noop_count++;
7172 if (x_noop_count >= 100)
7174 x_noop_count=0;
7176 if (next_noop_dpyinfo == 0)
7177 next_noop_dpyinfo = x_display_list;
7179 XNoOp (next_noop_dpyinfo->display);
7181 /* Each time we get here, cycle through the displays now open. */
7182 next_noop_dpyinfo = next_noop_dpyinfo->next;
7186 /* If the focus was just given to an auto-raising frame,
7187 raise it now. */
7188 /* ??? This ought to be able to handle more than one such frame. */
7189 if (pending_autoraise_frame)
7191 x_raise_frame (pending_autoraise_frame);
7192 pending_autoraise_frame = 0;
7195 UNBLOCK_INPUT;
7196 --handling_signal;
7197 return count;
7203 /***********************************************************************
7204 Text Cursor
7205 ***********************************************************************/
7207 /* Set clipping for output in glyph row ROW. W is the window in which
7208 we operate. GC is the graphics context to set clipping in.
7209 WHOLE_LINE_P non-zero means include the areas used for truncation
7210 mark display and alike in the clipping rectangle.
7212 ROW may be a text row or, e.g., a mode line. Text rows must be
7213 clipped to the interior of the window dedicated to text display,
7214 mode lines must be clipped to the whole window. */
7216 static void
7217 x_clip_to_row (w, row, gc, whole_line_p)
7218 struct window *w;
7219 struct glyph_row *row;
7220 GC gc;
7221 int whole_line_p;
7223 struct frame *f = XFRAME (WINDOW_FRAME (w));
7224 XRectangle clip_rect;
7225 int window_x, window_y, window_width, window_height;
7227 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
7229 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
7230 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
7231 clip_rect.y = max (clip_rect.y, window_y);
7232 clip_rect.width = window_width;
7233 clip_rect.height = row->visible_height;
7235 /* If clipping to the whole line, including trunc marks, extend
7236 the rectangle to the left and increase its width. */
7237 if (whole_line_p)
7239 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
7240 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
7243 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7247 /* Draw a hollow box cursor on window W in glyph row ROW. */
7249 static void
7250 x_draw_hollow_cursor (w, row)
7251 struct window *w;
7252 struct glyph_row *row;
7254 struct frame *f = XFRAME (WINDOW_FRAME (w));
7255 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7256 Display *dpy = FRAME_X_DISPLAY (f);
7257 int x, y, wd, h;
7258 XGCValues xgcv;
7259 struct glyph *cursor_glyph;
7260 GC gc;
7262 /* Compute frame-relative coordinates from window-relative
7263 coordinates. */
7264 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7265 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
7266 + row->ascent - w->phys_cursor_ascent);
7267 h = row->height - 1;
7269 /* Get the glyph the cursor is on. If we can't tell because
7270 the current matrix is invalid or such, give up. */
7271 cursor_glyph = get_phys_cursor_glyph (w);
7272 if (cursor_glyph == NULL)
7273 return;
7275 /* Compute the width of the rectangle to draw. If on a stretch
7276 glyph, and `x-stretch-block-cursor' is nil, don't draw a
7277 rectangle as wide as the glyph, but use a canonical character
7278 width instead. */
7279 wd = cursor_glyph->pixel_width - 1;
7280 if (cursor_glyph->type == STRETCH_GLYPH
7281 && !x_stretch_cursor_p)
7282 wd = min (CANON_X_UNIT (f), wd);
7283 w->phys_cursor_width = wd;
7285 /* The foreground of cursor_gc is typically the same as the normal
7286 background color, which can cause the cursor box to be invisible. */
7287 xgcv.foreground = f->output_data.x->cursor_pixel;
7288 if (dpyinfo->scratch_cursor_gc)
7289 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7290 else
7291 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7292 GCForeground, &xgcv);
7293 gc = dpyinfo->scratch_cursor_gc;
7295 /* Set clipping, draw the rectangle, and reset clipping again. */
7296 x_clip_to_row (w, row, gc, 0);
7297 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
7298 XSetClipMask (dpy, gc, None);
7302 /* Draw a bar cursor on window W in glyph row ROW.
7304 Implementation note: One would like to draw a bar cursor with an
7305 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7306 Unfortunately, I didn't find a font yet that has this property set.
7307 --gerd. */
7309 static void
7310 x_draw_bar_cursor (w, row, width, kind)
7311 struct window *w;
7312 struct glyph_row *row;
7313 int width;
7314 enum text_cursor_kinds kind;
7316 struct frame *f = XFRAME (w->frame);
7317 struct glyph *cursor_glyph;
7319 /* If cursor is out of bounds, don't draw garbage. This can happen
7320 in mini-buffer windows when switching between echo area glyphs
7321 and mini-buffer. */
7322 cursor_glyph = get_phys_cursor_glyph (w);
7323 if (cursor_glyph == NULL)
7324 return;
7326 /* If on an image, draw like a normal cursor. That's usually better
7327 visible than drawing a bar, esp. if the image is large so that
7328 the bar might not be in the window. */
7329 if (cursor_glyph->type == IMAGE_GLYPH)
7331 struct glyph_row *row;
7332 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7333 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7335 else
7337 Display *dpy = FRAME_X_DISPLAY (f);
7338 Window window = FRAME_X_WINDOW (f);
7339 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7340 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7341 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7342 XGCValues xgcv;
7344 /* If the glyph's background equals the color we normally draw
7345 the bar cursor in, the bar cursor in its normal color is
7346 invisible. Use the glyph's foreground color instead in this
7347 case, on the assumption that the glyph's colors are chosen so
7348 that the glyph is legible. */
7349 if (face->background == f->output_data.x->cursor_pixel)
7350 xgcv.background = xgcv.foreground = face->foreground;
7351 else
7352 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7353 xgcv.graphics_exposures = 0;
7355 if (gc)
7356 XChangeGC (dpy, gc, mask, &xgcv);
7357 else
7359 gc = XCreateGC (dpy, window, mask, &xgcv);
7360 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7363 if (width < 0)
7364 width = FRAME_CURSOR_WIDTH (f);
7365 width = min (cursor_glyph->pixel_width, width);
7367 w->phys_cursor_width = width;
7368 x_clip_to_row (w, row, gc, 0);
7370 if (kind == BAR_CURSOR)
7371 XFillRectangle (dpy, window, gc,
7372 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7373 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7374 width, row->height);
7375 else
7376 XFillRectangle (dpy, window, gc,
7377 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7378 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7379 row->height - width),
7380 cursor_glyph->pixel_width,
7381 width);
7383 XSetClipMask (dpy, gc, None);
7388 /* RIF: Define cursor CURSOR on frame F. */
7390 static void
7391 x_define_frame_cursor (f, cursor)
7392 struct frame *f;
7393 Cursor cursor;
7395 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7399 /* RIF: Clear area on frame F. */
7401 static void
7402 x_clear_frame_area (f, x, y, width, height)
7403 struct frame *f;
7404 int x, y, width, height;
7406 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7407 x, y, width, height, False);
7411 /* RIF: Draw cursor on window W. */
7413 static void
7414 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7415 struct window *w;
7416 struct glyph_row *glyph_row;
7417 int x, y;
7418 int cursor_type, cursor_width;
7419 int on_p, active_p;
7421 struct frame *f = XFRAME (WINDOW_FRAME (w));
7423 if (on_p)
7425 w->phys_cursor_type = cursor_type;
7426 w->phys_cursor_on_p = 1;
7428 switch (cursor_type)
7430 case HOLLOW_BOX_CURSOR:
7431 x_draw_hollow_cursor (w, glyph_row);
7432 break;
7434 case FILLED_BOX_CURSOR:
7435 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7436 break;
7438 case BAR_CURSOR:
7439 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7440 break;
7442 case HBAR_CURSOR:
7443 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7444 break;
7446 case NO_CURSOR:
7447 w->phys_cursor_width = 0;
7448 break;
7450 default:
7451 abort ();
7454 #ifdef HAVE_X_I18N
7455 if (w == XWINDOW (f->selected_window))
7456 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7457 xic_set_preeditarea (w, x, y);
7458 #endif
7461 #ifndef XFlush
7462 if (updating_frame != f)
7463 XFlush (FRAME_X_DISPLAY (f));
7464 #endif
7468 /* Icons. */
7470 /* Make the x-window of frame F use the gnu icon bitmap. */
7473 x_bitmap_icon (f, file)
7474 struct frame *f;
7475 Lisp_Object file;
7477 int bitmap_id;
7479 if (FRAME_X_WINDOW (f) == 0)
7480 return 1;
7482 /* Free up our existing icon bitmap if any. */
7483 if (f->output_data.x->icon_bitmap > 0)
7484 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7485 f->output_data.x->icon_bitmap = 0;
7487 if (STRINGP (file))
7488 bitmap_id = x_create_bitmap_from_file (f, file);
7489 else
7491 /* Create the GNU bitmap if necessary. */
7492 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7493 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
7494 = x_create_bitmap_from_data (f, gnu_bits,
7495 gnu_width, gnu_height);
7497 /* The first time we create the GNU bitmap,
7498 this increments the ref-count one extra time.
7499 As a result, the GNU bitmap is 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 f->output_data.x->font = (XFontStruct *) (fontp->font);
7888 f->output_data.x->baseline_offset = fontp->baseline_offset;
7889 f->output_data.x->fontset = -1;
7891 compute_fringe_widths (f, 1);
7893 /* Compute the scroll bar width in character columns. */
7894 if (f->scroll_bar_pixel_width > 0)
7896 int wid = FONT_WIDTH (f->output_data.x->font);
7897 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
7899 else
7901 int wid = FONT_WIDTH (f->output_data.x->font);
7902 f->scroll_bar_cols = (14 + wid - 1) / wid;
7905 /* Now make the frame display the given font. */
7906 if (FRAME_X_WINDOW (f) != 0)
7908 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
7909 f->output_data.x->font->fid);
7910 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
7911 f->output_data.x->font->fid);
7912 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
7913 f->output_data.x->font->fid);
7915 frame_update_line_height (f);
7917 /* Don't change the size of a tip frame; there's no point in
7918 doing it because it's done in Fx_show_tip, and it leads to
7919 problems because the tip frame has no widget. */
7920 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
7921 x_set_window_size (f, 0, f->width, f->height);
7923 else
7924 /* If we are setting a new frame's font for the first time,
7925 there are no faces yet, so this font's height is the line height. */
7926 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
7928 return build_string (fontp->full_name);
7931 /* Give frame F the fontset named FONTSETNAME as its default font, and
7932 return the full name of that fontset. FONTSETNAME may be a wildcard
7933 pattern; in that case, we choose some fontset that fits the pattern.
7934 The return value shows which fontset we chose. */
7936 Lisp_Object
7937 x_new_fontset (f, fontsetname)
7938 struct frame *f;
7939 char *fontsetname;
7941 int fontset = fs_query_fontset (build_string (fontsetname), 0);
7942 Lisp_Object result;
7944 if (fontset < 0)
7945 return Qnil;
7947 if (f->output_data.x->fontset == fontset)
7948 /* This fontset is already set in frame F. There's nothing more
7949 to do. */
7950 return fontset_name (fontset);
7952 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
7954 if (!STRINGP (result))
7955 /* Can't load ASCII font. */
7956 return Qnil;
7958 /* Since x_new_font doesn't update any fontset information, do it now. */
7959 f->output_data.x->fontset = fontset;
7961 #ifdef HAVE_X_I18N
7962 if (FRAME_XIC (f)
7963 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
7964 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
7965 #endif
7967 return build_string (fontsetname);
7971 /***********************************************************************
7972 X Input Methods
7973 ***********************************************************************/
7975 #ifdef HAVE_X_I18N
7977 #ifdef HAVE_X11R6
7979 /* XIM destroy callback function, which is called whenever the
7980 connection to input method XIM dies. CLIENT_DATA contains a
7981 pointer to the x_display_info structure corresponding to XIM. */
7983 static void
7984 xim_destroy_callback (xim, client_data, call_data)
7985 XIM xim;
7986 XPointer client_data;
7987 XPointer call_data;
7989 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
7990 Lisp_Object frame, tail;
7992 BLOCK_INPUT;
7994 /* No need to call XDestroyIC.. */
7995 FOR_EACH_FRAME (tail, frame)
7997 struct frame *f = XFRAME (frame);
7998 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8000 FRAME_XIC (f) = NULL;
8001 if (FRAME_XIC_FONTSET (f))
8003 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
8004 FRAME_XIC_FONTSET (f) = NULL;
8009 /* No need to call XCloseIM. */
8010 dpyinfo->xim = NULL;
8011 XFree (dpyinfo->xim_styles);
8012 UNBLOCK_INPUT;
8015 #endif /* HAVE_X11R6 */
8017 #ifdef HAVE_X11R6
8018 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8019 extern char *XSetIMValues P_ ((XIM, ...));
8020 #endif
8022 /* Open the connection to the XIM server on display DPYINFO.
8023 RESOURCE_NAME is the resource name Emacs uses. */
8025 static void
8026 xim_open_dpy (dpyinfo, resource_name)
8027 struct x_display_info *dpyinfo;
8028 char *resource_name;
8030 XIM xim;
8032 if (use_xim)
8034 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8035 EMACS_CLASS);
8036 dpyinfo->xim = xim;
8038 if (xim)
8040 #ifdef HAVE_X11R6
8041 XIMCallback destroy;
8042 #endif
8044 /* Get supported styles and XIM values. */
8045 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8047 #ifdef HAVE_X11R6
8048 destroy.callback = xim_destroy_callback;
8049 destroy.client_data = (XPointer)dpyinfo;
8050 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8051 #endif
8055 else
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 if (use_xim)
8132 #ifdef HAVE_X11R6_XIM
8133 struct xim_inst_t *xim_inst;
8134 int len;
8136 dpyinfo->xim = NULL;
8137 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8138 xim_inst->dpyinfo = dpyinfo;
8139 len = strlen (resource_name);
8140 xim_inst->resource_name = (char *) xmalloc (len + 1);
8141 bcopy (resource_name, xim_inst->resource_name, len + 1);
8142 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8143 resource_name, EMACS_CLASS,
8144 xim_instantiate_callback,
8145 /* Fixme: This is XPointer in
8146 XFree86 but (XPointer *) on
8147 Tru64, at least. */
8148 (XPointer) xim_inst);
8149 #else /* not HAVE_X11R6_XIM */
8150 dpyinfo->xim = NULL;
8151 xim_open_dpy (dpyinfo, resource_name);
8152 #endif /* not HAVE_X11R6_XIM */
8155 else
8156 dpyinfo->xim = NULL;
8160 /* Close the connection to the XIM server on display DPYINFO. */
8162 static void
8163 xim_close_dpy (dpyinfo)
8164 struct x_display_info *dpyinfo;
8166 if (use_xim)
8168 #ifdef HAVE_X11R6_XIM
8169 if (dpyinfo->display)
8170 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8171 NULL, EMACS_CLASS,
8172 xim_instantiate_callback, NULL);
8173 #endif /* not HAVE_X11R6_XIM */
8174 if (dpyinfo->display)
8175 XCloseIM (dpyinfo->xim);
8176 dpyinfo->xim = NULL;
8177 XFree (dpyinfo->xim_styles);
8181 #endif /* not HAVE_X11R6_XIM */
8185 /* Calculate the absolute position in frame F
8186 from its current recorded position values and gravity. */
8188 void
8189 x_calc_absolute_position (f)
8190 struct frame *f;
8192 Window child;
8193 int win_x = 0, win_y = 0;
8194 int flags = f->output_data.x->size_hint_flags;
8195 int this_window;
8197 /* We have nothing to do if the current position
8198 is already for the top-left corner. */
8199 if (! ((flags & XNegative) || (flags & YNegative)))
8200 return;
8202 this_window = FRAME_OUTER_WINDOW (f);
8204 /* Find the position of the outside upper-left corner of
8205 the inner window, with respect to the outer window.
8206 But do this only if we will need the results. */
8207 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8209 int count;
8211 BLOCK_INPUT;
8212 count = x_catch_errors (FRAME_X_DISPLAY (f));
8213 while (1)
8215 x_clear_errors (FRAME_X_DISPLAY (f));
8216 XTranslateCoordinates (FRAME_X_DISPLAY (f),
8218 /* From-window, to-window. */
8219 this_window,
8220 f->output_data.x->parent_desc,
8222 /* From-position, to-position. */
8223 0, 0, &win_x, &win_y,
8225 /* Child of win. */
8226 &child);
8227 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
8229 Window newroot, newparent = 0xdeadbeef;
8230 Window *newchildren;
8231 unsigned int nchildren;
8233 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
8234 &newparent, &newchildren, &nchildren))
8235 break;
8237 XFree ((char *) newchildren);
8239 f->output_data.x->parent_desc = newparent;
8241 else
8242 break;
8245 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
8246 UNBLOCK_INPUT;
8249 /* Treat negative positions as relative to the leftmost bottommost
8250 position that fits on the screen. */
8251 if (flags & XNegative)
8252 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8253 - 2 * f->output_data.x->border_width - win_x
8254 - PIXEL_WIDTH (f)
8255 + f->output_data.x->left_pos);
8258 int height = PIXEL_HEIGHT (f);
8260 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8261 /* Something is fishy here. When using Motif, starting Emacs with
8262 `-g -0-0', the frame appears too low by a few pixels.
8264 This seems to be so because initially, while Emacs is starting,
8265 the column widget's height and the frame's pixel height are
8266 different. The column widget's height is the right one. In
8267 later invocations, when Emacs is up, the frame's pixel height
8268 is right, though.
8270 It's not obvious where the initial small difference comes from.
8271 2000-12-01, gerd. */
8273 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8274 #endif
8276 if (flags & YNegative)
8277 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
8278 - 2 * f->output_data.x->border_width
8279 - win_y
8280 - height
8281 + f->output_data.x->top_pos);
8284 /* The left_pos and top_pos
8285 are now relative to the top and left screen edges,
8286 so the flags should correspond. */
8287 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
8290 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8291 to really change the position, and 0 when calling from
8292 x_make_frame_visible (in that case, XOFF and YOFF are the current
8293 position values). It is -1 when calling from x_set_frame_parameters,
8294 which means, do adjust for borders but don't change the gravity. */
8296 void
8297 x_set_offset (f, xoff, yoff, change_gravity)
8298 struct frame *f;
8299 register int xoff, yoff;
8300 int change_gravity;
8302 int modified_top, modified_left;
8304 if (change_gravity > 0)
8306 f->output_data.x->top_pos = yoff;
8307 f->output_data.x->left_pos = xoff;
8308 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
8309 if (xoff < 0)
8310 f->output_data.x->size_hint_flags |= XNegative;
8311 if (yoff < 0)
8312 f->output_data.x->size_hint_flags |= YNegative;
8313 f->output_data.x->win_gravity = NorthWestGravity;
8315 x_calc_absolute_position (f);
8317 BLOCK_INPUT;
8318 x_wm_set_size_hint (f, (long) 0, 0);
8320 modified_left = f->output_data.x->left_pos;
8321 modified_top = f->output_data.x->top_pos;
8322 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
8323 this seems to be unnecessary and incorrect. rms, 4/17/97. */
8324 /* It is a mystery why we need to add the border_width here
8325 when the frame is already visible, but experiment says we do. */
8326 if (change_gravity != 0)
8328 modified_left += f->output_data.x->border_width;
8329 modified_top += f->output_data.x->border_width;
8331 #endif
8333 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8334 modified_left, modified_top);
8335 UNBLOCK_INPUT;
8338 /* Check if we need to resize the frame due to a fullscreen request.
8339 If so needed, resize the frame. */
8340 static void
8341 x_check_fullscreen (f)
8342 struct frame *f;
8344 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
8346 int width, height, ign;
8348 x_real_positions (f, &f->output_data.x->left_pos,
8349 &f->output_data.x->top_pos);
8351 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8353 /* We do not need to move the window, it shall be taken care of
8354 when setting WM manager hints.
8355 If the frame is visible already, the position is checked by
8356 x_check_fullscreen_move. */
8357 if (f->width != width || f->height != height)
8359 change_frame_size (f, height, width, 0, 1, 0);
8360 SET_FRAME_GARBAGED (f);
8361 cancel_mouse_face (f);
8363 /* Wait for the change of frame size to occur */
8364 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
8369 /* If frame parameters are set after the frame is mapped, we need to move
8370 the window. This is done in xfns.c.
8371 Some window managers moves the window to the right position, some
8372 moves the outer window manager window to the specified position.
8373 Here we check that we are in the right spot. If not, make a second
8374 move, assuming we are dealing with the second kind of window manager. */
8375 static void
8376 x_check_fullscreen_move (f)
8377 struct frame *f;
8379 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
8381 int expect_top = f->output_data.x->top_pos;
8382 int expect_left = f->output_data.x->left_pos;
8384 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
8385 expect_top = 0;
8386 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
8387 expect_left = 0;
8389 if (expect_top != f->output_data.x->top_pos
8390 || expect_left != f->output_data.x->left_pos)
8391 x_set_offset (f, expect_left, expect_top, 1);
8393 /* Just do this once */
8394 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
8399 /* Change the size of frame F's X window to COLS/ROWS in the case F
8400 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8401 top-left-corner window gravity for this size change and subsequent
8402 size changes. Otherwise we leave the window gravity unchanged. */
8404 static void
8405 x_set_window_size_1 (f, change_gravity, cols, rows)
8406 struct frame *f;
8407 int change_gravity;
8408 int cols, rows;
8410 int pixelwidth, pixelheight;
8412 check_frame_size (f, &rows, &cols);
8413 f->output_data.x->vertical_scroll_bar_extra
8414 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8416 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
8417 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
8418 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
8420 compute_fringe_widths (f, 0);
8422 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
8423 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
8425 f->output_data.x->win_gravity = NorthWestGravity;
8426 x_wm_set_size_hint (f, (long) 0, 0);
8428 XSync (FRAME_X_DISPLAY (f), False);
8429 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8430 pixelwidth, pixelheight);
8432 /* Now, strictly speaking, we can't be sure that this is accurate,
8433 but the window manager will get around to dealing with the size
8434 change request eventually, and we'll hear how it went when the
8435 ConfigureNotify event gets here.
8437 We could just not bother storing any of this information here,
8438 and let the ConfigureNotify event set everything up, but that
8439 might be kind of confusing to the Lisp code, since size changes
8440 wouldn't be reported in the frame parameters until some random
8441 point in the future when the ConfigureNotify event arrives.
8443 We pass 1 for DELAY since we can't run Lisp code inside of
8444 a BLOCK_INPUT. */
8445 change_frame_size (f, rows, cols, 0, 1, 0);
8446 PIXEL_WIDTH (f) = pixelwidth;
8447 PIXEL_HEIGHT (f) = pixelheight;
8449 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8450 receive in the ConfigureNotify event; if we get what we asked
8451 for, then the event won't cause the screen to become garbaged, so
8452 we have to make sure to do it here. */
8453 SET_FRAME_GARBAGED (f);
8455 XFlush (FRAME_X_DISPLAY (f));
8459 /* Call this to change the size of frame F's x-window.
8460 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8461 for this size change and subsequent size changes.
8462 Otherwise we leave the window gravity unchanged. */
8464 void
8465 x_set_window_size (f, change_gravity, cols, rows)
8466 struct frame *f;
8467 int change_gravity;
8468 int cols, rows;
8470 BLOCK_INPUT;
8472 #ifdef USE_GTK
8473 if (FRAME_GTK_WIDGET (f))
8474 xg_frame_set_char_size (f, cols, rows);
8475 else
8476 x_set_window_size_1 (f, change_gravity, cols, rows);
8477 #elif USE_X_TOOLKIT
8479 if (f->output_data.x->widget != NULL)
8481 /* The x and y position of the widget is clobbered by the
8482 call to XtSetValues within EmacsFrameSetCharSize.
8483 This is a real kludge, but I don't understand Xt so I can't
8484 figure out a correct fix. Can anyone else tell me? -- rms. */
8485 int xpos = f->output_data.x->widget->core.x;
8486 int ypos = f->output_data.x->widget->core.y;
8487 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8488 f->output_data.x->widget->core.x = xpos;
8489 f->output_data.x->widget->core.y = ypos;
8491 else
8492 x_set_window_size_1 (f, change_gravity, cols, rows);
8494 #else /* not USE_X_TOOLKIT */
8496 x_set_window_size_1 (f, change_gravity, cols, rows);
8498 #endif /* not USE_X_TOOLKIT */
8500 /* If cursor was outside the new size, mark it as off. */
8501 mark_window_cursors_off (XWINDOW (f->root_window));
8503 /* Clear out any recollection of where the mouse highlighting was,
8504 since it might be in a place that's outside the new frame size.
8505 Actually checking whether it is outside is a pain in the neck,
8506 so don't try--just let the highlighting be done afresh with new size. */
8507 cancel_mouse_face (f);
8509 UNBLOCK_INPUT;
8512 /* Mouse warping. */
8514 void
8515 x_set_mouse_position (f, x, y)
8516 struct frame *f;
8517 int x, y;
8519 int pix_x, pix_y;
8521 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
8522 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
8524 if (pix_x < 0) pix_x = 0;
8525 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
8527 if (pix_y < 0) pix_y = 0;
8528 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
8530 BLOCK_INPUT;
8532 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8533 0, 0, 0, 0, pix_x, pix_y);
8534 UNBLOCK_INPUT;
8537 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8539 void
8540 x_set_mouse_pixel_position (f, pix_x, pix_y)
8541 struct frame *f;
8542 int pix_x, pix_y;
8544 BLOCK_INPUT;
8546 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8547 0, 0, 0, 0, pix_x, pix_y);
8548 UNBLOCK_INPUT;
8551 /* focus shifting, raising and lowering. */
8553 void
8554 x_focus_on_frame (f)
8555 struct frame *f;
8557 #if 0 /* This proves to be unpleasant. */
8558 x_raise_frame (f);
8559 #endif
8560 #if 0
8561 /* I don't think that the ICCCM allows programs to do things like this
8562 without the interaction of the window manager. Whatever you end up
8563 doing with this code, do it to x_unfocus_frame too. */
8564 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8565 RevertToPointerRoot, CurrentTime);
8566 #endif /* ! 0 */
8569 void
8570 x_unfocus_frame (f)
8571 struct frame *f;
8573 #if 0
8574 /* Look at the remarks in x_focus_on_frame. */
8575 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8576 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8577 RevertToPointerRoot, CurrentTime);
8578 #endif /* ! 0 */
8581 /* Raise frame F. */
8583 void
8584 x_raise_frame (f)
8585 struct frame *f;
8587 if (f->async_visible)
8589 BLOCK_INPUT;
8590 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8591 XFlush (FRAME_X_DISPLAY (f));
8592 UNBLOCK_INPUT;
8596 /* Lower frame F. */
8598 void
8599 x_lower_frame (f)
8600 struct frame *f;
8602 if (f->async_visible)
8604 BLOCK_INPUT;
8605 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8606 XFlush (FRAME_X_DISPLAY (f));
8607 UNBLOCK_INPUT;
8611 static void
8612 XTframe_raise_lower (f, raise_flag)
8613 FRAME_PTR f;
8614 int raise_flag;
8616 if (raise_flag)
8617 x_raise_frame (f);
8618 else
8619 x_lower_frame (f);
8622 /* Change of visibility. */
8624 /* This tries to wait until the frame is really visible.
8625 However, if the window manager asks the user where to position
8626 the frame, this will return before the user finishes doing that.
8627 The frame will not actually be visible at that time,
8628 but it will become visible later when the window manager
8629 finishes with it. */
8631 void
8632 x_make_frame_visible (f)
8633 struct frame *f;
8635 Lisp_Object type;
8636 int original_top, original_left;
8637 int retry_count = 2;
8639 retry:
8641 BLOCK_INPUT;
8643 type = x_icon_type (f);
8644 if (!NILP (type))
8645 x_bitmap_icon (f, type);
8647 if (! FRAME_VISIBLE_P (f))
8649 /* We test FRAME_GARBAGED_P here to make sure we don't
8650 call x_set_offset a second time
8651 if we get to x_make_frame_visible a second time
8652 before the window gets really visible. */
8653 if (! FRAME_ICONIFIED_P (f)
8654 && ! f->output_data.x->asked_for_visible)
8655 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
8657 f->output_data.x->asked_for_visible = 1;
8659 if (! EQ (Vx_no_window_manager, Qt))
8660 x_wm_set_window_state (f, NormalState);
8661 #ifdef USE_X_TOOLKIT
8662 /* This was XtPopup, but that did nothing for an iconified frame. */
8663 XtMapWidget (f->output_data.x->widget);
8664 #else /* not USE_X_TOOLKIT */
8665 #ifdef USE_GTK
8666 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8667 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8668 #else
8669 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8670 #endif /* not USE_GTK */
8671 #endif /* not USE_X_TOOLKIT */
8672 #if 0 /* This seems to bring back scroll bars in the wrong places
8673 if the window configuration has changed. They seem
8674 to come back ok without this. */
8675 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8676 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8677 #endif
8680 XFlush (FRAME_X_DISPLAY (f));
8682 /* Synchronize to ensure Emacs knows the frame is visible
8683 before we do anything else. We do this loop with input not blocked
8684 so that incoming events are handled. */
8686 Lisp_Object frame;
8687 int count;
8688 /* This must be before UNBLOCK_INPUT
8689 since events that arrive in response to the actions above
8690 will set it when they are handled. */
8691 int previously_visible = f->output_data.x->has_been_visible;
8693 original_left = f->output_data.x->left_pos;
8694 original_top = f->output_data.x->top_pos;
8696 /* This must come after we set COUNT. */
8697 UNBLOCK_INPUT;
8699 /* We unblock here so that arriving X events are processed. */
8701 /* Now move the window back to where it was "supposed to be".
8702 But don't do it if the gravity is negative.
8703 When the gravity is negative, this uses a position
8704 that is 3 pixels too low. Perhaps that's really the border width.
8706 Don't do this if the window has never been visible before,
8707 because the window manager may choose the position
8708 and we don't want to override it. */
8710 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
8711 && f->output_data.x->win_gravity == NorthWestGravity
8712 && previously_visible)
8714 Drawable rootw;
8715 int x, y;
8716 unsigned int width, height, border, depth;
8718 BLOCK_INPUT;
8720 /* On some window managers (such as FVWM) moving an existing
8721 window, even to the same place, causes the window manager
8722 to introduce an offset. This can cause the window to move
8723 to an unexpected location. Check the geometry (a little
8724 slow here) and then verify that the window is in the right
8725 place. If the window is not in the right place, move it
8726 there, and take the potential window manager hit. */
8727 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8728 &rootw, &x, &y, &width, &height, &border, &depth);
8730 if (original_left != x || original_top != y)
8731 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8732 original_left, original_top);
8734 UNBLOCK_INPUT;
8737 XSETFRAME (frame, f);
8739 /* Wait until the frame is visible. Process X events until a
8740 MapNotify event has been seen, or until we think we won't get a
8741 MapNotify at all.. */
8742 for (count = input_signal_count + 10;
8743 input_signal_count < count && !FRAME_VISIBLE_P (f);)
8745 /* Force processing of queued events. */
8746 x_sync (f);
8748 /* Machines that do polling rather than SIGIO have been
8749 observed to go into a busy-wait here. So we'll fake an
8750 alarm signal to let the handler know that there's something
8751 to be read. We used to raise a real alarm, but it seems
8752 that the handler isn't always enabled here. This is
8753 probably a bug. */
8754 if (input_polling_used ())
8756 /* It could be confusing if a real alarm arrives while
8757 processing the fake one. Turn it off and let the
8758 handler reset it. */
8759 extern void poll_for_input_1 P_ ((void));
8760 int old_poll_suppress_count = poll_suppress_count;
8761 poll_suppress_count = 1;
8762 poll_for_input_1 ();
8763 poll_suppress_count = old_poll_suppress_count;
8766 /* See if a MapNotify event has been processed. */
8767 FRAME_SAMPLE_VISIBILITY (f);
8770 /* 2000-09-28: In
8772 (let ((f (selected-frame)))
8773 (iconify-frame f)
8774 (raise-frame f))
8776 the frame is not raised with various window managers on
8777 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
8778 unknown reason, the call to XtMapWidget is completely ignored.
8779 Mapping the widget a second time works. */
8781 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
8782 goto retry;
8786 /* Change from mapped state to withdrawn state. */
8788 /* Make the frame visible (mapped and not iconified). */
8790 void
8791 x_make_frame_invisible (f)
8792 struct frame *f;
8794 Window window;
8796 /* Use the frame's outermost window, not the one we normally draw on. */
8797 window = FRAME_OUTER_WINDOW (f);
8799 /* Don't keep the highlight on an invisible frame. */
8800 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8801 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
8803 #if 0/* This might add unreliability; I don't trust it -- rms. */
8804 if (! f->async_visible && ! f->async_iconified)
8805 return;
8806 #endif
8808 BLOCK_INPUT;
8810 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
8811 that the current position of the window is user-specified, rather than
8812 program-specified, so that when the window is mapped again, it will be
8813 placed at the same location, without forcing the user to position it
8814 by hand again (they have already done that once for this window.) */
8815 x_wm_set_size_hint (f, (long) 0, 1);
8817 #ifdef USE_GTK
8818 if (FRAME_GTK_OUTER_WIDGET (f))
8819 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
8820 else
8821 #endif
8823 #ifdef HAVE_X11R4
8825 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
8826 DefaultScreen (FRAME_X_DISPLAY (f))))
8828 UNBLOCK_INPUT_RESIGNAL;
8829 error ("Can't notify window manager of window withdrawal");
8831 #else /* ! defined (HAVE_X11R4) */
8833 /* Tell the window manager what we're going to do. */
8834 if (! EQ (Vx_no_window_manager, Qt))
8836 XEvent unmap;
8838 unmap.xunmap.type = UnmapNotify;
8839 unmap.xunmap.window = window;
8840 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
8841 unmap.xunmap.from_configure = False;
8842 if (! XSendEvent (FRAME_X_DISPLAY (f),
8843 DefaultRootWindow (FRAME_X_DISPLAY (f)),
8844 False,
8845 SubstructureRedirectMaskSubstructureNotifyMask,
8846 &unmap))
8848 UNBLOCK_INPUT_RESIGNAL;
8849 error ("Can't notify window manager of withdrawal");
8853 /* Unmap the window ourselves. Cheeky! */
8854 XUnmapWindow (FRAME_X_DISPLAY (f), window);
8855 #endif /* ! defined (HAVE_X11R4) */
8858 /* We can't distinguish this from iconification
8859 just by the event that we get from the server.
8860 So we can't win using the usual strategy of letting
8861 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
8862 and synchronize with the server to make sure we agree. */
8863 f->visible = 0;
8864 FRAME_ICONIFIED_P (f) = 0;
8865 f->async_visible = 0;
8866 f->async_iconified = 0;
8868 x_sync (f);
8870 UNBLOCK_INPUT;
8873 /* Change window state from mapped to iconified. */
8875 void
8876 x_iconify_frame (f)
8877 struct frame *f;
8879 int result;
8880 Lisp_Object type;
8882 /* Don't keep the highlight on an invisible frame. */
8883 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8884 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
8886 if (f->async_iconified)
8887 return;
8889 BLOCK_INPUT;
8891 FRAME_SAMPLE_VISIBILITY (f);
8893 type = x_icon_type (f);
8894 if (!NILP (type))
8895 x_bitmap_icon (f, type);
8897 #ifdef USE_GTK
8898 if (FRAME_GTK_OUTER_WIDGET (f))
8900 if (! FRAME_VISIBLE_P (f))
8901 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8903 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8904 f->iconified = 1;
8905 f->visible = 1;
8906 f->async_iconified = 1;
8907 f->async_visible = 0;
8908 UNBLOCK_INPUT;
8909 return;
8911 #endif
8913 #ifdef USE_X_TOOLKIT
8915 if (! FRAME_VISIBLE_P (f))
8917 if (! EQ (Vx_no_window_manager, Qt))
8918 x_wm_set_window_state (f, IconicState);
8919 /* This was XtPopup, but that did nothing for an iconified frame. */
8920 XtMapWidget (f->output_data.x->widget);
8921 /* The server won't give us any event to indicate
8922 that an invisible frame was changed to an icon,
8923 so we have to record it here. */
8924 f->iconified = 1;
8925 f->visible = 1;
8926 f->async_iconified = 1;
8927 f->async_visible = 0;
8928 UNBLOCK_INPUT;
8929 return;
8932 result = XIconifyWindow (FRAME_X_DISPLAY (f),
8933 XtWindow (f->output_data.x->widget),
8934 DefaultScreen (FRAME_X_DISPLAY (f)));
8935 UNBLOCK_INPUT;
8937 if (!result)
8938 error ("Can't notify window manager of iconification");
8940 f->async_iconified = 1;
8941 f->async_visible = 0;
8944 BLOCK_INPUT;
8945 XFlush (FRAME_X_DISPLAY (f));
8946 UNBLOCK_INPUT;
8947 #else /* not USE_X_TOOLKIT */
8949 /* Make sure the X server knows where the window should be positioned,
8950 in case the user deiconifies with the window manager. */
8951 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
8952 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
8954 /* Since we don't know which revision of X we're running, we'll use both
8955 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
8957 /* X11R4: send a ClientMessage to the window manager using the
8958 WM_CHANGE_STATE type. */
8960 XEvent message;
8962 message.xclient.window = FRAME_X_WINDOW (f);
8963 message.xclient.type = ClientMessage;
8964 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
8965 message.xclient.format = 32;
8966 message.xclient.data.l[0] = IconicState;
8968 if (! XSendEvent (FRAME_X_DISPLAY (f),
8969 DefaultRootWindow (FRAME_X_DISPLAY (f)),
8970 False,
8971 SubstructureRedirectMask | SubstructureNotifyMask,
8972 &message))
8974 UNBLOCK_INPUT_RESIGNAL;
8975 error ("Can't notify window manager of iconification");
8979 /* X11R3: set the initial_state field of the window manager hints to
8980 IconicState. */
8981 x_wm_set_window_state (f, IconicState);
8983 if (!FRAME_VISIBLE_P (f))
8985 /* If the frame was withdrawn, before, we must map it. */
8986 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8989 f->async_iconified = 1;
8990 f->async_visible = 0;
8992 XFlush (FRAME_X_DISPLAY (f));
8993 UNBLOCK_INPUT;
8994 #endif /* not USE_X_TOOLKIT */
8998 /* Free X resources of frame F. */
9000 void
9001 x_free_frame_resources (f)
9002 struct frame *f;
9004 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9005 Lisp_Object bar;
9006 struct scroll_bar *b;
9008 BLOCK_INPUT;
9010 /* If a display connection is dead, don't try sending more
9011 commands to the X server. */
9012 if (dpyinfo->display)
9014 if (f->output_data.x->icon_desc)
9015 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9017 #ifdef USE_X_TOOLKIT
9018 /* Explicitly destroy the scroll bars of the frame. Without
9019 this, we get "BadDrawable" errors from the toolkit later on,
9020 presumably from expose events generated for the disappearing
9021 toolkit scroll bars. */
9022 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9024 b = XSCROLL_BAR (bar);
9025 x_scroll_bar_remove (b);
9027 #endif
9029 #ifdef HAVE_X_I18N
9030 if (FRAME_XIC (f))
9031 free_frame_xic (f);
9032 #endif
9034 #ifdef USE_X_TOOLKIT
9035 if (f->output_data.x->widget)
9037 XtDestroyWidget (f->output_data.x->widget);
9038 f->output_data.x->widget = NULL;
9040 /* Tooltips don't have widgets, only a simple X window, even if
9041 we are using a toolkit. */
9042 else if (FRAME_X_WINDOW (f))
9043 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9045 free_frame_menubar (f);
9046 #else /* !USE_X_TOOLKIT */
9048 #ifdef USE_GTK
9049 /* In the GTK version, tooltips are normal X
9050 frames. We must check and free both types. */
9051 if (FRAME_GTK_OUTER_WIDGET (f))
9053 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9054 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9055 FRAME_GTK_OUTER_WIDGET (f) = 0;
9057 #endif /* USE_GTK */
9059 if (FRAME_X_WINDOW (f))
9060 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9061 #endif /* !USE_X_TOOLKIT */
9063 unload_color (f, f->output_data.x->foreground_pixel);
9064 unload_color (f, f->output_data.x->background_pixel);
9065 unload_color (f, f->output_data.x->cursor_pixel);
9066 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9067 unload_color (f, f->output_data.x->border_pixel);
9068 unload_color (f, f->output_data.x->mouse_pixel);
9070 if (f->output_data.x->scroll_bar_background_pixel != -1)
9071 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9072 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9073 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9074 #ifdef USE_TOOLKIT_SCROLL_BARS
9075 /* Scrollbar shadow colors. */
9076 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9077 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9078 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9079 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9080 #endif /* USE_TOOLKIT_SCROLL_BARS */
9081 if (f->output_data.x->white_relief.allocated_p)
9082 unload_color (f, f->output_data.x->white_relief.pixel);
9083 if (f->output_data.x->black_relief.allocated_p)
9084 unload_color (f, f->output_data.x->black_relief.pixel);
9086 if (FRAME_FACE_CACHE (f))
9087 free_frame_faces (f);
9089 x_free_gcs (f);
9090 XFlush (FRAME_X_DISPLAY (f));
9093 if (f->output_data.x->saved_menu_event)
9094 xfree (f->output_data.x->saved_menu_event);
9096 xfree (f->output_data.x);
9097 f->output_data.x = NULL;
9099 if (f == dpyinfo->x_focus_frame)
9100 dpyinfo->x_focus_frame = 0;
9101 if (f == dpyinfo->x_focus_event_frame)
9102 dpyinfo->x_focus_event_frame = 0;
9103 if (f == dpyinfo->x_highlight_frame)
9104 dpyinfo->x_highlight_frame = 0;
9106 if (f == dpyinfo->mouse_face_mouse_frame)
9108 dpyinfo->mouse_face_beg_row
9109 = dpyinfo->mouse_face_beg_col = -1;
9110 dpyinfo->mouse_face_end_row
9111 = dpyinfo->mouse_face_end_col = -1;
9112 dpyinfo->mouse_face_window = Qnil;
9113 dpyinfo->mouse_face_deferred_gc = 0;
9114 dpyinfo->mouse_face_mouse_frame = 0;
9117 UNBLOCK_INPUT;
9121 /* Destroy the X window of frame F. */
9123 void
9124 x_destroy_window (f)
9125 struct frame *f;
9127 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9129 /* If a display connection is dead, don't try sending more
9130 commands to the X server. */
9131 if (dpyinfo->display != 0)
9132 x_free_frame_resources (f);
9134 dpyinfo->reference_count--;
9138 /* Setting window manager hints. */
9140 /* Set the normal size hints for the window manager, for frame F.
9141 FLAGS is the flags word to use--or 0 meaning preserve the flags
9142 that the window now has.
9143 If USER_POSITION is nonzero, we set the USPosition
9144 flag (this is useful when FLAGS is 0).
9145 The GTK version is in gtkutils.c */
9147 #ifndef USE_GTK
9148 void
9149 x_wm_set_size_hint (f, flags, user_position)
9150 struct frame *f;
9151 long flags;
9152 int user_position;
9154 XSizeHints size_hints;
9156 #ifdef USE_X_TOOLKIT
9157 Arg al[2];
9158 int ac = 0;
9159 Dimension widget_width, widget_height;
9160 #endif
9162 Window window = FRAME_OUTER_WINDOW (f);
9164 /* Setting PMaxSize caused various problems. */
9165 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9167 size_hints.x = f->output_data.x->left_pos;
9168 size_hints.y = f->output_data.x->top_pos;
9170 #ifdef USE_X_TOOLKIT
9171 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9172 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9173 XtGetValues (f->output_data.x->widget, al, ac);
9174 size_hints.height = widget_height;
9175 size_hints.width = widget_width;
9176 #else /* not USE_X_TOOLKIT */
9177 size_hints.height = PIXEL_HEIGHT (f);
9178 size_hints.width = PIXEL_WIDTH (f);
9179 #endif /* not USE_X_TOOLKIT */
9181 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
9182 size_hints.height_inc = f->output_data.x->line_height;
9183 size_hints.max_width
9184 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
9185 size_hints.max_height
9186 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
9188 /* Calculate the base and minimum sizes.
9190 (When we use the X toolkit, we don't do it here.
9191 Instead we copy the values that the widgets are using, below.) */
9192 #ifndef USE_X_TOOLKIT
9194 int base_width, base_height;
9195 int min_rows = 0, min_cols = 0;
9197 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
9198 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
9200 check_frame_size (f, &min_rows, &min_cols);
9202 /* The window manager uses the base width hints to calculate the
9203 current number of rows and columns in the frame while
9204 resizing; min_width and min_height aren't useful for this
9205 purpose, since they might not give the dimensions for a
9206 zero-row, zero-column frame.
9208 We use the base_width and base_height members if we have
9209 them; otherwise, we set the min_width and min_height members
9210 to the size for a zero x zero frame. */
9212 #ifdef HAVE_X11R4
9213 size_hints.flags |= PBaseSize;
9214 size_hints.base_width = base_width;
9215 size_hints.base_height = base_height;
9216 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9217 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9218 #else
9219 size_hints.min_width = base_width;
9220 size_hints.min_height = base_height;
9221 #endif
9224 /* If we don't need the old flags, we don't need the old hint at all. */
9225 if (flags)
9227 size_hints.flags |= flags;
9228 goto no_read;
9230 #endif /* not USE_X_TOOLKIT */
9233 XSizeHints hints; /* Sometimes I hate X Windows... */
9234 long supplied_return;
9235 int value;
9237 #ifdef HAVE_X11R4
9238 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9239 &supplied_return);
9240 #else
9241 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
9242 #endif
9244 #ifdef USE_X_TOOLKIT
9245 size_hints.base_height = hints.base_height;
9246 size_hints.base_width = hints.base_width;
9247 size_hints.min_height = hints.min_height;
9248 size_hints.min_width = hints.min_width;
9249 #endif
9251 if (flags)
9252 size_hints.flags |= flags;
9253 else
9255 if (value == 0)
9256 hints.flags = 0;
9257 if (hints.flags & PSize)
9258 size_hints.flags |= PSize;
9259 if (hints.flags & PPosition)
9260 size_hints.flags |= PPosition;
9261 if (hints.flags & USPosition)
9262 size_hints.flags |= USPosition;
9263 if (hints.flags & USSize)
9264 size_hints.flags |= USSize;
9268 #ifndef USE_X_TOOLKIT
9269 no_read:
9270 #endif
9272 #ifdef PWinGravity
9273 size_hints.win_gravity = f->output_data.x->win_gravity;
9274 size_hints.flags |= PWinGravity;
9276 if (user_position)
9278 size_hints.flags &= ~ PPosition;
9279 size_hints.flags |= USPosition;
9281 #endif /* PWinGravity */
9283 #ifdef HAVE_X11R4
9284 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9285 #else
9286 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9287 #endif
9289 #endif /* not USE_GTK */
9291 /* Used for IconicState or NormalState */
9293 void
9294 x_wm_set_window_state (f, state)
9295 struct frame *f;
9296 int state;
9298 #ifdef USE_X_TOOLKIT
9299 Arg al[1];
9301 XtSetArg (al[0], XtNinitialState, state);
9302 XtSetValues (f->output_data.x->widget, al, 1);
9303 #else /* not USE_X_TOOLKIT */
9304 Window window = FRAME_X_WINDOW (f);
9306 f->output_data.x->wm_hints.flags |= StateHint;
9307 f->output_data.x->wm_hints.initial_state = state;
9309 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9310 #endif /* not USE_X_TOOLKIT */
9313 void
9314 x_wm_set_icon_pixmap (f, pixmap_id)
9315 struct frame *f;
9316 int pixmap_id;
9318 Pixmap icon_pixmap;
9320 #ifndef USE_X_TOOLKIT
9321 Window window = FRAME_OUTER_WINDOW (f);
9322 #endif
9324 if (pixmap_id > 0)
9326 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9327 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9329 else
9331 /* It seems there is no way to turn off use of an icon pixmap.
9332 The following line does it, only if no icon has yet been created,
9333 for some window managers. But with mwm it crashes.
9334 Some people say it should clear the IconPixmapHint bit in this case,
9335 but that doesn't work, and the X consortium said it isn't the
9336 right thing at all. Since there is no way to win,
9337 best to explicitly give up. */
9338 #if 0
9339 f->output_data.x->wm_hints.icon_pixmap = None;
9340 #else
9341 return;
9342 #endif
9345 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
9348 Arg al[1];
9349 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9350 XtSetValues (f->output_data.x->widget, al, 1);
9353 #else /* not USE_X_TOOLKIT */
9355 f->output_data.x->wm_hints.flags |= IconPixmapHint;
9356 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9358 #endif /* not USE_X_TOOLKIT */
9361 void
9362 x_wm_set_icon_position (f, icon_x, icon_y)
9363 struct frame *f;
9364 int icon_x, icon_y;
9366 Window window = FRAME_OUTER_WINDOW (f);
9368 f->output_data.x->wm_hints.flags |= IconPositionHint;
9369 f->output_data.x->wm_hints.icon_x = icon_x;
9370 f->output_data.x->wm_hints.icon_y = icon_y;
9372 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9376 /***********************************************************************
9377 Fonts
9378 ***********************************************************************/
9380 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
9382 struct font_info *
9383 x_get_font_info (f, font_idx)
9384 FRAME_PTR f;
9385 int font_idx;
9387 return (FRAME_X_FONT_TABLE (f) + font_idx);
9391 /* Return a list of names of available fonts matching PATTERN on frame F.
9393 If SIZE is > 0, it is the size (maximum bounds width) of fonts
9394 to be listed.
9396 SIZE < 0 means include scalable fonts.
9398 Frame F null means we have not yet created any frame on X, and
9399 consult the first display in x_display_list. MAXNAMES sets a limit
9400 on how many fonts to match. */
9402 Lisp_Object
9403 x_list_fonts (f, pattern, size, maxnames)
9404 struct frame *f;
9405 Lisp_Object pattern;
9406 int size;
9407 int maxnames;
9409 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
9410 Lisp_Object tem, second_best;
9411 struct x_display_info *dpyinfo
9412 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
9413 Display *dpy = dpyinfo->display;
9414 int try_XLoadQueryFont = 0;
9415 int count;
9416 int allow_scalable_fonts_p = 0;
9418 if (size < 0)
9420 allow_scalable_fonts_p = 1;
9421 size = 0;
9424 patterns = Fassoc (pattern, Valternate_fontname_alist);
9425 if (NILP (patterns))
9426 patterns = Fcons (pattern, Qnil);
9428 if (maxnames == 1 && !size)
9429 /* We can return any single font matching PATTERN. */
9430 try_XLoadQueryFont = 1;
9432 for (; CONSP (patterns); patterns = XCDR (patterns))
9434 int num_fonts;
9435 char **names = NULL;
9437 pattern = XCAR (patterns);
9438 /* See if we cached the result for this particular query.
9439 The cache is an alist of the form:
9440 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
9441 tem = XCDR (dpyinfo->name_list_element);
9442 key = Fcons (Fcons (pattern, make_number (maxnames)),
9443 allow_scalable_fonts_p ? Qt : Qnil);
9444 list = Fassoc (key, tem);
9445 if (!NILP (list))
9447 list = Fcdr_safe (list);
9448 /* We have a cashed list. Don't have to get the list again. */
9449 goto label_cached;
9452 /* At first, put PATTERN in the cache. */
9454 BLOCK_INPUT;
9455 count = x_catch_errors (dpy);
9457 if (try_XLoadQueryFont)
9459 XFontStruct *font;
9460 unsigned long value;
9462 font = XLoadQueryFont (dpy, SDATA (pattern));
9463 if (x_had_errors_p (dpy))
9465 /* This error is perhaps due to insufficient memory on X
9466 server. Let's just ignore it. */
9467 font = NULL;
9468 x_clear_errors (dpy);
9471 if (font
9472 && XGetFontProperty (font, XA_FONT, &value))
9474 char *name = (char *) XGetAtomName (dpy, (Atom) value);
9475 int len = strlen (name);
9476 char *tmp;
9478 /* If DXPC (a Differential X Protocol Compressor)
9479 Ver.3.7 is running, XGetAtomName will return null
9480 string. We must avoid such a name. */
9481 if (len == 0)
9482 try_XLoadQueryFont = 0;
9483 else
9485 num_fonts = 1;
9486 names = (char **) alloca (sizeof (char *));
9487 /* Some systems only allow alloca assigned to a
9488 simple var. */
9489 tmp = (char *) alloca (len + 1); names[0] = tmp;
9490 bcopy (name, names[0], len + 1);
9491 XFree (name);
9494 else
9495 try_XLoadQueryFont = 0;
9497 if (font)
9498 XFreeFont (dpy, font);
9501 if (!try_XLoadQueryFont)
9503 /* We try at least 10 fonts because XListFonts will return
9504 auto-scaled fonts at the head. */
9505 if (maxnames < 0)
9507 int limit;
9509 for (limit = 500;;)
9511 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
9512 if (num_fonts == limit)
9514 BLOCK_INPUT;
9515 XFreeFontNames (names);
9516 UNBLOCK_INPUT;
9517 limit *= 2;
9519 else
9520 break;
9523 else
9524 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
9525 &num_fonts);
9527 if (x_had_errors_p (dpy))
9529 /* This error is perhaps due to insufficient memory on X
9530 server. Let's just ignore it. */
9531 names = NULL;
9532 x_clear_errors (dpy);
9536 x_uncatch_errors (dpy, count);
9537 UNBLOCK_INPUT;
9539 if (names)
9541 int i;
9543 /* Make a list of all the fonts we got back.
9544 Store that in the font cache for the display. */
9545 for (i = 0; i < num_fonts; i++)
9547 int width = 0;
9548 char *p = names[i];
9549 int average_width = -1, dashes = 0;
9551 /* Count the number of dashes in NAMES[I]. If there are
9552 14 dashes, and the field value following 12th dash
9553 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
9554 is usually too ugly to be used for editing. Let's
9555 ignore it. */
9556 while (*p)
9557 if (*p++ == '-')
9559 dashes++;
9560 if (dashes == 7) /* PIXEL_SIZE field */
9561 width = atoi (p);
9562 else if (dashes == 12) /* AVERAGE_WIDTH field */
9563 average_width = atoi (p);
9566 if (allow_scalable_fonts_p
9567 || dashes < 14 || average_width != 0)
9569 tem = build_string (names[i]);
9570 if (NILP (Fassoc (tem, list)))
9572 if (STRINGP (Vx_pixel_size_width_font_regexp)
9573 && ((fast_c_string_match_ignore_case
9574 (Vx_pixel_size_width_font_regexp, names[i]))
9575 >= 0))
9576 /* We can set the value of PIXEL_SIZE to the
9577 width of this font. */
9578 list = Fcons (Fcons (tem, make_number (width)), list);
9579 else
9580 /* For the moment, width is not known. */
9581 list = Fcons (Fcons (tem, Qnil), list);
9586 if (!try_XLoadQueryFont)
9588 BLOCK_INPUT;
9589 XFreeFontNames (names);
9590 UNBLOCK_INPUT;
9594 /* Now store the result in the cache. */
9595 XSETCDR (dpyinfo->name_list_element,
9596 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
9598 label_cached:
9599 if (NILP (list)) continue; /* Try the remaining alternatives. */
9601 newlist = second_best = Qnil;
9602 /* Make a list of the fonts that have the right width. */
9603 for (; CONSP (list); list = XCDR (list))
9605 int found_size;
9607 tem = XCAR (list);
9609 if (!CONSP (tem) || NILP (XCAR (tem)))
9610 continue;
9611 if (!size)
9613 newlist = Fcons (XCAR (tem), newlist);
9614 continue;
9617 if (!INTEGERP (XCDR (tem)))
9619 /* Since we have not yet known the size of this font, we
9620 must try slow function call XLoadQueryFont. */
9621 XFontStruct *thisinfo;
9623 BLOCK_INPUT;
9624 count = x_catch_errors (dpy);
9625 thisinfo = XLoadQueryFont (dpy,
9626 SDATA (XCAR (tem)));
9627 if (x_had_errors_p (dpy))
9629 /* This error is perhaps due to insufficient memory on X
9630 server. Let's just ignore it. */
9631 thisinfo = NULL;
9632 x_clear_errors (dpy);
9634 x_uncatch_errors (dpy, count);
9635 UNBLOCK_INPUT;
9637 if (thisinfo)
9639 XSETCDR (tem,
9640 (thisinfo->min_bounds.width == 0
9641 ? make_number (0)
9642 : make_number (thisinfo->max_bounds.width)));
9643 BLOCK_INPUT;
9644 XFreeFont (dpy, thisinfo);
9645 UNBLOCK_INPUT;
9647 else
9648 /* For unknown reason, the previous call of XListFont had
9649 returned a font which can't be opened. Record the size
9650 as 0 not to try to open it again. */
9651 XSETCDR (tem, make_number (0));
9654 found_size = XINT (XCDR (tem));
9655 if (found_size == size)
9656 newlist = Fcons (XCAR (tem), newlist);
9657 else if (found_size > 0)
9659 if (NILP (second_best))
9660 second_best = tem;
9661 else if (found_size < size)
9663 if (XINT (XCDR (second_best)) > size
9664 || XINT (XCDR (second_best)) < found_size)
9665 second_best = tem;
9667 else
9669 if (XINT (XCDR (second_best)) > size
9670 && XINT (XCDR (second_best)) > found_size)
9671 second_best = tem;
9675 if (!NILP (newlist))
9676 break;
9677 else if (!NILP (second_best))
9679 newlist = Fcons (XCAR (second_best), Qnil);
9680 break;
9684 return newlist;
9688 #if GLYPH_DEBUG
9690 /* Check that FONT is valid on frame F. It is if it can be found in F's
9691 font table. */
9693 static void
9694 x_check_font (f, font)
9695 struct frame *f;
9696 XFontStruct *font;
9698 int i;
9699 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9701 xassert (font != NULL);
9703 for (i = 0; i < dpyinfo->n_fonts; i++)
9704 if (dpyinfo->font_table[i].name
9705 && font == dpyinfo->font_table[i].font)
9706 break;
9708 xassert (i < dpyinfo->n_fonts);
9711 #endif /* GLYPH_DEBUG != 0 */
9713 /* Set *W to the minimum width, *H to the minimum font height of FONT.
9714 Note: There are (broken) X fonts out there with invalid XFontStruct
9715 min_bounds contents. For example, handa@etl.go.jp reports that
9716 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
9717 have font->min_bounds.width == 0. */
9719 static INLINE void
9720 x_font_min_bounds (font, w, h)
9721 XFontStruct *font;
9722 int *w, *h;
9724 *h = FONT_HEIGHT (font);
9725 *w = font->min_bounds.width;
9727 /* Try to handle the case where FONT->min_bounds has invalid
9728 contents. Since the only font known to have invalid min_bounds
9729 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
9730 if (*w <= 0)
9731 *w = font->max_bounds.width;
9735 /* Compute the smallest character width and smallest font height over
9736 all fonts available on frame F. Set the members smallest_char_width
9737 and smallest_font_height in F's x_display_info structure to
9738 the values computed. Value is non-zero if smallest_font_height or
9739 smallest_char_width become smaller than they were before. */
9741 static int
9742 x_compute_min_glyph_bounds (f)
9743 struct frame *f;
9745 int i;
9746 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9747 XFontStruct *font;
9748 int old_width = dpyinfo->smallest_char_width;
9749 int old_height = dpyinfo->smallest_font_height;
9751 dpyinfo->smallest_font_height = 100000;
9752 dpyinfo->smallest_char_width = 100000;
9754 for (i = 0; i < dpyinfo->n_fonts; ++i)
9755 if (dpyinfo->font_table[i].name)
9757 struct font_info *fontp = dpyinfo->font_table + i;
9758 int w, h;
9760 font = (XFontStruct *) fontp->font;
9761 xassert (font != (XFontStruct *) ~0);
9762 x_font_min_bounds (font, &w, &h);
9764 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
9765 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
9768 xassert (dpyinfo->smallest_char_width > 0
9769 && dpyinfo->smallest_font_height > 0);
9771 return (dpyinfo->n_fonts == 1
9772 || dpyinfo->smallest_char_width < old_width
9773 || dpyinfo->smallest_font_height < old_height);
9777 /* Load font named FONTNAME of the size SIZE for frame F, and return a
9778 pointer to the structure font_info while allocating it dynamically.
9779 If SIZE is 0, load any size of font.
9780 If loading is failed, return NULL. */
9782 struct font_info *
9783 x_load_font (f, fontname, size)
9784 struct frame *f;
9785 register char *fontname;
9786 int size;
9788 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9789 Lisp_Object font_names;
9790 int count;
9792 /* Get a list of all the fonts that match this name. Once we
9793 have a list of matching fonts, we compare them against the fonts
9794 we already have by comparing names. */
9795 font_names = x_list_fonts (f, build_string (fontname), size, 1);
9797 if (!NILP (font_names))
9799 Lisp_Object tail;
9800 int i;
9802 for (i = 0; i < dpyinfo->n_fonts; i++)
9803 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
9804 if (dpyinfo->font_table[i].name
9805 && (!strcmp (dpyinfo->font_table[i].name,
9806 SDATA (XCAR (tail)))
9807 || !strcmp (dpyinfo->font_table[i].full_name,
9808 SDATA (XCAR (tail)))))
9809 return (dpyinfo->font_table + i);
9812 /* Load the font and add it to the table. */
9814 char *full_name;
9815 XFontStruct *font;
9816 struct font_info *fontp;
9817 unsigned long value;
9818 int i;
9820 /* If we have found fonts by x_list_font, load one of them. If
9821 not, we still try to load a font by the name given as FONTNAME
9822 because XListFonts (called in x_list_font) of some X server has
9823 a bug of not finding a font even if the font surely exists and
9824 is loadable by XLoadQueryFont. */
9825 if (size > 0 && !NILP (font_names))
9826 fontname = (char *) SDATA (XCAR (font_names));
9828 BLOCK_INPUT;
9829 count = x_catch_errors (FRAME_X_DISPLAY (f));
9830 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
9831 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
9833 /* This error is perhaps due to insufficient memory on X
9834 server. Let's just ignore it. */
9835 font = NULL;
9836 x_clear_errors (FRAME_X_DISPLAY (f));
9838 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
9839 UNBLOCK_INPUT;
9840 if (!font)
9841 return NULL;
9843 /* Find a free slot in the font table. */
9844 for (i = 0; i < dpyinfo->n_fonts; ++i)
9845 if (dpyinfo->font_table[i].name == NULL)
9846 break;
9848 /* If no free slot found, maybe enlarge the font table. */
9849 if (i == dpyinfo->n_fonts
9850 && dpyinfo->n_fonts == dpyinfo->font_table_size)
9852 int sz;
9853 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
9854 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
9855 dpyinfo->font_table
9856 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
9859 fontp = dpyinfo->font_table + i;
9860 if (i == dpyinfo->n_fonts)
9861 ++dpyinfo->n_fonts;
9863 /* Now fill in the slots of *FONTP. */
9864 BLOCK_INPUT;
9865 fontp->font = font;
9866 fontp->font_idx = i;
9867 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
9868 bcopy (fontname, fontp->name, strlen (fontname) + 1);
9870 /* Try to get the full name of FONT. Put it in FULL_NAME. */
9871 full_name = 0;
9872 if (XGetFontProperty (font, XA_FONT, &value))
9874 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
9875 char *p = name;
9876 int dashes = 0;
9878 /* Count the number of dashes in the "full name".
9879 If it is too few, this isn't really the font's full name,
9880 so don't use it.
9881 In X11R4, the fonts did not come with their canonical names
9882 stored in them. */
9883 while (*p)
9885 if (*p == '-')
9886 dashes++;
9887 p++;
9890 if (dashes >= 13)
9892 full_name = (char *) xmalloc (p - name + 1);
9893 bcopy (name, full_name, p - name + 1);
9896 XFree (name);
9899 if (full_name != 0)
9900 fontp->full_name = full_name;
9901 else
9902 fontp->full_name = fontp->name;
9904 fontp->size = font->max_bounds.width;
9905 fontp->height = FONT_HEIGHT (font);
9907 if (NILP (font_names))
9909 /* We come here because of a bug of XListFonts mentioned at
9910 the head of this block. Let's store this information in
9911 the cache for x_list_fonts. */
9912 Lisp_Object lispy_name = build_string (fontname);
9913 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9914 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
9915 Qnil);
9917 XSETCDR (dpyinfo->name_list_element,
9918 Fcons (Fcons (key,
9919 Fcons (Fcons (lispy_full_name,
9920 make_number (fontp->size)),
9921 Qnil)),
9922 XCDR (dpyinfo->name_list_element)));
9923 if (full_name)
9925 key = Fcons (Fcons (lispy_full_name, make_number (256)),
9926 Qnil);
9927 XSETCDR (dpyinfo->name_list_element,
9928 Fcons (Fcons (key,
9929 Fcons (Fcons (lispy_full_name,
9930 make_number (fontp->size)),
9931 Qnil)),
9932 XCDR (dpyinfo->name_list_element)));
9936 /* The slot `encoding' specifies how to map a character
9937 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
9938 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
9939 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
9940 2:0xA020..0xFF7F). For the moment, we don't know which charset
9941 uses this font. So, we set information in fontp->encoding[1]
9942 which is never used by any charset. If mapping can't be
9943 decided, set FONT_ENCODING_NOT_DECIDED. */
9944 fontp->encoding[1]
9945 = (font->max_byte1 == 0
9946 /* 1-byte font */
9947 ? (font->min_char_or_byte2 < 0x80
9948 ? (font->max_char_or_byte2 < 0x80
9949 ? 0 /* 0x20..0x7F */
9950 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
9951 : 1) /* 0xA0..0xFF */
9952 /* 2-byte font */
9953 : (font->min_byte1 < 0x80
9954 ? (font->max_byte1 < 0x80
9955 ? (font->min_char_or_byte2 < 0x80
9956 ? (font->max_char_or_byte2 < 0x80
9957 ? 0 /* 0x2020..0x7F7F */
9958 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
9959 : 3) /* 0x20A0..0x7FFF */
9960 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
9961 : (font->min_char_or_byte2 < 0x80
9962 ? (font->max_char_or_byte2 < 0x80
9963 ? 2 /* 0xA020..0xFF7F */
9964 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
9965 : 1))); /* 0xA0A0..0xFFFF */
9967 fontp->baseline_offset
9968 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
9969 ? (long) value : 0);
9970 fontp->relative_compose
9971 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
9972 ? (long) value : 0);
9973 fontp->default_ascent
9974 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
9975 ? (long) value : 0);
9977 /* Set global flag fonts_changed_p to non-zero if the font loaded
9978 has a character with a smaller width than any other character
9979 before, or if the font loaded has a smaller height than any
9980 other font loaded before. If this happens, it will make a
9981 glyph matrix reallocation necessary. */
9982 fonts_changed_p |= x_compute_min_glyph_bounds (f);
9983 UNBLOCK_INPUT;
9984 return fontp;
9989 /* Return a pointer to struct font_info of a font named FONTNAME for
9990 frame F. If no such font is loaded, return NULL. */
9992 struct font_info *
9993 x_query_font (f, fontname)
9994 struct frame *f;
9995 register char *fontname;
9997 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9998 int i;
10000 for (i = 0; i < dpyinfo->n_fonts; i++)
10001 if (dpyinfo->font_table[i].name
10002 && (!strcmp (dpyinfo->font_table[i].name, fontname)
10003 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
10004 return (dpyinfo->font_table + i);
10005 return NULL;
10009 /* Find a CCL program for a font specified by FONTP, and set the member
10010 `encoder' of the structure. */
10012 void
10013 x_find_ccl_program (fontp)
10014 struct font_info *fontp;
10016 Lisp_Object list, elt;
10018 elt = Qnil;
10019 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
10021 elt = XCAR (list);
10022 if (CONSP (elt)
10023 && STRINGP (XCAR (elt))
10024 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10025 >= 0)
10026 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10027 >= 0)))
10028 break;
10031 if (! NILP (list))
10033 struct ccl_program *ccl
10034 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
10036 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
10037 xfree (ccl);
10038 else
10039 fontp->font_encoder = ccl;
10045 /***********************************************************************
10046 Initialization
10047 ***********************************************************************/
10049 #ifdef USE_X_TOOLKIT
10050 static XrmOptionDescRec emacs_options[] = {
10051 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10052 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10054 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10055 XrmoptionSepArg, NULL},
10056 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10058 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10059 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10060 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10061 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10062 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10063 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10064 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10066 #endif /* USE_X_TOOLKIT */
10068 static int x_initialized;
10070 #ifdef MULTI_KBOARD
10071 /* Test whether two display-name strings agree up to the dot that separates
10072 the screen number from the server number. */
10073 static int
10074 same_x_server (name1, name2)
10075 const char *name1, *name2;
10077 int seen_colon = 0;
10078 const unsigned char *system_name = SDATA (Vsystem_name);
10079 int system_name_length = strlen (system_name);
10080 int length_until_period = 0;
10082 while (system_name[length_until_period] != 0
10083 && system_name[length_until_period] != '.')
10084 length_until_period++;
10086 /* Treat `unix' like an empty host name. */
10087 if (! strncmp (name1, "unix:", 5))
10088 name1 += 4;
10089 if (! strncmp (name2, "unix:", 5))
10090 name2 += 4;
10091 /* Treat this host's name like an empty host name. */
10092 if (! strncmp (name1, system_name, system_name_length)
10093 && name1[system_name_length] == ':')
10094 name1 += system_name_length;
10095 if (! strncmp (name2, system_name, system_name_length)
10096 && name2[system_name_length] == ':')
10097 name2 += system_name_length;
10098 /* Treat this host's domainless name like an empty host name. */
10099 if (! strncmp (name1, system_name, length_until_period)
10100 && name1[length_until_period] == ':')
10101 name1 += length_until_period;
10102 if (! strncmp (name2, system_name, length_until_period)
10103 && name2[length_until_period] == ':')
10104 name2 += length_until_period;
10106 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10108 if (*name1 == ':')
10109 seen_colon++;
10110 if (seen_colon && *name1 == '.')
10111 return 1;
10113 return (seen_colon
10114 && (*name1 == '.' || *name1 == '\0')
10115 && (*name2 == '.' || *name2 == '\0'));
10117 #endif
10119 struct x_display_info *
10120 x_term_init (display_name, xrm_option, resource_name)
10121 Lisp_Object display_name;
10122 char *xrm_option;
10123 char *resource_name;
10125 int connection;
10126 Display *dpy;
10127 struct x_display_info *dpyinfo;
10128 XrmDatabase xrdb;
10130 BLOCK_INPUT;
10132 if (!x_initialized)
10134 x_initialize ();
10135 x_initialized = 1;
10138 #ifdef USE_GTK
10140 #define NUM_ARGV 10
10141 int argc;
10142 char *argv[NUM_ARGV];
10143 char **argv2 = argv;
10144 GdkAtom atom;
10146 /* GTK 2.0 can only handle one display, GTK 2.2 can handle more
10147 than one, but this remains to be implemented. */
10148 if (x_initialized > 1)
10149 return 0;
10151 x_initialized++;
10153 for (argc = 0; argc < NUM_ARGV; ++argc)
10154 argv[argc] = 0;
10156 argc = 0;
10157 argv[argc++] = initial_argv[0];
10159 if (! NILP (display_name))
10161 argv[argc++] = "--display";
10162 argv[argc++] = SDATA (display_name);
10165 argv[argc++] = "--name";
10166 argv[argc++] = resource_name;
10168 #ifdef HAVE_X11R5
10169 XSetLocaleModifiers ("");
10170 #endif
10172 gtk_init (&argc, &argv2);
10174 /* gtk_init does set_locale. We must fix locale after calling it. */
10175 fixup_locale ();
10176 xg_initialize ();
10178 dpy = GDK_DISPLAY ();
10180 /* NULL window -> events for all windows go to our function */
10181 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10183 /* Load our own gtkrc if it exists. */
10185 struct gcpro gcpro1, gcpro2;
10186 char *file = "~/.emacs.d/gtkrc";
10187 Lisp_Object s, abs_file;
10189 GCPRO2 (s, abs_file);
10190 s = make_string (file, strlen (file));
10191 abs_file = Fexpand_file_name(s, Qnil);
10193 if (! NILP (abs_file) && Ffile_readable_p (abs_file))
10194 gtk_rc_parse (SDATA (abs_file));
10196 UNGCPRO;
10199 XSetErrorHandler (x_error_handler);
10200 XSetIOErrorHandler (x_io_error_quitter);
10202 #else /* not USE_GTK */
10203 #ifdef USE_X_TOOLKIT
10204 /* weiner@footloose.sps.mot.com reports that this causes
10205 errors with X11R5:
10206 X protocol error: BadAtom (invalid Atom parameter)
10207 on protocol request 18skiloaf.
10208 So let's not use it until R6. */
10209 #ifdef HAVE_X11XTR6
10210 XtSetLanguageProc (NULL, NULL, NULL);
10211 #endif
10214 int argc = 0;
10215 char *argv[3];
10217 argv[0] = "";
10218 argc = 1;
10219 if (xrm_option)
10221 argv[argc++] = "-xrm";
10222 argv[argc++] = xrm_option;
10224 turn_on_atimers (0);
10225 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10226 resource_name, EMACS_CLASS,
10227 emacs_options, XtNumber (emacs_options),
10228 &argc, argv);
10229 turn_on_atimers (1);
10231 #ifdef HAVE_X11XTR6
10232 /* I think this is to compensate for XtSetLanguageProc. */
10233 fixup_locale ();
10234 #endif
10237 #else /* not USE_X_TOOLKIT */
10238 #ifdef HAVE_X11R5
10239 XSetLocaleModifiers ("");
10240 #endif
10241 dpy = XOpenDisplay (SDATA (display_name));
10242 #endif /* not USE_X_TOOLKIT */
10243 #endif /* not USE_GTK*/
10245 /* Detect failure. */
10246 if (dpy == 0)
10248 UNBLOCK_INPUT;
10249 return 0;
10252 /* We have definitely succeeded. Record the new connection. */
10254 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10255 bzero (dpyinfo, sizeof *dpyinfo);
10257 #ifdef MULTI_KBOARD
10259 struct x_display_info *share;
10260 Lisp_Object tail;
10262 for (share = x_display_list, tail = x_display_name_list; share;
10263 share = share->next, tail = XCDR (tail))
10264 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10265 SDATA (display_name)))
10266 break;
10267 if (share)
10268 dpyinfo->kboard = share->kboard;
10269 else
10271 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10272 init_kboard (dpyinfo->kboard);
10273 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10275 char *vendor = ServerVendor (dpy);
10276 UNBLOCK_INPUT;
10277 dpyinfo->kboard->Vsystem_key_alist
10278 = call1 (Qvendor_specific_keysyms,
10279 build_string (vendor ? vendor : ""));
10280 BLOCK_INPUT;
10283 dpyinfo->kboard->next_kboard = all_kboards;
10284 all_kboards = dpyinfo->kboard;
10285 /* Don't let the initial kboard remain current longer than necessary.
10286 That would cause problems if a file loaded on startup tries to
10287 prompt in the mini-buffer. */
10288 if (current_kboard == initial_kboard)
10289 current_kboard = dpyinfo->kboard;
10291 dpyinfo->kboard->reference_count++;
10293 #endif
10295 /* Put this display on the chain. */
10296 dpyinfo->next = x_display_list;
10297 x_display_list = dpyinfo;
10299 /* Put it on x_display_name_list as well, to keep them parallel. */
10300 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10301 x_display_name_list);
10302 dpyinfo->name_list_element = XCAR (x_display_name_list);
10304 dpyinfo->display = dpy;
10306 #if 0
10307 XSetAfterFunction (x_current_display, x_trace_wire);
10308 #endif /* ! 0 */
10310 dpyinfo->x_id_name
10311 = (char *) xmalloc (SBYTES (Vinvocation_name)
10312 + SBYTES (Vsystem_name)
10313 + 2);
10314 sprintf (dpyinfo->x_id_name, "%s@%s",
10315 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10317 /* Figure out which modifier bits mean what. */
10318 x_find_modifier_meanings (dpyinfo);
10320 /* Get the scroll bar cursor. */
10321 dpyinfo->vertical_scroll_bar_cursor
10322 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10324 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10325 resource_name, EMACS_CLASS);
10326 #ifdef HAVE_XRMSETDATABASE
10327 XrmSetDatabase (dpyinfo->display, xrdb);
10328 #else
10329 dpyinfo->display->db = xrdb;
10330 #endif
10331 /* Put the rdb where we can find it in a way that works on
10332 all versions. */
10333 dpyinfo->xrdb = xrdb;
10335 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10336 DefaultScreen (dpyinfo->display));
10337 select_visual (dpyinfo);
10338 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10339 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
10340 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
10341 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10342 dpyinfo->grabbed = 0;
10343 dpyinfo->reference_count = 0;
10344 dpyinfo->icon_bitmap_id = -1;
10345 dpyinfo->font_table = NULL;
10346 dpyinfo->n_fonts = 0;
10347 dpyinfo->font_table_size = 0;
10348 dpyinfo->bitmaps = 0;
10349 dpyinfo->bitmaps_size = 0;
10350 dpyinfo->bitmaps_last = 0;
10351 dpyinfo->scratch_cursor_gc = 0;
10352 dpyinfo->mouse_face_mouse_frame = 0;
10353 dpyinfo->mouse_face_deferred_gc = 0;
10354 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10355 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10356 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10357 dpyinfo->mouse_face_window = Qnil;
10358 dpyinfo->mouse_face_overlay = Qnil;
10359 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10360 dpyinfo->mouse_face_defer = 0;
10361 dpyinfo->mouse_face_hidden = 0;
10362 dpyinfo->x_focus_frame = 0;
10363 dpyinfo->x_focus_event_frame = 0;
10364 dpyinfo->x_highlight_frame = 0;
10365 dpyinfo->image_cache = make_image_cache ();
10367 /* See if a private colormap is requested. */
10368 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10370 if (dpyinfo->visual->class == PseudoColor)
10372 Lisp_Object value;
10373 value = display_x_get_resource (dpyinfo,
10374 build_string ("privateColormap"),
10375 build_string ("PrivateColormap"),
10376 Qnil, Qnil);
10377 if (STRINGP (value)
10378 && (!strcmp (SDATA (value), "true")
10379 || !strcmp (SDATA (value), "on")))
10380 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10383 else
10384 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10385 dpyinfo->visual, AllocNone);
10388 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10389 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10390 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10391 dpyinfo->resy = pixels * 25.4 / mm;
10392 pixels = DisplayWidth (dpyinfo->display, screen_number);
10393 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10394 dpyinfo->resx = pixels * 25.4 / mm;
10397 dpyinfo->Xatom_wm_protocols
10398 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10399 dpyinfo->Xatom_wm_take_focus
10400 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10401 dpyinfo->Xatom_wm_save_yourself
10402 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10403 dpyinfo->Xatom_wm_delete_window
10404 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10405 dpyinfo->Xatom_wm_change_state
10406 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10407 dpyinfo->Xatom_wm_configure_denied
10408 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10409 dpyinfo->Xatom_wm_window_moved
10410 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10411 dpyinfo->Xatom_editres
10412 = XInternAtom (dpyinfo->display, "Editres", False);
10413 dpyinfo->Xatom_CLIPBOARD
10414 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10415 dpyinfo->Xatom_TIMESTAMP
10416 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10417 dpyinfo->Xatom_TEXT
10418 = XInternAtom (dpyinfo->display, "TEXT", False);
10419 dpyinfo->Xatom_COMPOUND_TEXT
10420 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10421 dpyinfo->Xatom_UTF8_STRING
10422 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10423 dpyinfo->Xatom_DELETE
10424 = XInternAtom (dpyinfo->display, "DELETE", False);
10425 dpyinfo->Xatom_MULTIPLE
10426 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10427 dpyinfo->Xatom_INCR
10428 = XInternAtom (dpyinfo->display, "INCR", False);
10429 dpyinfo->Xatom_EMACS_TMP
10430 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10431 dpyinfo->Xatom_TARGETS
10432 = XInternAtom (dpyinfo->display, "TARGETS", False);
10433 dpyinfo->Xatom_NULL
10434 = XInternAtom (dpyinfo->display, "NULL", False);
10435 dpyinfo->Xatom_ATOM_PAIR
10436 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10437 /* For properties of font. */
10438 dpyinfo->Xatom_PIXEL_SIZE
10439 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10440 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10441 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10442 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10443 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10444 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10445 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10447 /* Ghostscript support. */
10448 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10449 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10451 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10452 False);
10454 dpyinfo->cut_buffers_initialized = 0;
10456 connection = ConnectionNumber (dpyinfo->display);
10457 dpyinfo->connection = connection;
10460 char null_bits[1];
10462 null_bits[0] = 0x00;
10464 dpyinfo->null_pixel
10465 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10466 null_bits, 1, 1, (long) 0, (long) 0,
10471 extern int gray_bitmap_width, gray_bitmap_height;
10472 extern char *gray_bitmap_bits;
10473 dpyinfo->gray
10474 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10475 gray_bitmap_bits,
10476 gray_bitmap_width, gray_bitmap_height,
10477 (unsigned long) 1, (unsigned long) 0, 1);
10480 #ifdef HAVE_X_I18N
10481 xim_initialize (dpyinfo, resource_name);
10482 #endif
10484 #ifdef subprocesses
10485 /* This is only needed for distinguishing keyboard and process input. */
10486 if (connection != 0)
10487 add_keyboard_wait_descriptor (connection);
10488 #endif
10490 #ifndef F_SETOWN_BUG
10491 #ifdef F_SETOWN
10492 #ifdef F_SETOWN_SOCK_NEG
10493 /* stdin is a socket here */
10494 fcntl (connection, F_SETOWN, -getpid ());
10495 #else /* ! defined (F_SETOWN_SOCK_NEG) */
10496 fcntl (connection, F_SETOWN, getpid ());
10497 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
10498 #endif /* ! defined (F_SETOWN) */
10499 #endif /* F_SETOWN_BUG */
10501 #ifdef SIGIO
10502 if (interrupt_input)
10503 init_sigio (connection);
10504 #endif /* ! defined (SIGIO) */
10506 #ifdef USE_LUCID
10507 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
10508 /* Make sure that we have a valid font for dialog boxes
10509 so that Xt does not crash. */
10511 Display *dpy = dpyinfo->display;
10512 XrmValue d, fr, to;
10513 Font font;
10514 int count;
10516 d.addr = (XPointer)&dpy;
10517 d.size = sizeof (Display *);
10518 fr.addr = XtDefaultFont;
10519 fr.size = sizeof (XtDefaultFont);
10520 to.size = sizeof (Font *);
10521 to.addr = (XPointer)&font;
10522 count = x_catch_errors (dpy);
10523 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10524 abort ();
10525 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10526 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10527 x_uncatch_errors (dpy, count);
10529 #endif
10530 #endif
10532 /* See if we should run in synchronous mode. This is useful
10533 for debugging X code. */
10535 Lisp_Object value;
10536 value = display_x_get_resource (dpyinfo,
10537 build_string ("synchronous"),
10538 build_string ("Synchronous"),
10539 Qnil, Qnil);
10540 if (STRINGP (value)
10541 && (!strcmp (SDATA (value), "true")
10542 || !strcmp (SDATA (value), "on")))
10543 XSynchronize (dpyinfo->display, True);
10547 Lisp_Object value;
10548 value = display_x_get_resource (dpyinfo,
10549 build_string ("useXIM"),
10550 build_string ("UseXIM"),
10551 Qnil, Qnil);
10552 if (STRINGP (value)
10553 && (!strcmp (XSTRING (value)->data, "false")
10554 || !strcmp (XSTRING (value)->data, "off")))
10555 use_xim = 0;
10558 UNBLOCK_INPUT;
10560 return dpyinfo;
10563 /* Get rid of display DPYINFO, assuming all frames are already gone,
10564 and without sending any more commands to the X server. */
10566 void
10567 x_delete_display (dpyinfo)
10568 struct x_display_info *dpyinfo;
10570 delete_keyboard_wait_descriptor (dpyinfo->connection);
10572 /* Discard this display from x_display_name_list and x_display_list.
10573 We can't use Fdelq because that can quit. */
10574 if (! NILP (x_display_name_list)
10575 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10576 x_display_name_list = XCDR (x_display_name_list);
10577 else
10579 Lisp_Object tail;
10581 tail = x_display_name_list;
10582 while (CONSP (tail) && CONSP (XCDR (tail)))
10584 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10586 XSETCDR (tail, XCDR (XCDR (tail)));
10587 break;
10589 tail = XCDR (tail);
10593 if (next_noop_dpyinfo == dpyinfo)
10594 next_noop_dpyinfo = dpyinfo->next;
10596 if (x_display_list == dpyinfo)
10597 x_display_list = dpyinfo->next;
10598 else
10600 struct x_display_info *tail;
10602 for (tail = x_display_list; tail; tail = tail->next)
10603 if (tail->next == dpyinfo)
10604 tail->next = tail->next->next;
10607 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
10608 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
10609 XrmDestroyDatabase (dpyinfo->xrdb);
10610 #endif
10611 #endif
10612 #ifdef MULTI_KBOARD
10613 if (--dpyinfo->kboard->reference_count == 0)
10614 delete_kboard (dpyinfo->kboard);
10615 #endif
10616 #ifdef HAVE_X_I18N
10617 if (dpyinfo->xim)
10618 xim_close_dpy (dpyinfo);
10619 #endif
10621 xfree (dpyinfo->font_table);
10622 xfree (dpyinfo->x_id_name);
10623 xfree (dpyinfo->color_cells);
10624 xfree (dpyinfo);
10627 #ifdef USE_X_TOOLKIT
10629 /* Atimer callback function for TIMER. Called every 0.1s to process
10630 Xt timeouts, if needed. We must avoid calling XtAppPending as
10631 much as possible because that function does an implicit XFlush
10632 that slows us down. */
10634 static void
10635 x_process_timeouts (timer)
10636 struct atimer *timer;
10638 if (toolkit_scroll_bar_interaction || popup_activated ())
10640 BLOCK_INPUT;
10641 while (XtAppPending (Xt_app_con) & XtIMTimer)
10642 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10643 UNBLOCK_INPUT;
10647 #endif /* USE_X_TOOLKIT */
10650 /* Set up use of X before we make the first connection. */
10652 extern frame_parm_handler x_frame_parm_handlers[];
10654 static struct redisplay_interface x_redisplay_interface =
10656 x_frame_parm_handlers,
10657 x_produce_glyphs,
10658 x_write_glyphs,
10659 x_insert_glyphs,
10660 x_clear_end_of_line,
10661 x_scroll_run,
10662 x_after_update_window_line,
10663 x_update_window_begin,
10664 x_update_window_end,
10665 x_cursor_to,
10666 x_flush,
10667 #ifndef XFlush
10668 x_flush,
10669 #else
10670 0, /* flush_display_optional */
10671 #endif
10672 x_clear_window_mouse_face,
10673 x_get_glyph_overhangs,
10674 x_fix_overlapping_area,
10675 x_draw_fringe_bitmap,
10676 x_per_char_metric,
10677 x_encode_char,
10678 x_compute_glyph_string_overhangs,
10679 x_draw_glyph_string,
10680 x_define_frame_cursor,
10681 x_clear_frame_area,
10682 x_draw_window_cursor,
10683 x_draw_vertical_window_border,
10684 x_shift_glyphs_for_insert
10687 void
10688 x_initialize ()
10690 rif = &x_redisplay_interface;
10692 clear_frame_hook = x_clear_frame;
10693 ins_del_lines_hook = x_ins_del_lines;
10694 delete_glyphs_hook = x_delete_glyphs;
10695 ring_bell_hook = XTring_bell;
10696 reset_terminal_modes_hook = XTreset_terminal_modes;
10697 set_terminal_modes_hook = XTset_terminal_modes;
10698 update_begin_hook = x_update_begin;
10699 update_end_hook = x_update_end;
10700 set_terminal_window_hook = XTset_terminal_window;
10701 read_socket_hook = XTread_socket;
10702 frame_up_to_date_hook = XTframe_up_to_date;
10703 mouse_position_hook = XTmouse_position;
10704 frame_rehighlight_hook = XTframe_rehighlight;
10705 frame_raise_lower_hook = XTframe_raise_lower;
10706 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10707 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10708 redeem_scroll_bar_hook = XTredeem_scroll_bar;
10709 judge_scroll_bars_hook = XTjudge_scroll_bars;
10711 scroll_region_ok = 1; /* we'll scroll partial frames */
10712 char_ins_del_ok = 1;
10713 line_ins_del_ok = 1; /* we'll just blt 'em */
10714 fast_clear_end_of_line = 1; /* X does this well */
10715 memory_below_frame = 0; /* we don't remember what scrolls
10716 off the bottom */
10717 baud_rate = 19200;
10719 x_noop_count = 0;
10720 last_tool_bar_item = -1;
10721 any_help_event_p = 0;
10723 /* Try to use interrupt input; if we can't, then start polling. */
10724 Fset_input_mode (Qt, Qnil, Qt, Qnil);
10726 #ifdef USE_X_TOOLKIT
10727 XtToolkitInitialize ();
10729 Xt_app_con = XtCreateApplicationContext ();
10731 /* Register a converter from strings to pixels, which uses
10732 Emacs' color allocation infrastructure. */
10733 XtAppSetTypeConverter (Xt_app_con,
10734 XtRString, XtRPixel, cvt_string_to_pixel,
10735 cvt_string_to_pixel_args,
10736 XtNumber (cvt_string_to_pixel_args),
10737 XtCacheByDisplay, cvt_pixel_dtor);
10739 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10741 /* Install an asynchronous timer that processes Xt timeout events
10742 every 0.1s. This is necessary because some widget sets use
10743 timeouts internally, for example the LessTif menu bar, or the
10744 Xaw3d scroll bar. When Xt timouts aren't processed, these
10745 widgets don't behave normally. */
10747 EMACS_TIME interval;
10748 EMACS_SET_SECS_USECS (interval, 0, 100000);
10749 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
10751 #endif
10753 #ifdef USE_TOOLKIT_SCROLL_BARS
10754 #ifndef USE_GTK
10755 xaw3d_arrow_scroll = False;
10756 xaw3d_pick_top = True;
10757 #endif
10758 #endif
10760 /* Note that there is no real way portable across R3/R4 to get the
10761 original error handler. */
10762 XSetErrorHandler (x_error_handler);
10763 XSetIOErrorHandler (x_io_error_quitter);
10765 /* Disable Window Change signals; they are handled by X events. */
10766 #ifdef SIGWINCH
10767 signal (SIGWINCH, SIG_DFL);
10768 #endif /* SIGWINCH */
10770 signal (SIGPIPE, x_connection_signal);
10772 #ifdef HAVE_X_SM
10773 x_session_initialize ();
10774 #endif
10778 void
10779 syms_of_xterm ()
10781 staticpro (&x_error_message_string);
10782 x_error_message_string = Qnil;
10784 staticpro (&x_display_name_list);
10785 x_display_name_list = Qnil;
10787 staticpro (&last_mouse_scroll_bar);
10788 last_mouse_scroll_bar = Qnil;
10790 staticpro (&Qvendor_specific_keysyms);
10791 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10793 staticpro (&Qutf_8);
10794 Qutf_8 = intern ("utf-8");
10795 staticpro (&Qlatin_1);
10796 Qlatin_1 = intern ("latin-1");
10798 staticpro (&last_mouse_press_frame);
10799 last_mouse_press_frame = Qnil;
10801 DEFVAR_BOOL ("x-use-underline-position-properties",
10802 &x_use_underline_position_properties,
10803 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10804 nil means ignore them. If you encounter fonts with bogus
10805 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10806 to 4.1, set this to nil. */);
10807 x_use_underline_position_properties = 1;
10809 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10810 doc: /* What X toolkit scroll bars Emacs uses.
10811 A value of nil means Emacs doesn't use X toolkit scroll bars.
10812 Otherwise, value is a symbol describing the X toolkit. */);
10813 #ifdef USE_TOOLKIT_SCROLL_BARS
10814 #ifdef USE_MOTIF
10815 Vx_toolkit_scroll_bars = intern ("motif");
10816 #elif defined HAVE_XAW3D
10817 Vx_toolkit_scroll_bars = intern ("xaw3d");
10818 #elif USE_GTK
10819 Vx_toolkit_scroll_bars = intern ("gtk");
10820 #else
10821 Vx_toolkit_scroll_bars = intern ("xaw");
10822 #endif
10823 #else
10824 Vx_toolkit_scroll_bars = Qnil;
10825 #endif
10827 staticpro (&last_mouse_motion_frame);
10828 last_mouse_motion_frame = Qnil;
10830 Qmodifier_value = intern ("modifier-value");
10831 Qalt = intern ("alt");
10832 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10833 Qhyper = intern ("hyper");
10834 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10835 Qmeta = intern ("meta");
10836 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10837 Qsuper = intern ("super");
10838 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10840 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10841 doc: /* Which keys Emacs uses for the alt modifier.
10842 This should be one of the symbols `alt', `hyper', `meta', `super'.
10843 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10844 is nil, which is the same as `alt'. */);
10845 Vx_alt_keysym = Qnil;
10847 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
10848 doc: /* Which keys Emacs uses for the hyper modifier.
10849 This should be one of the symbols `alt', `hyper', `meta', `super'.
10850 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10851 default is nil, which is the same as `hyper'. */);
10852 Vx_hyper_keysym = Qnil;
10854 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
10855 doc: /* Which keys Emacs uses for the meta modifier.
10856 This should be one of the symbols `alt', `hyper', `meta', `super'.
10857 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10858 default is nil, which is the same as `meta'. */);
10859 Vx_meta_keysym = Qnil;
10861 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
10862 doc: /* Which keys Emacs uses for the super modifier.
10863 This should be one of the symbols `alt', `hyper', `meta', `super'.
10864 For example, `super' means use the Super_L and Super_R keysyms. The
10865 default is nil, which is the same as `super'. */);
10866 Vx_super_keysym = Qnil;
10868 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
10869 doc: /* Hash table of character codes indexed by X keysym codes. */);
10870 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
10871 make_float (DEFAULT_REHASH_SIZE),
10872 make_float (DEFAULT_REHASH_THRESHOLD),
10873 Qnil, Qnil, Qnil);
10876 #endif /* HAVE_X_WINDOWS */