Tweak previous doc fix.
[emacs.git] / src / xterm.c
blob068129672f7a6257156727b4be2d8c2ae827cae3
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 return -1;
7144 interrupt_input_pending = 0;
7145 BLOCK_INPUT;
7147 /* So people can tell when we have read the available input. */
7148 input_signal_count++;
7150 ++handling_signal;
7152 #ifdef HAVE_X_SM
7153 /* Only check session manager input for the primary display. */
7154 if (terminal->id == 1 && x_session_have_connection ())
7156 struct input_event inev;
7157 BLOCK_INPUT;
7158 /* We don't need to EVENT_INIT (inev) here, as
7159 x_session_check_input copies an entire input_event. */
7160 if (x_session_check_input (&inev))
7162 kbd_buffer_store_event_hold (&inev, hold_quit);
7163 count++;
7165 UNBLOCK_INPUT;
7167 #endif
7169 /* For debugging, this gives a way to fake an I/O error. */
7170 if (terminal->display_info.x == XTread_socket_fake_io_error)
7172 XTread_socket_fake_io_error = 0;
7173 x_io_error_quitter (terminal->display_info.x->display);
7176 #if 0 /* This loop is a noop now. */
7177 /* Find the display we are supposed to read input for.
7178 It's the one communicating on descriptor SD. */
7179 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7181 #if 0 /* This ought to be unnecessary; let's verify it. */
7182 #ifdef FIOSNBIO
7183 /* If available, Xlib uses FIOSNBIO to make the socket
7184 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7185 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7186 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7187 fcntl (dpyinfo->connection, F_SETFL, 0);
7188 #endif /* ! defined (FIOSNBIO) */
7189 #endif
7191 #if 0 /* This code can't be made to work, with multiple displays,
7192 and appears not to be used on any system any more.
7193 Also keyboard.c doesn't turn O_NDELAY on and off
7194 for X connections. */
7195 #ifndef SIGIO
7196 #ifndef HAVE_SELECT
7197 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7199 extern int read_alarm_should_throw;
7200 read_alarm_should_throw = 1;
7201 XPeekEvent (dpyinfo->display, &event);
7202 read_alarm_should_throw = 0;
7204 #endif /* HAVE_SELECT */
7205 #endif /* SIGIO */
7206 #endif
7208 #endif
7210 #ifndef USE_GTK
7211 while (XPending (terminal->display_info.x->display))
7213 int finish;
7215 XNextEvent (terminal->display_info.x->display, &event);
7217 #ifdef HAVE_X_I18N
7218 /* Filter events for the current X input method. */
7219 if (x_filter_event (terminal->display_info.x, &event))
7220 break;
7221 #endif
7222 event_found = 1;
7224 count += handle_one_xevent (terminal->display_info.x,
7225 &event, &finish, hold_quit);
7227 if (finish == X_EVENT_GOTO_OUT)
7228 goto out;
7231 #else /* USE_GTK */
7233 /* For GTK we must use the GTK event loop. But XEvents gets passed
7234 to our filter function above, and then to the big event switch.
7235 We use a bunch of globals to communicate with our filter function,
7236 that is kind of ugly, but it works.
7238 There is no way to do one display at the time, GTK just does events
7239 from all displays. */
7241 while (gtk_events_pending ())
7243 current_count = count;
7244 current_hold_quit = hold_quit;
7246 gtk_main_iteration ();
7248 count = current_count;
7249 current_count = -1;
7250 current_hold_quit = 0;
7252 if (current_finish == X_EVENT_GOTO_OUT)
7253 break;
7255 #endif /* USE_GTK */
7257 out:;
7259 /* On some systems, an X bug causes Emacs to get no more events
7260 when the window is destroyed. Detect that. (1994.) */
7261 if (! event_found)
7263 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7264 One XNOOP in 100 loops will make Emacs terminate.
7265 B. Bretthauer, 1994 */
7266 x_noop_count++;
7267 if (x_noop_count >= 100)
7269 x_noop_count=0;
7271 if (next_noop_dpyinfo == 0)
7272 next_noop_dpyinfo = x_display_list;
7274 XNoOp (next_noop_dpyinfo->display);
7276 /* Each time we get here, cycle through the displays now open. */
7277 next_noop_dpyinfo = next_noop_dpyinfo->next;
7281 /* If the focus was just given to an auto-raising frame,
7282 raise it now. */
7283 /* ??? This ought to be able to handle more than one such frame. */
7284 if (pending_autoraise_frame)
7286 x_raise_frame (pending_autoraise_frame);
7287 pending_autoraise_frame = 0;
7290 --handling_signal;
7291 UNBLOCK_INPUT;
7293 return count;
7299 /***********************************************************************
7300 Text Cursor
7301 ***********************************************************************/
7303 /* Set clipping for output in glyph row ROW. W is the window in which
7304 we operate. GC is the graphics context to set clipping in.
7306 ROW may be a text row or, e.g., a mode line. Text rows must be
7307 clipped to the interior of the window dedicated to text display,
7308 mode lines must be clipped to the whole window. */
7310 static void
7311 x_clip_to_row (w, row, area, gc)
7312 struct window *w;
7313 struct glyph_row *row;
7314 int area;
7315 GC gc;
7317 struct frame *f = XFRAME (WINDOW_FRAME (w));
7318 XRectangle clip_rect;
7319 int window_x, window_y, window_width;
7321 window_box (w, area, &window_x, &window_y, &window_width, 0);
7323 clip_rect.x = window_x;
7324 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7325 clip_rect.y = max (clip_rect.y, window_y);
7326 clip_rect.width = window_width;
7327 clip_rect.height = row->visible_height;
7329 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7333 /* Draw a hollow box cursor on window W in glyph row ROW. */
7335 static void
7336 x_draw_hollow_cursor (w, row)
7337 struct window *w;
7338 struct glyph_row *row;
7340 struct frame *f = XFRAME (WINDOW_FRAME (w));
7341 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7342 Display *dpy = FRAME_X_DISPLAY (f);
7343 int x, y, wd, h;
7344 XGCValues xgcv;
7345 struct glyph *cursor_glyph;
7346 GC gc;
7348 /* Get the glyph the cursor is on. If we can't tell because
7349 the current matrix is invalid or such, give up. */
7350 cursor_glyph = get_phys_cursor_glyph (w);
7351 if (cursor_glyph == NULL)
7352 return;
7354 /* Compute frame-relative coordinates for phys cursor. */
7355 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7356 wd = w->phys_cursor_width;
7358 /* The foreground of cursor_gc is typically the same as the normal
7359 background color, which can cause the cursor box to be invisible. */
7360 xgcv.foreground = f->output_data.x->cursor_pixel;
7361 if (dpyinfo->scratch_cursor_gc)
7362 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7363 else
7364 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7365 GCForeground, &xgcv);
7366 gc = dpyinfo->scratch_cursor_gc;
7368 /* Set clipping, draw the rectangle, and reset clipping again. */
7369 x_clip_to_row (w, row, TEXT_AREA, gc);
7370 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7371 XSetClipMask (dpy, gc, None);
7375 /* Draw a bar cursor on window W in glyph row ROW.
7377 Implementation note: One would like to draw a bar cursor with an
7378 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7379 Unfortunately, I didn't find a font yet that has this property set.
7380 --gerd. */
7382 static void
7383 x_draw_bar_cursor (w, row, width, kind)
7384 struct window *w;
7385 struct glyph_row *row;
7386 int width;
7387 enum text_cursor_kinds kind;
7389 struct frame *f = XFRAME (w->frame);
7390 struct glyph *cursor_glyph;
7392 /* If cursor is out of bounds, don't draw garbage. This can happen
7393 in mini-buffer windows when switching between echo area glyphs
7394 and mini-buffer. */
7395 cursor_glyph = get_phys_cursor_glyph (w);
7396 if (cursor_glyph == NULL)
7397 return;
7399 /* If on an image, draw like a normal cursor. That's usually better
7400 visible than drawing a bar, esp. if the image is large so that
7401 the bar might not be in the window. */
7402 if (cursor_glyph->type == IMAGE_GLYPH)
7404 struct glyph_row *row;
7405 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7406 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7408 else
7410 Display *dpy = FRAME_X_DISPLAY (f);
7411 Window window = FRAME_X_WINDOW (f);
7412 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7413 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7414 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7415 XGCValues xgcv;
7417 /* If the glyph's background equals the color we normally draw
7418 the bar cursor in, the bar cursor in its normal color is
7419 invisible. Use the glyph's foreground color instead in this
7420 case, on the assumption that the glyph's colors are chosen so
7421 that the glyph is legible. */
7422 if (face->background == f->output_data.x->cursor_pixel)
7423 xgcv.background = xgcv.foreground = face->foreground;
7424 else
7425 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7426 xgcv.graphics_exposures = 0;
7428 if (gc)
7429 XChangeGC (dpy, gc, mask, &xgcv);
7430 else
7432 gc = XCreateGC (dpy, window, mask, &xgcv);
7433 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7436 if (width < 0)
7437 width = FRAME_CURSOR_WIDTH (f);
7438 width = min (cursor_glyph->pixel_width, width);
7440 w->phys_cursor_width = width;
7441 x_clip_to_row (w, row, TEXT_AREA, gc);
7443 if (kind == BAR_CURSOR)
7444 XFillRectangle (dpy, window, gc,
7445 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7446 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7447 width, row->height);
7448 else
7449 XFillRectangle (dpy, window, gc,
7450 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7451 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7452 row->height - width),
7453 min (FRAME_COLUMN_WIDTH (f), cursor_glyph->pixel_width),
7454 width);
7456 XSetClipMask (dpy, gc, None);
7461 /* RIF: Define cursor CURSOR on frame F. */
7463 static void
7464 x_define_frame_cursor (f, cursor)
7465 struct frame *f;
7466 Cursor cursor;
7468 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7472 /* RIF: Clear area on frame F. */
7474 static void
7475 x_clear_frame_area (f, x, y, width, height)
7476 struct frame *f;
7477 int x, y, width, height;
7479 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7480 x, y, width, height, False);
7484 /* RIF: Draw cursor on window W. */
7486 static void
7487 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7488 struct window *w;
7489 struct glyph_row *glyph_row;
7490 int x, y;
7491 int cursor_type, cursor_width;
7492 int on_p, active_p;
7494 struct frame *f = XFRAME (WINDOW_FRAME (w));
7496 if (on_p)
7498 w->phys_cursor_type = cursor_type;
7499 w->phys_cursor_on_p = 1;
7501 if (glyph_row->exact_window_width_line_p
7502 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7504 glyph_row->cursor_in_fringe_p = 1;
7505 draw_fringe_bitmap (w, glyph_row, 0);
7507 else
7508 switch (cursor_type)
7510 case HOLLOW_BOX_CURSOR:
7511 x_draw_hollow_cursor (w, glyph_row);
7512 break;
7514 case FILLED_BOX_CURSOR:
7515 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7516 break;
7518 case BAR_CURSOR:
7519 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7520 break;
7522 case HBAR_CURSOR:
7523 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7524 break;
7526 case NO_CURSOR:
7527 w->phys_cursor_width = 0;
7528 break;
7530 default:
7531 abort ();
7534 #ifdef HAVE_X_I18N
7535 if (w == XWINDOW (f->selected_window))
7536 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7537 xic_set_preeditarea (w, x, y);
7538 #endif
7541 #ifndef XFlush
7542 XFlush (FRAME_X_DISPLAY (f));
7543 #endif
7547 /* Icons. */
7549 /* Make the x-window of frame F use the gnu icon bitmap. */
7552 x_bitmap_icon (f, file)
7553 struct frame *f;
7554 Lisp_Object file;
7556 int bitmap_id;
7558 if (FRAME_X_WINDOW (f) == 0)
7559 return 1;
7561 /* Free up our existing icon bitmap and mask if any. */
7562 if (f->output_data.x->icon_bitmap > 0)
7563 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7564 f->output_data.x->icon_bitmap = 0;
7566 if (STRINGP (file))
7568 #ifdef USE_GTK
7569 /* Use gtk_window_set_icon_from_file () if available,
7570 It's not restricted to bitmaps */
7571 if (xg_set_icon (f, file))
7572 return 0;
7573 #endif /* USE_GTK */
7574 bitmap_id = x_create_bitmap_from_file (f, file);
7575 x_create_bitmap_mask (f, bitmap_id);
7577 else
7579 /* Create the GNU bitmap and mask if necessary. */
7580 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7582 int rc = -1;
7584 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7585 #ifdef USE_GTK
7586 if (xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7587 return 0;
7588 #else
7589 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7590 if (rc != -1)
7591 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7592 #endif /* USE_GTK */
7593 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
7595 /* If all else fails, use the (black and white) xbm image. */
7596 if (rc == -1)
7598 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7599 gnu_xbm_width, gnu_xbm_height);
7600 if (rc == -1)
7601 return 1;
7603 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7604 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7608 /* The first time we create the GNU bitmap and mask,
7609 this increments the ref-count one extra time.
7610 As a result, the GNU bitmap and mask are never freed.
7611 That way, we don't have to worry about allocating it again. */
7612 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7614 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7617 x_wm_set_icon_pixmap (f, bitmap_id);
7618 f->output_data.x->icon_bitmap = bitmap_id;
7620 return 0;
7624 /* Make the x-window of frame F use a rectangle with text.
7625 Use ICON_NAME as the text. */
7628 x_text_icon (f, icon_name)
7629 struct frame *f;
7630 char *icon_name;
7632 if (FRAME_X_WINDOW (f) == 0)
7633 return 1;
7636 XTextProperty text;
7637 text.value = (unsigned char *) icon_name;
7638 text.encoding = XA_STRING;
7639 text.format = 8;
7640 text.nitems = strlen (icon_name);
7641 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7644 if (f->output_data.x->icon_bitmap > 0)
7645 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7646 f->output_data.x->icon_bitmap = 0;
7647 x_wm_set_icon_pixmap (f, 0);
7649 return 0;
7652 #define X_ERROR_MESSAGE_SIZE 200
7654 /* If non-nil, this should be a string.
7655 It means catch X errors and store the error message in this string.
7657 The reason we use a stack is that x_catch_error/x_uncatch_error can
7658 be called from a signal handler.
7661 struct x_error_message_stack {
7662 char string[X_ERROR_MESSAGE_SIZE];
7663 Display *dpy;
7664 struct x_error_message_stack *prev;
7666 static struct x_error_message_stack *x_error_message;
7668 /* An X error handler which stores the error message in
7669 *x_error_message. This is called from x_error_handler if
7670 x_catch_errors is in effect. */
7672 static void
7673 x_error_catcher (display, error)
7674 Display *display;
7675 XErrorEvent *error;
7677 XGetErrorText (display, error->error_code,
7678 x_error_message->string,
7679 X_ERROR_MESSAGE_SIZE);
7682 /* Begin trapping X errors for display DPY. Actually we trap X errors
7683 for all displays, but DPY should be the display you are actually
7684 operating on.
7686 After calling this function, X protocol errors no longer cause
7687 Emacs to exit; instead, they are recorded in the string
7688 stored in *x_error_message.
7690 Calling x_check_errors signals an Emacs error if an X error has
7691 occurred since the last call to x_catch_errors or x_check_errors.
7693 Calling x_uncatch_errors resumes the normal error handling. */
7695 void x_check_errors ();
7697 void
7698 x_catch_errors (dpy)
7699 Display *dpy;
7701 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7703 /* Make sure any errors from previous requests have been dealt with. */
7704 XSync (dpy, False);
7706 data->dpy = dpy;
7707 data->string[0] = 0;
7708 data->prev = x_error_message;
7709 x_error_message = data;
7712 /* Undo the last x_catch_errors call.
7713 DPY should be the display that was passed to x_catch_errors. */
7715 void
7716 x_uncatch_errors ()
7718 struct x_error_message_stack *tmp;
7720 BLOCK_INPUT;
7722 /* The display may have been closed before this function is called.
7723 Check if it is still open before calling XSync. */
7724 if (x_display_info_for_display (x_error_message->dpy) != 0)
7725 XSync (x_error_message->dpy, False);
7727 tmp = x_error_message;
7728 x_error_message = x_error_message->prev;
7729 xfree (tmp);
7730 UNBLOCK_INPUT;
7733 /* If any X protocol errors have arrived since the last call to
7734 x_catch_errors or x_check_errors, signal an Emacs error using
7735 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7737 void
7738 x_check_errors (dpy, format)
7739 Display *dpy;
7740 char *format;
7742 /* Make sure to catch any errors incurred so far. */
7743 XSync (dpy, False);
7745 if (x_error_message->string[0])
7747 char string[X_ERROR_MESSAGE_SIZE];
7748 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7749 x_uncatch_errors ();
7750 error (format, string);
7754 /* Nonzero if we had any X protocol errors
7755 since we did x_catch_errors on DPY. */
7758 x_had_errors_p (dpy)
7759 Display *dpy;
7761 /* Make sure to catch any errors incurred so far. */
7762 XSync (dpy, False);
7764 return x_error_message->string[0] != 0;
7767 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7769 void
7770 x_clear_errors (dpy)
7771 Display *dpy;
7773 x_error_message->string[0] = 0;
7776 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7777 * idea. --lorentey */
7778 /* Close off all unclosed x_catch_errors calls. */
7780 void
7781 x_fully_uncatch_errors ()
7783 while (x_error_message)
7784 x_uncatch_errors ();
7786 #endif
7788 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7791 x_catching_errors ()
7793 return x_error_message != 0;
7796 #if 0
7797 static unsigned int x_wire_count;
7798 x_trace_wire ()
7800 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7802 #endif /* ! 0 */
7805 /* Handle SIGPIPE, which can happen when the connection to a server
7806 simply goes away. SIGPIPE is handled by x_connection_signal.
7807 Don't need to do anything, because the write which caused the
7808 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7809 which will do the appropriate cleanup for us. */
7811 static SIGTYPE
7812 x_connection_signal (signalnum) /* If we don't have an argument, */
7813 int signalnum; /* some compilers complain in signal calls. */
7815 #ifdef USG
7816 /* USG systems forget handlers when they are used;
7817 must reestablish each time */
7818 signal (signalnum, x_connection_signal);
7819 #endif /* USG */
7823 /************************************************************************
7824 Handling X errors
7825 ************************************************************************/
7827 /* Error message passed to x_connection_closed. */
7829 static char *error_msg;
7831 /* Function installed as fatal_error_signal_hook in
7832 x_connection_closed. Print the X error message, and exit normally,
7833 instead of dumping core when XtCloseDisplay fails. */
7835 static void
7836 x_fatal_error_signal ()
7838 fprintf (stderr, "%s\n", error_msg);
7839 exit (70);
7842 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7843 the text of an error message that lead to the connection loss. */
7845 static SIGTYPE
7846 x_connection_closed (dpy, error_message)
7847 Display *dpy;
7848 char *error_message;
7850 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7851 Lisp_Object frame, tail;
7852 int index = SPECPDL_INDEX ();
7854 error_msg = (char *) alloca (strlen (error_message) + 1);
7855 strcpy (error_msg, error_message);
7856 handling_signal = 0;
7858 /* Prevent being called recursively because of an error condition
7859 below. Otherwise, we might end up with printing ``can't find per
7860 display information'' in the recursive call instead of printing
7861 the original message here. */
7862 x_catch_errors (dpy);
7864 /* Inhibit redisplay while frames are being deleted. */
7865 specbind (Qinhibit_redisplay, Qt);
7867 if (dpyinfo)
7869 /* Protect display from being closed when we delete the last
7870 frame on it. */
7871 dpyinfo->reference_count++;
7872 dpyinfo->terminal->reference_count++;
7875 /* First delete frames whose mini-buffers are on frames
7876 that are on the dead display. */
7877 FOR_EACH_FRAME (tail, frame)
7879 Lisp_Object minibuf_frame;
7880 minibuf_frame
7881 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7882 if (FRAME_X_P (XFRAME (frame))
7883 && FRAME_X_P (XFRAME (minibuf_frame))
7884 && ! EQ (frame, minibuf_frame)
7885 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7886 delete_frame (frame, Qnoelisp);
7889 /* Now delete all remaining frames on the dead display.
7890 We are now sure none of these is used as the mini-buffer
7891 for another frame that we need to delete. */
7892 FOR_EACH_FRAME (tail, frame)
7893 if (FRAME_X_P (XFRAME (frame))
7894 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7896 /* Set this to t so that delete_frame won't get confused
7897 trying to find a replacement. */
7898 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7899 delete_frame (frame, Qnoelisp);
7902 /* We have to close the display to inform Xt that it doesn't
7903 exist anymore. If we don't, Xt will continue to wait for
7904 events from the display. As a consequence, a sequence of
7906 M-x make-frame-on-display RET :1 RET
7907 ...kill the new frame, so that we get an IO error...
7908 M-x make-frame-on-display RET :1 RET
7910 will indefinitely wait in Xt for events for display `:1', opened
7911 in the first call to make-frame-on-display.
7913 Closing the display is reported to lead to a bus error on
7914 OpenWindows in certain situations. I suspect that is a bug
7915 in OpenWindows. I don't know how to circumvent it here. */
7917 if (dpyinfo)
7919 #ifdef USE_X_TOOLKIT
7920 /* If DPYINFO is null, this means we didn't open the display
7921 in the first place, so don't try to close it. */
7923 extern void (*fatal_error_signal_hook) P_ ((void));
7924 fatal_error_signal_hook = x_fatal_error_signal;
7925 XtCloseDisplay (dpy);
7926 fatal_error_signal_hook = NULL;
7928 #endif
7930 #ifdef USE_GTK
7931 /* Due to bugs in some Gtk+ versions, just exit here if this
7932 is the last display/terminal. */
7933 if (terminal_list->next_terminal == NULL)
7935 fprintf (stderr, "%s\n", error_msg);
7936 shut_down_emacs (0, 0, Qnil);
7937 exit (70);
7939 xg_display_close (dpyinfo->display);
7940 #endif
7942 /* Indicate that this display is dead. */
7943 dpyinfo->display = 0;
7945 dpyinfo->reference_count--;
7946 dpyinfo->terminal->reference_count--;
7947 if (dpyinfo->reference_count != 0)
7948 /* We have just closed all frames on this display. */
7949 abort ();
7952 Lisp_Object tmp;
7953 XSETTERMINAL (tmp, dpyinfo->terminal);
7954 Fdelete_terminal (tmp, Qnoelisp);
7958 x_uncatch_errors ();
7960 if (terminal_list == 0)
7962 fprintf (stderr, "%s\n", error_msg);
7963 shut_down_emacs (0, 0, Qnil);
7964 exit (70);
7967 /* Ordinary stack unwind doesn't deal with these. */
7968 #ifdef SIGIO
7969 sigunblock (sigmask (SIGIO));
7970 #endif
7971 sigunblock (sigmask (SIGALRM));
7972 TOTALLY_UNBLOCK_INPUT;
7974 unbind_to (index, Qnil);
7975 clear_waiting_for_input ();
7976 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7977 longjmp), because returning from this function would get us back into
7978 Xlib's code which will directly call `exit'. */
7979 error ("%s", error_msg);
7982 /* We specifically use it before defining it, so that gcc doesn't inline it,
7983 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7984 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7986 /* This is the first-level handler for X protocol errors.
7987 It calls x_error_quitter or x_error_catcher. */
7989 static int
7990 x_error_handler (display, error)
7991 Display *display;
7992 XErrorEvent *error;
7994 if (x_error_message)
7995 x_error_catcher (display, error);
7996 else
7997 x_error_quitter (display, error);
7998 return 0;
8001 /* This is the usual handler for X protocol errors.
8002 It kills all frames on the display that we got the error for.
8003 If that was the only one, it prints an error message and kills Emacs. */
8005 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
8007 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
8008 #define NO_INLINE __attribute__((noinline))
8009 #else
8010 #define NO_INLINE
8011 #endif
8013 /* Some versions of GNU/Linux define noinline in their headers. */
8015 #ifdef noinline
8016 #undef noinline
8017 #endif
8019 /* On older GCC versions, just putting x_error_quitter
8020 after x_error_handler prevents inlining into the former. */
8022 static void NO_INLINE
8023 x_error_quitter (display, error)
8024 Display *display;
8025 XErrorEvent *error;
8027 char buf[256], buf1[356];
8029 /* Ignore BadName errors. They can happen because of fonts
8030 or colors that are not defined. */
8032 if (error->error_code == BadName)
8033 return;
8035 /* Note that there is no real way portable across R3/R4 to get the
8036 original error handler. */
8038 XGetErrorText (display, error->error_code, buf, sizeof (buf));
8039 sprintf (buf1, "X protocol error: %s on protocol request %d",
8040 buf, error->request_code);
8041 x_connection_closed (display, buf1);
8045 /* This is the handler for X IO errors, always.
8046 It kills all frames on the display that we lost touch with.
8047 If that was the only one, it prints an error message and kills Emacs. */
8049 static int
8050 x_io_error_quitter (display)
8051 Display *display;
8053 char buf[256];
8055 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
8056 x_connection_closed (display, buf);
8057 return 0;
8060 /* Changing the font of the frame. */
8062 /* Give frame F the font FONT-OBJECT as its default font. The return
8063 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
8064 frame. If it is negative, generate a new fontset from
8065 FONT-OBJECT. */
8067 Lisp_Object
8068 x_new_font (f, font_object, fontset)
8069 struct frame *f;
8070 Lisp_Object font_object;
8071 int fontset;
8073 struct font *font = XFONT_OBJECT (font_object);
8075 if (fontset < 0)
8076 fontset = fontset_from_font (font_object);
8077 FRAME_FONTSET (f) = fontset;
8078 if (FRAME_FONT (f) == font)
8079 /* This font is already set in frame F. There's nothing more to
8080 do. */
8081 return font_object;
8083 FRAME_FONT (f) = font;
8084 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8085 FRAME_COLUMN_WIDTH (f) = font->average_width;
8086 FRAME_SPACE_WIDTH (f) = font->space_width;
8087 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8089 compute_fringe_widths (f, 1);
8091 /* Compute the scroll bar width in character columns. */
8092 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8094 int wid = FRAME_COLUMN_WIDTH (f);
8095 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8096 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8098 else
8100 int wid = FRAME_COLUMN_WIDTH (f);
8101 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8104 if (FRAME_X_WINDOW (f) != 0)
8106 /* Don't change the size of a tip frame; there's no point in
8107 doing it because it's done in Fx_show_tip, and it leads to
8108 problems because the tip frame has no widget. */
8109 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8110 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8113 #ifdef HAVE_X_I18N
8114 if (FRAME_XIC (f)
8115 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8117 BLOCK_INPUT;
8118 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8119 UNBLOCK_INPUT;
8121 #endif
8123 return font_object;
8127 /***********************************************************************
8128 X Input Methods
8129 ***********************************************************************/
8131 #ifdef HAVE_X_I18N
8133 #ifdef HAVE_X11R6
8135 /* XIM destroy callback function, which is called whenever the
8136 connection to input method XIM dies. CLIENT_DATA contains a
8137 pointer to the x_display_info structure corresponding to XIM. */
8139 static void
8140 xim_destroy_callback (xim, client_data, call_data)
8141 XIM xim;
8142 XPointer client_data;
8143 XPointer call_data;
8145 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8146 Lisp_Object frame, tail;
8148 BLOCK_INPUT;
8150 /* No need to call XDestroyIC.. */
8151 FOR_EACH_FRAME (tail, frame)
8153 struct frame *f = XFRAME (frame);
8154 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8156 FRAME_XIC (f) = NULL;
8157 xic_free_xfontset (f);
8161 /* No need to call XCloseIM. */
8162 dpyinfo->xim = NULL;
8163 XFree (dpyinfo->xim_styles);
8164 UNBLOCK_INPUT;
8167 #endif /* HAVE_X11R6 */
8169 #ifdef HAVE_X11R6
8170 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8171 extern char *XSetIMValues P_ ((XIM, ...));
8172 #endif
8174 /* Open the connection to the XIM server on display DPYINFO.
8175 RESOURCE_NAME is the resource name Emacs uses. */
8177 static void
8178 xim_open_dpy (dpyinfo, resource_name)
8179 struct x_display_info *dpyinfo;
8180 char *resource_name;
8182 XIM xim;
8184 #ifdef HAVE_XIM
8185 if (use_xim)
8187 if (dpyinfo->xim)
8188 XCloseIM (dpyinfo->xim);
8189 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8190 EMACS_CLASS);
8191 dpyinfo->xim = xim;
8193 if (xim)
8195 #ifdef HAVE_X11R6
8196 XIMCallback destroy;
8197 #endif
8199 /* Get supported styles and XIM values. */
8200 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8202 #ifdef HAVE_X11R6
8203 destroy.callback = xim_destroy_callback;
8204 destroy.client_data = (XPointer)dpyinfo;
8205 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8206 #endif
8210 else
8211 #endif /* HAVE_XIM */
8212 dpyinfo->xim = NULL;
8216 #ifdef HAVE_X11R6_XIM
8218 /* XIM instantiate callback function, which is called whenever an XIM
8219 server is available. DISPLAY is the display of the XIM.
8220 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8221 when the callback was registered. */
8223 static void
8224 xim_instantiate_callback (display, client_data, call_data)
8225 Display *display;
8226 XPointer client_data;
8227 XPointer call_data;
8229 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8230 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8232 /* We don't support multiple XIM connections. */
8233 if (dpyinfo->xim)
8234 return;
8236 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8238 /* Create XIC for the existing frames on the same display, as long
8239 as they have no XIC. */
8240 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8242 Lisp_Object tail, frame;
8244 BLOCK_INPUT;
8245 FOR_EACH_FRAME (tail, frame)
8247 struct frame *f = XFRAME (frame);
8249 if (FRAME_X_P (f)
8250 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8251 if (FRAME_XIC (f) == NULL)
8253 create_frame_xic (f);
8254 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8255 xic_set_statusarea (f);
8256 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8258 struct window *w = XWINDOW (f->selected_window);
8259 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8264 UNBLOCK_INPUT;
8268 #endif /* HAVE_X11R6_XIM */
8271 /* Open a connection to the XIM server on display DPYINFO.
8272 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8273 connection only at the first time. On X11R6, open the connection
8274 in the XIM instantiate callback function. */
8276 static void
8277 xim_initialize (dpyinfo, resource_name)
8278 struct x_display_info *dpyinfo;
8279 char *resource_name;
8281 dpyinfo->xim = NULL;
8282 #ifdef HAVE_XIM
8283 if (use_xim)
8285 #ifdef HAVE_X11R6_XIM
8286 struct xim_inst_t *xim_inst;
8287 int len;
8289 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8290 dpyinfo->xim_callback_data = xim_inst;
8291 xim_inst->dpyinfo = dpyinfo;
8292 len = strlen (resource_name);
8293 xim_inst->resource_name = (char *) xmalloc (len + 1);
8294 bcopy (resource_name, xim_inst->resource_name, len + 1);
8295 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8296 resource_name, EMACS_CLASS,
8297 xim_instantiate_callback,
8298 /* This is XPointer in XFree86
8299 but (XPointer *) on Tru64, at
8300 least, hence the configure test. */
8301 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8302 #else /* not HAVE_X11R6_XIM */
8303 xim_open_dpy (dpyinfo, resource_name);
8304 #endif /* not HAVE_X11R6_XIM */
8306 #endif /* HAVE_XIM */
8310 /* Close the connection to the XIM server on display DPYINFO. */
8312 static void
8313 xim_close_dpy (dpyinfo)
8314 struct x_display_info *dpyinfo;
8316 #ifdef HAVE_XIM
8317 if (use_xim)
8319 #ifdef HAVE_X11R6_XIM
8320 if (dpyinfo->display)
8321 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8322 NULL, EMACS_CLASS,
8323 xim_instantiate_callback, NULL);
8324 xfree (dpyinfo->xim_callback_data->resource_name);
8325 xfree (dpyinfo->xim_callback_data);
8326 #endif /* HAVE_X11R6_XIM */
8327 if (dpyinfo->display)
8328 XCloseIM (dpyinfo->xim);
8329 dpyinfo->xim = NULL;
8330 XFree (dpyinfo->xim_styles);
8332 #endif /* HAVE_XIM */
8335 #endif /* not HAVE_X11R6_XIM */
8339 /* Calculate the absolute position in frame F
8340 from its current recorded position values and gravity. */
8342 void
8343 x_calc_absolute_position (f)
8344 struct frame *f;
8346 int flags = f->size_hint_flags;
8348 /* We have nothing to do if the current position
8349 is already for the top-left corner. */
8350 if (! ((flags & XNegative) || (flags & YNegative)))
8351 return;
8353 /* Treat negative positions as relative to the leftmost bottommost
8354 position that fits on the screen. */
8355 if (flags & XNegative)
8356 f->left_pos = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
8357 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8360 int height = FRAME_PIXEL_HEIGHT (f);
8362 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8363 /* Something is fishy here. When using Motif, starting Emacs with
8364 `-g -0-0', the frame appears too low by a few pixels.
8366 This seems to be so because initially, while Emacs is starting,
8367 the column widget's height and the frame's pixel height are
8368 different. The column widget's height is the right one. In
8369 later invocations, when Emacs is up, the frame's pixel height
8370 is right, though.
8372 It's not obvious where the initial small difference comes from.
8373 2000-12-01, gerd. */
8375 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8376 #endif
8378 if (flags & YNegative)
8379 f->top_pos = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
8380 - height + f->top_pos;
8383 /* The left_pos and top_pos
8384 are now relative to the top and left screen edges,
8385 so the flags should correspond. */
8386 f->size_hint_flags &= ~ (XNegative | YNegative);
8389 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8390 to really change the position, and 0 when calling from
8391 x_make_frame_visible (in that case, XOFF and YOFF are the current
8392 position values). It is -1 when calling from x_set_frame_parameters,
8393 which means, do adjust for borders but don't change the gravity. */
8395 void
8396 x_set_offset (f, xoff, yoff, change_gravity)
8397 struct frame *f;
8398 register int xoff, yoff;
8399 int change_gravity;
8401 int modified_top, modified_left;
8403 if (change_gravity > 0)
8405 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8406 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8408 f->top_pos = yoff;
8409 f->left_pos = xoff;
8410 f->size_hint_flags &= ~ (XNegative | YNegative);
8411 if (xoff < 0)
8412 f->size_hint_flags |= XNegative;
8413 if (yoff < 0)
8414 f->size_hint_flags |= YNegative;
8415 f->win_gravity = NorthWestGravity;
8417 x_calc_absolute_position (f);
8419 BLOCK_INPUT;
8420 x_wm_set_size_hint (f, (long) 0, 0);
8422 modified_left = f->left_pos;
8423 modified_top = f->top_pos;
8425 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8427 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8428 than the WM decorations. So we use the calculated offset instead
8429 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8430 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8431 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8434 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8435 modified_left, modified_top);
8437 x_sync_with_move (f, f->left_pos, f->top_pos,
8438 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8439 ? 1 : 0);
8441 /* change_gravity is non-zero when this function is called from Lisp to
8442 programmatically move a frame. In that case, we call
8443 x_check_expected_move to discover if we have a "Type A" or "Type B"
8444 window manager, and, for a "Type A" window manager, adjust the position
8445 of the frame.
8447 We call x_check_expected_move if a programmatic move occurred, and
8448 either the window manager type (A/B) is unknown or it is Type A but we
8449 need to compute the top/left offset adjustment for this frame. */
8451 if (change_gravity != 0 &&
8452 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8453 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8454 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8455 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8456 x_check_expected_move (f, modified_left, modified_top);
8458 UNBLOCK_INPUT;
8461 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8462 on the root window for frame F contains ATOMNAME.
8463 This is how a WM check shall be done according to the Window Manager
8464 Specification/Extended Window Manager Hints at
8465 http://freedesktop.org/wiki/Specifications/wm-spec. */
8467 static int
8468 wm_supports (f, atomname)
8469 struct frame *f;
8470 const char *atomname;
8472 Atom actual_type;
8473 unsigned long actual_size, bytes_remaining;
8474 int i, rc, actual_format;
8475 Atom prop_atom;
8476 Window wmcheck_window;
8477 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8478 Window target_window = dpyinfo->root_window;
8479 long max_len = 65536;
8480 Display *dpy = FRAME_X_DISPLAY (f);
8481 unsigned char *tmp_data = NULL;
8482 Atom target_type = XA_WINDOW;
8483 Atom want_atom;
8485 BLOCK_INPUT;
8487 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8489 x_catch_errors (dpy);
8490 rc = XGetWindowProperty (dpy, target_window,
8491 prop_atom, 0, max_len, False, target_type,
8492 &actual_type, &actual_format, &actual_size,
8493 &bytes_remaining, &tmp_data);
8495 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8497 if (tmp_data) XFree (tmp_data);
8498 x_uncatch_errors ();
8499 UNBLOCK_INPUT;
8500 return 0;
8503 wmcheck_window = *(Window *) tmp_data;
8504 XFree (tmp_data);
8506 /* Check if window exists. */
8507 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8508 x_sync (f);
8509 if (x_had_errors_p (dpy))
8511 x_uncatch_errors ();
8512 UNBLOCK_INPUT;
8513 return 0;
8516 if (dpyinfo->net_supported_window != wmcheck_window)
8518 /* Window changed, reload atoms */
8519 if (dpyinfo->net_supported_atoms != NULL)
8520 XFree (dpyinfo->net_supported_atoms);
8521 dpyinfo->net_supported_atoms = NULL;
8522 dpyinfo->nr_net_supported_atoms = 0;
8523 dpyinfo->net_supported_window = 0;
8525 target_type = XA_ATOM;
8526 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8527 tmp_data = NULL;
8528 rc = XGetWindowProperty (dpy, target_window,
8529 prop_atom, 0, max_len, False, target_type,
8530 &actual_type, &actual_format, &actual_size,
8531 &bytes_remaining, &tmp_data);
8533 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8535 if (tmp_data) XFree (tmp_data);
8536 x_uncatch_errors ();
8537 UNBLOCK_INPUT;
8538 return 0;
8541 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8542 dpyinfo->nr_net_supported_atoms = actual_size;
8543 dpyinfo->net_supported_window = wmcheck_window;
8546 rc = 0;
8547 want_atom = XInternAtom (dpy, atomname, False);
8549 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8550 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8552 x_uncatch_errors ();
8553 UNBLOCK_INPUT;
8555 return rc;
8558 /* Do fullscreen as specified in extended window manager hints */
8560 static int
8561 do_ewmh_fullscreen (f)
8562 struct frame *f;
8564 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8566 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8567 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8568 if (!have_net_atom)
8569 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8571 if (have_net_atom)
8573 Lisp_Object frame;
8574 const char *atom = "_NET_WM_STATE";
8575 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8576 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8577 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8578 const char *what = NULL;
8580 XSETFRAME (frame, f);
8582 /* If there are _NET_ atoms we assume we have extended window manager
8583 hints. */
8584 switch (f->want_fullscreen)
8586 case FULLSCREEN_BOTH:
8587 what = fs;
8588 break;
8589 case FULLSCREEN_WIDTH:
8590 what = fw;
8591 break;
8592 case FULLSCREEN_HEIGHT:
8593 what = fh;
8594 break;
8597 if (what != NULL && !wm_supports (f, what)) return 0;
8600 Fx_send_client_event (frame, make_number (0), frame,
8601 make_unibyte_string (atom, strlen (atom)),
8602 make_number (32),
8603 Fcons (make_number (0), /* Remove */
8604 Fcons
8605 (make_unibyte_string (fs,
8606 strlen (fs)),
8607 Qnil)));
8608 Fx_send_client_event (frame, make_number (0), frame,
8609 make_unibyte_string (atom, strlen (atom)),
8610 make_number (32),
8611 Fcons (make_number (0), /* Remove */
8612 Fcons
8613 (make_unibyte_string (fh,
8614 strlen (fh)),
8615 Qnil)));
8616 Fx_send_client_event (frame, make_number (0), frame,
8617 make_unibyte_string (atom, strlen (atom)),
8618 make_number (32),
8619 Fcons (make_number (0), /* Remove */
8620 Fcons
8621 (make_unibyte_string (fw,
8622 strlen (fw)),
8623 Qnil)));
8624 f->want_fullscreen = FULLSCREEN_NONE;
8625 if (what != NULL)
8626 Fx_send_client_event (frame, make_number (0), frame,
8627 make_unibyte_string (atom, strlen (atom)),
8628 make_number (32),
8629 Fcons (make_number (1), /* Add */
8630 Fcons
8631 (make_unibyte_string (what,
8632 strlen (what)),
8633 Qnil)));
8636 return have_net_atom;
8639 static void
8640 XTfullscreen_hook (f)
8641 FRAME_PTR f;
8643 if (f->async_visible)
8645 BLOCK_INPUT;
8646 do_ewmh_fullscreen (f);
8647 x_sync (f);
8648 UNBLOCK_INPUT;
8653 /* Check if we need to resize the frame due to a fullscreen request.
8654 If so needed, resize the frame. */
8655 static void
8656 x_check_fullscreen (f)
8657 struct frame *f;
8659 if (f->want_fullscreen & FULLSCREEN_BOTH)
8661 int width, height, ign;
8663 if (do_ewmh_fullscreen (f))
8664 return;
8666 x_real_positions (f, &f->left_pos, &f->top_pos);
8668 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8670 /* We do not need to move the window, it shall be taken care of
8671 when setting WM manager hints.
8672 If the frame is visible already, the position is checked by
8673 x_check_expected_move. */
8674 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8676 change_frame_size (f, height, width, 0, 1, 0);
8677 SET_FRAME_GARBAGED (f);
8678 cancel_mouse_face (f);
8680 /* Wait for the change of frame size to occur */
8681 f->want_fullscreen |= FULLSCREEN_WAIT;
8686 /* This function is called by x_set_offset to determine whether the window
8687 manager interfered with the positioning of the frame. Type A window
8688 managers position the surrounding window manager decorations a small
8689 amount above and left of the user-supplied position. Type B window
8690 managers position the surrounding window manager decorations at the
8691 user-specified position. If we detect a Type A window manager, we
8692 compensate by moving the window right and down by the proper amount. */
8694 static void
8695 x_check_expected_move (f, expected_left, expected_top)
8696 struct frame *f;
8697 int expected_left;
8698 int expected_top;
8700 int current_left = 0, current_top = 0;
8702 /* x_real_positions returns the left and top offsets of the outermost
8703 window manager window around the frame. */
8705 x_real_positions (f, &current_left, &current_top);
8707 if (current_left != expected_left || current_top != expected_top)
8709 /* It's a "Type A" window manager. */
8711 int adjusted_left;
8712 int adjusted_top;
8714 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8715 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8716 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8718 /* Now fix the mispositioned frame's location. */
8720 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8721 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8723 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8724 adjusted_left, adjusted_top);
8726 x_sync_with_move (f, expected_left, expected_top, 0);
8728 else
8729 /* It's a "Type B" window manager. We don't have to adjust the
8730 frame's position. */
8732 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8736 /* Wait for XGetGeometry to return up-to-date position information for a
8737 recently-moved frame. Call this immediately after calling XMoveWindow.
8738 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8739 frame has been moved to, so we use a fuzzy position comparison instead
8740 of an exact comparison. */
8742 static void
8743 x_sync_with_move (f, left, top, fuzzy)
8744 struct frame *f;
8745 int left, top, fuzzy;
8747 int count = 0;
8749 while (count++ < 50)
8751 int current_left = 0, current_top = 0;
8753 /* In theory, this call to XSync only needs to happen once, but in
8754 practice, it doesn't seem to work, hence the need for the surrounding
8755 loop. */
8757 XSync (FRAME_X_DISPLAY (f), False);
8758 x_real_positions (f, &current_left, &current_top);
8760 if (fuzzy)
8762 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8763 pixels. */
8765 if (eabs (current_left - left) <= 10
8766 && eabs (current_top - top) <= 40)
8767 return;
8769 else if (current_left == left && current_top == top)
8770 return;
8773 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8774 will then return up-to-date position info. */
8776 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8780 /* Change the size of frame F's X window to COLS/ROWS in the case F
8781 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8782 top-left-corner window gravity for this size change and subsequent
8783 size changes. Otherwise we leave the window gravity unchanged. */
8785 static void
8786 x_set_window_size_1 (f, change_gravity, cols, rows)
8787 struct frame *f;
8788 int change_gravity;
8789 int cols, rows;
8791 int pixelwidth, pixelheight;
8793 check_frame_size (f, &rows, &cols);
8794 f->scroll_bar_actual_width
8795 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8797 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8798 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8799 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8801 compute_fringe_widths (f, 0);
8803 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8804 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8806 f->win_gravity = NorthWestGravity;
8807 x_wm_set_size_hint (f, (long) 0, 0);
8809 XSync (FRAME_X_DISPLAY (f), False);
8810 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8811 pixelwidth, pixelheight);
8813 /* Now, strictly speaking, we can't be sure that this is accurate,
8814 but the window manager will get around to dealing with the size
8815 change request eventually, and we'll hear how it went when the
8816 ConfigureNotify event gets here.
8818 We could just not bother storing any of this information here,
8819 and let the ConfigureNotify event set everything up, but that
8820 might be kind of confusing to the Lisp code, since size changes
8821 wouldn't be reported in the frame parameters until some random
8822 point in the future when the ConfigureNotify event arrives.
8824 We pass 1 for DELAY since we can't run Lisp code inside of
8825 a BLOCK_INPUT. */
8826 change_frame_size (f, rows, cols, 0, 1, 0);
8827 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8828 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8830 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8831 receive in the ConfigureNotify event; if we get what we asked
8832 for, then the event won't cause the screen to become garbaged, so
8833 we have to make sure to do it here. */
8834 SET_FRAME_GARBAGED (f);
8836 XFlush (FRAME_X_DISPLAY (f));
8840 /* Call this to change the size of frame F's x-window.
8841 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8842 for this size change and subsequent size changes.
8843 Otherwise we leave the window gravity unchanged. */
8845 void
8846 x_set_window_size (f, change_gravity, cols, rows)
8847 struct frame *f;
8848 int change_gravity;
8849 int cols, rows;
8851 BLOCK_INPUT;
8853 #ifdef USE_GTK
8854 if (FRAME_GTK_WIDGET (f))
8855 xg_frame_set_char_size (f, cols, rows);
8856 else
8857 x_set_window_size_1 (f, change_gravity, cols, rows);
8858 #elif USE_X_TOOLKIT
8860 if (f->output_data.x->widget != NULL)
8862 /* The x and y position of the widget is clobbered by the
8863 call to XtSetValues within EmacsFrameSetCharSize.
8864 This is a real kludge, but I don't understand Xt so I can't
8865 figure out a correct fix. Can anyone else tell me? -- rms. */
8866 int xpos = f->output_data.x->widget->core.x;
8867 int ypos = f->output_data.x->widget->core.y;
8868 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8869 f->output_data.x->widget->core.x = xpos;
8870 f->output_data.x->widget->core.y = ypos;
8872 else
8873 x_set_window_size_1 (f, change_gravity, cols, rows);
8875 #else /* not USE_X_TOOLKIT */
8877 x_set_window_size_1 (f, change_gravity, cols, rows);
8879 #endif /* not USE_X_TOOLKIT */
8881 /* If cursor was outside the new size, mark it as off. */
8882 mark_window_cursors_off (XWINDOW (f->root_window));
8884 /* Clear out any recollection of where the mouse highlighting was,
8885 since it might be in a place that's outside the new frame size.
8886 Actually checking whether it is outside is a pain in the neck,
8887 so don't try--just let the highlighting be done afresh with new size. */
8888 cancel_mouse_face (f);
8890 UNBLOCK_INPUT;
8893 /* Mouse warping. */
8895 void
8896 x_set_mouse_position (f, x, y)
8897 struct frame *f;
8898 int x, y;
8900 int pix_x, pix_y;
8902 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8903 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8905 if (pix_x < 0) pix_x = 0;
8906 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8908 if (pix_y < 0) pix_y = 0;
8909 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8911 BLOCK_INPUT;
8913 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8914 0, 0, 0, 0, pix_x, pix_y);
8915 UNBLOCK_INPUT;
8918 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8920 void
8921 x_set_mouse_pixel_position (f, pix_x, pix_y)
8922 struct frame *f;
8923 int pix_x, pix_y;
8925 BLOCK_INPUT;
8927 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8928 0, 0, 0, 0, pix_x, pix_y);
8929 UNBLOCK_INPUT;
8932 /* focus shifting, raising and lowering. */
8934 void
8935 x_focus_on_frame (f)
8936 struct frame *f;
8938 #if 0 /* This proves to be unpleasant. */
8939 x_raise_frame (f);
8940 #endif
8941 #if 0
8942 /* I don't think that the ICCCM allows programs to do things like this
8943 without the interaction of the window manager. Whatever you end up
8944 doing with this code, do it to x_unfocus_frame too. */
8945 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8946 RevertToPointerRoot, CurrentTime);
8947 #endif /* ! 0 */
8950 void
8951 x_unfocus_frame (f)
8952 struct frame *f;
8954 #if 0
8955 /* Look at the remarks in x_focus_on_frame. */
8956 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8957 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8958 RevertToPointerRoot, CurrentTime);
8959 #endif /* ! 0 */
8962 /* Raise frame F. */
8964 void
8965 x_raise_frame (f)
8966 struct frame *f;
8968 BLOCK_INPUT;
8969 if (f->async_visible)
8970 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8972 XFlush (FRAME_X_DISPLAY (f));
8973 UNBLOCK_INPUT;
8976 /* Lower frame F. */
8978 void
8979 x_lower_frame (f)
8980 struct frame *f;
8982 if (f->async_visible)
8984 BLOCK_INPUT;
8985 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8986 XFlush (FRAME_X_DISPLAY (f));
8987 UNBLOCK_INPUT;
8991 /* Activate frame with Extended Window Manager Hints */
8993 void
8994 x_ewmh_activate_frame (f)
8995 FRAME_PTR f;
8997 /* See Window Manager Specification/Extended Window Manager Hints at
8998 http://freedesktop.org/wiki/Specifications/wm-spec */
9000 const char *atom = "_NET_ACTIVE_WINDOW";
9001 if (f->async_visible && wm_supports (f, atom))
9003 Lisp_Object frame;
9004 XSETFRAME (frame, f);
9005 Fx_send_client_event (frame, make_number (0), frame,
9006 make_unibyte_string (atom, strlen (atom)),
9007 make_number (32),
9008 Fcons (make_number (1),
9009 Fcons (make_number (last_user_time),
9010 Qnil)));
9014 static void
9015 XTframe_raise_lower (f, raise_flag)
9016 FRAME_PTR f;
9017 int raise_flag;
9019 if (raise_flag)
9020 x_raise_frame (f);
9021 else
9022 x_lower_frame (f);
9025 /* XEmbed implementation. */
9027 void
9028 xembed_set_info (f, flags)
9029 struct frame *f;
9030 enum xembed_info flags;
9032 Atom atom;
9033 unsigned long data[2];
9035 atom = XInternAtom (FRAME_X_DISPLAY (f), "_XEMBED_INFO", False);
9037 data[0] = XEMBED_VERSION;
9038 data[1] = flags;
9040 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), atom, atom,
9041 32, PropModeReplace, (unsigned char *) data, 2);
9044 void
9045 xembed_send_message (f, time, message, detail, data1, data2)
9046 struct frame *f;
9047 Time time;
9048 enum xembed_message message;
9049 long detail;
9050 long data1;
9051 long data2;
9053 XEvent event;
9055 event.xclient.type = ClientMessage;
9056 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9057 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
9058 event.xclient.format = 32;
9059 event.xclient.data.l[0] = time;
9060 event.xclient.data.l[1] = message;
9061 event.xclient.data.l[2] = detail;
9062 event.xclient.data.l[3] = data1;
9063 event.xclient.data.l[4] = data2;
9065 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9066 False, NoEventMask, &event);
9067 XSync (FRAME_X_DISPLAY (f), False);
9070 /* Change of visibility. */
9072 /* This tries to wait until the frame is really visible.
9073 However, if the window manager asks the user where to position
9074 the frame, this will return before the user finishes doing that.
9075 The frame will not actually be visible at that time,
9076 but it will become visible later when the window manager
9077 finishes with it. */
9079 void
9080 x_make_frame_visible (f)
9081 struct frame *f;
9083 Lisp_Object type;
9084 int original_top, original_left;
9085 int retry_count = 2;
9087 retry:
9089 BLOCK_INPUT;
9091 type = x_icon_type (f);
9092 if (!NILP (type))
9093 x_bitmap_icon (f, type);
9095 if (! FRAME_VISIBLE_P (f))
9097 /* We test FRAME_GARBAGED_P here to make sure we don't
9098 call x_set_offset a second time
9099 if we get to x_make_frame_visible a second time
9100 before the window gets really visible. */
9101 if (! FRAME_ICONIFIED_P (f)
9102 && ! FRAME_X_EMBEDDED_P (f)
9103 && ! f->output_data.x->asked_for_visible)
9104 x_set_offset (f, f->left_pos, f->top_pos, 0);
9106 f->output_data.x->asked_for_visible = 1;
9108 if (! EQ (Vx_no_window_manager, Qt))
9109 x_wm_set_window_state (f, NormalState);
9110 #ifdef USE_X_TOOLKIT
9111 if (FRAME_X_EMBEDDED_P (f))
9112 xembed_set_info (f, XEMBED_MAPPED);
9113 else
9115 /* This was XtPopup, but that did nothing for an iconified frame. */
9116 XtMapWidget (f->output_data.x->widget);
9118 #else /* not USE_X_TOOLKIT */
9119 #ifdef USE_GTK
9120 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9121 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9122 #else
9123 if (FRAME_X_EMBEDDED_P (f))
9124 xembed_set_info (f, XEMBED_MAPPED);
9125 else
9126 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9127 #endif /* not USE_GTK */
9128 #endif /* not USE_X_TOOLKIT */
9129 #if 0 /* This seems to bring back scroll bars in the wrong places
9130 if the window configuration has changed. They seem
9131 to come back ok without this. */
9132 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9133 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9134 #endif
9137 XFlush (FRAME_X_DISPLAY (f));
9139 /* Synchronize to ensure Emacs knows the frame is visible
9140 before we do anything else. We do this loop with input not blocked
9141 so that incoming events are handled. */
9143 Lisp_Object frame;
9144 int count;
9145 /* This must be before UNBLOCK_INPUT
9146 since events that arrive in response to the actions above
9147 will set it when they are handled. */
9148 int previously_visible = f->output_data.x->has_been_visible;
9150 original_left = f->left_pos;
9151 original_top = f->top_pos;
9153 /* This must come after we set COUNT. */
9154 UNBLOCK_INPUT;
9156 /* We unblock here so that arriving X events are processed. */
9158 /* Now move the window back to where it was "supposed to be".
9159 But don't do it if the gravity is negative.
9160 When the gravity is negative, this uses a position
9161 that is 3 pixels too low. Perhaps that's really the border width.
9163 Don't do this if the window has never been visible before,
9164 because the window manager may choose the position
9165 and we don't want to override it. */
9167 if (! FRAME_VISIBLE_P (f)
9168 && ! FRAME_ICONIFIED_P (f)
9169 && ! FRAME_X_EMBEDDED_P (f)
9170 && f->win_gravity == NorthWestGravity
9171 && previously_visible)
9173 Drawable rootw;
9174 int x, y;
9175 unsigned int width, height, border, depth;
9177 BLOCK_INPUT;
9179 /* On some window managers (such as FVWM) moving an existing
9180 window, even to the same place, causes the window manager
9181 to introduce an offset. This can cause the window to move
9182 to an unexpected location. Check the geometry (a little
9183 slow here) and then verify that the window is in the right
9184 place. If the window is not in the right place, move it
9185 there, and take the potential window manager hit. */
9186 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9187 &rootw, &x, &y, &width, &height, &border, &depth);
9189 if (original_left != x || original_top != y)
9190 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9191 original_left, original_top);
9193 UNBLOCK_INPUT;
9196 XSETFRAME (frame, f);
9198 /* Wait until the frame is visible. Process X events until a
9199 MapNotify event has been seen, or until we think we won't get a
9200 MapNotify at all.. */
9201 for (count = input_signal_count + 10;
9202 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9204 /* Force processing of queued events. */
9205 x_sync (f);
9207 /* Machines that do polling rather than SIGIO have been
9208 observed to go into a busy-wait here. So we'll fake an
9209 alarm signal to let the handler know that there's something
9210 to be read. We used to raise a real alarm, but it seems
9211 that the handler isn't always enabled here. This is
9212 probably a bug. */
9213 if (input_polling_used ())
9215 /* It could be confusing if a real alarm arrives while
9216 processing the fake one. Turn it off and let the
9217 handler reset it. */
9218 extern void poll_for_input_1 P_ ((void));
9219 int old_poll_suppress_count = poll_suppress_count;
9220 poll_suppress_count = 1;
9221 poll_for_input_1 ();
9222 poll_suppress_count = old_poll_suppress_count;
9225 /* See if a MapNotify event has been processed. */
9226 FRAME_SAMPLE_VISIBILITY (f);
9229 /* 2000-09-28: In
9231 (let ((f (selected-frame)))
9232 (iconify-frame f)
9233 (raise-frame f))
9235 the frame is not raised with various window managers on
9236 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9237 unknown reason, the call to XtMapWidget is completely ignored.
9238 Mapping the widget a second time works. */
9240 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9241 goto retry;
9245 /* Change from mapped state to withdrawn state. */
9247 /* Make the frame visible (mapped and not iconified). */
9249 void
9250 x_make_frame_invisible (f)
9251 struct frame *f;
9253 Window window;
9255 /* Use the frame's outermost window, not the one we normally draw on. */
9256 window = FRAME_OUTER_WINDOW (f);
9258 /* Don't keep the highlight on an invisible frame. */
9259 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9260 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9262 #if 0/* This might add unreliability; I don't trust it -- rms. */
9263 if (! f->async_visible && ! f->async_iconified)
9264 return;
9265 #endif
9267 BLOCK_INPUT;
9269 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9270 that the current position of the window is user-specified, rather than
9271 program-specified, so that when the window is mapped again, it will be
9272 placed at the same location, without forcing the user to position it
9273 by hand again (they have already done that once for this window.) */
9274 x_wm_set_size_hint (f, (long) 0, 1);
9276 #ifdef USE_GTK
9277 if (FRAME_GTK_OUTER_WIDGET (f))
9278 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9279 else
9280 #else
9281 if (FRAME_X_EMBEDDED_P (f))
9282 xembed_set_info (f, 0);
9283 else
9284 #endif
9287 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9288 DefaultScreen (FRAME_X_DISPLAY (f))))
9290 UNBLOCK_INPUT_RESIGNAL;
9291 error ("Can't notify window manager of window withdrawal");
9295 /* We can't distinguish this from iconification
9296 just by the event that we get from the server.
9297 So we can't win using the usual strategy of letting
9298 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9299 and synchronize with the server to make sure we agree. */
9300 f->visible = 0;
9301 FRAME_ICONIFIED_P (f) = 0;
9302 f->async_visible = 0;
9303 f->async_iconified = 0;
9305 x_sync (f);
9307 UNBLOCK_INPUT;
9310 /* Change window state from mapped to iconified. */
9312 void
9313 x_iconify_frame (f)
9314 struct frame *f;
9316 int result;
9317 Lisp_Object type;
9319 /* Don't keep the highlight on an invisible frame. */
9320 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9321 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9323 if (f->async_iconified)
9324 return;
9326 BLOCK_INPUT;
9328 FRAME_SAMPLE_VISIBILITY (f);
9330 type = x_icon_type (f);
9331 if (!NILP (type))
9332 x_bitmap_icon (f, type);
9334 #ifdef USE_GTK
9335 if (FRAME_GTK_OUTER_WIDGET (f))
9337 if (! FRAME_VISIBLE_P (f))
9338 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9340 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9341 f->iconified = 1;
9342 f->visible = 1;
9343 f->async_iconified = 1;
9344 f->async_visible = 0;
9345 UNBLOCK_INPUT;
9346 return;
9348 #endif
9350 #ifdef USE_X_TOOLKIT
9352 if (! FRAME_VISIBLE_P (f))
9354 if (! EQ (Vx_no_window_manager, Qt))
9355 x_wm_set_window_state (f, IconicState);
9356 /* This was XtPopup, but that did nothing for an iconified frame. */
9357 XtMapWidget (f->output_data.x->widget);
9358 /* The server won't give us any event to indicate
9359 that an invisible frame was changed to an icon,
9360 so we have to record it here. */
9361 f->iconified = 1;
9362 f->visible = 1;
9363 f->async_iconified = 1;
9364 f->async_visible = 0;
9365 UNBLOCK_INPUT;
9366 return;
9369 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9370 XtWindow (f->output_data.x->widget),
9371 DefaultScreen (FRAME_X_DISPLAY (f)));
9372 UNBLOCK_INPUT;
9374 if (!result)
9375 error ("Can't notify window manager of iconification");
9377 f->async_iconified = 1;
9378 f->async_visible = 0;
9381 BLOCK_INPUT;
9382 XFlush (FRAME_X_DISPLAY (f));
9383 UNBLOCK_INPUT;
9384 #else /* not USE_X_TOOLKIT */
9386 /* Make sure the X server knows where the window should be positioned,
9387 in case the user deiconifies with the window manager. */
9388 if (! FRAME_VISIBLE_P (f)
9389 && ! FRAME_ICONIFIED_P (f)
9390 && ! FRAME_X_EMBEDDED_P (f))
9391 x_set_offset (f, f->left_pos, f->top_pos, 0);
9393 /* Since we don't know which revision of X we're running, we'll use both
9394 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9396 /* X11R4: send a ClientMessage to the window manager using the
9397 WM_CHANGE_STATE type. */
9399 XEvent message;
9401 message.xclient.window = FRAME_X_WINDOW (f);
9402 message.xclient.type = ClientMessage;
9403 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9404 message.xclient.format = 32;
9405 message.xclient.data.l[0] = IconicState;
9407 if (! XSendEvent (FRAME_X_DISPLAY (f),
9408 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9409 False,
9410 SubstructureRedirectMask | SubstructureNotifyMask,
9411 &message))
9413 UNBLOCK_INPUT_RESIGNAL;
9414 error ("Can't notify window manager of iconification");
9418 /* X11R3: set the initial_state field of the window manager hints to
9419 IconicState. */
9420 x_wm_set_window_state (f, IconicState);
9422 if (!FRAME_VISIBLE_P (f))
9424 /* If the frame was withdrawn, before, we must map it. */
9425 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9428 f->async_iconified = 1;
9429 f->async_visible = 0;
9431 XFlush (FRAME_X_DISPLAY (f));
9432 UNBLOCK_INPUT;
9433 #endif /* not USE_X_TOOLKIT */
9437 /* Free X resources of frame F. */
9439 void
9440 x_free_frame_resources (f)
9441 struct frame *f;
9443 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9444 Lisp_Object bar;
9445 struct scroll_bar *b;
9447 BLOCK_INPUT;
9449 /* If a display connection is dead, don't try sending more
9450 commands to the X server. */
9451 if (dpyinfo->display)
9453 /* We must free faces before destroying windows because some
9454 font-driver (e.g. xft) access a window while finishing a
9455 face. */
9456 if (FRAME_FACE_CACHE (f))
9457 free_frame_faces (f);
9459 if (f->output_data.x->icon_desc)
9460 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9462 #ifdef USE_X_TOOLKIT
9463 /* Explicitly destroy the scroll bars of the frame. Without
9464 this, we get "BadDrawable" errors from the toolkit later on,
9465 presumably from expose events generated for the disappearing
9466 toolkit scroll bars. */
9467 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9469 b = XSCROLL_BAR (bar);
9470 x_scroll_bar_remove (b);
9472 #endif
9474 #ifdef HAVE_X_I18N
9475 if (FRAME_XIC (f))
9476 free_frame_xic (f);
9477 #endif
9479 #ifdef USE_X_TOOLKIT
9480 if (f->output_data.x->widget)
9482 XtDestroyWidget (f->output_data.x->widget);
9483 f->output_data.x->widget = NULL;
9485 /* Tooltips don't have widgets, only a simple X window, even if
9486 we are using a toolkit. */
9487 else if (FRAME_X_WINDOW (f))
9488 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9490 free_frame_menubar (f);
9491 #else /* !USE_X_TOOLKIT */
9493 #ifdef USE_GTK
9494 /* In the GTK version, tooltips are normal X
9495 frames. We must check and free both types. */
9496 if (FRAME_GTK_OUTER_WIDGET (f))
9498 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9499 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9500 FRAME_GTK_OUTER_WIDGET (f) = 0;
9502 #endif /* USE_GTK */
9504 if (FRAME_X_WINDOW (f))
9505 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9506 #endif /* !USE_X_TOOLKIT */
9508 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9509 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9510 unload_color (f, f->output_data.x->cursor_pixel);
9511 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9512 unload_color (f, f->output_data.x->border_pixel);
9513 unload_color (f, f->output_data.x->mouse_pixel);
9515 if (f->output_data.x->scroll_bar_background_pixel != -1)
9516 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9517 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9518 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9519 #ifdef USE_TOOLKIT_SCROLL_BARS
9520 /* Scrollbar shadow colors. */
9521 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9522 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9523 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9524 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9525 #endif /* USE_TOOLKIT_SCROLL_BARS */
9526 if (f->output_data.x->white_relief.allocated_p)
9527 unload_color (f, f->output_data.x->white_relief.pixel);
9528 if (f->output_data.x->black_relief.allocated_p)
9529 unload_color (f, f->output_data.x->black_relief.pixel);
9531 x_free_gcs (f);
9532 XFlush (FRAME_X_DISPLAY (f));
9535 xfree (f->output_data.x->saved_menu_event);
9536 xfree (f->output_data.x);
9537 f->output_data.x = NULL;
9539 if (f == dpyinfo->x_focus_frame)
9540 dpyinfo->x_focus_frame = 0;
9541 if (f == dpyinfo->x_focus_event_frame)
9542 dpyinfo->x_focus_event_frame = 0;
9543 if (f == dpyinfo->x_highlight_frame)
9544 dpyinfo->x_highlight_frame = 0;
9546 if (f == dpyinfo->mouse_face_mouse_frame)
9548 dpyinfo->mouse_face_beg_row
9549 = dpyinfo->mouse_face_beg_col = -1;
9550 dpyinfo->mouse_face_end_row
9551 = dpyinfo->mouse_face_end_col = -1;
9552 dpyinfo->mouse_face_window = Qnil;
9553 dpyinfo->mouse_face_deferred_gc = 0;
9554 dpyinfo->mouse_face_mouse_frame = 0;
9557 UNBLOCK_INPUT;
9561 /* Destroy the X window of frame F. */
9563 void
9564 x_destroy_window (f)
9565 struct frame *f;
9567 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9569 /* If a display connection is dead, don't try sending more
9570 commands to the X server. */
9571 if (dpyinfo->display != 0)
9572 x_free_frame_resources (f);
9574 dpyinfo->reference_count--;
9578 /* Setting window manager hints. */
9580 /* Set the normal size hints for the window manager, for frame F.
9581 FLAGS is the flags word to use--or 0 meaning preserve the flags
9582 that the window now has.
9583 If USER_POSITION is nonzero, we set the USPosition
9584 flag (this is useful when FLAGS is 0).
9585 The GTK version is in gtkutils.c */
9587 #ifndef USE_GTK
9588 void
9589 x_wm_set_size_hint (f, flags, user_position)
9590 struct frame *f;
9591 long flags;
9592 int user_position;
9594 XSizeHints size_hints;
9596 #ifdef USE_X_TOOLKIT
9597 Arg al[2];
9598 int ac = 0;
9599 Dimension widget_width, widget_height;
9600 #endif
9602 Window window = FRAME_OUTER_WINDOW (f);
9604 /* Setting PMaxSize caused various problems. */
9605 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9607 size_hints.x = f->left_pos;
9608 size_hints.y = f->top_pos;
9610 #ifdef USE_X_TOOLKIT
9611 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9612 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9613 XtGetValues (f->output_data.x->widget, al, ac);
9614 size_hints.height = widget_height;
9615 size_hints.width = widget_width;
9616 #else /* not USE_X_TOOLKIT */
9617 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9618 size_hints.width = FRAME_PIXEL_WIDTH (f);
9619 #endif /* not USE_X_TOOLKIT */
9621 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9622 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9623 size_hints.max_width = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
9624 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9625 size_hints.max_height = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
9626 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9628 /* Calculate the base and minimum sizes.
9630 (When we use the X toolkit, we don't do it here.
9631 Instead we copy the values that the widgets are using, below.) */
9632 #ifndef USE_X_TOOLKIT
9634 int base_width, base_height;
9635 int min_rows = 0, min_cols = 0;
9637 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9638 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9640 check_frame_size (f, &min_rows, &min_cols);
9642 /* The window manager uses the base width hints to calculate the
9643 current number of rows and columns in the frame while
9644 resizing; min_width and min_height aren't useful for this
9645 purpose, since they might not give the dimensions for a
9646 zero-row, zero-column frame.
9648 We use the base_width and base_height members if we have
9649 them; otherwise, we set the min_width and min_height members
9650 to the size for a zero x zero frame. */
9652 size_hints.flags |= PBaseSize;
9653 size_hints.base_width = base_width;
9654 size_hints.base_height = base_height;
9655 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9656 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9659 /* If we don't need the old flags, we don't need the old hint at all. */
9660 if (flags)
9662 size_hints.flags |= flags;
9663 goto no_read;
9665 #endif /* not USE_X_TOOLKIT */
9668 XSizeHints hints; /* Sometimes I hate X Windows... */
9669 long supplied_return;
9670 int value;
9672 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9673 &supplied_return);
9675 #ifdef USE_X_TOOLKIT
9676 size_hints.base_height = hints.base_height;
9677 size_hints.base_width = hints.base_width;
9678 size_hints.min_height = hints.min_height;
9679 size_hints.min_width = hints.min_width;
9680 #endif
9682 if (flags)
9683 size_hints.flags |= flags;
9684 else
9686 if (value == 0)
9687 hints.flags = 0;
9688 if (hints.flags & PSize)
9689 size_hints.flags |= PSize;
9690 if (hints.flags & PPosition)
9691 size_hints.flags |= PPosition;
9692 if (hints.flags & USPosition)
9693 size_hints.flags |= USPosition;
9694 if (hints.flags & USSize)
9695 size_hints.flags |= USSize;
9699 #ifndef USE_X_TOOLKIT
9700 no_read:
9701 #endif
9703 #ifdef PWinGravity
9704 size_hints.win_gravity = f->win_gravity;
9705 size_hints.flags |= PWinGravity;
9707 if (user_position)
9709 size_hints.flags &= ~ PPosition;
9710 size_hints.flags |= USPosition;
9712 #endif /* PWinGravity */
9714 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9716 #endif /* not USE_GTK */
9718 /* Used for IconicState or NormalState */
9720 void
9721 x_wm_set_window_state (f, state)
9722 struct frame *f;
9723 int state;
9725 #ifdef USE_X_TOOLKIT
9726 Arg al[1];
9728 XtSetArg (al[0], XtNinitialState, state);
9729 XtSetValues (f->output_data.x->widget, al, 1);
9730 #else /* not USE_X_TOOLKIT */
9731 Window window = FRAME_X_WINDOW (f);
9733 f->output_data.x->wm_hints.flags |= StateHint;
9734 f->output_data.x->wm_hints.initial_state = state;
9736 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9737 #endif /* not USE_X_TOOLKIT */
9740 void
9741 x_wm_set_icon_pixmap (f, pixmap_id)
9742 struct frame *f;
9743 int pixmap_id;
9745 Pixmap icon_pixmap, icon_mask;
9747 #ifndef USE_X_TOOLKIT
9748 Window window = FRAME_OUTER_WINDOW (f);
9749 #endif
9751 if (pixmap_id > 0)
9753 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9754 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9755 icon_mask = x_bitmap_mask (f, pixmap_id);
9756 f->output_data.x->wm_hints.icon_mask = icon_mask;
9758 else
9760 /* It seems there is no way to turn off use of an icon pixmap.
9761 The following line does it, only if no icon has yet been created,
9762 for some window managers. But with mwm it crashes.
9763 Some people say it should clear the IconPixmapHint bit in this case,
9764 but that doesn't work, and the X consortium said it isn't the
9765 right thing at all. Since there is no way to win,
9766 best to explicitly give up. */
9767 #if 0
9768 f->output_data.x->wm_hints.icon_pixmap = None;
9769 f->output_data.x->wm_hints.icon_mask = None;
9770 #else
9771 return;
9772 #endif
9776 #ifdef USE_GTK
9778 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9779 return;
9782 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9785 Arg al[1];
9786 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9787 XtSetValues (f->output_data.x->widget, al, 1);
9788 XtSetArg (al[0], XtNiconMask, icon_mask);
9789 XtSetValues (f->output_data.x->widget, al, 1);
9792 #else /* not USE_X_TOOLKIT && not USE_GTK */
9794 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9795 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9797 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9800 void
9801 x_wm_set_icon_position (f, icon_x, icon_y)
9802 struct frame *f;
9803 int icon_x, icon_y;
9805 Window window = FRAME_OUTER_WINDOW (f);
9807 f->output_data.x->wm_hints.flags |= IconPositionHint;
9808 f->output_data.x->wm_hints.icon_x = icon_x;
9809 f->output_data.x->wm_hints.icon_y = icon_y;
9811 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9815 /***********************************************************************
9816 Fonts
9817 ***********************************************************************/
9819 #if GLYPH_DEBUG
9821 /* Check that FONT is valid on frame F. It is if it can be found in F's
9822 font table. */
9824 static void
9825 x_check_font (f, font)
9826 struct frame *f;
9827 struct font *font;
9829 Lisp_Object frame;
9831 xassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9832 if (font->driver->check)
9833 xassert (font->driver->check (f, font) == 0);
9836 #endif /* GLYPH_DEBUG != 0 */
9839 /***********************************************************************
9840 Initialization
9841 ***********************************************************************/
9843 #ifdef USE_X_TOOLKIT
9844 static XrmOptionDescRec emacs_options[] = {
9845 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9846 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9848 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9849 XrmoptionSepArg, NULL},
9850 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9852 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9853 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9854 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9855 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9856 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9857 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9858 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9861 /* Whether atimer for Xt timeouts is activated or not. */
9863 static int x_timeout_atimer_activated_flag;
9865 #endif /* USE_X_TOOLKIT */
9867 static int x_initialized;
9869 #ifdef HAVE_X_SM
9870 static int x_session_initialized;
9871 #endif
9873 /* Test whether two display-name strings agree up to the dot that separates
9874 the screen number from the server number. */
9875 static int
9876 same_x_server (name1, name2)
9877 const char *name1, *name2;
9879 int seen_colon = 0;
9880 const unsigned char *system_name = SDATA (Vsystem_name);
9881 int system_name_length = strlen (system_name);
9882 int length_until_period = 0;
9884 while (system_name[length_until_period] != 0
9885 && system_name[length_until_period] != '.')
9886 length_until_period++;
9888 /* Treat `unix' like an empty host name. */
9889 if (! strncmp (name1, "unix:", 5))
9890 name1 += 4;
9891 if (! strncmp (name2, "unix:", 5))
9892 name2 += 4;
9893 /* Treat this host's name like an empty host name. */
9894 if (! strncmp (name1, system_name, system_name_length)
9895 && name1[system_name_length] == ':')
9896 name1 += system_name_length;
9897 if (! strncmp (name2, system_name, system_name_length)
9898 && name2[system_name_length] == ':')
9899 name2 += system_name_length;
9900 /* Treat this host's domainless name like an empty host name. */
9901 if (! strncmp (name1, system_name, length_until_period)
9902 && name1[length_until_period] == ':')
9903 name1 += length_until_period;
9904 if (! strncmp (name2, system_name, length_until_period)
9905 && name2[length_until_period] == ':')
9906 name2 += length_until_period;
9908 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
9910 if (*name1 == ':')
9911 seen_colon++;
9912 if (seen_colon && *name1 == '.')
9913 return 1;
9915 return (seen_colon
9916 && (*name1 == '.' || *name1 == '\0')
9917 && (*name2 == '.' || *name2 == '\0'));
9920 /* Count number of set bits in mask and number of bits to shift to
9921 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
9922 to 5. */
9923 static void
9924 get_bits_and_offset (mask, bits, offset)
9925 unsigned long mask;
9926 int *bits;
9927 int *offset;
9929 int nr = 0;
9930 int off = 0;
9932 while (!(mask & 1))
9934 off++;
9935 mask >>= 1;
9938 while (mask & 1)
9940 nr++;
9941 mask >>= 1;
9944 *offset = off;
9945 *bits = nr;
9948 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
9949 But don't permanently open it, just test its availability. */
9952 x_display_ok (display)
9953 const char *display;
9955 int dpy_ok = 1;
9956 Display *dpy;
9958 dpy = XOpenDisplay (display);
9959 if (dpy)
9960 XCloseDisplay (dpy);
9961 else
9962 dpy_ok = 0;
9963 return dpy_ok;
9966 /* Open a connection to X display DISPLAY_NAME, and return
9967 the structure that describes the open display.
9968 If we cannot contact the display, return null. */
9970 struct x_display_info *
9971 x_term_init (display_name, xrm_option, resource_name)
9972 Lisp_Object display_name;
9973 char *xrm_option;
9974 char *resource_name;
9976 int connection;
9977 Display *dpy;
9978 struct terminal *terminal;
9979 struct x_display_info *dpyinfo;
9980 XrmDatabase xrdb;
9982 BLOCK_INPUT;
9984 if (!x_initialized)
9986 x_initialize ();
9987 ++x_initialized;
9990 if (! x_display_ok (SDATA (display_name)))
9991 error ("Display %s can't be opened", SDATA (display_name));
9993 #ifdef USE_GTK
9995 #define NUM_ARGV 10
9996 int argc;
9997 char *argv[NUM_ARGV];
9998 char **argv2 = argv;
9999 GdkAtom atom;
10001 #ifndef HAVE_GTK_MULTIDISPLAY
10002 if (!EQ (Vinitial_window_system, intern ("x")))
10003 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10004 #endif
10006 if (x_initialized++ > 1)
10008 #ifdef HAVE_GTK_MULTIDISPLAY
10009 /* Opening another display. If xg_display_open returns less
10010 than zero, we are probably on GTK 2.0, which can only handle
10011 one display. GTK 2.2 or later can handle more than one. */
10012 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10013 #endif
10014 error ("Sorry, this version of GTK can only handle one display");
10016 else
10018 for (argc = 0; argc < NUM_ARGV; ++argc)
10019 argv[argc] = 0;
10021 argc = 0;
10022 argv[argc++] = initial_argv[0];
10024 if (! NILP (display_name))
10026 argv[argc++] = "--display";
10027 argv[argc++] = SDATA (display_name);
10030 argv[argc++] = "--name";
10031 argv[argc++] = resource_name;
10033 XSetLocaleModifiers ("");
10035 gtk_init (&argc, &argv2);
10037 /* gtk_init does set_locale. We must fix locale after calling it. */
10038 fixup_locale ();
10039 xg_initialize ();
10041 dpy = GDK_DISPLAY ();
10043 /* NULL window -> events for all windows go to our function */
10044 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10046 /* Load our own gtkrc if it exists. */
10048 char *file = "~/.emacs.d/gtkrc";
10049 Lisp_Object s, abs_file;
10051 s = make_string (file, strlen (file));
10052 abs_file = Fexpand_file_name (s, Qnil);
10054 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10055 gtk_rc_parse (SDATA (abs_file));
10058 XSetErrorHandler (x_error_handler);
10059 XSetIOErrorHandler (x_io_error_quitter);
10062 #else /* not USE_GTK */
10063 #ifdef USE_X_TOOLKIT
10064 /* weiner@footloose.sps.mot.com reports that this causes
10065 errors with X11R5:
10066 X protocol error: BadAtom (invalid Atom parameter)
10067 on protocol request 18skiloaf.
10068 So let's not use it until R6. */
10069 #ifdef HAVE_X11XTR6
10070 XtSetLanguageProc (NULL, NULL, NULL);
10071 #endif
10074 int argc = 0;
10075 char *argv[3];
10077 argv[0] = "";
10078 argc = 1;
10079 if (xrm_option)
10081 argv[argc++] = "-xrm";
10082 argv[argc++] = xrm_option;
10084 turn_on_atimers (0);
10085 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10086 resource_name, EMACS_CLASS,
10087 emacs_options, XtNumber (emacs_options),
10088 &argc, argv);
10089 turn_on_atimers (1);
10091 #ifdef HAVE_X11XTR6
10092 /* I think this is to compensate for XtSetLanguageProc. */
10093 fixup_locale ();
10094 #endif
10097 #else /* not USE_X_TOOLKIT */
10098 XSetLocaleModifiers ("");
10099 dpy = XOpenDisplay (SDATA (display_name));
10100 #endif /* not USE_X_TOOLKIT */
10101 #endif /* not USE_GTK*/
10103 /* Detect failure. */
10104 if (dpy == 0)
10106 UNBLOCK_INPUT;
10107 return 0;
10110 /* We have definitely succeeded. Record the new connection. */
10112 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10113 bzero (dpyinfo, sizeof *dpyinfo);
10115 terminal = x_create_terminal (dpyinfo);
10118 struct x_display_info *share;
10119 Lisp_Object tail;
10121 for (share = x_display_list, tail = x_display_name_list; share;
10122 share = share->next, tail = XCDR (tail))
10123 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10124 SDATA (display_name)))
10125 break;
10126 if (share)
10127 terminal->kboard = share->terminal->kboard;
10128 else
10130 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10131 init_kboard (terminal->kboard);
10132 terminal->kboard->Vwindow_system = intern ("x");
10133 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10135 char *vendor = ServerVendor (dpy);
10136 /* Temporarily hide the partially initialized terminal */
10137 terminal_list = terminal->next_terminal;
10138 UNBLOCK_INPUT;
10139 terminal->kboard->Vsystem_key_alist
10140 = call1 (Qvendor_specific_keysyms,
10141 vendor ? build_string (vendor) : empty_unibyte_string);
10142 BLOCK_INPUT;
10143 terminal->next_terminal = terminal_list;
10144 terminal_list = terminal;
10147 terminal->kboard->next_kboard = all_kboards;
10148 all_kboards = terminal->kboard;
10149 /* Don't let the initial kboard remain current longer than necessary.
10150 That would cause problems if a file loaded on startup tries to
10151 prompt in the mini-buffer. */
10152 if (current_kboard == initial_kboard)
10153 current_kboard = terminal->kboard;
10155 terminal->kboard->reference_count++;
10158 /* Put this display on the chain. */
10159 dpyinfo->next = x_display_list;
10160 x_display_list = dpyinfo;
10162 /* Put it on x_display_name_list as well, to keep them parallel. */
10163 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10164 x_display_name_list);
10165 dpyinfo->name_list_element = XCAR (x_display_name_list);
10167 dpyinfo->display = dpy;
10169 /* Set the name of the terminal. */
10170 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10171 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10172 terminal->name[SBYTES (display_name)] = 0;
10174 #if 0
10175 XSetAfterFunction (x_current_display, x_trace_wire);
10176 #endif /* ! 0 */
10178 dpyinfo->x_id_name
10179 = (char *) xmalloc (SBYTES (Vinvocation_name)
10180 + SBYTES (Vsystem_name)
10181 + 2);
10182 sprintf (dpyinfo->x_id_name, "%s@%s",
10183 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10185 /* Figure out which modifier bits mean what. */
10186 x_find_modifier_meanings (dpyinfo);
10188 /* Get the scroll bar cursor. */
10189 #ifdef USE_GTK
10190 /* We must create a GTK cursor, it is required for GTK widgets. */
10191 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10192 #endif /* USE_GTK */
10194 dpyinfo->vertical_scroll_bar_cursor
10195 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10197 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10198 resource_name, EMACS_CLASS);
10199 #ifdef HAVE_XRMSETDATABASE
10200 XrmSetDatabase (dpyinfo->display, xrdb);
10201 #else
10202 dpyinfo->display->db = xrdb;
10203 #endif
10204 /* Put the rdb where we can find it in a way that works on
10205 all versions. */
10206 dpyinfo->xrdb = xrdb;
10208 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10209 DefaultScreen (dpyinfo->display));
10210 select_visual (dpyinfo);
10211 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10212 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10213 dpyinfo->client_leader_window = 0;
10214 dpyinfo->grabbed = 0;
10215 dpyinfo->reference_count = 0;
10216 dpyinfo->icon_bitmap_id = -1;
10217 dpyinfo->n_fonts = 0;
10218 dpyinfo->bitmaps = 0;
10219 dpyinfo->bitmaps_size = 0;
10220 dpyinfo->bitmaps_last = 0;
10221 dpyinfo->scratch_cursor_gc = 0;
10222 dpyinfo->mouse_face_mouse_frame = 0;
10223 dpyinfo->mouse_face_deferred_gc = 0;
10224 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10225 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10226 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10227 dpyinfo->mouse_face_window = Qnil;
10228 dpyinfo->mouse_face_overlay = Qnil;
10229 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10230 dpyinfo->mouse_face_defer = 0;
10231 dpyinfo->mouse_face_hidden = 0;
10232 dpyinfo->x_focus_frame = 0;
10233 dpyinfo->x_focus_event_frame = 0;
10234 dpyinfo->x_highlight_frame = 0;
10235 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10237 /* See if we can construct pixel values from RGB values. */
10238 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10239 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10241 if (dpyinfo->visual->class == TrueColor)
10243 get_bits_and_offset (dpyinfo->visual->red_mask,
10244 &dpyinfo->red_bits, &dpyinfo->red_offset);
10245 get_bits_and_offset (dpyinfo->visual->blue_mask,
10246 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10247 get_bits_and_offset (dpyinfo->visual->green_mask,
10248 &dpyinfo->green_bits, &dpyinfo->green_offset);
10251 /* See if a private colormap is requested. */
10252 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10254 if (dpyinfo->visual->class == PseudoColor)
10256 Lisp_Object value;
10257 value = display_x_get_resource (dpyinfo,
10258 build_string ("privateColormap"),
10259 build_string ("PrivateColormap"),
10260 Qnil, Qnil);
10261 if (STRINGP (value)
10262 && (!strcmp (SDATA (value), "true")
10263 || !strcmp (SDATA (value), "on")))
10264 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10267 else
10268 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10269 dpyinfo->visual, AllocNone);
10272 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10273 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10274 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10275 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10276 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10277 pixels = DisplayWidth (dpyinfo->display, screen_number);
10278 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10279 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10280 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10283 dpyinfo->Xatom_wm_protocols
10284 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10285 dpyinfo->Xatom_wm_take_focus
10286 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10287 dpyinfo->Xatom_wm_save_yourself
10288 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10289 dpyinfo->Xatom_wm_delete_window
10290 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10291 dpyinfo->Xatom_wm_change_state
10292 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10293 dpyinfo->Xatom_wm_configure_denied
10294 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10295 dpyinfo->Xatom_wm_window_moved
10296 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10297 dpyinfo->Xatom_wm_client_leader
10298 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10299 dpyinfo->Xatom_editres
10300 = XInternAtom (dpyinfo->display, "Editres", False);
10301 dpyinfo->Xatom_CLIPBOARD
10302 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10303 dpyinfo->Xatom_TIMESTAMP
10304 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10305 dpyinfo->Xatom_TEXT
10306 = XInternAtom (dpyinfo->display, "TEXT", False);
10307 dpyinfo->Xatom_COMPOUND_TEXT
10308 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10309 dpyinfo->Xatom_UTF8_STRING
10310 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10311 dpyinfo->Xatom_DELETE
10312 = XInternAtom (dpyinfo->display, "DELETE", False);
10313 dpyinfo->Xatom_MULTIPLE
10314 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10315 dpyinfo->Xatom_INCR
10316 = XInternAtom (dpyinfo->display, "INCR", False);
10317 dpyinfo->Xatom_EMACS_TMP
10318 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10319 dpyinfo->Xatom_TARGETS
10320 = XInternAtom (dpyinfo->display, "TARGETS", False);
10321 dpyinfo->Xatom_NULL
10322 = XInternAtom (dpyinfo->display, "NULL", False);
10323 dpyinfo->Xatom_ATOM_PAIR
10324 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10325 /* For properties of font. */
10326 dpyinfo->Xatom_PIXEL_SIZE
10327 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10328 dpyinfo->Xatom_AVERAGE_WIDTH
10329 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10330 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10331 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10332 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10333 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10334 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10335 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10337 /* Ghostscript support. */
10338 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10339 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10341 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10342 False);
10344 dpyinfo->Xatom_XEMBED = XInternAtom (dpyinfo->display, "_XEMBED",
10345 False);
10347 dpyinfo->cut_buffers_initialized = 0;
10349 dpyinfo->x_dnd_atoms_size = 8;
10350 dpyinfo->x_dnd_atoms_length = 0;
10351 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10352 * dpyinfo->x_dnd_atoms_size);
10354 dpyinfo->net_supported_atoms = NULL;
10355 dpyinfo->nr_net_supported_atoms = 0;
10356 dpyinfo->net_supported_window = 0;
10358 connection = ConnectionNumber (dpyinfo->display);
10359 dpyinfo->connection = connection;
10362 extern int gray_bitmap_width, gray_bitmap_height;
10363 extern char *gray_bitmap_bits;
10364 dpyinfo->gray
10365 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10366 gray_bitmap_bits,
10367 gray_bitmap_width, gray_bitmap_height,
10368 (unsigned long) 1, (unsigned long) 0, 1);
10371 #ifdef HAVE_X_I18N
10372 xim_initialize (dpyinfo, resource_name);
10373 #endif
10375 #ifdef subprocesses
10376 /* This is only needed for distinguishing keyboard and process input. */
10377 if (connection != 0)
10378 add_keyboard_wait_descriptor (connection);
10379 #endif
10381 #ifdef F_SETOWN
10382 fcntl (connection, F_SETOWN, getpid ());
10383 #endif /* ! defined (F_SETOWN) */
10385 #ifdef SIGIO
10386 if (interrupt_input)
10387 init_sigio (connection);
10388 #endif /* ! defined (SIGIO) */
10390 #ifdef USE_LUCID
10392 Display *dpy = dpyinfo->display;
10393 XrmValue d, fr, to;
10394 Font font;
10396 d.addr = (XPointer)&dpy;
10397 d.size = sizeof (Display *);
10398 fr.addr = XtDefaultFont;
10399 fr.size = sizeof (XtDefaultFont);
10400 to.size = sizeof (Font *);
10401 to.addr = (XPointer)&font;
10402 x_catch_errors (dpy);
10403 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10404 abort ();
10405 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10406 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10407 x_uncatch_errors ();
10409 #endif
10411 /* See if we should run in synchronous mode. This is useful
10412 for debugging X code. */
10414 Lisp_Object value;
10415 value = display_x_get_resource (dpyinfo,
10416 build_string ("synchronous"),
10417 build_string ("Synchronous"),
10418 Qnil, Qnil);
10419 if (STRINGP (value)
10420 && (!strcmp (SDATA (value), "true")
10421 || !strcmp (SDATA (value), "on")))
10422 XSynchronize (dpyinfo->display, True);
10426 Lisp_Object value;
10427 value = display_x_get_resource (dpyinfo,
10428 build_string ("useXIM"),
10429 build_string ("UseXIM"),
10430 Qnil, Qnil);
10431 #ifdef USE_XIM
10432 if (STRINGP (value)
10433 && (!strcmp (SDATA (value), "false")
10434 || !strcmp (SDATA (value), "off")))
10435 use_xim = 0;
10436 #else
10437 if (STRINGP (value)
10438 && (!strcmp (SDATA (value), "true")
10439 || !strcmp (SDATA (value), "on")))
10440 use_xim = 1;
10441 #endif
10444 #ifdef HAVE_X_SM
10445 /* Only do this for the very first display in the Emacs session.
10446 Ignore X session management when Emacs was first started on a
10447 tty. */
10448 if (terminal->id == 1)
10449 x_session_initialize (dpyinfo);
10450 #endif
10452 UNBLOCK_INPUT;
10454 return dpyinfo;
10457 /* Get rid of display DPYINFO, deleting all frames on it,
10458 and without sending any more commands to the X server. */
10460 void
10461 x_delete_display (dpyinfo)
10462 struct x_display_info *dpyinfo;
10464 int i;
10465 struct terminal *t;
10467 /* Close all frames and delete the generic struct terminal for this
10468 X display. */
10469 for (t = terminal_list; t; t = t->next_terminal)
10470 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10472 #ifdef HAVE_X_SM
10473 /* Close X session management when we close its display. */
10474 if (t->id == 1 && x_session_have_connection ())
10475 x_session_close();
10476 #endif
10477 delete_terminal (t);
10478 break;
10481 delete_keyboard_wait_descriptor (dpyinfo->connection);
10483 /* Discard this display from x_display_name_list and x_display_list.
10484 We can't use Fdelq because that can quit. */
10485 if (! NILP (x_display_name_list)
10486 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10487 x_display_name_list = XCDR (x_display_name_list);
10488 else
10490 Lisp_Object tail;
10492 tail = x_display_name_list;
10493 while (CONSP (tail) && CONSP (XCDR (tail)))
10495 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10497 XSETCDR (tail, XCDR (XCDR (tail)));
10498 break;
10500 tail = XCDR (tail);
10504 if (next_noop_dpyinfo == dpyinfo)
10505 next_noop_dpyinfo = dpyinfo->next;
10507 if (x_display_list == dpyinfo)
10508 x_display_list = dpyinfo->next;
10509 else
10511 struct x_display_info *tail;
10513 for (tail = x_display_list; tail; tail = tail->next)
10514 if (tail->next == dpyinfo)
10515 tail->next = tail->next->next;
10518 /* Xt and GTK do this themselves. */
10519 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
10520 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
10521 XrmDestroyDatabase (dpyinfo->xrdb);
10522 #endif
10523 #endif
10525 xfree (dpyinfo->x_id_name);
10526 xfree (dpyinfo->x_dnd_atoms);
10527 xfree (dpyinfo->color_cells);
10528 xfree (dpyinfo);
10531 #ifdef USE_X_TOOLKIT
10533 /* Atimer callback function for TIMER. Called every 0.1s to process
10534 Xt timeouts, if needed. We must avoid calling XtAppPending as
10535 much as possible because that function does an implicit XFlush
10536 that slows us down. */
10538 static void
10539 x_process_timeouts (timer)
10540 struct atimer *timer;
10542 BLOCK_INPUT;
10543 x_timeout_atimer_activated_flag = 0;
10544 if (toolkit_scroll_bar_interaction || popup_activated ())
10546 while (XtAppPending (Xt_app_con) & XtIMTimer)
10547 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10548 /* Reactivate the atimer for next time. */
10549 x_activate_timeout_atimer ();
10551 UNBLOCK_INPUT;
10554 /* Install an asynchronous timer that processes Xt timeout events
10555 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10556 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10557 function whenever these variables are set. This is necessary
10558 because some widget sets use timeouts internally, for example the
10559 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10560 processed, these widgets don't behave normally. */
10562 void
10563 x_activate_timeout_atimer ()
10565 BLOCK_INPUT;
10566 if (!x_timeout_atimer_activated_flag)
10568 EMACS_TIME interval;
10570 EMACS_SET_SECS_USECS (interval, 0, 100000);
10571 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10572 x_timeout_atimer_activated_flag = 1;
10574 UNBLOCK_INPUT;
10577 #endif /* USE_X_TOOLKIT */
10580 /* Set up use of X before we make the first connection. */
10582 extern frame_parm_handler x_frame_parm_handlers[];
10584 static struct redisplay_interface x_redisplay_interface =
10586 x_frame_parm_handlers,
10587 x_produce_glyphs,
10588 x_write_glyphs,
10589 x_insert_glyphs,
10590 x_clear_end_of_line,
10591 x_scroll_run,
10592 x_after_update_window_line,
10593 x_update_window_begin,
10594 x_update_window_end,
10595 x_cursor_to,
10596 x_flush,
10597 #ifdef XFlush
10598 x_flush,
10599 #else
10600 0, /* flush_display_optional */
10601 #endif
10602 x_clear_window_mouse_face,
10603 x_get_glyph_overhangs,
10604 x_fix_overlapping_area,
10605 x_draw_fringe_bitmap,
10606 0, /* define_fringe_bitmap */
10607 0, /* destroy_fringe_bitmap */
10608 x_compute_glyph_string_overhangs,
10609 x_draw_glyph_string,
10610 x_define_frame_cursor,
10611 x_clear_frame_area,
10612 x_draw_window_cursor,
10613 x_draw_vertical_window_border,
10614 x_shift_glyphs_for_insert
10618 /* This function is called when the last frame on a display is deleted. */
10619 void
10620 x_delete_terminal (struct terminal *terminal)
10622 struct x_display_info *dpyinfo = terminal->display_info.x;
10623 int i;
10625 /* Protect against recursive calls. delete_frame in
10626 delete_terminal calls us back when it deletes our last frame. */
10627 if (!terminal->name)
10628 return;
10630 BLOCK_INPUT;
10631 #ifdef HAVE_X_I18N
10632 /* We must close our connection to the XIM server before closing the
10633 X display. */
10634 if (dpyinfo->xim)
10635 xim_close_dpy (dpyinfo);
10636 #endif
10638 /* If called from x_connection_closed, the display may already be closed
10639 and dpyinfo->display was set to 0 to indicate that. */
10640 if (dpyinfo->display)
10642 x_destroy_all_bitmaps (dpyinfo);
10643 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10645 #ifdef USE_GTK
10646 xg_display_close (dpyinfo->display);
10647 #else
10648 #ifdef USE_X_TOOLKIT
10649 XtCloseDisplay (dpyinfo->display);
10650 #else
10651 XCloseDisplay (dpyinfo->display);
10652 #endif
10653 #endif /* ! USE_GTK */
10656 x_delete_display (dpyinfo);
10657 UNBLOCK_INPUT;
10660 /* Create a struct terminal, initialize it with the X11 specific
10661 functions and make DISPLAY->TERMINAL point to it. */
10663 static struct terminal *
10664 x_create_terminal (struct x_display_info *dpyinfo)
10666 struct terminal *terminal;
10668 terminal = create_terminal ();
10670 terminal->type = output_x_window;
10671 terminal->display_info.x = dpyinfo;
10672 dpyinfo->terminal = terminal;
10674 /* kboard is initialized in x_term_init. */
10676 terminal->clear_frame_hook = x_clear_frame;
10677 terminal->ins_del_lines_hook = x_ins_del_lines;
10678 terminal->delete_glyphs_hook = x_delete_glyphs;
10679 terminal->ring_bell_hook = XTring_bell;
10680 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
10681 terminal->set_terminal_modes_hook = XTset_terminal_modes;
10682 terminal->update_begin_hook = x_update_begin;
10683 terminal->update_end_hook = x_update_end;
10684 terminal->set_terminal_window_hook = XTset_terminal_window;
10685 terminal->read_socket_hook = XTread_socket;
10686 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10687 terminal->mouse_position_hook = XTmouse_position;
10688 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10689 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10690 terminal->fullscreen_hook = XTfullscreen_hook;
10691 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10692 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10693 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10694 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10696 terminal->delete_frame_hook = x_destroy_window;
10697 terminal->delete_terminal_hook = x_delete_terminal;
10699 terminal->rif = &x_redisplay_interface;
10700 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10701 terminal->char_ins_del_ok = 1;
10702 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10703 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10704 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10705 off the bottom. */
10707 return terminal;
10710 void
10711 x_initialize ()
10713 baud_rate = 19200;
10715 x_noop_count = 0;
10716 last_tool_bar_item = -1;
10717 any_help_event_p = 0;
10718 ignore_next_mouse_click_timeout = 0;
10719 #ifdef HAVE_X_SM
10720 x_session_initialized = 0;
10721 #endif
10723 #ifdef USE_GTK
10724 current_count = -1;
10725 #endif
10727 /* Try to use interrupt input; if we can't, then start polling. */
10728 Fset_input_interrupt_mode (Qt);
10730 #ifdef USE_X_TOOLKIT
10731 XtToolkitInitialize ();
10733 Xt_app_con = XtCreateApplicationContext ();
10735 /* Register a converter from strings to pixels, which uses
10736 Emacs' color allocation infrastructure. */
10737 XtAppSetTypeConverter (Xt_app_con,
10738 XtRString, XtRPixel, cvt_string_to_pixel,
10739 cvt_string_to_pixel_args,
10740 XtNumber (cvt_string_to_pixel_args),
10741 XtCacheByDisplay, cvt_pixel_dtor);
10743 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10744 #endif
10746 #ifdef USE_TOOLKIT_SCROLL_BARS
10747 #ifndef USE_GTK
10748 xaw3d_arrow_scroll = False;
10749 xaw3d_pick_top = True;
10750 #endif
10751 #endif
10753 /* Note that there is no real way portable across R3/R4 to get the
10754 original error handler. */
10755 XSetErrorHandler (x_error_handler);
10756 XSetIOErrorHandler (x_io_error_quitter);
10758 /* Disable Window Change signals; they are handled by X events. */
10759 #if 0 /* Don't. We may want to open tty frames later. */
10760 #ifdef SIGWINCH
10761 signal (SIGWINCH, SIG_DFL);
10762 #endif /* SIGWINCH */
10763 #endif
10765 signal (SIGPIPE, x_connection_signal);
10769 void
10770 syms_of_xterm ()
10772 x_error_message = NULL;
10774 staticpro (&x_display_name_list);
10775 x_display_name_list = Qnil;
10777 staticpro (&last_mouse_scroll_bar);
10778 last_mouse_scroll_bar = Qnil;
10780 staticpro (&Qvendor_specific_keysyms);
10781 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10783 staticpro (&Qlatin_1);
10784 Qlatin_1 = intern ("latin-1");
10786 staticpro (&last_mouse_press_frame);
10787 last_mouse_press_frame = Qnil;
10789 DEFVAR_BOOL ("x-use-underline-position-properties",
10790 &x_use_underline_position_properties,
10791 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10792 A value of nil means ignore them. If you encounter fonts with bogus
10793 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10794 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10795 to override the font's UNDERLINE_POSITION for small font display
10796 sizes. */);
10797 x_use_underline_position_properties = 1;
10799 DEFVAR_BOOL ("x-underline-at-descent-line",
10800 &x_underline_at_descent_line,
10801 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
10802 A value of nil means to draw the underline according to the value of the
10803 variable `x-use-underline-position-properties', which is usually at the
10804 baseline level. The default value is nil. */);
10805 x_underline_at_descent_line = 0;
10807 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10808 &x_mouse_click_focus_ignore_position,
10809 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10810 This variable is only used when the window manager requires that you
10811 click on a frame to select it (give it focus). In that case, a value
10812 of nil, means that the selected window and cursor position changes to
10813 reflect the mouse click position, while a non-nil value means that the
10814 selected window or cursor position is preserved. */);
10815 x_mouse_click_focus_ignore_position = 0;
10817 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10818 doc: /* What X toolkit scroll bars Emacs uses.
10819 A value of nil means Emacs doesn't use X toolkit scroll bars.
10820 Otherwise, value is a symbol describing the X toolkit. */);
10821 #ifdef USE_TOOLKIT_SCROLL_BARS
10822 #ifdef USE_MOTIF
10823 Vx_toolkit_scroll_bars = intern ("motif");
10824 #elif defined HAVE_XAW3D
10825 Vx_toolkit_scroll_bars = intern ("xaw3d");
10826 #elif USE_GTK
10827 Vx_toolkit_scroll_bars = intern ("gtk");
10828 #else
10829 Vx_toolkit_scroll_bars = intern ("xaw");
10830 #endif
10831 #else
10832 Vx_toolkit_scroll_bars = Qnil;
10833 #endif
10835 staticpro (&last_mouse_motion_frame);
10836 last_mouse_motion_frame = Qnil;
10838 Qmodifier_value = intern ("modifier-value");
10839 Qalt = intern ("alt");
10840 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10841 Qhyper = intern ("hyper");
10842 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10843 Qmeta = intern ("meta");
10844 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10845 Qsuper = intern ("super");
10846 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10848 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10849 doc: /* Which keys Emacs uses for the alt modifier.
10850 This should be one of the symbols `alt', `hyper', `meta', `super'.
10851 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10852 is nil, which is the same as `alt'. */);
10853 Vx_alt_keysym = Qnil;
10855 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
10856 doc: /* Which keys Emacs uses for the hyper modifier.
10857 This should be one of the symbols `alt', `hyper', `meta', `super'.
10858 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10859 default is nil, which is the same as `hyper'. */);
10860 Vx_hyper_keysym = Qnil;
10862 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
10863 doc: /* Which keys Emacs uses for the meta modifier.
10864 This should be one of the symbols `alt', `hyper', `meta', `super'.
10865 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10866 default is nil, which is the same as `meta'. */);
10867 Vx_meta_keysym = Qnil;
10869 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
10870 doc: /* Which keys Emacs uses for the super modifier.
10871 This should be one of the symbols `alt', `hyper', `meta', `super'.
10872 For example, `super' means use the Super_L and Super_R keysyms. The
10873 default is nil, which is the same as `super'. */);
10874 Vx_super_keysym = Qnil;
10876 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
10877 doc: /* Hash table of character codes indexed by X keysym codes. */);
10878 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
10879 make_float (DEFAULT_REHASH_SIZE),
10880 make_float (DEFAULT_REHASH_THRESHOLD),
10881 Qnil, Qnil, Qnil);
10884 #endif /* HAVE_X_WINDOWS */
10886 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
10887 (do not change this comment) */