(comment-search-forward, comment-search-backward): Fix typos.
[emacs.git] / src / xterm.c
blob60ec7ec88300f9a459627de52edb89f2d4f93d12
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 97, 98, 1999, 2000, 01, 02, 2003
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
25 #include <config.h>
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
31 #include <stdio.h>
33 #ifdef HAVE_X_WINDOWS
35 #include "lisp.h"
36 #include "blockinput.h"
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
57 #include "systty.h"
58 #include "systime.h"
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
70 #include "charset.h"
71 #include "coding.h"
72 #include "ccl.h"
73 #include "frame.h"
74 #include "dispextern.h"
75 #include "fontset.h"
76 #include "termhooks.h"
77 #include "termopts.h"
78 #include "termchar.h"
79 #include "gnu.h"
80 #include "disptab.h"
81 #include "buffer.h"
82 #include "window.h"
83 #include "keyboard.h"
84 #include "intervals.h"
85 #include "process.h"
86 #include "atimer.h"
87 #include "keymap.h"
89 #ifdef USE_X_TOOLKIT
90 #include <X11/Shell.h>
91 #endif
93 #ifdef HAVE_SYS_TIME_H
94 #include <sys/time.h>
95 #endif
96 #ifdef HAVE_UNISTD_H
97 #include <unistd.h>
98 #endif
100 #ifdef USE_GTK
101 #include "gtkutil.h"
102 #endif
104 #ifdef USE_LUCID
105 extern int xlwmenu_window_p P_ ((Widget w, Window window));
106 extern void xlwmenu_redisplay P_ ((Widget));
107 #endif
109 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
111 extern void free_frame_menubar P_ ((struct frame *));
112 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
113 int));
114 #endif
116 #ifdef USE_X_TOOLKIT
117 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
118 #define HACK_EDITRES
119 extern void _XEditResCheckMessages ();
120 #endif /* not NO_EDITRES */
122 /* Include toolkit specific headers for the scroll bar widget. */
124 #ifdef USE_TOOLKIT_SCROLL_BARS
125 #if defined USE_MOTIF
126 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
127 #include <Xm/ScrollBar.h>
128 #else /* !USE_MOTIF i.e. use Xaw */
130 #ifdef HAVE_XAW3D
131 #include <X11/Xaw3d/Simple.h>
132 #include <X11/Xaw3d/Scrollbar.h>
133 #define ARROW_SCROLLBAR
134 #define XAW_ARROW_SCROLLBARS
135 #include <X11/Xaw3d/ScrollbarP.h>
136 #else /* !HAVE_XAW3D */
137 #include <X11/Xaw/Simple.h>
138 #include <X11/Xaw/Scrollbar.h>
139 #endif /* !HAVE_XAW3D */
140 #ifndef XtNpickTop
141 #define XtNpickTop "pickTop"
142 #endif /* !XtNpickTop */
143 #endif /* !USE_MOTIF */
144 #endif /* USE_TOOLKIT_SCROLL_BARS */
146 #endif /* USE_X_TOOLKIT */
148 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
149 #define x_any_window_to_frame x_window_to_frame
150 #define x_top_window_to_frame x_window_to_frame
151 #endif
153 #ifdef USE_X_TOOLKIT
154 #include "widget.h"
155 #ifndef XtNinitialState
156 #define XtNinitialState "initialState"
157 #endif
158 #endif
160 #define abs(x) ((x) < 0 ? -(x) : (x))
162 /* Default to using XIM if available. */
163 #ifdef USE_XIM
164 int use_xim = 1;
165 #else
166 int use_xim = 0; /* configure --without-xim */
167 #endif
171 /* Non-nil means Emacs uses toolkit scroll bars. */
173 Lisp_Object Vx_toolkit_scroll_bars;
175 /* Non-zero means that a HELP_EVENT has been generated since Emacs
176 start. */
178 static int any_help_event_p;
180 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
181 static Lisp_Object last_window;
183 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
185 int x_use_underline_position_properties;
187 /* This is a chain of structures for all the X displays currently in
188 use. */
190 struct x_display_info *x_display_list;
192 /* This is a list of cons cells, each of the form (NAME
193 . FONT-LIST-CACHE), one for each element of x_display_list and in
194 the same order. NAME is the name of the frame. FONT-LIST-CACHE
195 records previous values returned by x-list-fonts. */
197 Lisp_Object x_display_name_list;
199 /* Frame being updated by update_frame. This is declared in term.c.
200 This is set by update_begin and looked at by all the XT functions.
201 It is zero while not inside an update. In that case, the XT
202 functions assume that `selected_frame' is the frame to apply to. */
204 extern struct frame *updating_frame;
206 /* This is a frame waiting to be auto-raised, within XTread_socket. */
208 struct frame *pending_autoraise_frame;
210 #ifdef USE_X_TOOLKIT
211 /* The application context for Xt use. */
212 XtAppContext Xt_app_con;
213 static String Xt_default_resources[] = {0};
214 #endif /* USE_X_TOOLKIT */
216 /* Non-zero means user is interacting with a toolkit scroll bar. */
218 static int toolkit_scroll_bar_interaction;
220 /* Non-zero means to not move point as a result of clicking on a
221 frame to focus it (when focus-follows-mouse is nil). */
223 int x_mouse_click_focus_ignore_position;
225 /* Non-zero timeout value means ignore next mouse click if it arrives
226 before that timeout elapses (i.e. as part of the same sequence of
227 events resulting from clicking on a frame to select it). */
229 static unsigned long ignore_next_mouse_click_timeout;
231 /* Mouse movement.
233 Formerly, we used PointerMotionHintMask (in standard_event_mask)
234 so that we would have to call XQueryPointer after each MotionNotify
235 event to ask for another such event. However, this made mouse tracking
236 slow, and there was a bug that made it eventually stop.
238 Simply asking for MotionNotify all the time seems to work better.
240 In order to avoid asking for motion events and then throwing most
241 of them away or busy-polling the server for mouse positions, we ask
242 the server for pointer motion hints. This means that we get only
243 one event per group of mouse movements. "Groups" are delimited by
244 other kinds of events (focus changes and button clicks, for
245 example), or by XQueryPointer calls; when one of these happens, we
246 get another MotionNotify event the next time the mouse moves. This
247 is at least as efficient as getting motion events when mouse
248 tracking is on, and I suspect only negligibly worse when tracking
249 is off. */
251 /* Where the mouse was last time we reported a mouse event. */
253 static XRectangle last_mouse_glyph;
254 static Lisp_Object last_mouse_press_frame;
256 /* The scroll bar in which the last X motion event occurred.
258 If the last X motion event occurred in a scroll bar, we set this so
259 XTmouse_position can know whether to report a scroll bar motion or
260 an ordinary motion.
262 If the last X motion event didn't occur in a scroll bar, we set
263 this to Qnil, to tell XTmouse_position to return an ordinary motion
264 event. */
266 static Lisp_Object last_mouse_scroll_bar;
268 /* This is a hack. We would really prefer that XTmouse_position would
269 return the time associated with the position it returns, but there
270 doesn't seem to be any way to wrest the time-stamp from the server
271 along with the position query. So, we just keep track of the time
272 of the last movement we received, and return that in hopes that
273 it's somewhat accurate. */
275 static Time last_mouse_movement_time;
277 /* Incremented by XTread_socket whenever it really tries to read
278 events. */
280 #ifdef __STDC__
281 static int volatile input_signal_count;
282 #else
283 static int input_signal_count;
284 #endif
286 /* Used locally within XTread_socket. */
288 static int x_noop_count;
290 /* Initial values of argv and argc. */
292 extern char **initial_argv;
293 extern int initial_argc;
295 extern Lisp_Object Vcommand_line_args, Vsystem_name;
297 /* Tells if a window manager is present or not. */
299 extern Lisp_Object Vx_no_window_manager;
301 extern Lisp_Object Qeql;
303 extern int errno;
305 /* A mask of extra modifier bits to put into every keyboard char. */
307 extern EMACS_INT extra_keyboard_modifiers;
309 /* The keysyms to use for the various modifiers. */
311 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
312 Lisp_Object Vx_keysym_table;
313 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
315 static Lisp_Object Qvendor_specific_keysyms;
316 static Lisp_Object Qlatin_1;
318 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
319 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
321 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
322 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
323 static const XColor *x_color_cells P_ ((Display *, int *));
324 static void x_update_window_end P_ ((struct window *, int, int));
325 void x_delete_display P_ ((struct x_display_info *));
326 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
327 unsigned));
328 static int x_io_error_quitter P_ ((Display *));
329 int x_catch_errors P_ ((Display *));
330 void x_uncatch_errors P_ ((Display *, int));
331 void x_lower_frame P_ ((struct frame *));
332 void x_scroll_bar_clear P_ ((struct frame *));
333 int x_had_errors_p P_ ((Display *));
334 void x_wm_set_size_hint P_ ((struct frame *, long, int));
335 void x_raise_frame P_ ((struct frame *));
336 void x_set_window_size P_ ((struct frame *, int, int, int));
337 void x_wm_set_window_state P_ ((struct frame *, int));
338 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
339 void x_initialize P_ ((void));
340 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
341 static int x_compute_min_glyph_bounds P_ ((struct frame *));
342 static void x_update_end P_ ((struct frame *));
343 static void XTframe_up_to_date P_ ((struct frame *));
344 static void XTset_terminal_modes P_ ((void));
345 static void XTreset_terminal_modes P_ ((void));
346 static void x_clear_frame P_ ((void));
347 static void frame_highlight P_ ((struct frame *));
348 static void frame_unhighlight P_ ((struct frame *));
349 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
350 static void x_focus_changed P_ ((int, int, struct x_display_info *,
351 struct frame *, struct input_event *));
352 static void x_detect_focus_change P_ ((struct x_display_info *,
353 XEvent *, struct input_event *));
354 static void XTframe_rehighlight P_ ((struct frame *));
355 static void x_frame_rehighlight P_ ((struct x_display_info *));
356 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
357 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
358 enum text_cursor_kinds));
360 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, GC));
361 static void x_flush P_ ((struct frame *f));
362 static void x_update_begin P_ ((struct frame *));
363 static void x_update_window_begin P_ ((struct window *));
364 static void x_after_update_window_line P_ ((struct glyph_row *));
365 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
366 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
367 enum scroll_bar_part *,
368 Lisp_Object *, Lisp_Object *,
369 unsigned long *));
370 static void x_check_fullscreen P_ ((struct frame *));
371 static void x_check_expected_move P_ ((struct frame *));
372 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
373 int *, struct input_event *));
376 /* Flush display of frame F, or of all frames if F is null. */
378 static void
379 x_flush (f)
380 struct frame *f;
382 BLOCK_INPUT;
383 if (f == NULL)
385 Lisp_Object rest, frame;
386 FOR_EACH_FRAME (rest, frame)
387 x_flush (XFRAME (frame));
389 else if (FRAME_X_P (f))
390 XFlush (FRAME_X_DISPLAY (f));
391 UNBLOCK_INPUT;
395 /* Remove calls to XFlush by defining XFlush to an empty replacement.
396 Calls to XFlush should be unnecessary because the X output buffer
397 is flushed automatically as needed by calls to XPending,
398 XNextEvent, or XWindowEvent according to the XFlush man page.
399 XTread_socket calls XPending. Removing XFlush improves
400 performance. */
402 #define XFlush(DISPLAY) (void) 0
405 /***********************************************************************
406 Debugging
407 ***********************************************************************/
409 #if 0
411 /* This is a function useful for recording debugging information about
412 the sequence of occurrences in this file. */
414 struct record
416 char *locus;
417 int type;
420 struct record event_record[100];
422 int event_record_index;
424 record_event (locus, type)
425 char *locus;
426 int type;
428 if (event_record_index == sizeof (event_record) / sizeof (struct record))
429 event_record_index = 0;
431 event_record[event_record_index].locus = locus;
432 event_record[event_record_index].type = type;
433 event_record_index++;
436 #endif /* 0 */
440 /* Return the struct x_display_info corresponding to DPY. */
442 struct x_display_info *
443 x_display_info_for_display (dpy)
444 Display *dpy;
446 struct x_display_info *dpyinfo;
448 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
449 if (dpyinfo->display == dpy)
450 return dpyinfo;
452 return 0;
457 /***********************************************************************
458 Starting and ending an update
459 ***********************************************************************/
461 /* Start an update of frame F. This function is installed as a hook
462 for update_begin, i.e. it is called when update_begin is called.
463 This function is called prior to calls to x_update_window_begin for
464 each window being updated. Currently, there is nothing to do here
465 because all interesting stuff is done on a window basis. */
467 static void
468 x_update_begin (f)
469 struct frame *f;
471 /* Nothing to do. */
475 /* Start update of window W. Set the global variable updated_window
476 to the window being updated and set output_cursor to the cursor
477 position of W. */
479 static void
480 x_update_window_begin (w)
481 struct window *w;
483 struct frame *f = XFRAME (WINDOW_FRAME (w));
484 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
486 updated_window = w;
487 set_output_cursor (&w->cursor);
489 BLOCK_INPUT;
491 if (f == display_info->mouse_face_mouse_frame)
493 /* Don't do highlighting for mouse motion during the update. */
494 display_info->mouse_face_defer = 1;
496 /* If F needs to be redrawn, simply forget about any prior mouse
497 highlighting. */
498 if (FRAME_GARBAGED_P (f))
499 display_info->mouse_face_window = Qnil;
501 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
502 their mouse_face_p flag set, which means that they are always
503 unequal to rows in a desired matrix which never have that
504 flag set. So, rows containing mouse-face glyphs are never
505 scrolled, and we don't have to switch the mouse highlight off
506 here to prevent it from being scrolled. */
508 /* Can we tell that this update does not affect the window
509 where the mouse highlight is? If so, no need to turn off.
510 Likewise, don't do anything if the frame is garbaged;
511 in that case, the frame's current matrix that we would use
512 is all wrong, and we will redisplay that line anyway. */
513 if (!NILP (display_info->mouse_face_window)
514 && w == XWINDOW (display_info->mouse_face_window))
516 int i;
518 for (i = 0; i < w->desired_matrix->nrows; ++i)
519 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
520 break;
522 if (i < w->desired_matrix->nrows)
523 clear_mouse_face (display_info);
525 #endif /* 0 */
528 UNBLOCK_INPUT;
532 /* Draw a vertical window border from (x,y0) to (x,y1) */
534 static void
535 x_draw_vertical_window_border (w, x, y0, y1)
536 struct window *w;
537 int x, y0, y1;
539 struct frame *f = XFRAME (WINDOW_FRAME (w));
541 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
542 f->output_data.x->normal_gc, x, y0, x, y1);
545 /* End update of window W (which is equal to updated_window).
547 Draw vertical borders between horizontally adjacent windows, and
548 display W's cursor if CURSOR_ON_P is non-zero.
550 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
551 glyphs in mouse-face were overwritten. In that case we have to
552 make sure that the mouse-highlight is properly redrawn.
554 W may be a menu bar pseudo-window in case we don't have X toolkit
555 support. Such windows don't have a cursor, so don't display it
556 here. */
558 static void
559 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
560 struct window *w;
561 int cursor_on_p, mouse_face_overwritten_p;
563 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
565 if (!w->pseudo_window_p)
567 BLOCK_INPUT;
569 if (cursor_on_p)
570 display_and_set_cursor (w, 1, output_cursor.hpos,
571 output_cursor.vpos,
572 output_cursor.x, output_cursor.y);
574 x_draw_vertical_border (w);
576 draw_window_fringes (w);
578 UNBLOCK_INPUT;
581 /* If a row with mouse-face was overwritten, arrange for
582 XTframe_up_to_date to redisplay the mouse highlight. */
583 if (mouse_face_overwritten_p)
585 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
586 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
587 dpyinfo->mouse_face_window = Qnil;
590 updated_window = NULL;
594 /* End update of frame F. This function is installed as a hook in
595 update_end. */
597 static void
598 x_update_end (f)
599 struct frame *f;
601 /* Mouse highlight may be displayed again. */
602 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
604 #ifndef XFlush
605 BLOCK_INPUT;
606 XFlush (FRAME_X_DISPLAY (f));
607 UNBLOCK_INPUT;
608 #endif
612 /* This function is called from various places in xdisp.c whenever a
613 complete update has been performed. The global variable
614 updated_window is not available here. */
616 static void
617 XTframe_up_to_date (f)
618 struct frame *f;
620 if (FRAME_X_P (f))
622 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
624 if (dpyinfo->mouse_face_deferred_gc
625 || f == dpyinfo->mouse_face_mouse_frame)
627 BLOCK_INPUT;
628 if (dpyinfo->mouse_face_mouse_frame)
629 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
630 dpyinfo->mouse_face_mouse_x,
631 dpyinfo->mouse_face_mouse_y);
632 dpyinfo->mouse_face_deferred_gc = 0;
633 UNBLOCK_INPUT;
639 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
640 arrow bitmaps, or clear the fringes if no bitmaps are required
641 before DESIRED_ROW is made current. The window being updated is
642 found in updated_window. This function It is called from
643 update_window_line only if it is known that there are differences
644 between bitmaps to be drawn between current row and DESIRED_ROW. */
646 static void
647 x_after_update_window_line (desired_row)
648 struct glyph_row *desired_row;
650 struct window *w = updated_window;
651 struct frame *f;
652 int width, height;
654 xassert (w);
656 if (!desired_row->mode_line_p && !w->pseudo_window_p)
657 desired_row->redraw_fringe_bitmaps_p = 1;
659 /* When a window has disappeared, make sure that no rest of
660 full-width rows stays visible in the internal border. Could
661 check here if updated_window is the leftmost/rightmost window,
662 but I guess it's not worth doing since vertically split windows
663 are almost never used, internal border is rarely set, and the
664 overhead is very small. */
665 if (windows_or_buffers_changed
666 && desired_row->full_width_p
667 && (f = XFRAME (w->frame),
668 width = FRAME_INTERNAL_BORDER_WIDTH (f),
669 width != 0)
670 && (height = desired_row->visible_height,
671 height > 0))
673 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
675 /* Internal border is drawn below the tool bar. */
676 if (WINDOWP (f->tool_bar_window)
677 && w == XWINDOW (f->tool_bar_window))
678 y -= width;
680 BLOCK_INPUT;
681 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
682 0, y, width, height, False);
683 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
684 FRAME_PIXEL_WIDTH (f) - width,
685 y, width, height, False);
686 UNBLOCK_INPUT;
690 static void
691 x_draw_fringe_bitmap (w, row, p)
692 struct window *w;
693 struct glyph_row *row;
694 struct draw_fringe_bitmap_params *p;
696 struct frame *f = XFRAME (WINDOW_FRAME (w));
697 Display *display = FRAME_X_DISPLAY (f);
698 Window window = FRAME_X_WINDOW (f);
699 GC gc = f->output_data.x->normal_gc;
700 struct face *face = p->face;
701 int rowY;
703 /* Must clip because of partially visible lines. */
704 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
705 if (p->y < rowY)
707 /* Adjust position of "bottom aligned" bitmap on partially
708 visible last row. */
709 int oldY = row->y;
710 int oldVH = row->visible_height;
711 row->visible_height = p->h;
712 row->y -= rowY - p->y;
713 x_clip_to_row (w, row, gc);
714 row->y = oldY;
715 row->visible_height = oldVH;
717 else
718 x_clip_to_row (w, row, gc);
720 if (p->bx >= 0 && !p->overlay_p)
722 /* In case the same realized face is used for fringes and
723 for something displayed in the text (e.g. face `region' on
724 mono-displays, the fill style may have been changed to
725 FillSolid in x_draw_glyph_string_background. */
726 if (face->stipple)
727 XSetFillStyle (display, face->gc, FillOpaqueStippled);
728 else
729 XSetForeground (display, face->gc, face->background);
731 XFillRectangle (display, window, face->gc,
732 p->bx, p->by, p->nx, p->ny);
734 if (!face->stipple)
735 XSetForeground (display, face->gc, face->foreground);
738 if (p->which)
740 unsigned char *bits;
741 Pixmap pixmap, clipmask = (Pixmap) 0;
742 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
743 XGCValues gcv;
745 if (p->wd > 8)
746 bits = (unsigned char *)(p->bits + p->dh);
747 else
748 bits = (unsigned char *)p->bits + p->dh;
750 /* Draw the bitmap. I believe these small pixmaps can be cached
751 by the server. */
752 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
753 (p->cursor_p
754 ? (p->overlay_p ? face->background
755 : f->output_data.x->cursor_pixel)
756 : face->foreground),
757 face->background, depth);
759 if (p->overlay_p)
761 clipmask = XCreatePixmapFromBitmapData (display,
762 FRAME_X_DISPLAY_INFO (f)->root_window,
763 bits, p->wd, p->h,
764 1, 0, 1);
765 gcv.clip_mask = clipmask;
766 gcv.clip_x_origin = p->x;
767 gcv.clip_y_origin = p->y;
768 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
771 XCopyArea (display, pixmap, window, gc, 0, 0,
772 p->wd, p->h, p->x, p->y);
773 XFreePixmap (display, pixmap);
775 if (p->overlay_p)
777 gcv.clip_mask = (Pixmap) 0;
778 XChangeGC (display, gc, GCClipMask, &gcv);
779 XFreePixmap (display, clipmask);
783 XSetClipMask (display, gc, None);
788 /* This is called when starting Emacs and when restarting after
789 suspend. When starting Emacs, no X window is mapped. And nothing
790 must be done to Emacs's own window if it is suspended (though that
791 rarely happens). */
793 static void
794 XTset_terminal_modes ()
798 /* This is called when exiting or suspending Emacs. Exiting will make
799 the X-windows go away, and suspending requires no action. */
801 static void
802 XTreset_terminal_modes ()
808 /***********************************************************************
809 Display Iterator
810 ***********************************************************************/
812 /* Function prototypes of this page. */
814 static int x_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
817 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
818 is not contained in the font. */
820 static XCharStruct *
821 x_per_char_metric (font, char2b, font_type)
822 XFontStruct *font;
823 XChar2b *char2b;
824 int font_type; /* unused on X */
826 /* The result metric information. */
827 XCharStruct *pcm = NULL;
829 xassert (font && char2b);
831 if (font->per_char != NULL)
833 if (font->min_byte1 == 0 && font->max_byte1 == 0)
835 /* min_char_or_byte2 specifies the linear character index
836 corresponding to the first element of the per_char array,
837 max_char_or_byte2 is the index of the last character. A
838 character with non-zero CHAR2B->byte1 is not in the font.
839 A character with byte2 less than min_char_or_byte2 or
840 greater max_char_or_byte2 is not in the font. */
841 if (char2b->byte1 == 0
842 && char2b->byte2 >= font->min_char_or_byte2
843 && char2b->byte2 <= font->max_char_or_byte2)
844 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
846 else
848 /* If either min_byte1 or max_byte1 are nonzero, both
849 min_char_or_byte2 and max_char_or_byte2 are less than
850 256, and the 2-byte character index values corresponding
851 to the per_char array element N (counting from 0) are:
853 byte1 = N/D + min_byte1
854 byte2 = N\D + min_char_or_byte2
856 where:
858 D = max_char_or_byte2 - min_char_or_byte2 + 1
859 / = integer division
860 \ = integer modulus */
861 if (char2b->byte1 >= font->min_byte1
862 && char2b->byte1 <= font->max_byte1
863 && char2b->byte2 >= font->min_char_or_byte2
864 && char2b->byte2 <= font->max_char_or_byte2)
866 pcm = (font->per_char
867 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
868 * (char2b->byte1 - font->min_byte1))
869 + (char2b->byte2 - font->min_char_or_byte2));
873 else
875 /* If the per_char pointer is null, all glyphs between the first
876 and last character indexes inclusive have the same
877 information, as given by both min_bounds and max_bounds. */
878 if (char2b->byte2 >= font->min_char_or_byte2
879 && char2b->byte2 <= font->max_char_or_byte2)
880 pcm = &font->max_bounds;
883 return ((pcm == NULL
884 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
885 ? NULL : pcm);
889 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
890 the two-byte form of C. Encoding is returned in *CHAR2B. */
892 static int
893 x_encode_char (c, char2b, font_info, two_byte_p)
894 int c;
895 XChar2b *char2b;
896 struct font_info *font_info;
897 int *two_byte_p;
899 int charset = CHAR_CHARSET (c);
900 XFontStruct *font = font_info->font;
902 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
903 This may be either a program in a special encoder language or a
904 fixed encoding. */
905 if (font_info->font_encoder)
907 /* It's a program. */
908 struct ccl_program *ccl = font_info->font_encoder;
910 if (CHARSET_DIMENSION (charset) == 1)
912 ccl->reg[0] = charset;
913 ccl->reg[1] = char2b->byte2;
914 ccl->reg[2] = -1;
916 else
918 ccl->reg[0] = charset;
919 ccl->reg[1] = char2b->byte1;
920 ccl->reg[2] = char2b->byte2;
923 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
925 /* We assume that MSBs are appropriately set/reset by CCL
926 program. */
927 if (font->max_byte1 == 0) /* 1-byte font */
928 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
929 else
930 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
932 else if (font_info->encoding[charset])
934 /* Fixed encoding scheme. See fontset.h for the meaning of the
935 encoding numbers. */
936 int enc = font_info->encoding[charset];
938 if ((enc == 1 || enc == 2)
939 && CHARSET_DIMENSION (charset) == 2)
940 char2b->byte1 |= 0x80;
942 if (enc == 1 || enc == 3)
943 char2b->byte2 |= 0x80;
946 if (two_byte_p)
947 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
949 return FONT_TYPE_UNKNOWN;
954 /***********************************************************************
955 Glyph display
956 ***********************************************************************/
960 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
961 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
962 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
963 int));
964 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
965 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
966 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
967 static void x_draw_glyph_string P_ ((struct glyph_string *));
968 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
969 static void x_set_cursor_gc P_ ((struct glyph_string *));
970 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
971 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
972 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
973 unsigned long *, double, int));
974 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
975 double, int, unsigned long));
976 static void x_setup_relief_colors P_ ((struct glyph_string *));
977 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
978 static void x_draw_image_relief P_ ((struct glyph_string *));
979 static void x_draw_image_foreground P_ ((struct glyph_string *));
980 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
981 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
982 int, int, int));
983 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
984 int, int, int, int, int, int,
985 XRectangle *));
986 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
987 int, int, int, XRectangle *));
989 #if GLYPH_DEBUG
990 static void x_check_font P_ ((struct frame *, XFontStruct *));
991 #endif
994 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
995 face. */
997 static void
998 x_set_cursor_gc (s)
999 struct glyph_string *s;
1001 if (s->font == FRAME_FONT (s->f)
1002 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1003 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1004 && !s->cmp)
1005 s->gc = s->f->output_data.x->cursor_gc;
1006 else
1008 /* Cursor on non-default face: must merge. */
1009 XGCValues xgcv;
1010 unsigned long mask;
1012 xgcv.background = s->f->output_data.x->cursor_pixel;
1013 xgcv.foreground = s->face->background;
1015 /* If the glyph would be invisible, try a different foreground. */
1016 if (xgcv.foreground == xgcv.background)
1017 xgcv.foreground = s->face->foreground;
1018 if (xgcv.foreground == xgcv.background)
1019 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1020 if (xgcv.foreground == xgcv.background)
1021 xgcv.foreground = s->face->foreground;
1023 /* Make sure the cursor is distinct from text in this face. */
1024 if (xgcv.background == s->face->background
1025 && xgcv.foreground == s->face->foreground)
1027 xgcv.background = s->face->foreground;
1028 xgcv.foreground = s->face->background;
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;
1048 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1050 static void
1051 x_set_mouse_face_gc (s)
1052 struct glyph_string *s;
1054 int face_id;
1055 struct face *face;
1057 /* What face has to be used last for the mouse face? */
1058 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1059 face = FACE_FROM_ID (s->f, face_id);
1060 if (face == NULL)
1061 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1063 if (s->first_glyph->type == CHAR_GLYPH)
1064 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
1065 else
1066 face_id = FACE_FOR_CHAR (s->f, face, 0);
1067 s->face = FACE_FROM_ID (s->f, face_id);
1068 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1070 /* If font in this face is same as S->font, use it. */
1071 if (s->font == s->face->font)
1072 s->gc = s->face->gc;
1073 else
1075 /* Otherwise construct scratch_cursor_gc with values from FACE
1076 but font FONT. */
1077 XGCValues xgcv;
1078 unsigned long mask;
1080 xgcv.background = s->face->background;
1081 xgcv.foreground = s->face->foreground;
1082 IF_DEBUG (x_check_font (s->f, s->font));
1083 xgcv.font = s->font->fid;
1084 xgcv.graphics_exposures = False;
1085 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1087 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1088 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1089 mask, &xgcv);
1090 else
1091 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1092 = XCreateGC (s->display, s->window, mask, &xgcv);
1094 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1097 xassert (s->gc != 0);
1101 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1102 Faces to use in the mode line have already been computed when the
1103 matrix was built, so there isn't much to do, here. */
1105 static INLINE void
1106 x_set_mode_line_face_gc (s)
1107 struct glyph_string *s;
1109 s->gc = s->face->gc;
1113 /* Set S->gc of glyph string S for drawing that glyph string. Set
1114 S->stippled_p to a non-zero value if the face of S has a stipple
1115 pattern. */
1117 static INLINE void
1118 x_set_glyph_string_gc (s)
1119 struct glyph_string *s;
1121 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1123 if (s->hl == DRAW_NORMAL_TEXT)
1125 s->gc = s->face->gc;
1126 s->stippled_p = s->face->stipple != 0;
1128 else if (s->hl == DRAW_INVERSE_VIDEO)
1130 x_set_mode_line_face_gc (s);
1131 s->stippled_p = s->face->stipple != 0;
1133 else if (s->hl == DRAW_CURSOR)
1135 x_set_cursor_gc (s);
1136 s->stippled_p = 0;
1138 else if (s->hl == DRAW_MOUSE_FACE)
1140 x_set_mouse_face_gc (s);
1141 s->stippled_p = s->face->stipple != 0;
1143 else if (s->hl == DRAW_IMAGE_RAISED
1144 || s->hl == DRAW_IMAGE_SUNKEN)
1146 s->gc = s->face->gc;
1147 s->stippled_p = s->face->stipple != 0;
1149 else
1151 s->gc = s->face->gc;
1152 s->stippled_p = s->face->stipple != 0;
1155 /* GC must have been set. */
1156 xassert (s->gc != 0);
1160 /* Set clipping for output of glyph string S. S may be part of a mode
1161 line or menu if we don't have X toolkit support. */
1163 static INLINE void
1164 x_set_glyph_string_clipping (s)
1165 struct glyph_string *s;
1167 XRectangle r;
1168 get_glyph_string_clip_rect (s, &r);
1169 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
1173 /* RIF:
1174 Compute left and right overhang of glyph string S. If S is a glyph
1175 string for a composition, assume overhangs don't exist. */
1177 static void
1178 x_compute_glyph_string_overhangs (s)
1179 struct glyph_string *s;
1181 if (s->cmp == NULL
1182 && s->first_glyph->type == CHAR_GLYPH)
1184 XCharStruct cs;
1185 int direction, font_ascent, font_descent;
1186 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1187 &font_ascent, &font_descent, &cs);
1188 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1189 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1194 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1196 static INLINE void
1197 x_clear_glyph_string_rect (s, x, y, w, h)
1198 struct glyph_string *s;
1199 int x, y, w, h;
1201 XGCValues xgcv;
1202 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1203 XSetForeground (s->display, s->gc, xgcv.background);
1204 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1205 XSetForeground (s->display, s->gc, xgcv.foreground);
1209 /* Draw the background of glyph_string S. If S->background_filled_p
1210 is non-zero don't draw it. FORCE_P non-zero means draw the
1211 background even if it wouldn't be drawn normally. This is used
1212 when a string preceding S draws into the background of S, or S
1213 contains the first component of a composition. */
1215 static void
1216 x_draw_glyph_string_background (s, force_p)
1217 struct glyph_string *s;
1218 int force_p;
1220 /* Nothing to do if background has already been drawn or if it
1221 shouldn't be drawn in the first place. */
1222 if (!s->background_filled_p)
1224 int box_line_width = max (s->face->box_line_width, 0);
1226 if (s->stippled_p)
1228 /* Fill background with a stipple pattern. */
1229 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1230 XFillRectangle (s->display, s->window, s->gc, s->x,
1231 s->y + box_line_width,
1232 s->background_width,
1233 s->height - 2 * box_line_width);
1234 XSetFillStyle (s->display, s->gc, FillSolid);
1235 s->background_filled_p = 1;
1237 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1238 || s->font_not_found_p
1239 || s->extends_to_end_of_line_p
1240 || force_p)
1242 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1243 s->background_width,
1244 s->height - 2 * box_line_width);
1245 s->background_filled_p = 1;
1251 /* Draw the foreground of glyph string S. */
1253 static void
1254 x_draw_glyph_string_foreground (s)
1255 struct glyph_string *s;
1257 int i, x;
1259 /* If first glyph of S has a left box line, start drawing the text
1260 of S to the right of that box line. */
1261 if (s->face->box != FACE_NO_BOX
1262 && s->first_glyph->left_box_line_p)
1263 x = s->x + abs (s->face->box_line_width);
1264 else
1265 x = s->x;
1267 /* Draw characters of S as rectangles if S's font could not be
1268 loaded. */
1269 if (s->font_not_found_p)
1271 for (i = 0; i < s->nchars; ++i)
1273 struct glyph *g = s->first_glyph + i;
1274 XDrawRectangle (s->display, s->window,
1275 s->gc, x, s->y, g->pixel_width - 1,
1276 s->height - 1);
1277 x += g->pixel_width;
1280 else
1282 char *char1b = (char *) s->char2b;
1283 int boff = s->font_info->baseline_offset;
1285 if (s->font_info->vertical_centering)
1286 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1288 /* If we can use 8-bit functions, condense S->char2b. */
1289 if (!s->two_byte_p)
1290 for (i = 0; i < s->nchars; ++i)
1291 char1b[i] = s->char2b[i].byte2;
1293 /* Draw text with XDrawString if background has already been
1294 filled. Otherwise, use XDrawImageString. (Note that
1295 XDrawImageString is usually faster than XDrawString.) Always
1296 use XDrawImageString when drawing the cursor so that there is
1297 no chance that characters under a box cursor are invisible. */
1298 if (s->for_overlaps_p
1299 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1301 /* Draw characters with 16-bit or 8-bit functions. */
1302 if (s->two_byte_p)
1303 XDrawString16 (s->display, s->window, s->gc, x,
1304 s->ybase - boff, s->char2b, s->nchars);
1305 else
1306 XDrawString (s->display, s->window, s->gc, x,
1307 s->ybase - boff, char1b, s->nchars);
1309 else
1311 if (s->two_byte_p)
1312 XDrawImageString16 (s->display, s->window, s->gc, x,
1313 s->ybase - boff, s->char2b, s->nchars);
1314 else
1315 XDrawImageString (s->display, s->window, s->gc, x,
1316 s->ybase - boff, char1b, s->nchars);
1319 if (s->face->overstrike)
1321 /* For overstriking (to simulate bold-face), draw the
1322 characters again shifted to the right by one pixel. */
1323 if (s->two_byte_p)
1324 XDrawString16 (s->display, s->window, s->gc, x + 1,
1325 s->ybase - boff, s->char2b, s->nchars);
1326 else
1327 XDrawString (s->display, s->window, s->gc, x + 1,
1328 s->ybase - boff, char1b, s->nchars);
1333 /* Draw the foreground of composite glyph string S. */
1335 static void
1336 x_draw_composite_glyph_string_foreground (s)
1337 struct glyph_string *s;
1339 int i, x;
1341 /* If first glyph of S has a left box line, start drawing the text
1342 of S to the right of that box line. */
1343 if (s->face->box != FACE_NO_BOX
1344 && s->first_glyph->left_box_line_p)
1345 x = s->x + abs (s->face->box_line_width);
1346 else
1347 x = s->x;
1349 /* S is a glyph string for a composition. S->gidx is the index of
1350 the first character drawn for glyphs of this composition.
1351 S->gidx == 0 means we are drawing the very first character of
1352 this composition. */
1354 /* Draw a rectangle for the composition if the font for the very
1355 first character of the composition could not be loaded. */
1356 if (s->font_not_found_p)
1358 if (s->gidx == 0)
1359 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1360 s->width - 1, s->height - 1);
1362 else
1364 for (i = 0; i < s->nchars; i++, ++s->gidx)
1366 XDrawString16 (s->display, s->window, s->gc,
1367 x + s->cmp->offsets[s->gidx * 2],
1368 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1369 s->char2b + i, 1);
1370 if (s->face->overstrike)
1371 XDrawString16 (s->display, s->window, s->gc,
1372 x + s->cmp->offsets[s->gidx * 2] + 1,
1373 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1374 s->char2b + i, 1);
1380 #ifdef USE_X_TOOLKIT
1382 static struct frame *x_frame_of_widget P_ ((Widget));
1383 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1384 XrmValue *, XrmValue *, XtPointer *));
1385 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1386 XrmValue *, Cardinal *));
1389 /* Return the frame on which widget WIDGET is used.. Abort if frame
1390 cannot be determined. */
1392 static struct frame *
1393 x_frame_of_widget (widget)
1394 Widget widget;
1396 struct x_display_info *dpyinfo;
1397 Lisp_Object tail;
1398 struct frame *f;
1400 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1402 /* Find the top-level shell of the widget. Note that this function
1403 can be called when the widget is not yet realized, so XtWindow
1404 (widget) == 0. That's the reason we can't simply use
1405 x_any_window_to_frame. */
1406 while (!XtIsTopLevelShell (widget))
1407 widget = XtParent (widget);
1409 /* Look for a frame with that top-level widget. Allocate the color
1410 on that frame to get the right gamma correction value. */
1411 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1412 if (GC_FRAMEP (XCAR (tail))
1413 && (f = XFRAME (XCAR (tail)),
1414 (f->output_data.nothing != 1
1415 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1416 && f->output_data.x->widget == widget)
1417 return f;
1419 abort ();
1423 /* Allocate the color COLOR->pixel on the screen and display of
1424 widget WIDGET in colormap CMAP. If an exact match cannot be
1425 allocated, try the nearest color available. Value is non-zero
1426 if successful. This is called from lwlib. */
1429 x_alloc_nearest_color_for_widget (widget, cmap, color)
1430 Widget widget;
1431 Colormap cmap;
1432 XColor *color;
1434 struct frame *f = x_frame_of_widget (widget);
1435 return x_alloc_nearest_color (f, cmap, color);
1439 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1440 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1441 If this produces the same color as PIXEL, try a color where all RGB
1442 values have DELTA added. Return the allocated color in *PIXEL.
1443 DISPLAY is the X display, CMAP is the colormap to operate on.
1444 Value is non-zero if successful. */
1447 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1448 Widget widget;
1449 Display *display;
1450 Colormap cmap;
1451 unsigned long *pixel;
1452 double factor;
1453 int delta;
1455 struct frame *f = x_frame_of_widget (widget);
1456 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1460 /* Structure specifying which arguments should be passed by Xt to
1461 cvt_string_to_pixel. We want the widget's screen and colormap. */
1463 static XtConvertArgRec cvt_string_to_pixel_args[] =
1465 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1466 sizeof (Screen *)},
1467 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1468 sizeof (Colormap)}
1472 /* The address of this variable is returned by
1473 cvt_string_to_pixel. */
1475 static Pixel cvt_string_to_pixel_value;
1478 /* Convert a color name to a pixel color.
1480 DPY is the display we are working on.
1482 ARGS is an array of *NARGS XrmValue structures holding additional
1483 information about the widget for which the conversion takes place.
1484 The contents of this array are determined by the specification
1485 in cvt_string_to_pixel_args.
1487 FROM is a pointer to an XrmValue which points to the color name to
1488 convert. TO is an XrmValue in which to return the pixel color.
1490 CLOSURE_RET is a pointer to user-data, in which we record if
1491 we allocated the color or not.
1493 Value is True if successful, False otherwise. */
1495 static Boolean
1496 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1497 Display *dpy;
1498 XrmValue *args;
1499 Cardinal *nargs;
1500 XrmValue *from, *to;
1501 XtPointer *closure_ret;
1503 Screen *screen;
1504 Colormap cmap;
1505 Pixel pixel;
1506 String color_name;
1507 XColor color;
1509 if (*nargs != 2)
1511 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1512 "wrongParameters", "cvt_string_to_pixel",
1513 "XtToolkitError",
1514 "Screen and colormap args required", NULL, NULL);
1515 return False;
1518 screen = *(Screen **) args[0].addr;
1519 cmap = *(Colormap *) args[1].addr;
1520 color_name = (String) from->addr;
1522 if (strcmp (color_name, XtDefaultBackground) == 0)
1524 *closure_ret = (XtPointer) False;
1525 pixel = WhitePixelOfScreen (screen);
1527 else if (strcmp (color_name, XtDefaultForeground) == 0)
1529 *closure_ret = (XtPointer) False;
1530 pixel = BlackPixelOfScreen (screen);
1532 else if (XParseColor (dpy, cmap, color_name, &color)
1533 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1535 pixel = color.pixel;
1536 *closure_ret = (XtPointer) True;
1538 else
1540 String params[1];
1541 Cardinal nparams = 1;
1543 params[0] = color_name;
1544 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1545 "badValue", "cvt_string_to_pixel",
1546 "XtToolkitError", "Invalid color `%s'",
1547 params, &nparams);
1548 return False;
1551 if (to->addr != NULL)
1553 if (to->size < sizeof (Pixel))
1555 to->size = sizeof (Pixel);
1556 return False;
1559 *(Pixel *) to->addr = pixel;
1561 else
1563 cvt_string_to_pixel_value = pixel;
1564 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1567 to->size = sizeof (Pixel);
1568 return True;
1572 /* Free a pixel color which was previously allocated via
1573 cvt_string_to_pixel. This is registered as the destructor
1574 for this type of resource via XtSetTypeConverter.
1576 APP is the application context in which we work.
1578 TO is a pointer to an XrmValue holding the color to free.
1579 CLOSURE is the value we stored in CLOSURE_RET for this color
1580 in cvt_string_to_pixel.
1582 ARGS and NARGS are like for cvt_string_to_pixel. */
1584 static void
1585 cvt_pixel_dtor (app, to, closure, args, nargs)
1586 XtAppContext app;
1587 XrmValuePtr to;
1588 XtPointer closure;
1589 XrmValuePtr args;
1590 Cardinal *nargs;
1592 if (*nargs != 2)
1594 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1595 "XtToolkitError",
1596 "Screen and colormap arguments required",
1597 NULL, NULL);
1599 else if (closure != NULL)
1601 /* We did allocate the pixel, so free it. */
1602 Screen *screen = *(Screen **) args[0].addr;
1603 Colormap cmap = *(Colormap *) args[1].addr;
1604 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1605 (Pixel *) to->addr, 1);
1610 #endif /* USE_X_TOOLKIT */
1613 /* Value is an array of XColor structures for the contents of the
1614 color map of display DPY. Set *NCELLS to the size of the array.
1615 Note that this probably shouldn't be called for large color maps,
1616 say a 24-bit TrueColor map. */
1618 static const XColor *
1619 x_color_cells (dpy, ncells)
1620 Display *dpy;
1621 int *ncells;
1623 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1625 if (dpyinfo->color_cells == NULL)
1627 Screen *screen = dpyinfo->screen;
1628 int i;
1630 dpyinfo->ncolor_cells
1631 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1632 dpyinfo->color_cells
1633 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1634 * sizeof *dpyinfo->color_cells);
1636 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1637 dpyinfo->color_cells[i].pixel = i;
1639 XQueryColors (dpy, dpyinfo->cmap,
1640 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1643 *ncells = dpyinfo->ncolor_cells;
1644 return dpyinfo->color_cells;
1648 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1649 colors in COLORS. Use cached information, if available. */
1651 void
1652 x_query_colors (f, colors, ncolors)
1653 struct frame *f;
1654 XColor *colors;
1655 int ncolors;
1657 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1659 if (dpyinfo->color_cells)
1661 int i;
1662 for (i = 0; i < ncolors; ++i)
1664 unsigned long pixel = colors[i].pixel;
1665 xassert (pixel < dpyinfo->ncolor_cells);
1666 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1667 colors[i] = dpyinfo->color_cells[pixel];
1670 else
1671 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1675 /* On frame F, translate pixel color to RGB values for the color in
1676 COLOR. Use cached information, if available. */
1678 void
1679 x_query_color (f, color)
1680 struct frame *f;
1681 XColor *color;
1683 x_query_colors (f, color, 1);
1687 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1688 exact match can't be allocated, try the nearest color available.
1689 Value is non-zero if successful. Set *COLOR to the color
1690 allocated. */
1692 static int
1693 x_alloc_nearest_color_1 (dpy, cmap, color)
1694 Display *dpy;
1695 Colormap cmap;
1696 XColor *color;
1698 int rc;
1700 rc = XAllocColor (dpy, cmap, color);
1701 if (rc == 0)
1703 /* If we got to this point, the colormap is full, so we're going
1704 to try to get the next closest color. The algorithm used is
1705 a least-squares matching, which is what X uses for closest
1706 color matching with StaticColor visuals. */
1707 int nearest, i;
1708 unsigned long nearest_delta = ~0;
1709 int ncells;
1710 const XColor *cells = x_color_cells (dpy, &ncells);
1712 for (nearest = i = 0; i < ncells; ++i)
1714 long dred = (color->red >> 8) - (cells[i].red >> 8);
1715 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1716 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1717 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1719 if (delta < nearest_delta)
1721 nearest = i;
1722 nearest_delta = delta;
1726 color->red = cells[nearest].red;
1727 color->green = cells[nearest].green;
1728 color->blue = cells[nearest].blue;
1729 rc = XAllocColor (dpy, cmap, color);
1731 else
1733 /* If allocation succeeded, and the allocated pixel color is not
1734 equal to a cached pixel color recorded earlier, there was a
1735 change in the colormap, so clear the color cache. */
1736 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1737 XColor *cached_color;
1739 if (dpyinfo->color_cells
1740 && (cached_color = &dpyinfo->color_cells[color->pixel],
1741 (cached_color->red != color->red
1742 || cached_color->blue != color->blue
1743 || cached_color->green != color->green)))
1745 xfree (dpyinfo->color_cells);
1746 dpyinfo->color_cells = NULL;
1747 dpyinfo->ncolor_cells = 0;
1751 #ifdef DEBUG_X_COLORS
1752 if (rc)
1753 register_color (color->pixel);
1754 #endif /* DEBUG_X_COLORS */
1756 return rc;
1760 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1761 exact match can't be allocated, try the nearest color available.
1762 Value is non-zero if successful. Set *COLOR to the color
1763 allocated. */
1766 x_alloc_nearest_color (f, cmap, color)
1767 struct frame *f;
1768 Colormap cmap;
1769 XColor *color;
1771 gamma_correct (f, color);
1772 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1776 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1777 It's necessary to do this instead of just using PIXEL directly to
1778 get color reference counts right. */
1780 unsigned long
1781 x_copy_color (f, pixel)
1782 struct frame *f;
1783 unsigned long pixel;
1785 XColor color;
1787 color.pixel = pixel;
1788 BLOCK_INPUT;
1789 x_query_color (f, &color);
1790 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1791 UNBLOCK_INPUT;
1792 #ifdef DEBUG_X_COLORS
1793 register_color (pixel);
1794 #endif
1795 return color.pixel;
1799 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1800 It's necessary to do this instead of just using PIXEL directly to
1801 get color reference counts right. */
1803 unsigned long
1804 x_copy_dpy_color (dpy, cmap, pixel)
1805 Display *dpy;
1806 Colormap cmap;
1807 unsigned long pixel;
1809 XColor color;
1811 color.pixel = pixel;
1812 BLOCK_INPUT;
1813 XQueryColor (dpy, cmap, &color);
1814 XAllocColor (dpy, cmap, &color);
1815 UNBLOCK_INPUT;
1816 #ifdef DEBUG_X_COLORS
1817 register_color (pixel);
1818 #endif
1819 return color.pixel;
1823 /* Brightness beyond which a color won't have its highlight brightness
1824 boosted.
1826 Nominally, highlight colors for `3d' faces are calculated by
1827 brightening an object's color by a constant scale factor, but this
1828 doesn't yield good results for dark colors, so for colors who's
1829 brightness is less than this value (on a scale of 0-65535) have an
1830 use an additional additive factor.
1832 The value here is set so that the default menu-bar/mode-line color
1833 (grey75) will not have its highlights changed at all. */
1834 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1837 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1838 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1839 If this produces the same color as PIXEL, try a color where all RGB
1840 values have DELTA added. Return the allocated color in *PIXEL.
1841 DISPLAY is the X display, CMAP is the colormap to operate on.
1842 Value is non-zero if successful. */
1844 static int
1845 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1846 struct frame *f;
1847 Display *display;
1848 Colormap cmap;
1849 unsigned long *pixel;
1850 double factor;
1851 int delta;
1853 XColor color, new;
1854 long bright;
1855 int success_p;
1857 /* Get RGB color values. */
1858 color.pixel = *pixel;
1859 x_query_color (f, &color);
1861 /* Change RGB values by specified FACTOR. Avoid overflow! */
1862 xassert (factor >= 0);
1863 new.red = min (0xffff, factor * color.red);
1864 new.green = min (0xffff, factor * color.green);
1865 new.blue = min (0xffff, factor * color.blue);
1867 /* Calculate brightness of COLOR. */
1868 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1870 /* We only boost colors that are darker than
1871 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1872 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1873 /* Make an additive adjustment to NEW, because it's dark enough so
1874 that scaling by FACTOR alone isn't enough. */
1876 /* How far below the limit this color is (0 - 1, 1 being darker). */
1877 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1878 /* The additive adjustment. */
1879 int min_delta = delta * dimness * factor / 2;
1881 if (factor < 1)
1883 new.red = max (0, new.red - min_delta);
1884 new.green = max (0, new.green - min_delta);
1885 new.blue = max (0, new.blue - min_delta);
1887 else
1889 new.red = min (0xffff, min_delta + new.red);
1890 new.green = min (0xffff, min_delta + new.green);
1891 new.blue = min (0xffff, min_delta + new.blue);
1895 /* Try to allocate the color. */
1896 success_p = x_alloc_nearest_color (f, cmap, &new);
1897 if (success_p)
1899 if (new.pixel == *pixel)
1901 /* If we end up with the same color as before, try adding
1902 delta to the RGB values. */
1903 x_free_colors (f, &new.pixel, 1);
1905 new.red = min (0xffff, delta + color.red);
1906 new.green = min (0xffff, delta + color.green);
1907 new.blue = min (0xffff, delta + color.blue);
1908 success_p = x_alloc_nearest_color (f, cmap, &new);
1910 else
1911 success_p = 1;
1912 *pixel = new.pixel;
1915 return success_p;
1919 /* Set up the foreground color for drawing relief lines of glyph
1920 string S. RELIEF is a pointer to a struct relief containing the GC
1921 with which lines will be drawn. Use a color that is FACTOR or
1922 DELTA lighter or darker than the relief's background which is found
1923 in S->f->output_data.x->relief_background. If such a color cannot
1924 be allocated, use DEFAULT_PIXEL, instead. */
1926 static void
1927 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1928 struct frame *f;
1929 struct relief *relief;
1930 double factor;
1931 int delta;
1932 unsigned long default_pixel;
1934 XGCValues xgcv;
1935 struct x_output *di = f->output_data.x;
1936 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1937 unsigned long pixel;
1938 unsigned long background = di->relief_background;
1939 Colormap cmap = FRAME_X_COLORMAP (f);
1940 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1941 Display *dpy = FRAME_X_DISPLAY (f);
1943 xgcv.graphics_exposures = False;
1944 xgcv.line_width = 1;
1946 /* Free previously allocated color. The color cell will be reused
1947 when it has been freed as many times as it was allocated, so this
1948 doesn't affect faces using the same colors. */
1949 if (relief->gc
1950 && relief->allocated_p)
1952 x_free_colors (f, &relief->pixel, 1);
1953 relief->allocated_p = 0;
1956 /* Allocate new color. */
1957 xgcv.foreground = default_pixel;
1958 pixel = background;
1959 if (dpyinfo->n_planes != 1
1960 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1962 relief->allocated_p = 1;
1963 xgcv.foreground = relief->pixel = pixel;
1966 if (relief->gc == 0)
1968 xgcv.stipple = dpyinfo->gray;
1969 mask |= GCStipple;
1970 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1972 else
1973 XChangeGC (dpy, relief->gc, mask, &xgcv);
1977 /* Set up colors for the relief lines around glyph string S. */
1979 static void
1980 x_setup_relief_colors (s)
1981 struct glyph_string *s;
1983 struct x_output *di = s->f->output_data.x;
1984 unsigned long color;
1986 if (s->face->use_box_color_for_shadows_p)
1987 color = s->face->box_color;
1988 else if (s->first_glyph->type == IMAGE_GLYPH
1989 && s->img->pixmap
1990 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1991 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1992 else
1994 XGCValues xgcv;
1996 /* Get the background color of the face. */
1997 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
1998 color = xgcv.background;
2001 if (di->white_relief.gc == 0
2002 || color != di->relief_background)
2004 di->relief_background = color;
2005 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2006 WHITE_PIX_DEFAULT (s->f));
2007 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2008 BLACK_PIX_DEFAULT (s->f));
2013 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2014 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2015 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2016 relief. LEFT_P non-zero means draw a relief on the left side of
2017 the rectangle. RIGHT_P non-zero means draw a relief on the right
2018 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2019 when drawing. */
2021 static void
2022 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2023 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2024 struct frame *f;
2025 int left_x, top_y, right_x, bottom_y, width;
2026 int top_p, bot_p, left_p, right_p, raised_p;
2027 XRectangle *clip_rect;
2029 Display *dpy = FRAME_X_DISPLAY (f);
2030 Window window = FRAME_X_WINDOW (f);
2031 int i;
2032 GC gc;
2034 if (raised_p)
2035 gc = f->output_data.x->white_relief.gc;
2036 else
2037 gc = f->output_data.x->black_relief.gc;
2038 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2040 /* Top. */
2041 if (top_p)
2042 for (i = 0; i < width; ++i)
2043 XDrawLine (dpy, window, gc,
2044 left_x + i * left_p, top_y + i,
2045 right_x + 1 - i * right_p, top_y + i);
2047 /* Left. */
2048 if (left_p)
2049 for (i = 0; i < width; ++i)
2050 XDrawLine (dpy, window, gc,
2051 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2053 XSetClipMask (dpy, gc, None);
2054 if (raised_p)
2055 gc = f->output_data.x->black_relief.gc;
2056 else
2057 gc = f->output_data.x->white_relief.gc;
2058 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2060 /* Bottom. */
2061 if (bot_p)
2062 for (i = 0; i < width; ++i)
2063 XDrawLine (dpy, window, gc,
2064 left_x + i * left_p, bottom_y - i,
2065 right_x + 1 - i * right_p, bottom_y - i);
2067 /* Right. */
2068 if (right_p)
2069 for (i = 0; i < width; ++i)
2070 XDrawLine (dpy, window, gc,
2071 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2073 XSetClipMask (dpy, gc, None);
2077 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2078 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2079 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2080 left side of the rectangle. RIGHT_P non-zero means draw a line
2081 on the right side of the rectangle. CLIP_RECT is the clipping
2082 rectangle to use when drawing. */
2084 static void
2085 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2086 left_p, right_p, clip_rect)
2087 struct glyph_string *s;
2088 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2089 XRectangle *clip_rect;
2091 XGCValues xgcv;
2093 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2094 XSetForeground (s->display, s->gc, s->face->box_color);
2095 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2097 /* Top. */
2098 XFillRectangle (s->display, s->window, s->gc,
2099 left_x, top_y, right_x - left_x + 1, width);
2101 /* Left. */
2102 if (left_p)
2103 XFillRectangle (s->display, s->window, s->gc,
2104 left_x, top_y, width, bottom_y - top_y + 1);
2106 /* Bottom. */
2107 XFillRectangle (s->display, s->window, s->gc,
2108 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2110 /* Right. */
2111 if (right_p)
2112 XFillRectangle (s->display, s->window, s->gc,
2113 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2115 XSetForeground (s->display, s->gc, xgcv.foreground);
2116 XSetClipMask (s->display, s->gc, None);
2120 /* Draw a box around glyph string S. */
2122 static void
2123 x_draw_glyph_string_box (s)
2124 struct glyph_string *s;
2126 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2127 int left_p, right_p;
2128 struct glyph *last_glyph;
2129 XRectangle clip_rect;
2131 last_x = window_box_right (s->w, s->area);
2132 if (s->row->full_width_p
2133 && !s->w->pseudo_window_p)
2135 last_x += WINDOW_RIGHT_SCROLL_BAR_AREA_WIDTH (s->w);
2136 if (s->area != RIGHT_MARGIN_AREA
2137 || WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (s->w))
2138 last_x += WINDOW_RIGHT_FRINGE_WIDTH (s->w);
2141 /* The glyph that may have a right box line. */
2142 last_glyph = (s->cmp || s->img
2143 ? s->first_glyph
2144 : s->first_glyph + s->nchars - 1);
2146 width = abs (s->face->box_line_width);
2147 raised_p = s->face->box == FACE_RAISED_BOX;
2148 left_x = s->x;
2149 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2150 ? last_x - 1
2151 : min (last_x, s->x + s->background_width) - 1);
2152 top_y = s->y;
2153 bottom_y = top_y + s->height - 1;
2155 left_p = (s->first_glyph->left_box_line_p
2156 || (s->hl == DRAW_MOUSE_FACE
2157 && (s->prev == NULL
2158 || s->prev->hl != s->hl)));
2159 right_p = (last_glyph->right_box_line_p
2160 || (s->hl == DRAW_MOUSE_FACE
2161 && (s->next == NULL
2162 || s->next->hl != s->hl)));
2164 get_glyph_string_clip_rect (s, &clip_rect);
2166 if (s->face->box == FACE_SIMPLE_BOX)
2167 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2168 left_p, right_p, &clip_rect);
2169 else
2171 x_setup_relief_colors (s);
2172 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2173 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2178 /* Draw foreground of image glyph string S. */
2180 static void
2181 x_draw_image_foreground (s)
2182 struct glyph_string *s;
2184 int x = s->x;
2185 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2187 /* If first glyph of S has a left box line, start drawing it to the
2188 right of that line. */
2189 if (s->face->box != FACE_NO_BOX
2190 && s->first_glyph->left_box_line_p
2191 && s->slice.x == 0)
2192 x += abs (s->face->box_line_width);
2194 /* If there is a margin around the image, adjust x- and y-position
2195 by that margin. */
2196 if (s->slice.x == 0)
2197 x += s->img->hmargin;
2198 if (s->slice.y == 0)
2199 y += s->img->vmargin;
2201 if (s->img->pixmap)
2203 if (s->img->mask)
2205 /* We can't set both a clip mask and use XSetClipRectangles
2206 because the latter also sets a clip mask. We also can't
2207 trust on the shape extension to be available
2208 (XShapeCombineRegion). So, compute the rectangle to draw
2209 manually. */
2210 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2211 | GCFunction);
2212 XGCValues xgcv;
2213 XRectangle clip_rect, image_rect, r;
2215 xgcv.clip_mask = s->img->mask;
2216 xgcv.clip_x_origin = x;
2217 xgcv.clip_y_origin = y;
2218 xgcv.function = GXcopy;
2219 XChangeGC (s->display, s->gc, mask, &xgcv);
2221 get_glyph_string_clip_rect (s, &clip_rect);
2222 image_rect.x = x;
2223 image_rect.y = y;
2224 image_rect.width = s->slice.width;
2225 image_rect.height = s->slice.height;
2226 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2227 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2228 s->slice.x + r.x - x, s->slice.y + r.y - y,
2229 r.width, r.height, r.x, r.y);
2231 else
2233 XRectangle clip_rect, image_rect, r;
2235 get_glyph_string_clip_rect (s, &clip_rect);
2236 image_rect.x = x;
2237 image_rect.y = y;
2238 image_rect.width = s->slice.width;
2239 image_rect.height = s->slice.height;
2240 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2241 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2242 s->slice.x + r.x - x, s->slice.y + r.y - y,
2243 r.width, r.height, r.x, r.y);
2245 /* When the image has a mask, we can expect that at
2246 least part of a mouse highlight or a block cursor will
2247 be visible. If the image doesn't have a mask, make
2248 a block cursor visible by drawing a rectangle around
2249 the image. I believe it's looking better if we do
2250 nothing here for mouse-face. */
2251 if (s->hl == DRAW_CURSOR)
2253 int r = s->img->relief;
2254 if (r < 0) r = -r;
2255 XDrawRectangle (s->display, s->window, s->gc,
2256 x - r, y - r,
2257 s->slice.width + r*2 - 1,
2258 s->slice.height + r*2 - 1);
2262 else
2263 /* Draw a rectangle if image could not be loaded. */
2264 XDrawRectangle (s->display, s->window, s->gc, x, y,
2265 s->slice.width - 1, s->slice.height - 1);
2269 /* Draw a relief around the image glyph string S. */
2271 static void
2272 x_draw_image_relief (s)
2273 struct glyph_string *s;
2275 int x0, y0, x1, y1, thick, raised_p;
2276 XRectangle r;
2277 int x = s->x;
2278 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2280 /* If first glyph of S has a left box line, start drawing it to the
2281 right of that line. */
2282 if (s->face->box != FACE_NO_BOX
2283 && s->first_glyph->left_box_line_p
2284 && s->slice.x == 0)
2285 x += abs (s->face->box_line_width);
2287 /* If there is a margin around the image, adjust x- and y-position
2288 by that margin. */
2289 if (s->slice.x == 0)
2290 x += s->img->hmargin;
2291 if (s->slice.y == 0)
2292 y += s->img->vmargin;
2294 if (s->hl == DRAW_IMAGE_SUNKEN
2295 || s->hl == DRAW_IMAGE_RAISED)
2297 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2298 raised_p = s->hl == DRAW_IMAGE_RAISED;
2300 else
2302 thick = abs (s->img->relief);
2303 raised_p = s->img->relief > 0;
2306 x0 = x - thick;
2307 y0 = y - thick;
2308 x1 = x + s->slice.width + thick - 1;
2309 y1 = y + s->slice.height + thick - 1;
2311 x_setup_relief_colors (s);
2312 get_glyph_string_clip_rect (s, &r);
2313 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2314 s->slice.y == 0,
2315 s->slice.y + s->slice.height == s->img->height,
2316 s->slice.x == 0,
2317 s->slice.x + s->slice.width == s->img->width,
2318 &r);
2322 /* Draw the foreground of image glyph string S to PIXMAP. */
2324 static void
2325 x_draw_image_foreground_1 (s, pixmap)
2326 struct glyph_string *s;
2327 Pixmap pixmap;
2329 int x = 0;
2330 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2332 /* If first glyph of S has a left box line, start drawing it to the
2333 right of that line. */
2334 if (s->face->box != FACE_NO_BOX
2335 && s->first_glyph->left_box_line_p
2336 && s->slice.x == 0)
2337 x += abs (s->face->box_line_width);
2339 /* If there is a margin around the image, adjust x- and y-position
2340 by that margin. */
2341 if (s->slice.x == 0)
2342 x += s->img->hmargin;
2343 if (s->slice.y == 0)
2344 y += s->img->vmargin;
2346 if (s->img->pixmap)
2348 if (s->img->mask)
2350 /* We can't set both a clip mask and use XSetClipRectangles
2351 because the latter also sets a clip mask. We also can't
2352 trust on the shape extension to be available
2353 (XShapeCombineRegion). So, compute the rectangle to draw
2354 manually. */
2355 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2356 | GCFunction);
2357 XGCValues xgcv;
2359 xgcv.clip_mask = s->img->mask;
2360 xgcv.clip_x_origin = x;
2361 xgcv.clip_y_origin = y;
2362 xgcv.function = GXcopy;
2363 XChangeGC (s->display, s->gc, mask, &xgcv);
2365 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2366 s->slice.x, s->slice.y,
2367 s->slice.width, s->slice.height, x, y);
2368 XSetClipMask (s->display, s->gc, None);
2370 else
2372 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2373 s->slice.x, s->slice.y,
2374 s->slice.width, s->slice.height, x, y);
2376 /* When the image has a mask, we can expect that at
2377 least part of a mouse highlight or a block cursor will
2378 be visible. If the image doesn't have a mask, make
2379 a block cursor visible by drawing a rectangle around
2380 the image. I believe it's looking better if we do
2381 nothing here for mouse-face. */
2382 if (s->hl == DRAW_CURSOR)
2384 int r = s->img->relief;
2385 if (r < 0) r = -r;
2386 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2387 s->slice.width + r*2 - 1,
2388 s->slice.height + r*2 - 1);
2392 else
2393 /* Draw a rectangle if image could not be loaded. */
2394 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2395 s->slice.width - 1, s->slice.height - 1);
2399 /* Draw part of the background of glyph string S. X, Y, W, and H
2400 give the rectangle to draw. */
2402 static void
2403 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2404 struct glyph_string *s;
2405 int x, y, w, h;
2407 if (s->stippled_p)
2409 /* Fill background with a stipple pattern. */
2410 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2411 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2412 XSetFillStyle (s->display, s->gc, FillSolid);
2414 else
2415 x_clear_glyph_string_rect (s, x, y, w, h);
2419 /* Draw image glyph string S.
2421 s->y
2422 s->x +-------------------------
2423 | s->face->box
2425 | +-------------------------
2426 | | s->img->margin
2428 | | +-------------------
2429 | | | the image
2433 static void
2434 x_draw_image_glyph_string (s)
2435 struct glyph_string *s;
2437 int box_line_hwidth = abs (s->face->box_line_width);
2438 int box_line_vwidth = max (s->face->box_line_width, 0);
2439 int height;
2440 Pixmap pixmap = None;
2442 height = s->height;
2443 if (s->slice.y == 0)
2444 height -= box_line_vwidth;
2445 if (s->slice.y + s->slice.height >= s->img->height)
2446 height -= box_line_vwidth;
2448 /* Fill background with face under the image. Do it only if row is
2449 taller than image or if image has a clip mask to reduce
2450 flickering. */
2451 s->stippled_p = s->face->stipple != 0;
2452 if (height > s->slice.height
2453 || s->img->hmargin
2454 || s->img->vmargin
2455 || s->img->mask
2456 || s->img->pixmap == 0
2457 || s->width != s->background_width)
2459 if (s->img->mask)
2461 /* Create a pixmap as large as the glyph string. Fill it
2462 with the background color. Copy the image to it, using
2463 its mask. Copy the temporary pixmap to the display. */
2464 Screen *screen = FRAME_X_SCREEN (s->f);
2465 int depth = DefaultDepthOfScreen (screen);
2467 /* Create a pixmap as large as the glyph string. */
2468 pixmap = XCreatePixmap (s->display, s->window,
2469 s->background_width,
2470 s->height, depth);
2472 /* Don't clip in the following because we're working on the
2473 pixmap. */
2474 XSetClipMask (s->display, s->gc, None);
2476 /* Fill the pixmap with the background color/stipple. */
2477 if (s->stippled_p)
2479 /* Fill background with a stipple pattern. */
2480 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2481 XFillRectangle (s->display, pixmap, s->gc,
2482 0, 0, s->background_width, s->height);
2483 XSetFillStyle (s->display, s->gc, FillSolid);
2485 else
2487 XGCValues xgcv;
2488 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2489 &xgcv);
2490 XSetForeground (s->display, s->gc, xgcv.background);
2491 XFillRectangle (s->display, pixmap, s->gc,
2492 0, 0, s->background_width, s->height);
2493 XSetForeground (s->display, s->gc, xgcv.foreground);
2496 else
2498 int x = s->x;
2499 int y = s->y;
2501 if (s->first_glyph->left_box_line_p
2502 && s->slice.x == 0)
2503 x += box_line_hwidth;
2505 if (s->slice.y == 0)
2506 y += box_line_vwidth;
2508 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2511 s->background_filled_p = 1;
2514 /* Draw the foreground. */
2515 if (pixmap != None)
2517 x_draw_image_foreground_1 (s, pixmap);
2518 x_set_glyph_string_clipping (s);
2519 XCopyArea (s->display, pixmap, s->window, s->gc,
2520 0, 0, s->background_width, s->height, s->x, s->y);
2521 XFreePixmap (s->display, pixmap);
2523 else
2524 x_draw_image_foreground (s);
2526 /* If we must draw a relief around the image, do it. */
2527 if (s->img->relief
2528 || s->hl == DRAW_IMAGE_RAISED
2529 || s->hl == DRAW_IMAGE_SUNKEN)
2530 x_draw_image_relief (s);
2534 /* Draw stretch glyph string S. */
2536 static void
2537 x_draw_stretch_glyph_string (s)
2538 struct glyph_string *s;
2540 xassert (s->first_glyph->type == STRETCH_GLYPH);
2541 s->stippled_p = s->face->stipple != 0;
2543 if (s->hl == DRAW_CURSOR
2544 && !x_stretch_cursor_p)
2546 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2547 as wide as the stretch glyph. */
2548 int width = min (FRAME_COLUMN_WIDTH (s->f), s->background_width);
2550 /* Draw cursor. */
2551 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
2553 /* Clear rest using the GC of the original non-cursor face. */
2554 if (width < s->background_width)
2556 int x = s->x + width, y = s->y;
2557 int w = s->background_width - width, h = s->height;
2558 XRectangle r;
2559 GC gc;
2561 if (s->row->mouse_face_p
2562 && cursor_in_mouse_face_p (s->w))
2564 x_set_mouse_face_gc (s);
2565 gc = s->gc;
2567 else
2568 gc = s->face->gc;
2570 get_glyph_string_clip_rect (s, &r);
2571 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2573 if (s->face->stipple)
2575 /* Fill background with a stipple pattern. */
2576 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2577 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2578 XSetFillStyle (s->display, gc, FillSolid);
2580 else
2582 XGCValues xgcv;
2583 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2584 XSetForeground (s->display, gc, xgcv.background);
2585 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2586 XSetForeground (s->display, gc, xgcv.foreground);
2590 else if (!s->background_filled_p)
2591 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
2592 s->height);
2594 s->background_filled_p = 1;
2598 /* Draw glyph string S. */
2600 static void
2601 x_draw_glyph_string (s)
2602 struct glyph_string *s;
2604 int relief_drawn_p = 0;
2606 /* If S draws into the background of its successor, draw the
2607 background of the successor first so that S can draw into it.
2608 This makes S->next use XDrawString instead of XDrawImageString. */
2609 if (s->next && s->right_overhang && !s->for_overlaps_p)
2611 xassert (s->next->img == NULL);
2612 x_set_glyph_string_gc (s->next);
2613 x_set_glyph_string_clipping (s->next);
2614 x_draw_glyph_string_background (s->next, 1);
2617 /* Set up S->gc, set clipping and draw S. */
2618 x_set_glyph_string_gc (s);
2620 /* Draw relief (if any) in advance for char/composition so that the
2621 glyph string can be drawn over it. */
2622 if (!s->for_overlaps_p
2623 && s->face->box != FACE_NO_BOX
2624 && (s->first_glyph->type == CHAR_GLYPH
2625 || s->first_glyph->type == COMPOSITE_GLYPH))
2628 x_set_glyph_string_clipping (s);
2629 x_draw_glyph_string_background (s, 1);
2630 x_draw_glyph_string_box (s);
2631 x_set_glyph_string_clipping (s);
2632 relief_drawn_p = 1;
2634 else
2635 x_set_glyph_string_clipping (s);
2637 switch (s->first_glyph->type)
2639 case IMAGE_GLYPH:
2640 x_draw_image_glyph_string (s);
2641 break;
2643 case STRETCH_GLYPH:
2644 x_draw_stretch_glyph_string (s);
2645 break;
2647 case CHAR_GLYPH:
2648 if (s->for_overlaps_p)
2649 s->background_filled_p = 1;
2650 else
2651 x_draw_glyph_string_background (s, 0);
2652 x_draw_glyph_string_foreground (s);
2653 break;
2655 case COMPOSITE_GLYPH:
2656 if (s->for_overlaps_p || s->gidx > 0)
2657 s->background_filled_p = 1;
2658 else
2659 x_draw_glyph_string_background (s, 1);
2660 x_draw_composite_glyph_string_foreground (s);
2661 break;
2663 default:
2664 abort ();
2667 if (!s->for_overlaps_p)
2669 /* Draw underline. */
2670 if (s->face->underline_p)
2672 unsigned long tem, h;
2673 int y;
2675 /* Get the underline thickness. Default is 1 pixel. */
2676 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
2677 h = 1;
2679 /* Get the underline position. This is the recommended
2680 vertical offset in pixels from the baseline to the top of
2681 the underline. This is a signed value according to the
2682 specs, and its default is
2684 ROUND ((maximum descent) / 2), with
2685 ROUND(x) = floor (x + 0.5) */
2687 if (x_use_underline_position_properties
2688 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
2689 y = s->ybase + (long) tem;
2690 else if (s->face->font)
2691 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2692 else
2693 y = s->y + s->height - h;
2695 if (s->face->underline_defaulted_p)
2696 XFillRectangle (s->display, s->window, s->gc,
2697 s->x, y, s->width, h);
2698 else
2700 XGCValues xgcv;
2701 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2702 XSetForeground (s->display, s->gc, s->face->underline_color);
2703 XFillRectangle (s->display, s->window, s->gc,
2704 s->x, y, s->width, h);
2705 XSetForeground (s->display, s->gc, xgcv.foreground);
2709 /* Draw overline. */
2710 if (s->face->overline_p)
2712 unsigned long dy = 0, h = 1;
2714 if (s->face->overline_color_defaulted_p)
2715 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2716 s->width, h);
2717 else
2719 XGCValues xgcv;
2720 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2721 XSetForeground (s->display, s->gc, s->face->overline_color);
2722 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2723 s->width, h);
2724 XSetForeground (s->display, s->gc, xgcv.foreground);
2728 /* Draw strike-through. */
2729 if (s->face->strike_through_p)
2731 unsigned long h = 1;
2732 unsigned long dy = (s->height - h) / 2;
2734 if (s->face->strike_through_color_defaulted_p)
2735 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2736 s->width, h);
2737 else
2739 XGCValues xgcv;
2740 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2741 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2742 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2743 s->width, h);
2744 XSetForeground (s->display, s->gc, xgcv.foreground);
2748 /* Draw relief if not yet drawn. */
2749 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2750 x_draw_glyph_string_box (s);
2753 /* Reset clipping. */
2754 XSetClipMask (s->display, s->gc, None);
2757 /* Shift display to make room for inserted glyphs. */
2759 void
2760 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2761 struct frame *f;
2762 int x, y, width, height, shift_by;
2764 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2765 f->output_data.x->normal_gc,
2766 x, y, width, height,
2767 x + shift_by, y);
2770 /* Delete N glyphs at the nominal cursor position. Not implemented
2771 for X frames. */
2773 static void
2774 x_delete_glyphs (n)
2775 register int n;
2777 abort ();
2781 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2782 If they are <= 0, this is probably an error. */
2784 void
2785 x_clear_area (dpy, window, x, y, width, height, exposures)
2786 Display *dpy;
2787 Window window;
2788 int x, y;
2789 int width, height;
2790 int exposures;
2792 xassert (width > 0 && height > 0);
2793 XClearArea (dpy, window, x, y, width, height, exposures);
2797 /* Clear entire frame. If updating_frame is non-null, clear that
2798 frame. Otherwise clear the selected frame. */
2800 static void
2801 x_clear_frame ()
2803 struct frame *f;
2805 if (updating_frame)
2806 f = updating_frame;
2807 else
2808 f = SELECTED_FRAME ();
2810 /* Clearing the frame will erase any cursor, so mark them all as no
2811 longer visible. */
2812 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2813 output_cursor.hpos = output_cursor.vpos = 0;
2814 output_cursor.x = -1;
2816 /* We don't set the output cursor here because there will always
2817 follow an explicit cursor_to. */
2818 BLOCK_INPUT;
2819 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2821 /* We have to clear the scroll bars, too. If we have changed
2822 colors or something like that, then they should be notified. */
2823 x_scroll_bar_clear (f);
2825 XFlush (FRAME_X_DISPLAY (f));
2827 #ifdef USE_GTK
2828 xg_frame_cleared (f);
2829 #endif
2831 UNBLOCK_INPUT;
2836 /* Invert the middle quarter of the frame for .15 sec. */
2838 /* We use the select system call to do the waiting, so we have to make
2839 sure it's available. If it isn't, we just won't do visual bells. */
2841 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2844 /* Subtract the `struct timeval' values X and Y, storing the result in
2845 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2847 static int
2848 timeval_subtract (result, x, y)
2849 struct timeval *result, x, y;
2851 /* Perform the carry for the later subtraction by updating y. This
2852 is safer because on some systems the tv_sec member is unsigned. */
2853 if (x.tv_usec < y.tv_usec)
2855 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2856 y.tv_usec -= 1000000 * nsec;
2857 y.tv_sec += nsec;
2860 if (x.tv_usec - y.tv_usec > 1000000)
2862 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2863 y.tv_usec += 1000000 * nsec;
2864 y.tv_sec -= nsec;
2867 /* Compute the time remaining to wait. tv_usec is certainly
2868 positive. */
2869 result->tv_sec = x.tv_sec - y.tv_sec;
2870 result->tv_usec = x.tv_usec - y.tv_usec;
2872 /* Return indication of whether the result should be considered
2873 negative. */
2874 return x.tv_sec < y.tv_sec;
2877 void
2878 XTflash (f)
2879 struct frame *f;
2881 BLOCK_INPUT;
2884 GC gc;
2886 /* Create a GC that will use the GXxor function to flip foreground
2887 pixels into background pixels. */
2889 XGCValues values;
2891 values.function = GXxor;
2892 values.foreground = (f->output_data.x->foreground_pixel
2893 ^ f->output_data.x->background_pixel);
2895 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2896 GCFunction | GCForeground, &values);
2900 /* Get the height not including a menu bar widget. */
2901 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
2902 /* Height of each line to flash. */
2903 int flash_height = FRAME_LINE_HEIGHT (f);
2904 /* These will be the left and right margins of the rectangles. */
2905 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
2906 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
2908 int width;
2910 /* Don't flash the area between a scroll bar and the frame
2911 edge it is next to. */
2912 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
2914 case vertical_scroll_bar_left:
2915 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2916 break;
2918 case vertical_scroll_bar_right:
2919 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2920 break;
2922 default:
2923 break;
2926 width = flash_right - flash_left;
2928 /* If window is tall, flash top and bottom line. */
2929 if (height > 3 * FRAME_LINE_HEIGHT (f))
2931 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2932 flash_left,
2933 (FRAME_INTERNAL_BORDER_WIDTH (f)
2934 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
2935 width, flash_height);
2936 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2937 flash_left,
2938 (height - flash_height
2939 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2940 width, flash_height);
2942 else
2943 /* If it is short, flash it all. */
2944 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2945 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2946 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2948 x_flush (f);
2951 struct timeval wakeup;
2953 EMACS_GET_TIME (wakeup);
2955 /* Compute time to wait until, propagating carry from usecs. */
2956 wakeup.tv_usec += 150000;
2957 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
2958 wakeup.tv_usec %= 1000000;
2960 /* Keep waiting until past the time wakeup or any input gets
2961 available. */
2962 while (! detect_input_pending ())
2964 struct timeval current;
2965 struct timeval timeout;
2967 EMACS_GET_TIME (current);
2969 /* Break if result would be negative. */
2970 if (timeval_subtract (&current, wakeup, current))
2971 break;
2973 /* How long `select' should wait. */
2974 timeout.tv_sec = 0;
2975 timeout.tv_usec = 10000;
2977 /* Try to wait that long--but we might wake up sooner. */
2978 select (0, NULL, NULL, NULL, &timeout);
2982 /* If window is tall, flash top and bottom line. */
2983 if (height > 3 * FRAME_LINE_HEIGHT (f))
2985 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2986 flash_left,
2987 (FRAME_INTERNAL_BORDER_WIDTH (f)
2988 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
2989 width, flash_height);
2990 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2991 flash_left,
2992 (height - flash_height
2993 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2994 width, flash_height);
2996 else
2997 /* If it is short, flash it all. */
2998 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2999 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3000 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3002 XFreeGC (FRAME_X_DISPLAY (f), gc);
3003 x_flush (f);
3007 UNBLOCK_INPUT;
3010 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3013 /* Make audible bell. */
3015 void
3016 XTring_bell ()
3018 struct frame *f = SELECTED_FRAME ();
3020 if (FRAME_X_DISPLAY (f))
3022 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3023 if (visible_bell)
3024 XTflash (f);
3025 else
3026 #endif
3028 BLOCK_INPUT;
3029 XBell (FRAME_X_DISPLAY (f), 0);
3030 XFlush (FRAME_X_DISPLAY (f));
3031 UNBLOCK_INPUT;
3037 /* Specify how many text lines, from the top of the window,
3038 should be affected by insert-lines and delete-lines operations.
3039 This, and those operations, are used only within an update
3040 that is bounded by calls to x_update_begin and x_update_end. */
3042 static void
3043 XTset_terminal_window (n)
3044 register int n;
3046 /* This function intentionally left blank. */
3051 /***********************************************************************
3052 Line Dance
3053 ***********************************************************************/
3055 /* Perform an insert-lines or delete-lines operation, inserting N
3056 lines or deleting -N lines at vertical position VPOS. */
3058 static void
3059 x_ins_del_lines (vpos, n)
3060 int vpos, n;
3062 abort ();
3066 /* Scroll part of the display as described by RUN. */
3068 static void
3069 x_scroll_run (w, run)
3070 struct window *w;
3071 struct run *run;
3073 struct frame *f = XFRAME (w->frame);
3074 int x, y, width, height, from_y, to_y, bottom_y;
3076 /* Get frame-relative bounding box of the text display area of W,
3077 without mode lines. Include in this box the left and right
3078 fringe of W. */
3079 window_box (w, -1, &x, &y, &width, &height);
3081 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3082 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3083 bottom_y = y + height;
3085 if (to_y < from_y)
3087 /* Scrolling up. Make sure we don't copy part of the mode
3088 line at the bottom. */
3089 if (from_y + run->height > bottom_y)
3090 height = bottom_y - from_y;
3091 else
3092 height = run->height;
3094 else
3096 /* Scolling down. Make sure we don't copy over the mode line.
3097 at the bottom. */
3098 if (to_y + run->height > bottom_y)
3099 height = bottom_y - to_y;
3100 else
3101 height = run->height;
3104 BLOCK_INPUT;
3106 /* Cursor off. Will be switched on again in x_update_window_end. */
3107 updated_window = w;
3108 x_clear_cursor (w);
3110 XCopyArea (FRAME_X_DISPLAY (f),
3111 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3112 f->output_data.x->normal_gc,
3113 x, from_y,
3114 width, height,
3115 x, to_y);
3117 UNBLOCK_INPUT;
3122 /***********************************************************************
3123 Exposure Events
3124 ***********************************************************************/
3127 static void
3128 frame_highlight (f)
3129 struct frame *f;
3131 /* We used to only do this if Vx_no_window_manager was non-nil, but
3132 the ICCCM (section 4.1.6) says that the window's border pixmap
3133 and border pixel are window attributes which are "private to the
3134 client", so we can always change it to whatever we want. */
3135 BLOCK_INPUT;
3136 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3137 f->output_data.x->border_pixel);
3138 UNBLOCK_INPUT;
3139 x_update_cursor (f, 1);
3142 static void
3143 frame_unhighlight (f)
3144 struct frame *f;
3146 /* We used to only do this if Vx_no_window_manager was non-nil, but
3147 the ICCCM (section 4.1.6) says that the window's border pixmap
3148 and border pixel are window attributes which are "private to the
3149 client", so we can always change it to whatever we want. */
3150 BLOCK_INPUT;
3151 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3152 f->output_data.x->border_tile);
3153 UNBLOCK_INPUT;
3154 x_update_cursor (f, 1);
3157 /* The focus has changed. Update the frames as necessary to reflect
3158 the new situation. Note that we can't change the selected frame
3159 here, because the Lisp code we are interrupting might become confused.
3160 Each event gets marked with the frame in which it occurred, so the
3161 Lisp code can tell when the switch took place by examining the events. */
3163 static void
3164 x_new_focus_frame (dpyinfo, frame)
3165 struct x_display_info *dpyinfo;
3166 struct frame *frame;
3168 struct frame *old_focus = dpyinfo->x_focus_frame;
3170 if (frame != dpyinfo->x_focus_frame)
3172 /* Set this before calling other routines, so that they see
3173 the correct value of x_focus_frame. */
3174 dpyinfo->x_focus_frame = frame;
3176 if (old_focus && old_focus->auto_lower)
3177 x_lower_frame (old_focus);
3179 #if 0
3180 selected_frame = frame;
3181 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3182 selected_frame);
3183 Fselect_window (selected_frame->selected_window, Qnil);
3184 choose_minibuf_frame ();
3185 #endif /* ! 0 */
3187 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3188 pending_autoraise_frame = dpyinfo->x_focus_frame;
3189 else
3190 pending_autoraise_frame = 0;
3193 x_frame_rehighlight (dpyinfo);
3196 /* Handle FocusIn and FocusOut state changes for FRAME.
3197 If FRAME has focus and there exists more than one frame, puts
3198 a FOCUS_IN_EVENT into *BUFP. */
3200 static void
3201 x_focus_changed (type, state, dpyinfo, frame, bufp)
3202 int type;
3203 int state;
3204 struct x_display_info *dpyinfo;
3205 struct frame *frame;
3206 struct input_event *bufp;
3208 if (type == FocusIn)
3210 if (dpyinfo->x_focus_event_frame != frame)
3212 x_new_focus_frame (dpyinfo, frame);
3213 dpyinfo->x_focus_event_frame = frame;
3215 /* Don't stop displaying the initial startup message
3216 for a switch-frame event we don't need. */
3217 if (GC_NILP (Vterminal_frame)
3218 && GC_CONSP (Vframe_list)
3219 && !GC_NILP (XCDR (Vframe_list)))
3221 bufp->kind = FOCUS_IN_EVENT;
3222 XSETFRAME (bufp->frame_or_window, frame);
3226 frame->output_data.x->focus_state |= state;
3228 #ifdef HAVE_X_I18N
3229 if (FRAME_XIC (frame))
3230 XSetICFocus (FRAME_XIC (frame));
3231 #endif
3233 else if (type == FocusOut)
3235 frame->output_data.x->focus_state &= ~state;
3237 if (dpyinfo->x_focus_event_frame == frame)
3239 dpyinfo->x_focus_event_frame = 0;
3240 x_new_focus_frame (dpyinfo, 0);
3243 #ifdef HAVE_X_I18N
3244 if (FRAME_XIC (frame))
3245 XUnsetICFocus (FRAME_XIC (frame));
3246 #endif
3250 /* The focus may have changed. Figure out if it is a real focus change,
3251 by checking both FocusIn/Out and Enter/LeaveNotify events.
3253 Returns FOCUS_IN_EVENT event in *BUFP. */
3255 static void
3256 x_detect_focus_change (dpyinfo, event, bufp)
3257 struct x_display_info *dpyinfo;
3258 XEvent *event;
3259 struct input_event *bufp;
3261 struct frame *frame;
3262 int nr_events = 0;
3264 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3265 if (! frame)
3266 return;
3268 switch (event->type)
3270 case EnterNotify:
3271 case LeaveNotify:
3273 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3274 int focus_state
3275 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3277 if (event->xcrossing.detail != NotifyInferior
3278 && event->xcrossing.focus
3279 && ! (focus_state & FOCUS_EXPLICIT))
3280 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3281 FOCUS_IMPLICIT,
3282 dpyinfo, frame, bufp);
3284 break;
3286 case FocusIn:
3287 case FocusOut:
3288 x_focus_changed (event->type,
3289 (event->xfocus.detail == NotifyPointer ?
3290 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3291 dpyinfo, frame, bufp);
3292 break;
3297 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3299 void
3300 x_mouse_leave (dpyinfo)
3301 struct x_display_info *dpyinfo;
3303 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3306 /* The focus has changed, or we have redirected a frame's focus to
3307 another frame (this happens when a frame uses a surrogate
3308 mini-buffer frame). Shift the highlight as appropriate.
3310 The FRAME argument doesn't necessarily have anything to do with which
3311 frame is being highlighted or un-highlighted; we only use it to find
3312 the appropriate X display info. */
3314 static void
3315 XTframe_rehighlight (frame)
3316 struct frame *frame;
3318 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3321 static void
3322 x_frame_rehighlight (dpyinfo)
3323 struct x_display_info *dpyinfo;
3325 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3327 if (dpyinfo->x_focus_frame)
3329 dpyinfo->x_highlight_frame
3330 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3331 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3332 : dpyinfo->x_focus_frame);
3333 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3335 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3336 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3339 else
3340 dpyinfo->x_highlight_frame = 0;
3342 if (dpyinfo->x_highlight_frame != old_highlight)
3344 if (old_highlight)
3345 frame_unhighlight (old_highlight);
3346 if (dpyinfo->x_highlight_frame)
3347 frame_highlight (dpyinfo->x_highlight_frame);
3353 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3355 /* Initialize mode_switch_bit and modifier_meaning. */
3356 static void
3357 x_find_modifier_meanings (dpyinfo)
3358 struct x_display_info *dpyinfo;
3360 int min_code, max_code;
3361 KeySym *syms;
3362 int syms_per_code;
3363 XModifierKeymap *mods;
3365 dpyinfo->meta_mod_mask = 0;
3366 dpyinfo->shift_lock_mask = 0;
3367 dpyinfo->alt_mod_mask = 0;
3368 dpyinfo->super_mod_mask = 0;
3369 dpyinfo->hyper_mod_mask = 0;
3371 #ifdef HAVE_X11R4
3372 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3373 #else
3374 min_code = dpyinfo->display->min_keycode;
3375 max_code = dpyinfo->display->max_keycode;
3376 #endif
3378 syms = XGetKeyboardMapping (dpyinfo->display,
3379 min_code, max_code - min_code + 1,
3380 &syms_per_code);
3381 mods = XGetModifierMapping (dpyinfo->display);
3383 /* Scan the modifier table to see which modifier bits the Meta and
3384 Alt keysyms are on. */
3386 int row, col; /* The row and column in the modifier table. */
3388 for (row = 3; row < 8; row++)
3389 for (col = 0; col < mods->max_keypermod; col++)
3391 KeyCode code
3392 = mods->modifiermap[(row * mods->max_keypermod) + col];
3394 /* Zeroes are used for filler. Skip them. */
3395 if (code == 0)
3396 continue;
3398 /* Are any of this keycode's keysyms a meta key? */
3400 int code_col;
3402 for (code_col = 0; code_col < syms_per_code; code_col++)
3404 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3406 switch (sym)
3408 case XK_Meta_L:
3409 case XK_Meta_R:
3410 dpyinfo->meta_mod_mask |= (1 << row);
3411 break;
3413 case XK_Alt_L:
3414 case XK_Alt_R:
3415 dpyinfo->alt_mod_mask |= (1 << row);
3416 break;
3418 case XK_Hyper_L:
3419 case XK_Hyper_R:
3420 dpyinfo->hyper_mod_mask |= (1 << row);
3421 break;
3423 case XK_Super_L:
3424 case XK_Super_R:
3425 dpyinfo->super_mod_mask |= (1 << row);
3426 break;
3428 case XK_Shift_Lock:
3429 /* Ignore this if it's not on the lock modifier. */
3430 if ((1 << row) == LockMask)
3431 dpyinfo->shift_lock_mask = LockMask;
3432 break;
3439 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3440 if (! dpyinfo->meta_mod_mask)
3442 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3443 dpyinfo->alt_mod_mask = 0;
3446 /* If some keys are both alt and meta,
3447 make them just meta, not alt. */
3448 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3450 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3453 XFree ((char *) syms);
3454 XFreeModifiermap (mods);
3457 /* Convert between the modifier bits X uses and the modifier bits
3458 Emacs uses. */
3460 static unsigned int
3461 x_x_to_emacs_modifiers (dpyinfo, state)
3462 struct x_display_info *dpyinfo;
3463 unsigned int state;
3465 EMACS_UINT mod_meta = meta_modifier;
3466 EMACS_UINT mod_alt = alt_modifier;
3467 EMACS_UINT mod_hyper = hyper_modifier;
3468 EMACS_UINT mod_super = super_modifier;
3469 Lisp_Object tem;
3471 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3472 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3473 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3474 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3475 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3476 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3477 tem = Fget (Vx_super_keysym, Qmodifier_value);
3478 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3481 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3482 | ((state & ControlMask) ? ctrl_modifier : 0)
3483 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3484 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3485 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3486 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3489 static unsigned int
3490 x_emacs_to_x_modifiers (dpyinfo, state)
3491 struct x_display_info *dpyinfo;
3492 unsigned int state;
3494 EMACS_UINT mod_meta = meta_modifier;
3495 EMACS_UINT mod_alt = alt_modifier;
3496 EMACS_UINT mod_hyper = hyper_modifier;
3497 EMACS_UINT mod_super = super_modifier;
3499 Lisp_Object tem;
3501 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3502 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3503 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3504 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3505 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3506 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3507 tem = Fget (Vx_super_keysym, Qmodifier_value);
3508 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3511 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3512 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3513 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3514 | ((state & shift_modifier) ? ShiftMask : 0)
3515 | ((state & ctrl_modifier) ? ControlMask : 0)
3516 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3519 /* Convert a keysym to its name. */
3521 char *
3522 x_get_keysym_name (keysym)
3523 KeySym keysym;
3525 char *value;
3527 BLOCK_INPUT;
3528 value = XKeysymToString (keysym);
3529 UNBLOCK_INPUT;
3531 return value;
3536 /* Mouse clicks and mouse movement. Rah. */
3538 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3540 If the event is a button press, then note that we have grabbed
3541 the mouse. */
3543 static Lisp_Object
3544 construct_mouse_click (result, event, f)
3545 struct input_event *result;
3546 XButtonEvent *event;
3547 struct frame *f;
3549 /* Make the event type NO_EVENT; we'll change that when we decide
3550 otherwise. */
3551 result->kind = MOUSE_CLICK_EVENT;
3552 result->code = event->button - Button1;
3553 result->timestamp = event->time;
3554 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3555 event->state)
3556 | (event->type == ButtonRelease
3557 ? up_modifier
3558 : down_modifier));
3560 XSETINT (result->x, event->x);
3561 XSETINT (result->y, event->y);
3562 XSETFRAME (result->frame_or_window, f);
3563 result->arg = Qnil;
3564 return Qnil;
3568 /* Function to report a mouse movement to the mainstream Emacs code.
3569 The input handler calls this.
3571 We have received a mouse movement event, which is given in *event.
3572 If the mouse is over a different glyph than it was last time, tell
3573 the mainstream emacs code by setting mouse_moved. If not, ask for
3574 another motion event, so we can check again the next time it moves. */
3576 static XMotionEvent last_mouse_motion_event;
3577 static Lisp_Object last_mouse_motion_frame;
3579 static void
3580 note_mouse_movement (frame, event)
3581 FRAME_PTR frame;
3582 XMotionEvent *event;
3584 last_mouse_movement_time = event->time;
3585 last_mouse_motion_event = *event;
3586 XSETFRAME (last_mouse_motion_frame, frame);
3588 if (event->window != FRAME_X_WINDOW (frame))
3590 frame->mouse_moved = 1;
3591 last_mouse_scroll_bar = Qnil;
3592 note_mouse_highlight (frame, -1, -1);
3595 /* Has the mouse moved off the glyph it was on at the last sighting? */
3596 else if (event->x < last_mouse_glyph.x
3597 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3598 || event->y < last_mouse_glyph.y
3599 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3601 frame->mouse_moved = 1;
3602 last_mouse_scroll_bar = Qnil;
3603 note_mouse_highlight (frame, event->x, event->y);
3608 /************************************************************************
3609 Mouse Face
3610 ************************************************************************/
3612 static void
3613 redo_mouse_highlight ()
3615 if (!NILP (last_mouse_motion_frame)
3616 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3617 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3618 last_mouse_motion_event.x,
3619 last_mouse_motion_event.y);
3623 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
3626 /* Try to determine frame pixel position and size of the glyph under
3627 frame pixel coordinates X/Y on frame F . Return the position and
3628 size in *RECT. Value is non-zero if we could compute these
3629 values. */
3631 static int
3632 glyph_rect (f, x, y, rect)
3633 struct frame *f;
3634 int x, y;
3635 XRectangle *rect;
3637 Lisp_Object window;
3638 struct window *w;
3639 struct glyph_row *r, *end_row;
3641 window = window_from_coordinates (f, x, y, 0, &x, &y, 0);
3642 if (NILP (window))
3643 return 0;
3645 w = XWINDOW (window);
3646 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
3647 end_row = r + w->current_matrix->nrows - 1;
3649 for (; r < end_row && r->enabled_p; ++r)
3651 if (r->y >= y)
3653 struct glyph *g = r->glyphs[TEXT_AREA];
3654 struct glyph *end = g + r->used[TEXT_AREA];
3655 int gx = r->x;
3656 while (g < end && gx < x)
3657 gx += g->pixel_width, ++g;
3658 if (g < end)
3660 rect->width = g->pixel_width;
3661 rect->height = r->height;
3662 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
3663 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
3664 return 1;
3666 break;
3670 return 0;
3674 /* Return the current position of the mouse.
3675 *FP should be a frame which indicates which display to ask about.
3677 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3678 and *PART to the frame, window, and scroll bar part that the mouse
3679 is over. Set *X and *Y to the portion and whole of the mouse's
3680 position on the scroll bar.
3682 If the mouse movement started elsewhere, set *FP to the frame the
3683 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3684 the mouse is over.
3686 Set *TIME to the server time-stamp for the time at which the mouse
3687 was at this position.
3689 Don't store anything if we don't have a valid set of values to report.
3691 This clears the mouse_moved flag, so we can wait for the next mouse
3692 movement. */
3694 static void
3695 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3696 FRAME_PTR *fp;
3697 int insist;
3698 Lisp_Object *bar_window;
3699 enum scroll_bar_part *part;
3700 Lisp_Object *x, *y;
3701 unsigned long *time;
3703 FRAME_PTR f1;
3705 BLOCK_INPUT;
3707 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3708 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3709 else
3711 Window root;
3712 int root_x, root_y;
3714 Window dummy_window;
3715 int dummy;
3717 Lisp_Object frame, tail;
3719 /* Clear the mouse-moved flag for every frame on this display. */
3720 FOR_EACH_FRAME (tail, frame)
3721 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3722 XFRAME (frame)->mouse_moved = 0;
3724 last_mouse_scroll_bar = Qnil;
3726 /* Figure out which root window we're on. */
3727 XQueryPointer (FRAME_X_DISPLAY (*fp),
3728 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3730 /* The root window which contains the pointer. */
3731 &root,
3733 /* Trash which we can't trust if the pointer is on
3734 a different screen. */
3735 &dummy_window,
3737 /* The position on that root window. */
3738 &root_x, &root_y,
3740 /* More trash we can't trust. */
3741 &dummy, &dummy,
3743 /* Modifier keys and pointer buttons, about which
3744 we don't care. */
3745 (unsigned int *) &dummy);
3747 /* Now we have a position on the root; find the innermost window
3748 containing the pointer. */
3750 Window win, child;
3751 int win_x, win_y;
3752 int parent_x = 0, parent_y = 0;
3753 int count;
3755 win = root;
3757 /* XTranslateCoordinates can get errors if the window
3758 structure is changing at the same time this function
3759 is running. So at least we must not crash from them. */
3761 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
3763 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3764 && FRAME_LIVE_P (last_mouse_frame))
3766 /* If mouse was grabbed on a frame, give coords for that frame
3767 even if the mouse is now outside it. */
3768 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3770 /* From-window, to-window. */
3771 root, FRAME_X_WINDOW (last_mouse_frame),
3773 /* From-position, to-position. */
3774 root_x, root_y, &win_x, &win_y,
3776 /* Child of win. */
3777 &child);
3778 f1 = last_mouse_frame;
3780 else
3782 while (1)
3784 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3786 /* From-window, to-window. */
3787 root, win,
3789 /* From-position, to-position. */
3790 root_x, root_y, &win_x, &win_y,
3792 /* Child of win. */
3793 &child);
3795 if (child == None || child == win)
3796 break;
3798 win = child;
3799 parent_x = win_x;
3800 parent_y = win_y;
3803 /* Now we know that:
3804 win is the innermost window containing the pointer
3805 (XTC says it has no child containing the pointer),
3806 win_x and win_y are the pointer's position in it
3807 (XTC did this the last time through), and
3808 parent_x and parent_y are the pointer's position in win's parent.
3809 (They are what win_x and win_y were when win was child.
3810 If win is the root window, it has no parent, and
3811 parent_{x,y} are invalid, but that's okay, because we'll
3812 never use them in that case.) */
3814 /* Is win one of our frames? */
3815 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3817 #ifdef USE_X_TOOLKIT
3818 /* If we end up with the menu bar window, say it's not
3819 on the frame. */
3820 if (f1 != NULL
3821 && f1->output_data.x->menubar_widget
3822 && win == XtWindow (f1->output_data.x->menubar_widget))
3823 f1 = NULL;
3824 #endif /* USE_X_TOOLKIT */
3827 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3828 f1 = 0;
3830 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
3832 /* If not, is it one of our scroll bars? */
3833 if (! f1)
3835 struct scroll_bar *bar;
3837 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3839 if (bar)
3841 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3842 win_x = parent_x;
3843 win_y = parent_y;
3847 if (f1 == 0 && insist > 0)
3848 f1 = SELECTED_FRAME ();
3850 if (f1)
3852 /* Ok, we found a frame. Store all the values.
3853 last_mouse_glyph is a rectangle used to reduce the
3854 generation of mouse events. To not miss any motion
3855 events, we must divide the frame into rectangles of the
3856 size of the smallest character that could be displayed
3857 on it, i.e. into the same rectangles that matrices on
3858 the frame are divided into. */
3860 int width, height, gx, gy;
3861 XRectangle rect;
3863 if (glyph_rect (f1, win_x, win_y, &rect))
3864 last_mouse_glyph = rect;
3865 else
3867 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
3868 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
3869 gx = win_x;
3870 gy = win_y;
3872 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
3873 round down even for negative values. */
3874 if (gx < 0)
3875 gx -= width - 1;
3876 if (gy < 0)
3877 gy -= height - 1;
3878 gx = (gx + width - 1) / width * width;
3879 gy = (gy + height - 1) / height * height;
3881 last_mouse_glyph.width = width;
3882 last_mouse_glyph.height = height;
3883 last_mouse_glyph.x = gx;
3884 last_mouse_glyph.y = gy;
3887 *bar_window = Qnil;
3888 *part = 0;
3889 *fp = f1;
3890 XSETINT (*x, win_x);
3891 XSETINT (*y, win_y);
3892 *time = last_mouse_movement_time;
3897 UNBLOCK_INPUT;
3902 /***********************************************************************
3903 Scroll bars
3904 ***********************************************************************/
3906 /* Scroll bar support. */
3908 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
3909 manages it.
3910 This can be called in GC, so we have to make sure to strip off mark
3911 bits. */
3913 static struct scroll_bar *
3914 x_window_to_scroll_bar (display, window_id)
3915 Display *display;
3916 Window window_id;
3918 Lisp_Object tail;
3920 #ifdef USE_GTK
3921 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
3922 #endif /* USE_GTK */
3924 for (tail = Vframe_list;
3925 XGCTYPE (tail) == Lisp_Cons;
3926 tail = XCDR (tail))
3928 Lisp_Object frame, bar, condemned;
3930 frame = XCAR (tail);
3931 /* All elements of Vframe_list should be frames. */
3932 if (! GC_FRAMEP (frame))
3933 abort ();
3935 /* Scan this frame's scroll bar list for a scroll bar with the
3936 right window ID. */
3937 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3938 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3939 /* This trick allows us to search both the ordinary and
3940 condemned scroll bar lists with one loop. */
3941 ! GC_NILP (bar) || (bar = condemned,
3942 condemned = Qnil,
3943 ! GC_NILP (bar));
3944 bar = XSCROLL_BAR (bar)->next)
3945 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id &&
3946 FRAME_X_DISPLAY (XFRAME (frame)) == display)
3947 return XSCROLL_BAR (bar);
3950 return 0;
3954 #if defined USE_LUCID
3956 /* Return the Lucid menu bar WINDOW is part of. Return null
3957 if WINDOW is not part of a menu bar. */
3959 static Widget
3960 x_window_to_menu_bar (window)
3961 Window window;
3963 Lisp_Object tail;
3965 for (tail = Vframe_list;
3966 XGCTYPE (tail) == Lisp_Cons;
3967 tail = XCDR (tail))
3969 Lisp_Object frame = XCAR (tail);
3970 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
3972 if (menu_bar && xlwmenu_window_p (menu_bar, window))
3973 return menu_bar;
3976 return NULL;
3979 #endif /* USE_LUCID */
3982 /************************************************************************
3983 Toolkit scroll bars
3984 ************************************************************************/
3986 #ifdef USE_TOOLKIT_SCROLL_BARS
3988 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
3989 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
3990 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
3991 struct scroll_bar *));
3992 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
3993 int, int, int));
3996 /* Lisp window being scrolled. Set when starting to interact with
3997 a toolkit scroll bar, reset to nil when ending the interaction. */
3999 static Lisp_Object window_being_scrolled;
4001 /* Last scroll bar part sent in xm_scroll_callback. */
4003 static int last_scroll_bar_part;
4005 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4006 that movements of 1/20 of the screen size are mapped to up/down. */
4008 #ifndef USE_GTK
4009 /* Id of action hook installed for scroll bars. */
4011 static XtActionHookId action_hook_id;
4013 static Boolean xaw3d_arrow_scroll;
4015 /* Whether the drag scrolling maintains the mouse at the top of the
4016 thumb. If not, resizing the thumb needs to be done more carefully
4017 to avoid jerkyness. */
4019 static Boolean xaw3d_pick_top;
4021 extern void set_vertical_scroll_bar P_ ((struct window *));
4023 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4024 bars are used.. The hook is responsible for detecting when
4025 the user ends an interaction with the scroll bar, and generates
4026 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4028 static void
4029 xt_action_hook (widget, client_data, action_name, event, params,
4030 num_params)
4031 Widget widget;
4032 XtPointer client_data;
4033 String action_name;
4034 XEvent *event;
4035 String *params;
4036 Cardinal *num_params;
4038 int scroll_bar_p;
4039 char *end_action;
4041 #ifdef USE_MOTIF
4042 scroll_bar_p = XmIsScrollBar (widget);
4043 end_action = "Release";
4044 #else /* !USE_MOTIF i.e. use Xaw */
4045 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4046 end_action = "EndScroll";
4047 #endif /* USE_MOTIF */
4049 if (scroll_bar_p
4050 && strcmp (action_name, end_action) == 0
4051 && WINDOWP (window_being_scrolled))
4053 struct window *w;
4055 x_send_scroll_bar_event (window_being_scrolled,
4056 scroll_bar_end_scroll, 0, 0);
4057 w = XWINDOW (window_being_scrolled);
4059 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4061 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4062 /* The thumb size is incorrect while dragging: fix it. */
4063 set_vertical_scroll_bar (w);
4065 window_being_scrolled = Qnil;
4066 last_scroll_bar_part = -1;
4068 /* Xt timeouts no longer needed. */
4069 toolkit_scroll_bar_interaction = 0;
4072 #endif /* not USE_GTK */
4074 /* A vector of windows used for communication between
4075 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4077 static struct window **scroll_bar_windows;
4078 static int scroll_bar_windows_size;
4081 /* Send a client message with message type Xatom_Scrollbar for a
4082 scroll action to the frame of WINDOW. PART is a value identifying
4083 the part of the scroll bar that was clicked on. PORTION is the
4084 amount to scroll of a whole of WHOLE. */
4086 static void
4087 x_send_scroll_bar_event (window, part, portion, whole)
4088 Lisp_Object window;
4089 int part, portion, whole;
4091 XEvent event;
4092 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4093 struct window *w = XWINDOW (window);
4094 struct frame *f = XFRAME (w->frame);
4095 int i;
4097 BLOCK_INPUT;
4099 /* Construct a ClientMessage event to send to the frame. */
4100 ev->type = ClientMessage;
4101 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4102 ev->display = FRAME_X_DISPLAY (f);
4103 ev->window = FRAME_X_WINDOW (f);
4104 ev->format = 32;
4106 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4107 not enough to store a pointer or Lisp_Object on a 64 bit system.
4108 So, store the window in scroll_bar_windows and pass the index
4109 into that array in the event. */
4110 for (i = 0; i < scroll_bar_windows_size; ++i)
4111 if (scroll_bar_windows[i] == NULL)
4112 break;
4114 if (i == scroll_bar_windows_size)
4116 int new_size = max (10, 2 * scroll_bar_windows_size);
4117 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4118 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4120 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4121 nbytes);
4122 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4123 scroll_bar_windows_size = new_size;
4126 scroll_bar_windows[i] = w;
4127 ev->data.l[0] = (long) i;
4128 ev->data.l[1] = (long) part;
4129 ev->data.l[2] = (long) 0;
4130 ev->data.l[3] = (long) portion;
4131 ev->data.l[4] = (long) whole;
4133 /* Make Xt timeouts work while the scroll bar is active. */
4134 toolkit_scroll_bar_interaction = 1;
4136 /* Setting the event mask to zero means that the message will
4137 be sent to the client that created the window, and if that
4138 window no longer exists, no event will be sent. */
4139 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4140 UNBLOCK_INPUT;
4144 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4145 in *IEVENT. */
4147 static void
4148 x_scroll_bar_to_input_event (event, ievent)
4149 XEvent *event;
4150 struct input_event *ievent;
4152 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4153 Lisp_Object window;
4154 struct frame *f;
4155 struct window *w;
4157 w = scroll_bar_windows[ev->data.l[0]];
4158 scroll_bar_windows[ev->data.l[0]] = NULL;
4160 XSETWINDOW (window, w);
4161 f = XFRAME (w->frame);
4163 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4164 ievent->frame_or_window = window;
4165 ievent->arg = Qnil;
4166 #ifdef USE_GTK
4167 ievent->timestamp = CurrentTime;
4168 #else
4169 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4170 #endif
4171 ievent->part = ev->data.l[1];
4172 ievent->code = ev->data.l[2];
4173 ievent->x = make_number ((int) ev->data.l[3]);
4174 ievent->y = make_number ((int) ev->data.l[4]);
4175 ievent->modifiers = 0;
4179 #ifdef USE_MOTIF
4181 /* Minimum and maximum values used for Motif scroll bars. */
4183 #define XM_SB_MAX 10000000
4186 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4187 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4188 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4190 static void
4191 xm_scroll_callback (widget, client_data, call_data)
4192 Widget widget;
4193 XtPointer client_data, call_data;
4195 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4196 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4197 int part = -1, whole = 0, portion = 0;
4199 switch (cs->reason)
4201 case XmCR_DECREMENT:
4202 bar->dragging = Qnil;
4203 part = scroll_bar_up_arrow;
4204 break;
4206 case XmCR_INCREMENT:
4207 bar->dragging = Qnil;
4208 part = scroll_bar_down_arrow;
4209 break;
4211 case XmCR_PAGE_DECREMENT:
4212 bar->dragging = Qnil;
4213 part = scroll_bar_above_handle;
4214 break;
4216 case XmCR_PAGE_INCREMENT:
4217 bar->dragging = Qnil;
4218 part = scroll_bar_below_handle;
4219 break;
4221 case XmCR_TO_TOP:
4222 bar->dragging = Qnil;
4223 part = scroll_bar_to_top;
4224 break;
4226 case XmCR_TO_BOTTOM:
4227 bar->dragging = Qnil;
4228 part = scroll_bar_to_bottom;
4229 break;
4231 case XmCR_DRAG:
4233 int slider_size;
4235 /* Get the slider size. */
4236 BLOCK_INPUT;
4237 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4238 UNBLOCK_INPUT;
4240 whole = XM_SB_MAX - slider_size;
4241 portion = min (cs->value, whole);
4242 part = scroll_bar_handle;
4243 bar->dragging = make_number (cs->value);
4245 break;
4247 case XmCR_VALUE_CHANGED:
4248 break;
4251 if (part >= 0)
4253 window_being_scrolled = bar->window;
4254 last_scroll_bar_part = part;
4255 x_send_scroll_bar_event (bar->window, part, portion, whole);
4260 #else /* !USE_MOTIF, i.e. Xaw or GTK */
4261 #ifdef USE_GTK
4262 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4263 bar widget. DATA is a pointer to the scroll_bar structure. */
4265 static void
4266 xg_scroll_callback (widget, data)
4267 GtkRange *widget;
4268 gpointer data;
4270 struct scroll_bar *bar = (struct scroll_bar *) data;
4271 gdouble previous;
4272 gdouble position;
4273 gdouble *p;
4274 int diff;
4276 int part = -1, whole = 0, portion = 0;
4277 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4279 if (xg_ignore_gtk_scrollbar) return;
4281 position = gtk_adjustment_get_value (adj);
4283 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4284 if (! p)
4286 p = (gdouble*) xmalloc (sizeof (gdouble));
4287 *p = XG_SB_MIN;
4288 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4291 previous = *p;
4292 *p = position;
4294 diff = (int) (position - previous);
4296 if (diff == (int) adj->step_increment)
4298 part = scroll_bar_down_arrow;
4299 bar->dragging = Qnil;
4301 else if (-diff == (int) adj->step_increment)
4303 part = scroll_bar_up_arrow;
4304 bar->dragging = Qnil;
4306 else if (diff == (int) adj->page_increment)
4308 part = scroll_bar_below_handle;
4309 bar->dragging = Qnil;
4311 else if (-diff == (int) adj->page_increment)
4313 part = scroll_bar_above_handle;
4314 bar->dragging = Qnil;
4316 else
4318 part = scroll_bar_handle;
4319 whole = adj->upper - adj->page_size;
4320 portion = min ((int)position, whole);
4321 bar->dragging = make_number ((int)portion);
4324 if (part >= 0)
4326 window_being_scrolled = bar->window;
4327 last_scroll_bar_part = part;
4328 x_send_scroll_bar_event (bar->window, part, portion, whole);
4332 #else /* not USE_GTK */
4334 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4335 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4336 scroll bar struct. CALL_DATA is a pointer to a float saying where
4337 the thumb is. */
4339 static void
4340 xaw_jump_callback (widget, client_data, call_data)
4341 Widget widget;
4342 XtPointer client_data, call_data;
4344 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4345 float top = *(float *) call_data;
4346 float shown;
4347 int whole, portion, height;
4348 int part;
4350 /* Get the size of the thumb, a value between 0 and 1. */
4351 BLOCK_INPUT;
4352 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4353 UNBLOCK_INPUT;
4355 whole = 10000000;
4356 portion = shown < 1 ? top * whole : 0;
4358 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
4359 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4360 the bottom, so we force the scrolling whenever we see that we're
4361 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4362 we try to ensure that we always stay two pixels away from the
4363 bottom). */
4364 part = scroll_bar_down_arrow;
4365 else
4366 part = scroll_bar_handle;
4368 window_being_scrolled = bar->window;
4369 bar->dragging = make_number (portion);
4370 last_scroll_bar_part = part;
4371 x_send_scroll_bar_event (bar->window, part, portion, whole);
4375 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4376 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4377 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4378 the scroll bar. CALL_DATA is an integer specifying the action that
4379 has taken place. Its magnitude is in the range 0..height of the
4380 scroll bar. Negative values mean scroll towards buffer start.
4381 Values < height of scroll bar mean line-wise movement. */
4383 static void
4384 xaw_scroll_callback (widget, client_data, call_data)
4385 Widget widget;
4386 XtPointer client_data, call_data;
4388 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4389 /* The position really is stored cast to a pointer. */
4390 int position = (long) call_data;
4391 Dimension height;
4392 int part;
4394 /* Get the height of the scroll bar. */
4395 BLOCK_INPUT;
4396 XtVaGetValues (widget, XtNheight, &height, NULL);
4397 UNBLOCK_INPUT;
4399 if (abs (position) >= height)
4400 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4402 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4403 it maps line-movement to call_data = max(5, height/20). */
4404 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
4405 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4406 else
4407 part = scroll_bar_move_ratio;
4409 window_being_scrolled = bar->window;
4410 bar->dragging = Qnil;
4411 last_scroll_bar_part = part;
4412 x_send_scroll_bar_event (bar->window, part, position, height);
4415 #endif /* not USE_GTK */
4416 #endif /* not USE_MOTIF */
4418 #define SCROLL_BAR_NAME "verticalScrollBar"
4420 /* Create the widget for scroll bar BAR on frame F. Record the widget
4421 and X window of the scroll bar in BAR. */
4423 #ifdef USE_GTK
4424 static void
4425 x_create_toolkit_scroll_bar (f, bar)
4426 struct frame *f;
4427 struct scroll_bar *bar;
4429 char *scroll_bar_name = SCROLL_BAR_NAME;
4431 BLOCK_INPUT;
4432 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4433 scroll_bar_name);
4434 UNBLOCK_INPUT;
4437 #else /* not USE_GTK */
4439 static void
4440 x_create_toolkit_scroll_bar (f, bar)
4441 struct frame *f;
4442 struct scroll_bar *bar;
4444 Window xwindow;
4445 Widget widget;
4446 Arg av[20];
4447 int ac = 0;
4448 char *scroll_bar_name = SCROLL_BAR_NAME;
4449 unsigned long pixel;
4451 BLOCK_INPUT;
4453 #ifdef USE_MOTIF
4454 /* Set resources. Create the widget. */
4455 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4456 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4457 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4458 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4459 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4460 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4461 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4463 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4464 if (pixel != -1)
4466 XtSetArg (av[ac], XmNforeground, pixel);
4467 ++ac;
4470 pixel = f->output_data.x->scroll_bar_background_pixel;
4471 if (pixel != -1)
4473 XtSetArg (av[ac], XmNbackground, pixel);
4474 ++ac;
4477 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4478 scroll_bar_name, av, ac);
4480 /* Add one callback for everything that can happen. */
4481 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4482 (XtPointer) bar);
4483 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4484 (XtPointer) bar);
4485 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4486 (XtPointer) bar);
4487 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4488 (XtPointer) bar);
4489 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4490 (XtPointer) bar);
4491 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4492 (XtPointer) bar);
4493 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4494 (XtPointer) bar);
4496 /* Realize the widget. Only after that is the X window created. */
4497 XtRealizeWidget (widget);
4499 /* Set the cursor to an arrow. I didn't find a resource to do that.
4500 And I'm wondering why it hasn't an arrow cursor by default. */
4501 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4502 f->output_data.x->nontext_cursor);
4504 #else /* !USE_MOTIF i.e. use Xaw */
4506 /* Set resources. Create the widget. The background of the
4507 Xaw3d scroll bar widget is a little bit light for my taste.
4508 We don't alter it here to let users change it according
4509 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4510 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4511 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4512 /* For smoother scrolling with Xaw3d -sm */
4513 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4515 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4516 if (pixel != -1)
4518 XtSetArg (av[ac], XtNforeground, pixel);
4519 ++ac;
4522 pixel = f->output_data.x->scroll_bar_background_pixel;
4523 if (pixel != -1)
4525 XtSetArg (av[ac], XtNbackground, pixel);
4526 ++ac;
4529 /* Top/bottom shadow colors. */
4531 /* Allocate them, if necessary. */
4532 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4534 pixel = f->output_data.x->scroll_bar_background_pixel;
4535 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4536 &pixel, 1.2, 0x8000))
4537 pixel = -1;
4538 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4540 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4542 pixel = f->output_data.x->scroll_bar_background_pixel;
4543 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4544 &pixel, 0.6, 0x4000))
4545 pixel = -1;
4546 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4549 /* Tell the toolkit about them. */
4550 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4551 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4552 /* We tried to allocate a color for the top/bottom shadow, and
4553 failed, so tell Xaw3d to use dithering instead. */
4555 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4556 ++ac;
4558 else
4559 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4560 be more consistent with other emacs 3d colors, and since Xaw3d is
4561 not good at dealing with allocation failure. */
4563 /* This tells Xaw3d to use real colors instead of dithering for
4564 the shadows. */
4565 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4566 ++ac;
4568 /* Specify the colors. */
4569 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4570 if (pixel != -1)
4572 XtSetArg (av[ac], "topShadowPixel", pixel);
4573 ++ac;
4575 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4576 if (pixel != -1)
4578 XtSetArg (av[ac], "bottomShadowPixel", pixel);
4579 ++ac;
4583 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4584 f->output_data.x->edit_widget, av, ac);
4587 char *initial = "";
4588 char *val = initial;
4589 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4590 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4591 if (val == initial)
4592 { /* ARROW_SCROLL */
4593 xaw3d_arrow_scroll = True;
4594 /* Isn't that just a personal preference ? -sm */
4595 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4599 /* Define callbacks. */
4600 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4601 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4602 (XtPointer) bar);
4604 /* Realize the widget. Only after that is the X window created. */
4605 XtRealizeWidget (widget);
4607 #endif /* !USE_MOTIF */
4609 /* Install an action hook that lets us detect when the user
4610 finishes interacting with a scroll bar. */
4611 if (action_hook_id == 0)
4612 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4614 /* Remember X window and widget in the scroll bar vector. */
4615 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4616 xwindow = XtWindow (widget);
4617 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
4619 UNBLOCK_INPUT;
4621 #endif /* not USE_GTK */
4624 /* Set the thumb size and position of scroll bar BAR. We are currently
4625 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4627 #ifdef USE_GTK
4628 static void
4629 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4630 struct scroll_bar *bar;
4631 int portion, position, whole;
4633 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4636 #else /* not USE_GTK */
4637 static void
4638 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4639 struct scroll_bar *bar;
4640 int portion, position, whole;
4642 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4643 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4644 float top, shown;
4646 BLOCK_INPUT;
4648 #ifdef USE_MOTIF
4650 /* We use an estimate of 30 chars per line rather than the real
4651 `portion' value. This has the disadvantage that the thumb size
4652 is not very representative, but it makes our life a lot easier.
4653 Otherwise, we have to constantly adjust the thumb size, which
4654 we can't always do quickly enough: while dragging, the size of
4655 the thumb might prevent the user from dragging the thumb all the
4656 way to the end. but Motif and some versions of Xaw3d don't allow
4657 updating the thumb size while dragging. Also, even if we can update
4658 its size, the update will often happen too late.
4659 If you don't believe it, check out revision 1.650 of xterm.c to see
4660 what hoops we were going through and the still poor behavior we got. */
4661 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4662 /* When the thumb is at the bottom, position == whole.
4663 So we need to increase `whole' to make space for the thumb. */
4664 whole += portion;
4666 if (whole <= 0)
4667 top = 0, shown = 1;
4668 else
4670 top = (float) position / whole;
4671 shown = (float) portion / whole;
4674 if (NILP (bar->dragging))
4676 int size, value;
4678 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4679 is the scroll bar's maximum and MIN is the scroll bar's minimum
4680 value. */
4681 size = shown * XM_SB_MAX;
4682 size = min (size, XM_SB_MAX);
4683 size = max (size, 1);
4685 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4686 value = top * XM_SB_MAX;
4687 value = min (value, XM_SB_MAX - size);
4689 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4691 #else /* !USE_MOTIF i.e. use Xaw */
4693 if (whole == 0)
4694 top = 0, shown = 1;
4695 else
4697 top = (float) position / whole;
4698 shown = (float) portion / whole;
4702 float old_top, old_shown;
4703 Dimension height;
4704 XtVaGetValues (widget,
4705 XtNtopOfThumb, &old_top,
4706 XtNshown, &old_shown,
4707 XtNheight, &height,
4708 NULL);
4710 /* Massage the top+shown values. */
4711 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4712 top = max (0, min (1, top));
4713 else
4714 top = old_top;
4715 /* Keep two pixels available for moving the thumb down. */
4716 shown = max (0, min (1 - top - (2.0 / height), shown));
4718 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4719 check that your system's configuration file contains a define
4720 for `NARROWPROTO'. See s/freebsd.h for an example. */
4721 if (top != old_top || shown != old_shown)
4723 if (NILP (bar->dragging))
4724 XawScrollbarSetThumb (widget, top, shown);
4725 else
4727 #ifdef HAVE_XAW3D
4728 ScrollbarWidget sb = (ScrollbarWidget) widget;
4729 int scroll_mode = 0;
4731 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
4732 if (xaw3d_arrow_scroll)
4734 /* Xaw3d stupidly ignores resize requests while dragging
4735 so we have to make it believe it's not in dragging mode. */
4736 scroll_mode = sb->scrollbar.scroll_mode;
4737 if (scroll_mode == 2)
4738 sb->scrollbar.scroll_mode = 0;
4740 #endif
4741 /* Try to make the scrolling a tad smoother. */
4742 if (!xaw3d_pick_top)
4743 shown = min (shown, old_shown);
4745 XawScrollbarSetThumb (widget, top, shown);
4747 #ifdef HAVE_XAW3D
4748 if (xaw3d_arrow_scroll && scroll_mode == 2)
4749 sb->scrollbar.scroll_mode = scroll_mode;
4750 #endif
4754 #endif /* !USE_MOTIF */
4756 UNBLOCK_INPUT;
4758 #endif /* not USE_GTK */
4760 #endif /* USE_TOOLKIT_SCROLL_BARS */
4764 /************************************************************************
4765 Scroll bars, general
4766 ************************************************************************/
4768 /* Create a scroll bar and return the scroll bar vector for it. W is
4769 the Emacs window on which to create the scroll bar. TOP, LEFT,
4770 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4771 scroll bar. */
4773 static struct scroll_bar *
4774 x_scroll_bar_create (w, top, left, width, height)
4775 struct window *w;
4776 int top, left, width, height;
4778 struct frame *f = XFRAME (w->frame);
4779 struct scroll_bar *bar
4780 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
4782 BLOCK_INPUT;
4784 #ifdef USE_TOOLKIT_SCROLL_BARS
4785 x_create_toolkit_scroll_bar (f, bar);
4786 #else /* not USE_TOOLKIT_SCROLL_BARS */
4788 XSetWindowAttributes a;
4789 unsigned long mask;
4790 Window window;
4792 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4793 if (a.background_pixel == -1)
4794 a.background_pixel = f->output_data.x->background_pixel;
4796 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4797 | ButtonMotionMask | PointerMotionHintMask
4798 | ExposureMask);
4799 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4801 mask = (CWBackPixel | CWEventMask | CWCursor);
4803 /* Clear the area of W that will serve as a scroll bar. This is
4804 for the case that a window has been split horizontally. In
4805 this case, no clear_frame is generated to reduce flickering. */
4806 if (width > 0 && height > 0)
4807 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4808 left, top, width,
4809 window_box_height (w), False);
4811 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4812 /* Position and size of scroll bar. */
4813 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4814 top,
4815 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4816 height,
4817 /* Border width, depth, class, and visual. */
4819 CopyFromParent,
4820 CopyFromParent,
4821 CopyFromParent,
4822 /* Attributes. */
4823 mask, &a);
4824 SET_SCROLL_BAR_X_WINDOW (bar, window);
4826 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4828 XSETWINDOW (bar->window, w);
4829 XSETINT (bar->top, top);
4830 XSETINT (bar->left, left);
4831 XSETINT (bar->width, width);
4832 XSETINT (bar->height, height);
4833 XSETINT (bar->start, 0);
4834 XSETINT (bar->end, 0);
4835 bar->dragging = Qnil;
4837 /* Add bar to its frame's list of scroll bars. */
4838 bar->next = FRAME_SCROLL_BARS (f);
4839 bar->prev = Qnil;
4840 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4841 if (!NILP (bar->next))
4842 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4844 /* Map the window/widget. */
4845 #ifdef USE_TOOLKIT_SCROLL_BARS
4847 #ifdef USE_GTK
4848 xg_update_scrollbar_pos (f,
4849 SCROLL_BAR_X_WINDOW (bar),
4850 top,
4851 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4852 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4853 max (height, 1),
4854 left,
4855 width);
4856 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
4857 #else /* not USE_GTK */
4858 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4859 XtConfigureWidget (scroll_bar,
4860 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4861 top,
4862 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4863 max (height, 1), 0);
4864 XtMapWidget (scroll_bar);
4865 #endif /* not USE_GTK */
4867 #else /* not USE_TOOLKIT_SCROLL_BARS */
4868 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4869 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4871 UNBLOCK_INPUT;
4872 return bar;
4876 /* Draw BAR's handle in the proper position.
4878 If the handle is already drawn from START to END, don't bother
4879 redrawing it, unless REBUILD is non-zero; in that case, always
4880 redraw it. (REBUILD is handy for drawing the handle after expose
4881 events.)
4883 Normally, we want to constrain the start and end of the handle to
4884 fit inside its rectangle, but if the user is dragging the scroll
4885 bar handle, we want to let them drag it down all the way, so that
4886 the bar's top is as far down as it goes; otherwise, there's no way
4887 to move to the very end of the buffer. */
4889 #ifndef USE_TOOLKIT_SCROLL_BARS
4891 static void
4892 x_scroll_bar_set_handle (bar, start, end, rebuild)
4893 struct scroll_bar *bar;
4894 int start, end;
4895 int rebuild;
4897 int dragging = ! NILP (bar->dragging);
4898 Window w = SCROLL_BAR_X_WINDOW (bar);
4899 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4900 GC gc = f->output_data.x->normal_gc;
4902 /* If the display is already accurate, do nothing. */
4903 if (! rebuild
4904 && start == XINT (bar->start)
4905 && end == XINT (bar->end))
4906 return;
4908 BLOCK_INPUT;
4911 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
4912 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
4913 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4915 /* Make sure the values are reasonable, and try to preserve
4916 the distance between start and end. */
4918 int length = end - start;
4920 if (start < 0)
4921 start = 0;
4922 else if (start > top_range)
4923 start = top_range;
4924 end = start + length;
4926 if (end < start)
4927 end = start;
4928 else if (end > top_range && ! dragging)
4929 end = top_range;
4932 /* Store the adjusted setting in the scroll bar. */
4933 XSETINT (bar->start, start);
4934 XSETINT (bar->end, end);
4936 /* Clip the end position, just for display. */
4937 if (end > top_range)
4938 end = top_range;
4940 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
4941 below top positions, to make sure the handle is always at least
4942 that many pixels tall. */
4943 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
4945 /* Draw the empty space above the handle. Note that we can't clear
4946 zero-height areas; that means "clear to end of window." */
4947 if (0 < start)
4948 x_clear_area (FRAME_X_DISPLAY (f), w,
4949 /* x, y, width, height, and exposures. */
4950 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4951 VERTICAL_SCROLL_BAR_TOP_BORDER,
4952 inside_width, start,
4953 False);
4955 /* Change to proper foreground color if one is specified. */
4956 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4957 XSetForeground (FRAME_X_DISPLAY (f), gc,
4958 f->output_data.x->scroll_bar_foreground_pixel);
4960 /* Draw the handle itself. */
4961 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
4962 /* x, y, width, height */
4963 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4964 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
4965 inside_width, end - start);
4967 /* Restore the foreground color of the GC if we changed it above. */
4968 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4969 XSetForeground (FRAME_X_DISPLAY (f), gc,
4970 f->output_data.x->foreground_pixel);
4972 /* Draw the empty space below the handle. Note that we can't
4973 clear zero-height areas; that means "clear to end of window." */
4974 if (end < inside_height)
4975 x_clear_area (FRAME_X_DISPLAY (f), w,
4976 /* x, y, width, height, and exposures. */
4977 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4978 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
4979 inside_width, inside_height - end,
4980 False);
4984 UNBLOCK_INPUT;
4987 #endif /* !USE_TOOLKIT_SCROLL_BARS */
4989 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
4990 nil. */
4992 static void
4993 x_scroll_bar_remove (bar)
4994 struct scroll_bar *bar;
4996 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4997 BLOCK_INPUT;
4999 #ifdef USE_TOOLKIT_SCROLL_BARS
5000 #ifdef USE_GTK
5001 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
5002 #else /* not USE_GTK */
5003 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5004 #endif /* not USE_GTK */
5005 #else
5006 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
5007 #endif
5009 /* Disassociate this scroll bar from its window. */
5010 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5012 UNBLOCK_INPUT;
5016 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5017 that we are displaying PORTION characters out of a total of WHOLE
5018 characters, starting at POSITION. If WINDOW has no scroll bar,
5019 create one. */
5021 static void
5022 XTset_vertical_scroll_bar (w, portion, whole, position)
5023 struct window *w;
5024 int portion, whole, position;
5026 struct frame *f = XFRAME (w->frame);
5027 struct scroll_bar *bar;
5028 int top, height, left, sb_left, width, sb_width;
5029 int window_y, window_height;
5031 /* Get window dimensions. */
5032 window_box (w, -1, 0, &window_y, 0, &window_height);
5033 top = window_y;
5034 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5035 height = window_height;
5037 /* Compute the left edge of the scroll bar area. */
5038 left = WINDOW_SCROLL_BAR_AREA_X (w);
5040 /* Compute the width of the scroll bar which might be less than
5041 the width of the area reserved for the scroll bar. */
5042 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5043 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5044 else
5045 sb_width = width;
5047 /* Compute the left edge of the scroll bar. */
5048 #ifdef USE_TOOLKIT_SCROLL_BARS
5049 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5050 sb_left = left + width - sb_width - (width - sb_width) / 2;
5051 else
5052 sb_left = left + (width - sb_width) / 2;
5053 #else
5054 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5055 sb_left = left + width - sb_width;
5056 else
5057 sb_left = left;
5058 #endif
5060 /* Does the scroll bar exist yet? */
5061 if (NILP (w->vertical_scroll_bar))
5063 if (width > 0 && height > 0)
5065 BLOCK_INPUT;
5066 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5067 left, top, width, height, False);
5068 UNBLOCK_INPUT;
5071 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5073 else
5075 /* It may just need to be moved and resized. */
5076 unsigned int mask = 0;
5078 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5080 BLOCK_INPUT;
5082 if (sb_left != XINT (bar->left))
5083 mask |= CWX;
5084 if (top != XINT (bar->top))
5085 mask |= CWY;
5086 if (sb_width != XINT (bar->width))
5087 mask |= CWWidth;
5088 if (height != XINT (bar->height))
5089 mask |= CWHeight;
5091 #ifdef USE_TOOLKIT_SCROLL_BARS
5093 #ifdef USE_GTK
5094 if (mask)
5095 xg_update_scrollbar_pos (f,
5096 SCROLL_BAR_X_WINDOW (bar),
5097 top,
5098 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5099 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5100 max (height, 1),
5101 left,
5102 width);
5103 #else /* not USE_GTK */
5105 /* Since toolkit scroll bars are smaller than the space reserved
5106 for them on the frame, we have to clear "under" them. */
5107 if (width > 0 && height > 0)
5108 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5109 left, top, width, height, False);
5110 /* Move/size the scroll bar widget. */
5111 if (mask)
5112 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5113 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5114 top,
5115 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5116 max (height, 1), 0);
5118 #endif /* not USE_GTK */
5119 #else /* not USE_TOOLKIT_SCROLL_BARS */
5121 /* Clear areas not covered by the scroll bar because of
5122 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5123 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5125 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5126 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5127 height, False);
5128 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5129 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5130 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5131 height, False);
5134 /* Clear areas not covered by the scroll bar because it's not as
5135 wide as the area reserved for it. This makes sure a
5136 previous mode line display is cleared after C-x 2 C-x 1, for
5137 example. */
5139 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5140 int rest = area_width - sb_width;
5141 if (rest > 0 && height > 0)
5143 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5144 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5145 left + area_width - rest, top,
5146 rest, height, False);
5147 else
5148 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5149 left, top, rest, height, False);
5153 /* Move/size the scroll bar window. */
5154 if (mask)
5156 XWindowChanges wc;
5158 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5159 wc.y = top;
5160 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5161 wc.height = height;
5162 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
5163 mask, &wc);
5166 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5168 /* Remember new settings. */
5169 XSETINT (bar->left, sb_left);
5170 XSETINT (bar->top, top);
5171 XSETINT (bar->width, sb_width);
5172 XSETINT (bar->height, height);
5174 UNBLOCK_INPUT;
5177 #ifdef USE_TOOLKIT_SCROLL_BARS
5178 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5179 #else /* not USE_TOOLKIT_SCROLL_BARS */
5180 /* Set the scroll bar's current state, unless we're currently being
5181 dragged. */
5182 if (NILP (bar->dragging))
5184 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5186 if (whole == 0)
5187 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5188 else
5190 int start = ((double) position * top_range) / whole;
5191 int end = ((double) (position + portion) * top_range) / whole;
5192 x_scroll_bar_set_handle (bar, start, end, 0);
5195 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5197 XSETVECTOR (w->vertical_scroll_bar, bar);
5201 /* The following three hooks are used when we're doing a thorough
5202 redisplay of the frame. We don't explicitly know which scroll bars
5203 are going to be deleted, because keeping track of when windows go
5204 away is a real pain - "Can you say set-window-configuration, boys
5205 and girls?" Instead, we just assert at the beginning of redisplay
5206 that *all* scroll bars are to be removed, and then save a scroll bar
5207 from the fiery pit when we actually redisplay its window. */
5209 /* Arrange for all scroll bars on FRAME to be removed at the next call
5210 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5211 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5213 static void
5214 XTcondemn_scroll_bars (frame)
5215 FRAME_PTR frame;
5217 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5218 while (! NILP (FRAME_SCROLL_BARS (frame)))
5220 Lisp_Object bar;
5221 bar = FRAME_SCROLL_BARS (frame);
5222 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5223 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5224 XSCROLL_BAR (bar)->prev = Qnil;
5225 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5226 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5227 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5232 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5233 Note that WINDOW isn't necessarily condemned at all. */
5235 static void
5236 XTredeem_scroll_bar (window)
5237 struct window *window;
5239 struct scroll_bar *bar;
5240 struct frame *f;
5242 /* We can't redeem this window's scroll bar if it doesn't have one. */
5243 if (NILP (window->vertical_scroll_bar))
5244 abort ();
5246 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5248 /* Unlink it from the condemned list. */
5249 f = XFRAME (WINDOW_FRAME (window));
5250 if (NILP (bar->prev))
5252 /* If the prev pointer is nil, it must be the first in one of
5253 the lists. */
5254 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5255 /* It's not condemned. Everything's fine. */
5256 return;
5257 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5258 window->vertical_scroll_bar))
5259 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5260 else
5261 /* If its prev pointer is nil, it must be at the front of
5262 one or the other! */
5263 abort ();
5265 else
5266 XSCROLL_BAR (bar->prev)->next = bar->next;
5268 if (! NILP (bar->next))
5269 XSCROLL_BAR (bar->next)->prev = bar->prev;
5271 bar->next = FRAME_SCROLL_BARS (f);
5272 bar->prev = Qnil;
5273 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5274 if (! NILP (bar->next))
5275 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5278 /* Remove all scroll bars on FRAME that haven't been saved since the
5279 last call to `*condemn_scroll_bars_hook'. */
5281 static void
5282 XTjudge_scroll_bars (f)
5283 FRAME_PTR f;
5285 Lisp_Object bar, next;
5287 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5289 /* Clear out the condemned list now so we won't try to process any
5290 more events on the hapless scroll bars. */
5291 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5293 for (; ! NILP (bar); bar = next)
5295 struct scroll_bar *b = XSCROLL_BAR (bar);
5297 x_scroll_bar_remove (b);
5299 next = b->next;
5300 b->next = b->prev = Qnil;
5303 /* Now there should be no references to the condemned scroll bars,
5304 and they should get garbage-collected. */
5308 #ifndef USE_TOOLKIT_SCROLL_BARS
5309 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5310 is a no-op when using toolkit scroll bars.
5312 This may be called from a signal handler, so we have to ignore GC
5313 mark bits. */
5315 static void
5316 x_scroll_bar_expose (bar, event)
5317 struct scroll_bar *bar;
5318 XEvent *event;
5320 Window w = SCROLL_BAR_X_WINDOW (bar);
5321 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5322 GC gc = f->output_data.x->normal_gc;
5323 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5325 BLOCK_INPUT;
5327 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
5329 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5330 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5332 /* x, y, width, height */
5333 0, 0,
5334 XINT (bar->width) - 1 - width_trim - width_trim,
5335 XINT (bar->height) - 1);
5337 UNBLOCK_INPUT;
5340 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5342 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5343 is set to something other than NO_EVENT, it is enqueued.
5345 This may be called from a signal handler, so we have to ignore GC
5346 mark bits. */
5349 static void
5350 x_scroll_bar_handle_click (bar, event, emacs_event)
5351 struct scroll_bar *bar;
5352 XEvent *event;
5353 struct input_event *emacs_event;
5355 if (! GC_WINDOWP (bar->window))
5356 abort ();
5358 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5359 emacs_event->code = event->xbutton.button - Button1;
5360 emacs_event->modifiers
5361 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5362 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5363 event->xbutton.state)
5364 | (event->type == ButtonRelease
5365 ? up_modifier
5366 : down_modifier));
5367 emacs_event->frame_or_window = bar->window;
5368 emacs_event->arg = Qnil;
5369 emacs_event->timestamp = event->xbutton.time;
5371 #if 0
5372 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5373 int internal_height
5374 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5375 #endif
5376 int top_range
5377 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5378 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5380 if (y < 0) y = 0;
5381 if (y > top_range) y = top_range;
5383 if (y < XINT (bar->start))
5384 emacs_event->part = scroll_bar_above_handle;
5385 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5386 emacs_event->part = scroll_bar_handle;
5387 else
5388 emacs_event->part = scroll_bar_below_handle;
5390 /* Just because the user has clicked on the handle doesn't mean
5391 they want to drag it. Lisp code needs to be able to decide
5392 whether or not we're dragging. */
5393 #if 0
5394 /* If the user has just clicked on the handle, record where they're
5395 holding it. */
5396 if (event->type == ButtonPress
5397 && emacs_event->part == scroll_bar_handle)
5398 XSETINT (bar->dragging, y - XINT (bar->start));
5399 #endif
5401 #ifndef USE_TOOLKIT_SCROLL_BARS
5402 /* If the user has released the handle, set it to its final position. */
5403 if (event->type == ButtonRelease
5404 && ! NILP (bar->dragging))
5406 int new_start = y - XINT (bar->dragging);
5407 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5409 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5410 bar->dragging = Qnil;
5412 #endif
5414 /* Same deal here as the other #if 0. */
5415 #if 0
5416 /* Clicks on the handle are always reported as occurring at the top of
5417 the handle. */
5418 if (emacs_event->part == scroll_bar_handle)
5419 emacs_event->x = bar->start;
5420 else
5421 XSETINT (emacs_event->x, y);
5422 #else
5423 XSETINT (emacs_event->x, y);
5424 #endif
5426 XSETINT (emacs_event->y, top_range);
5430 #ifndef USE_TOOLKIT_SCROLL_BARS
5432 /* Handle some mouse motion while someone is dragging the scroll bar.
5434 This may be called from a signal handler, so we have to ignore GC
5435 mark bits. */
5437 static void
5438 x_scroll_bar_note_movement (bar, event)
5439 struct scroll_bar *bar;
5440 XEvent *event;
5442 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5444 last_mouse_movement_time = event->xmotion.time;
5446 f->mouse_moved = 1;
5447 XSETVECTOR (last_mouse_scroll_bar, bar);
5449 /* If we're dragging the bar, display it. */
5450 if (! GC_NILP (bar->dragging))
5452 /* Where should the handle be now? */
5453 int new_start = event->xmotion.y - XINT (bar->dragging);
5455 if (new_start != XINT (bar->start))
5457 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5459 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5464 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5466 /* Return information to the user about the current position of the mouse
5467 on the scroll bar. */
5469 static void
5470 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5471 FRAME_PTR *fp;
5472 Lisp_Object *bar_window;
5473 enum scroll_bar_part *part;
5474 Lisp_Object *x, *y;
5475 unsigned long *time;
5477 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5478 Window w = SCROLL_BAR_X_WINDOW (bar);
5479 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5480 int win_x, win_y;
5481 Window dummy_window;
5482 int dummy_coord;
5483 unsigned int dummy_mask;
5485 BLOCK_INPUT;
5487 /* Get the mouse's position relative to the scroll bar window, and
5488 report that. */
5489 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5491 /* Root, child, root x and root y. */
5492 &dummy_window, &dummy_window,
5493 &dummy_coord, &dummy_coord,
5495 /* Position relative to scroll bar. */
5496 &win_x, &win_y,
5498 /* Mouse buttons and modifier keys. */
5499 &dummy_mask))
5501 else
5503 #if 0
5504 int inside_height
5505 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5506 #endif
5507 int top_range
5508 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5510 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5512 if (! NILP (bar->dragging))
5513 win_y -= XINT (bar->dragging);
5515 if (win_y < 0)
5516 win_y = 0;
5517 if (win_y > top_range)
5518 win_y = top_range;
5520 *fp = f;
5521 *bar_window = bar->window;
5523 if (! NILP (bar->dragging))
5524 *part = scroll_bar_handle;
5525 else if (win_y < XINT (bar->start))
5526 *part = scroll_bar_above_handle;
5527 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5528 *part = scroll_bar_handle;
5529 else
5530 *part = scroll_bar_below_handle;
5532 XSETINT (*x, win_y);
5533 XSETINT (*y, top_range);
5535 f->mouse_moved = 0;
5536 last_mouse_scroll_bar = Qnil;
5539 *time = last_mouse_movement_time;
5541 UNBLOCK_INPUT;
5545 /* The screen has been cleared so we may have changed foreground or
5546 background colors, and the scroll bars may need to be redrawn.
5547 Clear out the scroll bars, and ask for expose events, so we can
5548 redraw them. */
5550 void
5551 x_scroll_bar_clear (f)
5552 FRAME_PTR f;
5554 #ifndef USE_TOOLKIT_SCROLL_BARS
5555 Lisp_Object bar;
5557 /* We can have scroll bars even if this is 0,
5558 if we just turned off scroll bar mode.
5559 But in that case we should not clear them. */
5560 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5561 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5562 bar = XSCROLL_BAR (bar)->next)
5563 XClearArea (FRAME_X_DISPLAY (f),
5564 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
5565 0, 0, 0, 0, True);
5566 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5570 /* Define a queue to save up SelectionRequest events for later handling. */
5572 struct selection_event_queue
5574 XEvent event;
5575 struct selection_event_queue *next;
5578 static struct selection_event_queue *queue;
5580 /* Nonzero means queue up certain events--don't process them yet. */
5582 static int x_queue_selection_requests;
5584 /* Queue up an X event *EVENT, to be processed later. */
5586 static void
5587 x_queue_event (f, event)
5588 FRAME_PTR f;
5589 XEvent *event;
5591 struct selection_event_queue *queue_tmp
5592 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
5594 if (queue_tmp != NULL)
5596 queue_tmp->event = *event;
5597 queue_tmp->next = queue;
5598 queue = queue_tmp;
5602 /* Take all the queued events and put them back
5603 so that they get processed afresh. */
5605 static void
5606 x_unqueue_events (display)
5607 Display *display;
5609 while (queue != NULL)
5611 struct selection_event_queue *queue_tmp = queue;
5612 XPutBackEvent (display, &queue_tmp->event);
5613 queue = queue_tmp->next;
5614 xfree ((char *)queue_tmp);
5618 /* Start queuing SelectionRequest events. */
5620 void
5621 x_start_queuing_selection_requests (display)
5622 Display *display;
5624 x_queue_selection_requests++;
5627 /* Stop queuing SelectionRequest events. */
5629 void
5630 x_stop_queuing_selection_requests (display)
5631 Display *display;
5633 x_queue_selection_requests--;
5634 x_unqueue_events (display);
5637 /* The main X event-reading loop - XTread_socket. */
5639 #if 0
5640 /* Time stamp of enter window event. This is only used by XTread_socket,
5641 but we have to put it out here, since static variables within functions
5642 sometimes don't work. */
5644 static Time enter_timestamp;
5645 #endif
5647 /* This holds the state XLookupString needs to implement dead keys
5648 and other tricks known as "compose processing". _X Window System_
5649 says that a portable program can't use this, but Stephen Gildea assures
5650 me that letting the compiler initialize it to zeros will work okay.
5652 This must be defined outside of XTread_socket, for the same reasons
5653 given for enter_timestamp, above. */
5655 static XComposeStatus compose_status;
5657 /* Record the last 100 characters stored
5658 to help debug the loss-of-chars-during-GC problem. */
5660 static int temp_index;
5661 static short temp_buffer[100];
5663 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5664 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5665 temp_index = 0; \
5666 temp_buffer[temp_index++] = (keysym)
5668 /* Set this to nonzero to fake an "X I/O error"
5669 on a particular display. */
5671 struct x_display_info *XTread_socket_fake_io_error;
5673 /* When we find no input here, we occasionally do a no-op command
5674 to verify that the X server is still running and we can still talk with it.
5675 We try all the open displays, one by one.
5676 This variable is used for cycling thru the displays. */
5678 static struct x_display_info *next_noop_dpyinfo;
5680 #define SET_SAVED_MENU_EVENT(size) \
5681 do \
5683 if (f->output_data.x->saved_menu_event == 0) \
5684 f->output_data.x->saved_menu_event \
5685 = (XEvent *) xmalloc (sizeof (XEvent)); \
5686 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5687 inev.kind = MENU_BAR_ACTIVATE_EVENT; \
5688 XSETFRAME (inev.frame_or_window, f); \
5690 while (0)
5692 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5693 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5696 enum
5698 X_EVENT_NORMAL,
5699 X_EVENT_GOTO_OUT,
5700 X_EVENT_DROP
5703 /* Filter events for the current X input method.
5704 DPYINFO is the display this event is for.
5705 EVENT is the X event to filter.
5707 Returns non-zero if the event was filtered, caller shall not process
5708 this event further.
5709 Returns zero if event is wasn't filtered. */
5711 #ifdef HAVE_X_I18N
5712 static int
5713 x_filter_event (dpyinfo, event)
5714 struct x_display_info *dpyinfo;
5715 XEvent *event;
5717 /* XFilterEvent returns non-zero if the input method has
5718 consumed the event. We pass the frame's X window to
5719 XFilterEvent because that's the one for which the IC
5720 was created. */
5722 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5723 event->xclient.window);
5725 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5727 #endif
5729 #ifdef USE_GTK
5730 static int current_count;
5731 static int current_finish;
5732 static struct input_event *current_hold_quit;
5734 /* This is the filter function invoked by the GTK event loop.
5735 It is invoked before the XEvent is translated to a GdkEvent,
5736 so we have a chance to act on the event before GTK. */
5737 static GdkFilterReturn
5738 event_handler_gdk (gxev, ev, data)
5739 GdkXEvent *gxev;
5740 GdkEvent *ev;
5741 gpointer data;
5743 XEvent *xev = (XEvent *) gxev;
5745 if (current_count >= 0)
5747 struct x_display_info *dpyinfo;
5749 dpyinfo = x_display_info_for_display (xev->xany.display);
5751 #ifdef HAVE_X_I18N
5752 /* Filter events for the current X input method.
5753 GTK calls XFilterEvent but not for key press and release,
5754 so we do it here. */
5755 if (xev->type == KeyPress || xev->type == KeyRelease)
5756 if (dpyinfo && x_filter_event (dpyinfo, xev))
5757 return GDK_FILTER_REMOVE;
5758 #endif
5760 if (! dpyinfo)
5761 current_finish = X_EVENT_NORMAL;
5762 else
5764 current_count +=
5765 handle_one_xevent (dpyinfo, xev, &current_finish,
5766 current_hold_quit);
5769 else
5770 current_finish = x_dispatch_event (xev, xev->xany.display);
5772 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5773 return GDK_FILTER_REMOVE;
5775 return GDK_FILTER_CONTINUE;
5777 #endif /* USE_GTK */
5780 /* Handles the XEvent EVENT on display DPYINFO.
5782 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5783 *FINISH is zero if caller should continue reading events.
5784 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5786 We return the number of characters stored into the buffer. */
5788 static int
5789 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5790 struct x_display_info *dpyinfo;
5791 XEvent *eventp;
5792 int *finish;
5793 struct input_event *hold_quit;
5795 struct input_event inev;
5796 int count = 0;
5797 int do_help = 0;
5798 int nbytes = 0;
5799 struct frame *f;
5800 struct coding_system coding;
5801 XEvent event = *eventp;
5803 *finish = X_EVENT_NORMAL;
5805 EVENT_INIT (inev);
5806 inev.kind = NO_EVENT;
5807 inev.arg = Qnil;
5809 switch (event.type)
5811 case ClientMessage:
5813 if (event.xclient.message_type
5814 == dpyinfo->Xatom_wm_protocols
5815 && event.xclient.format == 32)
5817 if (event.xclient.data.l[0]
5818 == dpyinfo->Xatom_wm_take_focus)
5820 /* Use x_any_window_to_frame because this
5821 could be the shell widget window
5822 if the frame has no title bar. */
5823 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5824 #ifdef HAVE_X_I18N
5825 /* Not quite sure this is needed -pd */
5826 if (f && FRAME_XIC (f))
5827 XSetICFocus (FRAME_XIC (f));
5828 #endif
5829 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5830 instructs the WM to set the input focus automatically for
5831 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5832 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5833 it has set the focus. So, XSetInputFocus below is not
5834 needed.
5836 The call to XSetInputFocus below has also caused trouble. In
5837 cases where the XSetInputFocus done by the WM and the one
5838 below are temporally close (on a fast machine), the call
5839 below can generate additional FocusIn events which confuse
5840 Emacs. */
5842 /* Since we set WM_TAKE_FOCUS, we must call
5843 XSetInputFocus explicitly. But not if f is null,
5844 since that might be an event for a deleted frame. */
5845 if (f)
5847 Display *d = event.xclient.display;
5848 /* Catch and ignore errors, in case window has been
5849 iconified by a window manager such as GWM. */
5850 int count = x_catch_errors (d);
5851 XSetInputFocus (d, event.xclient.window,
5852 /* The ICCCM says this is
5853 the only valid choice. */
5854 RevertToParent,
5855 event.xclient.data.l[1]);
5856 /* This is needed to detect the error
5857 if there is an error. */
5858 XSync (d, False);
5859 x_uncatch_errors (d, count);
5861 /* Not certain about handling scroll bars here */
5862 #endif /* 0 */
5863 goto done;
5866 if (event.xclient.data.l[0]
5867 == dpyinfo->Xatom_wm_save_yourself)
5869 /* Save state modify the WM_COMMAND property to
5870 something which can reinstate us. This notifies
5871 the session manager, who's looking for such a
5872 PropertyNotify. Can restart processing when
5873 a keyboard or mouse event arrives. */
5874 /* If we have a session manager, don't set this.
5875 KDE will then start two Emacsen, one for the
5876 session manager and one for this. */
5877 #ifdef HAVE_X_SM
5878 if (! x_session_have_connection ())
5879 #endif
5881 f = x_top_window_to_frame (dpyinfo,
5882 event.xclient.window);
5883 /* This is just so we only give real data once
5884 for a single Emacs process. */
5885 if (f == SELECTED_FRAME ())
5886 XSetCommand (FRAME_X_DISPLAY (f),
5887 event.xclient.window,
5888 initial_argv, initial_argc);
5889 else if (f)
5890 XSetCommand (FRAME_X_DISPLAY (f),
5891 event.xclient.window,
5892 0, 0);
5894 goto done;
5897 if (event.xclient.data.l[0]
5898 == dpyinfo->Xatom_wm_delete_window)
5900 f = x_any_window_to_frame (dpyinfo,
5901 event.xclient.window);
5902 if (!f)
5903 goto OTHER; /* May be a dialog that is to be removed */
5905 inev.kind = DELETE_WINDOW_EVENT;
5906 XSETFRAME (inev.frame_or_window, f);
5907 goto done;
5910 goto done;
5913 if (event.xclient.message_type
5914 == dpyinfo->Xatom_wm_configure_denied)
5916 goto done;
5919 if (event.xclient.message_type
5920 == dpyinfo->Xatom_wm_window_moved)
5922 int new_x, new_y;
5923 f = x_window_to_frame (dpyinfo, event.xclient.window);
5925 new_x = event.xclient.data.s[0];
5926 new_y = event.xclient.data.s[1];
5928 if (f)
5930 f->left_pos = new_x;
5931 f->top_pos = new_y;
5933 goto done;
5936 #ifdef HACK_EDITRES
5937 if (event.xclient.message_type
5938 == dpyinfo->Xatom_editres)
5940 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5941 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5942 &event, NULL);
5943 goto done;
5945 #endif /* HACK_EDITRES */
5947 if ((event.xclient.message_type
5948 == dpyinfo->Xatom_DONE)
5949 || (event.xclient.message_type
5950 == dpyinfo->Xatom_PAGE))
5952 /* Ghostview job completed. Kill it. We could
5953 reply with "Next" if we received "Page", but we
5954 currently never do because we are interested in
5955 images, only, which should have 1 page. */
5956 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5957 f = x_window_to_frame (dpyinfo, event.xclient.window);
5958 x_kill_gs_process (pixmap, f);
5959 expose_frame (f, 0, 0, 0, 0);
5960 goto done;
5963 #ifdef USE_TOOLKIT_SCROLL_BARS
5964 /* Scroll bar callbacks send a ClientMessage from which
5965 we construct an input_event. */
5966 if (event.xclient.message_type
5967 == dpyinfo->Xatom_Scrollbar)
5969 x_scroll_bar_to_input_event (&event, &inev);
5970 *finish = X_EVENT_GOTO_OUT;
5971 goto done;
5973 #endif /* USE_TOOLKIT_SCROLL_BARS */
5975 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5977 if (!f)
5978 goto OTHER;
5980 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev))
5981 *finish = X_EVENT_DROP;
5983 break;
5985 case SelectionNotify:
5986 #ifdef USE_X_TOOLKIT
5987 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
5988 goto OTHER;
5989 #endif /* not USE_X_TOOLKIT */
5990 x_handle_selection_notify (&event.xselection);
5991 break;
5993 case SelectionClear: /* Someone has grabbed ownership. */
5994 #ifdef USE_X_TOOLKIT
5995 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
5996 goto OTHER;
5997 #endif /* USE_X_TOOLKIT */
5999 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
6001 inev.kind = SELECTION_CLEAR_EVENT;
6002 SELECTION_EVENT_DISPLAY (&inev) = eventp->display;
6003 SELECTION_EVENT_SELECTION (&inev) = eventp->selection;
6004 SELECTION_EVENT_TIME (&inev) = eventp->time;
6005 inev.frame_or_window = Qnil;
6007 break;
6009 case SelectionRequest: /* Someone wants our selection. */
6010 #ifdef USE_X_TOOLKIT
6011 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6012 goto OTHER;
6013 #endif /* USE_X_TOOLKIT */
6014 if (x_queue_selection_requests)
6015 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
6016 &event);
6017 else
6019 XSelectionRequestEvent *eventp
6020 = (XSelectionRequestEvent *) &event;
6022 inev.kind = SELECTION_REQUEST_EVENT;
6023 SELECTION_EVENT_DISPLAY (&inev) = eventp->display;
6024 SELECTION_EVENT_REQUESTOR (&inev) = eventp->requestor;
6025 SELECTION_EVENT_SELECTION (&inev) = eventp->selection;
6026 SELECTION_EVENT_TARGET (&inev) = eventp->target;
6027 SELECTION_EVENT_PROPERTY (&inev) = eventp->property;
6028 SELECTION_EVENT_TIME (&inev) = eventp->time;
6029 inev.frame_or_window = Qnil;
6031 break;
6033 case PropertyNotify:
6034 #if 0 /* This is plain wrong. In the case that we are waiting for a
6035 PropertyNotify used as an ACK in incremental selection
6036 transfer, the property will be on the receiver's window. */
6037 #if defined USE_X_TOOLKIT
6038 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
6039 goto OTHER;
6040 #endif
6041 #endif
6042 x_handle_property_notify (&event.xproperty);
6043 goto OTHER;
6045 case ReparentNotify:
6046 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6047 if (f)
6049 int x, y;
6050 f->output_data.x->parent_desc = event.xreparent.parent;
6051 x_real_positions (f, &x, &y);
6052 f->left_pos = x;
6053 f->top_pos = y;
6055 /* Perhaps reparented due to a WM restart. Reset this. */
6056 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6058 goto OTHER;
6060 case Expose:
6061 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6062 if (f)
6064 x_check_fullscreen (f);
6066 if (f->async_visible == 0)
6068 f->async_visible = 1;
6069 f->async_iconified = 0;
6070 f->output_data.x->has_been_visible = 1;
6071 SET_FRAME_GARBAGED (f);
6073 else
6074 expose_frame (f,
6075 event.xexpose.x, event.xexpose.y,
6076 event.xexpose.width, event.xexpose.height);
6078 else
6080 #ifndef USE_TOOLKIT_SCROLL_BARS
6081 struct scroll_bar *bar;
6082 #endif
6083 #if defined USE_LUCID
6084 /* Submenus of the Lucid menu bar aren't widgets
6085 themselves, so there's no way to dispatch events
6086 to them. Recognize this case separately. */
6088 Widget widget
6089 = x_window_to_menu_bar (event.xexpose.window);
6090 if (widget)
6091 xlwmenu_redisplay (widget);
6093 #endif /* USE_LUCID */
6095 #ifdef USE_TOOLKIT_SCROLL_BARS
6096 /* Dispatch event to the widget. */
6097 goto OTHER;
6098 #else /* not USE_TOOLKIT_SCROLL_BARS */
6099 bar = x_window_to_scroll_bar (event.xexpose.display,
6100 event.xexpose.window);
6102 if (bar)
6103 x_scroll_bar_expose (bar, &event);
6104 #ifdef USE_X_TOOLKIT
6105 else
6106 goto OTHER;
6107 #endif /* USE_X_TOOLKIT */
6108 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6110 break;
6112 case GraphicsExpose: /* This occurs when an XCopyArea's
6113 source area was obscured or not
6114 available. */
6115 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6116 if (f)
6118 expose_frame (f,
6119 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6120 event.xgraphicsexpose.width,
6121 event.xgraphicsexpose.height);
6123 #ifdef USE_X_TOOLKIT
6124 else
6125 goto OTHER;
6126 #endif /* USE_X_TOOLKIT */
6127 break;
6129 case NoExpose: /* This occurs when an XCopyArea's
6130 source area was completely
6131 available. */
6132 break;
6134 case UnmapNotify:
6135 /* Redo the mouse-highlight after the tooltip has gone. */
6136 if (event.xmap.window == tip_window)
6138 tip_window = 0;
6139 redo_mouse_highlight ();
6142 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6143 if (f) /* F may no longer exist if
6144 the frame was deleted. */
6146 /* While a frame is unmapped, display generation is
6147 disabled; you don't want to spend time updating a
6148 display that won't ever be seen. */
6149 f->async_visible = 0;
6150 /* We can't distinguish, from the event, whether the window
6151 has become iconified or invisible. So assume, if it
6152 was previously visible, than now it is iconified.
6153 But x_make_frame_invisible clears both
6154 the visible flag and the iconified flag;
6155 and that way, we know the window is not iconified now. */
6156 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6158 f->async_iconified = 1;
6160 inev.kind = ICONIFY_EVENT;
6161 XSETFRAME (inev.frame_or_window, f);
6164 goto OTHER;
6166 case MapNotify:
6167 if (event.xmap.window == tip_window)
6168 /* The tooltip has been drawn already. Avoid
6169 the SET_FRAME_GARBAGED below. */
6170 goto OTHER;
6172 /* We use x_top_window_to_frame because map events can
6173 come for sub-windows and they don't mean that the
6174 frame is visible. */
6175 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6176 if (f)
6178 /* wait_reading_process_input will notice this and update
6179 the frame's display structures.
6180 If we where iconified, we should not set garbaged,
6181 because that stops redrawing on Expose events. This looks
6182 bad if we are called from a recursive event loop
6183 (x_dispatch_event), for example when a dialog is up. */
6184 if (! f->async_iconified)
6185 SET_FRAME_GARBAGED (f);
6187 f->async_visible = 1;
6188 f->async_iconified = 0;
6189 f->output_data.x->has_been_visible = 1;
6191 if (f->iconified)
6193 inev.kind = DEICONIFY_EVENT;
6194 XSETFRAME (inev.frame_or_window, f);
6196 else if (! NILP (Vframe_list)
6197 && ! NILP (XCDR (Vframe_list)))
6198 /* Force a redisplay sooner or later
6199 to update the frame titles
6200 in case this is the second frame. */
6201 record_asynch_buffer_change ();
6203 goto OTHER;
6205 case KeyPress:
6207 ignore_next_mouse_click_timeout = 0;
6209 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6210 /* Dispatch KeyPress events when in menu. */
6211 if (popup_activated ())
6212 goto OTHER;
6213 #endif
6215 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6217 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
6219 clear_mouse_face (dpyinfo);
6220 dpyinfo->mouse_face_hidden = 1;
6223 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6224 if (f == 0)
6226 /* Scroll bars consume key events, but we want
6227 the keys to go to the scroll bar's frame. */
6228 Widget widget = XtWindowToWidget (dpyinfo->display,
6229 event.xkey.window);
6230 if (widget && XmIsScrollBar (widget))
6232 widget = XtParent (widget);
6233 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6236 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6238 if (f != 0)
6240 KeySym keysym, orig_keysym;
6241 /* al%imercury@uunet.uu.net says that making this 81
6242 instead of 80 fixed a bug whereby meta chars made
6243 his Emacs hang.
6245 It seems that some version of XmbLookupString has
6246 a bug of not returning XBufferOverflow in
6247 status_return even if the input is too long to
6248 fit in 81 bytes. So, we must prepare sufficient
6249 bytes for copy_buffer. 513 bytes (256 chars for
6250 two-byte character set) seems to be a fairly good
6251 approximation. -- 2000.8.10 handa@etl.go.jp */
6252 unsigned char copy_buffer[513];
6253 unsigned char *copy_bufptr = copy_buffer;
6254 int copy_bufsiz = sizeof (copy_buffer);
6255 int modifiers;
6256 Lisp_Object coding_system = Qlatin_1;
6257 Lisp_Object c;
6259 #ifdef USE_GTK
6260 /* Don't pass keys to GTK. A Tab will shift focus to the
6261 tool bar in GTK 2.4. Keys will still go to menus and
6262 dialogs because in that case popup_activated is TRUE
6263 (see above). */
6264 *finish = X_EVENT_DROP;
6265 #endif
6267 event.xkey.state
6268 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6269 extra_keyboard_modifiers);
6270 modifiers = event.xkey.state;
6272 /* This will have to go some day... */
6274 /* make_lispy_event turns chars into control chars.
6275 Don't do it here because XLookupString is too eager. */
6276 event.xkey.state &= ~ControlMask;
6277 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6278 | dpyinfo->super_mod_mask
6279 | dpyinfo->hyper_mod_mask
6280 | dpyinfo->alt_mod_mask);
6282 /* In case Meta is ComposeCharacter,
6283 clear its status. According to Markus Ehrnsperger
6284 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6285 this enables ComposeCharacter to work whether or
6286 not it is combined with Meta. */
6287 if (modifiers & dpyinfo->meta_mod_mask)
6288 bzero (&compose_status, sizeof (compose_status));
6290 #ifdef HAVE_X_I18N
6291 if (FRAME_XIC (f))
6293 Status status_return;
6295 coding_system = Vlocale_coding_system;
6296 nbytes = XmbLookupString (FRAME_XIC (f),
6297 &event.xkey, copy_bufptr,
6298 copy_bufsiz, &keysym,
6299 &status_return);
6300 if (status_return == XBufferOverflow)
6302 copy_bufsiz = nbytes + 1;
6303 copy_bufptr = (char *) alloca (copy_bufsiz);
6304 nbytes = XmbLookupString (FRAME_XIC (f),
6305 &event.xkey, copy_bufptr,
6306 copy_bufsiz, &keysym,
6307 &status_return);
6309 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6310 #if 0 && defined X_HAVE_UTF8_STRING
6311 else if (status_return == XLookupKeySym)
6312 { /* Try again but with utf-8. */
6313 coding_system = Qutf_8;
6314 nbytes = Xutf8LookupString (FRAME_XIC (f),
6315 &event.xkey, copy_bufptr,
6316 copy_bufsiz, &keysym,
6317 &status_return);
6318 if (status_return == XBufferOverflow)
6320 copy_bufsiz = nbytes + 1;
6321 copy_bufptr = (char *) alloca (copy_bufsiz);
6322 nbytes = Xutf8LookupString (FRAME_XIC (f),
6323 &event.xkey,
6324 copy_bufptr,
6325 copy_bufsiz, &keysym,
6326 &status_return);
6329 #endif
6331 if (status_return == XLookupNone)
6332 break;
6333 else if (status_return == XLookupChars)
6335 keysym = NoSymbol;
6336 modifiers = 0;
6338 else if (status_return != XLookupKeySym
6339 && status_return != XLookupBoth)
6340 abort ();
6342 else
6343 nbytes = XLookupString (&event.xkey, copy_bufptr,
6344 copy_bufsiz, &keysym,
6345 &compose_status);
6346 #else
6347 nbytes = XLookupString (&event.xkey, copy_bufptr,
6348 copy_bufsiz, &keysym,
6349 &compose_status);
6350 #endif
6352 /* If not using XIM/XIC, and a compose sequence is in progress,
6353 we break here. Otherwise, chars_matched is always 0. */
6354 if (compose_status.chars_matched > 0 && nbytes == 0)
6355 break;
6357 orig_keysym = keysym;
6359 /* Common for all keysym input events. */
6360 XSETFRAME (inev.frame_or_window, f);
6361 inev.modifiers
6362 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6363 inev.timestamp = event.xkey.time;
6365 /* First deal with keysyms which have defined
6366 translations to characters. */
6367 if (keysym >= 32 && keysym < 128)
6368 /* Avoid explicitly decoding each ASCII character. */
6370 inev.kind = ASCII_KEYSTROKE_EVENT;
6371 inev.code = keysym;
6372 goto done_keysym;
6375 /* Now non-ASCII. */
6376 if (HASH_TABLE_P (Vx_keysym_table)
6377 && (NATNUMP (c = Fgethash (make_number (keysym),
6378 Vx_keysym_table,
6379 Qnil))))
6381 inev.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6382 ? ASCII_KEYSTROKE_EVENT
6383 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6384 inev.code = XFASTINT (c);
6385 goto done_keysym;
6388 /* Random non-modifier sorts of keysyms. */
6389 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6390 || keysym == XK_Delete
6391 #ifdef XK_ISO_Left_Tab
6392 || (keysym >= XK_ISO_Left_Tab
6393 && keysym <= XK_ISO_Enter)
6394 #endif
6395 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6396 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6397 #ifdef HPUX
6398 /* This recognizes the "extended function
6399 keys". It seems there's no cleaner way.
6400 Test IsModifierKey to avoid handling
6401 mode_switch incorrectly. */
6402 || ((unsigned) (keysym) >= XK_Select
6403 && (unsigned)(keysym) < XK_KP_Space)
6404 #endif
6405 #ifdef XK_dead_circumflex
6406 || orig_keysym == XK_dead_circumflex
6407 #endif
6408 #ifdef XK_dead_grave
6409 || orig_keysym == XK_dead_grave
6410 #endif
6411 #ifdef XK_dead_tilde
6412 || orig_keysym == XK_dead_tilde
6413 #endif
6414 #ifdef XK_dead_diaeresis
6415 || orig_keysym == XK_dead_diaeresis
6416 #endif
6417 #ifdef XK_dead_macron
6418 || orig_keysym == XK_dead_macron
6419 #endif
6420 #ifdef XK_dead_degree
6421 || orig_keysym == XK_dead_degree
6422 #endif
6423 #ifdef XK_dead_acute
6424 || orig_keysym == XK_dead_acute
6425 #endif
6426 #ifdef XK_dead_cedilla
6427 || orig_keysym == XK_dead_cedilla
6428 #endif
6429 #ifdef XK_dead_breve
6430 || orig_keysym == XK_dead_breve
6431 #endif
6432 #ifdef XK_dead_ogonek
6433 || orig_keysym == XK_dead_ogonek
6434 #endif
6435 #ifdef XK_dead_caron
6436 || orig_keysym == XK_dead_caron
6437 #endif
6438 #ifdef XK_dead_doubleacute
6439 || orig_keysym == XK_dead_doubleacute
6440 #endif
6441 #ifdef XK_dead_abovedot
6442 || orig_keysym == XK_dead_abovedot
6443 #endif
6444 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6445 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6446 /* Any "vendor-specific" key is ok. */
6447 || (orig_keysym & (1 << 28))
6448 || (keysym != NoSymbol && nbytes == 0))
6449 && ! (IsModifierKey (orig_keysym)
6450 #ifndef HAVE_X11R5
6451 #ifdef XK_Mode_switch
6452 || ((unsigned)(orig_keysym) == XK_Mode_switch)
6453 #endif
6454 #ifdef XK_Num_Lock
6455 || ((unsigned)(orig_keysym) == XK_Num_Lock)
6456 #endif
6457 #endif /* not HAVE_X11R5 */
6458 /* The symbols from XK_ISO_Lock
6459 to XK_ISO_Last_Group_Lock
6460 don't have real modifiers but
6461 should be treated similarly to
6462 Mode_switch by Emacs. */
6463 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6464 || ((unsigned)(orig_keysym)
6465 >= XK_ISO_Lock
6466 && (unsigned)(orig_keysym)
6467 <= XK_ISO_Last_Group_Lock)
6468 #endif
6471 STORE_KEYSYM_FOR_DEBUG (keysym);
6472 /* make_lispy_event will convert this to a symbolic
6473 key. */
6474 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
6475 inev.code = keysym;
6476 goto done_keysym;
6479 { /* Raw bytes, not keysym. */
6480 register int i;
6481 register int c;
6482 int nchars, len;
6484 /* The input should be decoded with `coding_system'
6485 which depends on which X*LookupString function
6486 we used just above and the locale. */
6487 setup_coding_system (coding_system, &coding);
6488 coding.src_multibyte = 0;
6489 coding.dst_multibyte = 1;
6490 /* The input is converted to events, thus we can't
6491 handle composition. Anyway, there's no XIM that
6492 gives us composition information. */
6493 coding.composing = COMPOSITION_DISABLED;
6495 for (i = 0; i < nbytes; i++)
6497 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6501 /* Decode the input data. */
6502 int require;
6503 unsigned char *p;
6505 require = decoding_buffer_size (&coding, nbytes);
6506 p = (unsigned char *) alloca (require);
6507 coding.mode |= CODING_MODE_LAST_BLOCK;
6508 /* We explicitly disable composition handling because
6509 key data should not contain any composition sequence. */
6510 coding.composing = COMPOSITION_DISABLED;
6511 decode_coding (&coding, copy_bufptr, p, nbytes, require);
6512 nbytes = coding.produced;
6513 nchars = coding.produced_char;
6514 copy_bufptr = p;
6517 /* Convert the input data to a sequence of
6518 character events. */
6519 for (i = 0; i < nbytes; i += len)
6521 if (nchars == nbytes)
6522 c = copy_bufptr[i], len = 1;
6523 else
6524 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6525 nbytes - i, len);
6526 inev.kind = (SINGLE_BYTE_CHAR_P (c)
6527 ? ASCII_KEYSTROKE_EVENT
6528 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6529 inev.code = c;
6530 kbd_buffer_store_event_hold (&inev, hold_quit);
6533 /* Previous code updated count by nchars rather than nbytes,
6534 but that seems bogus to me. ++kfs */
6535 count += nbytes;
6537 inev.kind = NO_EVENT; /* Already stored above. */
6539 if (keysym == NoSymbol)
6540 break;
6543 done_keysym:
6544 #ifdef HAVE_X_I18N
6545 /* Don't dispatch this event since XtDispatchEvent calls
6546 XFilterEvent, and two calls in a row may freeze the
6547 client. */
6548 break;
6549 #else
6550 goto OTHER;
6551 #endif
6553 case KeyRelease:
6554 #ifdef HAVE_X_I18N
6555 /* Don't dispatch this event since XtDispatchEvent calls
6556 XFilterEvent, and two calls in a row may freeze the
6557 client. */
6558 break;
6559 #else
6560 goto OTHER;
6561 #endif
6563 case EnterNotify:
6564 x_detect_focus_change (dpyinfo, &event, &inev);
6566 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6568 if (f && x_mouse_click_focus_ignore_position)
6569 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6571 #if 0
6572 if (event.xcrossing.focus)
6574 /* Avoid nasty pop/raise loops. */
6575 if (f && (!(f->auto_raise)
6576 || !(f->auto_lower)
6577 || (event.xcrossing.time - enter_timestamp) > 500))
6579 x_new_focus_frame (dpyinfo, f);
6580 enter_timestamp = event.xcrossing.time;
6583 else if (f == dpyinfo->x_focus_frame)
6584 x_new_focus_frame (dpyinfo, 0);
6585 #endif
6587 /* EnterNotify counts as mouse movement,
6588 so update things that depend on mouse position. */
6589 if (f && !f->output_data.x->hourglass_p)
6590 note_mouse_movement (f, &event.xmotion);
6591 goto OTHER;
6593 case FocusIn:
6594 x_detect_focus_change (dpyinfo, &event, &inev);
6595 goto OTHER;
6597 case LeaveNotify:
6598 x_detect_focus_change (dpyinfo, &event, &inev);
6600 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6601 if (f)
6603 if (f == dpyinfo->mouse_face_mouse_frame)
6605 /* If we move outside the frame, then we're
6606 certainly no longer on any text in the frame. */
6607 clear_mouse_face (dpyinfo);
6608 dpyinfo->mouse_face_mouse_frame = 0;
6611 /* Generate a nil HELP_EVENT to cancel a help-echo.
6612 Do it only if there's something to cancel.
6613 Otherwise, the startup message is cleared when
6614 the mouse leaves the frame. */
6615 if (any_help_event_p)
6616 do_help = -1;
6618 goto OTHER;
6620 case FocusOut:
6621 x_detect_focus_change (dpyinfo, &event, &inev);
6622 goto OTHER;
6624 case MotionNotify:
6626 previous_help_echo_string = help_echo_string;
6627 help_echo_string = help_echo_object = help_echo_window = Qnil;
6628 help_echo_pos = -1;
6630 if (dpyinfo->grabbed && last_mouse_frame
6631 && FRAME_LIVE_P (last_mouse_frame))
6632 f = last_mouse_frame;
6633 else
6634 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6636 if (dpyinfo->mouse_face_hidden)
6638 dpyinfo->mouse_face_hidden = 0;
6639 clear_mouse_face (dpyinfo);
6642 if (f)
6645 /* Generate SELECT_WINDOW_EVENTs when needed. */
6646 if (mouse_autoselect_window)
6648 Lisp_Object window;
6650 window = window_from_coordinates (f,
6651 event.xmotion.x, event.xmotion.y,
6652 0, 0, 0, 0);
6654 /* Window will be selected only when it is not selected now and
6655 last mouse movement event was not in it. Minibuffer window
6656 will be selected iff it is active. */
6657 if (WINDOWP (window)
6658 && !EQ (window, last_window)
6659 && !EQ (window, selected_window))
6661 inev.kind = SELECT_WINDOW_EVENT;
6662 inev.frame_or_window = window;
6665 last_window=window;
6667 note_mouse_movement (f, &event.xmotion);
6669 else
6671 #ifndef USE_TOOLKIT_SCROLL_BARS
6672 struct scroll_bar *bar
6673 = x_window_to_scroll_bar (event.xmotion.display,
6674 event.xmotion.window);
6676 if (bar)
6677 x_scroll_bar_note_movement (bar, &event);
6678 #endif /* USE_TOOLKIT_SCROLL_BARS */
6680 /* If we move outside the frame, then we're
6681 certainly no longer on any text in the frame. */
6682 clear_mouse_face (dpyinfo);
6685 /* If the contents of the global variable help_echo_string
6686 has changed, generate a HELP_EVENT. */
6687 if (!NILP (help_echo_string)
6688 || !NILP (previous_help_echo_string))
6689 do_help = 1;
6690 goto OTHER;
6693 case ConfigureNotify:
6694 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6695 if (f)
6697 #ifndef USE_X_TOOLKIT
6698 #ifdef USE_GTK
6699 xg_resize_widgets (f, event.xconfigure.width,
6700 event.xconfigure.height);
6701 #else /* not USE_GTK */
6702 /* If there is a pending resize for fullscreen, don't
6703 do this one, the right one will come later.
6704 The toolkit version doesn't seem to need this, but we
6705 need to reset it below. */
6706 int dont_resize
6707 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6708 && f->new_text_cols != 0);
6709 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6710 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6712 if (dont_resize)
6713 goto OTHER;
6715 /* In the toolkit version, change_frame_size
6716 is called by the code that handles resizing
6717 of the EmacsFrame widget. */
6719 /* Even if the number of character rows and columns has
6720 not changed, the font size may have changed, so we need
6721 to check the pixel dimensions as well. */
6722 if (columns != FRAME_COLS (f)
6723 || rows != FRAME_LINES (f)
6724 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6725 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6727 change_frame_size (f, rows, columns, 0, 1, 0);
6728 SET_FRAME_GARBAGED (f);
6729 cancel_mouse_face (f);
6731 #endif /* not USE_GTK */
6732 #endif
6734 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6735 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6737 #ifdef USE_GTK
6738 /* GTK creates windows but doesn't map them.
6739 Only get real positions and check fullscreen when mapped. */
6740 if (FRAME_GTK_OUTER_WIDGET (f)
6741 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6742 #endif
6744 /* What we have now is the position of Emacs's own window.
6745 Convert that to the position of the window manager window. */
6746 x_real_positions (f, &f->left_pos, &f->top_pos);
6748 x_check_expected_move (f);
6749 if (f->want_fullscreen & FULLSCREEN_WAIT)
6750 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6753 #ifdef HAVE_X_I18N
6754 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6755 xic_set_statusarea (f);
6756 #endif
6758 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6760 /* Since the WM decorations come below top_pos now,
6761 we must put them below top_pos in the future. */
6762 f->win_gravity = NorthWestGravity;
6763 x_wm_set_size_hint (f, (long) 0, 0);
6766 goto OTHER;
6768 case ButtonRelease:
6769 case ButtonPress:
6771 /* If we decide we want to generate an event to be seen
6772 by the rest of Emacs, we put it here. */
6773 int tool_bar_p = 0;
6775 bzero (&compose_status, sizeof (compose_status));
6777 if (dpyinfo->grabbed
6778 && last_mouse_frame
6779 && FRAME_LIVE_P (last_mouse_frame))
6780 f = last_mouse_frame;
6781 else
6782 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6784 if (f)
6786 /* Is this in the tool-bar? */
6787 if (WINDOWP (f->tool_bar_window)
6788 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6790 Lisp_Object window;
6791 int x = event.xbutton.x;
6792 int y = event.xbutton.y;
6794 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6795 if (EQ (window, f->tool_bar_window))
6797 if (event.xbutton.type == ButtonPress)
6798 handle_tool_bar_click (f, x, y, 1, 0);
6799 else
6800 handle_tool_bar_click (f, x, y, 0,
6801 x_x_to_emacs_modifiers (dpyinfo,
6802 event.xbutton.state));
6803 tool_bar_p = 1;
6807 if (!tool_bar_p)
6808 if (!dpyinfo->x_focus_frame
6809 || f == dpyinfo->x_focus_frame)
6811 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6812 if (! popup_activated ())
6813 #endif
6815 if (ignore_next_mouse_click_timeout)
6817 if (event.type == ButtonPress
6818 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6820 ignore_next_mouse_click_timeout = 0;
6821 construct_mouse_click (&inev, &event, f);
6823 if (event.type == ButtonRelease)
6824 ignore_next_mouse_click_timeout = 0;
6826 else
6827 construct_mouse_click (&inev, &event, f);
6831 else
6833 struct scroll_bar *bar
6834 = x_window_to_scroll_bar (event.xbutton.display,
6835 event.xbutton.window);
6837 #ifdef USE_TOOLKIT_SCROLL_BARS
6838 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6839 scroll bars. */
6840 if (bar && event.xbutton.state & ControlMask)
6842 x_scroll_bar_handle_click (bar, &event, &inev);
6843 *finish = X_EVENT_DROP;
6845 #else /* not USE_TOOLKIT_SCROLL_BARS */
6846 if (bar)
6847 x_scroll_bar_handle_click (bar, &event, &inev);
6848 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6851 if (event.type == ButtonPress)
6853 dpyinfo->grabbed |= (1 << event.xbutton.button);
6854 last_mouse_frame = f;
6855 /* Ignore any mouse motion that happened
6856 before this event; any subsequent mouse-movement
6857 Emacs events should reflect only motion after
6858 the ButtonPress. */
6859 if (f != 0)
6860 f->mouse_moved = 0;
6862 if (!tool_bar_p)
6863 last_tool_bar_item = -1;
6865 else
6866 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6868 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6869 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6870 /* For a down-event in the menu bar,
6871 don't pass it to Xt right now.
6872 Instead, save it away
6873 and we will pass it to Xt from kbd_buffer_get_event.
6874 That way, we can run some Lisp code first. */
6875 if (
6876 #ifdef USE_GTK
6877 ! popup_activated ()
6879 #endif
6880 f && event.type == ButtonPress
6881 /* Verify the event is really within the menu bar
6882 and not just sent to it due to grabbing. */
6883 && event.xbutton.x >= 0
6884 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6885 && event.xbutton.y >= 0
6886 && event.xbutton.y < f->output_data.x->menubar_height
6887 && event.xbutton.same_screen)
6889 SET_SAVED_BUTTON_EVENT;
6890 XSETFRAME (last_mouse_press_frame, f);
6891 #ifdef USE_GTK
6892 *finish = X_EVENT_DROP;
6893 #endif
6895 else if (event.type == ButtonPress)
6897 last_mouse_press_frame = Qnil;
6898 goto OTHER;
6901 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6902 but I am trying to be cautious. */
6903 else if (event.type == ButtonRelease)
6905 if (!NILP (last_mouse_press_frame))
6907 f = XFRAME (last_mouse_press_frame);
6908 if (f->output_data.x)
6909 SET_SAVED_BUTTON_EVENT;
6911 else
6912 goto OTHER;
6914 #endif /* USE_MOTIF */
6915 else
6916 goto OTHER;
6917 #endif /* USE_X_TOOLKIT || USE_GTK */
6919 break;
6921 case CirculateNotify:
6922 goto OTHER;
6924 case CirculateRequest:
6925 goto OTHER;
6927 case VisibilityNotify:
6928 goto OTHER;
6930 case MappingNotify:
6931 /* Someone has changed the keyboard mapping - update the
6932 local cache. */
6933 switch (event.xmapping.request)
6935 case MappingModifier:
6936 x_find_modifier_meanings (dpyinfo);
6937 /* This is meant to fall through. */
6938 case MappingKeyboard:
6939 XRefreshKeyboardMapping (&event.xmapping);
6941 goto OTHER;
6943 default:
6944 OTHER:
6945 #ifdef USE_X_TOOLKIT
6946 BLOCK_INPUT;
6947 if (*finish != X_EVENT_DROP)
6948 XtDispatchEvent (&event);
6949 UNBLOCK_INPUT;
6950 #endif /* USE_X_TOOLKIT */
6951 break;
6954 done:
6955 if (inev.kind != NO_EVENT)
6957 kbd_buffer_store_event_hold (&inev, hold_quit);
6958 count++;
6961 if (do_help
6962 && !(hold_quit && hold_quit->kind != NO_EVENT))
6964 Lisp_Object frame;
6966 if (f)
6967 XSETFRAME (frame, f);
6968 else
6969 frame = Qnil;
6971 if (do_help > 0)
6973 any_help_event_p = 1;
6974 gen_help_event (help_echo_string, frame, help_echo_window,
6975 help_echo_object, help_echo_pos);
6977 else
6979 help_echo_string = Qnil;
6980 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
6982 count++;
6985 *eventp = event;
6986 return count;
6990 /* Handles the XEvent EVENT on display DISPLAY.
6991 This is used for event loops outside the normal event handling,
6992 i.e. looping while a popup menu or a dialog is posted.
6994 Returns the value handle_one_xevent sets in the finish argument. */
6996 x_dispatch_event (event, display)
6997 XEvent *event;
6998 Display *display;
7000 struct x_display_info *dpyinfo;
7001 int finish = X_EVENT_NORMAL;
7003 dpyinfo = x_display_info_for_display (display);
7005 if (dpyinfo)
7006 handle_one_xevent (dpyinfo, event, &finish, 0);
7008 return finish;
7012 /* Read events coming from the X server.
7013 This routine is called by the SIGIO handler.
7014 We return as soon as there are no more events to be read.
7016 We return the number of characters stored into the buffer,
7017 thus pretending to be `read'.
7019 EXPECTED is nonzero if the caller knows input is available. */
7021 static int
7022 XTread_socket (sd, expected, hold_quit)
7023 register int sd;
7024 int expected;
7025 struct input_event *hold_quit;
7027 int count = 0;
7028 XEvent event;
7029 int event_found = 0;
7030 struct x_display_info *dpyinfo;
7032 if (interrupt_input_blocked)
7034 interrupt_input_pending = 1;
7035 return -1;
7038 interrupt_input_pending = 0;
7039 BLOCK_INPUT;
7041 /* So people can tell when we have read the available input. */
7042 input_signal_count++;
7044 ++handling_signal;
7046 /* Find the display we are supposed to read input for.
7047 It's the one communicating on descriptor SD. */
7048 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7050 #if 0 /* This ought to be unnecessary; let's verify it. */
7051 #ifdef FIOSNBIO
7052 /* If available, Xlib uses FIOSNBIO to make the socket
7053 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7054 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7055 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7056 fcntl (dpyinfo->connection, F_SETFL, 0);
7057 #endif /* ! defined (FIOSNBIO) */
7058 #endif
7060 #if 0 /* This code can't be made to work, with multiple displays,
7061 and appears not to be used on any system any more.
7062 Also keyboard.c doesn't turn O_NDELAY on and off
7063 for X connections. */
7064 #ifndef SIGIO
7065 #ifndef HAVE_SELECT
7066 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7068 extern int read_alarm_should_throw;
7069 read_alarm_should_throw = 1;
7070 XPeekEvent (dpyinfo->display, &event);
7071 read_alarm_should_throw = 0;
7073 #endif /* HAVE_SELECT */
7074 #endif /* SIGIO */
7075 #endif
7077 /* For debugging, this gives a way to fake an I/O error. */
7078 if (dpyinfo == XTread_socket_fake_io_error)
7080 XTread_socket_fake_io_error = 0;
7081 x_io_error_quitter (dpyinfo->display);
7084 #ifdef HAVE_X_SM
7086 struct input_event inev;
7087 BLOCK_INPUT;
7088 /* We don't need to EVENT_INIT (inev) here, as
7089 x_session_check_input copies an entire input_event. */
7090 if (x_session_check_input (&inev))
7092 kbd_buffer_store_event_hold (&inev, hold_quit);
7093 count++;
7095 UNBLOCK_INPUT;
7097 #endif
7099 #ifndef USE_GTK
7100 while (XPending (dpyinfo->display))
7102 int finish;
7104 XNextEvent (dpyinfo->display, &event);
7106 #ifdef HAVE_X_I18N
7107 /* Filter events for the current X input method. */
7108 if (x_filter_event (dpyinfo, &event))
7109 break;
7110 #endif
7111 event_found = 1;
7113 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
7115 if (finish == X_EVENT_GOTO_OUT)
7116 goto out;
7118 #endif /* not USE_GTK */
7121 #ifdef USE_GTK
7123 /* For GTK we must use the GTK event loop. But XEvents gets passed
7124 to our filter function above, and then to the big event switch.
7125 We use a bunch of globals to communicate with our filter function,
7126 that is kind of ugly, but it works.
7128 There is no way to do one display at the time, GTK just does events
7129 from all displays. */
7131 while (gtk_events_pending ())
7133 current_count = count;
7134 current_hold_quit = hold_quit;
7136 gtk_main_iteration ();
7138 count = current_count;
7139 current_count = -1;
7140 current_hold_quit = 0;
7142 if (current_finish == X_EVENT_GOTO_OUT)
7143 break;
7145 #endif /* USE_GTK */
7147 out:;
7149 /* On some systems, an X bug causes Emacs to get no more events
7150 when the window is destroyed. Detect that. (1994.) */
7151 if (! event_found)
7153 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7154 One XNOOP in 100 loops will make Emacs terminate.
7155 B. Bretthauer, 1994 */
7156 x_noop_count++;
7157 if (x_noop_count >= 100)
7159 x_noop_count=0;
7161 if (next_noop_dpyinfo == 0)
7162 next_noop_dpyinfo = x_display_list;
7164 XNoOp (next_noop_dpyinfo->display);
7166 /* Each time we get here, cycle through the displays now open. */
7167 next_noop_dpyinfo = next_noop_dpyinfo->next;
7171 /* If the focus was just given to an auto-raising frame,
7172 raise it now. */
7173 /* ??? This ought to be able to handle more than one such frame. */
7174 if (pending_autoraise_frame)
7176 x_raise_frame (pending_autoraise_frame);
7177 pending_autoraise_frame = 0;
7180 --handling_signal;
7181 UNBLOCK_INPUT;
7183 return count;
7189 /***********************************************************************
7190 Text Cursor
7191 ***********************************************************************/
7193 /* Set clipping for output in glyph row ROW. W is the window in which
7194 we operate. GC is the graphics context to set clipping in.
7196 ROW may be a text row or, e.g., a mode line. Text rows must be
7197 clipped to the interior of the window dedicated to text display,
7198 mode lines must be clipped to the whole window. */
7200 static void
7201 x_clip_to_row (w, row, gc)
7202 struct window *w;
7203 struct glyph_row *row;
7204 GC gc;
7206 struct frame *f = XFRAME (WINDOW_FRAME (w));
7207 XRectangle clip_rect;
7208 int window_y, window_width;
7210 window_box (w, -1, 0, &window_y, &window_width, 0);
7212 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
7213 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
7214 clip_rect.y = max (clip_rect.y, window_y);
7215 clip_rect.width = window_width;
7216 clip_rect.height = row->visible_height;
7218 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7222 /* Draw a hollow box cursor on window W in glyph row ROW. */
7224 static void
7225 x_draw_hollow_cursor (w, row)
7226 struct window *w;
7227 struct glyph_row *row;
7229 struct frame *f = XFRAME (WINDOW_FRAME (w));
7230 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7231 Display *dpy = FRAME_X_DISPLAY (f);
7232 int x, y, wd, h;
7233 XGCValues xgcv;
7234 struct glyph *cursor_glyph;
7235 GC gc;
7237 /* Get the glyph the cursor is on. If we can't tell because
7238 the current matrix is invalid or such, give up. */
7239 cursor_glyph = get_phys_cursor_glyph (w);
7240 if (cursor_glyph == NULL)
7241 return;
7243 /* Compute the width of the rectangle to draw. If on a stretch
7244 glyph, and `x-stretch-block-cursor' is nil, don't draw a
7245 rectangle as wide as the glyph, but use a canonical character
7246 width instead. */
7247 wd = cursor_glyph->pixel_width - 1;
7248 if (cursor_glyph->type == STRETCH_GLYPH
7249 && !x_stretch_cursor_p)
7250 wd = min (FRAME_COLUMN_WIDTH (f), wd);
7251 w->phys_cursor_width = wd;
7253 /* Compute frame-relative coordinates from window-relative
7254 coordinates. */
7255 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7256 y = WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y);
7258 /* Compute the proper height and ascent of the rectangle, based
7259 on the actual glyph. Using the full height of the row looks
7260 bad when there are tall images on that row. */
7261 h = max (min (FRAME_LINE_HEIGHT (f), row->height),
7262 cursor_glyph->ascent + cursor_glyph->descent);
7263 if (h < row->height)
7264 y += row->ascent /* - w->phys_cursor_ascent */ + cursor_glyph->descent - h;
7265 h--;
7267 /* The foreground of cursor_gc is typically the same as the normal
7268 background color, which can cause the cursor box to be invisible. */
7269 xgcv.foreground = f->output_data.x->cursor_pixel;
7270 if (dpyinfo->scratch_cursor_gc)
7271 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7272 else
7273 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7274 GCForeground, &xgcv);
7275 gc = dpyinfo->scratch_cursor_gc;
7277 /* Set clipping, draw the rectangle, and reset clipping again. */
7278 x_clip_to_row (w, row, gc);
7279 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
7280 XSetClipMask (dpy, gc, None);
7284 /* Draw a bar cursor on window W in glyph row ROW.
7286 Implementation note: One would like to draw a bar cursor with an
7287 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7288 Unfortunately, I didn't find a font yet that has this property set.
7289 --gerd. */
7291 static void
7292 x_draw_bar_cursor (w, row, width, kind)
7293 struct window *w;
7294 struct glyph_row *row;
7295 int width;
7296 enum text_cursor_kinds kind;
7298 struct frame *f = XFRAME (w->frame);
7299 struct glyph *cursor_glyph;
7301 /* If cursor is out of bounds, don't draw garbage. This can happen
7302 in mini-buffer windows when switching between echo area glyphs
7303 and mini-buffer. */
7304 cursor_glyph = get_phys_cursor_glyph (w);
7305 if (cursor_glyph == NULL)
7306 return;
7308 /* If on an image, draw like a normal cursor. That's usually better
7309 visible than drawing a bar, esp. if the image is large so that
7310 the bar might not be in the window. */
7311 if (cursor_glyph->type == IMAGE_GLYPH)
7313 struct glyph_row *row;
7314 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7315 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7317 else
7319 Display *dpy = FRAME_X_DISPLAY (f);
7320 Window window = FRAME_X_WINDOW (f);
7321 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7322 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7323 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7324 XGCValues xgcv;
7326 /* If the glyph's background equals the color we normally draw
7327 the bar cursor in, the bar cursor in its normal color is
7328 invisible. Use the glyph's foreground color instead in this
7329 case, on the assumption that the glyph's colors are chosen so
7330 that the glyph is legible. */
7331 if (face->background == f->output_data.x->cursor_pixel)
7332 xgcv.background = xgcv.foreground = face->foreground;
7333 else
7334 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7335 xgcv.graphics_exposures = 0;
7337 if (gc)
7338 XChangeGC (dpy, gc, mask, &xgcv);
7339 else
7341 gc = XCreateGC (dpy, window, mask, &xgcv);
7342 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7345 if (width < 0)
7346 width = FRAME_CURSOR_WIDTH (f);
7347 width = min (cursor_glyph->pixel_width, width);
7349 w->phys_cursor_width = width;
7350 x_clip_to_row (w, row, gc);
7352 if (kind == BAR_CURSOR)
7353 XFillRectangle (dpy, window, gc,
7354 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7355 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7356 width, row->height);
7357 else
7358 XFillRectangle (dpy, window, gc,
7359 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7360 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7361 row->height - width),
7362 cursor_glyph->pixel_width,
7363 width);
7365 XSetClipMask (dpy, gc, None);
7370 /* RIF: Define cursor CURSOR on frame F. */
7372 static void
7373 x_define_frame_cursor (f, cursor)
7374 struct frame *f;
7375 Cursor cursor;
7377 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7381 /* RIF: Clear area on frame F. */
7383 static void
7384 x_clear_frame_area (f, x, y, width, height)
7385 struct frame *f;
7386 int x, y, width, height;
7388 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7389 x, y, width, height, False);
7393 /* RIF: Draw cursor on window W. */
7395 static void
7396 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7397 struct window *w;
7398 struct glyph_row *glyph_row;
7399 int x, y;
7400 int cursor_type, cursor_width;
7401 int on_p, active_p;
7403 struct frame *f = XFRAME (WINDOW_FRAME (w));
7405 if (on_p)
7407 w->phys_cursor_type = cursor_type;
7408 w->phys_cursor_on_p = 1;
7410 if (glyph_row->exact_window_width_line_p
7411 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7413 glyph_row->cursor_in_fringe_p = 1;
7414 draw_fringe_bitmap (w, glyph_row, 0);
7416 else
7417 switch (cursor_type)
7419 case HOLLOW_BOX_CURSOR:
7420 x_draw_hollow_cursor (w, glyph_row);
7421 break;
7423 case FILLED_BOX_CURSOR:
7424 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7425 break;
7427 case BAR_CURSOR:
7428 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7429 break;
7431 case HBAR_CURSOR:
7432 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7433 break;
7435 case NO_CURSOR:
7436 w->phys_cursor_width = 0;
7437 break;
7439 default:
7440 abort ();
7443 #ifdef HAVE_X_I18N
7444 if (w == XWINDOW (f->selected_window))
7445 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7446 xic_set_preeditarea (w, x, y);
7447 #endif
7450 #ifndef XFlush
7451 if (updating_frame != f)
7452 XFlush (FRAME_X_DISPLAY (f));
7453 #endif
7457 /* Icons. */
7459 /* Make the x-window of frame F use the gnu icon bitmap. */
7462 x_bitmap_icon (f, file)
7463 struct frame *f;
7464 Lisp_Object file;
7466 int bitmap_id;
7468 if (FRAME_X_WINDOW (f) == 0)
7469 return 1;
7471 /* Free up our existing icon bitmap and mask if any. */
7472 if (f->output_data.x->icon_bitmap > 0)
7473 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7474 f->output_data.x->icon_bitmap = 0;
7476 if (STRINGP (file))
7478 #ifdef USE_GTK
7479 /* Use gtk_window_set_icon_from_file () if available,
7480 It's not restricted to bitmaps */
7481 if (xg_set_icon (f, file))
7482 return 0;
7483 #endif /* USE_GTK */
7484 bitmap_id = x_create_bitmap_from_file (f, file);
7485 x_create_bitmap_mask (f, bitmap_id);
7487 else
7489 /* Create the GNU bitmap and mask if necessary. */
7490 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7492 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
7493 = x_create_bitmap_from_data (f, gnu_bits,
7494 gnu_width, gnu_height);
7495 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7498 /* The first time we create the GNU bitmap and mask,
7499 this increments the ref-count one extra time.
7500 As a result, the GNU bitmap and mask are never freed.
7501 That way, we don't have to worry about allocating it again. */
7502 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7504 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7507 x_wm_set_icon_pixmap (f, bitmap_id);
7508 f->output_data.x->icon_bitmap = bitmap_id;
7510 return 0;
7514 /* Make the x-window of frame F use a rectangle with text.
7515 Use ICON_NAME as the text. */
7518 x_text_icon (f, icon_name)
7519 struct frame *f;
7520 char *icon_name;
7522 if (FRAME_X_WINDOW (f) == 0)
7523 return 1;
7525 #ifdef HAVE_X11R4
7527 XTextProperty text;
7528 text.value = (unsigned char *) icon_name;
7529 text.encoding = XA_STRING;
7530 text.format = 8;
7531 text.nitems = strlen (icon_name);
7532 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7534 #else /* not HAVE_X11R4 */
7535 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
7536 #endif /* not HAVE_X11R4 */
7538 if (f->output_data.x->icon_bitmap > 0)
7539 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7540 f->output_data.x->icon_bitmap = 0;
7541 x_wm_set_icon_pixmap (f, 0);
7543 return 0;
7546 #define X_ERROR_MESSAGE_SIZE 200
7548 /* If non-nil, this should be a string.
7549 It means catch X errors and store the error message in this string. */
7551 static Lisp_Object x_error_message_string;
7553 /* An X error handler which stores the error message in
7554 x_error_message_string. This is called from x_error_handler if
7555 x_catch_errors is in effect. */
7557 static void
7558 x_error_catcher (display, error)
7559 Display *display;
7560 XErrorEvent *error;
7562 XGetErrorText (display, error->error_code,
7563 SDATA (x_error_message_string),
7564 X_ERROR_MESSAGE_SIZE);
7567 /* Begin trapping X errors for display DPY. Actually we trap X errors
7568 for all displays, but DPY should be the display you are actually
7569 operating on.
7571 After calling this function, X protocol errors no longer cause
7572 Emacs to exit; instead, they are recorded in the string
7573 stored in x_error_message_string.
7575 Calling x_check_errors signals an Emacs error if an X error has
7576 occurred since the last call to x_catch_errors or x_check_errors.
7578 Calling x_uncatch_errors resumes the normal error handling. */
7580 void x_check_errors ();
7581 static Lisp_Object x_catch_errors_unwind ();
7584 x_catch_errors (dpy)
7585 Display *dpy;
7587 int count = SPECPDL_INDEX ();
7589 /* Make sure any errors from previous requests have been dealt with. */
7590 XSync (dpy, False);
7592 record_unwind_protect (x_catch_errors_unwind,
7593 Fcons (make_save_value (dpy, 0),
7594 x_error_message_string));
7596 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
7597 SSET (x_error_message_string, 0, 0);
7599 return count;
7602 /* Unbind the binding that we made to check for X errors. */
7604 static Lisp_Object
7605 x_catch_errors_unwind (old_val)
7606 Lisp_Object old_val;
7608 Lisp_Object first;
7610 first = XCAR (old_val);
7612 XSync (XSAVE_VALUE (first)->pointer, False);
7614 x_error_message_string = XCDR (old_val);
7615 return Qnil;
7618 /* If any X protocol errors have arrived since the last call to
7619 x_catch_errors or x_check_errors, signal an Emacs error using
7620 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7622 void
7623 x_check_errors (dpy, format)
7624 Display *dpy;
7625 char *format;
7627 /* Make sure to catch any errors incurred so far. */
7628 XSync (dpy, False);
7630 if (SREF (x_error_message_string, 0))
7631 error (format, SDATA (x_error_message_string));
7634 /* Nonzero if we had any X protocol errors
7635 since we did x_catch_errors on DPY. */
7638 x_had_errors_p (dpy)
7639 Display *dpy;
7641 /* Make sure to catch any errors incurred so far. */
7642 XSync (dpy, False);
7644 return SREF (x_error_message_string, 0) != 0;
7647 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7649 void
7650 x_clear_errors (dpy)
7651 Display *dpy;
7653 SSET (x_error_message_string, 0, 0);
7656 /* Stop catching X protocol errors and let them make Emacs die.
7657 DPY should be the display that was passed to x_catch_errors.
7658 COUNT should be the value that was returned by
7659 the corresponding call to x_catch_errors. */
7661 void
7662 x_uncatch_errors (dpy, count)
7663 Display *dpy;
7664 int count;
7666 unbind_to (count, Qnil);
7669 #if 0
7670 static unsigned int x_wire_count;
7671 x_trace_wire ()
7673 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7675 #endif /* ! 0 */
7678 /* Handle SIGPIPE, which can happen when the connection to a server
7679 simply goes away. SIGPIPE is handled by x_connection_signal.
7680 Don't need to do anything, because the write which caused the
7681 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7682 which will do the appropriate cleanup for us. */
7684 static SIGTYPE
7685 x_connection_signal (signalnum) /* If we don't have an argument, */
7686 int signalnum; /* some compilers complain in signal calls. */
7688 #ifdef USG
7689 /* USG systems forget handlers when they are used;
7690 must reestablish each time */
7691 signal (signalnum, x_connection_signal);
7692 #endif /* USG */
7696 /************************************************************************
7697 Handling X errors
7698 ************************************************************************/
7700 /* Error message passed to x_connection_closed. */
7702 static char *error_msg;
7704 /* Function installed as fatal_error_signal_hook in
7705 x_connection_closed. Print the X error message, and exit normally,
7706 instead of dumping core when XtCloseDisplay fails. */
7708 static void
7709 x_fatal_error_signal ()
7711 fprintf (stderr, "%s\n", error_msg);
7712 exit (70);
7715 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7716 the text of an error message that lead to the connection loss. */
7718 static SIGTYPE
7719 x_connection_closed (dpy, error_message)
7720 Display *dpy;
7721 char *error_message;
7723 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7724 Lisp_Object frame, tail;
7725 int count;
7727 error_msg = (char *) alloca (strlen (error_message) + 1);
7728 strcpy (error_msg, error_message);
7729 handling_signal = 0;
7731 /* Prevent being called recursively because of an error condition
7732 below. Otherwise, we might end up with printing ``can't find per
7733 display information'' in the recursive call instead of printing
7734 the original message here. */
7735 count = x_catch_errors (dpy);
7737 /* We have to close the display to inform Xt that it doesn't
7738 exist anymore. If we don't, Xt will continue to wait for
7739 events from the display. As a consequence, a sequence of
7741 M-x make-frame-on-display RET :1 RET
7742 ...kill the new frame, so that we get an IO error...
7743 M-x make-frame-on-display RET :1 RET
7745 will indefinitely wait in Xt for events for display `:1', opened
7746 in the first class to make-frame-on-display.
7748 Closing the display is reported to lead to a bus error on
7749 OpenWindows in certain situations. I suspect that is a bug
7750 in OpenWindows. I don't know how to cicumvent it here. */
7752 #ifdef USE_X_TOOLKIT
7753 /* If DPYINFO is null, this means we didn't open the display
7754 in the first place, so don't try to close it. */
7755 if (dpyinfo)
7757 extern void (*fatal_error_signal_hook) P_ ((void));
7758 fatal_error_signal_hook = x_fatal_error_signal;
7759 XtCloseDisplay (dpy);
7760 fatal_error_signal_hook = NULL;
7762 #endif
7764 #ifdef USE_GTK
7765 if (dpyinfo)
7766 xg_display_close (dpyinfo->display);
7767 #endif
7769 /* Indicate that this display is dead. */
7770 if (dpyinfo)
7771 dpyinfo->display = 0;
7773 /* First delete frames whose mini-buffers are on frames
7774 that are on the dead display. */
7775 FOR_EACH_FRAME (tail, frame)
7777 Lisp_Object minibuf_frame;
7778 minibuf_frame
7779 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7780 if (FRAME_X_P (XFRAME (frame))
7781 && FRAME_X_P (XFRAME (minibuf_frame))
7782 && ! EQ (frame, minibuf_frame)
7783 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7784 Fdelete_frame (frame, Qt);
7787 /* Now delete all remaining frames on the dead display.
7788 We are now sure none of these is used as the mini-buffer
7789 for another frame that we need to delete. */
7790 FOR_EACH_FRAME (tail, frame)
7791 if (FRAME_X_P (XFRAME (frame))
7792 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7794 /* Set this to t so that Fdelete_frame won't get confused
7795 trying to find a replacement. */
7796 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7797 Fdelete_frame (frame, Qt);
7800 if (dpyinfo)
7801 x_delete_display (dpyinfo);
7803 x_uncatch_errors (dpy, count);
7805 if (x_display_list == 0)
7807 fprintf (stderr, "%s\n", error_msg);
7808 shut_down_emacs (0, 0, Qnil);
7809 exit (70);
7812 /* Ordinary stack unwind doesn't deal with these. */
7813 #ifdef SIGIO
7814 sigunblock (sigmask (SIGIO));
7815 #endif
7816 sigunblock (sigmask (SIGALRM));
7817 TOTALLY_UNBLOCK_INPUT;
7819 clear_waiting_for_input ();
7820 error ("%s", error_msg);
7824 /* This is the usual handler for X protocol errors.
7825 It kills all frames on the display that we got the error for.
7826 If that was the only one, it prints an error message and kills Emacs. */
7828 static void
7829 x_error_quitter (display, error)
7830 Display *display;
7831 XErrorEvent *error;
7833 char buf[256], buf1[356];
7835 /* Note that there is no real way portable across R3/R4 to get the
7836 original error handler. */
7838 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7839 sprintf (buf1, "X protocol error: %s on protocol request %d",
7840 buf, error->request_code);
7841 x_connection_closed (display, buf1);
7845 /* This is the first-level handler for X protocol errors.
7846 It calls x_error_quitter or x_error_catcher. */
7848 static int
7849 x_error_handler (display, error)
7850 Display *display;
7851 XErrorEvent *error;
7853 if (! NILP (x_error_message_string))
7854 x_error_catcher (display, error);
7855 else
7856 x_error_quitter (display, error);
7857 return 0;
7860 /* This is the handler for X IO errors, always.
7861 It kills all frames on the display that we lost touch with.
7862 If that was the only one, it prints an error message and kills Emacs. */
7864 static int
7865 x_io_error_quitter (display)
7866 Display *display;
7868 char buf[256];
7870 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7871 x_connection_closed (display, buf);
7872 return 0;
7875 /* Changing the font of the frame. */
7877 /* Give frame F the font named FONTNAME as its default font, and
7878 return the full name of that font. FONTNAME may be a wildcard
7879 pattern; in that case, we choose some font that fits the pattern.
7880 The return value shows which font we chose. */
7882 Lisp_Object
7883 x_new_font (f, fontname)
7884 struct frame *f;
7885 register char *fontname;
7887 struct font_info *fontp
7888 = FS_LOAD_FONT (f, 0, fontname, -1);
7890 if (!fontp)
7891 return Qnil;
7893 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
7894 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
7895 FRAME_FONTSET (f) = -1;
7897 FRAME_COLUMN_WIDTH (f) = FONT_WIDTH (FRAME_FONT (f));
7898 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
7900 compute_fringe_widths (f, 1);
7902 /* Compute the scroll bar width in character columns. */
7903 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
7905 int wid = FRAME_COLUMN_WIDTH (f);
7906 FRAME_CONFIG_SCROLL_BAR_COLS (f)
7907 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
7909 else
7911 int wid = FRAME_COLUMN_WIDTH (f);
7912 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
7915 /* Now make the frame display the given font. */
7916 if (FRAME_X_WINDOW (f) != 0)
7918 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
7919 FRAME_FONT (f)->fid);
7920 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
7921 FRAME_FONT (f)->fid);
7922 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
7923 FRAME_FONT (f)->fid);
7925 /* Don't change the size of a tip frame; there's no point in
7926 doing it because it's done in Fx_show_tip, and it leads to
7927 problems because the tip frame has no widget. */
7928 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
7929 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
7932 return build_string (fontp->full_name);
7935 /* Give frame F the fontset named FONTSETNAME as its default font, and
7936 return the full name of that fontset. FONTSETNAME may be a wildcard
7937 pattern; in that case, we choose some fontset that fits the pattern.
7938 The return value shows which fontset we chose. */
7940 Lisp_Object
7941 x_new_fontset (f, fontsetname)
7942 struct frame *f;
7943 char *fontsetname;
7945 int fontset = fs_query_fontset (build_string (fontsetname), 0);
7946 Lisp_Object result;
7948 if (fontset < 0)
7949 return Qnil;
7951 if (FRAME_FONTSET (f) == fontset)
7952 /* This fontset is already set in frame F. There's nothing more
7953 to do. */
7954 return fontset_name (fontset);
7956 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
7958 if (!STRINGP (result))
7959 /* Can't load ASCII font. */
7960 return Qnil;
7962 /* Since x_new_font doesn't update any fontset information, do it now. */
7963 FRAME_FONTSET (f) = fontset;
7965 #ifdef HAVE_X_I18N
7966 if (FRAME_XIC (f)
7967 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
7968 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
7969 #endif
7971 return build_string (fontsetname);
7975 /***********************************************************************
7976 X Input Methods
7977 ***********************************************************************/
7979 #ifdef HAVE_X_I18N
7981 #ifdef HAVE_X11R6
7983 /* XIM destroy callback function, which is called whenever the
7984 connection to input method XIM dies. CLIENT_DATA contains a
7985 pointer to the x_display_info structure corresponding to XIM. */
7987 static void
7988 xim_destroy_callback (xim, client_data, call_data)
7989 XIM xim;
7990 XPointer client_data;
7991 XPointer call_data;
7993 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
7994 Lisp_Object frame, tail;
7996 BLOCK_INPUT;
7998 /* No need to call XDestroyIC.. */
7999 FOR_EACH_FRAME (tail, frame)
8001 struct frame *f = XFRAME (frame);
8002 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8004 FRAME_XIC (f) = NULL;
8005 if (FRAME_XIC_FONTSET (f))
8007 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
8008 FRAME_XIC_FONTSET (f) = NULL;
8013 /* No need to call XCloseIM. */
8014 dpyinfo->xim = NULL;
8015 XFree (dpyinfo->xim_styles);
8016 UNBLOCK_INPUT;
8019 #endif /* HAVE_X11R6 */
8021 #ifdef HAVE_X11R6
8022 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8023 extern char *XSetIMValues P_ ((XIM, ...));
8024 #endif
8026 /* Open the connection to the XIM server on display DPYINFO.
8027 RESOURCE_NAME is the resource name Emacs uses. */
8029 static void
8030 xim_open_dpy (dpyinfo, resource_name)
8031 struct x_display_info *dpyinfo;
8032 char *resource_name;
8034 XIM xim;
8036 #ifdef HAVE_XIM
8037 if (use_xim)
8039 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8040 EMACS_CLASS);
8041 dpyinfo->xim = xim;
8043 if (xim)
8045 #ifdef HAVE_X11R6
8046 XIMCallback destroy;
8047 #endif
8049 /* Get supported styles and XIM values. */
8050 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8052 #ifdef HAVE_X11R6
8053 destroy.callback = xim_destroy_callback;
8054 destroy.client_data = (XPointer)dpyinfo;
8055 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8056 #endif
8060 else
8061 #endif /* HAVE_XIM */
8062 dpyinfo->xim = NULL;
8066 #ifdef HAVE_X11R6_XIM
8068 struct xim_inst_t
8070 struct x_display_info *dpyinfo;
8071 char *resource_name;
8074 /* XIM instantiate callback function, which is called whenever an XIM
8075 server is available. DISPLAY is the display of the XIM.
8076 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8077 when the callback was registered. */
8079 static void
8080 xim_instantiate_callback (display, client_data, call_data)
8081 Display *display;
8082 XPointer client_data;
8083 XPointer call_data;
8085 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8086 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8088 /* We don't support multiple XIM connections. */
8089 if (dpyinfo->xim)
8090 return;
8092 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8094 /* Create XIC for the existing frames on the same display, as long
8095 as they have no XIC. */
8096 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8098 Lisp_Object tail, frame;
8100 BLOCK_INPUT;
8101 FOR_EACH_FRAME (tail, frame)
8103 struct frame *f = XFRAME (frame);
8105 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8106 if (FRAME_XIC (f) == NULL)
8108 create_frame_xic (f);
8109 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8110 xic_set_statusarea (f);
8111 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8113 struct window *w = XWINDOW (f->selected_window);
8114 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8119 UNBLOCK_INPUT;
8123 #endif /* HAVE_X11R6_XIM */
8126 /* Open a connection to the XIM server on display DPYINFO.
8127 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8128 connection only at the first time. On X11R6, open the connection
8129 in the XIM instantiate callback function. */
8131 static void
8132 xim_initialize (dpyinfo, resource_name)
8133 struct x_display_info *dpyinfo;
8134 char *resource_name;
8136 #ifdef HAVE_XIM
8137 if (use_xim)
8139 #ifdef HAVE_X11R6_XIM
8140 struct xim_inst_t *xim_inst;
8141 int len;
8143 dpyinfo->xim = NULL;
8144 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8145 xim_inst->dpyinfo = dpyinfo;
8146 len = strlen (resource_name);
8147 xim_inst->resource_name = (char *) xmalloc (len + 1);
8148 bcopy (resource_name, xim_inst->resource_name, len + 1);
8149 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8150 resource_name, EMACS_CLASS,
8151 xim_instantiate_callback,
8152 /* This is XPointer in XFree86
8153 but (XPointer *) on Tru64, at
8154 least, hence the configure test. */
8155 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8156 #else /* not HAVE_X11R6_XIM */
8157 dpyinfo->xim = NULL;
8158 xim_open_dpy (dpyinfo, resource_name);
8159 #endif /* not HAVE_X11R6_XIM */
8162 else
8163 #endif /* HAVE_XIM */
8164 dpyinfo->xim = NULL;
8168 /* Close the connection to the XIM server on display DPYINFO. */
8170 static void
8171 xim_close_dpy (dpyinfo)
8172 struct x_display_info *dpyinfo;
8174 #ifdef HAVE_XIM
8175 if (use_xim)
8177 #ifdef HAVE_X11R6_XIM
8178 if (dpyinfo->display)
8179 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8180 NULL, EMACS_CLASS,
8181 xim_instantiate_callback, NULL);
8182 #endif /* not HAVE_X11R6_XIM */
8183 if (dpyinfo->display)
8184 XCloseIM (dpyinfo->xim);
8185 dpyinfo->xim = NULL;
8186 XFree (dpyinfo->xim_styles);
8188 #endif /* HAVE_XIM */
8191 #endif /* not HAVE_X11R6_XIM */
8195 /* Calculate the absolute position in frame F
8196 from its current recorded position values and gravity. */
8198 void
8199 x_calc_absolute_position (f)
8200 struct frame *f;
8202 Window child;
8203 int win_x = 0, win_y = 0;
8204 int flags = f->size_hint_flags;
8206 /* We have nothing to do if the current position
8207 is already for the top-left corner. */
8208 if (! ((flags & XNegative) || (flags & YNegative)))
8209 return;
8211 /* Find the offsets of the outside upper-left corner of
8212 the inner window, with respect to the outer window.
8213 But do this only if we will need the results. */
8214 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8215 /* This is to get *_pixels_outer_diff. */
8216 x_real_positions (f, &win_x, &win_y);
8218 /* Treat negative positions as relative to the leftmost bottommost
8219 position that fits on the screen. */
8220 if (flags & XNegative)
8221 f->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8222 - 2 * FRAME_X_OUTPUT (f)->x_pixels_outer_diff
8223 - FRAME_PIXEL_WIDTH (f)
8224 + f->left_pos);
8227 int height = FRAME_PIXEL_HEIGHT (f);
8229 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8230 /* Something is fishy here. When using Motif, starting Emacs with
8231 `-g -0-0', the frame appears too low by a few pixels.
8233 This seems to be so because initially, while Emacs is starting,
8234 the column widget's height and the frame's pixel height are
8235 different. The column widget's height is the right one. In
8236 later invocations, when Emacs is up, the frame's pixel height
8237 is right, though.
8239 It's not obvious where the initial small difference comes from.
8240 2000-12-01, gerd. */
8242 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8243 #endif
8245 if (flags & YNegative)
8246 f->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
8247 - FRAME_X_OUTPUT (f)->y_pixels_outer_diff
8249 /* Assume the window manager decorations are the same size on
8250 three sides, i.e. left, right and bottom. This is to
8251 compensate for the bottom part. */
8252 - FRAME_X_OUTPUT (f)->x_pixels_outer_diff
8253 - height
8254 + f->top_pos);
8257 /* The left_pos and top_pos
8258 are now relative to the top and left screen edges,
8259 so the flags should correspond. */
8260 f->size_hint_flags &= ~ (XNegative | YNegative);
8263 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8264 to really change the position, and 0 when calling from
8265 x_make_frame_visible (in that case, XOFF and YOFF are the current
8266 position values). It is -1 when calling from x_set_frame_parameters,
8267 which means, do adjust for borders but don't change the gravity. */
8269 void
8270 x_set_offset (f, xoff, yoff, change_gravity)
8271 struct frame *f;
8272 register int xoff, yoff;
8273 int change_gravity;
8275 int modified_top, modified_left;
8277 if (change_gravity > 0)
8279 f->top_pos = yoff;
8280 f->left_pos = xoff;
8281 f->size_hint_flags &= ~ (XNegative | YNegative);
8282 if (xoff < 0)
8283 f->size_hint_flags |= XNegative;
8284 if (yoff < 0)
8285 f->size_hint_flags |= YNegative;
8286 f->win_gravity = NorthWestGravity;
8288 x_calc_absolute_position (f);
8290 BLOCK_INPUT;
8291 x_wm_set_size_hint (f, (long) 0, 0);
8293 modified_left = f->left_pos;
8294 modified_top = f->top_pos;
8296 if (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8298 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8299 than the WM decorations. So we use the calculated offset instead
8300 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8301 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8302 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8305 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8306 modified_left, modified_top);
8308 if (FRAME_VISIBLE_P (f)
8309 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN)
8311 FRAME_X_OUTPUT (f)->check_expected_move = 1;
8312 FRAME_X_OUTPUT (f)->expected_top = f->top_pos;
8313 FRAME_X_OUTPUT (f)->expected_left = f->left_pos;
8316 UNBLOCK_INPUT;
8319 /* Check if we need to resize the frame due to a fullscreen request.
8320 If so needed, resize the frame. */
8321 static void
8322 x_check_fullscreen (f)
8323 struct frame *f;
8325 if (f->want_fullscreen & FULLSCREEN_BOTH)
8327 int width, height, ign;
8329 x_real_positions (f, &f->left_pos, &f->top_pos);
8331 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8333 /* We do not need to move the window, it shall be taken care of
8334 when setting WM manager hints.
8335 If the frame is visible already, the position is checked by
8336 x_check_expected_move. */
8337 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8339 change_frame_size (f, height, width, 0, 1, 0);
8340 SET_FRAME_GARBAGED (f);
8341 cancel_mouse_face (f);
8343 /* Wait for the change of frame size to occur */
8344 f->want_fullscreen |= FULLSCREEN_WAIT;
8349 /* If frame parameters are set after the frame is mapped, we need to move
8350 the window.
8351 Some window managers moves the window to the right position, some
8352 moves the outer window manager window to the specified position.
8353 Here we check that we are in the right spot. If not, make a second
8354 move, assuming we are dealing with the second kind of window manager. */
8355 static void
8356 x_check_expected_move (f)
8357 struct frame *f;
8359 if (FRAME_X_OUTPUT (f)->check_expected_move)
8361 int expect_top = FRAME_X_OUTPUT (f)->expected_top;
8362 int expect_left = FRAME_X_OUTPUT (f)->expected_left;
8364 if (expect_top != f->top_pos || expect_left != f->left_pos)
8366 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8367 FRAME_X_OUTPUT (f)->move_offset_left = expect_left - f->left_pos;
8368 FRAME_X_OUTPUT (f)->move_offset_top = expect_top - f->top_pos;
8370 x_set_offset (f, expect_left, expect_top, 1);
8372 else if (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN)
8373 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8375 /* Just do this once */
8376 FRAME_X_OUTPUT (f)->check_expected_move = 0;
8381 /* Change the size of frame F's X window to COLS/ROWS in the case F
8382 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8383 top-left-corner window gravity for this size change and subsequent
8384 size changes. Otherwise we leave the window gravity unchanged. */
8386 static void
8387 x_set_window_size_1 (f, change_gravity, cols, rows)
8388 struct frame *f;
8389 int change_gravity;
8390 int cols, rows;
8392 int pixelwidth, pixelheight;
8394 check_frame_size (f, &rows, &cols);
8395 f->scroll_bar_actual_width
8396 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8398 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8399 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8400 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8402 compute_fringe_widths (f, 0);
8404 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8405 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8407 f->win_gravity = NorthWestGravity;
8408 x_wm_set_size_hint (f, (long) 0, 0);
8410 XSync (FRAME_X_DISPLAY (f), False);
8411 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8412 pixelwidth, pixelheight);
8414 /* Now, strictly speaking, we can't be sure that this is accurate,
8415 but the window manager will get around to dealing with the size
8416 change request eventually, and we'll hear how it went when the
8417 ConfigureNotify event gets here.
8419 We could just not bother storing any of this information here,
8420 and let the ConfigureNotify event set everything up, but that
8421 might be kind of confusing to the Lisp code, since size changes
8422 wouldn't be reported in the frame parameters until some random
8423 point in the future when the ConfigureNotify event arrives.
8425 We pass 1 for DELAY since we can't run Lisp code inside of
8426 a BLOCK_INPUT. */
8427 change_frame_size (f, rows, cols, 0, 1, 0);
8428 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8429 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8431 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8432 receive in the ConfigureNotify event; if we get what we asked
8433 for, then the event won't cause the screen to become garbaged, so
8434 we have to make sure to do it here. */
8435 SET_FRAME_GARBAGED (f);
8437 XFlush (FRAME_X_DISPLAY (f));
8441 /* Call this to change the size of frame F's x-window.
8442 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8443 for this size change and subsequent size changes.
8444 Otherwise we leave the window gravity unchanged. */
8446 void
8447 x_set_window_size (f, change_gravity, cols, rows)
8448 struct frame *f;
8449 int change_gravity;
8450 int cols, rows;
8452 BLOCK_INPUT;
8454 #ifdef USE_GTK
8455 if (FRAME_GTK_WIDGET (f))
8456 xg_frame_set_char_size (f, cols, rows);
8457 else
8458 x_set_window_size_1 (f, change_gravity, cols, rows);
8459 #elif USE_X_TOOLKIT
8461 if (f->output_data.x->widget != NULL)
8463 /* The x and y position of the widget is clobbered by the
8464 call to XtSetValues within EmacsFrameSetCharSize.
8465 This is a real kludge, but I don't understand Xt so I can't
8466 figure out a correct fix. Can anyone else tell me? -- rms. */
8467 int xpos = f->output_data.x->widget->core.x;
8468 int ypos = f->output_data.x->widget->core.y;
8469 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8470 f->output_data.x->widget->core.x = xpos;
8471 f->output_data.x->widget->core.y = ypos;
8473 else
8474 x_set_window_size_1 (f, change_gravity, cols, rows);
8476 #else /* not USE_X_TOOLKIT */
8478 x_set_window_size_1 (f, change_gravity, cols, rows);
8480 #endif /* not USE_X_TOOLKIT */
8482 /* If cursor was outside the new size, mark it as off. */
8483 mark_window_cursors_off (XWINDOW (f->root_window));
8485 /* Clear out any recollection of where the mouse highlighting was,
8486 since it might be in a place that's outside the new frame size.
8487 Actually checking whether it is outside is a pain in the neck,
8488 so don't try--just let the highlighting be done afresh with new size. */
8489 cancel_mouse_face (f);
8491 UNBLOCK_INPUT;
8494 /* Mouse warping. */
8496 void
8497 x_set_mouse_position (f, x, y)
8498 struct frame *f;
8499 int x, y;
8501 int pix_x, pix_y;
8503 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8504 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8506 if (pix_x < 0) pix_x = 0;
8507 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8509 if (pix_y < 0) pix_y = 0;
8510 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8512 BLOCK_INPUT;
8514 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8515 0, 0, 0, 0, pix_x, pix_y);
8516 UNBLOCK_INPUT;
8519 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8521 void
8522 x_set_mouse_pixel_position (f, pix_x, pix_y)
8523 struct frame *f;
8524 int pix_x, pix_y;
8526 BLOCK_INPUT;
8528 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8529 0, 0, 0, 0, pix_x, pix_y);
8530 UNBLOCK_INPUT;
8533 /* focus shifting, raising and lowering. */
8535 void
8536 x_focus_on_frame (f)
8537 struct frame *f;
8539 #if 0 /* This proves to be unpleasant. */
8540 x_raise_frame (f);
8541 #endif
8542 #if 0
8543 /* I don't think that the ICCCM allows programs to do things like this
8544 without the interaction of the window manager. Whatever you end up
8545 doing with this code, do it to x_unfocus_frame too. */
8546 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8547 RevertToPointerRoot, CurrentTime);
8548 #endif /* ! 0 */
8551 void
8552 x_unfocus_frame (f)
8553 struct frame *f;
8555 #if 0
8556 /* Look at the remarks in x_focus_on_frame. */
8557 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8558 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8559 RevertToPointerRoot, CurrentTime);
8560 #endif /* ! 0 */
8563 /* Raise frame F. */
8565 void
8566 x_raise_frame (f)
8567 struct frame *f;
8569 if (f->async_visible)
8571 BLOCK_INPUT;
8572 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8573 XFlush (FRAME_X_DISPLAY (f));
8574 UNBLOCK_INPUT;
8578 /* Lower frame F. */
8580 void
8581 x_lower_frame (f)
8582 struct frame *f;
8584 if (f->async_visible)
8586 BLOCK_INPUT;
8587 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8588 XFlush (FRAME_X_DISPLAY (f));
8589 UNBLOCK_INPUT;
8593 static void
8594 XTframe_raise_lower (f, raise_flag)
8595 FRAME_PTR f;
8596 int raise_flag;
8598 if (raise_flag)
8599 x_raise_frame (f);
8600 else
8601 x_lower_frame (f);
8604 /* Change of visibility. */
8606 /* This tries to wait until the frame is really visible.
8607 However, if the window manager asks the user where to position
8608 the frame, this will return before the user finishes doing that.
8609 The frame will not actually be visible at that time,
8610 but it will become visible later when the window manager
8611 finishes with it. */
8613 void
8614 x_make_frame_visible (f)
8615 struct frame *f;
8617 Lisp_Object type;
8618 int original_top, original_left;
8619 int retry_count = 2;
8621 retry:
8623 BLOCK_INPUT;
8625 type = x_icon_type (f);
8626 if (!NILP (type))
8627 x_bitmap_icon (f, type);
8629 if (! FRAME_VISIBLE_P (f))
8631 /* We test FRAME_GARBAGED_P here to make sure we don't
8632 call x_set_offset a second time
8633 if we get to x_make_frame_visible a second time
8634 before the window gets really visible. */
8635 if (! FRAME_ICONIFIED_P (f)
8636 && ! f->output_data.x->asked_for_visible)
8637 x_set_offset (f, f->left_pos, f->top_pos, 0);
8639 f->output_data.x->asked_for_visible = 1;
8641 if (! EQ (Vx_no_window_manager, Qt))
8642 x_wm_set_window_state (f, NormalState);
8643 #ifdef USE_X_TOOLKIT
8644 /* This was XtPopup, but that did nothing for an iconified frame. */
8645 XtMapWidget (f->output_data.x->widget);
8646 #else /* not USE_X_TOOLKIT */
8647 #ifdef USE_GTK
8648 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8649 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8650 #else
8651 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8652 #endif /* not USE_GTK */
8653 #endif /* not USE_X_TOOLKIT */
8654 #if 0 /* This seems to bring back scroll bars in the wrong places
8655 if the window configuration has changed. They seem
8656 to come back ok without this. */
8657 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8658 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8659 #endif
8662 XFlush (FRAME_X_DISPLAY (f));
8664 /* Synchronize to ensure Emacs knows the frame is visible
8665 before we do anything else. We do this loop with input not blocked
8666 so that incoming events are handled. */
8668 Lisp_Object frame;
8669 int count;
8670 /* This must be before UNBLOCK_INPUT
8671 since events that arrive in response to the actions above
8672 will set it when they are handled. */
8673 int previously_visible = f->output_data.x->has_been_visible;
8675 original_left = f->left_pos;
8676 original_top = f->top_pos;
8678 /* This must come after we set COUNT. */
8679 UNBLOCK_INPUT;
8681 /* We unblock here so that arriving X events are processed. */
8683 /* Now move the window back to where it was "supposed to be".
8684 But don't do it if the gravity is negative.
8685 When the gravity is negative, this uses a position
8686 that is 3 pixels too low. Perhaps that's really the border width.
8688 Don't do this if the window has never been visible before,
8689 because the window manager may choose the position
8690 and we don't want to override it. */
8692 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
8693 && f->win_gravity == NorthWestGravity
8694 && previously_visible)
8696 Drawable rootw;
8697 int x, y;
8698 unsigned int width, height, border, depth;
8700 BLOCK_INPUT;
8702 /* On some window managers (such as FVWM) moving an existing
8703 window, even to the same place, causes the window manager
8704 to introduce an offset. This can cause the window to move
8705 to an unexpected location. Check the geometry (a little
8706 slow here) and then verify that the window is in the right
8707 place. If the window is not in the right place, move it
8708 there, and take the potential window manager hit. */
8709 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8710 &rootw, &x, &y, &width, &height, &border, &depth);
8712 if (original_left != x || original_top != y)
8713 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8714 original_left, original_top);
8716 UNBLOCK_INPUT;
8719 XSETFRAME (frame, f);
8721 /* Wait until the frame is visible. Process X events until a
8722 MapNotify event has been seen, or until we think we won't get a
8723 MapNotify at all.. */
8724 for (count = input_signal_count + 10;
8725 input_signal_count < count && !FRAME_VISIBLE_P (f);)
8727 /* Force processing of queued events. */
8728 x_sync (f);
8730 /* Machines that do polling rather than SIGIO have been
8731 observed to go into a busy-wait here. So we'll fake an
8732 alarm signal to let the handler know that there's something
8733 to be read. We used to raise a real alarm, but it seems
8734 that the handler isn't always enabled here. This is
8735 probably a bug. */
8736 if (input_polling_used ())
8738 /* It could be confusing if a real alarm arrives while
8739 processing the fake one. Turn it off and let the
8740 handler reset it. */
8741 extern void poll_for_input_1 P_ ((void));
8742 int old_poll_suppress_count = poll_suppress_count;
8743 poll_suppress_count = 1;
8744 poll_for_input_1 ();
8745 poll_suppress_count = old_poll_suppress_count;
8748 /* See if a MapNotify event has been processed. */
8749 FRAME_SAMPLE_VISIBILITY (f);
8752 /* 2000-09-28: In
8754 (let ((f (selected-frame)))
8755 (iconify-frame f)
8756 (raise-frame f))
8758 the frame is not raised with various window managers on
8759 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
8760 unknown reason, the call to XtMapWidget is completely ignored.
8761 Mapping the widget a second time works. */
8763 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
8764 goto retry;
8768 /* Change from mapped state to withdrawn state. */
8770 /* Make the frame visible (mapped and not iconified). */
8772 void
8773 x_make_frame_invisible (f)
8774 struct frame *f;
8776 Window window;
8778 /* Use the frame's outermost window, not the one we normally draw on. */
8779 window = FRAME_OUTER_WINDOW (f);
8781 /* Don't keep the highlight on an invisible frame. */
8782 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8783 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
8785 #if 0/* This might add unreliability; I don't trust it -- rms. */
8786 if (! f->async_visible && ! f->async_iconified)
8787 return;
8788 #endif
8790 BLOCK_INPUT;
8792 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
8793 that the current position of the window is user-specified, rather than
8794 program-specified, so that when the window is mapped again, it will be
8795 placed at the same location, without forcing the user to position it
8796 by hand again (they have already done that once for this window.) */
8797 x_wm_set_size_hint (f, (long) 0, 1);
8799 #ifdef USE_GTK
8800 if (FRAME_GTK_OUTER_WIDGET (f))
8801 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
8802 else
8803 #endif
8805 #ifdef HAVE_X11R4
8807 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
8808 DefaultScreen (FRAME_X_DISPLAY (f))))
8810 UNBLOCK_INPUT_RESIGNAL;
8811 error ("Can't notify window manager of window withdrawal");
8813 #else /* ! defined (HAVE_X11R4) */
8815 /* Tell the window manager what we're going to do. */
8816 if (! EQ (Vx_no_window_manager, Qt))
8818 XEvent unmap;
8820 unmap.xunmap.type = UnmapNotify;
8821 unmap.xunmap.window = window;
8822 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
8823 unmap.xunmap.from_configure = False;
8824 if (! XSendEvent (FRAME_X_DISPLAY (f),
8825 DefaultRootWindow (FRAME_X_DISPLAY (f)),
8826 False,
8827 SubstructureRedirectMaskSubstructureNotifyMask,
8828 &unmap))
8830 UNBLOCK_INPUT_RESIGNAL;
8831 error ("Can't notify window manager of withdrawal");
8835 /* Unmap the window ourselves. Cheeky! */
8836 XUnmapWindow (FRAME_X_DISPLAY (f), window);
8837 #endif /* ! defined (HAVE_X11R4) */
8840 /* We can't distinguish this from iconification
8841 just by the event that we get from the server.
8842 So we can't win using the usual strategy of letting
8843 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
8844 and synchronize with the server to make sure we agree. */
8845 f->visible = 0;
8846 FRAME_ICONIFIED_P (f) = 0;
8847 f->async_visible = 0;
8848 f->async_iconified = 0;
8850 x_sync (f);
8852 UNBLOCK_INPUT;
8855 /* Change window state from mapped to iconified. */
8857 void
8858 x_iconify_frame (f)
8859 struct frame *f;
8861 int result;
8862 Lisp_Object type;
8864 /* Don't keep the highlight on an invisible frame. */
8865 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
8866 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
8868 if (f->async_iconified)
8869 return;
8871 BLOCK_INPUT;
8873 FRAME_SAMPLE_VISIBILITY (f);
8875 type = x_icon_type (f);
8876 if (!NILP (type))
8877 x_bitmap_icon (f, type);
8879 #ifdef USE_GTK
8880 if (FRAME_GTK_OUTER_WIDGET (f))
8882 if (! FRAME_VISIBLE_P (f))
8883 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8885 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8886 f->iconified = 1;
8887 f->visible = 1;
8888 f->async_iconified = 1;
8889 f->async_visible = 0;
8890 UNBLOCK_INPUT;
8891 return;
8893 #endif
8895 #ifdef USE_X_TOOLKIT
8897 if (! FRAME_VISIBLE_P (f))
8899 if (! EQ (Vx_no_window_manager, Qt))
8900 x_wm_set_window_state (f, IconicState);
8901 /* This was XtPopup, but that did nothing for an iconified frame. */
8902 XtMapWidget (f->output_data.x->widget);
8903 /* The server won't give us any event to indicate
8904 that an invisible frame was changed to an icon,
8905 so we have to record it here. */
8906 f->iconified = 1;
8907 f->visible = 1;
8908 f->async_iconified = 1;
8909 f->async_visible = 0;
8910 UNBLOCK_INPUT;
8911 return;
8914 result = XIconifyWindow (FRAME_X_DISPLAY (f),
8915 XtWindow (f->output_data.x->widget),
8916 DefaultScreen (FRAME_X_DISPLAY (f)));
8917 UNBLOCK_INPUT;
8919 if (!result)
8920 error ("Can't notify window manager of iconification");
8922 f->async_iconified = 1;
8923 f->async_visible = 0;
8926 BLOCK_INPUT;
8927 XFlush (FRAME_X_DISPLAY (f));
8928 UNBLOCK_INPUT;
8929 #else /* not USE_X_TOOLKIT */
8931 /* Make sure the X server knows where the window should be positioned,
8932 in case the user deiconifies with the window manager. */
8933 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
8934 x_set_offset (f, f->left_pos, f->top_pos, 0);
8936 /* Since we don't know which revision of X we're running, we'll use both
8937 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
8939 /* X11R4: send a ClientMessage to the window manager using the
8940 WM_CHANGE_STATE type. */
8942 XEvent message;
8944 message.xclient.window = FRAME_X_WINDOW (f);
8945 message.xclient.type = ClientMessage;
8946 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
8947 message.xclient.format = 32;
8948 message.xclient.data.l[0] = IconicState;
8950 if (! XSendEvent (FRAME_X_DISPLAY (f),
8951 DefaultRootWindow (FRAME_X_DISPLAY (f)),
8952 False,
8953 SubstructureRedirectMask | SubstructureNotifyMask,
8954 &message))
8956 UNBLOCK_INPUT_RESIGNAL;
8957 error ("Can't notify window manager of iconification");
8961 /* X11R3: set the initial_state field of the window manager hints to
8962 IconicState. */
8963 x_wm_set_window_state (f, IconicState);
8965 if (!FRAME_VISIBLE_P (f))
8967 /* If the frame was withdrawn, before, we must map it. */
8968 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8971 f->async_iconified = 1;
8972 f->async_visible = 0;
8974 XFlush (FRAME_X_DISPLAY (f));
8975 UNBLOCK_INPUT;
8976 #endif /* not USE_X_TOOLKIT */
8980 /* Free X resources of frame F. */
8982 void
8983 x_free_frame_resources (f)
8984 struct frame *f;
8986 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8987 Lisp_Object bar;
8988 struct scroll_bar *b;
8990 BLOCK_INPUT;
8992 /* If a display connection is dead, don't try sending more
8993 commands to the X server. */
8994 if (dpyinfo->display)
8996 if (f->output_data.x->icon_desc)
8997 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
8999 #ifdef USE_X_TOOLKIT
9000 /* Explicitly destroy the scroll bars of the frame. Without
9001 this, we get "BadDrawable" errors from the toolkit later on,
9002 presumably from expose events generated for the disappearing
9003 toolkit scroll bars. */
9004 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9006 b = XSCROLL_BAR (bar);
9007 x_scroll_bar_remove (b);
9009 #endif
9011 #ifdef HAVE_X_I18N
9012 if (FRAME_XIC (f))
9013 free_frame_xic (f);
9014 #endif
9016 #ifdef USE_X_TOOLKIT
9017 if (f->output_data.x->widget)
9019 XtDestroyWidget (f->output_data.x->widget);
9020 f->output_data.x->widget = NULL;
9022 /* Tooltips don't have widgets, only a simple X window, even if
9023 we are using a toolkit. */
9024 else if (FRAME_X_WINDOW (f))
9025 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9027 free_frame_menubar (f);
9028 #else /* !USE_X_TOOLKIT */
9030 #ifdef USE_GTK
9031 /* In the GTK version, tooltips are normal X
9032 frames. We must check and free both types. */
9033 if (FRAME_GTK_OUTER_WIDGET (f))
9035 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9036 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9037 FRAME_GTK_OUTER_WIDGET (f) = 0;
9039 #endif /* USE_GTK */
9041 if (FRAME_X_WINDOW (f))
9042 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9043 #endif /* !USE_X_TOOLKIT */
9045 unload_color (f, f->output_data.x->foreground_pixel);
9046 unload_color (f, f->output_data.x->background_pixel);
9047 unload_color (f, f->output_data.x->cursor_pixel);
9048 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9049 unload_color (f, f->output_data.x->border_pixel);
9050 unload_color (f, f->output_data.x->mouse_pixel);
9052 if (f->output_data.x->scroll_bar_background_pixel != -1)
9053 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9054 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9055 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9056 #ifdef USE_TOOLKIT_SCROLL_BARS
9057 /* Scrollbar shadow colors. */
9058 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9059 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9060 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9061 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9062 #endif /* USE_TOOLKIT_SCROLL_BARS */
9063 if (f->output_data.x->white_relief.allocated_p)
9064 unload_color (f, f->output_data.x->white_relief.pixel);
9065 if (f->output_data.x->black_relief.allocated_p)
9066 unload_color (f, f->output_data.x->black_relief.pixel);
9068 if (FRAME_FACE_CACHE (f))
9069 free_frame_faces (f);
9071 x_free_gcs (f);
9072 XFlush (FRAME_X_DISPLAY (f));
9075 if (f->output_data.x->saved_menu_event)
9076 xfree (f->output_data.x->saved_menu_event);
9078 xfree (f->output_data.x);
9079 f->output_data.x = NULL;
9081 if (f == dpyinfo->x_focus_frame)
9082 dpyinfo->x_focus_frame = 0;
9083 if (f == dpyinfo->x_focus_event_frame)
9084 dpyinfo->x_focus_event_frame = 0;
9085 if (f == dpyinfo->x_highlight_frame)
9086 dpyinfo->x_highlight_frame = 0;
9088 if (f == dpyinfo->mouse_face_mouse_frame)
9090 dpyinfo->mouse_face_beg_row
9091 = dpyinfo->mouse_face_beg_col = -1;
9092 dpyinfo->mouse_face_end_row
9093 = dpyinfo->mouse_face_end_col = -1;
9094 dpyinfo->mouse_face_window = Qnil;
9095 dpyinfo->mouse_face_deferred_gc = 0;
9096 dpyinfo->mouse_face_mouse_frame = 0;
9099 UNBLOCK_INPUT;
9103 /* Destroy the X window of frame F. */
9105 void
9106 x_destroy_window (f)
9107 struct frame *f;
9109 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9111 /* If a display connection is dead, don't try sending more
9112 commands to the X server. */
9113 if (dpyinfo->display != 0)
9114 x_free_frame_resources (f);
9116 dpyinfo->reference_count--;
9120 /* Setting window manager hints. */
9122 /* Set the normal size hints for the window manager, for frame F.
9123 FLAGS is the flags word to use--or 0 meaning preserve the flags
9124 that the window now has.
9125 If USER_POSITION is nonzero, we set the USPosition
9126 flag (this is useful when FLAGS is 0).
9127 The GTK version is in gtkutils.c */
9129 #ifndef USE_GTK
9130 void
9131 x_wm_set_size_hint (f, flags, user_position)
9132 struct frame *f;
9133 long flags;
9134 int user_position;
9136 XSizeHints size_hints;
9138 #ifdef USE_X_TOOLKIT
9139 Arg al[2];
9140 int ac = 0;
9141 Dimension widget_width, widget_height;
9142 #endif
9144 Window window = FRAME_OUTER_WINDOW (f);
9146 /* Setting PMaxSize caused various problems. */
9147 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9149 size_hints.x = f->left_pos;
9150 size_hints.y = f->top_pos;
9152 #ifdef USE_X_TOOLKIT
9153 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9154 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9155 XtGetValues (f->output_data.x->widget, al, ac);
9156 size_hints.height = widget_height;
9157 size_hints.width = widget_width;
9158 #else /* not USE_X_TOOLKIT */
9159 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9160 size_hints.width = FRAME_PIXEL_WIDTH (f);
9161 #endif /* not USE_X_TOOLKIT */
9163 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9164 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9165 size_hints.max_width
9166 = FRAME_X_DISPLAY_INFO (f)->width - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9167 size_hints.max_height
9168 = FRAME_X_DISPLAY_INFO (f)->height - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9170 /* Calculate the base and minimum sizes.
9172 (When we use the X toolkit, we don't do it here.
9173 Instead we copy the values that the widgets are using, below.) */
9174 #ifndef USE_X_TOOLKIT
9176 int base_width, base_height;
9177 int min_rows = 0, min_cols = 0;
9179 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9180 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9182 check_frame_size (f, &min_rows, &min_cols);
9184 /* The window manager uses the base width hints to calculate the
9185 current number of rows and columns in the frame while
9186 resizing; min_width and min_height aren't useful for this
9187 purpose, since they might not give the dimensions for a
9188 zero-row, zero-column frame.
9190 We use the base_width and base_height members if we have
9191 them; otherwise, we set the min_width and min_height members
9192 to the size for a zero x zero frame. */
9194 #ifdef HAVE_X11R4
9195 size_hints.flags |= PBaseSize;
9196 size_hints.base_width = base_width;
9197 size_hints.base_height = base_height;
9198 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9199 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9200 #else
9201 size_hints.min_width = base_width;
9202 size_hints.min_height = base_height;
9203 #endif
9206 /* If we don't need the old flags, we don't need the old hint at all. */
9207 if (flags)
9209 size_hints.flags |= flags;
9210 goto no_read;
9212 #endif /* not USE_X_TOOLKIT */
9215 XSizeHints hints; /* Sometimes I hate X Windows... */
9216 long supplied_return;
9217 int value;
9219 #ifdef HAVE_X11R4
9220 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9221 &supplied_return);
9222 #else
9223 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
9224 #endif
9226 #ifdef USE_X_TOOLKIT
9227 size_hints.base_height = hints.base_height;
9228 size_hints.base_width = hints.base_width;
9229 size_hints.min_height = hints.min_height;
9230 size_hints.min_width = hints.min_width;
9231 #endif
9233 if (flags)
9234 size_hints.flags |= flags;
9235 else
9237 if (value == 0)
9238 hints.flags = 0;
9239 if (hints.flags & PSize)
9240 size_hints.flags |= PSize;
9241 if (hints.flags & PPosition)
9242 size_hints.flags |= PPosition;
9243 if (hints.flags & USPosition)
9244 size_hints.flags |= USPosition;
9245 if (hints.flags & USSize)
9246 size_hints.flags |= USSize;
9250 #ifndef USE_X_TOOLKIT
9251 no_read:
9252 #endif
9254 #ifdef PWinGravity
9255 size_hints.win_gravity = f->win_gravity;
9256 size_hints.flags |= PWinGravity;
9258 if (user_position)
9260 size_hints.flags &= ~ PPosition;
9261 size_hints.flags |= USPosition;
9263 #endif /* PWinGravity */
9265 #ifdef HAVE_X11R4
9266 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9267 #else
9268 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9269 #endif
9271 #endif /* not USE_GTK */
9273 /* Used for IconicState or NormalState */
9275 void
9276 x_wm_set_window_state (f, state)
9277 struct frame *f;
9278 int state;
9280 #ifdef USE_X_TOOLKIT
9281 Arg al[1];
9283 XtSetArg (al[0], XtNinitialState, state);
9284 XtSetValues (f->output_data.x->widget, al, 1);
9285 #else /* not USE_X_TOOLKIT */
9286 Window window = FRAME_X_WINDOW (f);
9288 f->output_data.x->wm_hints.flags |= StateHint;
9289 f->output_data.x->wm_hints.initial_state = state;
9291 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9292 #endif /* not USE_X_TOOLKIT */
9295 void
9296 x_wm_set_icon_pixmap (f, pixmap_id)
9297 struct frame *f;
9298 int pixmap_id;
9300 Pixmap icon_pixmap, icon_mask;
9302 #ifndef USE_X_TOOLKIT
9303 Window window = FRAME_OUTER_WINDOW (f);
9304 #endif
9306 if (pixmap_id > 0)
9308 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9309 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9310 icon_mask = x_bitmap_mask (f, pixmap_id);
9311 f->output_data.x->wm_hints.icon_mask = icon_mask;
9313 else
9315 /* It seems there is no way to turn off use of an icon pixmap.
9316 The following line does it, only if no icon has yet been created,
9317 for some window managers. But with mwm it crashes.
9318 Some people say it should clear the IconPixmapHint bit in this case,
9319 but that doesn't work, and the X consortium said it isn't the
9320 right thing at all. Since there is no way to win,
9321 best to explicitly give up. */
9322 #if 0
9323 f->output_data.x->wm_hints.icon_pixmap = None;
9324 f->output_data.x->wm_hints.icon_mask = None;
9325 #else
9326 return;
9327 #endif
9330 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
9333 Arg al[1];
9334 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9335 XtSetValues (f->output_data.x->widget, al, 1);
9336 XtSetArg (al[0], XtNiconMask, icon_mask);
9337 XtSetValues (f->output_data.x->widget, al, 1);
9340 #else /* not USE_X_TOOLKIT */
9342 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9343 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9345 #endif /* not USE_X_TOOLKIT */
9348 void
9349 x_wm_set_icon_position (f, icon_x, icon_y)
9350 struct frame *f;
9351 int icon_x, icon_y;
9353 Window window = FRAME_OUTER_WINDOW (f);
9355 f->output_data.x->wm_hints.flags |= IconPositionHint;
9356 f->output_data.x->wm_hints.icon_x = icon_x;
9357 f->output_data.x->wm_hints.icon_y = icon_y;
9359 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9363 /***********************************************************************
9364 Fonts
9365 ***********************************************************************/
9367 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
9369 struct font_info *
9370 x_get_font_info (f, font_idx)
9371 FRAME_PTR f;
9372 int font_idx;
9374 return (FRAME_X_FONT_TABLE (f) + font_idx);
9378 /* Return a list of names of available fonts matching PATTERN on frame F.
9380 If SIZE is > 0, it is the size (maximum bounds width) of fonts
9381 to be listed.
9383 SIZE < 0 means include scalable fonts.
9385 Frame F null means we have not yet created any frame on X, and
9386 consult the first display in x_display_list. MAXNAMES sets a limit
9387 on how many fonts to match. */
9389 Lisp_Object
9390 x_list_fonts (f, pattern, size, maxnames)
9391 struct frame *f;
9392 Lisp_Object pattern;
9393 int size;
9394 int maxnames;
9396 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
9397 Lisp_Object tem, second_best;
9398 struct x_display_info *dpyinfo
9399 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
9400 Display *dpy = dpyinfo->display;
9401 int try_XLoadQueryFont = 0;
9402 int count;
9403 int allow_auto_scaled_font = 0;
9405 if (size < 0)
9407 allow_auto_scaled_font = 1;
9408 size = 0;
9411 patterns = Fassoc (pattern, Valternate_fontname_alist);
9412 if (NILP (patterns))
9413 patterns = Fcons (pattern, Qnil);
9415 if (maxnames == 1 && !size)
9416 /* We can return any single font matching PATTERN. */
9417 try_XLoadQueryFont = 1;
9419 for (; CONSP (patterns); patterns = XCDR (patterns))
9421 int num_fonts;
9422 char **names = NULL;
9424 pattern = XCAR (patterns);
9425 /* See if we cached the result for this particular query.
9426 The cache is an alist of the form:
9427 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
9428 tem = XCDR (dpyinfo->name_list_element);
9429 key = Fcons (Fcons (pattern, make_number (maxnames)),
9430 allow_auto_scaled_font ? Qt : Qnil);
9431 list = Fassoc (key, tem);
9432 if (!NILP (list))
9434 list = Fcdr_safe (list);
9435 /* We have a cashed list. Don't have to get the list again. */
9436 goto label_cached;
9439 /* At first, put PATTERN in the cache. */
9441 BLOCK_INPUT;
9442 count = x_catch_errors (dpy);
9444 if (try_XLoadQueryFont)
9446 XFontStruct *font;
9447 unsigned long value;
9449 font = XLoadQueryFont (dpy, SDATA (pattern));
9450 if (x_had_errors_p (dpy))
9452 /* This error is perhaps due to insufficient memory on X
9453 server. Let's just ignore it. */
9454 font = NULL;
9455 x_clear_errors (dpy);
9458 if (font
9459 && XGetFontProperty (font, XA_FONT, &value))
9461 char *name = (char *) XGetAtomName (dpy, (Atom) value);
9462 int len = strlen (name);
9463 char *tmp;
9465 /* If DXPC (a Differential X Protocol Compressor)
9466 Ver.3.7 is running, XGetAtomName will return null
9467 string. We must avoid such a name. */
9468 if (len == 0)
9469 try_XLoadQueryFont = 0;
9470 else
9472 num_fonts = 1;
9473 names = (char **) alloca (sizeof (char *));
9474 /* Some systems only allow alloca assigned to a
9475 simple var. */
9476 tmp = (char *) alloca (len + 1); names[0] = tmp;
9477 bcopy (name, names[0], len + 1);
9478 XFree (name);
9481 else
9482 try_XLoadQueryFont = 0;
9484 if (font)
9485 XFreeFont (dpy, font);
9488 if (!try_XLoadQueryFont)
9490 /* We try at least 10 fonts because XListFonts will return
9491 auto-scaled fonts at the head. */
9492 if (maxnames < 0)
9494 int limit;
9496 for (limit = 500;;)
9498 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
9499 if (num_fonts == limit)
9501 BLOCK_INPUT;
9502 XFreeFontNames (names);
9503 UNBLOCK_INPUT;
9504 limit *= 2;
9506 else
9507 break;
9510 else
9511 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
9512 &num_fonts);
9514 if (x_had_errors_p (dpy))
9516 /* This error is perhaps due to insufficient memory on X
9517 server. Let's just ignore it. */
9518 names = NULL;
9519 x_clear_errors (dpy);
9523 x_uncatch_errors (dpy, count);
9524 UNBLOCK_INPUT;
9526 if (names)
9528 int i;
9530 /* Make a list of all the fonts we got back.
9531 Store that in the font cache for the display. */
9532 for (i = 0; i < num_fonts; i++)
9534 int width = 0;
9535 char *p = names[i];
9536 int average_width = -1, resx = 0, dashes = 0;
9538 /* Count the number of dashes in NAMES[I]. If there are
9539 14 dashes, the field value following 9th dash
9540 (RESOLUTION_X) is nonzero, and the field value
9541 following 12th dash (AVERAGE_WIDTH) is 0, this is a
9542 auto-scaled font which is usually too ugly to be used
9543 for editing. Let's ignore it. */
9544 while (*p)
9545 if (*p++ == '-')
9547 dashes++;
9548 if (dashes == 7) /* PIXEL_SIZE field */
9549 width = atoi (p);
9550 else if (dashes == 9)
9551 resx = atoi (p);
9552 else if (dashes == 12) /* AVERAGE_WIDTH field */
9553 average_width = atoi (p);
9556 if (allow_auto_scaled_font
9557 || dashes < 14 || average_width != 0 || resx == 0)
9559 tem = build_string (names[i]);
9560 if (NILP (Fassoc (tem, list)))
9562 if (STRINGP (Vx_pixel_size_width_font_regexp)
9563 && ((fast_c_string_match_ignore_case
9564 (Vx_pixel_size_width_font_regexp, names[i]))
9565 >= 0))
9566 /* We can set the value of PIXEL_SIZE to the
9567 width of this font. */
9568 list = Fcons (Fcons (tem, make_number (width)), list);
9569 else
9570 /* For the moment, width is not known. */
9571 list = Fcons (Fcons (tem, Qnil), list);
9576 if (!try_XLoadQueryFont)
9578 BLOCK_INPUT;
9579 XFreeFontNames (names);
9580 UNBLOCK_INPUT;
9584 /* Now store the result in the cache. */
9585 XSETCDR (dpyinfo->name_list_element,
9586 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
9588 label_cached:
9589 if (NILP (list)) continue; /* Try the remaining alternatives. */
9591 newlist = second_best = Qnil;
9592 /* Make a list of the fonts that have the right width. */
9593 for (; CONSP (list); list = XCDR (list))
9595 int found_size;
9597 tem = XCAR (list);
9599 if (!CONSP (tem) || NILP (XCAR (tem)))
9600 continue;
9601 if (!size)
9603 newlist = Fcons (XCAR (tem), newlist);
9604 continue;
9607 if (!INTEGERP (XCDR (tem)))
9609 /* Since we have not yet known the size of this font, we
9610 must try slow function call XLoadQueryFont. */
9611 XFontStruct *thisinfo;
9613 BLOCK_INPUT;
9614 count = x_catch_errors (dpy);
9615 thisinfo = XLoadQueryFont (dpy,
9616 SDATA (XCAR (tem)));
9617 if (x_had_errors_p (dpy))
9619 /* This error is perhaps due to insufficient memory on X
9620 server. Let's just ignore it. */
9621 thisinfo = NULL;
9622 x_clear_errors (dpy);
9624 x_uncatch_errors (dpy, count);
9625 UNBLOCK_INPUT;
9627 if (thisinfo)
9629 XSETCDR (tem,
9630 (thisinfo->min_bounds.width == 0
9631 ? make_number (0)
9632 : make_number (thisinfo->max_bounds.width)));
9633 BLOCK_INPUT;
9634 XFreeFont (dpy, thisinfo);
9635 UNBLOCK_INPUT;
9637 else
9638 /* For unknown reason, the previous call of XListFont had
9639 returned a font which can't be opened. Record the size
9640 as 0 not to try to open it again. */
9641 XSETCDR (tem, make_number (0));
9644 found_size = XINT (XCDR (tem));
9645 if (found_size == size)
9646 newlist = Fcons (XCAR (tem), newlist);
9647 else if (found_size > 0)
9649 if (NILP (second_best))
9650 second_best = tem;
9651 else if (found_size < size)
9653 if (XINT (XCDR (second_best)) > size
9654 || XINT (XCDR (second_best)) < found_size)
9655 second_best = tem;
9657 else
9659 if (XINT (XCDR (second_best)) > size
9660 && XINT (XCDR (second_best)) > found_size)
9661 second_best = tem;
9665 if (!NILP (newlist))
9666 break;
9667 else if (!NILP (second_best))
9669 newlist = Fcons (XCAR (second_best), Qnil);
9670 break;
9674 return newlist;
9678 #if GLYPH_DEBUG
9680 /* Check that FONT is valid on frame F. It is if it can be found in F's
9681 font table. */
9683 static void
9684 x_check_font (f, font)
9685 struct frame *f;
9686 XFontStruct *font;
9688 int i;
9689 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9691 xassert (font != NULL);
9693 for (i = 0; i < dpyinfo->n_fonts; i++)
9694 if (dpyinfo->font_table[i].name
9695 && font == dpyinfo->font_table[i].font)
9696 break;
9698 xassert (i < dpyinfo->n_fonts);
9701 #endif /* GLYPH_DEBUG != 0 */
9703 /* Set *W to the minimum width, *H to the minimum font height of FONT.
9704 Note: There are (broken) X fonts out there with invalid XFontStruct
9705 min_bounds contents. For example, handa@etl.go.jp reports that
9706 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
9707 have font->min_bounds.width == 0. */
9709 static INLINE void
9710 x_font_min_bounds (font, w, h)
9711 XFontStruct *font;
9712 int *w, *h;
9714 *h = FONT_HEIGHT (font);
9715 *w = font->min_bounds.width;
9717 /* Try to handle the case where FONT->min_bounds has invalid
9718 contents. Since the only font known to have invalid min_bounds
9719 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
9720 if (*w <= 0)
9721 *w = font->max_bounds.width;
9725 /* Compute the smallest character width and smallest font height over
9726 all fonts available on frame F. Set the members smallest_char_width
9727 and smallest_font_height in F's x_display_info structure to
9728 the values computed. Value is non-zero if smallest_font_height or
9729 smallest_char_width become smaller than they were before. */
9731 static int
9732 x_compute_min_glyph_bounds (f)
9733 struct frame *f;
9735 int i;
9736 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9737 XFontStruct *font;
9738 int old_width = dpyinfo->smallest_char_width;
9739 int old_height = dpyinfo->smallest_font_height;
9741 dpyinfo->smallest_font_height = 100000;
9742 dpyinfo->smallest_char_width = 100000;
9744 for (i = 0; i < dpyinfo->n_fonts; ++i)
9745 if (dpyinfo->font_table[i].name)
9747 struct font_info *fontp = dpyinfo->font_table + i;
9748 int w, h;
9750 font = (XFontStruct *) fontp->font;
9751 xassert (font != (XFontStruct *) ~0);
9752 x_font_min_bounds (font, &w, &h);
9754 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
9755 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
9758 xassert (dpyinfo->smallest_char_width > 0
9759 && dpyinfo->smallest_font_height > 0);
9761 return (dpyinfo->n_fonts == 1
9762 || dpyinfo->smallest_char_width < old_width
9763 || dpyinfo->smallest_font_height < old_height);
9767 /* Load font named FONTNAME of the size SIZE for frame F, and return a
9768 pointer to the structure font_info while allocating it dynamically.
9769 If SIZE is 0, load any size of font.
9770 If loading is failed, return NULL. */
9772 struct font_info *
9773 x_load_font (f, fontname, size)
9774 struct frame *f;
9775 register char *fontname;
9776 int size;
9778 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9779 Lisp_Object font_names;
9780 int count;
9782 /* Get a list of all the fonts that match this name. Once we
9783 have a list of matching fonts, we compare them against the fonts
9784 we already have by comparing names. */
9785 font_names = x_list_fonts (f, build_string (fontname), size, 1);
9787 if (!NILP (font_names))
9789 Lisp_Object tail;
9790 int i;
9792 for (i = 0; i < dpyinfo->n_fonts; i++)
9793 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
9794 if (dpyinfo->font_table[i].name
9795 && (!strcmp (dpyinfo->font_table[i].name,
9796 SDATA (XCAR (tail)))
9797 || !strcmp (dpyinfo->font_table[i].full_name,
9798 SDATA (XCAR (tail)))))
9799 return (dpyinfo->font_table + i);
9802 /* Load the font and add it to the table. */
9804 char *full_name;
9805 XFontStruct *font;
9806 struct font_info *fontp;
9807 unsigned long value;
9808 int i;
9810 /* If we have found fonts by x_list_font, load one of them. If
9811 not, we still try to load a font by the name given as FONTNAME
9812 because XListFonts (called in x_list_font) of some X server has
9813 a bug of not finding a font even if the font surely exists and
9814 is loadable by XLoadQueryFont. */
9815 if (size > 0 && !NILP (font_names))
9816 fontname = (char *) SDATA (XCAR (font_names));
9818 BLOCK_INPUT;
9819 count = x_catch_errors (FRAME_X_DISPLAY (f));
9820 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
9821 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
9823 /* This error is perhaps due to insufficient memory on X
9824 server. Let's just ignore it. */
9825 font = NULL;
9826 x_clear_errors (FRAME_X_DISPLAY (f));
9828 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
9829 UNBLOCK_INPUT;
9830 if (!font)
9831 return NULL;
9833 /* Find a free slot in the font table. */
9834 for (i = 0; i < dpyinfo->n_fonts; ++i)
9835 if (dpyinfo->font_table[i].name == NULL)
9836 break;
9838 /* If no free slot found, maybe enlarge the font table. */
9839 if (i == dpyinfo->n_fonts
9840 && dpyinfo->n_fonts == dpyinfo->font_table_size)
9842 int sz;
9843 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
9844 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
9845 dpyinfo->font_table
9846 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
9849 fontp = dpyinfo->font_table + i;
9850 if (i == dpyinfo->n_fonts)
9851 ++dpyinfo->n_fonts;
9853 /* Now fill in the slots of *FONTP. */
9854 BLOCK_INPUT;
9855 bzero (fontp, sizeof (*fontp));
9856 fontp->font = font;
9857 fontp->font_idx = i;
9858 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
9859 bcopy (fontname, fontp->name, strlen (fontname) + 1);
9861 /* Try to get the full name of FONT. Put it in FULL_NAME. */
9862 full_name = 0;
9863 if (XGetFontProperty (font, XA_FONT, &value))
9865 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
9866 char *p = name;
9867 int dashes = 0;
9869 /* Count the number of dashes in the "full name".
9870 If it is too few, this isn't really the font's full name,
9871 so don't use it.
9872 In X11R4, the fonts did not come with their canonical names
9873 stored in them. */
9874 while (*p)
9876 if (*p == '-')
9877 dashes++;
9878 p++;
9881 if (dashes >= 13)
9883 full_name = (char *) xmalloc (p - name + 1);
9884 bcopy (name, full_name, p - name + 1);
9887 XFree (name);
9890 if (full_name != 0)
9891 fontp->full_name = full_name;
9892 else
9893 fontp->full_name = fontp->name;
9895 fontp->size = font->max_bounds.width;
9896 fontp->height = FONT_HEIGHT (font);
9898 if (NILP (font_names))
9900 /* We come here because of a bug of XListFonts mentioned at
9901 the head of this block. Let's store this information in
9902 the cache for x_list_fonts. */
9903 Lisp_Object lispy_name = build_string (fontname);
9904 Lisp_Object lispy_full_name = build_string (fontp->full_name);
9905 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
9906 Qnil);
9908 XSETCDR (dpyinfo->name_list_element,
9909 Fcons (Fcons (key,
9910 Fcons (Fcons (lispy_full_name,
9911 make_number (fontp->size)),
9912 Qnil)),
9913 XCDR (dpyinfo->name_list_element)));
9914 if (full_name)
9916 key = Fcons (Fcons (lispy_full_name, make_number (256)),
9917 Qnil);
9918 XSETCDR (dpyinfo->name_list_element,
9919 Fcons (Fcons (key,
9920 Fcons (Fcons (lispy_full_name,
9921 make_number (fontp->size)),
9922 Qnil)),
9923 XCDR (dpyinfo->name_list_element)));
9927 /* The slot `encoding' specifies how to map a character
9928 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
9929 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
9930 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
9931 2:0xA020..0xFF7F). For the moment, we don't know which charset
9932 uses this font. So, we set information in fontp->encoding[1]
9933 which is never used by any charset. If mapping can't be
9934 decided, set FONT_ENCODING_NOT_DECIDED. */
9935 fontp->encoding[1]
9936 = (font->max_byte1 == 0
9937 /* 1-byte font */
9938 ? (font->min_char_or_byte2 < 0x80
9939 ? (font->max_char_or_byte2 < 0x80
9940 ? 0 /* 0x20..0x7F */
9941 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
9942 : 1) /* 0xA0..0xFF */
9943 /* 2-byte font */
9944 : (font->min_byte1 < 0x80
9945 ? (font->max_byte1 < 0x80
9946 ? (font->min_char_or_byte2 < 0x80
9947 ? (font->max_char_or_byte2 < 0x80
9948 ? 0 /* 0x2020..0x7F7F */
9949 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
9950 : 3) /* 0x20A0..0x7FFF */
9951 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
9952 : (font->min_char_or_byte2 < 0x80
9953 ? (font->max_char_or_byte2 < 0x80
9954 ? 2 /* 0xA020..0xFF7F */
9955 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
9956 : 1))); /* 0xA0A0..0xFFFF */
9958 fontp->baseline_offset
9959 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
9960 ? (long) value : 0);
9961 fontp->relative_compose
9962 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
9963 ? (long) value : 0);
9964 fontp->default_ascent
9965 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
9966 ? (long) value : 0);
9968 /* Set global flag fonts_changed_p to non-zero if the font loaded
9969 has a character with a smaller width than any other character
9970 before, or if the font loaded has a smaller height than any
9971 other font loaded before. If this happens, it will make a
9972 glyph matrix reallocation necessary. */
9973 fonts_changed_p |= x_compute_min_glyph_bounds (f);
9974 UNBLOCK_INPUT;
9975 return fontp;
9980 /* Return a pointer to struct font_info of a font named FONTNAME for
9981 frame F. If no such font is loaded, return NULL. */
9983 struct font_info *
9984 x_query_font (f, fontname)
9985 struct frame *f;
9986 register char *fontname;
9988 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9989 int i;
9991 for (i = 0; i < dpyinfo->n_fonts; i++)
9992 if (dpyinfo->font_table[i].name
9993 && (!strcmp (dpyinfo->font_table[i].name, fontname)
9994 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
9995 return (dpyinfo->font_table + i);
9996 return NULL;
10000 /* Find a CCL program for a font specified by FONTP, and set the member
10001 `encoder' of the structure. */
10003 void
10004 x_find_ccl_program (fontp)
10005 struct font_info *fontp;
10007 Lisp_Object list, elt;
10009 elt = Qnil;
10010 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
10012 elt = XCAR (list);
10013 if (CONSP (elt)
10014 && STRINGP (XCAR (elt))
10015 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10016 >= 0)
10017 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10018 >= 0)))
10019 break;
10022 if (! NILP (list))
10024 struct ccl_program *ccl
10025 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
10027 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
10028 xfree (ccl);
10029 else
10030 fontp->font_encoder = ccl;
10036 /***********************************************************************
10037 Initialization
10038 ***********************************************************************/
10040 #ifdef USE_X_TOOLKIT
10041 static XrmOptionDescRec emacs_options[] = {
10042 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10043 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10045 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10046 XrmoptionSepArg, NULL},
10047 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10049 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10050 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10051 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10052 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10053 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10054 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10055 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10057 #endif /* USE_X_TOOLKIT */
10059 static int x_initialized;
10061 #ifdef MULTI_KBOARD
10062 /* Test whether two display-name strings agree up to the dot that separates
10063 the screen number from the server number. */
10064 static int
10065 same_x_server (name1, name2)
10066 const char *name1, *name2;
10068 int seen_colon = 0;
10069 const unsigned char *system_name = SDATA (Vsystem_name);
10070 int system_name_length = strlen (system_name);
10071 int length_until_period = 0;
10073 while (system_name[length_until_period] != 0
10074 && system_name[length_until_period] != '.')
10075 length_until_period++;
10077 /* Treat `unix' like an empty host name. */
10078 if (! strncmp (name1, "unix:", 5))
10079 name1 += 4;
10080 if (! strncmp (name2, "unix:", 5))
10081 name2 += 4;
10082 /* Treat this host's name like an empty host name. */
10083 if (! strncmp (name1, system_name, system_name_length)
10084 && name1[system_name_length] == ':')
10085 name1 += system_name_length;
10086 if (! strncmp (name2, system_name, system_name_length)
10087 && name2[system_name_length] == ':')
10088 name2 += system_name_length;
10089 /* Treat this host's domainless name like an empty host name. */
10090 if (! strncmp (name1, system_name, length_until_period)
10091 && name1[length_until_period] == ':')
10092 name1 += length_until_period;
10093 if (! strncmp (name2, system_name, length_until_period)
10094 && name2[length_until_period] == ':')
10095 name2 += length_until_period;
10097 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10099 if (*name1 == ':')
10100 seen_colon++;
10101 if (seen_colon && *name1 == '.')
10102 return 1;
10104 return (seen_colon
10105 && (*name1 == '.' || *name1 == '\0')
10106 && (*name2 == '.' || *name2 == '\0'));
10108 #endif
10110 /* Count number of set bits in mask and number of bits to shift to
10111 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10112 to 5. */
10113 static void
10114 get_bits_and_offset (mask, bits, offset)
10115 unsigned long mask;
10116 int *bits;
10117 int *offset;
10119 int nr = 0;
10120 int off = 0;
10122 while (!(mask & 1))
10124 off++;
10125 mask >>= 1;
10128 while (mask & 1)
10130 nr++;
10131 mask >>= 1;
10134 *offset = off;
10135 *bits = nr;
10138 struct x_display_info *
10139 x_term_init (display_name, xrm_option, resource_name)
10140 Lisp_Object display_name;
10141 char *xrm_option;
10142 char *resource_name;
10144 int connection;
10145 Display *dpy;
10146 struct x_display_info *dpyinfo;
10147 XrmDatabase xrdb;
10149 BLOCK_INPUT;
10151 if (!x_initialized)
10153 x_initialize ();
10154 ++x_initialized;
10157 #ifdef USE_GTK
10159 #define NUM_ARGV 10
10160 int argc;
10161 char *argv[NUM_ARGV];
10162 char **argv2 = argv;
10163 GdkAtom atom;
10165 if (x_initialized++ > 1)
10167 /* Opening another display. If xg_display_open returns less
10168 than zero, we are probably on GTK 2.0, which can only handle
10169 one display. GTK 2.2 or later can handle more than one. */
10170 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10171 error ("Sorry, this version of GTK can only handle one display");
10173 else
10175 for (argc = 0; argc < NUM_ARGV; ++argc)
10176 argv[argc] = 0;
10178 argc = 0;
10179 argv[argc++] = initial_argv[0];
10181 if (! NILP (display_name))
10183 argv[argc++] = "--display";
10184 argv[argc++] = SDATA (display_name);
10187 argv[argc++] = "--name";
10188 argv[argc++] = resource_name;
10190 #ifdef HAVE_X11R5
10191 XSetLocaleModifiers ("");
10192 #endif
10194 gtk_init (&argc, &argv2);
10196 /* gtk_init does set_locale. We must fix locale after calling it. */
10197 fixup_locale ();
10198 xg_initialize ();
10200 dpy = GDK_DISPLAY ();
10202 /* NULL window -> events for all windows go to our function */
10203 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10205 /* Load our own gtkrc if it exists. */
10207 struct gcpro gcpro1, gcpro2;
10208 char *file = "~/.emacs.d/gtkrc";
10209 Lisp_Object s, abs_file;
10211 GCPRO2 (s, abs_file);
10212 s = make_string (file, strlen (file));
10213 abs_file = Fexpand_file_name (s, Qnil);
10215 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10216 gtk_rc_parse (SDATA (abs_file));
10218 UNGCPRO;
10221 XSetErrorHandler (x_error_handler);
10222 XSetIOErrorHandler (x_io_error_quitter);
10225 #else /* not USE_GTK */
10226 #ifdef USE_X_TOOLKIT
10227 /* weiner@footloose.sps.mot.com reports that this causes
10228 errors with X11R5:
10229 X protocol error: BadAtom (invalid Atom parameter)
10230 on protocol request 18skiloaf.
10231 So let's not use it until R6. */
10232 #ifdef HAVE_X11XTR6
10233 XtSetLanguageProc (NULL, NULL, NULL);
10234 #endif
10237 int argc = 0;
10238 char *argv[3];
10240 argv[0] = "";
10241 argc = 1;
10242 if (xrm_option)
10244 argv[argc++] = "-xrm";
10245 argv[argc++] = xrm_option;
10247 turn_on_atimers (0);
10248 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10249 resource_name, EMACS_CLASS,
10250 emacs_options, XtNumber (emacs_options),
10251 &argc, argv);
10252 turn_on_atimers (1);
10254 #ifdef HAVE_X11XTR6
10255 /* I think this is to compensate for XtSetLanguageProc. */
10256 fixup_locale ();
10257 #endif
10260 #else /* not USE_X_TOOLKIT */
10261 #ifdef HAVE_X11R5
10262 XSetLocaleModifiers ("");
10263 #endif
10264 dpy = XOpenDisplay (SDATA (display_name));
10265 #endif /* not USE_X_TOOLKIT */
10266 #endif /* not USE_GTK*/
10268 /* Detect failure. */
10269 if (dpy == 0)
10271 UNBLOCK_INPUT;
10272 return 0;
10275 /* We have definitely succeeded. Record the new connection. */
10277 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10278 bzero (dpyinfo, sizeof *dpyinfo);
10280 #ifdef MULTI_KBOARD
10282 struct x_display_info *share;
10283 Lisp_Object tail;
10285 for (share = x_display_list, tail = x_display_name_list; share;
10286 share = share->next, tail = XCDR (tail))
10287 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10288 SDATA (display_name)))
10289 break;
10290 if (share)
10291 dpyinfo->kboard = share->kboard;
10292 else
10294 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10295 init_kboard (dpyinfo->kboard);
10296 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10298 char *vendor = ServerVendor (dpy);
10299 UNBLOCK_INPUT;
10300 dpyinfo->kboard->Vsystem_key_alist
10301 = call1 (Qvendor_specific_keysyms,
10302 build_string (vendor ? vendor : ""));
10303 BLOCK_INPUT;
10306 dpyinfo->kboard->next_kboard = all_kboards;
10307 all_kboards = dpyinfo->kboard;
10308 /* Don't let the initial kboard remain current longer than necessary.
10309 That would cause problems if a file loaded on startup tries to
10310 prompt in the mini-buffer. */
10311 if (current_kboard == initial_kboard)
10312 current_kboard = dpyinfo->kboard;
10314 dpyinfo->kboard->reference_count++;
10316 #endif
10318 /* Put this display on the chain. */
10319 dpyinfo->next = x_display_list;
10320 x_display_list = dpyinfo;
10322 /* Put it on x_display_name_list as well, to keep them parallel. */
10323 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10324 x_display_name_list);
10325 dpyinfo->name_list_element = XCAR (x_display_name_list);
10327 dpyinfo->display = dpy;
10329 #if 0
10330 XSetAfterFunction (x_current_display, x_trace_wire);
10331 #endif /* ! 0 */
10333 dpyinfo->x_id_name
10334 = (char *) xmalloc (SBYTES (Vinvocation_name)
10335 + SBYTES (Vsystem_name)
10336 + 2);
10337 sprintf (dpyinfo->x_id_name, "%s@%s",
10338 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10340 /* Figure out which modifier bits mean what. */
10341 x_find_modifier_meanings (dpyinfo);
10343 /* Get the scroll bar cursor. */
10344 #ifdef USE_GTK
10345 /* We must create a GTK cursor, it is required for GTK widgets. */
10346 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10347 #endif /* USE_GTK */
10349 dpyinfo->vertical_scroll_bar_cursor
10350 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10352 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10353 resource_name, EMACS_CLASS);
10354 #ifdef HAVE_XRMSETDATABASE
10355 XrmSetDatabase (dpyinfo->display, xrdb);
10356 #else
10357 dpyinfo->display->db = xrdb;
10358 #endif
10359 /* Put the rdb where we can find it in a way that works on
10360 all versions. */
10361 dpyinfo->xrdb = xrdb;
10363 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10364 DefaultScreen (dpyinfo->display));
10365 select_visual (dpyinfo);
10366 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10367 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
10368 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
10369 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10370 dpyinfo->client_leader_window = 0;
10371 dpyinfo->grabbed = 0;
10372 dpyinfo->reference_count = 0;
10373 dpyinfo->icon_bitmap_id = -1;
10374 dpyinfo->font_table = NULL;
10375 dpyinfo->n_fonts = 0;
10376 dpyinfo->font_table_size = 0;
10377 dpyinfo->bitmaps = 0;
10378 dpyinfo->bitmaps_size = 0;
10379 dpyinfo->bitmaps_last = 0;
10380 dpyinfo->scratch_cursor_gc = 0;
10381 dpyinfo->mouse_face_mouse_frame = 0;
10382 dpyinfo->mouse_face_deferred_gc = 0;
10383 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10384 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10385 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10386 dpyinfo->mouse_face_window = Qnil;
10387 dpyinfo->mouse_face_overlay = Qnil;
10388 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10389 dpyinfo->mouse_face_defer = 0;
10390 dpyinfo->mouse_face_hidden = 0;
10391 dpyinfo->x_focus_frame = 0;
10392 dpyinfo->x_focus_event_frame = 0;
10393 dpyinfo->x_highlight_frame = 0;
10394 dpyinfo->image_cache = make_image_cache ();
10395 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10397 /* See if we can construct pixel values from RGB values. */
10398 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10399 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10401 if (dpyinfo->visual->class == TrueColor)
10403 get_bits_and_offset (dpyinfo->visual->red_mask,
10404 &dpyinfo->red_bits, &dpyinfo->red_offset);
10405 get_bits_and_offset (dpyinfo->visual->blue_mask,
10406 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10407 get_bits_and_offset (dpyinfo->visual->green_mask,
10408 &dpyinfo->green_bits, &dpyinfo->green_offset);
10411 /* See if a private colormap is requested. */
10412 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10414 if (dpyinfo->visual->class == PseudoColor)
10416 Lisp_Object value;
10417 value = display_x_get_resource (dpyinfo,
10418 build_string ("privateColormap"),
10419 build_string ("PrivateColormap"),
10420 Qnil, Qnil);
10421 if (STRINGP (value)
10422 && (!strcmp (SDATA (value), "true")
10423 || !strcmp (SDATA (value), "on")))
10424 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10427 else
10428 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10429 dpyinfo->visual, AllocNone);
10432 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10433 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10434 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10435 dpyinfo->resy = pixels * 25.4 / mm;
10436 pixels = DisplayWidth (dpyinfo->display, screen_number);
10437 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10438 dpyinfo->resx = pixels * 25.4 / mm;
10441 dpyinfo->Xatom_wm_protocols
10442 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10443 dpyinfo->Xatom_wm_take_focus
10444 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10445 dpyinfo->Xatom_wm_save_yourself
10446 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10447 dpyinfo->Xatom_wm_delete_window
10448 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10449 dpyinfo->Xatom_wm_change_state
10450 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10451 dpyinfo->Xatom_wm_configure_denied
10452 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10453 dpyinfo->Xatom_wm_window_moved
10454 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10455 dpyinfo->Xatom_wm_client_leader
10456 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10457 dpyinfo->Xatom_editres
10458 = XInternAtom (dpyinfo->display, "Editres", False);
10459 dpyinfo->Xatom_CLIPBOARD
10460 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10461 dpyinfo->Xatom_TIMESTAMP
10462 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10463 dpyinfo->Xatom_TEXT
10464 = XInternAtom (dpyinfo->display, "TEXT", False);
10465 dpyinfo->Xatom_COMPOUND_TEXT
10466 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10467 dpyinfo->Xatom_UTF8_STRING
10468 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10469 dpyinfo->Xatom_DELETE
10470 = XInternAtom (dpyinfo->display, "DELETE", False);
10471 dpyinfo->Xatom_MULTIPLE
10472 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10473 dpyinfo->Xatom_INCR
10474 = XInternAtom (dpyinfo->display, "INCR", False);
10475 dpyinfo->Xatom_EMACS_TMP
10476 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10477 dpyinfo->Xatom_TARGETS
10478 = XInternAtom (dpyinfo->display, "TARGETS", False);
10479 dpyinfo->Xatom_NULL
10480 = XInternAtom (dpyinfo->display, "NULL", False);
10481 dpyinfo->Xatom_ATOM_PAIR
10482 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10483 /* For properties of font. */
10484 dpyinfo->Xatom_PIXEL_SIZE
10485 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10486 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10487 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10488 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10489 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10490 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10491 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10493 /* Ghostscript support. */
10494 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10495 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10497 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10498 False);
10500 dpyinfo->cut_buffers_initialized = 0;
10502 connection = ConnectionNumber (dpyinfo->display);
10503 dpyinfo->connection = connection;
10506 char null_bits[1];
10508 null_bits[0] = 0x00;
10510 dpyinfo->null_pixel
10511 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10512 null_bits, 1, 1, (long) 0, (long) 0,
10517 extern int gray_bitmap_width, gray_bitmap_height;
10518 extern char *gray_bitmap_bits;
10519 dpyinfo->gray
10520 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10521 gray_bitmap_bits,
10522 gray_bitmap_width, gray_bitmap_height,
10523 (unsigned long) 1, (unsigned long) 0, 1);
10526 #ifdef HAVE_X_I18N
10527 xim_initialize (dpyinfo, resource_name);
10528 #endif
10530 #ifdef subprocesses
10531 /* This is only needed for distinguishing keyboard and process input. */
10532 if (connection != 0)
10533 add_keyboard_wait_descriptor (connection);
10534 #endif
10536 #ifndef F_SETOWN_BUG
10537 #ifdef F_SETOWN
10538 #ifdef F_SETOWN_SOCK_NEG
10539 /* stdin is a socket here */
10540 fcntl (connection, F_SETOWN, -getpid ());
10541 #else /* ! defined (F_SETOWN_SOCK_NEG) */
10542 fcntl (connection, F_SETOWN, getpid ());
10543 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
10544 #endif /* ! defined (F_SETOWN) */
10545 #endif /* F_SETOWN_BUG */
10547 #ifdef SIGIO
10548 if (interrupt_input)
10549 init_sigio (connection);
10550 #endif /* ! defined (SIGIO) */
10552 #ifdef USE_LUCID
10553 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
10554 /* Make sure that we have a valid font for dialog boxes
10555 so that Xt does not crash. */
10557 Display *dpy = dpyinfo->display;
10558 XrmValue d, fr, to;
10559 Font font;
10560 int count;
10562 d.addr = (XPointer)&dpy;
10563 d.size = sizeof (Display *);
10564 fr.addr = XtDefaultFont;
10565 fr.size = sizeof (XtDefaultFont);
10566 to.size = sizeof (Font *);
10567 to.addr = (XPointer)&font;
10568 count = x_catch_errors (dpy);
10569 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10570 abort ();
10571 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10572 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10573 x_uncatch_errors (dpy, count);
10575 #endif
10576 #endif
10578 /* See if we should run in synchronous mode. This is useful
10579 for debugging X code. */
10581 Lisp_Object value;
10582 value = display_x_get_resource (dpyinfo,
10583 build_string ("synchronous"),
10584 build_string ("Synchronous"),
10585 Qnil, Qnil);
10586 if (STRINGP (value)
10587 && (!strcmp (SDATA (value), "true")
10588 || !strcmp (SDATA (value), "on")))
10589 XSynchronize (dpyinfo->display, True);
10593 Lisp_Object value;
10594 value = display_x_get_resource (dpyinfo,
10595 build_string ("useXIM"),
10596 build_string ("UseXIM"),
10597 Qnil, Qnil);
10598 #ifdef USE_XIM
10599 if (STRINGP (value)
10600 && (!strcmp (XSTRING (value)->data, "false")
10601 || !strcmp (XSTRING (value)->data, "off")))
10602 use_xim = 0;
10603 #else
10604 if (STRINGP (value)
10605 && (!strcmp (XSTRING (value)->data, "true")
10606 || !strcmp (XSTRING (value)->data, "on")))
10607 use_xim = 1;
10608 #endif
10611 #ifdef HAVE_X_SM
10612 /* Only do this for the first display. */
10613 if (x_initialized == 1)
10614 x_session_initialize (dpyinfo);
10615 #endif
10617 UNBLOCK_INPUT;
10619 return dpyinfo;
10622 /* Get rid of display DPYINFO, assuming all frames are already gone,
10623 and without sending any more commands to the X server. */
10625 void
10626 x_delete_display (dpyinfo)
10627 struct x_display_info *dpyinfo;
10629 int i;
10631 delete_keyboard_wait_descriptor (dpyinfo->connection);
10633 /* Discard this display from x_display_name_list and x_display_list.
10634 We can't use Fdelq because that can quit. */
10635 if (! NILP (x_display_name_list)
10636 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10637 x_display_name_list = XCDR (x_display_name_list);
10638 else
10640 Lisp_Object tail;
10642 tail = x_display_name_list;
10643 while (CONSP (tail) && CONSP (XCDR (tail)))
10645 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10647 XSETCDR (tail, XCDR (XCDR (tail)));
10648 break;
10650 tail = XCDR (tail);
10654 if (next_noop_dpyinfo == dpyinfo)
10655 next_noop_dpyinfo = dpyinfo->next;
10657 if (x_display_list == dpyinfo)
10658 x_display_list = dpyinfo->next;
10659 else
10661 struct x_display_info *tail;
10663 for (tail = x_display_list; tail; tail = tail->next)
10664 if (tail->next == dpyinfo)
10665 tail->next = tail->next->next;
10668 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
10669 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
10670 XrmDestroyDatabase (dpyinfo->xrdb);
10671 #endif
10672 #endif
10673 #ifdef MULTI_KBOARD
10674 if (--dpyinfo->kboard->reference_count == 0)
10675 delete_kboard (dpyinfo->kboard);
10676 #endif
10677 #ifdef HAVE_X_I18N
10678 if (dpyinfo->xim)
10679 xim_close_dpy (dpyinfo);
10680 #endif
10682 /* Free the font names in the font table. */
10683 for (i = 0; i < dpyinfo->n_fonts; i++)
10684 if (dpyinfo->font_table[i].name)
10686 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
10687 xfree (dpyinfo->font_table[i].full_name);
10688 xfree (dpyinfo->font_table[i].name);
10691 if (dpyinfo->font_table->font_encoder)
10692 xfree (dpyinfo->font_table->font_encoder);
10694 xfree (dpyinfo->font_table);
10695 xfree (dpyinfo->x_id_name);
10696 xfree (dpyinfo->color_cells);
10697 xfree (dpyinfo);
10700 #ifdef USE_X_TOOLKIT
10702 /* Atimer callback function for TIMER. Called every 0.1s to process
10703 Xt timeouts, if needed. We must avoid calling XtAppPending as
10704 much as possible because that function does an implicit XFlush
10705 that slows us down. */
10707 static void
10708 x_process_timeouts (timer)
10709 struct atimer *timer;
10711 if (toolkit_scroll_bar_interaction || popup_activated ())
10713 BLOCK_INPUT;
10714 while (XtAppPending (Xt_app_con) & XtIMTimer)
10715 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10716 UNBLOCK_INPUT;
10720 #endif /* USE_X_TOOLKIT */
10723 /* Set up use of X before we make the first connection. */
10725 extern frame_parm_handler x_frame_parm_handlers[];
10727 static struct redisplay_interface x_redisplay_interface =
10729 x_frame_parm_handlers,
10730 x_produce_glyphs,
10731 x_write_glyphs,
10732 x_insert_glyphs,
10733 x_clear_end_of_line,
10734 x_scroll_run,
10735 x_after_update_window_line,
10736 x_update_window_begin,
10737 x_update_window_end,
10738 x_cursor_to,
10739 x_flush,
10740 #ifndef XFlush
10741 x_flush,
10742 #else
10743 0, /* flush_display_optional */
10744 #endif
10745 x_clear_window_mouse_face,
10746 x_get_glyph_overhangs,
10747 x_fix_overlapping_area,
10748 x_draw_fringe_bitmap,
10749 0, /* define_fringe_bitmap */
10750 0, /* destroy_fringe_bitmap */
10751 x_per_char_metric,
10752 x_encode_char,
10753 x_compute_glyph_string_overhangs,
10754 x_draw_glyph_string,
10755 x_define_frame_cursor,
10756 x_clear_frame_area,
10757 x_draw_window_cursor,
10758 x_draw_vertical_window_border,
10759 x_shift_glyphs_for_insert
10762 void
10763 x_initialize ()
10765 rif = &x_redisplay_interface;
10767 clear_frame_hook = x_clear_frame;
10768 ins_del_lines_hook = x_ins_del_lines;
10769 delete_glyphs_hook = x_delete_glyphs;
10770 ring_bell_hook = XTring_bell;
10771 reset_terminal_modes_hook = XTreset_terminal_modes;
10772 set_terminal_modes_hook = XTset_terminal_modes;
10773 update_begin_hook = x_update_begin;
10774 update_end_hook = x_update_end;
10775 set_terminal_window_hook = XTset_terminal_window;
10776 read_socket_hook = XTread_socket;
10777 frame_up_to_date_hook = XTframe_up_to_date;
10778 mouse_position_hook = XTmouse_position;
10779 frame_rehighlight_hook = XTframe_rehighlight;
10780 frame_raise_lower_hook = XTframe_raise_lower;
10781 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10782 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10783 redeem_scroll_bar_hook = XTredeem_scroll_bar;
10784 judge_scroll_bars_hook = XTjudge_scroll_bars;
10786 scroll_region_ok = 1; /* we'll scroll partial frames */
10787 char_ins_del_ok = 1;
10788 line_ins_del_ok = 1; /* we'll just blt 'em */
10789 fast_clear_end_of_line = 1; /* X does this well */
10790 memory_below_frame = 0; /* we don't remember what scrolls
10791 off the bottom */
10792 baud_rate = 19200;
10794 x_noop_count = 0;
10795 last_tool_bar_item = -1;
10796 any_help_event_p = 0;
10797 ignore_next_mouse_click_timeout = 0;
10799 #ifdef USE_GTK
10800 current_count = -1;
10801 #endif
10803 /* Try to use interrupt input; if we can't, then start polling. */
10804 Fset_input_mode (Qt, Qnil, Qt, Qnil);
10806 #ifdef USE_X_TOOLKIT
10807 XtToolkitInitialize ();
10809 Xt_app_con = XtCreateApplicationContext ();
10811 /* Register a converter from strings to pixels, which uses
10812 Emacs' color allocation infrastructure. */
10813 XtAppSetTypeConverter (Xt_app_con,
10814 XtRString, XtRPixel, cvt_string_to_pixel,
10815 cvt_string_to_pixel_args,
10816 XtNumber (cvt_string_to_pixel_args),
10817 XtCacheByDisplay, cvt_pixel_dtor);
10819 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10821 /* Install an asynchronous timer that processes Xt timeout events
10822 every 0.1s. This is necessary because some widget sets use
10823 timeouts internally, for example the LessTif menu bar, or the
10824 Xaw3d scroll bar. When Xt timouts aren't processed, these
10825 widgets don't behave normally. */
10827 EMACS_TIME interval;
10828 EMACS_SET_SECS_USECS (interval, 0, 100000);
10829 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
10831 #endif
10833 #ifdef USE_TOOLKIT_SCROLL_BARS
10834 #ifndef USE_GTK
10835 xaw3d_arrow_scroll = False;
10836 xaw3d_pick_top = True;
10837 #endif
10838 #endif
10840 /* Note that there is no real way portable across R3/R4 to get the
10841 original error handler. */
10842 XSetErrorHandler (x_error_handler);
10843 XSetIOErrorHandler (x_io_error_quitter);
10845 /* Disable Window Change signals; they are handled by X events. */
10846 #ifdef SIGWINCH
10847 signal (SIGWINCH, SIG_DFL);
10848 #endif /* SIGWINCH */
10850 signal (SIGPIPE, x_connection_signal);
10854 void
10855 syms_of_xterm ()
10857 staticpro (&x_error_message_string);
10858 x_error_message_string = Qnil;
10860 staticpro (&x_display_name_list);
10861 x_display_name_list = Qnil;
10863 staticpro (&last_mouse_scroll_bar);
10864 last_mouse_scroll_bar = Qnil;
10866 staticpro (&Qvendor_specific_keysyms);
10867 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10869 staticpro (&Qutf_8);
10870 Qutf_8 = intern ("utf-8");
10871 staticpro (&Qlatin_1);
10872 Qlatin_1 = intern ("latin-1");
10874 staticpro (&last_mouse_press_frame);
10875 last_mouse_press_frame = Qnil;
10877 DEFVAR_BOOL ("x-use-underline-position-properties",
10878 &x_use_underline_position_properties,
10879 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10880 nil means ignore them. If you encounter fonts with bogus
10881 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10882 to 4.1, set this to nil. */);
10883 x_use_underline_position_properties = 1;
10885 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10886 &x_mouse_click_focus_ignore_position,
10887 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10888 This variable is only used when the window manager requires that you
10889 click on a frame to select it (give it focus). In that case, a value
10890 of nil, means that the selected window and cursor position changes to
10891 reflect the mouse click position, while a non-nil value means that the
10892 selected window or cursor position is preserved. */);
10893 x_mouse_click_focus_ignore_position = 0;
10895 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10896 doc: /* What X toolkit scroll bars Emacs uses.
10897 A value of nil means Emacs doesn't use X toolkit scroll bars.
10898 Otherwise, value is a symbol describing the X toolkit. */);
10899 #ifdef USE_TOOLKIT_SCROLL_BARS
10900 #ifdef USE_MOTIF
10901 Vx_toolkit_scroll_bars = intern ("motif");
10902 #elif defined HAVE_XAW3D
10903 Vx_toolkit_scroll_bars = intern ("xaw3d");
10904 #elif USE_GTK
10905 Vx_toolkit_scroll_bars = intern ("gtk");
10906 #else
10907 Vx_toolkit_scroll_bars = intern ("xaw");
10908 #endif
10909 #else
10910 Vx_toolkit_scroll_bars = Qnil;
10911 #endif
10913 staticpro (&last_mouse_motion_frame);
10914 last_mouse_motion_frame = Qnil;
10916 Qmodifier_value = intern ("modifier-value");
10917 Qalt = intern ("alt");
10918 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10919 Qhyper = intern ("hyper");
10920 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10921 Qmeta = intern ("meta");
10922 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10923 Qsuper = intern ("super");
10924 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10926 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10927 doc: /* Which keys Emacs uses for the alt modifier.
10928 This should be one of the symbols `alt', `hyper', `meta', `super'.
10929 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10930 is nil, which is the same as `alt'. */);
10931 Vx_alt_keysym = Qnil;
10933 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
10934 doc: /* Which keys Emacs uses for the hyper modifier.
10935 This should be one of the symbols `alt', `hyper', `meta', `super'.
10936 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10937 default is nil, which is the same as `hyper'. */);
10938 Vx_hyper_keysym = Qnil;
10940 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
10941 doc: /* Which keys Emacs uses for the meta modifier.
10942 This should be one of the symbols `alt', `hyper', `meta', `super'.
10943 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10944 default is nil, which is the same as `meta'. */);
10945 Vx_meta_keysym = Qnil;
10947 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
10948 doc: /* Which keys Emacs uses for the super modifier.
10949 This should be one of the symbols `alt', `hyper', `meta', `super'.
10950 For example, `super' means use the Super_L and Super_R keysyms. The
10951 default is nil, which is the same as `super'. */);
10952 Vx_super_keysym = Qnil;
10954 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
10955 doc: /* Hash table of character codes indexed by X keysym codes. */);
10956 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
10957 make_float (DEFAULT_REHASH_SIZE),
10958 make_float (DEFAULT_REHASH_THRESHOLD),
10959 Qnil, Qnil, Qnil);
10962 #endif /* HAVE_X_WINDOWS */
10964 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
10965 (do not change this comment) */