Switch to recommended form of GPLv3 permissions notice.
[emacs.git] / src / xterm.c
blob7590d2ee7cefdddc35b7ced88a9e04f2f483e1e5
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, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
22 /* Xt features made by Fred Pierresteguy. */
24 #include <config.h>
26 /* On 4.3 these lose if they come after xterm.h. */
27 /* Putting these at the beginning seems to be standard for other .c files. */
28 #include <signal.h>
30 #include <stdio.h>
32 #ifdef HAVE_X_WINDOWS
34 #include "lisp.h"
35 #include "blockinput.h"
37 /* Need syssignal.h for various externs and definitions that may be required
38 by some configurations for calls to signal later in this source file. */
39 #include "syssignal.h"
41 /* This may include sys/types.h, and that somehow loses
42 if this is not done before the other system files. */
43 #include "xterm.h"
44 #include <X11/cursorfont.h>
46 /* Load sys/types.h if not already loaded.
47 In some systems loading it twice is suicidal. */
48 #ifndef makedev
49 #include <sys/types.h>
50 #endif /* makedev */
52 #ifdef BSD_SYSTEM
53 #include <sys/ioctl.h>
54 #endif /* ! defined (BSD_SYSTEM) */
56 #include "systime.h"
58 #ifndef INCLUDED_FCNTL
59 #include <fcntl.h>
60 #endif
61 #include <ctype.h>
62 #include <errno.h>
63 #include <setjmp.h>
64 #include <sys/stat.h>
65 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
66 /* #include <sys/param.h> */
68 #include "charset.h"
69 #include "character.h"
70 #include "coding.h"
71 #include "frame.h"
72 #include "dispextern.h"
73 #include "fontset.h"
74 #include "termhooks.h"
75 #include "termopts.h"
76 #include "termchar.h"
77 #include "emacs-icon.h"
78 #include "disptab.h"
79 #include "buffer.h"
80 #include "window.h"
81 #include "keyboard.h"
82 #include "intervals.h"
83 #include "process.h"
84 #include "atimer.h"
85 #include "keymap.h"
86 #include "font.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_update_end P_ ((struct frame *));
338 static void XTframe_up_to_date P_ ((struct frame *));
339 static void XTset_terminal_modes P_ ((struct terminal *));
340 static void XTreset_terminal_modes P_ ((struct terminal *));
341 static void x_clear_frame P_ ((struct frame *));
342 static void frame_highlight P_ ((struct frame *));
343 static void frame_unhighlight P_ ((struct frame *));
344 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
345 static void x_focus_changed P_ ((int, int, struct x_display_info *,
346 struct frame *, struct input_event *));
347 static void x_detect_focus_change P_ ((struct x_display_info *,
348 XEvent *, struct input_event *));
349 static void XTframe_rehighlight P_ ((struct frame *));
350 static void x_frame_rehighlight P_ ((struct x_display_info *));
351 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
352 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
353 enum text_cursor_kinds));
355 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
356 static void x_flush P_ ((struct frame *f));
357 static void x_update_begin P_ ((struct frame *));
358 static void x_update_window_begin P_ ((struct window *));
359 static void x_after_update_window_line P_ ((struct glyph_row *));
360 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
361 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
362 enum scroll_bar_part *,
363 Lisp_Object *, Lisp_Object *,
364 unsigned long *));
365 static void x_check_fullscreen P_ ((struct frame *));
366 static void x_check_expected_move P_ ((struct frame *, int, int));
367 static void x_sync_with_move P_ ((struct frame *, int, int, int));
368 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
369 int *, struct input_event *));
370 /* Don't declare this NO_RETURN because we want no
371 interference with debugging failing X calls. */
372 static SIGTYPE x_connection_closed P_ ((Display *, char *));
375 /* Flush display of frame F, or of all frames if F is null. */
377 static void
378 x_flush (f)
379 struct frame *f;
381 /* Don't call XFlush when it is not safe to redisplay; the X
382 connection may be broken. */
383 if (!NILP (Vinhibit_redisplay))
384 return;
386 BLOCK_INPUT;
387 if (f == NULL)
389 Lisp_Object rest, frame;
390 FOR_EACH_FRAME (rest, frame)
391 if (FRAME_X_P (XFRAME (frame)))
392 x_flush (XFRAME (frame));
394 else if (FRAME_X_P (f))
395 XFlush (FRAME_X_DISPLAY (f));
396 UNBLOCK_INPUT;
400 /* Remove calls to XFlush by defining XFlush to an empty replacement.
401 Calls to XFlush should be unnecessary because the X output buffer
402 is flushed automatically as needed by calls to XPending,
403 XNextEvent, or XWindowEvent according to the XFlush man page.
404 XTread_socket calls XPending. Removing XFlush improves
405 performance. */
407 #define XFlush(DISPLAY) (void) 0
410 /***********************************************************************
411 Debugging
412 ***********************************************************************/
414 #if 0
416 /* This is a function useful for recording debugging information about
417 the sequence of occurrences in this file. */
419 struct record
421 char *locus;
422 int type;
425 struct record event_record[100];
427 int event_record_index;
429 record_event (locus, type)
430 char *locus;
431 int type;
433 if (event_record_index == sizeof (event_record) / sizeof (struct record))
434 event_record_index = 0;
436 event_record[event_record_index].locus = locus;
437 event_record[event_record_index].type = type;
438 event_record_index++;
441 #endif /* 0 */
445 /* Return the struct x_display_info corresponding to DPY. */
447 struct x_display_info *
448 x_display_info_for_display (dpy)
449 Display *dpy;
451 struct x_display_info *dpyinfo;
453 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
454 if (dpyinfo->display == dpy)
455 return dpyinfo;
457 return 0;
462 /***********************************************************************
463 Starting and ending an update
464 ***********************************************************************/
466 /* Start an update of frame F. This function is installed as a hook
467 for update_begin, i.e. it is called when update_begin is called.
468 This function is called prior to calls to x_update_window_begin for
469 each window being updated. Currently, there is nothing to do here
470 because all interesting stuff is done on a window basis. */
472 static void
473 x_update_begin (f)
474 struct frame *f;
476 /* Nothing to do. */
480 /* Start update of window W. Set the global variable updated_window
481 to the window being updated and set output_cursor to the cursor
482 position of W. */
484 static void
485 x_update_window_begin (w)
486 struct window *w;
488 struct frame *f = XFRAME (WINDOW_FRAME (w));
489 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
491 updated_window = w;
492 set_output_cursor (&w->cursor);
494 BLOCK_INPUT;
496 if (f == display_info->mouse_face_mouse_frame)
498 /* Don't do highlighting for mouse motion during the update. */
499 display_info->mouse_face_defer = 1;
501 /* If F needs to be redrawn, simply forget about any prior mouse
502 highlighting. */
503 if (FRAME_GARBAGED_P (f))
504 display_info->mouse_face_window = Qnil;
506 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
507 their mouse_face_p flag set, which means that they are always
508 unequal to rows in a desired matrix which never have that
509 flag set. So, rows containing mouse-face glyphs are never
510 scrolled, and we don't have to switch the mouse highlight off
511 here to prevent it from being scrolled. */
513 /* Can we tell that this update does not affect the window
514 where the mouse highlight is? If so, no need to turn off.
515 Likewise, don't do anything if the frame is garbaged;
516 in that case, the frame's current matrix that we would use
517 is all wrong, and we will redisplay that line anyway. */
518 if (!NILP (display_info->mouse_face_window)
519 && w == XWINDOW (display_info->mouse_face_window))
521 int i;
523 for (i = 0; i < w->desired_matrix->nrows; ++i)
524 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
525 break;
527 if (i < w->desired_matrix->nrows)
528 clear_mouse_face (display_info);
530 #endif /* 0 */
533 UNBLOCK_INPUT;
537 /* Draw a vertical window border from (x,y0) to (x,y1) */
539 static void
540 x_draw_vertical_window_border (w, x, y0, y1)
541 struct window *w;
542 int x, y0, y1;
544 struct frame *f = XFRAME (WINDOW_FRAME (w));
545 struct face *face;
547 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
548 if (face)
549 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
550 face->foreground);
552 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
553 f->output_data.x->normal_gc, x, y0, x, y1);
556 /* End update of window W (which is equal to updated_window).
558 Draw vertical borders between horizontally adjacent windows, and
559 display W's cursor if CURSOR_ON_P is non-zero.
561 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
562 glyphs in mouse-face were overwritten. In that case we have to
563 make sure that the mouse-highlight is properly redrawn.
565 W may be a menu bar pseudo-window in case we don't have X toolkit
566 support. Such windows don't have a cursor, so don't display it
567 here. */
569 static void
570 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
571 struct window *w;
572 int cursor_on_p, mouse_face_overwritten_p;
574 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
576 if (!w->pseudo_window_p)
578 BLOCK_INPUT;
580 if (cursor_on_p)
581 display_and_set_cursor (w, 1, output_cursor.hpos,
582 output_cursor.vpos,
583 output_cursor.x, output_cursor.y);
585 if (draw_window_fringes (w, 1))
586 x_draw_vertical_border (w);
588 UNBLOCK_INPUT;
591 /* If a row with mouse-face was overwritten, arrange for
592 XTframe_up_to_date to redisplay the mouse highlight. */
593 if (mouse_face_overwritten_p)
595 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
596 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
597 dpyinfo->mouse_face_window = Qnil;
600 updated_window = NULL;
604 /* End update of frame F. This function is installed as a hook in
605 update_end. */
607 static void
608 x_update_end (f)
609 struct frame *f;
611 /* Mouse highlight may be displayed again. */
612 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
614 #ifndef XFlush
615 BLOCK_INPUT;
616 XFlush (FRAME_X_DISPLAY (f));
617 UNBLOCK_INPUT;
618 #endif
622 /* This function is called from various places in xdisp.c whenever a
623 complete update has been performed. The global variable
624 updated_window is not available here. */
626 static void
627 XTframe_up_to_date (f)
628 struct frame *f;
630 if (FRAME_X_P (f))
632 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
634 if (dpyinfo->mouse_face_deferred_gc
635 || f == dpyinfo->mouse_face_mouse_frame)
637 BLOCK_INPUT;
638 if (dpyinfo->mouse_face_mouse_frame)
639 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
640 dpyinfo->mouse_face_mouse_x,
641 dpyinfo->mouse_face_mouse_y);
642 dpyinfo->mouse_face_deferred_gc = 0;
643 UNBLOCK_INPUT;
649 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
650 arrow bitmaps, or clear the fringes if no bitmaps are required
651 before DESIRED_ROW is made current. The window being updated is
652 found in updated_window. This function It is called from
653 update_window_line only if it is known that there are differences
654 between bitmaps to be drawn between current row and DESIRED_ROW. */
656 static void
657 x_after_update_window_line (desired_row)
658 struct glyph_row *desired_row;
660 struct window *w = updated_window;
661 struct frame *f;
662 int width, height;
664 xassert (w);
666 if (!desired_row->mode_line_p && !w->pseudo_window_p)
667 desired_row->redraw_fringe_bitmaps_p = 1;
669 /* When a window has disappeared, make sure that no rest of
670 full-width rows stays visible in the internal border. Could
671 check here if updated_window is the leftmost/rightmost window,
672 but I guess it's not worth doing since vertically split windows
673 are almost never used, internal border is rarely set, and the
674 overhead is very small. */
675 if (windows_or_buffers_changed
676 && desired_row->full_width_p
677 && (f = XFRAME (w->frame),
678 width = FRAME_INTERNAL_BORDER_WIDTH (f),
679 width != 0)
680 && (height = desired_row->visible_height,
681 height > 0))
683 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
685 /* Internal border is drawn below the tool bar. */
686 if (WINDOWP (f->tool_bar_window)
687 && w == XWINDOW (f->tool_bar_window))
688 y -= width;
690 BLOCK_INPUT;
691 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
692 0, y, width, height, False);
693 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
694 FRAME_PIXEL_WIDTH (f) - width,
695 y, width, height, False);
696 UNBLOCK_INPUT;
700 static void
701 x_draw_fringe_bitmap (w, row, p)
702 struct window *w;
703 struct glyph_row *row;
704 struct draw_fringe_bitmap_params *p;
706 struct frame *f = XFRAME (WINDOW_FRAME (w));
707 Display *display = FRAME_X_DISPLAY (f);
708 Window window = FRAME_X_WINDOW (f);
709 GC gc = f->output_data.x->normal_gc;
710 struct face *face = p->face;
711 int rowY;
713 /* Must clip because of partially visible lines. */
714 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
715 if (p->y < rowY)
717 /* Adjust position of "bottom aligned" bitmap on partially
718 visible last row. */
719 int oldY = row->y;
720 int oldVH = row->visible_height;
721 row->visible_height = p->h;
722 row->y -= rowY - p->y;
723 x_clip_to_row (w, row, -1, gc);
724 row->y = oldY;
725 row->visible_height = oldVH;
727 else
728 x_clip_to_row (w, row, -1, gc);
730 if (!p->overlay_p)
732 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
734 /* In case the same realized face is used for fringes and
735 for something displayed in the text (e.g. face `region' on
736 mono-displays, the fill style may have been changed to
737 FillSolid in x_draw_glyph_string_background. */
738 if (face->stipple)
739 XSetFillStyle (display, face->gc, FillOpaqueStippled);
740 else
741 XSetForeground (display, face->gc, face->background);
743 #ifdef USE_TOOLKIT_SCROLL_BARS
744 /* If the fringe is adjacent to the left (right) scroll bar of a
745 leftmost (rightmost, respectively) window, then extend its
746 background to the gap between the fringe and the bar. */
747 if ((WINDOW_LEFTMOST_P (w)
748 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
749 || (WINDOW_RIGHTMOST_P (w)
750 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
752 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
754 if (sb_width > 0)
756 int left = WINDOW_SCROLL_BAR_AREA_X (w);
757 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
758 * FRAME_COLUMN_WIDTH (f));
760 if (bx < 0)
762 /* Bitmap fills the fringe. */
763 if (left + width == p->x)
764 bx = left + sb_width;
765 else if (p->x + p->wd == left)
766 bx = left;
767 if (bx >= 0)
769 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
771 nx = width - sb_width;
772 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
773 row->y));
774 ny = row->visible_height;
777 else
779 if (left + width == bx)
781 bx = left + sb_width;
782 nx += width - sb_width;
784 else if (bx + nx == left)
785 nx += width - sb_width;
789 #endif
790 if (bx >= 0 && nx > 0)
791 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
793 if (!face->stipple)
794 XSetForeground (display, face->gc, face->foreground);
797 if (p->which)
799 unsigned char *bits;
800 Pixmap pixmap, clipmask = (Pixmap) 0;
801 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
802 XGCValues gcv;
804 if (p->wd > 8)
805 bits = (unsigned char *)(p->bits + p->dh);
806 else
807 bits = (unsigned char *)p->bits + p->dh;
809 /* Draw the bitmap. I believe these small pixmaps can be cached
810 by the server. */
811 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
812 (p->cursor_p
813 ? (p->overlay_p ? face->background
814 : f->output_data.x->cursor_pixel)
815 : face->foreground),
816 face->background, depth);
818 if (p->overlay_p)
820 clipmask = XCreatePixmapFromBitmapData (display,
821 FRAME_X_DISPLAY_INFO (f)->root_window,
822 bits, p->wd, p->h,
823 1, 0, 1);
824 gcv.clip_mask = clipmask;
825 gcv.clip_x_origin = p->x;
826 gcv.clip_y_origin = p->y;
827 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
830 XCopyArea (display, pixmap, window, gc, 0, 0,
831 p->wd, p->h, p->x, p->y);
832 XFreePixmap (display, pixmap);
834 if (p->overlay_p)
836 gcv.clip_mask = (Pixmap) 0;
837 XChangeGC (display, gc, GCClipMask, &gcv);
838 XFreePixmap (display, clipmask);
842 XSetClipMask (display, gc, None);
847 /* This is called when starting Emacs and when restarting after
848 suspend. When starting Emacs, no X window is mapped. And nothing
849 must be done to Emacs's own window if it is suspended (though that
850 rarely happens). */
852 static void
853 XTset_terminal_modes (struct terminal *terminal)
857 /* This is called when exiting or suspending Emacs. Exiting will make
858 the X-windows go away, and suspending requires no action. */
860 static void
861 XTreset_terminal_modes (struct terminal *terminal)
866 /***********************************************************************
867 Glyph display
868 ***********************************************************************/
872 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
873 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
874 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
875 int));
876 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
877 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
878 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
879 static void x_draw_glyph_string P_ ((struct glyph_string *));
880 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
881 static void x_set_cursor_gc P_ ((struct glyph_string *));
882 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
883 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
884 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
885 unsigned long *, double, int));
886 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
887 double, int, unsigned long));
888 static void x_setup_relief_colors P_ ((struct glyph_string *));
889 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
890 static void x_draw_image_relief P_ ((struct glyph_string *));
891 static void x_draw_image_foreground P_ ((struct glyph_string *));
892 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
893 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
894 int, int, int));
895 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
896 int, int, int, int, int, int,
897 XRectangle *));
898 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
899 int, int, int, XRectangle *));
901 #if GLYPH_DEBUG
902 static void x_check_font P_ ((struct frame *, struct font *));
903 #endif
906 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
907 face. */
909 static void
910 x_set_cursor_gc (s)
911 struct glyph_string *s;
913 if (s->font == FRAME_FONT (s->f)
914 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
915 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
916 && !s->cmp)
917 s->gc = s->f->output_data.x->cursor_gc;
918 else
920 /* Cursor on non-default face: must merge. */
921 XGCValues xgcv;
922 unsigned long mask;
924 xgcv.background = s->f->output_data.x->cursor_pixel;
925 xgcv.foreground = s->face->background;
927 /* If the glyph would be invisible, try a different foreground. */
928 if (xgcv.foreground == xgcv.background)
929 xgcv.foreground = s->face->foreground;
930 if (xgcv.foreground == xgcv.background)
931 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
932 if (xgcv.foreground == xgcv.background)
933 xgcv.foreground = s->face->foreground;
935 /* Make sure the cursor is distinct from text in this face. */
936 if (xgcv.background == s->face->background
937 && xgcv.foreground == s->face->foreground)
939 xgcv.background = s->face->foreground;
940 xgcv.foreground = s->face->background;
943 IF_DEBUG (x_check_font (s->f, s->font));
944 xgcv.graphics_exposures = False;
945 mask = GCForeground | GCBackground | GCGraphicsExposures;
947 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
948 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
949 mask, &xgcv);
950 else
951 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
952 = XCreateGC (s->display, s->window, mask, &xgcv);
954 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
959 /* Set up S->gc of glyph string S for drawing text in mouse face. */
961 static void
962 x_set_mouse_face_gc (s)
963 struct glyph_string *s;
965 int face_id;
966 struct face *face;
968 /* What face has to be used last for the mouse face? */
969 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
970 face = FACE_FROM_ID (s->f, face_id);
971 if (face == NULL)
972 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
974 if (s->first_glyph->type == CHAR_GLYPH)
975 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
976 else
977 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
978 s->face = FACE_FROM_ID (s->f, face_id);
979 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
981 if (s->font == s->face->font)
982 s->gc = s->face->gc;
983 else
985 /* Otherwise construct scratch_cursor_gc with values from FACE
986 except for FONT. */
987 XGCValues xgcv;
988 unsigned long mask;
990 xgcv.background = s->face->background;
991 xgcv.foreground = s->face->foreground;
992 xgcv.graphics_exposures = False;
993 mask = GCForeground | GCBackground | GCGraphicsExposures;
995 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
996 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
997 mask, &xgcv);
998 else
999 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1000 = XCreateGC (s->display, s->window, mask, &xgcv);
1002 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1005 xassert (s->gc != 0);
1009 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1010 Faces to use in the mode line have already been computed when the
1011 matrix was built, so there isn't much to do, here. */
1013 static INLINE void
1014 x_set_mode_line_face_gc (s)
1015 struct glyph_string *s;
1017 s->gc = s->face->gc;
1021 /* Set S->gc of glyph string S for drawing that glyph string. Set
1022 S->stippled_p to a non-zero value if the face of S has a stipple
1023 pattern. */
1025 static INLINE void
1026 x_set_glyph_string_gc (s)
1027 struct glyph_string *s;
1029 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1031 if (s->hl == DRAW_NORMAL_TEXT)
1033 s->gc = s->face->gc;
1034 s->stippled_p = s->face->stipple != 0;
1036 else if (s->hl == DRAW_INVERSE_VIDEO)
1038 x_set_mode_line_face_gc (s);
1039 s->stippled_p = s->face->stipple != 0;
1041 else if (s->hl == DRAW_CURSOR)
1043 x_set_cursor_gc (s);
1044 s->stippled_p = 0;
1046 else if (s->hl == DRAW_MOUSE_FACE)
1048 x_set_mouse_face_gc (s);
1049 s->stippled_p = s->face->stipple != 0;
1051 else if (s->hl == DRAW_IMAGE_RAISED
1052 || s->hl == DRAW_IMAGE_SUNKEN)
1054 s->gc = s->face->gc;
1055 s->stippled_p = s->face->stipple != 0;
1057 else
1059 s->gc = s->face->gc;
1060 s->stippled_p = s->face->stipple != 0;
1063 /* GC must have been set. */
1064 xassert (s->gc != 0);
1068 /* Set clipping for output of glyph string S. S may be part of a mode
1069 line or menu if we don't have X toolkit support. */
1071 static INLINE void
1072 x_set_glyph_string_clipping (s)
1073 struct glyph_string *s;
1075 XRectangle *r = s->clip;
1076 int n = get_glyph_string_clip_rects (s, r, 2);
1078 if (n > 0)
1079 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
1080 s->num_clips = n;
1084 /* Set SRC's clipping for output of glyph string DST. This is called
1085 when we are drawing DST's left_overhang or right_overhang only in
1086 the area of SRC. */
1088 static void
1089 x_set_glyph_string_clipping_exactly (src, dst)
1090 struct glyph_string *src, *dst;
1092 XRectangle r;
1094 r.x = src->x;
1095 r.width = src->width;
1096 r.y = src->y;
1097 r.height = src->height;
1098 dst->clip[0] = r;
1099 dst->num_clips = 1;
1100 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1104 /* RIF:
1105 Compute left and right overhang of glyph string S. */
1107 static void
1108 x_compute_glyph_string_overhangs (s)
1109 struct glyph_string *s;
1111 if (s->cmp == NULL
1112 && s->first_glyph->type == CHAR_GLYPH)
1114 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1115 struct font *font = s->font;
1116 struct font_metrics metrics;
1117 int i;
1119 for (i = 0; i < s->nchars; i++)
1120 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1121 font->driver->text_extents (font, code, s->nchars, &metrics);
1122 s->right_overhang = (metrics.rbearing > metrics.width
1123 ? metrics.rbearing - metrics.width : 0);
1124 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1126 else if (s->cmp)
1128 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1129 s->left_overhang = - s->cmp->lbearing;
1134 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1136 static INLINE void
1137 x_clear_glyph_string_rect (s, x, y, w, h)
1138 struct glyph_string *s;
1139 int x, y, w, h;
1141 XGCValues xgcv;
1142 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1143 XSetForeground (s->display, s->gc, xgcv.background);
1144 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1145 XSetForeground (s->display, s->gc, xgcv.foreground);
1149 /* Draw the background of glyph_string S. If S->background_filled_p
1150 is non-zero don't draw it. FORCE_P non-zero means draw the
1151 background even if it wouldn't be drawn normally. This is used
1152 when a string preceding S draws into the background of S, or S
1153 contains the first component of a composition. */
1155 static void
1156 x_draw_glyph_string_background (s, force_p)
1157 struct glyph_string *s;
1158 int force_p;
1160 /* Nothing to do if background has already been drawn or if it
1161 shouldn't be drawn in the first place. */
1162 if (!s->background_filled_p)
1164 int box_line_width = max (s->face->box_line_width, 0);
1166 if (s->stippled_p)
1168 /* Fill background with a stipple pattern. */
1169 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1170 XFillRectangle (s->display, s->window, s->gc, s->x,
1171 s->y + box_line_width,
1172 s->background_width,
1173 s->height - 2 * box_line_width);
1174 XSetFillStyle (s->display, s->gc, FillSolid);
1175 s->background_filled_p = 1;
1177 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1178 || s->font_not_found_p
1179 || s->extends_to_end_of_line_p
1180 || force_p)
1182 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1183 s->background_width,
1184 s->height - 2 * box_line_width);
1185 s->background_filled_p = 1;
1191 /* Draw the foreground of glyph string S. */
1193 static void
1194 x_draw_glyph_string_foreground (s)
1195 struct glyph_string *s;
1197 int i, x;
1199 /* If first glyph of S has a left box line, start drawing the text
1200 of S to the right of that box line. */
1201 if (s->face->box != FACE_NO_BOX
1202 && s->first_glyph->left_box_line_p)
1203 x = s->x + eabs (s->face->box_line_width);
1204 else
1205 x = s->x;
1207 /* Draw characters of S as rectangles if S's font could not be
1208 loaded. */
1209 if (s->font_not_found_p)
1211 for (i = 0; i < s->nchars; ++i)
1213 struct glyph *g = s->first_glyph + i;
1214 XDrawRectangle (s->display, s->window,
1215 s->gc, x, s->y, g->pixel_width - 1,
1216 s->height - 1);
1217 x += g->pixel_width;
1220 else
1222 struct font *font = s->font;
1223 int boff = font->baseline_offset;
1224 int y;
1226 if (font->vertical_centering)
1227 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1229 y = s->ybase - boff;
1230 if (s->for_overlaps
1231 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1232 font->driver->draw (s, 0, s->nchars, x, y, 0);
1233 else
1234 font->driver->draw (s, 0, s->nchars, x, y, 1);
1235 if (s->face->overstrike)
1236 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1240 /* Draw the foreground of composite glyph string S. */
1242 static void
1243 x_draw_composite_glyph_string_foreground (s)
1244 struct glyph_string *s;
1246 int i, j, x;
1248 /* If first glyph of S has a left box line, start drawing the text
1249 of S to the right of that box line. */
1250 if (s->face && s->face->box != FACE_NO_BOX
1251 && s->first_glyph->left_box_line_p)
1252 x = s->x + eabs (s->face->box_line_width);
1253 else
1254 x = s->x;
1256 /* S is a glyph string for a composition. S->gidx is the index of
1257 the first character drawn for glyphs of this composition.
1258 S->gidx == 0 means we are drawing the very first character of
1259 this composition. */
1261 /* Draw a rectangle for the composition if the font for the very
1262 first character of the composition could not be loaded. */
1263 if (s->font_not_found_p)
1265 if (s->gidx == 0)
1266 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1267 s->width - 1, s->height - 1);
1269 else
1271 struct font *font = s->font;
1272 int y = s->ybase;
1273 int width = 0;
1275 if (s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
1277 Lisp_Object gstring = AREF (XHASH_TABLE (composition_hash_table)
1278 ->key_and_value,
1279 s->cmp->hash_index * 2);
1280 int from;
1282 for (i = from = 0; i < s->nchars; i++)
1284 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
1285 Lisp_Object adjustment = LGLYPH_ADJUSTMENT (g);
1286 int xoff, yoff, wadjust;
1288 if (! VECTORP (adjustment))
1290 width += LGLYPH_WIDTH (g);
1291 continue;
1293 if (from < i)
1295 font->driver->draw (s, from, i, x, y, 0);
1296 x += width;
1298 xoff = XINT (AREF (adjustment, 0));
1299 yoff = XINT (AREF (adjustment, 1));
1300 wadjust = XINT (AREF (adjustment, 2));
1302 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1303 x += wadjust;
1304 from = i + 1;
1305 width = 0;
1307 if (from < i)
1308 font->driver->draw (s, from, i, x, y, 0);
1310 else
1312 for (i = 0, j = s->gidx; i < s->nchars; i++, j++)
1313 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1315 int xx = x + s->cmp->offsets[j * 2];
1316 int yy = y - s->cmp->offsets[j * 2 + 1];
1318 font->driver->draw (s, j, j + 1, xx, yy, 0);
1319 if (s->face->overstrike)
1320 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1327 #ifdef USE_X_TOOLKIT
1329 static struct frame *x_frame_of_widget P_ ((Widget));
1330 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1331 XrmValue *, XrmValue *, XtPointer *));
1332 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1333 XrmValue *, Cardinal *));
1336 /* Return the frame on which widget WIDGET is used.. Abort if frame
1337 cannot be determined. */
1339 static struct frame *
1340 x_frame_of_widget (widget)
1341 Widget widget;
1343 struct x_display_info *dpyinfo;
1344 Lisp_Object tail;
1345 struct frame *f;
1347 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1349 /* Find the top-level shell of the widget. Note that this function
1350 can be called when the widget is not yet realized, so XtWindow
1351 (widget) == 0. That's the reason we can't simply use
1352 x_any_window_to_frame. */
1353 while (!XtIsTopLevelShell (widget))
1354 widget = XtParent (widget);
1356 /* Look for a frame with that top-level widget. Allocate the color
1357 on that frame to get the right gamma correction value. */
1358 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1359 if (FRAMEP (XCAR (tail))
1360 && (f = XFRAME (XCAR (tail)),
1361 (FRAME_X_P (f)
1362 && f->output_data.nothing != 1
1363 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1364 && f->output_data.x->widget == widget)
1365 return f;
1367 abort ();
1371 /* Allocate the color COLOR->pixel on the screen and display of
1372 widget WIDGET in colormap CMAP. If an exact match cannot be
1373 allocated, try the nearest color available. Value is non-zero
1374 if successful. This is called from lwlib. */
1377 x_alloc_nearest_color_for_widget (widget, cmap, color)
1378 Widget widget;
1379 Colormap cmap;
1380 XColor *color;
1382 struct frame *f = x_frame_of_widget (widget);
1383 return x_alloc_nearest_color (f, cmap, color);
1387 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1388 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1389 If this produces the same color as PIXEL, try a color where all RGB
1390 values have DELTA added. Return the allocated color in *PIXEL.
1391 DISPLAY is the X display, CMAP is the colormap to operate on.
1392 Value is non-zero if successful. */
1395 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1396 Widget widget;
1397 Display *display;
1398 Colormap cmap;
1399 unsigned long *pixel;
1400 double factor;
1401 int delta;
1403 struct frame *f = x_frame_of_widget (widget);
1404 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1408 /* Structure specifying which arguments should be passed by Xt to
1409 cvt_string_to_pixel. We want the widget's screen and colormap. */
1411 static XtConvertArgRec cvt_string_to_pixel_args[] =
1413 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1414 sizeof (Screen *)},
1415 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1416 sizeof (Colormap)}
1420 /* The address of this variable is returned by
1421 cvt_string_to_pixel. */
1423 static Pixel cvt_string_to_pixel_value;
1426 /* Convert a color name to a pixel color.
1428 DPY is the display we are working on.
1430 ARGS is an array of *NARGS XrmValue structures holding additional
1431 information about the widget for which the conversion takes place.
1432 The contents of this array are determined by the specification
1433 in cvt_string_to_pixel_args.
1435 FROM is a pointer to an XrmValue which points to the color name to
1436 convert. TO is an XrmValue in which to return the pixel color.
1438 CLOSURE_RET is a pointer to user-data, in which we record if
1439 we allocated the color or not.
1441 Value is True if successful, False otherwise. */
1443 static Boolean
1444 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1445 Display *dpy;
1446 XrmValue *args;
1447 Cardinal *nargs;
1448 XrmValue *from, *to;
1449 XtPointer *closure_ret;
1451 Screen *screen;
1452 Colormap cmap;
1453 Pixel pixel;
1454 String color_name;
1455 XColor color;
1457 if (*nargs != 2)
1459 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1460 "wrongParameters", "cvt_string_to_pixel",
1461 "XtToolkitError",
1462 "Screen and colormap args required", NULL, NULL);
1463 return False;
1466 screen = *(Screen **) args[0].addr;
1467 cmap = *(Colormap *) args[1].addr;
1468 color_name = (String) from->addr;
1470 if (strcmp (color_name, XtDefaultBackground) == 0)
1472 *closure_ret = (XtPointer) False;
1473 pixel = WhitePixelOfScreen (screen);
1475 else if (strcmp (color_name, XtDefaultForeground) == 0)
1477 *closure_ret = (XtPointer) False;
1478 pixel = BlackPixelOfScreen (screen);
1480 else if (XParseColor (dpy, cmap, color_name, &color)
1481 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1483 pixel = color.pixel;
1484 *closure_ret = (XtPointer) True;
1486 else
1488 String params[1];
1489 Cardinal nparams = 1;
1491 params[0] = color_name;
1492 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1493 "badValue", "cvt_string_to_pixel",
1494 "XtToolkitError", "Invalid color `%s'",
1495 params, &nparams);
1496 return False;
1499 if (to->addr != NULL)
1501 if (to->size < sizeof (Pixel))
1503 to->size = sizeof (Pixel);
1504 return False;
1507 *(Pixel *) to->addr = pixel;
1509 else
1511 cvt_string_to_pixel_value = pixel;
1512 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1515 to->size = sizeof (Pixel);
1516 return True;
1520 /* Free a pixel color which was previously allocated via
1521 cvt_string_to_pixel. This is registered as the destructor
1522 for this type of resource via XtSetTypeConverter.
1524 APP is the application context in which we work.
1526 TO is a pointer to an XrmValue holding the color to free.
1527 CLOSURE is the value we stored in CLOSURE_RET for this color
1528 in cvt_string_to_pixel.
1530 ARGS and NARGS are like for cvt_string_to_pixel. */
1532 static void
1533 cvt_pixel_dtor (app, to, closure, args, nargs)
1534 XtAppContext app;
1535 XrmValuePtr to;
1536 XtPointer closure;
1537 XrmValuePtr args;
1538 Cardinal *nargs;
1540 if (*nargs != 2)
1542 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1543 "XtToolkitError",
1544 "Screen and colormap arguments required",
1545 NULL, NULL);
1547 else if (closure != NULL)
1549 /* We did allocate the pixel, so free it. */
1550 Screen *screen = *(Screen **) args[0].addr;
1551 Colormap cmap = *(Colormap *) args[1].addr;
1552 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1553 (Pixel *) to->addr, 1);
1558 #endif /* USE_X_TOOLKIT */
1561 /* Value is an array of XColor structures for the contents of the
1562 color map of display DPY. Set *NCELLS to the size of the array.
1563 Note that this probably shouldn't be called for large color maps,
1564 say a 24-bit TrueColor map. */
1566 static const XColor *
1567 x_color_cells (dpy, ncells)
1568 Display *dpy;
1569 int *ncells;
1571 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1573 if (dpyinfo->color_cells == NULL)
1575 Screen *screen = dpyinfo->screen;
1576 int i;
1578 dpyinfo->ncolor_cells
1579 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1580 dpyinfo->color_cells
1581 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1582 * sizeof *dpyinfo->color_cells);
1584 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1585 dpyinfo->color_cells[i].pixel = i;
1587 XQueryColors (dpy, dpyinfo->cmap,
1588 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1591 *ncells = dpyinfo->ncolor_cells;
1592 return dpyinfo->color_cells;
1596 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1597 colors in COLORS. Use cached information, if available. */
1599 void
1600 x_query_colors (f, colors, ncolors)
1601 struct frame *f;
1602 XColor *colors;
1603 int ncolors;
1605 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1607 if (dpyinfo->color_cells)
1609 int i;
1610 for (i = 0; i < ncolors; ++i)
1612 unsigned long pixel = colors[i].pixel;
1613 xassert (pixel < dpyinfo->ncolor_cells);
1614 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1615 colors[i] = dpyinfo->color_cells[pixel];
1618 else
1619 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1623 /* On frame F, translate pixel color to RGB values for the color in
1624 COLOR. Use cached information, if available. */
1626 void
1627 x_query_color (f, color)
1628 struct frame *f;
1629 XColor *color;
1631 x_query_colors (f, color, 1);
1635 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1636 exact match can't be allocated, try the nearest color available.
1637 Value is non-zero if successful. Set *COLOR to the color
1638 allocated. */
1640 static int
1641 x_alloc_nearest_color_1 (dpy, cmap, color)
1642 Display *dpy;
1643 Colormap cmap;
1644 XColor *color;
1646 int rc;
1648 rc = XAllocColor (dpy, cmap, color);
1649 if (rc == 0)
1651 /* If we got to this point, the colormap is full, so we're going
1652 to try to get the next closest color. The algorithm used is
1653 a least-squares matching, which is what X uses for closest
1654 color matching with StaticColor visuals. */
1655 int nearest, i;
1656 unsigned long nearest_delta = ~0;
1657 int ncells;
1658 const XColor *cells = x_color_cells (dpy, &ncells);
1660 for (nearest = i = 0; i < ncells; ++i)
1662 long dred = (color->red >> 8) - (cells[i].red >> 8);
1663 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1664 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1665 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1667 if (delta < nearest_delta)
1669 nearest = i;
1670 nearest_delta = delta;
1674 color->red = cells[nearest].red;
1675 color->green = cells[nearest].green;
1676 color->blue = cells[nearest].blue;
1677 rc = XAllocColor (dpy, cmap, color);
1679 else
1681 /* If allocation succeeded, and the allocated pixel color is not
1682 equal to a cached pixel color recorded earlier, there was a
1683 change in the colormap, so clear the color cache. */
1684 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1685 XColor *cached_color;
1687 if (dpyinfo->color_cells
1688 && (cached_color = &dpyinfo->color_cells[color->pixel],
1689 (cached_color->red != color->red
1690 || cached_color->blue != color->blue
1691 || cached_color->green != color->green)))
1693 xfree (dpyinfo->color_cells);
1694 dpyinfo->color_cells = NULL;
1695 dpyinfo->ncolor_cells = 0;
1699 #ifdef DEBUG_X_COLORS
1700 if (rc)
1701 register_color (color->pixel);
1702 #endif /* DEBUG_X_COLORS */
1704 return rc;
1708 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1709 exact match can't be allocated, try the nearest color available.
1710 Value is non-zero if successful. Set *COLOR to the color
1711 allocated. */
1714 x_alloc_nearest_color (f, cmap, color)
1715 struct frame *f;
1716 Colormap cmap;
1717 XColor *color;
1719 gamma_correct (f, color);
1720 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1724 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1725 It's necessary to do this instead of just using PIXEL directly to
1726 get color reference counts right. */
1728 unsigned long
1729 x_copy_color (f, pixel)
1730 struct frame *f;
1731 unsigned long pixel;
1733 XColor color;
1735 color.pixel = pixel;
1736 BLOCK_INPUT;
1737 x_query_color (f, &color);
1738 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1739 UNBLOCK_INPUT;
1740 #ifdef DEBUG_X_COLORS
1741 register_color (pixel);
1742 #endif
1743 return color.pixel;
1747 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1748 It's necessary to do this instead of just using PIXEL directly to
1749 get color reference counts right. */
1751 unsigned long
1752 x_copy_dpy_color (dpy, cmap, pixel)
1753 Display *dpy;
1754 Colormap cmap;
1755 unsigned long pixel;
1757 XColor color;
1759 color.pixel = pixel;
1760 BLOCK_INPUT;
1761 XQueryColor (dpy, cmap, &color);
1762 XAllocColor (dpy, cmap, &color);
1763 UNBLOCK_INPUT;
1764 #ifdef DEBUG_X_COLORS
1765 register_color (pixel);
1766 #endif
1767 return color.pixel;
1771 /* Brightness beyond which a color won't have its highlight brightness
1772 boosted.
1774 Nominally, highlight colors for `3d' faces are calculated by
1775 brightening an object's color by a constant scale factor, but this
1776 doesn't yield good results for dark colors, so for colors who's
1777 brightness is less than this value (on a scale of 0-65535) have an
1778 use an additional additive factor.
1780 The value here is set so that the default menu-bar/mode-line color
1781 (grey75) will not have its highlights changed at all. */
1782 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1785 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1786 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1787 If this produces the same color as PIXEL, try a color where all RGB
1788 values have DELTA added. Return the allocated color in *PIXEL.
1789 DISPLAY is the X display, CMAP is the colormap to operate on.
1790 Value is non-zero if successful. */
1792 static int
1793 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1794 struct frame *f;
1795 Display *display;
1796 Colormap cmap;
1797 unsigned long *pixel;
1798 double factor;
1799 int delta;
1801 XColor color, new;
1802 long bright;
1803 int success_p;
1805 /* Get RGB color values. */
1806 color.pixel = *pixel;
1807 x_query_color (f, &color);
1809 /* Change RGB values by specified FACTOR. Avoid overflow! */
1810 xassert (factor >= 0);
1811 new.red = min (0xffff, factor * color.red);
1812 new.green = min (0xffff, factor * color.green);
1813 new.blue = min (0xffff, factor * color.blue);
1815 /* Calculate brightness of COLOR. */
1816 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1818 /* We only boost colors that are darker than
1819 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1820 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1821 /* Make an additive adjustment to NEW, because it's dark enough so
1822 that scaling by FACTOR alone isn't enough. */
1824 /* How far below the limit this color is (0 - 1, 1 being darker). */
1825 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1826 /* The additive adjustment. */
1827 int min_delta = delta * dimness * factor / 2;
1829 if (factor < 1)
1831 new.red = max (0, new.red - min_delta);
1832 new.green = max (0, new.green - min_delta);
1833 new.blue = max (0, new.blue - min_delta);
1835 else
1837 new.red = min (0xffff, min_delta + new.red);
1838 new.green = min (0xffff, min_delta + new.green);
1839 new.blue = min (0xffff, min_delta + new.blue);
1843 /* Try to allocate the color. */
1844 success_p = x_alloc_nearest_color (f, cmap, &new);
1845 if (success_p)
1847 if (new.pixel == *pixel)
1849 /* If we end up with the same color as before, try adding
1850 delta to the RGB values. */
1851 x_free_colors (f, &new.pixel, 1);
1853 new.red = min (0xffff, delta + color.red);
1854 new.green = min (0xffff, delta + color.green);
1855 new.blue = min (0xffff, delta + color.blue);
1856 success_p = x_alloc_nearest_color (f, cmap, &new);
1858 else
1859 success_p = 1;
1860 *pixel = new.pixel;
1863 return success_p;
1867 /* Set up the foreground color for drawing relief lines of glyph
1868 string S. RELIEF is a pointer to a struct relief containing the GC
1869 with which lines will be drawn. Use a color that is FACTOR or
1870 DELTA lighter or darker than the relief's background which is found
1871 in S->f->output_data.x->relief_background. If such a color cannot
1872 be allocated, use DEFAULT_PIXEL, instead. */
1874 static void
1875 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1876 struct frame *f;
1877 struct relief *relief;
1878 double factor;
1879 int delta;
1880 unsigned long default_pixel;
1882 XGCValues xgcv;
1883 struct x_output *di = f->output_data.x;
1884 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1885 unsigned long pixel;
1886 unsigned long background = di->relief_background;
1887 Colormap cmap = FRAME_X_COLORMAP (f);
1888 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1889 Display *dpy = FRAME_X_DISPLAY (f);
1891 xgcv.graphics_exposures = False;
1892 xgcv.line_width = 1;
1894 /* Free previously allocated color. The color cell will be reused
1895 when it has been freed as many times as it was allocated, so this
1896 doesn't affect faces using the same colors. */
1897 if (relief->gc
1898 && relief->allocated_p)
1900 x_free_colors (f, &relief->pixel, 1);
1901 relief->allocated_p = 0;
1904 /* Allocate new color. */
1905 xgcv.foreground = default_pixel;
1906 pixel = background;
1907 if (dpyinfo->n_planes != 1
1908 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1910 relief->allocated_p = 1;
1911 xgcv.foreground = relief->pixel = pixel;
1914 if (relief->gc == 0)
1916 xgcv.stipple = dpyinfo->gray;
1917 mask |= GCStipple;
1918 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1920 else
1921 XChangeGC (dpy, relief->gc, mask, &xgcv);
1925 /* Set up colors for the relief lines around glyph string S. */
1927 static void
1928 x_setup_relief_colors (s)
1929 struct glyph_string *s;
1931 struct x_output *di = s->f->output_data.x;
1932 unsigned long color;
1934 if (s->face->use_box_color_for_shadows_p)
1935 color = s->face->box_color;
1936 else if (s->first_glyph->type == IMAGE_GLYPH
1937 && s->img->pixmap
1938 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1939 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1940 else
1942 XGCValues xgcv;
1944 /* Get the background color of the face. */
1945 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
1946 color = xgcv.background;
1949 if (di->white_relief.gc == 0
1950 || color != di->relief_background)
1952 di->relief_background = color;
1953 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1954 WHITE_PIX_DEFAULT (s->f));
1955 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1956 BLACK_PIX_DEFAULT (s->f));
1961 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1962 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1963 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1964 relief. LEFT_P non-zero means draw a relief on the left side of
1965 the rectangle. RIGHT_P non-zero means draw a relief on the right
1966 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1967 when drawing. */
1969 static void
1970 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
1971 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
1972 struct frame *f;
1973 int left_x, top_y, right_x, bottom_y, width;
1974 int top_p, bot_p, left_p, right_p, raised_p;
1975 XRectangle *clip_rect;
1977 Display *dpy = FRAME_X_DISPLAY (f);
1978 Window window = FRAME_X_WINDOW (f);
1979 int i;
1980 GC gc;
1982 if (raised_p)
1983 gc = f->output_data.x->white_relief.gc;
1984 else
1985 gc = f->output_data.x->black_relief.gc;
1986 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1988 /* Top. */
1989 if (top_p)
1990 for (i = 0; i < width; ++i)
1991 XDrawLine (dpy, window, gc,
1992 left_x + i * left_p, top_y + i,
1993 right_x + 1 - i * right_p, top_y + i);
1995 /* Left. */
1996 if (left_p)
1997 for (i = 0; i < width; ++i)
1998 XDrawLine (dpy, window, gc,
1999 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2001 XSetClipMask (dpy, gc, None);
2002 if (raised_p)
2003 gc = f->output_data.x->black_relief.gc;
2004 else
2005 gc = f->output_data.x->white_relief.gc;
2006 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2008 /* Bottom. */
2009 if (bot_p)
2010 for (i = 0; i < width; ++i)
2011 XDrawLine (dpy, window, gc,
2012 left_x + i * left_p, bottom_y - i,
2013 right_x + 1 - i * right_p, bottom_y - i);
2015 /* Right. */
2016 if (right_p)
2017 for (i = 0; i < width; ++i)
2018 XDrawLine (dpy, window, gc,
2019 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2021 XSetClipMask (dpy, gc, None);
2025 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2026 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2027 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2028 left side of the rectangle. RIGHT_P non-zero means draw a line
2029 on the right side of the rectangle. CLIP_RECT is the clipping
2030 rectangle to use when drawing. */
2032 static void
2033 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2034 left_p, right_p, clip_rect)
2035 struct glyph_string *s;
2036 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2037 XRectangle *clip_rect;
2039 XGCValues xgcv;
2041 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2042 XSetForeground (s->display, s->gc, s->face->box_color);
2043 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2045 /* Top. */
2046 XFillRectangle (s->display, s->window, s->gc,
2047 left_x, top_y, right_x - left_x + 1, width);
2049 /* Left. */
2050 if (left_p)
2051 XFillRectangle (s->display, s->window, s->gc,
2052 left_x, top_y, width, bottom_y - top_y + 1);
2054 /* Bottom. */
2055 XFillRectangle (s->display, s->window, s->gc,
2056 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2058 /* Right. */
2059 if (right_p)
2060 XFillRectangle (s->display, s->window, s->gc,
2061 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2063 XSetForeground (s->display, s->gc, xgcv.foreground);
2064 XSetClipMask (s->display, s->gc, None);
2068 /* Draw a box around glyph string S. */
2070 static void
2071 x_draw_glyph_string_box (s)
2072 struct glyph_string *s;
2074 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2075 int left_p, right_p;
2076 struct glyph *last_glyph;
2077 XRectangle clip_rect;
2079 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2080 ? WINDOW_RIGHT_EDGE_X (s->w)
2081 : window_box_right (s->w, s->area));
2083 /* The glyph that may have a right box line. */
2084 last_glyph = (s->cmp || s->img
2085 ? s->first_glyph
2086 : s->first_glyph + s->nchars - 1);
2088 width = eabs (s->face->box_line_width);
2089 raised_p = s->face->box == FACE_RAISED_BOX;
2090 left_x = s->x;
2091 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2092 ? last_x - 1
2093 : min (last_x, s->x + s->background_width) - 1);
2094 top_y = s->y;
2095 bottom_y = top_y + s->height - 1;
2097 left_p = (s->first_glyph->left_box_line_p
2098 || (s->hl == DRAW_MOUSE_FACE
2099 && (s->prev == NULL
2100 || s->prev->hl != s->hl)));
2101 right_p = (last_glyph->right_box_line_p
2102 || (s->hl == DRAW_MOUSE_FACE
2103 && (s->next == NULL
2104 || s->next->hl != s->hl)));
2106 get_glyph_string_clip_rect (s, &clip_rect);
2108 if (s->face->box == FACE_SIMPLE_BOX)
2109 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2110 left_p, right_p, &clip_rect);
2111 else
2113 x_setup_relief_colors (s);
2114 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2115 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2120 /* Draw foreground of image glyph string S. */
2122 static void
2123 x_draw_image_foreground (s)
2124 struct glyph_string *s;
2126 int x = s->x;
2127 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2129 /* If first glyph of S has a left box line, start drawing it to the
2130 right of that line. */
2131 if (s->face->box != FACE_NO_BOX
2132 && s->first_glyph->left_box_line_p
2133 && s->slice.x == 0)
2134 x += eabs (s->face->box_line_width);
2136 /* If there is a margin around the image, adjust x- and y-position
2137 by that margin. */
2138 if (s->slice.x == 0)
2139 x += s->img->hmargin;
2140 if (s->slice.y == 0)
2141 y += s->img->vmargin;
2143 if (s->img->pixmap)
2145 if (s->img->mask)
2147 /* We can't set both a clip mask and use XSetClipRectangles
2148 because the latter also sets a clip mask. We also can't
2149 trust on the shape extension to be available
2150 (XShapeCombineRegion). So, compute the rectangle to draw
2151 manually. */
2152 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2153 | GCFunction);
2154 XGCValues xgcv;
2155 XRectangle clip_rect, image_rect, r;
2157 xgcv.clip_mask = s->img->mask;
2158 xgcv.clip_x_origin = x;
2159 xgcv.clip_y_origin = y;
2160 xgcv.function = GXcopy;
2161 XChangeGC (s->display, s->gc, mask, &xgcv);
2163 get_glyph_string_clip_rect (s, &clip_rect);
2164 image_rect.x = x;
2165 image_rect.y = y;
2166 image_rect.width = s->slice.width;
2167 image_rect.height = s->slice.height;
2168 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2169 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2170 s->slice.x + r.x - x, s->slice.y + r.y - y,
2171 r.width, r.height, r.x, r.y);
2173 else
2175 XRectangle clip_rect, image_rect, r;
2177 get_glyph_string_clip_rect (s, &clip_rect);
2178 image_rect.x = x;
2179 image_rect.y = y;
2180 image_rect.width = s->slice.width;
2181 image_rect.height = s->slice.height;
2182 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2183 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2184 s->slice.x + r.x - x, s->slice.y + r.y - y,
2185 r.width, r.height, r.x, r.y);
2187 /* When the image has a mask, we can expect that at
2188 least part of a mouse highlight or a block cursor will
2189 be visible. If the image doesn't have a mask, make
2190 a block cursor visible by drawing a rectangle around
2191 the image. I believe it's looking better if we do
2192 nothing here for mouse-face. */
2193 if (s->hl == DRAW_CURSOR)
2195 int r = s->img->relief;
2196 if (r < 0) r = -r;
2197 XDrawRectangle (s->display, s->window, s->gc,
2198 x - r, y - r,
2199 s->slice.width + r*2 - 1,
2200 s->slice.height + r*2 - 1);
2204 else
2205 /* Draw a rectangle if image could not be loaded. */
2206 XDrawRectangle (s->display, s->window, s->gc, x, y,
2207 s->slice.width - 1, s->slice.height - 1);
2211 /* Draw a relief around the image glyph string S. */
2213 static void
2214 x_draw_image_relief (s)
2215 struct glyph_string *s;
2217 int x0, y0, x1, y1, thick, raised_p;
2218 XRectangle r;
2219 int x = s->x;
2220 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2222 /* If first glyph of S has a left box line, start drawing it to the
2223 right of that line. */
2224 if (s->face->box != FACE_NO_BOX
2225 && s->first_glyph->left_box_line_p
2226 && s->slice.x == 0)
2227 x += eabs (s->face->box_line_width);
2229 /* If there is a margin around the image, adjust x- and y-position
2230 by that margin. */
2231 if (s->slice.x == 0)
2232 x += s->img->hmargin;
2233 if (s->slice.y == 0)
2234 y += s->img->vmargin;
2236 if (s->hl == DRAW_IMAGE_SUNKEN
2237 || s->hl == DRAW_IMAGE_RAISED)
2239 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2240 raised_p = s->hl == DRAW_IMAGE_RAISED;
2242 else
2244 thick = eabs (s->img->relief);
2245 raised_p = s->img->relief > 0;
2248 x0 = x - thick;
2249 y0 = y - thick;
2250 x1 = x + s->slice.width + thick - 1;
2251 y1 = y + s->slice.height + thick - 1;
2253 x_setup_relief_colors (s);
2254 get_glyph_string_clip_rect (s, &r);
2255 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2256 s->slice.y == 0,
2257 s->slice.y + s->slice.height == s->img->height,
2258 s->slice.x == 0,
2259 s->slice.x + s->slice.width == s->img->width,
2260 &r);
2264 /* Draw the foreground of image glyph string S to PIXMAP. */
2266 static void
2267 x_draw_image_foreground_1 (s, pixmap)
2268 struct glyph_string *s;
2269 Pixmap pixmap;
2271 int x = 0;
2272 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2274 /* If first glyph of S has a left box line, start drawing it to the
2275 right of that line. */
2276 if (s->face->box != FACE_NO_BOX
2277 && s->first_glyph->left_box_line_p
2278 && s->slice.x == 0)
2279 x += eabs (s->face->box_line_width);
2281 /* If there is a margin around the image, adjust x- and y-position
2282 by that margin. */
2283 if (s->slice.x == 0)
2284 x += s->img->hmargin;
2285 if (s->slice.y == 0)
2286 y += s->img->vmargin;
2288 if (s->img->pixmap)
2290 if (s->img->mask)
2292 /* We can't set both a clip mask and use XSetClipRectangles
2293 because the latter also sets a clip mask. We also can't
2294 trust on the shape extension to be available
2295 (XShapeCombineRegion). So, compute the rectangle to draw
2296 manually. */
2297 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2298 | GCFunction);
2299 XGCValues xgcv;
2301 xgcv.clip_mask = s->img->mask;
2302 xgcv.clip_x_origin = x - s->slice.x;
2303 xgcv.clip_y_origin = y - s->slice.y;
2304 xgcv.function = GXcopy;
2305 XChangeGC (s->display, s->gc, mask, &xgcv);
2307 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2308 s->slice.x, s->slice.y,
2309 s->slice.width, s->slice.height, x, y);
2310 XSetClipMask (s->display, s->gc, None);
2312 else
2314 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2315 s->slice.x, s->slice.y,
2316 s->slice.width, s->slice.height, x, y);
2318 /* When the image has a mask, we can expect that at
2319 least part of a mouse highlight or a block cursor will
2320 be visible. If the image doesn't have a mask, make
2321 a block cursor visible by drawing a rectangle around
2322 the image. I believe it's looking better if we do
2323 nothing here for mouse-face. */
2324 if (s->hl == DRAW_CURSOR)
2326 int r = s->img->relief;
2327 if (r < 0) r = -r;
2328 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2329 s->slice.width + r*2 - 1,
2330 s->slice.height + r*2 - 1);
2334 else
2335 /* Draw a rectangle if image could not be loaded. */
2336 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2337 s->slice.width - 1, s->slice.height - 1);
2341 /* Draw part of the background of glyph string S. X, Y, W, and H
2342 give the rectangle to draw. */
2344 static void
2345 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2346 struct glyph_string *s;
2347 int x, y, w, h;
2349 if (s->stippled_p)
2351 /* Fill background with a stipple pattern. */
2352 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2353 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2354 XSetFillStyle (s->display, s->gc, FillSolid);
2356 else
2357 x_clear_glyph_string_rect (s, x, y, w, h);
2361 /* Draw image glyph string S.
2363 s->y
2364 s->x +-------------------------
2365 | s->face->box
2367 | +-------------------------
2368 | | s->img->margin
2370 | | +-------------------
2371 | | | the image
2375 static void
2376 x_draw_image_glyph_string (s)
2377 struct glyph_string *s;
2379 int box_line_hwidth = eabs (s->face->box_line_width);
2380 int box_line_vwidth = max (s->face->box_line_width, 0);
2381 int height;
2382 Pixmap pixmap = None;
2384 height = s->height;
2385 if (s->slice.y == 0)
2386 height -= box_line_vwidth;
2387 if (s->slice.y + s->slice.height >= s->img->height)
2388 height -= box_line_vwidth;
2390 /* Fill background with face under the image. Do it only if row is
2391 taller than image or if image has a clip mask to reduce
2392 flickering. */
2393 s->stippled_p = s->face->stipple != 0;
2394 if (height > s->slice.height
2395 || s->img->hmargin
2396 || s->img->vmargin
2397 || s->img->mask
2398 || s->img->pixmap == 0
2399 || s->width != s->background_width)
2401 if (s->img->mask)
2403 /* Create a pixmap as large as the glyph string. Fill it
2404 with the background color. Copy the image to it, using
2405 its mask. Copy the temporary pixmap to the display. */
2406 Screen *screen = FRAME_X_SCREEN (s->f);
2407 int depth = DefaultDepthOfScreen (screen);
2409 /* Create a pixmap as large as the glyph string. */
2410 pixmap = XCreatePixmap (s->display, s->window,
2411 s->background_width,
2412 s->height, depth);
2414 /* Don't clip in the following because we're working on the
2415 pixmap. */
2416 XSetClipMask (s->display, s->gc, None);
2418 /* Fill the pixmap with the background color/stipple. */
2419 if (s->stippled_p)
2421 /* Fill background with a stipple pattern. */
2422 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2423 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2424 XFillRectangle (s->display, pixmap, s->gc,
2425 0, 0, s->background_width, s->height);
2426 XSetFillStyle (s->display, s->gc, FillSolid);
2427 XSetTSOrigin (s->display, s->gc, 0, 0);
2429 else
2431 XGCValues xgcv;
2432 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2433 &xgcv);
2434 XSetForeground (s->display, s->gc, xgcv.background);
2435 XFillRectangle (s->display, pixmap, s->gc,
2436 0, 0, s->background_width, s->height);
2437 XSetForeground (s->display, s->gc, xgcv.foreground);
2440 else
2442 int x = s->x;
2443 int y = s->y;
2445 if (s->first_glyph->left_box_line_p
2446 && s->slice.x == 0)
2447 x += box_line_hwidth;
2449 if (s->slice.y == 0)
2450 y += box_line_vwidth;
2452 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2455 s->background_filled_p = 1;
2458 /* Draw the foreground. */
2459 if (pixmap != None)
2461 x_draw_image_foreground_1 (s, pixmap);
2462 x_set_glyph_string_clipping (s);
2463 XCopyArea (s->display, pixmap, s->window, s->gc,
2464 0, 0, s->background_width, s->height, s->x, s->y);
2465 XFreePixmap (s->display, pixmap);
2467 else
2468 x_draw_image_foreground (s);
2470 /* If we must draw a relief around the image, do it. */
2471 if (s->img->relief
2472 || s->hl == DRAW_IMAGE_RAISED
2473 || s->hl == DRAW_IMAGE_SUNKEN)
2474 x_draw_image_relief (s);
2478 /* Draw stretch glyph string S. */
2480 static void
2481 x_draw_stretch_glyph_string (s)
2482 struct glyph_string *s;
2484 xassert (s->first_glyph->type == STRETCH_GLYPH);
2486 if (s->hl == DRAW_CURSOR
2487 && !x_stretch_cursor_p)
2489 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2490 as wide as the stretch glyph. */
2491 int width, background_width = s->background_width;
2492 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2494 if (x < left_x)
2496 background_width -= left_x - x;
2497 x = left_x;
2499 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2501 /* Draw cursor. */
2502 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2504 /* Clear rest using the GC of the original non-cursor face. */
2505 if (width < background_width)
2507 int y = s->y;
2508 int w = background_width - width, h = s->height;
2509 XRectangle r;
2510 GC gc;
2512 x += width;
2513 if (s->row->mouse_face_p
2514 && cursor_in_mouse_face_p (s->w))
2516 x_set_mouse_face_gc (s);
2517 gc = s->gc;
2519 else
2520 gc = s->face->gc;
2522 get_glyph_string_clip_rect (s, &r);
2523 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2525 if (s->face->stipple)
2527 /* Fill background with a stipple pattern. */
2528 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2529 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2530 XSetFillStyle (s->display, gc, FillSolid);
2532 else
2534 XGCValues xgcv;
2535 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2536 XSetForeground (s->display, gc, xgcv.background);
2537 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2538 XSetForeground (s->display, gc, xgcv.foreground);
2542 else if (!s->background_filled_p)
2544 int background_width = s->background_width;
2545 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2547 /* Don't draw into left margin, fringe or scrollbar area
2548 except for header line and mode line. */
2549 if (x < left_x && !s->row->mode_line_p)
2551 background_width -= left_x - x;
2552 x = left_x;
2554 if (background_width > 0)
2555 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2558 s->background_filled_p = 1;
2562 /* Draw glyph string S. */
2564 static void
2565 x_draw_glyph_string (s)
2566 struct glyph_string *s;
2568 int relief_drawn_p = 0;
2570 /* If S draws into the background of its successors, draw the
2571 background of the successors first so that S can draw into it.
2572 This makes S->next use XDrawString instead of XDrawImageString. */
2573 if (s->next && s->right_overhang && !s->for_overlaps)
2575 int width;
2576 struct glyph_string *next;
2578 for (width = 0, next = s->next; next;
2579 width += next->width, next = next->next)
2580 if (next->first_glyph->type != IMAGE_GLYPH)
2582 x_set_glyph_string_gc (next);
2583 x_set_glyph_string_clipping (next);
2584 x_draw_glyph_string_background (next, 1);
2585 next->num_clips = 0;
2589 /* Set up S->gc, set clipping and draw S. */
2590 x_set_glyph_string_gc (s);
2592 /* Draw relief (if any) in advance for char/composition so that the
2593 glyph string can be drawn over it. */
2594 if (!s->for_overlaps
2595 && s->face->box != FACE_NO_BOX
2596 && (s->first_glyph->type == CHAR_GLYPH
2597 || s->first_glyph->type == COMPOSITE_GLYPH))
2600 x_set_glyph_string_clipping (s);
2601 x_draw_glyph_string_background (s, 1);
2602 x_draw_glyph_string_box (s);
2603 x_set_glyph_string_clipping (s);
2604 relief_drawn_p = 1;
2606 else if ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2607 || (s->next && s->next->hl != s->hl && s->right_overhang))
2608 /* We must clip just this glyph. left_overhang part has already
2609 drawn when s->prev was drawn, and right_overhang part will be
2610 drawn later when s->next is drawn. */
2611 x_set_glyph_string_clipping_exactly (s, s);
2612 else
2613 x_set_glyph_string_clipping (s);
2615 switch (s->first_glyph->type)
2617 case IMAGE_GLYPH:
2618 x_draw_image_glyph_string (s);
2619 break;
2621 case STRETCH_GLYPH:
2622 x_draw_stretch_glyph_string (s);
2623 break;
2625 case CHAR_GLYPH:
2626 if (s->for_overlaps)
2627 s->background_filled_p = 1;
2628 else
2629 x_draw_glyph_string_background (s, 0);
2630 x_draw_glyph_string_foreground (s);
2631 break;
2633 case COMPOSITE_GLYPH:
2634 if (s->for_overlaps || s->gidx > 0)
2635 s->background_filled_p = 1;
2636 else
2637 x_draw_glyph_string_background (s, 1);
2638 x_draw_composite_glyph_string_foreground (s);
2639 break;
2641 default:
2642 abort ();
2645 if (!s->for_overlaps)
2647 /* Draw underline. */
2648 if (s->face->underline_p)
2650 unsigned long thickness, position;
2651 int y;
2653 if (s->prev && s->prev->face->underline_p)
2655 /* We use the same underline style as the previous one. */
2656 thickness = s->prev->underline_thickness;
2657 position = s->prev->underline_position;
2659 else
2661 /* Get the underline thickness. Default is 1 pixel. */
2662 if (s->font && s->font->underline_thickness > 0)
2663 thickness = s->font->underline_thickness;
2664 else
2665 thickness = 1;
2666 if (x_underline_at_descent_line)
2667 position = (s->height - thickness) - s->ybase;
2668 else
2670 /* Get the underline position. This is the recommended
2671 vertical offset in pixels from the baseline to the top of
2672 the underline. This is a signed value according to the
2673 specs, and its default is
2675 ROUND ((maximum descent) / 2), with
2676 ROUND(x) = floor (x + 0.5) */
2678 if (x_use_underline_position_properties
2679 && s->font && s->font->underline_position >= 0)
2680 position = s->font->underline_position;
2681 else if (s->font)
2682 position = (s->font->descent + 1) / 2;
2684 s->underline_thickness = thickness;
2685 s->underline_position = position;
2687 y = s->ybase + position;
2688 if (s->face->underline_defaulted_p)
2689 XFillRectangle (s->display, s->window, s->gc,
2690 s->x, y, s->background_width, thickness);
2691 else
2693 XGCValues xgcv;
2694 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2695 XSetForeground (s->display, s->gc, s->face->underline_color);
2696 XFillRectangle (s->display, s->window, s->gc,
2697 s->x, y, s->background_width, thickness);
2698 XSetForeground (s->display, s->gc, xgcv.foreground);
2702 /* Draw overline. */
2703 if (s->face->overline_p)
2705 unsigned long dy = 0, h = 1;
2707 if (s->face->overline_color_defaulted_p)
2708 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2709 s->background_width, h);
2710 else
2712 XGCValues xgcv;
2713 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2714 XSetForeground (s->display, s->gc, s->face->overline_color);
2715 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2716 s->background_width, h);
2717 XSetForeground (s->display, s->gc, xgcv.foreground);
2721 /* Draw strike-through. */
2722 if (s->face->strike_through_p)
2724 unsigned long h = 1;
2725 unsigned long dy = (s->height - h) / 2;
2727 if (s->face->strike_through_color_defaulted_p)
2728 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2729 s->width, h);
2730 else
2732 XGCValues xgcv;
2733 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2734 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2735 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2736 s->width, h);
2737 XSetForeground (s->display, s->gc, xgcv.foreground);
2741 /* Draw relief if not yet drawn. */
2742 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2743 x_draw_glyph_string_box (s);
2745 if (s->prev)
2747 struct glyph_string *prev;
2749 for (prev = s->prev; prev; prev = prev->prev)
2750 if (prev->hl != s->hl
2751 && prev->x + prev->width + prev->right_overhang > s->x)
2753 /* As prev was drawn while clipped to its own area, we
2754 must draw the right_overhang part using s->hl now. */
2755 enum draw_glyphs_face save = prev->hl;
2757 prev->hl = s->hl;
2758 x_set_glyph_string_gc (prev);
2759 x_set_glyph_string_clipping_exactly (s, prev);
2760 if (prev->first_glyph->type == CHAR_GLYPH)
2761 x_draw_glyph_string_foreground (prev);
2762 else
2763 x_draw_composite_glyph_string_foreground (prev);
2764 XSetClipMask (prev->display, prev->gc, None);
2765 prev->hl = save;
2766 prev->num_clips = 0;
2770 if (s->next)
2772 struct glyph_string *next;
2774 for (next = s->next; next; next = next->next)
2775 if (next->hl != s->hl
2776 && next->x - next->left_overhang < s->x + s->width)
2778 /* As next will be drawn while clipped to its own area,
2779 we must draw the left_overhang part using s->hl now. */
2780 enum draw_glyphs_face save = next->hl;
2782 next->hl = s->hl;
2783 x_set_glyph_string_gc (next);
2784 x_set_glyph_string_clipping_exactly (s, next);
2785 if (next->first_glyph->type == CHAR_GLYPH)
2786 x_draw_glyph_string_foreground (next);
2787 else
2788 x_draw_composite_glyph_string_foreground (next);
2789 XSetClipMask (next->display, next->gc, None);
2790 next->hl = save;
2791 next->num_clips = 0;
2796 /* Reset clipping. */
2797 XSetClipMask (s->display, s->gc, None);
2798 s->num_clips = 0;
2801 /* Shift display to make room for inserted glyphs. */
2803 void
2804 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2805 struct frame *f;
2806 int x, y, width, height, shift_by;
2808 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2809 f->output_data.x->normal_gc,
2810 x, y, width, height,
2811 x + shift_by, y);
2814 /* Delete N glyphs at the nominal cursor position. Not implemented
2815 for X frames. */
2817 static void
2818 x_delete_glyphs (f, n)
2819 struct frame *f;
2820 register int n;
2822 abort ();
2826 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2827 If they are <= 0, this is probably an error. */
2829 void
2830 x_clear_area (dpy, window, x, y, width, height, exposures)
2831 Display *dpy;
2832 Window window;
2833 int x, y;
2834 int width, height;
2835 int exposures;
2837 xassert (width > 0 && height > 0);
2838 XClearArea (dpy, window, x, y, width, height, exposures);
2842 /* Clear an entire frame. */
2844 static void
2845 x_clear_frame (struct frame *f)
2847 /* Clearing the frame will erase any cursor, so mark them all as no
2848 longer visible. */
2849 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2850 output_cursor.hpos = output_cursor.vpos = 0;
2851 output_cursor.x = -1;
2853 /* We don't set the output cursor here because there will always
2854 follow an explicit cursor_to. */
2855 BLOCK_INPUT;
2856 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2858 /* We have to clear the scroll bars, too. If we have changed
2859 colors or something like that, then they should be notified. */
2860 x_scroll_bar_clear (f);
2862 XFlush (FRAME_X_DISPLAY (f));
2864 UNBLOCK_INPUT;
2869 /* Invert the middle quarter of the frame for .15 sec. */
2871 /* We use the select system call to do the waiting, so we have to make
2872 sure it's available. If it isn't, we just won't do visual bells. */
2874 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2877 /* Subtract the `struct timeval' values X and Y, storing the result in
2878 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2880 static int
2881 timeval_subtract (result, x, y)
2882 struct timeval *result, x, y;
2884 /* Perform the carry for the later subtraction by updating y. This
2885 is safer because on some systems the tv_sec member is unsigned. */
2886 if (x.tv_usec < y.tv_usec)
2888 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2889 y.tv_usec -= 1000000 * nsec;
2890 y.tv_sec += nsec;
2893 if (x.tv_usec - y.tv_usec > 1000000)
2895 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2896 y.tv_usec += 1000000 * nsec;
2897 y.tv_sec -= nsec;
2900 /* Compute the time remaining to wait. tv_usec is certainly
2901 positive. */
2902 result->tv_sec = x.tv_sec - y.tv_sec;
2903 result->tv_usec = x.tv_usec - y.tv_usec;
2905 /* Return indication of whether the result should be considered
2906 negative. */
2907 return x.tv_sec < y.tv_sec;
2910 void
2911 XTflash (f)
2912 struct frame *f;
2914 BLOCK_INPUT;
2917 GC gc;
2919 /* Create a GC that will use the GXxor function to flip foreground
2920 pixels into background pixels. */
2922 XGCValues values;
2924 values.function = GXxor;
2925 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
2926 ^ FRAME_BACKGROUND_PIXEL (f));
2928 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2929 GCFunction | GCForeground, &values);
2933 /* Get the height not including a menu bar widget. */
2934 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
2935 /* Height of each line to flash. */
2936 int flash_height = FRAME_LINE_HEIGHT (f);
2937 /* These will be the left and right margins of the rectangles. */
2938 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
2939 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
2941 int width;
2943 /* Don't flash the area between a scroll bar and the frame
2944 edge it is next to. */
2945 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
2947 case vertical_scroll_bar_left:
2948 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2949 break;
2951 case vertical_scroll_bar_right:
2952 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2953 break;
2955 default:
2956 break;
2959 width = flash_right - flash_left;
2961 /* If window is tall, flash top and bottom line. */
2962 if (height > 3 * FRAME_LINE_HEIGHT (f))
2964 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2965 flash_left,
2966 (FRAME_INTERNAL_BORDER_WIDTH (f)
2967 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
2968 width, flash_height);
2969 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2970 flash_left,
2971 (height - flash_height
2972 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2973 width, flash_height);
2975 else
2976 /* If it is short, flash it all. */
2977 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2978 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2979 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2981 x_flush (f);
2984 struct timeval wakeup;
2986 EMACS_GET_TIME (wakeup);
2988 /* Compute time to wait until, propagating carry from usecs. */
2989 wakeup.tv_usec += 150000;
2990 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
2991 wakeup.tv_usec %= 1000000;
2993 /* Keep waiting until past the time wakeup or any input gets
2994 available. */
2995 while (! detect_input_pending ())
2997 struct timeval current;
2998 struct timeval timeout;
3000 EMACS_GET_TIME (current);
3002 /* Break if result would be negative. */
3003 if (timeval_subtract (&current, wakeup, current))
3004 break;
3006 /* How long `select' should wait. */
3007 timeout.tv_sec = 0;
3008 timeout.tv_usec = 10000;
3010 /* Try to wait that long--but we might wake up sooner. */
3011 select (0, NULL, NULL, NULL, &timeout);
3015 /* If window is tall, flash top and bottom line. */
3016 if (height > 3 * FRAME_LINE_HEIGHT (f))
3018 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3019 flash_left,
3020 (FRAME_INTERNAL_BORDER_WIDTH (f)
3021 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3022 width, flash_height);
3023 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3024 flash_left,
3025 (height - flash_height
3026 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3027 width, flash_height);
3029 else
3030 /* If it is short, flash it all. */
3031 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3032 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3033 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3035 XFreeGC (FRAME_X_DISPLAY (f), gc);
3036 x_flush (f);
3040 UNBLOCK_INPUT;
3043 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3046 /* Make audible bell. */
3048 void
3049 XTring_bell ()
3051 struct frame *f = SELECTED_FRAME ();
3053 if (FRAME_X_DISPLAY (f))
3055 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3056 if (visible_bell)
3057 XTflash (f);
3058 else
3059 #endif
3061 BLOCK_INPUT;
3062 XBell (FRAME_X_DISPLAY (f), 0);
3063 XFlush (FRAME_X_DISPLAY (f));
3064 UNBLOCK_INPUT;
3070 /* Specify how many text lines, from the top of the window,
3071 should be affected by insert-lines and delete-lines operations.
3072 This, and those operations, are used only within an update
3073 that is bounded by calls to x_update_begin and x_update_end. */
3075 static void
3076 XTset_terminal_window (n)
3077 register int n;
3079 /* This function intentionally left blank. */
3084 /***********************************************************************
3085 Line Dance
3086 ***********************************************************************/
3088 /* Perform an insert-lines or delete-lines operation, inserting N
3089 lines or deleting -N lines at vertical position VPOS. */
3091 static void
3092 x_ins_del_lines (f, vpos, n)
3093 struct frame *f;
3094 int vpos, n;
3096 abort ();
3100 /* Scroll part of the display as described by RUN. */
3102 static void
3103 x_scroll_run (w, run)
3104 struct window *w;
3105 struct run *run;
3107 struct frame *f = XFRAME (w->frame);
3108 int x, y, width, height, from_y, to_y, bottom_y;
3110 /* Get frame-relative bounding box of the text display area of W,
3111 without mode lines. Include in this box the left and right
3112 fringe of W. */
3113 window_box (w, -1, &x, &y, &width, &height);
3115 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3116 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3117 bottom_y = y + height;
3119 if (to_y < from_y)
3121 /* Scrolling up. Make sure we don't copy part of the mode
3122 line at the bottom. */
3123 if (from_y + run->height > bottom_y)
3124 height = bottom_y - from_y;
3125 else
3126 height = run->height;
3128 else
3130 /* Scolling down. Make sure we don't copy over the mode line.
3131 at the bottom. */
3132 if (to_y + run->height > bottom_y)
3133 height = bottom_y - to_y;
3134 else
3135 height = run->height;
3138 BLOCK_INPUT;
3140 /* Cursor off. Will be switched on again in x_update_window_end. */
3141 updated_window = w;
3142 x_clear_cursor (w);
3144 XCopyArea (FRAME_X_DISPLAY (f),
3145 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3146 f->output_data.x->normal_gc,
3147 x, from_y,
3148 width, height,
3149 x, to_y);
3151 UNBLOCK_INPUT;
3156 /***********************************************************************
3157 Exposure Events
3158 ***********************************************************************/
3161 static void
3162 frame_highlight (f)
3163 struct frame *f;
3165 /* We used to only do this if Vx_no_window_manager was non-nil, but
3166 the ICCCM (section 4.1.6) says that the window's border pixmap
3167 and border pixel are window attributes which are "private to the
3168 client", so we can always change it to whatever we want. */
3169 BLOCK_INPUT;
3170 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3171 f->output_data.x->border_pixel);
3172 UNBLOCK_INPUT;
3173 x_update_cursor (f, 1);
3176 static void
3177 frame_unhighlight (f)
3178 struct frame *f;
3180 /* We used to only do this if Vx_no_window_manager was non-nil, but
3181 the ICCCM (section 4.1.6) says that the window's border pixmap
3182 and border pixel are window attributes which are "private to the
3183 client", so we can always change it to whatever we want. */
3184 BLOCK_INPUT;
3185 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3186 f->output_data.x->border_tile);
3187 UNBLOCK_INPUT;
3188 x_update_cursor (f, 1);
3191 /* The focus has changed. Update the frames as necessary to reflect
3192 the new situation. Note that we can't change the selected frame
3193 here, because the Lisp code we are interrupting might become confused.
3194 Each event gets marked with the frame in which it occurred, so the
3195 Lisp code can tell when the switch took place by examining the events. */
3197 static void
3198 x_new_focus_frame (dpyinfo, frame)
3199 struct x_display_info *dpyinfo;
3200 struct frame *frame;
3202 struct frame *old_focus = dpyinfo->x_focus_frame;
3204 if (frame != dpyinfo->x_focus_frame)
3206 /* Set this before calling other routines, so that they see
3207 the correct value of x_focus_frame. */
3208 dpyinfo->x_focus_frame = frame;
3210 if (old_focus && old_focus->auto_lower)
3211 x_lower_frame (old_focus);
3213 #if 0
3214 selected_frame = frame;
3215 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3216 selected_frame);
3217 Fselect_window (selected_frame->selected_window, Qnil);
3218 choose_minibuf_frame ();
3219 #endif /* ! 0 */
3221 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3222 pending_autoraise_frame = dpyinfo->x_focus_frame;
3223 else
3224 pending_autoraise_frame = 0;
3227 x_frame_rehighlight (dpyinfo);
3230 /* Handle FocusIn and FocusOut state changes for FRAME.
3231 If FRAME has focus and there exists more than one frame, puts
3232 a FOCUS_IN_EVENT into *BUFP. */
3234 static void
3235 x_focus_changed (type, state, dpyinfo, frame, bufp)
3236 int type;
3237 int state;
3238 struct x_display_info *dpyinfo;
3239 struct frame *frame;
3240 struct input_event *bufp;
3242 if (type == FocusIn)
3244 if (dpyinfo->x_focus_event_frame != frame)
3246 x_new_focus_frame (dpyinfo, frame);
3247 dpyinfo->x_focus_event_frame = frame;
3249 /* Don't stop displaying the initial startup message
3250 for a switch-frame event we don't need. */
3251 if (NILP (Vterminal_frame)
3252 && CONSP (Vframe_list)
3253 && !NILP (XCDR (Vframe_list)))
3255 bufp->kind = FOCUS_IN_EVENT;
3256 XSETFRAME (bufp->frame_or_window, frame);
3260 frame->output_data.x->focus_state |= state;
3262 #ifdef HAVE_X_I18N
3263 if (FRAME_XIC (frame))
3264 XSetICFocus (FRAME_XIC (frame));
3265 #endif
3267 else if (type == FocusOut)
3269 frame->output_data.x->focus_state &= ~state;
3271 if (dpyinfo->x_focus_event_frame == frame)
3273 dpyinfo->x_focus_event_frame = 0;
3274 x_new_focus_frame (dpyinfo, 0);
3277 #ifdef HAVE_X_I18N
3278 if (FRAME_XIC (frame))
3279 XUnsetICFocus (FRAME_XIC (frame));
3280 #endif
3284 /* The focus may have changed. Figure out if it is a real focus change,
3285 by checking both FocusIn/Out and Enter/LeaveNotify events.
3287 Returns FOCUS_IN_EVENT event in *BUFP. */
3289 static void
3290 x_detect_focus_change (dpyinfo, event, bufp)
3291 struct x_display_info *dpyinfo;
3292 XEvent *event;
3293 struct input_event *bufp;
3295 struct frame *frame;
3297 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3298 if (! frame)
3299 return;
3301 switch (event->type)
3303 case EnterNotify:
3304 case LeaveNotify:
3306 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3307 int focus_state
3308 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3310 if (event->xcrossing.detail != NotifyInferior
3311 && event->xcrossing.focus
3312 && ! (focus_state & FOCUS_EXPLICIT))
3313 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3314 FOCUS_IMPLICIT,
3315 dpyinfo, frame, bufp);
3317 break;
3319 case FocusIn:
3320 case FocusOut:
3321 x_focus_changed (event->type,
3322 (event->xfocus.detail == NotifyPointer ?
3323 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3324 dpyinfo, frame, bufp);
3325 break;
3327 case ClientMessage:
3328 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3330 enum xembed_message msg = event->xclient.data.l[1];
3331 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3332 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3334 break;
3339 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3341 void
3342 x_mouse_leave (dpyinfo)
3343 struct x_display_info *dpyinfo;
3345 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3348 /* The focus has changed, or we have redirected a frame's focus to
3349 another frame (this happens when a frame uses a surrogate
3350 mini-buffer frame). Shift the highlight as appropriate.
3352 The FRAME argument doesn't necessarily have anything to do with which
3353 frame is being highlighted or un-highlighted; we only use it to find
3354 the appropriate X display info. */
3356 static void
3357 XTframe_rehighlight (frame)
3358 struct frame *frame;
3360 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3363 static void
3364 x_frame_rehighlight (dpyinfo)
3365 struct x_display_info *dpyinfo;
3367 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3369 if (dpyinfo->x_focus_frame)
3371 dpyinfo->x_highlight_frame
3372 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3373 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3374 : dpyinfo->x_focus_frame);
3375 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3377 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3378 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3381 else
3382 dpyinfo->x_highlight_frame = 0;
3384 if (dpyinfo->x_highlight_frame != old_highlight)
3386 if (old_highlight)
3387 frame_unhighlight (old_highlight);
3388 if (dpyinfo->x_highlight_frame)
3389 frame_highlight (dpyinfo->x_highlight_frame);
3395 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3397 /* Initialize mode_switch_bit and modifier_meaning. */
3398 static void
3399 x_find_modifier_meanings (dpyinfo)
3400 struct x_display_info *dpyinfo;
3402 int min_code, max_code;
3403 KeySym *syms;
3404 int syms_per_code;
3405 XModifierKeymap *mods;
3407 dpyinfo->meta_mod_mask = 0;
3408 dpyinfo->shift_lock_mask = 0;
3409 dpyinfo->alt_mod_mask = 0;
3410 dpyinfo->super_mod_mask = 0;
3411 dpyinfo->hyper_mod_mask = 0;
3413 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3415 syms = XGetKeyboardMapping (dpyinfo->display,
3416 min_code, max_code - min_code + 1,
3417 &syms_per_code);
3418 mods = XGetModifierMapping (dpyinfo->display);
3420 /* Scan the modifier table to see which modifier bits the Meta and
3421 Alt keysyms are on. */
3423 int row, col; /* The row and column in the modifier table. */
3424 int found_alt_or_meta;
3426 for (row = 3; row < 8; row++)
3428 found_alt_or_meta = 0;
3429 for (col = 0; col < mods->max_keypermod; col++)
3431 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3433 /* Zeroes are used for filler. Skip them. */
3434 if (code == 0)
3435 continue;
3437 /* Are any of this keycode's keysyms a meta key? */
3439 int code_col;
3441 for (code_col = 0; code_col < syms_per_code; code_col++)
3443 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3445 switch (sym)
3447 case XK_Meta_L:
3448 case XK_Meta_R:
3449 found_alt_or_meta = 1;
3450 dpyinfo->meta_mod_mask |= (1 << row);
3451 break;
3453 case XK_Alt_L:
3454 case XK_Alt_R:
3455 found_alt_or_meta = 1;
3456 dpyinfo->alt_mod_mask |= (1 << row);
3457 break;
3459 case XK_Hyper_L:
3460 case XK_Hyper_R:
3461 if (!found_alt_or_meta)
3462 dpyinfo->hyper_mod_mask |= (1 << row);
3463 code_col = syms_per_code;
3464 col = mods->max_keypermod;
3465 break;
3467 case XK_Super_L:
3468 case XK_Super_R:
3469 if (!found_alt_or_meta)
3470 dpyinfo->super_mod_mask |= (1 << row);
3471 code_col = syms_per_code;
3472 col = mods->max_keypermod;
3473 break;
3475 case XK_Shift_Lock:
3476 /* Ignore this if it's not on the lock modifier. */
3477 if (!found_alt_or_meta && ((1 << row) == LockMask))
3478 dpyinfo->shift_lock_mask = LockMask;
3479 code_col = syms_per_code;
3480 col = mods->max_keypermod;
3481 break;
3489 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3490 if (! dpyinfo->meta_mod_mask)
3492 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3493 dpyinfo->alt_mod_mask = 0;
3496 /* If some keys are both alt and meta,
3497 make them just meta, not alt. */
3498 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3500 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3503 XFree ((char *) syms);
3504 XFreeModifiermap (mods);
3507 /* Convert between the modifier bits X uses and the modifier bits
3508 Emacs uses. */
3510 unsigned int
3511 x_x_to_emacs_modifiers (dpyinfo, state)
3512 struct x_display_info *dpyinfo;
3513 unsigned int state;
3515 EMACS_UINT mod_meta = meta_modifier;
3516 EMACS_UINT mod_alt = alt_modifier;
3517 EMACS_UINT mod_hyper = hyper_modifier;
3518 EMACS_UINT mod_super = super_modifier;
3519 Lisp_Object tem;
3521 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3522 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3523 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3524 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3525 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3526 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3527 tem = Fget (Vx_super_keysym, Qmodifier_value);
3528 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3531 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3532 | ((state & ControlMask) ? ctrl_modifier : 0)
3533 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3534 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3535 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3536 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3539 static unsigned int
3540 x_emacs_to_x_modifiers (dpyinfo, state)
3541 struct x_display_info *dpyinfo;
3542 unsigned int state;
3544 EMACS_UINT mod_meta = meta_modifier;
3545 EMACS_UINT mod_alt = alt_modifier;
3546 EMACS_UINT mod_hyper = hyper_modifier;
3547 EMACS_UINT mod_super = super_modifier;
3549 Lisp_Object tem;
3551 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3552 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3553 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3554 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3555 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3556 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3557 tem = Fget (Vx_super_keysym, Qmodifier_value);
3558 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3561 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3562 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3563 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3564 | ((state & shift_modifier) ? ShiftMask : 0)
3565 | ((state & ctrl_modifier) ? ControlMask : 0)
3566 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3569 /* Convert a keysym to its name. */
3571 char *
3572 x_get_keysym_name (keysym)
3573 KeySym keysym;
3575 char *value;
3577 BLOCK_INPUT;
3578 value = XKeysymToString (keysym);
3579 UNBLOCK_INPUT;
3581 return value;
3586 /* Mouse clicks and mouse movement. Rah. */
3588 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3590 If the event is a button press, then note that we have grabbed
3591 the mouse. */
3593 static Lisp_Object
3594 construct_mouse_click (result, event, f)
3595 struct input_event *result;
3596 XButtonEvent *event;
3597 struct frame *f;
3599 /* Make the event type NO_EVENT; we'll change that when we decide
3600 otherwise. */
3601 result->kind = MOUSE_CLICK_EVENT;
3602 result->code = event->button - Button1;
3603 result->timestamp = event->time;
3604 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3605 event->state)
3606 | (event->type == ButtonRelease
3607 ? up_modifier
3608 : down_modifier));
3610 XSETINT (result->x, event->x);
3611 XSETINT (result->y, event->y);
3612 XSETFRAME (result->frame_or_window, f);
3613 result->arg = Qnil;
3614 return Qnil;
3618 /* Function to report a mouse movement to the mainstream Emacs code.
3619 The input handler calls this.
3621 We have received a mouse movement event, which is given in *event.
3622 If the mouse is over a different glyph than it was last time, tell
3623 the mainstream emacs code by setting mouse_moved. If not, ask for
3624 another motion event, so we can check again the next time it moves. */
3626 static XMotionEvent last_mouse_motion_event;
3627 static Lisp_Object last_mouse_motion_frame;
3629 static int
3630 note_mouse_movement (frame, event)
3631 FRAME_PTR frame;
3632 XMotionEvent *event;
3634 last_mouse_movement_time = event->time;
3635 last_mouse_motion_event = *event;
3636 XSETFRAME (last_mouse_motion_frame, frame);
3638 if (!FRAME_X_OUTPUT (frame))
3639 return 0;
3641 if (event->window != FRAME_X_WINDOW (frame))
3643 frame->mouse_moved = 1;
3644 last_mouse_scroll_bar = Qnil;
3645 note_mouse_highlight (frame, -1, -1);
3646 last_mouse_glyph_frame = 0;
3647 return 1;
3651 /* Has the mouse moved off the glyph it was on at the last sighting? */
3652 if (frame != last_mouse_glyph_frame
3653 || event->x < last_mouse_glyph.x
3654 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3655 || event->y < last_mouse_glyph.y
3656 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3658 frame->mouse_moved = 1;
3659 last_mouse_scroll_bar = Qnil;
3660 note_mouse_highlight (frame, event->x, event->y);
3661 /* Remember which glyph we're now on. */
3662 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3663 last_mouse_glyph_frame = frame;
3664 return 1;
3667 return 0;
3671 /************************************************************************
3672 Mouse Face
3673 ************************************************************************/
3675 static void
3676 redo_mouse_highlight ()
3678 if (!NILP (last_mouse_motion_frame)
3679 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3680 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3681 last_mouse_motion_event.x,
3682 last_mouse_motion_event.y);
3687 /* Return the current position of the mouse.
3688 *FP should be a frame which indicates which display to ask about.
3690 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3691 and *PART to the frame, window, and scroll bar part that the mouse
3692 is over. Set *X and *Y to the portion and whole of the mouse's
3693 position on the scroll bar.
3695 If the mouse movement started elsewhere, set *FP to the frame the
3696 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3697 the mouse is over.
3699 Set *TIME to the server time-stamp for the time at which the mouse
3700 was at this position.
3702 Don't store anything if we don't have a valid set of values to report.
3704 This clears the mouse_moved flag, so we can wait for the next mouse
3705 movement. */
3707 static void
3708 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3709 FRAME_PTR *fp;
3710 int insist;
3711 Lisp_Object *bar_window;
3712 enum scroll_bar_part *part;
3713 Lisp_Object *x, *y;
3714 unsigned long *time;
3716 FRAME_PTR f1;
3718 BLOCK_INPUT;
3720 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3721 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3722 else
3724 Window root;
3725 int root_x, root_y;
3727 Window dummy_window;
3728 int dummy;
3730 Lisp_Object frame, tail;
3732 /* Clear the mouse-moved flag for every frame on this display. */
3733 FOR_EACH_FRAME (tail, frame)
3734 if (FRAME_X_P (XFRAME (frame))
3735 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3736 XFRAME (frame)->mouse_moved = 0;
3738 last_mouse_scroll_bar = Qnil;
3740 /* Figure out which root window we're on. */
3741 XQueryPointer (FRAME_X_DISPLAY (*fp),
3742 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3744 /* The root window which contains the pointer. */
3745 &root,
3747 /* Trash which we can't trust if the pointer is on
3748 a different screen. */
3749 &dummy_window,
3751 /* The position on that root window. */
3752 &root_x, &root_y,
3754 /* More trash we can't trust. */
3755 &dummy, &dummy,
3757 /* Modifier keys and pointer buttons, about which
3758 we don't care. */
3759 (unsigned int *) &dummy);
3761 /* Now we have a position on the root; find the innermost window
3762 containing the pointer. */
3764 Window win, child;
3765 int win_x, win_y;
3766 int parent_x = 0, parent_y = 0;
3768 win = root;
3770 /* XTranslateCoordinates can get errors if the window
3771 structure is changing at the same time this function
3772 is running. So at least we must not crash from them. */
3774 x_catch_errors (FRAME_X_DISPLAY (*fp));
3776 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3777 && FRAME_LIVE_P (last_mouse_frame))
3779 /* If mouse was grabbed on a frame, give coords for that frame
3780 even if the mouse is now outside it. */
3781 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3783 /* From-window, to-window. */
3784 root, FRAME_X_WINDOW (last_mouse_frame),
3786 /* From-position, to-position. */
3787 root_x, root_y, &win_x, &win_y,
3789 /* Child of win. */
3790 &child);
3791 f1 = last_mouse_frame;
3793 else
3795 while (1)
3797 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3799 /* From-window, to-window. */
3800 root, win,
3802 /* From-position, to-position. */
3803 root_x, root_y, &win_x, &win_y,
3805 /* Child of win. */
3806 &child);
3808 if (child == None || child == win)
3809 break;
3811 win = child;
3812 parent_x = win_x;
3813 parent_y = win_y;
3816 /* Now we know that:
3817 win is the innermost window containing the pointer
3818 (XTC says it has no child containing the pointer),
3819 win_x and win_y are the pointer's position in it
3820 (XTC did this the last time through), and
3821 parent_x and parent_y are the pointer's position in win's parent.
3822 (They are what win_x and win_y were when win was child.
3823 If win is the root window, it has no parent, and
3824 parent_{x,y} are invalid, but that's okay, because we'll
3825 never use them in that case.) */
3827 /* Is win one of our frames? */
3828 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3830 #ifdef USE_X_TOOLKIT
3831 /* If we end up with the menu bar window, say it's not
3832 on the frame. */
3833 if (f1 != NULL
3834 && f1->output_data.x->menubar_widget
3835 && win == XtWindow (f1->output_data.x->menubar_widget))
3836 f1 = NULL;
3837 #endif /* USE_X_TOOLKIT */
3840 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3841 f1 = 0;
3843 x_uncatch_errors ();
3845 /* If not, is it one of our scroll bars? */
3846 if (! f1)
3848 struct scroll_bar *bar;
3850 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3852 if (bar)
3854 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3855 win_x = parent_x;
3856 win_y = parent_y;
3860 if (f1 == 0 && insist > 0)
3861 f1 = SELECTED_FRAME ();
3863 if (f1)
3865 /* Ok, we found a frame. Store all the values.
3866 last_mouse_glyph is a rectangle used to reduce the
3867 generation of mouse events. To not miss any motion
3868 events, we must divide the frame into rectangles of the
3869 size of the smallest character that could be displayed
3870 on it, i.e. into the same rectangles that matrices on
3871 the frame are divided into. */
3873 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
3874 last_mouse_glyph_frame = f1;
3876 *bar_window = Qnil;
3877 *part = 0;
3878 *fp = f1;
3879 XSETINT (*x, win_x);
3880 XSETINT (*y, win_y);
3881 *time = last_mouse_movement_time;
3886 UNBLOCK_INPUT;
3891 /***********************************************************************
3892 Scroll bars
3893 ***********************************************************************/
3895 /* Scroll bar support. */
3897 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
3898 manages it.
3899 This can be called in GC, so we have to make sure to strip off mark
3900 bits. */
3902 static struct scroll_bar *
3903 x_window_to_scroll_bar (display, window_id)
3904 Display *display;
3905 Window window_id;
3907 Lisp_Object tail;
3909 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3910 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
3911 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
3913 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
3915 Lisp_Object frame, bar, condemned;
3917 frame = XCAR (tail);
3918 /* All elements of Vframe_list should be frames. */
3919 if (! FRAMEP (frame))
3920 abort ();
3922 if (! FRAME_X_P (XFRAME (frame)))
3923 continue;
3925 /* Scan this frame's scroll bar list for a scroll bar with the
3926 right window ID. */
3927 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3928 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3929 /* This trick allows us to search both the ordinary and
3930 condemned scroll bar lists with one loop. */
3931 ! NILP (bar) || (bar = condemned,
3932 condemned = Qnil,
3933 ! NILP (bar));
3934 bar = XSCROLL_BAR (bar)->next)
3935 if (XSCROLL_BAR (bar)->x_window == window_id &&
3936 FRAME_X_DISPLAY (XFRAME (frame)) == display)
3937 return XSCROLL_BAR (bar);
3940 return 0;
3944 #if defined USE_LUCID
3946 /* Return the Lucid menu bar WINDOW is part of. Return null
3947 if WINDOW is not part of a menu bar. */
3949 static Widget
3950 x_window_to_menu_bar (window)
3951 Window window;
3953 Lisp_Object tail;
3955 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
3957 if (FRAME_X_P (XFRAME (XCAR (tail))))
3959 Lisp_Object frame = XCAR (tail);
3960 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
3962 if (menu_bar && xlwmenu_window_p (menu_bar, window))
3963 return menu_bar;
3967 return NULL;
3970 #endif /* USE_LUCID */
3973 /************************************************************************
3974 Toolkit scroll bars
3975 ************************************************************************/
3977 #ifdef USE_TOOLKIT_SCROLL_BARS
3979 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
3980 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
3981 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
3982 struct scroll_bar *));
3983 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
3984 int, int, int));
3987 /* Lisp window being scrolled. Set when starting to interact with
3988 a toolkit scroll bar, reset to nil when ending the interaction. */
3990 static Lisp_Object window_being_scrolled;
3992 /* Last scroll bar part sent in xm_scroll_callback. */
3994 static int last_scroll_bar_part;
3996 /* Whether this is an Xaw with arrow-scrollbars. This should imply
3997 that movements of 1/20 of the screen size are mapped to up/down. */
3999 #ifndef USE_GTK
4000 /* Id of action hook installed for scroll bars. */
4002 static XtActionHookId action_hook_id;
4004 static Boolean xaw3d_arrow_scroll;
4006 /* Whether the drag scrolling maintains the mouse at the top of the
4007 thumb. If not, resizing the thumb needs to be done more carefully
4008 to avoid jerkyness. */
4010 static Boolean xaw3d_pick_top;
4012 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4013 bars are used.. The hook is responsible for detecting when
4014 the user ends an interaction with the scroll bar, and generates
4015 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4017 static void
4018 xt_action_hook (widget, client_data, action_name, event, params,
4019 num_params)
4020 Widget widget;
4021 XtPointer client_data;
4022 String action_name;
4023 XEvent *event;
4024 String *params;
4025 Cardinal *num_params;
4027 int scroll_bar_p;
4028 char *end_action;
4030 #ifdef USE_MOTIF
4031 scroll_bar_p = XmIsScrollBar (widget);
4032 end_action = "Release";
4033 #else /* !USE_MOTIF i.e. use Xaw */
4034 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4035 end_action = "EndScroll";
4036 #endif /* USE_MOTIF */
4038 if (scroll_bar_p
4039 && strcmp (action_name, end_action) == 0
4040 && WINDOWP (window_being_scrolled))
4042 struct window *w;
4044 x_send_scroll_bar_event (window_being_scrolled,
4045 scroll_bar_end_scroll, 0, 0);
4046 w = XWINDOW (window_being_scrolled);
4048 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4050 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4051 /* The thumb size is incorrect while dragging: fix it. */
4052 set_vertical_scroll_bar (w);
4054 window_being_scrolled = Qnil;
4055 last_scroll_bar_part = -1;
4057 /* Xt timeouts no longer needed. */
4058 toolkit_scroll_bar_interaction = 0;
4061 #endif /* not USE_GTK */
4063 /* A vector of windows used for communication between
4064 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4066 static struct window **scroll_bar_windows;
4067 static int scroll_bar_windows_size;
4070 /* Send a client message with message type Xatom_Scrollbar for a
4071 scroll action to the frame of WINDOW. PART is a value identifying
4072 the part of the scroll bar that was clicked on. PORTION is the
4073 amount to scroll of a whole of WHOLE. */
4075 static void
4076 x_send_scroll_bar_event (window, part, portion, whole)
4077 Lisp_Object window;
4078 int part, portion, whole;
4080 XEvent event;
4081 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4082 struct window *w = XWINDOW (window);
4083 struct frame *f = XFRAME (w->frame);
4084 int i;
4086 BLOCK_INPUT;
4088 /* Construct a ClientMessage event to send to the frame. */
4089 ev->type = ClientMessage;
4090 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4091 ev->display = FRAME_X_DISPLAY (f);
4092 ev->window = FRAME_X_WINDOW (f);
4093 ev->format = 32;
4095 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4096 not enough to store a pointer or Lisp_Object on a 64 bit system.
4097 So, store the window in scroll_bar_windows and pass the index
4098 into that array in the event. */
4099 for (i = 0; i < scroll_bar_windows_size; ++i)
4100 if (scroll_bar_windows[i] == NULL)
4101 break;
4103 if (i == scroll_bar_windows_size)
4105 int new_size = max (10, 2 * scroll_bar_windows_size);
4106 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4107 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4109 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4110 nbytes);
4111 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4112 scroll_bar_windows_size = new_size;
4115 scroll_bar_windows[i] = w;
4116 ev->data.l[0] = (long) i;
4117 ev->data.l[1] = (long) part;
4118 ev->data.l[2] = (long) 0;
4119 ev->data.l[3] = (long) portion;
4120 ev->data.l[4] = (long) whole;
4122 /* Make Xt timeouts work while the scroll bar is active. */
4123 toolkit_scroll_bar_interaction = 1;
4124 #ifdef USE_X_TOOLKIT
4125 x_activate_timeout_atimer ();
4126 #endif
4128 /* Setting the event mask to zero means that the message will
4129 be sent to the client that created the window, and if that
4130 window no longer exists, no event will be sent. */
4131 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4132 UNBLOCK_INPUT;
4136 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4137 in *IEVENT. */
4139 static void
4140 x_scroll_bar_to_input_event (event, ievent)
4141 XEvent *event;
4142 struct input_event *ievent;
4144 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4145 Lisp_Object window;
4146 struct frame *f;
4147 struct window *w;
4149 w = scroll_bar_windows[ev->data.l[0]];
4150 scroll_bar_windows[ev->data.l[0]] = NULL;
4152 XSETWINDOW (window, w);
4153 f = XFRAME (w->frame);
4155 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4156 ievent->frame_or_window = window;
4157 ievent->arg = Qnil;
4158 #ifdef USE_GTK
4159 ievent->timestamp = CurrentTime;
4160 #else
4161 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4162 #endif
4163 ievent->part = ev->data.l[1];
4164 ievent->code = ev->data.l[2];
4165 ievent->x = make_number ((int) ev->data.l[3]);
4166 ievent->y = make_number ((int) ev->data.l[4]);
4167 ievent->modifiers = 0;
4171 #ifdef USE_MOTIF
4173 /* Minimum and maximum values used for Motif scroll bars. */
4175 #define XM_SB_MAX 10000000
4178 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4179 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4180 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4182 static void
4183 xm_scroll_callback (widget, client_data, call_data)
4184 Widget widget;
4185 XtPointer client_data, call_data;
4187 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4188 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4189 int part = -1, whole = 0, portion = 0;
4191 switch (cs->reason)
4193 case XmCR_DECREMENT:
4194 bar->dragging = Qnil;
4195 part = scroll_bar_up_arrow;
4196 break;
4198 case XmCR_INCREMENT:
4199 bar->dragging = Qnil;
4200 part = scroll_bar_down_arrow;
4201 break;
4203 case XmCR_PAGE_DECREMENT:
4204 bar->dragging = Qnil;
4205 part = scroll_bar_above_handle;
4206 break;
4208 case XmCR_PAGE_INCREMENT:
4209 bar->dragging = Qnil;
4210 part = scroll_bar_below_handle;
4211 break;
4213 case XmCR_TO_TOP:
4214 bar->dragging = Qnil;
4215 part = scroll_bar_to_top;
4216 break;
4218 case XmCR_TO_BOTTOM:
4219 bar->dragging = Qnil;
4220 part = scroll_bar_to_bottom;
4221 break;
4223 case XmCR_DRAG:
4225 int slider_size;
4227 /* Get the slider size. */
4228 BLOCK_INPUT;
4229 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4230 UNBLOCK_INPUT;
4232 whole = XM_SB_MAX - slider_size;
4233 portion = min (cs->value, whole);
4234 part = scroll_bar_handle;
4235 bar->dragging = make_number (cs->value);
4237 break;
4239 case XmCR_VALUE_CHANGED:
4240 break;
4243 if (part >= 0)
4245 window_being_scrolled = bar->window;
4246 last_scroll_bar_part = part;
4247 x_send_scroll_bar_event (bar->window, part, portion, whole);
4251 #elif defined USE_GTK
4253 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4254 bar widget. DATA is a pointer to the scroll_bar structure. */
4256 static void
4257 xg_scroll_callback (widget, data)
4258 GtkRange *widget;
4259 gpointer data;
4261 struct scroll_bar *bar = (struct scroll_bar *) data;
4262 gdouble previous;
4263 gdouble position;
4264 gdouble *p;
4265 int diff;
4267 int part = -1, whole = 0, portion = 0;
4268 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4270 position = gtk_adjustment_get_value (adj);
4272 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4273 if (! p)
4275 p = (gdouble*) xmalloc (sizeof (gdouble));
4276 *p = XG_SB_MIN;
4277 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4280 previous = *p;
4281 *p = position;
4283 if (xg_ignore_gtk_scrollbar) return;
4285 diff = (int) (position - previous);
4287 if (diff == (int) adj->step_increment)
4289 part = scroll_bar_down_arrow;
4290 bar->dragging = Qnil;
4292 else if (-diff == (int) adj->step_increment)
4294 part = scroll_bar_up_arrow;
4295 bar->dragging = Qnil;
4297 else if (diff == (int) adj->page_increment)
4299 part = scroll_bar_below_handle;
4300 bar->dragging = Qnil;
4302 else if (-diff == (int) adj->page_increment)
4304 part = scroll_bar_above_handle;
4305 bar->dragging = Qnil;
4307 else
4309 part = scroll_bar_handle;
4310 whole = adj->upper - adj->page_size;
4311 portion = min ((int)position, whole);
4312 bar->dragging = make_number ((int)portion);
4315 if (part >= 0)
4317 window_being_scrolled = bar->window;
4318 last_scroll_bar_part = part;
4319 x_send_scroll_bar_event (bar->window, part, portion, whole);
4323 #else /* not USE_GTK and not USE_MOTIF */
4325 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4326 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4327 scroll bar struct. CALL_DATA is a pointer to a float saying where
4328 the thumb is. */
4330 static void
4331 xaw_jump_callback (widget, client_data, call_data)
4332 Widget widget;
4333 XtPointer client_data, call_data;
4335 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4336 float top = *(float *) call_data;
4337 float shown;
4338 int whole, portion, height;
4339 int part;
4341 /* Get the size of the thumb, a value between 0 and 1. */
4342 BLOCK_INPUT;
4343 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4344 UNBLOCK_INPUT;
4346 whole = 10000000;
4347 portion = shown < 1 ? top * whole : 0;
4349 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4350 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4351 the bottom, so we force the scrolling whenever we see that we're
4352 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4353 we try to ensure that we always stay two pixels away from the
4354 bottom). */
4355 part = scroll_bar_down_arrow;
4356 else
4357 part = scroll_bar_handle;
4359 window_being_scrolled = bar->window;
4360 bar->dragging = make_number (portion);
4361 last_scroll_bar_part = part;
4362 x_send_scroll_bar_event (bar->window, part, portion, whole);
4366 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4367 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4368 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4369 the scroll bar. CALL_DATA is an integer specifying the action that
4370 has taken place. Its magnitude is in the range 0..height of the
4371 scroll bar. Negative values mean scroll towards buffer start.
4372 Values < height of scroll bar mean line-wise movement. */
4374 static void
4375 xaw_scroll_callback (widget, client_data, call_data)
4376 Widget widget;
4377 XtPointer client_data, call_data;
4379 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4380 /* The position really is stored cast to a pointer. */
4381 int position = (long) call_data;
4382 Dimension height;
4383 int part;
4385 /* Get the height of the scroll bar. */
4386 BLOCK_INPUT;
4387 XtVaGetValues (widget, XtNheight, &height, NULL);
4388 UNBLOCK_INPUT;
4390 if (eabs (position) >= height)
4391 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4393 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4394 it maps line-movement to call_data = max(5, height/20). */
4395 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4396 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4397 else
4398 part = scroll_bar_move_ratio;
4400 window_being_scrolled = bar->window;
4401 bar->dragging = Qnil;
4402 last_scroll_bar_part = part;
4403 x_send_scroll_bar_event (bar->window, part, position, height);
4406 #endif /* not USE_GTK and not USE_MOTIF */
4408 #define SCROLL_BAR_NAME "verticalScrollBar"
4410 /* Create the widget for scroll bar BAR on frame F. Record the widget
4411 and X window of the scroll bar in BAR. */
4413 #ifdef USE_GTK
4414 static void
4415 x_create_toolkit_scroll_bar (f, bar)
4416 struct frame *f;
4417 struct scroll_bar *bar;
4419 char *scroll_bar_name = SCROLL_BAR_NAME;
4421 BLOCK_INPUT;
4422 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4423 scroll_bar_name);
4424 UNBLOCK_INPUT;
4427 #else /* not USE_GTK */
4429 static void
4430 x_create_toolkit_scroll_bar (f, bar)
4431 struct frame *f;
4432 struct scroll_bar *bar;
4434 Window xwindow;
4435 Widget widget;
4436 Arg av[20];
4437 int ac = 0;
4438 char *scroll_bar_name = SCROLL_BAR_NAME;
4439 unsigned long pixel;
4441 BLOCK_INPUT;
4443 #ifdef USE_MOTIF
4444 /* Set resources. Create the widget. */
4445 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4446 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4447 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4448 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4449 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4450 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4451 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4453 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4454 if (pixel != -1)
4456 XtSetArg (av[ac], XmNforeground, pixel);
4457 ++ac;
4460 pixel = f->output_data.x->scroll_bar_background_pixel;
4461 if (pixel != -1)
4463 XtSetArg (av[ac], XmNbackground, pixel);
4464 ++ac;
4467 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4468 scroll_bar_name, av, ac);
4470 /* Add one callback for everything that can happen. */
4471 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4472 (XtPointer) bar);
4473 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4474 (XtPointer) bar);
4475 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4476 (XtPointer) bar);
4477 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4478 (XtPointer) bar);
4479 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4480 (XtPointer) bar);
4481 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4482 (XtPointer) bar);
4483 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4484 (XtPointer) bar);
4486 /* Realize the widget. Only after that is the X window created. */
4487 XtRealizeWidget (widget);
4489 /* Set the cursor to an arrow. I didn't find a resource to do that.
4490 And I'm wondering why it hasn't an arrow cursor by default. */
4491 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4492 f->output_data.x->nontext_cursor);
4494 #else /* !USE_MOTIF i.e. use Xaw */
4496 /* Set resources. Create the widget. The background of the
4497 Xaw3d scroll bar widget is a little bit light for my taste.
4498 We don't alter it here to let users change it according
4499 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4500 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4501 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4502 /* For smoother scrolling with Xaw3d -sm */
4503 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4505 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4506 if (pixel != -1)
4508 XtSetArg (av[ac], XtNforeground, pixel);
4509 ++ac;
4512 pixel = f->output_data.x->scroll_bar_background_pixel;
4513 if (pixel != -1)
4515 XtSetArg (av[ac], XtNbackground, pixel);
4516 ++ac;
4519 /* Top/bottom shadow colors. */
4521 /* Allocate them, if necessary. */
4522 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4524 pixel = f->output_data.x->scroll_bar_background_pixel;
4525 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4526 &pixel, 1.2, 0x8000))
4527 pixel = -1;
4528 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4530 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4532 pixel = f->output_data.x->scroll_bar_background_pixel;
4533 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4534 &pixel, 0.6, 0x4000))
4535 pixel = -1;
4536 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4539 #ifdef XtNbeNiceToColormap
4540 /* Tell the toolkit about them. */
4541 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4542 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4543 /* We tried to allocate a color for the top/bottom shadow, and
4544 failed, so tell Xaw3d to use dithering instead. */
4546 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4547 ++ac;
4549 else
4550 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4551 be more consistent with other emacs 3d colors, and since Xaw3d is
4552 not good at dealing with allocation failure. */
4554 /* This tells Xaw3d to use real colors instead of dithering for
4555 the shadows. */
4556 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4557 ++ac;
4559 /* Specify the colors. */
4560 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4561 if (pixel != -1)
4563 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4564 ++ac;
4566 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4567 if (pixel != -1)
4569 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4570 ++ac;
4573 #endif
4575 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4576 f->output_data.x->edit_widget, av, ac);
4579 char *initial = "";
4580 char *val = initial;
4581 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4582 #ifdef XtNarrowScrollbars
4583 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4584 #endif
4585 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4586 if (xaw3d_arrow_scroll || val == initial)
4587 { /* ARROW_SCROLL */
4588 xaw3d_arrow_scroll = True;
4589 /* Isn't that just a personal preference ? --Stef */
4590 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4594 /* Define callbacks. */
4595 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4596 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4597 (XtPointer) bar);
4599 /* Realize the widget. Only after that is the X window created. */
4600 XtRealizeWidget (widget);
4602 #endif /* !USE_MOTIF */
4604 /* Install an action hook that lets us detect when the user
4605 finishes interacting with a scroll bar. */
4606 if (action_hook_id == 0)
4607 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4609 /* Remember X window and widget in the scroll bar vector. */
4610 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4611 xwindow = XtWindow (widget);
4612 bar->x_window = xwindow;
4614 UNBLOCK_INPUT;
4616 #endif /* not USE_GTK */
4619 /* Set the thumb size and position of scroll bar BAR. We are currently
4620 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4622 #ifdef USE_GTK
4623 static void
4624 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4625 struct scroll_bar *bar;
4626 int portion, position, whole;
4628 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4631 #else /* not USE_GTK */
4632 static void
4633 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4634 struct scroll_bar *bar;
4635 int portion, position, whole;
4637 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4638 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4639 float top, shown;
4641 BLOCK_INPUT;
4643 #ifdef USE_MOTIF
4645 /* We use an estimate of 30 chars per line rather than the real
4646 `portion' value. This has the disadvantage that the thumb size
4647 is not very representative, but it makes our life a lot easier.
4648 Otherwise, we have to constantly adjust the thumb size, which
4649 we can't always do quickly enough: while dragging, the size of
4650 the thumb might prevent the user from dragging the thumb all the
4651 way to the end. but Motif and some versions of Xaw3d don't allow
4652 updating the thumb size while dragging. Also, even if we can update
4653 its size, the update will often happen too late.
4654 If you don't believe it, check out revision 1.650 of xterm.c to see
4655 what hoops we were going through and the still poor behavior we got. */
4656 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4657 /* When the thumb is at the bottom, position == whole.
4658 So we need to increase `whole' to make space for the thumb. */
4659 whole += portion;
4661 if (whole <= 0)
4662 top = 0, shown = 1;
4663 else
4665 top = (float) position / whole;
4666 shown = (float) portion / whole;
4669 if (NILP (bar->dragging))
4671 int size, value;
4673 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4674 is the scroll bar's maximum and MIN is the scroll bar's minimum
4675 value. */
4676 size = shown * XM_SB_MAX;
4677 size = min (size, XM_SB_MAX);
4678 size = max (size, 1);
4680 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4681 value = top * XM_SB_MAX;
4682 value = min (value, XM_SB_MAX - size);
4684 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4686 #else /* !USE_MOTIF i.e. use Xaw */
4688 if (whole == 0)
4689 top = 0, shown = 1;
4690 else
4692 top = (float) position / whole;
4693 shown = (float) portion / whole;
4697 float old_top, old_shown;
4698 Dimension height;
4699 XtVaGetValues (widget,
4700 XtNtopOfThumb, &old_top,
4701 XtNshown, &old_shown,
4702 XtNheight, &height,
4703 NULL);
4705 /* Massage the top+shown values. */
4706 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4707 top = max (0, min (1, top));
4708 else
4709 top = old_top;
4710 /* Keep two pixels available for moving the thumb down. */
4711 shown = max (0, min (1 - top - (2.0 / height), shown));
4713 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4714 check that your system's configuration file contains a define
4715 for `NARROWPROTO'. See s/freebsd.h for an example. */
4716 if (top != old_top || shown != old_shown)
4718 if (NILP (bar->dragging))
4719 XawScrollbarSetThumb (widget, top, shown);
4720 else
4722 /* Try to make the scrolling a tad smoother. */
4723 if (!xaw3d_pick_top)
4724 shown = min (shown, old_shown);
4726 XawScrollbarSetThumb (widget, top, shown);
4730 #endif /* !USE_MOTIF */
4732 UNBLOCK_INPUT;
4734 #endif /* not USE_GTK */
4736 #endif /* USE_TOOLKIT_SCROLL_BARS */
4740 /************************************************************************
4741 Scroll bars, general
4742 ************************************************************************/
4744 /* Create a scroll bar and return the scroll bar vector for it. W is
4745 the Emacs window on which to create the scroll bar. TOP, LEFT,
4746 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4747 scroll bar. */
4749 static struct scroll_bar *
4750 x_scroll_bar_create (w, top, left, width, height)
4751 struct window *w;
4752 int top, left, width, height;
4754 struct frame *f = XFRAME (w->frame);
4755 struct scroll_bar *bar
4756 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4758 BLOCK_INPUT;
4760 #ifdef USE_TOOLKIT_SCROLL_BARS
4761 x_create_toolkit_scroll_bar (f, bar);
4762 #else /* not USE_TOOLKIT_SCROLL_BARS */
4764 XSetWindowAttributes a;
4765 unsigned long mask;
4766 Window window;
4768 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4769 if (a.background_pixel == -1)
4770 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4772 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4773 | ButtonMotionMask | PointerMotionHintMask
4774 | ExposureMask);
4775 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4777 mask = (CWBackPixel | CWEventMask | CWCursor);
4779 /* Clear the area of W that will serve as a scroll bar. This is
4780 for the case that a window has been split horizontally. In
4781 this case, no clear_frame is generated to reduce flickering. */
4782 if (width > 0 && height > 0)
4783 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4784 left, top, width,
4785 window_box_height (w), False);
4787 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4788 /* Position and size of scroll bar. */
4789 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4790 top,
4791 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4792 height,
4793 /* Border width, depth, class, and visual. */
4795 CopyFromParent,
4796 CopyFromParent,
4797 CopyFromParent,
4798 /* Attributes. */
4799 mask, &a);
4800 bar->x_window = window;
4802 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4804 XSETWINDOW (bar->window, w);
4805 bar->top = top;
4806 bar->left = left;
4807 bar->width = width;
4808 bar->height = height;
4809 bar->start = 0;
4810 bar->end = 0;
4811 bar->dragging = Qnil;
4812 bar->fringe_extended_p = 0;
4814 /* Add bar to its frame's list of scroll bars. */
4815 bar->next = FRAME_SCROLL_BARS (f);
4816 bar->prev = Qnil;
4817 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4818 if (!NILP (bar->next))
4819 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4821 /* Map the window/widget. */
4822 #ifdef USE_TOOLKIT_SCROLL_BARS
4824 #ifdef USE_GTK
4825 xg_update_scrollbar_pos (f,
4826 bar->x_window,
4827 top,
4828 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4829 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4830 max (height, 1));
4831 xg_show_scroll_bar (bar->x_window);
4832 #else /* not USE_GTK */
4833 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4834 XtConfigureWidget (scroll_bar,
4835 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4836 top,
4837 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4838 max (height, 1), 0);
4839 XtMapWidget (scroll_bar);
4840 #endif /* not USE_GTK */
4842 #else /* not USE_TOOLKIT_SCROLL_BARS */
4843 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
4844 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4846 UNBLOCK_INPUT;
4847 return bar;
4851 #ifndef USE_TOOLKIT_SCROLL_BARS
4853 /* Draw BAR's handle in the proper position.
4855 If the handle is already drawn from START to END, don't bother
4856 redrawing it, unless REBUILD is non-zero; in that case, always
4857 redraw it. (REBUILD is handy for drawing the handle after expose
4858 events.)
4860 Normally, we want to constrain the start and end of the handle to
4861 fit inside its rectangle, but if the user is dragging the scroll
4862 bar handle, we want to let them drag it down all the way, so that
4863 the bar's top is as far down as it goes; otherwise, there's no way
4864 to move to the very end of the buffer. */
4866 static void
4867 x_scroll_bar_set_handle (bar, start, end, rebuild)
4868 struct scroll_bar *bar;
4869 int start, end;
4870 int rebuild;
4872 int dragging = ! NILP (bar->dragging);
4873 Window w = bar->x_window;
4874 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4875 GC gc = f->output_data.x->normal_gc;
4877 /* If the display is already accurate, do nothing. */
4878 if (! rebuild
4879 && start == bar->start
4880 && end == bar->end)
4881 return;
4883 BLOCK_INPUT;
4886 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
4887 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
4888 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4890 /* Make sure the values are reasonable, and try to preserve
4891 the distance between start and end. */
4893 int length = end - start;
4895 if (start < 0)
4896 start = 0;
4897 else if (start > top_range)
4898 start = top_range;
4899 end = start + length;
4901 if (end < start)
4902 end = start;
4903 else if (end > top_range && ! dragging)
4904 end = top_range;
4907 /* Store the adjusted setting in the scroll bar. */
4908 bar->start = start;
4909 bar->end = end;
4911 /* Clip the end position, just for display. */
4912 if (end > top_range)
4913 end = top_range;
4915 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
4916 below top positions, to make sure the handle is always at least
4917 that many pixels tall. */
4918 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
4920 /* Draw the empty space above the handle. Note that we can't clear
4921 zero-height areas; that means "clear to end of window." */
4922 if (0 < start)
4923 x_clear_area (FRAME_X_DISPLAY (f), w,
4924 /* x, y, width, height, and exposures. */
4925 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4926 VERTICAL_SCROLL_BAR_TOP_BORDER,
4927 inside_width, start,
4928 False);
4930 /* Change to proper foreground color if one is specified. */
4931 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4932 XSetForeground (FRAME_X_DISPLAY (f), gc,
4933 f->output_data.x->scroll_bar_foreground_pixel);
4935 /* Draw the handle itself. */
4936 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
4937 /* x, y, width, height */
4938 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4939 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
4940 inside_width, end - start);
4942 /* Restore the foreground color of the GC if we changed it above. */
4943 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4944 XSetForeground (FRAME_X_DISPLAY (f), gc,
4945 FRAME_FOREGROUND_PIXEL (f));
4947 /* Draw the empty space below the handle. Note that we can't
4948 clear zero-height areas; that means "clear to end of window." */
4949 if (end < inside_height)
4950 x_clear_area (FRAME_X_DISPLAY (f), w,
4951 /* x, y, width, height, and exposures. */
4952 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4953 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
4954 inside_width, inside_height - end,
4955 False);
4959 UNBLOCK_INPUT;
4962 #endif /* !USE_TOOLKIT_SCROLL_BARS */
4964 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
4965 nil. */
4967 static void
4968 x_scroll_bar_remove (bar)
4969 struct scroll_bar *bar;
4971 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4972 BLOCK_INPUT;
4974 #ifdef USE_TOOLKIT_SCROLL_BARS
4975 #ifdef USE_GTK
4976 xg_remove_scroll_bar (f, bar->x_window);
4977 #else /* not USE_GTK */
4978 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
4979 #endif /* not USE_GTK */
4980 #else
4981 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
4982 #endif
4984 /* Disassociate this scroll bar from its window. */
4985 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
4987 UNBLOCK_INPUT;
4991 /* Set the handle of the vertical scroll bar for WINDOW to indicate
4992 that we are displaying PORTION characters out of a total of WHOLE
4993 characters, starting at POSITION. If WINDOW has no scroll bar,
4994 create one. */
4996 static void
4997 XTset_vertical_scroll_bar (w, portion, whole, position)
4998 struct window *w;
4999 int portion, whole, position;
5001 struct frame *f = XFRAME (w->frame);
5002 struct scroll_bar *bar;
5003 int top, height, left, sb_left, width, sb_width;
5004 int window_y, window_height;
5005 #ifdef USE_TOOLKIT_SCROLL_BARS
5006 int fringe_extended_p;
5007 #endif
5009 /* Get window dimensions. */
5010 window_box (w, -1, 0, &window_y, 0, &window_height);
5011 top = window_y;
5012 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5013 height = window_height;
5015 /* Compute the left edge of the scroll bar area. */
5016 left = WINDOW_SCROLL_BAR_AREA_X (w);
5018 /* Compute the width of the scroll bar which might be less than
5019 the width of the area reserved for the scroll bar. */
5020 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5021 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5022 else
5023 sb_width = width;
5025 /* Compute the left edge of the scroll bar. */
5026 #ifdef USE_TOOLKIT_SCROLL_BARS
5027 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5028 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5029 else
5030 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5031 #else
5032 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5033 sb_left = left + width - sb_width;
5034 else
5035 sb_left = left;
5036 #endif
5038 #ifdef USE_TOOLKIT_SCROLL_BARS
5039 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5040 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5041 && WINDOW_LEFT_FRINGE_WIDTH (w)
5042 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5043 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5044 else
5045 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5046 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5047 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5048 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5049 #endif
5051 /* Does the scroll bar exist yet? */
5052 if (NILP (w->vertical_scroll_bar))
5054 if (width > 0 && height > 0)
5056 BLOCK_INPUT;
5057 #ifdef USE_TOOLKIT_SCROLL_BARS
5058 if (fringe_extended_p)
5059 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5060 sb_left, top, sb_width, height, False);
5061 else
5062 #endif
5063 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5064 left, top, width, height, False);
5065 UNBLOCK_INPUT;
5068 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5070 else
5072 /* It may just need to be moved and resized. */
5073 unsigned int mask = 0;
5075 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5077 BLOCK_INPUT;
5079 if (sb_left != bar->left)
5080 mask |= CWX;
5081 if (top != bar->top)
5082 mask |= CWY;
5083 if (sb_width != bar->width)
5084 mask |= CWWidth;
5085 if (height != bar->height)
5086 mask |= CWHeight;
5088 #ifdef USE_TOOLKIT_SCROLL_BARS
5090 /* Move/size the scroll bar widget. */
5091 if (mask || bar->fringe_extended_p != fringe_extended_p)
5093 /* Since toolkit scroll bars are smaller than the space reserved
5094 for them on the frame, we have to clear "under" them. */
5095 if (width > 0 && height > 0)
5097 if (fringe_extended_p)
5098 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5099 sb_left, top, sb_width, height, False);
5100 else
5101 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5102 left, top, width, height, False);
5104 #ifdef USE_GTK
5105 xg_update_scrollbar_pos (f,
5106 bar->x_window,
5107 top,
5108 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5109 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5110 max (height, 1));
5111 #else /* not USE_GTK */
5112 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5113 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5114 top,
5115 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5116 max (height, 1), 0);
5117 #endif /* not USE_GTK */
5119 #else /* not USE_TOOLKIT_SCROLL_BARS */
5121 /* Clear areas not covered by the scroll bar because of
5122 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5123 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5125 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5126 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5127 height, False);
5128 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5129 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5130 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5131 height, False);
5134 /* Clear areas not covered by the scroll bar because it's not as
5135 wide as the area reserved for it. This makes sure a
5136 previous mode line display is cleared after C-x 2 C-x 1, for
5137 example. */
5139 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5140 int rest = area_width - sb_width;
5141 if (rest > 0 && height > 0)
5143 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5144 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5145 left + area_width - rest, top,
5146 rest, height, False);
5147 else
5148 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5149 left, top, rest, height, False);
5153 /* Move/size the scroll bar window. */
5154 if (mask)
5156 XWindowChanges wc;
5158 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5159 wc.y = top;
5160 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5161 wc.height = height;
5162 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5163 mask, &wc);
5166 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5168 /* Remember new settings. */
5169 bar->left = sb_left;
5170 bar->top = top;
5171 bar->width = sb_width;
5172 bar->height = height;
5174 UNBLOCK_INPUT;
5177 #ifdef USE_TOOLKIT_SCROLL_BARS
5178 bar->fringe_extended_p = fringe_extended_p;
5180 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5181 #else /* not USE_TOOLKIT_SCROLL_BARS */
5182 /* Set the scroll bar's current state, unless we're currently being
5183 dragged. */
5184 if (NILP (bar->dragging))
5186 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5188 if (whole == 0)
5189 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5190 else
5192 int start = ((double) position * top_range) / whole;
5193 int end = ((double) (position + portion) * top_range) / whole;
5194 x_scroll_bar_set_handle (bar, start, end, 0);
5197 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5199 XSETVECTOR (w->vertical_scroll_bar, bar);
5203 /* The following three hooks are used when we're doing a thorough
5204 redisplay of the frame. We don't explicitly know which scroll bars
5205 are going to be deleted, because keeping track of when windows go
5206 away is a real pain - "Can you say set-window-configuration, boys
5207 and girls?" Instead, we just assert at the beginning of redisplay
5208 that *all* scroll bars are to be removed, and then save a scroll bar
5209 from the fiery pit when we actually redisplay its window. */
5211 /* Arrange for all scroll bars on FRAME to be removed at the next call
5212 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5213 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5215 static void
5216 XTcondemn_scroll_bars (frame)
5217 FRAME_PTR frame;
5219 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5220 while (! NILP (FRAME_SCROLL_BARS (frame)))
5222 Lisp_Object bar;
5223 bar = FRAME_SCROLL_BARS (frame);
5224 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5225 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5226 XSCROLL_BAR (bar)->prev = Qnil;
5227 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5228 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5229 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5234 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5235 Note that WINDOW isn't necessarily condemned at all. */
5237 static void
5238 XTredeem_scroll_bar (window)
5239 struct window *window;
5241 struct scroll_bar *bar;
5242 struct frame *f;
5244 /* We can't redeem this window's scroll bar if it doesn't have one. */
5245 if (NILP (window->vertical_scroll_bar))
5246 abort ();
5248 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5250 /* Unlink it from the condemned list. */
5251 f = XFRAME (WINDOW_FRAME (window));
5252 if (NILP (bar->prev))
5254 /* If the prev pointer is nil, it must be the first in one of
5255 the lists. */
5256 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5257 /* It's not condemned. Everything's fine. */
5258 return;
5259 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5260 window->vertical_scroll_bar))
5261 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5262 else
5263 /* If its prev pointer is nil, it must be at the front of
5264 one or the other! */
5265 abort ();
5267 else
5268 XSCROLL_BAR (bar->prev)->next = bar->next;
5270 if (! NILP (bar->next))
5271 XSCROLL_BAR (bar->next)->prev = bar->prev;
5273 bar->next = FRAME_SCROLL_BARS (f);
5274 bar->prev = Qnil;
5275 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5276 if (! NILP (bar->next))
5277 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5280 /* Remove all scroll bars on FRAME that haven't been saved since the
5281 last call to `*condemn_scroll_bars_hook'. */
5283 static void
5284 XTjudge_scroll_bars (f)
5285 FRAME_PTR f;
5287 Lisp_Object bar, next;
5289 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5291 /* Clear out the condemned list now so we won't try to process any
5292 more events on the hapless scroll bars. */
5293 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5295 for (; ! NILP (bar); bar = next)
5297 struct scroll_bar *b = XSCROLL_BAR (bar);
5299 x_scroll_bar_remove (b);
5301 next = b->next;
5302 b->next = b->prev = Qnil;
5305 /* Now there should be no references to the condemned scroll bars,
5306 and they should get garbage-collected. */
5310 #ifndef USE_TOOLKIT_SCROLL_BARS
5311 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5312 is a no-op when using toolkit scroll bars.
5314 This may be called from a signal handler, so we have to ignore GC
5315 mark bits. */
5317 static void
5318 x_scroll_bar_expose (bar, event)
5319 struct scroll_bar *bar;
5320 XEvent *event;
5322 Window w = bar->x_window;
5323 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5324 GC gc = f->output_data.x->normal_gc;
5325 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5327 BLOCK_INPUT;
5329 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5331 /* Switch to scroll bar foreground color. */
5332 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5333 XSetForeground (FRAME_X_DISPLAY (f), gc,
5334 f->output_data.x->scroll_bar_foreground_pixel);
5336 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5337 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5339 /* x, y, width, height */
5340 0, 0,
5341 bar->width - 1 - width_trim - width_trim,
5342 bar->height - 1);
5344 /* Restore the foreground color of the GC if we changed it above. */
5345 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5346 XSetForeground (FRAME_X_DISPLAY (f), gc,
5347 FRAME_FOREGROUND_PIXEL (f));
5349 UNBLOCK_INPUT;
5352 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5354 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5355 is set to something other than NO_EVENT, it is enqueued.
5357 This may be called from a signal handler, so we have to ignore GC
5358 mark bits. */
5361 static void
5362 x_scroll_bar_handle_click (bar, event, emacs_event)
5363 struct scroll_bar *bar;
5364 XEvent *event;
5365 struct input_event *emacs_event;
5367 if (! WINDOWP (bar->window))
5368 abort ();
5370 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5371 emacs_event->code = event->xbutton.button - Button1;
5372 emacs_event->modifiers
5373 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5374 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5375 event->xbutton.state)
5376 | (event->type == ButtonRelease
5377 ? up_modifier
5378 : down_modifier));
5379 emacs_event->frame_or_window = bar->window;
5380 emacs_event->arg = Qnil;
5381 emacs_event->timestamp = event->xbutton.time;
5383 #if 0
5384 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5385 int internal_height
5386 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5387 #endif
5388 int top_range
5389 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5390 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5392 if (y < 0) y = 0;
5393 if (y > top_range) y = top_range;
5395 if (y < bar->start)
5396 emacs_event->part = scroll_bar_above_handle;
5397 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5398 emacs_event->part = scroll_bar_handle;
5399 else
5400 emacs_event->part = scroll_bar_below_handle;
5402 /* Just because the user has clicked on the handle doesn't mean
5403 they want to drag it. Lisp code needs to be able to decide
5404 whether or not we're dragging. */
5405 #if 0
5406 /* If the user has just clicked on the handle, record where they're
5407 holding it. */
5408 if (event->type == ButtonPress
5409 && emacs_event->part == scroll_bar_handle)
5410 XSETINT (bar->dragging, y - bar->start);
5411 #endif
5413 #ifndef USE_TOOLKIT_SCROLL_BARS
5414 /* If the user has released the handle, set it to its final position. */
5415 if (event->type == ButtonRelease
5416 && ! NILP (bar->dragging))
5418 int new_start = y - XINT (bar->dragging);
5419 int new_end = new_start + bar->end - bar->start;
5421 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5422 bar->dragging = Qnil;
5424 #endif
5426 /* Same deal here as the other #if 0. */
5427 #if 0
5428 /* Clicks on the handle are always reported as occurring at the top of
5429 the handle. */
5430 if (emacs_event->part == scroll_bar_handle)
5431 emacs_event->x = bar->start;
5432 else
5433 XSETINT (emacs_event->x, y);
5434 #else
5435 XSETINT (emacs_event->x, y);
5436 #endif
5438 XSETINT (emacs_event->y, top_range);
5442 #ifndef USE_TOOLKIT_SCROLL_BARS
5444 /* Handle some mouse motion while someone is dragging the scroll bar.
5446 This may be called from a signal handler, so we have to ignore GC
5447 mark bits. */
5449 static void
5450 x_scroll_bar_note_movement (bar, event)
5451 struct scroll_bar *bar;
5452 XEvent *event;
5454 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5456 last_mouse_movement_time = event->xmotion.time;
5458 f->mouse_moved = 1;
5459 XSETVECTOR (last_mouse_scroll_bar, bar);
5461 /* If we're dragging the bar, display it. */
5462 if (! NILP (bar->dragging))
5464 /* Where should the handle be now? */
5465 int new_start = event->xmotion.y - XINT (bar->dragging);
5467 if (new_start != bar->start)
5469 int new_end = new_start + bar->end - bar->start;
5471 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5476 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5478 /* Return information to the user about the current position of the mouse
5479 on the scroll bar. */
5481 static void
5482 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5483 FRAME_PTR *fp;
5484 Lisp_Object *bar_window;
5485 enum scroll_bar_part *part;
5486 Lisp_Object *x, *y;
5487 unsigned long *time;
5489 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5490 Window w = bar->x_window;
5491 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5492 int win_x, win_y;
5493 Window dummy_window;
5494 int dummy_coord;
5495 unsigned int dummy_mask;
5497 BLOCK_INPUT;
5499 /* Get the mouse's position relative to the scroll bar window, and
5500 report that. */
5501 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5503 /* Root, child, root x and root y. */
5504 &dummy_window, &dummy_window,
5505 &dummy_coord, &dummy_coord,
5507 /* Position relative to scroll bar. */
5508 &win_x, &win_y,
5510 /* Mouse buttons and modifier keys. */
5511 &dummy_mask))
5513 else
5515 #if 0
5516 int inside_height
5517 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5518 #endif
5519 int top_range
5520 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5522 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5524 if (! NILP (bar->dragging))
5525 win_y -= XINT (bar->dragging);
5527 if (win_y < 0)
5528 win_y = 0;
5529 if (win_y > top_range)
5530 win_y = top_range;
5532 *fp = f;
5533 *bar_window = bar->window;
5535 if (! NILP (bar->dragging))
5536 *part = scroll_bar_handle;
5537 else if (win_y < bar->start)
5538 *part = scroll_bar_above_handle;
5539 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5540 *part = scroll_bar_handle;
5541 else
5542 *part = scroll_bar_below_handle;
5544 XSETINT (*x, win_y);
5545 XSETINT (*y, top_range);
5547 f->mouse_moved = 0;
5548 last_mouse_scroll_bar = Qnil;
5551 *time = last_mouse_movement_time;
5553 UNBLOCK_INPUT;
5557 /* The screen has been cleared so we may have changed foreground or
5558 background colors, and the scroll bars may need to be redrawn.
5559 Clear out the scroll bars, and ask for expose events, so we can
5560 redraw them. */
5562 void
5563 x_scroll_bar_clear (f)
5564 FRAME_PTR f;
5566 #ifndef USE_TOOLKIT_SCROLL_BARS
5567 Lisp_Object bar;
5569 /* We can have scroll bars even if this is 0,
5570 if we just turned off scroll bar mode.
5571 But in that case we should not clear them. */
5572 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5573 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5574 bar = XSCROLL_BAR (bar)->next)
5575 XClearArea (FRAME_X_DISPLAY (f),
5576 XSCROLL_BAR (bar)->x_window,
5577 0, 0, 0, 0, True);
5578 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5582 /* The main X event-reading loop - XTread_socket. */
5584 #if 0
5585 /* Time stamp of enter window event. This is only used by XTread_socket,
5586 but we have to put it out here, since static variables within functions
5587 sometimes don't work. */
5589 static Time enter_timestamp;
5590 #endif
5592 /* This holds the state XLookupString needs to implement dead keys
5593 and other tricks known as "compose processing". _X Window System_
5594 says that a portable program can't use this, but Stephen Gildea assures
5595 me that letting the compiler initialize it to zeros will work okay.
5597 This must be defined outside of XTread_socket, for the same reasons
5598 given for enter_timestamp, above. */
5600 static XComposeStatus compose_status;
5602 /* Record the last 100 characters stored
5603 to help debug the loss-of-chars-during-GC problem. */
5605 static int temp_index;
5606 static short temp_buffer[100];
5608 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5609 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5610 temp_index = 0; \
5611 temp_buffer[temp_index++] = (keysym)
5613 /* Set this to nonzero to fake an "X I/O error"
5614 on a particular display. */
5616 struct x_display_info *XTread_socket_fake_io_error;
5618 /* When we find no input here, we occasionally do a no-op command
5619 to verify that the X server is still running and we can still talk with it.
5620 We try all the open displays, one by one.
5621 This variable is used for cycling thru the displays. */
5623 static struct x_display_info *next_noop_dpyinfo;
5625 #define SET_SAVED_MENU_EVENT(size) \
5626 do \
5628 if (f->output_data.x->saved_menu_event == 0) \
5629 f->output_data.x->saved_menu_event \
5630 = (XEvent *) xmalloc (sizeof (XEvent)); \
5631 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5632 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5633 XSETFRAME (inev.ie.frame_or_window, f); \
5635 while (0)
5637 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5638 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5641 enum
5643 X_EVENT_NORMAL,
5644 X_EVENT_GOTO_OUT,
5645 X_EVENT_DROP
5648 /* Filter events for the current X input method.
5649 DPYINFO is the display this event is for.
5650 EVENT is the X event to filter.
5652 Returns non-zero if the event was filtered, caller shall not process
5653 this event further.
5654 Returns zero if event is wasn't filtered. */
5656 #ifdef HAVE_X_I18N
5657 static int
5658 x_filter_event (dpyinfo, event)
5659 struct x_display_info *dpyinfo;
5660 XEvent *event;
5662 /* XFilterEvent returns non-zero if the input method has
5663 consumed the event. We pass the frame's X window to
5664 XFilterEvent because that's the one for which the IC
5665 was created. */
5667 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5668 event->xclient.window);
5670 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5672 #endif
5674 #ifdef USE_GTK
5675 static int current_count;
5676 static int current_finish;
5677 static struct input_event *current_hold_quit;
5679 /* This is the filter function invoked by the GTK event loop.
5680 It is invoked before the XEvent is translated to a GdkEvent,
5681 so we have a chance to act on the event before GTK. */
5682 static GdkFilterReturn
5683 event_handler_gdk (gxev, ev, data)
5684 GdkXEvent *gxev;
5685 GdkEvent *ev;
5686 gpointer data;
5688 XEvent *xev = (XEvent *) gxev;
5690 if (current_count >= 0)
5692 struct x_display_info *dpyinfo;
5694 dpyinfo = x_display_info_for_display (xev->xany.display);
5696 #ifdef HAVE_X_I18N
5697 /* Filter events for the current X input method.
5698 GTK calls XFilterEvent but not for key press and release,
5699 so we do it here. */
5700 if (xev->type == KeyPress || xev->type == KeyRelease)
5701 if (dpyinfo && x_filter_event (dpyinfo, xev))
5702 return GDK_FILTER_REMOVE;
5703 #endif
5705 if (! dpyinfo)
5706 current_finish = X_EVENT_NORMAL;
5707 else
5709 current_count +=
5710 handle_one_xevent (dpyinfo, xev, &current_finish,
5711 current_hold_quit);
5714 else
5715 current_finish = x_dispatch_event (xev, xev->xany.display);
5717 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5718 return GDK_FILTER_REMOVE;
5720 return GDK_FILTER_CONTINUE;
5722 #endif /* USE_GTK */
5725 /* Handles the XEvent EVENT on display DPYINFO.
5727 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5728 *FINISH is zero if caller should continue reading events.
5729 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5731 We return the number of characters stored into the buffer. */
5733 static int
5734 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5735 struct x_display_info *dpyinfo;
5736 XEvent *eventp;
5737 int *finish;
5738 struct input_event *hold_quit;
5740 union {
5741 struct input_event ie;
5742 struct selection_input_event sie;
5743 } inev;
5744 int count = 0;
5745 int do_help = 0;
5746 int nbytes = 0;
5747 struct frame *f = NULL;
5748 struct coding_system coding;
5749 XEvent event = *eventp;
5751 *finish = X_EVENT_NORMAL;
5753 EVENT_INIT (inev.ie);
5754 inev.ie.kind = NO_EVENT;
5755 inev.ie.arg = Qnil;
5757 switch (event.type)
5759 case ClientMessage:
5761 if (event.xclient.message_type
5762 == dpyinfo->Xatom_wm_protocols
5763 && event.xclient.format == 32)
5765 if (event.xclient.data.l[0]
5766 == dpyinfo->Xatom_wm_take_focus)
5768 /* Use x_any_window_to_frame because this
5769 could be the shell widget window
5770 if the frame has no title bar. */
5771 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5772 #ifdef HAVE_X_I18N
5773 /* Not quite sure this is needed -pd */
5774 if (f && FRAME_XIC (f))
5775 XSetICFocus (FRAME_XIC (f));
5776 #endif
5777 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5778 instructs the WM to set the input focus automatically for
5779 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5780 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5781 it has set the focus. So, XSetInputFocus below is not
5782 needed.
5784 The call to XSetInputFocus below has also caused trouble. In
5785 cases where the XSetInputFocus done by the WM and the one
5786 below are temporally close (on a fast machine), the call
5787 below can generate additional FocusIn events which confuse
5788 Emacs. */
5790 /* Since we set WM_TAKE_FOCUS, we must call
5791 XSetInputFocus explicitly. But not if f is null,
5792 since that might be an event for a deleted frame. */
5793 if (f)
5795 Display *d = event.xclient.display;
5796 /* Catch and ignore errors, in case window has been
5797 iconified by a window manager such as GWM. */
5798 x_catch_errors (d);
5799 XSetInputFocus (d, event.xclient.window,
5800 /* The ICCCM says this is
5801 the only valid choice. */
5802 RevertToParent,
5803 event.xclient.data.l[1]);
5804 /* This is needed to detect the error
5805 if there is an error. */
5806 XSync (d, False);
5807 x_uncatch_errors ();
5809 /* Not certain about handling scroll bars here */
5810 #endif /* 0 */
5811 goto done;
5814 if (event.xclient.data.l[0]
5815 == dpyinfo->Xatom_wm_save_yourself)
5817 /* Save state modify the WM_COMMAND property to
5818 something which can reinstate us. This notifies
5819 the session manager, who's looking for such a
5820 PropertyNotify. Can restart processing when
5821 a keyboard or mouse event arrives. */
5822 /* If we have a session manager, don't set this.
5823 KDE will then start two Emacsen, one for the
5824 session manager and one for this. */
5825 #ifdef HAVE_X_SM
5826 if (! x_session_have_connection ())
5827 #endif
5829 f = x_top_window_to_frame (dpyinfo,
5830 event.xclient.window);
5831 /* This is just so we only give real data once
5832 for a single Emacs process. */
5833 if (f == SELECTED_FRAME ())
5834 XSetCommand (FRAME_X_DISPLAY (f),
5835 event.xclient.window,
5836 initial_argv, initial_argc);
5837 else if (f)
5838 XSetCommand (FRAME_X_DISPLAY (f),
5839 event.xclient.window,
5840 0, 0);
5842 goto done;
5845 if (event.xclient.data.l[0]
5846 == dpyinfo->Xatom_wm_delete_window)
5848 f = x_any_window_to_frame (dpyinfo,
5849 event.xclient.window);
5850 if (!f)
5851 goto OTHER; /* May be a dialog that is to be removed */
5853 inev.ie.kind = DELETE_WINDOW_EVENT;
5854 XSETFRAME (inev.ie.frame_or_window, f);
5855 goto done;
5858 goto done;
5861 if (event.xclient.message_type
5862 == dpyinfo->Xatom_wm_configure_denied)
5864 goto done;
5867 if (event.xclient.message_type
5868 == dpyinfo->Xatom_wm_window_moved)
5870 int new_x, new_y;
5871 f = x_window_to_frame (dpyinfo, event.xclient.window);
5873 new_x = event.xclient.data.s[0];
5874 new_y = event.xclient.data.s[1];
5876 if (f)
5878 f->left_pos = new_x;
5879 f->top_pos = new_y;
5881 goto done;
5884 #ifdef HACK_EDITRES
5885 if (event.xclient.message_type
5886 == dpyinfo->Xatom_editres)
5888 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5889 if (f)
5890 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5891 &event, NULL);
5892 goto done;
5894 #endif /* HACK_EDITRES */
5896 if ((event.xclient.message_type
5897 == dpyinfo->Xatom_DONE)
5898 || (event.xclient.message_type
5899 == dpyinfo->Xatom_PAGE))
5901 /* Ghostview job completed. Kill it. We could
5902 reply with "Next" if we received "Page", but we
5903 currently never do because we are interested in
5904 images, only, which should have 1 page. */
5905 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5906 f = x_window_to_frame (dpyinfo, event.xclient.window);
5907 if (!f)
5908 goto OTHER;
5909 x_kill_gs_process (pixmap, f);
5910 expose_frame (f, 0, 0, 0, 0);
5911 goto done;
5914 #ifdef USE_TOOLKIT_SCROLL_BARS
5915 /* Scroll bar callbacks send a ClientMessage from which
5916 we construct an input_event. */
5917 if (event.xclient.message_type
5918 == dpyinfo->Xatom_Scrollbar)
5920 x_scroll_bar_to_input_event (&event, &inev.ie);
5921 *finish = X_EVENT_GOTO_OUT;
5922 goto done;
5924 #endif /* USE_TOOLKIT_SCROLL_BARS */
5926 /* XEmbed messages from the embedder (if any). */
5927 if (event.xclient.message_type
5928 == dpyinfo->Xatom_XEMBED)
5930 enum xembed_message msg = event.xclient.data.l[1];
5931 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
5932 x_detect_focus_change (dpyinfo, &event, &inev.ie);
5934 *finish = X_EVENT_GOTO_OUT;
5935 goto done;
5938 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5939 if (!f)
5940 goto OTHER;
5941 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
5942 *finish = X_EVENT_DROP;
5944 break;
5946 case SelectionNotify:
5947 last_user_time = event.xselection.time;
5948 #ifdef USE_X_TOOLKIT
5949 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
5950 goto OTHER;
5951 #endif /* not USE_X_TOOLKIT */
5952 x_handle_selection_notify (&event.xselection);
5953 break;
5955 case SelectionClear: /* Someone has grabbed ownership. */
5956 last_user_time = event.xselectionclear.time;
5957 #ifdef USE_X_TOOLKIT
5958 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
5959 goto OTHER;
5960 #endif /* USE_X_TOOLKIT */
5962 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
5964 inev.ie.kind = SELECTION_CLEAR_EVENT;
5965 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5966 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5967 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5968 inev.ie.frame_or_window = Qnil;
5970 break;
5972 case SelectionRequest: /* Someone wants our selection. */
5973 last_user_time = event.xselectionrequest.time;
5974 #ifdef USE_X_TOOLKIT
5975 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
5976 goto OTHER;
5977 #endif /* USE_X_TOOLKIT */
5979 XSelectionRequestEvent *eventp
5980 = (XSelectionRequestEvent *) &event;
5982 inev.ie.kind = SELECTION_REQUEST_EVENT;
5983 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5984 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
5985 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5986 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
5987 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
5988 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5989 inev.ie.frame_or_window = Qnil;
5991 break;
5993 case PropertyNotify:
5994 last_user_time = event.xproperty.time;
5995 #if 0 /* This is plain wrong. In the case that we are waiting for a
5996 PropertyNotify used as an ACK in incremental selection
5997 transfer, the property will be on the receiver's window. */
5998 #if defined USE_X_TOOLKIT
5999 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
6000 goto OTHER;
6001 #endif
6002 #endif
6003 x_handle_property_notify (&event.xproperty);
6004 goto OTHER;
6006 case ReparentNotify:
6007 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6008 if (f)
6010 int x, y;
6011 f->output_data.x->parent_desc = event.xreparent.parent;
6012 x_real_positions (f, &x, &y);
6013 f->left_pos = x;
6014 f->top_pos = y;
6016 /* Perhaps reparented due to a WM restart. Reset this. */
6017 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6018 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6020 goto OTHER;
6022 case Expose:
6023 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6024 if (f)
6026 x_check_fullscreen (f);
6028 #ifdef USE_GTK
6029 /* This seems to be needed for GTK 2.6. */
6030 x_clear_area (event.xexpose.display,
6031 event.xexpose.window,
6032 event.xexpose.x, event.xexpose.y,
6033 event.xexpose.width, event.xexpose.height,
6034 FALSE);
6035 #endif
6036 if (f->async_visible == 0)
6038 f->async_visible = 1;
6039 f->async_iconified = 0;
6040 f->output_data.x->has_been_visible = 1;
6041 SET_FRAME_GARBAGED (f);
6043 else
6044 expose_frame (f,
6045 event.xexpose.x, event.xexpose.y,
6046 event.xexpose.width, event.xexpose.height);
6048 else
6050 #ifndef USE_TOOLKIT_SCROLL_BARS
6051 struct scroll_bar *bar;
6052 #endif
6053 #if defined USE_LUCID
6054 /* Submenus of the Lucid menu bar aren't widgets
6055 themselves, so there's no way to dispatch events
6056 to them. Recognize this case separately. */
6058 Widget widget
6059 = x_window_to_menu_bar (event.xexpose.window);
6060 if (widget)
6061 xlwmenu_redisplay (widget);
6063 #endif /* USE_LUCID */
6065 #ifdef USE_TOOLKIT_SCROLL_BARS
6066 /* Dispatch event to the widget. */
6067 goto OTHER;
6068 #else /* not USE_TOOLKIT_SCROLL_BARS */
6069 bar = x_window_to_scroll_bar (event.xexpose.display,
6070 event.xexpose.window);
6072 if (bar)
6073 x_scroll_bar_expose (bar, &event);
6074 #ifdef USE_X_TOOLKIT
6075 else
6076 goto OTHER;
6077 #endif /* USE_X_TOOLKIT */
6078 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6080 break;
6082 case GraphicsExpose: /* This occurs when an XCopyArea's
6083 source area was obscured or not
6084 available. */
6085 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6086 if (f)
6088 expose_frame (f,
6089 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6090 event.xgraphicsexpose.width,
6091 event.xgraphicsexpose.height);
6093 #ifdef USE_X_TOOLKIT
6094 else
6095 goto OTHER;
6096 #endif /* USE_X_TOOLKIT */
6097 break;
6099 case NoExpose: /* This occurs when an XCopyArea's
6100 source area was completely
6101 available. */
6102 break;
6104 case UnmapNotify:
6105 /* Redo the mouse-highlight after the tooltip has gone. */
6106 if (event.xmap.window == tip_window)
6108 tip_window = 0;
6109 redo_mouse_highlight ();
6112 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6113 if (f) /* F may no longer exist if
6114 the frame was deleted. */
6116 /* While a frame is unmapped, display generation is
6117 disabled; you don't want to spend time updating a
6118 display that won't ever be seen. */
6119 f->async_visible = 0;
6120 /* We can't distinguish, from the event, whether the window
6121 has become iconified or invisible. So assume, if it
6122 was previously visible, than now it is iconified.
6123 But x_make_frame_invisible clears both
6124 the visible flag and the iconified flag;
6125 and that way, we know the window is not iconified now. */
6126 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6128 f->async_iconified = 1;
6130 inev.ie.kind = ICONIFY_EVENT;
6131 XSETFRAME (inev.ie.frame_or_window, f);
6134 goto OTHER;
6136 case MapNotify:
6137 if (event.xmap.window == tip_window)
6138 /* The tooltip has been drawn already. Avoid
6139 the SET_FRAME_GARBAGED below. */
6140 goto OTHER;
6142 /* We use x_top_window_to_frame because map events can
6143 come for sub-windows and they don't mean that the
6144 frame is visible. */
6145 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6146 if (f)
6148 /* wait_reading_process_output will notice this and update
6149 the frame's display structures.
6150 If we where iconified, we should not set garbaged,
6151 because that stops redrawing on Expose events. This looks
6152 bad if we are called from a recursive event loop
6153 (x_dispatch_event), for example when a dialog is up. */
6154 if (! f->async_iconified)
6155 SET_FRAME_GARBAGED (f);
6157 f->async_visible = 1;
6158 f->async_iconified = 0;
6159 f->output_data.x->has_been_visible = 1;
6161 if (f->iconified)
6163 inev.ie.kind = DEICONIFY_EVENT;
6164 XSETFRAME (inev.ie.frame_or_window, f);
6166 else if (! NILP (Vframe_list)
6167 && ! NILP (XCDR (Vframe_list)))
6168 /* Force a redisplay sooner or later
6169 to update the frame titles
6170 in case this is the second frame. */
6171 record_asynch_buffer_change ();
6173 goto OTHER;
6175 case KeyPress:
6177 last_user_time = event.xkey.time;
6178 ignore_next_mouse_click_timeout = 0;
6180 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6181 /* Dispatch KeyPress events when in menu. */
6182 if (popup_activated ())
6183 goto OTHER;
6184 #endif
6186 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6188 /* If mouse-highlight is an integer, input clears out
6189 mouse highlighting. */
6190 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6191 && (f == 0
6192 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6194 clear_mouse_face (dpyinfo);
6195 dpyinfo->mouse_face_hidden = 1;
6198 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6199 if (f == 0)
6201 /* Scroll bars consume key events, but we want
6202 the keys to go to the scroll bar's frame. */
6203 Widget widget = XtWindowToWidget (dpyinfo->display,
6204 event.xkey.window);
6205 if (widget && XmIsScrollBar (widget))
6207 widget = XtParent (widget);
6208 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6211 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6213 if (f != 0)
6215 KeySym keysym, orig_keysym;
6216 /* al%imercury@uunet.uu.net says that making this 81
6217 instead of 80 fixed a bug whereby meta chars made
6218 his Emacs hang.
6220 It seems that some version of XmbLookupString has
6221 a bug of not returning XBufferOverflow in
6222 status_return even if the input is too long to
6223 fit in 81 bytes. So, we must prepare sufficient
6224 bytes for copy_buffer. 513 bytes (256 chars for
6225 two-byte character set) seems to be a fairly good
6226 approximation. -- 2000.8.10 handa@etl.go.jp */
6227 unsigned char copy_buffer[513];
6228 unsigned char *copy_bufptr = copy_buffer;
6229 int copy_bufsiz = sizeof (copy_buffer);
6230 int modifiers;
6231 Lisp_Object coding_system = Qlatin_1;
6232 Lisp_Object c;
6234 #ifdef USE_GTK
6235 /* Don't pass keys to GTK. A Tab will shift focus to the
6236 tool bar in GTK 2.4. Keys will still go to menus and
6237 dialogs because in that case popup_activated is TRUE
6238 (see above). */
6239 *finish = X_EVENT_DROP;
6240 #endif
6242 event.xkey.state
6243 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6244 extra_keyboard_modifiers);
6245 modifiers = event.xkey.state;
6247 /* This will have to go some day... */
6249 /* make_lispy_event turns chars into control chars.
6250 Don't do it here because XLookupString is too eager. */
6251 event.xkey.state &= ~ControlMask;
6252 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6253 | dpyinfo->super_mod_mask
6254 | dpyinfo->hyper_mod_mask
6255 | dpyinfo->alt_mod_mask);
6257 /* In case Meta is ComposeCharacter,
6258 clear its status. According to Markus Ehrnsperger
6259 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6260 this enables ComposeCharacter to work whether or
6261 not it is combined with Meta. */
6262 if (modifiers & dpyinfo->meta_mod_mask)
6263 bzero (&compose_status, sizeof (compose_status));
6265 #ifdef HAVE_X_I18N
6266 if (FRAME_XIC (f))
6268 Status status_return;
6270 coding_system = Vlocale_coding_system;
6271 nbytes = XmbLookupString (FRAME_XIC (f),
6272 &event.xkey, copy_bufptr,
6273 copy_bufsiz, &keysym,
6274 &status_return);
6275 if (status_return == XBufferOverflow)
6277 copy_bufsiz = nbytes + 1;
6278 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6279 nbytes = XmbLookupString (FRAME_XIC (f),
6280 &event.xkey, copy_bufptr,
6281 copy_bufsiz, &keysym,
6282 &status_return);
6284 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6285 #if 0 && defined X_HAVE_UTF8_STRING
6286 else if (status_return == XLookupKeySym)
6287 { /* Try again but with utf-8. */
6288 coding_system = Qutf_8;
6289 nbytes = Xutf8LookupString (FRAME_XIC (f),
6290 &event.xkey, copy_bufptr,
6291 copy_bufsiz, &keysym,
6292 &status_return);
6293 if (status_return == XBufferOverflow)
6295 copy_bufsiz = nbytes + 1;
6296 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6297 nbytes = Xutf8LookupString (FRAME_XIC (f),
6298 &event.xkey,
6299 copy_bufptr,
6300 copy_bufsiz, &keysym,
6301 &status_return);
6304 #endif
6306 if (status_return == XLookupNone)
6307 break;
6308 else if (status_return == XLookupChars)
6310 keysym = NoSymbol;
6311 modifiers = 0;
6313 else if (status_return != XLookupKeySym
6314 && status_return != XLookupBoth)
6315 abort ();
6317 else
6318 nbytes = XLookupString (&event.xkey, copy_bufptr,
6319 copy_bufsiz, &keysym,
6320 &compose_status);
6321 #else
6322 nbytes = XLookupString (&event.xkey, copy_bufptr,
6323 copy_bufsiz, &keysym,
6324 &compose_status);
6325 #endif
6327 /* If not using XIM/XIC, and a compose sequence is in progress,
6328 we break here. Otherwise, chars_matched is always 0. */
6329 if (compose_status.chars_matched > 0 && nbytes == 0)
6330 break;
6332 bzero (&compose_status, sizeof (compose_status));
6333 orig_keysym = keysym;
6335 /* Common for all keysym input events. */
6336 XSETFRAME (inev.ie.frame_or_window, f);
6337 inev.ie.modifiers
6338 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6339 inev.ie.timestamp = event.xkey.time;
6341 /* First deal with keysyms which have defined
6342 translations to characters. */
6343 if (keysym >= 32 && keysym < 128)
6344 /* Avoid explicitly decoding each ASCII character. */
6346 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6347 inev.ie.code = keysym;
6348 goto done_keysym;
6351 /* Keysyms directly mapped to Unicode characters. */
6352 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6354 if (keysym < 0x01000080)
6355 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6356 else
6357 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6358 inev.ie.code = keysym & 0xFFFFFF;
6359 goto done_keysym;
6362 /* Now non-ASCII. */
6363 if (HASH_TABLE_P (Vx_keysym_table)
6364 && (NATNUMP (c = Fgethash (make_number (keysym),
6365 Vx_keysym_table,
6366 Qnil))))
6368 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6369 ? ASCII_KEYSTROKE_EVENT
6370 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6371 inev.ie.code = XFASTINT (c);
6372 goto done_keysym;
6375 /* Random non-modifier sorts of keysyms. */
6376 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6377 || keysym == XK_Delete
6378 #ifdef XK_ISO_Left_Tab
6379 || (keysym >= XK_ISO_Left_Tab
6380 && keysym <= XK_ISO_Enter)
6381 #endif
6382 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6383 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6384 #ifdef HPUX
6385 /* This recognizes the "extended function
6386 keys". It seems there's no cleaner way.
6387 Test IsModifierKey to avoid handling
6388 mode_switch incorrectly. */
6389 || ((unsigned) (keysym) >= XK_Select
6390 && (unsigned)(keysym) < XK_KP_Space)
6391 #endif
6392 #ifdef XK_dead_circumflex
6393 || orig_keysym == XK_dead_circumflex
6394 #endif
6395 #ifdef XK_dead_grave
6396 || orig_keysym == XK_dead_grave
6397 #endif
6398 #ifdef XK_dead_tilde
6399 || orig_keysym == XK_dead_tilde
6400 #endif
6401 #ifdef XK_dead_diaeresis
6402 || orig_keysym == XK_dead_diaeresis
6403 #endif
6404 #ifdef XK_dead_macron
6405 || orig_keysym == XK_dead_macron
6406 #endif
6407 #ifdef XK_dead_degree
6408 || orig_keysym == XK_dead_degree
6409 #endif
6410 #ifdef XK_dead_acute
6411 || orig_keysym == XK_dead_acute
6412 #endif
6413 #ifdef XK_dead_cedilla
6414 || orig_keysym == XK_dead_cedilla
6415 #endif
6416 #ifdef XK_dead_breve
6417 || orig_keysym == XK_dead_breve
6418 #endif
6419 #ifdef XK_dead_ogonek
6420 || orig_keysym == XK_dead_ogonek
6421 #endif
6422 #ifdef XK_dead_caron
6423 || orig_keysym == XK_dead_caron
6424 #endif
6425 #ifdef XK_dead_doubleacute
6426 || orig_keysym == XK_dead_doubleacute
6427 #endif
6428 #ifdef XK_dead_abovedot
6429 || orig_keysym == XK_dead_abovedot
6430 #endif
6431 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6432 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6433 /* Any "vendor-specific" key is ok. */
6434 || (orig_keysym & (1 << 28))
6435 || (keysym != NoSymbol && nbytes == 0))
6436 && ! (IsModifierKey (orig_keysym)
6437 /* The symbols from XK_ISO_Lock
6438 to XK_ISO_Last_Group_Lock
6439 don't have real modifiers but
6440 should be treated similarly to
6441 Mode_switch by Emacs. */
6442 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6443 || ((unsigned)(orig_keysym)
6444 >= XK_ISO_Lock
6445 && (unsigned)(orig_keysym)
6446 <= XK_ISO_Last_Group_Lock)
6447 #endif
6450 STORE_KEYSYM_FOR_DEBUG (keysym);
6451 /* make_lispy_event will convert this to a symbolic
6452 key. */
6453 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6454 inev.ie.code = keysym;
6455 goto done_keysym;
6458 { /* Raw bytes, not keysym. */
6459 register int i;
6460 register int c;
6461 int nchars, len;
6463 for (i = 0, nchars = 0; i < nbytes; i++)
6465 if (ASCII_BYTE_P (copy_bufptr[i]))
6466 nchars++;
6467 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6470 if (nchars < nbytes)
6472 /* Decode the input data. */
6473 int require;
6474 unsigned char *p;
6476 /* The input should be decoded with `coding_system'
6477 which depends on which X*LookupString function
6478 we used just above and the locale. */
6479 setup_coding_system (coding_system, &coding);
6480 coding.src_multibyte = 0;
6481 coding.dst_multibyte = 1;
6482 /* The input is converted to events, thus we can't
6483 handle composition. Anyway, there's no XIM that
6484 gives us composition information. */
6485 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6487 require = MAX_MULTIBYTE_LENGTH * nbytes;
6488 coding.destination = alloca (require);
6489 coding.dst_bytes = require;
6490 coding.mode |= CODING_MODE_LAST_BLOCK;
6491 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6492 nbytes = coding.produced;
6493 nchars = coding.produced_char;
6494 copy_bufptr = coding.destination;
6497 /* Convert the input data to a sequence of
6498 character events. */
6499 for (i = 0; i < nbytes; i += len)
6501 if (nchars == nbytes)
6502 c = copy_bufptr[i], len = 1;
6503 else
6504 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6505 nbytes - i, len);
6506 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6507 ? ASCII_KEYSTROKE_EVENT
6508 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6509 inev.ie.code = c;
6510 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6513 count += nchars;
6515 inev.ie.kind = NO_EVENT; /* Already stored above. */
6517 if (keysym == NoSymbol)
6518 break;
6521 done_keysym:
6522 #ifdef HAVE_X_I18N
6523 /* Don't dispatch this event since XtDispatchEvent calls
6524 XFilterEvent, and two calls in a row may freeze the
6525 client. */
6526 break;
6527 #else
6528 goto OTHER;
6529 #endif
6531 case KeyRelease:
6532 last_user_time = event.xkey.time;
6533 #ifdef HAVE_X_I18N
6534 /* Don't dispatch this event since XtDispatchEvent calls
6535 XFilterEvent, and two calls in a row may freeze the
6536 client. */
6537 break;
6538 #else
6539 goto OTHER;
6540 #endif
6542 case EnterNotify:
6543 last_user_time = event.xcrossing.time;
6544 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6546 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6548 if (f && x_mouse_click_focus_ignore_position)
6549 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6551 #if 0
6552 if (event.xcrossing.focus)
6554 /* Avoid nasty pop/raise loops. */
6555 if (f && (!(f->auto_raise)
6556 || !(f->auto_lower)
6557 || (event.xcrossing.time - enter_timestamp) > 500))
6559 x_new_focus_frame (dpyinfo, f);
6560 enter_timestamp = event.xcrossing.time;
6563 else if (f == dpyinfo->x_focus_frame)
6564 x_new_focus_frame (dpyinfo, 0);
6565 #endif
6567 /* EnterNotify counts as mouse movement,
6568 so update things that depend on mouse position. */
6569 if (f && !f->output_data.x->hourglass_p)
6570 note_mouse_movement (f, &event.xmotion);
6571 #ifdef USE_GTK
6572 /* We may get an EnterNotify on the buttons in the toolbar. In that
6573 case we moved out of any highlighted area and need to note this. */
6574 if (!f && last_mouse_glyph_frame)
6575 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6576 #endif
6577 goto OTHER;
6579 case FocusIn:
6580 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6581 goto OTHER;
6583 case LeaveNotify:
6584 last_user_time = event.xcrossing.time;
6585 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6587 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6588 if (f)
6590 if (f == dpyinfo->mouse_face_mouse_frame)
6592 /* If we move outside the frame, then we're
6593 certainly no longer on any text in the frame. */
6594 clear_mouse_face (dpyinfo);
6595 dpyinfo->mouse_face_mouse_frame = 0;
6598 /* Generate a nil HELP_EVENT to cancel a help-echo.
6599 Do it only if there's something to cancel.
6600 Otherwise, the startup message is cleared when
6601 the mouse leaves the frame. */
6602 if (any_help_event_p)
6603 do_help = -1;
6605 #ifdef USE_GTK
6606 /* See comment in EnterNotify above */
6607 else if (last_mouse_glyph_frame)
6608 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6609 #endif
6610 goto OTHER;
6612 case FocusOut:
6613 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6614 goto OTHER;
6616 case MotionNotify:
6618 last_user_time = event.xmotion.time;
6619 previous_help_echo_string = help_echo_string;
6620 help_echo_string = Qnil;
6622 if (dpyinfo->grabbed && last_mouse_frame
6623 && FRAME_LIVE_P (last_mouse_frame))
6624 f = last_mouse_frame;
6625 else
6626 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6628 if (dpyinfo->mouse_face_hidden)
6630 dpyinfo->mouse_face_hidden = 0;
6631 clear_mouse_face (dpyinfo);
6634 if (f)
6637 /* Generate SELECT_WINDOW_EVENTs when needed. */
6638 if (!NILP (Vmouse_autoselect_window))
6640 Lisp_Object window;
6642 window = window_from_coordinates (f,
6643 event.xmotion.x, event.xmotion.y,
6644 0, 0, 0, 0);
6646 /* Window will be selected only when it is not selected now and
6647 last mouse movement event was not in it. Minibuffer window
6648 will be selected only when it is active. */
6649 if (WINDOWP (window)
6650 && !EQ (window, last_window)
6651 && !EQ (window, selected_window)
6652 /* For click-to-focus window managers
6653 create event iff we don't leave the
6654 selected frame. */
6655 && (focus_follows_mouse
6656 || (EQ (XWINDOW (window)->frame,
6657 XWINDOW (selected_window)->frame))))
6659 inev.ie.kind = SELECT_WINDOW_EVENT;
6660 inev.ie.frame_or_window = window;
6663 last_window=window;
6665 if (!note_mouse_movement (f, &event.xmotion))
6666 help_echo_string = previous_help_echo_string;
6668 else
6670 #ifndef USE_TOOLKIT_SCROLL_BARS
6671 struct scroll_bar *bar
6672 = x_window_to_scroll_bar (event.xmotion.display,
6673 event.xmotion.window);
6675 if (bar)
6676 x_scroll_bar_note_movement (bar, &event);
6677 #endif /* USE_TOOLKIT_SCROLL_BARS */
6679 /* If we move outside the frame, then we're
6680 certainly no longer on any text in the frame. */
6681 clear_mouse_face (dpyinfo);
6684 /* If the contents of the global variable help_echo_string
6685 has changed, generate a HELP_EVENT. */
6686 if (!NILP (help_echo_string)
6687 || !NILP (previous_help_echo_string))
6688 do_help = 1;
6689 goto OTHER;
6692 case ConfigureNotify:
6693 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6694 #ifdef USE_GTK
6695 if (!f
6696 && (f = x_any_window_to_frame (dpyinfo, event.xconfigure.window))
6697 && event.xconfigure.window == FRAME_X_WINDOW (f))
6699 xg_frame_resized (f, event.xconfigure.width,
6700 event.xconfigure.height);
6701 f = 0;
6703 #endif
6704 if (f)
6706 #ifndef USE_X_TOOLKIT
6707 #ifndef USE_GTK
6708 /* If there is a pending resize for fullscreen, don't
6709 do this one, the right one will come later.
6710 The toolkit version doesn't seem to need this, but we
6711 need to reset it below. */
6712 int dont_resize
6713 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6714 && f->new_text_cols != 0);
6715 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6716 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6718 if (dont_resize)
6719 goto OTHER;
6721 /* In the toolkit version, change_frame_size
6722 is called by the code that handles resizing
6723 of the EmacsFrame widget. */
6725 /* Even if the number of character rows and columns has
6726 not changed, the font size may have changed, so we need
6727 to check the pixel dimensions as well. */
6728 if (columns != FRAME_COLS (f)
6729 || rows != FRAME_LINES (f)
6730 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6731 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6733 change_frame_size (f, rows, columns, 0, 1, 0);
6734 SET_FRAME_GARBAGED (f);
6735 cancel_mouse_face (f);
6738 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6739 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6740 #endif /* not USE_GTK */
6741 #endif
6743 #ifdef USE_GTK
6744 /* GTK creates windows but doesn't map them.
6745 Only get real positions and check fullscreen when mapped. */
6746 if (FRAME_GTK_OUTER_WIDGET (f)
6747 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6748 #endif
6750 x_real_positions (f, &f->left_pos, &f->top_pos);
6752 if (f->want_fullscreen & FULLSCREEN_WAIT)
6753 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6756 #ifdef HAVE_X_I18N
6757 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6758 xic_set_statusarea (f);
6759 #endif
6761 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6763 /* Since the WM decorations come below top_pos now,
6764 we must put them below top_pos in the future. */
6765 f->win_gravity = NorthWestGravity;
6766 x_wm_set_size_hint (f, (long) 0, 0);
6769 goto OTHER;
6771 case ButtonRelease:
6772 case ButtonPress:
6774 /* If we decide we want to generate an event to be seen
6775 by the rest of Emacs, we put it here. */
6776 int tool_bar_p = 0;
6778 bzero (&compose_status, sizeof (compose_status));
6779 last_mouse_glyph_frame = 0;
6780 last_user_time = event.xbutton.time;
6782 if (dpyinfo->grabbed
6783 && last_mouse_frame
6784 && FRAME_LIVE_P (last_mouse_frame))
6785 f = last_mouse_frame;
6786 else
6787 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6789 if (f)
6791 /* Is this in the tool-bar? */
6792 if (WINDOWP (f->tool_bar_window)
6793 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6795 Lisp_Object window;
6796 int x = event.xbutton.x;
6797 int y = event.xbutton.y;
6799 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6800 tool_bar_p = EQ (window, f->tool_bar_window);
6802 if (tool_bar_p && event.xbutton.button < 4)
6804 handle_tool_bar_click (f, x, y,
6805 event.xbutton.type == ButtonPress,
6806 x_x_to_emacs_modifiers (dpyinfo,
6807 event.xbutton.state));
6811 if (!tool_bar_p)
6812 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6813 if (! popup_activated ())
6814 #endif
6816 if (ignore_next_mouse_click_timeout)
6818 if (event.type == ButtonPress
6819 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6821 ignore_next_mouse_click_timeout = 0;
6822 construct_mouse_click (&inev.ie, &event.xbutton, f);
6824 if (event.type == ButtonRelease)
6825 ignore_next_mouse_click_timeout = 0;
6827 else
6828 construct_mouse_click (&inev.ie, &event.xbutton, f);
6830 if (FRAME_X_EMBEDDED_P (f))
6831 xembed_send_message (f, event.xbutton.time,
6832 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6834 else
6836 struct scroll_bar *bar
6837 = x_window_to_scroll_bar (event.xbutton.display,
6838 event.xbutton.window);
6840 #ifdef USE_TOOLKIT_SCROLL_BARS
6841 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6842 scroll bars. */
6843 if (bar && event.xbutton.state & ControlMask)
6845 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6846 *finish = X_EVENT_DROP;
6848 #else /* not USE_TOOLKIT_SCROLL_BARS */
6849 if (bar)
6850 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6851 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6854 if (event.type == ButtonPress)
6856 dpyinfo->grabbed |= (1 << event.xbutton.button);
6857 last_mouse_frame = f;
6859 if (!tool_bar_p)
6860 last_tool_bar_item = -1;
6862 else
6863 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6865 /* Ignore any mouse motion that happened before this event;
6866 any subsequent mouse-movement Emacs events should reflect
6867 only motion after the ButtonPress/Release. */
6868 if (f != 0)
6869 f->mouse_moved = 0;
6871 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6872 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6873 /* For a down-event in the menu bar,
6874 don't pass it to Xt right now.
6875 Instead, save it away
6876 and we will pass it to Xt from kbd_buffer_get_event.
6877 That way, we can run some Lisp code first. */
6878 if (
6879 #ifdef USE_GTK
6880 ! popup_activated ()
6881 /* Gtk+ menus only react to the first three buttons. */
6882 && event.xbutton.button < 3
6884 #endif
6885 f && event.type == ButtonPress
6886 /* Verify the event is really within the menu bar
6887 and not just sent to it due to grabbing. */
6888 && event.xbutton.x >= 0
6889 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6890 && event.xbutton.y >= 0
6891 && event.xbutton.y < f->output_data.x->menubar_height
6892 && event.xbutton.same_screen)
6894 SET_SAVED_BUTTON_EVENT;
6895 XSETFRAME (last_mouse_press_frame, f);
6896 #ifdef USE_GTK
6897 *finish = X_EVENT_DROP;
6898 #endif
6900 else if (event.type == ButtonPress)
6902 last_mouse_press_frame = Qnil;
6903 goto OTHER;
6906 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6907 but I am trying to be cautious. */
6908 else if (event.type == ButtonRelease)
6910 if (!NILP (last_mouse_press_frame))
6912 f = XFRAME (last_mouse_press_frame);
6913 if (f->output_data.x)
6914 SET_SAVED_BUTTON_EVENT;
6916 else
6917 goto OTHER;
6919 #endif /* USE_MOTIF */
6920 else
6921 goto OTHER;
6922 #endif /* USE_X_TOOLKIT || USE_GTK */
6924 break;
6926 case CirculateNotify:
6927 goto OTHER;
6929 case CirculateRequest:
6930 goto OTHER;
6932 case VisibilityNotify:
6933 goto OTHER;
6935 case MappingNotify:
6936 /* Someone has changed the keyboard mapping - update the
6937 local cache. */
6938 switch (event.xmapping.request)
6940 case MappingModifier:
6941 x_find_modifier_meanings (dpyinfo);
6942 /* This is meant to fall through. */
6943 case MappingKeyboard:
6944 XRefreshKeyboardMapping (&event.xmapping);
6946 goto OTHER;
6948 default:
6949 OTHER:
6950 #ifdef USE_X_TOOLKIT
6951 BLOCK_INPUT;
6952 if (*finish != X_EVENT_DROP)
6953 XtDispatchEvent (&event);
6954 UNBLOCK_INPUT;
6955 #endif /* USE_X_TOOLKIT */
6956 break;
6959 done:
6960 if (inev.ie.kind != NO_EVENT)
6962 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6963 count++;
6966 if (do_help
6967 && !(hold_quit && hold_quit->kind != NO_EVENT))
6969 Lisp_Object frame;
6971 if (f)
6972 XSETFRAME (frame, f);
6973 else
6974 frame = Qnil;
6976 if (do_help > 0)
6978 any_help_event_p = 1;
6979 gen_help_event (help_echo_string, frame, help_echo_window,
6980 help_echo_object, help_echo_pos);
6982 else
6984 help_echo_string = Qnil;
6985 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
6987 count++;
6990 *eventp = event;
6991 return count;
6995 /* Handles the XEvent EVENT on display DISPLAY.
6996 This is used for event loops outside the normal event handling,
6997 i.e. looping while a popup menu or a dialog is posted.
6999 Returns the value handle_one_xevent sets in the finish argument. */
7001 x_dispatch_event (event, display)
7002 XEvent *event;
7003 Display *display;
7005 struct x_display_info *dpyinfo;
7006 int finish = X_EVENT_NORMAL;
7008 dpyinfo = x_display_info_for_display (display);
7010 if (dpyinfo)
7011 handle_one_xevent (dpyinfo, event, &finish, 0);
7013 return finish;
7017 /* Read events coming from the X server.
7018 This routine is called by the SIGIO handler.
7019 We return as soon as there are no more events to be read.
7021 We return the number of characters stored into the buffer,
7022 thus pretending to be `read' (except the characters we store
7023 in the keyboard buffer can be multibyte, so are not necessarily
7024 C chars).
7026 EXPECTED is nonzero if the caller knows input is available. */
7028 static int
7029 XTread_socket (terminal, expected, hold_quit)
7030 struct terminal *terminal;
7031 int expected;
7032 struct input_event *hold_quit;
7034 int count = 0;
7035 XEvent event;
7036 int event_found = 0;
7037 #if 0
7038 struct x_display_info *dpyinfo;
7039 #endif
7041 if (interrupt_input_blocked)
7043 interrupt_input_pending = 1;
7044 return -1;
7047 interrupt_input_pending = 0;
7048 BLOCK_INPUT;
7050 /* So people can tell when we have read the available input. */
7051 input_signal_count++;
7053 ++handling_signal;
7055 #ifdef HAVE_X_SM
7056 /* Only check session manager input for the primary display. */
7057 if (terminal->id == 1 && x_session_have_connection ())
7059 struct input_event inev;
7060 BLOCK_INPUT;
7061 /* We don't need to EVENT_INIT (inev) here, as
7062 x_session_check_input copies an entire input_event. */
7063 if (x_session_check_input (&inev))
7065 kbd_buffer_store_event_hold (&inev, hold_quit);
7066 count++;
7068 UNBLOCK_INPUT;
7070 #endif
7072 /* For debugging, this gives a way to fake an I/O error. */
7073 if (terminal->display_info.x == XTread_socket_fake_io_error)
7075 XTread_socket_fake_io_error = 0;
7076 x_io_error_quitter (terminal->display_info.x->display);
7079 #if 0 /* This loop is a noop now. */
7080 /* Find the display we are supposed to read input for.
7081 It's the one communicating on descriptor SD. */
7082 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7084 #if 0 /* This ought to be unnecessary; let's verify it. */
7085 #ifdef FIOSNBIO
7086 /* If available, Xlib uses FIOSNBIO to make the socket
7087 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7088 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7089 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7090 fcntl (dpyinfo->connection, F_SETFL, 0);
7091 #endif /* ! defined (FIOSNBIO) */
7092 #endif
7094 #if 0 /* This code can't be made to work, with multiple displays,
7095 and appears not to be used on any system any more.
7096 Also keyboard.c doesn't turn O_NDELAY on and off
7097 for X connections. */
7098 #ifndef SIGIO
7099 #ifndef HAVE_SELECT
7100 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7102 extern int read_alarm_should_throw;
7103 read_alarm_should_throw = 1;
7104 XPeekEvent (dpyinfo->display, &event);
7105 read_alarm_should_throw = 0;
7107 #endif /* HAVE_SELECT */
7108 #endif /* SIGIO */
7109 #endif
7111 #endif
7113 #ifndef USE_GTK
7114 while (XPending (terminal->display_info.x->display))
7116 int finish;
7118 XNextEvent (terminal->display_info.x->display, &event);
7120 #ifdef HAVE_X_I18N
7121 /* Filter events for the current X input method. */
7122 if (x_filter_event (terminal->display_info.x, &event))
7123 break;
7124 #endif
7125 event_found = 1;
7127 count += handle_one_xevent (terminal->display_info.x,
7128 &event, &finish, hold_quit);
7130 if (finish == X_EVENT_GOTO_OUT)
7131 goto out;
7134 #else /* USE_GTK */
7136 /* For GTK we must use the GTK event loop. But XEvents gets passed
7137 to our filter function above, and then to the big event switch.
7138 We use a bunch of globals to communicate with our filter function,
7139 that is kind of ugly, but it works.
7141 There is no way to do one display at the time, GTK just does events
7142 from all displays. */
7144 while (gtk_events_pending ())
7146 current_count = count;
7147 current_hold_quit = hold_quit;
7149 gtk_main_iteration ();
7151 count = current_count;
7152 current_count = -1;
7153 current_hold_quit = 0;
7155 if (current_finish == X_EVENT_GOTO_OUT)
7156 break;
7158 #endif /* USE_GTK */
7160 out:;
7162 /* On some systems, an X bug causes Emacs to get no more events
7163 when the window is destroyed. Detect that. (1994.) */
7164 if (! event_found)
7166 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7167 One XNOOP in 100 loops will make Emacs terminate.
7168 B. Bretthauer, 1994 */
7169 x_noop_count++;
7170 if (x_noop_count >= 100)
7172 x_noop_count=0;
7174 if (next_noop_dpyinfo == 0)
7175 next_noop_dpyinfo = x_display_list;
7177 XNoOp (next_noop_dpyinfo->display);
7179 /* Each time we get here, cycle through the displays now open. */
7180 next_noop_dpyinfo = next_noop_dpyinfo->next;
7184 /* If the focus was just given to an auto-raising frame,
7185 raise it now. */
7186 /* ??? This ought to be able to handle more than one such frame. */
7187 if (pending_autoraise_frame)
7189 x_raise_frame (pending_autoraise_frame);
7190 pending_autoraise_frame = 0;
7193 --handling_signal;
7194 UNBLOCK_INPUT;
7196 return count;
7202 /***********************************************************************
7203 Text Cursor
7204 ***********************************************************************/
7206 /* Set clipping for output in glyph row ROW. W is the window in which
7207 we operate. GC is the graphics context to set clipping in.
7209 ROW may be a text row or, e.g., a mode line. Text rows must be
7210 clipped to the interior of the window dedicated to text display,
7211 mode lines must be clipped to the whole window. */
7213 static void
7214 x_clip_to_row (w, row, area, gc)
7215 struct window *w;
7216 struct glyph_row *row;
7217 int area;
7218 GC gc;
7220 struct frame *f = XFRAME (WINDOW_FRAME (w));
7221 XRectangle clip_rect;
7222 int window_x, window_y, window_width;
7224 window_box (w, area, &window_x, &window_y, &window_width, 0);
7226 clip_rect.x = window_x;
7227 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7228 clip_rect.y = max (clip_rect.y, window_y);
7229 clip_rect.width = window_width;
7230 clip_rect.height = row->visible_height;
7232 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7236 /* Draw a hollow box cursor on window W in glyph row ROW. */
7238 static void
7239 x_draw_hollow_cursor (w, row)
7240 struct window *w;
7241 struct glyph_row *row;
7243 struct frame *f = XFRAME (WINDOW_FRAME (w));
7244 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7245 Display *dpy = FRAME_X_DISPLAY (f);
7246 int x, y, wd, h;
7247 XGCValues xgcv;
7248 struct glyph *cursor_glyph;
7249 GC gc;
7251 /* Get the glyph the cursor is on. If we can't tell because
7252 the current matrix is invalid or such, give up. */
7253 cursor_glyph = get_phys_cursor_glyph (w);
7254 if (cursor_glyph == NULL)
7255 return;
7257 /* Compute frame-relative coordinates for phys cursor. */
7258 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7259 wd = w->phys_cursor_width;
7261 /* The foreground of cursor_gc is typically the same as the normal
7262 background color, which can cause the cursor box to be invisible. */
7263 xgcv.foreground = f->output_data.x->cursor_pixel;
7264 if (dpyinfo->scratch_cursor_gc)
7265 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7266 else
7267 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7268 GCForeground, &xgcv);
7269 gc = dpyinfo->scratch_cursor_gc;
7271 /* Set clipping, draw the rectangle, and reset clipping again. */
7272 x_clip_to_row (w, row, TEXT_AREA, gc);
7273 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7274 XSetClipMask (dpy, gc, None);
7278 /* Draw a bar cursor on window W in glyph row ROW.
7280 Implementation note: One would like to draw a bar cursor with an
7281 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7282 Unfortunately, I didn't find a font yet that has this property set.
7283 --gerd. */
7285 static void
7286 x_draw_bar_cursor (w, row, width, kind)
7287 struct window *w;
7288 struct glyph_row *row;
7289 int width;
7290 enum text_cursor_kinds kind;
7292 struct frame *f = XFRAME (w->frame);
7293 struct glyph *cursor_glyph;
7295 /* If cursor is out of bounds, don't draw garbage. This can happen
7296 in mini-buffer windows when switching between echo area glyphs
7297 and mini-buffer. */
7298 cursor_glyph = get_phys_cursor_glyph (w);
7299 if (cursor_glyph == NULL)
7300 return;
7302 /* If on an image, draw like a normal cursor. That's usually better
7303 visible than drawing a bar, esp. if the image is large so that
7304 the bar might not be in the window. */
7305 if (cursor_glyph->type == IMAGE_GLYPH)
7307 struct glyph_row *row;
7308 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7309 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7311 else
7313 Display *dpy = FRAME_X_DISPLAY (f);
7314 Window window = FRAME_X_WINDOW (f);
7315 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7316 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7317 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7318 XGCValues xgcv;
7320 /* If the glyph's background equals the color we normally draw
7321 the bar cursor in, the bar cursor in its normal color is
7322 invisible. Use the glyph's foreground color instead in this
7323 case, on the assumption that the glyph's colors are chosen so
7324 that the glyph is legible. */
7325 if (face->background == f->output_data.x->cursor_pixel)
7326 xgcv.background = xgcv.foreground = face->foreground;
7327 else
7328 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7329 xgcv.graphics_exposures = 0;
7331 if (gc)
7332 XChangeGC (dpy, gc, mask, &xgcv);
7333 else
7335 gc = XCreateGC (dpy, window, mask, &xgcv);
7336 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7339 if (width < 0)
7340 width = FRAME_CURSOR_WIDTH (f);
7341 width = min (cursor_glyph->pixel_width, width);
7343 w->phys_cursor_width = width;
7344 x_clip_to_row (w, row, TEXT_AREA, gc);
7346 if (kind == BAR_CURSOR)
7347 XFillRectangle (dpy, window, gc,
7348 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7349 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7350 width, row->height);
7351 else
7352 XFillRectangle (dpy, window, gc,
7353 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7354 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7355 row->height - width),
7356 cursor_glyph->pixel_width,
7357 width);
7359 XSetClipMask (dpy, gc, None);
7364 /* RIF: Define cursor CURSOR on frame F. */
7366 static void
7367 x_define_frame_cursor (f, cursor)
7368 struct frame *f;
7369 Cursor cursor;
7371 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7375 /* RIF: Clear area on frame F. */
7377 static void
7378 x_clear_frame_area (f, x, y, width, height)
7379 struct frame *f;
7380 int x, y, width, height;
7382 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7383 x, y, width, height, False);
7387 /* RIF: Draw cursor on window W. */
7389 static void
7390 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7391 struct window *w;
7392 struct glyph_row *glyph_row;
7393 int x, y;
7394 int cursor_type, cursor_width;
7395 int on_p, active_p;
7397 struct frame *f = XFRAME (WINDOW_FRAME (w));
7399 if (on_p)
7401 w->phys_cursor_type = cursor_type;
7402 w->phys_cursor_on_p = 1;
7404 if (glyph_row->exact_window_width_line_p
7405 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7407 glyph_row->cursor_in_fringe_p = 1;
7408 draw_fringe_bitmap (w, glyph_row, 0);
7410 else
7411 switch (cursor_type)
7413 case HOLLOW_BOX_CURSOR:
7414 x_draw_hollow_cursor (w, glyph_row);
7415 break;
7417 case FILLED_BOX_CURSOR:
7418 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7419 break;
7421 case BAR_CURSOR:
7422 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7423 break;
7425 case HBAR_CURSOR:
7426 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7427 break;
7429 case NO_CURSOR:
7430 w->phys_cursor_width = 0;
7431 break;
7433 default:
7434 abort ();
7437 #ifdef HAVE_X_I18N
7438 if (w == XWINDOW (f->selected_window))
7439 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7440 xic_set_preeditarea (w, x, y);
7441 #endif
7444 #ifndef XFlush
7445 XFlush (FRAME_X_DISPLAY (f));
7446 #endif
7450 /* Icons. */
7452 /* Make the x-window of frame F use the gnu icon bitmap. */
7455 x_bitmap_icon (f, file)
7456 struct frame *f;
7457 Lisp_Object file;
7459 int bitmap_id;
7461 if (FRAME_X_WINDOW (f) == 0)
7462 return 1;
7464 /* Free up our existing icon bitmap and mask if any. */
7465 if (f->output_data.x->icon_bitmap > 0)
7466 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7467 f->output_data.x->icon_bitmap = 0;
7469 if (STRINGP (file))
7471 #ifdef USE_GTK
7472 /* Use gtk_window_set_icon_from_file () if available,
7473 It's not restricted to bitmaps */
7474 if (xg_set_icon (f, file))
7475 return 0;
7476 #endif /* USE_GTK */
7477 bitmap_id = x_create_bitmap_from_file (f, file);
7478 x_create_bitmap_mask (f, bitmap_id);
7480 else
7482 /* Create the GNU bitmap and mask if necessary. */
7483 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7485 int rc = -1;
7487 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7488 #ifdef USE_GTK
7489 if (xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7490 return 0;
7491 #else
7492 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7493 if (rc != -1)
7494 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7495 #endif /* USE_GTK */
7496 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
7498 /* If all else fails, use the (black and white) xbm image. */
7499 if (rc == -1)
7501 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7502 gnu_xbm_width, gnu_xbm_height);
7503 if (rc == -1)
7504 return 1;
7506 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7507 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7511 /* The first time we create the GNU bitmap and mask,
7512 this increments the ref-count one extra time.
7513 As a result, the GNU bitmap and mask are never freed.
7514 That way, we don't have to worry about allocating it again. */
7515 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7517 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7520 x_wm_set_icon_pixmap (f, bitmap_id);
7521 f->output_data.x->icon_bitmap = bitmap_id;
7523 return 0;
7527 /* Make the x-window of frame F use a rectangle with text.
7528 Use ICON_NAME as the text. */
7531 x_text_icon (f, icon_name)
7532 struct frame *f;
7533 char *icon_name;
7535 if (FRAME_X_WINDOW (f) == 0)
7536 return 1;
7539 XTextProperty text;
7540 text.value = (unsigned char *) icon_name;
7541 text.encoding = XA_STRING;
7542 text.format = 8;
7543 text.nitems = strlen (icon_name);
7544 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7547 if (f->output_data.x->icon_bitmap > 0)
7548 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7549 f->output_data.x->icon_bitmap = 0;
7550 x_wm_set_icon_pixmap (f, 0);
7552 return 0;
7555 #define X_ERROR_MESSAGE_SIZE 200
7557 /* If non-nil, this should be a string.
7558 It means catch X errors and store the error message in this string.
7560 The reason we use a stack is that x_catch_error/x_uncatch_error can
7561 be called from a signal handler.
7564 struct x_error_message_stack {
7565 char string[X_ERROR_MESSAGE_SIZE];
7566 Display *dpy;
7567 struct x_error_message_stack *prev;
7569 static struct x_error_message_stack *x_error_message;
7571 /* An X error handler which stores the error message in
7572 *x_error_message. This is called from x_error_handler if
7573 x_catch_errors is in effect. */
7575 static void
7576 x_error_catcher (display, error)
7577 Display *display;
7578 XErrorEvent *error;
7580 XGetErrorText (display, error->error_code,
7581 x_error_message->string,
7582 X_ERROR_MESSAGE_SIZE);
7585 /* Begin trapping X errors for display DPY. Actually we trap X errors
7586 for all displays, but DPY should be the display you are actually
7587 operating on.
7589 After calling this function, X protocol errors no longer cause
7590 Emacs to exit; instead, they are recorded in the string
7591 stored in *x_error_message.
7593 Calling x_check_errors signals an Emacs error if an X error has
7594 occurred since the last call to x_catch_errors or x_check_errors.
7596 Calling x_uncatch_errors resumes the normal error handling. */
7598 void x_check_errors ();
7600 void
7601 x_catch_errors (dpy)
7602 Display *dpy;
7604 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7606 /* Make sure any errors from previous requests have been dealt with. */
7607 XSync (dpy, False);
7609 data->dpy = dpy;
7610 data->string[0] = 0;
7611 data->prev = x_error_message;
7612 x_error_message = data;
7615 /* Undo the last x_catch_errors call.
7616 DPY should be the display that was passed to x_catch_errors. */
7618 void
7619 x_uncatch_errors ()
7621 struct x_error_message_stack *tmp;
7623 BLOCK_INPUT;
7625 /* The display may have been closed before this function is called.
7626 Check if it is still open before calling XSync. */
7627 if (x_display_info_for_display (x_error_message->dpy) != 0)
7628 XSync (x_error_message->dpy, False);
7630 tmp = x_error_message;
7631 x_error_message = x_error_message->prev;
7632 xfree (tmp);
7633 UNBLOCK_INPUT;
7636 /* If any X protocol errors have arrived since the last call to
7637 x_catch_errors or x_check_errors, signal an Emacs error using
7638 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7640 void
7641 x_check_errors (dpy, format)
7642 Display *dpy;
7643 char *format;
7645 /* Make sure to catch any errors incurred so far. */
7646 XSync (dpy, False);
7648 if (x_error_message->string[0])
7650 char string[X_ERROR_MESSAGE_SIZE];
7651 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7652 x_uncatch_errors ();
7653 error (format, string);
7657 /* Nonzero if we had any X protocol errors
7658 since we did x_catch_errors on DPY. */
7661 x_had_errors_p (dpy)
7662 Display *dpy;
7664 /* Make sure to catch any errors incurred so far. */
7665 XSync (dpy, False);
7667 return x_error_message->string[0] != 0;
7670 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7672 void
7673 x_clear_errors (dpy)
7674 Display *dpy;
7676 x_error_message->string[0] = 0;
7679 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7680 * idea. --lorentey */
7681 /* Close off all unclosed x_catch_errors calls. */
7683 void
7684 x_fully_uncatch_errors ()
7686 while (x_error_message)
7687 x_uncatch_errors ();
7689 #endif
7691 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7694 x_catching_errors ()
7696 return x_error_message != 0;
7699 #if 0
7700 static unsigned int x_wire_count;
7701 x_trace_wire ()
7703 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7705 #endif /* ! 0 */
7708 /* Handle SIGPIPE, which can happen when the connection to a server
7709 simply goes away. SIGPIPE is handled by x_connection_signal.
7710 Don't need to do anything, because the write which caused the
7711 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7712 which will do the appropriate cleanup for us. */
7714 static SIGTYPE
7715 x_connection_signal (signalnum) /* If we don't have an argument, */
7716 int signalnum; /* some compilers complain in signal calls. */
7718 #ifdef USG
7719 /* USG systems forget handlers when they are used;
7720 must reestablish each time */
7721 signal (signalnum, x_connection_signal);
7722 #endif /* USG */
7726 /************************************************************************
7727 Handling X errors
7728 ************************************************************************/
7730 /* Error message passed to x_connection_closed. */
7732 static char *error_msg;
7734 /* Function installed as fatal_error_signal_hook in
7735 x_connection_closed. Print the X error message, and exit normally,
7736 instead of dumping core when XtCloseDisplay fails. */
7738 static void
7739 x_fatal_error_signal ()
7741 fprintf (stderr, "%s\n", error_msg);
7742 exit (70);
7745 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7746 the text of an error message that lead to the connection loss. */
7748 static SIGTYPE
7749 x_connection_closed (dpy, error_message)
7750 Display *dpy;
7751 char *error_message;
7753 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7754 Lisp_Object frame, tail;
7755 int index = SPECPDL_INDEX ();
7757 error_msg = (char *) alloca (strlen (error_message) + 1);
7758 strcpy (error_msg, error_message);
7759 handling_signal = 0;
7761 /* Prevent being called recursively because of an error condition
7762 below. Otherwise, we might end up with printing ``can't find per
7763 display information'' in the recursive call instead of printing
7764 the original message here. */
7765 x_catch_errors (dpy);
7767 /* Inhibit redisplay while frames are being deleted. */
7768 specbind (Qinhibit_redisplay, Qt);
7770 if (dpyinfo)
7772 /* Protect display from being closed when we delete the last
7773 frame on it. */
7774 dpyinfo->reference_count++;
7775 dpyinfo->terminal->reference_count++;
7778 /* First delete frames whose mini-buffers are on frames
7779 that are on the dead display. */
7780 FOR_EACH_FRAME (tail, frame)
7782 Lisp_Object minibuf_frame;
7783 minibuf_frame
7784 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7785 if (FRAME_X_P (XFRAME (frame))
7786 && FRAME_X_P (XFRAME (minibuf_frame))
7787 && ! EQ (frame, minibuf_frame)
7788 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7789 Fdelete_frame (frame, Qnoelisp);
7792 /* Now delete all remaining frames on the dead display.
7793 We are now sure none of these is used as the mini-buffer
7794 for another frame that we need to delete. */
7795 FOR_EACH_FRAME (tail, frame)
7796 if (FRAME_X_P (XFRAME (frame))
7797 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7799 /* Set this to t so that Fdelete_frame won't get confused
7800 trying to find a replacement. */
7801 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7802 Fdelete_frame (frame, Qnoelisp);
7805 /* We have to close the display to inform Xt that it doesn't
7806 exist anymore. If we don't, Xt will continue to wait for
7807 events from the display. As a consequence, a sequence of
7809 M-x make-frame-on-display RET :1 RET
7810 ...kill the new frame, so that we get an IO error...
7811 M-x make-frame-on-display RET :1 RET
7813 will indefinitely wait in Xt for events for display `:1', opened
7814 in the first call to make-frame-on-display.
7816 Closing the display is reported to lead to a bus error on
7817 OpenWindows in certain situations. I suspect that is a bug
7818 in OpenWindows. I don't know how to circumvent it here. */
7820 if (dpyinfo)
7822 #ifdef USE_X_TOOLKIT
7823 /* If DPYINFO is null, this means we didn't open the display
7824 in the first place, so don't try to close it. */
7826 extern void (*fatal_error_signal_hook) P_ ((void));
7827 fatal_error_signal_hook = x_fatal_error_signal;
7828 XtCloseDisplay (dpy);
7829 fatal_error_signal_hook = NULL;
7831 #endif
7833 #ifdef USE_GTK
7834 /* Due to bugs in some Gtk+ versions, just exit here if this
7835 is the last display/terminal. */
7836 if (terminal_list->next_terminal == NULL)
7838 fprintf (stderr, "%s\n", error_msg);
7839 shut_down_emacs (0, 0, Qnil);
7840 exit (70);
7842 xg_display_close (dpyinfo->display);
7843 #endif
7845 /* Indicate that this display is dead. */
7846 dpyinfo->display = 0;
7848 dpyinfo->reference_count--;
7849 dpyinfo->terminal->reference_count--;
7850 if (dpyinfo->reference_count != 0)
7851 /* We have just closed all frames on this display. */
7852 abort ();
7855 Lisp_Object tmp;
7856 XSETTERMINAL (tmp, dpyinfo->terminal);
7857 Fdelete_terminal (tmp, Qnoelisp);
7861 x_uncatch_errors ();
7863 if (terminal_list == 0)
7865 fprintf (stderr, "%s\n", error_msg);
7866 shut_down_emacs (0, 0, Qnil);
7867 exit (70);
7870 /* Ordinary stack unwind doesn't deal with these. */
7871 #ifdef SIGIO
7872 sigunblock (sigmask (SIGIO));
7873 #endif
7874 sigunblock (sigmask (SIGALRM));
7875 TOTALLY_UNBLOCK_INPUT;
7877 unbind_to (index, Qnil);
7878 clear_waiting_for_input ();
7879 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7880 longjmp), because returning from this function would get us back into
7881 Xlib's code which will directly call `exit'. */
7882 error ("%s", error_msg);
7885 /* We specifically use it before defining it, so that gcc doesn't inline it,
7886 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7887 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7889 /* This is the first-level handler for X protocol errors.
7890 It calls x_error_quitter or x_error_catcher. */
7892 static int
7893 x_error_handler (display, error)
7894 Display *display;
7895 XErrorEvent *error;
7897 if (x_error_message)
7898 x_error_catcher (display, error);
7899 else
7900 x_error_quitter (display, error);
7901 return 0;
7904 /* This is the usual handler for X protocol errors.
7905 It kills all frames on the display that we got the error for.
7906 If that was the only one, it prints an error message and kills Emacs. */
7908 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7910 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
7911 #define NO_INLINE __attribute__((noinline))
7912 #else
7913 #define NO_INLINE
7914 #endif
7916 /* Some versions of GNU/Linux define noinline in their headers. */
7918 #ifdef noinline
7919 #undef noinline
7920 #endif
7922 /* On older GCC versions, just putting x_error_quitter
7923 after x_error_handler prevents inlining into the former. */
7925 static void NO_INLINE
7926 x_error_quitter (display, error)
7927 Display *display;
7928 XErrorEvent *error;
7930 char buf[256], buf1[356];
7932 /* Ignore BadName errors. They can happen because of fonts
7933 or colors that are not defined. */
7935 if (error->error_code == BadName)
7936 return;
7938 /* Note that there is no real way portable across R3/R4 to get the
7939 original error handler. */
7941 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7942 sprintf (buf1, "X protocol error: %s on protocol request %d",
7943 buf, error->request_code);
7944 x_connection_closed (display, buf1);
7948 /* This is the handler for X IO errors, always.
7949 It kills all frames on the display that we lost touch with.
7950 If that was the only one, it prints an error message and kills Emacs. */
7952 static int
7953 x_io_error_quitter (display)
7954 Display *display;
7956 char buf[256];
7958 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7959 x_connection_closed (display, buf);
7960 return 0;
7963 /* Changing the font of the frame. */
7965 /* Give frame F the font FONT-OBJECT as its default font. The return
7966 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
7967 frame. If it is negative, generate a new fontset from
7968 FONT-OBJECT. */
7970 Lisp_Object
7971 x_new_font (f, font_object, fontset)
7972 struct frame *f;
7973 Lisp_Object font_object;
7974 int fontset;
7976 struct font *font = XFONT_OBJECT (font_object);
7978 if (fontset < 0)
7979 fontset = fontset_from_font (font_object);
7980 FRAME_FONTSET (f) = fontset;
7981 if (FRAME_FONT (f) == font)
7982 /* This font is already set in frame F. There's nothing more to
7983 do. */
7984 return font_object;
7986 FRAME_FONT (f) = font;
7987 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
7988 FRAME_COLUMN_WIDTH (f) = font->average_width;
7989 FRAME_SPACE_WIDTH (f) = font->space_width;
7990 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
7992 compute_fringe_widths (f, 1);
7994 /* Compute the scroll bar width in character columns. */
7995 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
7997 int wid = FRAME_COLUMN_WIDTH (f);
7998 FRAME_CONFIG_SCROLL_BAR_COLS (f)
7999 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8001 else
8003 int wid = FRAME_COLUMN_WIDTH (f);
8004 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8007 if (FRAME_X_WINDOW (f) != 0)
8009 /* Don't change the size of a tip frame; there's no point in
8010 doing it because it's done in Fx_show_tip, and it leads to
8011 problems because the tip frame has no widget. */
8012 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8013 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8016 #ifdef HAVE_X_I18N
8017 if (FRAME_XIC (f)
8018 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8020 BLOCK_INPUT;
8021 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8022 UNBLOCK_INPUT;
8024 #endif
8026 return font_object;
8030 /***********************************************************************
8031 X Input Methods
8032 ***********************************************************************/
8034 #ifdef HAVE_X_I18N
8036 #ifdef HAVE_X11R6
8038 /* XIM destroy callback function, which is called whenever the
8039 connection to input method XIM dies. CLIENT_DATA contains a
8040 pointer to the x_display_info structure corresponding to XIM. */
8042 static void
8043 xim_destroy_callback (xim, client_data, call_data)
8044 XIM xim;
8045 XPointer client_data;
8046 XPointer call_data;
8048 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8049 Lisp_Object frame, tail;
8051 BLOCK_INPUT;
8053 /* No need to call XDestroyIC.. */
8054 FOR_EACH_FRAME (tail, frame)
8056 struct frame *f = XFRAME (frame);
8057 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8059 FRAME_XIC (f) = NULL;
8060 xic_free_xfontset (f);
8064 /* No need to call XCloseIM. */
8065 dpyinfo->xim = NULL;
8066 XFree (dpyinfo->xim_styles);
8067 UNBLOCK_INPUT;
8070 #endif /* HAVE_X11R6 */
8072 #ifdef HAVE_X11R6
8073 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8074 extern char *XSetIMValues P_ ((XIM, ...));
8075 #endif
8077 /* Open the connection to the XIM server on display DPYINFO.
8078 RESOURCE_NAME is the resource name Emacs uses. */
8080 static void
8081 xim_open_dpy (dpyinfo, resource_name)
8082 struct x_display_info *dpyinfo;
8083 char *resource_name;
8085 XIM xim;
8087 #ifdef HAVE_XIM
8088 if (use_xim)
8090 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8091 EMACS_CLASS);
8092 dpyinfo->xim = xim;
8094 if (xim)
8096 #ifdef HAVE_X11R6
8097 XIMCallback destroy;
8098 #endif
8100 /* Get supported styles and XIM values. */
8101 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8103 #ifdef HAVE_X11R6
8104 destroy.callback = xim_destroy_callback;
8105 destroy.client_data = (XPointer)dpyinfo;
8106 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8107 #endif
8111 else
8112 #endif /* HAVE_XIM */
8113 dpyinfo->xim = NULL;
8117 #ifdef HAVE_X11R6_XIM
8119 struct xim_inst_t
8121 struct x_display_info *dpyinfo;
8122 char *resource_name;
8125 /* XIM instantiate callback function, which is called whenever an XIM
8126 server is available. DISPLAY is the display of the XIM.
8127 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8128 when the callback was registered. */
8130 static void
8131 xim_instantiate_callback (display, client_data, call_data)
8132 Display *display;
8133 XPointer client_data;
8134 XPointer call_data;
8136 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8137 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8139 /* We don't support multiple XIM connections. */
8140 if (dpyinfo->xim)
8141 return;
8143 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8145 /* Create XIC for the existing frames on the same display, as long
8146 as they have no XIC. */
8147 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8149 Lisp_Object tail, frame;
8151 BLOCK_INPUT;
8152 FOR_EACH_FRAME (tail, frame)
8154 struct frame *f = XFRAME (frame);
8156 if (FRAME_X_P (f)
8157 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8158 if (FRAME_XIC (f) == NULL)
8160 create_frame_xic (f);
8161 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8162 xic_set_statusarea (f);
8163 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8165 struct window *w = XWINDOW (f->selected_window);
8166 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8171 UNBLOCK_INPUT;
8175 #endif /* HAVE_X11R6_XIM */
8178 /* Open a connection to the XIM server on display DPYINFO.
8179 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8180 connection only at the first time. On X11R6, open the connection
8181 in the XIM instantiate callback function. */
8183 static void
8184 xim_initialize (dpyinfo, resource_name)
8185 struct x_display_info *dpyinfo;
8186 char *resource_name;
8188 #ifdef HAVE_XIM
8189 if (use_xim)
8191 #ifdef HAVE_X11R6_XIM
8192 struct xim_inst_t *xim_inst;
8193 int len;
8195 dpyinfo->xim = NULL;
8196 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8197 xim_inst->dpyinfo = dpyinfo;
8198 len = strlen (resource_name);
8199 xim_inst->resource_name = (char *) xmalloc (len + 1);
8200 bcopy (resource_name, xim_inst->resource_name, len + 1);
8201 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8202 resource_name, EMACS_CLASS,
8203 xim_instantiate_callback,
8204 /* This is XPointer in XFree86
8205 but (XPointer *) on Tru64, at
8206 least, hence the configure test. */
8207 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8208 #else /* not HAVE_X11R6_XIM */
8209 dpyinfo->xim = NULL;
8210 xim_open_dpy (dpyinfo, resource_name);
8211 #endif /* not HAVE_X11R6_XIM */
8214 else
8215 #endif /* HAVE_XIM */
8216 dpyinfo->xim = NULL;
8220 /* Close the connection to the XIM server on display DPYINFO. */
8222 static void
8223 xim_close_dpy (dpyinfo)
8224 struct x_display_info *dpyinfo;
8226 #ifdef HAVE_XIM
8227 if (use_xim)
8229 #ifdef HAVE_X11R6_XIM
8230 if (dpyinfo->display)
8231 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8232 NULL, EMACS_CLASS,
8233 xim_instantiate_callback, NULL);
8234 #endif /* not HAVE_X11R6_XIM */
8235 if (dpyinfo->display)
8236 XCloseIM (dpyinfo->xim);
8237 dpyinfo->xim = NULL;
8238 XFree (dpyinfo->xim_styles);
8240 #endif /* HAVE_XIM */
8243 #endif /* not HAVE_X11R6_XIM */
8247 /* Calculate the absolute position in frame F
8248 from its current recorded position values and gravity. */
8250 void
8251 x_calc_absolute_position (f)
8252 struct frame *f;
8254 int flags = f->size_hint_flags;
8256 /* We have nothing to do if the current position
8257 is already for the top-left corner. */
8258 if (! ((flags & XNegative) || (flags & YNegative)))
8259 return;
8261 /* Treat negative positions as relative to the leftmost bottommost
8262 position that fits on the screen. */
8263 if (flags & XNegative)
8264 f->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8265 - FRAME_PIXEL_WIDTH (f) + f->left_pos);
8268 int height = FRAME_PIXEL_HEIGHT (f);
8270 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8271 /* Something is fishy here. When using Motif, starting Emacs with
8272 `-g -0-0', the frame appears too low by a few pixels.
8274 This seems to be so because initially, while Emacs is starting,
8275 the column widget's height and the frame's pixel height are
8276 different. The column widget's height is the right one. In
8277 later invocations, when Emacs is up, the frame's pixel height
8278 is right, though.
8280 It's not obvious where the initial small difference comes from.
8281 2000-12-01, gerd. */
8283 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8284 #endif
8286 if (flags & YNegative)
8287 f->top_pos = (FRAME_X_DISPLAY_INFO (f)->height - height + f->top_pos);
8290 /* The left_pos and top_pos
8291 are now relative to the top and left screen edges,
8292 so the flags should correspond. */
8293 f->size_hint_flags &= ~ (XNegative | YNegative);
8296 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8297 to really change the position, and 0 when calling from
8298 x_make_frame_visible (in that case, XOFF and YOFF are the current
8299 position values). It is -1 when calling from x_set_frame_parameters,
8300 which means, do adjust for borders but don't change the gravity. */
8302 void
8303 x_set_offset (f, xoff, yoff, change_gravity)
8304 struct frame *f;
8305 register int xoff, yoff;
8306 int change_gravity;
8308 int modified_top, modified_left;
8310 if (change_gravity > 0)
8312 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8313 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8315 f->top_pos = yoff;
8316 f->left_pos = xoff;
8317 f->size_hint_flags &= ~ (XNegative | YNegative);
8318 if (xoff < 0)
8319 f->size_hint_flags |= XNegative;
8320 if (yoff < 0)
8321 f->size_hint_flags |= YNegative;
8322 f->win_gravity = NorthWestGravity;
8324 x_calc_absolute_position (f);
8326 BLOCK_INPUT;
8327 x_wm_set_size_hint (f, (long) 0, 0);
8329 modified_left = f->left_pos;
8330 modified_top = f->top_pos;
8332 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8334 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8335 than the WM decorations. So we use the calculated offset instead
8336 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8337 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8338 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8341 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8342 modified_left, modified_top);
8344 x_sync_with_move (f, f->left_pos, f->top_pos,
8345 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8346 ? 1 : 0);
8348 /* change_gravity is non-zero when this function is called from Lisp to
8349 programmatically move a frame. In that case, we call
8350 x_check_expected_move to discover if we have a "Type A" or "Type B"
8351 window manager, and, for a "Type A" window manager, adjust the position
8352 of the frame.
8354 We call x_check_expected_move if a programmatic move occurred, and
8355 either the window manager type (A/B) is unknown or it is Type A but we
8356 need to compute the top/left offset adjustment for this frame. */
8358 if (change_gravity != 0 &&
8359 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8360 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8361 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8362 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8363 x_check_expected_move (f, modified_left, modified_top);
8365 UNBLOCK_INPUT;
8368 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8369 on the root window for frame F contains ATOMNAME.
8370 This is how a WM check shall be done according to the Window Manager
8371 Specification/Extended Window Manager Hints at
8372 http://freedesktop.org/wiki/Specifications/wm-spec. */
8374 static int
8375 wm_supports (f, atomname)
8376 struct frame *f;
8377 const char *atomname;
8379 Atom actual_type;
8380 unsigned long actual_size, bytes_remaining;
8381 int i, rc, actual_format;
8382 Atom prop_atom;
8383 Window wmcheck_window;
8384 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8385 Window target_window = dpyinfo->root_window;
8386 long max_len = 65536;
8387 Display *dpy = FRAME_X_DISPLAY (f);
8388 unsigned char *tmp_data = NULL;
8389 Atom target_type = XA_WINDOW;
8390 Atom want_atom;
8392 BLOCK_INPUT;
8394 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8396 x_catch_errors (dpy);
8397 rc = XGetWindowProperty (dpy, target_window,
8398 prop_atom, 0, max_len, False, target_type,
8399 &actual_type, &actual_format, &actual_size,
8400 &bytes_remaining, &tmp_data);
8402 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8404 if (tmp_data) XFree (tmp_data);
8405 x_uncatch_errors ();
8406 UNBLOCK_INPUT;
8407 return 0;
8410 wmcheck_window = *(Window *) tmp_data;
8411 XFree (tmp_data);
8413 /* Check if window exists. */
8414 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8415 x_sync (f);
8416 if (x_had_errors_p (dpy))
8418 x_uncatch_errors ();
8419 UNBLOCK_INPUT;
8420 return 0;
8423 if (dpyinfo->net_supported_window != wmcheck_window)
8425 /* Window changed, reload atoms */
8426 if (dpyinfo->net_supported_atoms != NULL)
8427 XFree (dpyinfo->net_supported_atoms);
8428 dpyinfo->net_supported_atoms = NULL;
8429 dpyinfo->nr_net_supported_atoms = 0;
8430 dpyinfo->net_supported_window = 0;
8432 target_type = XA_ATOM;
8433 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8434 tmp_data = NULL;
8435 rc = XGetWindowProperty (dpy, target_window,
8436 prop_atom, 0, max_len, False, target_type,
8437 &actual_type, &actual_format, &actual_size,
8438 &bytes_remaining, &tmp_data);
8440 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8442 if (tmp_data) XFree (tmp_data);
8443 x_uncatch_errors ();
8444 UNBLOCK_INPUT;
8445 return 0;
8448 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8449 dpyinfo->nr_net_supported_atoms = actual_size;
8450 dpyinfo->net_supported_window = wmcheck_window;
8453 rc = 0;
8454 want_atom = XInternAtom (dpy, atomname, False);
8456 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8457 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8459 x_uncatch_errors ();
8460 UNBLOCK_INPUT;
8462 return rc;
8465 /* Do fullscreen as specified in extended window manager hints */
8467 static int
8468 do_ewmh_fullscreen (f)
8469 struct frame *f;
8471 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8473 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8474 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8475 if (!have_net_atom)
8476 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8478 if (have_net_atom)
8480 Lisp_Object frame;
8481 const char *atom = "_NET_WM_STATE";
8482 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8483 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8484 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8485 const char *what = NULL;
8487 XSETFRAME (frame, f);
8489 /* If there are _NET_ atoms we assume we have extended window manager
8490 hints. */
8491 switch (f->want_fullscreen)
8493 case FULLSCREEN_BOTH:
8494 what = fs;
8495 break;
8496 case FULLSCREEN_WIDTH:
8497 what = fw;
8498 break;
8499 case FULLSCREEN_HEIGHT:
8500 what = fh;
8501 break;
8504 if (what != NULL && !wm_supports (f, what)) return 0;
8507 Fx_send_client_event (frame, make_number (0), frame,
8508 make_unibyte_string (atom, strlen (atom)),
8509 make_number (32),
8510 Fcons (make_number (0), /* Remove */
8511 Fcons
8512 (make_unibyte_string (fs,
8513 strlen (fs)),
8514 Qnil)));
8515 Fx_send_client_event (frame, make_number (0), frame,
8516 make_unibyte_string (atom, strlen (atom)),
8517 make_number (32),
8518 Fcons (make_number (0), /* Remove */
8519 Fcons
8520 (make_unibyte_string (fh,
8521 strlen (fh)),
8522 Qnil)));
8523 Fx_send_client_event (frame, make_number (0), frame,
8524 make_unibyte_string (atom, strlen (atom)),
8525 make_number (32),
8526 Fcons (make_number (0), /* Remove */
8527 Fcons
8528 (make_unibyte_string (fw,
8529 strlen (fw)),
8530 Qnil)));
8531 f->want_fullscreen = FULLSCREEN_NONE;
8532 if (what != NULL)
8533 Fx_send_client_event (frame, make_number (0), frame,
8534 make_unibyte_string (atom, strlen (atom)),
8535 make_number (32),
8536 Fcons (make_number (1), /* Add */
8537 Fcons
8538 (make_unibyte_string (what,
8539 strlen (what)),
8540 Qnil)));
8543 return have_net_atom;
8546 static void
8547 XTfullscreen_hook (f)
8548 FRAME_PTR f;
8550 if (f->async_visible)
8552 BLOCK_INPUT;
8553 do_ewmh_fullscreen (f);
8554 x_sync (f);
8555 UNBLOCK_INPUT;
8560 /* Check if we need to resize the frame due to a fullscreen request.
8561 If so needed, resize the frame. */
8562 static void
8563 x_check_fullscreen (f)
8564 struct frame *f;
8566 if (f->want_fullscreen & FULLSCREEN_BOTH)
8568 int width, height, ign;
8570 if (do_ewmh_fullscreen (f))
8571 return;
8573 x_real_positions (f, &f->left_pos, &f->top_pos);
8575 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8577 /* We do not need to move the window, it shall be taken care of
8578 when setting WM manager hints.
8579 If the frame is visible already, the position is checked by
8580 x_check_expected_move. */
8581 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8583 change_frame_size (f, height, width, 0, 1, 0);
8584 SET_FRAME_GARBAGED (f);
8585 cancel_mouse_face (f);
8587 /* Wait for the change of frame size to occur */
8588 f->want_fullscreen |= FULLSCREEN_WAIT;
8593 /* This function is called by x_set_offset to determine whether the window
8594 manager interfered with the positioning of the frame. Type A window
8595 managers position the surrounding window manager decorations a small
8596 amount above and left of the user-supplied position. Type B window
8597 managers position the surrounding window manager decorations at the
8598 user-specified position. If we detect a Type A window manager, we
8599 compensate by moving the window right and down by the proper amount. */
8601 static void
8602 x_check_expected_move (f, expected_left, expected_top)
8603 struct frame *f;
8604 int expected_left;
8605 int expected_top;
8607 int current_left = 0, current_top = 0;
8609 /* x_real_positions returns the left and top offsets of the outermost
8610 window manager window around the frame. */
8612 x_real_positions (f, &current_left, &current_top);
8614 if (current_left != expected_left || current_top != expected_top)
8616 /* It's a "Type A" window manager. */
8618 int adjusted_left;
8619 int adjusted_top;
8621 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8622 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8623 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8625 /* Now fix the mispositioned frame's location. */
8627 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8628 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8630 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8631 adjusted_left, adjusted_top);
8633 x_sync_with_move (f, expected_left, expected_top, 0);
8635 else
8636 /* It's a "Type B" window manager. We don't have to adjust the
8637 frame's position. */
8639 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8643 /* Wait for XGetGeometry to return up-to-date position information for a
8644 recently-moved frame. Call this immediately after calling XMoveWindow.
8645 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8646 frame has been moved to, so we use a fuzzy position comparison instead
8647 of an exact comparison. */
8649 static void
8650 x_sync_with_move (f, left, top, fuzzy)
8651 struct frame *f;
8652 int left, top, fuzzy;
8654 int count = 0;
8656 while (count++ < 50)
8658 int current_left = 0, current_top = 0;
8660 /* In theory, this call to XSync only needs to happen once, but in
8661 practice, it doesn't seem to work, hence the need for the surrounding
8662 loop. */
8664 XSync (FRAME_X_DISPLAY (f), False);
8665 x_real_positions (f, &current_left, &current_top);
8667 if (fuzzy)
8669 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8670 pixels. */
8672 if (eabs (current_left - left) <= 10
8673 && eabs (current_top - top) <= 40)
8674 return;
8676 else if (current_left == left && current_top == top)
8677 return;
8680 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8681 will then return up-to-date position info. */
8683 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8687 /* Change the size of frame F's X window to COLS/ROWS in the case F
8688 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8689 top-left-corner window gravity for this size change and subsequent
8690 size changes. Otherwise we leave the window gravity unchanged. */
8692 static void
8693 x_set_window_size_1 (f, change_gravity, cols, rows)
8694 struct frame *f;
8695 int change_gravity;
8696 int cols, rows;
8698 int pixelwidth, pixelheight;
8700 check_frame_size (f, &rows, &cols);
8701 f->scroll_bar_actual_width
8702 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8704 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8705 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8706 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8708 compute_fringe_widths (f, 0);
8710 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8711 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8713 f->win_gravity = NorthWestGravity;
8714 x_wm_set_size_hint (f, (long) 0, 0);
8716 XSync (FRAME_X_DISPLAY (f), False);
8717 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8718 pixelwidth, pixelheight);
8720 /* Now, strictly speaking, we can't be sure that this is accurate,
8721 but the window manager will get around to dealing with the size
8722 change request eventually, and we'll hear how it went when the
8723 ConfigureNotify event gets here.
8725 We could just not bother storing any of this information here,
8726 and let the ConfigureNotify event set everything up, but that
8727 might be kind of confusing to the Lisp code, since size changes
8728 wouldn't be reported in the frame parameters until some random
8729 point in the future when the ConfigureNotify event arrives.
8731 We pass 1 for DELAY since we can't run Lisp code inside of
8732 a BLOCK_INPUT. */
8733 change_frame_size (f, rows, cols, 0, 1, 0);
8734 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8735 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8737 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8738 receive in the ConfigureNotify event; if we get what we asked
8739 for, then the event won't cause the screen to become garbaged, so
8740 we have to make sure to do it here. */
8741 SET_FRAME_GARBAGED (f);
8743 XFlush (FRAME_X_DISPLAY (f));
8747 /* Call this to change the size of frame F's x-window.
8748 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8749 for this size change and subsequent size changes.
8750 Otherwise we leave the window gravity unchanged. */
8752 void
8753 x_set_window_size (f, change_gravity, cols, rows)
8754 struct frame *f;
8755 int change_gravity;
8756 int cols, rows;
8758 BLOCK_INPUT;
8760 #ifdef USE_GTK
8761 if (FRAME_GTK_WIDGET (f))
8762 xg_frame_set_char_size (f, cols, rows);
8763 else
8764 x_set_window_size_1 (f, change_gravity, cols, rows);
8765 #elif USE_X_TOOLKIT
8767 if (f->output_data.x->widget != NULL)
8769 /* The x and y position of the widget is clobbered by the
8770 call to XtSetValues within EmacsFrameSetCharSize.
8771 This is a real kludge, but I don't understand Xt so I can't
8772 figure out a correct fix. Can anyone else tell me? -- rms. */
8773 int xpos = f->output_data.x->widget->core.x;
8774 int ypos = f->output_data.x->widget->core.y;
8775 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8776 f->output_data.x->widget->core.x = xpos;
8777 f->output_data.x->widget->core.y = ypos;
8779 else
8780 x_set_window_size_1 (f, change_gravity, cols, rows);
8782 #else /* not USE_X_TOOLKIT */
8784 x_set_window_size_1 (f, change_gravity, cols, rows);
8786 #endif /* not USE_X_TOOLKIT */
8788 /* If cursor was outside the new size, mark it as off. */
8789 mark_window_cursors_off (XWINDOW (f->root_window));
8791 /* Clear out any recollection of where the mouse highlighting was,
8792 since it might be in a place that's outside the new frame size.
8793 Actually checking whether it is outside is a pain in the neck,
8794 so don't try--just let the highlighting be done afresh with new size. */
8795 cancel_mouse_face (f);
8797 UNBLOCK_INPUT;
8800 /* Mouse warping. */
8802 void
8803 x_set_mouse_position (f, x, y)
8804 struct frame *f;
8805 int x, y;
8807 int pix_x, pix_y;
8809 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8810 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8812 if (pix_x < 0) pix_x = 0;
8813 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8815 if (pix_y < 0) pix_y = 0;
8816 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8818 BLOCK_INPUT;
8820 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8821 0, 0, 0, 0, pix_x, pix_y);
8822 UNBLOCK_INPUT;
8825 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8827 void
8828 x_set_mouse_pixel_position (f, pix_x, pix_y)
8829 struct frame *f;
8830 int pix_x, pix_y;
8832 BLOCK_INPUT;
8834 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8835 0, 0, 0, 0, pix_x, pix_y);
8836 UNBLOCK_INPUT;
8839 /* focus shifting, raising and lowering. */
8841 void
8842 x_focus_on_frame (f)
8843 struct frame *f;
8845 #if 0 /* This proves to be unpleasant. */
8846 x_raise_frame (f);
8847 #endif
8848 #if 0
8849 /* I don't think that the ICCCM allows programs to do things like this
8850 without the interaction of the window manager. Whatever you end up
8851 doing with this code, do it to x_unfocus_frame too. */
8852 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8853 RevertToPointerRoot, CurrentTime);
8854 #endif /* ! 0 */
8857 void
8858 x_unfocus_frame (f)
8859 struct frame *f;
8861 #if 0
8862 /* Look at the remarks in x_focus_on_frame. */
8863 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8864 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8865 RevertToPointerRoot, CurrentTime);
8866 #endif /* ! 0 */
8869 /* Raise frame F. */
8871 void
8872 x_raise_frame (f)
8873 struct frame *f;
8875 BLOCK_INPUT;
8876 if (f->async_visible)
8877 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8879 XFlush (FRAME_X_DISPLAY (f));
8880 UNBLOCK_INPUT;
8883 /* Lower frame F. */
8885 void
8886 x_lower_frame (f)
8887 struct frame *f;
8889 if (f->async_visible)
8891 BLOCK_INPUT;
8892 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8893 XFlush (FRAME_X_DISPLAY (f));
8894 UNBLOCK_INPUT;
8898 /* Activate frame with Extended Window Manager Hints */
8900 void
8901 x_ewmh_activate_frame (f)
8902 FRAME_PTR f;
8904 /* See Window Manager Specification/Extended Window Manager Hints at
8905 http://freedesktop.org/wiki/Specifications/wm-spec */
8907 const char *atom = "_NET_ACTIVE_WINDOW";
8908 if (f->async_visible && wm_supports (f, atom))
8910 Lisp_Object frame;
8911 XSETFRAME (frame, f);
8912 Fx_send_client_event (frame, make_number (0), frame,
8913 make_unibyte_string (atom, strlen (atom)),
8914 make_number (32),
8915 Fcons (make_number (1),
8916 Fcons (make_number (last_user_time),
8917 Qnil)));
8921 static void
8922 XTframe_raise_lower (f, raise_flag)
8923 FRAME_PTR f;
8924 int raise_flag;
8926 if (raise_flag)
8927 x_raise_frame (f);
8928 else
8929 x_lower_frame (f);
8932 /* XEmbed implementation. */
8934 void
8935 xembed_set_info (f, flags)
8936 struct frame *f;
8937 enum xembed_info flags;
8939 Atom atom;
8940 unsigned long data[2];
8942 atom = XInternAtom (FRAME_X_DISPLAY (f), "_XEMBED_INFO", False);
8944 data[0] = XEMBED_VERSION;
8945 data[1] = flags;
8947 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), atom, atom,
8948 32, PropModeReplace, (unsigned char *) data, 2);
8951 void
8952 xembed_send_message (f, time, message, detail, data1, data2)
8953 struct frame *f;
8954 Time time;
8955 enum xembed_message message;
8956 long detail;
8957 long data1;
8958 long data2;
8960 XEvent event;
8962 event.xclient.type = ClientMessage;
8963 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
8964 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
8965 event.xclient.format = 32;
8966 event.xclient.data.l[0] = time;
8967 event.xclient.data.l[1] = message;
8968 event.xclient.data.l[2] = detail;
8969 event.xclient.data.l[3] = data1;
8970 event.xclient.data.l[4] = data2;
8972 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
8973 False, NoEventMask, &event);
8974 XSync (FRAME_X_DISPLAY (f), False);
8977 /* Change of visibility. */
8979 /* This tries to wait until the frame is really visible.
8980 However, if the window manager asks the user where to position
8981 the frame, this will return before the user finishes doing that.
8982 The frame will not actually be visible at that time,
8983 but it will become visible later when the window manager
8984 finishes with it. */
8986 void
8987 x_make_frame_visible (f)
8988 struct frame *f;
8990 Lisp_Object type;
8991 int original_top, original_left;
8992 int retry_count = 2;
8994 retry:
8996 BLOCK_INPUT;
8998 type = x_icon_type (f);
8999 if (!NILP (type))
9000 x_bitmap_icon (f, type);
9002 if (! FRAME_VISIBLE_P (f))
9004 /* We test FRAME_GARBAGED_P here to make sure we don't
9005 call x_set_offset a second time
9006 if we get to x_make_frame_visible a second time
9007 before the window gets really visible. */
9008 if (! FRAME_ICONIFIED_P (f)
9009 && ! FRAME_X_EMBEDDED_P (f)
9010 && ! f->output_data.x->asked_for_visible)
9011 x_set_offset (f, f->left_pos, f->top_pos, 0);
9013 f->output_data.x->asked_for_visible = 1;
9015 if (! EQ (Vx_no_window_manager, Qt))
9016 x_wm_set_window_state (f, NormalState);
9017 #ifdef USE_X_TOOLKIT
9018 if (FRAME_X_EMBEDDED_P (f))
9019 xembed_set_info (f, XEMBED_MAPPED);
9020 else
9022 /* This was XtPopup, but that did nothing for an iconified frame. */
9023 XtMapWidget (f->output_data.x->widget);
9025 #else /* not USE_X_TOOLKIT */
9026 #ifdef USE_GTK
9027 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9028 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9029 #else
9030 if (FRAME_X_EMBEDDED_P (f))
9031 xembed_set_info (f, XEMBED_MAPPED);
9032 else
9033 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9034 #endif /* not USE_GTK */
9035 #endif /* not USE_X_TOOLKIT */
9036 #if 0 /* This seems to bring back scroll bars in the wrong places
9037 if the window configuration has changed. They seem
9038 to come back ok without this. */
9039 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9040 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9041 #endif
9044 XFlush (FRAME_X_DISPLAY (f));
9046 /* Synchronize to ensure Emacs knows the frame is visible
9047 before we do anything else. We do this loop with input not blocked
9048 so that incoming events are handled. */
9050 Lisp_Object frame;
9051 int count;
9052 /* This must be before UNBLOCK_INPUT
9053 since events that arrive in response to the actions above
9054 will set it when they are handled. */
9055 int previously_visible = f->output_data.x->has_been_visible;
9057 original_left = f->left_pos;
9058 original_top = f->top_pos;
9060 /* This must come after we set COUNT. */
9061 UNBLOCK_INPUT;
9063 /* We unblock here so that arriving X events are processed. */
9065 /* Now move the window back to where it was "supposed to be".
9066 But don't do it if the gravity is negative.
9067 When the gravity is negative, this uses a position
9068 that is 3 pixels too low. Perhaps that's really the border width.
9070 Don't do this if the window has never been visible before,
9071 because the window manager may choose the position
9072 and we don't want to override it. */
9074 if (! FRAME_VISIBLE_P (f)
9075 && ! FRAME_ICONIFIED_P (f)
9076 && ! FRAME_X_EMBEDDED_P (f)
9077 && f->win_gravity == NorthWestGravity
9078 && previously_visible)
9080 Drawable rootw;
9081 int x, y;
9082 unsigned int width, height, border, depth;
9084 BLOCK_INPUT;
9086 /* On some window managers (such as FVWM) moving an existing
9087 window, even to the same place, causes the window manager
9088 to introduce an offset. This can cause the window to move
9089 to an unexpected location. Check the geometry (a little
9090 slow here) and then verify that the window is in the right
9091 place. If the window is not in the right place, move it
9092 there, and take the potential window manager hit. */
9093 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9094 &rootw, &x, &y, &width, &height, &border, &depth);
9096 if (original_left != x || original_top != y)
9097 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9098 original_left, original_top);
9100 UNBLOCK_INPUT;
9103 XSETFRAME (frame, f);
9105 /* Wait until the frame is visible. Process X events until a
9106 MapNotify event has been seen, or until we think we won't get a
9107 MapNotify at all.. */
9108 for (count = input_signal_count + 10;
9109 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9111 /* Force processing of queued events. */
9112 x_sync (f);
9114 /* Machines that do polling rather than SIGIO have been
9115 observed to go into a busy-wait here. So we'll fake an
9116 alarm signal to let the handler know that there's something
9117 to be read. We used to raise a real alarm, but it seems
9118 that the handler isn't always enabled here. This is
9119 probably a bug. */
9120 if (input_polling_used ())
9122 /* It could be confusing if a real alarm arrives while
9123 processing the fake one. Turn it off and let the
9124 handler reset it. */
9125 extern void poll_for_input_1 P_ ((void));
9126 int old_poll_suppress_count = poll_suppress_count;
9127 poll_suppress_count = 1;
9128 poll_for_input_1 ();
9129 poll_suppress_count = old_poll_suppress_count;
9132 /* See if a MapNotify event has been processed. */
9133 FRAME_SAMPLE_VISIBILITY (f);
9136 /* 2000-09-28: In
9138 (let ((f (selected-frame)))
9139 (iconify-frame f)
9140 (raise-frame f))
9142 the frame is not raised with various window managers on
9143 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9144 unknown reason, the call to XtMapWidget is completely ignored.
9145 Mapping the widget a second time works. */
9147 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9148 goto retry;
9152 /* Change from mapped state to withdrawn state. */
9154 /* Make the frame visible (mapped and not iconified). */
9156 void
9157 x_make_frame_invisible (f)
9158 struct frame *f;
9160 Window window;
9162 /* Use the frame's outermost window, not the one we normally draw on. */
9163 window = FRAME_OUTER_WINDOW (f);
9165 /* Don't keep the highlight on an invisible frame. */
9166 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9167 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9169 #if 0/* This might add unreliability; I don't trust it -- rms. */
9170 if (! f->async_visible && ! f->async_iconified)
9171 return;
9172 #endif
9174 BLOCK_INPUT;
9176 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9177 that the current position of the window is user-specified, rather than
9178 program-specified, so that when the window is mapped again, it will be
9179 placed at the same location, without forcing the user to position it
9180 by hand again (they have already done that once for this window.) */
9181 x_wm_set_size_hint (f, (long) 0, 1);
9183 #ifdef USE_GTK
9184 if (FRAME_GTK_OUTER_WIDGET (f))
9185 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9186 else
9187 #else
9188 if (FRAME_X_EMBEDDED_P (f))
9189 xembed_set_info (f, 0);
9190 else
9191 #endif
9194 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9195 DefaultScreen (FRAME_X_DISPLAY (f))))
9197 UNBLOCK_INPUT_RESIGNAL;
9198 error ("Can't notify window manager of window withdrawal");
9202 /* We can't distinguish this from iconification
9203 just by the event that we get from the server.
9204 So we can't win using the usual strategy of letting
9205 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9206 and synchronize with the server to make sure we agree. */
9207 f->visible = 0;
9208 FRAME_ICONIFIED_P (f) = 0;
9209 f->async_visible = 0;
9210 f->async_iconified = 0;
9212 x_sync (f);
9214 UNBLOCK_INPUT;
9217 /* Change window state from mapped to iconified. */
9219 void
9220 x_iconify_frame (f)
9221 struct frame *f;
9223 int result;
9224 Lisp_Object type;
9226 /* Don't keep the highlight on an invisible frame. */
9227 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9228 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9230 if (f->async_iconified)
9231 return;
9233 BLOCK_INPUT;
9235 FRAME_SAMPLE_VISIBILITY (f);
9237 type = x_icon_type (f);
9238 if (!NILP (type))
9239 x_bitmap_icon (f, type);
9241 #ifdef USE_GTK
9242 if (FRAME_GTK_OUTER_WIDGET (f))
9244 if (! FRAME_VISIBLE_P (f))
9245 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9247 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9248 f->iconified = 1;
9249 f->visible = 1;
9250 f->async_iconified = 1;
9251 f->async_visible = 0;
9252 UNBLOCK_INPUT;
9253 return;
9255 #endif
9257 #ifdef USE_X_TOOLKIT
9259 if (! FRAME_VISIBLE_P (f))
9261 if (! EQ (Vx_no_window_manager, Qt))
9262 x_wm_set_window_state (f, IconicState);
9263 /* This was XtPopup, but that did nothing for an iconified frame. */
9264 XtMapWidget (f->output_data.x->widget);
9265 /* The server won't give us any event to indicate
9266 that an invisible frame was changed to an icon,
9267 so we have to record it here. */
9268 f->iconified = 1;
9269 f->visible = 1;
9270 f->async_iconified = 1;
9271 f->async_visible = 0;
9272 UNBLOCK_INPUT;
9273 return;
9276 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9277 XtWindow (f->output_data.x->widget),
9278 DefaultScreen (FRAME_X_DISPLAY (f)));
9279 UNBLOCK_INPUT;
9281 if (!result)
9282 error ("Can't notify window manager of iconification");
9284 f->async_iconified = 1;
9285 f->async_visible = 0;
9288 BLOCK_INPUT;
9289 XFlush (FRAME_X_DISPLAY (f));
9290 UNBLOCK_INPUT;
9291 #else /* not USE_X_TOOLKIT */
9293 /* Make sure the X server knows where the window should be positioned,
9294 in case the user deiconifies with the window manager. */
9295 if (! FRAME_VISIBLE_P (f)
9296 && ! FRAME_ICONIFIED_P (f)
9297 && ! FRAME_X_EMBEDDED_P (f))
9298 x_set_offset (f, f->left_pos, f->top_pos, 0);
9300 /* Since we don't know which revision of X we're running, we'll use both
9301 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9303 /* X11R4: send a ClientMessage to the window manager using the
9304 WM_CHANGE_STATE type. */
9306 XEvent message;
9308 message.xclient.window = FRAME_X_WINDOW (f);
9309 message.xclient.type = ClientMessage;
9310 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9311 message.xclient.format = 32;
9312 message.xclient.data.l[0] = IconicState;
9314 if (! XSendEvent (FRAME_X_DISPLAY (f),
9315 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9316 False,
9317 SubstructureRedirectMask | SubstructureNotifyMask,
9318 &message))
9320 UNBLOCK_INPUT_RESIGNAL;
9321 error ("Can't notify window manager of iconification");
9325 /* X11R3: set the initial_state field of the window manager hints to
9326 IconicState. */
9327 x_wm_set_window_state (f, IconicState);
9329 if (!FRAME_VISIBLE_P (f))
9331 /* If the frame was withdrawn, before, we must map it. */
9332 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9335 f->async_iconified = 1;
9336 f->async_visible = 0;
9338 XFlush (FRAME_X_DISPLAY (f));
9339 UNBLOCK_INPUT;
9340 #endif /* not USE_X_TOOLKIT */
9344 /* Free X resources of frame F. */
9346 void
9347 x_free_frame_resources (f)
9348 struct frame *f;
9350 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9351 Lisp_Object bar;
9352 struct scroll_bar *b;
9354 BLOCK_INPUT;
9356 /* If a display connection is dead, don't try sending more
9357 commands to the X server. */
9358 if (dpyinfo->display)
9360 /* We must free faces before destroying windows because some
9361 font-driver (e.g. xft) access a window while finishing a
9362 face. */
9363 if (FRAME_FACE_CACHE (f))
9364 free_frame_faces (f);
9366 if (f->output_data.x->icon_desc)
9367 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9369 #ifdef USE_X_TOOLKIT
9370 /* Explicitly destroy the scroll bars of the frame. Without
9371 this, we get "BadDrawable" errors from the toolkit later on,
9372 presumably from expose events generated for the disappearing
9373 toolkit scroll bars. */
9374 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9376 b = XSCROLL_BAR (bar);
9377 x_scroll_bar_remove (b);
9379 #endif
9381 #ifdef HAVE_X_I18N
9382 if (FRAME_XIC (f))
9383 free_frame_xic (f);
9384 #endif
9386 #ifdef USE_X_TOOLKIT
9387 if (f->output_data.x->widget)
9389 XtDestroyWidget (f->output_data.x->widget);
9390 f->output_data.x->widget = NULL;
9392 /* Tooltips don't have widgets, only a simple X window, even if
9393 we are using a toolkit. */
9394 else if (FRAME_X_WINDOW (f))
9395 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9397 free_frame_menubar (f);
9398 #else /* !USE_X_TOOLKIT */
9400 #ifdef USE_GTK
9401 /* In the GTK version, tooltips are normal X
9402 frames. We must check and free both types. */
9403 if (FRAME_GTK_OUTER_WIDGET (f))
9405 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9406 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9407 FRAME_GTK_OUTER_WIDGET (f) = 0;
9409 #endif /* USE_GTK */
9411 if (FRAME_X_WINDOW (f))
9412 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9413 #endif /* !USE_X_TOOLKIT */
9415 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9416 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9417 unload_color (f, f->output_data.x->cursor_pixel);
9418 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9419 unload_color (f, f->output_data.x->border_pixel);
9420 unload_color (f, f->output_data.x->mouse_pixel);
9422 if (f->output_data.x->scroll_bar_background_pixel != -1)
9423 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9424 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9425 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9426 #ifdef USE_TOOLKIT_SCROLL_BARS
9427 /* Scrollbar shadow colors. */
9428 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9429 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9430 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9431 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9432 #endif /* USE_TOOLKIT_SCROLL_BARS */
9433 if (f->output_data.x->white_relief.allocated_p)
9434 unload_color (f, f->output_data.x->white_relief.pixel);
9435 if (f->output_data.x->black_relief.allocated_p)
9436 unload_color (f, f->output_data.x->black_relief.pixel);
9438 if (FRAME_FACE_CACHE (f))
9439 free_frame_faces (f);
9441 x_free_gcs (f);
9442 XFlush (FRAME_X_DISPLAY (f));
9445 if (f->output_data.x->saved_menu_event)
9446 xfree (f->output_data.x->saved_menu_event);
9448 xfree (f->output_data.x);
9449 f->output_data.x = NULL;
9451 if (f == dpyinfo->x_focus_frame)
9452 dpyinfo->x_focus_frame = 0;
9453 if (f == dpyinfo->x_focus_event_frame)
9454 dpyinfo->x_focus_event_frame = 0;
9455 if (f == dpyinfo->x_highlight_frame)
9456 dpyinfo->x_highlight_frame = 0;
9458 if (f == dpyinfo->mouse_face_mouse_frame)
9460 dpyinfo->mouse_face_beg_row
9461 = dpyinfo->mouse_face_beg_col = -1;
9462 dpyinfo->mouse_face_end_row
9463 = dpyinfo->mouse_face_end_col = -1;
9464 dpyinfo->mouse_face_window = Qnil;
9465 dpyinfo->mouse_face_deferred_gc = 0;
9466 dpyinfo->mouse_face_mouse_frame = 0;
9469 UNBLOCK_INPUT;
9473 /* Destroy the X window of frame F. */
9475 void
9476 x_destroy_window (f)
9477 struct frame *f;
9479 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9481 /* If a display connection is dead, don't try sending more
9482 commands to the X server. */
9483 if (dpyinfo->display != 0)
9484 x_free_frame_resources (f);
9486 dpyinfo->reference_count--;
9490 /* Setting window manager hints. */
9492 /* Set the normal size hints for the window manager, for frame F.
9493 FLAGS is the flags word to use--or 0 meaning preserve the flags
9494 that the window now has.
9495 If USER_POSITION is nonzero, we set the USPosition
9496 flag (this is useful when FLAGS is 0).
9497 The GTK version is in gtkutils.c */
9499 #ifndef USE_GTK
9500 void
9501 x_wm_set_size_hint (f, flags, user_position)
9502 struct frame *f;
9503 long flags;
9504 int user_position;
9506 XSizeHints size_hints;
9508 #ifdef USE_X_TOOLKIT
9509 Arg al[2];
9510 int ac = 0;
9511 Dimension widget_width, widget_height;
9512 #endif
9514 Window window = FRAME_OUTER_WINDOW (f);
9516 /* Setting PMaxSize caused various problems. */
9517 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9519 size_hints.x = f->left_pos;
9520 size_hints.y = f->top_pos;
9522 #ifdef USE_X_TOOLKIT
9523 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9524 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9525 XtGetValues (f->output_data.x->widget, al, ac);
9526 size_hints.height = widget_height;
9527 size_hints.width = widget_width;
9528 #else /* not USE_X_TOOLKIT */
9529 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9530 size_hints.width = FRAME_PIXEL_WIDTH (f);
9531 #endif /* not USE_X_TOOLKIT */
9533 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9534 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9535 size_hints.max_width
9536 = FRAME_X_DISPLAY_INFO (f)->width - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9537 size_hints.max_height
9538 = FRAME_X_DISPLAY_INFO (f)->height - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9540 /* Calculate the base and minimum sizes.
9542 (When we use the X toolkit, we don't do it here.
9543 Instead we copy the values that the widgets are using, below.) */
9544 #ifndef USE_X_TOOLKIT
9546 int base_width, base_height;
9547 int min_rows = 0, min_cols = 0;
9549 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9550 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9552 check_frame_size (f, &min_rows, &min_cols);
9554 /* The window manager uses the base width hints to calculate the
9555 current number of rows and columns in the frame while
9556 resizing; min_width and min_height aren't useful for this
9557 purpose, since they might not give the dimensions for a
9558 zero-row, zero-column frame.
9560 We use the base_width and base_height members if we have
9561 them; otherwise, we set the min_width and min_height members
9562 to the size for a zero x zero frame. */
9564 size_hints.flags |= PBaseSize;
9565 size_hints.base_width = base_width;
9566 size_hints.base_height = base_height;
9567 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9568 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9571 /* If we don't need the old flags, we don't need the old hint at all. */
9572 if (flags)
9574 size_hints.flags |= flags;
9575 goto no_read;
9577 #endif /* not USE_X_TOOLKIT */
9580 XSizeHints hints; /* Sometimes I hate X Windows... */
9581 long supplied_return;
9582 int value;
9584 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9585 &supplied_return);
9587 #ifdef USE_X_TOOLKIT
9588 size_hints.base_height = hints.base_height;
9589 size_hints.base_width = hints.base_width;
9590 size_hints.min_height = hints.min_height;
9591 size_hints.min_width = hints.min_width;
9592 #endif
9594 if (flags)
9595 size_hints.flags |= flags;
9596 else
9598 if (value == 0)
9599 hints.flags = 0;
9600 if (hints.flags & PSize)
9601 size_hints.flags |= PSize;
9602 if (hints.flags & PPosition)
9603 size_hints.flags |= PPosition;
9604 if (hints.flags & USPosition)
9605 size_hints.flags |= USPosition;
9606 if (hints.flags & USSize)
9607 size_hints.flags |= USSize;
9611 #ifndef USE_X_TOOLKIT
9612 no_read:
9613 #endif
9615 #ifdef PWinGravity
9616 size_hints.win_gravity = f->win_gravity;
9617 size_hints.flags |= PWinGravity;
9619 if (user_position)
9621 size_hints.flags &= ~ PPosition;
9622 size_hints.flags |= USPosition;
9624 #endif /* PWinGravity */
9626 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9628 #endif /* not USE_GTK */
9630 /* Used for IconicState or NormalState */
9632 void
9633 x_wm_set_window_state (f, state)
9634 struct frame *f;
9635 int state;
9637 #ifdef USE_X_TOOLKIT
9638 Arg al[1];
9640 XtSetArg (al[0], XtNinitialState, state);
9641 XtSetValues (f->output_data.x->widget, al, 1);
9642 #else /* not USE_X_TOOLKIT */
9643 Window window = FRAME_X_WINDOW (f);
9645 f->output_data.x->wm_hints.flags |= StateHint;
9646 f->output_data.x->wm_hints.initial_state = state;
9648 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9649 #endif /* not USE_X_TOOLKIT */
9652 void
9653 x_wm_set_icon_pixmap (f, pixmap_id)
9654 struct frame *f;
9655 int pixmap_id;
9657 Pixmap icon_pixmap, icon_mask;
9659 #ifndef USE_X_TOOLKIT
9660 Window window = FRAME_OUTER_WINDOW (f);
9661 #endif
9663 if (pixmap_id > 0)
9665 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9666 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9667 icon_mask = x_bitmap_mask (f, pixmap_id);
9668 f->output_data.x->wm_hints.icon_mask = icon_mask;
9670 else
9672 /* It seems there is no way to turn off use of an icon pixmap.
9673 The following line does it, only if no icon has yet been created,
9674 for some window managers. But with mwm it crashes.
9675 Some people say it should clear the IconPixmapHint bit in this case,
9676 but that doesn't work, and the X consortium said it isn't the
9677 right thing at all. Since there is no way to win,
9678 best to explicitly give up. */
9679 #if 0
9680 f->output_data.x->wm_hints.icon_pixmap = None;
9681 f->output_data.x->wm_hints.icon_mask = None;
9682 #else
9683 return;
9684 #endif
9688 #ifdef USE_GTK
9690 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9691 return;
9694 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9697 Arg al[1];
9698 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9699 XtSetValues (f->output_data.x->widget, al, 1);
9700 XtSetArg (al[0], XtNiconMask, icon_mask);
9701 XtSetValues (f->output_data.x->widget, al, 1);
9704 #else /* not USE_X_TOOLKIT && not USE_GTK */
9706 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9707 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9709 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9712 void
9713 x_wm_set_icon_position (f, icon_x, icon_y)
9714 struct frame *f;
9715 int icon_x, icon_y;
9717 Window window = FRAME_OUTER_WINDOW (f);
9719 f->output_data.x->wm_hints.flags |= IconPositionHint;
9720 f->output_data.x->wm_hints.icon_x = icon_x;
9721 f->output_data.x->wm_hints.icon_y = icon_y;
9723 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9727 /***********************************************************************
9728 Fonts
9729 ***********************************************************************/
9731 #if GLYPH_DEBUG
9733 /* Check that FONT is valid on frame F. It is if it can be found in F's
9734 font table. */
9736 static void
9737 x_check_font (f, font)
9738 struct frame *f;
9739 struct font *font;
9741 Lisp_Object frame;
9743 xassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9744 if (font->driver->check)
9745 xassert (font->driver->check (f, font) == 0);
9748 #endif /* GLYPH_DEBUG != 0 */
9751 /***********************************************************************
9752 Initialization
9753 ***********************************************************************/
9755 #ifdef USE_X_TOOLKIT
9756 static XrmOptionDescRec emacs_options[] = {
9757 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9758 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9760 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9761 XrmoptionSepArg, NULL},
9762 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9764 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9765 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9766 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9767 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9768 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9769 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9770 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9773 /* Whether atimer for Xt timeouts is activated or not. */
9775 static int x_timeout_atimer_activated_flag;
9777 #endif /* USE_X_TOOLKIT */
9779 static int x_initialized;
9781 #ifdef HAVE_X_SM
9782 static int x_session_initialized;
9783 #endif
9785 #ifdef MULTI_KBOARD
9786 /* Test whether two display-name strings agree up to the dot that separates
9787 the screen number from the server number. */
9788 static int
9789 same_x_server (name1, name2)
9790 const char *name1, *name2;
9792 int seen_colon = 0;
9793 const unsigned char *system_name = SDATA (Vsystem_name);
9794 int system_name_length = strlen (system_name);
9795 int length_until_period = 0;
9797 while (system_name[length_until_period] != 0
9798 && system_name[length_until_period] != '.')
9799 length_until_period++;
9801 /* Treat `unix' like an empty host name. */
9802 if (! strncmp (name1, "unix:", 5))
9803 name1 += 4;
9804 if (! strncmp (name2, "unix:", 5))
9805 name2 += 4;
9806 /* Treat this host's name like an empty host name. */
9807 if (! strncmp (name1, system_name, system_name_length)
9808 && name1[system_name_length] == ':')
9809 name1 += system_name_length;
9810 if (! strncmp (name2, system_name, system_name_length)
9811 && name2[system_name_length] == ':')
9812 name2 += system_name_length;
9813 /* Treat this host's domainless name like an empty host name. */
9814 if (! strncmp (name1, system_name, length_until_period)
9815 && name1[length_until_period] == ':')
9816 name1 += length_until_period;
9817 if (! strncmp (name2, system_name, length_until_period)
9818 && name2[length_until_period] == ':')
9819 name2 += length_until_period;
9821 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
9823 if (*name1 == ':')
9824 seen_colon++;
9825 if (seen_colon && *name1 == '.')
9826 return 1;
9828 return (seen_colon
9829 && (*name1 == '.' || *name1 == '\0')
9830 && (*name2 == '.' || *name2 == '\0'));
9832 #endif
9834 /* Count number of set bits in mask and number of bits to shift to
9835 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
9836 to 5. */
9837 static void
9838 get_bits_and_offset (mask, bits, offset)
9839 unsigned long mask;
9840 int *bits;
9841 int *offset;
9843 int nr = 0;
9844 int off = 0;
9846 while (!(mask & 1))
9848 off++;
9849 mask >>= 1;
9852 while (mask & 1)
9854 nr++;
9855 mask >>= 1;
9858 *offset = off;
9859 *bits = nr;
9862 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
9863 But don't permanently open it, just test its availability. */
9866 x_display_ok (display)
9867 const char *display;
9869 int dpy_ok = 1;
9870 Display *dpy;
9872 dpy = XOpenDisplay (display);
9873 if (dpy)
9874 XCloseDisplay (dpy);
9875 else
9876 dpy_ok = 0;
9877 return dpy_ok;
9880 /* Open a connection to X display DISPLAY_NAME, and return
9881 the structure that describes the open display.
9882 If we cannot contact the display, return null. */
9884 struct x_display_info *
9885 x_term_init (display_name, xrm_option, resource_name)
9886 Lisp_Object display_name;
9887 char *xrm_option;
9888 char *resource_name;
9890 int connection;
9891 Display *dpy;
9892 struct terminal *terminal;
9893 struct x_display_info *dpyinfo;
9894 XrmDatabase xrdb;
9896 BLOCK_INPUT;
9898 if (!x_initialized)
9900 x_initialize ();
9901 ++x_initialized;
9904 if (! x_display_ok (SDATA (display_name)))
9905 error ("Display %s can't be opened", SDATA (display_name));
9907 #ifdef USE_GTK
9909 #define NUM_ARGV 10
9910 int argc;
9911 char *argv[NUM_ARGV];
9912 char **argv2 = argv;
9913 GdkAtom atom;
9915 #ifndef HAVE_GTK_MULTIDISPLAY
9916 if (!EQ (Vinitial_window_system, intern ("x")))
9917 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
9918 #endif
9920 if (x_initialized++ > 1)
9922 #ifdef HAVE_GTK_MULTIDISPLAY
9923 /* Opening another display. If xg_display_open returns less
9924 than zero, we are probably on GTK 2.0, which can only handle
9925 one display. GTK 2.2 or later can handle more than one. */
9926 if (xg_display_open (SDATA (display_name), &dpy) < 0)
9927 #endif
9928 error ("Sorry, this version of GTK can only handle one display");
9930 else
9932 for (argc = 0; argc < NUM_ARGV; ++argc)
9933 argv[argc] = 0;
9935 argc = 0;
9936 argv[argc++] = initial_argv[0];
9938 if (! NILP (display_name))
9940 argv[argc++] = "--display";
9941 argv[argc++] = SDATA (display_name);
9944 argv[argc++] = "--name";
9945 argv[argc++] = resource_name;
9947 XSetLocaleModifiers ("");
9949 gtk_init (&argc, &argv2);
9951 /* gtk_init does set_locale. We must fix locale after calling it. */
9952 fixup_locale ();
9953 xg_initialize ();
9955 dpy = GDK_DISPLAY ();
9957 /* NULL window -> events for all windows go to our function */
9958 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
9960 /* Load our own gtkrc if it exists. */
9962 char *file = "~/.emacs.d/gtkrc";
9963 Lisp_Object s, abs_file;
9965 s = make_string (file, strlen (file));
9966 abs_file = Fexpand_file_name (s, Qnil);
9968 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
9969 gtk_rc_parse (SDATA (abs_file));
9972 XSetErrorHandler (x_error_handler);
9973 XSetIOErrorHandler (x_io_error_quitter);
9976 #else /* not USE_GTK */
9977 #ifdef USE_X_TOOLKIT
9978 /* weiner@footloose.sps.mot.com reports that this causes
9979 errors with X11R5:
9980 X protocol error: BadAtom (invalid Atom parameter)
9981 on protocol request 18skiloaf.
9982 So let's not use it until R6. */
9983 #ifdef HAVE_X11XTR6
9984 XtSetLanguageProc (NULL, NULL, NULL);
9985 #endif
9988 int argc = 0;
9989 char *argv[3];
9991 argv[0] = "";
9992 argc = 1;
9993 if (xrm_option)
9995 argv[argc++] = "-xrm";
9996 argv[argc++] = xrm_option;
9998 turn_on_atimers (0);
9999 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10000 resource_name, EMACS_CLASS,
10001 emacs_options, XtNumber (emacs_options),
10002 &argc, argv);
10003 turn_on_atimers (1);
10005 #ifdef HAVE_X11XTR6
10006 /* I think this is to compensate for XtSetLanguageProc. */
10007 fixup_locale ();
10008 #endif
10011 #else /* not USE_X_TOOLKIT */
10012 XSetLocaleModifiers ("");
10013 dpy = XOpenDisplay (SDATA (display_name));
10014 #endif /* not USE_X_TOOLKIT */
10015 #endif /* not USE_GTK*/
10017 /* Detect failure. */
10018 if (dpy == 0)
10020 UNBLOCK_INPUT;
10021 return 0;
10024 /* We have definitely succeeded. Record the new connection. */
10026 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10027 bzero (dpyinfo, sizeof *dpyinfo);
10029 terminal = x_create_terminal (dpyinfo);
10031 #ifdef MULTI_KBOARD
10033 struct x_display_info *share;
10034 Lisp_Object tail;
10036 for (share = x_display_list, tail = x_display_name_list; share;
10037 share = share->next, tail = XCDR (tail))
10038 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10039 SDATA (display_name)))
10040 break;
10041 if (share)
10042 terminal->kboard = share->terminal->kboard;
10043 else
10045 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10046 init_kboard (terminal->kboard);
10047 terminal->kboard->Vwindow_system = intern ("x");
10048 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10050 char *vendor = ServerVendor (dpy);
10051 UNBLOCK_INPUT;
10052 terminal->kboard->Vsystem_key_alist
10053 = call1 (Qvendor_specific_keysyms,
10054 vendor ? build_string (vendor) : empty_unibyte_string);
10055 BLOCK_INPUT;
10058 terminal->kboard->next_kboard = all_kboards;
10059 all_kboards = terminal->kboard;
10060 /* Don't let the initial kboard remain current longer than necessary.
10061 That would cause problems if a file loaded on startup tries to
10062 prompt in the mini-buffer. */
10063 if (current_kboard == initial_kboard)
10064 current_kboard = terminal->kboard;
10066 terminal->kboard->reference_count++;
10068 #endif
10070 /* Put this display on the chain. */
10071 dpyinfo->next = x_display_list;
10072 x_display_list = dpyinfo;
10074 /* Put it on x_display_name_list as well, to keep them parallel. */
10075 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10076 x_display_name_list);
10077 dpyinfo->name_list_element = XCAR (x_display_name_list);
10079 dpyinfo->display = dpy;
10081 /* Set the name of the terminal. */
10082 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10083 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10084 terminal->name[SBYTES (display_name)] = 0;
10086 #if 0
10087 XSetAfterFunction (x_current_display, x_trace_wire);
10088 #endif /* ! 0 */
10090 dpyinfo->x_id_name
10091 = (char *) xmalloc (SBYTES (Vinvocation_name)
10092 + SBYTES (Vsystem_name)
10093 + 2);
10094 sprintf (dpyinfo->x_id_name, "%s@%s",
10095 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10097 /* Figure out which modifier bits mean what. */
10098 x_find_modifier_meanings (dpyinfo);
10100 /* Get the scroll bar cursor. */
10101 #ifdef USE_GTK
10102 /* We must create a GTK cursor, it is required for GTK widgets. */
10103 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10104 #endif /* USE_GTK */
10106 dpyinfo->vertical_scroll_bar_cursor
10107 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10109 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10110 resource_name, EMACS_CLASS);
10111 #ifdef HAVE_XRMSETDATABASE
10112 XrmSetDatabase (dpyinfo->display, xrdb);
10113 #else
10114 dpyinfo->display->db = xrdb;
10115 #endif
10116 /* Put the rdb where we can find it in a way that works on
10117 all versions. */
10118 dpyinfo->xrdb = xrdb;
10120 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10121 DefaultScreen (dpyinfo->display));
10122 select_visual (dpyinfo);
10123 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10124 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
10125 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
10126 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10127 dpyinfo->client_leader_window = 0;
10128 dpyinfo->grabbed = 0;
10129 dpyinfo->reference_count = 0;
10130 dpyinfo->icon_bitmap_id = -1;
10131 dpyinfo->n_fonts = 0;
10132 dpyinfo->bitmaps = 0;
10133 dpyinfo->bitmaps_size = 0;
10134 dpyinfo->bitmaps_last = 0;
10135 dpyinfo->scratch_cursor_gc = 0;
10136 dpyinfo->mouse_face_mouse_frame = 0;
10137 dpyinfo->mouse_face_deferred_gc = 0;
10138 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10139 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10140 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10141 dpyinfo->mouse_face_window = Qnil;
10142 dpyinfo->mouse_face_overlay = Qnil;
10143 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10144 dpyinfo->mouse_face_defer = 0;
10145 dpyinfo->mouse_face_hidden = 0;
10146 dpyinfo->x_focus_frame = 0;
10147 dpyinfo->x_focus_event_frame = 0;
10148 dpyinfo->x_highlight_frame = 0;
10149 dpyinfo->terminal->image_cache = make_image_cache ();
10150 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10152 /* See if we can construct pixel values from RGB values. */
10153 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10154 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10156 if (dpyinfo->visual->class == TrueColor)
10158 get_bits_and_offset (dpyinfo->visual->red_mask,
10159 &dpyinfo->red_bits, &dpyinfo->red_offset);
10160 get_bits_and_offset (dpyinfo->visual->blue_mask,
10161 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10162 get_bits_and_offset (dpyinfo->visual->green_mask,
10163 &dpyinfo->green_bits, &dpyinfo->green_offset);
10166 /* See if a private colormap is requested. */
10167 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10169 if (dpyinfo->visual->class == PseudoColor)
10171 Lisp_Object value;
10172 value = display_x_get_resource (dpyinfo,
10173 build_string ("privateColormap"),
10174 build_string ("PrivateColormap"),
10175 Qnil, Qnil);
10176 if (STRINGP (value)
10177 && (!strcmp (SDATA (value), "true")
10178 || !strcmp (SDATA (value), "on")))
10179 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10182 else
10183 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10184 dpyinfo->visual, AllocNone);
10187 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10188 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10189 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10190 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10191 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10192 pixels = DisplayWidth (dpyinfo->display, screen_number);
10193 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10194 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10195 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10198 dpyinfo->Xatom_wm_protocols
10199 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10200 dpyinfo->Xatom_wm_take_focus
10201 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10202 dpyinfo->Xatom_wm_save_yourself
10203 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10204 dpyinfo->Xatom_wm_delete_window
10205 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10206 dpyinfo->Xatom_wm_change_state
10207 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10208 dpyinfo->Xatom_wm_configure_denied
10209 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10210 dpyinfo->Xatom_wm_window_moved
10211 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10212 dpyinfo->Xatom_wm_client_leader
10213 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10214 dpyinfo->Xatom_editres
10215 = XInternAtom (dpyinfo->display, "Editres", False);
10216 dpyinfo->Xatom_CLIPBOARD
10217 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10218 dpyinfo->Xatom_TIMESTAMP
10219 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10220 dpyinfo->Xatom_TEXT
10221 = XInternAtom (dpyinfo->display, "TEXT", False);
10222 dpyinfo->Xatom_COMPOUND_TEXT
10223 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10224 dpyinfo->Xatom_UTF8_STRING
10225 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10226 dpyinfo->Xatom_DELETE
10227 = XInternAtom (dpyinfo->display, "DELETE", False);
10228 dpyinfo->Xatom_MULTIPLE
10229 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10230 dpyinfo->Xatom_INCR
10231 = XInternAtom (dpyinfo->display, "INCR", False);
10232 dpyinfo->Xatom_EMACS_TMP
10233 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10234 dpyinfo->Xatom_TARGETS
10235 = XInternAtom (dpyinfo->display, "TARGETS", False);
10236 dpyinfo->Xatom_NULL
10237 = XInternAtom (dpyinfo->display, "NULL", False);
10238 dpyinfo->Xatom_ATOM_PAIR
10239 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10240 /* For properties of font. */
10241 dpyinfo->Xatom_PIXEL_SIZE
10242 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10243 dpyinfo->Xatom_AVERAGE_WIDTH
10244 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10245 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10246 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10247 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10248 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10249 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10250 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10252 /* Ghostscript support. */
10253 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10254 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10256 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10257 False);
10259 dpyinfo->Xatom_XEMBED = XInternAtom (dpyinfo->display, "_XEMBED",
10260 False);
10262 dpyinfo->cut_buffers_initialized = 0;
10264 dpyinfo->x_dnd_atoms_size = 8;
10265 dpyinfo->x_dnd_atoms_length = 0;
10266 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10267 * dpyinfo->x_dnd_atoms_size);
10269 dpyinfo->net_supported_atoms = NULL;
10270 dpyinfo->nr_net_supported_atoms = 0;
10271 dpyinfo->net_supported_window = 0;
10273 connection = ConnectionNumber (dpyinfo->display);
10274 dpyinfo->connection = connection;
10277 char null_bits[1];
10279 null_bits[0] = 0x00;
10281 dpyinfo->null_pixel
10282 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10283 null_bits, 1, 1, (long) 0, (long) 0,
10288 extern int gray_bitmap_width, gray_bitmap_height;
10289 extern char *gray_bitmap_bits;
10290 dpyinfo->gray
10291 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10292 gray_bitmap_bits,
10293 gray_bitmap_width, gray_bitmap_height,
10294 (unsigned long) 1, (unsigned long) 0, 1);
10297 #ifdef HAVE_X_I18N
10298 xim_initialize (dpyinfo, resource_name);
10299 #endif
10301 #ifdef subprocesses
10302 /* This is only needed for distinguishing keyboard and process input. */
10303 if (connection != 0)
10304 add_keyboard_wait_descriptor (connection);
10305 #endif
10307 #ifdef F_SETOWN
10308 fcntl (connection, F_SETOWN, getpid ());
10309 #endif /* ! defined (F_SETOWN) */
10311 #ifdef SIGIO
10312 if (interrupt_input)
10313 init_sigio (connection);
10314 #endif /* ! defined (SIGIO) */
10316 #ifdef USE_LUCID
10318 Display *dpy = dpyinfo->display;
10319 XrmValue d, fr, to;
10320 Font font;
10322 d.addr = (XPointer)&dpy;
10323 d.size = sizeof (Display *);
10324 fr.addr = XtDefaultFont;
10325 fr.size = sizeof (XtDefaultFont);
10326 to.size = sizeof (Font *);
10327 to.addr = (XPointer)&font;
10328 x_catch_errors (dpy);
10329 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10330 abort ();
10331 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10332 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10333 x_uncatch_errors ();
10335 #endif
10337 /* See if we should run in synchronous mode. This is useful
10338 for debugging X code. */
10340 Lisp_Object value;
10341 value = display_x_get_resource (dpyinfo,
10342 build_string ("synchronous"),
10343 build_string ("Synchronous"),
10344 Qnil, Qnil);
10345 if (STRINGP (value)
10346 && (!strcmp (SDATA (value), "true")
10347 || !strcmp (SDATA (value), "on")))
10348 XSynchronize (dpyinfo->display, True);
10352 Lisp_Object value;
10353 value = display_x_get_resource (dpyinfo,
10354 build_string ("useXIM"),
10355 build_string ("UseXIM"),
10356 Qnil, Qnil);
10357 #ifdef USE_XIM
10358 if (STRINGP (value)
10359 && (!strcmp (SDATA (value), "false")
10360 || !strcmp (SDATA (value), "off")))
10361 use_xim = 0;
10362 #else
10363 if (STRINGP (value)
10364 && (!strcmp (SDATA (value), "true")
10365 || !strcmp (SDATA (value), "on")))
10366 use_xim = 1;
10367 #endif
10370 #ifdef HAVE_X_SM
10371 /* Only do this for the very first display in the Emacs session.
10372 Ignore X session management when Emacs was first started on a
10373 tty. */
10374 if (terminal->id == 1)
10375 x_session_initialize (dpyinfo);
10376 #endif
10378 UNBLOCK_INPUT;
10380 return dpyinfo;
10383 /* Get rid of display DPYINFO, deleting all frames on it,
10384 and without sending any more commands to the X server. */
10386 void
10387 x_delete_display (dpyinfo)
10388 struct x_display_info *dpyinfo;
10390 int i;
10391 struct terminal *t;
10393 /* Close all frames and delete the generic struct terminal for this
10394 X display. */
10395 for (t = terminal_list; t; t = t->next_terminal)
10396 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10398 #ifdef HAVE_X_SM
10399 /* Close X session management when we close its display. */
10400 if (t->id == 1 && x_session_have_connection ())
10401 x_session_close();
10402 #endif
10403 delete_terminal (t);
10404 break;
10407 delete_keyboard_wait_descriptor (dpyinfo->connection);
10409 /* Discard this display from x_display_name_list and x_display_list.
10410 We can't use Fdelq because that can quit. */
10411 if (! NILP (x_display_name_list)
10412 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10413 x_display_name_list = XCDR (x_display_name_list);
10414 else
10416 Lisp_Object tail;
10418 tail = x_display_name_list;
10419 while (CONSP (tail) && CONSP (XCDR (tail)))
10421 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10423 XSETCDR (tail, XCDR (XCDR (tail)));
10424 break;
10426 tail = XCDR (tail);
10430 if (next_noop_dpyinfo == dpyinfo)
10431 next_noop_dpyinfo = dpyinfo->next;
10433 if (x_display_list == dpyinfo)
10434 x_display_list = dpyinfo->next;
10435 else
10437 struct x_display_info *tail;
10439 for (tail = x_display_list; tail; tail = tail->next)
10440 if (tail->next == dpyinfo)
10441 tail->next = tail->next->next;
10444 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
10445 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
10446 XrmDestroyDatabase (dpyinfo->xrdb);
10447 #endif
10448 #endif
10449 #ifdef HAVE_X_I18N
10450 if (dpyinfo->xim)
10451 xim_close_dpy (dpyinfo);
10452 #endif
10454 if (dpyinfo->x_id_name)
10455 xfree (dpyinfo->x_id_name);
10456 if (dpyinfo->color_cells)
10457 xfree (dpyinfo->color_cells);
10458 xfree (dpyinfo);
10461 #ifdef USE_X_TOOLKIT
10463 /* Atimer callback function for TIMER. Called every 0.1s to process
10464 Xt timeouts, if needed. We must avoid calling XtAppPending as
10465 much as possible because that function does an implicit XFlush
10466 that slows us down. */
10468 static void
10469 x_process_timeouts (timer)
10470 struct atimer *timer;
10472 BLOCK_INPUT;
10473 x_timeout_atimer_activated_flag = 0;
10474 if (toolkit_scroll_bar_interaction || popup_activated ())
10476 while (XtAppPending (Xt_app_con) & XtIMTimer)
10477 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10478 /* Reactivate the atimer for next time. */
10479 x_activate_timeout_atimer ();
10481 UNBLOCK_INPUT;
10484 /* Install an asynchronous timer that processes Xt timeout events
10485 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10486 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10487 function whenever these variables are set. This is necessary
10488 because some widget sets use timeouts internally, for example the
10489 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10490 processed, these widgets don't behave normally. */
10492 void
10493 x_activate_timeout_atimer ()
10495 BLOCK_INPUT;
10496 if (!x_timeout_atimer_activated_flag)
10498 EMACS_TIME interval;
10500 EMACS_SET_SECS_USECS (interval, 0, 100000);
10501 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10502 x_timeout_atimer_activated_flag = 1;
10504 UNBLOCK_INPUT;
10507 #endif /* USE_X_TOOLKIT */
10510 /* Set up use of X before we make the first connection. */
10512 extern frame_parm_handler x_frame_parm_handlers[];
10514 static struct redisplay_interface x_redisplay_interface =
10516 x_frame_parm_handlers,
10517 x_produce_glyphs,
10518 x_write_glyphs,
10519 x_insert_glyphs,
10520 x_clear_end_of_line,
10521 x_scroll_run,
10522 x_after_update_window_line,
10523 x_update_window_begin,
10524 x_update_window_end,
10525 x_cursor_to,
10526 x_flush,
10527 #ifdef XFlush
10528 x_flush,
10529 #else
10530 0, /* flush_display_optional */
10531 #endif
10532 x_clear_window_mouse_face,
10533 x_get_glyph_overhangs,
10534 x_fix_overlapping_area,
10535 x_draw_fringe_bitmap,
10536 0, /* define_fringe_bitmap */
10537 0, /* destroy_fringe_bitmap */
10538 x_compute_glyph_string_overhangs,
10539 x_draw_glyph_string,
10540 x_define_frame_cursor,
10541 x_clear_frame_area,
10542 x_draw_window_cursor,
10543 x_draw_vertical_window_border,
10544 x_shift_glyphs_for_insert
10548 /* This function is called when the last frame on a display is deleted. */
10549 void
10550 x_delete_terminal (struct terminal *terminal)
10552 struct x_display_info *dpyinfo = terminal->display_info.x;
10553 int i;
10555 /* Protect against recursive calls. Fdelete_frame in
10556 delete_terminal calls us back when it deletes our last frame. */
10557 if (!terminal->name)
10558 return;
10560 BLOCK_INPUT;
10561 /* If called from x_connection_closed, the display may already be closed
10562 and dpyinfo->display was set to 0 to indicate that. */
10563 if (dpyinfo->display)
10565 x_destroy_all_bitmaps (dpyinfo);
10566 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10568 #ifdef USE_GTK
10569 xg_display_close (dpyinfo->display);
10570 #else
10571 #ifdef USE_X_TOOLKIT
10572 XtCloseDisplay (dpyinfo->display);
10573 #else
10574 XCloseDisplay (dpyinfo->display);
10575 #endif
10576 #endif /* ! USE_GTK */
10579 x_delete_display (dpyinfo);
10580 UNBLOCK_INPUT;
10583 /* Create a struct terminal, initialize it with the X11 specific
10584 functions and make DISPLAY->TERMINAL point to it. */
10586 static struct terminal *
10587 x_create_terminal (struct x_display_info *dpyinfo)
10589 struct terminal *terminal;
10591 terminal = create_terminal ();
10593 terminal->type = output_x_window;
10594 terminal->display_info.x = dpyinfo;
10595 dpyinfo->terminal = terminal;
10597 /* kboard is initialized in x_term_init. */
10599 terminal->clear_frame_hook = x_clear_frame;
10600 terminal->ins_del_lines_hook = x_ins_del_lines;
10601 terminal->delete_glyphs_hook = x_delete_glyphs;
10602 terminal->ring_bell_hook = XTring_bell;
10603 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
10604 terminal->set_terminal_modes_hook = XTset_terminal_modes;
10605 terminal->update_begin_hook = x_update_begin;
10606 terminal->update_end_hook = x_update_end;
10607 terminal->set_terminal_window_hook = XTset_terminal_window;
10608 terminal->read_socket_hook = XTread_socket;
10609 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10610 terminal->mouse_position_hook = XTmouse_position;
10611 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10612 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10613 terminal->fullscreen_hook = XTfullscreen_hook;
10614 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10615 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10616 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10617 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10619 terminal->delete_frame_hook = x_destroy_window;
10620 terminal->delete_terminal_hook = x_delete_terminal;
10622 terminal->rif = &x_redisplay_interface;
10623 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10624 terminal->char_ins_del_ok = 1;
10625 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10626 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10627 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10628 off the bottom. */
10630 return terminal;
10633 void
10634 x_initialize ()
10636 baud_rate = 19200;
10638 x_noop_count = 0;
10639 last_tool_bar_item = -1;
10640 any_help_event_p = 0;
10641 ignore_next_mouse_click_timeout = 0;
10642 #ifdef HAVE_X_SM
10643 x_session_initialized = 0;
10644 #endif
10646 #ifdef USE_GTK
10647 current_count = -1;
10648 #endif
10650 /* Try to use interrupt input; if we can't, then start polling. */
10651 Fset_input_interrupt_mode (Qt);
10653 #ifdef USE_X_TOOLKIT
10654 XtToolkitInitialize ();
10656 Xt_app_con = XtCreateApplicationContext ();
10658 /* Register a converter from strings to pixels, which uses
10659 Emacs' color allocation infrastructure. */
10660 XtAppSetTypeConverter (Xt_app_con,
10661 XtRString, XtRPixel, cvt_string_to_pixel,
10662 cvt_string_to_pixel_args,
10663 XtNumber (cvt_string_to_pixel_args),
10664 XtCacheByDisplay, cvt_pixel_dtor);
10666 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10667 #endif
10669 #ifdef USE_TOOLKIT_SCROLL_BARS
10670 #ifndef USE_GTK
10671 xaw3d_arrow_scroll = False;
10672 xaw3d_pick_top = True;
10673 #endif
10674 #endif
10676 /* Note that there is no real way portable across R3/R4 to get the
10677 original error handler. */
10678 XSetErrorHandler (x_error_handler);
10679 XSetIOErrorHandler (x_io_error_quitter);
10681 /* Disable Window Change signals; they are handled by X events. */
10682 #if 0 /* Don't. We may want to open tty frames later. */
10683 #ifdef SIGWINCH
10684 signal (SIGWINCH, SIG_DFL);
10685 #endif /* SIGWINCH */
10686 #endif
10688 signal (SIGPIPE, x_connection_signal);
10692 void
10693 syms_of_xterm ()
10695 x_error_message = NULL;
10697 staticpro (&x_display_name_list);
10698 x_display_name_list = Qnil;
10700 staticpro (&last_mouse_scroll_bar);
10701 last_mouse_scroll_bar = Qnil;
10703 staticpro (&Qvendor_specific_keysyms);
10704 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10706 staticpro (&Qlatin_1);
10707 Qlatin_1 = intern ("latin-1");
10709 staticpro (&last_mouse_press_frame);
10710 last_mouse_press_frame = Qnil;
10712 DEFVAR_BOOL ("x-use-underline-position-properties",
10713 &x_use_underline_position_properties,
10714 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10715 A value of nil means ignore them. If you encounter fonts with bogus
10716 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10717 to 4.1, set this to nil. */);
10718 x_use_underline_position_properties = 1;
10720 DEFVAR_BOOL ("x-underline-at-descent-line",
10721 &x_underline_at_descent_line,
10722 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
10723 A value of nil means to draw the underline according to the value of the
10724 variable `x-use-underline-position-properties', which is usually at the
10725 baseline level. The default value is nil. */);
10726 x_underline_at_descent_line = 0;
10728 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10729 &x_mouse_click_focus_ignore_position,
10730 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10731 This variable is only used when the window manager requires that you
10732 click on a frame to select it (give it focus). In that case, a value
10733 of nil, means that the selected window and cursor position changes to
10734 reflect the mouse click position, while a non-nil value means that the
10735 selected window or cursor position is preserved. */);
10736 x_mouse_click_focus_ignore_position = 0;
10738 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10739 doc: /* What X toolkit scroll bars Emacs uses.
10740 A value of nil means Emacs doesn't use X toolkit scroll bars.
10741 Otherwise, value is a symbol describing the X toolkit. */);
10742 #ifdef USE_TOOLKIT_SCROLL_BARS
10743 #ifdef USE_MOTIF
10744 Vx_toolkit_scroll_bars = intern ("motif");
10745 #elif defined HAVE_XAW3D
10746 Vx_toolkit_scroll_bars = intern ("xaw3d");
10747 #elif USE_GTK
10748 Vx_toolkit_scroll_bars = intern ("gtk");
10749 #else
10750 Vx_toolkit_scroll_bars = intern ("xaw");
10751 #endif
10752 #else
10753 Vx_toolkit_scroll_bars = Qnil;
10754 #endif
10756 staticpro (&last_mouse_motion_frame);
10757 last_mouse_motion_frame = Qnil;
10759 Qmodifier_value = intern ("modifier-value");
10760 Qalt = intern ("alt");
10761 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10762 Qhyper = intern ("hyper");
10763 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10764 Qmeta = intern ("meta");
10765 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10766 Qsuper = intern ("super");
10767 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10769 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10770 doc: /* Which keys Emacs uses for the alt modifier.
10771 This should be one of the symbols `alt', `hyper', `meta', `super'.
10772 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10773 is nil, which is the same as `alt'. */);
10774 Vx_alt_keysym = Qnil;
10776 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
10777 doc: /* Which keys Emacs uses for the hyper modifier.
10778 This should be one of the symbols `alt', `hyper', `meta', `super'.
10779 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10780 default is nil, which is the same as `hyper'. */);
10781 Vx_hyper_keysym = Qnil;
10783 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
10784 doc: /* Which keys Emacs uses for the meta modifier.
10785 This should be one of the symbols `alt', `hyper', `meta', `super'.
10786 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10787 default is nil, which is the same as `meta'. */);
10788 Vx_meta_keysym = Qnil;
10790 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
10791 doc: /* Which keys Emacs uses for the super modifier.
10792 This should be one of the symbols `alt', `hyper', `meta', `super'.
10793 For example, `super' means use the Super_L and Super_R keysyms. The
10794 default is nil, which is the same as `super'. */);
10795 Vx_super_keysym = Qnil;
10797 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
10798 doc: /* Hash table of character codes indexed by X keysym codes. */);
10799 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
10800 make_float (DEFAULT_REHASH_SIZE),
10801 make_float (DEFAULT_REHASH_THRESHOLD),
10802 Qnil, Qnil, Qnil);
10805 #endif /* HAVE_X_WINDOWS */
10807 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
10808 (do not change this comment) */