(Buffer Internals): Describe new auto_save_file_format field of the
[emacs.git] / src / xterm.c
blobc1bfd11985e743649068441de2954679d5da2546
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,03,04
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 - s->slice.x;
2361 xgcv.clip_y_origin = y - s->slice.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;
3263 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3264 if (! frame)
3265 return;
3267 switch (event->type)
3269 case EnterNotify:
3270 case LeaveNotify:
3272 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3273 int focus_state
3274 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3276 if (event->xcrossing.detail != NotifyInferior
3277 && event->xcrossing.focus
3278 && ! (focus_state & FOCUS_EXPLICIT))
3279 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3280 FOCUS_IMPLICIT,
3281 dpyinfo, frame, bufp);
3283 break;
3285 case FocusIn:
3286 case FocusOut:
3287 x_focus_changed (event->type,
3288 (event->xfocus.detail == NotifyPointer ?
3289 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3290 dpyinfo, frame, bufp);
3291 break;
3296 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3298 void
3299 x_mouse_leave (dpyinfo)
3300 struct x_display_info *dpyinfo;
3302 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3305 /* The focus has changed, or we have redirected a frame's focus to
3306 another frame (this happens when a frame uses a surrogate
3307 mini-buffer frame). Shift the highlight as appropriate.
3309 The FRAME argument doesn't necessarily have anything to do with which
3310 frame is being highlighted or un-highlighted; we only use it to find
3311 the appropriate X display info. */
3313 static void
3314 XTframe_rehighlight (frame)
3315 struct frame *frame;
3317 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3320 static void
3321 x_frame_rehighlight (dpyinfo)
3322 struct x_display_info *dpyinfo;
3324 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3326 if (dpyinfo->x_focus_frame)
3328 dpyinfo->x_highlight_frame
3329 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3330 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3331 : dpyinfo->x_focus_frame);
3332 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3334 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3335 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3338 else
3339 dpyinfo->x_highlight_frame = 0;
3341 if (dpyinfo->x_highlight_frame != old_highlight)
3343 if (old_highlight)
3344 frame_unhighlight (old_highlight);
3345 if (dpyinfo->x_highlight_frame)
3346 frame_highlight (dpyinfo->x_highlight_frame);
3352 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3354 /* Initialize mode_switch_bit and modifier_meaning. */
3355 static void
3356 x_find_modifier_meanings (dpyinfo)
3357 struct x_display_info *dpyinfo;
3359 int min_code, max_code;
3360 KeySym *syms;
3361 int syms_per_code;
3362 XModifierKeymap *mods;
3364 dpyinfo->meta_mod_mask = 0;
3365 dpyinfo->shift_lock_mask = 0;
3366 dpyinfo->alt_mod_mask = 0;
3367 dpyinfo->super_mod_mask = 0;
3368 dpyinfo->hyper_mod_mask = 0;
3370 #ifdef HAVE_X11R4
3371 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3372 #else
3373 min_code = dpyinfo->display->min_keycode;
3374 max_code = dpyinfo->display->max_keycode;
3375 #endif
3377 syms = XGetKeyboardMapping (dpyinfo->display,
3378 min_code, max_code - min_code + 1,
3379 &syms_per_code);
3380 mods = XGetModifierMapping (dpyinfo->display);
3382 /* Scan the modifier table to see which modifier bits the Meta and
3383 Alt keysyms are on. */
3385 int row, col; /* The row and column in the modifier table. */
3387 for (row = 3; row < 8; row++)
3388 for (col = 0; col < mods->max_keypermod; col++)
3390 KeyCode code
3391 = mods->modifiermap[(row * mods->max_keypermod) + col];
3393 /* Zeroes are used for filler. Skip them. */
3394 if (code == 0)
3395 continue;
3397 /* Are any of this keycode's keysyms a meta key? */
3399 int code_col;
3401 for (code_col = 0; code_col < syms_per_code; code_col++)
3403 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3405 switch (sym)
3407 case XK_Meta_L:
3408 case XK_Meta_R:
3409 dpyinfo->meta_mod_mask |= (1 << row);
3410 break;
3412 case XK_Alt_L:
3413 case XK_Alt_R:
3414 dpyinfo->alt_mod_mask |= (1 << row);
3415 break;
3417 case XK_Hyper_L:
3418 case XK_Hyper_R:
3419 dpyinfo->hyper_mod_mask |= (1 << row);
3420 break;
3422 case XK_Super_L:
3423 case XK_Super_R:
3424 dpyinfo->super_mod_mask |= (1 << row);
3425 break;
3427 case XK_Shift_Lock:
3428 /* Ignore this if it's not on the lock modifier. */
3429 if ((1 << row) == LockMask)
3430 dpyinfo->shift_lock_mask = LockMask;
3431 break;
3438 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3439 if (! dpyinfo->meta_mod_mask)
3441 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3442 dpyinfo->alt_mod_mask = 0;
3445 /* If some keys are both alt and meta,
3446 make them just meta, not alt. */
3447 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3449 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3452 XFree ((char *) syms);
3453 XFreeModifiermap (mods);
3456 /* Convert between the modifier bits X uses and the modifier bits
3457 Emacs uses. */
3459 static unsigned int
3460 x_x_to_emacs_modifiers (dpyinfo, state)
3461 struct x_display_info *dpyinfo;
3462 unsigned int state;
3464 EMACS_UINT mod_meta = meta_modifier;
3465 EMACS_UINT mod_alt = alt_modifier;
3466 EMACS_UINT mod_hyper = hyper_modifier;
3467 EMACS_UINT mod_super = super_modifier;
3468 Lisp_Object tem;
3470 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3471 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3472 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3473 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3474 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3475 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3476 tem = Fget (Vx_super_keysym, Qmodifier_value);
3477 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3480 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3481 | ((state & ControlMask) ? ctrl_modifier : 0)
3482 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3483 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3484 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3485 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3488 static unsigned int
3489 x_emacs_to_x_modifiers (dpyinfo, state)
3490 struct x_display_info *dpyinfo;
3491 unsigned int state;
3493 EMACS_UINT mod_meta = meta_modifier;
3494 EMACS_UINT mod_alt = alt_modifier;
3495 EMACS_UINT mod_hyper = hyper_modifier;
3496 EMACS_UINT mod_super = super_modifier;
3498 Lisp_Object tem;
3500 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3501 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3502 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3503 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3504 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3505 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3506 tem = Fget (Vx_super_keysym, Qmodifier_value);
3507 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3510 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3511 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3512 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3513 | ((state & shift_modifier) ? ShiftMask : 0)
3514 | ((state & ctrl_modifier) ? ControlMask : 0)
3515 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3518 /* Convert a keysym to its name. */
3520 char *
3521 x_get_keysym_name (keysym)
3522 KeySym keysym;
3524 char *value;
3526 BLOCK_INPUT;
3527 value = XKeysymToString (keysym);
3528 UNBLOCK_INPUT;
3530 return value;
3535 /* Mouse clicks and mouse movement. Rah. */
3537 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3539 If the event is a button press, then note that we have grabbed
3540 the mouse. */
3542 static Lisp_Object
3543 construct_mouse_click (result, event, f)
3544 struct input_event *result;
3545 XButtonEvent *event;
3546 struct frame *f;
3548 /* Make the event type NO_EVENT; we'll change that when we decide
3549 otherwise. */
3550 result->kind = MOUSE_CLICK_EVENT;
3551 result->code = event->button - Button1;
3552 result->timestamp = event->time;
3553 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3554 event->state)
3555 | (event->type == ButtonRelease
3556 ? up_modifier
3557 : down_modifier));
3559 XSETINT (result->x, event->x);
3560 XSETINT (result->y, event->y);
3561 XSETFRAME (result->frame_or_window, f);
3562 result->arg = Qnil;
3563 return Qnil;
3567 /* Function to report a mouse movement to the mainstream Emacs code.
3568 The input handler calls this.
3570 We have received a mouse movement event, which is given in *event.
3571 If the mouse is over a different glyph than it was last time, tell
3572 the mainstream emacs code by setting mouse_moved. If not, ask for
3573 another motion event, so we can check again the next time it moves. */
3575 static XMotionEvent last_mouse_motion_event;
3576 static Lisp_Object last_mouse_motion_frame;
3578 static void
3579 note_mouse_movement (frame, event)
3580 FRAME_PTR frame;
3581 XMotionEvent *event;
3583 last_mouse_movement_time = event->time;
3584 last_mouse_motion_event = *event;
3585 XSETFRAME (last_mouse_motion_frame, frame);
3587 if (event->window != FRAME_X_WINDOW (frame))
3589 frame->mouse_moved = 1;
3590 last_mouse_scroll_bar = Qnil;
3591 note_mouse_highlight (frame, -1, -1);
3594 /* Has the mouse moved off the glyph it was on at the last sighting? */
3595 else if (event->x < last_mouse_glyph.x
3596 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3597 || event->y < last_mouse_glyph.y
3598 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3600 frame->mouse_moved = 1;
3601 last_mouse_scroll_bar = Qnil;
3602 note_mouse_highlight (frame, event->x, event->y);
3607 /************************************************************************
3608 Mouse Face
3609 ************************************************************************/
3611 static void
3612 redo_mouse_highlight ()
3614 if (!NILP (last_mouse_motion_frame)
3615 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3616 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3617 last_mouse_motion_event.x,
3618 last_mouse_motion_event.y);
3622 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
3625 /* Try to determine frame pixel position and size of the glyph under
3626 frame pixel coordinates X/Y on frame F . Return the position and
3627 size in *RECT. Value is non-zero if we could compute these
3628 values. */
3630 static int
3631 glyph_rect (f, x, y, rect)
3632 struct frame *f;
3633 int x, y;
3634 XRectangle *rect;
3636 Lisp_Object window;
3637 struct window *w;
3638 struct glyph_row *r, *end_row;
3640 window = window_from_coordinates (f, x, y, 0, &x, &y, 0);
3641 if (NILP (window))
3642 return 0;
3644 w = XWINDOW (window);
3645 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
3646 end_row = r + w->current_matrix->nrows - 1;
3648 for (; r < end_row && r->enabled_p; ++r)
3650 if (r->y >= y)
3652 struct glyph *g = r->glyphs[TEXT_AREA];
3653 struct glyph *end = g + r->used[TEXT_AREA];
3654 int gx = r->x;
3655 while (g < end && gx < x)
3656 gx += g->pixel_width, ++g;
3657 if (g < end)
3659 rect->width = g->pixel_width;
3660 rect->height = r->height;
3661 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
3662 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
3663 return 1;
3665 break;
3669 return 0;
3673 /* Return the current position of the mouse.
3674 *FP should be a frame which indicates which display to ask about.
3676 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3677 and *PART to the frame, window, and scroll bar part that the mouse
3678 is over. Set *X and *Y to the portion and whole of the mouse's
3679 position on the scroll bar.
3681 If the mouse movement started elsewhere, set *FP to the frame the
3682 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3683 the mouse is over.
3685 Set *TIME to the server time-stamp for the time at which the mouse
3686 was at this position.
3688 Don't store anything if we don't have a valid set of values to report.
3690 This clears the mouse_moved flag, so we can wait for the next mouse
3691 movement. */
3693 static void
3694 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3695 FRAME_PTR *fp;
3696 int insist;
3697 Lisp_Object *bar_window;
3698 enum scroll_bar_part *part;
3699 Lisp_Object *x, *y;
3700 unsigned long *time;
3702 FRAME_PTR f1;
3704 BLOCK_INPUT;
3706 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3707 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3708 else
3710 Window root;
3711 int root_x, root_y;
3713 Window dummy_window;
3714 int dummy;
3716 Lisp_Object frame, tail;
3718 /* Clear the mouse-moved flag for every frame on this display. */
3719 FOR_EACH_FRAME (tail, frame)
3720 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3721 XFRAME (frame)->mouse_moved = 0;
3723 last_mouse_scroll_bar = Qnil;
3725 /* Figure out which root window we're on. */
3726 XQueryPointer (FRAME_X_DISPLAY (*fp),
3727 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3729 /* The root window which contains the pointer. */
3730 &root,
3732 /* Trash which we can't trust if the pointer is on
3733 a different screen. */
3734 &dummy_window,
3736 /* The position on that root window. */
3737 &root_x, &root_y,
3739 /* More trash we can't trust. */
3740 &dummy, &dummy,
3742 /* Modifier keys and pointer buttons, about which
3743 we don't care. */
3744 (unsigned int *) &dummy);
3746 /* Now we have a position on the root; find the innermost window
3747 containing the pointer. */
3749 Window win, child;
3750 int win_x, win_y;
3751 int parent_x = 0, parent_y = 0;
3752 int count;
3754 win = root;
3756 /* XTranslateCoordinates can get errors if the window
3757 structure is changing at the same time this function
3758 is running. So at least we must not crash from them. */
3760 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
3762 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3763 && FRAME_LIVE_P (last_mouse_frame))
3765 /* If mouse was grabbed on a frame, give coords for that frame
3766 even if the mouse is now outside it. */
3767 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3769 /* From-window, to-window. */
3770 root, FRAME_X_WINDOW (last_mouse_frame),
3772 /* From-position, to-position. */
3773 root_x, root_y, &win_x, &win_y,
3775 /* Child of win. */
3776 &child);
3777 f1 = last_mouse_frame;
3779 else
3781 while (1)
3783 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3785 /* From-window, to-window. */
3786 root, win,
3788 /* From-position, to-position. */
3789 root_x, root_y, &win_x, &win_y,
3791 /* Child of win. */
3792 &child);
3794 if (child == None || child == win)
3795 break;
3797 win = child;
3798 parent_x = win_x;
3799 parent_y = win_y;
3802 /* Now we know that:
3803 win is the innermost window containing the pointer
3804 (XTC says it has no child containing the pointer),
3805 win_x and win_y are the pointer's position in it
3806 (XTC did this the last time through), and
3807 parent_x and parent_y are the pointer's position in win's parent.
3808 (They are what win_x and win_y were when win was child.
3809 If win is the root window, it has no parent, and
3810 parent_{x,y} are invalid, but that's okay, because we'll
3811 never use them in that case.) */
3813 /* Is win one of our frames? */
3814 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3816 #ifdef USE_X_TOOLKIT
3817 /* If we end up with the menu bar window, say it's not
3818 on the frame. */
3819 if (f1 != NULL
3820 && f1->output_data.x->menubar_widget
3821 && win == XtWindow (f1->output_data.x->menubar_widget))
3822 f1 = NULL;
3823 #endif /* USE_X_TOOLKIT */
3826 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3827 f1 = 0;
3829 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
3831 /* If not, is it one of our scroll bars? */
3832 if (! f1)
3834 struct scroll_bar *bar;
3836 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3838 if (bar)
3840 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3841 win_x = parent_x;
3842 win_y = parent_y;
3846 if (f1 == 0 && insist > 0)
3847 f1 = SELECTED_FRAME ();
3849 if (f1)
3851 /* Ok, we found a frame. Store all the values.
3852 last_mouse_glyph is a rectangle used to reduce the
3853 generation of mouse events. To not miss any motion
3854 events, we must divide the frame into rectangles of the
3855 size of the smallest character that could be displayed
3856 on it, i.e. into the same rectangles that matrices on
3857 the frame are divided into. */
3859 int width, height, gx, gy;
3860 XRectangle rect;
3862 if (glyph_rect (f1, win_x, win_y, &rect))
3863 last_mouse_glyph = rect;
3864 else
3866 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
3867 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
3868 gx = win_x;
3869 gy = win_y;
3871 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
3872 round down even for negative values. */
3873 if (gx < 0)
3874 gx -= width - 1;
3875 if (gy < 0)
3876 gy -= height - 1;
3877 gx = (gx + width - 1) / width * width;
3878 gy = (gy + height - 1) / height * height;
3880 last_mouse_glyph.width = width;
3881 last_mouse_glyph.height = height;
3882 last_mouse_glyph.x = gx;
3883 last_mouse_glyph.y = gy;
3886 *bar_window = Qnil;
3887 *part = 0;
3888 *fp = f1;
3889 XSETINT (*x, win_x);
3890 XSETINT (*y, win_y);
3891 *time = last_mouse_movement_time;
3896 UNBLOCK_INPUT;
3901 /***********************************************************************
3902 Scroll bars
3903 ***********************************************************************/
3905 /* Scroll bar support. */
3907 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
3908 manages it.
3909 This can be called in GC, so we have to make sure to strip off mark
3910 bits. */
3912 static struct scroll_bar *
3913 x_window_to_scroll_bar (display, window_id)
3914 Display *display;
3915 Window window_id;
3917 Lisp_Object tail;
3919 #ifdef USE_GTK
3920 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
3921 #endif /* USE_GTK */
3923 for (tail = Vframe_list;
3924 XGCTYPE (tail) == Lisp_Cons;
3925 tail = XCDR (tail))
3927 Lisp_Object frame, bar, condemned;
3929 frame = XCAR (tail);
3930 /* All elements of Vframe_list should be frames. */
3931 if (! GC_FRAMEP (frame))
3932 abort ();
3934 /* Scan this frame's scroll bar list for a scroll bar with the
3935 right window ID. */
3936 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3937 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3938 /* This trick allows us to search both the ordinary and
3939 condemned scroll bar lists with one loop. */
3940 ! GC_NILP (bar) || (bar = condemned,
3941 condemned = Qnil,
3942 ! GC_NILP (bar));
3943 bar = XSCROLL_BAR (bar)->next)
3944 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id &&
3945 FRAME_X_DISPLAY (XFRAME (frame)) == display)
3946 return XSCROLL_BAR (bar);
3949 return 0;
3953 #if defined USE_LUCID
3955 /* Return the Lucid menu bar WINDOW is part of. Return null
3956 if WINDOW is not part of a menu bar. */
3958 static Widget
3959 x_window_to_menu_bar (window)
3960 Window window;
3962 Lisp_Object tail;
3964 for (tail = Vframe_list;
3965 XGCTYPE (tail) == Lisp_Cons;
3966 tail = XCDR (tail))
3968 Lisp_Object frame = XCAR (tail);
3969 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
3971 if (menu_bar && xlwmenu_window_p (menu_bar, window))
3972 return menu_bar;
3975 return NULL;
3978 #endif /* USE_LUCID */
3981 /************************************************************************
3982 Toolkit scroll bars
3983 ************************************************************************/
3985 #ifdef USE_TOOLKIT_SCROLL_BARS
3987 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
3988 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
3989 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
3990 struct scroll_bar *));
3991 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
3992 int, int, int));
3995 /* Lisp window being scrolled. Set when starting to interact with
3996 a toolkit scroll bar, reset to nil when ending the interaction. */
3998 static Lisp_Object window_being_scrolled;
4000 /* Last scroll bar part sent in xm_scroll_callback. */
4002 static int last_scroll_bar_part;
4004 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4005 that movements of 1/20 of the screen size are mapped to up/down. */
4007 #ifndef USE_GTK
4008 /* Id of action hook installed for scroll bars. */
4010 static XtActionHookId action_hook_id;
4012 static Boolean xaw3d_arrow_scroll;
4014 /* Whether the drag scrolling maintains the mouse at the top of the
4015 thumb. If not, resizing the thumb needs to be done more carefully
4016 to avoid jerkyness. */
4018 static Boolean xaw3d_pick_top;
4020 extern void set_vertical_scroll_bar P_ ((struct window *));
4022 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4023 bars are used.. The hook is responsible for detecting when
4024 the user ends an interaction with the scroll bar, and generates
4025 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4027 static void
4028 xt_action_hook (widget, client_data, action_name, event, params,
4029 num_params)
4030 Widget widget;
4031 XtPointer client_data;
4032 String action_name;
4033 XEvent *event;
4034 String *params;
4035 Cardinal *num_params;
4037 int scroll_bar_p;
4038 char *end_action;
4040 #ifdef USE_MOTIF
4041 scroll_bar_p = XmIsScrollBar (widget);
4042 end_action = "Release";
4043 #else /* !USE_MOTIF i.e. use Xaw */
4044 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4045 end_action = "EndScroll";
4046 #endif /* USE_MOTIF */
4048 if (scroll_bar_p
4049 && strcmp (action_name, end_action) == 0
4050 && WINDOWP (window_being_scrolled))
4052 struct window *w;
4054 x_send_scroll_bar_event (window_being_scrolled,
4055 scroll_bar_end_scroll, 0, 0);
4056 w = XWINDOW (window_being_scrolled);
4058 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4060 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4061 /* The thumb size is incorrect while dragging: fix it. */
4062 set_vertical_scroll_bar (w);
4064 window_being_scrolled = Qnil;
4065 last_scroll_bar_part = -1;
4067 /* Xt timeouts no longer needed. */
4068 toolkit_scroll_bar_interaction = 0;
4071 #endif /* not USE_GTK */
4073 /* A vector of windows used for communication between
4074 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4076 static struct window **scroll_bar_windows;
4077 static int scroll_bar_windows_size;
4080 /* Send a client message with message type Xatom_Scrollbar for a
4081 scroll action to the frame of WINDOW. PART is a value identifying
4082 the part of the scroll bar that was clicked on. PORTION is the
4083 amount to scroll of a whole of WHOLE. */
4085 static void
4086 x_send_scroll_bar_event (window, part, portion, whole)
4087 Lisp_Object window;
4088 int part, portion, whole;
4090 XEvent event;
4091 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4092 struct window *w = XWINDOW (window);
4093 struct frame *f = XFRAME (w->frame);
4094 int i;
4096 BLOCK_INPUT;
4098 /* Construct a ClientMessage event to send to the frame. */
4099 ev->type = ClientMessage;
4100 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4101 ev->display = FRAME_X_DISPLAY (f);
4102 ev->window = FRAME_X_WINDOW (f);
4103 ev->format = 32;
4105 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4106 not enough to store a pointer or Lisp_Object on a 64 bit system.
4107 So, store the window in scroll_bar_windows and pass the index
4108 into that array in the event. */
4109 for (i = 0; i < scroll_bar_windows_size; ++i)
4110 if (scroll_bar_windows[i] == NULL)
4111 break;
4113 if (i == scroll_bar_windows_size)
4115 int new_size = max (10, 2 * scroll_bar_windows_size);
4116 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4117 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4119 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4120 nbytes);
4121 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4122 scroll_bar_windows_size = new_size;
4125 scroll_bar_windows[i] = w;
4126 ev->data.l[0] = (long) i;
4127 ev->data.l[1] = (long) part;
4128 ev->data.l[2] = (long) 0;
4129 ev->data.l[3] = (long) portion;
4130 ev->data.l[4] = (long) whole;
4132 /* Make Xt timeouts work while the scroll bar is active. */
4133 toolkit_scroll_bar_interaction = 1;
4135 /* Setting the event mask to zero means that the message will
4136 be sent to the client that created the window, and if that
4137 window no longer exists, no event will be sent. */
4138 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4139 UNBLOCK_INPUT;
4143 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4144 in *IEVENT. */
4146 static void
4147 x_scroll_bar_to_input_event (event, ievent)
4148 XEvent *event;
4149 struct input_event *ievent;
4151 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4152 Lisp_Object window;
4153 struct frame *f;
4154 struct window *w;
4156 w = scroll_bar_windows[ev->data.l[0]];
4157 scroll_bar_windows[ev->data.l[0]] = NULL;
4159 XSETWINDOW (window, w);
4160 f = XFRAME (w->frame);
4162 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4163 ievent->frame_or_window = window;
4164 ievent->arg = Qnil;
4165 #ifdef USE_GTK
4166 ievent->timestamp = CurrentTime;
4167 #else
4168 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4169 #endif
4170 ievent->part = ev->data.l[1];
4171 ievent->code = ev->data.l[2];
4172 ievent->x = make_number ((int) ev->data.l[3]);
4173 ievent->y = make_number ((int) ev->data.l[4]);
4174 ievent->modifiers = 0;
4178 #ifdef USE_MOTIF
4180 /* Minimum and maximum values used for Motif scroll bars. */
4182 #define XM_SB_MAX 10000000
4185 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4186 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4187 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4189 static void
4190 xm_scroll_callback (widget, client_data, call_data)
4191 Widget widget;
4192 XtPointer client_data, call_data;
4194 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4195 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4196 int part = -1, whole = 0, portion = 0;
4198 switch (cs->reason)
4200 case XmCR_DECREMENT:
4201 bar->dragging = Qnil;
4202 part = scroll_bar_up_arrow;
4203 break;
4205 case XmCR_INCREMENT:
4206 bar->dragging = Qnil;
4207 part = scroll_bar_down_arrow;
4208 break;
4210 case XmCR_PAGE_DECREMENT:
4211 bar->dragging = Qnil;
4212 part = scroll_bar_above_handle;
4213 break;
4215 case XmCR_PAGE_INCREMENT:
4216 bar->dragging = Qnil;
4217 part = scroll_bar_below_handle;
4218 break;
4220 case XmCR_TO_TOP:
4221 bar->dragging = Qnil;
4222 part = scroll_bar_to_top;
4223 break;
4225 case XmCR_TO_BOTTOM:
4226 bar->dragging = Qnil;
4227 part = scroll_bar_to_bottom;
4228 break;
4230 case XmCR_DRAG:
4232 int slider_size;
4234 /* Get the slider size. */
4235 BLOCK_INPUT;
4236 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4237 UNBLOCK_INPUT;
4239 whole = XM_SB_MAX - slider_size;
4240 portion = min (cs->value, whole);
4241 part = scroll_bar_handle;
4242 bar->dragging = make_number (cs->value);
4244 break;
4246 case XmCR_VALUE_CHANGED:
4247 break;
4250 if (part >= 0)
4252 window_being_scrolled = bar->window;
4253 last_scroll_bar_part = part;
4254 x_send_scroll_bar_event (bar->window, part, portion, whole);
4259 #else /* !USE_MOTIF, i.e. Xaw or GTK */
4260 #ifdef USE_GTK
4261 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4262 bar widget. DATA is a pointer to the scroll_bar structure. */
4264 static void
4265 xg_scroll_callback (widget, data)
4266 GtkRange *widget;
4267 gpointer data;
4269 struct scroll_bar *bar = (struct scroll_bar *) data;
4270 gdouble previous;
4271 gdouble position;
4272 gdouble *p;
4273 int diff;
4275 int part = -1, whole = 0, portion = 0;
4276 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4278 if (xg_ignore_gtk_scrollbar) return;
4280 position = gtk_adjustment_get_value (adj);
4282 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4283 if (! p)
4285 p = (gdouble*) xmalloc (sizeof (gdouble));
4286 *p = XG_SB_MIN;
4287 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4290 previous = *p;
4291 *p = position;
4293 diff = (int) (position - previous);
4295 if (diff == (int) adj->step_increment)
4297 part = scroll_bar_down_arrow;
4298 bar->dragging = Qnil;
4300 else if (-diff == (int) adj->step_increment)
4302 part = scroll_bar_up_arrow;
4303 bar->dragging = Qnil;
4305 else if (diff == (int) adj->page_increment)
4307 part = scroll_bar_below_handle;
4308 bar->dragging = Qnil;
4310 else if (-diff == (int) adj->page_increment)
4312 part = scroll_bar_above_handle;
4313 bar->dragging = Qnil;
4315 else
4317 part = scroll_bar_handle;
4318 whole = adj->upper - adj->page_size;
4319 portion = min ((int)position, whole);
4320 bar->dragging = make_number ((int)portion);
4323 if (part >= 0)
4325 window_being_scrolled = bar->window;
4326 last_scroll_bar_part = part;
4327 x_send_scroll_bar_event (bar->window, part, portion, whole);
4331 #else /* not USE_GTK */
4333 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4334 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4335 scroll bar struct. CALL_DATA is a pointer to a float saying where
4336 the thumb is. */
4338 static void
4339 xaw_jump_callback (widget, client_data, call_data)
4340 Widget widget;
4341 XtPointer client_data, call_data;
4343 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4344 float top = *(float *) call_data;
4345 float shown;
4346 int whole, portion, height;
4347 int part;
4349 /* Get the size of the thumb, a value between 0 and 1. */
4350 BLOCK_INPUT;
4351 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4352 UNBLOCK_INPUT;
4354 whole = 10000000;
4355 portion = shown < 1 ? top * whole : 0;
4357 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
4358 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4359 the bottom, so we force the scrolling whenever we see that we're
4360 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4361 we try to ensure that we always stay two pixels away from the
4362 bottom). */
4363 part = scroll_bar_down_arrow;
4364 else
4365 part = scroll_bar_handle;
4367 window_being_scrolled = bar->window;
4368 bar->dragging = make_number (portion);
4369 last_scroll_bar_part = part;
4370 x_send_scroll_bar_event (bar->window, part, portion, whole);
4374 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4375 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4376 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4377 the scroll bar. CALL_DATA is an integer specifying the action that
4378 has taken place. Its magnitude is in the range 0..height of the
4379 scroll bar. Negative values mean scroll towards buffer start.
4380 Values < height of scroll bar mean line-wise movement. */
4382 static void
4383 xaw_scroll_callback (widget, client_data, call_data)
4384 Widget widget;
4385 XtPointer client_data, call_data;
4387 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4388 /* The position really is stored cast to a pointer. */
4389 int position = (long) call_data;
4390 Dimension height;
4391 int part;
4393 /* Get the height of the scroll bar. */
4394 BLOCK_INPUT;
4395 XtVaGetValues (widget, XtNheight, &height, NULL);
4396 UNBLOCK_INPUT;
4398 if (abs (position) >= height)
4399 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4401 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4402 it maps line-movement to call_data = max(5, height/20). */
4403 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
4404 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4405 else
4406 part = scroll_bar_move_ratio;
4408 window_being_scrolled = bar->window;
4409 bar->dragging = Qnil;
4410 last_scroll_bar_part = part;
4411 x_send_scroll_bar_event (bar->window, part, position, height);
4414 #endif /* not USE_GTK */
4415 #endif /* not USE_MOTIF */
4417 #define SCROLL_BAR_NAME "verticalScrollBar"
4419 /* Create the widget for scroll bar BAR on frame F. Record the widget
4420 and X window of the scroll bar in BAR. */
4422 #ifdef USE_GTK
4423 static void
4424 x_create_toolkit_scroll_bar (f, bar)
4425 struct frame *f;
4426 struct scroll_bar *bar;
4428 char *scroll_bar_name = SCROLL_BAR_NAME;
4430 BLOCK_INPUT;
4431 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4432 scroll_bar_name);
4433 UNBLOCK_INPUT;
4436 #else /* not USE_GTK */
4438 static void
4439 x_create_toolkit_scroll_bar (f, bar)
4440 struct frame *f;
4441 struct scroll_bar *bar;
4443 Window xwindow;
4444 Widget widget;
4445 Arg av[20];
4446 int ac = 0;
4447 char *scroll_bar_name = SCROLL_BAR_NAME;
4448 unsigned long pixel;
4450 BLOCK_INPUT;
4452 #ifdef USE_MOTIF
4453 /* Set resources. Create the widget. */
4454 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4455 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4456 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4457 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4458 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4459 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4460 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4462 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4463 if (pixel != -1)
4465 XtSetArg (av[ac], XmNforeground, pixel);
4466 ++ac;
4469 pixel = f->output_data.x->scroll_bar_background_pixel;
4470 if (pixel != -1)
4472 XtSetArg (av[ac], XmNbackground, pixel);
4473 ++ac;
4476 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4477 scroll_bar_name, av, ac);
4479 /* Add one callback for everything that can happen. */
4480 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4481 (XtPointer) bar);
4482 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4483 (XtPointer) bar);
4484 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4485 (XtPointer) bar);
4486 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4487 (XtPointer) bar);
4488 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4489 (XtPointer) bar);
4490 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4491 (XtPointer) bar);
4492 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4493 (XtPointer) bar);
4495 /* Realize the widget. Only after that is the X window created. */
4496 XtRealizeWidget (widget);
4498 /* Set the cursor to an arrow. I didn't find a resource to do that.
4499 And I'm wondering why it hasn't an arrow cursor by default. */
4500 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4501 f->output_data.x->nontext_cursor);
4503 #else /* !USE_MOTIF i.e. use Xaw */
4505 /* Set resources. Create the widget. The background of the
4506 Xaw3d scroll bar widget is a little bit light for my taste.
4507 We don't alter it here to let users change it according
4508 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4509 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4510 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4511 /* For smoother scrolling with Xaw3d -sm */
4512 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4514 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4515 if (pixel != -1)
4517 XtSetArg (av[ac], XtNforeground, pixel);
4518 ++ac;
4521 pixel = f->output_data.x->scroll_bar_background_pixel;
4522 if (pixel != -1)
4524 XtSetArg (av[ac], XtNbackground, pixel);
4525 ++ac;
4528 /* Top/bottom shadow colors. */
4530 /* Allocate them, if necessary. */
4531 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4533 pixel = f->output_data.x->scroll_bar_background_pixel;
4534 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4535 &pixel, 1.2, 0x8000))
4536 pixel = -1;
4537 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4539 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4541 pixel = f->output_data.x->scroll_bar_background_pixel;
4542 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4543 &pixel, 0.6, 0x4000))
4544 pixel = -1;
4545 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4548 /* Tell the toolkit about them. */
4549 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4550 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4551 /* We tried to allocate a color for the top/bottom shadow, and
4552 failed, so tell Xaw3d to use dithering instead. */
4554 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4555 ++ac;
4557 else
4558 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4559 be more consistent with other emacs 3d colors, and since Xaw3d is
4560 not good at dealing with allocation failure. */
4562 /* This tells Xaw3d to use real colors instead of dithering for
4563 the shadows. */
4564 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4565 ++ac;
4567 /* Specify the colors. */
4568 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4569 if (pixel != -1)
4571 XtSetArg (av[ac], "topShadowPixel", pixel);
4572 ++ac;
4574 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4575 if (pixel != -1)
4577 XtSetArg (av[ac], "bottomShadowPixel", pixel);
4578 ++ac;
4582 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4583 f->output_data.x->edit_widget, av, ac);
4586 char *initial = "";
4587 char *val = initial;
4588 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4589 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4590 if (val == initial)
4591 { /* ARROW_SCROLL */
4592 xaw3d_arrow_scroll = True;
4593 /* Isn't that just a personal preference ? -sm */
4594 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4598 /* Define callbacks. */
4599 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4600 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4601 (XtPointer) bar);
4603 /* Realize the widget. Only after that is the X window created. */
4604 XtRealizeWidget (widget);
4606 #endif /* !USE_MOTIF */
4608 /* Install an action hook that lets us detect when the user
4609 finishes interacting with a scroll bar. */
4610 if (action_hook_id == 0)
4611 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4613 /* Remember X window and widget in the scroll bar vector. */
4614 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4615 xwindow = XtWindow (widget);
4616 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
4618 UNBLOCK_INPUT;
4620 #endif /* not USE_GTK */
4623 /* Set the thumb size and position of scroll bar BAR. We are currently
4624 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4626 #ifdef USE_GTK
4627 static void
4628 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4629 struct scroll_bar *bar;
4630 int portion, position, whole;
4632 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4635 #else /* not USE_GTK */
4636 static void
4637 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4638 struct scroll_bar *bar;
4639 int portion, position, whole;
4641 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4642 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4643 float top, shown;
4645 BLOCK_INPUT;
4647 #ifdef USE_MOTIF
4649 /* We use an estimate of 30 chars per line rather than the real
4650 `portion' value. This has the disadvantage that the thumb size
4651 is not very representative, but it makes our life a lot easier.
4652 Otherwise, we have to constantly adjust the thumb size, which
4653 we can't always do quickly enough: while dragging, the size of
4654 the thumb might prevent the user from dragging the thumb all the
4655 way to the end. but Motif and some versions of Xaw3d don't allow
4656 updating the thumb size while dragging. Also, even if we can update
4657 its size, the update will often happen too late.
4658 If you don't believe it, check out revision 1.650 of xterm.c to see
4659 what hoops we were going through and the still poor behavior we got. */
4660 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4661 /* When the thumb is at the bottom, position == whole.
4662 So we need to increase `whole' to make space for the thumb. */
4663 whole += portion;
4665 if (whole <= 0)
4666 top = 0, shown = 1;
4667 else
4669 top = (float) position / whole;
4670 shown = (float) portion / whole;
4673 if (NILP (bar->dragging))
4675 int size, value;
4677 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4678 is the scroll bar's maximum and MIN is the scroll bar's minimum
4679 value. */
4680 size = shown * XM_SB_MAX;
4681 size = min (size, XM_SB_MAX);
4682 size = max (size, 1);
4684 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4685 value = top * XM_SB_MAX;
4686 value = min (value, XM_SB_MAX - size);
4688 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4690 #else /* !USE_MOTIF i.e. use Xaw */
4692 if (whole == 0)
4693 top = 0, shown = 1;
4694 else
4696 top = (float) position / whole;
4697 shown = (float) portion / whole;
4701 float old_top, old_shown;
4702 Dimension height;
4703 XtVaGetValues (widget,
4704 XtNtopOfThumb, &old_top,
4705 XtNshown, &old_shown,
4706 XtNheight, &height,
4707 NULL);
4709 /* Massage the top+shown values. */
4710 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4711 top = max (0, min (1, top));
4712 else
4713 top = old_top;
4714 /* Keep two pixels available for moving the thumb down. */
4715 shown = max (0, min (1 - top - (2.0 / height), shown));
4717 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4718 check that your system's configuration file contains a define
4719 for `NARROWPROTO'. See s/freebsd.h for an example. */
4720 if (top != old_top || shown != old_shown)
4722 if (NILP (bar->dragging))
4723 XawScrollbarSetThumb (widget, top, shown);
4724 else
4726 #ifdef HAVE_XAW3D
4727 ScrollbarWidget sb = (ScrollbarWidget) widget;
4728 int scroll_mode = 0;
4730 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
4731 if (xaw3d_arrow_scroll)
4733 /* Xaw3d stupidly ignores resize requests while dragging
4734 so we have to make it believe it's not in dragging mode. */
4735 scroll_mode = sb->scrollbar.scroll_mode;
4736 if (scroll_mode == 2)
4737 sb->scrollbar.scroll_mode = 0;
4739 #endif
4740 /* Try to make the scrolling a tad smoother. */
4741 if (!xaw3d_pick_top)
4742 shown = min (shown, old_shown);
4744 XawScrollbarSetThumb (widget, top, shown);
4746 #ifdef HAVE_XAW3D
4747 if (xaw3d_arrow_scroll && scroll_mode == 2)
4748 sb->scrollbar.scroll_mode = scroll_mode;
4749 #endif
4753 #endif /* !USE_MOTIF */
4755 UNBLOCK_INPUT;
4757 #endif /* not USE_GTK */
4759 #endif /* USE_TOOLKIT_SCROLL_BARS */
4763 /************************************************************************
4764 Scroll bars, general
4765 ************************************************************************/
4767 /* Create a scroll bar and return the scroll bar vector for it. W is
4768 the Emacs window on which to create the scroll bar. TOP, LEFT,
4769 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4770 scroll bar. */
4772 static struct scroll_bar *
4773 x_scroll_bar_create (w, top, left, width, height)
4774 struct window *w;
4775 int top, left, width, height;
4777 struct frame *f = XFRAME (w->frame);
4778 struct scroll_bar *bar
4779 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
4781 BLOCK_INPUT;
4783 #ifdef USE_TOOLKIT_SCROLL_BARS
4784 x_create_toolkit_scroll_bar (f, bar);
4785 #else /* not USE_TOOLKIT_SCROLL_BARS */
4787 XSetWindowAttributes a;
4788 unsigned long mask;
4789 Window window;
4791 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4792 if (a.background_pixel == -1)
4793 a.background_pixel = f->output_data.x->background_pixel;
4795 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4796 | ButtonMotionMask | PointerMotionHintMask
4797 | ExposureMask);
4798 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4800 mask = (CWBackPixel | CWEventMask | CWCursor);
4802 /* Clear the area of W that will serve as a scroll bar. This is
4803 for the case that a window has been split horizontally. In
4804 this case, no clear_frame is generated to reduce flickering. */
4805 if (width > 0 && height > 0)
4806 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4807 left, top, width,
4808 window_box_height (w), False);
4810 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4811 /* Position and size of scroll bar. */
4812 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4813 top,
4814 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4815 height,
4816 /* Border width, depth, class, and visual. */
4818 CopyFromParent,
4819 CopyFromParent,
4820 CopyFromParent,
4821 /* Attributes. */
4822 mask, &a);
4823 SET_SCROLL_BAR_X_WINDOW (bar, window);
4825 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4827 XSETWINDOW (bar->window, w);
4828 XSETINT (bar->top, top);
4829 XSETINT (bar->left, left);
4830 XSETINT (bar->width, width);
4831 XSETINT (bar->height, height);
4832 XSETINT (bar->start, 0);
4833 XSETINT (bar->end, 0);
4834 bar->dragging = Qnil;
4836 /* Add bar to its frame's list of scroll bars. */
4837 bar->next = FRAME_SCROLL_BARS (f);
4838 bar->prev = Qnil;
4839 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4840 if (!NILP (bar->next))
4841 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4843 /* Map the window/widget. */
4844 #ifdef USE_TOOLKIT_SCROLL_BARS
4846 #ifdef USE_GTK
4847 xg_update_scrollbar_pos (f,
4848 SCROLL_BAR_X_WINDOW (bar),
4849 top,
4850 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4851 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4852 max (height, 1),
4853 left,
4854 width);
4855 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
4856 #else /* not USE_GTK */
4857 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4858 XtConfigureWidget (scroll_bar,
4859 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4860 top,
4861 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4862 max (height, 1), 0);
4863 XtMapWidget (scroll_bar);
4864 #endif /* not USE_GTK */
4866 #else /* not USE_TOOLKIT_SCROLL_BARS */
4867 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4868 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4870 UNBLOCK_INPUT;
4871 return bar;
4875 /* Draw BAR's handle in the proper position.
4877 If the handle is already drawn from START to END, don't bother
4878 redrawing it, unless REBUILD is non-zero; in that case, always
4879 redraw it. (REBUILD is handy for drawing the handle after expose
4880 events.)
4882 Normally, we want to constrain the start and end of the handle to
4883 fit inside its rectangle, but if the user is dragging the scroll
4884 bar handle, we want to let them drag it down all the way, so that
4885 the bar's top is as far down as it goes; otherwise, there's no way
4886 to move to the very end of the buffer. */
4888 #ifndef USE_TOOLKIT_SCROLL_BARS
4890 static void
4891 x_scroll_bar_set_handle (bar, start, end, rebuild)
4892 struct scroll_bar *bar;
4893 int start, end;
4894 int rebuild;
4896 int dragging = ! NILP (bar->dragging);
4897 Window w = SCROLL_BAR_X_WINDOW (bar);
4898 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4899 GC gc = f->output_data.x->normal_gc;
4901 /* If the display is already accurate, do nothing. */
4902 if (! rebuild
4903 && start == XINT (bar->start)
4904 && end == XINT (bar->end))
4905 return;
4907 BLOCK_INPUT;
4910 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
4911 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
4912 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4914 /* Make sure the values are reasonable, and try to preserve
4915 the distance between start and end. */
4917 int length = end - start;
4919 if (start < 0)
4920 start = 0;
4921 else if (start > top_range)
4922 start = top_range;
4923 end = start + length;
4925 if (end < start)
4926 end = start;
4927 else if (end > top_range && ! dragging)
4928 end = top_range;
4931 /* Store the adjusted setting in the scroll bar. */
4932 XSETINT (bar->start, start);
4933 XSETINT (bar->end, end);
4935 /* Clip the end position, just for display. */
4936 if (end > top_range)
4937 end = top_range;
4939 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
4940 below top positions, to make sure the handle is always at least
4941 that many pixels tall. */
4942 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
4944 /* Draw the empty space above the handle. Note that we can't clear
4945 zero-height areas; that means "clear to end of window." */
4946 if (0 < start)
4947 x_clear_area (FRAME_X_DISPLAY (f), w,
4948 /* x, y, width, height, and exposures. */
4949 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4950 VERTICAL_SCROLL_BAR_TOP_BORDER,
4951 inside_width, start,
4952 False);
4954 /* Change to proper foreground color if one is specified. */
4955 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4956 XSetForeground (FRAME_X_DISPLAY (f), gc,
4957 f->output_data.x->scroll_bar_foreground_pixel);
4959 /* Draw the handle itself. */
4960 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
4961 /* x, y, width, height */
4962 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4963 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
4964 inside_width, end - start);
4966 /* Restore the foreground color of the GC if we changed it above. */
4967 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4968 XSetForeground (FRAME_X_DISPLAY (f), gc,
4969 f->output_data.x->foreground_pixel);
4971 /* Draw the empty space below the handle. Note that we can't
4972 clear zero-height areas; that means "clear to end of window." */
4973 if (end < inside_height)
4974 x_clear_area (FRAME_X_DISPLAY (f), w,
4975 /* x, y, width, height, and exposures. */
4976 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4977 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
4978 inside_width, inside_height - end,
4979 False);
4983 UNBLOCK_INPUT;
4986 #endif /* !USE_TOOLKIT_SCROLL_BARS */
4988 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
4989 nil. */
4991 static void
4992 x_scroll_bar_remove (bar)
4993 struct scroll_bar *bar;
4995 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4996 BLOCK_INPUT;
4998 #ifdef USE_TOOLKIT_SCROLL_BARS
4999 #ifdef USE_GTK
5000 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
5001 #else /* not USE_GTK */
5002 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5003 #endif /* not USE_GTK */
5004 #else
5005 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
5006 #endif
5008 /* Disassociate this scroll bar from its window. */
5009 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5011 UNBLOCK_INPUT;
5015 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5016 that we are displaying PORTION characters out of a total of WHOLE
5017 characters, starting at POSITION. If WINDOW has no scroll bar,
5018 create one. */
5020 static void
5021 XTset_vertical_scroll_bar (w, portion, whole, position)
5022 struct window *w;
5023 int portion, whole, position;
5025 struct frame *f = XFRAME (w->frame);
5026 struct scroll_bar *bar;
5027 int top, height, left, sb_left, width, sb_width;
5028 int window_y, window_height;
5030 /* Get window dimensions. */
5031 window_box (w, -1, 0, &window_y, 0, &window_height);
5032 top = window_y;
5033 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5034 height = window_height;
5036 /* Compute the left edge of the scroll bar area. */
5037 left = WINDOW_SCROLL_BAR_AREA_X (w);
5039 /* Compute the width of the scroll bar which might be less than
5040 the width of the area reserved for the scroll bar. */
5041 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5042 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5043 else
5044 sb_width = width;
5046 /* Compute the left edge of the scroll bar. */
5047 #ifdef USE_TOOLKIT_SCROLL_BARS
5048 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5049 sb_left = left + width - sb_width - (width - sb_width) / 2;
5050 else
5051 sb_left = left + (width - sb_width) / 2;
5052 #else
5053 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5054 sb_left = left + width - sb_width;
5055 else
5056 sb_left = left;
5057 #endif
5059 /* Does the scroll bar exist yet? */
5060 if (NILP (w->vertical_scroll_bar))
5062 if (width > 0 && height > 0)
5064 BLOCK_INPUT;
5065 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5066 left, top, width, height, False);
5067 UNBLOCK_INPUT;
5070 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5072 else
5074 /* It may just need to be moved and resized. */
5075 unsigned int mask = 0;
5077 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5079 BLOCK_INPUT;
5081 if (sb_left != XINT (bar->left))
5082 mask |= CWX;
5083 if (top != XINT (bar->top))
5084 mask |= CWY;
5085 if (sb_width != XINT (bar->width))
5086 mask |= CWWidth;
5087 if (height != XINT (bar->height))
5088 mask |= CWHeight;
5090 #ifdef USE_TOOLKIT_SCROLL_BARS
5092 #ifdef USE_GTK
5093 if (mask)
5094 xg_update_scrollbar_pos (f,
5095 SCROLL_BAR_X_WINDOW (bar),
5096 top,
5097 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5098 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5099 max (height, 1),
5100 left,
5101 width);
5102 #else /* not USE_GTK */
5104 /* Since toolkit scroll bars are smaller than the space reserved
5105 for them on the frame, we have to clear "under" them. */
5106 if (width > 0 && height > 0)
5107 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5108 left, top, width, height, False);
5109 /* Move/size the scroll bar widget. */
5110 if (mask)
5111 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5112 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5113 top,
5114 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5115 max (height, 1), 0);
5117 #endif /* not USE_GTK */
5118 #else /* not USE_TOOLKIT_SCROLL_BARS */
5120 /* Clear areas not covered by the scroll bar because of
5121 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5122 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5124 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5125 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5126 height, False);
5127 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5128 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5129 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5130 height, False);
5133 /* Clear areas not covered by the scroll bar because it's not as
5134 wide as the area reserved for it. This makes sure a
5135 previous mode line display is cleared after C-x 2 C-x 1, for
5136 example. */
5138 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5139 int rest = area_width - sb_width;
5140 if (rest > 0 && height > 0)
5142 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5143 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5144 left + area_width - rest, top,
5145 rest, height, False);
5146 else
5147 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5148 left, top, rest, height, False);
5152 /* Move/size the scroll bar window. */
5153 if (mask)
5155 XWindowChanges wc;
5157 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5158 wc.y = top;
5159 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5160 wc.height = height;
5161 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
5162 mask, &wc);
5165 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5167 /* Remember new settings. */
5168 XSETINT (bar->left, sb_left);
5169 XSETINT (bar->top, top);
5170 XSETINT (bar->width, sb_width);
5171 XSETINT (bar->height, height);
5173 UNBLOCK_INPUT;
5176 #ifdef USE_TOOLKIT_SCROLL_BARS
5177 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5178 #else /* not USE_TOOLKIT_SCROLL_BARS */
5179 /* Set the scroll bar's current state, unless we're currently being
5180 dragged. */
5181 if (NILP (bar->dragging))
5183 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5185 if (whole == 0)
5186 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5187 else
5189 int start = ((double) position * top_range) / whole;
5190 int end = ((double) (position + portion) * top_range) / whole;
5191 x_scroll_bar_set_handle (bar, start, end, 0);
5194 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5196 XSETVECTOR (w->vertical_scroll_bar, bar);
5200 /* The following three hooks are used when we're doing a thorough
5201 redisplay of the frame. We don't explicitly know which scroll bars
5202 are going to be deleted, because keeping track of when windows go
5203 away is a real pain - "Can you say set-window-configuration, boys
5204 and girls?" Instead, we just assert at the beginning of redisplay
5205 that *all* scroll bars are to be removed, and then save a scroll bar
5206 from the fiery pit when we actually redisplay its window. */
5208 /* Arrange for all scroll bars on FRAME to be removed at the next call
5209 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5210 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5212 static void
5213 XTcondemn_scroll_bars (frame)
5214 FRAME_PTR frame;
5216 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5217 while (! NILP (FRAME_SCROLL_BARS (frame)))
5219 Lisp_Object bar;
5220 bar = FRAME_SCROLL_BARS (frame);
5221 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5222 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5223 XSCROLL_BAR (bar)->prev = Qnil;
5224 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5225 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5226 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5231 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5232 Note that WINDOW isn't necessarily condemned at all. */
5234 static void
5235 XTredeem_scroll_bar (window)
5236 struct window *window;
5238 struct scroll_bar *bar;
5239 struct frame *f;
5241 /* We can't redeem this window's scroll bar if it doesn't have one. */
5242 if (NILP (window->vertical_scroll_bar))
5243 abort ();
5245 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5247 /* Unlink it from the condemned list. */
5248 f = XFRAME (WINDOW_FRAME (window));
5249 if (NILP (bar->prev))
5251 /* If the prev pointer is nil, it must be the first in one of
5252 the lists. */
5253 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5254 /* It's not condemned. Everything's fine. */
5255 return;
5256 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5257 window->vertical_scroll_bar))
5258 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5259 else
5260 /* If its prev pointer is nil, it must be at the front of
5261 one or the other! */
5262 abort ();
5264 else
5265 XSCROLL_BAR (bar->prev)->next = bar->next;
5267 if (! NILP (bar->next))
5268 XSCROLL_BAR (bar->next)->prev = bar->prev;
5270 bar->next = FRAME_SCROLL_BARS (f);
5271 bar->prev = Qnil;
5272 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5273 if (! NILP (bar->next))
5274 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5277 /* Remove all scroll bars on FRAME that haven't been saved since the
5278 last call to `*condemn_scroll_bars_hook'. */
5280 static void
5281 XTjudge_scroll_bars (f)
5282 FRAME_PTR f;
5284 Lisp_Object bar, next;
5286 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5288 /* Clear out the condemned list now so we won't try to process any
5289 more events on the hapless scroll bars. */
5290 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5292 for (; ! NILP (bar); bar = next)
5294 struct scroll_bar *b = XSCROLL_BAR (bar);
5296 x_scroll_bar_remove (b);
5298 next = b->next;
5299 b->next = b->prev = Qnil;
5302 /* Now there should be no references to the condemned scroll bars,
5303 and they should get garbage-collected. */
5307 #ifndef USE_TOOLKIT_SCROLL_BARS
5308 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5309 is a no-op when using toolkit scroll bars.
5311 This may be called from a signal handler, so we have to ignore GC
5312 mark bits. */
5314 static void
5315 x_scroll_bar_expose (bar, event)
5316 struct scroll_bar *bar;
5317 XEvent *event;
5319 Window w = SCROLL_BAR_X_WINDOW (bar);
5320 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5321 GC gc = f->output_data.x->normal_gc;
5322 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5324 BLOCK_INPUT;
5326 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
5328 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5329 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5331 /* x, y, width, height */
5332 0, 0,
5333 XINT (bar->width) - 1 - width_trim - width_trim,
5334 XINT (bar->height) - 1);
5336 UNBLOCK_INPUT;
5339 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5341 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5342 is set to something other than NO_EVENT, it is enqueued.
5344 This may be called from a signal handler, so we have to ignore GC
5345 mark bits. */
5348 static void
5349 x_scroll_bar_handle_click (bar, event, emacs_event)
5350 struct scroll_bar *bar;
5351 XEvent *event;
5352 struct input_event *emacs_event;
5354 if (! GC_WINDOWP (bar->window))
5355 abort ();
5357 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5358 emacs_event->code = event->xbutton.button - Button1;
5359 emacs_event->modifiers
5360 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5361 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5362 event->xbutton.state)
5363 | (event->type == ButtonRelease
5364 ? up_modifier
5365 : down_modifier));
5366 emacs_event->frame_or_window = bar->window;
5367 emacs_event->arg = Qnil;
5368 emacs_event->timestamp = event->xbutton.time;
5370 #if 0
5371 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5372 int internal_height
5373 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5374 #endif
5375 int top_range
5376 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5377 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5379 if (y < 0) y = 0;
5380 if (y > top_range) y = top_range;
5382 if (y < XINT (bar->start))
5383 emacs_event->part = scroll_bar_above_handle;
5384 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5385 emacs_event->part = scroll_bar_handle;
5386 else
5387 emacs_event->part = scroll_bar_below_handle;
5389 /* Just because the user has clicked on the handle doesn't mean
5390 they want to drag it. Lisp code needs to be able to decide
5391 whether or not we're dragging. */
5392 #if 0
5393 /* If the user has just clicked on the handle, record where they're
5394 holding it. */
5395 if (event->type == ButtonPress
5396 && emacs_event->part == scroll_bar_handle)
5397 XSETINT (bar->dragging, y - XINT (bar->start));
5398 #endif
5400 #ifndef USE_TOOLKIT_SCROLL_BARS
5401 /* If the user has released the handle, set it to its final position. */
5402 if (event->type == ButtonRelease
5403 && ! NILP (bar->dragging))
5405 int new_start = y - XINT (bar->dragging);
5406 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5408 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5409 bar->dragging = Qnil;
5411 #endif
5413 /* Same deal here as the other #if 0. */
5414 #if 0
5415 /* Clicks on the handle are always reported as occurring at the top of
5416 the handle. */
5417 if (emacs_event->part == scroll_bar_handle)
5418 emacs_event->x = bar->start;
5419 else
5420 XSETINT (emacs_event->x, y);
5421 #else
5422 XSETINT (emacs_event->x, y);
5423 #endif
5425 XSETINT (emacs_event->y, top_range);
5429 #ifndef USE_TOOLKIT_SCROLL_BARS
5431 /* Handle some mouse motion while someone is dragging the scroll bar.
5433 This may be called from a signal handler, so we have to ignore GC
5434 mark bits. */
5436 static void
5437 x_scroll_bar_note_movement (bar, event)
5438 struct scroll_bar *bar;
5439 XEvent *event;
5441 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5443 last_mouse_movement_time = event->xmotion.time;
5445 f->mouse_moved = 1;
5446 XSETVECTOR (last_mouse_scroll_bar, bar);
5448 /* If we're dragging the bar, display it. */
5449 if (! GC_NILP (bar->dragging))
5451 /* Where should the handle be now? */
5452 int new_start = event->xmotion.y - XINT (bar->dragging);
5454 if (new_start != XINT (bar->start))
5456 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5458 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5463 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5465 /* Return information to the user about the current position of the mouse
5466 on the scroll bar. */
5468 static void
5469 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5470 FRAME_PTR *fp;
5471 Lisp_Object *bar_window;
5472 enum scroll_bar_part *part;
5473 Lisp_Object *x, *y;
5474 unsigned long *time;
5476 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5477 Window w = SCROLL_BAR_X_WINDOW (bar);
5478 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5479 int win_x, win_y;
5480 Window dummy_window;
5481 int dummy_coord;
5482 unsigned int dummy_mask;
5484 BLOCK_INPUT;
5486 /* Get the mouse's position relative to the scroll bar window, and
5487 report that. */
5488 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5490 /* Root, child, root x and root y. */
5491 &dummy_window, &dummy_window,
5492 &dummy_coord, &dummy_coord,
5494 /* Position relative to scroll bar. */
5495 &win_x, &win_y,
5497 /* Mouse buttons and modifier keys. */
5498 &dummy_mask))
5500 else
5502 #if 0
5503 int inside_height
5504 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5505 #endif
5506 int top_range
5507 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5509 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5511 if (! NILP (bar->dragging))
5512 win_y -= XINT (bar->dragging);
5514 if (win_y < 0)
5515 win_y = 0;
5516 if (win_y > top_range)
5517 win_y = top_range;
5519 *fp = f;
5520 *bar_window = bar->window;
5522 if (! NILP (bar->dragging))
5523 *part = scroll_bar_handle;
5524 else if (win_y < XINT (bar->start))
5525 *part = scroll_bar_above_handle;
5526 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5527 *part = scroll_bar_handle;
5528 else
5529 *part = scroll_bar_below_handle;
5531 XSETINT (*x, win_y);
5532 XSETINT (*y, top_range);
5534 f->mouse_moved = 0;
5535 last_mouse_scroll_bar = Qnil;
5538 *time = last_mouse_movement_time;
5540 UNBLOCK_INPUT;
5544 /* The screen has been cleared so we may have changed foreground or
5545 background colors, and the scroll bars may need to be redrawn.
5546 Clear out the scroll bars, and ask for expose events, so we can
5547 redraw them. */
5549 void
5550 x_scroll_bar_clear (f)
5551 FRAME_PTR f;
5553 #ifndef USE_TOOLKIT_SCROLL_BARS
5554 Lisp_Object bar;
5556 /* We can have scroll bars even if this is 0,
5557 if we just turned off scroll bar mode.
5558 But in that case we should not clear them. */
5559 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5560 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5561 bar = XSCROLL_BAR (bar)->next)
5562 XClearArea (FRAME_X_DISPLAY (f),
5563 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
5564 0, 0, 0, 0, True);
5565 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5569 /* Define a queue to save up SelectionRequest events for later handling. */
5571 struct selection_event_queue
5573 XEvent event;
5574 struct selection_event_queue *next;
5577 static struct selection_event_queue *queue;
5579 /* Nonzero means queue up certain events--don't process them yet. */
5581 static int x_queue_selection_requests;
5583 /* Queue up an X event *EVENT, to be processed later. */
5585 static void
5586 x_queue_event (f, event)
5587 FRAME_PTR f;
5588 XEvent *event;
5590 struct selection_event_queue *queue_tmp
5591 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
5593 if (queue_tmp != NULL)
5595 queue_tmp->event = *event;
5596 queue_tmp->next = queue;
5597 queue = queue_tmp;
5601 /* Take all the queued events and put them back
5602 so that they get processed afresh. */
5604 static void
5605 x_unqueue_events (display)
5606 Display *display;
5608 while (queue != NULL)
5610 struct selection_event_queue *queue_tmp = queue;
5611 XPutBackEvent (display, &queue_tmp->event);
5612 queue = queue_tmp->next;
5613 xfree ((char *)queue_tmp);
5617 /* Start queuing SelectionRequest events. */
5619 void
5620 x_start_queuing_selection_requests (display)
5621 Display *display;
5623 x_queue_selection_requests++;
5626 /* Stop queuing SelectionRequest events. */
5628 void
5629 x_stop_queuing_selection_requests (display)
5630 Display *display;
5632 x_queue_selection_requests--;
5633 x_unqueue_events (display);
5636 /* The main X event-reading loop - XTread_socket. */
5638 #if 0
5639 /* Time stamp of enter window event. This is only used by XTread_socket,
5640 but we have to put it out here, since static variables within functions
5641 sometimes don't work. */
5643 static Time enter_timestamp;
5644 #endif
5646 /* This holds the state XLookupString needs to implement dead keys
5647 and other tricks known as "compose processing". _X Window System_
5648 says that a portable program can't use this, but Stephen Gildea assures
5649 me that letting the compiler initialize it to zeros will work okay.
5651 This must be defined outside of XTread_socket, for the same reasons
5652 given for enter_timestamp, above. */
5654 static XComposeStatus compose_status;
5656 /* Record the last 100 characters stored
5657 to help debug the loss-of-chars-during-GC problem. */
5659 static int temp_index;
5660 static short temp_buffer[100];
5662 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5663 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5664 temp_index = 0; \
5665 temp_buffer[temp_index++] = (keysym)
5667 /* Set this to nonzero to fake an "X I/O error"
5668 on a particular display. */
5670 struct x_display_info *XTread_socket_fake_io_error;
5672 /* When we find no input here, we occasionally do a no-op command
5673 to verify that the X server is still running and we can still talk with it.
5674 We try all the open displays, one by one.
5675 This variable is used for cycling thru the displays. */
5677 static struct x_display_info *next_noop_dpyinfo;
5679 #define SET_SAVED_MENU_EVENT(size) \
5680 do \
5682 if (f->output_data.x->saved_menu_event == 0) \
5683 f->output_data.x->saved_menu_event \
5684 = (XEvent *) xmalloc (sizeof (XEvent)); \
5685 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5686 inev.kind = MENU_BAR_ACTIVATE_EVENT; \
5687 XSETFRAME (inev.frame_or_window, f); \
5689 while (0)
5691 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5692 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5695 enum
5697 X_EVENT_NORMAL,
5698 X_EVENT_GOTO_OUT,
5699 X_EVENT_DROP
5702 /* Filter events for the current X input method.
5703 DPYINFO is the display this event is for.
5704 EVENT is the X event to filter.
5706 Returns non-zero if the event was filtered, caller shall not process
5707 this event further.
5708 Returns zero if event is wasn't filtered. */
5710 #ifdef HAVE_X_I18N
5711 static int
5712 x_filter_event (dpyinfo, event)
5713 struct x_display_info *dpyinfo;
5714 XEvent *event;
5716 /* XFilterEvent returns non-zero if the input method has
5717 consumed the event. We pass the frame's X window to
5718 XFilterEvent because that's the one for which the IC
5719 was created. */
5721 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5722 event->xclient.window);
5724 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5726 #endif
5728 #ifdef USE_GTK
5729 static int current_count;
5730 static int current_finish;
5731 static struct input_event *current_hold_quit;
5733 /* This is the filter function invoked by the GTK event loop.
5734 It is invoked before the XEvent is translated to a GdkEvent,
5735 so we have a chance to act on the event before GTK. */
5736 static GdkFilterReturn
5737 event_handler_gdk (gxev, ev, data)
5738 GdkXEvent *gxev;
5739 GdkEvent *ev;
5740 gpointer data;
5742 XEvent *xev = (XEvent *) gxev;
5744 if (current_count >= 0)
5746 struct x_display_info *dpyinfo;
5748 dpyinfo = x_display_info_for_display (xev->xany.display);
5750 #ifdef HAVE_X_I18N
5751 /* Filter events for the current X input method.
5752 GTK calls XFilterEvent but not for key press and release,
5753 so we do it here. */
5754 if (xev->type == KeyPress || xev->type == KeyRelease)
5755 if (dpyinfo && x_filter_event (dpyinfo, xev))
5756 return GDK_FILTER_REMOVE;
5757 #endif
5759 if (! dpyinfo)
5760 current_finish = X_EVENT_NORMAL;
5761 else
5763 current_count +=
5764 handle_one_xevent (dpyinfo, xev, &current_finish,
5765 current_hold_quit);
5768 else
5769 current_finish = x_dispatch_event (xev, xev->xany.display);
5771 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5772 return GDK_FILTER_REMOVE;
5774 return GDK_FILTER_CONTINUE;
5776 #endif /* USE_GTK */
5779 /* Handles the XEvent EVENT on display DPYINFO.
5781 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5782 *FINISH is zero if caller should continue reading events.
5783 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5785 We return the number of characters stored into the buffer. */
5787 static int
5788 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5789 struct x_display_info *dpyinfo;
5790 XEvent *eventp;
5791 int *finish;
5792 struct input_event *hold_quit;
5794 struct input_event inev;
5795 int count = 0;
5796 int do_help = 0;
5797 int nbytes = 0;
5798 struct frame *f;
5799 struct coding_system coding;
5800 XEvent event = *eventp;
5802 *finish = X_EVENT_NORMAL;
5804 EVENT_INIT (inev);
5805 inev.kind = NO_EVENT;
5806 inev.arg = Qnil;
5808 switch (event.type)
5810 case ClientMessage:
5812 if (event.xclient.message_type
5813 == dpyinfo->Xatom_wm_protocols
5814 && event.xclient.format == 32)
5816 if (event.xclient.data.l[0]
5817 == dpyinfo->Xatom_wm_take_focus)
5819 /* Use x_any_window_to_frame because this
5820 could be the shell widget window
5821 if the frame has no title bar. */
5822 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5823 #ifdef HAVE_X_I18N
5824 /* Not quite sure this is needed -pd */
5825 if (f && FRAME_XIC (f))
5826 XSetICFocus (FRAME_XIC (f));
5827 #endif
5828 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5829 instructs the WM to set the input focus automatically for
5830 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5831 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5832 it has set the focus. So, XSetInputFocus below is not
5833 needed.
5835 The call to XSetInputFocus below has also caused trouble. In
5836 cases where the XSetInputFocus done by the WM and the one
5837 below are temporally close (on a fast machine), the call
5838 below can generate additional FocusIn events which confuse
5839 Emacs. */
5841 /* Since we set WM_TAKE_FOCUS, we must call
5842 XSetInputFocus explicitly. But not if f is null,
5843 since that might be an event for a deleted frame. */
5844 if (f)
5846 Display *d = event.xclient.display;
5847 /* Catch and ignore errors, in case window has been
5848 iconified by a window manager such as GWM. */
5849 int count = x_catch_errors (d);
5850 XSetInputFocus (d, event.xclient.window,
5851 /* The ICCCM says this is
5852 the only valid choice. */
5853 RevertToParent,
5854 event.xclient.data.l[1]);
5855 /* This is needed to detect the error
5856 if there is an error. */
5857 XSync (d, False);
5858 x_uncatch_errors (d, count);
5860 /* Not certain about handling scroll bars here */
5861 #endif /* 0 */
5862 goto done;
5865 if (event.xclient.data.l[0]
5866 == dpyinfo->Xatom_wm_save_yourself)
5868 /* Save state modify the WM_COMMAND property to
5869 something which can reinstate us. This notifies
5870 the session manager, who's looking for such a
5871 PropertyNotify. Can restart processing when
5872 a keyboard or mouse event arrives. */
5873 /* If we have a session manager, don't set this.
5874 KDE will then start two Emacsen, one for the
5875 session manager and one for this. */
5876 #ifdef HAVE_X_SM
5877 if (! x_session_have_connection ())
5878 #endif
5880 f = x_top_window_to_frame (dpyinfo,
5881 event.xclient.window);
5882 /* This is just so we only give real data once
5883 for a single Emacs process. */
5884 if (f == SELECTED_FRAME ())
5885 XSetCommand (FRAME_X_DISPLAY (f),
5886 event.xclient.window,
5887 initial_argv, initial_argc);
5888 else if (f)
5889 XSetCommand (FRAME_X_DISPLAY (f),
5890 event.xclient.window,
5891 0, 0);
5893 goto done;
5896 if (event.xclient.data.l[0]
5897 == dpyinfo->Xatom_wm_delete_window)
5899 f = x_any_window_to_frame (dpyinfo,
5900 event.xclient.window);
5901 if (!f)
5902 goto OTHER; /* May be a dialog that is to be removed */
5904 inev.kind = DELETE_WINDOW_EVENT;
5905 XSETFRAME (inev.frame_or_window, f);
5906 goto done;
5909 goto done;
5912 if (event.xclient.message_type
5913 == dpyinfo->Xatom_wm_configure_denied)
5915 goto done;
5918 if (event.xclient.message_type
5919 == dpyinfo->Xatom_wm_window_moved)
5921 int new_x, new_y;
5922 f = x_window_to_frame (dpyinfo, event.xclient.window);
5924 new_x = event.xclient.data.s[0];
5925 new_y = event.xclient.data.s[1];
5927 if (f)
5929 f->left_pos = new_x;
5930 f->top_pos = new_y;
5932 goto done;
5935 #ifdef HACK_EDITRES
5936 if (event.xclient.message_type
5937 == dpyinfo->Xatom_editres)
5939 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5940 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5941 &event, NULL);
5942 goto done;
5944 #endif /* HACK_EDITRES */
5946 if ((event.xclient.message_type
5947 == dpyinfo->Xatom_DONE)
5948 || (event.xclient.message_type
5949 == dpyinfo->Xatom_PAGE))
5951 /* Ghostview job completed. Kill it. We could
5952 reply with "Next" if we received "Page", but we
5953 currently never do because we are interested in
5954 images, only, which should have 1 page. */
5955 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5956 f = x_window_to_frame (dpyinfo, event.xclient.window);
5957 x_kill_gs_process (pixmap, f);
5958 expose_frame (f, 0, 0, 0, 0);
5959 goto done;
5962 #ifdef USE_TOOLKIT_SCROLL_BARS
5963 /* Scroll bar callbacks send a ClientMessage from which
5964 we construct an input_event. */
5965 if (event.xclient.message_type
5966 == dpyinfo->Xatom_Scrollbar)
5968 x_scroll_bar_to_input_event (&event, &inev);
5969 *finish = X_EVENT_GOTO_OUT;
5970 goto done;
5972 #endif /* USE_TOOLKIT_SCROLL_BARS */
5974 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5976 if (!f)
5977 goto OTHER;
5979 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev))
5980 *finish = X_EVENT_DROP;
5982 break;
5984 case SelectionNotify:
5985 #ifdef USE_X_TOOLKIT
5986 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
5987 goto OTHER;
5988 #endif /* not USE_X_TOOLKIT */
5989 x_handle_selection_notify (&event.xselection);
5990 break;
5992 case SelectionClear: /* Someone has grabbed ownership. */
5993 #ifdef USE_X_TOOLKIT
5994 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
5995 goto OTHER;
5996 #endif /* USE_X_TOOLKIT */
5998 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
6000 inev.kind = SELECTION_CLEAR_EVENT;
6001 SELECTION_EVENT_DISPLAY (&inev) = eventp->display;
6002 SELECTION_EVENT_SELECTION (&inev) = eventp->selection;
6003 SELECTION_EVENT_TIME (&inev) = eventp->time;
6004 inev.frame_or_window = Qnil;
6006 break;
6008 case SelectionRequest: /* Someone wants our selection. */
6009 #ifdef USE_X_TOOLKIT
6010 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6011 goto OTHER;
6012 #endif /* USE_X_TOOLKIT */
6013 if (x_queue_selection_requests)
6014 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
6015 &event);
6016 else
6018 XSelectionRequestEvent *eventp
6019 = (XSelectionRequestEvent *) &event;
6021 inev.kind = SELECTION_REQUEST_EVENT;
6022 SELECTION_EVENT_DISPLAY (&inev) = eventp->display;
6023 SELECTION_EVENT_REQUESTOR (&inev) = eventp->requestor;
6024 SELECTION_EVENT_SELECTION (&inev) = eventp->selection;
6025 SELECTION_EVENT_TARGET (&inev) = eventp->target;
6026 SELECTION_EVENT_PROPERTY (&inev) = eventp->property;
6027 SELECTION_EVENT_TIME (&inev) = eventp->time;
6028 inev.frame_or_window = Qnil;
6030 break;
6032 case PropertyNotify:
6033 #if 0 /* This is plain wrong. In the case that we are waiting for a
6034 PropertyNotify used as an ACK in incremental selection
6035 transfer, the property will be on the receiver's window. */
6036 #if defined USE_X_TOOLKIT
6037 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
6038 goto OTHER;
6039 #endif
6040 #endif
6041 x_handle_property_notify (&event.xproperty);
6042 goto OTHER;
6044 case ReparentNotify:
6045 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6046 if (f)
6048 int x, y;
6049 f->output_data.x->parent_desc = event.xreparent.parent;
6050 x_real_positions (f, &x, &y);
6051 f->left_pos = x;
6052 f->top_pos = y;
6054 /* Perhaps reparented due to a WM restart. Reset this. */
6055 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6057 goto OTHER;
6059 case Expose:
6060 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6061 if (f)
6063 x_check_fullscreen (f);
6065 if (f->async_visible == 0)
6067 f->async_visible = 1;
6068 f->async_iconified = 0;
6069 f->output_data.x->has_been_visible = 1;
6070 SET_FRAME_GARBAGED (f);
6072 else
6073 expose_frame (f,
6074 event.xexpose.x, event.xexpose.y,
6075 event.xexpose.width, event.xexpose.height);
6077 else
6079 #ifndef USE_TOOLKIT_SCROLL_BARS
6080 struct scroll_bar *bar;
6081 #endif
6082 #if defined USE_LUCID
6083 /* Submenus of the Lucid menu bar aren't widgets
6084 themselves, so there's no way to dispatch events
6085 to them. Recognize this case separately. */
6087 Widget widget
6088 = x_window_to_menu_bar (event.xexpose.window);
6089 if (widget)
6090 xlwmenu_redisplay (widget);
6092 #endif /* USE_LUCID */
6094 #ifdef USE_TOOLKIT_SCROLL_BARS
6095 /* Dispatch event to the widget. */
6096 goto OTHER;
6097 #else /* not USE_TOOLKIT_SCROLL_BARS */
6098 bar = x_window_to_scroll_bar (event.xexpose.display,
6099 event.xexpose.window);
6101 if (bar)
6102 x_scroll_bar_expose (bar, &event);
6103 #ifdef USE_X_TOOLKIT
6104 else
6105 goto OTHER;
6106 #endif /* USE_X_TOOLKIT */
6107 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6109 break;
6111 case GraphicsExpose: /* This occurs when an XCopyArea's
6112 source area was obscured or not
6113 available. */
6114 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6115 if (f)
6117 expose_frame (f,
6118 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6119 event.xgraphicsexpose.width,
6120 event.xgraphicsexpose.height);
6122 #ifdef USE_X_TOOLKIT
6123 else
6124 goto OTHER;
6125 #endif /* USE_X_TOOLKIT */
6126 break;
6128 case NoExpose: /* This occurs when an XCopyArea's
6129 source area was completely
6130 available. */
6131 break;
6133 case UnmapNotify:
6134 /* Redo the mouse-highlight after the tooltip has gone. */
6135 if (event.xmap.window == tip_window)
6137 tip_window = 0;
6138 redo_mouse_highlight ();
6141 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6142 if (f) /* F may no longer exist if
6143 the frame was deleted. */
6145 /* While a frame is unmapped, display generation is
6146 disabled; you don't want to spend time updating a
6147 display that won't ever be seen. */
6148 f->async_visible = 0;
6149 /* We can't distinguish, from the event, whether the window
6150 has become iconified or invisible. So assume, if it
6151 was previously visible, than now it is iconified.
6152 But x_make_frame_invisible clears both
6153 the visible flag and the iconified flag;
6154 and that way, we know the window is not iconified now. */
6155 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6157 f->async_iconified = 1;
6159 inev.kind = ICONIFY_EVENT;
6160 XSETFRAME (inev.frame_or_window, f);
6163 goto OTHER;
6165 case MapNotify:
6166 if (event.xmap.window == tip_window)
6167 /* The tooltip has been drawn already. Avoid
6168 the SET_FRAME_GARBAGED below. */
6169 goto OTHER;
6171 /* We use x_top_window_to_frame because map events can
6172 come for sub-windows and they don't mean that the
6173 frame is visible. */
6174 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6175 if (f)
6177 /* wait_reading_process_output will notice this and update
6178 the frame's display structures.
6179 If we where iconified, we should not set garbaged,
6180 because that stops redrawing on Expose events. This looks
6181 bad if we are called from a recursive event loop
6182 (x_dispatch_event), for example when a dialog is up. */
6183 if (! f->async_iconified)
6184 SET_FRAME_GARBAGED (f);
6186 f->async_visible = 1;
6187 f->async_iconified = 0;
6188 f->output_data.x->has_been_visible = 1;
6190 if (f->iconified)
6192 inev.kind = DEICONIFY_EVENT;
6193 XSETFRAME (inev.frame_or_window, f);
6195 else if (! NILP (Vframe_list)
6196 && ! NILP (XCDR (Vframe_list)))
6197 /* Force a redisplay sooner or later
6198 to update the frame titles
6199 in case this is the second frame. */
6200 record_asynch_buffer_change ();
6202 goto OTHER;
6204 case KeyPress:
6206 ignore_next_mouse_click_timeout = 0;
6208 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6209 /* Dispatch KeyPress events when in menu. */
6210 if (popup_activated ())
6211 goto OTHER;
6212 #endif
6214 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6216 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
6218 clear_mouse_face (dpyinfo);
6219 dpyinfo->mouse_face_hidden = 1;
6222 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6223 if (f == 0)
6225 /* Scroll bars consume key events, but we want
6226 the keys to go to the scroll bar's frame. */
6227 Widget widget = XtWindowToWidget (dpyinfo->display,
6228 event.xkey.window);
6229 if (widget && XmIsScrollBar (widget))
6231 widget = XtParent (widget);
6232 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6235 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6237 if (f != 0)
6239 KeySym keysym, orig_keysym;
6240 /* al%imercury@uunet.uu.net says that making this 81
6241 instead of 80 fixed a bug whereby meta chars made
6242 his Emacs hang.
6244 It seems that some version of XmbLookupString has
6245 a bug of not returning XBufferOverflow in
6246 status_return even if the input is too long to
6247 fit in 81 bytes. So, we must prepare sufficient
6248 bytes for copy_buffer. 513 bytes (256 chars for
6249 two-byte character set) seems to be a fairly good
6250 approximation. -- 2000.8.10 handa@etl.go.jp */
6251 unsigned char copy_buffer[513];
6252 unsigned char *copy_bufptr = copy_buffer;
6253 int copy_bufsiz = sizeof (copy_buffer);
6254 int modifiers;
6255 Lisp_Object coding_system = Qlatin_1;
6256 Lisp_Object c;
6258 #ifdef USE_GTK
6259 /* Don't pass keys to GTK. A Tab will shift focus to the
6260 tool bar in GTK 2.4. Keys will still go to menus and
6261 dialogs because in that case popup_activated is TRUE
6262 (see above). */
6263 *finish = X_EVENT_DROP;
6264 #endif
6266 event.xkey.state
6267 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6268 extra_keyboard_modifiers);
6269 modifiers = event.xkey.state;
6271 /* This will have to go some day... */
6273 /* make_lispy_event turns chars into control chars.
6274 Don't do it here because XLookupString is too eager. */
6275 event.xkey.state &= ~ControlMask;
6276 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6277 | dpyinfo->super_mod_mask
6278 | dpyinfo->hyper_mod_mask
6279 | dpyinfo->alt_mod_mask);
6281 /* In case Meta is ComposeCharacter,
6282 clear its status. According to Markus Ehrnsperger
6283 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6284 this enables ComposeCharacter to work whether or
6285 not it is combined with Meta. */
6286 if (modifiers & dpyinfo->meta_mod_mask)
6287 bzero (&compose_status, sizeof (compose_status));
6289 #ifdef HAVE_X_I18N
6290 if (FRAME_XIC (f))
6292 Status status_return;
6294 coding_system = Vlocale_coding_system;
6295 nbytes = XmbLookupString (FRAME_XIC (f),
6296 &event.xkey, copy_bufptr,
6297 copy_bufsiz, &keysym,
6298 &status_return);
6299 if (status_return == XBufferOverflow)
6301 copy_bufsiz = nbytes + 1;
6302 copy_bufptr = (char *) alloca (copy_bufsiz);
6303 nbytes = XmbLookupString (FRAME_XIC (f),
6304 &event.xkey, copy_bufptr,
6305 copy_bufsiz, &keysym,
6306 &status_return);
6308 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6309 #if 0 && defined X_HAVE_UTF8_STRING
6310 else if (status_return == XLookupKeySym)
6311 { /* Try again but with utf-8. */
6312 coding_system = Qutf_8;
6313 nbytes = Xutf8LookupString (FRAME_XIC (f),
6314 &event.xkey, copy_bufptr,
6315 copy_bufsiz, &keysym,
6316 &status_return);
6317 if (status_return == XBufferOverflow)
6319 copy_bufsiz = nbytes + 1;
6320 copy_bufptr = (char *) alloca (copy_bufsiz);
6321 nbytes = Xutf8LookupString (FRAME_XIC (f),
6322 &event.xkey,
6323 copy_bufptr,
6324 copy_bufsiz, &keysym,
6325 &status_return);
6328 #endif
6330 if (status_return == XLookupNone)
6331 break;
6332 else if (status_return == XLookupChars)
6334 keysym = NoSymbol;
6335 modifiers = 0;
6337 else if (status_return != XLookupKeySym
6338 && status_return != XLookupBoth)
6339 abort ();
6341 else
6342 nbytes = XLookupString (&event.xkey, copy_bufptr,
6343 copy_bufsiz, &keysym,
6344 &compose_status);
6345 #else
6346 nbytes = XLookupString (&event.xkey, copy_bufptr,
6347 copy_bufsiz, &keysym,
6348 &compose_status);
6349 #endif
6351 /* If not using XIM/XIC, and a compose sequence is in progress,
6352 we break here. Otherwise, chars_matched is always 0. */
6353 if (compose_status.chars_matched > 0 && nbytes == 0)
6354 break;
6356 orig_keysym = keysym;
6358 /* Common for all keysym input events. */
6359 XSETFRAME (inev.frame_or_window, f);
6360 inev.modifiers
6361 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6362 inev.timestamp = event.xkey.time;
6364 /* First deal with keysyms which have defined
6365 translations to characters. */
6366 if (keysym >= 32 && keysym < 128)
6367 /* Avoid explicitly decoding each ASCII character. */
6369 inev.kind = ASCII_KEYSTROKE_EVENT;
6370 inev.code = keysym;
6371 goto done_keysym;
6374 /* Now non-ASCII. */
6375 if (HASH_TABLE_P (Vx_keysym_table)
6376 && (NATNUMP (c = Fgethash (make_number (keysym),
6377 Vx_keysym_table,
6378 Qnil))))
6380 inev.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6381 ? ASCII_KEYSTROKE_EVENT
6382 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6383 inev.code = XFASTINT (c);
6384 goto done_keysym;
6387 /* Random non-modifier sorts of keysyms. */
6388 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6389 || keysym == XK_Delete
6390 #ifdef XK_ISO_Left_Tab
6391 || (keysym >= XK_ISO_Left_Tab
6392 && keysym <= XK_ISO_Enter)
6393 #endif
6394 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6395 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6396 #ifdef HPUX
6397 /* This recognizes the "extended function
6398 keys". It seems there's no cleaner way.
6399 Test IsModifierKey to avoid handling
6400 mode_switch incorrectly. */
6401 || ((unsigned) (keysym) >= XK_Select
6402 && (unsigned)(keysym) < XK_KP_Space)
6403 #endif
6404 #ifdef XK_dead_circumflex
6405 || orig_keysym == XK_dead_circumflex
6406 #endif
6407 #ifdef XK_dead_grave
6408 || orig_keysym == XK_dead_grave
6409 #endif
6410 #ifdef XK_dead_tilde
6411 || orig_keysym == XK_dead_tilde
6412 #endif
6413 #ifdef XK_dead_diaeresis
6414 || orig_keysym == XK_dead_diaeresis
6415 #endif
6416 #ifdef XK_dead_macron
6417 || orig_keysym == XK_dead_macron
6418 #endif
6419 #ifdef XK_dead_degree
6420 || orig_keysym == XK_dead_degree
6421 #endif
6422 #ifdef XK_dead_acute
6423 || orig_keysym == XK_dead_acute
6424 #endif
6425 #ifdef XK_dead_cedilla
6426 || orig_keysym == XK_dead_cedilla
6427 #endif
6428 #ifdef XK_dead_breve
6429 || orig_keysym == XK_dead_breve
6430 #endif
6431 #ifdef XK_dead_ogonek
6432 || orig_keysym == XK_dead_ogonek
6433 #endif
6434 #ifdef XK_dead_caron
6435 || orig_keysym == XK_dead_caron
6436 #endif
6437 #ifdef XK_dead_doubleacute
6438 || orig_keysym == XK_dead_doubleacute
6439 #endif
6440 #ifdef XK_dead_abovedot
6441 || orig_keysym == XK_dead_abovedot
6442 #endif
6443 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6444 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6445 /* Any "vendor-specific" key is ok. */
6446 || (orig_keysym & (1 << 28))
6447 || (keysym != NoSymbol && nbytes == 0))
6448 && ! (IsModifierKey (orig_keysym)
6449 #ifndef HAVE_X11R5
6450 #ifdef XK_Mode_switch
6451 || ((unsigned)(orig_keysym) == XK_Mode_switch)
6452 #endif
6453 #ifdef XK_Num_Lock
6454 || ((unsigned)(orig_keysym) == XK_Num_Lock)
6455 #endif
6456 #endif /* not HAVE_X11R5 */
6457 /* The symbols from XK_ISO_Lock
6458 to XK_ISO_Last_Group_Lock
6459 don't have real modifiers but
6460 should be treated similarly to
6461 Mode_switch by Emacs. */
6462 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6463 || ((unsigned)(orig_keysym)
6464 >= XK_ISO_Lock
6465 && (unsigned)(orig_keysym)
6466 <= XK_ISO_Last_Group_Lock)
6467 #endif
6470 STORE_KEYSYM_FOR_DEBUG (keysym);
6471 /* make_lispy_event will convert this to a symbolic
6472 key. */
6473 inev.kind = NON_ASCII_KEYSTROKE_EVENT;
6474 inev.code = keysym;
6475 goto done_keysym;
6478 { /* Raw bytes, not keysym. */
6479 register int i;
6480 register int c;
6481 int nchars, len;
6483 /* The input should be decoded with `coding_system'
6484 which depends on which X*LookupString function
6485 we used just above and the locale. */
6486 setup_coding_system (coding_system, &coding);
6487 coding.src_multibyte = 0;
6488 coding.dst_multibyte = 1;
6489 /* The input is converted to events, thus we can't
6490 handle composition. Anyway, there's no XIM that
6491 gives us composition information. */
6492 coding.composing = COMPOSITION_DISABLED;
6494 for (i = 0; i < nbytes; i++)
6496 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6500 /* Decode the input data. */
6501 int require;
6502 unsigned char *p;
6504 require = decoding_buffer_size (&coding, nbytes);
6505 p = (unsigned char *) alloca (require);
6506 coding.mode |= CODING_MODE_LAST_BLOCK;
6507 /* We explicitly disable composition handling because
6508 key data should not contain any composition sequence. */
6509 coding.composing = COMPOSITION_DISABLED;
6510 decode_coding (&coding, copy_bufptr, p, nbytes, require);
6511 nbytes = coding.produced;
6512 nchars = coding.produced_char;
6513 copy_bufptr = p;
6516 /* Convert the input data to a sequence of
6517 character events. */
6518 for (i = 0; i < nbytes; i += len)
6520 if (nchars == nbytes)
6521 c = copy_bufptr[i], len = 1;
6522 else
6523 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6524 nbytes - i, len);
6525 inev.kind = (SINGLE_BYTE_CHAR_P (c)
6526 ? ASCII_KEYSTROKE_EVENT
6527 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6528 inev.code = c;
6529 kbd_buffer_store_event_hold (&inev, hold_quit);
6532 /* Previous code updated count by nchars rather than nbytes,
6533 but that seems bogus to me. ++kfs */
6534 count += nbytes;
6536 inev.kind = NO_EVENT; /* Already stored above. */
6538 if (keysym == NoSymbol)
6539 break;
6542 done_keysym:
6543 #ifdef HAVE_X_I18N
6544 /* Don't dispatch this event since XtDispatchEvent calls
6545 XFilterEvent, and two calls in a row may freeze the
6546 client. */
6547 break;
6548 #else
6549 goto OTHER;
6550 #endif
6552 case KeyRelease:
6553 #ifdef HAVE_X_I18N
6554 /* Don't dispatch this event since XtDispatchEvent calls
6555 XFilterEvent, and two calls in a row may freeze the
6556 client. */
6557 break;
6558 #else
6559 goto OTHER;
6560 #endif
6562 case EnterNotify:
6563 x_detect_focus_change (dpyinfo, &event, &inev);
6565 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6567 if (f && x_mouse_click_focus_ignore_position)
6568 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6570 #if 0
6571 if (event.xcrossing.focus)
6573 /* Avoid nasty pop/raise loops. */
6574 if (f && (!(f->auto_raise)
6575 || !(f->auto_lower)
6576 || (event.xcrossing.time - enter_timestamp) > 500))
6578 x_new_focus_frame (dpyinfo, f);
6579 enter_timestamp = event.xcrossing.time;
6582 else if (f == dpyinfo->x_focus_frame)
6583 x_new_focus_frame (dpyinfo, 0);
6584 #endif
6586 /* EnterNotify counts as mouse movement,
6587 so update things that depend on mouse position. */
6588 if (f && !f->output_data.x->hourglass_p)
6589 note_mouse_movement (f, &event.xmotion);
6590 goto OTHER;
6592 case FocusIn:
6593 x_detect_focus_change (dpyinfo, &event, &inev);
6594 goto OTHER;
6596 case LeaveNotify:
6597 x_detect_focus_change (dpyinfo, &event, &inev);
6599 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6600 if (f)
6602 if (f == dpyinfo->mouse_face_mouse_frame)
6604 /* If we move outside the frame, then we're
6605 certainly no longer on any text in the frame. */
6606 clear_mouse_face (dpyinfo);
6607 dpyinfo->mouse_face_mouse_frame = 0;
6610 /* Generate a nil HELP_EVENT to cancel a help-echo.
6611 Do it only if there's something to cancel.
6612 Otherwise, the startup message is cleared when
6613 the mouse leaves the frame. */
6614 if (any_help_event_p)
6615 do_help = -1;
6617 goto OTHER;
6619 case FocusOut:
6620 x_detect_focus_change (dpyinfo, &event, &inev);
6621 goto OTHER;
6623 case MotionNotify:
6625 previous_help_echo_string = help_echo_string;
6626 help_echo_string = help_echo_object = help_echo_window = Qnil;
6627 help_echo_pos = -1;
6629 if (dpyinfo->grabbed && last_mouse_frame
6630 && FRAME_LIVE_P (last_mouse_frame))
6631 f = last_mouse_frame;
6632 else
6633 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6635 if (dpyinfo->mouse_face_hidden)
6637 dpyinfo->mouse_face_hidden = 0;
6638 clear_mouse_face (dpyinfo);
6641 if (f)
6644 /* Generate SELECT_WINDOW_EVENTs when needed. */
6645 if (mouse_autoselect_window)
6647 Lisp_Object window;
6649 window = window_from_coordinates (f,
6650 event.xmotion.x, event.xmotion.y,
6651 0, 0, 0, 0);
6653 /* Window will be selected only when it is not selected now and
6654 last mouse movement event was not in it. Minibuffer window
6655 will be selected iff it is active. */
6656 if (WINDOWP (window)
6657 && !EQ (window, last_window)
6658 && !EQ (window, selected_window))
6660 inev.kind = SELECT_WINDOW_EVENT;
6661 inev.frame_or_window = window;
6664 last_window=window;
6666 note_mouse_movement (f, &event.xmotion);
6668 else
6670 #ifndef USE_TOOLKIT_SCROLL_BARS
6671 struct scroll_bar *bar
6672 = x_window_to_scroll_bar (event.xmotion.display,
6673 event.xmotion.window);
6675 if (bar)
6676 x_scroll_bar_note_movement (bar, &event);
6677 #endif /* USE_TOOLKIT_SCROLL_BARS */
6679 /* If we move outside the frame, then we're
6680 certainly no longer on any text in the frame. */
6681 clear_mouse_face (dpyinfo);
6684 /* If the contents of the global variable help_echo_string
6685 has changed, generate a HELP_EVENT. */
6686 if (!NILP (help_echo_string)
6687 || !NILP (previous_help_echo_string))
6688 do_help = 1;
6689 goto OTHER;
6692 case ConfigureNotify:
6693 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6694 if (f)
6696 #ifndef USE_X_TOOLKIT
6697 #ifdef USE_GTK
6698 xg_resize_widgets (f, event.xconfigure.width,
6699 event.xconfigure.height);
6700 #else /* not USE_GTK */
6701 /* If there is a pending resize for fullscreen, don't
6702 do this one, the right one will come later.
6703 The toolkit version doesn't seem to need this, but we
6704 need to reset it below. */
6705 int dont_resize
6706 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6707 && f->new_text_cols != 0);
6708 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6709 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6711 if (dont_resize)
6712 goto OTHER;
6714 /* In the toolkit version, change_frame_size
6715 is called by the code that handles resizing
6716 of the EmacsFrame widget. */
6718 /* Even if the number of character rows and columns has
6719 not changed, the font size may have changed, so we need
6720 to check the pixel dimensions as well. */
6721 if (columns != FRAME_COLS (f)
6722 || rows != FRAME_LINES (f)
6723 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6724 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6726 change_frame_size (f, rows, columns, 0, 1, 0);
6727 SET_FRAME_GARBAGED (f);
6728 cancel_mouse_face (f);
6730 #endif /* not USE_GTK */
6731 #endif
6733 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6734 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6736 #ifdef USE_GTK
6737 /* GTK creates windows but doesn't map them.
6738 Only get real positions and check fullscreen when mapped. */
6739 if (FRAME_GTK_OUTER_WIDGET (f)
6740 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6741 #endif
6743 /* What we have now is the position of Emacs's own window.
6744 Convert that to the position of the window manager window. */
6745 x_real_positions (f, &f->left_pos, &f->top_pos);
6747 x_check_expected_move (f);
6748 if (f->want_fullscreen & FULLSCREEN_WAIT)
6749 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6752 #ifdef HAVE_X_I18N
6753 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6754 xic_set_statusarea (f);
6755 #endif
6757 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6759 /* Since the WM decorations come below top_pos now,
6760 we must put them below top_pos in the future. */
6761 f->win_gravity = NorthWestGravity;
6762 x_wm_set_size_hint (f, (long) 0, 0);
6765 goto OTHER;
6767 case ButtonRelease:
6768 case ButtonPress:
6770 /* If we decide we want to generate an event to be seen
6771 by the rest of Emacs, we put it here. */
6772 int tool_bar_p = 0;
6774 bzero (&compose_status, sizeof (compose_status));
6776 if (dpyinfo->grabbed
6777 && last_mouse_frame
6778 && FRAME_LIVE_P (last_mouse_frame))
6779 f = last_mouse_frame;
6780 else
6781 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6783 if (f)
6785 /* Is this in the tool-bar? */
6786 if (WINDOWP (f->tool_bar_window)
6787 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6789 Lisp_Object window;
6790 int x = event.xbutton.x;
6791 int y = event.xbutton.y;
6793 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6794 if (EQ (window, f->tool_bar_window))
6796 if (event.xbutton.type == ButtonPress)
6797 handle_tool_bar_click (f, x, y, 1, 0);
6798 else
6799 handle_tool_bar_click (f, x, y, 0,
6800 x_x_to_emacs_modifiers (dpyinfo,
6801 event.xbutton.state));
6802 tool_bar_p = 1;
6806 if (!tool_bar_p)
6807 if (!dpyinfo->x_focus_frame
6808 || f == dpyinfo->x_focus_frame)
6810 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6811 if (! popup_activated ())
6812 #endif
6814 if (ignore_next_mouse_click_timeout)
6816 if (event.type == ButtonPress
6817 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6819 ignore_next_mouse_click_timeout = 0;
6820 construct_mouse_click (&inev, &event, f);
6822 if (event.type == ButtonRelease)
6823 ignore_next_mouse_click_timeout = 0;
6825 else
6826 construct_mouse_click (&inev, &event, f);
6830 else
6832 struct scroll_bar *bar
6833 = x_window_to_scroll_bar (event.xbutton.display,
6834 event.xbutton.window);
6836 #ifdef USE_TOOLKIT_SCROLL_BARS
6837 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6838 scroll bars. */
6839 if (bar && event.xbutton.state & ControlMask)
6841 x_scroll_bar_handle_click (bar, &event, &inev);
6842 *finish = X_EVENT_DROP;
6844 #else /* not USE_TOOLKIT_SCROLL_BARS */
6845 if (bar)
6846 x_scroll_bar_handle_click (bar, &event, &inev);
6847 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6850 if (event.type == ButtonPress)
6852 dpyinfo->grabbed |= (1 << event.xbutton.button);
6853 last_mouse_frame = f;
6854 /* Ignore any mouse motion that happened
6855 before this event; any subsequent mouse-movement
6856 Emacs events should reflect only motion after
6857 the ButtonPress. */
6858 if (f != 0)
6859 f->mouse_moved = 0;
6861 if (!tool_bar_p)
6862 last_tool_bar_item = -1;
6864 else
6865 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6867 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6868 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6869 /* For a down-event in the menu bar,
6870 don't pass it to Xt right now.
6871 Instead, save it away
6872 and we will pass it to Xt from kbd_buffer_get_event.
6873 That way, we can run some Lisp code first. */
6874 if (
6875 #ifdef USE_GTK
6876 ! popup_activated ()
6878 #endif
6879 f && event.type == ButtonPress
6880 /* Verify the event is really within the menu bar
6881 and not just sent to it due to grabbing. */
6882 && event.xbutton.x >= 0
6883 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6884 && event.xbutton.y >= 0
6885 && event.xbutton.y < f->output_data.x->menubar_height
6886 && event.xbutton.same_screen)
6888 SET_SAVED_BUTTON_EVENT;
6889 XSETFRAME (last_mouse_press_frame, f);
6890 #ifdef USE_GTK
6891 *finish = X_EVENT_DROP;
6892 #endif
6894 else if (event.type == ButtonPress)
6896 last_mouse_press_frame = Qnil;
6897 goto OTHER;
6900 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6901 but I am trying to be cautious. */
6902 else if (event.type == ButtonRelease)
6904 if (!NILP (last_mouse_press_frame))
6906 f = XFRAME (last_mouse_press_frame);
6907 if (f->output_data.x)
6908 SET_SAVED_BUTTON_EVENT;
6910 else
6911 goto OTHER;
6913 #endif /* USE_MOTIF */
6914 else
6915 goto OTHER;
6916 #endif /* USE_X_TOOLKIT || USE_GTK */
6918 break;
6920 case CirculateNotify:
6921 goto OTHER;
6923 case CirculateRequest:
6924 goto OTHER;
6926 case VisibilityNotify:
6927 goto OTHER;
6929 case MappingNotify:
6930 /* Someone has changed the keyboard mapping - update the
6931 local cache. */
6932 switch (event.xmapping.request)
6934 case MappingModifier:
6935 x_find_modifier_meanings (dpyinfo);
6936 /* This is meant to fall through. */
6937 case MappingKeyboard:
6938 XRefreshKeyboardMapping (&event.xmapping);
6940 goto OTHER;
6942 default:
6943 OTHER:
6944 #ifdef USE_X_TOOLKIT
6945 BLOCK_INPUT;
6946 if (*finish != X_EVENT_DROP)
6947 XtDispatchEvent (&event);
6948 UNBLOCK_INPUT;
6949 #endif /* USE_X_TOOLKIT */
6950 break;
6953 done:
6954 if (inev.kind != NO_EVENT)
6956 kbd_buffer_store_event_hold (&inev, hold_quit);
6957 count++;
6960 if (do_help
6961 && !(hold_quit && hold_quit->kind != NO_EVENT))
6963 Lisp_Object frame;
6965 if (f)
6966 XSETFRAME (frame, f);
6967 else
6968 frame = Qnil;
6970 if (do_help > 0)
6972 any_help_event_p = 1;
6973 gen_help_event (help_echo_string, frame, help_echo_window,
6974 help_echo_object, help_echo_pos);
6976 else
6978 help_echo_string = Qnil;
6979 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
6981 count++;
6984 *eventp = event;
6985 return count;
6989 /* Handles the XEvent EVENT on display DISPLAY.
6990 This is used for event loops outside the normal event handling,
6991 i.e. looping while a popup menu or a dialog is posted.
6993 Returns the value handle_one_xevent sets in the finish argument. */
6995 x_dispatch_event (event, display)
6996 XEvent *event;
6997 Display *display;
6999 struct x_display_info *dpyinfo;
7000 int finish = X_EVENT_NORMAL;
7002 dpyinfo = x_display_info_for_display (display);
7004 if (dpyinfo)
7005 handle_one_xevent (dpyinfo, event, &finish, 0);
7007 return finish;
7011 /* Read events coming from the X server.
7012 This routine is called by the SIGIO handler.
7013 We return as soon as there are no more events to be read.
7015 We return the number of characters stored into the buffer,
7016 thus pretending to be `read'.
7018 EXPECTED is nonzero if the caller knows input is available. */
7020 static int
7021 XTread_socket (sd, expected, hold_quit)
7022 register int sd;
7023 int expected;
7024 struct input_event *hold_quit;
7026 int count = 0;
7027 XEvent event;
7028 int event_found = 0;
7029 struct x_display_info *dpyinfo;
7031 if (interrupt_input_blocked)
7033 interrupt_input_pending = 1;
7034 return -1;
7037 interrupt_input_pending = 0;
7038 BLOCK_INPUT;
7040 /* So people can tell when we have read the available input. */
7041 input_signal_count++;
7043 ++handling_signal;
7045 /* Find the display we are supposed to read input for.
7046 It's the one communicating on descriptor SD. */
7047 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7049 #if 0 /* This ought to be unnecessary; let's verify it. */
7050 #ifdef FIOSNBIO
7051 /* If available, Xlib uses FIOSNBIO to make the socket
7052 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7053 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7054 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7055 fcntl (dpyinfo->connection, F_SETFL, 0);
7056 #endif /* ! defined (FIOSNBIO) */
7057 #endif
7059 #if 0 /* This code can't be made to work, with multiple displays,
7060 and appears not to be used on any system any more.
7061 Also keyboard.c doesn't turn O_NDELAY on and off
7062 for X connections. */
7063 #ifndef SIGIO
7064 #ifndef HAVE_SELECT
7065 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7067 extern int read_alarm_should_throw;
7068 read_alarm_should_throw = 1;
7069 XPeekEvent (dpyinfo->display, &event);
7070 read_alarm_should_throw = 0;
7072 #endif /* HAVE_SELECT */
7073 #endif /* SIGIO */
7074 #endif
7076 /* For debugging, this gives a way to fake an I/O error. */
7077 if (dpyinfo == XTread_socket_fake_io_error)
7079 XTread_socket_fake_io_error = 0;
7080 x_io_error_quitter (dpyinfo->display);
7083 #ifdef HAVE_X_SM
7085 struct input_event inev;
7086 BLOCK_INPUT;
7087 /* We don't need to EVENT_INIT (inev) here, as
7088 x_session_check_input copies an entire input_event. */
7089 if (x_session_check_input (&inev))
7091 kbd_buffer_store_event_hold (&inev, hold_quit);
7092 count++;
7094 UNBLOCK_INPUT;
7096 #endif
7098 #ifndef USE_GTK
7099 while (XPending (dpyinfo->display))
7101 int finish;
7103 XNextEvent (dpyinfo->display, &event);
7105 #ifdef HAVE_X_I18N
7106 /* Filter events for the current X input method. */
7107 if (x_filter_event (dpyinfo, &event))
7108 break;
7109 #endif
7110 event_found = 1;
7112 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
7114 if (finish == X_EVENT_GOTO_OUT)
7115 goto out;
7117 #endif /* not USE_GTK */
7120 #ifdef USE_GTK
7122 /* For GTK we must use the GTK event loop. But XEvents gets passed
7123 to our filter function above, and then to the big event switch.
7124 We use a bunch of globals to communicate with our filter function,
7125 that is kind of ugly, but it works.
7127 There is no way to do one display at the time, GTK just does events
7128 from all displays. */
7130 while (gtk_events_pending ())
7132 current_count = count;
7133 current_hold_quit = hold_quit;
7135 gtk_main_iteration ();
7137 count = current_count;
7138 current_count = -1;
7139 current_hold_quit = 0;
7141 if (current_finish == X_EVENT_GOTO_OUT)
7142 break;
7144 #endif /* USE_GTK */
7146 out:;
7148 /* On some systems, an X bug causes Emacs to get no more events
7149 when the window is destroyed. Detect that. (1994.) */
7150 if (! event_found)
7152 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7153 One XNOOP in 100 loops will make Emacs terminate.
7154 B. Bretthauer, 1994 */
7155 x_noop_count++;
7156 if (x_noop_count >= 100)
7158 x_noop_count=0;
7160 if (next_noop_dpyinfo == 0)
7161 next_noop_dpyinfo = x_display_list;
7163 XNoOp (next_noop_dpyinfo->display);
7165 /* Each time we get here, cycle through the displays now open. */
7166 next_noop_dpyinfo = next_noop_dpyinfo->next;
7170 /* If the focus was just given to an auto-raising frame,
7171 raise it now. */
7172 /* ??? This ought to be able to handle more than one such frame. */
7173 if (pending_autoraise_frame)
7175 x_raise_frame (pending_autoraise_frame);
7176 pending_autoraise_frame = 0;
7179 --handling_signal;
7180 UNBLOCK_INPUT;
7182 return count;
7188 /***********************************************************************
7189 Text Cursor
7190 ***********************************************************************/
7192 /* Set clipping for output in glyph row ROW. W is the window in which
7193 we operate. GC is the graphics context to set clipping in.
7195 ROW may be a text row or, e.g., a mode line. Text rows must be
7196 clipped to the interior of the window dedicated to text display,
7197 mode lines must be clipped to the whole window. */
7199 static void
7200 x_clip_to_row (w, row, gc)
7201 struct window *w;
7202 struct glyph_row *row;
7203 GC gc;
7205 struct frame *f = XFRAME (WINDOW_FRAME (w));
7206 XRectangle clip_rect;
7207 int window_y, window_width;
7209 window_box (w, -1, 0, &window_y, &window_width, 0);
7211 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
7212 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
7213 clip_rect.y = max (clip_rect.y, window_y);
7214 clip_rect.width = window_width;
7215 clip_rect.height = row->visible_height;
7217 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7221 /* Draw a hollow box cursor on window W in glyph row ROW. */
7223 static void
7224 x_draw_hollow_cursor (w, row)
7225 struct window *w;
7226 struct glyph_row *row;
7228 struct frame *f = XFRAME (WINDOW_FRAME (w));
7229 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7230 Display *dpy = FRAME_X_DISPLAY (f);
7231 int x, y, wd, h;
7232 XGCValues xgcv;
7233 struct glyph *cursor_glyph;
7234 GC gc;
7236 /* Get the glyph the cursor is on. If we can't tell because
7237 the current matrix is invalid or such, give up. */
7238 cursor_glyph = get_phys_cursor_glyph (w);
7239 if (cursor_glyph == NULL)
7240 return;
7242 /* Compute the width of the rectangle to draw. If on a stretch
7243 glyph, and `x-stretch-block-cursor' is nil, don't draw a
7244 rectangle as wide as the glyph, but use a canonical character
7245 width instead. */
7246 wd = cursor_glyph->pixel_width - 1;
7247 if (cursor_glyph->type == STRETCH_GLYPH
7248 && !x_stretch_cursor_p)
7249 wd = min (FRAME_COLUMN_WIDTH (f), wd);
7250 w->phys_cursor_width = wd;
7252 /* Compute frame-relative coordinates from window-relative
7253 coordinates. */
7254 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7255 y = WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y);
7257 /* Compute the proper height and ascent of the rectangle, based
7258 on the actual glyph. Using the full height of the row looks
7259 bad when there are tall images on that row. */
7260 h = max (min (FRAME_LINE_HEIGHT (f), row->height),
7261 cursor_glyph->ascent + cursor_glyph->descent);
7262 if (h < row->height)
7263 y += row->ascent /* - w->phys_cursor_ascent */ + cursor_glyph->descent - h;
7264 h--;
7266 /* The foreground of cursor_gc is typically the same as the normal
7267 background color, which can cause the cursor box to be invisible. */
7268 xgcv.foreground = f->output_data.x->cursor_pixel;
7269 if (dpyinfo->scratch_cursor_gc)
7270 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7271 else
7272 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7273 GCForeground, &xgcv);
7274 gc = dpyinfo->scratch_cursor_gc;
7276 /* Set clipping, draw the rectangle, and reset clipping again. */
7277 x_clip_to_row (w, row, gc);
7278 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
7279 XSetClipMask (dpy, gc, None);
7283 /* Draw a bar cursor on window W in glyph row ROW.
7285 Implementation note: One would like to draw a bar cursor with an
7286 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7287 Unfortunately, I didn't find a font yet that has this property set.
7288 --gerd. */
7290 static void
7291 x_draw_bar_cursor (w, row, width, kind)
7292 struct window *w;
7293 struct glyph_row *row;
7294 int width;
7295 enum text_cursor_kinds kind;
7297 struct frame *f = XFRAME (w->frame);
7298 struct glyph *cursor_glyph;
7300 /* If cursor is out of bounds, don't draw garbage. This can happen
7301 in mini-buffer windows when switching between echo area glyphs
7302 and mini-buffer. */
7303 cursor_glyph = get_phys_cursor_glyph (w);
7304 if (cursor_glyph == NULL)
7305 return;
7307 /* If on an image, draw like a normal cursor. That's usually better
7308 visible than drawing a bar, esp. if the image is large so that
7309 the bar might not be in the window. */
7310 if (cursor_glyph->type == IMAGE_GLYPH)
7312 struct glyph_row *row;
7313 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7314 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7316 else
7318 Display *dpy = FRAME_X_DISPLAY (f);
7319 Window window = FRAME_X_WINDOW (f);
7320 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7321 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7322 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7323 XGCValues xgcv;
7325 /* If the glyph's background equals the color we normally draw
7326 the bar cursor in, the bar cursor in its normal color is
7327 invisible. Use the glyph's foreground color instead in this
7328 case, on the assumption that the glyph's colors are chosen so
7329 that the glyph is legible. */
7330 if (face->background == f->output_data.x->cursor_pixel)
7331 xgcv.background = xgcv.foreground = face->foreground;
7332 else
7333 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7334 xgcv.graphics_exposures = 0;
7336 if (gc)
7337 XChangeGC (dpy, gc, mask, &xgcv);
7338 else
7340 gc = XCreateGC (dpy, window, mask, &xgcv);
7341 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7344 if (width < 0)
7345 width = FRAME_CURSOR_WIDTH (f);
7346 width = min (cursor_glyph->pixel_width, width);
7348 w->phys_cursor_width = width;
7349 x_clip_to_row (w, row, gc);
7351 if (kind == BAR_CURSOR)
7352 XFillRectangle (dpy, window, gc,
7353 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7354 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7355 width, row->height);
7356 else
7357 XFillRectangle (dpy, window, gc,
7358 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7359 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7360 row->height - width),
7361 cursor_glyph->pixel_width,
7362 width);
7364 XSetClipMask (dpy, gc, None);
7369 /* RIF: Define cursor CURSOR on frame F. */
7371 static void
7372 x_define_frame_cursor (f, cursor)
7373 struct frame *f;
7374 Cursor cursor;
7376 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7380 /* RIF: Clear area on frame F. */
7382 static void
7383 x_clear_frame_area (f, x, y, width, height)
7384 struct frame *f;
7385 int x, y, width, height;
7387 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7388 x, y, width, height, False);
7392 /* RIF: Draw cursor on window W. */
7394 static void
7395 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7396 struct window *w;
7397 struct glyph_row *glyph_row;
7398 int x, y;
7399 int cursor_type, cursor_width;
7400 int on_p, active_p;
7402 struct frame *f = XFRAME (WINDOW_FRAME (w));
7404 if (on_p)
7406 w->phys_cursor_type = cursor_type;
7407 w->phys_cursor_on_p = 1;
7409 if (glyph_row->exact_window_width_line_p
7410 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7412 glyph_row->cursor_in_fringe_p = 1;
7413 draw_fringe_bitmap (w, glyph_row, 0);
7415 else
7416 switch (cursor_type)
7418 case HOLLOW_BOX_CURSOR:
7419 x_draw_hollow_cursor (w, glyph_row);
7420 break;
7422 case FILLED_BOX_CURSOR:
7423 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7424 break;
7426 case BAR_CURSOR:
7427 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7428 break;
7430 case HBAR_CURSOR:
7431 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7432 break;
7434 case NO_CURSOR:
7435 w->phys_cursor_width = 0;
7436 break;
7438 default:
7439 abort ();
7442 #ifdef HAVE_X_I18N
7443 if (w == XWINDOW (f->selected_window))
7444 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7445 xic_set_preeditarea (w, x, y);
7446 #endif
7449 #ifndef XFlush
7450 if (updating_frame != f)
7451 XFlush (FRAME_X_DISPLAY (f));
7452 #endif
7456 /* Icons. */
7458 /* Make the x-window of frame F use the gnu icon bitmap. */
7461 x_bitmap_icon (f, file)
7462 struct frame *f;
7463 Lisp_Object file;
7465 int bitmap_id;
7467 if (FRAME_X_WINDOW (f) == 0)
7468 return 1;
7470 /* Free up our existing icon bitmap and mask if any. */
7471 if (f->output_data.x->icon_bitmap > 0)
7472 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7473 f->output_data.x->icon_bitmap = 0;
7475 if (STRINGP (file))
7477 #ifdef USE_GTK
7478 /* Use gtk_window_set_icon_from_file () if available,
7479 It's not restricted to bitmaps */
7480 if (xg_set_icon (f, file))
7481 return 0;
7482 #endif /* USE_GTK */
7483 bitmap_id = x_create_bitmap_from_file (f, file);
7484 x_create_bitmap_mask (f, bitmap_id);
7486 else
7488 /* Create the GNU bitmap and mask if necessary. */
7489 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7491 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
7492 = x_create_bitmap_from_data (f, gnu_bits,
7493 gnu_width, gnu_height);
7494 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7497 /* The first time we create the GNU bitmap and mask,
7498 this increments the ref-count one extra time.
7499 As a result, the GNU bitmap and mask are never freed.
7500 That way, we don't have to worry about allocating it again. */
7501 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7503 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7506 x_wm_set_icon_pixmap (f, bitmap_id);
7507 f->output_data.x->icon_bitmap = bitmap_id;
7509 return 0;
7513 /* Make the x-window of frame F use a rectangle with text.
7514 Use ICON_NAME as the text. */
7517 x_text_icon (f, icon_name)
7518 struct frame *f;
7519 char *icon_name;
7521 if (FRAME_X_WINDOW (f) == 0)
7522 return 1;
7524 #ifdef HAVE_X11R4
7526 XTextProperty text;
7527 text.value = (unsigned char *) icon_name;
7528 text.encoding = XA_STRING;
7529 text.format = 8;
7530 text.nitems = strlen (icon_name);
7531 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7533 #else /* not HAVE_X11R4 */
7534 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
7535 #endif /* not HAVE_X11R4 */
7537 if (f->output_data.x->icon_bitmap > 0)
7538 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7539 f->output_data.x->icon_bitmap = 0;
7540 x_wm_set_icon_pixmap (f, 0);
7542 return 0;
7545 #define X_ERROR_MESSAGE_SIZE 200
7547 /* If non-nil, this should be a string.
7548 It means catch X errors and store the error message in this string. */
7550 static Lisp_Object x_error_message_string;
7552 /* An X error handler which stores the error message in
7553 x_error_message_string. This is called from x_error_handler if
7554 x_catch_errors is in effect. */
7556 static void
7557 x_error_catcher (display, error)
7558 Display *display;
7559 XErrorEvent *error;
7561 XGetErrorText (display, error->error_code,
7562 SDATA (x_error_message_string),
7563 X_ERROR_MESSAGE_SIZE);
7566 /* Begin trapping X errors for display DPY. Actually we trap X errors
7567 for all displays, but DPY should be the display you are actually
7568 operating on.
7570 After calling this function, X protocol errors no longer cause
7571 Emacs to exit; instead, they are recorded in the string
7572 stored in x_error_message_string.
7574 Calling x_check_errors signals an Emacs error if an X error has
7575 occurred since the last call to x_catch_errors or x_check_errors.
7577 Calling x_uncatch_errors resumes the normal error handling. */
7579 void x_check_errors ();
7580 static Lisp_Object x_catch_errors_unwind ();
7583 x_catch_errors (dpy)
7584 Display *dpy;
7586 int count = SPECPDL_INDEX ();
7588 /* Make sure any errors from previous requests have been dealt with. */
7589 XSync (dpy, False);
7591 record_unwind_protect (x_catch_errors_unwind,
7592 Fcons (make_save_value (dpy, 0),
7593 x_error_message_string));
7595 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
7596 SSET (x_error_message_string, 0, 0);
7598 return count;
7601 /* Unbind the binding that we made to check for X errors. */
7603 static Lisp_Object
7604 x_catch_errors_unwind (old_val)
7605 Lisp_Object old_val;
7607 Lisp_Object first = XCAR (old_val);
7608 Display *dpy = XSAVE_VALUE (first)->pointer;
7610 /* The display may have been closed before this function is called.
7611 Check if it is still open before calling XSync. */
7612 if (x_display_info_for_display (dpy) != 0)
7613 XSync (dpy, False);
7615 x_error_message_string = XCDR (old_val);
7616 return Qnil;
7619 /* If any X protocol errors have arrived since the last call to
7620 x_catch_errors or x_check_errors, signal an Emacs error using
7621 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7623 void
7624 x_check_errors (dpy, format)
7625 Display *dpy;
7626 char *format;
7628 /* Make sure to catch any errors incurred so far. */
7629 XSync (dpy, False);
7631 if (SREF (x_error_message_string, 0))
7632 error (format, SDATA (x_error_message_string));
7635 /* Nonzero if we had any X protocol errors
7636 since we did x_catch_errors on DPY. */
7639 x_had_errors_p (dpy)
7640 Display *dpy;
7642 /* Make sure to catch any errors incurred so far. */
7643 XSync (dpy, False);
7645 return SREF (x_error_message_string, 0) != 0;
7648 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7650 void
7651 x_clear_errors (dpy)
7652 Display *dpy;
7654 SSET (x_error_message_string, 0, 0);
7657 /* Stop catching X protocol errors and let them make Emacs die.
7658 DPY should be the display that was passed to x_catch_errors.
7659 COUNT should be the value that was returned by
7660 the corresponding call to x_catch_errors. */
7662 void
7663 x_uncatch_errors (dpy, count)
7664 Display *dpy;
7665 int count;
7667 unbind_to (count, Qnil);
7670 #if 0
7671 static unsigned int x_wire_count;
7672 x_trace_wire ()
7674 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7676 #endif /* ! 0 */
7679 /* Handle SIGPIPE, which can happen when the connection to a server
7680 simply goes away. SIGPIPE is handled by x_connection_signal.
7681 Don't need to do anything, because the write which caused the
7682 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7683 which will do the appropriate cleanup for us. */
7685 static SIGTYPE
7686 x_connection_signal (signalnum) /* If we don't have an argument, */
7687 int signalnum; /* some compilers complain in signal calls. */
7689 #ifdef USG
7690 /* USG systems forget handlers when they are used;
7691 must reestablish each time */
7692 signal (signalnum, x_connection_signal);
7693 #endif /* USG */
7697 /************************************************************************
7698 Handling X errors
7699 ************************************************************************/
7701 /* Error message passed to x_connection_closed. */
7703 static char *error_msg;
7705 /* Function installed as fatal_error_signal_hook in
7706 x_connection_closed. Print the X error message, and exit normally,
7707 instead of dumping core when XtCloseDisplay fails. */
7709 static void
7710 x_fatal_error_signal ()
7712 fprintf (stderr, "%s\n", error_msg);
7713 exit (70);
7716 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7717 the text of an error message that lead to the connection loss. */
7719 static SIGTYPE
7720 x_connection_closed (dpy, error_message)
7721 Display *dpy;
7722 char *error_message;
7724 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7725 Lisp_Object frame, tail;
7726 int count;
7728 error_msg = (char *) alloca (strlen (error_message) + 1);
7729 strcpy (error_msg, error_message);
7730 handling_signal = 0;
7732 /* Prevent being called recursively because of an error condition
7733 below. Otherwise, we might end up with printing ``can't find per
7734 display information'' in the recursive call instead of printing
7735 the original message here. */
7736 count = x_catch_errors (dpy);
7738 /* We have to close the display to inform Xt that it doesn't
7739 exist anymore. If we don't, Xt will continue to wait for
7740 events from the display. As a consequence, a sequence of
7742 M-x make-frame-on-display RET :1 RET
7743 ...kill the new frame, so that we get an IO error...
7744 M-x make-frame-on-display RET :1 RET
7746 will indefinitely wait in Xt for events for display `:1', opened
7747 in the first class to make-frame-on-display.
7749 Closing the display is reported to lead to a bus error on
7750 OpenWindows in certain situations. I suspect that is a bug
7751 in OpenWindows. I don't know how to cicumvent it here. */
7753 #ifdef USE_X_TOOLKIT
7754 /* If DPYINFO is null, this means we didn't open the display
7755 in the first place, so don't try to close it. */
7756 if (dpyinfo)
7758 extern void (*fatal_error_signal_hook) P_ ((void));
7759 fatal_error_signal_hook = x_fatal_error_signal;
7760 XtCloseDisplay (dpy);
7761 fatal_error_signal_hook = NULL;
7763 #endif
7765 #ifdef USE_GTK
7766 if (dpyinfo)
7767 xg_display_close (dpyinfo->display);
7768 #endif
7770 /* Indicate that this display is dead. */
7771 if (dpyinfo)
7772 dpyinfo->display = 0;
7774 /* First delete frames whose mini-buffers are on frames
7775 that are on the dead display. */
7776 FOR_EACH_FRAME (tail, frame)
7778 Lisp_Object minibuf_frame;
7779 minibuf_frame
7780 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7781 if (FRAME_X_P (XFRAME (frame))
7782 && FRAME_X_P (XFRAME (minibuf_frame))
7783 && ! EQ (frame, minibuf_frame)
7784 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7785 Fdelete_frame (frame, Qt);
7788 /* Now delete all remaining frames on the dead display.
7789 We are now sure none of these is used as the mini-buffer
7790 for another frame that we need to delete. */
7791 FOR_EACH_FRAME (tail, frame)
7792 if (FRAME_X_P (XFRAME (frame))
7793 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7795 /* Set this to t so that Fdelete_frame won't get confused
7796 trying to find a replacement. */
7797 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7798 Fdelete_frame (frame, Qt);
7801 if (dpyinfo)
7802 x_delete_display (dpyinfo);
7804 x_uncatch_errors (dpy, count);
7806 if (x_display_list == 0)
7808 fprintf (stderr, "%s\n", error_msg);
7809 shut_down_emacs (0, 0, Qnil);
7810 exit (70);
7813 /* Ordinary stack unwind doesn't deal with these. */
7814 #ifdef SIGIO
7815 sigunblock (sigmask (SIGIO));
7816 #endif
7817 sigunblock (sigmask (SIGALRM));
7818 TOTALLY_UNBLOCK_INPUT;
7820 clear_waiting_for_input ();
7821 error ("%s", error_msg);
7825 /* This is the usual handler for X protocol errors.
7826 It kills all frames on the display that we got the error for.
7827 If that was the only one, it prints an error message and kills Emacs. */
7829 static void
7830 x_error_quitter (display, error)
7831 Display *display;
7832 XErrorEvent *error;
7834 char buf[256], buf1[356];
7836 /* Note that there is no real way portable across R3/R4 to get the
7837 original error handler. */
7839 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7840 sprintf (buf1, "X protocol error: %s on protocol request %d",
7841 buf, error->request_code);
7842 x_connection_closed (display, buf1);
7846 /* This is the first-level handler for X protocol errors.
7847 It calls x_error_quitter or x_error_catcher. */
7849 static int
7850 x_error_handler (display, error)
7851 Display *display;
7852 XErrorEvent *error;
7854 if (! NILP (x_error_message_string))
7855 x_error_catcher (display, error);
7856 else
7857 x_error_quitter (display, error);
7858 return 0;
7861 /* This is the handler for X IO errors, always.
7862 It kills all frames on the display that we lost touch with.
7863 If that was the only one, it prints an error message and kills Emacs. */
7865 static int
7866 x_io_error_quitter (display)
7867 Display *display;
7869 char buf[256];
7871 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7872 x_connection_closed (display, buf);
7873 return 0;
7876 /* Changing the font of the frame. */
7878 /* Give frame F the font named FONTNAME as its default font, and
7879 return the full name of that font. FONTNAME may be a wildcard
7880 pattern; in that case, we choose some font that fits the pattern.
7881 The return value shows which font we chose. */
7883 Lisp_Object
7884 x_new_font (f, fontname)
7885 struct frame *f;
7886 register char *fontname;
7888 struct font_info *fontp
7889 = FS_LOAD_FONT (f, 0, fontname, -1);
7891 if (!fontp)
7892 return Qnil;
7894 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
7895 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
7896 FRAME_FONTSET (f) = -1;
7898 FRAME_COLUMN_WIDTH (f) = FONT_WIDTH (FRAME_FONT (f));
7899 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
7901 compute_fringe_widths (f, 1);
7903 /* Compute the scroll bar width in character columns. */
7904 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
7906 int wid = FRAME_COLUMN_WIDTH (f);
7907 FRAME_CONFIG_SCROLL_BAR_COLS (f)
7908 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
7910 else
7912 int wid = FRAME_COLUMN_WIDTH (f);
7913 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
7916 /* Now make the frame display the given font. */
7917 if (FRAME_X_WINDOW (f) != 0)
7919 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
7920 FRAME_FONT (f)->fid);
7921 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
7922 FRAME_FONT (f)->fid);
7923 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
7924 FRAME_FONT (f)->fid);
7926 /* Don't change the size of a tip frame; there's no point in
7927 doing it because it's done in Fx_show_tip, and it leads to
7928 problems because the tip frame has no widget. */
7929 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
7930 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
7933 return build_string (fontp->full_name);
7936 /* Give frame F the fontset named FONTSETNAME as its default font, and
7937 return the full name of that fontset. FONTSETNAME may be a wildcard
7938 pattern; in that case, we choose some fontset that fits the pattern.
7939 The return value shows which fontset we chose. */
7941 Lisp_Object
7942 x_new_fontset (f, fontsetname)
7943 struct frame *f;
7944 char *fontsetname;
7946 int fontset = fs_query_fontset (build_string (fontsetname), 0);
7947 Lisp_Object result;
7949 if (fontset < 0)
7950 return Qnil;
7952 if (FRAME_FONTSET (f) == fontset)
7953 /* This fontset is already set in frame F. There's nothing more
7954 to do. */
7955 return fontset_name (fontset);
7957 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
7959 if (!STRINGP (result))
7960 /* Can't load ASCII font. */
7961 return Qnil;
7963 /* Since x_new_font doesn't update any fontset information, do it now. */
7964 FRAME_FONTSET (f) = fontset;
7966 #ifdef HAVE_X_I18N
7967 if (FRAME_XIC (f)
7968 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
7969 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
7970 #endif
7972 return build_string (fontsetname);
7976 /***********************************************************************
7977 X Input Methods
7978 ***********************************************************************/
7980 #ifdef HAVE_X_I18N
7982 #ifdef HAVE_X11R6
7984 /* XIM destroy callback function, which is called whenever the
7985 connection to input method XIM dies. CLIENT_DATA contains a
7986 pointer to the x_display_info structure corresponding to XIM. */
7988 static void
7989 xim_destroy_callback (xim, client_data, call_data)
7990 XIM xim;
7991 XPointer client_data;
7992 XPointer call_data;
7994 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
7995 Lisp_Object frame, tail;
7997 BLOCK_INPUT;
7999 /* No need to call XDestroyIC.. */
8000 FOR_EACH_FRAME (tail, frame)
8002 struct frame *f = XFRAME (frame);
8003 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8005 FRAME_XIC (f) = NULL;
8006 if (FRAME_XIC_FONTSET (f))
8008 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
8009 FRAME_XIC_FONTSET (f) = NULL;
8014 /* No need to call XCloseIM. */
8015 dpyinfo->xim = NULL;
8016 XFree (dpyinfo->xim_styles);
8017 UNBLOCK_INPUT;
8020 #endif /* HAVE_X11R6 */
8022 #ifdef HAVE_X11R6
8023 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8024 extern char *XSetIMValues P_ ((XIM, ...));
8025 #endif
8027 /* Open the connection to the XIM server on display DPYINFO.
8028 RESOURCE_NAME is the resource name Emacs uses. */
8030 static void
8031 xim_open_dpy (dpyinfo, resource_name)
8032 struct x_display_info *dpyinfo;
8033 char *resource_name;
8035 XIM xim;
8037 #ifdef HAVE_XIM
8038 if (use_xim)
8040 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8041 EMACS_CLASS);
8042 dpyinfo->xim = xim;
8044 if (xim)
8046 #ifdef HAVE_X11R6
8047 XIMCallback destroy;
8048 #endif
8050 /* Get supported styles and XIM values. */
8051 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8053 #ifdef HAVE_X11R6
8054 destroy.callback = xim_destroy_callback;
8055 destroy.client_data = (XPointer)dpyinfo;
8056 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8057 #endif
8061 else
8062 #endif /* HAVE_XIM */
8063 dpyinfo->xim = NULL;
8067 #ifdef HAVE_X11R6_XIM
8069 struct xim_inst_t
8071 struct x_display_info *dpyinfo;
8072 char *resource_name;
8075 /* XIM instantiate callback function, which is called whenever an XIM
8076 server is available. DISPLAY is the display of the XIM.
8077 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8078 when the callback was registered. */
8080 static void
8081 xim_instantiate_callback (display, client_data, call_data)
8082 Display *display;
8083 XPointer client_data;
8084 XPointer call_data;
8086 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8087 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8089 /* We don't support multiple XIM connections. */
8090 if (dpyinfo->xim)
8091 return;
8093 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8095 /* Create XIC for the existing frames on the same display, as long
8096 as they have no XIC. */
8097 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8099 Lisp_Object tail, frame;
8101 BLOCK_INPUT;
8102 FOR_EACH_FRAME (tail, frame)
8104 struct frame *f = XFRAME (frame);
8106 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8107 if (FRAME_XIC (f) == NULL)
8109 create_frame_xic (f);
8110 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8111 xic_set_statusarea (f);
8112 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8114 struct window *w = XWINDOW (f->selected_window);
8115 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8120 UNBLOCK_INPUT;
8124 #endif /* HAVE_X11R6_XIM */
8127 /* Open a connection to the XIM server on display DPYINFO.
8128 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8129 connection only at the first time. On X11R6, open the connection
8130 in the XIM instantiate callback function. */
8132 static void
8133 xim_initialize (dpyinfo, resource_name)
8134 struct x_display_info *dpyinfo;
8135 char *resource_name;
8137 #ifdef HAVE_XIM
8138 if (use_xim)
8140 #ifdef HAVE_X11R6_XIM
8141 struct xim_inst_t *xim_inst;
8142 int len;
8144 dpyinfo->xim = NULL;
8145 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8146 xim_inst->dpyinfo = dpyinfo;
8147 len = strlen (resource_name);
8148 xim_inst->resource_name = (char *) xmalloc (len + 1);
8149 bcopy (resource_name, xim_inst->resource_name, len + 1);
8150 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8151 resource_name, EMACS_CLASS,
8152 xim_instantiate_callback,
8153 /* This is XPointer in XFree86
8154 but (XPointer *) on Tru64, at
8155 least, hence the configure test. */
8156 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8157 #else /* not HAVE_X11R6_XIM */
8158 dpyinfo->xim = NULL;
8159 xim_open_dpy (dpyinfo, resource_name);
8160 #endif /* not HAVE_X11R6_XIM */
8163 else
8164 #endif /* HAVE_XIM */
8165 dpyinfo->xim = NULL;
8169 /* Close the connection to the XIM server on display DPYINFO. */
8171 static void
8172 xim_close_dpy (dpyinfo)
8173 struct x_display_info *dpyinfo;
8175 #ifdef HAVE_XIM
8176 if (use_xim)
8178 #ifdef HAVE_X11R6_XIM
8179 if (dpyinfo->display)
8180 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8181 NULL, EMACS_CLASS,
8182 xim_instantiate_callback, NULL);
8183 #endif /* not HAVE_X11R6_XIM */
8184 if (dpyinfo->display)
8185 XCloseIM (dpyinfo->xim);
8186 dpyinfo->xim = NULL;
8187 XFree (dpyinfo->xim_styles);
8189 #endif /* HAVE_XIM */
8192 #endif /* not HAVE_X11R6_XIM */
8196 /* Calculate the absolute position in frame F
8197 from its current recorded position values and gravity. */
8199 void
8200 x_calc_absolute_position (f)
8201 struct frame *f;
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) */