Fix typo.
[emacs/old-mirror.git] / src / xterm.c
blob00064758cdcc02556efe1216b1e7be3600aeec03
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, 2009
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"
87 #include "fontset.h"
89 #ifdef USE_X_TOOLKIT
90 #include <X11/Shell.h>
91 #endif
93 #ifdef HAVE_SYS_TIME_H
94 #include <sys/time.h>
95 #endif
96 #ifdef HAVE_UNISTD_H
97 #include <unistd.h>
98 #endif
100 #ifdef USE_GTK
101 #include "gtkutil.h"
102 #endif
104 #ifdef USE_LUCID
105 extern int xlwmenu_window_p P_ ((Widget w, Window window));
106 extern void xlwmenu_redisplay P_ ((Widget));
107 #endif
109 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
111 extern void free_frame_menubar P_ ((struct frame *));
112 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
113 int));
114 #endif
116 #ifdef USE_X_TOOLKIT
117 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
118 #define HACK_EDITRES
119 extern void _XEditResCheckMessages ();
120 #endif /* not NO_EDITRES */
122 /* Include toolkit specific headers for the scroll bar widget. */
124 #ifdef USE_TOOLKIT_SCROLL_BARS
125 #if defined USE_MOTIF
126 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
127 #include <Xm/ScrollBar.h>
128 #else /* !USE_MOTIF i.e. use Xaw */
130 #ifdef HAVE_XAW3D
131 #include <X11/Xaw3d/Simple.h>
132 #include <X11/Xaw3d/Scrollbar.h>
133 #include <X11/Xaw3d/ThreeD.h>
134 #else /* !HAVE_XAW3D */
135 #include <X11/Xaw/Simple.h>
136 #include <X11/Xaw/Scrollbar.h>
137 #endif /* !HAVE_XAW3D */
138 #ifndef XtNpickTop
139 #define XtNpickTop "pickTop"
140 #endif /* !XtNpickTop */
141 #endif /* !USE_MOTIF */
142 #endif /* USE_TOOLKIT_SCROLL_BARS */
144 #endif /* USE_X_TOOLKIT */
146 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
147 #define x_any_window_to_frame x_window_to_frame
148 #define x_top_window_to_frame x_window_to_frame
149 #endif
151 #ifdef USE_X_TOOLKIT
152 #include "widget.h"
153 #ifndef XtNinitialState
154 #define XtNinitialState "initialState"
155 #endif
156 #endif
158 /* Default to using XIM if available. */
159 #ifdef USE_XIM
160 int use_xim = 1;
161 #else
162 int use_xim = 0; /* configure --without-xim */
163 #endif
167 /* Non-nil means Emacs uses toolkit scroll bars. */
169 Lisp_Object Vx_toolkit_scroll_bars;
171 /* Non-zero means that a HELP_EVENT has been generated since Emacs
172 start. */
174 static int any_help_event_p;
176 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
177 static Lisp_Object last_window;
179 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
181 int x_use_underline_position_properties;
183 /* Non-zero means to draw the underline at the same place as the descent line. */
185 int x_underline_at_descent_line;
187 /* This is a chain of structures for all the X displays currently in
188 use. */
190 struct x_display_info *x_display_list;
192 /* This is a list of cons cells, each of the form (NAME
193 . FONT-LIST-CACHE), one for each element of x_display_list and in
194 the same order. NAME is the name of the frame. FONT-LIST-CACHE
195 records previous values returned by x-list-fonts. */
197 Lisp_Object x_display_name_list;
199 /* Frame being updated by update_frame. This is declared in term.c.
200 This is set by update_begin and looked at by all the XT functions.
201 It is zero while not inside an update. In that case, the XT
202 functions assume that `selected_frame' is the frame to apply to. */
204 extern struct frame *updating_frame;
206 /* This is a frame waiting to be auto-raised, within XTread_socket. */
208 struct frame *pending_autoraise_frame;
210 #ifdef USE_X_TOOLKIT
211 /* The application context for Xt use. */
212 XtAppContext Xt_app_con;
213 static String Xt_default_resources[] = {0};
214 #endif /* USE_X_TOOLKIT */
216 /* Non-zero means user is interacting with a toolkit scroll bar. */
218 static int toolkit_scroll_bar_interaction;
220 /* Non-zero means to not move point as a result of clicking on a
221 frame to focus it (when focus-follows-mouse is nil). */
223 int x_mouse_click_focus_ignore_position;
225 /* Non-zero timeout value means ignore next mouse click if it arrives
226 before that timeout elapses (i.e. as part of the same sequence of
227 events resulting from clicking on a frame to select it). */
229 static unsigned long ignore_next_mouse_click_timeout;
231 /* Mouse movement.
233 Formerly, we used PointerMotionHintMask (in standard_event_mask)
234 so that we would have to call XQueryPointer after each MotionNotify
235 event to ask for another such event. However, this made mouse tracking
236 slow, and there was a bug that made it eventually stop.
238 Simply asking for MotionNotify all the time seems to work better.
240 In order to avoid asking for motion events and then throwing most
241 of them away or busy-polling the server for mouse positions, we ask
242 the server for pointer motion hints. This means that we get only
243 one event per group of mouse movements. "Groups" are delimited by
244 other kinds of events (focus changes and button clicks, for
245 example), or by XQueryPointer calls; when one of these happens, we
246 get another MotionNotify event the next time the mouse moves. This
247 is at least as efficient as getting motion events when mouse
248 tracking is on, and I suspect only negligibly worse when tracking
249 is off. */
251 /* Where the mouse was last time we reported a mouse event. */
253 static XRectangle last_mouse_glyph;
254 static FRAME_PTR last_mouse_glyph_frame;
255 static Lisp_Object last_mouse_press_frame;
257 /* The scroll bar in which the last X motion event occurred.
259 If the last X motion event occurred in a scroll bar, we set this so
260 XTmouse_position can know whether to report a scroll bar motion or
261 an ordinary motion.
263 If the last X motion event didn't occur in a scroll bar, we set
264 this to Qnil, to tell XTmouse_position to return an ordinary motion
265 event. */
267 static Lisp_Object last_mouse_scroll_bar;
269 /* This is a hack. We would really prefer that XTmouse_position would
270 return the time associated with the position it returns, but there
271 doesn't seem to be any way to wrest the time-stamp from the server
272 along with the position query. So, we just keep track of the time
273 of the last movement we received, and return that in hopes that
274 it's somewhat accurate. */
276 static Time last_mouse_movement_time;
278 /* Time for last user interaction as returned in X events. */
280 static Time last_user_time;
282 /* Incremented by XTread_socket whenever it really tries to read
283 events. */
285 #ifdef __STDC__
286 static int volatile input_signal_count;
287 #else
288 static int input_signal_count;
289 #endif
291 /* Used locally within XTread_socket. */
293 static int x_noop_count;
295 /* Initial values of argv and argc. */
297 extern char **initial_argv;
298 extern int initial_argc;
300 extern Lisp_Object Vcommand_line_args, Vsystem_name;
302 /* Tells if a window manager is present or not. */
304 extern Lisp_Object Vx_no_window_manager;
306 extern Lisp_Object Qeql;
308 extern int errno;
310 /* A mask of extra modifier bits to put into every keyboard char. */
312 extern EMACS_INT extra_keyboard_modifiers;
314 /* The keysyms to use for the various modifiers. */
316 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
317 Lisp_Object Vx_keysym_table;
318 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
320 static Lisp_Object Qvendor_specific_keysyms;
321 static Lisp_Object Qlatin_1;
323 /* Used in x_flush. */
325 extern Lisp_Object Vinhibit_redisplay;
327 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
328 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
330 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
331 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
332 static const XColor *x_color_cells P_ ((Display *, int *));
333 static void x_update_window_end P_ ((struct window *, int, int));
335 static int x_io_error_quitter P_ ((Display *));
336 static struct terminal *x_create_terminal P_ ((struct x_display_info *));
337 void x_delete_terminal P_ ((struct terminal *));
338 static void x_update_end P_ ((struct frame *));
339 static void XTframe_up_to_date P_ ((struct frame *));
340 static void XTset_terminal_modes P_ ((struct terminal *));
341 static void XTreset_terminal_modes P_ ((struct terminal *));
342 static void x_clear_frame P_ ((struct frame *));
343 static void frame_highlight P_ ((struct frame *));
344 static void frame_unhighlight P_ ((struct frame *));
345 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
346 static void x_focus_changed P_ ((int, int, struct x_display_info *,
347 struct frame *, struct input_event *));
348 static void x_detect_focus_change P_ ((struct x_display_info *,
349 XEvent *, struct input_event *));
350 static void XTframe_rehighlight P_ ((struct frame *));
351 static void x_frame_rehighlight P_ ((struct x_display_info *));
352 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
353 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
354 enum text_cursor_kinds));
356 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
357 static void x_flush P_ ((struct frame *f));
358 static void x_update_begin P_ ((struct frame *));
359 static void x_update_window_begin P_ ((struct window *));
360 static void x_after_update_window_line P_ ((struct glyph_row *));
361 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
362 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
363 enum scroll_bar_part *,
364 Lisp_Object *, Lisp_Object *,
365 unsigned long *));
366 static void x_check_fullscreen P_ ((struct frame *));
367 static void x_check_expected_move P_ ((struct frame *, int, int));
368 static void x_sync_with_move P_ ((struct frame *, int, int, int));
369 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
370 int *, struct input_event *));
371 /* Don't declare this NO_RETURN because we want no
372 interference with debugging failing X calls. */
373 static SIGTYPE x_connection_closed P_ ((Display *, char *));
376 /* Flush display of frame F, or of all frames if F is null. */
378 static void
379 x_flush (f)
380 struct frame *f;
382 /* Don't call XFlush when it is not safe to redisplay; the X
383 connection may be broken. */
384 if (!NILP (Vinhibit_redisplay))
385 return;
387 BLOCK_INPUT;
388 if (f == NULL)
390 Lisp_Object rest, frame;
391 FOR_EACH_FRAME (rest, frame)
392 if (FRAME_X_P (XFRAME (frame)))
393 x_flush (XFRAME (frame));
395 else if (FRAME_X_P (f))
396 XFlush (FRAME_X_DISPLAY (f));
397 UNBLOCK_INPUT;
401 /* Remove calls to XFlush by defining XFlush to an empty replacement.
402 Calls to XFlush should be unnecessary because the X output buffer
403 is flushed automatically as needed by calls to XPending,
404 XNextEvent, or XWindowEvent according to the XFlush man page.
405 XTread_socket calls XPending. Removing XFlush improves
406 performance. */
408 #define XFlush(DISPLAY) (void) 0
411 /***********************************************************************
412 Debugging
413 ***********************************************************************/
415 #if 0
417 /* This is a function useful for recording debugging information about
418 the sequence of occurrences in this file. */
420 struct record
422 char *locus;
423 int type;
426 struct record event_record[100];
428 int event_record_index;
430 record_event (locus, type)
431 char *locus;
432 int type;
434 if (event_record_index == sizeof (event_record) / sizeof (struct record))
435 event_record_index = 0;
437 event_record[event_record_index].locus = locus;
438 event_record[event_record_index].type = type;
439 event_record_index++;
442 #endif /* 0 */
446 /* Return the struct x_display_info corresponding to DPY. */
448 struct x_display_info *
449 x_display_info_for_display (dpy)
450 Display *dpy;
452 struct x_display_info *dpyinfo;
454 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
455 if (dpyinfo->display == dpy)
456 return dpyinfo;
458 return 0;
461 #define OPAQUE 0xffffffff
462 #define OPACITY "_NET_WM_WINDOW_OPACITY"
464 void
465 x_set_frame_alpha (f)
466 struct frame *f;
468 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
469 Display *dpy = FRAME_X_DISPLAY (f);
470 Window win = FRAME_OUTER_WINDOW (f);
471 double alpha = 1.0;
472 double alpha_min = 1.0;
473 unsigned long opac;
475 if (FRAME_X_DISPLAY_INFO (f)->root_window != FRAME_X_OUTPUT (f)->parent_desc)
476 /* Since the WM decoration lies under the FRAME_OUTER_WINDOW,
477 we must treat the former instead of the latter. */
478 win = FRAME_X_OUTPUT(f)->parent_desc;
480 if (dpyinfo->x_highlight_frame == f)
481 alpha = f->alpha[0];
482 else
483 alpha = f->alpha[1];
485 if (FLOATP (Vframe_alpha_lower_limit))
486 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
487 else if (INTEGERP (Vframe_alpha_lower_limit))
488 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
490 if (alpha < 0.0)
491 return;
492 else if (alpha > 1.0)
493 alpha = 1.0;
494 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
495 alpha = alpha_min;
497 opac = alpha * OPAQUE;
499 /* return unless necessary */
501 unsigned char *data;
502 Atom actual;
503 int rc, format;
504 unsigned long n, left;
506 x_catch_errors (dpy);
507 rc = XGetWindowProperty(dpy, win, XInternAtom(dpy, OPACITY, False),
508 0L, 1L, False, XA_CARDINAL,
509 &actual, &format, &n, &left,
510 &data);
512 if (rc == Success && actual != None)
513 if (*(unsigned long *)data == opac)
515 XFree ((void *) data);
516 x_uncatch_errors ();
517 return;
519 else
520 XFree ((void *) data);
521 x_uncatch_errors ();
524 x_catch_errors (dpy);
525 XChangeProperty (dpy, win, XInternAtom (dpy, OPACITY, False),
526 XA_CARDINAL, 32, PropModeReplace,
527 (unsigned char *) &opac, 1L);
528 x_uncatch_errors ();
532 x_display_pixel_height (dpyinfo)
533 struct x_display_info *dpyinfo;
535 return HeightOfScreen (dpyinfo->screen);
539 x_display_pixel_width (dpyinfo)
540 struct x_display_info *dpyinfo;
542 return WidthOfScreen (dpyinfo->screen);
546 /***********************************************************************
547 Starting and ending an update
548 ***********************************************************************/
550 /* Start an update of frame F. This function is installed as a hook
551 for update_begin, i.e. it is called when update_begin is called.
552 This function is called prior to calls to x_update_window_begin for
553 each window being updated. Currently, there is nothing to do here
554 because all interesting stuff is done on a window basis. */
556 static void
557 x_update_begin (f)
558 struct frame *f;
560 /* Nothing to do. */
564 /* Start update of window W. Set the global variable updated_window
565 to the window being updated and set output_cursor to the cursor
566 position of W. */
568 static void
569 x_update_window_begin (w)
570 struct window *w;
572 struct frame *f = XFRAME (WINDOW_FRAME (w));
573 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
575 updated_window = w;
576 set_output_cursor (&w->cursor);
578 BLOCK_INPUT;
580 if (f == display_info->mouse_face_mouse_frame)
582 /* Don't do highlighting for mouse motion during the update. */
583 display_info->mouse_face_defer = 1;
585 /* If F needs to be redrawn, simply forget about any prior mouse
586 highlighting. */
587 if (FRAME_GARBAGED_P (f))
588 display_info->mouse_face_window = Qnil;
590 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
591 their mouse_face_p flag set, which means that they are always
592 unequal to rows in a desired matrix which never have that
593 flag set. So, rows containing mouse-face glyphs are never
594 scrolled, and we don't have to switch the mouse highlight off
595 here to prevent it from being scrolled. */
597 /* Can we tell that this update does not affect the window
598 where the mouse highlight is? If so, no need to turn off.
599 Likewise, don't do anything if the frame is garbaged;
600 in that case, the frame's current matrix that we would use
601 is all wrong, and we will redisplay that line anyway. */
602 if (!NILP (display_info->mouse_face_window)
603 && w == XWINDOW (display_info->mouse_face_window))
605 int i;
607 for (i = 0; i < w->desired_matrix->nrows; ++i)
608 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
609 break;
611 if (i < w->desired_matrix->nrows)
612 clear_mouse_face (display_info);
614 #endif /* 0 */
617 UNBLOCK_INPUT;
621 /* Draw a vertical window border from (x,y0) to (x,y1) */
623 static void
624 x_draw_vertical_window_border (w, x, y0, y1)
625 struct window *w;
626 int x, y0, y1;
628 struct frame *f = XFRAME (WINDOW_FRAME (w));
629 struct face *face;
631 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
632 if (face)
633 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
634 face->foreground);
636 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
637 f->output_data.x->normal_gc, x, y0, x, y1);
640 /* End update of window W (which is equal to updated_window).
642 Draw vertical borders between horizontally adjacent windows, and
643 display W's cursor if CURSOR_ON_P is non-zero.
645 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
646 glyphs in mouse-face were overwritten. In that case we have to
647 make sure that the mouse-highlight is properly redrawn.
649 W may be a menu bar pseudo-window in case we don't have X toolkit
650 support. Such windows don't have a cursor, so don't display it
651 here. */
653 static void
654 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
655 struct window *w;
656 int cursor_on_p, mouse_face_overwritten_p;
658 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
660 if (!w->pseudo_window_p)
662 BLOCK_INPUT;
664 if (cursor_on_p)
665 display_and_set_cursor (w, 1, output_cursor.hpos,
666 output_cursor.vpos,
667 output_cursor.x, output_cursor.y);
669 if (draw_window_fringes (w, 1))
670 x_draw_vertical_border (w);
672 UNBLOCK_INPUT;
675 /* If a row with mouse-face was overwritten, arrange for
676 XTframe_up_to_date to redisplay the mouse highlight. */
677 if (mouse_face_overwritten_p)
679 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
680 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
681 dpyinfo->mouse_face_window = Qnil;
684 updated_window = NULL;
688 /* End update of frame F. This function is installed as a hook in
689 update_end. */
691 static void
692 x_update_end (f)
693 struct frame *f;
695 /* Mouse highlight may be displayed again. */
696 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
698 #ifndef XFlush
699 BLOCK_INPUT;
700 XFlush (FRAME_X_DISPLAY (f));
701 UNBLOCK_INPUT;
702 #endif
706 /* This function is called from various places in xdisp.c whenever a
707 complete update has been performed. The global variable
708 updated_window is not available here. */
710 static void
711 XTframe_up_to_date (f)
712 struct frame *f;
714 if (FRAME_X_P (f))
716 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
718 if (dpyinfo->mouse_face_deferred_gc
719 || f == dpyinfo->mouse_face_mouse_frame)
721 BLOCK_INPUT;
722 if (dpyinfo->mouse_face_mouse_frame)
723 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
724 dpyinfo->mouse_face_mouse_x,
725 dpyinfo->mouse_face_mouse_y);
726 dpyinfo->mouse_face_deferred_gc = 0;
727 UNBLOCK_INPUT;
733 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
734 arrow bitmaps, or clear the fringes if no bitmaps are required
735 before DESIRED_ROW is made current. The window being updated is
736 found in updated_window. This function It is called from
737 update_window_line only if it is known that there are differences
738 between bitmaps to be drawn between current row and DESIRED_ROW. */
740 static void
741 x_after_update_window_line (desired_row)
742 struct glyph_row *desired_row;
744 struct window *w = updated_window;
745 struct frame *f;
746 int width, height;
748 xassert (w);
750 if (!desired_row->mode_line_p && !w->pseudo_window_p)
751 desired_row->redraw_fringe_bitmaps_p = 1;
753 /* When a window has disappeared, make sure that no rest of
754 full-width rows stays visible in the internal border. Could
755 check here if updated_window is the leftmost/rightmost window,
756 but I guess it's not worth doing since vertically split windows
757 are almost never used, internal border is rarely set, and the
758 overhead is very small. */
759 if (windows_or_buffers_changed
760 && desired_row->full_width_p
761 && (f = XFRAME (w->frame),
762 width = FRAME_INTERNAL_BORDER_WIDTH (f),
763 width != 0)
764 && (height = desired_row->visible_height,
765 height > 0))
767 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
769 /* Internal border is drawn below the tool bar. */
770 if (WINDOWP (f->tool_bar_window)
771 && w == XWINDOW (f->tool_bar_window))
772 y -= width;
774 BLOCK_INPUT;
775 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
776 0, y, width, height, False);
777 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
778 FRAME_PIXEL_WIDTH (f) - width,
779 y, width, height, False);
780 UNBLOCK_INPUT;
784 static void
785 x_draw_fringe_bitmap (w, row, p)
786 struct window *w;
787 struct glyph_row *row;
788 struct draw_fringe_bitmap_params *p;
790 struct frame *f = XFRAME (WINDOW_FRAME (w));
791 Display *display = FRAME_X_DISPLAY (f);
792 Window window = FRAME_X_WINDOW (f);
793 GC gc = f->output_data.x->normal_gc;
794 struct face *face = p->face;
795 int rowY;
797 /* Must clip because of partially visible lines. */
798 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
799 if (p->y < rowY)
801 /* Adjust position of "bottom aligned" bitmap on partially
802 visible last row. */
803 int oldY = row->y;
804 int oldVH = row->visible_height;
805 row->visible_height = p->h;
806 row->y -= rowY - p->y;
807 x_clip_to_row (w, row, -1, gc);
808 row->y = oldY;
809 row->visible_height = oldVH;
811 else
812 x_clip_to_row (w, row, -1, gc);
814 if (!p->overlay_p)
816 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
818 /* In case the same realized face is used for fringes and
819 for something displayed in the text (e.g. face `region' on
820 mono-displays, the fill style may have been changed to
821 FillSolid in x_draw_glyph_string_background. */
822 if (face->stipple)
823 XSetFillStyle (display, face->gc, FillOpaqueStippled);
824 else
825 XSetForeground (display, face->gc, face->background);
827 #ifdef USE_TOOLKIT_SCROLL_BARS
828 /* If the fringe is adjacent to the left (right) scroll bar of a
829 leftmost (rightmost, respectively) window, then extend its
830 background to the gap between the fringe and the bar. */
831 if ((WINDOW_LEFTMOST_P (w)
832 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
833 || (WINDOW_RIGHTMOST_P (w)
834 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
836 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
838 if (sb_width > 0)
840 int left = WINDOW_SCROLL_BAR_AREA_X (w);
841 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
842 * FRAME_COLUMN_WIDTH (f));
844 if (bx < 0)
846 /* Bitmap fills the fringe. */
847 if (left + width == p->x)
848 bx = left + sb_width;
849 else if (p->x + p->wd == left)
850 bx = left;
851 if (bx >= 0)
853 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
855 nx = width - sb_width;
856 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
857 row->y));
858 ny = row->visible_height;
861 else
863 if (left + width == bx)
865 bx = left + sb_width;
866 nx += width - sb_width;
868 else if (bx + nx == left)
869 nx += width - sb_width;
873 #endif
874 if (bx >= 0 && nx > 0)
875 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
877 if (!face->stipple)
878 XSetForeground (display, face->gc, face->foreground);
881 if (p->which)
883 unsigned char *bits;
884 Pixmap pixmap, clipmask = (Pixmap) 0;
885 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
886 XGCValues gcv;
888 if (p->wd > 8)
889 bits = (unsigned char *)(p->bits + p->dh);
890 else
891 bits = (unsigned char *)p->bits + p->dh;
893 /* Draw the bitmap. I believe these small pixmaps can be cached
894 by the server. */
895 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
896 (p->cursor_p
897 ? (p->overlay_p ? face->background
898 : f->output_data.x->cursor_pixel)
899 : face->foreground),
900 face->background, depth);
902 if (p->overlay_p)
904 clipmask = XCreatePixmapFromBitmapData (display,
905 FRAME_X_DISPLAY_INFO (f)->root_window,
906 bits, p->wd, p->h,
907 1, 0, 1);
908 gcv.clip_mask = clipmask;
909 gcv.clip_x_origin = p->x;
910 gcv.clip_y_origin = p->y;
911 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
914 XCopyArea (display, pixmap, window, gc, 0, 0,
915 p->wd, p->h, p->x, p->y);
916 XFreePixmap (display, pixmap);
918 if (p->overlay_p)
920 gcv.clip_mask = (Pixmap) 0;
921 XChangeGC (display, gc, GCClipMask, &gcv);
922 XFreePixmap (display, clipmask);
926 XSetClipMask (display, gc, None);
931 /* This is called when starting Emacs and when restarting after
932 suspend. When starting Emacs, no X window is mapped. And nothing
933 must be done to Emacs's own window if it is suspended (though that
934 rarely happens). */
936 static void
937 XTset_terminal_modes (struct terminal *terminal)
941 /* This is called when exiting or suspending Emacs. Exiting will make
942 the X-windows go away, and suspending requires no action. */
944 static void
945 XTreset_terminal_modes (struct terminal *terminal)
950 /***********************************************************************
951 Glyph display
952 ***********************************************************************/
956 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
957 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
958 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
959 int));
960 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
961 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
962 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
963 static void x_draw_glyph_string P_ ((struct glyph_string *));
964 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
965 static void x_set_cursor_gc P_ ((struct glyph_string *));
966 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
967 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
968 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
969 unsigned long *, double, int));
970 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
971 double, int, unsigned long));
972 static void x_setup_relief_colors P_ ((struct glyph_string *));
973 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
974 static void x_draw_image_relief P_ ((struct glyph_string *));
975 static void x_draw_image_foreground P_ ((struct glyph_string *));
976 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
977 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
978 int, int, int));
979 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
980 int, int, int, int, int, int,
981 XRectangle *));
982 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
983 int, int, int, XRectangle *));
985 #if GLYPH_DEBUG
986 static void x_check_font P_ ((struct frame *, struct font *));
987 #endif
990 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
991 face. */
993 static void
994 x_set_cursor_gc (s)
995 struct glyph_string *s;
997 if (s->font == FRAME_FONT (s->f)
998 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
999 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1000 && !s->cmp)
1001 s->gc = s->f->output_data.x->cursor_gc;
1002 else
1004 /* Cursor on non-default face: must merge. */
1005 XGCValues xgcv;
1006 unsigned long mask;
1008 xgcv.background = s->f->output_data.x->cursor_pixel;
1009 xgcv.foreground = s->face->background;
1011 /* If the glyph would be invisible, try a different foreground. */
1012 if (xgcv.foreground == xgcv.background)
1013 xgcv.foreground = s->face->foreground;
1014 if (xgcv.foreground == xgcv.background)
1015 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1016 if (xgcv.foreground == xgcv.background)
1017 xgcv.foreground = s->face->foreground;
1019 /* Make sure the cursor is distinct from text in this face. */
1020 if (xgcv.background == s->face->background
1021 && xgcv.foreground == s->face->foreground)
1023 xgcv.background = s->face->foreground;
1024 xgcv.foreground = s->face->background;
1027 IF_DEBUG (x_check_font (s->f, s->font));
1028 xgcv.graphics_exposures = False;
1029 mask = GCForeground | GCBackground | GCGraphicsExposures;
1031 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1032 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1033 mask, &xgcv);
1034 else
1035 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1036 = XCreateGC (s->display, s->window, mask, &xgcv);
1038 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1043 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1045 static void
1046 x_set_mouse_face_gc (s)
1047 struct glyph_string *s;
1049 int face_id;
1050 struct face *face;
1052 /* What face has to be used last for the mouse face? */
1053 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1054 face = FACE_FROM_ID (s->f, face_id);
1055 if (face == NULL)
1056 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1058 if (s->first_glyph->type == CHAR_GLYPH)
1059 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1060 else
1061 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1062 s->face = FACE_FROM_ID (s->f, face_id);
1063 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1065 if (s->font == s->face->font)
1066 s->gc = s->face->gc;
1067 else
1069 /* Otherwise construct scratch_cursor_gc with values from FACE
1070 except for FONT. */
1071 XGCValues xgcv;
1072 unsigned long mask;
1074 xgcv.background = s->face->background;
1075 xgcv.foreground = s->face->foreground;
1076 xgcv.graphics_exposures = False;
1077 mask = GCForeground | GCBackground | GCGraphicsExposures;
1079 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1080 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1081 mask, &xgcv);
1082 else
1083 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1084 = XCreateGC (s->display, s->window, mask, &xgcv);
1086 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1089 xassert (s->gc != 0);
1093 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1094 Faces to use in the mode line have already been computed when the
1095 matrix was built, so there isn't much to do, here. */
1097 static INLINE void
1098 x_set_mode_line_face_gc (s)
1099 struct glyph_string *s;
1101 s->gc = s->face->gc;
1105 /* Set S->gc of glyph string S for drawing that glyph string. Set
1106 S->stippled_p to a non-zero value if the face of S has a stipple
1107 pattern. */
1109 static INLINE void
1110 x_set_glyph_string_gc (s)
1111 struct glyph_string *s;
1113 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1115 if (s->hl == DRAW_NORMAL_TEXT)
1117 s->gc = s->face->gc;
1118 s->stippled_p = s->face->stipple != 0;
1120 else if (s->hl == DRAW_INVERSE_VIDEO)
1122 x_set_mode_line_face_gc (s);
1123 s->stippled_p = s->face->stipple != 0;
1125 else if (s->hl == DRAW_CURSOR)
1127 x_set_cursor_gc (s);
1128 s->stippled_p = 0;
1130 else if (s->hl == DRAW_MOUSE_FACE)
1132 x_set_mouse_face_gc (s);
1133 s->stippled_p = s->face->stipple != 0;
1135 else if (s->hl == DRAW_IMAGE_RAISED
1136 || s->hl == DRAW_IMAGE_SUNKEN)
1138 s->gc = s->face->gc;
1139 s->stippled_p = s->face->stipple != 0;
1141 else
1143 s->gc = s->face->gc;
1144 s->stippled_p = s->face->stipple != 0;
1147 /* GC must have been set. */
1148 xassert (s->gc != 0);
1152 /* Set clipping for output of glyph string S. S may be part of a mode
1153 line or menu if we don't have X toolkit support. */
1155 static INLINE void
1156 x_set_glyph_string_clipping (s)
1157 struct glyph_string *s;
1159 XRectangle *r = s->clip;
1160 int n = get_glyph_string_clip_rects (s, r, 2);
1162 if (n > 0)
1163 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
1164 s->num_clips = n;
1168 /* Set SRC's clipping for output of glyph string DST. This is called
1169 when we are drawing DST's left_overhang or right_overhang only in
1170 the area of SRC. */
1172 static void
1173 x_set_glyph_string_clipping_exactly (src, dst)
1174 struct glyph_string *src, *dst;
1176 XRectangle r;
1178 r.x = src->x;
1179 r.width = src->width;
1180 r.y = src->y;
1181 r.height = src->height;
1182 dst->clip[0] = r;
1183 dst->num_clips = 1;
1184 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1188 /* RIF:
1189 Compute left and right overhang of glyph string S. */
1191 static void
1192 x_compute_glyph_string_overhangs (s)
1193 struct glyph_string *s;
1195 if (s->cmp == NULL
1196 && s->first_glyph->type == CHAR_GLYPH)
1198 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1199 struct font *font = s->font;
1200 struct font_metrics metrics;
1201 int i;
1203 for (i = 0; i < s->nchars; i++)
1204 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1205 font->driver->text_extents (font, code, s->nchars, &metrics);
1206 s->right_overhang = (metrics.rbearing > metrics.width
1207 ? metrics.rbearing - metrics.width : 0);
1208 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1210 else if (s->cmp)
1212 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1213 s->left_overhang = - s->cmp->lbearing;
1218 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1220 static INLINE void
1221 x_clear_glyph_string_rect (s, x, y, w, h)
1222 struct glyph_string *s;
1223 int x, y, w, h;
1225 XGCValues xgcv;
1226 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1227 XSetForeground (s->display, s->gc, xgcv.background);
1228 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1229 XSetForeground (s->display, s->gc, xgcv.foreground);
1233 /* Draw the background of glyph_string S. If S->background_filled_p
1234 is non-zero don't draw it. FORCE_P non-zero means draw the
1235 background even if it wouldn't be drawn normally. This is used
1236 when a string preceding S draws into the background of S, or S
1237 contains the first component of a composition. */
1239 static void
1240 x_draw_glyph_string_background (s, force_p)
1241 struct glyph_string *s;
1242 int force_p;
1244 /* Nothing to do if background has already been drawn or if it
1245 shouldn't be drawn in the first place. */
1246 if (!s->background_filled_p)
1248 int box_line_width = max (s->face->box_line_width, 0);
1250 if (s->stippled_p)
1252 /* Fill background with a stipple pattern. */
1253 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1254 XFillRectangle (s->display, s->window, s->gc, s->x,
1255 s->y + box_line_width,
1256 s->background_width,
1257 s->height - 2 * box_line_width);
1258 XSetFillStyle (s->display, s->gc, FillSolid);
1259 s->background_filled_p = 1;
1261 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1262 || s->font_not_found_p
1263 || s->extends_to_end_of_line_p
1264 || force_p)
1266 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1267 s->background_width,
1268 s->height - 2 * box_line_width);
1269 s->background_filled_p = 1;
1275 /* Draw the foreground of glyph string S. */
1277 static void
1278 x_draw_glyph_string_foreground (s)
1279 struct glyph_string *s;
1281 int i, x;
1283 /* If first glyph of S has a left box line, start drawing the text
1284 of S to the right of that box line. */
1285 if (s->face->box != FACE_NO_BOX
1286 && s->first_glyph->left_box_line_p)
1287 x = s->x + eabs (s->face->box_line_width);
1288 else
1289 x = s->x;
1291 /* Draw characters of S as rectangles if S's font could not be
1292 loaded. */
1293 if (s->font_not_found_p)
1295 for (i = 0; i < s->nchars; ++i)
1297 struct glyph *g = s->first_glyph + i;
1298 XDrawRectangle (s->display, s->window,
1299 s->gc, x, s->y, g->pixel_width - 1,
1300 s->height - 1);
1301 x += g->pixel_width;
1304 else
1306 struct font *font = s->font;
1307 int boff = font->baseline_offset;
1308 int y;
1310 if (font->vertical_centering)
1311 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1313 y = s->ybase - boff;
1314 if (s->for_overlaps
1315 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1316 font->driver->draw (s, 0, s->nchars, x, y, 0);
1317 else
1318 font->driver->draw (s, 0, s->nchars, x, y, 1);
1319 if (s->face->overstrike)
1320 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1324 /* Draw the foreground of composite glyph string S. */
1326 static void
1327 x_draw_composite_glyph_string_foreground (s)
1328 struct glyph_string *s;
1330 int i, j, x;
1331 struct font *font = s->font;
1333 /* If first glyph of S has a left box line, start drawing the text
1334 of S to the right of that box line. */
1335 if (s->face && s->face->box != FACE_NO_BOX
1336 && s->first_glyph->left_box_line_p)
1337 x = s->x + eabs (s->face->box_line_width);
1338 else
1339 x = s->x;
1341 /* S is a glyph string for a composition. S->cmp_from is the index
1342 of the first character drawn for glyphs of this composition.
1343 S->cmp_from == 0 means we are drawing the very first character of
1344 this composition. */
1346 /* Draw a rectangle for the composition if the font for the very
1347 first character of the composition could not be loaded. */
1348 if (s->font_not_found_p)
1350 if (s->cmp_from == 0)
1351 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1352 s->width - 1, s->height - 1);
1354 else if (! s->first_glyph->u.cmp.automatic)
1356 int y = s->ybase;
1358 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1359 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1361 int xx = x + s->cmp->offsets[j * 2];
1362 int yy = y - s->cmp->offsets[j * 2 + 1];
1364 font->driver->draw (s, j, j + 1, xx, yy, 0);
1365 if (s->face->overstrike)
1366 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1369 else
1371 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1372 Lisp_Object glyph;
1373 int y = s->ybase;
1374 int width = 0;
1376 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1378 glyph = LGSTRING_GLYPH (gstring, i);
1379 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1380 width += LGLYPH_WIDTH (glyph);
1381 else
1383 int xoff, yoff, wadjust;
1385 if (j < i)
1387 font->driver->draw (s, j, i, x, y, 0);
1388 x += width;
1390 xoff = LGLYPH_XOFF (glyph);
1391 yoff = LGLYPH_YOFF (glyph);
1392 wadjust = LGLYPH_WADJUST (glyph);
1393 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1394 x += wadjust;
1395 j = i + 1;
1396 width = 0;
1399 if (j < i)
1400 font->driver->draw (s, j, i, x, y, 0);
1405 #ifdef USE_X_TOOLKIT
1407 static struct frame *x_frame_of_widget P_ ((Widget));
1408 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1409 XrmValue *, XrmValue *, XtPointer *));
1410 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1411 XrmValue *, Cardinal *));
1414 /* Return the frame on which widget WIDGET is used.. Abort if frame
1415 cannot be determined. */
1417 static struct frame *
1418 x_frame_of_widget (widget)
1419 Widget widget;
1421 struct x_display_info *dpyinfo;
1422 Lisp_Object tail;
1423 struct frame *f;
1425 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1427 /* Find the top-level shell of the widget. Note that this function
1428 can be called when the widget is not yet realized, so XtWindow
1429 (widget) == 0. That's the reason we can't simply use
1430 x_any_window_to_frame. */
1431 while (!XtIsTopLevelShell (widget))
1432 widget = XtParent (widget);
1434 /* Look for a frame with that top-level widget. Allocate the color
1435 on that frame to get the right gamma correction value. */
1436 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1437 if (FRAMEP (XCAR (tail))
1438 && (f = XFRAME (XCAR (tail)),
1439 (FRAME_X_P (f)
1440 && f->output_data.nothing != 1
1441 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1442 && f->output_data.x->widget == widget)
1443 return f;
1445 abort ();
1449 /* Allocate the color COLOR->pixel on the screen and display of
1450 widget WIDGET in colormap CMAP. If an exact match cannot be
1451 allocated, try the nearest color available. Value is non-zero
1452 if successful. This is called from lwlib. */
1455 x_alloc_nearest_color_for_widget (widget, cmap, color)
1456 Widget widget;
1457 Colormap cmap;
1458 XColor *color;
1460 struct frame *f = x_frame_of_widget (widget);
1461 return x_alloc_nearest_color (f, cmap, color);
1465 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1466 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1467 If this produces the same color as PIXEL, try a color where all RGB
1468 values have DELTA added. Return the allocated color in *PIXEL.
1469 DISPLAY is the X display, CMAP is the colormap to operate on.
1470 Value is non-zero if successful. */
1473 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1474 Widget widget;
1475 Display *display;
1476 Colormap cmap;
1477 unsigned long *pixel;
1478 double factor;
1479 int delta;
1481 struct frame *f = x_frame_of_widget (widget);
1482 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1486 /* Structure specifying which arguments should be passed by Xt to
1487 cvt_string_to_pixel. We want the widget's screen and colormap. */
1489 static XtConvertArgRec cvt_string_to_pixel_args[] =
1491 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1492 sizeof (Screen *)},
1493 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1494 sizeof (Colormap)}
1498 /* The address of this variable is returned by
1499 cvt_string_to_pixel. */
1501 static Pixel cvt_string_to_pixel_value;
1504 /* Convert a color name to a pixel color.
1506 DPY is the display we are working on.
1508 ARGS is an array of *NARGS XrmValue structures holding additional
1509 information about the widget for which the conversion takes place.
1510 The contents of this array are determined by the specification
1511 in cvt_string_to_pixel_args.
1513 FROM is a pointer to an XrmValue which points to the color name to
1514 convert. TO is an XrmValue in which to return the pixel color.
1516 CLOSURE_RET is a pointer to user-data, in which we record if
1517 we allocated the color or not.
1519 Value is True if successful, False otherwise. */
1521 static Boolean
1522 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1523 Display *dpy;
1524 XrmValue *args;
1525 Cardinal *nargs;
1526 XrmValue *from, *to;
1527 XtPointer *closure_ret;
1529 Screen *screen;
1530 Colormap cmap;
1531 Pixel pixel;
1532 String color_name;
1533 XColor color;
1535 if (*nargs != 2)
1537 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1538 "wrongParameters", "cvt_string_to_pixel",
1539 "XtToolkitError",
1540 "Screen and colormap args required", NULL, NULL);
1541 return False;
1544 screen = *(Screen **) args[0].addr;
1545 cmap = *(Colormap *) args[1].addr;
1546 color_name = (String) from->addr;
1548 if (strcmp (color_name, XtDefaultBackground) == 0)
1550 *closure_ret = (XtPointer) False;
1551 pixel = WhitePixelOfScreen (screen);
1553 else if (strcmp (color_name, XtDefaultForeground) == 0)
1555 *closure_ret = (XtPointer) False;
1556 pixel = BlackPixelOfScreen (screen);
1558 else if (XParseColor (dpy, cmap, color_name, &color)
1559 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1561 pixel = color.pixel;
1562 *closure_ret = (XtPointer) True;
1564 else
1566 String params[1];
1567 Cardinal nparams = 1;
1569 params[0] = color_name;
1570 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1571 "badValue", "cvt_string_to_pixel",
1572 "XtToolkitError", "Invalid color `%s'",
1573 params, &nparams);
1574 return False;
1577 if (to->addr != NULL)
1579 if (to->size < sizeof (Pixel))
1581 to->size = sizeof (Pixel);
1582 return False;
1585 *(Pixel *) to->addr = pixel;
1587 else
1589 cvt_string_to_pixel_value = pixel;
1590 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1593 to->size = sizeof (Pixel);
1594 return True;
1598 /* Free a pixel color which was previously allocated via
1599 cvt_string_to_pixel. This is registered as the destructor
1600 for this type of resource via XtSetTypeConverter.
1602 APP is the application context in which we work.
1604 TO is a pointer to an XrmValue holding the color to free.
1605 CLOSURE is the value we stored in CLOSURE_RET for this color
1606 in cvt_string_to_pixel.
1608 ARGS and NARGS are like for cvt_string_to_pixel. */
1610 static void
1611 cvt_pixel_dtor (app, to, closure, args, nargs)
1612 XtAppContext app;
1613 XrmValuePtr to;
1614 XtPointer closure;
1615 XrmValuePtr args;
1616 Cardinal *nargs;
1618 if (*nargs != 2)
1620 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1621 "XtToolkitError",
1622 "Screen and colormap arguments required",
1623 NULL, NULL);
1625 else if (closure != NULL)
1627 /* We did allocate the pixel, so free it. */
1628 Screen *screen = *(Screen **) args[0].addr;
1629 Colormap cmap = *(Colormap *) args[1].addr;
1630 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1631 (Pixel *) to->addr, 1);
1636 #endif /* USE_X_TOOLKIT */
1639 /* Value is an array of XColor structures for the contents of the
1640 color map of display DPY. Set *NCELLS to the size of the array.
1641 Note that this probably shouldn't be called for large color maps,
1642 say a 24-bit TrueColor map. */
1644 static const XColor *
1645 x_color_cells (dpy, ncells)
1646 Display *dpy;
1647 int *ncells;
1649 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1651 if (dpyinfo->color_cells == NULL)
1653 Screen *screen = dpyinfo->screen;
1654 int i;
1656 dpyinfo->ncolor_cells
1657 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1658 dpyinfo->color_cells
1659 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1660 * sizeof *dpyinfo->color_cells);
1662 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1663 dpyinfo->color_cells[i].pixel = i;
1665 XQueryColors (dpy, dpyinfo->cmap,
1666 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1669 *ncells = dpyinfo->ncolor_cells;
1670 return dpyinfo->color_cells;
1674 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1675 colors in COLORS. Use cached information, if available. */
1677 void
1678 x_query_colors (f, colors, ncolors)
1679 struct frame *f;
1680 XColor *colors;
1681 int ncolors;
1683 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1685 if (dpyinfo->color_cells)
1687 int i;
1688 for (i = 0; i < ncolors; ++i)
1690 unsigned long pixel = colors[i].pixel;
1691 xassert (pixel < dpyinfo->ncolor_cells);
1692 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1693 colors[i] = dpyinfo->color_cells[pixel];
1696 else
1697 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1701 /* On frame F, translate pixel color to RGB values for the color in
1702 COLOR. Use cached information, if available. */
1704 void
1705 x_query_color (f, color)
1706 struct frame *f;
1707 XColor *color;
1709 x_query_colors (f, color, 1);
1713 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1714 exact match can't be allocated, try the nearest color available.
1715 Value is non-zero if successful. Set *COLOR to the color
1716 allocated. */
1718 static int
1719 x_alloc_nearest_color_1 (dpy, cmap, color)
1720 Display *dpy;
1721 Colormap cmap;
1722 XColor *color;
1724 int rc;
1726 rc = XAllocColor (dpy, cmap, color);
1727 if (rc == 0)
1729 /* If we got to this point, the colormap is full, so we're going
1730 to try to get the next closest color. The algorithm used is
1731 a least-squares matching, which is what X uses for closest
1732 color matching with StaticColor visuals. */
1733 int nearest, i;
1734 unsigned long nearest_delta = ~0;
1735 int ncells;
1736 const XColor *cells = x_color_cells (dpy, &ncells);
1738 for (nearest = i = 0; i < ncells; ++i)
1740 long dred = (color->red >> 8) - (cells[i].red >> 8);
1741 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1742 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1743 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1745 if (delta < nearest_delta)
1747 nearest = i;
1748 nearest_delta = delta;
1752 color->red = cells[nearest].red;
1753 color->green = cells[nearest].green;
1754 color->blue = cells[nearest].blue;
1755 rc = XAllocColor (dpy, cmap, color);
1757 else
1759 /* If allocation succeeded, and the allocated pixel color is not
1760 equal to a cached pixel color recorded earlier, there was a
1761 change in the colormap, so clear the color cache. */
1762 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1763 XColor *cached_color;
1765 if (dpyinfo->color_cells
1766 && (cached_color = &dpyinfo->color_cells[color->pixel],
1767 (cached_color->red != color->red
1768 || cached_color->blue != color->blue
1769 || cached_color->green != color->green)))
1771 xfree (dpyinfo->color_cells);
1772 dpyinfo->color_cells = NULL;
1773 dpyinfo->ncolor_cells = 0;
1777 #ifdef DEBUG_X_COLORS
1778 if (rc)
1779 register_color (color->pixel);
1780 #endif /* DEBUG_X_COLORS */
1782 return rc;
1786 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1787 exact match can't be allocated, try the nearest color available.
1788 Value is non-zero if successful. Set *COLOR to the color
1789 allocated. */
1792 x_alloc_nearest_color (f, cmap, color)
1793 struct frame *f;
1794 Colormap cmap;
1795 XColor *color;
1797 gamma_correct (f, color);
1798 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1802 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1803 It's necessary to do this instead of just using PIXEL directly to
1804 get color reference counts right. */
1806 unsigned long
1807 x_copy_color (f, pixel)
1808 struct frame *f;
1809 unsigned long pixel;
1811 XColor color;
1813 color.pixel = pixel;
1814 BLOCK_INPUT;
1815 x_query_color (f, &color);
1816 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1817 UNBLOCK_INPUT;
1818 #ifdef DEBUG_X_COLORS
1819 register_color (pixel);
1820 #endif
1821 return color.pixel;
1825 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1826 It's necessary to do this instead of just using PIXEL directly to
1827 get color reference counts right. */
1829 unsigned long
1830 x_copy_dpy_color (dpy, cmap, pixel)
1831 Display *dpy;
1832 Colormap cmap;
1833 unsigned long pixel;
1835 XColor color;
1837 color.pixel = pixel;
1838 BLOCK_INPUT;
1839 XQueryColor (dpy, cmap, &color);
1840 XAllocColor (dpy, cmap, &color);
1841 UNBLOCK_INPUT;
1842 #ifdef DEBUG_X_COLORS
1843 register_color (pixel);
1844 #endif
1845 return color.pixel;
1849 /* Brightness beyond which a color won't have its highlight brightness
1850 boosted.
1852 Nominally, highlight colors for `3d' faces are calculated by
1853 brightening an object's color by a constant scale factor, but this
1854 doesn't yield good results for dark colors, so for colors who's
1855 brightness is less than this value (on a scale of 0-65535) have an
1856 use an additional additive factor.
1858 The value here is set so that the default menu-bar/mode-line color
1859 (grey75) will not have its highlights changed at all. */
1860 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1863 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1864 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1865 If this produces the same color as PIXEL, try a color where all RGB
1866 values have DELTA added. Return the allocated color in *PIXEL.
1867 DISPLAY is the X display, CMAP is the colormap to operate on.
1868 Value is non-zero if successful. */
1870 static int
1871 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1872 struct frame *f;
1873 Display *display;
1874 Colormap cmap;
1875 unsigned long *pixel;
1876 double factor;
1877 int delta;
1879 XColor color, new;
1880 long bright;
1881 int success_p;
1883 /* Get RGB color values. */
1884 color.pixel = *pixel;
1885 x_query_color (f, &color);
1887 /* Change RGB values by specified FACTOR. Avoid overflow! */
1888 xassert (factor >= 0);
1889 new.red = min (0xffff, factor * color.red);
1890 new.green = min (0xffff, factor * color.green);
1891 new.blue = min (0xffff, factor * color.blue);
1893 /* Calculate brightness of COLOR. */
1894 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1896 /* We only boost colors that are darker than
1897 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1898 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1899 /* Make an additive adjustment to NEW, because it's dark enough so
1900 that scaling by FACTOR alone isn't enough. */
1902 /* How far below the limit this color is (0 - 1, 1 being darker). */
1903 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1904 /* The additive adjustment. */
1905 int min_delta = delta * dimness * factor / 2;
1907 if (factor < 1)
1909 new.red = max (0, new.red - min_delta);
1910 new.green = max (0, new.green - min_delta);
1911 new.blue = max (0, new.blue - min_delta);
1913 else
1915 new.red = min (0xffff, min_delta + new.red);
1916 new.green = min (0xffff, min_delta + new.green);
1917 new.blue = min (0xffff, min_delta + new.blue);
1921 /* Try to allocate the color. */
1922 success_p = x_alloc_nearest_color (f, cmap, &new);
1923 if (success_p)
1925 if (new.pixel == *pixel)
1927 /* If we end up with the same color as before, try adding
1928 delta to the RGB values. */
1929 x_free_colors (f, &new.pixel, 1);
1931 new.red = min (0xffff, delta + color.red);
1932 new.green = min (0xffff, delta + color.green);
1933 new.blue = min (0xffff, delta + color.blue);
1934 success_p = x_alloc_nearest_color (f, cmap, &new);
1936 else
1937 success_p = 1;
1938 *pixel = new.pixel;
1941 return success_p;
1945 /* Set up the foreground color for drawing relief lines of glyph
1946 string S. RELIEF is a pointer to a struct relief containing the GC
1947 with which lines will be drawn. Use a color that is FACTOR or
1948 DELTA lighter or darker than the relief's background which is found
1949 in S->f->output_data.x->relief_background. If such a color cannot
1950 be allocated, use DEFAULT_PIXEL, instead. */
1952 static void
1953 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1954 struct frame *f;
1955 struct relief *relief;
1956 double factor;
1957 int delta;
1958 unsigned long default_pixel;
1960 XGCValues xgcv;
1961 struct x_output *di = f->output_data.x;
1962 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1963 unsigned long pixel;
1964 unsigned long background = di->relief_background;
1965 Colormap cmap = FRAME_X_COLORMAP (f);
1966 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1967 Display *dpy = FRAME_X_DISPLAY (f);
1969 xgcv.graphics_exposures = False;
1970 xgcv.line_width = 1;
1972 /* Free previously allocated color. The color cell will be reused
1973 when it has been freed as many times as it was allocated, so this
1974 doesn't affect faces using the same colors. */
1975 if (relief->gc
1976 && relief->allocated_p)
1978 x_free_colors (f, &relief->pixel, 1);
1979 relief->allocated_p = 0;
1982 /* Allocate new color. */
1983 xgcv.foreground = default_pixel;
1984 pixel = background;
1985 if (dpyinfo->n_planes != 1
1986 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1988 relief->allocated_p = 1;
1989 xgcv.foreground = relief->pixel = pixel;
1992 if (relief->gc == 0)
1994 xgcv.stipple = dpyinfo->gray;
1995 mask |= GCStipple;
1996 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1998 else
1999 XChangeGC (dpy, relief->gc, mask, &xgcv);
2003 /* Set up colors for the relief lines around glyph string S. */
2005 static void
2006 x_setup_relief_colors (s)
2007 struct glyph_string *s;
2009 struct x_output *di = s->f->output_data.x;
2010 unsigned long color;
2012 if (s->face->use_box_color_for_shadows_p)
2013 color = s->face->box_color;
2014 else if (s->first_glyph->type == IMAGE_GLYPH
2015 && s->img->pixmap
2016 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2017 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2018 else
2020 XGCValues xgcv;
2022 /* Get the background color of the face. */
2023 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2024 color = xgcv.background;
2027 if (di->white_relief.gc == 0
2028 || color != di->relief_background)
2030 di->relief_background = color;
2031 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2032 WHITE_PIX_DEFAULT (s->f));
2033 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2034 BLACK_PIX_DEFAULT (s->f));
2039 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2040 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2041 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2042 relief. LEFT_P non-zero means draw a relief on the left side of
2043 the rectangle. RIGHT_P non-zero means draw a relief on the right
2044 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2045 when drawing. */
2047 static void
2048 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2049 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2050 struct frame *f;
2051 int left_x, top_y, right_x, bottom_y, width;
2052 int top_p, bot_p, left_p, right_p, raised_p;
2053 XRectangle *clip_rect;
2055 Display *dpy = FRAME_X_DISPLAY (f);
2056 Window window = FRAME_X_WINDOW (f);
2057 int i;
2058 GC gc;
2060 if (raised_p)
2061 gc = f->output_data.x->white_relief.gc;
2062 else
2063 gc = f->output_data.x->black_relief.gc;
2064 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2066 /* Top. */
2067 if (top_p)
2068 for (i = 0; i < width; ++i)
2069 XDrawLine (dpy, window, gc,
2070 left_x + i * left_p, top_y + i,
2071 right_x + 1 - i * right_p, top_y + i);
2073 /* Left. */
2074 if (left_p)
2075 for (i = 0; i < width; ++i)
2076 XDrawLine (dpy, window, gc,
2077 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2079 XSetClipMask (dpy, gc, None);
2080 if (raised_p)
2081 gc = f->output_data.x->black_relief.gc;
2082 else
2083 gc = f->output_data.x->white_relief.gc;
2084 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2086 /* Bottom. */
2087 if (bot_p)
2088 for (i = 0; i < width; ++i)
2089 XDrawLine (dpy, window, gc,
2090 left_x + i * left_p, bottom_y - i,
2091 right_x + 1 - i * right_p, bottom_y - i);
2093 /* Right. */
2094 if (right_p)
2095 for (i = 0; i < width; ++i)
2096 XDrawLine (dpy, window, gc,
2097 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2099 XSetClipMask (dpy, gc, None);
2103 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2104 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2105 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2106 left side of the rectangle. RIGHT_P non-zero means draw a line
2107 on the right side of the rectangle. CLIP_RECT is the clipping
2108 rectangle to use when drawing. */
2110 static void
2111 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2112 left_p, right_p, clip_rect)
2113 struct glyph_string *s;
2114 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2115 XRectangle *clip_rect;
2117 XGCValues xgcv;
2119 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2120 XSetForeground (s->display, s->gc, s->face->box_color);
2121 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2123 /* Top. */
2124 XFillRectangle (s->display, s->window, s->gc,
2125 left_x, top_y, right_x - left_x + 1, width);
2127 /* Left. */
2128 if (left_p)
2129 XFillRectangle (s->display, s->window, s->gc,
2130 left_x, top_y, width, bottom_y - top_y + 1);
2132 /* Bottom. */
2133 XFillRectangle (s->display, s->window, s->gc,
2134 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2136 /* Right. */
2137 if (right_p)
2138 XFillRectangle (s->display, s->window, s->gc,
2139 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2141 XSetForeground (s->display, s->gc, xgcv.foreground);
2142 XSetClipMask (s->display, s->gc, None);
2146 /* Draw a box around glyph string S. */
2148 static void
2149 x_draw_glyph_string_box (s)
2150 struct glyph_string *s;
2152 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2153 int left_p, right_p;
2154 struct glyph *last_glyph;
2155 XRectangle clip_rect;
2157 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2158 ? WINDOW_RIGHT_EDGE_X (s->w)
2159 : window_box_right (s->w, s->area));
2161 /* The glyph that may have a right box line. */
2162 last_glyph = (s->cmp || s->img
2163 ? s->first_glyph
2164 : s->first_glyph + s->nchars - 1);
2166 width = eabs (s->face->box_line_width);
2167 raised_p = s->face->box == FACE_RAISED_BOX;
2168 left_x = s->x;
2169 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2170 ? last_x - 1
2171 : min (last_x, s->x + s->background_width) - 1);
2172 top_y = s->y;
2173 bottom_y = top_y + s->height - 1;
2175 left_p = (s->first_glyph->left_box_line_p
2176 || (s->hl == DRAW_MOUSE_FACE
2177 && (s->prev == NULL
2178 || s->prev->hl != s->hl)));
2179 right_p = (last_glyph->right_box_line_p
2180 || (s->hl == DRAW_MOUSE_FACE
2181 && (s->next == NULL
2182 || s->next->hl != s->hl)));
2184 get_glyph_string_clip_rect (s, &clip_rect);
2186 if (s->face->box == FACE_SIMPLE_BOX)
2187 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2188 left_p, right_p, &clip_rect);
2189 else
2191 x_setup_relief_colors (s);
2192 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2193 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2198 /* Draw foreground of image glyph string S. */
2200 static void
2201 x_draw_image_foreground (s)
2202 struct glyph_string *s;
2204 int x = s->x;
2205 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2207 /* If first glyph of S has a left box line, start drawing it to the
2208 right of that line. */
2209 if (s->face->box != FACE_NO_BOX
2210 && s->first_glyph->left_box_line_p
2211 && s->slice.x == 0)
2212 x += eabs (s->face->box_line_width);
2214 /* If there is a margin around the image, adjust x- and y-position
2215 by that margin. */
2216 if (s->slice.x == 0)
2217 x += s->img->hmargin;
2218 if (s->slice.y == 0)
2219 y += s->img->vmargin;
2221 if (s->img->pixmap)
2223 if (s->img->mask)
2225 /* We can't set both a clip mask and use XSetClipRectangles
2226 because the latter also sets a clip mask. We also can't
2227 trust on the shape extension to be available
2228 (XShapeCombineRegion). So, compute the rectangle to draw
2229 manually. */
2230 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2231 | GCFunction);
2232 XGCValues xgcv;
2233 XRectangle clip_rect, image_rect, r;
2235 xgcv.clip_mask = s->img->mask;
2236 xgcv.clip_x_origin = x;
2237 xgcv.clip_y_origin = y;
2238 xgcv.function = GXcopy;
2239 XChangeGC (s->display, s->gc, mask, &xgcv);
2241 get_glyph_string_clip_rect (s, &clip_rect);
2242 image_rect.x = x;
2243 image_rect.y = y;
2244 image_rect.width = s->slice.width;
2245 image_rect.height = s->slice.height;
2246 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2247 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2248 s->slice.x + r.x - x, s->slice.y + r.y - y,
2249 r.width, r.height, r.x, r.y);
2251 else
2253 XRectangle clip_rect, image_rect, r;
2255 get_glyph_string_clip_rect (s, &clip_rect);
2256 image_rect.x = x;
2257 image_rect.y = y;
2258 image_rect.width = s->slice.width;
2259 image_rect.height = s->slice.height;
2260 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2261 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2262 s->slice.x + r.x - x, s->slice.y + r.y - y,
2263 r.width, r.height, r.x, r.y);
2265 /* When the image has a mask, we can expect that at
2266 least part of a mouse highlight or a block cursor will
2267 be visible. If the image doesn't have a mask, make
2268 a block cursor visible by drawing a rectangle around
2269 the image. I believe it's looking better if we do
2270 nothing here for mouse-face. */
2271 if (s->hl == DRAW_CURSOR)
2273 int r = s->img->relief;
2274 if (r < 0) r = -r;
2275 XDrawRectangle (s->display, s->window, s->gc,
2276 x - r, y - r,
2277 s->slice.width + r*2 - 1,
2278 s->slice.height + r*2 - 1);
2282 else
2283 /* Draw a rectangle if image could not be loaded. */
2284 XDrawRectangle (s->display, s->window, s->gc, x, y,
2285 s->slice.width - 1, s->slice.height - 1);
2289 /* Draw a relief around the image glyph string S. */
2291 static void
2292 x_draw_image_relief (s)
2293 struct glyph_string *s;
2295 int x0, y0, x1, y1, thick, raised_p;
2296 XRectangle r;
2297 int x = s->x;
2298 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2300 /* If first glyph of S has a left box line, start drawing it to the
2301 right of that line. */
2302 if (s->face->box != FACE_NO_BOX
2303 && s->first_glyph->left_box_line_p
2304 && s->slice.x == 0)
2305 x += eabs (s->face->box_line_width);
2307 /* If there is a margin around the image, adjust x- and y-position
2308 by that margin. */
2309 if (s->slice.x == 0)
2310 x += s->img->hmargin;
2311 if (s->slice.y == 0)
2312 y += s->img->vmargin;
2314 if (s->hl == DRAW_IMAGE_SUNKEN
2315 || s->hl == DRAW_IMAGE_RAISED)
2317 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2318 raised_p = s->hl == DRAW_IMAGE_RAISED;
2320 else
2322 thick = eabs (s->img->relief);
2323 raised_p = s->img->relief > 0;
2326 x0 = x - thick;
2327 y0 = y - thick;
2328 x1 = x + s->slice.width + thick - 1;
2329 y1 = y + s->slice.height + thick - 1;
2331 x_setup_relief_colors (s);
2332 get_glyph_string_clip_rect (s, &r);
2333 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2334 s->slice.y == 0,
2335 s->slice.y + s->slice.height == s->img->height,
2336 s->slice.x == 0,
2337 s->slice.x + s->slice.width == s->img->width,
2338 &r);
2342 /* Draw the foreground of image glyph string S to PIXMAP. */
2344 static void
2345 x_draw_image_foreground_1 (s, pixmap)
2346 struct glyph_string *s;
2347 Pixmap pixmap;
2349 int x = 0;
2350 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2352 /* If first glyph of S has a left box line, start drawing it to the
2353 right of that line. */
2354 if (s->face->box != FACE_NO_BOX
2355 && s->first_glyph->left_box_line_p
2356 && s->slice.x == 0)
2357 x += eabs (s->face->box_line_width);
2359 /* If there is a margin around the image, adjust x- and y-position
2360 by that margin. */
2361 if (s->slice.x == 0)
2362 x += s->img->hmargin;
2363 if (s->slice.y == 0)
2364 y += s->img->vmargin;
2366 if (s->img->pixmap)
2368 if (s->img->mask)
2370 /* We can't set both a clip mask and use XSetClipRectangles
2371 because the latter also sets a clip mask. We also can't
2372 trust on the shape extension to be available
2373 (XShapeCombineRegion). So, compute the rectangle to draw
2374 manually. */
2375 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2376 | GCFunction);
2377 XGCValues xgcv;
2379 xgcv.clip_mask = s->img->mask;
2380 xgcv.clip_x_origin = x - s->slice.x;
2381 xgcv.clip_y_origin = y - s->slice.y;
2382 xgcv.function = GXcopy;
2383 XChangeGC (s->display, s->gc, mask, &xgcv);
2385 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2386 s->slice.x, s->slice.y,
2387 s->slice.width, s->slice.height, x, y);
2388 XSetClipMask (s->display, s->gc, None);
2390 else
2392 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2393 s->slice.x, s->slice.y,
2394 s->slice.width, s->slice.height, x, y);
2396 /* When the image has a mask, we can expect that at
2397 least part of a mouse highlight or a block cursor will
2398 be visible. If the image doesn't have a mask, make
2399 a block cursor visible by drawing a rectangle around
2400 the image. I believe it's looking better if we do
2401 nothing here for mouse-face. */
2402 if (s->hl == DRAW_CURSOR)
2404 int r = s->img->relief;
2405 if (r < 0) r = -r;
2406 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2407 s->slice.width + r*2 - 1,
2408 s->slice.height + r*2 - 1);
2412 else
2413 /* Draw a rectangle if image could not be loaded. */
2414 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2415 s->slice.width - 1, s->slice.height - 1);
2419 /* Draw part of the background of glyph string S. X, Y, W, and H
2420 give the rectangle to draw. */
2422 static void
2423 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2424 struct glyph_string *s;
2425 int x, y, w, h;
2427 if (s->stippled_p)
2429 /* Fill background with a stipple pattern. */
2430 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2431 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2432 XSetFillStyle (s->display, s->gc, FillSolid);
2434 else
2435 x_clear_glyph_string_rect (s, x, y, w, h);
2439 /* Draw image glyph string S.
2441 s->y
2442 s->x +-------------------------
2443 | s->face->box
2445 | +-------------------------
2446 | | s->img->margin
2448 | | +-------------------
2449 | | | the image
2453 static void
2454 x_draw_image_glyph_string (s)
2455 struct glyph_string *s;
2457 int box_line_hwidth = eabs (s->face->box_line_width);
2458 int box_line_vwidth = max (s->face->box_line_width, 0);
2459 int height;
2460 Pixmap pixmap = None;
2462 height = s->height;
2463 if (s->slice.y == 0)
2464 height -= box_line_vwidth;
2465 if (s->slice.y + s->slice.height >= s->img->height)
2466 height -= box_line_vwidth;
2468 /* Fill background with face under the image. Do it only if row is
2469 taller than image or if image has a clip mask to reduce
2470 flickering. */
2471 s->stippled_p = s->face->stipple != 0;
2472 if (height > s->slice.height
2473 || s->img->hmargin
2474 || s->img->vmargin
2475 || s->img->mask
2476 || s->img->pixmap == 0
2477 || s->width != s->background_width)
2479 if (s->img->mask)
2481 /* Create a pixmap as large as the glyph string. Fill it
2482 with the background color. Copy the image to it, using
2483 its mask. Copy the temporary pixmap to the display. */
2484 Screen *screen = FRAME_X_SCREEN (s->f);
2485 int depth = DefaultDepthOfScreen (screen);
2487 /* Create a pixmap as large as the glyph string. */
2488 pixmap = XCreatePixmap (s->display, s->window,
2489 s->background_width,
2490 s->height, depth);
2492 /* Don't clip in the following because we're working on the
2493 pixmap. */
2494 XSetClipMask (s->display, s->gc, None);
2496 /* Fill the pixmap with the background color/stipple. */
2497 if (s->stippled_p)
2499 /* Fill background with a stipple pattern. */
2500 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2501 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2502 XFillRectangle (s->display, pixmap, s->gc,
2503 0, 0, s->background_width, s->height);
2504 XSetFillStyle (s->display, s->gc, FillSolid);
2505 XSetTSOrigin (s->display, s->gc, 0, 0);
2507 else
2509 XGCValues xgcv;
2510 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2511 &xgcv);
2512 XSetForeground (s->display, s->gc, xgcv.background);
2513 XFillRectangle (s->display, pixmap, s->gc,
2514 0, 0, s->background_width, s->height);
2515 XSetForeground (s->display, s->gc, xgcv.foreground);
2518 else
2520 int x = s->x;
2521 int y = s->y;
2523 if (s->first_glyph->left_box_line_p
2524 && s->slice.x == 0)
2525 x += box_line_hwidth;
2527 if (s->slice.y == 0)
2528 y += box_line_vwidth;
2530 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2533 s->background_filled_p = 1;
2536 /* Draw the foreground. */
2537 if (pixmap != None)
2539 x_draw_image_foreground_1 (s, pixmap);
2540 x_set_glyph_string_clipping (s);
2541 XCopyArea (s->display, pixmap, s->window, s->gc,
2542 0, 0, s->background_width, s->height, s->x, s->y);
2543 XFreePixmap (s->display, pixmap);
2545 else
2546 x_draw_image_foreground (s);
2548 /* If we must draw a relief around the image, do it. */
2549 if (s->img->relief
2550 || s->hl == DRAW_IMAGE_RAISED
2551 || s->hl == DRAW_IMAGE_SUNKEN)
2552 x_draw_image_relief (s);
2556 /* Draw stretch glyph string S. */
2558 static void
2559 x_draw_stretch_glyph_string (s)
2560 struct glyph_string *s;
2562 xassert (s->first_glyph->type == STRETCH_GLYPH);
2564 if (s->hl == DRAW_CURSOR
2565 && !x_stretch_cursor_p)
2567 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2568 as wide as the stretch glyph. */
2569 int width, background_width = s->background_width;
2570 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2572 if (x < left_x)
2574 background_width -= left_x - x;
2575 x = left_x;
2577 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2579 /* Draw cursor. */
2580 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2582 /* Clear rest using the GC of the original non-cursor face. */
2583 if (width < background_width)
2585 int y = s->y;
2586 int w = background_width - width, h = s->height;
2587 XRectangle r;
2588 GC gc;
2590 x += width;
2591 if (s->row->mouse_face_p
2592 && cursor_in_mouse_face_p (s->w))
2594 x_set_mouse_face_gc (s);
2595 gc = s->gc;
2597 else
2598 gc = s->face->gc;
2600 get_glyph_string_clip_rect (s, &r);
2601 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2603 if (s->face->stipple)
2605 /* Fill background with a stipple pattern. */
2606 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2607 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2608 XSetFillStyle (s->display, gc, FillSolid);
2610 else
2612 XGCValues xgcv;
2613 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2614 XSetForeground (s->display, gc, xgcv.background);
2615 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2616 XSetForeground (s->display, gc, xgcv.foreground);
2620 else if (!s->background_filled_p)
2622 int background_width = s->background_width;
2623 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2625 /* Don't draw into left margin, fringe or scrollbar area
2626 except for header line and mode line. */
2627 if (x < left_x && !s->row->mode_line_p)
2629 background_width -= left_x - x;
2630 x = left_x;
2632 if (background_width > 0)
2633 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2636 s->background_filled_p = 1;
2640 /* Draw glyph string S. */
2642 static void
2643 x_draw_glyph_string (s)
2644 struct glyph_string *s;
2646 int relief_drawn_p = 0;
2648 /* If S draws into the background of its successors, draw the
2649 background of the successors first so that S can draw into it.
2650 This makes S->next use XDrawString instead of XDrawImageString. */
2651 if (s->next && s->right_overhang && !s->for_overlaps)
2653 int width;
2654 struct glyph_string *next;
2656 for (width = 0, next = s->next;
2657 next && width < s->right_overhang;
2658 width += next->width, next = next->next)
2659 if (next->first_glyph->type != IMAGE_GLYPH)
2661 x_set_glyph_string_gc (next);
2662 x_set_glyph_string_clipping (next);
2663 if (next->first_glyph->type == STRETCH_GLYPH)
2664 x_draw_stretch_glyph_string (next);
2665 else
2666 x_draw_glyph_string_background (next, 1);
2667 next->num_clips = 0;
2671 /* Set up S->gc, set clipping and draw S. */
2672 x_set_glyph_string_gc (s);
2674 /* Draw relief (if any) in advance for char/composition so that the
2675 glyph string can be drawn over it. */
2676 if (!s->for_overlaps
2677 && s->face->box != FACE_NO_BOX
2678 && (s->first_glyph->type == CHAR_GLYPH
2679 || s->first_glyph->type == COMPOSITE_GLYPH))
2682 x_set_glyph_string_clipping (s);
2683 x_draw_glyph_string_background (s, 1);
2684 x_draw_glyph_string_box (s);
2685 x_set_glyph_string_clipping (s);
2686 relief_drawn_p = 1;
2688 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2689 && !s->clip_tail
2690 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2691 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2692 /* We must clip just this glyph. left_overhang part has already
2693 drawn when s->prev was drawn, and right_overhang part will be
2694 drawn later when s->next is drawn. */
2695 x_set_glyph_string_clipping_exactly (s, s);
2696 else
2697 x_set_glyph_string_clipping (s);
2699 switch (s->first_glyph->type)
2701 case IMAGE_GLYPH:
2702 x_draw_image_glyph_string (s);
2703 break;
2705 case STRETCH_GLYPH:
2706 x_draw_stretch_glyph_string (s);
2707 break;
2709 case CHAR_GLYPH:
2710 if (s->for_overlaps)
2711 s->background_filled_p = 1;
2712 else
2713 x_draw_glyph_string_background (s, 0);
2714 x_draw_glyph_string_foreground (s);
2715 break;
2717 case COMPOSITE_GLYPH:
2718 if (s->for_overlaps || (s->cmp_from > 0
2719 && ! s->first_glyph->u.cmp.automatic))
2720 s->background_filled_p = 1;
2721 else
2722 x_draw_glyph_string_background (s, 1);
2723 x_draw_composite_glyph_string_foreground (s);
2724 break;
2726 default:
2727 abort ();
2730 if (!s->for_overlaps)
2732 /* Draw underline. */
2733 if (s->face->underline_p)
2735 unsigned long thickness, position;
2736 int y;
2738 if (s->prev && s->prev->face->underline_p)
2740 /* We use the same underline style as the previous one. */
2741 thickness = s->prev->underline_thickness;
2742 position = s->prev->underline_position;
2744 else
2746 /* Get the underline thickness. Default is 1 pixel. */
2747 if (s->font && s->font->underline_thickness > 0)
2748 thickness = s->font->underline_thickness;
2749 else
2750 thickness = 1;
2751 if (x_underline_at_descent_line)
2752 position = (s->height - thickness) - (s->ybase - s->y);
2753 else
2755 /* Get the underline position. This is the recommended
2756 vertical offset in pixels from the baseline to the top of
2757 the underline. This is a signed value according to the
2758 specs, and its default is
2760 ROUND ((maximum descent) / 2), with
2761 ROUND(x) = floor (x + 0.5) */
2763 if (x_use_underline_position_properties
2764 && s->font && s->font->underline_position >= 0)
2765 position = s->font->underline_position;
2766 else if (s->font)
2767 position = (s->font->descent + 1) / 2;
2768 else
2769 position = underline_minimum_offset;
2771 position = max (position, underline_minimum_offset);
2773 /* Check the sanity of thickness and position. We should
2774 avoid drawing underline out of the current line area. */
2775 if (s->y + s->height <= s->ybase + position)
2776 position = (s->height - 1) - (s->ybase - s->y);
2777 if (s->y + s->height < s->ybase + position + thickness)
2778 thickness = (s->y + s->height) - (s->ybase + position);
2779 s->underline_thickness = thickness;
2780 s->underline_position = position;
2781 y = s->ybase + position;
2782 if (s->face->underline_defaulted_p)
2783 XFillRectangle (s->display, s->window, s->gc,
2784 s->x, y, s->background_width, thickness);
2785 else
2787 XGCValues xgcv;
2788 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2789 XSetForeground (s->display, s->gc, s->face->underline_color);
2790 XFillRectangle (s->display, s->window, s->gc,
2791 s->x, y, s->background_width, thickness);
2792 XSetForeground (s->display, s->gc, xgcv.foreground);
2796 /* Draw overline. */
2797 if (s->face->overline_p)
2799 unsigned long dy = 0, h = 1;
2801 if (s->face->overline_color_defaulted_p)
2802 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2803 s->background_width, h);
2804 else
2806 XGCValues xgcv;
2807 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2808 XSetForeground (s->display, s->gc, s->face->overline_color);
2809 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2810 s->background_width, h);
2811 XSetForeground (s->display, s->gc, xgcv.foreground);
2815 /* Draw strike-through. */
2816 if (s->face->strike_through_p)
2818 unsigned long h = 1;
2819 unsigned long dy = (s->height - h) / 2;
2821 if (s->face->strike_through_color_defaulted_p)
2822 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2823 s->width, h);
2824 else
2826 XGCValues xgcv;
2827 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2828 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2829 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2830 s->width, h);
2831 XSetForeground (s->display, s->gc, xgcv.foreground);
2835 /* Draw relief if not yet drawn. */
2836 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2837 x_draw_glyph_string_box (s);
2839 if (s->prev)
2841 struct glyph_string *prev;
2843 for (prev = s->prev; prev; prev = prev->prev)
2844 if (prev->hl != s->hl
2845 && prev->x + prev->width + prev->right_overhang > s->x)
2847 /* As prev was drawn while clipped to its own area, we
2848 must draw the right_overhang part using s->hl now. */
2849 enum draw_glyphs_face save = prev->hl;
2851 prev->hl = s->hl;
2852 x_set_glyph_string_gc (prev);
2853 x_set_glyph_string_clipping_exactly (s, prev);
2854 if (prev->first_glyph->type == CHAR_GLYPH)
2855 x_draw_glyph_string_foreground (prev);
2856 else
2857 x_draw_composite_glyph_string_foreground (prev);
2858 XSetClipMask (prev->display, prev->gc, None);
2859 prev->hl = save;
2860 prev->num_clips = 0;
2864 if (s->next)
2866 struct glyph_string *next;
2868 for (next = s->next; next; next = next->next)
2869 if (next->hl != s->hl
2870 && next->x - next->left_overhang < s->x + s->width)
2872 /* As next will be drawn while clipped to its own area,
2873 we must draw the left_overhang part using s->hl now. */
2874 enum draw_glyphs_face save = next->hl;
2876 next->hl = s->hl;
2877 x_set_glyph_string_gc (next);
2878 x_set_glyph_string_clipping_exactly (s, next);
2879 if (next->first_glyph->type == CHAR_GLYPH)
2880 x_draw_glyph_string_foreground (next);
2881 else
2882 x_draw_composite_glyph_string_foreground (next);
2883 XSetClipMask (next->display, next->gc, None);
2884 next->hl = save;
2885 next->num_clips = 0;
2890 /* Reset clipping. */
2891 XSetClipMask (s->display, s->gc, None);
2892 s->num_clips = 0;
2895 /* Shift display to make room for inserted glyphs. */
2897 void
2898 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2899 struct frame *f;
2900 int x, y, width, height, shift_by;
2902 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2903 f->output_data.x->normal_gc,
2904 x, y, width, height,
2905 x + shift_by, y);
2908 /* Delete N glyphs at the nominal cursor position. Not implemented
2909 for X frames. */
2911 static void
2912 x_delete_glyphs (f, n)
2913 struct frame *f;
2914 register int n;
2916 abort ();
2920 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2921 If they are <= 0, this is probably an error. */
2923 void
2924 x_clear_area (dpy, window, x, y, width, height, exposures)
2925 Display *dpy;
2926 Window window;
2927 int x, y;
2928 int width, height;
2929 int exposures;
2931 xassert (width > 0 && height > 0);
2932 XClearArea (dpy, window, x, y, width, height, exposures);
2936 /* Clear an entire frame. */
2938 static void
2939 x_clear_frame (struct frame *f)
2941 /* Clearing the frame will erase any cursor, so mark them all as no
2942 longer visible. */
2943 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2944 output_cursor.hpos = output_cursor.vpos = 0;
2945 output_cursor.x = -1;
2947 /* We don't set the output cursor here because there will always
2948 follow an explicit cursor_to. */
2949 BLOCK_INPUT;
2950 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2952 /* We have to clear the scroll bars, too. If we have changed
2953 colors or something like that, then they should be notified. */
2954 x_scroll_bar_clear (f);
2956 XFlush (FRAME_X_DISPLAY (f));
2958 UNBLOCK_INPUT;
2963 /* Invert the middle quarter of the frame for .15 sec. */
2965 /* We use the select system call to do the waiting, so we have to make
2966 sure it's available. If it isn't, we just won't do visual bells. */
2968 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2971 /* Subtract the `struct timeval' values X and Y, storing the result in
2972 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2974 static int
2975 timeval_subtract (result, x, y)
2976 struct timeval *result, x, y;
2978 /* Perform the carry for the later subtraction by updating y. This
2979 is safer because on some systems the tv_sec member is unsigned. */
2980 if (x.tv_usec < y.tv_usec)
2982 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2983 y.tv_usec -= 1000000 * nsec;
2984 y.tv_sec += nsec;
2987 if (x.tv_usec - y.tv_usec > 1000000)
2989 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2990 y.tv_usec += 1000000 * nsec;
2991 y.tv_sec -= nsec;
2994 /* Compute the time remaining to wait. tv_usec is certainly
2995 positive. */
2996 result->tv_sec = x.tv_sec - y.tv_sec;
2997 result->tv_usec = x.tv_usec - y.tv_usec;
2999 /* Return indication of whether the result should be considered
3000 negative. */
3001 return x.tv_sec < y.tv_sec;
3004 void
3005 XTflash (f)
3006 struct frame *f;
3008 BLOCK_INPUT;
3011 GC gc;
3013 /* Create a GC that will use the GXxor function to flip foreground
3014 pixels into background pixels. */
3016 XGCValues values;
3018 values.function = GXxor;
3019 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3020 ^ FRAME_BACKGROUND_PIXEL (f));
3022 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3023 GCFunction | GCForeground, &values);
3027 /* Get the height not including a menu bar widget. */
3028 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
3029 /* Height of each line to flash. */
3030 int flash_height = FRAME_LINE_HEIGHT (f);
3031 /* These will be the left and right margins of the rectangles. */
3032 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3033 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3035 int width;
3037 /* Don't flash the area between a scroll bar and the frame
3038 edge it is next to. */
3039 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3041 case vertical_scroll_bar_left:
3042 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3043 break;
3045 case vertical_scroll_bar_right:
3046 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3047 break;
3049 default:
3050 break;
3053 width = flash_right - flash_left;
3055 /* If window is tall, flash top and bottom line. */
3056 if (height > 3 * FRAME_LINE_HEIGHT (f))
3058 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3059 flash_left,
3060 (FRAME_INTERNAL_BORDER_WIDTH (f)
3061 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3062 width, flash_height);
3063 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3064 flash_left,
3065 (height - flash_height
3066 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3067 width, flash_height);
3069 else
3070 /* If it is short, flash it all. */
3071 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3072 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3073 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3075 x_flush (f);
3078 struct timeval wakeup;
3080 EMACS_GET_TIME (wakeup);
3082 /* Compute time to wait until, propagating carry from usecs. */
3083 wakeup.tv_usec += 150000;
3084 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3085 wakeup.tv_usec %= 1000000;
3087 /* Keep waiting until past the time wakeup or any input gets
3088 available. */
3089 while (! detect_input_pending ())
3091 struct timeval current;
3092 struct timeval timeout;
3094 EMACS_GET_TIME (current);
3096 /* Break if result would be negative. */
3097 if (timeval_subtract (&current, wakeup, current))
3098 break;
3100 /* How long `select' should wait. */
3101 timeout.tv_sec = 0;
3102 timeout.tv_usec = 10000;
3104 /* Try to wait that long--but we might wake up sooner. */
3105 select (0, NULL, NULL, NULL, &timeout);
3109 /* If window is tall, flash top and bottom line. */
3110 if (height > 3 * FRAME_LINE_HEIGHT (f))
3112 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3113 flash_left,
3114 (FRAME_INTERNAL_BORDER_WIDTH (f)
3115 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3116 width, flash_height);
3117 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3118 flash_left,
3119 (height - flash_height
3120 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3121 width, flash_height);
3123 else
3124 /* If it is short, flash it all. */
3125 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3126 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3127 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3129 XFreeGC (FRAME_X_DISPLAY (f), gc);
3130 x_flush (f);
3134 UNBLOCK_INPUT;
3137 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3140 /* Make audible bell. */
3142 void
3143 XTring_bell ()
3145 struct frame *f = SELECTED_FRAME ();
3147 if (FRAME_X_DISPLAY (f))
3149 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3150 if (visible_bell)
3151 XTflash (f);
3152 else
3153 #endif
3155 BLOCK_INPUT;
3156 XBell (FRAME_X_DISPLAY (f), 0);
3157 XFlush (FRAME_X_DISPLAY (f));
3158 UNBLOCK_INPUT;
3164 /* Specify how many text lines, from the top of the window,
3165 should be affected by insert-lines and delete-lines operations.
3166 This, and those operations, are used only within an update
3167 that is bounded by calls to x_update_begin and x_update_end. */
3169 static void
3170 XTset_terminal_window (n)
3171 register int n;
3173 /* This function intentionally left blank. */
3178 /***********************************************************************
3179 Line Dance
3180 ***********************************************************************/
3182 /* Perform an insert-lines or delete-lines operation, inserting N
3183 lines or deleting -N lines at vertical position VPOS. */
3185 static void
3186 x_ins_del_lines (f, vpos, n)
3187 struct frame *f;
3188 int vpos, n;
3190 abort ();
3194 /* Scroll part of the display as described by RUN. */
3196 static void
3197 x_scroll_run (w, run)
3198 struct window *w;
3199 struct run *run;
3201 struct frame *f = XFRAME (w->frame);
3202 int x, y, width, height, from_y, to_y, bottom_y;
3204 /* Get frame-relative bounding box of the text display area of W,
3205 without mode lines. Include in this box the left and right
3206 fringe of W. */
3207 window_box (w, -1, &x, &y, &width, &height);
3209 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3210 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3211 bottom_y = y + height;
3213 if (to_y < from_y)
3215 /* Scrolling up. Make sure we don't copy part of the mode
3216 line at the bottom. */
3217 if (from_y + run->height > bottom_y)
3218 height = bottom_y - from_y;
3219 else
3220 height = run->height;
3222 else
3224 /* Scolling down. Make sure we don't copy over the mode line.
3225 at the bottom. */
3226 if (to_y + run->height > bottom_y)
3227 height = bottom_y - to_y;
3228 else
3229 height = run->height;
3232 BLOCK_INPUT;
3234 /* Cursor off. Will be switched on again in x_update_window_end. */
3235 updated_window = w;
3236 x_clear_cursor (w);
3238 XCopyArea (FRAME_X_DISPLAY (f),
3239 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3240 f->output_data.x->normal_gc,
3241 x, from_y,
3242 width, height,
3243 x, to_y);
3245 UNBLOCK_INPUT;
3250 /***********************************************************************
3251 Exposure Events
3252 ***********************************************************************/
3255 static void
3256 frame_highlight (f)
3257 struct frame *f;
3259 /* We used to only do this if Vx_no_window_manager was non-nil, but
3260 the ICCCM (section 4.1.6) says that the window's border pixmap
3261 and border pixel are window attributes which are "private to the
3262 client", so we can always change it to whatever we want. */
3263 BLOCK_INPUT;
3264 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3265 f->output_data.x->border_pixel);
3266 UNBLOCK_INPUT;
3267 x_update_cursor (f, 1);
3268 x_set_frame_alpha (f);
3271 static void
3272 frame_unhighlight (f)
3273 struct frame *f;
3275 /* We used to only do this if Vx_no_window_manager was non-nil, but
3276 the ICCCM (section 4.1.6) says that the window's border pixmap
3277 and border pixel are window attributes which are "private to the
3278 client", so we can always change it to whatever we want. */
3279 BLOCK_INPUT;
3280 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3281 f->output_data.x->border_tile);
3282 UNBLOCK_INPUT;
3283 x_update_cursor (f, 1);
3284 x_set_frame_alpha (f);
3287 /* The focus has changed. Update the frames as necessary to reflect
3288 the new situation. Note that we can't change the selected frame
3289 here, because the Lisp code we are interrupting might become confused.
3290 Each event gets marked with the frame in which it occurred, so the
3291 Lisp code can tell when the switch took place by examining the events. */
3293 static void
3294 x_new_focus_frame (dpyinfo, frame)
3295 struct x_display_info *dpyinfo;
3296 struct frame *frame;
3298 struct frame *old_focus = dpyinfo->x_focus_frame;
3300 if (frame != dpyinfo->x_focus_frame)
3302 /* Set this before calling other routines, so that they see
3303 the correct value of x_focus_frame. */
3304 dpyinfo->x_focus_frame = frame;
3306 if (old_focus && old_focus->auto_lower)
3307 x_lower_frame (old_focus);
3309 #if 0
3310 selected_frame = frame;
3311 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3312 selected_frame);
3313 Fselect_window (selected_frame->selected_window, Qnil);
3314 choose_minibuf_frame ();
3315 #endif /* ! 0 */
3317 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3318 pending_autoraise_frame = dpyinfo->x_focus_frame;
3319 else
3320 pending_autoraise_frame = 0;
3323 x_frame_rehighlight (dpyinfo);
3326 /* Handle FocusIn and FocusOut state changes for FRAME.
3327 If FRAME has focus and there exists more than one frame, puts
3328 a FOCUS_IN_EVENT into *BUFP. */
3330 static void
3331 x_focus_changed (type, state, dpyinfo, frame, bufp)
3332 int type;
3333 int state;
3334 struct x_display_info *dpyinfo;
3335 struct frame *frame;
3336 struct input_event *bufp;
3338 if (type == FocusIn)
3340 if (dpyinfo->x_focus_event_frame != frame)
3342 x_new_focus_frame (dpyinfo, frame);
3343 dpyinfo->x_focus_event_frame = frame;
3345 /* Don't stop displaying the initial startup message
3346 for a switch-frame event we don't need. */
3347 if (NILP (Vterminal_frame)
3348 && CONSP (Vframe_list)
3349 && !NILP (XCDR (Vframe_list)))
3351 bufp->kind = FOCUS_IN_EVENT;
3352 XSETFRAME (bufp->frame_or_window, frame);
3356 frame->output_data.x->focus_state |= state;
3358 #ifdef HAVE_X_I18N
3359 if (FRAME_XIC (frame))
3360 XSetICFocus (FRAME_XIC (frame));
3361 #endif
3363 else if (type == FocusOut)
3365 frame->output_data.x->focus_state &= ~state;
3367 if (dpyinfo->x_focus_event_frame == frame)
3369 dpyinfo->x_focus_event_frame = 0;
3370 x_new_focus_frame (dpyinfo, 0);
3373 #ifdef HAVE_X_I18N
3374 if (FRAME_XIC (frame))
3375 XUnsetICFocus (FRAME_XIC (frame));
3376 #endif
3380 /* The focus may have changed. Figure out if it is a real focus change,
3381 by checking both FocusIn/Out and Enter/LeaveNotify events.
3383 Returns FOCUS_IN_EVENT event in *BUFP. */
3385 static void
3386 x_detect_focus_change (dpyinfo, event, bufp)
3387 struct x_display_info *dpyinfo;
3388 XEvent *event;
3389 struct input_event *bufp;
3391 struct frame *frame;
3393 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3394 if (! frame)
3395 return;
3397 switch (event->type)
3399 case EnterNotify:
3400 case LeaveNotify:
3402 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3403 int focus_state
3404 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3406 if (event->xcrossing.detail != NotifyInferior
3407 && event->xcrossing.focus
3408 && ! (focus_state & FOCUS_EXPLICIT))
3409 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3410 FOCUS_IMPLICIT,
3411 dpyinfo, frame, bufp);
3413 break;
3415 case FocusIn:
3416 case FocusOut:
3417 x_focus_changed (event->type,
3418 (event->xfocus.detail == NotifyPointer ?
3419 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3420 dpyinfo, frame, bufp);
3421 break;
3423 case ClientMessage:
3424 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3426 enum xembed_message msg = event->xclient.data.l[1];
3427 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3428 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3430 break;
3435 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3437 void
3438 x_mouse_leave (dpyinfo)
3439 struct x_display_info *dpyinfo;
3441 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3444 /* The focus has changed, or we have redirected a frame's focus to
3445 another frame (this happens when a frame uses a surrogate
3446 mini-buffer frame). Shift the highlight as appropriate.
3448 The FRAME argument doesn't necessarily have anything to do with which
3449 frame is being highlighted or un-highlighted; we only use it to find
3450 the appropriate X display info. */
3452 static void
3453 XTframe_rehighlight (frame)
3454 struct frame *frame;
3456 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3459 static void
3460 x_frame_rehighlight (dpyinfo)
3461 struct x_display_info *dpyinfo;
3463 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3465 if (dpyinfo->x_focus_frame)
3467 dpyinfo->x_highlight_frame
3468 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3469 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3470 : dpyinfo->x_focus_frame);
3471 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3473 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3474 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3477 else
3478 dpyinfo->x_highlight_frame = 0;
3480 if (dpyinfo->x_highlight_frame != old_highlight)
3482 if (old_highlight)
3483 frame_unhighlight (old_highlight);
3484 if (dpyinfo->x_highlight_frame)
3485 frame_highlight (dpyinfo->x_highlight_frame);
3491 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3493 /* Initialize mode_switch_bit and modifier_meaning. */
3494 static void
3495 x_find_modifier_meanings (dpyinfo)
3496 struct x_display_info *dpyinfo;
3498 int min_code, max_code;
3499 KeySym *syms;
3500 int syms_per_code;
3501 XModifierKeymap *mods;
3503 dpyinfo->meta_mod_mask = 0;
3504 dpyinfo->shift_lock_mask = 0;
3505 dpyinfo->alt_mod_mask = 0;
3506 dpyinfo->super_mod_mask = 0;
3507 dpyinfo->hyper_mod_mask = 0;
3509 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3511 syms = XGetKeyboardMapping (dpyinfo->display,
3512 min_code, max_code - min_code + 1,
3513 &syms_per_code);
3514 mods = XGetModifierMapping (dpyinfo->display);
3516 /* Scan the modifier table to see which modifier bits the Meta and
3517 Alt keysyms are on. */
3519 int row, col; /* The row and column in the modifier table. */
3520 int found_alt_or_meta;
3522 for (row = 3; row < 8; row++)
3524 found_alt_or_meta = 0;
3525 for (col = 0; col < mods->max_keypermod; col++)
3527 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3529 /* Zeroes are used for filler. Skip them. */
3530 if (code == 0)
3531 continue;
3533 /* Are any of this keycode's keysyms a meta key? */
3535 int code_col;
3537 for (code_col = 0; code_col < syms_per_code; code_col++)
3539 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3541 switch (sym)
3543 case XK_Meta_L:
3544 case XK_Meta_R:
3545 found_alt_or_meta = 1;
3546 dpyinfo->meta_mod_mask |= (1 << row);
3547 break;
3549 case XK_Alt_L:
3550 case XK_Alt_R:
3551 found_alt_or_meta = 1;
3552 dpyinfo->alt_mod_mask |= (1 << row);
3553 break;
3555 case XK_Hyper_L:
3556 case XK_Hyper_R:
3557 if (!found_alt_or_meta)
3558 dpyinfo->hyper_mod_mask |= (1 << row);
3559 code_col = syms_per_code;
3560 col = mods->max_keypermod;
3561 break;
3563 case XK_Super_L:
3564 case XK_Super_R:
3565 if (!found_alt_or_meta)
3566 dpyinfo->super_mod_mask |= (1 << row);
3567 code_col = syms_per_code;
3568 col = mods->max_keypermod;
3569 break;
3571 case XK_Shift_Lock:
3572 /* Ignore this if it's not on the lock modifier. */
3573 if (!found_alt_or_meta && ((1 << row) == LockMask))
3574 dpyinfo->shift_lock_mask = LockMask;
3575 code_col = syms_per_code;
3576 col = mods->max_keypermod;
3577 break;
3585 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3586 if (! dpyinfo->meta_mod_mask)
3588 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3589 dpyinfo->alt_mod_mask = 0;
3592 /* If some keys are both alt and meta,
3593 make them just meta, not alt. */
3594 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3596 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3599 XFree ((char *) syms);
3600 XFreeModifiermap (mods);
3603 /* Convert between the modifier bits X uses and the modifier bits
3604 Emacs uses. */
3606 unsigned int
3607 x_x_to_emacs_modifiers (dpyinfo, state)
3608 struct x_display_info *dpyinfo;
3609 unsigned int state;
3611 EMACS_UINT mod_meta = meta_modifier;
3612 EMACS_UINT mod_alt = alt_modifier;
3613 EMACS_UINT mod_hyper = hyper_modifier;
3614 EMACS_UINT mod_super = super_modifier;
3615 Lisp_Object tem;
3617 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3618 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3619 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3620 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3621 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3622 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3623 tem = Fget (Vx_super_keysym, Qmodifier_value);
3624 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3627 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3628 | ((state & ControlMask) ? ctrl_modifier : 0)
3629 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3630 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3631 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3632 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3635 static unsigned int
3636 x_emacs_to_x_modifiers (dpyinfo, state)
3637 struct x_display_info *dpyinfo;
3638 unsigned int state;
3640 EMACS_UINT mod_meta = meta_modifier;
3641 EMACS_UINT mod_alt = alt_modifier;
3642 EMACS_UINT mod_hyper = hyper_modifier;
3643 EMACS_UINT mod_super = super_modifier;
3645 Lisp_Object tem;
3647 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3648 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3649 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3650 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3651 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3652 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3653 tem = Fget (Vx_super_keysym, Qmodifier_value);
3654 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3657 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3658 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3659 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3660 | ((state & shift_modifier) ? ShiftMask : 0)
3661 | ((state & ctrl_modifier) ? ControlMask : 0)
3662 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3665 /* Convert a keysym to its name. */
3667 char *
3668 x_get_keysym_name (keysym)
3669 KeySym keysym;
3671 char *value;
3673 BLOCK_INPUT;
3674 value = XKeysymToString (keysym);
3675 UNBLOCK_INPUT;
3677 return value;
3682 /* Mouse clicks and mouse movement. Rah. */
3684 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3686 If the event is a button press, then note that we have grabbed
3687 the mouse. */
3689 static Lisp_Object
3690 construct_mouse_click (result, event, f)
3691 struct input_event *result;
3692 XButtonEvent *event;
3693 struct frame *f;
3695 /* Make the event type NO_EVENT; we'll change that when we decide
3696 otherwise. */
3697 result->kind = MOUSE_CLICK_EVENT;
3698 result->code = event->button - Button1;
3699 result->timestamp = event->time;
3700 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3701 event->state)
3702 | (event->type == ButtonRelease
3703 ? up_modifier
3704 : down_modifier));
3706 XSETINT (result->x, event->x);
3707 XSETINT (result->y, event->y);
3708 XSETFRAME (result->frame_or_window, f);
3709 result->arg = Qnil;
3710 return Qnil;
3714 /* Function to report a mouse movement to the mainstream Emacs code.
3715 The input handler calls this.
3717 We have received a mouse movement event, which is given in *event.
3718 If the mouse is over a different glyph than it was last time, tell
3719 the mainstream emacs code by setting mouse_moved. If not, ask for
3720 another motion event, so we can check again the next time it moves. */
3722 static XMotionEvent last_mouse_motion_event;
3723 static Lisp_Object last_mouse_motion_frame;
3725 static int
3726 note_mouse_movement (frame, event)
3727 FRAME_PTR frame;
3728 XMotionEvent *event;
3730 last_mouse_movement_time = event->time;
3731 last_mouse_motion_event = *event;
3732 XSETFRAME (last_mouse_motion_frame, frame);
3734 if (!FRAME_X_OUTPUT (frame))
3735 return 0;
3737 if (event->window != FRAME_X_WINDOW (frame))
3739 frame->mouse_moved = 1;
3740 last_mouse_scroll_bar = Qnil;
3741 note_mouse_highlight (frame, -1, -1);
3742 last_mouse_glyph_frame = 0;
3743 return 1;
3747 /* Has the mouse moved off the glyph it was on at the last sighting? */
3748 if (frame != last_mouse_glyph_frame
3749 || event->x < last_mouse_glyph.x
3750 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3751 || event->y < last_mouse_glyph.y
3752 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3754 frame->mouse_moved = 1;
3755 last_mouse_scroll_bar = Qnil;
3756 note_mouse_highlight (frame, event->x, event->y);
3757 /* Remember which glyph we're now on. */
3758 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3759 last_mouse_glyph_frame = frame;
3760 return 1;
3763 return 0;
3767 /************************************************************************
3768 Mouse Face
3769 ************************************************************************/
3771 static void
3772 redo_mouse_highlight ()
3774 if (!NILP (last_mouse_motion_frame)
3775 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3776 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3777 last_mouse_motion_event.x,
3778 last_mouse_motion_event.y);
3783 /* Return the current position of the mouse.
3784 *FP should be a frame which indicates which display to ask about.
3786 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3787 and *PART to the frame, window, and scroll bar part that the mouse
3788 is over. Set *X and *Y to the portion and whole of the mouse's
3789 position on the scroll bar.
3791 If the mouse movement started elsewhere, set *FP to the frame the
3792 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3793 the mouse is over.
3795 Set *TIME to the server time-stamp for the time at which the mouse
3796 was at this position.
3798 Don't store anything if we don't have a valid set of values to report.
3800 This clears the mouse_moved flag, so we can wait for the next mouse
3801 movement. */
3803 static void
3804 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3805 FRAME_PTR *fp;
3806 int insist;
3807 Lisp_Object *bar_window;
3808 enum scroll_bar_part *part;
3809 Lisp_Object *x, *y;
3810 unsigned long *time;
3812 FRAME_PTR f1;
3814 BLOCK_INPUT;
3816 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3817 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3818 else
3820 Window root;
3821 int root_x, root_y;
3823 Window dummy_window;
3824 int dummy;
3826 Lisp_Object frame, tail;
3828 /* Clear the mouse-moved flag for every frame on this display. */
3829 FOR_EACH_FRAME (tail, frame)
3830 if (FRAME_X_P (XFRAME (frame))
3831 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3832 XFRAME (frame)->mouse_moved = 0;
3834 last_mouse_scroll_bar = Qnil;
3836 /* Figure out which root window we're on. */
3837 XQueryPointer (FRAME_X_DISPLAY (*fp),
3838 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3840 /* The root window which contains the pointer. */
3841 &root,
3843 /* Trash which we can't trust if the pointer is on
3844 a different screen. */
3845 &dummy_window,
3847 /* The position on that root window. */
3848 &root_x, &root_y,
3850 /* More trash we can't trust. */
3851 &dummy, &dummy,
3853 /* Modifier keys and pointer buttons, about which
3854 we don't care. */
3855 (unsigned int *) &dummy);
3857 /* Now we have a position on the root; find the innermost window
3858 containing the pointer. */
3860 Window win, child;
3861 int win_x, win_y;
3862 int parent_x = 0, parent_y = 0;
3864 win = root;
3866 /* XTranslateCoordinates can get errors if the window
3867 structure is changing at the same time this function
3868 is running. So at least we must not crash from them. */
3870 x_catch_errors (FRAME_X_DISPLAY (*fp));
3872 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3873 && FRAME_LIVE_P (last_mouse_frame))
3875 /* If mouse was grabbed on a frame, give coords for that frame
3876 even if the mouse is now outside it. */
3877 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3879 /* From-window, to-window. */
3880 root, FRAME_X_WINDOW (last_mouse_frame),
3882 /* From-position, to-position. */
3883 root_x, root_y, &win_x, &win_y,
3885 /* Child of win. */
3886 &child);
3887 f1 = last_mouse_frame;
3889 else
3891 while (1)
3893 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3895 /* From-window, to-window. */
3896 root, win,
3898 /* From-position, to-position. */
3899 root_x, root_y, &win_x, &win_y,
3901 /* Child of win. */
3902 &child);
3904 if (child == None || child == win)
3905 break;
3907 win = child;
3908 parent_x = win_x;
3909 parent_y = win_y;
3912 /* Now we know that:
3913 win is the innermost window containing the pointer
3914 (XTC says it has no child containing the pointer),
3915 win_x and win_y are the pointer's position in it
3916 (XTC did this the last time through), and
3917 parent_x and parent_y are the pointer's position in win's parent.
3918 (They are what win_x and win_y were when win was child.
3919 If win is the root window, it has no parent, and
3920 parent_{x,y} are invalid, but that's okay, because we'll
3921 never use them in that case.) */
3923 /* Is win one of our frames? */
3924 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3926 #ifdef USE_X_TOOLKIT
3927 /* If we end up with the menu bar window, say it's not
3928 on the frame. */
3929 if (f1 != NULL
3930 && f1->output_data.x->menubar_widget
3931 && win == XtWindow (f1->output_data.x->menubar_widget))
3932 f1 = NULL;
3933 #endif /* USE_X_TOOLKIT */
3936 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3937 f1 = 0;
3939 x_uncatch_errors ();
3941 /* If not, is it one of our scroll bars? */
3942 if (! f1)
3944 struct scroll_bar *bar;
3946 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3948 if (bar)
3950 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3951 win_x = parent_x;
3952 win_y = parent_y;
3956 if (f1 == 0 && insist > 0)
3957 f1 = SELECTED_FRAME ();
3959 if (f1)
3961 /* Ok, we found a frame. Store all the values.
3962 last_mouse_glyph is a rectangle used to reduce the
3963 generation of mouse events. To not miss any motion
3964 events, we must divide the frame into rectangles of the
3965 size of the smallest character that could be displayed
3966 on it, i.e. into the same rectangles that matrices on
3967 the frame are divided into. */
3969 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
3970 last_mouse_glyph_frame = f1;
3972 *bar_window = Qnil;
3973 *part = 0;
3974 *fp = f1;
3975 XSETINT (*x, win_x);
3976 XSETINT (*y, win_y);
3977 *time = last_mouse_movement_time;
3982 UNBLOCK_INPUT;
3987 /***********************************************************************
3988 Scroll bars
3989 ***********************************************************************/
3991 /* Scroll bar support. */
3993 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
3994 manages it.
3995 This can be called in GC, so we have to make sure to strip off mark
3996 bits. */
3998 static struct scroll_bar *
3999 x_window_to_scroll_bar (display, window_id)
4000 Display *display;
4001 Window window_id;
4003 Lisp_Object tail;
4005 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4006 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4007 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4009 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4011 Lisp_Object frame, bar, condemned;
4013 frame = XCAR (tail);
4014 /* All elements of Vframe_list should be frames. */
4015 if (! FRAMEP (frame))
4016 abort ();
4018 if (! FRAME_X_P (XFRAME (frame)))
4019 continue;
4021 /* Scan this frame's scroll bar list for a scroll bar with the
4022 right window ID. */
4023 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4024 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4025 /* This trick allows us to search both the ordinary and
4026 condemned scroll bar lists with one loop. */
4027 ! NILP (bar) || (bar = condemned,
4028 condemned = Qnil,
4029 ! NILP (bar));
4030 bar = XSCROLL_BAR (bar)->next)
4031 if (XSCROLL_BAR (bar)->x_window == window_id &&
4032 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4033 return XSCROLL_BAR (bar);
4036 return 0;
4040 #if defined USE_LUCID
4042 /* Return the Lucid menu bar WINDOW is part of. Return null
4043 if WINDOW is not part of a menu bar. */
4045 static Widget
4046 x_window_to_menu_bar (window)
4047 Window window;
4049 Lisp_Object tail;
4051 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4053 if (FRAME_X_P (XFRAME (XCAR (tail))))
4055 Lisp_Object frame = XCAR (tail);
4056 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4058 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4059 return menu_bar;
4063 return NULL;
4066 #endif /* USE_LUCID */
4069 /************************************************************************
4070 Toolkit scroll bars
4071 ************************************************************************/
4073 #ifdef USE_TOOLKIT_SCROLL_BARS
4075 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4076 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4077 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4078 struct scroll_bar *));
4079 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4080 int, int, int));
4083 /* Lisp window being scrolled. Set when starting to interact with
4084 a toolkit scroll bar, reset to nil when ending the interaction. */
4086 static Lisp_Object window_being_scrolled;
4088 /* Last scroll bar part sent in xm_scroll_callback. */
4090 static int last_scroll_bar_part;
4092 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4093 that movements of 1/20 of the screen size are mapped to up/down. */
4095 #ifndef USE_GTK
4096 /* Id of action hook installed for scroll bars. */
4098 static XtActionHookId action_hook_id;
4100 static Boolean xaw3d_arrow_scroll;
4102 /* Whether the drag scrolling maintains the mouse at the top of the
4103 thumb. If not, resizing the thumb needs to be done more carefully
4104 to avoid jerkyness. */
4106 static Boolean xaw3d_pick_top;
4108 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4109 bars are used.. The hook is responsible for detecting when
4110 the user ends an interaction with the scroll bar, and generates
4111 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4113 static void
4114 xt_action_hook (widget, client_data, action_name, event, params,
4115 num_params)
4116 Widget widget;
4117 XtPointer client_data;
4118 String action_name;
4119 XEvent *event;
4120 String *params;
4121 Cardinal *num_params;
4123 int scroll_bar_p;
4124 char *end_action;
4126 #ifdef USE_MOTIF
4127 scroll_bar_p = XmIsScrollBar (widget);
4128 end_action = "Release";
4129 #else /* !USE_MOTIF i.e. use Xaw */
4130 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4131 end_action = "EndScroll";
4132 #endif /* USE_MOTIF */
4134 if (scroll_bar_p
4135 && strcmp (action_name, end_action) == 0
4136 && WINDOWP (window_being_scrolled))
4138 struct window *w;
4140 x_send_scroll_bar_event (window_being_scrolled,
4141 scroll_bar_end_scroll, 0, 0);
4142 w = XWINDOW (window_being_scrolled);
4144 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4146 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4147 /* The thumb size is incorrect while dragging: fix it. */
4148 set_vertical_scroll_bar (w);
4150 window_being_scrolled = Qnil;
4151 last_scroll_bar_part = -1;
4153 /* Xt timeouts no longer needed. */
4154 toolkit_scroll_bar_interaction = 0;
4157 #endif /* not USE_GTK */
4159 /* A vector of windows used for communication between
4160 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4162 static struct window **scroll_bar_windows;
4163 static int scroll_bar_windows_size;
4166 /* Send a client message with message type Xatom_Scrollbar for a
4167 scroll action to the frame of WINDOW. PART is a value identifying
4168 the part of the scroll bar that was clicked on. PORTION is the
4169 amount to scroll of a whole of WHOLE. */
4171 static void
4172 x_send_scroll_bar_event (window, part, portion, whole)
4173 Lisp_Object window;
4174 int part, portion, whole;
4176 XEvent event;
4177 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4178 struct window *w = XWINDOW (window);
4179 struct frame *f = XFRAME (w->frame);
4180 int i;
4182 BLOCK_INPUT;
4184 /* Construct a ClientMessage event to send to the frame. */
4185 ev->type = ClientMessage;
4186 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4187 ev->display = FRAME_X_DISPLAY (f);
4188 ev->window = FRAME_X_WINDOW (f);
4189 ev->format = 32;
4191 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4192 not enough to store a pointer or Lisp_Object on a 64 bit system.
4193 So, store the window in scroll_bar_windows and pass the index
4194 into that array in the event. */
4195 for (i = 0; i < scroll_bar_windows_size; ++i)
4196 if (scroll_bar_windows[i] == NULL)
4197 break;
4199 if (i == scroll_bar_windows_size)
4201 int new_size = max (10, 2 * scroll_bar_windows_size);
4202 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4203 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4205 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4206 nbytes);
4207 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4208 scroll_bar_windows_size = new_size;
4211 scroll_bar_windows[i] = w;
4212 ev->data.l[0] = (long) i;
4213 ev->data.l[1] = (long) part;
4214 ev->data.l[2] = (long) 0;
4215 ev->data.l[3] = (long) portion;
4216 ev->data.l[4] = (long) whole;
4218 /* Make Xt timeouts work while the scroll bar is active. */
4219 toolkit_scroll_bar_interaction = 1;
4220 #ifdef USE_X_TOOLKIT
4221 x_activate_timeout_atimer ();
4222 #endif
4224 /* Setting the event mask to zero means that the message will
4225 be sent to the client that created the window, and if that
4226 window no longer exists, no event will be sent. */
4227 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4228 UNBLOCK_INPUT;
4232 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4233 in *IEVENT. */
4235 static void
4236 x_scroll_bar_to_input_event (event, ievent)
4237 XEvent *event;
4238 struct input_event *ievent;
4240 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4241 Lisp_Object window;
4242 struct frame *f;
4243 struct window *w;
4245 w = scroll_bar_windows[ev->data.l[0]];
4246 scroll_bar_windows[ev->data.l[0]] = NULL;
4248 XSETWINDOW (window, w);
4249 f = XFRAME (w->frame);
4251 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4252 ievent->frame_or_window = window;
4253 ievent->arg = Qnil;
4254 #ifdef USE_GTK
4255 ievent->timestamp = CurrentTime;
4256 #else
4257 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4258 #endif
4259 ievent->part = ev->data.l[1];
4260 ievent->code = ev->data.l[2];
4261 ievent->x = make_number ((int) ev->data.l[3]);
4262 ievent->y = make_number ((int) ev->data.l[4]);
4263 ievent->modifiers = 0;
4267 #ifdef USE_MOTIF
4269 /* Minimum and maximum values used for Motif scroll bars. */
4271 #define XM_SB_MAX 10000000
4274 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4275 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4276 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4278 static void
4279 xm_scroll_callback (widget, client_data, call_data)
4280 Widget widget;
4281 XtPointer client_data, call_data;
4283 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4284 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4285 int part = -1, whole = 0, portion = 0;
4287 switch (cs->reason)
4289 case XmCR_DECREMENT:
4290 bar->dragging = Qnil;
4291 part = scroll_bar_up_arrow;
4292 break;
4294 case XmCR_INCREMENT:
4295 bar->dragging = Qnil;
4296 part = scroll_bar_down_arrow;
4297 break;
4299 case XmCR_PAGE_DECREMENT:
4300 bar->dragging = Qnil;
4301 part = scroll_bar_above_handle;
4302 break;
4304 case XmCR_PAGE_INCREMENT:
4305 bar->dragging = Qnil;
4306 part = scroll_bar_below_handle;
4307 break;
4309 case XmCR_TO_TOP:
4310 bar->dragging = Qnil;
4311 part = scroll_bar_to_top;
4312 break;
4314 case XmCR_TO_BOTTOM:
4315 bar->dragging = Qnil;
4316 part = scroll_bar_to_bottom;
4317 break;
4319 case XmCR_DRAG:
4321 int slider_size;
4323 /* Get the slider size. */
4324 BLOCK_INPUT;
4325 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4326 UNBLOCK_INPUT;
4328 whole = XM_SB_MAX - slider_size;
4329 portion = min (cs->value, whole);
4330 part = scroll_bar_handle;
4331 bar->dragging = make_number (cs->value);
4333 break;
4335 case XmCR_VALUE_CHANGED:
4336 break;
4339 if (part >= 0)
4341 window_being_scrolled = bar->window;
4342 last_scroll_bar_part = part;
4343 x_send_scroll_bar_event (bar->window, part, portion, whole);
4347 #elif defined USE_GTK
4349 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4350 bar widget. DATA is a pointer to the scroll_bar structure. */
4352 static void
4353 xg_scroll_callback (widget, data)
4354 GtkRange *widget;
4355 gpointer data;
4357 struct scroll_bar *bar = (struct scroll_bar *) data;
4358 gdouble previous;
4359 gdouble position;
4360 gdouble *p;
4361 int diff;
4363 int part = -1, whole = 0, portion = 0;
4364 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4366 position = gtk_adjustment_get_value (adj);
4368 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4369 if (! p)
4371 p = (gdouble*) xmalloc (sizeof (gdouble));
4372 *p = XG_SB_MIN;
4373 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4376 previous = *p;
4377 *p = position;
4379 if (xg_ignore_gtk_scrollbar) return;
4381 diff = (int) (position - previous);
4383 if (diff == (int) adj->step_increment)
4385 part = scroll_bar_down_arrow;
4386 bar->dragging = Qnil;
4388 else if (-diff == (int) adj->step_increment)
4390 part = scroll_bar_up_arrow;
4391 bar->dragging = Qnil;
4393 else if (diff == (int) adj->page_increment)
4395 part = scroll_bar_below_handle;
4396 bar->dragging = Qnil;
4398 else if (-diff == (int) adj->page_increment)
4400 part = scroll_bar_above_handle;
4401 bar->dragging = Qnil;
4403 else
4405 part = scroll_bar_handle;
4406 whole = adj->upper - adj->page_size;
4407 portion = min ((int)position, whole);
4408 bar->dragging = make_number ((int)portion);
4411 if (part >= 0)
4413 window_being_scrolled = bar->window;
4414 last_scroll_bar_part = part;
4415 x_send_scroll_bar_event (bar->window, part, portion, whole);
4419 #else /* not USE_GTK and not USE_MOTIF */
4421 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4422 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4423 scroll bar struct. CALL_DATA is a pointer to a float saying where
4424 the thumb is. */
4426 static void
4427 xaw_jump_callback (widget, client_data, call_data)
4428 Widget widget;
4429 XtPointer client_data, call_data;
4431 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4432 float top = *(float *) call_data;
4433 float shown;
4434 int whole, portion, height;
4435 int part;
4437 /* Get the size of the thumb, a value between 0 and 1. */
4438 BLOCK_INPUT;
4439 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4440 UNBLOCK_INPUT;
4442 whole = 10000000;
4443 portion = shown < 1 ? top * whole : 0;
4445 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4446 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4447 the bottom, so we force the scrolling whenever we see that we're
4448 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4449 we try to ensure that we always stay two pixels away from the
4450 bottom). */
4451 part = scroll_bar_down_arrow;
4452 else
4453 part = scroll_bar_handle;
4455 window_being_scrolled = bar->window;
4456 bar->dragging = make_number (portion);
4457 last_scroll_bar_part = part;
4458 x_send_scroll_bar_event (bar->window, part, portion, whole);
4462 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4463 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4464 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4465 the scroll bar. CALL_DATA is an integer specifying the action that
4466 has taken place. Its magnitude is in the range 0..height of the
4467 scroll bar. Negative values mean scroll towards buffer start.
4468 Values < height of scroll bar mean line-wise movement. */
4470 static void
4471 xaw_scroll_callback (widget, client_data, call_data)
4472 Widget widget;
4473 XtPointer client_data, call_data;
4475 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4476 /* The position really is stored cast to a pointer. */
4477 int position = (long) call_data;
4478 Dimension height;
4479 int part;
4481 /* Get the height of the scroll bar. */
4482 BLOCK_INPUT;
4483 XtVaGetValues (widget, XtNheight, &height, NULL);
4484 UNBLOCK_INPUT;
4486 if (eabs (position) >= height)
4487 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4489 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4490 it maps line-movement to call_data = max(5, height/20). */
4491 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4492 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4493 else
4494 part = scroll_bar_move_ratio;
4496 window_being_scrolled = bar->window;
4497 bar->dragging = Qnil;
4498 last_scroll_bar_part = part;
4499 x_send_scroll_bar_event (bar->window, part, position, height);
4502 #endif /* not USE_GTK and not USE_MOTIF */
4504 #define SCROLL_BAR_NAME "verticalScrollBar"
4506 /* Create the widget for scroll bar BAR on frame F. Record the widget
4507 and X window of the scroll bar in BAR. */
4509 #ifdef USE_GTK
4510 static void
4511 x_create_toolkit_scroll_bar (f, bar)
4512 struct frame *f;
4513 struct scroll_bar *bar;
4515 char *scroll_bar_name = SCROLL_BAR_NAME;
4517 BLOCK_INPUT;
4518 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4519 scroll_bar_name);
4520 UNBLOCK_INPUT;
4523 #else /* not USE_GTK */
4525 static void
4526 x_create_toolkit_scroll_bar (f, bar)
4527 struct frame *f;
4528 struct scroll_bar *bar;
4530 Window xwindow;
4531 Widget widget;
4532 Arg av[20];
4533 int ac = 0;
4534 char *scroll_bar_name = SCROLL_BAR_NAME;
4535 unsigned long pixel;
4537 BLOCK_INPUT;
4539 #ifdef USE_MOTIF
4540 /* Set resources. Create the widget. */
4541 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4542 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4543 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4544 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4545 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4546 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4547 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4549 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4550 if (pixel != -1)
4552 XtSetArg (av[ac], XmNforeground, pixel);
4553 ++ac;
4556 pixel = f->output_data.x->scroll_bar_background_pixel;
4557 if (pixel != -1)
4559 XtSetArg (av[ac], XmNbackground, pixel);
4560 ++ac;
4563 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4564 scroll_bar_name, av, ac);
4566 /* Add one callback for everything that can happen. */
4567 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4568 (XtPointer) bar);
4569 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4570 (XtPointer) bar);
4571 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4572 (XtPointer) bar);
4573 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4574 (XtPointer) bar);
4575 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4576 (XtPointer) bar);
4577 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4578 (XtPointer) bar);
4579 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4580 (XtPointer) bar);
4582 /* Realize the widget. Only after that is the X window created. */
4583 XtRealizeWidget (widget);
4585 /* Set the cursor to an arrow. I didn't find a resource to do that.
4586 And I'm wondering why it hasn't an arrow cursor by default. */
4587 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4588 f->output_data.x->nontext_cursor);
4590 #else /* !USE_MOTIF i.e. use Xaw */
4592 /* Set resources. Create the widget. The background of the
4593 Xaw3d scroll bar widget is a little bit light for my taste.
4594 We don't alter it here to let users change it according
4595 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4596 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4597 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4598 /* For smoother scrolling with Xaw3d -sm */
4599 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4601 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4602 if (pixel != -1)
4604 XtSetArg (av[ac], XtNforeground, pixel);
4605 ++ac;
4608 pixel = f->output_data.x->scroll_bar_background_pixel;
4609 if (pixel != -1)
4611 XtSetArg (av[ac], XtNbackground, pixel);
4612 ++ac;
4615 /* Top/bottom shadow colors. */
4617 /* Allocate them, if necessary. */
4618 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4620 pixel = f->output_data.x->scroll_bar_background_pixel;
4621 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4622 &pixel, 1.2, 0x8000))
4623 pixel = -1;
4624 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4626 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4628 pixel = f->output_data.x->scroll_bar_background_pixel;
4629 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4630 &pixel, 0.6, 0x4000))
4631 pixel = -1;
4632 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4635 #ifdef XtNbeNiceToColormap
4636 /* Tell the toolkit about them. */
4637 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4638 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4639 /* We tried to allocate a color for the top/bottom shadow, and
4640 failed, so tell Xaw3d to use dithering instead. */
4642 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4643 ++ac;
4645 else
4646 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4647 be more consistent with other emacs 3d colors, and since Xaw3d is
4648 not good at dealing with allocation failure. */
4650 /* This tells Xaw3d to use real colors instead of dithering for
4651 the shadows. */
4652 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4653 ++ac;
4655 /* Specify the colors. */
4656 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4657 if (pixel != -1)
4659 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4660 ++ac;
4662 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4663 if (pixel != -1)
4665 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4666 ++ac;
4669 #endif
4671 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4672 f->output_data.x->edit_widget, av, ac);
4675 char *initial = "";
4676 char *val = initial;
4677 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4678 #ifdef XtNarrowScrollbars
4679 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4680 #endif
4681 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4682 if (xaw3d_arrow_scroll || val == initial)
4683 { /* ARROW_SCROLL */
4684 xaw3d_arrow_scroll = True;
4685 /* Isn't that just a personal preference ? --Stef */
4686 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4690 /* Define callbacks. */
4691 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4692 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4693 (XtPointer) bar);
4695 /* Realize the widget. Only after that is the X window created. */
4696 XtRealizeWidget (widget);
4698 #endif /* !USE_MOTIF */
4700 /* Install an action hook that lets us detect when the user
4701 finishes interacting with a scroll bar. */
4702 if (action_hook_id == 0)
4703 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4705 /* Remember X window and widget in the scroll bar vector. */
4706 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4707 xwindow = XtWindow (widget);
4708 bar->x_window = xwindow;
4710 UNBLOCK_INPUT;
4712 #endif /* not USE_GTK */
4715 /* Set the thumb size and position of scroll bar BAR. We are currently
4716 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4718 #ifdef USE_GTK
4719 static void
4720 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4721 struct scroll_bar *bar;
4722 int portion, position, whole;
4724 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4727 #else /* not USE_GTK */
4728 static void
4729 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4730 struct scroll_bar *bar;
4731 int portion, position, whole;
4733 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4734 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4735 float top, shown;
4737 BLOCK_INPUT;
4739 #ifdef USE_MOTIF
4741 /* We use an estimate of 30 chars per line rather than the real
4742 `portion' value. This has the disadvantage that the thumb size
4743 is not very representative, but it makes our life a lot easier.
4744 Otherwise, we have to constantly adjust the thumb size, which
4745 we can't always do quickly enough: while dragging, the size of
4746 the thumb might prevent the user from dragging the thumb all the
4747 way to the end. but Motif and some versions of Xaw3d don't allow
4748 updating the thumb size while dragging. Also, even if we can update
4749 its size, the update will often happen too late.
4750 If you don't believe it, check out revision 1.650 of xterm.c to see
4751 what hoops we were going through and the still poor behavior we got. */
4752 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4753 /* When the thumb is at the bottom, position == whole.
4754 So we need to increase `whole' to make space for the thumb. */
4755 whole += portion;
4757 if (whole <= 0)
4758 top = 0, shown = 1;
4759 else
4761 top = (float) position / whole;
4762 shown = (float) portion / whole;
4765 if (NILP (bar->dragging))
4767 int size, value;
4769 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4770 is the scroll bar's maximum and MIN is the scroll bar's minimum
4771 value. */
4772 size = shown * XM_SB_MAX;
4773 size = min (size, XM_SB_MAX);
4774 size = max (size, 1);
4776 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4777 value = top * XM_SB_MAX;
4778 value = min (value, XM_SB_MAX - size);
4780 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4782 #else /* !USE_MOTIF i.e. use Xaw */
4784 if (whole == 0)
4785 top = 0, shown = 1;
4786 else
4788 top = (float) position / whole;
4789 shown = (float) portion / whole;
4793 float old_top, old_shown;
4794 Dimension height;
4795 XtVaGetValues (widget,
4796 XtNtopOfThumb, &old_top,
4797 XtNshown, &old_shown,
4798 XtNheight, &height,
4799 NULL);
4801 /* Massage the top+shown values. */
4802 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4803 top = max (0, min (1, top));
4804 else
4805 top = old_top;
4806 /* Keep two pixels available for moving the thumb down. */
4807 shown = max (0, min (1 - top - (2.0 / height), shown));
4809 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4810 check that your system's configuration file contains a define
4811 for `NARROWPROTO'. See s/freebsd.h for an example. */
4812 if (top != old_top || shown != old_shown)
4814 if (NILP (bar->dragging))
4815 XawScrollbarSetThumb (widget, top, shown);
4816 else
4818 /* Try to make the scrolling a tad smoother. */
4819 if (!xaw3d_pick_top)
4820 shown = min (shown, old_shown);
4822 XawScrollbarSetThumb (widget, top, shown);
4826 #endif /* !USE_MOTIF */
4828 UNBLOCK_INPUT;
4830 #endif /* not USE_GTK */
4832 #endif /* USE_TOOLKIT_SCROLL_BARS */
4836 /************************************************************************
4837 Scroll bars, general
4838 ************************************************************************/
4840 /* Create a scroll bar and return the scroll bar vector for it. W is
4841 the Emacs window on which to create the scroll bar. TOP, LEFT,
4842 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4843 scroll bar. */
4845 static struct scroll_bar *
4846 x_scroll_bar_create (w, top, left, width, height)
4847 struct window *w;
4848 int top, left, width, height;
4850 struct frame *f = XFRAME (w->frame);
4851 struct scroll_bar *bar
4852 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4854 BLOCK_INPUT;
4856 #ifdef USE_TOOLKIT_SCROLL_BARS
4857 x_create_toolkit_scroll_bar (f, bar);
4858 #else /* not USE_TOOLKIT_SCROLL_BARS */
4860 XSetWindowAttributes a;
4861 unsigned long mask;
4862 Window window;
4864 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4865 if (a.background_pixel == -1)
4866 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4868 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4869 | ButtonMotionMask | PointerMotionHintMask
4870 | ExposureMask);
4871 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4873 mask = (CWBackPixel | CWEventMask | CWCursor);
4875 /* Clear the area of W that will serve as a scroll bar. This is
4876 for the case that a window has been split horizontally. In
4877 this case, no clear_frame is generated to reduce flickering. */
4878 if (width > 0 && height > 0)
4879 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4880 left, top, width,
4881 window_box_height (w), False);
4883 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4884 /* Position and size of scroll bar. */
4885 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4886 top,
4887 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4888 height,
4889 /* Border width, depth, class, and visual. */
4891 CopyFromParent,
4892 CopyFromParent,
4893 CopyFromParent,
4894 /* Attributes. */
4895 mask, &a);
4896 bar->x_window = window;
4898 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4900 XSETWINDOW (bar->window, w);
4901 bar->top = top;
4902 bar->left = left;
4903 bar->width = width;
4904 bar->height = height;
4905 bar->start = 0;
4906 bar->end = 0;
4907 bar->dragging = Qnil;
4908 bar->fringe_extended_p = 0;
4910 /* Add bar to its frame's list of scroll bars. */
4911 bar->next = FRAME_SCROLL_BARS (f);
4912 bar->prev = Qnil;
4913 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4914 if (!NILP (bar->next))
4915 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4917 /* Map the window/widget. */
4918 #ifdef USE_TOOLKIT_SCROLL_BARS
4920 #ifdef USE_GTK
4921 xg_update_scrollbar_pos (f,
4922 bar->x_window,
4923 top,
4924 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4925 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4926 max (height, 1));
4927 xg_show_scroll_bar (bar->x_window);
4928 #else /* not USE_GTK */
4929 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4930 XtConfigureWidget (scroll_bar,
4931 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4932 top,
4933 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4934 max (height, 1), 0);
4935 XtMapWidget (scroll_bar);
4936 #endif /* not USE_GTK */
4938 #else /* not USE_TOOLKIT_SCROLL_BARS */
4939 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
4940 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4942 UNBLOCK_INPUT;
4943 return bar;
4947 #ifndef USE_TOOLKIT_SCROLL_BARS
4949 /* Draw BAR's handle in the proper position.
4951 If the handle is already drawn from START to END, don't bother
4952 redrawing it, unless REBUILD is non-zero; in that case, always
4953 redraw it. (REBUILD is handy for drawing the handle after expose
4954 events.)
4956 Normally, we want to constrain the start and end of the handle to
4957 fit inside its rectangle, but if the user is dragging the scroll
4958 bar handle, we want to let them drag it down all the way, so that
4959 the bar's top is as far down as it goes; otherwise, there's no way
4960 to move to the very end of the buffer. */
4962 static void
4963 x_scroll_bar_set_handle (bar, start, end, rebuild)
4964 struct scroll_bar *bar;
4965 int start, end;
4966 int rebuild;
4968 int dragging = ! NILP (bar->dragging);
4969 Window w = bar->x_window;
4970 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4971 GC gc = f->output_data.x->normal_gc;
4973 /* If the display is already accurate, do nothing. */
4974 if (! rebuild
4975 && start == bar->start
4976 && end == bar->end)
4977 return;
4979 BLOCK_INPUT;
4982 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
4983 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
4984 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4986 /* Make sure the values are reasonable, and try to preserve
4987 the distance between start and end. */
4989 int length = end - start;
4991 if (start < 0)
4992 start = 0;
4993 else if (start > top_range)
4994 start = top_range;
4995 end = start + length;
4997 if (end < start)
4998 end = start;
4999 else if (end > top_range && ! dragging)
5000 end = top_range;
5003 /* Store the adjusted setting in the scroll bar. */
5004 bar->start = start;
5005 bar->end = end;
5007 /* Clip the end position, just for display. */
5008 if (end > top_range)
5009 end = top_range;
5011 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5012 below top positions, to make sure the handle is always at least
5013 that many pixels tall. */
5014 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5016 /* Draw the empty space above the handle. Note that we can't clear
5017 zero-height areas; that means "clear to end of window." */
5018 if (0 < start)
5019 x_clear_area (FRAME_X_DISPLAY (f), w,
5020 /* x, y, width, height, and exposures. */
5021 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5022 VERTICAL_SCROLL_BAR_TOP_BORDER,
5023 inside_width, start,
5024 False);
5026 /* Change to proper foreground color if one is specified. */
5027 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5028 XSetForeground (FRAME_X_DISPLAY (f), gc,
5029 f->output_data.x->scroll_bar_foreground_pixel);
5031 /* Draw the handle itself. */
5032 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5033 /* x, y, width, height */
5034 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5035 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5036 inside_width, end - start);
5038 /* Restore the foreground color of the GC if we changed it above. */
5039 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5040 XSetForeground (FRAME_X_DISPLAY (f), gc,
5041 FRAME_FOREGROUND_PIXEL (f));
5043 /* Draw the empty space below the handle. Note that we can't
5044 clear zero-height areas; that means "clear to end of window." */
5045 if (end < inside_height)
5046 x_clear_area (FRAME_X_DISPLAY (f), w,
5047 /* x, y, width, height, and exposures. */
5048 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5049 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5050 inside_width, inside_height - end,
5051 False);
5055 UNBLOCK_INPUT;
5058 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5060 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5061 nil. */
5063 static void
5064 x_scroll_bar_remove (bar)
5065 struct scroll_bar *bar;
5067 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5068 BLOCK_INPUT;
5070 #ifdef USE_TOOLKIT_SCROLL_BARS
5071 #ifdef USE_GTK
5072 xg_remove_scroll_bar (f, bar->x_window);
5073 #else /* not USE_GTK */
5074 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5075 #endif /* not USE_GTK */
5076 #else
5077 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5078 #endif
5080 /* Disassociate this scroll bar from its window. */
5081 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5083 UNBLOCK_INPUT;
5087 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5088 that we are displaying PORTION characters out of a total of WHOLE
5089 characters, starting at POSITION. If WINDOW has no scroll bar,
5090 create one. */
5092 static void
5093 XTset_vertical_scroll_bar (w, portion, whole, position)
5094 struct window *w;
5095 int portion, whole, position;
5097 struct frame *f = XFRAME (w->frame);
5098 struct scroll_bar *bar;
5099 int top, height, left, sb_left, width, sb_width;
5100 int window_y, window_height;
5101 #ifdef USE_TOOLKIT_SCROLL_BARS
5102 int fringe_extended_p;
5103 #endif
5105 /* Get window dimensions. */
5106 window_box (w, -1, 0, &window_y, 0, &window_height);
5107 top = window_y;
5108 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5109 height = window_height;
5111 /* Compute the left edge of the scroll bar area. */
5112 left = WINDOW_SCROLL_BAR_AREA_X (w);
5114 /* Compute the width of the scroll bar which might be less than
5115 the width of the area reserved for the scroll bar. */
5116 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5117 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5118 else
5119 sb_width = width;
5121 /* Compute the left edge of the scroll bar. */
5122 #ifdef USE_TOOLKIT_SCROLL_BARS
5123 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5124 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5125 else
5126 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5127 #else
5128 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5129 sb_left = left + width - sb_width;
5130 else
5131 sb_left = left;
5132 #endif
5134 #ifdef USE_TOOLKIT_SCROLL_BARS
5135 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5136 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5137 && WINDOW_LEFT_FRINGE_WIDTH (w)
5138 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5139 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5140 else
5141 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5142 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5143 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5144 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5145 #endif
5147 /* Does the scroll bar exist yet? */
5148 if (NILP (w->vertical_scroll_bar))
5150 if (width > 0 && height > 0)
5152 BLOCK_INPUT;
5153 #ifdef USE_TOOLKIT_SCROLL_BARS
5154 if (fringe_extended_p)
5155 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5156 sb_left, top, sb_width, height, False);
5157 else
5158 #endif
5159 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5160 left, top, width, height, False);
5161 UNBLOCK_INPUT;
5164 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5166 else
5168 /* It may just need to be moved and resized. */
5169 unsigned int mask = 0;
5171 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5173 BLOCK_INPUT;
5175 if (sb_left != bar->left)
5176 mask |= CWX;
5177 if (top != bar->top)
5178 mask |= CWY;
5179 if (sb_width != bar->width)
5180 mask |= CWWidth;
5181 if (height != bar->height)
5182 mask |= CWHeight;
5184 #ifdef USE_TOOLKIT_SCROLL_BARS
5186 /* Move/size the scroll bar widget. */
5187 if (mask || bar->fringe_extended_p != fringe_extended_p)
5189 /* Since toolkit scroll bars are smaller than the space reserved
5190 for them on the frame, we have to clear "under" them. */
5191 if (width > 0 && height > 0)
5193 if (fringe_extended_p)
5194 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5195 sb_left, top, sb_width, height, False);
5196 else
5197 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5198 left, top, width, height, False);
5200 #ifdef USE_GTK
5201 xg_update_scrollbar_pos (f,
5202 bar->x_window,
5203 top,
5204 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5205 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5206 max (height, 1));
5207 #else /* not USE_GTK */
5208 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5209 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5210 top,
5211 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5212 max (height, 1), 0);
5213 #endif /* not USE_GTK */
5215 #else /* not USE_TOOLKIT_SCROLL_BARS */
5217 /* Clear areas not covered by the scroll bar because of
5218 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5219 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5221 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5222 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5223 height, False);
5224 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5225 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5226 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5227 height, False);
5230 /* Clear areas not covered by the scroll bar because it's not as
5231 wide as the area reserved for it. This makes sure a
5232 previous mode line display is cleared after C-x 2 C-x 1, for
5233 example. */
5235 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5236 int rest = area_width - sb_width;
5237 if (rest > 0 && height > 0)
5239 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5240 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5241 left + area_width - rest, top,
5242 rest, height, False);
5243 else
5244 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5245 left, top, rest, height, False);
5249 /* Move/size the scroll bar window. */
5250 if (mask)
5252 XWindowChanges wc;
5254 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5255 wc.y = top;
5256 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5257 wc.height = height;
5258 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5259 mask, &wc);
5262 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5264 /* Remember new settings. */
5265 bar->left = sb_left;
5266 bar->top = top;
5267 bar->width = sb_width;
5268 bar->height = height;
5270 UNBLOCK_INPUT;
5273 #ifdef USE_TOOLKIT_SCROLL_BARS
5274 bar->fringe_extended_p = fringe_extended_p;
5276 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5277 #else /* not USE_TOOLKIT_SCROLL_BARS */
5278 /* Set the scroll bar's current state, unless we're currently being
5279 dragged. */
5280 if (NILP (bar->dragging))
5282 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5284 if (whole == 0)
5285 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5286 else
5288 int start = ((double) position * top_range) / whole;
5289 int end = ((double) (position + portion) * top_range) / whole;
5290 x_scroll_bar_set_handle (bar, start, end, 0);
5293 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5295 XSETVECTOR (w->vertical_scroll_bar, bar);
5299 /* The following three hooks are used when we're doing a thorough
5300 redisplay of the frame. We don't explicitly know which scroll bars
5301 are going to be deleted, because keeping track of when windows go
5302 away is a real pain - "Can you say set-window-configuration, boys
5303 and girls?" Instead, we just assert at the beginning of redisplay
5304 that *all* scroll bars are to be removed, and then save a scroll bar
5305 from the fiery pit when we actually redisplay its window. */
5307 /* Arrange for all scroll bars on FRAME to be removed at the next call
5308 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5309 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5311 static void
5312 XTcondemn_scroll_bars (frame)
5313 FRAME_PTR frame;
5315 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5316 while (! NILP (FRAME_SCROLL_BARS (frame)))
5318 Lisp_Object bar;
5319 bar = FRAME_SCROLL_BARS (frame);
5320 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5321 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5322 XSCROLL_BAR (bar)->prev = Qnil;
5323 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5324 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5325 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5330 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5331 Note that WINDOW isn't necessarily condemned at all. */
5333 static void
5334 XTredeem_scroll_bar (window)
5335 struct window *window;
5337 struct scroll_bar *bar;
5338 struct frame *f;
5340 /* We can't redeem this window's scroll bar if it doesn't have one. */
5341 if (NILP (window->vertical_scroll_bar))
5342 abort ();
5344 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5346 /* Unlink it from the condemned list. */
5347 f = XFRAME (WINDOW_FRAME (window));
5348 if (NILP (bar->prev))
5350 /* If the prev pointer is nil, it must be the first in one of
5351 the lists. */
5352 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5353 /* It's not condemned. Everything's fine. */
5354 return;
5355 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5356 window->vertical_scroll_bar))
5357 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5358 else
5359 /* If its prev pointer is nil, it must be at the front of
5360 one or the other! */
5361 abort ();
5363 else
5364 XSCROLL_BAR (bar->prev)->next = bar->next;
5366 if (! NILP (bar->next))
5367 XSCROLL_BAR (bar->next)->prev = bar->prev;
5369 bar->next = FRAME_SCROLL_BARS (f);
5370 bar->prev = Qnil;
5371 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5372 if (! NILP (bar->next))
5373 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5376 /* Remove all scroll bars on FRAME that haven't been saved since the
5377 last call to `*condemn_scroll_bars_hook'. */
5379 static void
5380 XTjudge_scroll_bars (f)
5381 FRAME_PTR f;
5383 Lisp_Object bar, next;
5385 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5387 /* Clear out the condemned list now so we won't try to process any
5388 more events on the hapless scroll bars. */
5389 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5391 for (; ! NILP (bar); bar = next)
5393 struct scroll_bar *b = XSCROLL_BAR (bar);
5395 x_scroll_bar_remove (b);
5397 next = b->next;
5398 b->next = b->prev = Qnil;
5401 /* Now there should be no references to the condemned scroll bars,
5402 and they should get garbage-collected. */
5406 #ifndef USE_TOOLKIT_SCROLL_BARS
5407 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5408 is a no-op when using toolkit scroll bars.
5410 This may be called from a signal handler, so we have to ignore GC
5411 mark bits. */
5413 static void
5414 x_scroll_bar_expose (bar, event)
5415 struct scroll_bar *bar;
5416 XEvent *event;
5418 Window w = bar->x_window;
5419 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5420 GC gc = f->output_data.x->normal_gc;
5421 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5423 BLOCK_INPUT;
5425 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5427 /* Switch to scroll bar foreground color. */
5428 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5429 XSetForeground (FRAME_X_DISPLAY (f), gc,
5430 f->output_data.x->scroll_bar_foreground_pixel);
5432 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5433 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5435 /* x, y, width, height */
5436 0, 0,
5437 bar->width - 1 - width_trim - width_trim,
5438 bar->height - 1);
5440 /* Restore the foreground color of the GC if we changed it above. */
5441 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5442 XSetForeground (FRAME_X_DISPLAY (f), gc,
5443 FRAME_FOREGROUND_PIXEL (f));
5445 UNBLOCK_INPUT;
5448 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5450 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5451 is set to something other than NO_EVENT, it is enqueued.
5453 This may be called from a signal handler, so we have to ignore GC
5454 mark bits. */
5457 static void
5458 x_scroll_bar_handle_click (bar, event, emacs_event)
5459 struct scroll_bar *bar;
5460 XEvent *event;
5461 struct input_event *emacs_event;
5463 if (! WINDOWP (bar->window))
5464 abort ();
5466 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5467 emacs_event->code = event->xbutton.button - Button1;
5468 emacs_event->modifiers
5469 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5470 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5471 event->xbutton.state)
5472 | (event->type == ButtonRelease
5473 ? up_modifier
5474 : down_modifier));
5475 emacs_event->frame_or_window = bar->window;
5476 emacs_event->arg = Qnil;
5477 emacs_event->timestamp = event->xbutton.time;
5479 #if 0
5480 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5481 int internal_height
5482 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5483 #endif
5484 int top_range
5485 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5486 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5488 if (y < 0) y = 0;
5489 if (y > top_range) y = top_range;
5491 if (y < bar->start)
5492 emacs_event->part = scroll_bar_above_handle;
5493 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5494 emacs_event->part = scroll_bar_handle;
5495 else
5496 emacs_event->part = scroll_bar_below_handle;
5498 /* Just because the user has clicked on the handle doesn't mean
5499 they want to drag it. Lisp code needs to be able to decide
5500 whether or not we're dragging. */
5501 #if 0
5502 /* If the user has just clicked on the handle, record where they're
5503 holding it. */
5504 if (event->type == ButtonPress
5505 && emacs_event->part == scroll_bar_handle)
5506 XSETINT (bar->dragging, y - bar->start);
5507 #endif
5509 #ifndef USE_TOOLKIT_SCROLL_BARS
5510 /* If the user has released the handle, set it to its final position. */
5511 if (event->type == ButtonRelease
5512 && ! NILP (bar->dragging))
5514 int new_start = y - XINT (bar->dragging);
5515 int new_end = new_start + bar->end - bar->start;
5517 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5518 bar->dragging = Qnil;
5520 #endif
5522 /* Same deal here as the other #if 0. */
5523 #if 0
5524 /* Clicks on the handle are always reported as occurring at the top of
5525 the handle. */
5526 if (emacs_event->part == scroll_bar_handle)
5527 emacs_event->x = bar->start;
5528 else
5529 XSETINT (emacs_event->x, y);
5530 #else
5531 XSETINT (emacs_event->x, y);
5532 #endif
5534 XSETINT (emacs_event->y, top_range);
5538 #ifndef USE_TOOLKIT_SCROLL_BARS
5540 /* Handle some mouse motion while someone is dragging the scroll bar.
5542 This may be called from a signal handler, so we have to ignore GC
5543 mark bits. */
5545 static void
5546 x_scroll_bar_note_movement (bar, event)
5547 struct scroll_bar *bar;
5548 XEvent *event;
5550 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5552 last_mouse_movement_time = event->xmotion.time;
5554 f->mouse_moved = 1;
5555 XSETVECTOR (last_mouse_scroll_bar, bar);
5557 /* If we're dragging the bar, display it. */
5558 if (! NILP (bar->dragging))
5560 /* Where should the handle be now? */
5561 int new_start = event->xmotion.y - XINT (bar->dragging);
5563 if (new_start != bar->start)
5565 int new_end = new_start + bar->end - bar->start;
5567 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5572 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5574 /* Return information to the user about the current position of the mouse
5575 on the scroll bar. */
5577 static void
5578 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5579 FRAME_PTR *fp;
5580 Lisp_Object *bar_window;
5581 enum scroll_bar_part *part;
5582 Lisp_Object *x, *y;
5583 unsigned long *time;
5585 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5586 Window w = bar->x_window;
5587 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5588 int win_x, win_y;
5589 Window dummy_window;
5590 int dummy_coord;
5591 unsigned int dummy_mask;
5593 BLOCK_INPUT;
5595 /* Get the mouse's position relative to the scroll bar window, and
5596 report that. */
5597 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5599 /* Root, child, root x and root y. */
5600 &dummy_window, &dummy_window,
5601 &dummy_coord, &dummy_coord,
5603 /* Position relative to scroll bar. */
5604 &win_x, &win_y,
5606 /* Mouse buttons and modifier keys. */
5607 &dummy_mask))
5609 else
5611 #if 0
5612 int inside_height
5613 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5614 #endif
5615 int top_range
5616 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5618 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5620 if (! NILP (bar->dragging))
5621 win_y -= XINT (bar->dragging);
5623 if (win_y < 0)
5624 win_y = 0;
5625 if (win_y > top_range)
5626 win_y = top_range;
5628 *fp = f;
5629 *bar_window = bar->window;
5631 if (! NILP (bar->dragging))
5632 *part = scroll_bar_handle;
5633 else if (win_y < bar->start)
5634 *part = scroll_bar_above_handle;
5635 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5636 *part = scroll_bar_handle;
5637 else
5638 *part = scroll_bar_below_handle;
5640 XSETINT (*x, win_y);
5641 XSETINT (*y, top_range);
5643 f->mouse_moved = 0;
5644 last_mouse_scroll_bar = Qnil;
5647 *time = last_mouse_movement_time;
5649 UNBLOCK_INPUT;
5653 /* The screen has been cleared so we may have changed foreground or
5654 background colors, and the scroll bars may need to be redrawn.
5655 Clear out the scroll bars, and ask for expose events, so we can
5656 redraw them. */
5658 void
5659 x_scroll_bar_clear (f)
5660 FRAME_PTR f;
5662 #ifndef USE_TOOLKIT_SCROLL_BARS
5663 Lisp_Object bar;
5665 /* We can have scroll bars even if this is 0,
5666 if we just turned off scroll bar mode.
5667 But in that case we should not clear them. */
5668 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5669 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5670 bar = XSCROLL_BAR (bar)->next)
5671 XClearArea (FRAME_X_DISPLAY (f),
5672 XSCROLL_BAR (bar)->x_window,
5673 0, 0, 0, 0, True);
5674 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5678 /* The main X event-reading loop - XTread_socket. */
5680 #if 0
5681 /* Time stamp of enter window event. This is only used by XTread_socket,
5682 but we have to put it out here, since static variables within functions
5683 sometimes don't work. */
5685 static Time enter_timestamp;
5686 #endif
5688 /* This holds the state XLookupString needs to implement dead keys
5689 and other tricks known as "compose processing". _X Window System_
5690 says that a portable program can't use this, but Stephen Gildea assures
5691 me that letting the compiler initialize it to zeros will work okay.
5693 This must be defined outside of XTread_socket, for the same reasons
5694 given for enter_timestamp, above. */
5696 static XComposeStatus compose_status;
5698 /* Record the last 100 characters stored
5699 to help debug the loss-of-chars-during-GC problem. */
5701 static int temp_index;
5702 static short temp_buffer[100];
5704 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5705 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5706 temp_index = 0; \
5707 temp_buffer[temp_index++] = (keysym)
5709 /* Set this to nonzero to fake an "X I/O error"
5710 on a particular display. */
5712 struct x_display_info *XTread_socket_fake_io_error;
5714 /* When we find no input here, we occasionally do a no-op command
5715 to verify that the X server is still running and we can still talk with it.
5716 We try all the open displays, one by one.
5717 This variable is used for cycling thru the displays. */
5719 static struct x_display_info *next_noop_dpyinfo;
5721 #define SET_SAVED_MENU_EVENT(size) \
5722 do \
5724 if (f->output_data.x->saved_menu_event == 0) \
5725 f->output_data.x->saved_menu_event \
5726 = (XEvent *) xmalloc (sizeof (XEvent)); \
5727 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5728 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5729 XSETFRAME (inev.ie.frame_or_window, f); \
5731 while (0)
5733 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5734 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5737 enum
5739 X_EVENT_NORMAL,
5740 X_EVENT_GOTO_OUT,
5741 X_EVENT_DROP
5744 /* Filter events for the current X input method.
5745 DPYINFO is the display this event is for.
5746 EVENT is the X event to filter.
5748 Returns non-zero if the event was filtered, caller shall not process
5749 this event further.
5750 Returns zero if event is wasn't filtered. */
5752 #ifdef HAVE_X_I18N
5753 static int
5754 x_filter_event (dpyinfo, event)
5755 struct x_display_info *dpyinfo;
5756 XEvent *event;
5758 /* XFilterEvent returns non-zero if the input method has
5759 consumed the event. We pass the frame's X window to
5760 XFilterEvent because that's the one for which the IC
5761 was created. */
5763 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5764 event->xclient.window);
5766 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5768 #endif
5770 #ifdef USE_GTK
5771 static int current_count;
5772 static int current_finish;
5773 static struct input_event *current_hold_quit;
5775 /* This is the filter function invoked by the GTK event loop.
5776 It is invoked before the XEvent is translated to a GdkEvent,
5777 so we have a chance to act on the event before GTK. */
5778 static GdkFilterReturn
5779 event_handler_gdk (gxev, ev, data)
5780 GdkXEvent *gxev;
5781 GdkEvent *ev;
5782 gpointer data;
5784 XEvent *xev = (XEvent *) gxev;
5786 if (current_count >= 0)
5788 struct x_display_info *dpyinfo;
5790 dpyinfo = x_display_info_for_display (xev->xany.display);
5792 #ifdef HAVE_X_I18N
5793 /* Filter events for the current X input method.
5794 GTK calls XFilterEvent but not for key press and release,
5795 so we do it here. */
5796 if (xev->type == KeyPress || xev->type == KeyRelease)
5797 if (dpyinfo && x_filter_event (dpyinfo, xev))
5798 return GDK_FILTER_REMOVE;
5799 #endif
5801 if (! dpyinfo)
5802 current_finish = X_EVENT_NORMAL;
5803 else
5805 current_count +=
5806 handle_one_xevent (dpyinfo, xev, &current_finish,
5807 current_hold_quit);
5810 else
5811 current_finish = x_dispatch_event (xev, xev->xany.display);
5813 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5814 return GDK_FILTER_REMOVE;
5816 return GDK_FILTER_CONTINUE;
5818 #endif /* USE_GTK */
5821 /* Handles the XEvent EVENT on display DPYINFO.
5823 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5824 *FINISH is zero if caller should continue reading events.
5825 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5827 We return the number of characters stored into the buffer. */
5829 static int
5830 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5831 struct x_display_info *dpyinfo;
5832 XEvent *eventp;
5833 int *finish;
5834 struct input_event *hold_quit;
5836 union {
5837 struct input_event ie;
5838 struct selection_input_event sie;
5839 } inev;
5840 int count = 0;
5841 int do_help = 0;
5842 int nbytes = 0;
5843 struct frame *f = NULL;
5844 struct coding_system coding;
5845 XEvent event = *eventp;
5847 *finish = X_EVENT_NORMAL;
5849 EVENT_INIT (inev.ie);
5850 inev.ie.kind = NO_EVENT;
5851 inev.ie.arg = Qnil;
5853 switch (event.type)
5855 case ClientMessage:
5857 if (event.xclient.message_type
5858 == dpyinfo->Xatom_wm_protocols
5859 && event.xclient.format == 32)
5861 if (event.xclient.data.l[0]
5862 == dpyinfo->Xatom_wm_take_focus)
5864 /* Use x_any_window_to_frame because this
5865 could be the shell widget window
5866 if the frame has no title bar. */
5867 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5868 #ifdef HAVE_X_I18N
5869 /* Not quite sure this is needed -pd */
5870 if (f && FRAME_XIC (f))
5871 XSetICFocus (FRAME_XIC (f));
5872 #endif
5873 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5874 instructs the WM to set the input focus automatically for
5875 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5876 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5877 it has set the focus. So, XSetInputFocus below is not
5878 needed.
5880 The call to XSetInputFocus below has also caused trouble. In
5881 cases where the XSetInputFocus done by the WM and the one
5882 below are temporally close (on a fast machine), the call
5883 below can generate additional FocusIn events which confuse
5884 Emacs. */
5886 /* Since we set WM_TAKE_FOCUS, we must call
5887 XSetInputFocus explicitly. But not if f is null,
5888 since that might be an event for a deleted frame. */
5889 if (f)
5891 Display *d = event.xclient.display;
5892 /* Catch and ignore errors, in case window has been
5893 iconified by a window manager such as GWM. */
5894 x_catch_errors (d);
5895 XSetInputFocus (d, event.xclient.window,
5896 /* The ICCCM says this is
5897 the only valid choice. */
5898 RevertToParent,
5899 event.xclient.data.l[1]);
5900 /* This is needed to detect the error
5901 if there is an error. */
5902 XSync (d, False);
5903 x_uncatch_errors ();
5905 /* Not certain about handling scroll bars here */
5906 #endif /* 0 */
5907 goto done;
5910 if (event.xclient.data.l[0]
5911 == dpyinfo->Xatom_wm_save_yourself)
5913 /* Save state modify the WM_COMMAND property to
5914 something which can reinstate us. This notifies
5915 the session manager, who's looking for such a
5916 PropertyNotify. Can restart processing when
5917 a keyboard or mouse event arrives. */
5918 /* If we have a session manager, don't set this.
5919 KDE will then start two Emacsen, one for the
5920 session manager and one for this. */
5921 #ifdef HAVE_X_SM
5922 if (! x_session_have_connection ())
5923 #endif
5925 f = x_top_window_to_frame (dpyinfo,
5926 event.xclient.window);
5927 /* This is just so we only give real data once
5928 for a single Emacs process. */
5929 if (f == SELECTED_FRAME ())
5930 XSetCommand (FRAME_X_DISPLAY (f),
5931 event.xclient.window,
5932 initial_argv, initial_argc);
5933 else if (f)
5934 XSetCommand (FRAME_X_DISPLAY (f),
5935 event.xclient.window,
5936 0, 0);
5938 goto done;
5941 if (event.xclient.data.l[0]
5942 == dpyinfo->Xatom_wm_delete_window)
5944 f = x_any_window_to_frame (dpyinfo,
5945 event.xclient.window);
5946 if (!f)
5947 goto OTHER; /* May be a dialog that is to be removed */
5949 inev.ie.kind = DELETE_WINDOW_EVENT;
5950 XSETFRAME (inev.ie.frame_or_window, f);
5951 goto done;
5954 goto done;
5957 if (event.xclient.message_type
5958 == dpyinfo->Xatom_wm_configure_denied)
5960 goto done;
5963 if (event.xclient.message_type
5964 == dpyinfo->Xatom_wm_window_moved)
5966 int new_x, new_y;
5967 f = x_window_to_frame (dpyinfo, event.xclient.window);
5969 new_x = event.xclient.data.s[0];
5970 new_y = event.xclient.data.s[1];
5972 if (f)
5974 f->left_pos = new_x;
5975 f->top_pos = new_y;
5977 goto done;
5980 #ifdef HACK_EDITRES
5981 if (event.xclient.message_type
5982 == dpyinfo->Xatom_editres)
5984 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5985 if (f)
5986 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5987 &event, NULL);
5988 goto done;
5990 #endif /* HACK_EDITRES */
5992 if ((event.xclient.message_type
5993 == dpyinfo->Xatom_DONE)
5994 || (event.xclient.message_type
5995 == dpyinfo->Xatom_PAGE))
5997 /* Ghostview job completed. Kill it. We could
5998 reply with "Next" if we received "Page", but we
5999 currently never do because we are interested in
6000 images, only, which should have 1 page. */
6001 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
6002 f = x_window_to_frame (dpyinfo, event.xclient.window);
6003 if (!f)
6004 goto OTHER;
6005 x_kill_gs_process (pixmap, f);
6006 expose_frame (f, 0, 0, 0, 0);
6007 goto done;
6010 #ifdef USE_TOOLKIT_SCROLL_BARS
6011 /* Scroll bar callbacks send a ClientMessage from which
6012 we construct an input_event. */
6013 if (event.xclient.message_type
6014 == dpyinfo->Xatom_Scrollbar)
6016 x_scroll_bar_to_input_event (&event, &inev.ie);
6017 *finish = X_EVENT_GOTO_OUT;
6018 goto done;
6020 #endif /* USE_TOOLKIT_SCROLL_BARS */
6022 /* XEmbed messages from the embedder (if any). */
6023 if (event.xclient.message_type
6024 == dpyinfo->Xatom_XEMBED)
6026 enum xembed_message msg = event.xclient.data.l[1];
6027 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6028 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6030 *finish = X_EVENT_GOTO_OUT;
6031 goto done;
6034 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6035 if (!f)
6036 goto OTHER;
6037 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
6038 *finish = X_EVENT_DROP;
6040 break;
6042 case SelectionNotify:
6043 last_user_time = event.xselection.time;
6044 #ifdef USE_X_TOOLKIT
6045 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6046 goto OTHER;
6047 #endif /* not USE_X_TOOLKIT */
6048 x_handle_selection_notify (&event.xselection);
6049 break;
6051 case SelectionClear: /* Someone has grabbed ownership. */
6052 last_user_time = event.xselectionclear.time;
6053 #ifdef USE_X_TOOLKIT
6054 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6055 goto OTHER;
6056 #endif /* USE_X_TOOLKIT */
6058 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
6060 inev.ie.kind = SELECTION_CLEAR_EVENT;
6061 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6062 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6063 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6064 inev.ie.frame_or_window = Qnil;
6066 break;
6068 case SelectionRequest: /* Someone wants our selection. */
6069 last_user_time = event.xselectionrequest.time;
6070 #ifdef USE_X_TOOLKIT
6071 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6072 goto OTHER;
6073 #endif /* USE_X_TOOLKIT */
6075 XSelectionRequestEvent *eventp
6076 = (XSelectionRequestEvent *) &event;
6078 inev.ie.kind = SELECTION_REQUEST_EVENT;
6079 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6080 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6081 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6082 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6083 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6084 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6085 inev.ie.frame_or_window = Qnil;
6087 break;
6089 case PropertyNotify:
6090 last_user_time = event.xproperty.time;
6091 #if 0 /* This is plain wrong. In the case that we are waiting for a
6092 PropertyNotify used as an ACK in incremental selection
6093 transfer, the property will be on the receiver's window. */
6094 #if defined USE_X_TOOLKIT
6095 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
6096 goto OTHER;
6097 #endif
6098 #endif
6099 x_handle_property_notify (&event.xproperty);
6100 goto OTHER;
6102 case ReparentNotify:
6103 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6104 if (f)
6106 int x, y;
6107 f->output_data.x->parent_desc = event.xreparent.parent;
6108 x_real_positions (f, &x, &y);
6109 f->left_pos = x;
6110 f->top_pos = y;
6112 /* Perhaps reparented due to a WM restart. Reset this. */
6113 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6114 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6116 goto OTHER;
6118 case Expose:
6119 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6120 if (f)
6122 x_check_fullscreen (f);
6124 #ifdef USE_GTK
6125 /* This seems to be needed for GTK 2.6. */
6126 x_clear_area (event.xexpose.display,
6127 event.xexpose.window,
6128 event.xexpose.x, event.xexpose.y,
6129 event.xexpose.width, event.xexpose.height,
6130 FALSE);
6131 #endif
6132 if (f->async_visible == 0)
6134 f->async_visible = 1;
6135 f->async_iconified = 0;
6136 f->output_data.x->has_been_visible = 1;
6137 SET_FRAME_GARBAGED (f);
6139 else
6140 expose_frame (f,
6141 event.xexpose.x, event.xexpose.y,
6142 event.xexpose.width, event.xexpose.height);
6144 else
6146 #ifndef USE_TOOLKIT_SCROLL_BARS
6147 struct scroll_bar *bar;
6148 #endif
6149 #if defined USE_LUCID
6150 /* Submenus of the Lucid menu bar aren't widgets
6151 themselves, so there's no way to dispatch events
6152 to them. Recognize this case separately. */
6154 Widget widget
6155 = x_window_to_menu_bar (event.xexpose.window);
6156 if (widget)
6157 xlwmenu_redisplay (widget);
6159 #endif /* USE_LUCID */
6161 #ifdef USE_TOOLKIT_SCROLL_BARS
6162 /* Dispatch event to the widget. */
6163 goto OTHER;
6164 #else /* not USE_TOOLKIT_SCROLL_BARS */
6165 bar = x_window_to_scroll_bar (event.xexpose.display,
6166 event.xexpose.window);
6168 if (bar)
6169 x_scroll_bar_expose (bar, &event);
6170 #ifdef USE_X_TOOLKIT
6171 else
6172 goto OTHER;
6173 #endif /* USE_X_TOOLKIT */
6174 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6176 break;
6178 case GraphicsExpose: /* This occurs when an XCopyArea's
6179 source area was obscured or not
6180 available. */
6181 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6182 if (f)
6184 expose_frame (f,
6185 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6186 event.xgraphicsexpose.width,
6187 event.xgraphicsexpose.height);
6189 #ifdef USE_X_TOOLKIT
6190 else
6191 goto OTHER;
6192 #endif /* USE_X_TOOLKIT */
6193 break;
6195 case NoExpose: /* This occurs when an XCopyArea's
6196 source area was completely
6197 available. */
6198 break;
6200 case UnmapNotify:
6201 /* Redo the mouse-highlight after the tooltip has gone. */
6202 if (event.xmap.window == tip_window)
6204 tip_window = 0;
6205 redo_mouse_highlight ();
6208 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6209 if (f) /* F may no longer exist if
6210 the frame was deleted. */
6212 /* While a frame is unmapped, display generation is
6213 disabled; you don't want to spend time updating a
6214 display that won't ever be seen. */
6215 f->async_visible = 0;
6216 /* We can't distinguish, from the event, whether the window
6217 has become iconified or invisible. So assume, if it
6218 was previously visible, than now it is iconified.
6219 But x_make_frame_invisible clears both
6220 the visible flag and the iconified flag;
6221 and that way, we know the window is not iconified now. */
6222 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6224 f->async_iconified = 1;
6226 inev.ie.kind = ICONIFY_EVENT;
6227 XSETFRAME (inev.ie.frame_or_window, f);
6230 goto OTHER;
6232 case MapNotify:
6233 if (event.xmap.window == tip_window)
6234 /* The tooltip has been drawn already. Avoid
6235 the SET_FRAME_GARBAGED below. */
6236 goto OTHER;
6238 /* We use x_top_window_to_frame because map events can
6239 come for sub-windows and they don't mean that the
6240 frame is visible. */
6241 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6242 if (f)
6244 /* wait_reading_process_output will notice this and update
6245 the frame's display structures.
6246 If we where iconified, we should not set garbaged,
6247 because that stops redrawing on Expose events. This looks
6248 bad if we are called from a recursive event loop
6249 (x_dispatch_event), for example when a dialog is up. */
6250 if (! f->async_iconified)
6251 SET_FRAME_GARBAGED (f);
6253 f->async_visible = 1;
6254 f->async_iconified = 0;
6255 f->output_data.x->has_been_visible = 1;
6257 if (f->iconified)
6259 inev.ie.kind = DEICONIFY_EVENT;
6260 XSETFRAME (inev.ie.frame_or_window, f);
6262 else if (! NILP (Vframe_list)
6263 && ! NILP (XCDR (Vframe_list)))
6264 /* Force a redisplay sooner or later
6265 to update the frame titles
6266 in case this is the second frame. */
6267 record_asynch_buffer_change ();
6269 goto OTHER;
6271 case KeyPress:
6273 last_user_time = event.xkey.time;
6274 ignore_next_mouse_click_timeout = 0;
6276 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6277 /* Dispatch KeyPress events when in menu. */
6278 if (popup_activated ())
6279 goto OTHER;
6280 #endif
6282 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6284 /* If mouse-highlight is an integer, input clears out
6285 mouse highlighting. */
6286 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6287 && (f == 0
6288 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6290 clear_mouse_face (dpyinfo);
6291 dpyinfo->mouse_face_hidden = 1;
6294 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6295 if (f == 0)
6297 /* Scroll bars consume key events, but we want
6298 the keys to go to the scroll bar's frame. */
6299 Widget widget = XtWindowToWidget (dpyinfo->display,
6300 event.xkey.window);
6301 if (widget && XmIsScrollBar (widget))
6303 widget = XtParent (widget);
6304 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6307 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6309 if (f != 0)
6311 KeySym keysym, orig_keysym;
6312 /* al%imercury@uunet.uu.net says that making this 81
6313 instead of 80 fixed a bug whereby meta chars made
6314 his Emacs hang.
6316 It seems that some version of XmbLookupString has
6317 a bug of not returning XBufferOverflow in
6318 status_return even if the input is too long to
6319 fit in 81 bytes. So, we must prepare sufficient
6320 bytes for copy_buffer. 513 bytes (256 chars for
6321 two-byte character set) seems to be a fairly good
6322 approximation. -- 2000.8.10 handa@etl.go.jp */
6323 unsigned char copy_buffer[513];
6324 unsigned char *copy_bufptr = copy_buffer;
6325 int copy_bufsiz = sizeof (copy_buffer);
6326 int modifiers;
6327 Lisp_Object coding_system = Qlatin_1;
6328 Lisp_Object c;
6330 #ifdef USE_GTK
6331 /* Don't pass keys to GTK. A Tab will shift focus to the
6332 tool bar in GTK 2.4. Keys will still go to menus and
6333 dialogs because in that case popup_activated is TRUE
6334 (see above). */
6335 *finish = X_EVENT_DROP;
6336 #endif
6338 event.xkey.state
6339 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6340 extra_keyboard_modifiers);
6341 modifiers = event.xkey.state;
6343 /* This will have to go some day... */
6345 /* make_lispy_event turns chars into control chars.
6346 Don't do it here because XLookupString is too eager. */
6347 event.xkey.state &= ~ControlMask;
6348 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6349 | dpyinfo->super_mod_mask
6350 | dpyinfo->hyper_mod_mask
6351 | dpyinfo->alt_mod_mask);
6353 /* In case Meta is ComposeCharacter,
6354 clear its status. According to Markus Ehrnsperger
6355 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6356 this enables ComposeCharacter to work whether or
6357 not it is combined with Meta. */
6358 if (modifiers & dpyinfo->meta_mod_mask)
6359 bzero (&compose_status, sizeof (compose_status));
6361 #ifdef HAVE_X_I18N
6362 if (FRAME_XIC (f))
6364 Status status_return;
6366 coding_system = Vlocale_coding_system;
6367 nbytes = XmbLookupString (FRAME_XIC (f),
6368 &event.xkey, copy_bufptr,
6369 copy_bufsiz, &keysym,
6370 &status_return);
6371 if (status_return == XBufferOverflow)
6373 copy_bufsiz = nbytes + 1;
6374 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6375 nbytes = XmbLookupString (FRAME_XIC (f),
6376 &event.xkey, copy_bufptr,
6377 copy_bufsiz, &keysym,
6378 &status_return);
6380 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6381 #if 0 && defined X_HAVE_UTF8_STRING
6382 else if (status_return == XLookupKeySym)
6383 { /* Try again but with utf-8. */
6384 coding_system = Qutf_8;
6385 nbytes = Xutf8LookupString (FRAME_XIC (f),
6386 &event.xkey, copy_bufptr,
6387 copy_bufsiz, &keysym,
6388 &status_return);
6389 if (status_return == XBufferOverflow)
6391 copy_bufsiz = nbytes + 1;
6392 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6393 nbytes = Xutf8LookupString (FRAME_XIC (f),
6394 &event.xkey,
6395 copy_bufptr,
6396 copy_bufsiz, &keysym,
6397 &status_return);
6400 #endif
6402 if (status_return == XLookupNone)
6403 break;
6404 else if (status_return == XLookupChars)
6406 keysym = NoSymbol;
6407 modifiers = 0;
6409 else if (status_return != XLookupKeySym
6410 && status_return != XLookupBoth)
6411 abort ();
6413 else
6414 nbytes = XLookupString (&event.xkey, copy_bufptr,
6415 copy_bufsiz, &keysym,
6416 &compose_status);
6417 #else
6418 nbytes = XLookupString (&event.xkey, copy_bufptr,
6419 copy_bufsiz, &keysym,
6420 &compose_status);
6421 #endif
6423 /* If not using XIM/XIC, and a compose sequence is in progress,
6424 we break here. Otherwise, chars_matched is always 0. */
6425 if (compose_status.chars_matched > 0 && nbytes == 0)
6426 break;
6428 bzero (&compose_status, sizeof (compose_status));
6429 orig_keysym = keysym;
6431 /* Common for all keysym input events. */
6432 XSETFRAME (inev.ie.frame_or_window, f);
6433 inev.ie.modifiers
6434 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6435 inev.ie.timestamp = event.xkey.time;
6437 /* First deal with keysyms which have defined
6438 translations to characters. */
6439 if (keysym >= 32 && keysym < 128)
6440 /* Avoid explicitly decoding each ASCII character. */
6442 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6443 inev.ie.code = keysym;
6444 goto done_keysym;
6447 /* Keysyms directly mapped to Unicode characters. */
6448 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6450 if (keysym < 0x01000080)
6451 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6452 else
6453 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6454 inev.ie.code = keysym & 0xFFFFFF;
6455 goto done_keysym;
6458 /* Now non-ASCII. */
6459 if (HASH_TABLE_P (Vx_keysym_table)
6460 && (NATNUMP (c = Fgethash (make_number (keysym),
6461 Vx_keysym_table,
6462 Qnil))))
6464 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6465 ? ASCII_KEYSTROKE_EVENT
6466 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6467 inev.ie.code = XFASTINT (c);
6468 goto done_keysym;
6471 /* Random non-modifier sorts of keysyms. */
6472 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6473 || keysym == XK_Delete
6474 #ifdef XK_ISO_Left_Tab
6475 || (keysym >= XK_ISO_Left_Tab
6476 && keysym <= XK_ISO_Enter)
6477 #endif
6478 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6479 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6480 #ifdef HPUX
6481 /* This recognizes the "extended function
6482 keys". It seems there's no cleaner way.
6483 Test IsModifierKey to avoid handling
6484 mode_switch incorrectly. */
6485 || ((unsigned) (keysym) >= XK_Select
6486 && (unsigned)(keysym) < XK_KP_Space)
6487 #endif
6488 #ifdef XK_dead_circumflex
6489 || orig_keysym == XK_dead_circumflex
6490 #endif
6491 #ifdef XK_dead_grave
6492 || orig_keysym == XK_dead_grave
6493 #endif
6494 #ifdef XK_dead_tilde
6495 || orig_keysym == XK_dead_tilde
6496 #endif
6497 #ifdef XK_dead_diaeresis
6498 || orig_keysym == XK_dead_diaeresis
6499 #endif
6500 #ifdef XK_dead_macron
6501 || orig_keysym == XK_dead_macron
6502 #endif
6503 #ifdef XK_dead_degree
6504 || orig_keysym == XK_dead_degree
6505 #endif
6506 #ifdef XK_dead_acute
6507 || orig_keysym == XK_dead_acute
6508 #endif
6509 #ifdef XK_dead_cedilla
6510 || orig_keysym == XK_dead_cedilla
6511 #endif
6512 #ifdef XK_dead_breve
6513 || orig_keysym == XK_dead_breve
6514 #endif
6515 #ifdef XK_dead_ogonek
6516 || orig_keysym == XK_dead_ogonek
6517 #endif
6518 #ifdef XK_dead_caron
6519 || orig_keysym == XK_dead_caron
6520 #endif
6521 #ifdef XK_dead_doubleacute
6522 || orig_keysym == XK_dead_doubleacute
6523 #endif
6524 #ifdef XK_dead_abovedot
6525 || orig_keysym == XK_dead_abovedot
6526 #endif
6527 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6528 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6529 /* Any "vendor-specific" key is ok. */
6530 || (orig_keysym & (1 << 28))
6531 || (keysym != NoSymbol && nbytes == 0))
6532 && ! (IsModifierKey (orig_keysym)
6533 /* The symbols from XK_ISO_Lock
6534 to XK_ISO_Last_Group_Lock
6535 don't have real modifiers but
6536 should be treated similarly to
6537 Mode_switch by Emacs. */
6538 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6539 || ((unsigned)(orig_keysym)
6540 >= XK_ISO_Lock
6541 && (unsigned)(orig_keysym)
6542 <= XK_ISO_Last_Group_Lock)
6543 #endif
6546 STORE_KEYSYM_FOR_DEBUG (keysym);
6547 /* make_lispy_event will convert this to a symbolic
6548 key. */
6549 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6550 inev.ie.code = keysym;
6551 goto done_keysym;
6554 { /* Raw bytes, not keysym. */
6555 register int i;
6556 register int c;
6557 int nchars, len;
6559 for (i = 0, nchars = 0; i < nbytes; i++)
6561 if (ASCII_BYTE_P (copy_bufptr[i]))
6562 nchars++;
6563 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6566 if (nchars < nbytes)
6568 /* Decode the input data. */
6569 int require;
6570 unsigned char *p;
6572 /* The input should be decoded with `coding_system'
6573 which depends on which X*LookupString function
6574 we used just above and the locale. */
6575 setup_coding_system (coding_system, &coding);
6576 coding.src_multibyte = 0;
6577 coding.dst_multibyte = 1;
6578 /* The input is converted to events, thus we can't
6579 handle composition. Anyway, there's no XIM that
6580 gives us composition information. */
6581 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6583 require = MAX_MULTIBYTE_LENGTH * nbytes;
6584 coding.destination = alloca (require);
6585 coding.dst_bytes = require;
6586 coding.mode |= CODING_MODE_LAST_BLOCK;
6587 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6588 nbytes = coding.produced;
6589 nchars = coding.produced_char;
6590 copy_bufptr = coding.destination;
6593 /* Convert the input data to a sequence of
6594 character events. */
6595 for (i = 0; i < nbytes; i += len)
6597 if (nchars == nbytes)
6598 c = copy_bufptr[i], len = 1;
6599 else
6600 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6601 nbytes - i, len);
6602 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6603 ? ASCII_KEYSTROKE_EVENT
6604 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6605 inev.ie.code = c;
6606 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6609 count += nchars;
6611 inev.ie.kind = NO_EVENT; /* Already stored above. */
6613 if (keysym == NoSymbol)
6614 break;
6617 done_keysym:
6618 #ifdef HAVE_X_I18N
6619 /* Don't dispatch this event since XtDispatchEvent calls
6620 XFilterEvent, and two calls in a row may freeze the
6621 client. */
6622 break;
6623 #else
6624 goto OTHER;
6625 #endif
6627 case KeyRelease:
6628 last_user_time = event.xkey.time;
6629 #ifdef HAVE_X_I18N
6630 /* Don't dispatch this event since XtDispatchEvent calls
6631 XFilterEvent, and two calls in a row may freeze the
6632 client. */
6633 break;
6634 #else
6635 goto OTHER;
6636 #endif
6638 case EnterNotify:
6639 last_user_time = event.xcrossing.time;
6640 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6642 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6644 if (f && x_mouse_click_focus_ignore_position)
6645 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6647 #if 0
6648 if (event.xcrossing.focus)
6650 /* Avoid nasty pop/raise loops. */
6651 if (f && (!(f->auto_raise)
6652 || !(f->auto_lower)
6653 || (event.xcrossing.time - enter_timestamp) > 500))
6655 x_new_focus_frame (dpyinfo, f);
6656 enter_timestamp = event.xcrossing.time;
6659 else if (f == dpyinfo->x_focus_frame)
6660 x_new_focus_frame (dpyinfo, 0);
6661 #endif
6663 /* EnterNotify counts as mouse movement,
6664 so update things that depend on mouse position. */
6665 if (f && !f->output_data.x->hourglass_p)
6666 note_mouse_movement (f, &event.xmotion);
6667 #ifdef USE_GTK
6668 /* We may get an EnterNotify on the buttons in the toolbar. In that
6669 case we moved out of any highlighted area and need to note this. */
6670 if (!f && last_mouse_glyph_frame)
6671 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6672 #endif
6673 goto OTHER;
6675 case FocusIn:
6676 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6677 goto OTHER;
6679 case LeaveNotify:
6680 last_user_time = event.xcrossing.time;
6681 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6683 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6684 if (f)
6686 if (f == dpyinfo->mouse_face_mouse_frame)
6688 /* If we move outside the frame, then we're
6689 certainly no longer on any text in the frame. */
6690 clear_mouse_face (dpyinfo);
6691 dpyinfo->mouse_face_mouse_frame = 0;
6694 /* Generate a nil HELP_EVENT to cancel a help-echo.
6695 Do it only if there's something to cancel.
6696 Otherwise, the startup message is cleared when
6697 the mouse leaves the frame. */
6698 if (any_help_event_p)
6699 do_help = -1;
6701 #ifdef USE_GTK
6702 /* See comment in EnterNotify above */
6703 else if (last_mouse_glyph_frame)
6704 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6705 #endif
6706 goto OTHER;
6708 case FocusOut:
6709 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6710 goto OTHER;
6712 case MotionNotify:
6714 last_user_time = event.xmotion.time;
6715 previous_help_echo_string = help_echo_string;
6716 help_echo_string = Qnil;
6718 if (dpyinfo->grabbed && last_mouse_frame
6719 && FRAME_LIVE_P (last_mouse_frame))
6720 f = last_mouse_frame;
6721 else
6722 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6724 if (dpyinfo->mouse_face_hidden)
6726 dpyinfo->mouse_face_hidden = 0;
6727 clear_mouse_face (dpyinfo);
6730 if (f)
6733 /* Generate SELECT_WINDOW_EVENTs when needed.
6734 Don't let popup menus influence things (bug#1261). */
6735 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6737 Lisp_Object window;
6739 window = window_from_coordinates (f,
6740 event.xmotion.x, event.xmotion.y,
6741 0, 0, 0, 0);
6743 /* Window will be selected only when it is not selected now and
6744 last mouse movement event was not in it. Minibuffer window
6745 will be selected only when it is active. */
6746 if (WINDOWP (window)
6747 && !EQ (window, last_window)
6748 && !EQ (window, selected_window)
6749 /* For click-to-focus window managers
6750 create event iff we don't leave the
6751 selected frame. */
6752 && (focus_follows_mouse
6753 || (EQ (XWINDOW (window)->frame,
6754 XWINDOW (selected_window)->frame))))
6756 inev.ie.kind = SELECT_WINDOW_EVENT;
6757 inev.ie.frame_or_window = window;
6760 last_window=window;
6762 if (!note_mouse_movement (f, &event.xmotion))
6763 help_echo_string = previous_help_echo_string;
6765 else
6767 #ifndef USE_TOOLKIT_SCROLL_BARS
6768 struct scroll_bar *bar
6769 = x_window_to_scroll_bar (event.xmotion.display,
6770 event.xmotion.window);
6772 if (bar)
6773 x_scroll_bar_note_movement (bar, &event);
6774 #endif /* USE_TOOLKIT_SCROLL_BARS */
6776 /* If we move outside the frame, then we're
6777 certainly no longer on any text in the frame. */
6778 clear_mouse_face (dpyinfo);
6781 /* If the contents of the global variable help_echo_string
6782 has changed, generate a HELP_EVENT. */
6783 if (!NILP (help_echo_string)
6784 || !NILP (previous_help_echo_string))
6785 do_help = 1;
6786 goto OTHER;
6789 case ConfigureNotify:
6790 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6791 #ifdef USE_GTK
6792 if (!f
6793 && (f = x_any_window_to_frame (dpyinfo, event.xconfigure.window))
6794 && event.xconfigure.window == FRAME_X_WINDOW (f))
6796 xg_frame_resized (f, event.xconfigure.width,
6797 event.xconfigure.height);
6798 f = 0;
6800 #endif
6801 if (f)
6803 #ifndef USE_X_TOOLKIT
6804 #ifndef USE_GTK
6805 /* If there is a pending resize for fullscreen, don't
6806 do this one, the right one will come later.
6807 The toolkit version doesn't seem to need this, but we
6808 need to reset it below. */
6809 int dont_resize
6810 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6811 && f->new_text_cols != 0);
6812 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6813 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6815 if (dont_resize)
6816 goto OTHER;
6818 /* In the toolkit version, change_frame_size
6819 is called by the code that handles resizing
6820 of the EmacsFrame widget. */
6822 /* Even if the number of character rows and columns has
6823 not changed, the font size may have changed, so we need
6824 to check the pixel dimensions as well. */
6825 if (columns != FRAME_COLS (f)
6826 || rows != FRAME_LINES (f)
6827 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6828 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6830 change_frame_size (f, rows, columns, 0, 1, 0);
6831 SET_FRAME_GARBAGED (f);
6832 cancel_mouse_face (f);
6835 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6836 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6837 #endif /* not USE_GTK */
6838 #endif
6840 #ifdef USE_GTK
6841 /* GTK creates windows but doesn't map them.
6842 Only get real positions and check fullscreen when mapped. */
6843 if (FRAME_GTK_OUTER_WIDGET (f)
6844 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6845 #endif
6847 x_real_positions (f, &f->left_pos, &f->top_pos);
6849 if (f->want_fullscreen & FULLSCREEN_WAIT)
6850 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6853 #ifdef HAVE_X_I18N
6854 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6855 xic_set_statusarea (f);
6856 #endif
6858 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6860 /* Since the WM decorations come below top_pos now,
6861 we must put them below top_pos in the future. */
6862 f->win_gravity = NorthWestGravity;
6863 x_wm_set_size_hint (f, (long) 0, 0);
6866 goto OTHER;
6868 case ButtonRelease:
6869 case ButtonPress:
6871 /* If we decide we want to generate an event to be seen
6872 by the rest of Emacs, we put it here. */
6873 int tool_bar_p = 0;
6875 bzero (&compose_status, sizeof (compose_status));
6876 last_mouse_glyph_frame = 0;
6877 last_user_time = event.xbutton.time;
6879 if (dpyinfo->grabbed
6880 && last_mouse_frame
6881 && FRAME_LIVE_P (last_mouse_frame))
6882 f = last_mouse_frame;
6883 else
6884 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6886 if (f)
6888 /* Is this in the tool-bar? */
6889 if (WINDOWP (f->tool_bar_window)
6890 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6892 Lisp_Object window;
6893 int x = event.xbutton.x;
6894 int y = event.xbutton.y;
6896 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6897 tool_bar_p = EQ (window, f->tool_bar_window);
6899 if (tool_bar_p && event.xbutton.button < 4)
6901 handle_tool_bar_click (f, x, y,
6902 event.xbutton.type == ButtonPress,
6903 x_x_to_emacs_modifiers (dpyinfo,
6904 event.xbutton.state));
6908 if (!tool_bar_p)
6909 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6910 if (! popup_activated ())
6911 #endif
6913 if (ignore_next_mouse_click_timeout)
6915 if (event.type == ButtonPress
6916 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6918 ignore_next_mouse_click_timeout = 0;
6919 construct_mouse_click (&inev.ie, &event.xbutton, f);
6921 if (event.type == ButtonRelease)
6922 ignore_next_mouse_click_timeout = 0;
6924 else
6925 construct_mouse_click (&inev.ie, &event.xbutton, f);
6927 if (FRAME_X_EMBEDDED_P (f))
6928 xembed_send_message (f, event.xbutton.time,
6929 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6931 else
6933 struct scroll_bar *bar
6934 = x_window_to_scroll_bar (event.xbutton.display,
6935 event.xbutton.window);
6937 #ifdef USE_TOOLKIT_SCROLL_BARS
6938 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6939 scroll bars. */
6940 if (bar && event.xbutton.state & ControlMask)
6942 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6943 *finish = X_EVENT_DROP;
6945 #else /* not USE_TOOLKIT_SCROLL_BARS */
6946 if (bar)
6947 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6948 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6951 if (event.type == ButtonPress)
6953 dpyinfo->grabbed |= (1 << event.xbutton.button);
6954 last_mouse_frame = f;
6956 if (!tool_bar_p)
6957 last_tool_bar_item = -1;
6959 else
6960 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6962 /* Ignore any mouse motion that happened before this event;
6963 any subsequent mouse-movement Emacs events should reflect
6964 only motion after the ButtonPress/Release. */
6965 if (f != 0)
6966 f->mouse_moved = 0;
6968 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6969 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6970 /* For a down-event in the menu bar,
6971 don't pass it to Xt right now.
6972 Instead, save it away
6973 and we will pass it to Xt from kbd_buffer_get_event.
6974 That way, we can run some Lisp code first. */
6975 if (
6976 #ifdef USE_GTK
6977 ! popup_activated ()
6978 /* Gtk+ menus only react to the first three buttons. */
6979 && event.xbutton.button < 3
6981 #endif
6982 f && event.type == ButtonPress
6983 /* Verify the event is really within the menu bar
6984 and not just sent to it due to grabbing. */
6985 && event.xbutton.x >= 0
6986 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6987 && event.xbutton.y >= 0
6988 && event.xbutton.y < f->output_data.x->menubar_height
6989 && event.xbutton.same_screen)
6991 SET_SAVED_BUTTON_EVENT;
6992 XSETFRAME (last_mouse_press_frame, f);
6993 #ifdef USE_GTK
6994 *finish = X_EVENT_DROP;
6995 #endif
6997 else if (event.type == ButtonPress)
6999 last_mouse_press_frame = Qnil;
7000 goto OTHER;
7003 #ifdef USE_MOTIF /* This should do not harm for Lucid,
7004 but I am trying to be cautious. */
7005 else if (event.type == ButtonRelease)
7007 if (!NILP (last_mouse_press_frame))
7009 f = XFRAME (last_mouse_press_frame);
7010 if (f->output_data.x)
7011 SET_SAVED_BUTTON_EVENT;
7013 else
7014 goto OTHER;
7016 #endif /* USE_MOTIF */
7017 else
7018 goto OTHER;
7019 #endif /* USE_X_TOOLKIT || USE_GTK */
7021 break;
7023 case CirculateNotify:
7024 goto OTHER;
7026 case CirculateRequest:
7027 goto OTHER;
7029 case VisibilityNotify:
7030 goto OTHER;
7032 case MappingNotify:
7033 /* Someone has changed the keyboard mapping - update the
7034 local cache. */
7035 switch (event.xmapping.request)
7037 case MappingModifier:
7038 x_find_modifier_meanings (dpyinfo);
7039 /* This is meant to fall through. */
7040 case MappingKeyboard:
7041 XRefreshKeyboardMapping (&event.xmapping);
7043 goto OTHER;
7045 default:
7046 OTHER:
7047 #ifdef USE_X_TOOLKIT
7048 BLOCK_INPUT;
7049 if (*finish != X_EVENT_DROP)
7050 XtDispatchEvent (&event);
7051 UNBLOCK_INPUT;
7052 #endif /* USE_X_TOOLKIT */
7053 break;
7056 done:
7057 if (inev.ie.kind != NO_EVENT)
7059 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7060 count++;
7063 if (do_help
7064 && !(hold_quit && hold_quit->kind != NO_EVENT))
7066 Lisp_Object frame;
7068 if (f)
7069 XSETFRAME (frame, f);
7070 else
7071 frame = Qnil;
7073 if (do_help > 0)
7075 any_help_event_p = 1;
7076 gen_help_event (help_echo_string, frame, help_echo_window,
7077 help_echo_object, help_echo_pos);
7079 else
7081 help_echo_string = Qnil;
7082 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7084 count++;
7087 *eventp = event;
7088 return count;
7092 /* Handles the XEvent EVENT on display DISPLAY.
7093 This is used for event loops outside the normal event handling,
7094 i.e. looping while a popup menu or a dialog is posted.
7096 Returns the value handle_one_xevent sets in the finish argument. */
7098 x_dispatch_event (event, display)
7099 XEvent *event;
7100 Display *display;
7102 struct x_display_info *dpyinfo;
7103 int finish = X_EVENT_NORMAL;
7105 dpyinfo = x_display_info_for_display (display);
7107 if (dpyinfo)
7108 handle_one_xevent (dpyinfo, event, &finish, 0);
7110 return finish;
7114 /* Read events coming from the X server.
7115 This routine is called by the SIGIO handler.
7116 We return as soon as there are no more events to be read.
7118 We return the number of characters stored into the buffer,
7119 thus pretending to be `read' (except the characters we store
7120 in the keyboard buffer can be multibyte, so are not necessarily
7121 C chars).
7123 EXPECTED is nonzero if the caller knows input is available. */
7125 static int
7126 XTread_socket (terminal, expected, hold_quit)
7127 struct terminal *terminal;
7128 int expected;
7129 struct input_event *hold_quit;
7131 int count = 0;
7132 XEvent event;
7133 int event_found = 0;
7134 #if 0
7135 struct x_display_info *dpyinfo;
7136 #endif
7138 if (interrupt_input_blocked)
7140 interrupt_input_pending = 1;
7141 #ifdef SYNC_INPUT
7142 pending_signals = 1;
7143 #endif
7144 return -1;
7147 interrupt_input_pending = 0;
7148 #ifdef SYNC_INPUT
7149 pending_signals = pending_atimers;
7150 #endif
7151 BLOCK_INPUT;
7153 /* So people can tell when we have read the available input. */
7154 input_signal_count++;
7156 ++handling_signal;
7158 #ifdef HAVE_X_SM
7159 /* Only check session manager input for the primary display. */
7160 if (terminal->id == 1 && x_session_have_connection ())
7162 struct input_event inev;
7163 BLOCK_INPUT;
7164 /* We don't need to EVENT_INIT (inev) here, as
7165 x_session_check_input copies an entire input_event. */
7166 if (x_session_check_input (&inev))
7168 kbd_buffer_store_event_hold (&inev, hold_quit);
7169 count++;
7171 UNBLOCK_INPUT;
7173 #endif
7175 /* For debugging, this gives a way to fake an I/O error. */
7176 if (terminal->display_info.x == XTread_socket_fake_io_error)
7178 XTread_socket_fake_io_error = 0;
7179 x_io_error_quitter (terminal->display_info.x->display);
7182 #if 0 /* This loop is a noop now. */
7183 /* Find the display we are supposed to read input for.
7184 It's the one communicating on descriptor SD. */
7185 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7187 #if 0 /* This ought to be unnecessary; let's verify it. */
7188 #ifdef FIOSNBIO
7189 /* If available, Xlib uses FIOSNBIO to make the socket
7190 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7191 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7192 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7193 fcntl (dpyinfo->connection, F_SETFL, 0);
7194 #endif /* ! defined (FIOSNBIO) */
7195 #endif
7197 #if 0 /* This code can't be made to work, with multiple displays,
7198 and appears not to be used on any system any more.
7199 Also keyboard.c doesn't turn O_NDELAY on and off
7200 for X connections. */
7201 #ifndef SIGIO
7202 #ifndef HAVE_SELECT
7203 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7205 extern int read_alarm_should_throw;
7206 read_alarm_should_throw = 1;
7207 XPeekEvent (dpyinfo->display, &event);
7208 read_alarm_should_throw = 0;
7210 #endif /* HAVE_SELECT */
7211 #endif /* SIGIO */
7212 #endif
7214 #endif
7216 #ifndef USE_GTK
7217 while (XPending (terminal->display_info.x->display))
7219 int finish;
7221 XNextEvent (terminal->display_info.x->display, &event);
7223 #ifdef HAVE_X_I18N
7224 /* Filter events for the current X input method. */
7225 if (x_filter_event (terminal->display_info.x, &event))
7226 break;
7227 #endif
7228 event_found = 1;
7230 count += handle_one_xevent (terminal->display_info.x,
7231 &event, &finish, hold_quit);
7233 if (finish == X_EVENT_GOTO_OUT)
7234 goto out;
7237 #else /* USE_GTK */
7239 /* For GTK we must use the GTK event loop. But XEvents gets passed
7240 to our filter function above, and then to the big event switch.
7241 We use a bunch of globals to communicate with our filter function,
7242 that is kind of ugly, but it works.
7244 There is no way to do one display at the time, GTK just does events
7245 from all displays. */
7247 while (gtk_events_pending ())
7249 current_count = count;
7250 current_hold_quit = hold_quit;
7252 gtk_main_iteration ();
7254 count = current_count;
7255 current_count = -1;
7256 current_hold_quit = 0;
7258 if (current_finish == X_EVENT_GOTO_OUT)
7259 break;
7261 #endif /* USE_GTK */
7263 out:;
7265 /* On some systems, an X bug causes Emacs to get no more events
7266 when the window is destroyed. Detect that. (1994.) */
7267 if (! event_found)
7269 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7270 One XNOOP in 100 loops will make Emacs terminate.
7271 B. Bretthauer, 1994 */
7272 x_noop_count++;
7273 if (x_noop_count >= 100)
7275 x_noop_count=0;
7277 if (next_noop_dpyinfo == 0)
7278 next_noop_dpyinfo = x_display_list;
7280 XNoOp (next_noop_dpyinfo->display);
7282 /* Each time we get here, cycle through the displays now open. */
7283 next_noop_dpyinfo = next_noop_dpyinfo->next;
7287 /* If the focus was just given to an auto-raising frame,
7288 raise it now. */
7289 /* ??? This ought to be able to handle more than one such frame. */
7290 if (pending_autoraise_frame)
7292 x_raise_frame (pending_autoraise_frame);
7293 pending_autoraise_frame = 0;
7296 --handling_signal;
7297 UNBLOCK_INPUT;
7299 return count;
7305 /***********************************************************************
7306 Text Cursor
7307 ***********************************************************************/
7309 /* Set clipping for output in glyph row ROW. W is the window in which
7310 we operate. GC is the graphics context to set clipping in.
7312 ROW may be a text row or, e.g., a mode line. Text rows must be
7313 clipped to the interior of the window dedicated to text display,
7314 mode lines must be clipped to the whole window. */
7316 static void
7317 x_clip_to_row (w, row, area, gc)
7318 struct window *w;
7319 struct glyph_row *row;
7320 int area;
7321 GC gc;
7323 struct frame *f = XFRAME (WINDOW_FRAME (w));
7324 XRectangle clip_rect;
7325 int window_x, window_y, window_width;
7327 window_box (w, area, &window_x, &window_y, &window_width, 0);
7329 clip_rect.x = window_x;
7330 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7331 clip_rect.y = max (clip_rect.y, window_y);
7332 clip_rect.width = window_width;
7333 clip_rect.height = row->visible_height;
7335 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7339 /* Draw a hollow box cursor on window W in glyph row ROW. */
7341 static void
7342 x_draw_hollow_cursor (w, row)
7343 struct window *w;
7344 struct glyph_row *row;
7346 struct frame *f = XFRAME (WINDOW_FRAME (w));
7347 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7348 Display *dpy = FRAME_X_DISPLAY (f);
7349 int x, y, wd, h;
7350 XGCValues xgcv;
7351 struct glyph *cursor_glyph;
7352 GC gc;
7354 /* Get the glyph the cursor is on. If we can't tell because
7355 the current matrix is invalid or such, give up. */
7356 cursor_glyph = get_phys_cursor_glyph (w);
7357 if (cursor_glyph == NULL)
7358 return;
7360 /* Compute frame-relative coordinates for phys cursor. */
7361 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7362 wd = w->phys_cursor_width;
7364 /* The foreground of cursor_gc is typically the same as the normal
7365 background color, which can cause the cursor box to be invisible. */
7366 xgcv.foreground = f->output_data.x->cursor_pixel;
7367 if (dpyinfo->scratch_cursor_gc)
7368 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7369 else
7370 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7371 GCForeground, &xgcv);
7372 gc = dpyinfo->scratch_cursor_gc;
7374 /* Set clipping, draw the rectangle, and reset clipping again. */
7375 x_clip_to_row (w, row, TEXT_AREA, gc);
7376 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7377 XSetClipMask (dpy, gc, None);
7381 /* Draw a bar cursor on window W in glyph row ROW.
7383 Implementation note: One would like to draw a bar cursor with an
7384 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7385 Unfortunately, I didn't find a font yet that has this property set.
7386 --gerd. */
7388 static void
7389 x_draw_bar_cursor (w, row, width, kind)
7390 struct window *w;
7391 struct glyph_row *row;
7392 int width;
7393 enum text_cursor_kinds kind;
7395 struct frame *f = XFRAME (w->frame);
7396 struct glyph *cursor_glyph;
7398 /* If cursor is out of bounds, don't draw garbage. This can happen
7399 in mini-buffer windows when switching between echo area glyphs
7400 and mini-buffer. */
7401 cursor_glyph = get_phys_cursor_glyph (w);
7402 if (cursor_glyph == NULL)
7403 return;
7405 /* If on an image, draw like a normal cursor. That's usually better
7406 visible than drawing a bar, esp. if the image is large so that
7407 the bar might not be in the window. */
7408 if (cursor_glyph->type == IMAGE_GLYPH)
7410 struct glyph_row *row;
7411 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7412 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7414 else
7416 Display *dpy = FRAME_X_DISPLAY (f);
7417 Window window = FRAME_X_WINDOW (f);
7418 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7419 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7420 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7421 XGCValues xgcv;
7423 /* If the glyph's background equals the color we normally draw
7424 the bar cursor in, the bar cursor in its normal color is
7425 invisible. Use the glyph's foreground color instead in this
7426 case, on the assumption that the glyph's colors are chosen so
7427 that the glyph is legible. */
7428 if (face->background == f->output_data.x->cursor_pixel)
7429 xgcv.background = xgcv.foreground = face->foreground;
7430 else
7431 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7432 xgcv.graphics_exposures = 0;
7434 if (gc)
7435 XChangeGC (dpy, gc, mask, &xgcv);
7436 else
7438 gc = XCreateGC (dpy, window, mask, &xgcv);
7439 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7442 if (width < 0)
7443 width = FRAME_CURSOR_WIDTH (f);
7444 width = min (cursor_glyph->pixel_width, width);
7446 w->phys_cursor_width = width;
7447 x_clip_to_row (w, row, TEXT_AREA, gc);
7449 if (kind == BAR_CURSOR)
7450 XFillRectangle (dpy, window, gc,
7451 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7452 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7453 width, row->height);
7454 else
7455 XFillRectangle (dpy, window, gc,
7456 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7457 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7458 row->height - width),
7459 min (FRAME_COLUMN_WIDTH (f), cursor_glyph->pixel_width),
7460 width);
7462 XSetClipMask (dpy, gc, None);
7467 /* RIF: Define cursor CURSOR on frame F. */
7469 static void
7470 x_define_frame_cursor (f, cursor)
7471 struct frame *f;
7472 Cursor cursor;
7474 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7478 /* RIF: Clear area on frame F. */
7480 static void
7481 x_clear_frame_area (f, x, y, width, height)
7482 struct frame *f;
7483 int x, y, width, height;
7485 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7486 x, y, width, height, False);
7490 /* RIF: Draw cursor on window W. */
7492 static void
7493 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7494 struct window *w;
7495 struct glyph_row *glyph_row;
7496 int x, y;
7497 int cursor_type, cursor_width;
7498 int on_p, active_p;
7500 struct frame *f = XFRAME (WINDOW_FRAME (w));
7502 if (on_p)
7504 w->phys_cursor_type = cursor_type;
7505 w->phys_cursor_on_p = 1;
7507 if (glyph_row->exact_window_width_line_p
7508 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7510 glyph_row->cursor_in_fringe_p = 1;
7511 draw_fringe_bitmap (w, glyph_row, 0);
7513 else
7514 switch (cursor_type)
7516 case HOLLOW_BOX_CURSOR:
7517 x_draw_hollow_cursor (w, glyph_row);
7518 break;
7520 case FILLED_BOX_CURSOR:
7521 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7522 break;
7524 case BAR_CURSOR:
7525 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7526 break;
7528 case HBAR_CURSOR:
7529 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7530 break;
7532 case NO_CURSOR:
7533 w->phys_cursor_width = 0;
7534 break;
7536 default:
7537 abort ();
7540 #ifdef HAVE_X_I18N
7541 if (w == XWINDOW (f->selected_window))
7542 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7543 xic_set_preeditarea (w, x, y);
7544 #endif
7547 #ifndef XFlush
7548 XFlush (FRAME_X_DISPLAY (f));
7549 #endif
7553 /* Icons. */
7555 /* Make the x-window of frame F use the gnu icon bitmap. */
7558 x_bitmap_icon (f, file)
7559 struct frame *f;
7560 Lisp_Object file;
7562 int bitmap_id;
7564 if (FRAME_X_WINDOW (f) == 0)
7565 return 1;
7567 /* Free up our existing icon bitmap and mask if any. */
7568 if (f->output_data.x->icon_bitmap > 0)
7569 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7570 f->output_data.x->icon_bitmap = 0;
7572 if (STRINGP (file))
7574 #ifdef USE_GTK
7575 /* Use gtk_window_set_icon_from_file () if available,
7576 It's not restricted to bitmaps */
7577 if (xg_set_icon (f, file))
7578 return 0;
7579 #endif /* USE_GTK */
7580 bitmap_id = x_create_bitmap_from_file (f, file);
7581 x_create_bitmap_mask (f, bitmap_id);
7583 else
7585 /* Create the GNU bitmap and mask if necessary. */
7586 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7588 int rc = -1;
7590 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7591 #ifdef USE_GTK
7592 if (xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7593 return 0;
7594 #else
7595 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7596 if (rc != -1)
7597 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7598 #endif /* USE_GTK */
7599 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
7601 /* If all else fails, use the (black and white) xbm image. */
7602 if (rc == -1)
7604 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7605 gnu_xbm_width, gnu_xbm_height);
7606 if (rc == -1)
7607 return 1;
7609 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7610 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7614 /* The first time we create the GNU bitmap and mask,
7615 this increments the ref-count one extra time.
7616 As a result, the GNU bitmap and mask are never freed.
7617 That way, we don't have to worry about allocating it again. */
7618 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7620 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7623 x_wm_set_icon_pixmap (f, bitmap_id);
7624 f->output_data.x->icon_bitmap = bitmap_id;
7626 return 0;
7630 /* Make the x-window of frame F use a rectangle with text.
7631 Use ICON_NAME as the text. */
7634 x_text_icon (f, icon_name)
7635 struct frame *f;
7636 char *icon_name;
7638 if (FRAME_X_WINDOW (f) == 0)
7639 return 1;
7642 XTextProperty text;
7643 text.value = (unsigned char *) icon_name;
7644 text.encoding = XA_STRING;
7645 text.format = 8;
7646 text.nitems = strlen (icon_name);
7647 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7650 if (f->output_data.x->icon_bitmap > 0)
7651 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7652 f->output_data.x->icon_bitmap = 0;
7653 x_wm_set_icon_pixmap (f, 0);
7655 return 0;
7658 #define X_ERROR_MESSAGE_SIZE 200
7660 /* If non-nil, this should be a string.
7661 It means catch X errors and store the error message in this string.
7663 The reason we use a stack is that x_catch_error/x_uncatch_error can
7664 be called from a signal handler.
7667 struct x_error_message_stack {
7668 char string[X_ERROR_MESSAGE_SIZE];
7669 Display *dpy;
7670 struct x_error_message_stack *prev;
7672 static struct x_error_message_stack *x_error_message;
7674 /* An X error handler which stores the error message in
7675 *x_error_message. This is called from x_error_handler if
7676 x_catch_errors is in effect. */
7678 static void
7679 x_error_catcher (display, error)
7680 Display *display;
7681 XErrorEvent *error;
7683 XGetErrorText (display, error->error_code,
7684 x_error_message->string,
7685 X_ERROR_MESSAGE_SIZE);
7688 /* Begin trapping X errors for display DPY. Actually we trap X errors
7689 for all displays, but DPY should be the display you are actually
7690 operating on.
7692 After calling this function, X protocol errors no longer cause
7693 Emacs to exit; instead, they are recorded in the string
7694 stored in *x_error_message.
7696 Calling x_check_errors signals an Emacs error if an X error has
7697 occurred since the last call to x_catch_errors or x_check_errors.
7699 Calling x_uncatch_errors resumes the normal error handling. */
7701 void x_check_errors ();
7703 void
7704 x_catch_errors (dpy)
7705 Display *dpy;
7707 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7709 /* Make sure any errors from previous requests have been dealt with. */
7710 XSync (dpy, False);
7712 data->dpy = dpy;
7713 data->string[0] = 0;
7714 data->prev = x_error_message;
7715 x_error_message = data;
7718 /* Undo the last x_catch_errors call.
7719 DPY should be the display that was passed to x_catch_errors. */
7721 void
7722 x_uncatch_errors ()
7724 struct x_error_message_stack *tmp;
7726 BLOCK_INPUT;
7728 /* The display may have been closed before this function is called.
7729 Check if it is still open before calling XSync. */
7730 if (x_display_info_for_display (x_error_message->dpy) != 0)
7731 XSync (x_error_message->dpy, False);
7733 tmp = x_error_message;
7734 x_error_message = x_error_message->prev;
7735 xfree (tmp);
7736 UNBLOCK_INPUT;
7739 /* If any X protocol errors have arrived since the last call to
7740 x_catch_errors or x_check_errors, signal an Emacs error using
7741 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7743 void
7744 x_check_errors (dpy, format)
7745 Display *dpy;
7746 char *format;
7748 /* Make sure to catch any errors incurred so far. */
7749 XSync (dpy, False);
7751 if (x_error_message->string[0])
7753 char string[X_ERROR_MESSAGE_SIZE];
7754 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7755 x_uncatch_errors ();
7756 error (format, string);
7760 /* Nonzero if we had any X protocol errors
7761 since we did x_catch_errors on DPY. */
7764 x_had_errors_p (dpy)
7765 Display *dpy;
7767 /* Make sure to catch any errors incurred so far. */
7768 XSync (dpy, False);
7770 return x_error_message->string[0] != 0;
7773 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7775 void
7776 x_clear_errors (dpy)
7777 Display *dpy;
7779 x_error_message->string[0] = 0;
7782 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7783 * idea. --lorentey */
7784 /* Close off all unclosed x_catch_errors calls. */
7786 void
7787 x_fully_uncatch_errors ()
7789 while (x_error_message)
7790 x_uncatch_errors ();
7792 #endif
7794 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7797 x_catching_errors ()
7799 return x_error_message != 0;
7802 #if 0
7803 static unsigned int x_wire_count;
7804 x_trace_wire ()
7806 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7808 #endif /* ! 0 */
7811 /* Handle SIGPIPE, which can happen when the connection to a server
7812 simply goes away. SIGPIPE is handled by x_connection_signal.
7813 Don't need to do anything, because the write which caused the
7814 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7815 which will do the appropriate cleanup for us. */
7817 static SIGTYPE
7818 x_connection_signal (signalnum) /* If we don't have an argument, */
7819 int signalnum; /* some compilers complain in signal calls. */
7821 #ifdef USG
7822 /* USG systems forget handlers when they are used;
7823 must reestablish each time */
7824 signal (signalnum, x_connection_signal);
7825 #endif /* USG */
7829 /************************************************************************
7830 Handling X errors
7831 ************************************************************************/
7833 /* Error message passed to x_connection_closed. */
7835 static char *error_msg;
7837 /* Function installed as fatal_error_signal_hook in
7838 x_connection_closed. Print the X error message, and exit normally,
7839 instead of dumping core when XtCloseDisplay fails. */
7841 static void
7842 x_fatal_error_signal ()
7844 fprintf (stderr, "%s\n", error_msg);
7845 exit (70);
7848 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7849 the text of an error message that lead to the connection loss. */
7851 static SIGTYPE
7852 x_connection_closed (dpy, error_message)
7853 Display *dpy;
7854 char *error_message;
7856 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7857 Lisp_Object frame, tail;
7858 int index = SPECPDL_INDEX ();
7860 error_msg = (char *) alloca (strlen (error_message) + 1);
7861 strcpy (error_msg, error_message);
7862 handling_signal = 0;
7864 /* Prevent being called recursively because of an error condition
7865 below. Otherwise, we might end up with printing ``can't find per
7866 display information'' in the recursive call instead of printing
7867 the original message here. */
7868 x_catch_errors (dpy);
7870 /* Inhibit redisplay while frames are being deleted. */
7871 specbind (Qinhibit_redisplay, Qt);
7873 if (dpyinfo)
7875 /* Protect display from being closed when we delete the last
7876 frame on it. */
7877 dpyinfo->reference_count++;
7878 dpyinfo->terminal->reference_count++;
7881 /* First delete frames whose mini-buffers are on frames
7882 that are on the dead display. */
7883 FOR_EACH_FRAME (tail, frame)
7885 Lisp_Object minibuf_frame;
7886 minibuf_frame
7887 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7888 if (FRAME_X_P (XFRAME (frame))
7889 && FRAME_X_P (XFRAME (minibuf_frame))
7890 && ! EQ (frame, minibuf_frame)
7891 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7892 delete_frame (frame, Qnoelisp);
7895 /* Now delete all remaining frames on the dead display.
7896 We are now sure none of these is used as the mini-buffer
7897 for another frame that we need to delete. */
7898 FOR_EACH_FRAME (tail, frame)
7899 if (FRAME_X_P (XFRAME (frame))
7900 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7902 /* Set this to t so that delete_frame won't get confused
7903 trying to find a replacement. */
7904 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7905 delete_frame (frame, Qnoelisp);
7908 /* We have to close the display to inform Xt that it doesn't
7909 exist anymore. If we don't, Xt will continue to wait for
7910 events from the display. As a consequence, a sequence of
7912 M-x make-frame-on-display RET :1 RET
7913 ...kill the new frame, so that we get an IO error...
7914 M-x make-frame-on-display RET :1 RET
7916 will indefinitely wait in Xt for events for display `:1', opened
7917 in the first call to make-frame-on-display.
7919 Closing the display is reported to lead to a bus error on
7920 OpenWindows in certain situations. I suspect that is a bug
7921 in OpenWindows. I don't know how to circumvent it here. */
7923 if (dpyinfo)
7925 #ifdef USE_X_TOOLKIT
7926 /* If DPYINFO is null, this means we didn't open the display
7927 in the first place, so don't try to close it. */
7929 extern void (*fatal_error_signal_hook) P_ ((void));
7930 fatal_error_signal_hook = x_fatal_error_signal;
7931 XtCloseDisplay (dpy);
7932 fatal_error_signal_hook = NULL;
7934 #endif
7936 #ifdef USE_GTK
7937 /* Due to bugs in some Gtk+ versions, just exit here if this
7938 is the last display/terminal. */
7939 if (terminal_list->next_terminal == NULL)
7941 fprintf (stderr, "%s\n", error_msg);
7942 shut_down_emacs (0, 0, Qnil);
7943 exit (70);
7945 xg_display_close (dpyinfo->display);
7946 #endif
7948 /* Indicate that this display is dead. */
7949 dpyinfo->display = 0;
7951 dpyinfo->reference_count--;
7952 dpyinfo->terminal->reference_count--;
7953 if (dpyinfo->reference_count != 0)
7954 /* We have just closed all frames on this display. */
7955 abort ();
7958 Lisp_Object tmp;
7959 XSETTERMINAL (tmp, dpyinfo->terminal);
7960 Fdelete_terminal (tmp, Qnoelisp);
7964 x_uncatch_errors ();
7966 if (terminal_list == 0)
7968 fprintf (stderr, "%s\n", error_msg);
7969 shut_down_emacs (0, 0, Qnil);
7970 exit (70);
7973 /* Ordinary stack unwind doesn't deal with these. */
7974 #ifdef SIGIO
7975 sigunblock (sigmask (SIGIO));
7976 #endif
7977 sigunblock (sigmask (SIGALRM));
7978 TOTALLY_UNBLOCK_INPUT;
7980 unbind_to (index, Qnil);
7981 clear_waiting_for_input ();
7982 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7983 longjmp), because returning from this function would get us back into
7984 Xlib's code which will directly call `exit'. */
7985 error ("%s", error_msg);
7988 /* We specifically use it before defining it, so that gcc doesn't inline it,
7989 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7990 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7992 /* This is the first-level handler for X protocol errors.
7993 It calls x_error_quitter or x_error_catcher. */
7995 static int
7996 x_error_handler (display, error)
7997 Display *display;
7998 XErrorEvent *error;
8000 if (x_error_message)
8001 x_error_catcher (display, error);
8002 else
8003 x_error_quitter (display, error);
8004 return 0;
8007 /* This is the usual handler for X protocol errors.
8008 It kills all frames on the display that we got the error for.
8009 If that was the only one, it prints an error message and kills Emacs. */
8011 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
8013 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
8014 #define NO_INLINE __attribute__((noinline))
8015 #else
8016 #define NO_INLINE
8017 #endif
8019 /* Some versions of GNU/Linux define noinline in their headers. */
8021 #ifdef noinline
8022 #undef noinline
8023 #endif
8025 /* On older GCC versions, just putting x_error_quitter
8026 after x_error_handler prevents inlining into the former. */
8028 static void NO_INLINE
8029 x_error_quitter (display, error)
8030 Display *display;
8031 XErrorEvent *error;
8033 char buf[256], buf1[356];
8035 /* Ignore BadName errors. They can happen because of fonts
8036 or colors that are not defined. */
8038 if (error->error_code == BadName)
8039 return;
8041 /* Note that there is no real way portable across R3/R4 to get the
8042 original error handler. */
8044 XGetErrorText (display, error->error_code, buf, sizeof (buf));
8045 sprintf (buf1, "X protocol error: %s on protocol request %d",
8046 buf, error->request_code);
8047 x_connection_closed (display, buf1);
8051 /* This is the handler for X IO errors, always.
8052 It kills all frames on the display that we lost touch with.
8053 If that was the only one, it prints an error message and kills Emacs. */
8055 static int
8056 x_io_error_quitter (display)
8057 Display *display;
8059 char buf[256];
8061 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
8062 x_connection_closed (display, buf);
8063 return 0;
8066 /* Changing the font of the frame. */
8068 /* Give frame F the font FONT-OBJECT as its default font. The return
8069 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
8070 frame. If it is negative, generate a new fontset from
8071 FONT-OBJECT. */
8073 Lisp_Object
8074 x_new_font (f, font_object, fontset)
8075 struct frame *f;
8076 Lisp_Object font_object;
8077 int fontset;
8079 struct font *font = XFONT_OBJECT (font_object);
8081 if (fontset < 0)
8082 fontset = fontset_from_font (font_object);
8083 FRAME_FONTSET (f) = fontset;
8084 if (FRAME_FONT (f) == font)
8085 /* This font is already set in frame F. There's nothing more to
8086 do. */
8087 return font_object;
8089 FRAME_FONT (f) = font;
8090 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8091 FRAME_COLUMN_WIDTH (f) = font->average_width;
8092 FRAME_SPACE_WIDTH (f) = font->space_width;
8093 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8095 compute_fringe_widths (f, 1);
8097 /* Compute the scroll bar width in character columns. */
8098 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8100 int wid = FRAME_COLUMN_WIDTH (f);
8101 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8102 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8104 else
8106 int wid = FRAME_COLUMN_WIDTH (f);
8107 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8110 if (FRAME_X_WINDOW (f) != 0)
8112 /* Don't change the size of a tip frame; there's no point in
8113 doing it because it's done in Fx_show_tip, and it leads to
8114 problems because the tip frame has no widget. */
8115 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8116 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8119 #ifdef HAVE_X_I18N
8120 if (FRAME_XIC (f)
8121 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8123 BLOCK_INPUT;
8124 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8125 UNBLOCK_INPUT;
8127 #endif
8129 return font_object;
8133 /***********************************************************************
8134 X Input Methods
8135 ***********************************************************************/
8137 #ifdef HAVE_X_I18N
8139 #ifdef HAVE_X11R6
8141 /* XIM destroy callback function, which is called whenever the
8142 connection to input method XIM dies. CLIENT_DATA contains a
8143 pointer to the x_display_info structure corresponding to XIM. */
8145 static void
8146 xim_destroy_callback (xim, client_data, call_data)
8147 XIM xim;
8148 XPointer client_data;
8149 XPointer call_data;
8151 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8152 Lisp_Object frame, tail;
8154 BLOCK_INPUT;
8156 /* No need to call XDestroyIC.. */
8157 FOR_EACH_FRAME (tail, frame)
8159 struct frame *f = XFRAME (frame);
8160 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8162 FRAME_XIC (f) = NULL;
8163 xic_free_xfontset (f);
8167 /* No need to call XCloseIM. */
8168 dpyinfo->xim = NULL;
8169 XFree (dpyinfo->xim_styles);
8170 UNBLOCK_INPUT;
8173 #endif /* HAVE_X11R6 */
8175 #ifdef HAVE_X11R6
8176 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8177 extern char *XSetIMValues P_ ((XIM, ...));
8178 #endif
8180 /* Open the connection to the XIM server on display DPYINFO.
8181 RESOURCE_NAME is the resource name Emacs uses. */
8183 static void
8184 xim_open_dpy (dpyinfo, resource_name)
8185 struct x_display_info *dpyinfo;
8186 char *resource_name;
8188 XIM xim;
8190 #ifdef HAVE_XIM
8191 if (use_xim)
8193 if (dpyinfo->xim)
8194 XCloseIM (dpyinfo->xim);
8195 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8196 EMACS_CLASS);
8197 dpyinfo->xim = xim;
8199 if (xim)
8201 #ifdef HAVE_X11R6
8202 XIMCallback destroy;
8203 #endif
8205 /* Get supported styles and XIM values. */
8206 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8208 #ifdef HAVE_X11R6
8209 destroy.callback = xim_destroy_callback;
8210 destroy.client_data = (XPointer)dpyinfo;
8211 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8212 #endif
8216 else
8217 #endif /* HAVE_XIM */
8218 dpyinfo->xim = NULL;
8222 #ifdef HAVE_X11R6_XIM
8224 /* XIM instantiate callback function, which is called whenever an XIM
8225 server is available. DISPLAY is the display of the XIM.
8226 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8227 when the callback was registered. */
8229 static void
8230 xim_instantiate_callback (display, client_data, call_data)
8231 Display *display;
8232 XPointer client_data;
8233 XPointer call_data;
8235 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8236 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8238 /* We don't support multiple XIM connections. */
8239 if (dpyinfo->xim)
8240 return;
8242 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8244 /* Create XIC for the existing frames on the same display, as long
8245 as they have no XIC. */
8246 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8248 Lisp_Object tail, frame;
8250 BLOCK_INPUT;
8251 FOR_EACH_FRAME (tail, frame)
8253 struct frame *f = XFRAME (frame);
8255 if (FRAME_X_P (f)
8256 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8257 if (FRAME_XIC (f) == NULL)
8259 create_frame_xic (f);
8260 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8261 xic_set_statusarea (f);
8262 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8264 struct window *w = XWINDOW (f->selected_window);
8265 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8270 UNBLOCK_INPUT;
8274 #endif /* HAVE_X11R6_XIM */
8277 /* Open a connection to the XIM server on display DPYINFO.
8278 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8279 connection only at the first time. On X11R6, open the connection
8280 in the XIM instantiate callback function. */
8282 static void
8283 xim_initialize (dpyinfo, resource_name)
8284 struct x_display_info *dpyinfo;
8285 char *resource_name;
8287 dpyinfo->xim = NULL;
8288 #ifdef HAVE_XIM
8289 if (use_xim)
8291 #ifdef HAVE_X11R6_XIM
8292 struct xim_inst_t *xim_inst;
8293 int len;
8295 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8296 dpyinfo->xim_callback_data = xim_inst;
8297 xim_inst->dpyinfo = dpyinfo;
8298 len = strlen (resource_name);
8299 xim_inst->resource_name = (char *) xmalloc (len + 1);
8300 bcopy (resource_name, xim_inst->resource_name, len + 1);
8301 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8302 resource_name, EMACS_CLASS,
8303 xim_instantiate_callback,
8304 /* This is XPointer in XFree86
8305 but (XPointer *) on Tru64, at
8306 least, hence the configure test. */
8307 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8308 #else /* not HAVE_X11R6_XIM */
8309 xim_open_dpy (dpyinfo, resource_name);
8310 #endif /* not HAVE_X11R6_XIM */
8312 #endif /* HAVE_XIM */
8316 /* Close the connection to the XIM server on display DPYINFO. */
8318 static void
8319 xim_close_dpy (dpyinfo)
8320 struct x_display_info *dpyinfo;
8322 #ifdef HAVE_XIM
8323 if (use_xim)
8325 #ifdef HAVE_X11R6_XIM
8326 if (dpyinfo->display)
8327 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8328 NULL, EMACS_CLASS,
8329 xim_instantiate_callback, NULL);
8330 xfree (dpyinfo->xim_callback_data->resource_name);
8331 xfree (dpyinfo->xim_callback_data);
8332 #endif /* HAVE_X11R6_XIM */
8333 if (dpyinfo->display)
8334 XCloseIM (dpyinfo->xim);
8335 dpyinfo->xim = NULL;
8336 XFree (dpyinfo->xim_styles);
8338 #endif /* HAVE_XIM */
8341 #endif /* not HAVE_X11R6_XIM */
8345 /* Calculate the absolute position in frame F
8346 from its current recorded position values and gravity. */
8348 void
8349 x_calc_absolute_position (f)
8350 struct frame *f;
8352 int flags = f->size_hint_flags;
8354 /* We have nothing to do if the current position
8355 is already for the top-left corner. */
8356 if (! ((flags & XNegative) || (flags & YNegative)))
8357 return;
8359 /* Treat negative positions as relative to the leftmost bottommost
8360 position that fits on the screen. */
8361 if (flags & XNegative)
8362 f->left_pos = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
8363 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8366 int height = FRAME_PIXEL_HEIGHT (f);
8368 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8369 /* Something is fishy here. When using Motif, starting Emacs with
8370 `-g -0-0', the frame appears too low by a few pixels.
8372 This seems to be so because initially, while Emacs is starting,
8373 the column widget's height and the frame's pixel height are
8374 different. The column widget's height is the right one. In
8375 later invocations, when Emacs is up, the frame's pixel height
8376 is right, though.
8378 It's not obvious where the initial small difference comes from.
8379 2000-12-01, gerd. */
8381 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8382 #endif
8384 if (flags & YNegative)
8385 f->top_pos = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
8386 - height + f->top_pos;
8389 /* The left_pos and top_pos
8390 are now relative to the top and left screen edges,
8391 so the flags should correspond. */
8392 f->size_hint_flags &= ~ (XNegative | YNegative);
8395 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8396 to really change the position, and 0 when calling from
8397 x_make_frame_visible (in that case, XOFF and YOFF are the current
8398 position values). It is -1 when calling from x_set_frame_parameters,
8399 which means, do adjust for borders but don't change the gravity. */
8401 void
8402 x_set_offset (f, xoff, yoff, change_gravity)
8403 struct frame *f;
8404 register int xoff, yoff;
8405 int change_gravity;
8407 int modified_top, modified_left;
8409 if (change_gravity > 0)
8411 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8412 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8414 f->top_pos = yoff;
8415 f->left_pos = xoff;
8416 f->size_hint_flags &= ~ (XNegative | YNegative);
8417 if (xoff < 0)
8418 f->size_hint_flags |= XNegative;
8419 if (yoff < 0)
8420 f->size_hint_flags |= YNegative;
8421 f->win_gravity = NorthWestGravity;
8423 x_calc_absolute_position (f);
8425 BLOCK_INPUT;
8426 x_wm_set_size_hint (f, (long) 0, 0);
8428 modified_left = f->left_pos;
8429 modified_top = f->top_pos;
8431 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8433 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8434 than the WM decorations. So we use the calculated offset instead
8435 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8436 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8437 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8440 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8441 modified_left, modified_top);
8443 x_sync_with_move (f, f->left_pos, f->top_pos,
8444 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8445 ? 1 : 0);
8447 /* change_gravity is non-zero when this function is called from Lisp to
8448 programmatically move a frame. In that case, we call
8449 x_check_expected_move to discover if we have a "Type A" or "Type B"
8450 window manager, and, for a "Type A" window manager, adjust the position
8451 of the frame.
8453 We call x_check_expected_move if a programmatic move occurred, and
8454 either the window manager type (A/B) is unknown or it is Type A but we
8455 need to compute the top/left offset adjustment for this frame. */
8457 if (change_gravity != 0 &&
8458 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8459 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8460 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8461 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8462 x_check_expected_move (f, modified_left, modified_top);
8464 UNBLOCK_INPUT;
8467 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8468 on the root window for frame F contains ATOMNAME.
8469 This is how a WM check shall be done according to the Window Manager
8470 Specification/Extended Window Manager Hints at
8471 http://freedesktop.org/wiki/Specifications/wm-spec. */
8473 static int
8474 wm_supports (f, atomname)
8475 struct frame *f;
8476 const char *atomname;
8478 Atom actual_type;
8479 unsigned long actual_size, bytes_remaining;
8480 int i, rc, actual_format;
8481 Atom prop_atom;
8482 Window wmcheck_window;
8483 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8484 Window target_window = dpyinfo->root_window;
8485 long max_len = 65536;
8486 Display *dpy = FRAME_X_DISPLAY (f);
8487 unsigned char *tmp_data = NULL;
8488 Atom target_type = XA_WINDOW;
8489 Atom want_atom;
8491 BLOCK_INPUT;
8493 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8495 x_catch_errors (dpy);
8496 rc = XGetWindowProperty (dpy, target_window,
8497 prop_atom, 0, max_len, False, target_type,
8498 &actual_type, &actual_format, &actual_size,
8499 &bytes_remaining, &tmp_data);
8501 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8503 if (tmp_data) XFree (tmp_data);
8504 x_uncatch_errors ();
8505 UNBLOCK_INPUT;
8506 return 0;
8509 wmcheck_window = *(Window *) tmp_data;
8510 XFree (tmp_data);
8512 /* Check if window exists. */
8513 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8514 x_sync (f);
8515 if (x_had_errors_p (dpy))
8517 x_uncatch_errors ();
8518 UNBLOCK_INPUT;
8519 return 0;
8522 if (dpyinfo->net_supported_window != wmcheck_window)
8524 /* Window changed, reload atoms */
8525 if (dpyinfo->net_supported_atoms != NULL)
8526 XFree (dpyinfo->net_supported_atoms);
8527 dpyinfo->net_supported_atoms = NULL;
8528 dpyinfo->nr_net_supported_atoms = 0;
8529 dpyinfo->net_supported_window = 0;
8531 target_type = XA_ATOM;
8532 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8533 tmp_data = NULL;
8534 rc = XGetWindowProperty (dpy, target_window,
8535 prop_atom, 0, max_len, False, target_type,
8536 &actual_type, &actual_format, &actual_size,
8537 &bytes_remaining, &tmp_data);
8539 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8541 if (tmp_data) XFree (tmp_data);
8542 x_uncatch_errors ();
8543 UNBLOCK_INPUT;
8544 return 0;
8547 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8548 dpyinfo->nr_net_supported_atoms = actual_size;
8549 dpyinfo->net_supported_window = wmcheck_window;
8552 rc = 0;
8553 want_atom = XInternAtom (dpy, atomname, False);
8555 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8556 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8558 x_uncatch_errors ();
8559 UNBLOCK_INPUT;
8561 return rc;
8564 /* Do fullscreen as specified in extended window manager hints */
8566 static int
8567 do_ewmh_fullscreen (f)
8568 struct frame *f;
8570 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8572 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8573 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8574 if (!have_net_atom)
8575 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8577 if (have_net_atom)
8579 Lisp_Object frame;
8580 const char *atom = "_NET_WM_STATE";
8581 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8582 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8583 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8584 const char *what = NULL;
8586 XSETFRAME (frame, f);
8588 /* If there are _NET_ atoms we assume we have extended window manager
8589 hints. */
8590 switch (f->want_fullscreen)
8592 case FULLSCREEN_BOTH:
8593 what = fs;
8594 break;
8595 case FULLSCREEN_WIDTH:
8596 what = fw;
8597 break;
8598 case FULLSCREEN_HEIGHT:
8599 what = fh;
8600 break;
8603 if (what != NULL && !wm_supports (f, what)) return 0;
8606 Fx_send_client_event (frame, make_number (0), frame,
8607 make_unibyte_string (atom, strlen (atom)),
8608 make_number (32),
8609 Fcons (make_number (0), /* Remove */
8610 Fcons
8611 (make_unibyte_string (fs,
8612 strlen (fs)),
8613 Qnil)));
8614 Fx_send_client_event (frame, make_number (0), frame,
8615 make_unibyte_string (atom, strlen (atom)),
8616 make_number (32),
8617 Fcons (make_number (0), /* Remove */
8618 Fcons
8619 (make_unibyte_string (fh,
8620 strlen (fh)),
8621 Qnil)));
8622 Fx_send_client_event (frame, make_number (0), frame,
8623 make_unibyte_string (atom, strlen (atom)),
8624 make_number (32),
8625 Fcons (make_number (0), /* Remove */
8626 Fcons
8627 (make_unibyte_string (fw,
8628 strlen (fw)),
8629 Qnil)));
8630 f->want_fullscreen = FULLSCREEN_NONE;
8631 if (what != NULL)
8632 Fx_send_client_event (frame, make_number (0), frame,
8633 make_unibyte_string (atom, strlen (atom)),
8634 make_number (32),
8635 Fcons (make_number (1), /* Add */
8636 Fcons
8637 (make_unibyte_string (what,
8638 strlen (what)),
8639 Qnil)));
8642 return have_net_atom;
8645 static void
8646 XTfullscreen_hook (f)
8647 FRAME_PTR f;
8649 if (f->async_visible)
8651 BLOCK_INPUT;
8652 do_ewmh_fullscreen (f);
8653 x_sync (f);
8654 UNBLOCK_INPUT;
8659 /* Check if we need to resize the frame due to a fullscreen request.
8660 If so needed, resize the frame. */
8661 static void
8662 x_check_fullscreen (f)
8663 struct frame *f;
8665 if (f->want_fullscreen & FULLSCREEN_BOTH)
8667 int width, height, ign;
8669 if (do_ewmh_fullscreen (f))
8670 return;
8672 x_real_positions (f, &f->left_pos, &f->top_pos);
8674 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8676 /* We do not need to move the window, it shall be taken care of
8677 when setting WM manager hints.
8678 If the frame is visible already, the position is checked by
8679 x_check_expected_move. */
8680 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8682 change_frame_size (f, height, width, 0, 1, 0);
8683 SET_FRAME_GARBAGED (f);
8684 cancel_mouse_face (f);
8686 /* Wait for the change of frame size to occur */
8687 f->want_fullscreen |= FULLSCREEN_WAIT;
8692 /* This function is called by x_set_offset to determine whether the window
8693 manager interfered with the positioning of the frame. Type A window
8694 managers position the surrounding window manager decorations a small
8695 amount above and left of the user-supplied position. Type B window
8696 managers position the surrounding window manager decorations at the
8697 user-specified position. If we detect a Type A window manager, we
8698 compensate by moving the window right and down by the proper amount. */
8700 static void
8701 x_check_expected_move (f, expected_left, expected_top)
8702 struct frame *f;
8703 int expected_left;
8704 int expected_top;
8706 int current_left = 0, current_top = 0;
8708 /* x_real_positions returns the left and top offsets of the outermost
8709 window manager window around the frame. */
8711 x_real_positions (f, &current_left, &current_top);
8713 if (current_left != expected_left || current_top != expected_top)
8715 /* It's a "Type A" window manager. */
8717 int adjusted_left;
8718 int adjusted_top;
8720 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8721 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8722 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8724 /* Now fix the mispositioned frame's location. */
8726 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8727 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8729 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8730 adjusted_left, adjusted_top);
8732 x_sync_with_move (f, expected_left, expected_top, 0);
8734 else
8735 /* It's a "Type B" window manager. We don't have to adjust the
8736 frame's position. */
8738 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8742 /* Wait for XGetGeometry to return up-to-date position information for a
8743 recently-moved frame. Call this immediately after calling XMoveWindow.
8744 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8745 frame has been moved to, so we use a fuzzy position comparison instead
8746 of an exact comparison. */
8748 static void
8749 x_sync_with_move (f, left, top, fuzzy)
8750 struct frame *f;
8751 int left, top, fuzzy;
8753 int count = 0;
8755 while (count++ < 50)
8757 int current_left = 0, current_top = 0;
8759 /* In theory, this call to XSync only needs to happen once, but in
8760 practice, it doesn't seem to work, hence the need for the surrounding
8761 loop. */
8763 XSync (FRAME_X_DISPLAY (f), False);
8764 x_real_positions (f, &current_left, &current_top);
8766 if (fuzzy)
8768 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8769 pixels. */
8771 if (eabs (current_left - left) <= 10
8772 && eabs (current_top - top) <= 40)
8773 return;
8775 else if (current_left == left && current_top == top)
8776 return;
8779 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8780 will then return up-to-date position info. */
8782 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8786 /* Change the size of frame F's X window to COLS/ROWS in the case F
8787 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8788 top-left-corner window gravity for this size change and subsequent
8789 size changes. Otherwise we leave the window gravity unchanged. */
8791 static void
8792 x_set_window_size_1 (f, change_gravity, cols, rows)
8793 struct frame *f;
8794 int change_gravity;
8795 int cols, rows;
8797 int pixelwidth, pixelheight;
8799 check_frame_size (f, &rows, &cols);
8800 f->scroll_bar_actual_width
8801 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8803 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8804 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8805 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8807 compute_fringe_widths (f, 0);
8809 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8810 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8812 f->win_gravity = NorthWestGravity;
8813 x_wm_set_size_hint (f, (long) 0, 0);
8815 XSync (FRAME_X_DISPLAY (f), False);
8816 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8817 pixelwidth, pixelheight);
8819 /* Now, strictly speaking, we can't be sure that this is accurate,
8820 but the window manager will get around to dealing with the size
8821 change request eventually, and we'll hear how it went when the
8822 ConfigureNotify event gets here.
8824 We could just not bother storing any of this information here,
8825 and let the ConfigureNotify event set everything up, but that
8826 might be kind of confusing to the Lisp code, since size changes
8827 wouldn't be reported in the frame parameters until some random
8828 point in the future when the ConfigureNotify event arrives.
8830 We pass 1 for DELAY since we can't run Lisp code inside of
8831 a BLOCK_INPUT. */
8832 change_frame_size (f, rows, cols, 0, 1, 0);
8833 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8834 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8836 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8837 receive in the ConfigureNotify event; if we get what we asked
8838 for, then the event won't cause the screen to become garbaged, so
8839 we have to make sure to do it here. */
8840 SET_FRAME_GARBAGED (f);
8842 XFlush (FRAME_X_DISPLAY (f));
8846 /* Call this to change the size of frame F's x-window.
8847 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8848 for this size change and subsequent size changes.
8849 Otherwise we leave the window gravity unchanged. */
8851 void
8852 x_set_window_size (f, change_gravity, cols, rows)
8853 struct frame *f;
8854 int change_gravity;
8855 int cols, rows;
8857 BLOCK_INPUT;
8859 #ifdef USE_GTK
8860 if (FRAME_GTK_WIDGET (f))
8861 xg_frame_set_char_size (f, cols, rows);
8862 else
8863 x_set_window_size_1 (f, change_gravity, cols, rows);
8864 #elif USE_X_TOOLKIT
8866 if (f->output_data.x->widget != NULL)
8868 /* The x and y position of the widget is clobbered by the
8869 call to XtSetValues within EmacsFrameSetCharSize.
8870 This is a real kludge, but I don't understand Xt so I can't
8871 figure out a correct fix. Can anyone else tell me? -- rms. */
8872 int xpos = f->output_data.x->widget->core.x;
8873 int ypos = f->output_data.x->widget->core.y;
8874 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8875 f->output_data.x->widget->core.x = xpos;
8876 f->output_data.x->widget->core.y = ypos;
8878 else
8879 x_set_window_size_1 (f, change_gravity, cols, rows);
8881 #else /* not USE_X_TOOLKIT */
8883 x_set_window_size_1 (f, change_gravity, cols, rows);
8885 #endif /* not USE_X_TOOLKIT */
8887 /* If cursor was outside the new size, mark it as off. */
8888 mark_window_cursors_off (XWINDOW (f->root_window));
8890 /* Clear out any recollection of where the mouse highlighting was,
8891 since it might be in a place that's outside the new frame size.
8892 Actually checking whether it is outside is a pain in the neck,
8893 so don't try--just let the highlighting be done afresh with new size. */
8894 cancel_mouse_face (f);
8896 UNBLOCK_INPUT;
8899 /* Mouse warping. */
8901 void
8902 x_set_mouse_position (f, x, y)
8903 struct frame *f;
8904 int x, y;
8906 int pix_x, pix_y;
8908 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8909 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8911 if (pix_x < 0) pix_x = 0;
8912 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8914 if (pix_y < 0) pix_y = 0;
8915 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8917 BLOCK_INPUT;
8919 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8920 0, 0, 0, 0, pix_x, pix_y);
8921 UNBLOCK_INPUT;
8924 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8926 void
8927 x_set_mouse_pixel_position (f, pix_x, pix_y)
8928 struct frame *f;
8929 int pix_x, pix_y;
8931 BLOCK_INPUT;
8933 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8934 0, 0, 0, 0, pix_x, pix_y);
8935 UNBLOCK_INPUT;
8938 /* focus shifting, raising and lowering. */
8940 void
8941 x_focus_on_frame (f)
8942 struct frame *f;
8944 #if 0 /* This proves to be unpleasant. */
8945 x_raise_frame (f);
8946 #endif
8947 #if 0
8948 /* I don't think that the ICCCM allows programs to do things like this
8949 without the interaction of the window manager. Whatever you end up
8950 doing with this code, do it to x_unfocus_frame too. */
8951 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8952 RevertToPointerRoot, CurrentTime);
8953 #endif /* ! 0 */
8956 void
8957 x_unfocus_frame (f)
8958 struct frame *f;
8960 #if 0
8961 /* Look at the remarks in x_focus_on_frame. */
8962 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8963 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8964 RevertToPointerRoot, CurrentTime);
8965 #endif /* ! 0 */
8968 /* Raise frame F. */
8970 void
8971 x_raise_frame (f)
8972 struct frame *f;
8974 BLOCK_INPUT;
8975 if (f->async_visible)
8976 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8978 XFlush (FRAME_X_DISPLAY (f));
8979 UNBLOCK_INPUT;
8982 /* Lower frame F. */
8984 void
8985 x_lower_frame (f)
8986 struct frame *f;
8988 if (f->async_visible)
8990 BLOCK_INPUT;
8991 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8992 XFlush (FRAME_X_DISPLAY (f));
8993 UNBLOCK_INPUT;
8997 /* Activate frame with Extended Window Manager Hints */
8999 void
9000 x_ewmh_activate_frame (f)
9001 FRAME_PTR f;
9003 /* See Window Manager Specification/Extended Window Manager Hints at
9004 http://freedesktop.org/wiki/Specifications/wm-spec */
9006 const char *atom = "_NET_ACTIVE_WINDOW";
9007 if (f->async_visible && wm_supports (f, atom))
9009 Lisp_Object frame;
9010 XSETFRAME (frame, f);
9011 Fx_send_client_event (frame, make_number (0), frame,
9012 make_unibyte_string (atom, strlen (atom)),
9013 make_number (32),
9014 Fcons (make_number (1),
9015 Fcons (make_number (last_user_time),
9016 Qnil)));
9020 static void
9021 XTframe_raise_lower (f, raise_flag)
9022 FRAME_PTR f;
9023 int raise_flag;
9025 if (raise_flag)
9026 x_raise_frame (f);
9027 else
9028 x_lower_frame (f);
9031 /* XEmbed implementation. */
9033 void
9034 xembed_set_info (f, flags)
9035 struct frame *f;
9036 enum xembed_info flags;
9038 Atom atom;
9039 unsigned long data[2];
9041 atom = XInternAtom (FRAME_X_DISPLAY (f), "_XEMBED_INFO", False);
9043 data[0] = XEMBED_VERSION;
9044 data[1] = flags;
9046 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), atom, atom,
9047 32, PropModeReplace, (unsigned char *) data, 2);
9050 void
9051 xembed_send_message (f, time, message, detail, data1, data2)
9052 struct frame *f;
9053 Time time;
9054 enum xembed_message message;
9055 long detail;
9056 long data1;
9057 long data2;
9059 XEvent event;
9061 event.xclient.type = ClientMessage;
9062 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9063 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
9064 event.xclient.format = 32;
9065 event.xclient.data.l[0] = time;
9066 event.xclient.data.l[1] = message;
9067 event.xclient.data.l[2] = detail;
9068 event.xclient.data.l[3] = data1;
9069 event.xclient.data.l[4] = data2;
9071 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9072 False, NoEventMask, &event);
9073 XSync (FRAME_X_DISPLAY (f), False);
9076 /* Change of visibility. */
9078 /* This tries to wait until the frame is really visible.
9079 However, if the window manager asks the user where to position
9080 the frame, this will return before the user finishes doing that.
9081 The frame will not actually be visible at that time,
9082 but it will become visible later when the window manager
9083 finishes with it. */
9085 void
9086 x_make_frame_visible (f)
9087 struct frame *f;
9089 Lisp_Object type;
9090 int original_top, original_left;
9091 int retry_count = 2;
9093 retry:
9095 BLOCK_INPUT;
9097 type = x_icon_type (f);
9098 if (!NILP (type))
9099 x_bitmap_icon (f, type);
9101 if (! FRAME_VISIBLE_P (f))
9103 /* We test FRAME_GARBAGED_P here to make sure we don't
9104 call x_set_offset a second time
9105 if we get to x_make_frame_visible a second time
9106 before the window gets really visible. */
9107 if (! FRAME_ICONIFIED_P (f)
9108 && ! FRAME_X_EMBEDDED_P (f)
9109 && ! f->output_data.x->asked_for_visible)
9110 x_set_offset (f, f->left_pos, f->top_pos, 0);
9112 f->output_data.x->asked_for_visible = 1;
9114 if (! EQ (Vx_no_window_manager, Qt))
9115 x_wm_set_window_state (f, NormalState);
9116 #ifdef USE_X_TOOLKIT
9117 if (FRAME_X_EMBEDDED_P (f))
9118 xembed_set_info (f, XEMBED_MAPPED);
9119 else
9121 /* This was XtPopup, but that did nothing for an iconified frame. */
9122 XtMapWidget (f->output_data.x->widget);
9124 #else /* not USE_X_TOOLKIT */
9125 #ifdef USE_GTK
9126 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9127 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9128 #else
9129 if (FRAME_X_EMBEDDED_P (f))
9130 xembed_set_info (f, XEMBED_MAPPED);
9131 else
9132 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9133 #endif /* not USE_GTK */
9134 #endif /* not USE_X_TOOLKIT */
9135 #if 0 /* This seems to bring back scroll bars in the wrong places
9136 if the window configuration has changed. They seem
9137 to come back ok without this. */
9138 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9139 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9140 #endif
9143 XFlush (FRAME_X_DISPLAY (f));
9145 /* Synchronize to ensure Emacs knows the frame is visible
9146 before we do anything else. We do this loop with input not blocked
9147 so that incoming events are handled. */
9149 Lisp_Object frame;
9150 int count;
9151 /* This must be before UNBLOCK_INPUT
9152 since events that arrive in response to the actions above
9153 will set it when they are handled. */
9154 int previously_visible = f->output_data.x->has_been_visible;
9156 original_left = f->left_pos;
9157 original_top = f->top_pos;
9159 /* This must come after we set COUNT. */
9160 UNBLOCK_INPUT;
9162 /* We unblock here so that arriving X events are processed. */
9164 /* Now move the window back to where it was "supposed to be".
9165 But don't do it if the gravity is negative.
9166 When the gravity is negative, this uses a position
9167 that is 3 pixels too low. Perhaps that's really the border width.
9169 Don't do this if the window has never been visible before,
9170 because the window manager may choose the position
9171 and we don't want to override it. */
9173 if (! FRAME_VISIBLE_P (f)
9174 && ! FRAME_ICONIFIED_P (f)
9175 && ! FRAME_X_EMBEDDED_P (f)
9176 && f->win_gravity == NorthWestGravity
9177 && previously_visible)
9179 Drawable rootw;
9180 int x, y;
9181 unsigned int width, height, border, depth;
9183 BLOCK_INPUT;
9185 /* On some window managers (such as FVWM) moving an existing
9186 window, even to the same place, causes the window manager
9187 to introduce an offset. This can cause the window to move
9188 to an unexpected location. Check the geometry (a little
9189 slow here) and then verify that the window is in the right
9190 place. If the window is not in the right place, move it
9191 there, and take the potential window manager hit. */
9192 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9193 &rootw, &x, &y, &width, &height, &border, &depth);
9195 if (original_left != x || original_top != y)
9196 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9197 original_left, original_top);
9199 UNBLOCK_INPUT;
9202 XSETFRAME (frame, f);
9204 /* Wait until the frame is visible. Process X events until a
9205 MapNotify event has been seen, or until we think we won't get a
9206 MapNotify at all.. */
9207 for (count = input_signal_count + 10;
9208 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9210 /* Force processing of queued events. */
9211 x_sync (f);
9213 /* Machines that do polling rather than SIGIO have been
9214 observed to go into a busy-wait here. So we'll fake an
9215 alarm signal to let the handler know that there's something
9216 to be read. We used to raise a real alarm, but it seems
9217 that the handler isn't always enabled here. This is
9218 probably a bug. */
9219 if (input_polling_used ())
9221 /* It could be confusing if a real alarm arrives while
9222 processing the fake one. Turn it off and let the
9223 handler reset it. */
9224 extern void poll_for_input_1 P_ ((void));
9225 int old_poll_suppress_count = poll_suppress_count;
9226 poll_suppress_count = 1;
9227 poll_for_input_1 ();
9228 poll_suppress_count = old_poll_suppress_count;
9231 /* See if a MapNotify event has been processed. */
9232 FRAME_SAMPLE_VISIBILITY (f);
9235 /* 2000-09-28: In
9237 (let ((f (selected-frame)))
9238 (iconify-frame f)
9239 (raise-frame f))
9241 the frame is not raised with various window managers on
9242 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9243 unknown reason, the call to XtMapWidget is completely ignored.
9244 Mapping the widget a second time works. */
9246 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9247 goto retry;
9251 /* Change from mapped state to withdrawn state. */
9253 /* Make the frame visible (mapped and not iconified). */
9255 void
9256 x_make_frame_invisible (f)
9257 struct frame *f;
9259 Window window;
9261 /* Use the frame's outermost window, not the one we normally draw on. */
9262 window = FRAME_OUTER_WINDOW (f);
9264 /* Don't keep the highlight on an invisible frame. */
9265 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9266 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9268 #if 0/* This might add unreliability; I don't trust it -- rms. */
9269 if (! f->async_visible && ! f->async_iconified)
9270 return;
9271 #endif
9273 BLOCK_INPUT;
9275 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9276 that the current position of the window is user-specified, rather than
9277 program-specified, so that when the window is mapped again, it will be
9278 placed at the same location, without forcing the user to position it
9279 by hand again (they have already done that once for this window.) */
9280 x_wm_set_size_hint (f, (long) 0, 1);
9282 #ifdef USE_GTK
9283 if (FRAME_GTK_OUTER_WIDGET (f))
9284 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9285 else
9286 #else
9287 if (FRAME_X_EMBEDDED_P (f))
9288 xembed_set_info (f, 0);
9289 else
9290 #endif
9293 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9294 DefaultScreen (FRAME_X_DISPLAY (f))))
9296 UNBLOCK_INPUT_RESIGNAL;
9297 error ("Can't notify window manager of window withdrawal");
9301 /* We can't distinguish this from iconification
9302 just by the event that we get from the server.
9303 So we can't win using the usual strategy of letting
9304 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9305 and synchronize with the server to make sure we agree. */
9306 f->visible = 0;
9307 FRAME_ICONIFIED_P (f) = 0;
9308 f->async_visible = 0;
9309 f->async_iconified = 0;
9311 x_sync (f);
9313 UNBLOCK_INPUT;
9316 /* Change window state from mapped to iconified. */
9318 void
9319 x_iconify_frame (f)
9320 struct frame *f;
9322 int result;
9323 Lisp_Object type;
9325 /* Don't keep the highlight on an invisible frame. */
9326 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9327 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9329 if (f->async_iconified)
9330 return;
9332 BLOCK_INPUT;
9334 FRAME_SAMPLE_VISIBILITY (f);
9336 type = x_icon_type (f);
9337 if (!NILP (type))
9338 x_bitmap_icon (f, type);
9340 #ifdef USE_GTK
9341 if (FRAME_GTK_OUTER_WIDGET (f))
9343 if (! FRAME_VISIBLE_P (f))
9344 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9346 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9347 f->iconified = 1;
9348 f->visible = 1;
9349 f->async_iconified = 1;
9350 f->async_visible = 0;
9351 UNBLOCK_INPUT;
9352 return;
9354 #endif
9356 #ifdef USE_X_TOOLKIT
9358 if (! FRAME_VISIBLE_P (f))
9360 if (! EQ (Vx_no_window_manager, Qt))
9361 x_wm_set_window_state (f, IconicState);
9362 /* This was XtPopup, but that did nothing for an iconified frame. */
9363 XtMapWidget (f->output_data.x->widget);
9364 /* The server won't give us any event to indicate
9365 that an invisible frame was changed to an icon,
9366 so we have to record it here. */
9367 f->iconified = 1;
9368 f->visible = 1;
9369 f->async_iconified = 1;
9370 f->async_visible = 0;
9371 UNBLOCK_INPUT;
9372 return;
9375 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9376 XtWindow (f->output_data.x->widget),
9377 DefaultScreen (FRAME_X_DISPLAY (f)));
9378 UNBLOCK_INPUT;
9380 if (!result)
9381 error ("Can't notify window manager of iconification");
9383 f->async_iconified = 1;
9384 f->async_visible = 0;
9387 BLOCK_INPUT;
9388 XFlush (FRAME_X_DISPLAY (f));
9389 UNBLOCK_INPUT;
9390 #else /* not USE_X_TOOLKIT */
9392 /* Make sure the X server knows where the window should be positioned,
9393 in case the user deiconifies with the window manager. */
9394 if (! FRAME_VISIBLE_P (f)
9395 && ! FRAME_ICONIFIED_P (f)
9396 && ! FRAME_X_EMBEDDED_P (f))
9397 x_set_offset (f, f->left_pos, f->top_pos, 0);
9399 /* Since we don't know which revision of X we're running, we'll use both
9400 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9402 /* X11R4: send a ClientMessage to the window manager using the
9403 WM_CHANGE_STATE type. */
9405 XEvent message;
9407 message.xclient.window = FRAME_X_WINDOW (f);
9408 message.xclient.type = ClientMessage;
9409 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9410 message.xclient.format = 32;
9411 message.xclient.data.l[0] = IconicState;
9413 if (! XSendEvent (FRAME_X_DISPLAY (f),
9414 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9415 False,
9416 SubstructureRedirectMask | SubstructureNotifyMask,
9417 &message))
9419 UNBLOCK_INPUT_RESIGNAL;
9420 error ("Can't notify window manager of iconification");
9424 /* X11R3: set the initial_state field of the window manager hints to
9425 IconicState. */
9426 x_wm_set_window_state (f, IconicState);
9428 if (!FRAME_VISIBLE_P (f))
9430 /* If the frame was withdrawn, before, we must map it. */
9431 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9434 f->async_iconified = 1;
9435 f->async_visible = 0;
9437 XFlush (FRAME_X_DISPLAY (f));
9438 UNBLOCK_INPUT;
9439 #endif /* not USE_X_TOOLKIT */
9443 /* Free X resources of frame F. */
9445 void
9446 x_free_frame_resources (f)
9447 struct frame *f;
9449 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9450 Lisp_Object bar;
9451 struct scroll_bar *b;
9453 BLOCK_INPUT;
9455 /* If a display connection is dead, don't try sending more
9456 commands to the X server. */
9457 if (dpyinfo->display)
9459 /* We must free faces before destroying windows because some
9460 font-driver (e.g. xft) access a window while finishing a
9461 face. */
9462 if (FRAME_FACE_CACHE (f))
9463 free_frame_faces (f);
9465 if (f->output_data.x->icon_desc)
9466 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9468 #ifdef USE_X_TOOLKIT
9469 /* Explicitly destroy the scroll bars of the frame. Without
9470 this, we get "BadDrawable" errors from the toolkit later on,
9471 presumably from expose events generated for the disappearing
9472 toolkit scroll bars. */
9473 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9475 b = XSCROLL_BAR (bar);
9476 x_scroll_bar_remove (b);
9478 #endif
9480 #ifdef HAVE_X_I18N
9481 if (FRAME_XIC (f))
9482 free_frame_xic (f);
9483 #endif
9485 #ifdef USE_X_TOOLKIT
9486 if (f->output_data.x->widget)
9488 XtDestroyWidget (f->output_data.x->widget);
9489 f->output_data.x->widget = NULL;
9491 /* Tooltips don't have widgets, only a simple X window, even if
9492 we are using a toolkit. */
9493 else if (FRAME_X_WINDOW (f))
9494 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9496 free_frame_menubar (f);
9497 #else /* !USE_X_TOOLKIT */
9499 #ifdef USE_GTK
9500 /* In the GTK version, tooltips are normal X
9501 frames. We must check and free both types. */
9502 if (FRAME_GTK_OUTER_WIDGET (f))
9504 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9505 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9506 FRAME_GTK_OUTER_WIDGET (f) = 0;
9508 #endif /* USE_GTK */
9510 if (FRAME_X_WINDOW (f))
9511 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9512 #endif /* !USE_X_TOOLKIT */
9514 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9515 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9516 unload_color (f, f->output_data.x->cursor_pixel);
9517 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9518 unload_color (f, f->output_data.x->border_pixel);
9519 unload_color (f, f->output_data.x->mouse_pixel);
9521 if (f->output_data.x->scroll_bar_background_pixel != -1)
9522 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9523 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9524 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9525 #ifdef USE_TOOLKIT_SCROLL_BARS
9526 /* Scrollbar shadow colors. */
9527 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9528 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9529 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9530 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9531 #endif /* USE_TOOLKIT_SCROLL_BARS */
9532 if (f->output_data.x->white_relief.allocated_p)
9533 unload_color (f, f->output_data.x->white_relief.pixel);
9534 if (f->output_data.x->black_relief.allocated_p)
9535 unload_color (f, f->output_data.x->black_relief.pixel);
9537 x_free_gcs (f);
9538 XFlush (FRAME_X_DISPLAY (f));
9541 xfree (f->output_data.x->saved_menu_event);
9542 xfree (f->output_data.x);
9543 f->output_data.x = NULL;
9545 if (f == dpyinfo->x_focus_frame)
9546 dpyinfo->x_focus_frame = 0;
9547 if (f == dpyinfo->x_focus_event_frame)
9548 dpyinfo->x_focus_event_frame = 0;
9549 if (f == dpyinfo->x_highlight_frame)
9550 dpyinfo->x_highlight_frame = 0;
9552 if (f == dpyinfo->mouse_face_mouse_frame)
9554 dpyinfo->mouse_face_beg_row
9555 = dpyinfo->mouse_face_beg_col = -1;
9556 dpyinfo->mouse_face_end_row
9557 = dpyinfo->mouse_face_end_col = -1;
9558 dpyinfo->mouse_face_window = Qnil;
9559 dpyinfo->mouse_face_deferred_gc = 0;
9560 dpyinfo->mouse_face_mouse_frame = 0;
9563 UNBLOCK_INPUT;
9567 /* Destroy the X window of frame F. */
9569 void
9570 x_destroy_window (f)
9571 struct frame *f;
9573 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9575 /* If a display connection is dead, don't try sending more
9576 commands to the X server. */
9577 if (dpyinfo->display != 0)
9578 x_free_frame_resources (f);
9580 dpyinfo->reference_count--;
9584 /* Setting window manager hints. */
9586 /* Set the normal size hints for the window manager, for frame F.
9587 FLAGS is the flags word to use--or 0 meaning preserve the flags
9588 that the window now has.
9589 If USER_POSITION is nonzero, we set the USPosition
9590 flag (this is useful when FLAGS is 0).
9591 The GTK version is in gtkutils.c */
9593 #ifndef USE_GTK
9594 void
9595 x_wm_set_size_hint (f, flags, user_position)
9596 struct frame *f;
9597 long flags;
9598 int user_position;
9600 XSizeHints size_hints;
9602 #ifdef USE_X_TOOLKIT
9603 Arg al[2];
9604 int ac = 0;
9605 Dimension widget_width, widget_height;
9606 #endif
9608 Window window = FRAME_OUTER_WINDOW (f);
9610 /* Setting PMaxSize caused various problems. */
9611 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9613 size_hints.x = f->left_pos;
9614 size_hints.y = f->top_pos;
9616 #ifdef USE_X_TOOLKIT
9617 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9618 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9619 XtGetValues (f->output_data.x->widget, al, ac);
9620 size_hints.height = widget_height;
9621 size_hints.width = widget_width;
9622 #else /* not USE_X_TOOLKIT */
9623 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9624 size_hints.width = FRAME_PIXEL_WIDTH (f);
9625 #endif /* not USE_X_TOOLKIT */
9627 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9628 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9629 size_hints.max_width = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
9630 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9631 size_hints.max_height = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
9632 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9634 /* Calculate the base and minimum sizes.
9636 (When we use the X toolkit, we don't do it here.
9637 Instead we copy the values that the widgets are using, below.) */
9638 #ifndef USE_X_TOOLKIT
9640 int base_width, base_height;
9641 int min_rows = 0, min_cols = 0;
9643 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9644 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9646 check_frame_size (f, &min_rows, &min_cols);
9648 /* The window manager uses the base width hints to calculate the
9649 current number of rows and columns in the frame while
9650 resizing; min_width and min_height aren't useful for this
9651 purpose, since they might not give the dimensions for a
9652 zero-row, zero-column frame.
9654 We use the base_width and base_height members if we have
9655 them; otherwise, we set the min_width and min_height members
9656 to the size for a zero x zero frame. */
9658 size_hints.flags |= PBaseSize;
9659 size_hints.base_width = base_width;
9660 size_hints.base_height = base_height;
9661 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9662 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9665 /* If we don't need the old flags, we don't need the old hint at all. */
9666 if (flags)
9668 size_hints.flags |= flags;
9669 goto no_read;
9671 #endif /* not USE_X_TOOLKIT */
9674 XSizeHints hints; /* Sometimes I hate X Windows... */
9675 long supplied_return;
9676 int value;
9678 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9679 &supplied_return);
9681 #ifdef USE_X_TOOLKIT
9682 size_hints.base_height = hints.base_height;
9683 size_hints.base_width = hints.base_width;
9684 size_hints.min_height = hints.min_height;
9685 size_hints.min_width = hints.min_width;
9686 #endif
9688 if (flags)
9689 size_hints.flags |= flags;
9690 else
9692 if (value == 0)
9693 hints.flags = 0;
9694 if (hints.flags & PSize)
9695 size_hints.flags |= PSize;
9696 if (hints.flags & PPosition)
9697 size_hints.flags |= PPosition;
9698 if (hints.flags & USPosition)
9699 size_hints.flags |= USPosition;
9700 if (hints.flags & USSize)
9701 size_hints.flags |= USSize;
9705 #ifndef USE_X_TOOLKIT
9706 no_read:
9707 #endif
9709 #ifdef PWinGravity
9710 size_hints.win_gravity = f->win_gravity;
9711 size_hints.flags |= PWinGravity;
9713 if (user_position)
9715 size_hints.flags &= ~ PPosition;
9716 size_hints.flags |= USPosition;
9718 #endif /* PWinGravity */
9720 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9722 #endif /* not USE_GTK */
9724 /* Used for IconicState or NormalState */
9726 void
9727 x_wm_set_window_state (f, state)
9728 struct frame *f;
9729 int state;
9731 #ifdef USE_X_TOOLKIT
9732 Arg al[1];
9734 XtSetArg (al[0], XtNinitialState, state);
9735 XtSetValues (f->output_data.x->widget, al, 1);
9736 #else /* not USE_X_TOOLKIT */
9737 Window window = FRAME_X_WINDOW (f);
9739 f->output_data.x->wm_hints.flags |= StateHint;
9740 f->output_data.x->wm_hints.initial_state = state;
9742 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9743 #endif /* not USE_X_TOOLKIT */
9746 void
9747 x_wm_set_icon_pixmap (f, pixmap_id)
9748 struct frame *f;
9749 int pixmap_id;
9751 Pixmap icon_pixmap, icon_mask;
9753 #ifndef USE_X_TOOLKIT
9754 Window window = FRAME_OUTER_WINDOW (f);
9755 #endif
9757 if (pixmap_id > 0)
9759 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9760 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9761 icon_mask = x_bitmap_mask (f, pixmap_id);
9762 f->output_data.x->wm_hints.icon_mask = icon_mask;
9764 else
9766 /* It seems there is no way to turn off use of an icon pixmap.
9767 The following line does it, only if no icon has yet been created,
9768 for some window managers. But with mwm it crashes.
9769 Some people say it should clear the IconPixmapHint bit in this case,
9770 but that doesn't work, and the X consortium said it isn't the
9771 right thing at all. Since there is no way to win,
9772 best to explicitly give up. */
9773 #if 0
9774 f->output_data.x->wm_hints.icon_pixmap = None;
9775 f->output_data.x->wm_hints.icon_mask = None;
9776 #else
9777 return;
9778 #endif
9782 #ifdef USE_GTK
9784 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9785 return;
9788 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9791 Arg al[1];
9792 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9793 XtSetValues (f->output_data.x->widget, al, 1);
9794 XtSetArg (al[0], XtNiconMask, icon_mask);
9795 XtSetValues (f->output_data.x->widget, al, 1);
9798 #else /* not USE_X_TOOLKIT && not USE_GTK */
9800 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9801 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9803 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9806 void
9807 x_wm_set_icon_position (f, icon_x, icon_y)
9808 struct frame *f;
9809 int icon_x, icon_y;
9811 Window window = FRAME_OUTER_WINDOW (f);
9813 f->output_data.x->wm_hints.flags |= IconPositionHint;
9814 f->output_data.x->wm_hints.icon_x = icon_x;
9815 f->output_data.x->wm_hints.icon_y = icon_y;
9817 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9821 /***********************************************************************
9822 Fonts
9823 ***********************************************************************/
9825 #if GLYPH_DEBUG
9827 /* Check that FONT is valid on frame F. It is if it can be found in F's
9828 font table. */
9830 static void
9831 x_check_font (f, font)
9832 struct frame *f;
9833 struct font *font;
9835 Lisp_Object frame;
9837 xassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9838 if (font->driver->check)
9839 xassert (font->driver->check (f, font) == 0);
9842 #endif /* GLYPH_DEBUG != 0 */
9845 /***********************************************************************
9846 Initialization
9847 ***********************************************************************/
9849 #ifdef USE_X_TOOLKIT
9850 static XrmOptionDescRec emacs_options[] = {
9851 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9852 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9854 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9855 XrmoptionSepArg, NULL},
9856 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9858 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9859 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9860 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9861 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9862 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9863 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9864 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9867 /* Whether atimer for Xt timeouts is activated or not. */
9869 static int x_timeout_atimer_activated_flag;
9871 #endif /* USE_X_TOOLKIT */
9873 static int x_initialized;
9875 #ifdef HAVE_X_SM
9876 static int x_session_initialized;
9877 #endif
9879 /* Test whether two display-name strings agree up to the dot that separates
9880 the screen number from the server number. */
9881 static int
9882 same_x_server (name1, name2)
9883 const char *name1, *name2;
9885 int seen_colon = 0;
9886 const unsigned char *system_name = SDATA (Vsystem_name);
9887 int system_name_length = strlen (system_name);
9888 int length_until_period = 0;
9890 while (system_name[length_until_period] != 0
9891 && system_name[length_until_period] != '.')
9892 length_until_period++;
9894 /* Treat `unix' like an empty host name. */
9895 if (! strncmp (name1, "unix:", 5))
9896 name1 += 4;
9897 if (! strncmp (name2, "unix:", 5))
9898 name2 += 4;
9899 /* Treat this host's name like an empty host name. */
9900 if (! strncmp (name1, system_name, system_name_length)
9901 && name1[system_name_length] == ':')
9902 name1 += system_name_length;
9903 if (! strncmp (name2, system_name, system_name_length)
9904 && name2[system_name_length] == ':')
9905 name2 += system_name_length;
9906 /* Treat this host's domainless name like an empty host name. */
9907 if (! strncmp (name1, system_name, length_until_period)
9908 && name1[length_until_period] == ':')
9909 name1 += length_until_period;
9910 if (! strncmp (name2, system_name, length_until_period)
9911 && name2[length_until_period] == ':')
9912 name2 += length_until_period;
9914 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
9916 if (*name1 == ':')
9917 seen_colon++;
9918 if (seen_colon && *name1 == '.')
9919 return 1;
9921 return (seen_colon
9922 && (*name1 == '.' || *name1 == '\0')
9923 && (*name2 == '.' || *name2 == '\0'));
9926 /* Count number of set bits in mask and number of bits to shift to
9927 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
9928 to 5. */
9929 static void
9930 get_bits_and_offset (mask, bits, offset)
9931 unsigned long mask;
9932 int *bits;
9933 int *offset;
9935 int nr = 0;
9936 int off = 0;
9938 while (!(mask & 1))
9940 off++;
9941 mask >>= 1;
9944 while (mask & 1)
9946 nr++;
9947 mask >>= 1;
9950 *offset = off;
9951 *bits = nr;
9954 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
9955 But don't permanently open it, just test its availability. */
9958 x_display_ok (display)
9959 const char *display;
9961 int dpy_ok = 1;
9962 Display *dpy;
9964 dpy = XOpenDisplay (display);
9965 if (dpy)
9966 XCloseDisplay (dpy);
9967 else
9968 dpy_ok = 0;
9969 return dpy_ok;
9972 /* Open a connection to X display DISPLAY_NAME, and return
9973 the structure that describes the open display.
9974 If we cannot contact the display, return null. */
9976 struct x_display_info *
9977 x_term_init (display_name, xrm_option, resource_name)
9978 Lisp_Object display_name;
9979 char *xrm_option;
9980 char *resource_name;
9982 int connection;
9983 Display *dpy;
9984 struct terminal *terminal;
9985 struct x_display_info *dpyinfo;
9986 XrmDatabase xrdb;
9988 BLOCK_INPUT;
9990 if (!x_initialized)
9992 x_initialize ();
9993 ++x_initialized;
9996 if (! x_display_ok (SDATA (display_name)))
9997 error ("Display %s can't be opened", SDATA (display_name));
9999 #ifdef USE_GTK
10001 #define NUM_ARGV 10
10002 int argc;
10003 char *argv[NUM_ARGV];
10004 char **argv2 = argv;
10005 GdkAtom atom;
10007 #ifndef HAVE_GTK_MULTIDISPLAY
10008 if (!EQ (Vinitial_window_system, intern ("x")))
10009 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10010 #endif
10012 if (x_initialized++ > 1)
10014 #ifdef HAVE_GTK_MULTIDISPLAY
10015 /* Opening another display. If xg_display_open returns less
10016 than zero, we are probably on GTK 2.0, which can only handle
10017 one display. GTK 2.2 or later can handle more than one. */
10018 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10019 #endif
10020 error ("Sorry, this version of GTK can only handle one display");
10022 else
10024 for (argc = 0; argc < NUM_ARGV; ++argc)
10025 argv[argc] = 0;
10027 argc = 0;
10028 argv[argc++] = initial_argv[0];
10030 if (! NILP (display_name))
10032 argv[argc++] = "--display";
10033 argv[argc++] = SDATA (display_name);
10036 argv[argc++] = "--name";
10037 argv[argc++] = resource_name;
10039 XSetLocaleModifiers ("");
10041 gtk_init (&argc, &argv2);
10043 /* gtk_init does set_locale. We must fix locale after calling it. */
10044 fixup_locale ();
10045 xg_initialize ();
10047 dpy = GDK_DISPLAY ();
10049 /* NULL window -> events for all windows go to our function */
10050 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10052 /* Load our own gtkrc if it exists. */
10054 char *file = "~/.emacs.d/gtkrc";
10055 Lisp_Object s, abs_file;
10057 s = make_string (file, strlen (file));
10058 abs_file = Fexpand_file_name (s, Qnil);
10060 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10061 gtk_rc_parse (SDATA (abs_file));
10064 XSetErrorHandler (x_error_handler);
10065 XSetIOErrorHandler (x_io_error_quitter);
10068 #else /* not USE_GTK */
10069 #ifdef USE_X_TOOLKIT
10070 /* weiner@footloose.sps.mot.com reports that this causes
10071 errors with X11R5:
10072 X protocol error: BadAtom (invalid Atom parameter)
10073 on protocol request 18skiloaf.
10074 So let's not use it until R6. */
10075 #ifdef HAVE_X11XTR6
10076 XtSetLanguageProc (NULL, NULL, NULL);
10077 #endif
10080 int argc = 0;
10081 char *argv[3];
10083 argv[0] = "";
10084 argc = 1;
10085 if (xrm_option)
10087 argv[argc++] = "-xrm";
10088 argv[argc++] = xrm_option;
10090 turn_on_atimers (0);
10091 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10092 resource_name, EMACS_CLASS,
10093 emacs_options, XtNumber (emacs_options),
10094 &argc, argv);
10095 turn_on_atimers (1);
10097 #ifdef HAVE_X11XTR6
10098 /* I think this is to compensate for XtSetLanguageProc. */
10099 fixup_locale ();
10100 #endif
10103 #else /* not USE_X_TOOLKIT */
10104 XSetLocaleModifiers ("");
10105 dpy = XOpenDisplay (SDATA (display_name));
10106 #endif /* not USE_X_TOOLKIT */
10107 #endif /* not USE_GTK*/
10109 /* Detect failure. */
10110 if (dpy == 0)
10112 UNBLOCK_INPUT;
10113 return 0;
10116 /* We have definitely succeeded. Record the new connection. */
10118 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10119 bzero (dpyinfo, sizeof *dpyinfo);
10121 terminal = x_create_terminal (dpyinfo);
10124 struct x_display_info *share;
10125 Lisp_Object tail;
10127 for (share = x_display_list, tail = x_display_name_list; share;
10128 share = share->next, tail = XCDR (tail))
10129 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10130 SDATA (display_name)))
10131 break;
10132 if (share)
10133 terminal->kboard = share->terminal->kboard;
10134 else
10136 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10137 init_kboard (terminal->kboard);
10138 terminal->kboard->Vwindow_system = intern ("x");
10139 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10141 char *vendor = ServerVendor (dpy);
10142 /* Temporarily hide the partially initialized terminal */
10143 terminal_list = terminal->next_terminal;
10144 UNBLOCK_INPUT;
10145 terminal->kboard->Vsystem_key_alist
10146 = call1 (Qvendor_specific_keysyms,
10147 vendor ? build_string (vendor) : empty_unibyte_string);
10148 BLOCK_INPUT;
10149 terminal->next_terminal = terminal_list;
10150 terminal_list = terminal;
10153 terminal->kboard->next_kboard = all_kboards;
10154 all_kboards = terminal->kboard;
10155 /* Don't let the initial kboard remain current longer than necessary.
10156 That would cause problems if a file loaded on startup tries to
10157 prompt in the mini-buffer. */
10158 if (current_kboard == initial_kboard)
10159 current_kboard = terminal->kboard;
10161 terminal->kboard->reference_count++;
10164 /* Put this display on the chain. */
10165 dpyinfo->next = x_display_list;
10166 x_display_list = dpyinfo;
10168 /* Put it on x_display_name_list as well, to keep them parallel. */
10169 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10170 x_display_name_list);
10171 dpyinfo->name_list_element = XCAR (x_display_name_list);
10173 dpyinfo->display = dpy;
10175 /* Set the name of the terminal. */
10176 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10177 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10178 terminal->name[SBYTES (display_name)] = 0;
10180 #if 0
10181 XSetAfterFunction (x_current_display, x_trace_wire);
10182 #endif /* ! 0 */
10184 dpyinfo->x_id_name
10185 = (char *) xmalloc (SBYTES (Vinvocation_name)
10186 + SBYTES (Vsystem_name)
10187 + 2);
10188 sprintf (dpyinfo->x_id_name, "%s@%s",
10189 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10191 /* Figure out which modifier bits mean what. */
10192 x_find_modifier_meanings (dpyinfo);
10194 /* Get the scroll bar cursor. */
10195 #ifdef USE_GTK
10196 /* We must create a GTK cursor, it is required for GTK widgets. */
10197 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10198 #endif /* USE_GTK */
10200 dpyinfo->vertical_scroll_bar_cursor
10201 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10203 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10204 resource_name, EMACS_CLASS);
10205 #ifdef HAVE_XRMSETDATABASE
10206 XrmSetDatabase (dpyinfo->display, xrdb);
10207 #else
10208 dpyinfo->display->db = xrdb;
10209 #endif
10210 /* Put the rdb where we can find it in a way that works on
10211 all versions. */
10212 dpyinfo->xrdb = xrdb;
10214 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10215 DefaultScreen (dpyinfo->display));
10216 select_visual (dpyinfo);
10217 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10218 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10219 dpyinfo->client_leader_window = 0;
10220 dpyinfo->grabbed = 0;
10221 dpyinfo->reference_count = 0;
10222 dpyinfo->icon_bitmap_id = -1;
10223 dpyinfo->n_fonts = 0;
10224 dpyinfo->bitmaps = 0;
10225 dpyinfo->bitmaps_size = 0;
10226 dpyinfo->bitmaps_last = 0;
10227 dpyinfo->scratch_cursor_gc = 0;
10228 dpyinfo->mouse_face_mouse_frame = 0;
10229 dpyinfo->mouse_face_deferred_gc = 0;
10230 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10231 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10232 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10233 dpyinfo->mouse_face_window = Qnil;
10234 dpyinfo->mouse_face_overlay = Qnil;
10235 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10236 dpyinfo->mouse_face_defer = 0;
10237 dpyinfo->mouse_face_hidden = 0;
10238 dpyinfo->x_focus_frame = 0;
10239 dpyinfo->x_focus_event_frame = 0;
10240 dpyinfo->x_highlight_frame = 0;
10241 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10243 /* See if we can construct pixel values from RGB values. */
10244 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10245 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10247 if (dpyinfo->visual->class == TrueColor)
10249 get_bits_and_offset (dpyinfo->visual->red_mask,
10250 &dpyinfo->red_bits, &dpyinfo->red_offset);
10251 get_bits_and_offset (dpyinfo->visual->blue_mask,
10252 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10253 get_bits_and_offset (dpyinfo->visual->green_mask,
10254 &dpyinfo->green_bits, &dpyinfo->green_offset);
10257 /* See if a private colormap is requested. */
10258 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10260 if (dpyinfo->visual->class == PseudoColor)
10262 Lisp_Object value;
10263 value = display_x_get_resource (dpyinfo,
10264 build_string ("privateColormap"),
10265 build_string ("PrivateColormap"),
10266 Qnil, Qnil);
10267 if (STRINGP (value)
10268 && (!strcmp (SDATA (value), "true")
10269 || !strcmp (SDATA (value), "on")))
10270 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10273 else
10274 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10275 dpyinfo->visual, AllocNone);
10278 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10279 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10280 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10281 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10282 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10283 pixels = DisplayWidth (dpyinfo->display, screen_number);
10284 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10285 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10286 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10289 dpyinfo->Xatom_wm_protocols
10290 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10291 dpyinfo->Xatom_wm_take_focus
10292 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10293 dpyinfo->Xatom_wm_save_yourself
10294 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10295 dpyinfo->Xatom_wm_delete_window
10296 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10297 dpyinfo->Xatom_wm_change_state
10298 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10299 dpyinfo->Xatom_wm_configure_denied
10300 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10301 dpyinfo->Xatom_wm_window_moved
10302 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10303 dpyinfo->Xatom_wm_client_leader
10304 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10305 dpyinfo->Xatom_editres
10306 = XInternAtom (dpyinfo->display, "Editres", False);
10307 dpyinfo->Xatom_CLIPBOARD
10308 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10309 dpyinfo->Xatom_TIMESTAMP
10310 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10311 dpyinfo->Xatom_TEXT
10312 = XInternAtom (dpyinfo->display, "TEXT", False);
10313 dpyinfo->Xatom_COMPOUND_TEXT
10314 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10315 dpyinfo->Xatom_UTF8_STRING
10316 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10317 dpyinfo->Xatom_DELETE
10318 = XInternAtom (dpyinfo->display, "DELETE", False);
10319 dpyinfo->Xatom_MULTIPLE
10320 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10321 dpyinfo->Xatom_INCR
10322 = XInternAtom (dpyinfo->display, "INCR", False);
10323 dpyinfo->Xatom_EMACS_TMP
10324 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10325 dpyinfo->Xatom_TARGETS
10326 = XInternAtom (dpyinfo->display, "TARGETS", False);
10327 dpyinfo->Xatom_NULL
10328 = XInternAtom (dpyinfo->display, "NULL", False);
10329 dpyinfo->Xatom_ATOM_PAIR
10330 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10331 /* For properties of font. */
10332 dpyinfo->Xatom_PIXEL_SIZE
10333 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10334 dpyinfo->Xatom_AVERAGE_WIDTH
10335 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10336 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10337 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10338 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10339 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10340 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10341 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10343 /* Ghostscript support. */
10344 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10345 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10347 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10348 False);
10350 dpyinfo->Xatom_XEMBED = XInternAtom (dpyinfo->display, "_XEMBED",
10351 False);
10353 dpyinfo->cut_buffers_initialized = 0;
10355 dpyinfo->x_dnd_atoms_size = 8;
10356 dpyinfo->x_dnd_atoms_length = 0;
10357 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10358 * dpyinfo->x_dnd_atoms_size);
10360 dpyinfo->net_supported_atoms = NULL;
10361 dpyinfo->nr_net_supported_atoms = 0;
10362 dpyinfo->net_supported_window = 0;
10364 connection = ConnectionNumber (dpyinfo->display);
10365 dpyinfo->connection = connection;
10368 extern int gray_bitmap_width, gray_bitmap_height;
10369 extern char *gray_bitmap_bits;
10370 dpyinfo->gray
10371 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10372 gray_bitmap_bits,
10373 gray_bitmap_width, gray_bitmap_height,
10374 (unsigned long) 1, (unsigned long) 0, 1);
10377 #ifdef HAVE_X_I18N
10378 xim_initialize (dpyinfo, resource_name);
10379 #endif
10381 #ifdef subprocesses
10382 /* This is only needed for distinguishing keyboard and process input. */
10383 if (connection != 0)
10384 add_keyboard_wait_descriptor (connection);
10385 #endif
10387 #ifdef F_SETOWN
10388 fcntl (connection, F_SETOWN, getpid ());
10389 #endif /* ! defined (F_SETOWN) */
10391 #ifdef SIGIO
10392 if (interrupt_input)
10393 init_sigio (connection);
10394 #endif /* ! defined (SIGIO) */
10396 #ifdef USE_LUCID
10398 Display *dpy = dpyinfo->display;
10399 XrmValue d, fr, to;
10400 Font font;
10402 d.addr = (XPointer)&dpy;
10403 d.size = sizeof (Display *);
10404 fr.addr = XtDefaultFont;
10405 fr.size = sizeof (XtDefaultFont);
10406 to.size = sizeof (Font *);
10407 to.addr = (XPointer)&font;
10408 x_catch_errors (dpy);
10409 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10410 abort ();
10411 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10412 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10413 x_uncatch_errors ();
10415 #endif
10417 /* See if we should run in synchronous mode. This is useful
10418 for debugging X code. */
10420 Lisp_Object value;
10421 value = display_x_get_resource (dpyinfo,
10422 build_string ("synchronous"),
10423 build_string ("Synchronous"),
10424 Qnil, Qnil);
10425 if (STRINGP (value)
10426 && (!strcmp (SDATA (value), "true")
10427 || !strcmp (SDATA (value), "on")))
10428 XSynchronize (dpyinfo->display, True);
10432 Lisp_Object value;
10433 value = display_x_get_resource (dpyinfo,
10434 build_string ("useXIM"),
10435 build_string ("UseXIM"),
10436 Qnil, Qnil);
10437 #ifdef USE_XIM
10438 if (STRINGP (value)
10439 && (!strcmp (SDATA (value), "false")
10440 || !strcmp (SDATA (value), "off")))
10441 use_xim = 0;
10442 #else
10443 if (STRINGP (value)
10444 && (!strcmp (SDATA (value), "true")
10445 || !strcmp (SDATA (value), "on")))
10446 use_xim = 1;
10447 #endif
10450 #ifdef HAVE_X_SM
10451 /* Only do this for the very first display in the Emacs session.
10452 Ignore X session management when Emacs was first started on a
10453 tty. */
10454 if (terminal->id == 1)
10455 x_session_initialize (dpyinfo);
10456 #endif
10458 UNBLOCK_INPUT;
10460 return dpyinfo;
10463 /* Get rid of display DPYINFO, deleting all frames on it,
10464 and without sending any more commands to the X server. */
10466 void
10467 x_delete_display (dpyinfo)
10468 struct x_display_info *dpyinfo;
10470 int i;
10471 struct terminal *t;
10473 /* Close all frames and delete the generic struct terminal for this
10474 X display. */
10475 for (t = terminal_list; t; t = t->next_terminal)
10476 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10478 #ifdef HAVE_X_SM
10479 /* Close X session management when we close its display. */
10480 if (t->id == 1 && x_session_have_connection ())
10481 x_session_close();
10482 #endif
10483 delete_terminal (t);
10484 break;
10487 delete_keyboard_wait_descriptor (dpyinfo->connection);
10489 /* Discard this display from x_display_name_list and x_display_list.
10490 We can't use Fdelq because that can quit. */
10491 if (! NILP (x_display_name_list)
10492 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10493 x_display_name_list = XCDR (x_display_name_list);
10494 else
10496 Lisp_Object tail;
10498 tail = x_display_name_list;
10499 while (CONSP (tail) && CONSP (XCDR (tail)))
10501 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10503 XSETCDR (tail, XCDR (XCDR (tail)));
10504 break;
10506 tail = XCDR (tail);
10510 if (next_noop_dpyinfo == dpyinfo)
10511 next_noop_dpyinfo = dpyinfo->next;
10513 if (x_display_list == dpyinfo)
10514 x_display_list = dpyinfo->next;
10515 else
10517 struct x_display_info *tail;
10519 for (tail = x_display_list; tail; tail = tail->next)
10520 if (tail->next == dpyinfo)
10521 tail->next = tail->next->next;
10524 /* Xt and GTK do this themselves. */
10525 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
10526 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
10527 XrmDestroyDatabase (dpyinfo->xrdb);
10528 #endif
10529 #endif
10531 xfree (dpyinfo->x_id_name);
10532 xfree (dpyinfo->x_dnd_atoms);
10533 xfree (dpyinfo->color_cells);
10534 xfree (dpyinfo);
10537 #ifdef USE_X_TOOLKIT
10539 /* Atimer callback function for TIMER. Called every 0.1s to process
10540 Xt timeouts, if needed. We must avoid calling XtAppPending as
10541 much as possible because that function does an implicit XFlush
10542 that slows us down. */
10544 static void
10545 x_process_timeouts (timer)
10546 struct atimer *timer;
10548 BLOCK_INPUT;
10549 x_timeout_atimer_activated_flag = 0;
10550 if (toolkit_scroll_bar_interaction || popup_activated ())
10552 while (XtAppPending (Xt_app_con) & XtIMTimer)
10553 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10554 /* Reactivate the atimer for next time. */
10555 x_activate_timeout_atimer ();
10557 UNBLOCK_INPUT;
10560 /* Install an asynchronous timer that processes Xt timeout events
10561 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10562 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10563 function whenever these variables are set. This is necessary
10564 because some widget sets use timeouts internally, for example the
10565 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10566 processed, these widgets don't behave normally. */
10568 void
10569 x_activate_timeout_atimer ()
10571 BLOCK_INPUT;
10572 if (!x_timeout_atimer_activated_flag)
10574 EMACS_TIME interval;
10576 EMACS_SET_SECS_USECS (interval, 0, 100000);
10577 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10578 x_timeout_atimer_activated_flag = 1;
10580 UNBLOCK_INPUT;
10583 #endif /* USE_X_TOOLKIT */
10586 /* Set up use of X before we make the first connection. */
10588 extern frame_parm_handler x_frame_parm_handlers[];
10590 static struct redisplay_interface x_redisplay_interface =
10592 x_frame_parm_handlers,
10593 x_produce_glyphs,
10594 x_write_glyphs,
10595 x_insert_glyphs,
10596 x_clear_end_of_line,
10597 x_scroll_run,
10598 x_after_update_window_line,
10599 x_update_window_begin,
10600 x_update_window_end,
10601 x_cursor_to,
10602 x_flush,
10603 #ifdef XFlush
10604 x_flush,
10605 #else
10606 0, /* flush_display_optional */
10607 #endif
10608 x_clear_window_mouse_face,
10609 x_get_glyph_overhangs,
10610 x_fix_overlapping_area,
10611 x_draw_fringe_bitmap,
10612 0, /* define_fringe_bitmap */
10613 0, /* destroy_fringe_bitmap */
10614 x_compute_glyph_string_overhangs,
10615 x_draw_glyph_string,
10616 x_define_frame_cursor,
10617 x_clear_frame_area,
10618 x_draw_window_cursor,
10619 x_draw_vertical_window_border,
10620 x_shift_glyphs_for_insert
10624 /* This function is called when the last frame on a display is deleted. */
10625 void
10626 x_delete_terminal (struct terminal *terminal)
10628 struct x_display_info *dpyinfo = terminal->display_info.x;
10629 int i;
10631 /* Protect against recursive calls. delete_frame in
10632 delete_terminal calls us back when it deletes our last frame. */
10633 if (!terminal->name)
10634 return;
10636 BLOCK_INPUT;
10637 #ifdef HAVE_X_I18N
10638 /* We must close our connection to the XIM server before closing the
10639 X display. */
10640 if (dpyinfo->xim)
10641 xim_close_dpy (dpyinfo);
10642 #endif
10644 /* If called from x_connection_closed, the display may already be closed
10645 and dpyinfo->display was set to 0 to indicate that. */
10646 if (dpyinfo->display)
10648 x_destroy_all_bitmaps (dpyinfo);
10649 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10651 #ifdef USE_GTK
10652 xg_display_close (dpyinfo->display);
10653 #else
10654 #ifdef USE_X_TOOLKIT
10655 XtCloseDisplay (dpyinfo->display);
10656 #else
10657 XCloseDisplay (dpyinfo->display);
10658 #endif
10659 #endif /* ! USE_GTK */
10662 x_delete_display (dpyinfo);
10663 UNBLOCK_INPUT;
10666 /* Create a struct terminal, initialize it with the X11 specific
10667 functions and make DISPLAY->TERMINAL point to it. */
10669 static struct terminal *
10670 x_create_terminal (struct x_display_info *dpyinfo)
10672 struct terminal *terminal;
10674 terminal = create_terminal ();
10676 terminal->type = output_x_window;
10677 terminal->display_info.x = dpyinfo;
10678 dpyinfo->terminal = terminal;
10680 /* kboard is initialized in x_term_init. */
10682 terminal->clear_frame_hook = x_clear_frame;
10683 terminal->ins_del_lines_hook = x_ins_del_lines;
10684 terminal->delete_glyphs_hook = x_delete_glyphs;
10685 terminal->ring_bell_hook = XTring_bell;
10686 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
10687 terminal->set_terminal_modes_hook = XTset_terminal_modes;
10688 terminal->update_begin_hook = x_update_begin;
10689 terminal->update_end_hook = x_update_end;
10690 terminal->set_terminal_window_hook = XTset_terminal_window;
10691 terminal->read_socket_hook = XTread_socket;
10692 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10693 terminal->mouse_position_hook = XTmouse_position;
10694 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10695 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10696 terminal->fullscreen_hook = XTfullscreen_hook;
10697 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10698 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10699 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10700 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10702 terminal->delete_frame_hook = x_destroy_window;
10703 terminal->delete_terminal_hook = x_delete_terminal;
10705 terminal->rif = &x_redisplay_interface;
10706 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10707 terminal->char_ins_del_ok = 1;
10708 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10709 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10710 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10711 off the bottom. */
10713 return terminal;
10716 void
10717 x_initialize ()
10719 baud_rate = 19200;
10721 x_noop_count = 0;
10722 last_tool_bar_item = -1;
10723 any_help_event_p = 0;
10724 ignore_next_mouse_click_timeout = 0;
10725 #ifdef HAVE_X_SM
10726 x_session_initialized = 0;
10727 #endif
10729 #ifdef USE_GTK
10730 current_count = -1;
10731 #endif
10733 /* Try to use interrupt input; if we can't, then start polling. */
10734 Fset_input_interrupt_mode (Qt);
10736 #ifdef USE_X_TOOLKIT
10737 XtToolkitInitialize ();
10739 Xt_app_con = XtCreateApplicationContext ();
10741 /* Register a converter from strings to pixels, which uses
10742 Emacs' color allocation infrastructure. */
10743 XtAppSetTypeConverter (Xt_app_con,
10744 XtRString, XtRPixel, cvt_string_to_pixel,
10745 cvt_string_to_pixel_args,
10746 XtNumber (cvt_string_to_pixel_args),
10747 XtCacheByDisplay, cvt_pixel_dtor);
10749 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10750 #endif
10752 #ifdef USE_TOOLKIT_SCROLL_BARS
10753 #ifndef USE_GTK
10754 xaw3d_arrow_scroll = False;
10755 xaw3d_pick_top = True;
10756 #endif
10757 #endif
10759 /* Note that there is no real way portable across R3/R4 to get the
10760 original error handler. */
10761 XSetErrorHandler (x_error_handler);
10762 XSetIOErrorHandler (x_io_error_quitter);
10764 /* Disable Window Change signals; they are handled by X events. */
10765 #if 0 /* Don't. We may want to open tty frames later. */
10766 #ifdef SIGWINCH
10767 signal (SIGWINCH, SIG_DFL);
10768 #endif /* SIGWINCH */
10769 #endif
10771 signal (SIGPIPE, x_connection_signal);
10775 void
10776 syms_of_xterm ()
10778 x_error_message = NULL;
10780 staticpro (&x_display_name_list);
10781 x_display_name_list = Qnil;
10783 staticpro (&last_mouse_scroll_bar);
10784 last_mouse_scroll_bar = Qnil;
10786 staticpro (&Qvendor_specific_keysyms);
10787 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10789 staticpro (&Qlatin_1);
10790 Qlatin_1 = intern ("latin-1");
10792 staticpro (&last_mouse_press_frame);
10793 last_mouse_press_frame = Qnil;
10795 DEFVAR_BOOL ("x-use-underline-position-properties",
10796 &x_use_underline_position_properties,
10797 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10798 A value of nil means ignore them. If you encounter fonts with bogus
10799 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10800 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10801 to override the font's UNDERLINE_POSITION for small font display
10802 sizes. */);
10803 x_use_underline_position_properties = 1;
10805 DEFVAR_BOOL ("x-underline-at-descent-line",
10806 &x_underline_at_descent_line,
10807 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
10808 A value of nil means to draw the underline according to the value of the
10809 variable `x-use-underline-position-properties', which is usually at the
10810 baseline level. The default value is nil. */);
10811 x_underline_at_descent_line = 0;
10813 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10814 &x_mouse_click_focus_ignore_position,
10815 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10816 This variable is only used when the window manager requires that you
10817 click on a frame to select it (give it focus). In that case, a value
10818 of nil, means that the selected window and cursor position changes to
10819 reflect the mouse click position, while a non-nil value means that the
10820 selected window or cursor position is preserved. */);
10821 x_mouse_click_focus_ignore_position = 0;
10823 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10824 doc: /* What X toolkit scroll bars Emacs uses.
10825 A value of nil means Emacs doesn't use X toolkit scroll bars.
10826 Otherwise, value is a symbol describing the X toolkit. */);
10827 #ifdef USE_TOOLKIT_SCROLL_BARS
10828 #ifdef USE_MOTIF
10829 Vx_toolkit_scroll_bars = intern ("motif");
10830 #elif defined HAVE_XAW3D
10831 Vx_toolkit_scroll_bars = intern ("xaw3d");
10832 #elif USE_GTK
10833 Vx_toolkit_scroll_bars = intern ("gtk");
10834 #else
10835 Vx_toolkit_scroll_bars = intern ("xaw");
10836 #endif
10837 #else
10838 Vx_toolkit_scroll_bars = Qnil;
10839 #endif
10841 staticpro (&last_mouse_motion_frame);
10842 last_mouse_motion_frame = Qnil;
10844 Qmodifier_value = intern ("modifier-value");
10845 Qalt = intern ("alt");
10846 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10847 Qhyper = intern ("hyper");
10848 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10849 Qmeta = intern ("meta");
10850 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10851 Qsuper = intern ("super");
10852 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10854 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10855 doc: /* Which keys Emacs uses for the alt modifier.
10856 This should be one of the symbols `alt', `hyper', `meta', `super'.
10857 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10858 is nil, which is the same as `alt'. */);
10859 Vx_alt_keysym = Qnil;
10861 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
10862 doc: /* Which keys Emacs uses for the hyper modifier.
10863 This should be one of the symbols `alt', `hyper', `meta', `super'.
10864 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10865 default is nil, which is the same as `hyper'. */);
10866 Vx_hyper_keysym = Qnil;
10868 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
10869 doc: /* Which keys Emacs uses for the meta modifier.
10870 This should be one of the symbols `alt', `hyper', `meta', `super'.
10871 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10872 default is nil, which is the same as `meta'. */);
10873 Vx_meta_keysym = Qnil;
10875 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
10876 doc: /* Which keys Emacs uses for the super modifier.
10877 This should be one of the symbols `alt', `hyper', `meta', `super'.
10878 For example, `super' means use the Super_L and Super_R keysyms. The
10879 default is nil, which is the same as `super'. */);
10880 Vx_super_keysym = Qnil;
10882 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
10883 doc: /* Hash table of character codes indexed by X keysym codes. */);
10884 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
10885 make_float (DEFAULT_REHASH_SIZE),
10886 make_float (DEFAULT_REHASH_THRESHOLD),
10887 Qnil, Qnil, Qnil);
10890 #endif /* HAVE_X_WINDOWS */
10892 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
10893 (do not change this comment) */