Don't require xt-mouse.
[emacs.git] / src / xterm.c
blob29c840ff79948ef3f54002d55653f7cde8f02ba6
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007 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 3, 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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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 "systime.h"
59 #ifndef INCLUDED_FCNTL
60 #include <fcntl.h>
61 #endif
62 #include <ctype.h>
63 #include <errno.h>
64 #include <setjmp.h>
65 #include <sys/stat.h>
66 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
67 /* #include <sys/param.h> */
69 #include "charset.h"
70 #include "coding.h"
71 #include "ccl.h"
72 #include "frame.h"
73 #include "dispextern.h"
74 #include "fontset.h"
75 #include "termhooks.h"
76 #include "termopts.h"
77 #include "termchar.h"
78 #include "gnu.h"
79 #include "disptab.h"
80 #include "buffer.h"
81 #include "window.h"
82 #include "keyboard.h"
83 #include "intervals.h"
84 #include "process.h"
85 #include "atimer.h"
86 #include "keymap.h"
88 #ifdef USE_X_TOOLKIT
89 #include <X11/Shell.h>
90 #endif
92 #ifdef HAVE_SYS_TIME_H
93 #include <sys/time.h>
94 #endif
95 #ifdef HAVE_UNISTD_H
96 #include <unistd.h>
97 #endif
99 #ifdef USE_GTK
100 #include "gtkutil.h"
101 #endif
103 #ifdef USE_LUCID
104 extern int xlwmenu_window_p P_ ((Widget w, Window window));
105 extern void xlwmenu_redisplay P_ ((Widget));
106 #endif
108 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
110 extern void free_frame_menubar P_ ((struct frame *));
111 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
112 int));
113 #endif
115 #ifdef USE_X_TOOLKIT
116 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
117 #define HACK_EDITRES
118 extern void _XEditResCheckMessages ();
119 #endif /* not NO_EDITRES */
121 /* Include toolkit specific headers for the scroll bar widget. */
123 #ifdef USE_TOOLKIT_SCROLL_BARS
124 #if defined USE_MOTIF
125 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
126 #include <Xm/ScrollBar.h>
127 #else /* !USE_MOTIF i.e. use Xaw */
129 #ifdef HAVE_XAW3D
130 #include <X11/Xaw3d/Simple.h>
131 #include <X11/Xaw3d/Scrollbar.h>
132 #include <X11/Xaw3d/ThreeD.h>
133 #else /* !HAVE_XAW3D */
134 #include <X11/Xaw/Simple.h>
135 #include <X11/Xaw/Scrollbar.h>
136 #endif /* !HAVE_XAW3D */
137 #ifndef XtNpickTop
138 #define XtNpickTop "pickTop"
139 #endif /* !XtNpickTop */
140 #endif /* !USE_MOTIF */
141 #endif /* USE_TOOLKIT_SCROLL_BARS */
143 #endif /* USE_X_TOOLKIT */
145 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
146 #define x_any_window_to_frame x_window_to_frame
147 #define x_top_window_to_frame x_window_to_frame
148 #endif
150 #ifdef USE_X_TOOLKIT
151 #include "widget.h"
152 #ifndef XtNinitialState
153 #define XtNinitialState "initialState"
154 #endif
155 #endif
157 /* Default to using XIM if available. */
158 #ifdef USE_XIM
159 int use_xim = 1;
160 #else
161 int use_xim = 0; /* configure --without-xim */
162 #endif
166 /* Non-nil means Emacs uses toolkit scroll bars. */
168 Lisp_Object Vx_toolkit_scroll_bars;
170 /* Non-zero means that a HELP_EVENT has been generated since Emacs
171 start. */
173 static int any_help_event_p;
175 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
176 static Lisp_Object last_window;
178 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
180 int x_use_underline_position_properties;
182 /* Non-zero means to draw the underline at the same place as the descent line. */
184 int x_underline_at_descent_line;
186 /* This is a chain of structures for all the X displays currently in
187 use. */
189 struct x_display_info *x_display_list;
191 /* This is a list of cons cells, each of the form (NAME
192 . FONT-LIST-CACHE), one for each element of x_display_list and in
193 the same order. NAME is the name of the frame. FONT-LIST-CACHE
194 records previous values returned by x-list-fonts. */
196 Lisp_Object x_display_name_list;
198 /* Frame being updated by update_frame. This is declared in term.c.
199 This is set by update_begin and looked at by all the XT functions.
200 It is zero while not inside an update. In that case, the XT
201 functions assume that `selected_frame' is the frame to apply to. */
203 extern struct frame *updating_frame;
205 /* This is a frame waiting to be auto-raised, within XTread_socket. */
207 struct frame *pending_autoraise_frame;
209 #ifdef USE_X_TOOLKIT
210 /* The application context for Xt use. */
211 XtAppContext Xt_app_con;
212 static String Xt_default_resources[] = {0};
213 #endif /* USE_X_TOOLKIT */
215 /* Non-zero means user is interacting with a toolkit scroll bar. */
217 static int toolkit_scroll_bar_interaction;
219 /* Non-zero means to not move point as a result of clicking on a
220 frame to focus it (when focus-follows-mouse is nil). */
222 int x_mouse_click_focus_ignore_position;
224 /* Non-zero timeout value means ignore next mouse click if it arrives
225 before that timeout elapses (i.e. as part of the same sequence of
226 events resulting from clicking on a frame to select it). */
228 static unsigned long ignore_next_mouse_click_timeout;
230 /* Mouse movement.
232 Formerly, we used PointerMotionHintMask (in standard_event_mask)
233 so that we would have to call XQueryPointer after each MotionNotify
234 event to ask for another such event. However, this made mouse tracking
235 slow, and there was a bug that made it eventually stop.
237 Simply asking for MotionNotify all the time seems to work better.
239 In order to avoid asking for motion events and then throwing most
240 of them away or busy-polling the server for mouse positions, we ask
241 the server for pointer motion hints. This means that we get only
242 one event per group of mouse movements. "Groups" are delimited by
243 other kinds of events (focus changes and button clicks, for
244 example), or by XQueryPointer calls; when one of these happens, we
245 get another MotionNotify event the next time the mouse moves. This
246 is at least as efficient as getting motion events when mouse
247 tracking is on, and I suspect only negligibly worse when tracking
248 is off. */
250 /* Where the mouse was last time we reported a mouse event. */
252 static XRectangle last_mouse_glyph;
253 static FRAME_PTR last_mouse_glyph_frame;
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 /* Time for last user interaction as returned in X events. */
279 static Time last_user_time;
281 /* Incremented by XTread_socket whenever it really tries to read
282 events. */
284 #ifdef __STDC__
285 static int volatile input_signal_count;
286 #else
287 static int input_signal_count;
288 #endif
290 /* Used locally within XTread_socket. */
292 static int x_noop_count;
294 /* Initial values of argv and argc. */
296 extern char **initial_argv;
297 extern int initial_argc;
299 extern Lisp_Object Vcommand_line_args, Vsystem_name;
301 /* Tells if a window manager is present or not. */
303 extern Lisp_Object Vx_no_window_manager;
305 extern Lisp_Object Qeql;
307 extern int errno;
309 /* A mask of extra modifier bits to put into every keyboard char. */
311 extern EMACS_INT extra_keyboard_modifiers;
313 /* The keysyms to use for the various modifiers. */
315 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
316 Lisp_Object Vx_keysym_table;
317 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
319 static Lisp_Object Qvendor_specific_keysyms;
320 static Lisp_Object Qlatin_1;
322 /* Used in x_flush. */
324 extern Lisp_Object Vinhibit_redisplay;
326 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
327 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
329 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
330 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
331 static const XColor *x_color_cells P_ ((Display *, int *));
332 static void x_update_window_end P_ ((struct window *, int, int));
334 static int x_io_error_quitter P_ ((Display *));
335 static struct terminal *x_create_terminal P_ ((struct x_display_info *));
336 void x_delete_terminal P_ ((struct terminal *));
337 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
338 static int x_compute_min_glyph_bounds P_ ((struct frame *));
339 static void x_update_end P_ ((struct frame *));
340 static void XTframe_up_to_date P_ ((struct frame *));
341 static void XTset_terminal_modes P_ ((struct terminal *));
342 static void XTreset_terminal_modes P_ ((struct terminal *));
343 static void x_clear_frame P_ ((struct frame *));
344 static void frame_highlight P_ ((struct frame *));
345 static void frame_unhighlight P_ ((struct frame *));
346 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
347 static void x_focus_changed P_ ((int, int, struct x_display_info *,
348 struct frame *, struct input_event *));
349 static void x_detect_focus_change P_ ((struct x_display_info *,
350 XEvent *, struct input_event *));
351 static void XTframe_rehighlight P_ ((struct frame *));
352 static void x_frame_rehighlight P_ ((struct x_display_info *));
353 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
354 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
355 enum text_cursor_kinds));
357 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
358 static void x_flush P_ ((struct frame *f));
359 static void x_update_begin P_ ((struct frame *));
360 static void x_update_window_begin P_ ((struct window *));
361 static void x_after_update_window_line P_ ((struct glyph_row *));
362 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
363 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
364 enum scroll_bar_part *,
365 Lisp_Object *, Lisp_Object *,
366 unsigned long *));
367 static void x_check_fullscreen P_ ((struct frame *));
368 static void x_check_expected_move P_ ((struct frame *, int, int));
369 static void x_sync_with_move P_ ((struct frame *, int, int, int));
370 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
371 int *, struct input_event *));
372 /* Don't declare this NO_RETURN because we want no
373 interference with debugging failing X calls. */
374 static SIGTYPE x_connection_closed P_ ((Display *, char *));
377 /* Flush display of frame F, or of all frames if F is null. */
379 static void
380 x_flush (f)
381 struct frame *f;
383 /* Don't call XFlush when it is not safe to redisplay; the X
384 connection may be broken. */
385 if (!NILP (Vinhibit_redisplay))
386 return;
388 BLOCK_INPUT;
389 if (f == NULL)
391 Lisp_Object rest, frame;
392 FOR_EACH_FRAME (rest, frame)
393 if (FRAME_X_P (XFRAME (frame)))
394 x_flush (XFRAME (frame));
396 else if (FRAME_X_P (f))
397 XFlush (FRAME_X_DISPLAY (f));
398 UNBLOCK_INPUT;
402 /* Remove calls to XFlush by defining XFlush to an empty replacement.
403 Calls to XFlush should be unnecessary because the X output buffer
404 is flushed automatically as needed by calls to XPending,
405 XNextEvent, or XWindowEvent according to the XFlush man page.
406 XTread_socket calls XPending. Removing XFlush improves
407 performance. */
409 #define XFlush(DISPLAY) (void) 0
412 /***********************************************************************
413 Debugging
414 ***********************************************************************/
416 #if 0
418 /* This is a function useful for recording debugging information about
419 the sequence of occurrences in this file. */
421 struct record
423 char *locus;
424 int type;
427 struct record event_record[100];
429 int event_record_index;
431 record_event (locus, type)
432 char *locus;
433 int type;
435 if (event_record_index == sizeof (event_record) / sizeof (struct record))
436 event_record_index = 0;
438 event_record[event_record_index].locus = locus;
439 event_record[event_record_index].type = type;
440 event_record_index++;
443 #endif /* 0 */
447 /* Return the struct x_display_info corresponding to DPY. */
449 struct x_display_info *
450 x_display_info_for_display (dpy)
451 Display *dpy;
453 struct x_display_info *dpyinfo;
455 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
456 if (dpyinfo->display == dpy)
457 return dpyinfo;
459 return 0;
464 /***********************************************************************
465 Starting and ending an update
466 ***********************************************************************/
468 /* Start an update of frame F. This function is installed as a hook
469 for update_begin, i.e. it is called when update_begin is called.
470 This function is called prior to calls to x_update_window_begin for
471 each window being updated. Currently, there is nothing to do here
472 because all interesting stuff is done on a window basis. */
474 static void
475 x_update_begin (f)
476 struct frame *f;
478 /* Nothing to do. */
482 /* Start update of window W. Set the global variable updated_window
483 to the window being updated and set output_cursor to the cursor
484 position of W. */
486 static void
487 x_update_window_begin (w)
488 struct window *w;
490 struct frame *f = XFRAME (WINDOW_FRAME (w));
491 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
493 updated_window = w;
494 set_output_cursor (&w->cursor);
496 BLOCK_INPUT;
498 if (f == display_info->mouse_face_mouse_frame)
500 /* Don't do highlighting for mouse motion during the update. */
501 display_info->mouse_face_defer = 1;
503 /* If F needs to be redrawn, simply forget about any prior mouse
504 highlighting. */
505 if (FRAME_GARBAGED_P (f))
506 display_info->mouse_face_window = Qnil;
508 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
509 their mouse_face_p flag set, which means that they are always
510 unequal to rows in a desired matrix which never have that
511 flag set. So, rows containing mouse-face glyphs are never
512 scrolled, and we don't have to switch the mouse highlight off
513 here to prevent it from being scrolled. */
515 /* Can we tell that this update does not affect the window
516 where the mouse highlight is? If so, no need to turn off.
517 Likewise, don't do anything if the frame is garbaged;
518 in that case, the frame's current matrix that we would use
519 is all wrong, and we will redisplay that line anyway. */
520 if (!NILP (display_info->mouse_face_window)
521 && w == XWINDOW (display_info->mouse_face_window))
523 int i;
525 for (i = 0; i < w->desired_matrix->nrows; ++i)
526 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
527 break;
529 if (i < w->desired_matrix->nrows)
530 clear_mouse_face (display_info);
532 #endif /* 0 */
535 UNBLOCK_INPUT;
539 /* Draw a vertical window border from (x,y0) to (x,y1) */
541 static void
542 x_draw_vertical_window_border (w, x, y0, y1)
543 struct window *w;
544 int x, y0, y1;
546 struct frame *f = XFRAME (WINDOW_FRAME (w));
547 struct face *face;
549 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
550 if (face)
551 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
552 face->foreground);
554 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
555 f->output_data.x->normal_gc, x, y0, x, y1);
558 /* End update of window W (which is equal to updated_window).
560 Draw vertical borders between horizontally adjacent windows, and
561 display W's cursor if CURSOR_ON_P is non-zero.
563 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
564 glyphs in mouse-face were overwritten. In that case we have to
565 make sure that the mouse-highlight is properly redrawn.
567 W may be a menu bar pseudo-window in case we don't have X toolkit
568 support. Such windows don't have a cursor, so don't display it
569 here. */
571 static void
572 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
573 struct window *w;
574 int cursor_on_p, mouse_face_overwritten_p;
576 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
578 if (!w->pseudo_window_p)
580 BLOCK_INPUT;
582 if (cursor_on_p)
583 display_and_set_cursor (w, 1, output_cursor.hpos,
584 output_cursor.vpos,
585 output_cursor.x, output_cursor.y);
587 if (draw_window_fringes (w, 1))
588 x_draw_vertical_border (w);
590 UNBLOCK_INPUT;
593 /* If a row with mouse-face was overwritten, arrange for
594 XTframe_up_to_date to redisplay the mouse highlight. */
595 if (mouse_face_overwritten_p)
597 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
598 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
599 dpyinfo->mouse_face_window = Qnil;
602 updated_window = NULL;
606 /* End update of frame F. This function is installed as a hook in
607 update_end. */
609 static void
610 x_update_end (f)
611 struct frame *f;
613 /* Mouse highlight may be displayed again. */
614 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
616 #ifndef XFlush
617 BLOCK_INPUT;
618 XFlush (FRAME_X_DISPLAY (f));
619 UNBLOCK_INPUT;
620 #endif
624 /* This function is called from various places in xdisp.c whenever a
625 complete update has been performed. The global variable
626 updated_window is not available here. */
628 static void
629 XTframe_up_to_date (f)
630 struct frame *f;
632 if (FRAME_X_P (f))
634 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
636 if (dpyinfo->mouse_face_deferred_gc
637 || f == dpyinfo->mouse_face_mouse_frame)
639 BLOCK_INPUT;
640 if (dpyinfo->mouse_face_mouse_frame)
641 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
642 dpyinfo->mouse_face_mouse_x,
643 dpyinfo->mouse_face_mouse_y);
644 dpyinfo->mouse_face_deferred_gc = 0;
645 UNBLOCK_INPUT;
651 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
652 arrow bitmaps, or clear the fringes if no bitmaps are required
653 before DESIRED_ROW is made current. The window being updated is
654 found in updated_window. This function It is called from
655 update_window_line only if it is known that there are differences
656 between bitmaps to be drawn between current row and DESIRED_ROW. */
658 static void
659 x_after_update_window_line (desired_row)
660 struct glyph_row *desired_row;
662 struct window *w = updated_window;
663 struct frame *f;
664 int width, height;
666 xassert (w);
668 if (!desired_row->mode_line_p && !w->pseudo_window_p)
669 desired_row->redraw_fringe_bitmaps_p = 1;
671 /* When a window has disappeared, make sure that no rest of
672 full-width rows stays visible in the internal border. Could
673 check here if updated_window is the leftmost/rightmost window,
674 but I guess it's not worth doing since vertically split windows
675 are almost never used, internal border is rarely set, and the
676 overhead is very small. */
677 if (windows_or_buffers_changed
678 && desired_row->full_width_p
679 && (f = XFRAME (w->frame),
680 width = FRAME_INTERNAL_BORDER_WIDTH (f),
681 width != 0)
682 && (height = desired_row->visible_height,
683 height > 0))
685 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
687 /* Internal border is drawn below the tool bar. */
688 if (WINDOWP (f->tool_bar_window)
689 && w == XWINDOW (f->tool_bar_window))
690 y -= width;
692 BLOCK_INPUT;
693 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
694 0, y, width, height, False);
695 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
696 FRAME_PIXEL_WIDTH (f) - width,
697 y, width, height, False);
698 UNBLOCK_INPUT;
702 static void
703 x_draw_fringe_bitmap (w, row, p)
704 struct window *w;
705 struct glyph_row *row;
706 struct draw_fringe_bitmap_params *p;
708 struct frame *f = XFRAME (WINDOW_FRAME (w));
709 Display *display = FRAME_X_DISPLAY (f);
710 Window window = FRAME_X_WINDOW (f);
711 GC gc = f->output_data.x->normal_gc;
712 struct face *face = p->face;
713 int rowY;
715 /* Must clip because of partially visible lines. */
716 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
717 if (p->y < rowY)
719 /* Adjust position of "bottom aligned" bitmap on partially
720 visible last row. */
721 int oldY = row->y;
722 int oldVH = row->visible_height;
723 row->visible_height = p->h;
724 row->y -= rowY - p->y;
725 x_clip_to_row (w, row, -1, gc);
726 row->y = oldY;
727 row->visible_height = oldVH;
729 else
730 x_clip_to_row (w, row, -1, gc);
732 if (!p->overlay_p)
734 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
736 /* In case the same realized face is used for fringes and
737 for something displayed in the text (e.g. face `region' on
738 mono-displays, the fill style may have been changed to
739 FillSolid in x_draw_glyph_string_background. */
740 if (face->stipple)
741 XSetFillStyle (display, face->gc, FillOpaqueStippled);
742 else
743 XSetForeground (display, face->gc, face->background);
745 #ifdef USE_TOOLKIT_SCROLL_BARS
746 /* If the fringe is adjacent to the left (right) scroll bar of a
747 leftmost (rightmost, respectively) window, then extend its
748 background to the gap between the fringe and the bar. */
749 if ((WINDOW_LEFTMOST_P (w)
750 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
751 || (WINDOW_RIGHTMOST_P (w)
752 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
754 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
756 if (sb_width > 0)
758 int left = WINDOW_SCROLL_BAR_AREA_X (w);
759 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
760 * FRAME_COLUMN_WIDTH (f));
762 if (bx < 0)
764 /* Bitmap fills the fringe. */
765 if (left + width == p->x)
766 bx = left + sb_width;
767 else if (p->x + p->wd == left)
768 bx = left;
769 if (bx >= 0)
771 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
773 nx = width - sb_width;
774 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
775 row->y));
776 ny = row->visible_height;
779 else
781 if (left + width == bx)
783 bx = left + sb_width;
784 nx += width - sb_width;
786 else if (bx + nx == left)
787 nx += width - sb_width;
791 #endif
792 if (bx >= 0 && nx > 0)
793 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
795 if (!face->stipple)
796 XSetForeground (display, face->gc, face->foreground);
799 if (p->which)
801 unsigned char *bits;
802 Pixmap pixmap, clipmask = (Pixmap) 0;
803 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
804 XGCValues gcv;
806 if (p->wd > 8)
807 bits = (unsigned char *)(p->bits + p->dh);
808 else
809 bits = (unsigned char *)p->bits + p->dh;
811 /* Draw the bitmap. I believe these small pixmaps can be cached
812 by the server. */
813 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
814 (p->cursor_p
815 ? (p->overlay_p ? face->background
816 : f->output_data.x->cursor_pixel)
817 : face->foreground),
818 face->background, depth);
820 if (p->overlay_p)
822 clipmask = XCreatePixmapFromBitmapData (display,
823 FRAME_X_DISPLAY_INFO (f)->root_window,
824 bits, p->wd, p->h,
825 1, 0, 1);
826 gcv.clip_mask = clipmask;
827 gcv.clip_x_origin = p->x;
828 gcv.clip_y_origin = p->y;
829 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
832 XCopyArea (display, pixmap, window, gc, 0, 0,
833 p->wd, p->h, p->x, p->y);
834 XFreePixmap (display, pixmap);
836 if (p->overlay_p)
838 gcv.clip_mask = (Pixmap) 0;
839 XChangeGC (display, gc, GCClipMask, &gcv);
840 XFreePixmap (display, clipmask);
844 XSetClipMask (display, gc, None);
849 /* This is called when starting Emacs and when restarting after
850 suspend. When starting Emacs, no X window is mapped. And nothing
851 must be done to Emacs's own window if it is suspended (though that
852 rarely happens). */
854 static void
855 XTset_terminal_modes (struct terminal *terminal)
859 /* This is called when exiting or suspending Emacs. Exiting will make
860 the X-windows go away, and suspending requires no action. */
862 static void
863 XTreset_terminal_modes (struct terminal *terminal)
869 /***********************************************************************
870 Display Iterator
871 ***********************************************************************/
873 /* Function prototypes of this page. */
875 static int x_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
878 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
879 is not contained in the font. */
881 static XCharStruct *
882 x_per_char_metric (font, char2b, font_type)
883 XFontStruct *font;
884 XChar2b *char2b;
885 int font_type; /* unused on X */
887 /* The result metric information. */
888 XCharStruct *pcm = NULL;
890 xassert (font && char2b);
892 if (font->per_char != NULL)
894 if (font->min_byte1 == 0 && font->max_byte1 == 0)
896 /* min_char_or_byte2 specifies the linear character index
897 corresponding to the first element of the per_char array,
898 max_char_or_byte2 is the index of the last character. A
899 character with non-zero CHAR2B->byte1 is not in the font.
900 A character with byte2 less than min_char_or_byte2 or
901 greater max_char_or_byte2 is not in the font. */
902 if (char2b->byte1 == 0
903 && char2b->byte2 >= font->min_char_or_byte2
904 && char2b->byte2 <= font->max_char_or_byte2)
905 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
907 else
909 /* If either min_byte1 or max_byte1 are nonzero, both
910 min_char_or_byte2 and max_char_or_byte2 are less than
911 256, and the 2-byte character index values corresponding
912 to the per_char array element N (counting from 0) are:
914 byte1 = N/D + min_byte1
915 byte2 = N\D + min_char_or_byte2
917 where:
919 D = max_char_or_byte2 - min_char_or_byte2 + 1
920 / = integer division
921 \ = integer modulus */
922 if (char2b->byte1 >= font->min_byte1
923 && char2b->byte1 <= font->max_byte1
924 && char2b->byte2 >= font->min_char_or_byte2
925 && char2b->byte2 <= font->max_char_or_byte2)
927 pcm = (font->per_char
928 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
929 * (char2b->byte1 - font->min_byte1))
930 + (char2b->byte2 - font->min_char_or_byte2));
934 else
936 /* If the per_char pointer is null, all glyphs between the first
937 and last character indexes inclusive have the same
938 information, as given by both min_bounds and max_bounds. */
939 if (char2b->byte2 >= font->min_char_or_byte2
940 && char2b->byte2 <= font->max_char_or_byte2)
941 pcm = &font->max_bounds;
944 return ((pcm == NULL
945 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
946 ? NULL : pcm);
950 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
951 the two-byte form of C. Encoding is returned in *CHAR2B. */
953 static int
954 x_encode_char (c, char2b, font_info, two_byte_p)
955 int c;
956 XChar2b *char2b;
957 struct font_info *font_info;
958 int *two_byte_p;
960 int charset = CHAR_CHARSET (c);
961 XFontStruct *font = font_info->font;
963 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
964 This may be either a program in a special encoder language or a
965 fixed encoding. */
966 if (font_info->font_encoder)
968 /* It's a program. */
969 struct ccl_program *ccl = font_info->font_encoder;
971 check_ccl_update (ccl);
972 if (CHARSET_DIMENSION (charset) == 1)
974 ccl->reg[0] = charset;
975 ccl->reg[1] = char2b->byte2;
976 ccl->reg[2] = -1;
978 else
980 ccl->reg[0] = charset;
981 ccl->reg[1] = char2b->byte1;
982 ccl->reg[2] = char2b->byte2;
985 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
987 /* We assume that MSBs are appropriately set/reset by CCL
988 program. */
989 if (font->max_byte1 == 0) /* 1-byte font */
990 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
991 else
992 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
994 else if (font_info->encoding[charset])
996 /* Fixed encoding scheme. See fontset.h for the meaning of the
997 encoding numbers. */
998 int enc = font_info->encoding[charset];
1000 if ((enc == 1 || enc == 2)
1001 && CHARSET_DIMENSION (charset) == 2)
1002 char2b->byte1 |= 0x80;
1004 if (enc == 1 || enc == 3)
1005 char2b->byte2 |= 0x80;
1008 if (two_byte_p)
1009 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1011 return FONT_TYPE_UNKNOWN;
1016 /***********************************************************************
1017 Glyph display
1018 ***********************************************************************/
1022 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
1023 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
1024 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
1025 int));
1026 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
1027 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
1028 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
1029 static void x_draw_glyph_string P_ ((struct glyph_string *));
1030 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
1031 static void x_set_cursor_gc P_ ((struct glyph_string *));
1032 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
1033 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
1034 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
1035 unsigned long *, double, int));
1036 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
1037 double, int, unsigned long));
1038 static void x_setup_relief_colors P_ ((struct glyph_string *));
1039 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
1040 static void x_draw_image_relief P_ ((struct glyph_string *));
1041 static void x_draw_image_foreground P_ ((struct glyph_string *));
1042 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
1043 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
1044 int, int, int));
1045 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
1046 int, int, int, int, int, int,
1047 XRectangle *));
1048 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
1049 int, int, int, XRectangle *));
1051 #if GLYPH_DEBUG
1052 static void x_check_font P_ ((struct frame *, XFontStruct *));
1053 #endif
1056 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1057 face. */
1059 static void
1060 x_set_cursor_gc (s)
1061 struct glyph_string *s;
1063 if (s->font == FRAME_FONT (s->f)
1064 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1065 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1066 && !s->cmp)
1067 s->gc = s->f->output_data.x->cursor_gc;
1068 else
1070 /* Cursor on non-default face: must merge. */
1071 XGCValues xgcv;
1072 unsigned long mask;
1074 xgcv.background = s->f->output_data.x->cursor_pixel;
1075 xgcv.foreground = s->face->background;
1077 /* If the glyph would be invisible, try a different foreground. */
1078 if (xgcv.foreground == xgcv.background)
1079 xgcv.foreground = s->face->foreground;
1080 if (xgcv.foreground == xgcv.background)
1081 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1082 if (xgcv.foreground == xgcv.background)
1083 xgcv.foreground = s->face->foreground;
1085 /* Make sure the cursor is distinct from text in this face. */
1086 if (xgcv.background == s->face->background
1087 && xgcv.foreground == s->face->foreground)
1089 xgcv.background = s->face->foreground;
1090 xgcv.foreground = s->face->background;
1093 IF_DEBUG (x_check_font (s->f, s->font));
1094 xgcv.font = s->font->fid;
1095 xgcv.graphics_exposures = False;
1096 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1098 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1099 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1100 mask, &xgcv);
1101 else
1102 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1103 = XCreateGC (s->display, s->window, mask, &xgcv);
1105 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1110 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1112 static void
1113 x_set_mouse_face_gc (s)
1114 struct glyph_string *s;
1116 int face_id;
1117 struct face *face;
1119 /* What face has to be used last for the mouse face? */
1120 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1121 face = FACE_FROM_ID (s->f, face_id);
1122 if (face == NULL)
1123 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1125 if (s->first_glyph->type == CHAR_GLYPH)
1126 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
1127 else
1128 face_id = FACE_FOR_CHAR (s->f, face, 0);
1129 s->face = FACE_FROM_ID (s->f, face_id);
1130 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1132 /* If font in this face is same as S->font, use it. */
1133 if (s->font == s->face->font)
1134 s->gc = s->face->gc;
1135 else
1137 /* Otherwise construct scratch_cursor_gc with values from FACE
1138 but font FONT. */
1139 XGCValues xgcv;
1140 unsigned long mask;
1142 xgcv.background = s->face->background;
1143 xgcv.foreground = s->face->foreground;
1144 IF_DEBUG (x_check_font (s->f, s->font));
1145 xgcv.font = s->font->fid;
1146 xgcv.graphics_exposures = False;
1147 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1149 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1150 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1151 mask, &xgcv);
1152 else
1153 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1154 = XCreateGC (s->display, s->window, mask, &xgcv);
1156 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1159 xassert (s->gc != 0);
1163 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1164 Faces to use in the mode line have already been computed when the
1165 matrix was built, so there isn't much to do, here. */
1167 static INLINE void
1168 x_set_mode_line_face_gc (s)
1169 struct glyph_string *s;
1171 s->gc = s->face->gc;
1175 /* Set S->gc of glyph string S for drawing that glyph string. Set
1176 S->stippled_p to a non-zero value if the face of S has a stipple
1177 pattern. */
1179 static INLINE void
1180 x_set_glyph_string_gc (s)
1181 struct glyph_string *s;
1183 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1185 if (s->hl == DRAW_NORMAL_TEXT)
1187 s->gc = s->face->gc;
1188 s->stippled_p = s->face->stipple != 0;
1190 else if (s->hl == DRAW_INVERSE_VIDEO)
1192 x_set_mode_line_face_gc (s);
1193 s->stippled_p = s->face->stipple != 0;
1195 else if (s->hl == DRAW_CURSOR)
1197 x_set_cursor_gc (s);
1198 s->stippled_p = 0;
1200 else if (s->hl == DRAW_MOUSE_FACE)
1202 x_set_mouse_face_gc (s);
1203 s->stippled_p = s->face->stipple != 0;
1205 else if (s->hl == DRAW_IMAGE_RAISED
1206 || s->hl == DRAW_IMAGE_SUNKEN)
1208 s->gc = s->face->gc;
1209 s->stippled_p = s->face->stipple != 0;
1211 else
1213 s->gc = s->face->gc;
1214 s->stippled_p = s->face->stipple != 0;
1217 /* GC must have been set. */
1218 xassert (s->gc != 0);
1222 /* Set clipping for output of glyph string S. S may be part of a mode
1223 line or menu if we don't have X toolkit support. */
1225 static INLINE void
1226 x_set_glyph_string_clipping (s)
1227 struct glyph_string *s;
1229 XRectangle r;
1230 get_glyph_string_clip_rect (s, &r);
1231 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
1235 /* RIF:
1236 Compute left and right overhang of glyph string S. If S is a glyph
1237 string for a composition, assume overhangs don't exist. */
1239 static void
1240 x_compute_glyph_string_overhangs (s)
1241 struct glyph_string *s;
1243 if (s->cmp == NULL
1244 && s->first_glyph->type == CHAR_GLYPH)
1246 XCharStruct cs;
1247 int direction, font_ascent, font_descent;
1248 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1249 &font_ascent, &font_descent, &cs);
1250 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1251 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1256 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1258 static INLINE void
1259 x_clear_glyph_string_rect (s, x, y, w, h)
1260 struct glyph_string *s;
1261 int x, y, w, h;
1263 XGCValues xgcv;
1264 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1265 XSetForeground (s->display, s->gc, xgcv.background);
1266 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1267 XSetForeground (s->display, s->gc, xgcv.foreground);
1271 /* Draw the background of glyph_string S. If S->background_filled_p
1272 is non-zero don't draw it. FORCE_P non-zero means draw the
1273 background even if it wouldn't be drawn normally. This is used
1274 when a string preceding S draws into the background of S, or S
1275 contains the first component of a composition. */
1277 static void
1278 x_draw_glyph_string_background (s, force_p)
1279 struct glyph_string *s;
1280 int force_p;
1282 /* Nothing to do if background has already been drawn or if it
1283 shouldn't be drawn in the first place. */
1284 if (!s->background_filled_p)
1286 int box_line_width = max (s->face->box_line_width, 0);
1288 if (s->stippled_p)
1290 /* Fill background with a stipple pattern. */
1291 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1292 XFillRectangle (s->display, s->window, s->gc, s->x,
1293 s->y + box_line_width,
1294 s->background_width,
1295 s->height - 2 * box_line_width);
1296 XSetFillStyle (s->display, s->gc, FillSolid);
1297 s->background_filled_p = 1;
1299 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1300 || s->font_not_found_p
1301 || s->extends_to_end_of_line_p
1302 || force_p)
1304 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1305 s->background_width,
1306 s->height - 2 * box_line_width);
1307 s->background_filled_p = 1;
1313 /* Draw the foreground of glyph string S. */
1315 static void
1316 x_draw_glyph_string_foreground (s)
1317 struct glyph_string *s;
1319 int i, x;
1321 /* If first glyph of S has a left box line, start drawing the text
1322 of S to the right of that box line. */
1323 if (s->face->box != FACE_NO_BOX
1324 && s->first_glyph->left_box_line_p)
1325 x = s->x + eabs (s->face->box_line_width);
1326 else
1327 x = s->x;
1329 /* Draw characters of S as rectangles if S's font could not be
1330 loaded. */
1331 if (s->font_not_found_p)
1333 for (i = 0; i < s->nchars; ++i)
1335 struct glyph *g = s->first_glyph + i;
1336 XDrawRectangle (s->display, s->window,
1337 s->gc, x, s->y, g->pixel_width - 1,
1338 s->height - 1);
1339 x += g->pixel_width;
1342 else
1344 char *char1b = (char *) s->char2b;
1345 int boff = s->font_info->baseline_offset;
1347 if (s->font_info->vertical_centering)
1348 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1350 /* If we can use 8-bit functions, condense S->char2b. */
1351 if (!s->two_byte_p)
1352 for (i = 0; i < s->nchars; ++i)
1353 char1b[i] = s->char2b[i].byte2;
1355 /* Draw text with XDrawString if background has already been
1356 filled. Otherwise, use XDrawImageString. (Note that
1357 XDrawImageString is usually faster than XDrawString.) Always
1358 use XDrawImageString when drawing the cursor so that there is
1359 no chance that characters under a box cursor are invisible. */
1360 if (s->for_overlaps
1361 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1363 /* Draw characters with 16-bit or 8-bit functions. */
1364 if (s->two_byte_p)
1365 XDrawString16 (s->display, s->window, s->gc, x,
1366 s->ybase - boff, s->char2b, s->nchars);
1367 else
1368 XDrawString (s->display, s->window, s->gc, x,
1369 s->ybase - boff, char1b, s->nchars);
1371 else
1373 if (s->two_byte_p)
1374 XDrawImageString16 (s->display, s->window, s->gc, x,
1375 s->ybase - boff, s->char2b, s->nchars);
1376 else
1377 XDrawImageString (s->display, s->window, s->gc, x,
1378 s->ybase - boff, char1b, s->nchars);
1381 if (s->face->overstrike)
1383 /* For overstriking (to simulate bold-face), draw the
1384 characters again shifted to the right by one pixel. */
1385 if (s->two_byte_p)
1386 XDrawString16 (s->display, s->window, s->gc, x + 1,
1387 s->ybase - boff, s->char2b, s->nchars);
1388 else
1389 XDrawString (s->display, s->window, s->gc, x + 1,
1390 s->ybase - boff, char1b, s->nchars);
1395 /* Draw the foreground of composite glyph string S. */
1397 static void
1398 x_draw_composite_glyph_string_foreground (s)
1399 struct glyph_string *s;
1401 int i, x;
1403 /* If first glyph of S has a left box line, start drawing the text
1404 of S to the right of that box line. */
1405 if (s->face->box != FACE_NO_BOX
1406 && s->first_glyph->left_box_line_p)
1407 x = s->x + eabs (s->face->box_line_width);
1408 else
1409 x = s->x;
1411 /* S is a glyph string for a composition. S->gidx is the index of
1412 the first character drawn for glyphs of this composition.
1413 S->gidx == 0 means we are drawing the very first character of
1414 this composition. */
1416 /* Draw a rectangle for the composition if the font for the very
1417 first character of the composition could not be loaded. */
1418 if (s->font_not_found_p)
1420 if (s->gidx == 0)
1421 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1422 s->width - 1, s->height - 1);
1424 else
1426 for (i = 0; i < s->nchars; i++, ++s->gidx)
1428 XDrawString16 (s->display, s->window, s->gc,
1429 x + s->cmp->offsets[s->gidx * 2],
1430 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1431 s->char2b + i, 1);
1432 if (s->face->overstrike)
1433 XDrawString16 (s->display, s->window, s->gc,
1434 x + s->cmp->offsets[s->gidx * 2] + 1,
1435 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1436 s->char2b + i, 1);
1442 #ifdef USE_X_TOOLKIT
1444 static struct frame *x_frame_of_widget P_ ((Widget));
1445 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1446 XrmValue *, XrmValue *, XtPointer *));
1447 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1448 XrmValue *, Cardinal *));
1451 /* Return the frame on which widget WIDGET is used.. Abort if frame
1452 cannot be determined. */
1454 static struct frame *
1455 x_frame_of_widget (widget)
1456 Widget widget;
1458 struct x_display_info *dpyinfo;
1459 Lisp_Object tail;
1460 struct frame *f;
1462 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1464 /* Find the top-level shell of the widget. Note that this function
1465 can be called when the widget is not yet realized, so XtWindow
1466 (widget) == 0. That's the reason we can't simply use
1467 x_any_window_to_frame. */
1468 while (!XtIsTopLevelShell (widget))
1469 widget = XtParent (widget);
1471 /* Look for a frame with that top-level widget. Allocate the color
1472 on that frame to get the right gamma correction value. */
1473 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1474 if (GC_FRAMEP (XCAR (tail))
1475 && (f = XFRAME (XCAR (tail)),
1476 (FRAME_X_P (f)
1477 && f->output_data.nothing != 1
1478 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1479 && f->output_data.x->widget == widget)
1480 return f;
1482 abort ();
1486 /* Allocate the color COLOR->pixel on the screen and display of
1487 widget WIDGET in colormap CMAP. If an exact match cannot be
1488 allocated, try the nearest color available. Value is non-zero
1489 if successful. This is called from lwlib. */
1492 x_alloc_nearest_color_for_widget (widget, cmap, color)
1493 Widget widget;
1494 Colormap cmap;
1495 XColor *color;
1497 struct frame *f = x_frame_of_widget (widget);
1498 return x_alloc_nearest_color (f, cmap, color);
1502 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1503 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1504 If this produces the same color as PIXEL, try a color where all RGB
1505 values have DELTA added. Return the allocated color in *PIXEL.
1506 DISPLAY is the X display, CMAP is the colormap to operate on.
1507 Value is non-zero if successful. */
1510 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1511 Widget widget;
1512 Display *display;
1513 Colormap cmap;
1514 unsigned long *pixel;
1515 double factor;
1516 int delta;
1518 struct frame *f = x_frame_of_widget (widget);
1519 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1523 /* Structure specifying which arguments should be passed by Xt to
1524 cvt_string_to_pixel. We want the widget's screen and colormap. */
1526 static XtConvertArgRec cvt_string_to_pixel_args[] =
1528 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1529 sizeof (Screen *)},
1530 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1531 sizeof (Colormap)}
1535 /* The address of this variable is returned by
1536 cvt_string_to_pixel. */
1538 static Pixel cvt_string_to_pixel_value;
1541 /* Convert a color name to a pixel color.
1543 DPY is the display we are working on.
1545 ARGS is an array of *NARGS XrmValue structures holding additional
1546 information about the widget for which the conversion takes place.
1547 The contents of this array are determined by the specification
1548 in cvt_string_to_pixel_args.
1550 FROM is a pointer to an XrmValue which points to the color name to
1551 convert. TO is an XrmValue in which to return the pixel color.
1553 CLOSURE_RET is a pointer to user-data, in which we record if
1554 we allocated the color or not.
1556 Value is True if successful, False otherwise. */
1558 static Boolean
1559 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1560 Display *dpy;
1561 XrmValue *args;
1562 Cardinal *nargs;
1563 XrmValue *from, *to;
1564 XtPointer *closure_ret;
1566 Screen *screen;
1567 Colormap cmap;
1568 Pixel pixel;
1569 String color_name;
1570 XColor color;
1572 if (*nargs != 2)
1574 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1575 "wrongParameters", "cvt_string_to_pixel",
1576 "XtToolkitError",
1577 "Screen and colormap args required", NULL, NULL);
1578 return False;
1581 screen = *(Screen **) args[0].addr;
1582 cmap = *(Colormap *) args[1].addr;
1583 color_name = (String) from->addr;
1585 if (strcmp (color_name, XtDefaultBackground) == 0)
1587 *closure_ret = (XtPointer) False;
1588 pixel = WhitePixelOfScreen (screen);
1590 else if (strcmp (color_name, XtDefaultForeground) == 0)
1592 *closure_ret = (XtPointer) False;
1593 pixel = BlackPixelOfScreen (screen);
1595 else if (XParseColor (dpy, cmap, color_name, &color)
1596 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1598 pixel = color.pixel;
1599 *closure_ret = (XtPointer) True;
1601 else
1603 String params[1];
1604 Cardinal nparams = 1;
1606 params[0] = color_name;
1607 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1608 "badValue", "cvt_string_to_pixel",
1609 "XtToolkitError", "Invalid color `%s'",
1610 params, &nparams);
1611 return False;
1614 if (to->addr != NULL)
1616 if (to->size < sizeof (Pixel))
1618 to->size = sizeof (Pixel);
1619 return False;
1622 *(Pixel *) to->addr = pixel;
1624 else
1626 cvt_string_to_pixel_value = pixel;
1627 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1630 to->size = sizeof (Pixel);
1631 return True;
1635 /* Free a pixel color which was previously allocated via
1636 cvt_string_to_pixel. This is registered as the destructor
1637 for this type of resource via XtSetTypeConverter.
1639 APP is the application context in which we work.
1641 TO is a pointer to an XrmValue holding the color to free.
1642 CLOSURE is the value we stored in CLOSURE_RET for this color
1643 in cvt_string_to_pixel.
1645 ARGS and NARGS are like for cvt_string_to_pixel. */
1647 static void
1648 cvt_pixel_dtor (app, to, closure, args, nargs)
1649 XtAppContext app;
1650 XrmValuePtr to;
1651 XtPointer closure;
1652 XrmValuePtr args;
1653 Cardinal *nargs;
1655 if (*nargs != 2)
1657 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1658 "XtToolkitError",
1659 "Screen and colormap arguments required",
1660 NULL, NULL);
1662 else if (closure != NULL)
1664 /* We did allocate the pixel, so free it. */
1665 Screen *screen = *(Screen **) args[0].addr;
1666 Colormap cmap = *(Colormap *) args[1].addr;
1667 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1668 (Pixel *) to->addr, 1);
1673 #endif /* USE_X_TOOLKIT */
1676 /* Value is an array of XColor structures for the contents of the
1677 color map of display DPY. Set *NCELLS to the size of the array.
1678 Note that this probably shouldn't be called for large color maps,
1679 say a 24-bit TrueColor map. */
1681 static const XColor *
1682 x_color_cells (dpy, ncells)
1683 Display *dpy;
1684 int *ncells;
1686 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1688 if (dpyinfo->color_cells == NULL)
1690 Screen *screen = dpyinfo->screen;
1691 int i;
1693 dpyinfo->ncolor_cells
1694 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1695 dpyinfo->color_cells
1696 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1697 * sizeof *dpyinfo->color_cells);
1699 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1700 dpyinfo->color_cells[i].pixel = i;
1702 XQueryColors (dpy, dpyinfo->cmap,
1703 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1706 *ncells = dpyinfo->ncolor_cells;
1707 return dpyinfo->color_cells;
1711 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1712 colors in COLORS. Use cached information, if available. */
1714 void
1715 x_query_colors (f, colors, ncolors)
1716 struct frame *f;
1717 XColor *colors;
1718 int ncolors;
1720 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1722 if (dpyinfo->color_cells)
1724 int i;
1725 for (i = 0; i < ncolors; ++i)
1727 unsigned long pixel = colors[i].pixel;
1728 xassert (pixel < dpyinfo->ncolor_cells);
1729 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1730 colors[i] = dpyinfo->color_cells[pixel];
1733 else
1734 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1738 /* On frame F, translate pixel color to RGB values for the color in
1739 COLOR. Use cached information, if available. */
1741 void
1742 x_query_color (f, color)
1743 struct frame *f;
1744 XColor *color;
1746 x_query_colors (f, color, 1);
1750 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1751 exact match can't be allocated, try the nearest color available.
1752 Value is non-zero if successful. Set *COLOR to the color
1753 allocated. */
1755 static int
1756 x_alloc_nearest_color_1 (dpy, cmap, color)
1757 Display *dpy;
1758 Colormap cmap;
1759 XColor *color;
1761 int rc;
1763 rc = XAllocColor (dpy, cmap, color);
1764 if (rc == 0)
1766 /* If we got to this point, the colormap is full, so we're going
1767 to try to get the next closest color. The algorithm used is
1768 a least-squares matching, which is what X uses for closest
1769 color matching with StaticColor visuals. */
1770 int nearest, i;
1771 unsigned long nearest_delta = ~0;
1772 int ncells;
1773 const XColor *cells = x_color_cells (dpy, &ncells);
1775 for (nearest = i = 0; i < ncells; ++i)
1777 long dred = (color->red >> 8) - (cells[i].red >> 8);
1778 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1779 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1780 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1782 if (delta < nearest_delta)
1784 nearest = i;
1785 nearest_delta = delta;
1789 color->red = cells[nearest].red;
1790 color->green = cells[nearest].green;
1791 color->blue = cells[nearest].blue;
1792 rc = XAllocColor (dpy, cmap, color);
1794 else
1796 /* If allocation succeeded, and the allocated pixel color is not
1797 equal to a cached pixel color recorded earlier, there was a
1798 change in the colormap, so clear the color cache. */
1799 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1800 XColor *cached_color;
1802 if (dpyinfo->color_cells
1803 && (cached_color = &dpyinfo->color_cells[color->pixel],
1804 (cached_color->red != color->red
1805 || cached_color->blue != color->blue
1806 || cached_color->green != color->green)))
1808 xfree (dpyinfo->color_cells);
1809 dpyinfo->color_cells = NULL;
1810 dpyinfo->ncolor_cells = 0;
1814 #ifdef DEBUG_X_COLORS
1815 if (rc)
1816 register_color (color->pixel);
1817 #endif /* DEBUG_X_COLORS */
1819 return rc;
1823 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1824 exact match can't be allocated, try the nearest color available.
1825 Value is non-zero if successful. Set *COLOR to the color
1826 allocated. */
1829 x_alloc_nearest_color (f, cmap, color)
1830 struct frame *f;
1831 Colormap cmap;
1832 XColor *color;
1834 gamma_correct (f, color);
1835 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1839 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1840 It's necessary to do this instead of just using PIXEL directly to
1841 get color reference counts right. */
1843 unsigned long
1844 x_copy_color (f, pixel)
1845 struct frame *f;
1846 unsigned long pixel;
1848 XColor color;
1850 color.pixel = pixel;
1851 BLOCK_INPUT;
1852 x_query_color (f, &color);
1853 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1854 UNBLOCK_INPUT;
1855 #ifdef DEBUG_X_COLORS
1856 register_color (pixel);
1857 #endif
1858 return color.pixel;
1862 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1863 It's necessary to do this instead of just using PIXEL directly to
1864 get color reference counts right. */
1866 unsigned long
1867 x_copy_dpy_color (dpy, cmap, pixel)
1868 Display *dpy;
1869 Colormap cmap;
1870 unsigned long pixel;
1872 XColor color;
1874 color.pixel = pixel;
1875 BLOCK_INPUT;
1876 XQueryColor (dpy, cmap, &color);
1877 XAllocColor (dpy, cmap, &color);
1878 UNBLOCK_INPUT;
1879 #ifdef DEBUG_X_COLORS
1880 register_color (pixel);
1881 #endif
1882 return color.pixel;
1886 /* Brightness beyond which a color won't have its highlight brightness
1887 boosted.
1889 Nominally, highlight colors for `3d' faces are calculated by
1890 brightening an object's color by a constant scale factor, but this
1891 doesn't yield good results for dark colors, so for colors who's
1892 brightness is less than this value (on a scale of 0-65535) have an
1893 use an additional additive factor.
1895 The value here is set so that the default menu-bar/mode-line color
1896 (grey75) will not have its highlights changed at all. */
1897 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1900 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1901 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1902 If this produces the same color as PIXEL, try a color where all RGB
1903 values have DELTA added. Return the allocated color in *PIXEL.
1904 DISPLAY is the X display, CMAP is the colormap to operate on.
1905 Value is non-zero if successful. */
1907 static int
1908 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1909 struct frame *f;
1910 Display *display;
1911 Colormap cmap;
1912 unsigned long *pixel;
1913 double factor;
1914 int delta;
1916 XColor color, new;
1917 long bright;
1918 int success_p;
1920 /* Get RGB color values. */
1921 color.pixel = *pixel;
1922 x_query_color (f, &color);
1924 /* Change RGB values by specified FACTOR. Avoid overflow! */
1925 xassert (factor >= 0);
1926 new.red = min (0xffff, factor * color.red);
1927 new.green = min (0xffff, factor * color.green);
1928 new.blue = min (0xffff, factor * color.blue);
1930 /* Calculate brightness of COLOR. */
1931 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1933 /* We only boost colors that are darker than
1934 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1935 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1936 /* Make an additive adjustment to NEW, because it's dark enough so
1937 that scaling by FACTOR alone isn't enough. */
1939 /* How far below the limit this color is (0 - 1, 1 being darker). */
1940 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1941 /* The additive adjustment. */
1942 int min_delta = delta * dimness * factor / 2;
1944 if (factor < 1)
1946 new.red = max (0, new.red - min_delta);
1947 new.green = max (0, new.green - min_delta);
1948 new.blue = max (0, new.blue - min_delta);
1950 else
1952 new.red = min (0xffff, min_delta + new.red);
1953 new.green = min (0xffff, min_delta + new.green);
1954 new.blue = min (0xffff, min_delta + new.blue);
1958 /* Try to allocate the color. */
1959 success_p = x_alloc_nearest_color (f, cmap, &new);
1960 if (success_p)
1962 if (new.pixel == *pixel)
1964 /* If we end up with the same color as before, try adding
1965 delta to the RGB values. */
1966 x_free_colors (f, &new.pixel, 1);
1968 new.red = min (0xffff, delta + color.red);
1969 new.green = min (0xffff, delta + color.green);
1970 new.blue = min (0xffff, delta + color.blue);
1971 success_p = x_alloc_nearest_color (f, cmap, &new);
1973 else
1974 success_p = 1;
1975 *pixel = new.pixel;
1978 return success_p;
1982 /* Set up the foreground color for drawing relief lines of glyph
1983 string S. RELIEF is a pointer to a struct relief containing the GC
1984 with which lines will be drawn. Use a color that is FACTOR or
1985 DELTA lighter or darker than the relief's background which is found
1986 in S->f->output_data.x->relief_background. If such a color cannot
1987 be allocated, use DEFAULT_PIXEL, instead. */
1989 static void
1990 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1991 struct frame *f;
1992 struct relief *relief;
1993 double factor;
1994 int delta;
1995 unsigned long default_pixel;
1997 XGCValues xgcv;
1998 struct x_output *di = f->output_data.x;
1999 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2000 unsigned long pixel;
2001 unsigned long background = di->relief_background;
2002 Colormap cmap = FRAME_X_COLORMAP (f);
2003 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2004 Display *dpy = FRAME_X_DISPLAY (f);
2006 xgcv.graphics_exposures = False;
2007 xgcv.line_width = 1;
2009 /* Free previously allocated color. The color cell will be reused
2010 when it has been freed as many times as it was allocated, so this
2011 doesn't affect faces using the same colors. */
2012 if (relief->gc
2013 && relief->allocated_p)
2015 x_free_colors (f, &relief->pixel, 1);
2016 relief->allocated_p = 0;
2019 /* Allocate new color. */
2020 xgcv.foreground = default_pixel;
2021 pixel = background;
2022 if (dpyinfo->n_planes != 1
2023 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2025 relief->allocated_p = 1;
2026 xgcv.foreground = relief->pixel = pixel;
2029 if (relief->gc == 0)
2031 xgcv.stipple = dpyinfo->gray;
2032 mask |= GCStipple;
2033 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
2035 else
2036 XChangeGC (dpy, relief->gc, mask, &xgcv);
2040 /* Set up colors for the relief lines around glyph string S. */
2042 static void
2043 x_setup_relief_colors (s)
2044 struct glyph_string *s;
2046 struct x_output *di = s->f->output_data.x;
2047 unsigned long color;
2049 if (s->face->use_box_color_for_shadows_p)
2050 color = s->face->box_color;
2051 else if (s->first_glyph->type == IMAGE_GLYPH
2052 && s->img->pixmap
2053 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2054 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2055 else
2057 XGCValues xgcv;
2059 /* Get the background color of the face. */
2060 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2061 color = xgcv.background;
2064 if (di->white_relief.gc == 0
2065 || color != di->relief_background)
2067 di->relief_background = color;
2068 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2069 WHITE_PIX_DEFAULT (s->f));
2070 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2071 BLACK_PIX_DEFAULT (s->f));
2076 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2077 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2078 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2079 relief. LEFT_P non-zero means draw a relief on the left side of
2080 the rectangle. RIGHT_P non-zero means draw a relief on the right
2081 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2082 when drawing. */
2084 static void
2085 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2086 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2087 struct frame *f;
2088 int left_x, top_y, right_x, bottom_y, width;
2089 int top_p, bot_p, left_p, right_p, raised_p;
2090 XRectangle *clip_rect;
2092 Display *dpy = FRAME_X_DISPLAY (f);
2093 Window window = FRAME_X_WINDOW (f);
2094 int i;
2095 GC gc;
2097 if (raised_p)
2098 gc = f->output_data.x->white_relief.gc;
2099 else
2100 gc = f->output_data.x->black_relief.gc;
2101 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2103 /* Top. */
2104 if (top_p)
2105 for (i = 0; i < width; ++i)
2106 XDrawLine (dpy, window, gc,
2107 left_x + i * left_p, top_y + i,
2108 right_x + 1 - i * right_p, top_y + i);
2110 /* Left. */
2111 if (left_p)
2112 for (i = 0; i < width; ++i)
2113 XDrawLine (dpy, window, gc,
2114 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2116 XSetClipMask (dpy, gc, None);
2117 if (raised_p)
2118 gc = f->output_data.x->black_relief.gc;
2119 else
2120 gc = f->output_data.x->white_relief.gc;
2121 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2123 /* Bottom. */
2124 if (bot_p)
2125 for (i = 0; i < width; ++i)
2126 XDrawLine (dpy, window, gc,
2127 left_x + i * left_p, bottom_y - i,
2128 right_x + 1 - i * right_p, bottom_y - i);
2130 /* Right. */
2131 if (right_p)
2132 for (i = 0; i < width; ++i)
2133 XDrawLine (dpy, window, gc,
2134 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2136 XSetClipMask (dpy, gc, None);
2140 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2141 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2142 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2143 left side of the rectangle. RIGHT_P non-zero means draw a line
2144 on the right side of the rectangle. CLIP_RECT is the clipping
2145 rectangle to use when drawing. */
2147 static void
2148 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2149 left_p, right_p, clip_rect)
2150 struct glyph_string *s;
2151 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2152 XRectangle *clip_rect;
2154 XGCValues xgcv;
2156 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2157 XSetForeground (s->display, s->gc, s->face->box_color);
2158 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2160 /* Top. */
2161 XFillRectangle (s->display, s->window, s->gc,
2162 left_x, top_y, right_x - left_x + 1, width);
2164 /* Left. */
2165 if (left_p)
2166 XFillRectangle (s->display, s->window, s->gc,
2167 left_x, top_y, width, bottom_y - top_y + 1);
2169 /* Bottom. */
2170 XFillRectangle (s->display, s->window, s->gc,
2171 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2173 /* Right. */
2174 if (right_p)
2175 XFillRectangle (s->display, s->window, s->gc,
2176 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2178 XSetForeground (s->display, s->gc, xgcv.foreground);
2179 XSetClipMask (s->display, s->gc, None);
2183 /* Draw a box around glyph string S. */
2185 static void
2186 x_draw_glyph_string_box (s)
2187 struct glyph_string *s;
2189 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2190 int left_p, right_p;
2191 struct glyph *last_glyph;
2192 XRectangle clip_rect;
2194 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2195 ? WINDOW_RIGHT_EDGE_X (s->w)
2196 : window_box_right (s->w, s->area));
2198 /* The glyph that may have a right box line. */
2199 last_glyph = (s->cmp || s->img
2200 ? s->first_glyph
2201 : s->first_glyph + s->nchars - 1);
2203 width = eabs (s->face->box_line_width);
2204 raised_p = s->face->box == FACE_RAISED_BOX;
2205 left_x = s->x;
2206 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2207 ? last_x - 1
2208 : min (last_x, s->x + s->background_width) - 1);
2209 top_y = s->y;
2210 bottom_y = top_y + s->height - 1;
2212 left_p = (s->first_glyph->left_box_line_p
2213 || (s->hl == DRAW_MOUSE_FACE
2214 && (s->prev == NULL
2215 || s->prev->hl != s->hl)));
2216 right_p = (last_glyph->right_box_line_p
2217 || (s->hl == DRAW_MOUSE_FACE
2218 && (s->next == NULL
2219 || s->next->hl != s->hl)));
2221 get_glyph_string_clip_rect (s, &clip_rect);
2223 if (s->face->box == FACE_SIMPLE_BOX)
2224 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2225 left_p, right_p, &clip_rect);
2226 else
2228 x_setup_relief_colors (s);
2229 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2230 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2235 /* Draw foreground of image glyph string S. */
2237 static void
2238 x_draw_image_foreground (s)
2239 struct glyph_string *s;
2241 int x = s->x;
2242 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2244 /* If first glyph of S has a left box line, start drawing it to the
2245 right of that line. */
2246 if (s->face->box != FACE_NO_BOX
2247 && s->first_glyph->left_box_line_p
2248 && s->slice.x == 0)
2249 x += eabs (s->face->box_line_width);
2251 /* If there is a margin around the image, adjust x- and y-position
2252 by that margin. */
2253 if (s->slice.x == 0)
2254 x += s->img->hmargin;
2255 if (s->slice.y == 0)
2256 y += s->img->vmargin;
2258 if (s->img->pixmap)
2260 if (s->img->mask)
2262 /* We can't set both a clip mask and use XSetClipRectangles
2263 because the latter also sets a clip mask. We also can't
2264 trust on the shape extension to be available
2265 (XShapeCombineRegion). So, compute the rectangle to draw
2266 manually. */
2267 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2268 | GCFunction);
2269 XGCValues xgcv;
2270 XRectangle clip_rect, image_rect, r;
2272 xgcv.clip_mask = s->img->mask;
2273 xgcv.clip_x_origin = x;
2274 xgcv.clip_y_origin = y;
2275 xgcv.function = GXcopy;
2276 XChangeGC (s->display, s->gc, mask, &xgcv);
2278 get_glyph_string_clip_rect (s, &clip_rect);
2279 image_rect.x = x;
2280 image_rect.y = y;
2281 image_rect.width = s->slice.width;
2282 image_rect.height = s->slice.height;
2283 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2284 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2285 s->slice.x + r.x - x, s->slice.y + r.y - y,
2286 r.width, r.height, r.x, r.y);
2288 else
2290 XRectangle clip_rect, image_rect, r;
2292 get_glyph_string_clip_rect (s, &clip_rect);
2293 image_rect.x = x;
2294 image_rect.y = y;
2295 image_rect.width = s->slice.width;
2296 image_rect.height = s->slice.height;
2297 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2298 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2299 s->slice.x + r.x - x, s->slice.y + r.y - y,
2300 r.width, r.height, r.x, r.y);
2302 /* When the image has a mask, we can expect that at
2303 least part of a mouse highlight or a block cursor will
2304 be visible. If the image doesn't have a mask, make
2305 a block cursor visible by drawing a rectangle around
2306 the image. I believe it's looking better if we do
2307 nothing here for mouse-face. */
2308 if (s->hl == DRAW_CURSOR)
2310 int r = s->img->relief;
2311 if (r < 0) r = -r;
2312 XDrawRectangle (s->display, s->window, s->gc,
2313 x - r, y - r,
2314 s->slice.width + r*2 - 1,
2315 s->slice.height + r*2 - 1);
2319 else
2320 /* Draw a rectangle if image could not be loaded. */
2321 XDrawRectangle (s->display, s->window, s->gc, x, y,
2322 s->slice.width - 1, s->slice.height - 1);
2326 /* Draw a relief around the image glyph string S. */
2328 static void
2329 x_draw_image_relief (s)
2330 struct glyph_string *s;
2332 int x0, y0, x1, y1, thick, raised_p;
2333 XRectangle r;
2334 int x = s->x;
2335 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2337 /* If first glyph of S has a left box line, start drawing it to the
2338 right of that line. */
2339 if (s->face->box != FACE_NO_BOX
2340 && s->first_glyph->left_box_line_p
2341 && s->slice.x == 0)
2342 x += eabs (s->face->box_line_width);
2344 /* If there is a margin around the image, adjust x- and y-position
2345 by that margin. */
2346 if (s->slice.x == 0)
2347 x += s->img->hmargin;
2348 if (s->slice.y == 0)
2349 y += s->img->vmargin;
2351 if (s->hl == DRAW_IMAGE_SUNKEN
2352 || s->hl == DRAW_IMAGE_RAISED)
2354 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2355 raised_p = s->hl == DRAW_IMAGE_RAISED;
2357 else
2359 thick = eabs (s->img->relief);
2360 raised_p = s->img->relief > 0;
2363 x0 = x - thick;
2364 y0 = y - thick;
2365 x1 = x + s->slice.width + thick - 1;
2366 y1 = y + s->slice.height + thick - 1;
2368 x_setup_relief_colors (s);
2369 get_glyph_string_clip_rect (s, &r);
2370 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2371 s->slice.y == 0,
2372 s->slice.y + s->slice.height == s->img->height,
2373 s->slice.x == 0,
2374 s->slice.x + s->slice.width == s->img->width,
2375 &r);
2379 /* Draw the foreground of image glyph string S to PIXMAP. */
2381 static void
2382 x_draw_image_foreground_1 (s, pixmap)
2383 struct glyph_string *s;
2384 Pixmap pixmap;
2386 int x = 0;
2387 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2389 /* If first glyph of S has a left box line, start drawing it to the
2390 right of that line. */
2391 if (s->face->box != FACE_NO_BOX
2392 && s->first_glyph->left_box_line_p
2393 && s->slice.x == 0)
2394 x += eabs (s->face->box_line_width);
2396 /* If there is a margin around the image, adjust x- and y-position
2397 by that margin. */
2398 if (s->slice.x == 0)
2399 x += s->img->hmargin;
2400 if (s->slice.y == 0)
2401 y += s->img->vmargin;
2403 if (s->img->pixmap)
2405 if (s->img->mask)
2407 /* We can't set both a clip mask and use XSetClipRectangles
2408 because the latter also sets a clip mask. We also can't
2409 trust on the shape extension to be available
2410 (XShapeCombineRegion). So, compute the rectangle to draw
2411 manually. */
2412 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2413 | GCFunction);
2414 XGCValues xgcv;
2416 xgcv.clip_mask = s->img->mask;
2417 xgcv.clip_x_origin = x - s->slice.x;
2418 xgcv.clip_y_origin = y - s->slice.y;
2419 xgcv.function = GXcopy;
2420 XChangeGC (s->display, s->gc, mask, &xgcv);
2422 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2423 s->slice.x, s->slice.y,
2424 s->slice.width, s->slice.height, x, y);
2425 XSetClipMask (s->display, s->gc, None);
2427 else
2429 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2430 s->slice.x, s->slice.y,
2431 s->slice.width, s->slice.height, x, y);
2433 /* When the image has a mask, we can expect that at
2434 least part of a mouse highlight or a block cursor will
2435 be visible. If the image doesn't have a mask, make
2436 a block cursor visible by drawing a rectangle around
2437 the image. I believe it's looking better if we do
2438 nothing here for mouse-face. */
2439 if (s->hl == DRAW_CURSOR)
2441 int r = s->img->relief;
2442 if (r < 0) r = -r;
2443 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2444 s->slice.width + r*2 - 1,
2445 s->slice.height + r*2 - 1);
2449 else
2450 /* Draw a rectangle if image could not be loaded. */
2451 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2452 s->slice.width - 1, s->slice.height - 1);
2456 /* Draw part of the background of glyph string S. X, Y, W, and H
2457 give the rectangle to draw. */
2459 static void
2460 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2461 struct glyph_string *s;
2462 int x, y, w, h;
2464 if (s->stippled_p)
2466 /* Fill background with a stipple pattern. */
2467 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2468 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2469 XSetFillStyle (s->display, s->gc, FillSolid);
2471 else
2472 x_clear_glyph_string_rect (s, x, y, w, h);
2476 /* Draw image glyph string S.
2478 s->y
2479 s->x +-------------------------
2480 | s->face->box
2482 | +-------------------------
2483 | | s->img->margin
2485 | | +-------------------
2486 | | | the image
2490 static void
2491 x_draw_image_glyph_string (s)
2492 struct glyph_string *s;
2494 int box_line_hwidth = eabs (s->face->box_line_width);
2495 int box_line_vwidth = max (s->face->box_line_width, 0);
2496 int height;
2497 Pixmap pixmap = None;
2499 height = s->height;
2500 if (s->slice.y == 0)
2501 height -= box_line_vwidth;
2502 if (s->slice.y + s->slice.height >= s->img->height)
2503 height -= box_line_vwidth;
2505 /* Fill background with face under the image. Do it only if row is
2506 taller than image or if image has a clip mask to reduce
2507 flickering. */
2508 s->stippled_p = s->face->stipple != 0;
2509 if (height > s->slice.height
2510 || s->img->hmargin
2511 || s->img->vmargin
2512 || s->img->mask
2513 || s->img->pixmap == 0
2514 || s->width != s->background_width)
2516 if (s->img->mask)
2518 /* Create a pixmap as large as the glyph string. Fill it
2519 with the background color. Copy the image to it, using
2520 its mask. Copy the temporary pixmap to the display. */
2521 Screen *screen = FRAME_X_SCREEN (s->f);
2522 int depth = DefaultDepthOfScreen (screen);
2524 /* Create a pixmap as large as the glyph string. */
2525 pixmap = XCreatePixmap (s->display, s->window,
2526 s->background_width,
2527 s->height, depth);
2529 /* Don't clip in the following because we're working on the
2530 pixmap. */
2531 XSetClipMask (s->display, s->gc, None);
2533 /* Fill the pixmap with the background color/stipple. */
2534 if (s->stippled_p)
2536 /* Fill background with a stipple pattern. */
2537 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2538 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2539 XFillRectangle (s->display, pixmap, s->gc,
2540 0, 0, s->background_width, s->height);
2541 XSetFillStyle (s->display, s->gc, FillSolid);
2542 XSetTSOrigin (s->display, s->gc, 0, 0);
2544 else
2546 XGCValues xgcv;
2547 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2548 &xgcv);
2549 XSetForeground (s->display, s->gc, xgcv.background);
2550 XFillRectangle (s->display, pixmap, s->gc,
2551 0, 0, s->background_width, s->height);
2552 XSetForeground (s->display, s->gc, xgcv.foreground);
2555 else
2557 int x = s->x;
2558 int y = s->y;
2560 if (s->first_glyph->left_box_line_p
2561 && s->slice.x == 0)
2562 x += box_line_hwidth;
2564 if (s->slice.y == 0)
2565 y += box_line_vwidth;
2567 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2570 s->background_filled_p = 1;
2573 /* Draw the foreground. */
2574 if (pixmap != None)
2576 x_draw_image_foreground_1 (s, pixmap);
2577 x_set_glyph_string_clipping (s);
2578 XCopyArea (s->display, pixmap, s->window, s->gc,
2579 0, 0, s->background_width, s->height, s->x, s->y);
2580 XFreePixmap (s->display, pixmap);
2582 else
2583 x_draw_image_foreground (s);
2585 /* If we must draw a relief around the image, do it. */
2586 if (s->img->relief
2587 || s->hl == DRAW_IMAGE_RAISED
2588 || s->hl == DRAW_IMAGE_SUNKEN)
2589 x_draw_image_relief (s);
2593 /* Draw stretch glyph string S. */
2595 static void
2596 x_draw_stretch_glyph_string (s)
2597 struct glyph_string *s;
2599 xassert (s->first_glyph->type == STRETCH_GLYPH);
2600 s->stippled_p = s->face->stipple != 0;
2602 if (s->hl == DRAW_CURSOR
2603 && !x_stretch_cursor_p)
2605 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2606 as wide as the stretch glyph. */
2607 int width, background_width = s->background_width;
2608 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2610 if (x < left_x)
2612 background_width -= left_x - x;
2613 x = left_x;
2615 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2617 /* Draw cursor. */
2618 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2620 /* Clear rest using the GC of the original non-cursor face. */
2621 if (width < background_width)
2623 int y = s->y;
2624 int w = background_width - width, h = s->height;
2625 XRectangle r;
2626 GC gc;
2628 x += width;
2629 if (s->row->mouse_face_p
2630 && cursor_in_mouse_face_p (s->w))
2632 x_set_mouse_face_gc (s);
2633 gc = s->gc;
2635 else
2636 gc = s->face->gc;
2638 get_glyph_string_clip_rect (s, &r);
2639 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2641 if (s->face->stipple)
2643 /* Fill background with a stipple pattern. */
2644 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2645 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2646 XSetFillStyle (s->display, gc, FillSolid);
2648 else
2650 XGCValues xgcv;
2651 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2652 XSetForeground (s->display, gc, xgcv.background);
2653 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2654 XSetForeground (s->display, gc, xgcv.foreground);
2658 else if (!s->background_filled_p)
2660 int background_width = s->background_width;
2661 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2663 /* Don't draw into left margin, fringe or scrollbar area
2664 except for header line and mode line. */
2665 if (x < left_x && !s->row->mode_line_p)
2667 background_width -= left_x - x;
2668 x = left_x;
2670 if (background_width > 0)
2671 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2674 s->background_filled_p = 1;
2678 /* Draw glyph string S. */
2680 static void
2681 x_draw_glyph_string (s)
2682 struct glyph_string *s;
2684 int relief_drawn_p = 0;
2686 /* If S draws into the background of its successor, draw the
2687 background of the successor first so that S can draw into it.
2688 This makes S->next use XDrawString instead of XDrawImageString. */
2689 if (s->next && s->right_overhang && !s->for_overlaps)
2691 xassert (s->next->img == NULL);
2692 x_set_glyph_string_gc (s->next);
2693 x_set_glyph_string_clipping (s->next);
2694 x_draw_glyph_string_background (s->next, 1);
2697 /* Set up S->gc, set clipping and draw S. */
2698 x_set_glyph_string_gc (s);
2700 /* Draw relief (if any) in advance for char/composition so that the
2701 glyph string can be drawn over it. */
2702 if (!s->for_overlaps
2703 && s->face->box != FACE_NO_BOX
2704 && (s->first_glyph->type == CHAR_GLYPH
2705 || s->first_glyph->type == COMPOSITE_GLYPH))
2708 x_set_glyph_string_clipping (s);
2709 x_draw_glyph_string_background (s, 1);
2710 x_draw_glyph_string_box (s);
2711 x_set_glyph_string_clipping (s);
2712 relief_drawn_p = 1;
2714 else
2715 x_set_glyph_string_clipping (s);
2717 switch (s->first_glyph->type)
2719 case IMAGE_GLYPH:
2720 x_draw_image_glyph_string (s);
2721 break;
2723 case STRETCH_GLYPH:
2724 x_draw_stretch_glyph_string (s);
2725 break;
2727 case CHAR_GLYPH:
2728 if (s->for_overlaps)
2729 s->background_filled_p = 1;
2730 else
2731 x_draw_glyph_string_background (s, 0);
2732 x_draw_glyph_string_foreground (s);
2733 break;
2735 case COMPOSITE_GLYPH:
2736 if (s->for_overlaps || s->gidx > 0)
2737 s->background_filled_p = 1;
2738 else
2739 x_draw_glyph_string_background (s, 1);
2740 x_draw_composite_glyph_string_foreground (s);
2741 break;
2743 default:
2744 abort ();
2747 if (!s->for_overlaps)
2749 /* Draw underline. */
2750 if (s->face->underline_p)
2752 unsigned long tem, h;
2753 int y;
2755 /* Get the underline thickness. Default is 1 pixel. */
2756 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
2757 h = 1;
2759 y = s->y + s->height - h;
2760 if (!x_underline_at_descent_line)
2762 /* Get the underline position. This is the recommended
2763 vertical offset in pixels from the baseline to the top of
2764 the underline. This is a signed value according to the
2765 specs, and its default is
2767 ROUND ((maximum descent) / 2), with
2768 ROUND(x) = floor (x + 0.5) */
2770 if (x_use_underline_position_properties
2771 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
2772 y = s->ybase + (long) tem;
2773 else if (s->face->font)
2774 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2777 if (s->face->underline_defaulted_p)
2778 XFillRectangle (s->display, s->window, s->gc,
2779 s->x, y, s->background_width, h);
2780 else
2782 XGCValues xgcv;
2783 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2784 XSetForeground (s->display, s->gc, s->face->underline_color);
2785 XFillRectangle (s->display, s->window, s->gc,
2786 s->x, y, s->background_width, h);
2787 XSetForeground (s->display, s->gc, xgcv.foreground);
2791 /* Draw overline. */
2792 if (s->face->overline_p)
2794 unsigned long dy = 0, h = 1;
2796 if (s->face->overline_color_defaulted_p)
2797 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2798 s->background_width, h);
2799 else
2801 XGCValues xgcv;
2802 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2803 XSetForeground (s->display, s->gc, s->face->overline_color);
2804 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2805 s->background_width, h);
2806 XSetForeground (s->display, s->gc, xgcv.foreground);
2810 /* Draw strike-through. */
2811 if (s->face->strike_through_p)
2813 unsigned long h = 1;
2814 unsigned long dy = (s->height - h) / 2;
2816 if (s->face->strike_through_color_defaulted_p)
2817 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2818 s->width, h);
2819 else
2821 XGCValues xgcv;
2822 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2823 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2824 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2825 s->width, h);
2826 XSetForeground (s->display, s->gc, xgcv.foreground);
2830 /* Draw relief if not yet drawn. */
2831 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2832 x_draw_glyph_string_box (s);
2835 /* Reset clipping. */
2836 XSetClipMask (s->display, s->gc, None);
2839 /* Shift display to make room for inserted glyphs. */
2841 void
2842 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2843 struct frame *f;
2844 int x, y, width, height, shift_by;
2846 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2847 f->output_data.x->normal_gc,
2848 x, y, width, height,
2849 x + shift_by, y);
2852 /* Delete N glyphs at the nominal cursor position. Not implemented
2853 for X frames. */
2855 static void
2856 x_delete_glyphs (f, n)
2857 struct frame *f;
2858 register int n;
2860 abort ();
2864 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2865 If they are <= 0, this is probably an error. */
2867 void
2868 x_clear_area (dpy, window, x, y, width, height, exposures)
2869 Display *dpy;
2870 Window window;
2871 int x, y;
2872 int width, height;
2873 int exposures;
2875 xassert (width > 0 && height > 0);
2876 XClearArea (dpy, window, x, y, width, height, exposures);
2880 /* Clear an entire frame. */
2882 static void
2883 x_clear_frame (struct frame *f)
2885 /* Clearing the frame will erase any cursor, so mark them all as no
2886 longer visible. */
2887 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2888 output_cursor.hpos = output_cursor.vpos = 0;
2889 output_cursor.x = -1;
2891 /* We don't set the output cursor here because there will always
2892 follow an explicit cursor_to. */
2893 BLOCK_INPUT;
2894 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2896 /* We have to clear the scroll bars, too. If we have changed
2897 colors or something like that, then they should be notified. */
2898 x_scroll_bar_clear (f);
2900 XFlush (FRAME_X_DISPLAY (f));
2902 UNBLOCK_INPUT;
2907 /* Invert the middle quarter of the frame for .15 sec. */
2909 /* We use the select system call to do the waiting, so we have to make
2910 sure it's available. If it isn't, we just won't do visual bells. */
2912 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2915 /* Subtract the `struct timeval' values X and Y, storing the result in
2916 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2918 static int
2919 timeval_subtract (result, x, y)
2920 struct timeval *result, x, y;
2922 /* Perform the carry for the later subtraction by updating y. This
2923 is safer because on some systems the tv_sec member is unsigned. */
2924 if (x.tv_usec < y.tv_usec)
2926 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2927 y.tv_usec -= 1000000 * nsec;
2928 y.tv_sec += nsec;
2931 if (x.tv_usec - y.tv_usec > 1000000)
2933 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2934 y.tv_usec += 1000000 * nsec;
2935 y.tv_sec -= nsec;
2938 /* Compute the time remaining to wait. tv_usec is certainly
2939 positive. */
2940 result->tv_sec = x.tv_sec - y.tv_sec;
2941 result->tv_usec = x.tv_usec - y.tv_usec;
2943 /* Return indication of whether the result should be considered
2944 negative. */
2945 return x.tv_sec < y.tv_sec;
2948 void
2949 XTflash (f)
2950 struct frame *f;
2952 BLOCK_INPUT;
2955 GC gc;
2957 /* Create a GC that will use the GXxor function to flip foreground
2958 pixels into background pixels. */
2960 XGCValues values;
2962 values.function = GXxor;
2963 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
2964 ^ FRAME_BACKGROUND_PIXEL (f));
2966 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2967 GCFunction | GCForeground, &values);
2971 /* Get the height not including a menu bar widget. */
2972 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
2973 /* Height of each line to flash. */
2974 int flash_height = FRAME_LINE_HEIGHT (f);
2975 /* These will be the left and right margins of the rectangles. */
2976 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
2977 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
2979 int width;
2981 /* Don't flash the area between a scroll bar and the frame
2982 edge it is next to. */
2983 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
2985 case vertical_scroll_bar_left:
2986 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2987 break;
2989 case vertical_scroll_bar_right:
2990 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2991 break;
2993 default:
2994 break;
2997 width = flash_right - flash_left;
2999 /* If window is tall, flash top and bottom line. */
3000 if (height > 3 * FRAME_LINE_HEIGHT (f))
3002 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3003 flash_left,
3004 (FRAME_INTERNAL_BORDER_WIDTH (f)
3005 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3006 width, flash_height);
3007 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3008 flash_left,
3009 (height - flash_height
3010 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3011 width, flash_height);
3013 else
3014 /* If it is short, flash it all. */
3015 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3016 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3017 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3019 x_flush (f);
3022 struct timeval wakeup;
3024 EMACS_GET_TIME (wakeup);
3026 /* Compute time to wait until, propagating carry from usecs. */
3027 wakeup.tv_usec += 150000;
3028 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3029 wakeup.tv_usec %= 1000000;
3031 /* Keep waiting until past the time wakeup or any input gets
3032 available. */
3033 while (! detect_input_pending ())
3035 struct timeval current;
3036 struct timeval timeout;
3038 EMACS_GET_TIME (current);
3040 /* Break if result would be negative. */
3041 if (timeval_subtract (&current, wakeup, current))
3042 break;
3044 /* How long `select' should wait. */
3045 timeout.tv_sec = 0;
3046 timeout.tv_usec = 10000;
3048 /* Try to wait that long--but we might wake up sooner. */
3049 select (0, NULL, NULL, NULL, &timeout);
3053 /* If window is tall, flash top and bottom line. */
3054 if (height > 3 * FRAME_LINE_HEIGHT (f))
3056 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3057 flash_left,
3058 (FRAME_INTERNAL_BORDER_WIDTH (f)
3059 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3060 width, flash_height);
3061 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3062 flash_left,
3063 (height - flash_height
3064 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3065 width, flash_height);
3067 else
3068 /* If it is short, flash it all. */
3069 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3070 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3071 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3073 XFreeGC (FRAME_X_DISPLAY (f), gc);
3074 x_flush (f);
3078 UNBLOCK_INPUT;
3081 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3084 /* Make audible bell. */
3086 void
3087 XTring_bell ()
3089 struct frame *f = SELECTED_FRAME ();
3091 if (FRAME_X_DISPLAY (f))
3093 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3094 if (visible_bell)
3095 XTflash (f);
3096 else
3097 #endif
3099 BLOCK_INPUT;
3100 XBell (FRAME_X_DISPLAY (f), 0);
3101 XFlush (FRAME_X_DISPLAY (f));
3102 UNBLOCK_INPUT;
3108 /* Specify how many text lines, from the top of the window,
3109 should be affected by insert-lines and delete-lines operations.
3110 This, and those operations, are used only within an update
3111 that is bounded by calls to x_update_begin and x_update_end. */
3113 static void
3114 XTset_terminal_window (n)
3115 register int n;
3117 /* This function intentionally left blank. */
3122 /***********************************************************************
3123 Line Dance
3124 ***********************************************************************/
3126 /* Perform an insert-lines or delete-lines operation, inserting N
3127 lines or deleting -N lines at vertical position VPOS. */
3129 static void
3130 x_ins_del_lines (f, vpos, n)
3131 struct frame *f;
3132 int vpos, n;
3134 abort ();
3138 /* Scroll part of the display as described by RUN. */
3140 static void
3141 x_scroll_run (w, run)
3142 struct window *w;
3143 struct run *run;
3145 struct frame *f = XFRAME (w->frame);
3146 int x, y, width, height, from_y, to_y, bottom_y;
3148 /* Get frame-relative bounding box of the text display area of W,
3149 without mode lines. Include in this box the left and right
3150 fringe of W. */
3151 window_box (w, -1, &x, &y, &width, &height);
3153 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3154 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3155 bottom_y = y + height;
3157 if (to_y < from_y)
3159 /* Scrolling up. Make sure we don't copy part of the mode
3160 line at the bottom. */
3161 if (from_y + run->height > bottom_y)
3162 height = bottom_y - from_y;
3163 else
3164 height = run->height;
3166 else
3168 /* Scolling down. Make sure we don't copy over the mode line.
3169 at the bottom. */
3170 if (to_y + run->height > bottom_y)
3171 height = bottom_y - to_y;
3172 else
3173 height = run->height;
3176 BLOCK_INPUT;
3178 /* Cursor off. Will be switched on again in x_update_window_end. */
3179 updated_window = w;
3180 x_clear_cursor (w);
3182 XCopyArea (FRAME_X_DISPLAY (f),
3183 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3184 f->output_data.x->normal_gc,
3185 x, from_y,
3186 width, height,
3187 x, to_y);
3189 UNBLOCK_INPUT;
3194 /***********************************************************************
3195 Exposure Events
3196 ***********************************************************************/
3199 static void
3200 frame_highlight (f)
3201 struct frame *f;
3203 /* We used to only do this if Vx_no_window_manager was non-nil, but
3204 the ICCCM (section 4.1.6) says that the window's border pixmap
3205 and border pixel are window attributes which are "private to the
3206 client", so we can always change it to whatever we want. */
3207 BLOCK_INPUT;
3208 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3209 f->output_data.x->border_pixel);
3210 UNBLOCK_INPUT;
3211 x_update_cursor (f, 1);
3214 static void
3215 frame_unhighlight (f)
3216 struct frame *f;
3218 /* We used to only do this if Vx_no_window_manager was non-nil, but
3219 the ICCCM (section 4.1.6) says that the window's border pixmap
3220 and border pixel are window attributes which are "private to the
3221 client", so we can always change it to whatever we want. */
3222 BLOCK_INPUT;
3223 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3224 f->output_data.x->border_tile);
3225 UNBLOCK_INPUT;
3226 x_update_cursor (f, 1);
3229 /* The focus has changed. Update the frames as necessary to reflect
3230 the new situation. Note that we can't change the selected frame
3231 here, because the Lisp code we are interrupting might become confused.
3232 Each event gets marked with the frame in which it occurred, so the
3233 Lisp code can tell when the switch took place by examining the events. */
3235 static void
3236 x_new_focus_frame (dpyinfo, frame)
3237 struct x_display_info *dpyinfo;
3238 struct frame *frame;
3240 struct frame *old_focus = dpyinfo->x_focus_frame;
3242 if (frame != dpyinfo->x_focus_frame)
3244 /* Set this before calling other routines, so that they see
3245 the correct value of x_focus_frame. */
3246 dpyinfo->x_focus_frame = frame;
3248 if (old_focus && old_focus->auto_lower)
3249 x_lower_frame (old_focus);
3251 #if 0
3252 selected_frame = frame;
3253 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3254 selected_frame);
3255 Fselect_window (selected_frame->selected_window, Qnil);
3256 choose_minibuf_frame ();
3257 #endif /* ! 0 */
3259 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3260 pending_autoraise_frame = dpyinfo->x_focus_frame;
3261 else
3262 pending_autoraise_frame = 0;
3265 x_frame_rehighlight (dpyinfo);
3268 /* Handle FocusIn and FocusOut state changes for FRAME.
3269 If FRAME has focus and there exists more than one frame, puts
3270 a FOCUS_IN_EVENT into *BUFP. */
3272 static void
3273 x_focus_changed (type, state, dpyinfo, frame, bufp)
3274 int type;
3275 int state;
3276 struct x_display_info *dpyinfo;
3277 struct frame *frame;
3278 struct input_event *bufp;
3280 if (type == FocusIn)
3282 if (dpyinfo->x_focus_event_frame != frame)
3284 x_new_focus_frame (dpyinfo, frame);
3285 dpyinfo->x_focus_event_frame = frame;
3287 /* Don't stop displaying the initial startup message
3288 for a switch-frame event we don't need. */
3289 if (GC_NILP (Vterminal_frame)
3290 && GC_CONSP (Vframe_list)
3291 && !GC_NILP (XCDR (Vframe_list)))
3293 bufp->kind = FOCUS_IN_EVENT;
3294 XSETFRAME (bufp->frame_or_window, frame);
3298 frame->output_data.x->focus_state |= state;
3300 #ifdef HAVE_X_I18N
3301 if (FRAME_XIC (frame))
3302 XSetICFocus (FRAME_XIC (frame));
3303 #endif
3305 else if (type == FocusOut)
3307 frame->output_data.x->focus_state &= ~state;
3309 if (dpyinfo->x_focus_event_frame == frame)
3311 dpyinfo->x_focus_event_frame = 0;
3312 x_new_focus_frame (dpyinfo, 0);
3315 #ifdef HAVE_X_I18N
3316 if (FRAME_XIC (frame))
3317 XUnsetICFocus (FRAME_XIC (frame));
3318 #endif
3322 /* The focus may have changed. Figure out if it is a real focus change,
3323 by checking both FocusIn/Out and Enter/LeaveNotify events.
3325 Returns FOCUS_IN_EVENT event in *BUFP. */
3327 static void
3328 x_detect_focus_change (dpyinfo, event, bufp)
3329 struct x_display_info *dpyinfo;
3330 XEvent *event;
3331 struct input_event *bufp;
3333 struct frame *frame;
3335 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3336 if (! frame)
3337 return;
3339 switch (event->type)
3341 case EnterNotify:
3342 case LeaveNotify:
3344 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3345 int focus_state
3346 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3348 if (event->xcrossing.detail != NotifyInferior
3349 && event->xcrossing.focus
3350 && ! (focus_state & FOCUS_EXPLICIT))
3351 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3352 FOCUS_IMPLICIT,
3353 dpyinfo, frame, bufp);
3355 break;
3357 case FocusIn:
3358 case FocusOut:
3359 x_focus_changed (event->type,
3360 (event->xfocus.detail == NotifyPointer ?
3361 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3362 dpyinfo, frame, bufp);
3363 break;
3368 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3370 void
3371 x_mouse_leave (dpyinfo)
3372 struct x_display_info *dpyinfo;
3374 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3377 /* The focus has changed, or we have redirected a frame's focus to
3378 another frame (this happens when a frame uses a surrogate
3379 mini-buffer frame). Shift the highlight as appropriate.
3381 The FRAME argument doesn't necessarily have anything to do with which
3382 frame is being highlighted or un-highlighted; we only use it to find
3383 the appropriate X display info. */
3385 static void
3386 XTframe_rehighlight (frame)
3387 struct frame *frame;
3389 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3392 static void
3393 x_frame_rehighlight (dpyinfo)
3394 struct x_display_info *dpyinfo;
3396 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3398 if (dpyinfo->x_focus_frame)
3400 dpyinfo->x_highlight_frame
3401 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3402 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3403 : dpyinfo->x_focus_frame);
3404 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3406 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3407 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3410 else
3411 dpyinfo->x_highlight_frame = 0;
3413 if (dpyinfo->x_highlight_frame != old_highlight)
3415 if (old_highlight)
3416 frame_unhighlight (old_highlight);
3417 if (dpyinfo->x_highlight_frame)
3418 frame_highlight (dpyinfo->x_highlight_frame);
3424 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3426 /* Initialize mode_switch_bit and modifier_meaning. */
3427 static void
3428 x_find_modifier_meanings (dpyinfo)
3429 struct x_display_info *dpyinfo;
3431 int min_code, max_code;
3432 KeySym *syms;
3433 int syms_per_code;
3434 XModifierKeymap *mods;
3436 dpyinfo->meta_mod_mask = 0;
3437 dpyinfo->shift_lock_mask = 0;
3438 dpyinfo->alt_mod_mask = 0;
3439 dpyinfo->super_mod_mask = 0;
3440 dpyinfo->hyper_mod_mask = 0;
3442 #ifdef HAVE_X11R4
3443 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3444 #else
3445 min_code = dpyinfo->display->min_keycode;
3446 max_code = dpyinfo->display->max_keycode;
3447 #endif
3449 syms = XGetKeyboardMapping (dpyinfo->display,
3450 min_code, max_code - min_code + 1,
3451 &syms_per_code);
3452 mods = XGetModifierMapping (dpyinfo->display);
3454 /* Scan the modifier table to see which modifier bits the Meta and
3455 Alt keysyms are on. */
3457 int row, col; /* The row and column in the modifier table. */
3458 int found_alt_or_meta;
3460 for (row = 3; row < 8; row++)
3462 found_alt_or_meta = 0;
3463 for (col = 0; col < mods->max_keypermod; col++)
3465 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3467 /* Zeroes are used for filler. Skip them. */
3468 if (code == 0)
3469 continue;
3471 /* Are any of this keycode's keysyms a meta key? */
3473 int code_col;
3475 for (code_col = 0; code_col < syms_per_code; code_col++)
3477 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3479 switch (sym)
3481 case XK_Meta_L:
3482 case XK_Meta_R:
3483 found_alt_or_meta = 1;
3484 dpyinfo->meta_mod_mask |= (1 << row);
3485 break;
3487 case XK_Alt_L:
3488 case XK_Alt_R:
3489 found_alt_or_meta = 1;
3490 dpyinfo->alt_mod_mask |= (1 << row);
3491 break;
3493 case XK_Hyper_L:
3494 case XK_Hyper_R:
3495 if (!found_alt_or_meta)
3496 dpyinfo->hyper_mod_mask |= (1 << row);
3497 code_col = syms_per_code;
3498 col = mods->max_keypermod;
3499 break;
3501 case XK_Super_L:
3502 case XK_Super_R:
3503 if (!found_alt_or_meta)
3504 dpyinfo->super_mod_mask |= (1 << row);
3505 code_col = syms_per_code;
3506 col = mods->max_keypermod;
3507 break;
3509 case XK_Shift_Lock:
3510 /* Ignore this if it's not on the lock modifier. */
3511 if (!found_alt_or_meta && ((1 << row) == LockMask))
3512 dpyinfo->shift_lock_mask = LockMask;
3513 code_col = syms_per_code;
3514 col = mods->max_keypermod;
3515 break;
3523 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3524 if (! dpyinfo->meta_mod_mask)
3526 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3527 dpyinfo->alt_mod_mask = 0;
3530 /* If some keys are both alt and meta,
3531 make them just meta, not alt. */
3532 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3534 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3537 XFree ((char *) syms);
3538 XFreeModifiermap (mods);
3541 /* Convert between the modifier bits X uses and the modifier bits
3542 Emacs uses. */
3544 unsigned int
3545 x_x_to_emacs_modifiers (dpyinfo, state)
3546 struct x_display_info *dpyinfo;
3547 unsigned int state;
3549 EMACS_UINT mod_meta = meta_modifier;
3550 EMACS_UINT mod_alt = alt_modifier;
3551 EMACS_UINT mod_hyper = hyper_modifier;
3552 EMACS_UINT mod_super = super_modifier;
3553 Lisp_Object tem;
3555 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3556 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3557 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3558 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3559 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3560 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3561 tem = Fget (Vx_super_keysym, Qmodifier_value);
3562 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3565 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3566 | ((state & ControlMask) ? ctrl_modifier : 0)
3567 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3568 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3569 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3570 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3573 static unsigned int
3574 x_emacs_to_x_modifiers (dpyinfo, state)
3575 struct x_display_info *dpyinfo;
3576 unsigned int state;
3578 EMACS_UINT mod_meta = meta_modifier;
3579 EMACS_UINT mod_alt = alt_modifier;
3580 EMACS_UINT mod_hyper = hyper_modifier;
3581 EMACS_UINT mod_super = super_modifier;
3583 Lisp_Object tem;
3585 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3586 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3587 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3588 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3589 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3590 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3591 tem = Fget (Vx_super_keysym, Qmodifier_value);
3592 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3595 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3596 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3597 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3598 | ((state & shift_modifier) ? ShiftMask : 0)
3599 | ((state & ctrl_modifier) ? ControlMask : 0)
3600 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3603 /* Convert a keysym to its name. */
3605 char *
3606 x_get_keysym_name (keysym)
3607 KeySym keysym;
3609 char *value;
3611 BLOCK_INPUT;
3612 value = XKeysymToString (keysym);
3613 UNBLOCK_INPUT;
3615 return value;
3620 /* Mouse clicks and mouse movement. Rah. */
3622 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3624 If the event is a button press, then note that we have grabbed
3625 the mouse. */
3627 static Lisp_Object
3628 construct_mouse_click (result, event, f)
3629 struct input_event *result;
3630 XButtonEvent *event;
3631 struct frame *f;
3633 /* Make the event type NO_EVENT; we'll change that when we decide
3634 otherwise. */
3635 result->kind = MOUSE_CLICK_EVENT;
3636 result->code = event->button - Button1;
3637 result->timestamp = event->time;
3638 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3639 event->state)
3640 | (event->type == ButtonRelease
3641 ? up_modifier
3642 : down_modifier));
3644 XSETINT (result->x, event->x);
3645 XSETINT (result->y, event->y);
3646 XSETFRAME (result->frame_or_window, f);
3647 result->arg = Qnil;
3648 return Qnil;
3652 /* Function to report a mouse movement to the mainstream Emacs code.
3653 The input handler calls this.
3655 We have received a mouse movement event, which is given in *event.
3656 If the mouse is over a different glyph than it was last time, tell
3657 the mainstream emacs code by setting mouse_moved. If not, ask for
3658 another motion event, so we can check again the next time it moves. */
3660 static XMotionEvent last_mouse_motion_event;
3661 static Lisp_Object last_mouse_motion_frame;
3663 static int
3664 note_mouse_movement (frame, event)
3665 FRAME_PTR frame;
3666 XMotionEvent *event;
3668 last_mouse_movement_time = event->time;
3669 last_mouse_motion_event = *event;
3670 XSETFRAME (last_mouse_motion_frame, frame);
3672 if (!FRAME_X_OUTPUT (frame))
3673 return 0;
3675 if (event->window != FRAME_X_WINDOW (frame))
3677 frame->mouse_moved = 1;
3678 last_mouse_scroll_bar = Qnil;
3679 note_mouse_highlight (frame, -1, -1);
3680 last_mouse_glyph_frame = 0;
3681 return 1;
3685 /* Has the mouse moved off the glyph it was on at the last sighting? */
3686 if (frame != last_mouse_glyph_frame
3687 || event->x < last_mouse_glyph.x
3688 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3689 || event->y < last_mouse_glyph.y
3690 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3692 frame->mouse_moved = 1;
3693 last_mouse_scroll_bar = Qnil;
3694 note_mouse_highlight (frame, event->x, event->y);
3695 /* Remember which glyph we're now on. */
3696 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3697 last_mouse_glyph_frame = frame;
3698 return 1;
3701 return 0;
3705 /************************************************************************
3706 Mouse Face
3707 ************************************************************************/
3709 static void
3710 redo_mouse_highlight ()
3712 if (!NILP (last_mouse_motion_frame)
3713 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3714 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3715 last_mouse_motion_event.x,
3716 last_mouse_motion_event.y);
3721 /* Return the current position of the mouse.
3722 *FP should be a frame which indicates which display to ask about.
3724 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3725 and *PART to the frame, window, and scroll bar part that the mouse
3726 is over. Set *X and *Y to the portion and whole of the mouse's
3727 position on the scroll bar.
3729 If the mouse movement started elsewhere, set *FP to the frame the
3730 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3731 the mouse is over.
3733 Set *TIME to the server time-stamp for the time at which the mouse
3734 was at this position.
3736 Don't store anything if we don't have a valid set of values to report.
3738 This clears the mouse_moved flag, so we can wait for the next mouse
3739 movement. */
3741 static void
3742 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3743 FRAME_PTR *fp;
3744 int insist;
3745 Lisp_Object *bar_window;
3746 enum scroll_bar_part *part;
3747 Lisp_Object *x, *y;
3748 unsigned long *time;
3750 FRAME_PTR f1;
3752 BLOCK_INPUT;
3754 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3755 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3756 else
3758 Window root;
3759 int root_x, root_y;
3761 Window dummy_window;
3762 int dummy;
3764 Lisp_Object frame, tail;
3766 /* Clear the mouse-moved flag for every frame on this display. */
3767 FOR_EACH_FRAME (tail, frame)
3768 if (FRAME_X_P (XFRAME (frame))
3769 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3770 XFRAME (frame)->mouse_moved = 0;
3772 last_mouse_scroll_bar = Qnil;
3774 /* Figure out which root window we're on. */
3775 XQueryPointer (FRAME_X_DISPLAY (*fp),
3776 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3778 /* The root window which contains the pointer. */
3779 &root,
3781 /* Trash which we can't trust if the pointer is on
3782 a different screen. */
3783 &dummy_window,
3785 /* The position on that root window. */
3786 &root_x, &root_y,
3788 /* More trash we can't trust. */
3789 &dummy, &dummy,
3791 /* Modifier keys and pointer buttons, about which
3792 we don't care. */
3793 (unsigned int *) &dummy);
3795 /* Now we have a position on the root; find the innermost window
3796 containing the pointer. */
3798 Window win, child;
3799 int win_x, win_y;
3800 int parent_x = 0, parent_y = 0;
3802 win = root;
3804 /* XTranslateCoordinates can get errors if the window
3805 structure is changing at the same time this function
3806 is running. So at least we must not crash from them. */
3808 x_catch_errors (FRAME_X_DISPLAY (*fp));
3810 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3811 && FRAME_LIVE_P (last_mouse_frame))
3813 /* If mouse was grabbed on a frame, give coords for that frame
3814 even if the mouse is now outside it. */
3815 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3817 /* From-window, to-window. */
3818 root, FRAME_X_WINDOW (last_mouse_frame),
3820 /* From-position, to-position. */
3821 root_x, root_y, &win_x, &win_y,
3823 /* Child of win. */
3824 &child);
3825 f1 = last_mouse_frame;
3827 else
3829 while (1)
3831 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3833 /* From-window, to-window. */
3834 root, win,
3836 /* From-position, to-position. */
3837 root_x, root_y, &win_x, &win_y,
3839 /* Child of win. */
3840 &child);
3842 if (child == None || child == win)
3843 break;
3845 win = child;
3846 parent_x = win_x;
3847 parent_y = win_y;
3850 /* Now we know that:
3851 win is the innermost window containing the pointer
3852 (XTC says it has no child containing the pointer),
3853 win_x and win_y are the pointer's position in it
3854 (XTC did this the last time through), and
3855 parent_x and parent_y are the pointer's position in win's parent.
3856 (They are what win_x and win_y were when win was child.
3857 If win is the root window, it has no parent, and
3858 parent_{x,y} are invalid, but that's okay, because we'll
3859 never use them in that case.) */
3861 /* Is win one of our frames? */
3862 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3864 #ifdef USE_X_TOOLKIT
3865 /* If we end up with the menu bar window, say it's not
3866 on the frame. */
3867 if (f1 != NULL
3868 && f1->output_data.x->menubar_widget
3869 && win == XtWindow (f1->output_data.x->menubar_widget))
3870 f1 = NULL;
3871 #endif /* USE_X_TOOLKIT */
3874 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3875 f1 = 0;
3877 x_uncatch_errors ();
3879 /* If not, is it one of our scroll bars? */
3880 if (! f1)
3882 struct scroll_bar *bar;
3884 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3886 if (bar)
3888 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3889 win_x = parent_x;
3890 win_y = parent_y;
3894 if (f1 == 0 && insist > 0)
3895 f1 = SELECTED_FRAME ();
3897 if (f1)
3899 /* Ok, we found a frame. Store all the values.
3900 last_mouse_glyph is a rectangle used to reduce the
3901 generation of mouse events. To not miss any motion
3902 events, we must divide the frame into rectangles of the
3903 size of the smallest character that could be displayed
3904 on it, i.e. into the same rectangles that matrices on
3905 the frame are divided into. */
3907 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
3908 last_mouse_glyph_frame = f1;
3910 *bar_window = Qnil;
3911 *part = 0;
3912 *fp = f1;
3913 XSETINT (*x, win_x);
3914 XSETINT (*y, win_y);
3915 *time = last_mouse_movement_time;
3920 UNBLOCK_INPUT;
3925 /***********************************************************************
3926 Scroll bars
3927 ***********************************************************************/
3929 /* Scroll bar support. */
3931 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
3932 manages it.
3933 This can be called in GC, so we have to make sure to strip off mark
3934 bits. */
3936 static struct scroll_bar *
3937 x_window_to_scroll_bar (display, window_id)
3938 Display *display;
3939 Window window_id;
3941 Lisp_Object tail;
3943 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3944 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
3945 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
3947 for (tail = Vframe_list;
3948 XGCTYPE (tail) == Lisp_Cons;
3949 tail = XCDR (tail))
3951 Lisp_Object frame, bar, condemned;
3953 frame = XCAR (tail);
3954 /* All elements of Vframe_list should be frames. */
3955 if (! GC_FRAMEP (frame))
3956 abort ();
3958 if (! FRAME_X_P (XFRAME (frame)))
3959 continue;
3961 /* Scan this frame's scroll bar list for a scroll bar with the
3962 right window ID. */
3963 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3964 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3965 /* This trick allows us to search both the ordinary and
3966 condemned scroll bar lists with one loop. */
3967 ! GC_NILP (bar) || (bar = condemned,
3968 condemned = Qnil,
3969 ! GC_NILP (bar));
3970 bar = XSCROLL_BAR (bar)->next)
3971 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id &&
3972 FRAME_X_DISPLAY (XFRAME (frame)) == display)
3973 return XSCROLL_BAR (bar);
3976 return 0;
3980 #if defined USE_LUCID
3982 /* Return the Lucid menu bar WINDOW is part of. Return null
3983 if WINDOW is not part of a menu bar. */
3985 static Widget
3986 x_window_to_menu_bar (window)
3987 Window window;
3989 Lisp_Object tail;
3991 for (tail = Vframe_list;
3992 XGCTYPE (tail) == Lisp_Cons;
3993 tail = XCDR (tail))
3995 if (FRAME_X_P (XFRAME (XCAR (tail))))
3997 Lisp_Object frame = XCAR (tail);
3998 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4000 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4001 return menu_bar;
4005 return NULL;
4008 #endif /* USE_LUCID */
4011 /************************************************************************
4012 Toolkit scroll bars
4013 ************************************************************************/
4015 #ifdef USE_TOOLKIT_SCROLL_BARS
4017 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4018 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4019 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4020 struct scroll_bar *));
4021 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4022 int, int, int));
4025 /* Lisp window being scrolled. Set when starting to interact with
4026 a toolkit scroll bar, reset to nil when ending the interaction. */
4028 static Lisp_Object window_being_scrolled;
4030 /* Last scroll bar part sent in xm_scroll_callback. */
4032 static int last_scroll_bar_part;
4034 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4035 that movements of 1/20 of the screen size are mapped to up/down. */
4037 #ifndef USE_GTK
4038 /* Id of action hook installed for scroll bars. */
4040 static XtActionHookId action_hook_id;
4042 static Boolean xaw3d_arrow_scroll;
4044 /* Whether the drag scrolling maintains the mouse at the top of the
4045 thumb. If not, resizing the thumb needs to be done more carefully
4046 to avoid jerkyness. */
4048 static Boolean xaw3d_pick_top;
4050 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4051 bars are used.. The hook is responsible for detecting when
4052 the user ends an interaction with the scroll bar, and generates
4053 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4055 static void
4056 xt_action_hook (widget, client_data, action_name, event, params,
4057 num_params)
4058 Widget widget;
4059 XtPointer client_data;
4060 String action_name;
4061 XEvent *event;
4062 String *params;
4063 Cardinal *num_params;
4065 int scroll_bar_p;
4066 char *end_action;
4068 #ifdef USE_MOTIF
4069 scroll_bar_p = XmIsScrollBar (widget);
4070 end_action = "Release";
4071 #else /* !USE_MOTIF i.e. use Xaw */
4072 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4073 end_action = "EndScroll";
4074 #endif /* USE_MOTIF */
4076 if (scroll_bar_p
4077 && strcmp (action_name, end_action) == 0
4078 && WINDOWP (window_being_scrolled))
4080 struct window *w;
4082 x_send_scroll_bar_event (window_being_scrolled,
4083 scroll_bar_end_scroll, 0, 0);
4084 w = XWINDOW (window_being_scrolled);
4086 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4088 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4089 /* The thumb size is incorrect while dragging: fix it. */
4090 set_vertical_scroll_bar (w);
4092 window_being_scrolled = Qnil;
4093 last_scroll_bar_part = -1;
4095 /* Xt timeouts no longer needed. */
4096 toolkit_scroll_bar_interaction = 0;
4099 #endif /* not USE_GTK */
4101 /* A vector of windows used for communication between
4102 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4104 static struct window **scroll_bar_windows;
4105 static int scroll_bar_windows_size;
4108 /* Send a client message with message type Xatom_Scrollbar for a
4109 scroll action to the frame of WINDOW. PART is a value identifying
4110 the part of the scroll bar that was clicked on. PORTION is the
4111 amount to scroll of a whole of WHOLE. */
4113 static void
4114 x_send_scroll_bar_event (window, part, portion, whole)
4115 Lisp_Object window;
4116 int part, portion, whole;
4118 XEvent event;
4119 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4120 struct window *w = XWINDOW (window);
4121 struct frame *f = XFRAME (w->frame);
4122 int i;
4124 BLOCK_INPUT;
4126 /* Construct a ClientMessage event to send to the frame. */
4127 ev->type = ClientMessage;
4128 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4129 ev->display = FRAME_X_DISPLAY (f);
4130 ev->window = FRAME_X_WINDOW (f);
4131 ev->format = 32;
4133 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4134 not enough to store a pointer or Lisp_Object on a 64 bit system.
4135 So, store the window in scroll_bar_windows and pass the index
4136 into that array in the event. */
4137 for (i = 0; i < scroll_bar_windows_size; ++i)
4138 if (scroll_bar_windows[i] == NULL)
4139 break;
4141 if (i == scroll_bar_windows_size)
4143 int new_size = max (10, 2 * scroll_bar_windows_size);
4144 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4145 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4147 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4148 nbytes);
4149 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4150 scroll_bar_windows_size = new_size;
4153 scroll_bar_windows[i] = w;
4154 ev->data.l[0] = (long) i;
4155 ev->data.l[1] = (long) part;
4156 ev->data.l[2] = (long) 0;
4157 ev->data.l[3] = (long) portion;
4158 ev->data.l[4] = (long) whole;
4160 /* Make Xt timeouts work while the scroll bar is active. */
4161 toolkit_scroll_bar_interaction = 1;
4162 #ifdef USE_X_TOOLKIT
4163 x_activate_timeout_atimer ();
4164 #endif
4166 /* Setting the event mask to zero means that the message will
4167 be sent to the client that created the window, and if that
4168 window no longer exists, no event will be sent. */
4169 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4170 UNBLOCK_INPUT;
4174 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4175 in *IEVENT. */
4177 static void
4178 x_scroll_bar_to_input_event (event, ievent)
4179 XEvent *event;
4180 struct input_event *ievent;
4182 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4183 Lisp_Object window;
4184 struct frame *f;
4185 struct window *w;
4187 w = scroll_bar_windows[ev->data.l[0]];
4188 scroll_bar_windows[ev->data.l[0]] = NULL;
4190 XSETWINDOW (window, w);
4191 f = XFRAME (w->frame);
4193 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4194 ievent->frame_or_window = window;
4195 ievent->arg = Qnil;
4196 #ifdef USE_GTK
4197 ievent->timestamp = CurrentTime;
4198 #else
4199 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4200 #endif
4201 ievent->part = ev->data.l[1];
4202 ievent->code = ev->data.l[2];
4203 ievent->x = make_number ((int) ev->data.l[3]);
4204 ievent->y = make_number ((int) ev->data.l[4]);
4205 ievent->modifiers = 0;
4209 #ifdef USE_MOTIF
4211 /* Minimum and maximum values used for Motif scroll bars. */
4213 #define XM_SB_MAX 10000000
4216 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4217 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4218 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4220 static void
4221 xm_scroll_callback (widget, client_data, call_data)
4222 Widget widget;
4223 XtPointer client_data, call_data;
4225 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4226 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4227 int part = -1, whole = 0, portion = 0;
4229 switch (cs->reason)
4231 case XmCR_DECREMENT:
4232 bar->dragging = Qnil;
4233 part = scroll_bar_up_arrow;
4234 break;
4236 case XmCR_INCREMENT:
4237 bar->dragging = Qnil;
4238 part = scroll_bar_down_arrow;
4239 break;
4241 case XmCR_PAGE_DECREMENT:
4242 bar->dragging = Qnil;
4243 part = scroll_bar_above_handle;
4244 break;
4246 case XmCR_PAGE_INCREMENT:
4247 bar->dragging = Qnil;
4248 part = scroll_bar_below_handle;
4249 break;
4251 case XmCR_TO_TOP:
4252 bar->dragging = Qnil;
4253 part = scroll_bar_to_top;
4254 break;
4256 case XmCR_TO_BOTTOM:
4257 bar->dragging = Qnil;
4258 part = scroll_bar_to_bottom;
4259 break;
4261 case XmCR_DRAG:
4263 int slider_size;
4265 /* Get the slider size. */
4266 BLOCK_INPUT;
4267 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4268 UNBLOCK_INPUT;
4270 whole = XM_SB_MAX - slider_size;
4271 portion = min (cs->value, whole);
4272 part = scroll_bar_handle;
4273 bar->dragging = make_number (cs->value);
4275 break;
4277 case XmCR_VALUE_CHANGED:
4278 break;
4281 if (part >= 0)
4283 window_being_scrolled = bar->window;
4284 last_scroll_bar_part = part;
4285 x_send_scroll_bar_event (bar->window, part, portion, whole);
4290 #else /* !USE_MOTIF, i.e. Xaw or GTK */
4291 #ifdef USE_GTK
4292 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4293 bar widget. DATA is a pointer to the scroll_bar structure. */
4295 static void
4296 xg_scroll_callback (widget, data)
4297 GtkRange *widget;
4298 gpointer data;
4300 struct scroll_bar *bar = (struct scroll_bar *) data;
4301 gdouble previous;
4302 gdouble position;
4303 gdouble *p;
4304 int diff;
4306 int part = -1, whole = 0, portion = 0;
4307 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4309 position = gtk_adjustment_get_value (adj);
4311 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4312 if (! p)
4314 p = (gdouble*) xmalloc (sizeof (gdouble));
4315 *p = XG_SB_MIN;
4316 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4319 previous = *p;
4320 *p = position;
4322 if (xg_ignore_gtk_scrollbar) return;
4324 diff = (int) (position - previous);
4326 if (diff == (int) adj->step_increment)
4328 part = scroll_bar_down_arrow;
4329 bar->dragging = Qnil;
4331 else if (-diff == (int) adj->step_increment)
4333 part = scroll_bar_up_arrow;
4334 bar->dragging = Qnil;
4336 else if (diff == (int) adj->page_increment)
4338 part = scroll_bar_below_handle;
4339 bar->dragging = Qnil;
4341 else if (-diff == (int) adj->page_increment)
4343 part = scroll_bar_above_handle;
4344 bar->dragging = Qnil;
4346 else
4348 part = scroll_bar_handle;
4349 whole = adj->upper - adj->page_size;
4350 portion = min ((int)position, whole);
4351 bar->dragging = make_number ((int)portion);
4354 if (part >= 0)
4356 window_being_scrolled = bar->window;
4357 last_scroll_bar_part = part;
4358 x_send_scroll_bar_event (bar->window, part, portion, whole);
4362 #else /* not USE_GTK */
4364 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4365 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4366 scroll bar struct. CALL_DATA is a pointer to a float saying where
4367 the thumb is. */
4369 static void
4370 xaw_jump_callback (widget, client_data, call_data)
4371 Widget widget;
4372 XtPointer client_data, call_data;
4374 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4375 float top = *(float *) call_data;
4376 float shown;
4377 int whole, portion, height;
4378 int part;
4380 /* Get the size of the thumb, a value between 0 and 1. */
4381 BLOCK_INPUT;
4382 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4383 UNBLOCK_INPUT;
4385 whole = 10000000;
4386 portion = shown < 1 ? top * whole : 0;
4388 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4389 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4390 the bottom, so we force the scrolling whenever we see that we're
4391 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4392 we try to ensure that we always stay two pixels away from the
4393 bottom). */
4394 part = scroll_bar_down_arrow;
4395 else
4396 part = scroll_bar_handle;
4398 window_being_scrolled = bar->window;
4399 bar->dragging = make_number (portion);
4400 last_scroll_bar_part = part;
4401 x_send_scroll_bar_event (bar->window, part, portion, whole);
4405 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4406 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4407 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4408 the scroll bar. CALL_DATA is an integer specifying the action that
4409 has taken place. Its magnitude is in the range 0..height of the
4410 scroll bar. Negative values mean scroll towards buffer start.
4411 Values < height of scroll bar mean line-wise movement. */
4413 static void
4414 xaw_scroll_callback (widget, client_data, call_data)
4415 Widget widget;
4416 XtPointer client_data, call_data;
4418 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4419 /* The position really is stored cast to a pointer. */
4420 int position = (long) call_data;
4421 Dimension height;
4422 int part;
4424 /* Get the height of the scroll bar. */
4425 BLOCK_INPUT;
4426 XtVaGetValues (widget, XtNheight, &height, NULL);
4427 UNBLOCK_INPUT;
4429 if (eabs (position) >= height)
4430 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4432 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4433 it maps line-movement to call_data = max(5, height/20). */
4434 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4435 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4436 else
4437 part = scroll_bar_move_ratio;
4439 window_being_scrolled = bar->window;
4440 bar->dragging = Qnil;
4441 last_scroll_bar_part = part;
4442 x_send_scroll_bar_event (bar->window, part, position, height);
4445 #endif /* not USE_GTK */
4446 #endif /* not USE_MOTIF */
4448 #define SCROLL_BAR_NAME "verticalScrollBar"
4450 /* Create the widget for scroll bar BAR on frame F. Record the widget
4451 and X window of the scroll bar in BAR. */
4453 #ifdef USE_GTK
4454 static void
4455 x_create_toolkit_scroll_bar (f, bar)
4456 struct frame *f;
4457 struct scroll_bar *bar;
4459 char *scroll_bar_name = SCROLL_BAR_NAME;
4461 BLOCK_INPUT;
4462 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4463 scroll_bar_name);
4464 UNBLOCK_INPUT;
4467 #else /* not USE_GTK */
4469 static void
4470 x_create_toolkit_scroll_bar (f, bar)
4471 struct frame *f;
4472 struct scroll_bar *bar;
4474 Window xwindow;
4475 Widget widget;
4476 Arg av[20];
4477 int ac = 0;
4478 char *scroll_bar_name = SCROLL_BAR_NAME;
4479 unsigned long pixel;
4481 BLOCK_INPUT;
4483 #ifdef USE_MOTIF
4484 /* Set resources. Create the widget. */
4485 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4486 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4487 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4488 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4489 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4490 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4491 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4493 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4494 if (pixel != -1)
4496 XtSetArg (av[ac], XmNforeground, pixel);
4497 ++ac;
4500 pixel = f->output_data.x->scroll_bar_background_pixel;
4501 if (pixel != -1)
4503 XtSetArg (av[ac], XmNbackground, pixel);
4504 ++ac;
4507 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4508 scroll_bar_name, av, ac);
4510 /* Add one callback for everything that can happen. */
4511 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4512 (XtPointer) bar);
4513 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4514 (XtPointer) bar);
4515 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4516 (XtPointer) bar);
4517 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4518 (XtPointer) bar);
4519 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4520 (XtPointer) bar);
4521 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4522 (XtPointer) bar);
4523 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4524 (XtPointer) bar);
4526 /* Realize the widget. Only after that is the X window created. */
4527 XtRealizeWidget (widget);
4529 /* Set the cursor to an arrow. I didn't find a resource to do that.
4530 And I'm wondering why it hasn't an arrow cursor by default. */
4531 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4532 f->output_data.x->nontext_cursor);
4534 #else /* !USE_MOTIF i.e. use Xaw */
4536 /* Set resources. Create the widget. The background of the
4537 Xaw3d scroll bar widget is a little bit light for my taste.
4538 We don't alter it here to let users change it according
4539 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4540 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4541 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4542 /* For smoother scrolling with Xaw3d -sm */
4543 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4545 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4546 if (pixel != -1)
4548 XtSetArg (av[ac], XtNforeground, pixel);
4549 ++ac;
4552 pixel = f->output_data.x->scroll_bar_background_pixel;
4553 if (pixel != -1)
4555 XtSetArg (av[ac], XtNbackground, pixel);
4556 ++ac;
4559 /* Top/bottom shadow colors. */
4561 /* Allocate them, if necessary. */
4562 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4564 pixel = f->output_data.x->scroll_bar_background_pixel;
4565 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4566 &pixel, 1.2, 0x8000))
4567 pixel = -1;
4568 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4570 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4572 pixel = f->output_data.x->scroll_bar_background_pixel;
4573 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4574 &pixel, 0.6, 0x4000))
4575 pixel = -1;
4576 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4579 #ifdef XtNbeNiceToColormap
4580 /* Tell the toolkit about them. */
4581 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4582 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4583 /* We tried to allocate a color for the top/bottom shadow, and
4584 failed, so tell Xaw3d to use dithering instead. */
4586 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4587 ++ac;
4589 else
4590 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4591 be more consistent with other emacs 3d colors, and since Xaw3d is
4592 not good at dealing with allocation failure. */
4594 /* This tells Xaw3d to use real colors instead of dithering for
4595 the shadows. */
4596 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4597 ++ac;
4599 /* Specify the colors. */
4600 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4601 if (pixel != -1)
4603 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4604 ++ac;
4606 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4607 if (pixel != -1)
4609 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4610 ++ac;
4613 #endif
4615 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4616 f->output_data.x->edit_widget, av, ac);
4619 char *initial = "";
4620 char *val = initial;
4621 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4622 #ifdef XtNarrowScrollbars
4623 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4624 #endif
4625 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4626 if (xaw3d_arrow_scroll || val == initial)
4627 { /* ARROW_SCROLL */
4628 xaw3d_arrow_scroll = True;
4629 /* Isn't that just a personal preference ? --Stef */
4630 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4634 /* Define callbacks. */
4635 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4636 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4637 (XtPointer) bar);
4639 /* Realize the widget. Only after that is the X window created. */
4640 XtRealizeWidget (widget);
4642 #endif /* !USE_MOTIF */
4644 /* Install an action hook that lets us detect when the user
4645 finishes interacting with a scroll bar. */
4646 if (action_hook_id == 0)
4647 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4649 /* Remember X window and widget in the scroll bar vector. */
4650 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4651 xwindow = XtWindow (widget);
4652 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
4654 UNBLOCK_INPUT;
4656 #endif /* not USE_GTK */
4659 /* Set the thumb size and position of scroll bar BAR. We are currently
4660 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4662 #ifdef USE_GTK
4663 static void
4664 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4665 struct scroll_bar *bar;
4666 int portion, position, whole;
4668 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4671 #else /* not USE_GTK */
4672 static void
4673 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4674 struct scroll_bar *bar;
4675 int portion, position, whole;
4677 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4678 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4679 float top, shown;
4681 BLOCK_INPUT;
4683 #ifdef USE_MOTIF
4685 /* We use an estimate of 30 chars per line rather than the real
4686 `portion' value. This has the disadvantage that the thumb size
4687 is not very representative, but it makes our life a lot easier.
4688 Otherwise, we have to constantly adjust the thumb size, which
4689 we can't always do quickly enough: while dragging, the size of
4690 the thumb might prevent the user from dragging the thumb all the
4691 way to the end. but Motif and some versions of Xaw3d don't allow
4692 updating the thumb size while dragging. Also, even if we can update
4693 its size, the update will often happen too late.
4694 If you don't believe it, check out revision 1.650 of xterm.c to see
4695 what hoops we were going through and the still poor behavior we got. */
4696 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4697 /* When the thumb is at the bottom, position == whole.
4698 So we need to increase `whole' to make space for the thumb. */
4699 whole += portion;
4701 if (whole <= 0)
4702 top = 0, shown = 1;
4703 else
4705 top = (float) position / whole;
4706 shown = (float) portion / whole;
4709 if (NILP (bar->dragging))
4711 int size, value;
4713 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4714 is the scroll bar's maximum and MIN is the scroll bar's minimum
4715 value. */
4716 size = shown * XM_SB_MAX;
4717 size = min (size, XM_SB_MAX);
4718 size = max (size, 1);
4720 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4721 value = top * XM_SB_MAX;
4722 value = min (value, XM_SB_MAX - size);
4724 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4726 #else /* !USE_MOTIF i.e. use Xaw */
4728 if (whole == 0)
4729 top = 0, shown = 1;
4730 else
4732 top = (float) position / whole;
4733 shown = (float) portion / whole;
4737 float old_top, old_shown;
4738 Dimension height;
4739 XtVaGetValues (widget,
4740 XtNtopOfThumb, &old_top,
4741 XtNshown, &old_shown,
4742 XtNheight, &height,
4743 NULL);
4745 /* Massage the top+shown values. */
4746 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4747 top = max (0, min (1, top));
4748 else
4749 top = old_top;
4750 /* Keep two pixels available for moving the thumb down. */
4751 shown = max (0, min (1 - top - (2.0 / height), shown));
4753 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4754 check that your system's configuration file contains a define
4755 for `NARROWPROTO'. See s/freebsd.h for an example. */
4756 if (top != old_top || shown != old_shown)
4758 if (NILP (bar->dragging))
4759 XawScrollbarSetThumb (widget, top, shown);
4760 else
4762 /* Try to make the scrolling a tad smoother. */
4763 if (!xaw3d_pick_top)
4764 shown = min (shown, old_shown);
4766 XawScrollbarSetThumb (widget, top, shown);
4770 #endif /* !USE_MOTIF */
4772 UNBLOCK_INPUT;
4774 #endif /* not USE_GTK */
4776 #endif /* USE_TOOLKIT_SCROLL_BARS */
4780 /************************************************************************
4781 Scroll bars, general
4782 ************************************************************************/
4784 /* Create a scroll bar and return the scroll bar vector for it. W is
4785 the Emacs window on which to create the scroll bar. TOP, LEFT,
4786 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4787 scroll bar. */
4789 static struct scroll_bar *
4790 x_scroll_bar_create (w, top, left, width, height)
4791 struct window *w;
4792 int top, left, width, height;
4794 struct frame *f = XFRAME (w->frame);
4795 struct scroll_bar *bar
4796 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
4798 BLOCK_INPUT;
4800 #ifdef USE_TOOLKIT_SCROLL_BARS
4801 x_create_toolkit_scroll_bar (f, bar);
4802 #else /* not USE_TOOLKIT_SCROLL_BARS */
4804 XSetWindowAttributes a;
4805 unsigned long mask;
4806 Window window;
4808 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4809 if (a.background_pixel == -1)
4810 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4812 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4813 | ButtonMotionMask | PointerMotionHintMask
4814 | ExposureMask);
4815 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4817 mask = (CWBackPixel | CWEventMask | CWCursor);
4819 /* Clear the area of W that will serve as a scroll bar. This is
4820 for the case that a window has been split horizontally. In
4821 this case, no clear_frame is generated to reduce flickering. */
4822 if (width > 0 && height > 0)
4823 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4824 left, top, width,
4825 window_box_height (w), False);
4827 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4828 /* Position and size of scroll bar. */
4829 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4830 top,
4831 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4832 height,
4833 /* Border width, depth, class, and visual. */
4835 CopyFromParent,
4836 CopyFromParent,
4837 CopyFromParent,
4838 /* Attributes. */
4839 mask, &a);
4840 SET_SCROLL_BAR_X_WINDOW (bar, window);
4842 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4844 XSETWINDOW (bar->window, w);
4845 XSETINT (bar->top, top);
4846 XSETINT (bar->left, left);
4847 XSETINT (bar->width, width);
4848 XSETINT (bar->height, height);
4849 XSETINT (bar->start, 0);
4850 XSETINT (bar->end, 0);
4851 bar->dragging = Qnil;
4852 #ifdef USE_TOOLKIT_SCROLL_BARS
4853 bar->fringe_extended_p = Qnil;
4854 #endif
4856 /* Add bar to its frame's list of scroll bars. */
4857 bar->next = FRAME_SCROLL_BARS (f);
4858 bar->prev = Qnil;
4859 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4860 if (!NILP (bar->next))
4861 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4863 /* Map the window/widget. */
4864 #ifdef USE_TOOLKIT_SCROLL_BARS
4866 #ifdef USE_GTK
4867 xg_update_scrollbar_pos (f,
4868 SCROLL_BAR_X_WINDOW (bar),
4869 top,
4870 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4871 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4872 max (height, 1));
4873 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
4874 #else /* not USE_GTK */
4875 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4876 XtConfigureWidget (scroll_bar,
4877 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4878 top,
4879 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4880 max (height, 1), 0);
4881 XtMapWidget (scroll_bar);
4882 #endif /* not USE_GTK */
4884 #else /* not USE_TOOLKIT_SCROLL_BARS */
4885 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4886 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4888 UNBLOCK_INPUT;
4889 return bar;
4893 /* Draw BAR's handle in the proper position.
4895 If the handle is already drawn from START to END, don't bother
4896 redrawing it, unless REBUILD is non-zero; in that case, always
4897 redraw it. (REBUILD is handy for drawing the handle after expose
4898 events.)
4900 Normally, we want to constrain the start and end of the handle to
4901 fit inside its rectangle, but if the user is dragging the scroll
4902 bar handle, we want to let them drag it down all the way, so that
4903 the bar's top is as far down as it goes; otherwise, there's no way
4904 to move to the very end of the buffer. */
4906 #ifndef USE_TOOLKIT_SCROLL_BARS
4908 static void
4909 x_scroll_bar_set_handle (bar, start, end, rebuild)
4910 struct scroll_bar *bar;
4911 int start, end;
4912 int rebuild;
4914 int dragging = ! NILP (bar->dragging);
4915 Window w = SCROLL_BAR_X_WINDOW (bar);
4916 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4917 GC gc = f->output_data.x->normal_gc;
4919 /* If the display is already accurate, do nothing. */
4920 if (! rebuild
4921 && start == XINT (bar->start)
4922 && end == XINT (bar->end))
4923 return;
4925 BLOCK_INPUT;
4928 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
4929 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
4930 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4932 /* Make sure the values are reasonable, and try to preserve
4933 the distance between start and end. */
4935 int length = end - start;
4937 if (start < 0)
4938 start = 0;
4939 else if (start > top_range)
4940 start = top_range;
4941 end = start + length;
4943 if (end < start)
4944 end = start;
4945 else if (end > top_range && ! dragging)
4946 end = top_range;
4949 /* Store the adjusted setting in the scroll bar. */
4950 XSETINT (bar->start, start);
4951 XSETINT (bar->end, end);
4953 /* Clip the end position, just for display. */
4954 if (end > top_range)
4955 end = top_range;
4957 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
4958 below top positions, to make sure the handle is always at least
4959 that many pixels tall. */
4960 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
4962 /* Draw the empty space above the handle. Note that we can't clear
4963 zero-height areas; that means "clear to end of window." */
4964 if (0 < start)
4965 x_clear_area (FRAME_X_DISPLAY (f), w,
4966 /* x, y, width, height, and exposures. */
4967 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4968 VERTICAL_SCROLL_BAR_TOP_BORDER,
4969 inside_width, start,
4970 False);
4972 /* Change to proper foreground color if one is specified. */
4973 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4974 XSetForeground (FRAME_X_DISPLAY (f), gc,
4975 f->output_data.x->scroll_bar_foreground_pixel);
4977 /* Draw the handle itself. */
4978 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
4979 /* x, y, width, height */
4980 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4981 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
4982 inside_width, end - start);
4984 /* Restore the foreground color of the GC if we changed it above. */
4985 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4986 XSetForeground (FRAME_X_DISPLAY (f), gc,
4987 FRAME_FOREGROUND_PIXEL (f));
4989 /* Draw the empty space below the handle. Note that we can't
4990 clear zero-height areas; that means "clear to end of window." */
4991 if (end < inside_height)
4992 x_clear_area (FRAME_X_DISPLAY (f), w,
4993 /* x, y, width, height, and exposures. */
4994 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4995 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
4996 inside_width, inside_height - end,
4997 False);
5001 UNBLOCK_INPUT;
5004 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5006 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5007 nil. */
5009 static void
5010 x_scroll_bar_remove (bar)
5011 struct scroll_bar *bar;
5013 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5014 BLOCK_INPUT;
5016 #ifdef USE_TOOLKIT_SCROLL_BARS
5017 #ifdef USE_GTK
5018 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
5019 #else /* not USE_GTK */
5020 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5021 #endif /* not USE_GTK */
5022 #else
5023 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
5024 #endif
5026 /* Disassociate this scroll bar from its window. */
5027 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5029 UNBLOCK_INPUT;
5033 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5034 that we are displaying PORTION characters out of a total of WHOLE
5035 characters, starting at POSITION. If WINDOW has no scroll bar,
5036 create one. */
5038 static void
5039 XTset_vertical_scroll_bar (w, portion, whole, position)
5040 struct window *w;
5041 int portion, whole, position;
5043 struct frame *f = XFRAME (w->frame);
5044 struct scroll_bar *bar;
5045 int top, height, left, sb_left, width, sb_width;
5046 int window_y, window_height;
5047 #ifdef USE_TOOLKIT_SCROLL_BARS
5048 int fringe_extended_p;
5049 #endif
5051 /* Get window dimensions. */
5052 window_box (w, -1, 0, &window_y, 0, &window_height);
5053 top = window_y;
5054 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5055 height = window_height;
5057 /* Compute the left edge of the scroll bar area. */
5058 left = WINDOW_SCROLL_BAR_AREA_X (w);
5060 /* Compute the width of the scroll bar which might be less than
5061 the width of the area reserved for the scroll bar. */
5062 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5063 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5064 else
5065 sb_width = width;
5067 /* Compute the left edge of the scroll bar. */
5068 #ifdef USE_TOOLKIT_SCROLL_BARS
5069 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5070 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5071 else
5072 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5073 #else
5074 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5075 sb_left = left + width - sb_width;
5076 else
5077 sb_left = left;
5078 #endif
5080 #ifdef USE_TOOLKIT_SCROLL_BARS
5081 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5082 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5083 && WINDOW_LEFT_FRINGE_WIDTH (w)
5084 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5085 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5086 else
5087 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5088 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5089 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5090 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5091 #endif
5093 /* Does the scroll bar exist yet? */
5094 if (NILP (w->vertical_scroll_bar))
5096 if (width > 0 && height > 0)
5098 BLOCK_INPUT;
5099 #ifdef USE_TOOLKIT_SCROLL_BARS
5100 if (fringe_extended_p)
5101 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5102 sb_left, top, sb_width, height, False);
5103 else
5104 #endif
5105 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5106 left, top, width, height, False);
5107 UNBLOCK_INPUT;
5110 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5112 else
5114 /* It may just need to be moved and resized. */
5115 unsigned int mask = 0;
5117 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5119 BLOCK_INPUT;
5121 if (sb_left != XINT (bar->left))
5122 mask |= CWX;
5123 if (top != XINT (bar->top))
5124 mask |= CWY;
5125 if (sb_width != XINT (bar->width))
5126 mask |= CWWidth;
5127 if (height != XINT (bar->height))
5128 mask |= CWHeight;
5130 #ifdef USE_TOOLKIT_SCROLL_BARS
5132 /* Move/size the scroll bar widget. */
5133 if (mask || !NILP (bar->fringe_extended_p) != fringe_extended_p)
5135 /* Since toolkit scroll bars are smaller than the space reserved
5136 for them on the frame, we have to clear "under" them. */
5137 if (width > 0 && height > 0)
5139 if (fringe_extended_p)
5140 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5141 sb_left, top, sb_width, height, False);
5142 else
5143 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5144 left, top, width, height, False);
5146 #ifdef USE_GTK
5147 xg_update_scrollbar_pos (f,
5148 SCROLL_BAR_X_WINDOW (bar),
5149 top,
5150 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5151 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5152 max (height, 1));
5153 #else /* not USE_GTK */
5154 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5155 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5156 top,
5157 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5158 max (height, 1), 0);
5159 #endif /* not USE_GTK */
5161 #else /* not USE_TOOLKIT_SCROLL_BARS */
5163 /* Clear areas not covered by the scroll bar because of
5164 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5165 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5167 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5168 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5169 height, False);
5170 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5171 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5172 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5173 height, False);
5176 /* Clear areas not covered by the scroll bar because it's not as
5177 wide as the area reserved for it. This makes sure a
5178 previous mode line display is cleared after C-x 2 C-x 1, for
5179 example. */
5181 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5182 int rest = area_width - sb_width;
5183 if (rest > 0 && height > 0)
5185 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5186 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5187 left + area_width - rest, top,
5188 rest, height, False);
5189 else
5190 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5191 left, top, rest, height, False);
5195 /* Move/size the scroll bar window. */
5196 if (mask)
5198 XWindowChanges wc;
5200 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5201 wc.y = top;
5202 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5203 wc.height = height;
5204 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
5205 mask, &wc);
5208 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5210 /* Remember new settings. */
5211 XSETINT (bar->left, sb_left);
5212 XSETINT (bar->top, top);
5213 XSETINT (bar->width, sb_width);
5214 XSETINT (bar->height, height);
5216 UNBLOCK_INPUT;
5219 #ifdef USE_TOOLKIT_SCROLL_BARS
5220 bar->fringe_extended_p = fringe_extended_p ? Qt : Qnil;
5222 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5223 #else /* not USE_TOOLKIT_SCROLL_BARS */
5224 /* Set the scroll bar's current state, unless we're currently being
5225 dragged. */
5226 if (NILP (bar->dragging))
5228 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5230 if (whole == 0)
5231 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5232 else
5234 int start = ((double) position * top_range) / whole;
5235 int end = ((double) (position + portion) * top_range) / whole;
5236 x_scroll_bar_set_handle (bar, start, end, 0);
5239 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5241 XSETVECTOR (w->vertical_scroll_bar, bar);
5245 /* The following three hooks are used when we're doing a thorough
5246 redisplay of the frame. We don't explicitly know which scroll bars
5247 are going to be deleted, because keeping track of when windows go
5248 away is a real pain - "Can you say set-window-configuration, boys
5249 and girls?" Instead, we just assert at the beginning of redisplay
5250 that *all* scroll bars are to be removed, and then save a scroll bar
5251 from the fiery pit when we actually redisplay its window. */
5253 /* Arrange for all scroll bars on FRAME to be removed at the next call
5254 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5255 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5257 static void
5258 XTcondemn_scroll_bars (frame)
5259 FRAME_PTR frame;
5261 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5262 while (! NILP (FRAME_SCROLL_BARS (frame)))
5264 Lisp_Object bar;
5265 bar = FRAME_SCROLL_BARS (frame);
5266 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5267 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5268 XSCROLL_BAR (bar)->prev = Qnil;
5269 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5270 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5271 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5276 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5277 Note that WINDOW isn't necessarily condemned at all. */
5279 static void
5280 XTredeem_scroll_bar (window)
5281 struct window *window;
5283 struct scroll_bar *bar;
5284 struct frame *f;
5286 /* We can't redeem this window's scroll bar if it doesn't have one. */
5287 if (NILP (window->vertical_scroll_bar))
5288 abort ();
5290 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5292 /* Unlink it from the condemned list. */
5293 f = XFRAME (WINDOW_FRAME (window));
5294 if (NILP (bar->prev))
5296 /* If the prev pointer is nil, it must be the first in one of
5297 the lists. */
5298 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5299 /* It's not condemned. Everything's fine. */
5300 return;
5301 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5302 window->vertical_scroll_bar))
5303 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5304 else
5305 /* If its prev pointer is nil, it must be at the front of
5306 one or the other! */
5307 abort ();
5309 else
5310 XSCROLL_BAR (bar->prev)->next = bar->next;
5312 if (! NILP (bar->next))
5313 XSCROLL_BAR (bar->next)->prev = bar->prev;
5315 bar->next = FRAME_SCROLL_BARS (f);
5316 bar->prev = Qnil;
5317 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5318 if (! NILP (bar->next))
5319 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5322 /* Remove all scroll bars on FRAME that haven't been saved since the
5323 last call to `*condemn_scroll_bars_hook'. */
5325 static void
5326 XTjudge_scroll_bars (f)
5327 FRAME_PTR f;
5329 Lisp_Object bar, next;
5331 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5333 /* Clear out the condemned list now so we won't try to process any
5334 more events on the hapless scroll bars. */
5335 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5337 for (; ! NILP (bar); bar = next)
5339 struct scroll_bar *b = XSCROLL_BAR (bar);
5341 x_scroll_bar_remove (b);
5343 next = b->next;
5344 b->next = b->prev = Qnil;
5347 /* Now there should be no references to the condemned scroll bars,
5348 and they should get garbage-collected. */
5352 #ifndef USE_TOOLKIT_SCROLL_BARS
5353 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5354 is a no-op when using toolkit scroll bars.
5356 This may be called from a signal handler, so we have to ignore GC
5357 mark bits. */
5359 static void
5360 x_scroll_bar_expose (bar, event)
5361 struct scroll_bar *bar;
5362 XEvent *event;
5364 Window w = SCROLL_BAR_X_WINDOW (bar);
5365 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5366 GC gc = f->output_data.x->normal_gc;
5367 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5369 BLOCK_INPUT;
5371 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
5373 /* Switch to scroll bar foreground color. */
5374 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5375 XSetForeground (FRAME_X_DISPLAY (f), gc,
5376 f->output_data.x->scroll_bar_foreground_pixel);
5378 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5379 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5381 /* x, y, width, height */
5382 0, 0,
5383 XINT (bar->width) - 1 - width_trim - width_trim,
5384 XINT (bar->height) - 1);
5386 /* Restore the foreground color of the GC if we changed it above. */
5387 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5388 XSetForeground (FRAME_X_DISPLAY (f), gc,
5389 FRAME_FOREGROUND_PIXEL (f));
5391 UNBLOCK_INPUT;
5394 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5396 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5397 is set to something other than NO_EVENT, it is enqueued.
5399 This may be called from a signal handler, so we have to ignore GC
5400 mark bits. */
5403 static void
5404 x_scroll_bar_handle_click (bar, event, emacs_event)
5405 struct scroll_bar *bar;
5406 XEvent *event;
5407 struct input_event *emacs_event;
5409 if (! GC_WINDOWP (bar->window))
5410 abort ();
5412 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5413 emacs_event->code = event->xbutton.button - Button1;
5414 emacs_event->modifiers
5415 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5416 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5417 event->xbutton.state)
5418 | (event->type == ButtonRelease
5419 ? up_modifier
5420 : down_modifier));
5421 emacs_event->frame_or_window = bar->window;
5422 emacs_event->arg = Qnil;
5423 emacs_event->timestamp = event->xbutton.time;
5425 #if 0
5426 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5427 int internal_height
5428 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5429 #endif
5430 int top_range
5431 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5432 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5434 if (y < 0) y = 0;
5435 if (y > top_range) y = top_range;
5437 if (y < XINT (bar->start))
5438 emacs_event->part = scroll_bar_above_handle;
5439 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5440 emacs_event->part = scroll_bar_handle;
5441 else
5442 emacs_event->part = scroll_bar_below_handle;
5444 /* Just because the user has clicked on the handle doesn't mean
5445 they want to drag it. Lisp code needs to be able to decide
5446 whether or not we're dragging. */
5447 #if 0
5448 /* If the user has just clicked on the handle, record where they're
5449 holding it. */
5450 if (event->type == ButtonPress
5451 && emacs_event->part == scroll_bar_handle)
5452 XSETINT (bar->dragging, y - XINT (bar->start));
5453 #endif
5455 #ifndef USE_TOOLKIT_SCROLL_BARS
5456 /* If the user has released the handle, set it to its final position. */
5457 if (event->type == ButtonRelease
5458 && ! NILP (bar->dragging))
5460 int new_start = y - XINT (bar->dragging);
5461 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5463 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5464 bar->dragging = Qnil;
5466 #endif
5468 /* Same deal here as the other #if 0. */
5469 #if 0
5470 /* Clicks on the handle are always reported as occurring at the top of
5471 the handle. */
5472 if (emacs_event->part == scroll_bar_handle)
5473 emacs_event->x = bar->start;
5474 else
5475 XSETINT (emacs_event->x, y);
5476 #else
5477 XSETINT (emacs_event->x, y);
5478 #endif
5480 XSETINT (emacs_event->y, top_range);
5484 #ifndef USE_TOOLKIT_SCROLL_BARS
5486 /* Handle some mouse motion while someone is dragging the scroll bar.
5488 This may be called from a signal handler, so we have to ignore GC
5489 mark bits. */
5491 static void
5492 x_scroll_bar_note_movement (bar, event)
5493 struct scroll_bar *bar;
5494 XEvent *event;
5496 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5498 last_mouse_movement_time = event->xmotion.time;
5500 f->mouse_moved = 1;
5501 XSETVECTOR (last_mouse_scroll_bar, bar);
5503 /* If we're dragging the bar, display it. */
5504 if (! GC_NILP (bar->dragging))
5506 /* Where should the handle be now? */
5507 int new_start = event->xmotion.y - XINT (bar->dragging);
5509 if (new_start != XINT (bar->start))
5511 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5513 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5518 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5520 /* Return information to the user about the current position of the mouse
5521 on the scroll bar. */
5523 static void
5524 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5525 FRAME_PTR *fp;
5526 Lisp_Object *bar_window;
5527 enum scroll_bar_part *part;
5528 Lisp_Object *x, *y;
5529 unsigned long *time;
5531 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5532 Window w = SCROLL_BAR_X_WINDOW (bar);
5533 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5534 int win_x, win_y;
5535 Window dummy_window;
5536 int dummy_coord;
5537 unsigned int dummy_mask;
5539 BLOCK_INPUT;
5541 /* Get the mouse's position relative to the scroll bar window, and
5542 report that. */
5543 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5545 /* Root, child, root x and root y. */
5546 &dummy_window, &dummy_window,
5547 &dummy_coord, &dummy_coord,
5549 /* Position relative to scroll bar. */
5550 &win_x, &win_y,
5552 /* Mouse buttons and modifier keys. */
5553 &dummy_mask))
5555 else
5557 #if 0
5558 int inside_height
5559 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5560 #endif
5561 int top_range
5562 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5564 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5566 if (! NILP (bar->dragging))
5567 win_y -= XINT (bar->dragging);
5569 if (win_y < 0)
5570 win_y = 0;
5571 if (win_y > top_range)
5572 win_y = top_range;
5574 *fp = f;
5575 *bar_window = bar->window;
5577 if (! NILP (bar->dragging))
5578 *part = scroll_bar_handle;
5579 else if (win_y < XINT (bar->start))
5580 *part = scroll_bar_above_handle;
5581 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5582 *part = scroll_bar_handle;
5583 else
5584 *part = scroll_bar_below_handle;
5586 XSETINT (*x, win_y);
5587 XSETINT (*y, top_range);
5589 f->mouse_moved = 0;
5590 last_mouse_scroll_bar = Qnil;
5593 *time = last_mouse_movement_time;
5595 UNBLOCK_INPUT;
5599 /* The screen has been cleared so we may have changed foreground or
5600 background colors, and the scroll bars may need to be redrawn.
5601 Clear out the scroll bars, and ask for expose events, so we can
5602 redraw them. */
5604 void
5605 x_scroll_bar_clear (f)
5606 FRAME_PTR f;
5608 #ifndef USE_TOOLKIT_SCROLL_BARS
5609 Lisp_Object bar;
5611 /* We can have scroll bars even if this is 0,
5612 if we just turned off scroll bar mode.
5613 But in that case we should not clear them. */
5614 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5615 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5616 bar = XSCROLL_BAR (bar)->next)
5617 XClearArea (FRAME_X_DISPLAY (f),
5618 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
5619 0, 0, 0, 0, True);
5620 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5624 /* The main X event-reading loop - XTread_socket. */
5626 #if 0
5627 /* Time stamp of enter window event. This is only used by XTread_socket,
5628 but we have to put it out here, since static variables within functions
5629 sometimes don't work. */
5631 static Time enter_timestamp;
5632 #endif
5634 /* This holds the state XLookupString needs to implement dead keys
5635 and other tricks known as "compose processing". _X Window System_
5636 says that a portable program can't use this, but Stephen Gildea assures
5637 me that letting the compiler initialize it to zeros will work okay.
5639 This must be defined outside of XTread_socket, for the same reasons
5640 given for enter_timestamp, above. */
5642 static XComposeStatus compose_status;
5644 /* Record the last 100 characters stored
5645 to help debug the loss-of-chars-during-GC problem. */
5647 static int temp_index;
5648 static short temp_buffer[100];
5650 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5651 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5652 temp_index = 0; \
5653 temp_buffer[temp_index++] = (keysym)
5655 /* Set this to nonzero to fake an "X I/O error"
5656 on a particular display. */
5658 struct x_display_info *XTread_socket_fake_io_error;
5660 /* When we find no input here, we occasionally do a no-op command
5661 to verify that the X server is still running and we can still talk with it.
5662 We try all the open displays, one by one.
5663 This variable is used for cycling thru the displays. */
5665 static struct x_display_info *next_noop_dpyinfo;
5667 #define SET_SAVED_MENU_EVENT(size) \
5668 do \
5670 if (f->output_data.x->saved_menu_event == 0) \
5671 f->output_data.x->saved_menu_event \
5672 = (XEvent *) xmalloc (sizeof (XEvent)); \
5673 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5674 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5675 XSETFRAME (inev.ie.frame_or_window, f); \
5677 while (0)
5679 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5680 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5683 enum
5685 X_EVENT_NORMAL,
5686 X_EVENT_GOTO_OUT,
5687 X_EVENT_DROP
5690 /* Filter events for the current X input method.
5691 DPYINFO is the display this event is for.
5692 EVENT is the X event to filter.
5694 Returns non-zero if the event was filtered, caller shall not process
5695 this event further.
5696 Returns zero if event is wasn't filtered. */
5698 #ifdef HAVE_X_I18N
5699 static int
5700 x_filter_event (dpyinfo, event)
5701 struct x_display_info *dpyinfo;
5702 XEvent *event;
5704 /* XFilterEvent returns non-zero if the input method has
5705 consumed the event. We pass the frame's X window to
5706 XFilterEvent because that's the one for which the IC
5707 was created. */
5709 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5710 event->xclient.window);
5712 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5714 #endif
5716 #ifdef USE_GTK
5717 static int current_count;
5718 static int current_finish;
5719 static struct input_event *current_hold_quit;
5721 /* This is the filter function invoked by the GTK event loop.
5722 It is invoked before the XEvent is translated to a GdkEvent,
5723 so we have a chance to act on the event before GTK. */
5724 static GdkFilterReturn
5725 event_handler_gdk (gxev, ev, data)
5726 GdkXEvent *gxev;
5727 GdkEvent *ev;
5728 gpointer data;
5730 XEvent *xev = (XEvent *) gxev;
5732 if (current_count >= 0)
5734 struct x_display_info *dpyinfo;
5736 dpyinfo = x_display_info_for_display (xev->xany.display);
5738 #ifdef HAVE_X_I18N
5739 /* Filter events for the current X input method.
5740 GTK calls XFilterEvent but not for key press and release,
5741 so we do it here. */
5742 if (xev->type == KeyPress || xev->type == KeyRelease)
5743 if (dpyinfo && x_filter_event (dpyinfo, xev))
5744 return GDK_FILTER_REMOVE;
5745 #endif
5747 if (! dpyinfo)
5748 current_finish = X_EVENT_NORMAL;
5749 else
5751 current_count +=
5752 handle_one_xevent (dpyinfo, xev, &current_finish,
5753 current_hold_quit);
5756 else
5757 current_finish = x_dispatch_event (xev, xev->xany.display);
5759 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5760 return GDK_FILTER_REMOVE;
5762 return GDK_FILTER_CONTINUE;
5764 #endif /* USE_GTK */
5767 /* Handles the XEvent EVENT on display DPYINFO.
5769 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5770 *FINISH is zero if caller should continue reading events.
5771 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5773 We return the number of characters stored into the buffer. */
5775 static int
5776 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5777 struct x_display_info *dpyinfo;
5778 XEvent *eventp;
5779 int *finish;
5780 struct input_event *hold_quit;
5782 union {
5783 struct input_event ie;
5784 struct selection_input_event sie;
5785 } inev;
5786 int count = 0;
5787 int do_help = 0;
5788 int nbytes = 0;
5789 struct frame *f = NULL;
5790 struct coding_system coding;
5791 XEvent event = *eventp;
5793 *finish = X_EVENT_NORMAL;
5795 EVENT_INIT (inev.ie);
5796 inev.ie.kind = NO_EVENT;
5797 inev.ie.arg = Qnil;
5799 switch (event.type)
5801 case ClientMessage:
5803 if (event.xclient.message_type
5804 == dpyinfo->Xatom_wm_protocols
5805 && event.xclient.format == 32)
5807 if (event.xclient.data.l[0]
5808 == dpyinfo->Xatom_wm_take_focus)
5810 /* Use x_any_window_to_frame because this
5811 could be the shell widget window
5812 if the frame has no title bar. */
5813 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5814 #ifdef HAVE_X_I18N
5815 /* Not quite sure this is needed -pd */
5816 if (f && FRAME_XIC (f))
5817 XSetICFocus (FRAME_XIC (f));
5818 #endif
5819 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5820 instructs the WM to set the input focus automatically for
5821 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5822 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5823 it has set the focus. So, XSetInputFocus below is not
5824 needed.
5826 The call to XSetInputFocus below has also caused trouble. In
5827 cases where the XSetInputFocus done by the WM and the one
5828 below are temporally close (on a fast machine), the call
5829 below can generate additional FocusIn events which confuse
5830 Emacs. */
5832 /* Since we set WM_TAKE_FOCUS, we must call
5833 XSetInputFocus explicitly. But not if f is null,
5834 since that might be an event for a deleted frame. */
5835 if (f)
5837 Display *d = event.xclient.display;
5838 /* Catch and ignore errors, in case window has been
5839 iconified by a window manager such as GWM. */
5840 x_catch_errors (d);
5841 XSetInputFocus (d, event.xclient.window,
5842 /* The ICCCM says this is
5843 the only valid choice. */
5844 RevertToParent,
5845 event.xclient.data.l[1]);
5846 /* This is needed to detect the error
5847 if there is an error. */
5848 XSync (d, False);
5849 x_uncatch_errors ();
5851 /* Not certain about handling scroll bars here */
5852 #endif /* 0 */
5853 goto done;
5856 if (event.xclient.data.l[0]
5857 == dpyinfo->Xatom_wm_save_yourself)
5859 /* Save state modify the WM_COMMAND property to
5860 something which can reinstate us. This notifies
5861 the session manager, who's looking for such a
5862 PropertyNotify. Can restart processing when
5863 a keyboard or mouse event arrives. */
5864 /* If we have a session manager, don't set this.
5865 KDE will then start two Emacsen, one for the
5866 session manager and one for this. */
5867 #ifdef HAVE_X_SM
5868 if (! x_session_have_connection ())
5869 #endif
5871 f = x_top_window_to_frame (dpyinfo,
5872 event.xclient.window);
5873 /* This is just so we only give real data once
5874 for a single Emacs process. */
5875 if (f == SELECTED_FRAME ())
5876 XSetCommand (FRAME_X_DISPLAY (f),
5877 event.xclient.window,
5878 initial_argv, initial_argc);
5879 else if (f)
5880 XSetCommand (FRAME_X_DISPLAY (f),
5881 event.xclient.window,
5882 0, 0);
5884 goto done;
5887 if (event.xclient.data.l[0]
5888 == dpyinfo->Xatom_wm_delete_window)
5890 f = x_any_window_to_frame (dpyinfo,
5891 event.xclient.window);
5892 if (!f)
5893 goto OTHER; /* May be a dialog that is to be removed */
5895 inev.ie.kind = DELETE_WINDOW_EVENT;
5896 XSETFRAME (inev.ie.frame_or_window, f);
5897 goto done;
5900 goto done;
5903 if (event.xclient.message_type
5904 == dpyinfo->Xatom_wm_configure_denied)
5906 goto done;
5909 if (event.xclient.message_type
5910 == dpyinfo->Xatom_wm_window_moved)
5912 int new_x, new_y;
5913 f = x_window_to_frame (dpyinfo, event.xclient.window);
5915 new_x = event.xclient.data.s[0];
5916 new_y = event.xclient.data.s[1];
5918 if (f)
5920 f->left_pos = new_x;
5921 f->top_pos = new_y;
5923 goto done;
5926 #ifdef HACK_EDITRES
5927 if (event.xclient.message_type
5928 == dpyinfo->Xatom_editres)
5930 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5931 if (f)
5932 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5933 &event, NULL);
5934 goto done;
5936 #endif /* HACK_EDITRES */
5938 if ((event.xclient.message_type
5939 == dpyinfo->Xatom_DONE)
5940 || (event.xclient.message_type
5941 == dpyinfo->Xatom_PAGE))
5943 /* Ghostview job completed. Kill it. We could
5944 reply with "Next" if we received "Page", but we
5945 currently never do because we are interested in
5946 images, only, which should have 1 page. */
5947 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5948 f = x_window_to_frame (dpyinfo, event.xclient.window);
5949 if (!f)
5950 goto OTHER;
5951 x_kill_gs_process (pixmap, f);
5952 expose_frame (f, 0, 0, 0, 0);
5953 goto done;
5956 #ifdef USE_TOOLKIT_SCROLL_BARS
5957 /* Scroll bar callbacks send a ClientMessage from which
5958 we construct an input_event. */
5959 if (event.xclient.message_type
5960 == dpyinfo->Xatom_Scrollbar)
5962 x_scroll_bar_to_input_event (&event, &inev.ie);
5963 *finish = X_EVENT_GOTO_OUT;
5964 goto done;
5966 #endif /* USE_TOOLKIT_SCROLL_BARS */
5968 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5969 if (!f)
5970 goto OTHER;
5971 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
5972 *finish = X_EVENT_DROP;
5974 break;
5976 case SelectionNotify:
5977 last_user_time = event.xselection.time;
5978 #ifdef USE_X_TOOLKIT
5979 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
5980 goto OTHER;
5981 #endif /* not USE_X_TOOLKIT */
5982 x_handle_selection_notify (&event.xselection);
5983 break;
5985 case SelectionClear: /* Someone has grabbed ownership. */
5986 last_user_time = event.xselectionclear.time;
5987 #ifdef USE_X_TOOLKIT
5988 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
5989 goto OTHER;
5990 #endif /* USE_X_TOOLKIT */
5992 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
5994 inev.ie.kind = SELECTION_CLEAR_EVENT;
5995 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5996 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5997 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5998 inev.ie.frame_or_window = Qnil;
6000 break;
6002 case SelectionRequest: /* Someone wants our selection. */
6003 last_user_time = event.xselectionrequest.time;
6004 #ifdef USE_X_TOOLKIT
6005 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6006 goto OTHER;
6007 #endif /* USE_X_TOOLKIT */
6009 XSelectionRequestEvent *eventp
6010 = (XSelectionRequestEvent *) &event;
6012 inev.ie.kind = SELECTION_REQUEST_EVENT;
6013 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6014 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6015 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6016 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6017 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6018 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6019 inev.ie.frame_or_window = Qnil;
6021 break;
6023 case PropertyNotify:
6024 last_user_time = event.xproperty.time;
6025 #if 0 /* This is plain wrong. In the case that we are waiting for a
6026 PropertyNotify used as an ACK in incremental selection
6027 transfer, the property will be on the receiver's window. */
6028 #if defined USE_X_TOOLKIT
6029 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
6030 goto OTHER;
6031 #endif
6032 #endif
6033 x_handle_property_notify (&event.xproperty);
6034 goto OTHER;
6036 case ReparentNotify:
6037 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6038 if (f)
6040 int x, y;
6041 f->output_data.x->parent_desc = event.xreparent.parent;
6042 x_real_positions (f, &x, &y);
6043 f->left_pos = x;
6044 f->top_pos = y;
6046 /* Perhaps reparented due to a WM restart. Reset this. */
6047 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6048 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6050 goto OTHER;
6052 case Expose:
6053 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6054 if (f)
6056 x_check_fullscreen (f);
6058 #ifdef USE_GTK
6059 /* This seems to be needed for GTK 2.6. */
6060 x_clear_area (event.xexpose.display,
6061 event.xexpose.window,
6062 event.xexpose.x, event.xexpose.y,
6063 event.xexpose.width, event.xexpose.height,
6064 FALSE);
6065 #endif
6066 if (f->async_visible == 0)
6068 f->async_visible = 1;
6069 f->async_iconified = 0;
6070 f->output_data.x->has_been_visible = 1;
6071 SET_FRAME_GARBAGED (f);
6073 else
6074 expose_frame (f,
6075 event.xexpose.x, event.xexpose.y,
6076 event.xexpose.width, event.xexpose.height);
6078 else
6080 #ifndef USE_TOOLKIT_SCROLL_BARS
6081 struct scroll_bar *bar;
6082 #endif
6083 #if defined USE_LUCID
6084 /* Submenus of the Lucid menu bar aren't widgets
6085 themselves, so there's no way to dispatch events
6086 to them. Recognize this case separately. */
6088 Widget widget
6089 = x_window_to_menu_bar (event.xexpose.window);
6090 if (widget)
6091 xlwmenu_redisplay (widget);
6093 #endif /* USE_LUCID */
6095 #ifdef USE_TOOLKIT_SCROLL_BARS
6096 /* Dispatch event to the widget. */
6097 goto OTHER;
6098 #else /* not USE_TOOLKIT_SCROLL_BARS */
6099 bar = x_window_to_scroll_bar (event.xexpose.display,
6100 event.xexpose.window);
6102 if (bar)
6103 x_scroll_bar_expose (bar, &event);
6104 #ifdef USE_X_TOOLKIT
6105 else
6106 goto OTHER;
6107 #endif /* USE_X_TOOLKIT */
6108 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6110 break;
6112 case GraphicsExpose: /* This occurs when an XCopyArea's
6113 source area was obscured or not
6114 available. */
6115 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6116 if (f)
6118 expose_frame (f,
6119 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6120 event.xgraphicsexpose.width,
6121 event.xgraphicsexpose.height);
6123 #ifdef USE_X_TOOLKIT
6124 else
6125 goto OTHER;
6126 #endif /* USE_X_TOOLKIT */
6127 break;
6129 case NoExpose: /* This occurs when an XCopyArea's
6130 source area was completely
6131 available. */
6132 break;
6134 case UnmapNotify:
6135 /* Redo the mouse-highlight after the tooltip has gone. */
6136 if (event.xmap.window == tip_window)
6138 tip_window = 0;
6139 redo_mouse_highlight ();
6142 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6143 if (f) /* F may no longer exist if
6144 the frame was deleted. */
6146 /* While a frame is unmapped, display generation is
6147 disabled; you don't want to spend time updating a
6148 display that won't ever be seen. */
6149 f->async_visible = 0;
6150 /* We can't distinguish, from the event, whether the window
6151 has become iconified or invisible. So assume, if it
6152 was previously visible, than now it is iconified.
6153 But x_make_frame_invisible clears both
6154 the visible flag and the iconified flag;
6155 and that way, we know the window is not iconified now. */
6156 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6158 f->async_iconified = 1;
6160 inev.ie.kind = ICONIFY_EVENT;
6161 XSETFRAME (inev.ie.frame_or_window, f);
6164 goto OTHER;
6166 case MapNotify:
6167 if (event.xmap.window == tip_window)
6168 /* The tooltip has been drawn already. Avoid
6169 the SET_FRAME_GARBAGED below. */
6170 goto OTHER;
6172 /* We use x_top_window_to_frame because map events can
6173 come for sub-windows and they don't mean that the
6174 frame is visible. */
6175 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6176 if (f)
6178 /* wait_reading_process_output will notice this and update
6179 the frame's display structures.
6180 If we where iconified, we should not set garbaged,
6181 because that stops redrawing on Expose events. This looks
6182 bad if we are called from a recursive event loop
6183 (x_dispatch_event), for example when a dialog is up. */
6184 if (! f->async_iconified)
6185 SET_FRAME_GARBAGED (f);
6187 f->async_visible = 1;
6188 f->async_iconified = 0;
6189 f->output_data.x->has_been_visible = 1;
6191 if (f->iconified)
6193 inev.ie.kind = DEICONIFY_EVENT;
6194 XSETFRAME (inev.ie.frame_or_window, f);
6196 else if (! NILP (Vframe_list)
6197 && ! NILP (XCDR (Vframe_list)))
6198 /* Force a redisplay sooner or later
6199 to update the frame titles
6200 in case this is the second frame. */
6201 record_asynch_buffer_change ();
6203 goto OTHER;
6205 case KeyPress:
6207 last_user_time = event.xkey.time;
6208 ignore_next_mouse_click_timeout = 0;
6210 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6211 /* Dispatch KeyPress events when in menu. */
6212 if (popup_activated ())
6213 goto OTHER;
6214 #endif
6216 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6218 /* If mouse-highlight is an integer, input clears out
6219 mouse highlighting. */
6220 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6221 && (f == 0
6222 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6224 clear_mouse_face (dpyinfo);
6225 dpyinfo->mouse_face_hidden = 1;
6228 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6229 if (f == 0)
6231 /* Scroll bars consume key events, but we want
6232 the keys to go to the scroll bar's frame. */
6233 Widget widget = XtWindowToWidget (dpyinfo->display,
6234 event.xkey.window);
6235 if (widget && XmIsScrollBar (widget))
6237 widget = XtParent (widget);
6238 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6241 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6243 if (f != 0)
6245 KeySym keysym, orig_keysym;
6246 /* al%imercury@uunet.uu.net says that making this 81
6247 instead of 80 fixed a bug whereby meta chars made
6248 his Emacs hang.
6250 It seems that some version of XmbLookupString has
6251 a bug of not returning XBufferOverflow in
6252 status_return even if the input is too long to
6253 fit in 81 bytes. So, we must prepare sufficient
6254 bytes for copy_buffer. 513 bytes (256 chars for
6255 two-byte character set) seems to be a fairly good
6256 approximation. -- 2000.8.10 handa@etl.go.jp */
6257 unsigned char copy_buffer[513];
6258 unsigned char *copy_bufptr = copy_buffer;
6259 int copy_bufsiz = sizeof (copy_buffer);
6260 int modifiers;
6261 Lisp_Object coding_system = Qlatin_1;
6262 Lisp_Object c;
6264 #ifdef USE_GTK
6265 /* Don't pass keys to GTK. A Tab will shift focus to the
6266 tool bar in GTK 2.4. Keys will still go to menus and
6267 dialogs because in that case popup_activated is TRUE
6268 (see above). */
6269 *finish = X_EVENT_DROP;
6270 #endif
6272 event.xkey.state
6273 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6274 extra_keyboard_modifiers);
6275 modifiers = event.xkey.state;
6277 /* This will have to go some day... */
6279 /* make_lispy_event turns chars into control chars.
6280 Don't do it here because XLookupString is too eager. */
6281 event.xkey.state &= ~ControlMask;
6282 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6283 | dpyinfo->super_mod_mask
6284 | dpyinfo->hyper_mod_mask
6285 | dpyinfo->alt_mod_mask);
6287 /* In case Meta is ComposeCharacter,
6288 clear its status. According to Markus Ehrnsperger
6289 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6290 this enables ComposeCharacter to work whether or
6291 not it is combined with Meta. */
6292 if (modifiers & dpyinfo->meta_mod_mask)
6293 bzero (&compose_status, sizeof (compose_status));
6295 #ifdef HAVE_X_I18N
6296 if (FRAME_XIC (f))
6298 Status status_return;
6300 coding_system = Vlocale_coding_system;
6301 nbytes = XmbLookupString (FRAME_XIC (f),
6302 &event.xkey, copy_bufptr,
6303 copy_bufsiz, &keysym,
6304 &status_return);
6305 if (status_return == XBufferOverflow)
6307 copy_bufsiz = nbytes + 1;
6308 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6309 nbytes = XmbLookupString (FRAME_XIC (f),
6310 &event.xkey, copy_bufptr,
6311 copy_bufsiz, &keysym,
6312 &status_return);
6314 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6315 #if 0 && defined X_HAVE_UTF8_STRING
6316 else if (status_return == XLookupKeySym)
6317 { /* Try again but with utf-8. */
6318 coding_system = Qutf_8;
6319 nbytes = Xutf8LookupString (FRAME_XIC (f),
6320 &event.xkey, copy_bufptr,
6321 copy_bufsiz, &keysym,
6322 &status_return);
6323 if (status_return == XBufferOverflow)
6325 copy_bufsiz = nbytes + 1;
6326 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6327 nbytes = Xutf8LookupString (FRAME_XIC (f),
6328 &event.xkey,
6329 copy_bufptr,
6330 copy_bufsiz, &keysym,
6331 &status_return);
6334 #endif
6336 if (status_return == XLookupNone)
6337 break;
6338 else if (status_return == XLookupChars)
6340 keysym = NoSymbol;
6341 modifiers = 0;
6343 else if (status_return != XLookupKeySym
6344 && status_return != XLookupBoth)
6345 abort ();
6347 else
6348 nbytes = XLookupString (&event.xkey, copy_bufptr,
6349 copy_bufsiz, &keysym,
6350 &compose_status);
6351 #else
6352 nbytes = XLookupString (&event.xkey, copy_bufptr,
6353 copy_bufsiz, &keysym,
6354 &compose_status);
6355 #endif
6357 /* If not using XIM/XIC, and a compose sequence is in progress,
6358 we break here. Otherwise, chars_matched is always 0. */
6359 if (compose_status.chars_matched > 0 && nbytes == 0)
6360 break;
6362 bzero (&compose_status, sizeof (compose_status));
6363 orig_keysym = keysym;
6365 /* Common for all keysym input events. */
6366 XSETFRAME (inev.ie.frame_or_window, f);
6367 inev.ie.modifiers
6368 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6369 inev.ie.timestamp = event.xkey.time;
6371 /* First deal with keysyms which have defined
6372 translations to characters. */
6373 if (keysym >= 32 && keysym < 128)
6374 /* Avoid explicitly decoding each ASCII character. */
6376 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6377 inev.ie.code = keysym;
6378 goto done_keysym;
6381 /* Keysyms directly mapped to supported Unicode characters. */
6382 if ((keysym >= 0x01000000 && keysym <= 0x010033ff)
6383 || (keysym >= 0x0100e000 && keysym <= 0x0100ffff))
6385 int code = keysym & 0xFFFF, charset_id, c1, c2;
6387 if (code < 0x80)
6389 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6390 inev.ie.code = code;
6392 else if (code < 0x100)
6394 if (code < 0xA0)
6395 charset_id = CHARSET_8_BIT_CONTROL;
6396 else
6397 charset_id = charset_latin_iso8859_1;
6398 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6399 inev.ie.code = MAKE_CHAR (charset_id, code, 0);
6401 else
6403 if (code < 0x2500)
6404 charset_id = charset_mule_unicode_0100_24ff,
6405 code -= 0x100;
6406 else if (code < 0xE000)
6407 charset_id = charset_mule_unicode_2500_33ff,
6408 code -= 0x2500;
6409 else
6410 charset_id = charset_mule_unicode_e000_ffff,
6411 code -= 0xE000;
6412 c1 = (code / 96) + 32, c2 = (code % 96) + 32;
6413 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6414 inev.ie.code = MAKE_CHAR (charset_id, c1, c2);
6416 goto done_keysym;
6419 /* Now non-ASCII. */
6420 if (HASH_TABLE_P (Vx_keysym_table)
6421 && (NATNUMP (c = Fgethash (make_number (keysym),
6422 Vx_keysym_table,
6423 Qnil))))
6425 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6426 ? ASCII_KEYSTROKE_EVENT
6427 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6428 inev.ie.code = XFASTINT (c);
6429 goto done_keysym;
6432 /* Random non-modifier sorts of keysyms. */
6433 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6434 || keysym == XK_Delete
6435 #ifdef XK_ISO_Left_Tab
6436 || (keysym >= XK_ISO_Left_Tab
6437 && keysym <= XK_ISO_Enter)
6438 #endif
6439 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6440 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6441 #ifdef HPUX
6442 /* This recognizes the "extended function
6443 keys". It seems there's no cleaner way.
6444 Test IsModifierKey to avoid handling
6445 mode_switch incorrectly. */
6446 || ((unsigned) (keysym) >= XK_Select
6447 && (unsigned)(keysym) < XK_KP_Space)
6448 #endif
6449 #ifdef XK_dead_circumflex
6450 || orig_keysym == XK_dead_circumflex
6451 #endif
6452 #ifdef XK_dead_grave
6453 || orig_keysym == XK_dead_grave
6454 #endif
6455 #ifdef XK_dead_tilde
6456 || orig_keysym == XK_dead_tilde
6457 #endif
6458 #ifdef XK_dead_diaeresis
6459 || orig_keysym == XK_dead_diaeresis
6460 #endif
6461 #ifdef XK_dead_macron
6462 || orig_keysym == XK_dead_macron
6463 #endif
6464 #ifdef XK_dead_degree
6465 || orig_keysym == XK_dead_degree
6466 #endif
6467 #ifdef XK_dead_acute
6468 || orig_keysym == XK_dead_acute
6469 #endif
6470 #ifdef XK_dead_cedilla
6471 || orig_keysym == XK_dead_cedilla
6472 #endif
6473 #ifdef XK_dead_breve
6474 || orig_keysym == XK_dead_breve
6475 #endif
6476 #ifdef XK_dead_ogonek
6477 || orig_keysym == XK_dead_ogonek
6478 #endif
6479 #ifdef XK_dead_caron
6480 || orig_keysym == XK_dead_caron
6481 #endif
6482 #ifdef XK_dead_doubleacute
6483 || orig_keysym == XK_dead_doubleacute
6484 #endif
6485 #ifdef XK_dead_abovedot
6486 || orig_keysym == XK_dead_abovedot
6487 #endif
6488 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6489 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6490 /* Any "vendor-specific" key is ok. */
6491 || (orig_keysym & (1 << 28))
6492 || (keysym != NoSymbol && nbytes == 0))
6493 && ! (IsModifierKey (orig_keysym)
6494 #ifndef HAVE_X11R5
6495 #ifdef XK_Mode_switch
6496 || ((unsigned)(orig_keysym) == XK_Mode_switch)
6497 #endif
6498 #ifdef XK_Num_Lock
6499 || ((unsigned)(orig_keysym) == XK_Num_Lock)
6500 #endif
6501 #endif /* not HAVE_X11R5 */
6502 /* The symbols from XK_ISO_Lock
6503 to XK_ISO_Last_Group_Lock
6504 don't have real modifiers but
6505 should be treated similarly to
6506 Mode_switch by Emacs. */
6507 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6508 || ((unsigned)(orig_keysym)
6509 >= XK_ISO_Lock
6510 && (unsigned)(orig_keysym)
6511 <= XK_ISO_Last_Group_Lock)
6512 #endif
6515 STORE_KEYSYM_FOR_DEBUG (keysym);
6516 /* make_lispy_event will convert this to a symbolic
6517 key. */
6518 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6519 inev.ie.code = keysym;
6520 goto done_keysym;
6523 { /* Raw bytes, not keysym. */
6524 register int i;
6525 register int c;
6526 int nchars, len;
6528 /* The input should be decoded with `coding_system'
6529 which depends on which X*LookupString function
6530 we used just above and the locale. */
6531 setup_coding_system (coding_system, &coding);
6532 coding.src_multibyte = 0;
6533 coding.dst_multibyte = 1;
6534 /* The input is converted to events, thus we can't
6535 handle composition. Anyway, there's no XIM that
6536 gives us composition information. */
6537 coding.composing = COMPOSITION_DISABLED;
6539 if (nbytes > 0)
6541 /* Decode the input data. */
6542 int require;
6543 unsigned char *p;
6545 for (i = 0; i < nbytes; i++)
6547 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6550 require = decoding_buffer_size (&coding, nbytes);
6551 p = (unsigned char *) alloca (require);
6552 coding.mode |= CODING_MODE_LAST_BLOCK;
6553 /* We explicitly disable composition handling because
6554 key data should not contain any composition sequence. */
6555 coding.composing = COMPOSITION_DISABLED;
6556 decode_coding (&coding, copy_bufptr, p, nbytes, require);
6557 nbytes = coding.produced;
6558 nchars = coding.produced_char;
6559 copy_bufptr = p;
6561 /* Convert the input data to a sequence of
6562 character events. */
6563 for (i = 0; i < nbytes; i += len)
6565 if (nchars == nbytes)
6566 c = copy_bufptr[i], len = 1;
6567 else
6568 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6569 nbytes - i, len);
6570 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6571 ? ASCII_KEYSTROKE_EVENT
6572 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6573 inev.ie.code = c;
6574 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6578 /* Previous code updated count by nchars rather than nbytes,
6579 but that seems bogus to me. ++kfs */
6580 count += nbytes;
6582 inev.ie.kind = NO_EVENT; /* Already stored above. */
6584 if (keysym == NoSymbol)
6585 break;
6588 done_keysym:
6589 #ifdef HAVE_X_I18N
6590 /* Don't dispatch this event since XtDispatchEvent calls
6591 XFilterEvent, and two calls in a row may freeze the
6592 client. */
6593 break;
6594 #else
6595 goto OTHER;
6596 #endif
6598 case KeyRelease:
6599 last_user_time = event.xkey.time;
6600 #ifdef HAVE_X_I18N
6601 /* Don't dispatch this event since XtDispatchEvent calls
6602 XFilterEvent, and two calls in a row may freeze the
6603 client. */
6604 break;
6605 #else
6606 goto OTHER;
6607 #endif
6609 case EnterNotify:
6610 last_user_time = event.xcrossing.time;
6611 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6613 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6615 if (f && x_mouse_click_focus_ignore_position)
6616 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6618 #if 0
6619 if (event.xcrossing.focus)
6621 /* Avoid nasty pop/raise loops. */
6622 if (f && (!(f->auto_raise)
6623 || !(f->auto_lower)
6624 || (event.xcrossing.time - enter_timestamp) > 500))
6626 x_new_focus_frame (dpyinfo, f);
6627 enter_timestamp = event.xcrossing.time;
6630 else if (f == dpyinfo->x_focus_frame)
6631 x_new_focus_frame (dpyinfo, 0);
6632 #endif
6634 /* EnterNotify counts as mouse movement,
6635 so update things that depend on mouse position. */
6636 if (f && !f->output_data.x->hourglass_p)
6637 note_mouse_movement (f, &event.xmotion);
6638 #ifdef USE_GTK
6639 /* We may get an EnterNotify on the buttons in the toolbar. In that
6640 case we moved out of any highlighted area and need to note this. */
6641 if (!f && last_mouse_glyph_frame)
6642 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6643 #endif
6644 goto OTHER;
6646 case FocusIn:
6647 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6648 goto OTHER;
6650 case LeaveNotify:
6651 last_user_time = event.xcrossing.time;
6652 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6654 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6655 if (f)
6657 if (f == dpyinfo->mouse_face_mouse_frame)
6659 /* If we move outside the frame, then we're
6660 certainly no longer on any text in the frame. */
6661 clear_mouse_face (dpyinfo);
6662 dpyinfo->mouse_face_mouse_frame = 0;
6665 /* Generate a nil HELP_EVENT to cancel a help-echo.
6666 Do it only if there's something to cancel.
6667 Otherwise, the startup message is cleared when
6668 the mouse leaves the frame. */
6669 if (any_help_event_p)
6670 do_help = -1;
6672 #ifdef USE_GTK
6673 /* See comment in EnterNotify above */
6674 else if (last_mouse_glyph_frame)
6675 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6676 #endif
6677 goto OTHER;
6679 case FocusOut:
6680 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6681 goto OTHER;
6683 case MotionNotify:
6685 last_user_time = event.xmotion.time;
6686 previous_help_echo_string = help_echo_string;
6687 help_echo_string = Qnil;
6689 if (dpyinfo->grabbed && last_mouse_frame
6690 && FRAME_LIVE_P (last_mouse_frame))
6691 f = last_mouse_frame;
6692 else
6693 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6695 if (dpyinfo->mouse_face_hidden)
6697 dpyinfo->mouse_face_hidden = 0;
6698 clear_mouse_face (dpyinfo);
6701 if (f)
6704 /* Generate SELECT_WINDOW_EVENTs when needed. */
6705 if (!NILP (Vmouse_autoselect_window))
6707 Lisp_Object window;
6709 window = window_from_coordinates (f,
6710 event.xmotion.x, event.xmotion.y,
6711 0, 0, 0, 0);
6713 /* Window will be selected only when it is not selected now and
6714 last mouse movement event was not in it. Minibuffer window
6715 will be selected only when it is active. */
6716 if (WINDOWP (window)
6717 && !EQ (window, last_window)
6718 && !EQ (window, selected_window)
6719 /* For click-to-focus window managers
6720 create event iff we don't leave the
6721 selected frame. */
6722 && (focus_follows_mouse
6723 || (EQ (XWINDOW (window)->frame,
6724 XWINDOW (selected_window)->frame))))
6726 inev.ie.kind = SELECT_WINDOW_EVENT;
6727 inev.ie.frame_or_window = window;
6730 last_window=window;
6732 if (!note_mouse_movement (f, &event.xmotion))
6733 help_echo_string = previous_help_echo_string;
6735 else
6737 #ifndef USE_TOOLKIT_SCROLL_BARS
6738 struct scroll_bar *bar
6739 = x_window_to_scroll_bar (event.xmotion.display,
6740 event.xmotion.window);
6742 if (bar)
6743 x_scroll_bar_note_movement (bar, &event);
6744 #endif /* USE_TOOLKIT_SCROLL_BARS */
6746 /* If we move outside the frame, then we're
6747 certainly no longer on any text in the frame. */
6748 clear_mouse_face (dpyinfo);
6751 /* If the contents of the global variable help_echo_string
6752 has changed, generate a HELP_EVENT. */
6753 if (!NILP (help_echo_string)
6754 || !NILP (previous_help_echo_string))
6755 do_help = 1;
6756 goto OTHER;
6759 case ConfigureNotify:
6760 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6761 if (f)
6763 #ifndef USE_X_TOOLKIT
6764 #ifdef USE_GTK
6765 xg_resize_widgets (f, event.xconfigure.width,
6766 event.xconfigure.height);
6767 #else /* not USE_GTK */
6768 /* If there is a pending resize for fullscreen, don't
6769 do this one, the right one will come later.
6770 The toolkit version doesn't seem to need this, but we
6771 need to reset it below. */
6772 int dont_resize
6773 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6774 && f->new_text_cols != 0);
6775 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6776 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6778 if (dont_resize)
6779 goto OTHER;
6781 /* In the toolkit version, change_frame_size
6782 is called by the code that handles resizing
6783 of the EmacsFrame widget. */
6785 /* Even if the number of character rows and columns has
6786 not changed, the font size may have changed, so we need
6787 to check the pixel dimensions as well. */
6788 if (columns != FRAME_COLS (f)
6789 || rows != FRAME_LINES (f)
6790 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6791 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6793 change_frame_size (f, rows, columns, 0, 1, 0);
6794 SET_FRAME_GARBAGED (f);
6795 cancel_mouse_face (f);
6797 #endif /* not USE_GTK */
6798 #endif
6800 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6801 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6803 #ifdef USE_GTK
6804 /* GTK creates windows but doesn't map them.
6805 Only get real positions and check fullscreen when mapped. */
6806 if (FRAME_GTK_OUTER_WIDGET (f)
6807 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6808 #endif
6810 x_real_positions (f, &f->left_pos, &f->top_pos);
6812 if (f->want_fullscreen & FULLSCREEN_WAIT)
6813 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6816 #ifdef HAVE_X_I18N
6817 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6818 xic_set_statusarea (f);
6819 #endif
6821 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6823 /* Since the WM decorations come below top_pos now,
6824 we must put them below top_pos in the future. */
6825 f->win_gravity = NorthWestGravity;
6826 x_wm_set_size_hint (f, (long) 0, 0);
6829 goto OTHER;
6831 case ButtonRelease:
6832 case ButtonPress:
6834 /* If we decide we want to generate an event to be seen
6835 by the rest of Emacs, we put it here. */
6836 int tool_bar_p = 0;
6838 bzero (&compose_status, sizeof (compose_status));
6839 last_mouse_glyph_frame = 0;
6840 last_user_time = event.xbutton.time;
6842 if (dpyinfo->grabbed
6843 && last_mouse_frame
6844 && FRAME_LIVE_P (last_mouse_frame))
6845 f = last_mouse_frame;
6846 else
6847 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6849 if (f)
6851 /* Is this in the tool-bar? */
6852 if (WINDOWP (f->tool_bar_window)
6853 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6855 Lisp_Object window;
6856 int x = event.xbutton.x;
6857 int y = event.xbutton.y;
6859 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6860 tool_bar_p = EQ (window, f->tool_bar_window);
6862 if (tool_bar_p && event.xbutton.button < 4)
6864 if (event.xbutton.type == ButtonPress)
6865 handle_tool_bar_click (f, x, y, 1, 0);
6866 else
6867 handle_tool_bar_click (f, x, y, 0,
6868 x_x_to_emacs_modifiers (dpyinfo,
6869 event.xbutton.state));
6873 if (!tool_bar_p)
6874 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6875 if (! popup_activated ())
6876 #endif
6878 if (ignore_next_mouse_click_timeout)
6880 if (event.type == ButtonPress
6881 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6883 ignore_next_mouse_click_timeout = 0;
6884 construct_mouse_click (&inev.ie, &event.xbutton, f);
6886 if (event.type == ButtonRelease)
6887 ignore_next_mouse_click_timeout = 0;
6889 else
6890 construct_mouse_click (&inev.ie, &event.xbutton, f);
6893 else
6895 struct scroll_bar *bar
6896 = x_window_to_scroll_bar (event.xbutton.display,
6897 event.xbutton.window);
6899 #ifdef USE_TOOLKIT_SCROLL_BARS
6900 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6901 scroll bars. */
6902 if (bar && event.xbutton.state & ControlMask)
6904 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6905 *finish = X_EVENT_DROP;
6907 #else /* not USE_TOOLKIT_SCROLL_BARS */
6908 if (bar)
6909 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6910 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6913 if (event.type == ButtonPress)
6915 dpyinfo->grabbed |= (1 << event.xbutton.button);
6916 last_mouse_frame = f;
6918 if (!tool_bar_p)
6919 last_tool_bar_item = -1;
6921 else
6922 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6924 /* Ignore any mouse motion that happened before this event;
6925 any subsequent mouse-movement Emacs events should reflect
6926 only motion after the ButtonPress/Release. */
6927 if (f != 0)
6928 f->mouse_moved = 0;
6930 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6931 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6932 /* For a down-event in the menu bar,
6933 don't pass it to Xt right now.
6934 Instead, save it away
6935 and we will pass it to Xt from kbd_buffer_get_event.
6936 That way, we can run some Lisp code first. */
6937 if (
6938 #ifdef USE_GTK
6939 ! popup_activated ()
6941 #endif
6942 f && event.type == ButtonPress
6943 /* Verify the event is really within the menu bar
6944 and not just sent to it due to grabbing. */
6945 && event.xbutton.x >= 0
6946 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6947 && event.xbutton.y >= 0
6948 && event.xbutton.y < f->output_data.x->menubar_height
6949 && event.xbutton.same_screen)
6951 SET_SAVED_BUTTON_EVENT;
6952 XSETFRAME (last_mouse_press_frame, f);
6953 #ifdef USE_GTK
6954 *finish = X_EVENT_DROP;
6955 #endif
6957 else if (event.type == ButtonPress)
6959 last_mouse_press_frame = Qnil;
6960 goto OTHER;
6963 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6964 but I am trying to be cautious. */
6965 else if (event.type == ButtonRelease)
6967 if (!NILP (last_mouse_press_frame))
6969 f = XFRAME (last_mouse_press_frame);
6970 if (f->output_data.x)
6971 SET_SAVED_BUTTON_EVENT;
6973 else
6974 goto OTHER;
6976 #endif /* USE_MOTIF */
6977 else
6978 goto OTHER;
6979 #endif /* USE_X_TOOLKIT || USE_GTK */
6981 break;
6983 case CirculateNotify:
6984 goto OTHER;
6986 case CirculateRequest:
6987 goto OTHER;
6989 case VisibilityNotify:
6990 goto OTHER;
6992 case MappingNotify:
6993 /* Someone has changed the keyboard mapping - update the
6994 local cache. */
6995 switch (event.xmapping.request)
6997 case MappingModifier:
6998 x_find_modifier_meanings (dpyinfo);
6999 /* This is meant to fall through. */
7000 case MappingKeyboard:
7001 XRefreshKeyboardMapping (&event.xmapping);
7003 goto OTHER;
7005 default:
7006 OTHER:
7007 #ifdef USE_X_TOOLKIT
7008 BLOCK_INPUT;
7009 if (*finish != X_EVENT_DROP)
7010 XtDispatchEvent (&event);
7011 UNBLOCK_INPUT;
7012 #endif /* USE_X_TOOLKIT */
7013 break;
7016 done:
7017 if (inev.ie.kind != NO_EVENT)
7019 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7020 count++;
7023 if (do_help
7024 && !(hold_quit && hold_quit->kind != NO_EVENT))
7026 Lisp_Object frame;
7028 if (f)
7029 XSETFRAME (frame, f);
7030 else
7031 frame = Qnil;
7033 if (do_help > 0)
7035 any_help_event_p = 1;
7036 gen_help_event (help_echo_string, frame, help_echo_window,
7037 help_echo_object, help_echo_pos);
7039 else
7041 help_echo_string = Qnil;
7042 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7044 count++;
7047 *eventp = event;
7048 return count;
7052 /* Handles the XEvent EVENT on display DISPLAY.
7053 This is used for event loops outside the normal event handling,
7054 i.e. looping while a popup menu or a dialog is posted.
7056 Returns the value handle_one_xevent sets in the finish argument. */
7058 x_dispatch_event (event, display)
7059 XEvent *event;
7060 Display *display;
7062 struct x_display_info *dpyinfo;
7063 int finish = X_EVENT_NORMAL;
7065 dpyinfo = x_display_info_for_display (display);
7067 if (dpyinfo)
7068 handle_one_xevent (dpyinfo, event, &finish, 0);
7070 return finish;
7074 /* Read events coming from the X server.
7075 This routine is called by the SIGIO handler.
7076 We return as soon as there are no more events to be read.
7078 We return the number of characters stored into the buffer,
7079 thus pretending to be `read'.
7081 EXPECTED is nonzero if the caller knows input is available. */
7083 static int
7084 XTread_socket (terminal, expected, hold_quit)
7085 struct terminal *terminal;
7086 int expected;
7087 struct input_event *hold_quit;
7089 int count = 0;
7090 XEvent event;
7091 int event_found = 0;
7092 struct x_display_info *dpyinfo;
7094 if (interrupt_input_blocked)
7096 interrupt_input_pending = 1;
7097 return -1;
7100 interrupt_input_pending = 0;
7101 BLOCK_INPUT;
7103 /* So people can tell when we have read the available input. */
7104 input_signal_count++;
7106 ++handling_signal;
7108 #ifdef HAVE_X_SM
7109 /* Only check session manager input for the primary display. */
7110 if (terminal->id == 1 && x_session_have_connection ())
7112 struct input_event inev;
7113 BLOCK_INPUT;
7114 /* We don't need to EVENT_INIT (inev) here, as
7115 x_session_check_input copies an entire input_event. */
7116 if (x_session_check_input (&inev))
7118 kbd_buffer_store_event_hold (&inev, hold_quit);
7119 count++;
7121 UNBLOCK_INPUT;
7123 #endif
7125 /* For debugging, this gives a way to fake an I/O error. */
7126 if (terminal->display_info.x == XTread_socket_fake_io_error)
7128 XTread_socket_fake_io_error = 0;
7129 x_io_error_quitter (dpyinfo->display);
7132 #if 0 /* This loop is a noop now. */
7133 /* Find the display we are supposed to read input for.
7134 It's the one communicating on descriptor SD. */
7135 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7137 #if 0 /* This ought to be unnecessary; let's verify it. */
7138 #ifdef FIOSNBIO
7139 /* If available, Xlib uses FIOSNBIO to make the socket
7140 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7141 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7142 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7143 fcntl (dpyinfo->connection, F_SETFL, 0);
7144 #endif /* ! defined (FIOSNBIO) */
7145 #endif
7147 #if 0 /* This code can't be made to work, with multiple displays,
7148 and appears not to be used on any system any more.
7149 Also keyboard.c doesn't turn O_NDELAY on and off
7150 for X connections. */
7151 #ifndef SIGIO
7152 #ifndef HAVE_SELECT
7153 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7155 extern int read_alarm_should_throw;
7156 read_alarm_should_throw = 1;
7157 XPeekEvent (dpyinfo->display, &event);
7158 read_alarm_should_throw = 0;
7160 #endif /* HAVE_SELECT */
7161 #endif /* SIGIO */
7162 #endif
7164 #endif
7166 #ifndef USE_GTK
7167 while (XPending (terminal->display_info.x->display))
7169 int finish;
7171 XNextEvent (terminal->display_info.x->display, &event);
7173 #ifdef HAVE_X_I18N
7174 /* Filter events for the current X input method. */
7175 if (x_filter_event (terminal->display_info.x, &event))
7176 break;
7177 #endif
7178 event_found = 1;
7180 count += handle_one_xevent (terminal->display_info.x,
7181 &event, &finish, hold_quit);
7183 if (finish == X_EVENT_GOTO_OUT)
7184 goto out;
7187 #else /* USE_GTK */
7189 /* For GTK we must use the GTK event loop. But XEvents gets passed
7190 to our filter function above, and then to the big event switch.
7191 We use a bunch of globals to communicate with our filter function,
7192 that is kind of ugly, but it works.
7194 There is no way to do one display at the time, GTK just does events
7195 from all displays. */
7197 while (gtk_events_pending ())
7199 current_count = count;
7200 current_hold_quit = hold_quit;
7202 gtk_main_iteration ();
7204 count = current_count;
7205 current_count = -1;
7206 current_hold_quit = 0;
7208 if (current_finish == X_EVENT_GOTO_OUT)
7209 break;
7211 #endif /* USE_GTK */
7213 out:;
7215 /* On some systems, an X bug causes Emacs to get no more events
7216 when the window is destroyed. Detect that. (1994.) */
7217 if (! event_found)
7219 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7220 One XNOOP in 100 loops will make Emacs terminate.
7221 B. Bretthauer, 1994 */
7222 x_noop_count++;
7223 if (x_noop_count >= 100)
7225 x_noop_count=0;
7227 if (next_noop_dpyinfo == 0)
7228 next_noop_dpyinfo = x_display_list;
7230 XNoOp (next_noop_dpyinfo->display);
7232 /* Each time we get here, cycle through the displays now open. */
7233 next_noop_dpyinfo = next_noop_dpyinfo->next;
7237 /* If the focus was just given to an auto-raising frame,
7238 raise it now. */
7239 /* ??? This ought to be able to handle more than one such frame. */
7240 if (pending_autoraise_frame)
7242 x_raise_frame (pending_autoraise_frame);
7243 pending_autoraise_frame = 0;
7246 --handling_signal;
7247 UNBLOCK_INPUT;
7249 return count;
7255 /***********************************************************************
7256 Text Cursor
7257 ***********************************************************************/
7259 /* Set clipping for output in glyph row ROW. W is the window in which
7260 we operate. GC is the graphics context to set clipping in.
7262 ROW may be a text row or, e.g., a mode line. Text rows must be
7263 clipped to the interior of the window dedicated to text display,
7264 mode lines must be clipped to the whole window. */
7266 static void
7267 x_clip_to_row (w, row, area, gc)
7268 struct window *w;
7269 struct glyph_row *row;
7270 int area;
7271 GC gc;
7273 struct frame *f = XFRAME (WINDOW_FRAME (w));
7274 XRectangle clip_rect;
7275 int window_x, window_y, window_width;
7277 window_box (w, area, &window_x, &window_y, &window_width, 0);
7279 clip_rect.x = window_x;
7280 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7281 clip_rect.y = max (clip_rect.y, window_y);
7282 clip_rect.width = window_width;
7283 clip_rect.height = row->visible_height;
7285 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7289 /* Draw a hollow box cursor on window W in glyph row ROW. */
7291 static void
7292 x_draw_hollow_cursor (w, row)
7293 struct window *w;
7294 struct glyph_row *row;
7296 struct frame *f = XFRAME (WINDOW_FRAME (w));
7297 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7298 Display *dpy = FRAME_X_DISPLAY (f);
7299 int x, y, wd, h;
7300 XGCValues xgcv;
7301 struct glyph *cursor_glyph;
7302 GC gc;
7304 /* Get the glyph the cursor is on. If we can't tell because
7305 the current matrix is invalid or such, give up. */
7306 cursor_glyph = get_phys_cursor_glyph (w);
7307 if (cursor_glyph == NULL)
7308 return;
7310 /* Compute frame-relative coordinates for phys cursor. */
7311 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7312 wd = w->phys_cursor_width;
7314 /* The foreground of cursor_gc is typically the same as the normal
7315 background color, which can cause the cursor box to be invisible. */
7316 xgcv.foreground = f->output_data.x->cursor_pixel;
7317 if (dpyinfo->scratch_cursor_gc)
7318 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7319 else
7320 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7321 GCForeground, &xgcv);
7322 gc = dpyinfo->scratch_cursor_gc;
7324 /* Set clipping, draw the rectangle, and reset clipping again. */
7325 x_clip_to_row (w, row, TEXT_AREA, gc);
7326 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7327 XSetClipMask (dpy, gc, None);
7331 /* Draw a bar cursor on window W in glyph row ROW.
7333 Implementation note: One would like to draw a bar cursor with an
7334 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7335 Unfortunately, I didn't find a font yet that has this property set.
7336 --gerd. */
7338 static void
7339 x_draw_bar_cursor (w, row, width, kind)
7340 struct window *w;
7341 struct glyph_row *row;
7342 int width;
7343 enum text_cursor_kinds kind;
7345 struct frame *f = XFRAME (w->frame);
7346 struct glyph *cursor_glyph;
7348 /* If cursor is out of bounds, don't draw garbage. This can happen
7349 in mini-buffer windows when switching between echo area glyphs
7350 and mini-buffer. */
7351 cursor_glyph = get_phys_cursor_glyph (w);
7352 if (cursor_glyph == NULL)
7353 return;
7355 /* If on an image, draw like a normal cursor. That's usually better
7356 visible than drawing a bar, esp. if the image is large so that
7357 the bar might not be in the window. */
7358 if (cursor_glyph->type == IMAGE_GLYPH)
7360 struct glyph_row *row;
7361 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7362 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7364 else
7366 Display *dpy = FRAME_X_DISPLAY (f);
7367 Window window = FRAME_X_WINDOW (f);
7368 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7369 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7370 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7371 XGCValues xgcv;
7373 /* If the glyph's background equals the color we normally draw
7374 the bar cursor in, the bar cursor in its normal color is
7375 invisible. Use the glyph's foreground color instead in this
7376 case, on the assumption that the glyph's colors are chosen so
7377 that the glyph is legible. */
7378 if (face->background == f->output_data.x->cursor_pixel)
7379 xgcv.background = xgcv.foreground = face->foreground;
7380 else
7381 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7382 xgcv.graphics_exposures = 0;
7384 if (gc)
7385 XChangeGC (dpy, gc, mask, &xgcv);
7386 else
7388 gc = XCreateGC (dpy, window, mask, &xgcv);
7389 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7392 if (width < 0)
7393 width = FRAME_CURSOR_WIDTH (f);
7394 width = min (cursor_glyph->pixel_width, width);
7396 w->phys_cursor_width = width;
7397 x_clip_to_row (w, row, TEXT_AREA, gc);
7399 if (kind == BAR_CURSOR)
7400 XFillRectangle (dpy, window, gc,
7401 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7402 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7403 width, row->height);
7404 else
7405 XFillRectangle (dpy, window, gc,
7406 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7407 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7408 row->height - width),
7409 cursor_glyph->pixel_width,
7410 width);
7412 XSetClipMask (dpy, gc, None);
7417 /* RIF: Define cursor CURSOR on frame F. */
7419 static void
7420 x_define_frame_cursor (f, cursor)
7421 struct frame *f;
7422 Cursor cursor;
7424 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7428 /* RIF: Clear area on frame F. */
7430 static void
7431 x_clear_frame_area (f, x, y, width, height)
7432 struct frame *f;
7433 int x, y, width, height;
7435 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7436 x, y, width, height, False);
7440 /* RIF: Draw cursor on window W. */
7442 static void
7443 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7444 struct window *w;
7445 struct glyph_row *glyph_row;
7446 int x, y;
7447 int cursor_type, cursor_width;
7448 int on_p, active_p;
7450 struct frame *f = XFRAME (WINDOW_FRAME (w));
7452 if (on_p)
7454 w->phys_cursor_type = cursor_type;
7455 w->phys_cursor_on_p = 1;
7457 if (glyph_row->exact_window_width_line_p
7458 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7460 glyph_row->cursor_in_fringe_p = 1;
7461 draw_fringe_bitmap (w, glyph_row, 0);
7463 else
7464 switch (cursor_type)
7466 case HOLLOW_BOX_CURSOR:
7467 x_draw_hollow_cursor (w, glyph_row);
7468 break;
7470 case FILLED_BOX_CURSOR:
7471 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7472 break;
7474 case BAR_CURSOR:
7475 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7476 break;
7478 case HBAR_CURSOR:
7479 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7480 break;
7482 case NO_CURSOR:
7483 w->phys_cursor_width = 0;
7484 break;
7486 default:
7487 abort ();
7490 #ifdef HAVE_X_I18N
7491 if (w == XWINDOW (f->selected_window))
7492 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7493 xic_set_preeditarea (w, x, y);
7494 #endif
7497 #ifndef XFlush
7498 XFlush (FRAME_X_DISPLAY (f));
7499 #endif
7503 /* Icons. */
7505 /* Make the x-window of frame F use the gnu icon bitmap. */
7508 x_bitmap_icon (f, file)
7509 struct frame *f;
7510 Lisp_Object file;
7512 int bitmap_id;
7514 if (FRAME_X_WINDOW (f) == 0)
7515 return 1;
7517 /* Free up our existing icon bitmap and mask if any. */
7518 if (f->output_data.x->icon_bitmap > 0)
7519 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7520 f->output_data.x->icon_bitmap = 0;
7522 if (STRINGP (file))
7524 #ifdef USE_GTK
7525 /* Use gtk_window_set_icon_from_file () if available,
7526 It's not restricted to bitmaps */
7527 if (xg_set_icon (f, file))
7528 return 0;
7529 #endif /* USE_GTK */
7530 bitmap_id = x_create_bitmap_from_file (f, file);
7531 x_create_bitmap_mask (f, bitmap_id);
7533 else
7535 /* Create the GNU bitmap and mask if necessary. */
7536 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7538 int rc = -1;
7540 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7541 #ifdef USE_GTK
7542 if (xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7543 return 0;
7544 #else
7545 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7546 if (rc != -1)
7547 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7548 #endif /* USE_GTK */
7549 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
7551 /* If all else fails, use the (black and white) xbm image. */
7552 if (rc == -1)
7554 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7555 gnu_xbm_width, gnu_xbm_height);
7556 if (rc == -1)
7557 return 1;
7559 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7560 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7564 /* The first time we create the GNU bitmap and mask,
7565 this increments the ref-count one extra time.
7566 As a result, the GNU bitmap and mask are never freed.
7567 That way, we don't have to worry about allocating it again. */
7568 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7570 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7573 x_wm_set_icon_pixmap (f, bitmap_id);
7574 f->output_data.x->icon_bitmap = bitmap_id;
7576 return 0;
7580 /* Make the x-window of frame F use a rectangle with text.
7581 Use ICON_NAME as the text. */
7584 x_text_icon (f, icon_name)
7585 struct frame *f;
7586 char *icon_name;
7588 if (FRAME_X_WINDOW (f) == 0)
7589 return 1;
7591 #ifdef HAVE_X11R4
7593 XTextProperty text;
7594 text.value = (unsigned char *) icon_name;
7595 text.encoding = XA_STRING;
7596 text.format = 8;
7597 text.nitems = strlen (icon_name);
7598 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7600 #else /* not HAVE_X11R4 */
7601 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
7602 #endif /* not HAVE_X11R4 */
7604 if (f->output_data.x->icon_bitmap > 0)
7605 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7606 f->output_data.x->icon_bitmap = 0;
7607 x_wm_set_icon_pixmap (f, 0);
7609 return 0;
7612 #define X_ERROR_MESSAGE_SIZE 200
7614 /* If non-nil, this should be a string.
7615 It means catch X errors and store the error message in this string.
7617 The reason we use a stack is that x_catch_error/x_uncatch_error can
7618 be called from a signal handler.
7621 struct x_error_message_stack {
7622 char string[X_ERROR_MESSAGE_SIZE];
7623 Display *dpy;
7624 struct x_error_message_stack *prev;
7626 static struct x_error_message_stack *x_error_message;
7628 /* An X error handler which stores the error message in
7629 *x_error_message. This is called from x_error_handler if
7630 x_catch_errors is in effect. */
7632 static void
7633 x_error_catcher (display, error)
7634 Display *display;
7635 XErrorEvent *error;
7637 XGetErrorText (display, error->error_code,
7638 x_error_message->string,
7639 X_ERROR_MESSAGE_SIZE);
7642 /* Begin trapping X errors for display DPY. Actually we trap X errors
7643 for all displays, but DPY should be the display you are actually
7644 operating on.
7646 After calling this function, X protocol errors no longer cause
7647 Emacs to exit; instead, they are recorded in the string
7648 stored in *x_error_message.
7650 Calling x_check_errors signals an Emacs error if an X error has
7651 occurred since the last call to x_catch_errors or x_check_errors.
7653 Calling x_uncatch_errors resumes the normal error handling. */
7655 void x_check_errors ();
7657 void
7658 x_catch_errors (dpy)
7659 Display *dpy;
7661 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7663 /* Make sure any errors from previous requests have been dealt with. */
7664 XSync (dpy, False);
7666 data->dpy = dpy;
7667 data->string[0] = 0;
7668 data->prev = x_error_message;
7669 x_error_message = data;
7672 /* Undo the last x_catch_errors call.
7673 DPY should be the display that was passed to x_catch_errors. */
7675 void
7676 x_uncatch_errors ()
7678 struct x_error_message_stack *tmp;
7680 BLOCK_INPUT;
7682 /* The display may have been closed before this function is called.
7683 Check if it is still open before calling XSync. */
7684 if (x_display_info_for_display (x_error_message->dpy) != 0)
7685 XSync (x_error_message->dpy, False);
7687 tmp = x_error_message;
7688 x_error_message = x_error_message->prev;
7689 xfree (tmp);
7690 UNBLOCK_INPUT;
7693 /* If any X protocol errors have arrived since the last call to
7694 x_catch_errors or x_check_errors, signal an Emacs error using
7695 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7697 void
7698 x_check_errors (dpy, format)
7699 Display *dpy;
7700 char *format;
7702 /* Make sure to catch any errors incurred so far. */
7703 XSync (dpy, False);
7705 if (x_error_message->string[0])
7707 char string[X_ERROR_MESSAGE_SIZE];
7708 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7709 x_uncatch_errors ();
7710 error (format, string);
7714 /* Nonzero if we had any X protocol errors
7715 since we did x_catch_errors on DPY. */
7718 x_had_errors_p (dpy)
7719 Display *dpy;
7721 /* Make sure to catch any errors incurred so far. */
7722 XSync (dpy, False);
7724 return x_error_message->string[0] != 0;
7727 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7729 void
7730 x_clear_errors (dpy)
7731 Display *dpy;
7733 x_error_message->string[0] = 0;
7736 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7737 * idea. --lorentey */
7738 /* Close off all unclosed x_catch_errors calls. */
7740 void
7741 x_fully_uncatch_errors ()
7743 while (x_error_message)
7744 x_uncatch_errors ();
7746 #endif
7748 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7751 x_catching_errors ()
7753 return x_error_message != 0;
7756 #if 0
7757 static unsigned int x_wire_count;
7758 x_trace_wire ()
7760 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7762 #endif /* ! 0 */
7765 /* Handle SIGPIPE, which can happen when the connection to a server
7766 simply goes away. SIGPIPE is handled by x_connection_signal.
7767 Don't need to do anything, because the write which caused the
7768 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7769 which will do the appropriate cleanup for us. */
7771 static SIGTYPE
7772 x_connection_signal (signalnum) /* If we don't have an argument, */
7773 int signalnum; /* some compilers complain in signal calls. */
7775 #ifdef USG
7776 /* USG systems forget handlers when they are used;
7777 must reestablish each time */
7778 signal (signalnum, x_connection_signal);
7779 #endif /* USG */
7783 /************************************************************************
7784 Handling X errors
7785 ************************************************************************/
7787 /* Error message passed to x_connection_closed. */
7789 static char *error_msg;
7791 /* Function installed as fatal_error_signal_hook in
7792 x_connection_closed. Print the X error message, and exit normally,
7793 instead of dumping core when XtCloseDisplay fails. */
7795 static void
7796 x_fatal_error_signal ()
7798 fprintf (stderr, "%s\n", error_msg);
7799 exit (70);
7802 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7803 the text of an error message that lead to the connection loss. */
7805 static SIGTYPE
7806 x_connection_closed (dpy, error_message)
7807 Display *dpy;
7808 char *error_message;
7810 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7811 Lisp_Object frame, tail;
7812 int index = SPECPDL_INDEX ();
7814 error_msg = (char *) alloca (strlen (error_message) + 1);
7815 strcpy (error_msg, error_message);
7816 handling_signal = 0;
7818 /* Prevent being called recursively because of an error condition
7819 below. Otherwise, we might end up with printing ``can't find per
7820 display information'' in the recursive call instead of printing
7821 the original message here. */
7822 x_catch_errors (dpy);
7824 /* Inhibit redisplay while frames are being deleted. */
7825 specbind (Qinhibit_redisplay, Qt);
7827 if (dpyinfo)
7829 /* Protect display from being closed when we delete the last
7830 frame on it. */
7831 dpyinfo->reference_count++;
7832 dpyinfo->terminal->reference_count++;
7835 /* First delete frames whose mini-buffers are on frames
7836 that are on the dead display. */
7837 FOR_EACH_FRAME (tail, frame)
7839 Lisp_Object minibuf_frame;
7840 minibuf_frame
7841 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7842 if (FRAME_X_P (XFRAME (frame))
7843 && FRAME_X_P (XFRAME (minibuf_frame))
7844 && ! EQ (frame, minibuf_frame)
7845 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7846 Fdelete_frame (frame, Qt);
7849 /* Now delete all remaining frames on the dead display.
7850 We are now sure none of these is used as the mini-buffer
7851 for another frame that we need to delete. */
7852 FOR_EACH_FRAME (tail, frame)
7853 if (FRAME_X_P (XFRAME (frame))
7854 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7856 /* Set this to t so that Fdelete_frame won't get confused
7857 trying to find a replacement. */
7858 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7859 Fdelete_frame (frame, Qt);
7862 /* We have to close the display to inform Xt that it doesn't
7863 exist anymore. If we don't, Xt will continue to wait for
7864 events from the display. As a consequence, a sequence of
7866 M-x make-frame-on-display RET :1 RET
7867 ...kill the new frame, so that we get an IO error...
7868 M-x make-frame-on-display RET :1 RET
7870 will indefinitely wait in Xt for events for display `:1', opened
7871 in the first class to make-frame-on-display.
7873 Closing the display is reported to lead to a bus error on
7874 OpenWindows in certain situations. I suspect that is a bug
7875 in OpenWindows. I don't know how to cicumvent it here. */
7877 #ifdef USE_X_TOOLKIT
7878 /* If DPYINFO is null, this means we didn't open the display
7879 in the first place, so don't try to close it. */
7880 if (dpyinfo)
7882 extern void (*fatal_error_signal_hook) P_ ((void));
7883 fatal_error_signal_hook = x_fatal_error_signal;
7884 XtCloseDisplay (dpy);
7885 fatal_error_signal_hook = NULL;
7887 #endif
7889 #ifdef USE_GTK
7890 if (dpyinfo)
7891 xg_display_close (dpyinfo->display);
7892 #endif
7894 if (dpyinfo)
7896 /* Indicate that this display is dead. */
7897 dpyinfo->display = 0;
7899 dpyinfo->reference_count--;
7900 dpyinfo->terminal->reference_count--;
7901 if (dpyinfo->reference_count != 0)
7902 /* We have just closed all frames on this display. */
7903 abort ();
7905 x_delete_display (dpyinfo);
7908 x_uncatch_errors ();
7910 if (terminal_list == 0)
7912 fprintf (stderr, "%s\n", error_msg);
7913 shut_down_emacs (0, 0, Qnil);
7914 exit (70);
7917 /* Ordinary stack unwind doesn't deal with these. */
7918 #ifdef SIGIO
7919 sigunblock (sigmask (SIGIO));
7920 #endif
7921 sigunblock (sigmask (SIGALRM));
7922 TOTALLY_UNBLOCK_INPUT;
7924 unbind_to (index, Qnil);
7925 clear_waiting_for_input ();
7926 error ("%s", error_msg);
7929 /* We specifically use it before defining it, so that gcc doesn't inline it,
7930 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7931 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7933 /* This is the first-level handler for X protocol errors.
7934 It calls x_error_quitter or x_error_catcher. */
7936 static int
7937 x_error_handler (display, error)
7938 Display *display;
7939 XErrorEvent *error;
7941 if (x_error_message)
7942 x_error_catcher (display, error);
7943 else
7944 x_error_quitter (display, error);
7945 return 0;
7948 /* This is the usual handler for X protocol errors.
7949 It kills all frames on the display that we got the error for.
7950 If that was the only one, it prints an error message and kills Emacs. */
7952 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7954 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
7955 #define NO_INLINE __attribute__((noinline))
7956 #else
7957 #define NO_INLINE
7958 #endif
7960 /* Some versions of GNU/Linux define noinline in their headers. */
7962 #ifdef noinline
7963 #undef noinline
7964 #endif
7966 /* On older GCC versions, just putting x_error_quitter
7967 after x_error_handler prevents inlining into the former. */
7969 static void NO_INLINE
7970 x_error_quitter (display, error)
7971 Display *display;
7972 XErrorEvent *error;
7974 char buf[256], buf1[356];
7976 /* Ignore BadName errors. They can happen because of fonts
7977 or colors that are not defined. */
7979 if (error->error_code == BadName)
7980 return;
7982 /* Note that there is no real way portable across R3/R4 to get the
7983 original error handler. */
7985 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7986 sprintf (buf1, "X protocol error: %s on protocol request %d",
7987 buf, error->request_code);
7988 x_connection_closed (display, buf1);
7992 /* This is the handler for X IO errors, always.
7993 It kills all frames on the display that we lost touch with.
7994 If that was the only one, it prints an error message and kills Emacs. */
7996 static int
7997 x_io_error_quitter (display)
7998 Display *display;
8000 char buf[256];
8002 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
8003 x_connection_closed (display, buf);
8004 return 0;
8007 /* Changing the font of the frame. */
8009 /* Give frame F the font named FONTNAME as its default font, and
8010 return the full name of that font. FONTNAME may be a wildcard
8011 pattern; in that case, we choose some font that fits the pattern.
8012 The return value shows which font we chose. */
8014 Lisp_Object
8015 x_new_font (f, fontname)
8016 struct frame *f;
8017 register char *fontname;
8019 struct font_info *fontp
8020 = FS_LOAD_FONT (f, 0, fontname, -1);
8022 if (!fontp)
8023 return Qnil;
8025 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
8026 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
8027 FRAME_FONTSET (f) = -1;
8029 FRAME_COLUMN_WIDTH (f) = fontp->average_width;
8030 FRAME_SPACE_WIDTH (f) = fontp->space_width;
8031 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
8033 compute_fringe_widths (f, 1);
8035 /* Compute the scroll bar width in character columns. */
8036 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8038 int wid = FRAME_COLUMN_WIDTH (f);
8039 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8040 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8042 else
8044 int wid = FRAME_COLUMN_WIDTH (f);
8045 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8048 /* Now make the frame display the given font. */
8049 if (FRAME_X_WINDOW (f) != 0)
8051 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
8052 FRAME_FONT (f)->fid);
8053 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
8054 FRAME_FONT (f)->fid);
8055 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
8056 FRAME_FONT (f)->fid);
8058 /* Don't change the size of a tip frame; there's no point in
8059 doing it because it's done in Fx_show_tip, and it leads to
8060 problems because the tip frame has no widget. */
8061 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8062 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8065 return build_string (fontp->full_name);
8068 /* Give frame F the fontset named FONTSETNAME as its default font, and
8069 return the full name of that fontset. FONTSETNAME may be a wildcard
8070 pattern; in that case, we choose some fontset that fits the pattern.
8071 The return value shows which fontset we chose. */
8073 Lisp_Object
8074 x_new_fontset (f, fontsetname)
8075 struct frame *f;
8076 char *fontsetname;
8078 int fontset = fs_query_fontset (build_string (fontsetname), 0);
8079 Lisp_Object result;
8081 if (fontset < 0)
8082 return Qnil;
8084 if (FRAME_FONTSET (f) == fontset)
8085 /* This fontset is already set in frame F. There's nothing more
8086 to do. */
8087 return fontset_name (fontset);
8089 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
8091 if (!STRINGP (result))
8092 /* Can't load ASCII font. */
8093 return Qnil;
8095 /* Since x_new_font doesn't update any fontset information, do it now. */
8096 FRAME_FONTSET (f) = fontset;
8098 #ifdef HAVE_X_I18N
8099 if (FRAME_XIC (f)
8100 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8101 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8102 #endif
8104 return build_string (fontsetname);
8108 /***********************************************************************
8109 X Input Methods
8110 ***********************************************************************/
8112 #ifdef HAVE_X_I18N
8114 #ifdef HAVE_X11R6
8116 /* XIM destroy callback function, which is called whenever the
8117 connection to input method XIM dies. CLIENT_DATA contains a
8118 pointer to the x_display_info structure corresponding to XIM. */
8120 static void
8121 xim_destroy_callback (xim, client_data, call_data)
8122 XIM xim;
8123 XPointer client_data;
8124 XPointer call_data;
8126 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8127 Lisp_Object frame, tail;
8129 BLOCK_INPUT;
8131 /* No need to call XDestroyIC.. */
8132 FOR_EACH_FRAME (tail, frame)
8134 struct frame *f = XFRAME (frame);
8135 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8137 FRAME_XIC (f) = NULL;
8138 xic_free_xfontset (f);
8142 /* No need to call XCloseIM. */
8143 dpyinfo->xim = NULL;
8144 XFree (dpyinfo->xim_styles);
8145 UNBLOCK_INPUT;
8148 #endif /* HAVE_X11R6 */
8150 #ifdef HAVE_X11R6
8151 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8152 extern char *XSetIMValues P_ ((XIM, ...));
8153 #endif
8155 /* Open the connection to the XIM server on display DPYINFO.
8156 RESOURCE_NAME is the resource name Emacs uses. */
8158 static void
8159 xim_open_dpy (dpyinfo, resource_name)
8160 struct x_display_info *dpyinfo;
8161 char *resource_name;
8163 XIM xim;
8165 #ifdef HAVE_XIM
8166 if (use_xim)
8168 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8169 EMACS_CLASS);
8170 dpyinfo->xim = xim;
8172 if (xim)
8174 #ifdef HAVE_X11R6
8175 XIMCallback destroy;
8176 #endif
8178 /* Get supported styles and XIM values. */
8179 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8181 #ifdef HAVE_X11R6
8182 destroy.callback = xim_destroy_callback;
8183 destroy.client_data = (XPointer)dpyinfo;
8184 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8185 #endif
8189 else
8190 #endif /* HAVE_XIM */
8191 dpyinfo->xim = NULL;
8195 #ifdef HAVE_X11R6_XIM
8197 struct xim_inst_t
8199 struct x_display_info *dpyinfo;
8200 char *resource_name;
8203 /* XIM instantiate callback function, which is called whenever an XIM
8204 server is available. DISPLAY is the display of the XIM.
8205 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8206 when the callback was registered. */
8208 static void
8209 xim_instantiate_callback (display, client_data, call_data)
8210 Display *display;
8211 XPointer client_data;
8212 XPointer call_data;
8214 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8215 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8217 /* We don't support multiple XIM connections. */
8218 if (dpyinfo->xim)
8219 return;
8221 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8223 /* Create XIC for the existing frames on the same display, as long
8224 as they have no XIC. */
8225 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8227 Lisp_Object tail, frame;
8229 BLOCK_INPUT;
8230 FOR_EACH_FRAME (tail, frame)
8232 struct frame *f = XFRAME (frame);
8234 if (FRAME_X_P (f)
8235 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8236 if (FRAME_XIC (f) == NULL)
8238 create_frame_xic (f);
8239 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8240 xic_set_statusarea (f);
8241 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8243 struct window *w = XWINDOW (f->selected_window);
8244 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8249 UNBLOCK_INPUT;
8253 #endif /* HAVE_X11R6_XIM */
8256 /* Open a connection to the XIM server on display DPYINFO.
8257 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8258 connection only at the first time. On X11R6, open the connection
8259 in the XIM instantiate callback function. */
8261 static void
8262 xim_initialize (dpyinfo, resource_name)
8263 struct x_display_info *dpyinfo;
8264 char *resource_name;
8266 #ifdef HAVE_XIM
8267 if (use_xim)
8269 #ifdef HAVE_X11R6_XIM
8270 struct xim_inst_t *xim_inst;
8271 int len;
8273 dpyinfo->xim = NULL;
8274 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8275 xim_inst->dpyinfo = dpyinfo;
8276 len = strlen (resource_name);
8277 xim_inst->resource_name = (char *) xmalloc (len + 1);
8278 bcopy (resource_name, xim_inst->resource_name, len + 1);
8279 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8280 resource_name, EMACS_CLASS,
8281 xim_instantiate_callback,
8282 /* This is XPointer in XFree86
8283 but (XPointer *) on Tru64, at
8284 least, hence the configure test. */
8285 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8286 #else /* not HAVE_X11R6_XIM */
8287 dpyinfo->xim = NULL;
8288 xim_open_dpy (dpyinfo, resource_name);
8289 #endif /* not HAVE_X11R6_XIM */
8292 else
8293 #endif /* HAVE_XIM */
8294 dpyinfo->xim = NULL;
8298 /* Close the connection to the XIM server on display DPYINFO. */
8300 static void
8301 xim_close_dpy (dpyinfo)
8302 struct x_display_info *dpyinfo;
8304 #ifdef HAVE_XIM
8305 if (use_xim)
8307 #ifdef HAVE_X11R6_XIM
8308 if (dpyinfo->display)
8309 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8310 NULL, EMACS_CLASS,
8311 xim_instantiate_callback, NULL);
8312 #endif /* not HAVE_X11R6_XIM */
8313 if (dpyinfo->display)
8314 XCloseIM (dpyinfo->xim);
8315 dpyinfo->xim = NULL;
8316 XFree (dpyinfo->xim_styles);
8318 #endif /* HAVE_XIM */
8321 #endif /* not HAVE_X11R6_XIM */
8325 /* Calculate the absolute position in frame F
8326 from its current recorded position values and gravity. */
8328 void
8329 x_calc_absolute_position (f)
8330 struct frame *f;
8332 int flags = f->size_hint_flags;
8334 /* We have nothing to do if the current position
8335 is already for the top-left corner. */
8336 if (! ((flags & XNegative) || (flags & YNegative)))
8337 return;
8339 /* Treat negative positions as relative to the leftmost bottommost
8340 position that fits on the screen. */
8341 if (flags & XNegative)
8342 f->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8343 - FRAME_PIXEL_WIDTH (f) + f->left_pos);
8346 int height = FRAME_PIXEL_HEIGHT (f);
8348 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8349 /* Something is fishy here. When using Motif, starting Emacs with
8350 `-g -0-0', the frame appears too low by a few pixels.
8352 This seems to be so because initially, while Emacs is starting,
8353 the column widget's height and the frame's pixel height are
8354 different. The column widget's height is the right one. In
8355 later invocations, when Emacs is up, the frame's pixel height
8356 is right, though.
8358 It's not obvious where the initial small difference comes from.
8359 2000-12-01, gerd. */
8361 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8362 #endif
8364 if (flags & YNegative)
8365 f->top_pos = (FRAME_X_DISPLAY_INFO (f)->height - height + f->top_pos);
8368 /* The left_pos and top_pos
8369 are now relative to the top and left screen edges,
8370 so the flags should correspond. */
8371 f->size_hint_flags &= ~ (XNegative | YNegative);
8374 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8375 to really change the position, and 0 when calling from
8376 x_make_frame_visible (in that case, XOFF and YOFF are the current
8377 position values). It is -1 when calling from x_set_frame_parameters,
8378 which means, do adjust for borders but don't change the gravity. */
8380 void
8381 x_set_offset (f, xoff, yoff, change_gravity)
8382 struct frame *f;
8383 register int xoff, yoff;
8384 int change_gravity;
8386 int modified_top, modified_left;
8388 if (change_gravity != 0)
8390 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8391 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8393 f->top_pos = yoff;
8394 f->left_pos = xoff;
8395 f->size_hint_flags &= ~ (XNegative | YNegative);
8396 if (xoff < 0)
8397 f->size_hint_flags |= XNegative;
8398 if (yoff < 0)
8399 f->size_hint_flags |= YNegative;
8400 f->win_gravity = NorthWestGravity;
8402 x_calc_absolute_position (f);
8404 BLOCK_INPUT;
8405 x_wm_set_size_hint (f, (long) 0, 0);
8407 modified_left = f->left_pos;
8408 modified_top = f->top_pos;
8410 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8412 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8413 than the WM decorations. So we use the calculated offset instead
8414 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8415 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8416 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8419 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8420 modified_left, modified_top);
8422 x_sync_with_move (f, f->left_pos, f->top_pos,
8423 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8424 ? 1 : 0);
8426 /* change_gravity is non-zero when this function is called from Lisp to
8427 programmatically move a frame. In that case, we call
8428 x_check_expected_move to discover if we have a "Type A" or "Type B"
8429 window manager, and, for a "Type A" window manager, adjust the position
8430 of the frame.
8432 We call x_check_expected_move if a programmatic move occurred, and
8433 either the window manager type (A/B) is unknown or it is Type A but we
8434 need to compute the top/left offset adjustment for this frame. */
8436 if (change_gravity != 0 &&
8437 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8438 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8439 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8440 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8441 x_check_expected_move (f, modified_left, modified_top);
8443 UNBLOCK_INPUT;
8446 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8447 on the root window for frame F contains ATOMNAME.
8448 This is how a WM check shall be done according to the Window Manager
8449 Specification/Extended Window Manager Hints at
8450 http://freedesktop.org/wiki/Standards_2fwm_2dspec. */
8452 static int
8453 wm_supports (f, atomname)
8454 struct frame *f;
8455 const char *atomname;
8457 Atom actual_type;
8458 unsigned long actual_size, bytes_remaining;
8459 int i, rc, actual_format;
8460 Atom prop_atom;
8461 Window wmcheck_window;
8462 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8463 Window target_window = dpyinfo->root_window;
8464 long max_len = 65536;
8465 Display *dpy = FRAME_X_DISPLAY (f);
8466 unsigned char *tmp_data = NULL;
8467 Atom target_type = XA_WINDOW;
8468 Atom want_atom;
8470 BLOCK_INPUT;
8472 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8474 x_catch_errors (dpy);
8475 rc = XGetWindowProperty (dpy, target_window,
8476 prop_atom, 0, max_len, False, target_type,
8477 &actual_type, &actual_format, &actual_size,
8478 &bytes_remaining, &tmp_data);
8480 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8482 if (tmp_data) XFree (tmp_data);
8483 x_uncatch_errors ();
8484 UNBLOCK_INPUT;
8485 return 0;
8488 wmcheck_window = *(Window *) tmp_data;
8489 XFree (tmp_data);
8491 /* Check if window exists. */
8492 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8493 x_sync (f);
8494 if (x_had_errors_p (dpy))
8496 x_uncatch_errors ();
8497 UNBLOCK_INPUT;
8498 return 0;
8501 if (dpyinfo->net_supported_window != wmcheck_window)
8503 /* Window changed, reload atoms */
8504 if (dpyinfo->net_supported_atoms != NULL)
8505 XFree (dpyinfo->net_supported_atoms);
8506 dpyinfo->net_supported_atoms = NULL;
8507 dpyinfo->nr_net_supported_atoms = 0;
8508 dpyinfo->net_supported_window = 0;
8510 target_type = XA_ATOM;
8511 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8512 tmp_data = NULL;
8513 rc = XGetWindowProperty (dpy, target_window,
8514 prop_atom, 0, max_len, False, target_type,
8515 &actual_type, &actual_format, &actual_size,
8516 &bytes_remaining, &tmp_data);
8518 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8520 if (tmp_data) XFree (tmp_data);
8521 x_uncatch_errors ();
8522 UNBLOCK_INPUT;
8523 return 0;
8526 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8527 dpyinfo->nr_net_supported_atoms = actual_size;
8528 dpyinfo->net_supported_window = wmcheck_window;
8531 rc = 0;
8532 want_atom = XInternAtom (dpy, atomname, False);
8534 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8535 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8537 x_uncatch_errors ();
8538 UNBLOCK_INPUT;
8540 return rc;
8543 /* Do fullscreen as specified in extended window manager hints */
8545 static int
8546 do_ewmh_fullscreen (f)
8547 struct frame *f;
8549 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8551 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8552 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8553 if (!have_net_atom)
8554 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8556 if (have_net_atom)
8558 Lisp_Object frame;
8559 const char *atom = "_NET_WM_STATE";
8560 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8561 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8562 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8563 const char *what = NULL;
8565 XSETFRAME (frame, f);
8567 /* If there are _NET_ atoms we assume we have extended window manager
8568 hints. */
8569 switch (f->want_fullscreen)
8571 case FULLSCREEN_BOTH:
8572 what = fs;
8573 break;
8574 case FULLSCREEN_WIDTH:
8575 what = fw;
8576 break;
8577 case FULLSCREEN_HEIGHT:
8578 what = fh;
8579 break;
8582 if (what != NULL && !wm_supports (f, what)) return 0;
8585 Fx_send_client_event (frame, make_number (0), frame,
8586 make_unibyte_string (atom, strlen (atom)),
8587 make_number (32),
8588 Fcons (make_number (0), /* Remove */
8589 Fcons
8590 (make_unibyte_string (fs,
8591 strlen (fs)),
8592 Qnil)));
8593 Fx_send_client_event (frame, make_number (0), frame,
8594 make_unibyte_string (atom, strlen (atom)),
8595 make_number (32),
8596 Fcons (make_number (0), /* Remove */
8597 Fcons
8598 (make_unibyte_string (fh,
8599 strlen (fh)),
8600 Qnil)));
8601 Fx_send_client_event (frame, make_number (0), frame,
8602 make_unibyte_string (atom, strlen (atom)),
8603 make_number (32),
8604 Fcons (make_number (0), /* Remove */
8605 Fcons
8606 (make_unibyte_string (fw,
8607 strlen (fw)),
8608 Qnil)));
8609 f->want_fullscreen = FULLSCREEN_NONE;
8610 if (what != NULL)
8611 Fx_send_client_event (frame, make_number (0), frame,
8612 make_unibyte_string (atom, strlen (atom)),
8613 make_number (32),
8614 Fcons (make_number (1), /* Add */
8615 Fcons
8616 (make_unibyte_string (what,
8617 strlen (what)),
8618 Qnil)));
8621 return have_net_atom;
8624 static void
8625 XTfullscreen_hook (f)
8626 FRAME_PTR f;
8628 if (f->async_visible)
8630 BLOCK_INPUT;
8631 do_ewmh_fullscreen (f);
8632 x_sync (f);
8633 UNBLOCK_INPUT;
8638 /* Check if we need to resize the frame due to a fullscreen request.
8639 If so needed, resize the frame. */
8640 static void
8641 x_check_fullscreen (f)
8642 struct frame *f;
8644 if (f->want_fullscreen & FULLSCREEN_BOTH)
8646 int width, height, ign;
8648 if (do_ewmh_fullscreen (f))
8649 return;
8651 x_real_positions (f, &f->left_pos, &f->top_pos);
8653 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8655 /* We do not need to move the window, it shall be taken care of
8656 when setting WM manager hints.
8657 If the frame is visible already, the position is checked by
8658 x_check_expected_move. */
8659 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8661 change_frame_size (f, height, width, 0, 1, 0);
8662 SET_FRAME_GARBAGED (f);
8663 cancel_mouse_face (f);
8665 /* Wait for the change of frame size to occur */
8666 f->want_fullscreen |= FULLSCREEN_WAIT;
8671 /* This function is called by x_set_offset to determine whether the window
8672 manager interfered with the positioning of the frame. Type A window
8673 managers position the surrounding window manager decorations a small
8674 amount above and left of the user-supplied position. Type B window
8675 managers position the surrounding window manager decorations at the
8676 user-specified position. If we detect a Type A window manager, we
8677 compensate by moving the window right and down by the proper amount. */
8679 static void
8680 x_check_expected_move (f, expected_left, expected_top)
8681 struct frame *f;
8682 int expected_left;
8683 int expected_top;
8685 int current_left = 0, current_top = 0;
8687 /* x_real_positions returns the left and top offsets of the outermost
8688 window manager window around the frame. */
8690 x_real_positions (f, &current_left, &current_top);
8692 if (current_left != expected_left || current_top != expected_top)
8694 /* It's a "Type A" window manager. */
8696 int adjusted_left;
8697 int adjusted_top;
8699 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8700 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8701 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8703 /* Now fix the mispositioned frame's location. */
8705 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8706 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8708 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8709 adjusted_left, adjusted_top);
8711 x_sync_with_move (f, expected_left, expected_top, 0);
8713 else
8714 /* It's a "Type B" window manager. We don't have to adjust the
8715 frame's position. */
8717 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8721 /* Wait for XGetGeometry to return up-to-date position information for a
8722 recently-moved frame. Call this immediately after calling XMoveWindow.
8723 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8724 frame has been moved to, so we use a fuzzy position comparison instead
8725 of an exact comparison. */
8727 static void
8728 x_sync_with_move (f, left, top, fuzzy)
8729 struct frame *f;
8730 int left, top, fuzzy;
8732 int count = 0;
8734 while (count++ < 50)
8736 int current_left = 0, current_top = 0;
8738 /* In theory, this call to XSync only needs to happen once, but in
8739 practice, it doesn't seem to work, hence the need for the surrounding
8740 loop. */
8742 XSync (FRAME_X_DISPLAY (f), False);
8743 x_real_positions (f, &current_left, &current_top);
8745 if (fuzzy)
8747 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8748 pixels. */
8750 if (eabs (current_left - left) <= 10
8751 && eabs (current_top - top) <= 40)
8752 return;
8754 else if (current_left == left && current_top == top)
8755 return;
8758 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8759 will then return up-to-date position info. */
8761 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8765 /* Change the size of frame F's X window to COLS/ROWS in the case F
8766 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8767 top-left-corner window gravity for this size change and subsequent
8768 size changes. Otherwise we leave the window gravity unchanged. */
8770 static void
8771 x_set_window_size_1 (f, change_gravity, cols, rows)
8772 struct frame *f;
8773 int change_gravity;
8774 int cols, rows;
8776 int pixelwidth, pixelheight;
8778 check_frame_size (f, &rows, &cols);
8779 f->scroll_bar_actual_width
8780 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8782 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8783 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8784 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8786 compute_fringe_widths (f, 0);
8788 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8789 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8791 f->win_gravity = NorthWestGravity;
8792 x_wm_set_size_hint (f, (long) 0, 0);
8794 XSync (FRAME_X_DISPLAY (f), False);
8795 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8796 pixelwidth, pixelheight);
8798 /* Now, strictly speaking, we can't be sure that this is accurate,
8799 but the window manager will get around to dealing with the size
8800 change request eventually, and we'll hear how it went when the
8801 ConfigureNotify event gets here.
8803 We could just not bother storing any of this information here,
8804 and let the ConfigureNotify event set everything up, but that
8805 might be kind of confusing to the Lisp code, since size changes
8806 wouldn't be reported in the frame parameters until some random
8807 point in the future when the ConfigureNotify event arrives.
8809 We pass 1 for DELAY since we can't run Lisp code inside of
8810 a BLOCK_INPUT. */
8811 change_frame_size (f, rows, cols, 0, 1, 0);
8812 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8813 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8815 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8816 receive in the ConfigureNotify event; if we get what we asked
8817 for, then the event won't cause the screen to become garbaged, so
8818 we have to make sure to do it here. */
8819 SET_FRAME_GARBAGED (f);
8821 XFlush (FRAME_X_DISPLAY (f));
8825 /* Call this to change the size of frame F's x-window.
8826 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8827 for this size change and subsequent size changes.
8828 Otherwise we leave the window gravity unchanged. */
8830 void
8831 x_set_window_size (f, change_gravity, cols, rows)
8832 struct frame *f;
8833 int change_gravity;
8834 int cols, rows;
8836 BLOCK_INPUT;
8838 #ifdef USE_GTK
8839 if (FRAME_GTK_WIDGET (f))
8840 xg_frame_set_char_size (f, cols, rows);
8841 else
8842 x_set_window_size_1 (f, change_gravity, cols, rows);
8843 #elif USE_X_TOOLKIT
8845 if (f->output_data.x->widget != NULL)
8847 /* The x and y position of the widget is clobbered by the
8848 call to XtSetValues within EmacsFrameSetCharSize.
8849 This is a real kludge, but I don't understand Xt so I can't
8850 figure out a correct fix. Can anyone else tell me? -- rms. */
8851 int xpos = f->output_data.x->widget->core.x;
8852 int ypos = f->output_data.x->widget->core.y;
8853 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8854 f->output_data.x->widget->core.x = xpos;
8855 f->output_data.x->widget->core.y = ypos;
8857 else
8858 x_set_window_size_1 (f, change_gravity, cols, rows);
8860 #else /* not USE_X_TOOLKIT */
8862 x_set_window_size_1 (f, change_gravity, cols, rows);
8864 #endif /* not USE_X_TOOLKIT */
8866 /* If cursor was outside the new size, mark it as off. */
8867 mark_window_cursors_off (XWINDOW (f->root_window));
8869 /* Clear out any recollection of where the mouse highlighting was,
8870 since it might be in a place that's outside the new frame size.
8871 Actually checking whether it is outside is a pain in the neck,
8872 so don't try--just let the highlighting be done afresh with new size. */
8873 cancel_mouse_face (f);
8875 UNBLOCK_INPUT;
8878 /* Mouse warping. */
8880 void
8881 x_set_mouse_position (f, x, y)
8882 struct frame *f;
8883 int x, y;
8885 int pix_x, pix_y;
8887 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8888 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8890 if (pix_x < 0) pix_x = 0;
8891 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8893 if (pix_y < 0) pix_y = 0;
8894 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8896 BLOCK_INPUT;
8898 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8899 0, 0, 0, 0, pix_x, pix_y);
8900 UNBLOCK_INPUT;
8903 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8905 void
8906 x_set_mouse_pixel_position (f, pix_x, pix_y)
8907 struct frame *f;
8908 int pix_x, pix_y;
8910 BLOCK_INPUT;
8912 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8913 0, 0, 0, 0, pix_x, pix_y);
8914 UNBLOCK_INPUT;
8917 /* focus shifting, raising and lowering. */
8919 void
8920 x_focus_on_frame (f)
8921 struct frame *f;
8923 #if 0 /* This proves to be unpleasant. */
8924 x_raise_frame (f);
8925 #endif
8926 #if 0
8927 /* I don't think that the ICCCM allows programs to do things like this
8928 without the interaction of the window manager. Whatever you end up
8929 doing with this code, do it to x_unfocus_frame too. */
8930 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8931 RevertToPointerRoot, CurrentTime);
8932 #endif /* ! 0 */
8935 void
8936 x_unfocus_frame (f)
8937 struct frame *f;
8939 #if 0
8940 /* Look at the remarks in x_focus_on_frame. */
8941 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8942 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8943 RevertToPointerRoot, CurrentTime);
8944 #endif /* ! 0 */
8947 /* Raise frame F. */
8949 void
8950 x_raise_frame (f)
8951 struct frame *f;
8953 BLOCK_INPUT;
8954 if (f->async_visible)
8955 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8957 XFlush (FRAME_X_DISPLAY (f));
8958 UNBLOCK_INPUT;
8961 /* Lower frame F. */
8963 void
8964 x_lower_frame (f)
8965 struct frame *f;
8967 if (f->async_visible)
8969 BLOCK_INPUT;
8970 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8971 XFlush (FRAME_X_DISPLAY (f));
8972 UNBLOCK_INPUT;
8976 /* Activate frame with Extended Window Manager Hints */
8978 void
8979 x_ewmh_activate_frame (f)
8980 FRAME_PTR f;
8982 /* See Window Manager Specification/Extended Window Manager Hints at
8983 http://freedesktop.org/wiki/Standards_2fwm_2dspec */
8985 const char *atom = "_NET_ACTIVE_WINDOW";
8986 if (f->async_visible && wm_supports (f, atom))
8988 Lisp_Object frame;
8989 XSETFRAME (frame, f);
8990 Fx_send_client_event (frame, make_number (0), frame,
8991 make_unibyte_string (atom, strlen (atom)),
8992 make_number (32),
8993 Fcons (make_number (1),
8994 Fcons (make_number (last_user_time),
8995 Qnil)));
8999 static void
9000 XTframe_raise_lower (f, raise_flag)
9001 FRAME_PTR f;
9002 int raise_flag;
9004 if (raise_flag)
9005 x_raise_frame (f);
9006 else
9007 x_lower_frame (f);
9010 /* Change of visibility. */
9012 /* This tries to wait until the frame is really visible.
9013 However, if the window manager asks the user where to position
9014 the frame, this will return before the user finishes doing that.
9015 The frame will not actually be visible at that time,
9016 but it will become visible later when the window manager
9017 finishes with it. */
9019 void
9020 x_make_frame_visible (f)
9021 struct frame *f;
9023 Lisp_Object type;
9024 int original_top, original_left;
9025 int retry_count = 2;
9027 retry:
9029 BLOCK_INPUT;
9031 type = x_icon_type (f);
9032 if (!NILP (type))
9033 x_bitmap_icon (f, type);
9035 if (! FRAME_VISIBLE_P (f))
9037 /* We test FRAME_GARBAGED_P here to make sure we don't
9038 call x_set_offset a second time
9039 if we get to x_make_frame_visible a second time
9040 before the window gets really visible. */
9041 if (! FRAME_ICONIFIED_P (f)
9042 && ! f->output_data.x->asked_for_visible)
9043 x_set_offset (f, f->left_pos, f->top_pos, 0);
9045 f->output_data.x->asked_for_visible = 1;
9047 if (! EQ (Vx_no_window_manager, Qt))
9048 x_wm_set_window_state (f, NormalState);
9049 #ifdef USE_X_TOOLKIT
9050 /* This was XtPopup, but that did nothing for an iconified frame. */
9051 XtMapWidget (f->output_data.x->widget);
9052 #else /* not USE_X_TOOLKIT */
9053 #ifdef USE_GTK
9054 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9055 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9056 #else
9057 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9058 #endif /* not USE_GTK */
9059 #endif /* not USE_X_TOOLKIT */
9060 #if 0 /* This seems to bring back scroll bars in the wrong places
9061 if the window configuration has changed. They seem
9062 to come back ok without this. */
9063 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9064 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9065 #endif
9068 XFlush (FRAME_X_DISPLAY (f));
9070 /* Synchronize to ensure Emacs knows the frame is visible
9071 before we do anything else. We do this loop with input not blocked
9072 so that incoming events are handled. */
9074 Lisp_Object frame;
9075 int count;
9076 /* This must be before UNBLOCK_INPUT
9077 since events that arrive in response to the actions above
9078 will set it when they are handled. */
9079 int previously_visible = f->output_data.x->has_been_visible;
9081 original_left = f->left_pos;
9082 original_top = f->top_pos;
9084 /* This must come after we set COUNT. */
9085 UNBLOCK_INPUT;
9087 /* We unblock here so that arriving X events are processed. */
9089 /* Now move the window back to where it was "supposed to be".
9090 But don't do it if the gravity is negative.
9091 When the gravity is negative, this uses a position
9092 that is 3 pixels too low. Perhaps that's really the border width.
9094 Don't do this if the window has never been visible before,
9095 because the window manager may choose the position
9096 and we don't want to override it. */
9098 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
9099 && f->win_gravity == NorthWestGravity
9100 && previously_visible)
9102 Drawable rootw;
9103 int x, y;
9104 unsigned int width, height, border, depth;
9106 BLOCK_INPUT;
9108 /* On some window managers (such as FVWM) moving an existing
9109 window, even to the same place, causes the window manager
9110 to introduce an offset. This can cause the window to move
9111 to an unexpected location. Check the geometry (a little
9112 slow here) and then verify that the window is in the right
9113 place. If the window is not in the right place, move it
9114 there, and take the potential window manager hit. */
9115 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9116 &rootw, &x, &y, &width, &height, &border, &depth);
9118 if (original_left != x || original_top != y)
9119 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9120 original_left, original_top);
9122 UNBLOCK_INPUT;
9125 XSETFRAME (frame, f);
9127 /* Wait until the frame is visible. Process X events until a
9128 MapNotify event has been seen, or until we think we won't get a
9129 MapNotify at all.. */
9130 for (count = input_signal_count + 10;
9131 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9133 /* Force processing of queued events. */
9134 x_sync (f);
9136 /* Machines that do polling rather than SIGIO have been
9137 observed to go into a busy-wait here. So we'll fake an
9138 alarm signal to let the handler know that there's something
9139 to be read. We used to raise a real alarm, but it seems
9140 that the handler isn't always enabled here. This is
9141 probably a bug. */
9142 if (input_polling_used ())
9144 /* It could be confusing if a real alarm arrives while
9145 processing the fake one. Turn it off and let the
9146 handler reset it. */
9147 extern void poll_for_input_1 P_ ((void));
9148 int old_poll_suppress_count = poll_suppress_count;
9149 poll_suppress_count = 1;
9150 poll_for_input_1 ();
9151 poll_suppress_count = old_poll_suppress_count;
9154 /* See if a MapNotify event has been processed. */
9155 FRAME_SAMPLE_VISIBILITY (f);
9158 /* 2000-09-28: In
9160 (let ((f (selected-frame)))
9161 (iconify-frame f)
9162 (raise-frame f))
9164 the frame is not raised with various window managers on
9165 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9166 unknown reason, the call to XtMapWidget is completely ignored.
9167 Mapping the widget a second time works. */
9169 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9170 goto retry;
9174 /* Change from mapped state to withdrawn state. */
9176 /* Make the frame visible (mapped and not iconified). */
9178 void
9179 x_make_frame_invisible (f)
9180 struct frame *f;
9182 Window window;
9184 /* Use the frame's outermost window, not the one we normally draw on. */
9185 window = FRAME_OUTER_WINDOW (f);
9187 /* Don't keep the highlight on an invisible frame. */
9188 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9189 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9191 #if 0/* This might add unreliability; I don't trust it -- rms. */
9192 if (! f->async_visible && ! f->async_iconified)
9193 return;
9194 #endif
9196 BLOCK_INPUT;
9198 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9199 that the current position of the window is user-specified, rather than
9200 program-specified, so that when the window is mapped again, it will be
9201 placed at the same location, without forcing the user to position it
9202 by hand again (they have already done that once for this window.) */
9203 x_wm_set_size_hint (f, (long) 0, 1);
9205 #ifdef USE_GTK
9206 if (FRAME_GTK_OUTER_WIDGET (f))
9207 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9208 else
9209 #endif
9211 #ifdef HAVE_X11R4
9213 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9214 DefaultScreen (FRAME_X_DISPLAY (f))))
9216 UNBLOCK_INPUT_RESIGNAL;
9217 error ("Can't notify window manager of window withdrawal");
9219 #else /* ! defined (HAVE_X11R4) */
9221 /* Tell the window manager what we're going to do. */
9222 if (! EQ (Vx_no_window_manager, Qt))
9224 XEvent unmap;
9226 unmap.xunmap.type = UnmapNotify;
9227 unmap.xunmap.window = window;
9228 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
9229 unmap.xunmap.from_configure = False;
9230 if (! XSendEvent (FRAME_X_DISPLAY (f),
9231 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9232 False,
9233 SubstructureRedirectMaskSubstructureNotifyMask,
9234 &unmap))
9236 UNBLOCK_INPUT_RESIGNAL;
9237 error ("Can't notify window manager of withdrawal");
9241 /* Unmap the window ourselves. Cheeky! */
9242 XUnmapWindow (FRAME_X_DISPLAY (f), window);
9243 #endif /* ! defined (HAVE_X11R4) */
9246 /* We can't distinguish this from iconification
9247 just by the event that we get from the server.
9248 So we can't win using the usual strategy of letting
9249 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9250 and synchronize with the server to make sure we agree. */
9251 f->visible = 0;
9252 FRAME_ICONIFIED_P (f) = 0;
9253 f->async_visible = 0;
9254 f->async_iconified = 0;
9256 x_sync (f);
9258 UNBLOCK_INPUT;
9261 /* Change window state from mapped to iconified. */
9263 void
9264 x_iconify_frame (f)
9265 struct frame *f;
9267 int result;
9268 Lisp_Object type;
9270 /* Don't keep the highlight on an invisible frame. */
9271 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9272 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9274 if (f->async_iconified)
9275 return;
9277 BLOCK_INPUT;
9279 FRAME_SAMPLE_VISIBILITY (f);
9281 type = x_icon_type (f);
9282 if (!NILP (type))
9283 x_bitmap_icon (f, type);
9285 #ifdef USE_GTK
9286 if (FRAME_GTK_OUTER_WIDGET (f))
9288 if (! FRAME_VISIBLE_P (f))
9289 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9291 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9292 f->iconified = 1;
9293 f->visible = 1;
9294 f->async_iconified = 1;
9295 f->async_visible = 0;
9296 UNBLOCK_INPUT;
9297 return;
9299 #endif
9301 #ifdef USE_X_TOOLKIT
9303 if (! FRAME_VISIBLE_P (f))
9305 if (! EQ (Vx_no_window_manager, Qt))
9306 x_wm_set_window_state (f, IconicState);
9307 /* This was XtPopup, but that did nothing for an iconified frame. */
9308 XtMapWidget (f->output_data.x->widget);
9309 /* The server won't give us any event to indicate
9310 that an invisible frame was changed to an icon,
9311 so we have to record it here. */
9312 f->iconified = 1;
9313 f->visible = 1;
9314 f->async_iconified = 1;
9315 f->async_visible = 0;
9316 UNBLOCK_INPUT;
9317 return;
9320 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9321 XtWindow (f->output_data.x->widget),
9322 DefaultScreen (FRAME_X_DISPLAY (f)));
9323 UNBLOCK_INPUT;
9325 if (!result)
9326 error ("Can't notify window manager of iconification");
9328 f->async_iconified = 1;
9329 f->async_visible = 0;
9332 BLOCK_INPUT;
9333 XFlush (FRAME_X_DISPLAY (f));
9334 UNBLOCK_INPUT;
9335 #else /* not USE_X_TOOLKIT */
9337 /* Make sure the X server knows where the window should be positioned,
9338 in case the user deiconifies with the window manager. */
9339 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
9340 x_set_offset (f, f->left_pos, f->top_pos, 0);
9342 /* Since we don't know which revision of X we're running, we'll use both
9343 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9345 /* X11R4: send a ClientMessage to the window manager using the
9346 WM_CHANGE_STATE type. */
9348 XEvent message;
9350 message.xclient.window = FRAME_X_WINDOW (f);
9351 message.xclient.type = ClientMessage;
9352 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9353 message.xclient.format = 32;
9354 message.xclient.data.l[0] = IconicState;
9356 if (! XSendEvent (FRAME_X_DISPLAY (f),
9357 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9358 False,
9359 SubstructureRedirectMask | SubstructureNotifyMask,
9360 &message))
9362 UNBLOCK_INPUT_RESIGNAL;
9363 error ("Can't notify window manager of iconification");
9367 /* X11R3: set the initial_state field of the window manager hints to
9368 IconicState. */
9369 x_wm_set_window_state (f, IconicState);
9371 if (!FRAME_VISIBLE_P (f))
9373 /* If the frame was withdrawn, before, we must map it. */
9374 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9377 f->async_iconified = 1;
9378 f->async_visible = 0;
9380 XFlush (FRAME_X_DISPLAY (f));
9381 UNBLOCK_INPUT;
9382 #endif /* not USE_X_TOOLKIT */
9386 /* Free X resources of frame F. */
9388 void
9389 x_free_frame_resources (f)
9390 struct frame *f;
9392 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9393 Lisp_Object bar;
9394 struct scroll_bar *b;
9396 BLOCK_INPUT;
9398 /* If a display connection is dead, don't try sending more
9399 commands to the X server. */
9400 if (dpyinfo->display)
9402 if (f->output_data.x->icon_desc)
9403 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9405 #ifdef USE_X_TOOLKIT
9406 /* Explicitly destroy the scroll bars of the frame. Without
9407 this, we get "BadDrawable" errors from the toolkit later on,
9408 presumably from expose events generated for the disappearing
9409 toolkit scroll bars. */
9410 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9412 b = XSCROLL_BAR (bar);
9413 x_scroll_bar_remove (b);
9415 #endif
9417 #ifdef HAVE_X_I18N
9418 if (FRAME_XIC (f))
9419 free_frame_xic (f);
9420 #endif
9422 #ifdef USE_X_TOOLKIT
9423 if (f->output_data.x->widget)
9425 XtDestroyWidget (f->output_data.x->widget);
9426 f->output_data.x->widget = NULL;
9428 /* Tooltips don't have widgets, only a simple X window, even if
9429 we are using a toolkit. */
9430 else if (FRAME_X_WINDOW (f))
9431 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9433 free_frame_menubar (f);
9434 #else /* !USE_X_TOOLKIT */
9436 #ifdef USE_GTK
9437 /* In the GTK version, tooltips are normal X
9438 frames. We must check and free both types. */
9439 if (FRAME_GTK_OUTER_WIDGET (f))
9441 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9442 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9443 FRAME_GTK_OUTER_WIDGET (f) = 0;
9445 #endif /* USE_GTK */
9447 if (FRAME_X_WINDOW (f))
9448 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9449 #endif /* !USE_X_TOOLKIT */
9451 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9452 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9453 unload_color (f, f->output_data.x->cursor_pixel);
9454 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9455 unload_color (f, f->output_data.x->border_pixel);
9456 unload_color (f, f->output_data.x->mouse_pixel);
9458 if (f->output_data.x->scroll_bar_background_pixel != -1)
9459 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9460 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9461 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9462 #ifdef USE_TOOLKIT_SCROLL_BARS
9463 /* Scrollbar shadow colors. */
9464 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9465 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9466 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9467 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9468 #endif /* USE_TOOLKIT_SCROLL_BARS */
9469 if (f->output_data.x->white_relief.allocated_p)
9470 unload_color (f, f->output_data.x->white_relief.pixel);
9471 if (f->output_data.x->black_relief.allocated_p)
9472 unload_color (f, f->output_data.x->black_relief.pixel);
9474 if (FRAME_FACE_CACHE (f))
9475 free_frame_faces (f);
9477 x_free_gcs (f);
9478 XFlush (FRAME_X_DISPLAY (f));
9481 if (f->output_data.x->saved_menu_event)
9482 xfree (f->output_data.x->saved_menu_event);
9484 xfree (f->output_data.x);
9485 f->output_data.x = NULL;
9487 if (f == dpyinfo->x_focus_frame)
9488 dpyinfo->x_focus_frame = 0;
9489 if (f == dpyinfo->x_focus_event_frame)
9490 dpyinfo->x_focus_event_frame = 0;
9491 if (f == dpyinfo->x_highlight_frame)
9492 dpyinfo->x_highlight_frame = 0;
9494 if (f == dpyinfo->mouse_face_mouse_frame)
9496 dpyinfo->mouse_face_beg_row
9497 = dpyinfo->mouse_face_beg_col = -1;
9498 dpyinfo->mouse_face_end_row
9499 = dpyinfo->mouse_face_end_col = -1;
9500 dpyinfo->mouse_face_window = Qnil;
9501 dpyinfo->mouse_face_deferred_gc = 0;
9502 dpyinfo->mouse_face_mouse_frame = 0;
9505 UNBLOCK_INPUT;
9509 /* Destroy the X window of frame F. */
9511 void
9512 x_destroy_window (f)
9513 struct frame *f;
9515 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9517 /* If a display connection is dead, don't try sending more
9518 commands to the X server. */
9519 if (dpyinfo->display != 0)
9520 x_free_frame_resources (f);
9522 dpyinfo->reference_count--;
9526 /* Setting window manager hints. */
9528 /* Set the normal size hints for the window manager, for frame F.
9529 FLAGS is the flags word to use--or 0 meaning preserve the flags
9530 that the window now has.
9531 If USER_POSITION is nonzero, we set the USPosition
9532 flag (this is useful when FLAGS is 0).
9533 The GTK version is in gtkutils.c */
9535 #ifndef USE_GTK
9536 void
9537 x_wm_set_size_hint (f, flags, user_position)
9538 struct frame *f;
9539 long flags;
9540 int user_position;
9542 XSizeHints size_hints;
9544 #ifdef USE_X_TOOLKIT
9545 Arg al[2];
9546 int ac = 0;
9547 Dimension widget_width, widget_height;
9548 #endif
9550 Window window = FRAME_OUTER_WINDOW (f);
9552 /* Setting PMaxSize caused various problems. */
9553 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9555 size_hints.x = f->left_pos;
9556 size_hints.y = f->top_pos;
9558 #ifdef USE_X_TOOLKIT
9559 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9560 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9561 XtGetValues (f->output_data.x->widget, al, ac);
9562 size_hints.height = widget_height;
9563 size_hints.width = widget_width;
9564 #else /* not USE_X_TOOLKIT */
9565 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9566 size_hints.width = FRAME_PIXEL_WIDTH (f);
9567 #endif /* not USE_X_TOOLKIT */
9569 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9570 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9571 size_hints.max_width
9572 = FRAME_X_DISPLAY_INFO (f)->width - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9573 size_hints.max_height
9574 = FRAME_X_DISPLAY_INFO (f)->height - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9576 /* Calculate the base and minimum sizes.
9578 (When we use the X toolkit, we don't do it here.
9579 Instead we copy the values that the widgets are using, below.) */
9580 #ifndef USE_X_TOOLKIT
9582 int base_width, base_height;
9583 int min_rows = 0, min_cols = 0;
9585 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9586 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9588 check_frame_size (f, &min_rows, &min_cols);
9590 /* The window manager uses the base width hints to calculate the
9591 current number of rows and columns in the frame while
9592 resizing; min_width and min_height aren't useful for this
9593 purpose, since they might not give the dimensions for a
9594 zero-row, zero-column frame.
9596 We use the base_width and base_height members if we have
9597 them; otherwise, we set the min_width and min_height members
9598 to the size for a zero x zero frame. */
9600 #ifdef HAVE_X11R4
9601 size_hints.flags |= PBaseSize;
9602 size_hints.base_width = base_width;
9603 size_hints.base_height = base_height;
9604 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9605 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9606 #else
9607 size_hints.min_width = base_width;
9608 size_hints.min_height = base_height;
9609 #endif
9612 /* If we don't need the old flags, we don't need the old hint at all. */
9613 if (flags)
9615 size_hints.flags |= flags;
9616 goto no_read;
9618 #endif /* not USE_X_TOOLKIT */
9621 XSizeHints hints; /* Sometimes I hate X Windows... */
9622 long supplied_return;
9623 int value;
9625 #ifdef HAVE_X11R4
9626 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9627 &supplied_return);
9628 #else
9629 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
9630 #endif
9632 #ifdef USE_X_TOOLKIT
9633 size_hints.base_height = hints.base_height;
9634 size_hints.base_width = hints.base_width;
9635 size_hints.min_height = hints.min_height;
9636 size_hints.min_width = hints.min_width;
9637 #endif
9639 if (flags)
9640 size_hints.flags |= flags;
9641 else
9643 if (value == 0)
9644 hints.flags = 0;
9645 if (hints.flags & PSize)
9646 size_hints.flags |= PSize;
9647 if (hints.flags & PPosition)
9648 size_hints.flags |= PPosition;
9649 if (hints.flags & USPosition)
9650 size_hints.flags |= USPosition;
9651 if (hints.flags & USSize)
9652 size_hints.flags |= USSize;
9656 #ifndef USE_X_TOOLKIT
9657 no_read:
9658 #endif
9660 #ifdef PWinGravity
9661 size_hints.win_gravity = f->win_gravity;
9662 size_hints.flags |= PWinGravity;
9664 if (user_position)
9666 size_hints.flags &= ~ PPosition;
9667 size_hints.flags |= USPosition;
9669 #endif /* PWinGravity */
9671 #ifdef HAVE_X11R4
9672 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9673 #else
9674 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9675 #endif
9677 #endif /* not USE_GTK */
9679 /* Used for IconicState or NormalState */
9681 void
9682 x_wm_set_window_state (f, state)
9683 struct frame *f;
9684 int state;
9686 #ifdef USE_X_TOOLKIT
9687 Arg al[1];
9689 XtSetArg (al[0], XtNinitialState, state);
9690 XtSetValues (f->output_data.x->widget, al, 1);
9691 #else /* not USE_X_TOOLKIT */
9692 Window window = FRAME_X_WINDOW (f);
9694 f->output_data.x->wm_hints.flags |= StateHint;
9695 f->output_data.x->wm_hints.initial_state = state;
9697 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9698 #endif /* not USE_X_TOOLKIT */
9701 void
9702 x_wm_set_icon_pixmap (f, pixmap_id)
9703 struct frame *f;
9704 int pixmap_id;
9706 Pixmap icon_pixmap, icon_mask;
9708 #ifndef USE_X_TOOLKIT
9709 Window window = FRAME_OUTER_WINDOW (f);
9710 #endif
9712 if (pixmap_id > 0)
9714 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9715 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9716 icon_mask = x_bitmap_mask (f, pixmap_id);
9717 f->output_data.x->wm_hints.icon_mask = icon_mask;
9719 else
9721 /* It seems there is no way to turn off use of an icon pixmap.
9722 The following line does it, only if no icon has yet been created,
9723 for some window managers. But with mwm it crashes.
9724 Some people say it should clear the IconPixmapHint bit in this case,
9725 but that doesn't work, and the X consortium said it isn't the
9726 right thing at all. Since there is no way to win,
9727 best to explicitly give up. */
9728 #if 0
9729 f->output_data.x->wm_hints.icon_pixmap = None;
9730 f->output_data.x->wm_hints.icon_mask = None;
9731 #else
9732 return;
9733 #endif
9737 #ifdef USE_GTK
9739 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9740 return;
9743 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9746 Arg al[1];
9747 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9748 XtSetValues (f->output_data.x->widget, al, 1);
9749 XtSetArg (al[0], XtNiconMask, icon_mask);
9750 XtSetValues (f->output_data.x->widget, al, 1);
9753 #else /* not USE_X_TOOLKIT && not USE_GTK */
9755 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9756 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9758 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9761 void
9762 x_wm_set_icon_position (f, icon_x, icon_y)
9763 struct frame *f;
9764 int icon_x, icon_y;
9766 Window window = FRAME_OUTER_WINDOW (f);
9768 f->output_data.x->wm_hints.flags |= IconPositionHint;
9769 f->output_data.x->wm_hints.icon_x = icon_x;
9770 f->output_data.x->wm_hints.icon_y = icon_y;
9772 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9776 /***********************************************************************
9777 Fonts
9778 ***********************************************************************/
9780 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
9782 struct font_info *
9783 x_get_font_info (f, font_idx)
9784 FRAME_PTR f;
9785 int font_idx;
9787 return (FRAME_X_FONT_TABLE (f) + font_idx);
9791 /* Return a list of names of available fonts matching PATTERN on frame F.
9793 If SIZE is > 0, it is the size (maximum bounds width) of fonts
9794 to be listed.
9796 SIZE < 0 means include scalable fonts.
9798 Frame F null means we have not yet created any frame on X, and
9799 consult the first display in x_display_list. MAXNAMES sets a limit
9800 on how many fonts to match. */
9802 Lisp_Object
9803 x_list_fonts (f, pattern, size, maxnames)
9804 struct frame *f;
9805 Lisp_Object pattern;
9806 int size;
9807 int maxnames;
9809 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
9810 Lisp_Object tem, second_best;
9811 struct x_display_info *dpyinfo
9812 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
9813 Display *dpy = dpyinfo->display;
9814 int try_XLoadQueryFont = 0;
9815 int allow_auto_scaled_font = 0;
9817 if (size < 0)
9819 allow_auto_scaled_font = 1;
9820 size = 0;
9823 patterns = Fassoc (pattern, Valternate_fontname_alist);
9824 if (NILP (patterns))
9825 patterns = Fcons (pattern, Qnil);
9827 if (maxnames == 1 && !size)
9828 /* We can return any single font matching PATTERN. */
9829 try_XLoadQueryFont = 1;
9831 for (; CONSP (patterns); patterns = XCDR (patterns))
9833 int num_fonts;
9834 char **names = NULL;
9836 pattern = XCAR (patterns);
9837 /* See if we cached the result for this particular query.
9838 The cache is an alist of the form:
9839 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
9840 tem = XCDR (dpyinfo->name_list_element);
9841 key = Fcons (Fcons (pattern, make_number (maxnames)),
9842 allow_auto_scaled_font ? Qt : Qnil);
9843 list = Fassoc (key, tem);
9844 if (!NILP (list))
9846 list = Fcdr_safe (list);
9847 /* We have a cashed list. Don't have to get the list again. */
9848 goto label_cached;
9851 /* At first, put PATTERN in the cache. */
9853 BLOCK_INPUT;
9854 x_catch_errors (dpy);
9856 if (try_XLoadQueryFont)
9858 XFontStruct *font;
9859 unsigned long value;
9861 font = XLoadQueryFont (dpy, SDATA (pattern));
9862 if (x_had_errors_p (dpy))
9864 /* This error is perhaps due to insufficient memory on X
9865 server. Let's just ignore it. */
9866 font = NULL;
9867 x_clear_errors (dpy);
9870 if (font
9871 && XGetFontProperty (font, XA_FONT, &value))
9873 char *name = (char *) XGetAtomName (dpy, (Atom) value);
9874 int len = strlen (name);
9875 char *tmp;
9877 /* If DXPC (a Differential X Protocol Compressor)
9878 Ver.3.7 is running, XGetAtomName will return null
9879 string. We must avoid such a name. */
9880 if (len == 0)
9881 try_XLoadQueryFont = 0;
9882 else
9884 num_fonts = 1;
9885 names = (char **) alloca (sizeof (char *));
9886 /* Some systems only allow alloca assigned to a
9887 simple var. */
9888 tmp = (char *) alloca (len + 1); names[0] = tmp;
9889 bcopy (name, names[0], len + 1);
9890 XFree (name);
9893 else
9894 try_XLoadQueryFont = 0;
9896 if (font)
9897 XFreeFont (dpy, font);
9900 if (!try_XLoadQueryFont)
9902 /* We try at least 10 fonts because XListFonts will return
9903 auto-scaled fonts at the head. */
9904 if (maxnames < 0)
9906 int limit;
9908 for (limit = 500;;)
9910 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
9911 if (num_fonts == limit)
9913 BLOCK_INPUT;
9914 XFreeFontNames (names);
9915 UNBLOCK_INPUT;
9916 limit *= 2;
9918 else
9919 break;
9922 else
9923 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
9924 &num_fonts);
9926 if (x_had_errors_p (dpy))
9928 /* This error is perhaps due to insufficient memory on X
9929 server. Let's just ignore it. */
9930 names = NULL;
9931 x_clear_errors (dpy);
9935 x_uncatch_errors ();
9936 UNBLOCK_INPUT;
9938 if (names)
9940 int i;
9942 /* Make a list of all the fonts we got back.
9943 Store that in the font cache for the display. */
9944 for (i = 0; i < num_fonts; i++)
9946 int width = 0;
9947 char *p = names[i];
9948 int average_width = -1, resx = 0, dashes = 0;
9950 /* Count the number of dashes in NAMES[I]. If there are
9951 14 dashes, the field value following 9th dash
9952 (RESOLUTION_X) is nonzero, and the field value
9953 following 12th dash (AVERAGE_WIDTH) is 0, this is a
9954 auto-scaled font which is usually too ugly to be used
9955 for editing. Let's ignore it. */
9956 while (*p)
9957 if (*p++ == '-')
9959 dashes++;
9960 if (dashes == 7) /* PIXEL_SIZE field */
9961 width = atoi (p);
9962 else if (dashes == 9)
9963 resx = atoi (p);
9964 else if (dashes == 12) /* AVERAGE_WIDTH field */
9965 average_width = atoi (p);
9968 if (allow_auto_scaled_font
9969 || dashes < 14 || average_width != 0 || resx == 0)
9971 tem = build_string (names[i]);
9972 if (NILP (Fassoc (tem, list)))
9974 if (STRINGP (Vx_pixel_size_width_font_regexp)
9975 && ((fast_c_string_match_ignore_case
9976 (Vx_pixel_size_width_font_regexp, names[i]))
9977 >= 0))
9978 /* We can set the value of PIXEL_SIZE to the
9979 width of this font. */
9980 list = Fcons (Fcons (tem, make_number (width)), list);
9981 else
9982 /* For the moment, width is not known. */
9983 list = Fcons (Fcons (tem, Qnil), list);
9988 if (!try_XLoadQueryFont)
9990 BLOCK_INPUT;
9991 XFreeFontNames (names);
9992 UNBLOCK_INPUT;
9996 /* Now store the result in the cache. */
9997 XSETCDR (dpyinfo->name_list_element,
9998 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
10000 label_cached:
10001 if (NILP (list)) continue; /* Try the remaining alternatives. */
10003 newlist = second_best = Qnil;
10004 /* Make a list of the fonts that have the right width. */
10005 for (; CONSP (list); list = XCDR (list))
10007 int found_size;
10009 tem = XCAR (list);
10011 if (!CONSP (tem) || NILP (XCAR (tem)))
10012 continue;
10013 if (!size)
10015 newlist = Fcons (XCAR (tem), newlist);
10016 continue;
10019 if (!INTEGERP (XCDR (tem)))
10021 /* Since we have not yet known the size of this font, we
10022 must try slow function call XLoadQueryFont. */
10023 XFontStruct *thisinfo;
10025 BLOCK_INPUT;
10026 x_catch_errors (dpy);
10027 thisinfo = XLoadQueryFont (dpy,
10028 SDATA (XCAR (tem)));
10029 if (x_had_errors_p (dpy))
10031 /* This error is perhaps due to insufficient memory on X
10032 server. Let's just ignore it. */
10033 thisinfo = NULL;
10034 x_clear_errors (dpy);
10036 x_uncatch_errors ();
10037 UNBLOCK_INPUT;
10039 if (thisinfo)
10041 XSETCDR (tem,
10042 (thisinfo->min_bounds.width == 0
10043 ? make_number (0)
10044 : make_number (thisinfo->max_bounds.width)));
10045 BLOCK_INPUT;
10046 XFreeFont (dpy, thisinfo);
10047 UNBLOCK_INPUT;
10049 else
10050 /* For unknown reason, the previous call of XListFont had
10051 returned a font which can't be opened. Record the size
10052 as 0 not to try to open it again. */
10053 XSETCDR (tem, make_number (0));
10056 found_size = XINT (XCDR (tem));
10057 if (found_size == size)
10058 newlist = Fcons (XCAR (tem), newlist);
10059 else if (found_size > 0)
10061 if (NILP (second_best))
10062 second_best = tem;
10063 else if (found_size < size)
10065 if (XINT (XCDR (second_best)) > size
10066 || XINT (XCDR (second_best)) < found_size)
10067 second_best = tem;
10069 else
10071 if (XINT (XCDR (second_best)) > size
10072 && XINT (XCDR (second_best)) > found_size)
10073 second_best = tem;
10077 if (!NILP (newlist))
10078 break;
10079 else if (!NILP (second_best))
10081 newlist = Fcons (XCAR (second_best), Qnil);
10082 break;
10086 return newlist;
10090 #if GLYPH_DEBUG
10092 /* Check that FONT is valid on frame F. It is if it can be found in F's
10093 font table. */
10095 static void
10096 x_check_font (f, font)
10097 struct frame *f;
10098 XFontStruct *font;
10100 int i;
10101 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10103 xassert (font != NULL);
10105 for (i = 0; i < dpyinfo->n_fonts; i++)
10106 if (dpyinfo->font_table[i].name
10107 && font == dpyinfo->font_table[i].font)
10108 break;
10110 xassert (i < dpyinfo->n_fonts);
10113 #endif /* GLYPH_DEBUG != 0 */
10115 /* Set *W to the minimum width, *H to the minimum font height of FONT.
10116 Note: There are (broken) X fonts out there with invalid XFontStruct
10117 min_bounds contents. For example, handa@etl.go.jp reports that
10118 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
10119 have font->min_bounds.width == 0. */
10121 static INLINE void
10122 x_font_min_bounds (font, w, h)
10123 XFontStruct *font;
10124 int *w, *h;
10126 *h = FONT_HEIGHT (font);
10127 *w = font->min_bounds.width;
10129 /* Try to handle the case where FONT->min_bounds has invalid
10130 contents. Since the only font known to have invalid min_bounds
10131 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
10132 if (*w <= 0)
10133 *w = font->max_bounds.width;
10137 /* Compute the smallest character width and smallest font height over
10138 all fonts available on frame F. Set the members smallest_char_width
10139 and smallest_font_height in F's x_display_info structure to
10140 the values computed. Value is non-zero if smallest_font_height or
10141 smallest_char_width become smaller than they were before. */
10143 static int
10144 x_compute_min_glyph_bounds (f)
10145 struct frame *f;
10147 int i;
10148 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10149 XFontStruct *font;
10150 int old_width = dpyinfo->smallest_char_width;
10151 int old_height = dpyinfo->smallest_font_height;
10153 dpyinfo->smallest_font_height = 100000;
10154 dpyinfo->smallest_char_width = 100000;
10156 for (i = 0; i < dpyinfo->n_fonts; ++i)
10157 if (dpyinfo->font_table[i].name)
10159 struct font_info *fontp = dpyinfo->font_table + i;
10160 int w, h;
10162 font = (XFontStruct *) fontp->font;
10163 xassert (font != (XFontStruct *) ~0);
10164 x_font_min_bounds (font, &w, &h);
10166 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
10167 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
10170 xassert (dpyinfo->smallest_char_width > 0
10171 && dpyinfo->smallest_font_height > 0);
10173 return (dpyinfo->n_fonts == 1
10174 || dpyinfo->smallest_char_width < old_width
10175 || dpyinfo->smallest_font_height < old_height);
10179 /* Load font named FONTNAME of the size SIZE for frame F, and return a
10180 pointer to the structure font_info while allocating it dynamically.
10181 If SIZE is 0, load any size of font.
10182 If loading is failed, return NULL. */
10184 struct font_info *
10185 x_load_font (f, fontname, size)
10186 struct frame *f;
10187 register char *fontname;
10188 int size;
10190 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10191 Lisp_Object font_names;
10193 /* Get a list of all the fonts that match this name. Once we
10194 have a list of matching fonts, we compare them against the fonts
10195 we already have by comparing names. */
10196 font_names = x_list_fonts (f, build_string (fontname), size, 1);
10198 if (!NILP (font_names))
10200 Lisp_Object tail;
10201 int i;
10203 for (i = 0; i < dpyinfo->n_fonts; i++)
10204 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
10205 if (dpyinfo->font_table[i].name
10206 && (!strcmp (dpyinfo->font_table[i].name,
10207 SDATA (XCAR (tail)))
10208 || !strcmp (dpyinfo->font_table[i].full_name,
10209 SDATA (XCAR (tail)))))
10210 return (dpyinfo->font_table + i);
10213 /* Load the font and add it to the table. */
10215 char *full_name;
10216 XFontStruct *font;
10217 struct font_info *fontp;
10218 unsigned long value;
10219 int i;
10221 /* If we have found fonts by x_list_font, load one of them. If
10222 not, we still try to load a font by the name given as FONTNAME
10223 because XListFonts (called in x_list_font) of some X server has
10224 a bug of not finding a font even if the font surely exists and
10225 is loadable by XLoadQueryFont. */
10226 if (size > 0 && !NILP (font_names))
10227 fontname = (char *) SDATA (XCAR (font_names));
10229 BLOCK_INPUT;
10230 x_catch_errors (FRAME_X_DISPLAY (f));
10231 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
10232 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10234 /* This error is perhaps due to insufficient memory on X
10235 server. Let's just ignore it. */
10236 font = NULL;
10237 x_clear_errors (FRAME_X_DISPLAY (f));
10239 x_uncatch_errors ();
10240 UNBLOCK_INPUT;
10241 if (!font)
10242 return NULL;
10244 /* Find a free slot in the font table. */
10245 for (i = 0; i < dpyinfo->n_fonts; ++i)
10246 if (dpyinfo->font_table[i].name == NULL)
10247 break;
10249 /* If no free slot found, maybe enlarge the font table. */
10250 if (i == dpyinfo->n_fonts
10251 && dpyinfo->n_fonts == dpyinfo->font_table_size)
10253 int sz;
10254 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
10255 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
10256 dpyinfo->font_table
10257 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
10260 fontp = dpyinfo->font_table + i;
10261 if (i == dpyinfo->n_fonts)
10262 ++dpyinfo->n_fonts;
10264 /* Now fill in the slots of *FONTP. */
10265 BLOCK_INPUT;
10266 bzero (fontp, sizeof (*fontp));
10267 fontp->font = font;
10268 fontp->font_idx = i;
10269 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
10270 bcopy (fontname, fontp->name, strlen (fontname) + 1);
10272 if (font->min_bounds.width == font->max_bounds.width)
10274 /* Fixed width font. */
10275 fontp->average_width = fontp->space_width = font->min_bounds.width;
10277 else
10279 XChar2b char2b;
10280 XCharStruct *pcm;
10282 char2b.byte1 = 0x00, char2b.byte2 = 0x20;
10283 pcm = x_per_char_metric (font, &char2b, 0);
10284 if (pcm)
10285 fontp->space_width = pcm->width;
10286 else
10287 fontp->space_width = FONT_WIDTH (font);
10289 fontp->average_width
10290 = (XGetFontProperty (font, dpyinfo->Xatom_AVERAGE_WIDTH, &value)
10291 ? (long) value / 10 : 0);
10292 if (fontp->average_width < 0)
10293 fontp->average_width = - fontp->average_width;
10294 if (fontp->average_width == 0)
10296 if (pcm)
10298 int width = pcm->width;
10299 for (char2b.byte2 = 33; char2b.byte2 <= 126; char2b.byte2++)
10300 if ((pcm = x_per_char_metric (font, &char2b, 0)) != NULL)
10301 width += pcm->width;
10302 fontp->average_width = width / 95;
10304 else
10305 fontp->average_width = FONT_WIDTH (font);
10309 /* Try to get the full name of FONT. Put it in FULL_NAME. */
10310 full_name = 0;
10311 if (XGetFontProperty (font, XA_FONT, &value))
10313 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
10314 char *p = name;
10315 int dashes = 0;
10317 /* Count the number of dashes in the "full name".
10318 If it is too few, this isn't really the font's full name,
10319 so don't use it.
10320 In X11R4, the fonts did not come with their canonical names
10321 stored in them. */
10322 while (*p)
10324 if (*p == '-')
10325 dashes++;
10326 p++;
10329 if (dashes >= 13)
10331 full_name = (char *) xmalloc (p - name + 1);
10332 bcopy (name, full_name, p - name + 1);
10335 XFree (name);
10338 if (full_name != 0)
10339 fontp->full_name = full_name;
10340 else
10341 fontp->full_name = fontp->name;
10343 fontp->size = font->max_bounds.width;
10344 fontp->height = FONT_HEIGHT (font);
10346 if (NILP (font_names))
10348 /* We come here because of a bug of XListFonts mentioned at
10349 the head of this block. Let's store this information in
10350 the cache for x_list_fonts. */
10351 Lisp_Object lispy_name = build_string (fontname);
10352 Lisp_Object lispy_full_name = build_string (fontp->full_name);
10353 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
10354 Qnil);
10356 XSETCDR (dpyinfo->name_list_element,
10357 Fcons (Fcons (key,
10358 Fcons (Fcons (lispy_full_name,
10359 make_number (fontp->size)),
10360 Qnil)),
10361 XCDR (dpyinfo->name_list_element)));
10362 if (full_name)
10364 key = Fcons (Fcons (lispy_full_name, make_number (256)),
10365 Qnil);
10366 XSETCDR (dpyinfo->name_list_element,
10367 Fcons (Fcons (key,
10368 Fcons (Fcons (lispy_full_name,
10369 make_number (fontp->size)),
10370 Qnil)),
10371 XCDR (dpyinfo->name_list_element)));
10375 /* The slot `encoding' specifies how to map a character
10376 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
10377 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
10378 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
10379 2:0xA020..0xFF7F). For the moment, we don't know which charset
10380 uses this font. So, we set information in fontp->encoding[1]
10381 which is never used by any charset. If mapping can't be
10382 decided, set FONT_ENCODING_NOT_DECIDED. */
10383 fontp->encoding[1]
10384 = (font->max_byte1 == 0
10385 /* 1-byte font */
10386 ? (font->min_char_or_byte2 < 0x80
10387 ? (font->max_char_or_byte2 < 0x80
10388 ? 0 /* 0x20..0x7F */
10389 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
10390 : 1) /* 0xA0..0xFF */
10391 /* 2-byte font */
10392 : (font->min_byte1 < 0x80
10393 ? (font->max_byte1 < 0x80
10394 ? (font->min_char_or_byte2 < 0x80
10395 ? (font->max_char_or_byte2 < 0x80
10396 ? 0 /* 0x2020..0x7F7F */
10397 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
10398 : 3) /* 0x20A0..0x7FFF */
10399 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
10400 : (font->min_char_or_byte2 < 0x80
10401 ? (font->max_char_or_byte2 < 0x80
10402 ? 2 /* 0xA020..0xFF7F */
10403 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
10404 : 1))); /* 0xA0A0..0xFFFF */
10406 fontp->baseline_offset
10407 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
10408 ? (long) value : 0);
10409 fontp->relative_compose
10410 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
10411 ? (long) value : 0);
10412 fontp->default_ascent
10413 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
10414 ? (long) value : 0);
10416 /* Set global flag fonts_changed_p to non-zero if the font loaded
10417 has a character with a smaller width than any other character
10418 before, or if the font loaded has a smaller height than any
10419 other font loaded before. If this happens, it will make a
10420 glyph matrix reallocation necessary. */
10421 fonts_changed_p |= x_compute_min_glyph_bounds (f);
10422 UNBLOCK_INPUT;
10423 return fontp;
10428 /* Return a pointer to struct font_info of a font named FONTNAME for
10429 frame F. If no such font is loaded, return NULL. */
10431 struct font_info *
10432 x_query_font (f, fontname)
10433 struct frame *f;
10434 register char *fontname;
10436 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10437 int i;
10439 for (i = 0; i < dpyinfo->n_fonts; i++)
10440 if (dpyinfo->font_table[i].name
10441 && (!xstricmp (dpyinfo->font_table[i].name, fontname)
10442 || !xstricmp (dpyinfo->font_table[i].full_name, fontname)))
10443 return (dpyinfo->font_table + i);
10444 return NULL;
10448 /* Find a CCL program for a font specified by FONTP, and set the member
10449 `encoder' of the structure. */
10451 void
10452 x_find_ccl_program (fontp)
10453 struct font_info *fontp;
10455 Lisp_Object list, elt;
10457 elt = Qnil;
10458 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
10460 elt = XCAR (list);
10461 if (CONSP (elt)
10462 && STRINGP (XCAR (elt))
10463 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10464 >= 0)
10465 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10466 >= 0)))
10467 break;
10470 if (! NILP (list))
10472 struct ccl_program *ccl
10473 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
10475 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
10476 xfree (ccl);
10477 else
10478 fontp->font_encoder = ccl;
10484 /***********************************************************************
10485 Initialization
10486 ***********************************************************************/
10488 #ifdef USE_X_TOOLKIT
10489 static XrmOptionDescRec emacs_options[] = {
10490 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10491 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10493 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10494 XrmoptionSepArg, NULL},
10495 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10497 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10498 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10499 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10500 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10501 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10502 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10503 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10506 /* Whether atimer for Xt timeouts is activated or not. */
10508 static int x_timeout_atimer_activated_flag;
10510 #endif /* USE_X_TOOLKIT */
10512 static int x_initialized;
10514 #ifdef HAVE_X_SM
10515 static int x_session_initialized;
10516 #endif
10518 #ifdef MULTI_KBOARD
10519 /* Test whether two display-name strings agree up to the dot that separates
10520 the screen number from the server number. */
10521 static int
10522 same_x_server (name1, name2)
10523 const char *name1, *name2;
10525 int seen_colon = 0;
10526 const unsigned char *system_name = SDATA (Vsystem_name);
10527 int system_name_length = strlen (system_name);
10528 int length_until_period = 0;
10530 while (system_name[length_until_period] != 0
10531 && system_name[length_until_period] != '.')
10532 length_until_period++;
10534 /* Treat `unix' like an empty host name. */
10535 if (! strncmp (name1, "unix:", 5))
10536 name1 += 4;
10537 if (! strncmp (name2, "unix:", 5))
10538 name2 += 4;
10539 /* Treat this host's name like an empty host name. */
10540 if (! strncmp (name1, system_name, system_name_length)
10541 && name1[system_name_length] == ':')
10542 name1 += system_name_length;
10543 if (! strncmp (name2, system_name, system_name_length)
10544 && name2[system_name_length] == ':')
10545 name2 += system_name_length;
10546 /* Treat this host's domainless name like an empty host name. */
10547 if (! strncmp (name1, system_name, length_until_period)
10548 && name1[length_until_period] == ':')
10549 name1 += length_until_period;
10550 if (! strncmp (name2, system_name, length_until_period)
10551 && name2[length_until_period] == ':')
10552 name2 += length_until_period;
10554 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10556 if (*name1 == ':')
10557 seen_colon++;
10558 if (seen_colon && *name1 == '.')
10559 return 1;
10561 return (seen_colon
10562 && (*name1 == '.' || *name1 == '\0')
10563 && (*name2 == '.' || *name2 == '\0'));
10565 #endif
10567 /* Count number of set bits in mask and number of bits to shift to
10568 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10569 to 5. */
10570 static void
10571 get_bits_and_offset (mask, bits, offset)
10572 unsigned long mask;
10573 int *bits;
10574 int *offset;
10576 int nr = 0;
10577 int off = 0;
10579 while (!(mask & 1))
10581 off++;
10582 mask >>= 1;
10585 while (mask & 1)
10587 nr++;
10588 mask >>= 1;
10591 *offset = off;
10592 *bits = nr;
10595 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
10596 But don't permanently open it, just test its availability. */
10599 x_display_ok (display)
10600 const char *display;
10602 int dpy_ok = 1;
10603 Display *dpy;
10605 dpy = XOpenDisplay (display);
10606 if (dpy)
10607 XCloseDisplay (dpy);
10608 else
10609 dpy_ok = 0;
10610 return dpy_ok;
10613 /* Open a connection to X display DISPLAY_NAME, and return
10614 the structure that describes the open display.
10615 If we cannot contact the display, return null. */
10617 struct x_display_info *
10618 x_term_init (display_name, xrm_option, resource_name)
10619 Lisp_Object display_name;
10620 char *xrm_option;
10621 char *resource_name;
10623 int connection;
10624 Display *dpy;
10625 struct terminal *terminal;
10626 struct x_display_info *dpyinfo;
10627 XrmDatabase xrdb;
10629 BLOCK_INPUT;
10631 if (!x_initialized)
10633 x_initialize ();
10634 ++x_initialized;
10637 if (! x_display_ok (SDATA (display_name)))
10638 error ("Display %s can't be opened", SDATA (display_name));
10640 #ifdef USE_GTK
10642 #define NUM_ARGV 10
10643 int argc;
10644 char *argv[NUM_ARGV];
10645 char **argv2 = argv;
10646 GdkAtom atom;
10648 #ifndef HAVE_GTK_MULTIDISPLAY
10649 if (!EQ (Vinitial_window_system, intern ("x")))
10650 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10651 #endif
10653 if (x_initialized++ > 1)
10655 #ifdef HAVE_GTK_MULTIDISPLAY
10656 /* Opening another display. If xg_display_open returns less
10657 than zero, we are probably on GTK 2.0, which can only handle
10658 one display. GTK 2.2 or later can handle more than one. */
10659 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10660 #endif
10661 error ("Sorry, this version of GTK can only handle one display");
10663 else
10665 for (argc = 0; argc < NUM_ARGV; ++argc)
10666 argv[argc] = 0;
10668 argc = 0;
10669 argv[argc++] = initial_argv[0];
10671 if (! NILP (display_name))
10673 argv[argc++] = "--display";
10674 argv[argc++] = SDATA (display_name);
10677 argv[argc++] = "--name";
10678 argv[argc++] = resource_name;
10680 #ifdef HAVE_X11R5
10681 XSetLocaleModifiers ("");
10682 #endif
10684 gtk_init (&argc, &argv2);
10686 /* gtk_init does set_locale. We must fix locale after calling it. */
10687 fixup_locale ();
10688 xg_initialize ();
10690 dpy = GDK_DISPLAY ();
10692 /* NULL window -> events for all windows go to our function */
10693 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10695 /* Load our own gtkrc if it exists. */
10697 char *file = "~/.emacs.d/gtkrc";
10698 Lisp_Object s, abs_file;
10700 s = make_string (file, strlen (file));
10701 abs_file = Fexpand_file_name (s, Qnil);
10703 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10704 gtk_rc_parse (SDATA (abs_file));
10707 XSetErrorHandler (x_error_handler);
10708 XSetIOErrorHandler (x_io_error_quitter);
10711 #else /* not USE_GTK */
10712 #ifdef USE_X_TOOLKIT
10713 /* weiner@footloose.sps.mot.com reports that this causes
10714 errors with X11R5:
10715 X protocol error: BadAtom (invalid Atom parameter)
10716 on protocol request 18skiloaf.
10717 So let's not use it until R6. */
10718 #ifdef HAVE_X11XTR6
10719 XtSetLanguageProc (NULL, NULL, NULL);
10720 #endif
10723 int argc = 0;
10724 char *argv[3];
10726 argv[0] = "";
10727 argc = 1;
10728 if (xrm_option)
10730 argv[argc++] = "-xrm";
10731 argv[argc++] = xrm_option;
10733 turn_on_atimers (0);
10734 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10735 resource_name, EMACS_CLASS,
10736 emacs_options, XtNumber (emacs_options),
10737 &argc, argv);
10738 turn_on_atimers (1);
10740 #ifdef HAVE_X11XTR6
10741 /* I think this is to compensate for XtSetLanguageProc. */
10742 fixup_locale ();
10743 #endif
10746 #else /* not USE_X_TOOLKIT */
10747 #ifdef HAVE_X11R5
10748 XSetLocaleModifiers ("");
10749 #endif
10750 dpy = XOpenDisplay (SDATA (display_name));
10751 #endif /* not USE_X_TOOLKIT */
10752 #endif /* not USE_GTK*/
10754 /* Detect failure. */
10755 if (dpy == 0)
10757 UNBLOCK_INPUT;
10758 return 0;
10761 /* We have definitely succeeded. Record the new connection. */
10763 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10764 bzero (dpyinfo, sizeof *dpyinfo);
10766 terminal = x_create_terminal (dpyinfo);
10768 #ifdef MULTI_KBOARD
10770 struct x_display_info *share;
10771 Lisp_Object tail;
10773 for (share = x_display_list, tail = x_display_name_list; share;
10774 share = share->next, tail = XCDR (tail))
10775 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10776 SDATA (display_name)))
10777 break;
10778 if (share)
10779 terminal->kboard = share->terminal->kboard;
10780 else
10782 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10783 init_kboard (terminal->kboard);
10784 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10786 char *vendor = ServerVendor (dpy);
10787 UNBLOCK_INPUT;
10788 terminal->kboard->Vsystem_key_alist
10789 = call1 (Qvendor_specific_keysyms,
10790 vendor ? build_string (vendor) : empty_unibyte_string);
10791 BLOCK_INPUT;
10794 terminal->kboard->next_kboard = all_kboards;
10795 all_kboards = terminal->kboard;
10796 /* Don't let the initial kboard remain current longer than necessary.
10797 That would cause problems if a file loaded on startup tries to
10798 prompt in the mini-buffer. */
10799 if (current_kboard == initial_kboard)
10800 current_kboard = terminal->kboard;
10802 terminal->kboard->reference_count++;
10804 #endif
10806 /* Put this display on the chain. */
10807 dpyinfo->next = x_display_list;
10808 x_display_list = dpyinfo;
10810 /* Put it on x_display_name_list as well, to keep them parallel. */
10811 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10812 x_display_name_list);
10813 dpyinfo->name_list_element = XCAR (x_display_name_list);
10815 dpyinfo->display = dpy;
10817 /* Set the name of the terminal. */
10818 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10819 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10820 terminal->name[SBYTES (display_name)] = 0;
10822 #if 0
10823 XSetAfterFunction (x_current_display, x_trace_wire);
10824 #endif /* ! 0 */
10826 dpyinfo->x_id_name
10827 = (char *) xmalloc (SBYTES (Vinvocation_name)
10828 + SBYTES (Vsystem_name)
10829 + 2);
10830 sprintf (dpyinfo->x_id_name, "%s@%s",
10831 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10833 /* Figure out which modifier bits mean what. */
10834 x_find_modifier_meanings (dpyinfo);
10836 /* Get the scroll bar cursor. */
10837 #ifdef USE_GTK
10838 /* We must create a GTK cursor, it is required for GTK widgets. */
10839 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10840 #endif /* USE_GTK */
10842 dpyinfo->vertical_scroll_bar_cursor
10843 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10845 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10846 resource_name, EMACS_CLASS);
10847 #ifdef HAVE_XRMSETDATABASE
10848 XrmSetDatabase (dpyinfo->display, xrdb);
10849 #else
10850 dpyinfo->display->db = xrdb;
10851 #endif
10852 /* Put the rdb where we can find it in a way that works on
10853 all versions. */
10854 dpyinfo->xrdb = xrdb;
10856 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10857 DefaultScreen (dpyinfo->display));
10858 select_visual (dpyinfo);
10859 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10860 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
10861 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
10862 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10863 dpyinfo->client_leader_window = 0;
10864 dpyinfo->grabbed = 0;
10865 dpyinfo->reference_count = 0;
10866 dpyinfo->icon_bitmap_id = -1;
10867 dpyinfo->font_table = NULL;
10868 dpyinfo->n_fonts = 0;
10869 dpyinfo->font_table_size = 0;
10870 dpyinfo->bitmaps = 0;
10871 dpyinfo->bitmaps_size = 0;
10872 dpyinfo->bitmaps_last = 0;
10873 dpyinfo->scratch_cursor_gc = 0;
10874 dpyinfo->mouse_face_mouse_frame = 0;
10875 dpyinfo->mouse_face_deferred_gc = 0;
10876 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10877 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10878 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10879 dpyinfo->mouse_face_window = Qnil;
10880 dpyinfo->mouse_face_overlay = Qnil;
10881 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10882 dpyinfo->mouse_face_defer = 0;
10883 dpyinfo->mouse_face_hidden = 0;
10884 dpyinfo->x_focus_frame = 0;
10885 dpyinfo->x_focus_event_frame = 0;
10886 dpyinfo->x_highlight_frame = 0;
10887 dpyinfo->image_cache = make_image_cache ();
10888 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10890 /* See if we can construct pixel values from RGB values. */
10891 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10892 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10894 if (dpyinfo->visual->class == TrueColor)
10896 get_bits_and_offset (dpyinfo->visual->red_mask,
10897 &dpyinfo->red_bits, &dpyinfo->red_offset);
10898 get_bits_and_offset (dpyinfo->visual->blue_mask,
10899 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10900 get_bits_and_offset (dpyinfo->visual->green_mask,
10901 &dpyinfo->green_bits, &dpyinfo->green_offset);
10904 /* See if a private colormap is requested. */
10905 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10907 if (dpyinfo->visual->class == PseudoColor)
10909 Lisp_Object value;
10910 value = display_x_get_resource (dpyinfo,
10911 build_string ("privateColormap"),
10912 build_string ("PrivateColormap"),
10913 Qnil, Qnil);
10914 if (STRINGP (value)
10915 && (!strcmp (SDATA (value), "true")
10916 || !strcmp (SDATA (value), "on")))
10917 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10920 else
10921 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10922 dpyinfo->visual, AllocNone);
10925 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10926 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10927 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10928 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10929 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10930 pixels = DisplayWidth (dpyinfo->display, screen_number);
10931 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10932 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10933 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10936 dpyinfo->Xatom_wm_protocols
10937 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10938 dpyinfo->Xatom_wm_take_focus
10939 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10940 dpyinfo->Xatom_wm_save_yourself
10941 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10942 dpyinfo->Xatom_wm_delete_window
10943 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10944 dpyinfo->Xatom_wm_change_state
10945 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10946 dpyinfo->Xatom_wm_configure_denied
10947 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10948 dpyinfo->Xatom_wm_window_moved
10949 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10950 dpyinfo->Xatom_wm_client_leader
10951 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10952 dpyinfo->Xatom_editres
10953 = XInternAtom (dpyinfo->display, "Editres", False);
10954 dpyinfo->Xatom_CLIPBOARD
10955 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10956 dpyinfo->Xatom_TIMESTAMP
10957 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10958 dpyinfo->Xatom_TEXT
10959 = XInternAtom (dpyinfo->display, "TEXT", False);
10960 dpyinfo->Xatom_COMPOUND_TEXT
10961 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10962 dpyinfo->Xatom_UTF8_STRING
10963 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10964 dpyinfo->Xatom_DELETE
10965 = XInternAtom (dpyinfo->display, "DELETE", False);
10966 dpyinfo->Xatom_MULTIPLE
10967 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10968 dpyinfo->Xatom_INCR
10969 = XInternAtom (dpyinfo->display, "INCR", False);
10970 dpyinfo->Xatom_EMACS_TMP
10971 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10972 dpyinfo->Xatom_TARGETS
10973 = XInternAtom (dpyinfo->display, "TARGETS", False);
10974 dpyinfo->Xatom_NULL
10975 = XInternAtom (dpyinfo->display, "NULL", False);
10976 dpyinfo->Xatom_ATOM_PAIR
10977 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10978 /* For properties of font. */
10979 dpyinfo->Xatom_PIXEL_SIZE
10980 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10981 dpyinfo->Xatom_AVERAGE_WIDTH
10982 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10983 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10984 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10985 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10986 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10987 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10988 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10990 /* Ghostscript support. */
10991 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10992 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10994 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10995 False);
10997 dpyinfo->cut_buffers_initialized = 0;
10999 dpyinfo->x_dnd_atoms_size = 8;
11000 dpyinfo->x_dnd_atoms_length = 0;
11001 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
11002 * dpyinfo->x_dnd_atoms_size);
11004 dpyinfo->net_supported_atoms = NULL;
11005 dpyinfo->nr_net_supported_atoms = 0;
11006 dpyinfo->net_supported_window = 0;
11008 connection = ConnectionNumber (dpyinfo->display);
11009 dpyinfo->connection = connection;
11012 char null_bits[1];
11014 null_bits[0] = 0x00;
11016 dpyinfo->null_pixel
11017 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
11018 null_bits, 1, 1, (long) 0, (long) 0,
11023 extern int gray_bitmap_width, gray_bitmap_height;
11024 extern char *gray_bitmap_bits;
11025 dpyinfo->gray
11026 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
11027 gray_bitmap_bits,
11028 gray_bitmap_width, gray_bitmap_height,
11029 (unsigned long) 1, (unsigned long) 0, 1);
11032 #ifdef HAVE_X_I18N
11033 xim_initialize (dpyinfo, resource_name);
11034 #endif
11036 #ifdef subprocesses
11037 /* This is only needed for distinguishing keyboard and process input. */
11038 if (connection != 0)
11039 add_keyboard_wait_descriptor (connection);
11040 #endif
11042 #ifndef F_SETOWN_BUG
11043 #ifdef F_SETOWN
11044 #ifdef F_SETOWN_SOCK_NEG
11045 /* stdin is a socket here */
11046 fcntl (connection, F_SETOWN, -getpid ());
11047 #else /* ! defined (F_SETOWN_SOCK_NEG) */
11048 fcntl (connection, F_SETOWN, getpid ());
11049 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
11050 #endif /* ! defined (F_SETOWN) */
11051 #endif /* F_SETOWN_BUG */
11053 #ifdef SIGIO
11054 if (interrupt_input)
11055 init_sigio (connection);
11056 #endif /* ! defined (SIGIO) */
11058 #ifdef USE_LUCID
11059 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
11060 /* Make sure that we have a valid font for dialog boxes
11061 so that Xt does not crash. */
11063 Display *dpy = dpyinfo->display;
11064 XrmValue d, fr, to;
11065 Font font;
11067 d.addr = (XPointer)&dpy;
11068 d.size = sizeof (Display *);
11069 fr.addr = XtDefaultFont;
11070 fr.size = sizeof (XtDefaultFont);
11071 to.size = sizeof (Font *);
11072 to.addr = (XPointer)&font;
11073 x_catch_errors (dpy);
11074 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
11075 abort ();
11076 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
11077 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
11078 x_uncatch_errors ();
11080 #endif
11081 #endif
11083 /* See if we should run in synchronous mode. This is useful
11084 for debugging X code. */
11086 Lisp_Object value;
11087 value = display_x_get_resource (dpyinfo,
11088 build_string ("synchronous"),
11089 build_string ("Synchronous"),
11090 Qnil, Qnil);
11091 if (STRINGP (value)
11092 && (!strcmp (SDATA (value), "true")
11093 || !strcmp (SDATA (value), "on")))
11094 XSynchronize (dpyinfo->display, True);
11098 Lisp_Object value;
11099 value = display_x_get_resource (dpyinfo,
11100 build_string ("useXIM"),
11101 build_string ("UseXIM"),
11102 Qnil, Qnil);
11103 #ifdef USE_XIM
11104 if (STRINGP (value)
11105 && (!strcmp (XSTRING (value)->data, "false")
11106 || !strcmp (XSTRING (value)->data, "off")))
11107 use_xim = 0;
11108 #else
11109 if (STRINGP (value)
11110 && (!strcmp (XSTRING (value)->data, "true")
11111 || !strcmp (XSTRING (value)->data, "on")))
11112 use_xim = 1;
11113 #endif
11116 #ifdef HAVE_X_SM
11117 /* Only do this for the very first display in the Emacs session.
11118 Ignore X session management when Emacs was first started on a
11119 tty. */
11120 if (terminal->id == 1)
11121 x_session_initialize (dpyinfo);
11122 #endif
11124 UNBLOCK_INPUT;
11126 return dpyinfo;
11129 /* Get rid of display DPYINFO, deleting all frames on it,
11130 and without sending any more commands to the X server. */
11132 void
11133 x_delete_display (dpyinfo)
11134 struct x_display_info *dpyinfo;
11136 int i;
11137 struct terminal *t;
11139 /* Close all frames and delete the generic struct terminal for this
11140 X display. */
11141 for (t = terminal_list; t; t = t->next_terminal)
11142 if (t->type == output_x_window && t->display_info.x == dpyinfo)
11144 #ifdef HAVE_X_SM
11145 /* Close X session management when we close its display. */
11146 if (t->id == 1 && x_session_have_connection ())
11147 x_session_close();
11148 #endif
11149 delete_terminal (t);
11150 break;
11153 delete_keyboard_wait_descriptor (dpyinfo->connection);
11155 /* Discard this display from x_display_name_list and x_display_list.
11156 We can't use Fdelq because that can quit. */
11157 if (! NILP (x_display_name_list)
11158 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
11159 x_display_name_list = XCDR (x_display_name_list);
11160 else
11162 Lisp_Object tail;
11164 tail = x_display_name_list;
11165 while (CONSP (tail) && CONSP (XCDR (tail)))
11167 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
11169 XSETCDR (tail, XCDR (XCDR (tail)));
11170 break;
11172 tail = XCDR (tail);
11176 if (next_noop_dpyinfo == dpyinfo)
11177 next_noop_dpyinfo = dpyinfo->next;
11179 if (x_display_list == dpyinfo)
11180 x_display_list = dpyinfo->next;
11181 else
11183 struct x_display_info *tail;
11185 for (tail = x_display_list; tail; tail = tail->next)
11186 if (tail->next == dpyinfo)
11187 tail->next = tail->next->next;
11190 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
11191 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
11192 XrmDestroyDatabase (dpyinfo->xrdb);
11193 #endif
11194 #endif
11195 #ifdef HAVE_X_I18N
11196 if (dpyinfo->xim)
11197 xim_close_dpy (dpyinfo);
11198 #endif
11200 /* Free the font names in the font table. */
11201 for (i = 0; i < dpyinfo->n_fonts; i++)
11202 if (dpyinfo->font_table[i].name)
11204 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
11205 xfree (dpyinfo->font_table[i].full_name);
11206 xfree (dpyinfo->font_table[i].name);
11209 if (dpyinfo->font_table)
11211 if (dpyinfo->font_table->font_encoder)
11212 xfree (dpyinfo->font_table->font_encoder);
11213 xfree (dpyinfo->font_table);
11215 if (dpyinfo->x_id_name)
11216 xfree (dpyinfo->x_id_name);
11217 if (dpyinfo->color_cells)
11218 xfree (dpyinfo->color_cells);
11219 xfree (dpyinfo);
11222 #ifdef USE_X_TOOLKIT
11224 /* Atimer callback function for TIMER. Called every 0.1s to process
11225 Xt timeouts, if needed. We must avoid calling XtAppPending as
11226 much as possible because that function does an implicit XFlush
11227 that slows us down. */
11229 static void
11230 x_process_timeouts (timer)
11231 struct atimer *timer;
11233 BLOCK_INPUT;
11234 x_timeout_atimer_activated_flag = 0;
11235 if (toolkit_scroll_bar_interaction || popup_activated ())
11237 while (XtAppPending (Xt_app_con) & XtIMTimer)
11238 XtAppProcessEvent (Xt_app_con, XtIMTimer);
11239 /* Reactivate the atimer for next time. */
11240 x_activate_timeout_atimer ();
11242 UNBLOCK_INPUT;
11245 /* Install an asynchronous timer that processes Xt timeout events
11246 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
11247 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
11248 function whenever these variables are set. This is necessary
11249 because some widget sets use timeouts internally, for example the
11250 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
11251 processed, these widgets don't behave normally. */
11253 void
11254 x_activate_timeout_atimer ()
11256 BLOCK_INPUT;
11257 if (!x_timeout_atimer_activated_flag)
11259 EMACS_TIME interval;
11261 EMACS_SET_SECS_USECS (interval, 0, 100000);
11262 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
11263 x_timeout_atimer_activated_flag = 1;
11265 UNBLOCK_INPUT;
11268 #endif /* USE_X_TOOLKIT */
11271 /* Set up use of X before we make the first connection. */
11273 extern frame_parm_handler x_frame_parm_handlers[];
11275 static struct redisplay_interface x_redisplay_interface =
11277 x_frame_parm_handlers,
11278 x_produce_glyphs,
11279 x_write_glyphs,
11280 x_insert_glyphs,
11281 x_clear_end_of_line,
11282 x_scroll_run,
11283 x_after_update_window_line,
11284 x_update_window_begin,
11285 x_update_window_end,
11286 x_cursor_to,
11287 x_flush,
11288 #ifdef XFlush
11289 x_flush,
11290 #else
11291 0, /* flush_display_optional */
11292 #endif
11293 x_clear_window_mouse_face,
11294 x_get_glyph_overhangs,
11295 x_fix_overlapping_area,
11296 x_draw_fringe_bitmap,
11297 0, /* define_fringe_bitmap */
11298 0, /* destroy_fringe_bitmap */
11299 x_per_char_metric,
11300 x_encode_char,
11301 x_compute_glyph_string_overhangs,
11302 x_draw_glyph_string,
11303 x_define_frame_cursor,
11304 x_clear_frame_area,
11305 x_draw_window_cursor,
11306 x_draw_vertical_window_border,
11307 x_shift_glyphs_for_insert
11311 /* This function is called when the last frame on a display is deleted. */
11312 void
11313 x_delete_terminal (struct terminal *terminal)
11315 struct x_display_info *dpyinfo = terminal->display_info.x;
11316 int i;
11318 /* Protect against recursive calls. Fdelete_frame in
11319 delete_terminal calls us back when it deletes our last frame. */
11320 if (!terminal->name)
11321 return;
11323 BLOCK_INPUT;
11324 /* Free the fonts in the font table. */
11325 for (i = 0; i < dpyinfo->n_fonts; i++)
11326 if (dpyinfo->font_table[i].name)
11328 XFreeFont (dpyinfo->display, dpyinfo->font_table[i].font);
11331 x_destroy_all_bitmaps (dpyinfo);
11332 XSetCloseDownMode (dpyinfo->display, DestroyAll);
11334 #ifdef USE_GTK
11335 xg_display_close (dpyinfo->display);
11336 #else
11337 #ifdef USE_X_TOOLKIT
11338 XtCloseDisplay (dpyinfo->display);
11339 #else
11340 XCloseDisplay (dpyinfo->display);
11341 #endif
11342 #endif /* ! USE_GTK */
11344 x_delete_display (dpyinfo);
11345 UNBLOCK_INPUT;
11348 /* Create a struct terminal, initialize it with the X11 specific
11349 functions and make DISPLAY->TERMINAL point to it. */
11351 static struct terminal *
11352 x_create_terminal (struct x_display_info *dpyinfo)
11354 struct terminal *terminal;
11356 terminal = create_terminal ();
11358 terminal->type = output_x_window;
11359 terminal->display_info.x = dpyinfo;
11360 dpyinfo->terminal = terminal;
11362 /* kboard is initialized in x_term_init. */
11364 terminal->clear_frame_hook = x_clear_frame;
11365 terminal->ins_del_lines_hook = x_ins_del_lines;
11366 terminal->delete_glyphs_hook = x_delete_glyphs;
11367 terminal->ring_bell_hook = XTring_bell;
11368 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
11369 terminal->set_terminal_modes_hook = XTset_terminal_modes;
11370 terminal->update_begin_hook = x_update_begin;
11371 terminal->update_end_hook = x_update_end;
11372 terminal->set_terminal_window_hook = XTset_terminal_window;
11373 terminal->read_socket_hook = XTread_socket;
11374 terminal->frame_up_to_date_hook = XTframe_up_to_date;
11375 terminal->mouse_position_hook = XTmouse_position;
11376 terminal->frame_rehighlight_hook = XTframe_rehighlight;
11377 terminal->frame_raise_lower_hook = XTframe_raise_lower;
11378 terminal->fullscreen_hook = XTfullscreen_hook;
11379 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
11380 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
11381 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
11382 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
11384 terminal->delete_frame_hook = x_destroy_window;
11385 terminal->delete_terminal_hook = x_delete_terminal;
11387 terminal->rif = &x_redisplay_interface;
11388 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
11389 terminal->char_ins_del_ok = 1;
11390 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
11391 terminal->fast_clear_end_of_line = 1; /* X does this well. */
11392 terminal->memory_below_frame = 0; /* We don't remember what scrolls
11393 off the bottom. */
11395 return terminal;
11398 void
11399 x_initialize ()
11401 baud_rate = 19200;
11403 x_noop_count = 0;
11404 last_tool_bar_item = -1;
11405 any_help_event_p = 0;
11406 ignore_next_mouse_click_timeout = 0;
11407 #ifdef HAVE_X_SM
11408 x_session_initialized = 0;
11409 #endif
11411 #ifdef USE_GTK
11412 current_count = -1;
11413 #endif
11415 /* Try to use interrupt input; if we can't, then start polling. */
11416 Fset_input_interrupt_mode (Qt);
11418 #ifdef USE_X_TOOLKIT
11419 XtToolkitInitialize ();
11421 Xt_app_con = XtCreateApplicationContext ();
11423 /* Register a converter from strings to pixels, which uses
11424 Emacs' color allocation infrastructure. */
11425 XtAppSetTypeConverter (Xt_app_con,
11426 XtRString, XtRPixel, cvt_string_to_pixel,
11427 cvt_string_to_pixel_args,
11428 XtNumber (cvt_string_to_pixel_args),
11429 XtCacheByDisplay, cvt_pixel_dtor);
11431 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
11432 #endif
11434 #ifdef USE_TOOLKIT_SCROLL_BARS
11435 #ifndef USE_GTK
11436 xaw3d_arrow_scroll = False;
11437 xaw3d_pick_top = True;
11438 #endif
11439 #endif
11441 /* Note that there is no real way portable across R3/R4 to get the
11442 original error handler. */
11443 XSetErrorHandler (x_error_handler);
11444 XSetIOErrorHandler (x_io_error_quitter);
11446 /* Disable Window Change signals; they are handled by X events. */
11447 #if 0 /* Don't. We may want to open tty frames later. */
11448 #ifdef SIGWINCH
11449 signal (SIGWINCH, SIG_DFL);
11450 #endif /* SIGWINCH */
11451 #endif
11453 signal (SIGPIPE, x_connection_signal);
11457 void
11458 syms_of_xterm ()
11460 x_error_message = NULL;
11462 staticpro (&x_display_name_list);
11463 x_display_name_list = Qnil;
11465 staticpro (&last_mouse_scroll_bar);
11466 last_mouse_scroll_bar = Qnil;
11468 staticpro (&Qvendor_specific_keysyms);
11469 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
11471 staticpro (&Qutf_8);
11472 Qutf_8 = intern ("utf-8");
11473 staticpro (&Qlatin_1);
11474 Qlatin_1 = intern ("latin-1");
11476 staticpro (&last_mouse_press_frame);
11477 last_mouse_press_frame = Qnil;
11479 DEFVAR_BOOL ("x-use-underline-position-properties",
11480 &x_use_underline_position_properties,
11481 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
11482 A value of nil means ignore them. If you encounter fonts with bogus
11483 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
11484 to 4.1, set this to nil. */);
11485 x_use_underline_position_properties = 1;
11487 DEFVAR_BOOL ("x-underline-at-descent-line",
11488 &x_underline_at_descent_line,
11489 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
11490 A value of nil means to draw the underline according to the value of the
11491 variable `x-use-underline-position-properties', which is usually at the
11492 baseline level. The default value is nil. */);
11493 x_underline_at_descent_line = 0;
11495 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
11496 &x_mouse_click_focus_ignore_position,
11497 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
11498 This variable is only used when the window manager requires that you
11499 click on a frame to select it (give it focus). In that case, a value
11500 of nil, means that the selected window and cursor position changes to
11501 reflect the mouse click position, while a non-nil value means that the
11502 selected window or cursor position is preserved. */);
11503 x_mouse_click_focus_ignore_position = 0;
11505 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
11506 doc: /* What X toolkit scroll bars Emacs uses.
11507 A value of nil means Emacs doesn't use X toolkit scroll bars.
11508 Otherwise, value is a symbol describing the X toolkit. */);
11509 #ifdef USE_TOOLKIT_SCROLL_BARS
11510 #ifdef USE_MOTIF
11511 Vx_toolkit_scroll_bars = intern ("motif");
11512 #elif defined HAVE_XAW3D
11513 Vx_toolkit_scroll_bars = intern ("xaw3d");
11514 #elif USE_GTK
11515 Vx_toolkit_scroll_bars = intern ("gtk");
11516 #else
11517 Vx_toolkit_scroll_bars = intern ("xaw");
11518 #endif
11519 #else
11520 Vx_toolkit_scroll_bars = Qnil;
11521 #endif
11523 staticpro (&last_mouse_motion_frame);
11524 last_mouse_motion_frame = Qnil;
11526 Qmodifier_value = intern ("modifier-value");
11527 Qalt = intern ("alt");
11528 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
11529 Qhyper = intern ("hyper");
11530 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11531 Qmeta = intern ("meta");
11532 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11533 Qsuper = intern ("super");
11534 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11536 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
11537 doc: /* Which keys Emacs uses for the alt modifier.
11538 This should be one of the symbols `alt', `hyper', `meta', `super'.
11539 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11540 is nil, which is the same as `alt'. */);
11541 Vx_alt_keysym = Qnil;
11543 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
11544 doc: /* Which keys Emacs uses for the hyper modifier.
11545 This should be one of the symbols `alt', `hyper', `meta', `super'.
11546 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11547 default is nil, which is the same as `hyper'. */);
11548 Vx_hyper_keysym = Qnil;
11550 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
11551 doc: /* Which keys Emacs uses for the meta modifier.
11552 This should be one of the symbols `alt', `hyper', `meta', `super'.
11553 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11554 default is nil, which is the same as `meta'. */);
11555 Vx_meta_keysym = Qnil;
11557 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
11558 doc: /* Which keys Emacs uses for the super modifier.
11559 This should be one of the symbols `alt', `hyper', `meta', `super'.
11560 For example, `super' means use the Super_L and Super_R keysyms. The
11561 default is nil, which is the same as `super'. */);
11562 Vx_super_keysym = Qnil;
11564 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
11565 doc: /* Hash table of character codes indexed by X keysym codes. */);
11566 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
11567 make_float (DEFAULT_REHASH_SIZE),
11568 make_float (DEFAULT_REHASH_THRESHOLD),
11569 Qnil, Qnil, Qnil);
11572 #endif /* HAVE_X_WINDOWS */
11574 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
11575 (do not change this comment) */