(calcFunc-fdiv): Allow `fdiv' to divide fractions.
[emacs.git] / src / xterm.c
blob90db15d9ed4d6c7717026f46acadaaf62399280b
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, 2010
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>
31 #include <setjmp.h>
33 #ifdef HAVE_X_WINDOWS
35 #include "lisp.h"
36 #include "blockinput.h"
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef HAVE_SYS_IOCTL_H
54 #include <sys/ioctl.h>
55 #endif /* ! defined (HAVE_SYS_IOCTL_H) */
57 #include "systime.h"
59 #ifndef INCLUDED_FCNTL
60 #include <fcntl.h>
61 #endif
62 #include <ctype.h>
63 #include <errno.h>
64 #include <setjmp.h>
65 #include <sys/stat.h>
66 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
67 /* #include <sys/param.h> */
69 #include "charset.h"
70 #include "character.h"
71 #include "coding.h"
72 #include "frame.h"
73 #include "dispextern.h"
74 #include "fontset.h"
75 #include "termhooks.h"
76 #include "termopts.h"
77 #include "termchar.h"
78 #include "emacs-icon.h"
79 #include "disptab.h"
80 #include "buffer.h"
81 #include "window.h"
82 #include "keyboard.h"
83 #include "intervals.h"
84 #include "process.h"
85 #include "atimer.h"
86 #include "keymap.h"
87 #include "font.h"
88 #include "fontset.h"
89 #include "xsettings.h"
90 #include "xgselect.h"
91 #include "sysselect.h"
93 #ifdef USE_X_TOOLKIT
94 #include <X11/Shell.h>
95 #endif
97 #ifdef HAVE_SYS_TIME_H
98 #include <sys/time.h>
99 #endif
100 #ifdef HAVE_UNISTD_H
101 #include <unistd.h>
102 #endif
104 #ifdef USE_GTK
105 #include "gtkutil.h"
106 #endif
108 #ifdef USE_LUCID
109 extern int xlwmenu_window_p P_ ((Widget w, Window window));
110 extern void xlwmenu_redisplay P_ ((Widget));
111 #endif
113 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
115 extern void free_frame_menubar P_ ((struct frame *));
116 #endif
118 #ifdef USE_X_TOOLKIT
119 #if !defined(NO_EDITRES)
120 #define HACK_EDITRES
121 extern void _XEditResCheckMessages ();
122 #endif /* not NO_EDITRES */
124 /* Include toolkit specific headers for the scroll bar widget. */
126 #ifdef USE_TOOLKIT_SCROLL_BARS
127 #if defined USE_MOTIF
128 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
129 #include <Xm/ScrollBar.h>
130 #else /* !USE_MOTIF i.e. use Xaw */
132 #ifdef HAVE_XAW3D
133 #include <X11/Xaw3d/Simple.h>
134 #include <X11/Xaw3d/Scrollbar.h>
135 #include <X11/Xaw3d/ThreeD.h>
136 #else /* !HAVE_XAW3D */
137 #include <X11/Xaw/Simple.h>
138 #include <X11/Xaw/Scrollbar.h>
139 #endif /* !HAVE_XAW3D */
140 #ifndef XtNpickTop
141 #define XtNpickTop "pickTop"
142 #endif /* !XtNpickTop */
143 #endif /* !USE_MOTIF */
144 #endif /* USE_TOOLKIT_SCROLL_BARS */
146 #endif /* USE_X_TOOLKIT */
148 #ifdef USE_X_TOOLKIT
149 #include "widget.h"
150 #ifndef XtNinitialState
151 #define XtNinitialState "initialState"
152 #endif
153 #endif
155 /* Default to using XIM if available. */
156 #ifdef USE_XIM
157 int use_xim = 1;
158 #else
159 int use_xim = 0; /* configure --without-xim */
160 #endif
164 /* Non-nil means Emacs uses toolkit scroll bars. */
166 Lisp_Object Vx_toolkit_scroll_bars;
168 /* Non-zero means that a HELP_EVENT has been generated since Emacs
169 start. */
171 static int any_help_event_p;
173 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
174 static Lisp_Object last_window;
176 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
178 int x_use_underline_position_properties;
180 /* Non-zero means to draw the underline at the same place as the descent line. */
182 int x_underline_at_descent_line;
184 /* This is a chain of structures for all the X displays currently in
185 use. */
187 struct x_display_info *x_display_list;
189 /* This is a list of cons cells, each of the form (NAME
190 . FONT-LIST-CACHE), one for each element of x_display_list and in
191 the same order. NAME is the name of the frame. FONT-LIST-CACHE
192 records previous values returned by x-list-fonts. */
194 Lisp_Object x_display_name_list;
196 /* Frame being updated by update_frame. This is declared in term.c.
197 This is set by update_begin and looked at by all the XT functions.
198 It is zero while not inside an update. In that case, the XT
199 functions assume that `selected_frame' is the frame to apply to. */
201 extern struct frame *updating_frame;
203 /* This is a frame waiting to be auto-raised, within XTread_socket. */
205 static struct frame *pending_autoraise_frame;
207 /* This is a frame waiting for an event matching mask, within XTread_socket. */
209 static struct {
210 struct frame *f;
211 int eventtype;
212 } pending_event_wait;
214 #ifdef USE_X_TOOLKIT
215 /* The application context for Xt use. */
216 XtAppContext Xt_app_con;
217 static String Xt_default_resources[] = {0};
218 #endif /* USE_X_TOOLKIT */
220 /* Non-zero means user is interacting with a toolkit scroll bar. */
222 static int toolkit_scroll_bar_interaction;
224 /* Non-zero means to not move point as a result of clicking on a
225 frame to focus it (when focus-follows-mouse is nil). */
227 int x_mouse_click_focus_ignore_position;
229 /* Non-zero timeout value means ignore next mouse click if it arrives
230 before that timeout elapses (i.e. as part of the same sequence of
231 events resulting from clicking on a frame to select it). */
233 static unsigned long ignore_next_mouse_click_timeout;
235 /* Mouse movement.
237 Formerly, we used PointerMotionHintMask (in standard_event_mask)
238 so that we would have to call XQueryPointer after each MotionNotify
239 event to ask for another such event. However, this made mouse tracking
240 slow, and there was a bug that made it eventually stop.
242 Simply asking for MotionNotify all the time seems to work better.
244 In order to avoid asking for motion events and then throwing most
245 of them away or busy-polling the server for mouse positions, we ask
246 the server for pointer motion hints. This means that we get only
247 one event per group of mouse movements. "Groups" are delimited by
248 other kinds of events (focus changes and button clicks, for
249 example), or by XQueryPointer calls; when one of these happens, we
250 get another MotionNotify event the next time the mouse moves. This
251 is at least as efficient as getting motion events when mouse
252 tracking is on, and I suspect only negligibly worse when tracking
253 is off. */
255 /* Where the mouse was last time we reported a mouse event. */
257 static XRectangle last_mouse_glyph;
258 static FRAME_PTR last_mouse_glyph_frame;
259 static Lisp_Object last_mouse_press_frame;
261 /* The scroll bar in which the last X motion event occurred.
263 If the last X motion event occurred in a scroll bar, we set this so
264 XTmouse_position can know whether to report a scroll bar motion or
265 an ordinary motion.
267 If the last X motion event didn't occur in a scroll bar, we set
268 this to Qnil, to tell XTmouse_position to return an ordinary motion
269 event. */
271 static Lisp_Object last_mouse_scroll_bar;
273 /* This is a hack. We would really prefer that XTmouse_position would
274 return the time associated with the position it returns, but there
275 doesn't seem to be any way to wrest the time-stamp from the server
276 along with the position query. So, we just keep track of the time
277 of the last movement we received, and return that in hopes that
278 it's somewhat accurate. */
280 static Time last_mouse_movement_time;
282 /* Time for last user interaction as returned in X events. */
284 static Time last_user_time;
286 /* Incremented by XTread_socket whenever it really tries to read
287 events. */
289 #ifdef __STDC__
290 static int volatile input_signal_count;
291 #else
292 static int input_signal_count;
293 #endif
295 /* Used locally within XTread_socket. */
297 static int x_noop_count;
299 /* Initial values of argv and argc. */
301 extern char **initial_argv;
302 extern int initial_argc;
304 extern Lisp_Object Vcommand_line_args, Vsystem_name;
306 /* Tells if a window manager is present or not. */
308 extern Lisp_Object Vx_no_window_manager;
310 extern Lisp_Object Qeql;
312 /* A mask of extra modifier bits to put into every keyboard char. */
314 extern EMACS_INT extra_keyboard_modifiers;
316 /* The keysyms to use for the various modifiers. */
318 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
319 Lisp_Object Vx_keysym_table;
320 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
322 static Lisp_Object Qvendor_specific_keysyms;
323 static Lisp_Object Qlatin_1;
325 #ifdef USE_GTK
326 /* The name of the Emacs icon file. */
327 static Lisp_Object xg_default_icon_file;
329 /* Used in gtkutil.c. */
330 Lisp_Object Qx_gtk_map_stock;
331 #endif
333 /* Used in x_flush. */
335 extern Lisp_Object Vinhibit_redisplay;
337 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
338 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
340 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
341 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
342 static const XColor *x_color_cells P_ ((Display *, int *));
343 static void x_update_window_end P_ ((struct window *, int, int));
345 static int x_io_error_quitter P_ ((Display *));
346 static struct terminal *x_create_terminal P_ ((struct x_display_info *));
347 void x_delete_terminal P_ ((struct terminal *));
348 static void x_update_end P_ ((struct frame *));
349 static void XTframe_up_to_date P_ ((struct frame *));
350 static void XTset_terminal_modes P_ ((struct terminal *));
351 static void XTreset_terminal_modes P_ ((struct terminal *));
352 static void x_clear_frame P_ ((struct frame *));
353 static void frame_highlight P_ ((struct frame *));
354 static void frame_unhighlight P_ ((struct frame *));
355 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
356 static void x_focus_changed P_ ((int, int, struct x_display_info *,
357 struct frame *, struct input_event *));
358 static void x_detect_focus_change P_ ((struct x_display_info *,
359 XEvent *, struct input_event *));
360 static void XTframe_rehighlight P_ ((struct frame *));
361 static void x_frame_rehighlight P_ ((struct x_display_info *));
362 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
363 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
364 enum text_cursor_kinds));
366 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
367 static void x_flush P_ ((struct frame *f));
368 static void x_update_begin P_ ((struct frame *));
369 static void x_update_window_begin P_ ((struct window *));
370 static void x_after_update_window_line P_ ((struct glyph_row *));
371 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
372 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
373 enum scroll_bar_part *,
374 Lisp_Object *, Lisp_Object *,
375 unsigned long *));
376 static void x_handle_net_wm_state P_ ((struct frame *, XPropertyEvent *));
377 static void x_check_fullscreen P_ ((struct frame *));
378 static void x_check_expected_move P_ ((struct frame *, int, int));
379 static void x_sync_with_move P_ ((struct frame *, int, int, int));
380 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
381 int *, struct input_event *));
382 /* Don't declare this NO_RETURN because we want no
383 interference with debugging failing X calls. */
384 static SIGTYPE x_connection_closed P_ ((Display *, char *));
387 /* Flush display of frame F, or of all frames if F is null. */
389 static void
390 x_flush (f)
391 struct frame *f;
393 /* Don't call XFlush when it is not safe to redisplay; the X
394 connection may be broken. */
395 if (!NILP (Vinhibit_redisplay))
396 return;
398 BLOCK_INPUT;
399 if (f == NULL)
401 Lisp_Object rest, frame;
402 FOR_EACH_FRAME (rest, frame)
403 if (FRAME_X_P (XFRAME (frame)))
404 x_flush (XFRAME (frame));
406 else if (FRAME_X_P (f))
407 XFlush (FRAME_X_DISPLAY (f));
408 UNBLOCK_INPUT;
412 /* Remove calls to XFlush by defining XFlush to an empty replacement.
413 Calls to XFlush should be unnecessary because the X output buffer
414 is flushed automatically as needed by calls to XPending,
415 XNextEvent, or XWindowEvent according to the XFlush man page.
416 XTread_socket calls XPending. Removing XFlush improves
417 performance. */
419 #define XFlush(DISPLAY) (void) 0
422 /***********************************************************************
423 Debugging
424 ***********************************************************************/
426 #if 0
428 /* This is a function useful for recording debugging information about
429 the sequence of occurrences in this file. */
431 struct record
433 char *locus;
434 int type;
437 struct record event_record[100];
439 int event_record_index;
441 record_event (locus, type)
442 char *locus;
443 int type;
445 if (event_record_index == sizeof (event_record) / sizeof (struct record))
446 event_record_index = 0;
448 event_record[event_record_index].locus = locus;
449 event_record[event_record_index].type = type;
450 event_record_index++;
453 #endif /* 0 */
457 /* Return the struct x_display_info corresponding to DPY. */
459 struct x_display_info *
460 x_display_info_for_display (dpy)
461 Display *dpy;
463 struct x_display_info *dpyinfo;
465 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
466 if (dpyinfo->display == dpy)
467 return dpyinfo;
469 return 0;
472 #define OPAQUE 0xffffffff
473 #define OPACITY "_NET_WM_WINDOW_OPACITY"
475 void
476 x_set_frame_alpha (f)
477 struct frame *f;
479 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
480 Display *dpy = FRAME_X_DISPLAY (f);
481 Window win = FRAME_OUTER_WINDOW (f);
482 double alpha = 1.0;
483 double alpha_min = 1.0;
484 unsigned long opac;
486 if (FRAME_X_DISPLAY_INFO (f)->root_window != FRAME_X_OUTPUT (f)->parent_desc)
487 /* Since the WM decoration lies under the FRAME_OUTER_WINDOW,
488 we must treat the former instead of the latter. */
489 win = FRAME_X_OUTPUT(f)->parent_desc;
491 if (dpyinfo->x_highlight_frame == f)
492 alpha = f->alpha[0];
493 else
494 alpha = f->alpha[1];
496 if (FLOATP (Vframe_alpha_lower_limit))
497 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
498 else if (INTEGERP (Vframe_alpha_lower_limit))
499 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
501 if (alpha < 0.0)
502 return;
503 else if (alpha > 1.0)
504 alpha = 1.0;
505 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
506 alpha = alpha_min;
508 opac = alpha * OPAQUE;
510 /* return unless necessary */
512 unsigned char *data;
513 Atom actual;
514 int rc, format;
515 unsigned long n, left;
517 x_catch_errors (dpy);
518 rc = XGetWindowProperty(dpy, win, XInternAtom(dpy, OPACITY, False),
519 0L, 1L, False, XA_CARDINAL,
520 &actual, &format, &n, &left,
521 &data);
523 if (rc == Success && actual != None)
524 if (*(unsigned long *)data == opac)
526 XFree ((void *) data);
527 x_uncatch_errors ();
528 return;
530 else
531 XFree ((void *) data);
532 x_uncatch_errors ();
535 x_catch_errors (dpy);
536 XChangeProperty (dpy, win, XInternAtom (dpy, OPACITY, False),
537 XA_CARDINAL, 32, PropModeReplace,
538 (unsigned char *) &opac, 1L);
539 x_uncatch_errors ();
543 x_display_pixel_height (dpyinfo)
544 struct x_display_info *dpyinfo;
546 return HeightOfScreen (dpyinfo->screen);
550 x_display_pixel_width (dpyinfo)
551 struct x_display_info *dpyinfo;
553 return WidthOfScreen (dpyinfo->screen);
557 /***********************************************************************
558 Starting and ending an update
559 ***********************************************************************/
561 /* Start an update of frame F. This function is installed as a hook
562 for update_begin, i.e. it is called when update_begin is called.
563 This function is called prior to calls to x_update_window_begin for
564 each window being updated. Currently, there is nothing to do here
565 because all interesting stuff is done on a window basis. */
567 static void
568 x_update_begin (f)
569 struct frame *f;
571 /* Nothing to do. */
575 /* Start update of window W. Set the global variable updated_window
576 to the window being updated and set output_cursor to the cursor
577 position of W. */
579 static void
580 x_update_window_begin (w)
581 struct window *w;
583 struct frame *f = XFRAME (WINDOW_FRAME (w));
584 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
586 updated_window = w;
587 set_output_cursor (&w->cursor);
589 BLOCK_INPUT;
591 if (f == display_info->mouse_face_mouse_frame)
593 /* Don't do highlighting for mouse motion during the update. */
594 display_info->mouse_face_defer = 1;
596 /* If F needs to be redrawn, simply forget about any prior mouse
597 highlighting. */
598 if (FRAME_GARBAGED_P (f))
599 display_info->mouse_face_window = Qnil;
602 UNBLOCK_INPUT;
606 /* Draw a vertical window border from (x,y0) to (x,y1) */
608 static void
609 x_draw_vertical_window_border (w, x, y0, y1)
610 struct window *w;
611 int x, y0, y1;
613 struct frame *f = XFRAME (WINDOW_FRAME (w));
614 struct face *face;
616 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
617 if (face)
618 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
619 face->foreground);
621 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
622 f->output_data.x->normal_gc, x, y0, x, y1);
625 /* End update of window W (which is equal to updated_window).
627 Draw vertical borders between horizontally adjacent windows, and
628 display W's cursor if CURSOR_ON_P is non-zero.
630 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
631 glyphs in mouse-face were overwritten. In that case we have to
632 make sure that the mouse-highlight is properly redrawn.
634 W may be a menu bar pseudo-window in case we don't have X toolkit
635 support. Such windows don't have a cursor, so don't display it
636 here. */
638 static void
639 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
640 struct window *w;
641 int cursor_on_p, mouse_face_overwritten_p;
643 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
645 if (!w->pseudo_window_p)
647 BLOCK_INPUT;
649 if (cursor_on_p)
650 display_and_set_cursor (w, 1, output_cursor.hpos,
651 output_cursor.vpos,
652 output_cursor.x, output_cursor.y);
654 if (draw_window_fringes (w, 1))
655 x_draw_vertical_border (w);
657 UNBLOCK_INPUT;
660 /* If a row with mouse-face was overwritten, arrange for
661 XTframe_up_to_date to redisplay the mouse highlight. */
662 if (mouse_face_overwritten_p)
664 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
665 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
666 dpyinfo->mouse_face_window = Qnil;
669 updated_window = NULL;
673 /* End update of frame F. This function is installed as a hook in
674 update_end. */
676 static void
677 x_update_end (f)
678 struct frame *f;
680 /* Mouse highlight may be displayed again. */
681 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
683 #ifndef XFlush
684 BLOCK_INPUT;
685 XFlush (FRAME_X_DISPLAY (f));
686 UNBLOCK_INPUT;
687 #endif
691 /* This function is called from various places in xdisp.c whenever a
692 complete update has been performed. The global variable
693 updated_window is not available here. */
695 static void
696 XTframe_up_to_date (f)
697 struct frame *f;
699 if (FRAME_X_P (f))
701 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
703 if (dpyinfo->mouse_face_deferred_gc
704 || f == dpyinfo->mouse_face_mouse_frame)
706 BLOCK_INPUT;
707 if (dpyinfo->mouse_face_mouse_frame)
708 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
709 dpyinfo->mouse_face_mouse_x,
710 dpyinfo->mouse_face_mouse_y);
711 dpyinfo->mouse_face_deferred_gc = 0;
712 UNBLOCK_INPUT;
718 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
719 arrow bitmaps, or clear the fringes if no bitmaps are required
720 before DESIRED_ROW is made current. The window being updated is
721 found in updated_window. This function It is called from
722 update_window_line only if it is known that there are differences
723 between bitmaps to be drawn between current row and DESIRED_ROW. */
725 static void
726 x_after_update_window_line (desired_row)
727 struct glyph_row *desired_row;
729 struct window *w = updated_window;
730 struct frame *f;
731 int width, height;
733 xassert (w);
735 if (!desired_row->mode_line_p && !w->pseudo_window_p)
736 desired_row->redraw_fringe_bitmaps_p = 1;
738 /* When a window has disappeared, make sure that no rest of
739 full-width rows stays visible in the internal border. Could
740 check here if updated_window is the leftmost/rightmost window,
741 but I guess it's not worth doing since vertically split windows
742 are almost never used, internal border is rarely set, and the
743 overhead is very small. */
744 if (windows_or_buffers_changed
745 && desired_row->full_width_p
746 && (f = XFRAME (w->frame),
747 width = FRAME_INTERNAL_BORDER_WIDTH (f),
748 width != 0)
749 && (height = desired_row->visible_height,
750 height > 0))
752 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
754 BLOCK_INPUT;
755 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
756 0, y, width, height, False);
757 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
758 FRAME_PIXEL_WIDTH (f) - width,
759 y, width, height, False);
760 UNBLOCK_INPUT;
764 static void
765 x_draw_fringe_bitmap (w, row, p)
766 struct window *w;
767 struct glyph_row *row;
768 struct draw_fringe_bitmap_params *p;
770 struct frame *f = XFRAME (WINDOW_FRAME (w));
771 Display *display = FRAME_X_DISPLAY (f);
772 Window window = FRAME_X_WINDOW (f);
773 GC gc = f->output_data.x->normal_gc;
774 struct face *face = p->face;
775 int rowY;
777 /* Must clip because of partially visible lines. */
778 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
779 if (p->y < rowY)
781 /* Adjust position of "bottom aligned" bitmap on partially
782 visible last row. */
783 int oldY = row->y;
784 int oldVH = row->visible_height;
785 row->visible_height = p->h;
786 row->y -= rowY - p->y;
787 x_clip_to_row (w, row, -1, gc);
788 row->y = oldY;
789 row->visible_height = oldVH;
791 else
792 x_clip_to_row (w, row, -1, gc);
794 if (!p->overlay_p)
796 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
798 /* In case the same realized face is used for fringes and
799 for something displayed in the text (e.g. face `region' on
800 mono-displays, the fill style may have been changed to
801 FillSolid in x_draw_glyph_string_background. */
802 if (face->stipple)
803 XSetFillStyle (display, face->gc, FillOpaqueStippled);
804 else
805 XSetForeground (display, face->gc, face->background);
807 #ifdef USE_TOOLKIT_SCROLL_BARS
808 /* If the fringe is adjacent to the left (right) scroll bar of a
809 leftmost (rightmost, respectively) window, then extend its
810 background to the gap between the fringe and the bar. */
811 if ((WINDOW_LEFTMOST_P (w)
812 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
813 || (WINDOW_RIGHTMOST_P (w)
814 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
816 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
818 if (sb_width > 0)
820 int left = WINDOW_SCROLL_BAR_AREA_X (w);
821 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
822 * FRAME_COLUMN_WIDTH (f));
824 if (bx < 0)
826 /* Bitmap fills the fringe. */
827 if (left + width == p->x)
828 bx = left + sb_width;
829 else if (p->x + p->wd == left)
830 bx = left;
831 if (bx >= 0)
833 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
835 nx = width - sb_width;
836 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
837 row->y));
838 ny = row->visible_height;
841 else
843 if (left + width == bx)
845 bx = left + sb_width;
846 nx += width - sb_width;
848 else if (bx + nx == left)
849 nx += width - sb_width;
853 #endif
854 if (bx >= 0 && nx > 0)
855 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
857 if (!face->stipple)
858 XSetForeground (display, face->gc, face->foreground);
861 if (p->which)
863 unsigned char *bits;
864 Pixmap pixmap, clipmask = (Pixmap) 0;
865 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
866 XGCValues gcv;
868 if (p->wd > 8)
869 bits = (unsigned char *)(p->bits + p->dh);
870 else
871 bits = (unsigned char *)p->bits + p->dh;
873 /* Draw the bitmap. I believe these small pixmaps can be cached
874 by the server. */
875 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
876 (p->cursor_p
877 ? (p->overlay_p ? face->background
878 : f->output_data.x->cursor_pixel)
879 : face->foreground),
880 face->background, depth);
882 if (p->overlay_p)
884 clipmask = XCreatePixmapFromBitmapData (display,
885 FRAME_X_DISPLAY_INFO (f)->root_window,
886 bits, p->wd, p->h,
887 1, 0, 1);
888 gcv.clip_mask = clipmask;
889 gcv.clip_x_origin = p->x;
890 gcv.clip_y_origin = p->y;
891 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
894 XCopyArea (display, pixmap, window, gc, 0, 0,
895 p->wd, p->h, p->x, p->y);
896 XFreePixmap (display, pixmap);
898 if (p->overlay_p)
900 gcv.clip_mask = (Pixmap) 0;
901 XChangeGC (display, gc, GCClipMask, &gcv);
902 XFreePixmap (display, clipmask);
906 XSetClipMask (display, gc, None);
911 /* This is called when starting Emacs and when restarting after
912 suspend. When starting Emacs, no X window is mapped. And nothing
913 must be done to Emacs's own window if it is suspended (though that
914 rarely happens). */
916 static void
917 XTset_terminal_modes (struct terminal *terminal)
921 /* This is called when exiting or suspending Emacs. Exiting will make
922 the X-windows go away, and suspending requires no action. */
924 static void
925 XTreset_terminal_modes (struct terminal *terminal)
930 /***********************************************************************
931 Glyph display
932 ***********************************************************************/
936 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
937 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
938 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
939 int));
940 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
941 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
942 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
943 static void x_draw_glyph_string P_ ((struct glyph_string *));
944 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
945 static void x_set_cursor_gc P_ ((struct glyph_string *));
946 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
947 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
948 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
949 unsigned long *, double, int));
950 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
951 double, int, unsigned long));
952 static void x_setup_relief_colors P_ ((struct glyph_string *));
953 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
954 static void x_draw_image_relief P_ ((struct glyph_string *));
955 static void x_draw_image_foreground P_ ((struct glyph_string *));
956 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
957 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
958 int, int, int));
959 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
960 int, int, int, int, int, int,
961 XRectangle *));
962 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
963 int, int, int, XRectangle *));
965 #if GLYPH_DEBUG
966 static void x_check_font P_ ((struct frame *, struct font *));
967 #endif
970 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
971 face. */
973 static void
974 x_set_cursor_gc (s)
975 struct glyph_string *s;
977 if (s->font == FRAME_FONT (s->f)
978 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
979 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
980 && !s->cmp)
981 s->gc = s->f->output_data.x->cursor_gc;
982 else
984 /* Cursor on non-default face: must merge. */
985 XGCValues xgcv;
986 unsigned long mask;
988 xgcv.background = s->f->output_data.x->cursor_pixel;
989 xgcv.foreground = s->face->background;
991 /* If the glyph would be invisible, try a different foreground. */
992 if (xgcv.foreground == xgcv.background)
993 xgcv.foreground = s->face->foreground;
994 if (xgcv.foreground == xgcv.background)
995 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
996 if (xgcv.foreground == xgcv.background)
997 xgcv.foreground = s->face->foreground;
999 /* Make sure the cursor is distinct from text in this face. */
1000 if (xgcv.background == s->face->background
1001 && xgcv.foreground == s->face->foreground)
1003 xgcv.background = s->face->foreground;
1004 xgcv.foreground = s->face->background;
1007 IF_DEBUG (x_check_font (s->f, s->font));
1008 xgcv.graphics_exposures = False;
1009 mask = GCForeground | GCBackground | GCGraphicsExposures;
1011 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1012 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1013 mask, &xgcv);
1014 else
1015 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1016 = XCreateGC (s->display, s->window, mask, &xgcv);
1018 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1023 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1025 static void
1026 x_set_mouse_face_gc (s)
1027 struct glyph_string *s;
1029 int face_id;
1030 struct face *face;
1032 /* What face has to be used last for the mouse face? */
1033 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1034 face = FACE_FROM_ID (s->f, face_id);
1035 if (face == NULL)
1036 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1038 if (s->first_glyph->type == CHAR_GLYPH)
1039 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1040 else
1041 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1042 s->face = FACE_FROM_ID (s->f, face_id);
1043 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1045 if (s->font == s->face->font)
1046 s->gc = s->face->gc;
1047 else
1049 /* Otherwise construct scratch_cursor_gc with values from FACE
1050 except for FONT. */
1051 XGCValues xgcv;
1052 unsigned long mask;
1054 xgcv.background = s->face->background;
1055 xgcv.foreground = s->face->foreground;
1056 xgcv.graphics_exposures = False;
1057 mask = GCForeground | GCBackground | GCGraphicsExposures;
1059 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1060 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1061 mask, &xgcv);
1062 else
1063 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1064 = XCreateGC (s->display, s->window, mask, &xgcv);
1066 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1069 xassert (s->gc != 0);
1073 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1074 Faces to use in the mode line have already been computed when the
1075 matrix was built, so there isn't much to do, here. */
1077 static INLINE void
1078 x_set_mode_line_face_gc (s)
1079 struct glyph_string *s;
1081 s->gc = s->face->gc;
1085 /* Set S->gc of glyph string S for drawing that glyph string. Set
1086 S->stippled_p to a non-zero value if the face of S has a stipple
1087 pattern. */
1089 static INLINE void
1090 x_set_glyph_string_gc (s)
1091 struct glyph_string *s;
1093 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1095 if (s->hl == DRAW_NORMAL_TEXT)
1097 s->gc = s->face->gc;
1098 s->stippled_p = s->face->stipple != 0;
1100 else if (s->hl == DRAW_INVERSE_VIDEO)
1102 x_set_mode_line_face_gc (s);
1103 s->stippled_p = s->face->stipple != 0;
1105 else if (s->hl == DRAW_CURSOR)
1107 x_set_cursor_gc (s);
1108 s->stippled_p = 0;
1110 else if (s->hl == DRAW_MOUSE_FACE)
1112 x_set_mouse_face_gc (s);
1113 s->stippled_p = s->face->stipple != 0;
1115 else if (s->hl == DRAW_IMAGE_RAISED
1116 || s->hl == DRAW_IMAGE_SUNKEN)
1118 s->gc = s->face->gc;
1119 s->stippled_p = s->face->stipple != 0;
1121 else
1123 s->gc = s->face->gc;
1124 s->stippled_p = s->face->stipple != 0;
1127 /* GC must have been set. */
1128 xassert (s->gc != 0);
1132 /* Set clipping for output of glyph string S. S may be part of a mode
1133 line or menu if we don't have X toolkit support. */
1135 static INLINE void
1136 x_set_glyph_string_clipping (s)
1137 struct glyph_string *s;
1139 XRectangle *r = s->clip;
1140 int n = get_glyph_string_clip_rects (s, r, 2);
1142 if (n > 0)
1143 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
1144 s->num_clips = n;
1148 /* Set SRC's clipping for output of glyph string DST. This is called
1149 when we are drawing DST's left_overhang or right_overhang only in
1150 the area of SRC. */
1152 static void
1153 x_set_glyph_string_clipping_exactly (src, dst)
1154 struct glyph_string *src, *dst;
1156 XRectangle r;
1158 r.x = src->x;
1159 r.width = src->width;
1160 r.y = src->y;
1161 r.height = src->height;
1162 dst->clip[0] = r;
1163 dst->num_clips = 1;
1164 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1168 /* RIF:
1169 Compute left and right overhang of glyph string S. */
1171 static void
1172 x_compute_glyph_string_overhangs (s)
1173 struct glyph_string *s;
1175 if (s->cmp == NULL
1176 && (s->first_glyph->type == CHAR_GLYPH
1177 || s->first_glyph->type == COMPOSITE_GLYPH))
1179 struct font_metrics metrics;
1181 if (s->first_glyph->type == CHAR_GLYPH)
1183 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1184 struct font *font = s->font;
1185 int i;
1187 for (i = 0; i < s->nchars; i++)
1188 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1189 font->driver->text_extents (font, code, s->nchars, &metrics);
1191 else
1193 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1195 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1197 s->right_overhang = (metrics.rbearing > metrics.width
1198 ? metrics.rbearing - metrics.width : 0);
1199 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1201 else if (s->cmp)
1203 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1204 s->left_overhang = - s->cmp->lbearing;
1209 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1211 static INLINE void
1212 x_clear_glyph_string_rect (s, x, y, w, h)
1213 struct glyph_string *s;
1214 int x, y, w, h;
1216 XGCValues xgcv;
1217 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1218 XSetForeground (s->display, s->gc, xgcv.background);
1219 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1220 XSetForeground (s->display, s->gc, xgcv.foreground);
1224 /* Draw the background of glyph_string S. If S->background_filled_p
1225 is non-zero don't draw it. FORCE_P non-zero means draw the
1226 background even if it wouldn't be drawn normally. This is used
1227 when a string preceding S draws into the background of S, or S
1228 contains the first component of a composition. */
1230 static void
1231 x_draw_glyph_string_background (s, force_p)
1232 struct glyph_string *s;
1233 int force_p;
1235 /* Nothing to do if background has already been drawn or if it
1236 shouldn't be drawn in the first place. */
1237 if (!s->background_filled_p)
1239 int box_line_width = max (s->face->box_line_width, 0);
1241 if (s->stippled_p)
1243 /* Fill background with a stipple pattern. */
1244 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1245 XFillRectangle (s->display, s->window, s->gc, s->x,
1246 s->y + box_line_width,
1247 s->background_width,
1248 s->height - 2 * box_line_width);
1249 XSetFillStyle (s->display, s->gc, FillSolid);
1250 s->background_filled_p = 1;
1252 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1253 || s->font_not_found_p
1254 || s->extends_to_end_of_line_p
1255 || force_p)
1257 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1258 s->background_width,
1259 s->height - 2 * box_line_width);
1260 s->background_filled_p = 1;
1266 /* Draw the foreground of glyph string S. */
1268 static void
1269 x_draw_glyph_string_foreground (s)
1270 struct glyph_string *s;
1272 int i, x;
1274 /* If first glyph of S has a left box line, start drawing the text
1275 of S to the right of that box line. */
1276 if (s->face->box != FACE_NO_BOX
1277 && s->first_glyph->left_box_line_p)
1278 x = s->x + eabs (s->face->box_line_width);
1279 else
1280 x = s->x;
1282 /* Draw characters of S as rectangles if S's font could not be
1283 loaded. */
1284 if (s->font_not_found_p)
1286 for (i = 0; i < s->nchars; ++i)
1288 struct glyph *g = s->first_glyph + i;
1289 XDrawRectangle (s->display, s->window,
1290 s->gc, x, s->y, g->pixel_width - 1,
1291 s->height - 1);
1292 x += g->pixel_width;
1295 else
1297 struct font *font = s->font;
1298 int boff = font->baseline_offset;
1299 int y;
1301 if (font->vertical_centering)
1302 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1304 y = s->ybase - boff;
1305 if (s->for_overlaps
1306 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1307 font->driver->draw (s, 0, s->nchars, x, y, 0);
1308 else
1309 font->driver->draw (s, 0, s->nchars, x, y, 1);
1310 if (s->face->overstrike)
1311 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1315 /* Draw the foreground of composite glyph string S. */
1317 static void
1318 x_draw_composite_glyph_string_foreground (s)
1319 struct glyph_string *s;
1321 int i, j, x;
1322 struct font *font = s->font;
1324 /* If first glyph of S has a left box line, start drawing the text
1325 of S to the right of that box line. */
1326 if (s->face && s->face->box != FACE_NO_BOX
1327 && s->first_glyph->left_box_line_p)
1328 x = s->x + eabs (s->face->box_line_width);
1329 else
1330 x = s->x;
1332 /* S is a glyph string for a composition. S->cmp_from is the index
1333 of the first character drawn for glyphs of this composition.
1334 S->cmp_from == 0 means we are drawing the very first character of
1335 this composition. */
1337 /* Draw a rectangle for the composition if the font for the very
1338 first character of the composition could not be loaded. */
1339 if (s->font_not_found_p)
1341 if (s->cmp_from == 0)
1342 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1343 s->width - 1, s->height - 1);
1345 else if (! s->first_glyph->u.cmp.automatic)
1347 int y = s->ybase;
1349 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1350 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1352 int xx = x + s->cmp->offsets[j * 2];
1353 int yy = y - s->cmp->offsets[j * 2 + 1];
1355 font->driver->draw (s, j, j + 1, xx, yy, 0);
1356 if (s->face->overstrike)
1357 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1360 else
1362 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1363 Lisp_Object glyph;
1364 int y = s->ybase;
1365 int width = 0;
1367 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1369 glyph = LGSTRING_GLYPH (gstring, i);
1370 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1371 width += LGLYPH_WIDTH (glyph);
1372 else
1374 int xoff, yoff, wadjust;
1376 if (j < i)
1378 font->driver->draw (s, j, i, x, y, 0);
1379 if (s->face->overstrike)
1380 font->driver->draw (s, j, i, x + 1, y, 0);
1381 x += width;
1383 xoff = LGLYPH_XOFF (glyph);
1384 yoff = LGLYPH_YOFF (glyph);
1385 wadjust = LGLYPH_WADJUST (glyph);
1386 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1387 if (s->face->overstrike)
1388 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff, 0);
1389 x += wadjust;
1390 j = i + 1;
1391 width = 0;
1394 if (j < i)
1396 font->driver->draw (s, j, i, x, y, 0);
1397 if (s->face->overstrike)
1398 font->driver->draw (s, j, i, x + 1, y, 0);
1404 #ifdef USE_X_TOOLKIT
1406 static struct frame *x_frame_of_widget P_ ((Widget));
1407 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1408 XrmValue *, XrmValue *, XtPointer *));
1409 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1410 XrmValue *, Cardinal *));
1413 /* Return the frame on which widget WIDGET is used.. Abort if frame
1414 cannot be determined. */
1416 static struct frame *
1417 x_frame_of_widget (widget)
1418 Widget widget;
1420 struct x_display_info *dpyinfo;
1421 Lisp_Object tail;
1422 struct frame *f;
1424 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1426 /* Find the top-level shell of the widget. Note that this function
1427 can be called when the widget is not yet realized, so XtWindow
1428 (widget) == 0. That's the reason we can't simply use
1429 x_any_window_to_frame. */
1430 while (!XtIsTopLevelShell (widget))
1431 widget = XtParent (widget);
1433 /* Look for a frame with that top-level widget. Allocate the color
1434 on that frame to get the right gamma correction value. */
1435 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1436 if (FRAMEP (XCAR (tail))
1437 && (f = XFRAME (XCAR (tail)),
1438 (FRAME_X_P (f)
1439 && f->output_data.nothing != 1
1440 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1441 && f->output_data.x->widget == widget)
1442 return f;
1444 abort ();
1448 /* Allocate the color COLOR->pixel on the screen and display of
1449 widget WIDGET in colormap CMAP. If an exact match cannot be
1450 allocated, try the nearest color available. Value is non-zero
1451 if successful. This is called from lwlib. */
1454 x_alloc_nearest_color_for_widget (widget, cmap, color)
1455 Widget widget;
1456 Colormap cmap;
1457 XColor *color;
1459 struct frame *f = x_frame_of_widget (widget);
1460 return x_alloc_nearest_color (f, cmap, color);
1464 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1465 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1466 If this produces the same color as PIXEL, try a color where all RGB
1467 values have DELTA added. Return the allocated color in *PIXEL.
1468 DISPLAY is the X display, CMAP is the colormap to operate on.
1469 Value is non-zero if successful. */
1472 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1473 Widget widget;
1474 Display *display;
1475 Colormap cmap;
1476 unsigned long *pixel;
1477 double factor;
1478 int delta;
1480 struct frame *f = x_frame_of_widget (widget);
1481 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1485 /* Structure specifying which arguments should be passed by Xt to
1486 cvt_string_to_pixel. We want the widget's screen and colormap. */
1488 static XtConvertArgRec cvt_string_to_pixel_args[] =
1490 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1491 sizeof (Screen *)},
1492 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1493 sizeof (Colormap)}
1497 /* The address of this variable is returned by
1498 cvt_string_to_pixel. */
1500 static Pixel cvt_string_to_pixel_value;
1503 /* Convert a color name to a pixel color.
1505 DPY is the display we are working on.
1507 ARGS is an array of *NARGS XrmValue structures holding additional
1508 information about the widget for which the conversion takes place.
1509 The contents of this array are determined by the specification
1510 in cvt_string_to_pixel_args.
1512 FROM is a pointer to an XrmValue which points to the color name to
1513 convert. TO is an XrmValue in which to return the pixel color.
1515 CLOSURE_RET is a pointer to user-data, in which we record if
1516 we allocated the color or not.
1518 Value is True if successful, False otherwise. */
1520 static Boolean
1521 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1522 Display *dpy;
1523 XrmValue *args;
1524 Cardinal *nargs;
1525 XrmValue *from, *to;
1526 XtPointer *closure_ret;
1528 Screen *screen;
1529 Colormap cmap;
1530 Pixel pixel;
1531 String color_name;
1532 XColor color;
1534 if (*nargs != 2)
1536 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1537 "wrongParameters", "cvt_string_to_pixel",
1538 "XtToolkitError",
1539 "Screen and colormap args required", NULL, NULL);
1540 return False;
1543 screen = *(Screen **) args[0].addr;
1544 cmap = *(Colormap *) args[1].addr;
1545 color_name = (String) from->addr;
1547 if (strcmp (color_name, XtDefaultBackground) == 0)
1549 *closure_ret = (XtPointer) False;
1550 pixel = WhitePixelOfScreen (screen);
1552 else if (strcmp (color_name, XtDefaultForeground) == 0)
1554 *closure_ret = (XtPointer) False;
1555 pixel = BlackPixelOfScreen (screen);
1557 else if (XParseColor (dpy, cmap, color_name, &color)
1558 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1560 pixel = color.pixel;
1561 *closure_ret = (XtPointer) True;
1563 else
1565 String params[1];
1566 Cardinal nparams = 1;
1568 params[0] = color_name;
1569 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1570 "badValue", "cvt_string_to_pixel",
1571 "XtToolkitError", "Invalid color `%s'",
1572 params, &nparams);
1573 return False;
1576 if (to->addr != NULL)
1578 if (to->size < sizeof (Pixel))
1580 to->size = sizeof (Pixel);
1581 return False;
1584 *(Pixel *) to->addr = pixel;
1586 else
1588 cvt_string_to_pixel_value = pixel;
1589 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1592 to->size = sizeof (Pixel);
1593 return True;
1597 /* Free a pixel color which was previously allocated via
1598 cvt_string_to_pixel. This is registered as the destructor
1599 for this type of resource via XtSetTypeConverter.
1601 APP is the application context in which we work.
1603 TO is a pointer to an XrmValue holding the color to free.
1604 CLOSURE is the value we stored in CLOSURE_RET for this color
1605 in cvt_string_to_pixel.
1607 ARGS and NARGS are like for cvt_string_to_pixel. */
1609 static void
1610 cvt_pixel_dtor (app, to, closure, args, nargs)
1611 XtAppContext app;
1612 XrmValuePtr to;
1613 XtPointer closure;
1614 XrmValuePtr args;
1615 Cardinal *nargs;
1617 if (*nargs != 2)
1619 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1620 "XtToolkitError",
1621 "Screen and colormap arguments required",
1622 NULL, NULL);
1624 else if (closure != NULL)
1626 /* We did allocate the pixel, so free it. */
1627 Screen *screen = *(Screen **) args[0].addr;
1628 Colormap cmap = *(Colormap *) args[1].addr;
1629 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1630 (Pixel *) to->addr, 1);
1635 #endif /* USE_X_TOOLKIT */
1638 /* Value is an array of XColor structures for the contents of the
1639 color map of display DPY. Set *NCELLS to the size of the array.
1640 Note that this probably shouldn't be called for large color maps,
1641 say a 24-bit TrueColor map. */
1643 static const XColor *
1644 x_color_cells (dpy, ncells)
1645 Display *dpy;
1646 int *ncells;
1648 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1650 if (dpyinfo->color_cells == NULL)
1652 Screen *screen = dpyinfo->screen;
1653 int i;
1655 dpyinfo->ncolor_cells
1656 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1657 dpyinfo->color_cells
1658 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1659 * sizeof *dpyinfo->color_cells);
1661 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1662 dpyinfo->color_cells[i].pixel = i;
1664 XQueryColors (dpy, dpyinfo->cmap,
1665 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1668 *ncells = dpyinfo->ncolor_cells;
1669 return dpyinfo->color_cells;
1673 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1674 colors in COLORS. Use cached information, if available. */
1676 void
1677 x_query_colors (f, colors, ncolors)
1678 struct frame *f;
1679 XColor *colors;
1680 int ncolors;
1682 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1684 if (dpyinfo->color_cells)
1686 int i;
1687 for (i = 0; i < ncolors; ++i)
1689 unsigned long pixel = colors[i].pixel;
1690 xassert (pixel < dpyinfo->ncolor_cells);
1691 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1692 colors[i] = dpyinfo->color_cells[pixel];
1695 else
1696 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1700 /* On frame F, translate pixel color to RGB values for the color in
1701 COLOR. Use cached information, if available. */
1703 void
1704 x_query_color (f, color)
1705 struct frame *f;
1706 XColor *color;
1708 x_query_colors (f, color, 1);
1712 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1713 exact match can't be allocated, try the nearest color available.
1714 Value is non-zero if successful. Set *COLOR to the color
1715 allocated. */
1717 static int
1718 x_alloc_nearest_color_1 (dpy, cmap, color)
1719 Display *dpy;
1720 Colormap cmap;
1721 XColor *color;
1723 int rc;
1725 rc = XAllocColor (dpy, cmap, color);
1726 if (rc == 0)
1728 /* If we got to this point, the colormap is full, so we're going
1729 to try to get the next closest color. The algorithm used is
1730 a least-squares matching, which is what X uses for closest
1731 color matching with StaticColor visuals. */
1732 int nearest, i;
1733 unsigned long nearest_delta = ~0;
1734 int ncells;
1735 const XColor *cells = x_color_cells (dpy, &ncells);
1737 for (nearest = i = 0; i < ncells; ++i)
1739 long dred = (color->red >> 8) - (cells[i].red >> 8);
1740 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1741 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1742 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1744 if (delta < nearest_delta)
1746 nearest = i;
1747 nearest_delta = delta;
1751 color->red = cells[nearest].red;
1752 color->green = cells[nearest].green;
1753 color->blue = cells[nearest].blue;
1754 rc = XAllocColor (dpy, cmap, color);
1756 else
1758 /* If allocation succeeded, and the allocated pixel color is not
1759 equal to a cached pixel color recorded earlier, there was a
1760 change in the colormap, so clear the color cache. */
1761 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1762 XColor *cached_color;
1764 if (dpyinfo->color_cells
1765 && (cached_color = &dpyinfo->color_cells[color->pixel],
1766 (cached_color->red != color->red
1767 || cached_color->blue != color->blue
1768 || cached_color->green != color->green)))
1770 xfree (dpyinfo->color_cells);
1771 dpyinfo->color_cells = NULL;
1772 dpyinfo->ncolor_cells = 0;
1776 #ifdef DEBUG_X_COLORS
1777 if (rc)
1778 register_color (color->pixel);
1779 #endif /* DEBUG_X_COLORS */
1781 return rc;
1785 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1786 exact match can't be allocated, try the nearest color available.
1787 Value is non-zero if successful. Set *COLOR to the color
1788 allocated. */
1791 x_alloc_nearest_color (f, cmap, color)
1792 struct frame *f;
1793 Colormap cmap;
1794 XColor *color;
1796 gamma_correct (f, color);
1797 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1801 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1802 It's necessary to do this instead of just using PIXEL directly to
1803 get color reference counts right. */
1805 unsigned long
1806 x_copy_color (f, pixel)
1807 struct frame *f;
1808 unsigned long pixel;
1810 XColor color;
1812 color.pixel = pixel;
1813 BLOCK_INPUT;
1814 x_query_color (f, &color);
1815 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1816 UNBLOCK_INPUT;
1817 #ifdef DEBUG_X_COLORS
1818 register_color (pixel);
1819 #endif
1820 return color.pixel;
1824 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1825 It's necessary to do this instead of just using PIXEL directly to
1826 get color reference counts right. */
1828 unsigned long
1829 x_copy_dpy_color (dpy, cmap, pixel)
1830 Display *dpy;
1831 Colormap cmap;
1832 unsigned long pixel;
1834 XColor color;
1836 color.pixel = pixel;
1837 BLOCK_INPUT;
1838 XQueryColor (dpy, cmap, &color);
1839 XAllocColor (dpy, cmap, &color);
1840 UNBLOCK_INPUT;
1841 #ifdef DEBUG_X_COLORS
1842 register_color (pixel);
1843 #endif
1844 return color.pixel;
1848 /* Brightness beyond which a color won't have its highlight brightness
1849 boosted.
1851 Nominally, highlight colors for `3d' faces are calculated by
1852 brightening an object's color by a constant scale factor, but this
1853 doesn't yield good results for dark colors, so for colors who's
1854 brightness is less than this value (on a scale of 0-65535) have an
1855 use an additional additive factor.
1857 The value here is set so that the default menu-bar/mode-line color
1858 (grey75) will not have its highlights changed at all. */
1859 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1862 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1863 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1864 If this produces the same color as PIXEL, try a color where all RGB
1865 values have DELTA added. Return the allocated color in *PIXEL.
1866 DISPLAY is the X display, CMAP is the colormap to operate on.
1867 Value is non-zero if successful. */
1869 static int
1870 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1871 struct frame *f;
1872 Display *display;
1873 Colormap cmap;
1874 unsigned long *pixel;
1875 double factor;
1876 int delta;
1878 XColor color, new;
1879 long bright;
1880 int success_p;
1882 /* Get RGB color values. */
1883 color.pixel = *pixel;
1884 x_query_color (f, &color);
1886 /* Change RGB values by specified FACTOR. Avoid overflow! */
1887 xassert (factor >= 0);
1888 new.red = min (0xffff, factor * color.red);
1889 new.green = min (0xffff, factor * color.green);
1890 new.blue = min (0xffff, factor * color.blue);
1892 /* Calculate brightness of COLOR. */
1893 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1895 /* We only boost colors that are darker than
1896 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1897 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1898 /* Make an additive adjustment to NEW, because it's dark enough so
1899 that scaling by FACTOR alone isn't enough. */
1901 /* How far below the limit this color is (0 - 1, 1 being darker). */
1902 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1903 /* The additive adjustment. */
1904 int min_delta = delta * dimness * factor / 2;
1906 if (factor < 1)
1908 new.red = max (0, new.red - min_delta);
1909 new.green = max (0, new.green - min_delta);
1910 new.blue = max (0, new.blue - min_delta);
1912 else
1914 new.red = min (0xffff, min_delta + new.red);
1915 new.green = min (0xffff, min_delta + new.green);
1916 new.blue = min (0xffff, min_delta + new.blue);
1920 /* Try to allocate the color. */
1921 success_p = x_alloc_nearest_color (f, cmap, &new);
1922 if (success_p)
1924 if (new.pixel == *pixel)
1926 /* If we end up with the same color as before, try adding
1927 delta to the RGB values. */
1928 x_free_colors (f, &new.pixel, 1);
1930 new.red = min (0xffff, delta + color.red);
1931 new.green = min (0xffff, delta + color.green);
1932 new.blue = min (0xffff, delta + color.blue);
1933 success_p = x_alloc_nearest_color (f, cmap, &new);
1935 else
1936 success_p = 1;
1937 *pixel = new.pixel;
1940 return success_p;
1944 /* Set up the foreground color for drawing relief lines of glyph
1945 string S. RELIEF is a pointer to a struct relief containing the GC
1946 with which lines will be drawn. Use a color that is FACTOR or
1947 DELTA lighter or darker than the relief's background which is found
1948 in S->f->output_data.x->relief_background. If such a color cannot
1949 be allocated, use DEFAULT_PIXEL, instead. */
1951 static void
1952 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1953 struct frame *f;
1954 struct relief *relief;
1955 double factor;
1956 int delta;
1957 unsigned long default_pixel;
1959 XGCValues xgcv;
1960 struct x_output *di = f->output_data.x;
1961 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1962 unsigned long pixel;
1963 unsigned long background = di->relief_background;
1964 Colormap cmap = FRAME_X_COLORMAP (f);
1965 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1966 Display *dpy = FRAME_X_DISPLAY (f);
1968 xgcv.graphics_exposures = False;
1969 xgcv.line_width = 1;
1971 /* Free previously allocated color. The color cell will be reused
1972 when it has been freed as many times as it was allocated, so this
1973 doesn't affect faces using the same colors. */
1974 if (relief->gc
1975 && relief->allocated_p)
1977 x_free_colors (f, &relief->pixel, 1);
1978 relief->allocated_p = 0;
1981 /* Allocate new color. */
1982 xgcv.foreground = default_pixel;
1983 pixel = background;
1984 if (dpyinfo->n_planes != 1
1985 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1987 relief->allocated_p = 1;
1988 xgcv.foreground = relief->pixel = pixel;
1991 if (relief->gc == 0)
1993 xgcv.stipple = dpyinfo->gray;
1994 mask |= GCStipple;
1995 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1997 else
1998 XChangeGC (dpy, relief->gc, mask, &xgcv);
2002 /* Set up colors for the relief lines around glyph string S. */
2004 static void
2005 x_setup_relief_colors (s)
2006 struct glyph_string *s;
2008 struct x_output *di = s->f->output_data.x;
2009 unsigned long color;
2011 if (s->face->use_box_color_for_shadows_p)
2012 color = s->face->box_color;
2013 else if (s->first_glyph->type == IMAGE_GLYPH
2014 && s->img->pixmap
2015 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2016 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2017 else
2019 XGCValues xgcv;
2021 /* Get the background color of the face. */
2022 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2023 color = xgcv.background;
2026 if (di->white_relief.gc == 0
2027 || color != di->relief_background)
2029 di->relief_background = color;
2030 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2031 WHITE_PIX_DEFAULT (s->f));
2032 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2033 BLACK_PIX_DEFAULT (s->f));
2038 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2039 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2040 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2041 relief. LEFT_P non-zero means draw a relief on the left side of
2042 the rectangle. RIGHT_P non-zero means draw a relief on the right
2043 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2044 when drawing. */
2046 static void
2047 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2048 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2049 struct frame *f;
2050 int left_x, top_y, right_x, bottom_y, width;
2051 int top_p, bot_p, left_p, right_p, raised_p;
2052 XRectangle *clip_rect;
2054 Display *dpy = FRAME_X_DISPLAY (f);
2055 Window window = FRAME_X_WINDOW (f);
2056 int i;
2057 GC gc;
2059 if (raised_p)
2060 gc = f->output_data.x->white_relief.gc;
2061 else
2062 gc = f->output_data.x->black_relief.gc;
2063 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2065 /* Top. */
2066 if (top_p)
2067 for (i = 0; i < width; ++i)
2068 XDrawLine (dpy, window, gc,
2069 left_x + i * left_p, top_y + i,
2070 right_x + 1 - i * right_p, top_y + i);
2072 /* Left. */
2073 if (left_p)
2074 for (i = 0; i < width; ++i)
2075 XDrawLine (dpy, window, gc,
2076 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2078 XSetClipMask (dpy, gc, None);
2079 if (raised_p)
2080 gc = f->output_data.x->black_relief.gc;
2081 else
2082 gc = f->output_data.x->white_relief.gc;
2083 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2085 /* Bottom. */
2086 if (bot_p)
2087 for (i = 0; i < width; ++i)
2088 XDrawLine (dpy, window, gc,
2089 left_x + i * left_p, bottom_y - i,
2090 right_x + 1 - i * right_p, bottom_y - i);
2092 /* Right. */
2093 if (right_p)
2094 for (i = 0; i < width; ++i)
2095 XDrawLine (dpy, window, gc,
2096 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2098 XSetClipMask (dpy, gc, None);
2102 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2103 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2104 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2105 left side of the rectangle. RIGHT_P non-zero means draw a line
2106 on the right side of the rectangle. CLIP_RECT is the clipping
2107 rectangle to use when drawing. */
2109 static void
2110 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2111 left_p, right_p, clip_rect)
2112 struct glyph_string *s;
2113 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2114 XRectangle *clip_rect;
2116 XGCValues xgcv;
2118 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2119 XSetForeground (s->display, s->gc, s->face->box_color);
2120 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2122 /* Top. */
2123 XFillRectangle (s->display, s->window, s->gc,
2124 left_x, top_y, right_x - left_x + 1, width);
2126 /* Left. */
2127 if (left_p)
2128 XFillRectangle (s->display, s->window, s->gc,
2129 left_x, top_y, width, bottom_y - top_y + 1);
2131 /* Bottom. */
2132 XFillRectangle (s->display, s->window, s->gc,
2133 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2135 /* Right. */
2136 if (right_p)
2137 XFillRectangle (s->display, s->window, s->gc,
2138 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2140 XSetForeground (s->display, s->gc, xgcv.foreground);
2141 XSetClipMask (s->display, s->gc, None);
2145 /* Draw a box around glyph string S. */
2147 static void
2148 x_draw_glyph_string_box (s)
2149 struct glyph_string *s;
2151 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2152 int left_p, right_p;
2153 struct glyph *last_glyph;
2154 XRectangle clip_rect;
2156 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2157 ? WINDOW_RIGHT_EDGE_X (s->w)
2158 : window_box_right (s->w, s->area));
2160 /* The glyph that may have a right box line. */
2161 last_glyph = (s->cmp || s->img
2162 ? s->first_glyph
2163 : s->first_glyph + s->nchars - 1);
2165 width = eabs (s->face->box_line_width);
2166 raised_p = s->face->box == FACE_RAISED_BOX;
2167 left_x = s->x;
2168 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2169 ? last_x - 1
2170 : min (last_x, s->x + s->background_width) - 1);
2171 top_y = s->y;
2172 bottom_y = top_y + s->height - 1;
2174 left_p = (s->first_glyph->left_box_line_p
2175 || (s->hl == DRAW_MOUSE_FACE
2176 && (s->prev == NULL
2177 || s->prev->hl != s->hl)));
2178 right_p = (last_glyph->right_box_line_p
2179 || (s->hl == DRAW_MOUSE_FACE
2180 && (s->next == NULL
2181 || s->next->hl != s->hl)));
2183 get_glyph_string_clip_rect (s, &clip_rect);
2185 if (s->face->box == FACE_SIMPLE_BOX)
2186 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2187 left_p, right_p, &clip_rect);
2188 else
2190 x_setup_relief_colors (s);
2191 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2192 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2197 /* Draw foreground of image glyph string S. */
2199 static void
2200 x_draw_image_foreground (s)
2201 struct glyph_string *s;
2203 int x = s->x;
2204 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2206 /* If first glyph of S has a left box line, start drawing it to the
2207 right of that line. */
2208 if (s->face->box != FACE_NO_BOX
2209 && s->first_glyph->left_box_line_p
2210 && s->slice.x == 0)
2211 x += eabs (s->face->box_line_width);
2213 /* If there is a margin around the image, adjust x- and y-position
2214 by that margin. */
2215 if (s->slice.x == 0)
2216 x += s->img->hmargin;
2217 if (s->slice.y == 0)
2218 y += s->img->vmargin;
2220 if (s->img->pixmap)
2222 if (s->img->mask)
2224 /* We can't set both a clip mask and use XSetClipRectangles
2225 because the latter also sets a clip mask. We also can't
2226 trust on the shape extension to be available
2227 (XShapeCombineRegion). So, compute the rectangle to draw
2228 manually. */
2229 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2230 | GCFunction);
2231 XGCValues xgcv;
2232 XRectangle clip_rect, image_rect, r;
2234 xgcv.clip_mask = s->img->mask;
2235 xgcv.clip_x_origin = x;
2236 xgcv.clip_y_origin = y;
2237 xgcv.function = GXcopy;
2238 XChangeGC (s->display, s->gc, mask, &xgcv);
2240 get_glyph_string_clip_rect (s, &clip_rect);
2241 image_rect.x = x;
2242 image_rect.y = y;
2243 image_rect.width = s->slice.width;
2244 image_rect.height = s->slice.height;
2245 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2246 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2247 s->slice.x + r.x - x, s->slice.y + r.y - y,
2248 r.width, r.height, r.x, r.y);
2250 else
2252 XRectangle clip_rect, image_rect, r;
2254 get_glyph_string_clip_rect (s, &clip_rect);
2255 image_rect.x = x;
2256 image_rect.y = y;
2257 image_rect.width = s->slice.width;
2258 image_rect.height = s->slice.height;
2259 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2260 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2261 s->slice.x + r.x - x, s->slice.y + r.y - y,
2262 r.width, r.height, r.x, r.y);
2264 /* When the image has a mask, we can expect that at
2265 least part of a mouse highlight or a block cursor will
2266 be visible. If the image doesn't have a mask, make
2267 a block cursor visible by drawing a rectangle around
2268 the image. I believe it's looking better if we do
2269 nothing here for mouse-face. */
2270 if (s->hl == DRAW_CURSOR)
2272 int r = s->img->relief;
2273 if (r < 0) r = -r;
2274 XDrawRectangle (s->display, s->window, s->gc,
2275 x - r, y - r,
2276 s->slice.width + r*2 - 1,
2277 s->slice.height + r*2 - 1);
2281 else
2282 /* Draw a rectangle if image could not be loaded. */
2283 XDrawRectangle (s->display, s->window, s->gc, x, y,
2284 s->slice.width - 1, s->slice.height - 1);
2288 /* Draw a relief around the image glyph string S. */
2290 static void
2291 x_draw_image_relief (s)
2292 struct glyph_string *s;
2294 int x0, y0, x1, y1, thick, raised_p;
2295 XRectangle r;
2296 int x = s->x;
2297 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2299 /* If first glyph of S has a left box line, start drawing it to the
2300 right of that line. */
2301 if (s->face->box != FACE_NO_BOX
2302 && s->first_glyph->left_box_line_p
2303 && s->slice.x == 0)
2304 x += eabs (s->face->box_line_width);
2306 /* If there is a margin around the image, adjust x- and y-position
2307 by that margin. */
2308 if (s->slice.x == 0)
2309 x += s->img->hmargin;
2310 if (s->slice.y == 0)
2311 y += s->img->vmargin;
2313 if (s->hl == DRAW_IMAGE_SUNKEN
2314 || s->hl == DRAW_IMAGE_RAISED)
2316 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2317 raised_p = s->hl == DRAW_IMAGE_RAISED;
2319 else
2321 thick = eabs (s->img->relief);
2322 raised_p = s->img->relief > 0;
2325 x0 = x - thick;
2326 y0 = y - thick;
2327 x1 = x + s->slice.width + thick - 1;
2328 y1 = y + s->slice.height + thick - 1;
2330 x_setup_relief_colors (s);
2331 get_glyph_string_clip_rect (s, &r);
2332 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2333 s->slice.y == 0,
2334 s->slice.y + s->slice.height == s->img->height,
2335 s->slice.x == 0,
2336 s->slice.x + s->slice.width == s->img->width,
2337 &r);
2341 /* Draw the foreground of image glyph string S to PIXMAP. */
2343 static void
2344 x_draw_image_foreground_1 (s, pixmap)
2345 struct glyph_string *s;
2346 Pixmap pixmap;
2348 int x = 0;
2349 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2351 /* If first glyph of S has a left box line, start drawing it to the
2352 right of that line. */
2353 if (s->face->box != FACE_NO_BOX
2354 && s->first_glyph->left_box_line_p
2355 && s->slice.x == 0)
2356 x += eabs (s->face->box_line_width);
2358 /* If there is a margin around the image, adjust x- and y-position
2359 by that margin. */
2360 if (s->slice.x == 0)
2361 x += s->img->hmargin;
2362 if (s->slice.y == 0)
2363 y += s->img->vmargin;
2365 if (s->img->pixmap)
2367 if (s->img->mask)
2369 /* We can't set both a clip mask and use XSetClipRectangles
2370 because the latter also sets a clip mask. We also can't
2371 trust on the shape extension to be available
2372 (XShapeCombineRegion). So, compute the rectangle to draw
2373 manually. */
2374 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2375 | GCFunction);
2376 XGCValues xgcv;
2378 xgcv.clip_mask = s->img->mask;
2379 xgcv.clip_x_origin = x - s->slice.x;
2380 xgcv.clip_y_origin = y - s->slice.y;
2381 xgcv.function = GXcopy;
2382 XChangeGC (s->display, s->gc, mask, &xgcv);
2384 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2385 s->slice.x, s->slice.y,
2386 s->slice.width, s->slice.height, x, y);
2387 XSetClipMask (s->display, s->gc, None);
2389 else
2391 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2392 s->slice.x, s->slice.y,
2393 s->slice.width, s->slice.height, x, y);
2395 /* When the image has a mask, we can expect that at
2396 least part of a mouse highlight or a block cursor will
2397 be visible. If the image doesn't have a mask, make
2398 a block cursor visible by drawing a rectangle around
2399 the image. I believe it's looking better if we do
2400 nothing here for mouse-face. */
2401 if (s->hl == DRAW_CURSOR)
2403 int r = s->img->relief;
2404 if (r < 0) r = -r;
2405 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2406 s->slice.width + r*2 - 1,
2407 s->slice.height + r*2 - 1);
2411 else
2412 /* Draw a rectangle if image could not be loaded. */
2413 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2414 s->slice.width - 1, s->slice.height - 1);
2418 /* Draw part of the background of glyph string S. X, Y, W, and H
2419 give the rectangle to draw. */
2421 static void
2422 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2423 struct glyph_string *s;
2424 int x, y, w, h;
2426 if (s->stippled_p)
2428 /* Fill background with a stipple pattern. */
2429 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2430 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2431 XSetFillStyle (s->display, s->gc, FillSolid);
2433 else
2434 x_clear_glyph_string_rect (s, x, y, w, h);
2438 /* Draw image glyph string S.
2440 s->y
2441 s->x +-------------------------
2442 | s->face->box
2444 | +-------------------------
2445 | | s->img->margin
2447 | | +-------------------
2448 | | | the image
2452 static void
2453 x_draw_image_glyph_string (s)
2454 struct glyph_string *s;
2456 int box_line_hwidth = eabs (s->face->box_line_width);
2457 int box_line_vwidth = max (s->face->box_line_width, 0);
2458 int height;
2459 Pixmap pixmap = None;
2461 height = s->height;
2462 if (s->slice.y == 0)
2463 height -= box_line_vwidth;
2464 if (s->slice.y + s->slice.height >= s->img->height)
2465 height -= box_line_vwidth;
2467 /* Fill background with face under the image. Do it only if row is
2468 taller than image or if image has a clip mask to reduce
2469 flickering. */
2470 s->stippled_p = s->face->stipple != 0;
2471 if (height > s->slice.height
2472 || s->img->hmargin
2473 || s->img->vmargin
2474 || s->img->mask
2475 || s->img->pixmap == 0
2476 || s->width != s->background_width)
2478 if (s->img->mask)
2480 /* Create a pixmap as large as the glyph string. Fill it
2481 with the background color. Copy the image to it, using
2482 its mask. Copy the temporary pixmap to the display. */
2483 Screen *screen = FRAME_X_SCREEN (s->f);
2484 int depth = DefaultDepthOfScreen (screen);
2486 /* Create a pixmap as large as the glyph string. */
2487 pixmap = XCreatePixmap (s->display, s->window,
2488 s->background_width,
2489 s->height, depth);
2491 /* Don't clip in the following because we're working on the
2492 pixmap. */
2493 XSetClipMask (s->display, s->gc, None);
2495 /* Fill the pixmap with the background color/stipple. */
2496 if (s->stippled_p)
2498 /* Fill background with a stipple pattern. */
2499 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2500 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2501 XFillRectangle (s->display, pixmap, s->gc,
2502 0, 0, s->background_width, s->height);
2503 XSetFillStyle (s->display, s->gc, FillSolid);
2504 XSetTSOrigin (s->display, s->gc, 0, 0);
2506 else
2508 XGCValues xgcv;
2509 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2510 &xgcv);
2511 XSetForeground (s->display, s->gc, xgcv.background);
2512 XFillRectangle (s->display, pixmap, s->gc,
2513 0, 0, s->background_width, s->height);
2514 XSetForeground (s->display, s->gc, xgcv.foreground);
2517 else
2519 int x = s->x;
2520 int y = s->y;
2522 if (s->first_glyph->left_box_line_p
2523 && s->slice.x == 0)
2524 x += box_line_hwidth;
2526 if (s->slice.y == 0)
2527 y += box_line_vwidth;
2529 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2532 s->background_filled_p = 1;
2535 /* Draw the foreground. */
2536 if (pixmap != None)
2538 x_draw_image_foreground_1 (s, pixmap);
2539 x_set_glyph_string_clipping (s);
2540 XCopyArea (s->display, pixmap, s->window, s->gc,
2541 0, 0, s->background_width, s->height, s->x, s->y);
2542 XFreePixmap (s->display, pixmap);
2544 else
2545 x_draw_image_foreground (s);
2547 /* If we must draw a relief around the image, do it. */
2548 if (s->img->relief
2549 || s->hl == DRAW_IMAGE_RAISED
2550 || s->hl == DRAW_IMAGE_SUNKEN)
2551 x_draw_image_relief (s);
2555 /* Draw stretch glyph string S. */
2557 static void
2558 x_draw_stretch_glyph_string (s)
2559 struct glyph_string *s;
2561 xassert (s->first_glyph->type == STRETCH_GLYPH);
2563 if (s->hl == DRAW_CURSOR
2564 && !x_stretch_cursor_p)
2566 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2567 as wide as the stretch glyph. */
2568 int width, background_width = s->background_width;
2569 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2571 if (x < left_x)
2573 background_width -= left_x - x;
2574 x = left_x;
2576 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2578 /* Draw cursor. */
2579 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2581 /* Clear rest using the GC of the original non-cursor face. */
2582 if (width < background_width)
2584 int y = s->y;
2585 int w = background_width - width, h = s->height;
2586 XRectangle r;
2587 GC gc;
2589 x += width;
2590 if (s->row->mouse_face_p
2591 && cursor_in_mouse_face_p (s->w))
2593 x_set_mouse_face_gc (s);
2594 gc = s->gc;
2596 else
2597 gc = s->face->gc;
2599 get_glyph_string_clip_rect (s, &r);
2600 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2602 if (s->face->stipple)
2604 /* Fill background with a stipple pattern. */
2605 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2606 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2607 XSetFillStyle (s->display, gc, FillSolid);
2609 else
2611 XGCValues xgcv;
2612 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2613 XSetForeground (s->display, gc, xgcv.background);
2614 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2615 XSetForeground (s->display, gc, xgcv.foreground);
2619 else if (!s->background_filled_p)
2621 int background_width = s->background_width;
2622 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2624 /* Don't draw into left margin, fringe or scrollbar area
2625 except for header line and mode line. */
2626 if (x < left_x && !s->row->mode_line_p)
2628 background_width -= left_x - x;
2629 x = left_x;
2631 if (background_width > 0)
2632 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2635 s->background_filled_p = 1;
2639 /* Draw glyph string S. */
2641 static void
2642 x_draw_glyph_string (s)
2643 struct glyph_string *s;
2645 int relief_drawn_p = 0;
2647 /* If S draws into the background of its successors, draw the
2648 background of the successors first so that S can draw into it.
2649 This makes S->next use XDrawString instead of XDrawImageString. */
2650 if (s->next && s->right_overhang && !s->for_overlaps)
2652 int width;
2653 struct glyph_string *next;
2655 for (width = 0, next = s->next;
2656 next && width < s->right_overhang;
2657 width += next->width, next = next->next)
2658 if (next->first_glyph->type != IMAGE_GLYPH)
2660 x_set_glyph_string_gc (next);
2661 x_set_glyph_string_clipping (next);
2662 if (next->first_glyph->type == STRETCH_GLYPH)
2663 x_draw_stretch_glyph_string (next);
2664 else
2665 x_draw_glyph_string_background (next, 1);
2666 next->num_clips = 0;
2670 /* Set up S->gc, set clipping and draw S. */
2671 x_set_glyph_string_gc (s);
2673 /* Draw relief (if any) in advance for char/composition so that the
2674 glyph string can be drawn over it. */
2675 if (!s->for_overlaps
2676 && s->face->box != FACE_NO_BOX
2677 && (s->first_glyph->type == CHAR_GLYPH
2678 || s->first_glyph->type == COMPOSITE_GLYPH))
2681 x_set_glyph_string_clipping (s);
2682 x_draw_glyph_string_background (s, 1);
2683 x_draw_glyph_string_box (s);
2684 x_set_glyph_string_clipping (s);
2685 relief_drawn_p = 1;
2687 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2688 && !s->clip_tail
2689 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2690 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2691 /* We must clip just this glyph. left_overhang part has already
2692 drawn when s->prev was drawn, and right_overhang part will be
2693 drawn later when s->next is drawn. */
2694 x_set_glyph_string_clipping_exactly (s, s);
2695 else
2696 x_set_glyph_string_clipping (s);
2698 switch (s->first_glyph->type)
2700 case IMAGE_GLYPH:
2701 x_draw_image_glyph_string (s);
2702 break;
2704 case STRETCH_GLYPH:
2705 x_draw_stretch_glyph_string (s);
2706 break;
2708 case CHAR_GLYPH:
2709 if (s->for_overlaps)
2710 s->background_filled_p = 1;
2711 else
2712 x_draw_glyph_string_background (s, 0);
2713 x_draw_glyph_string_foreground (s);
2714 break;
2716 case COMPOSITE_GLYPH:
2717 if (s->for_overlaps || (s->cmp_from > 0
2718 && ! s->first_glyph->u.cmp.automatic))
2719 s->background_filled_p = 1;
2720 else
2721 x_draw_glyph_string_background (s, 1);
2722 x_draw_composite_glyph_string_foreground (s);
2723 break;
2725 default:
2726 abort ();
2729 if (!s->for_overlaps)
2731 /* Draw underline. */
2732 if (s->face->underline_p)
2734 unsigned long thickness, position;
2735 int y;
2737 if (s->prev && s->prev->face->underline_p)
2739 /* We use the same underline style as the previous one. */
2740 thickness = s->prev->underline_thickness;
2741 position = s->prev->underline_position;
2743 else
2745 /* Get the underline thickness. Default is 1 pixel. */
2746 if (s->font && s->font->underline_thickness > 0)
2747 thickness = s->font->underline_thickness;
2748 else
2749 thickness = 1;
2750 if (x_underline_at_descent_line)
2751 position = (s->height - thickness) - (s->ybase - s->y);
2752 else
2754 /* Get the underline position. This is the recommended
2755 vertical offset in pixels from the baseline to the top of
2756 the underline. This is a signed value according to the
2757 specs, and its default is
2759 ROUND ((maximum descent) / 2), with
2760 ROUND(x) = floor (x + 0.5) */
2762 if (x_use_underline_position_properties
2763 && s->font && s->font->underline_position >= 0)
2764 position = s->font->underline_position;
2765 else if (s->font)
2766 position = (s->font->descent + 1) / 2;
2767 else
2768 position = underline_minimum_offset;
2770 position = max (position, underline_minimum_offset);
2772 /* Check the sanity of thickness and position. We should
2773 avoid drawing underline out of the current line area. */
2774 if (s->y + s->height <= s->ybase + position)
2775 position = (s->height - 1) - (s->ybase - s->y);
2776 if (s->y + s->height < s->ybase + position + thickness)
2777 thickness = (s->y + s->height) - (s->ybase + position);
2778 s->underline_thickness = thickness;
2779 s->underline_position = position;
2780 y = s->ybase + position;
2781 if (s->face->underline_defaulted_p)
2782 XFillRectangle (s->display, s->window, s->gc,
2783 s->x, y, s->width, thickness);
2784 else
2786 XGCValues xgcv;
2787 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2788 XSetForeground (s->display, s->gc, s->face->underline_color);
2789 XFillRectangle (s->display, s->window, s->gc,
2790 s->x, y, s->width, thickness);
2791 XSetForeground (s->display, s->gc, xgcv.foreground);
2795 /* Draw overline. */
2796 if (s->face->overline_p)
2798 unsigned long dy = 0, h = 1;
2800 if (s->face->overline_color_defaulted_p)
2801 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2802 s->width, h);
2803 else
2805 XGCValues xgcv;
2806 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2807 XSetForeground (s->display, s->gc, s->face->overline_color);
2808 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2809 s->width, h);
2810 XSetForeground (s->display, s->gc, xgcv.foreground);
2814 /* Draw strike-through. */
2815 if (s->face->strike_through_p)
2817 unsigned long h = 1;
2818 unsigned long dy = (s->height - h) / 2;
2820 if (s->face->strike_through_color_defaulted_p)
2821 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2822 s->width, h);
2823 else
2825 XGCValues xgcv;
2826 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2827 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2828 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2829 s->width, h);
2830 XSetForeground (s->display, s->gc, xgcv.foreground);
2834 /* Draw relief if not yet drawn. */
2835 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2836 x_draw_glyph_string_box (s);
2838 if (s->prev)
2840 struct glyph_string *prev;
2842 for (prev = s->prev; prev; prev = prev->prev)
2843 if (prev->hl != s->hl
2844 && prev->x + prev->width + prev->right_overhang > s->x)
2846 /* As prev was drawn while clipped to its own area, we
2847 must draw the right_overhang part using s->hl now. */
2848 enum draw_glyphs_face save = prev->hl;
2850 prev->hl = s->hl;
2851 x_set_glyph_string_gc (prev);
2852 x_set_glyph_string_clipping_exactly (s, prev);
2853 if (prev->first_glyph->type == CHAR_GLYPH)
2854 x_draw_glyph_string_foreground (prev);
2855 else
2856 x_draw_composite_glyph_string_foreground (prev);
2857 XSetClipMask (prev->display, prev->gc, None);
2858 prev->hl = save;
2859 prev->num_clips = 0;
2863 if (s->next)
2865 struct glyph_string *next;
2867 for (next = s->next; next; next = next->next)
2868 if (next->hl != s->hl
2869 && next->x - next->left_overhang < s->x + s->width)
2871 /* As next will be drawn while clipped to its own area,
2872 we must draw the left_overhang part using s->hl now. */
2873 enum draw_glyphs_face save = next->hl;
2875 next->hl = s->hl;
2876 x_set_glyph_string_gc (next);
2877 x_set_glyph_string_clipping_exactly (s, next);
2878 if (next->first_glyph->type == CHAR_GLYPH)
2879 x_draw_glyph_string_foreground (next);
2880 else
2881 x_draw_composite_glyph_string_foreground (next);
2882 XSetClipMask (next->display, next->gc, None);
2883 next->hl = save;
2884 next->num_clips = 0;
2889 /* Reset clipping. */
2890 XSetClipMask (s->display, s->gc, None);
2891 s->num_clips = 0;
2894 /* Shift display to make room for inserted glyphs. */
2896 void
2897 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2898 struct frame *f;
2899 int x, y, width, height, shift_by;
2901 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2902 f->output_data.x->normal_gc,
2903 x, y, width, height,
2904 x + shift_by, y);
2907 /* Delete N glyphs at the nominal cursor position. Not implemented
2908 for X frames. */
2910 static void
2911 x_delete_glyphs (f, n)
2912 struct frame *f;
2913 register int n;
2915 abort ();
2919 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2920 If they are <= 0, this is probably an error. */
2922 void
2923 x_clear_area (dpy, window, x, y, width, height, exposures)
2924 Display *dpy;
2925 Window window;
2926 int x, y;
2927 int width, height;
2928 int exposures;
2930 xassert (width > 0 && height > 0);
2931 XClearArea (dpy, window, x, y, width, height, exposures);
2935 /* Clear an entire frame. */
2937 static void
2938 x_clear_frame (struct frame *f)
2940 /* Clearing the frame will erase any cursor, so mark them all as no
2941 longer visible. */
2942 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2943 output_cursor.hpos = output_cursor.vpos = 0;
2944 output_cursor.x = -1;
2946 /* We don't set the output cursor here because there will always
2947 follow an explicit cursor_to. */
2948 BLOCK_INPUT;
2949 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2951 /* We have to clear the scroll bars, too. If we have changed
2952 colors or something like that, then they should be notified. */
2953 x_scroll_bar_clear (f);
2955 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2956 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2957 redisplay, do it here. */
2958 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2959 #endif
2961 XFlush (FRAME_X_DISPLAY (f));
2963 UNBLOCK_INPUT;
2968 /* Invert the middle quarter of the frame for .15 sec. */
2970 /* We use the select system call to do the waiting, so we have to make
2971 sure it's available. If it isn't, we just won't do visual bells. */
2973 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2976 /* Subtract the `struct timeval' values X and Y, storing the result in
2977 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2979 static int
2980 timeval_subtract (result, x, y)
2981 struct timeval *result, x, y;
2983 /* Perform the carry for the later subtraction by updating y. This
2984 is safer because on some systems the tv_sec member is unsigned. */
2985 if (x.tv_usec < y.tv_usec)
2987 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2988 y.tv_usec -= 1000000 * nsec;
2989 y.tv_sec += nsec;
2992 if (x.tv_usec - y.tv_usec > 1000000)
2994 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2995 y.tv_usec += 1000000 * nsec;
2996 y.tv_sec -= nsec;
2999 /* Compute the time remaining to wait. tv_usec is certainly
3000 positive. */
3001 result->tv_sec = x.tv_sec - y.tv_sec;
3002 result->tv_usec = x.tv_usec - y.tv_usec;
3004 /* Return indication of whether the result should be considered
3005 negative. */
3006 return x.tv_sec < y.tv_sec;
3009 void
3010 XTflash (f)
3011 struct frame *f;
3013 BLOCK_INPUT;
3016 #ifdef USE_GTK
3017 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3018 when the scroll bars and the edit widget share the same X window. */
3019 GdkGCValues vals;
3020 GdkGC *gc;
3021 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3022 ^ FRAME_BACKGROUND_PIXEL (f));
3023 vals.function = GDK_XOR;
3024 gc = gdk_gc_new_with_values (FRAME_GTK_WIDGET (f)->window,
3025 &vals,
3026 GDK_GC_FUNCTION
3027 | GDK_GC_FOREGROUND);
3028 #define XFillRectangle(d, win, gc, x, y, w, h) \
3029 gdk_draw_rectangle (FRAME_GTK_WIDGET (f)->window, \
3030 gc, TRUE, x, y, w, h)
3031 #else
3032 GC gc;
3034 /* Create a GC that will use the GXxor function to flip foreground
3035 pixels into background pixels. */
3037 XGCValues values;
3039 values.function = GXxor;
3040 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3041 ^ FRAME_BACKGROUND_PIXEL (f));
3043 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3044 GCFunction | GCForeground, &values);
3046 #endif
3048 /* Get the height not including a menu bar widget. */
3049 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
3050 /* Height of each line to flash. */
3051 int flash_height = FRAME_LINE_HEIGHT (f);
3052 /* These will be the left and right margins of the rectangles. */
3053 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3054 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3056 int width;
3058 /* Don't flash the area between a scroll bar and the frame
3059 edge it is next to. */
3060 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3062 case vertical_scroll_bar_left:
3063 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3064 break;
3066 case vertical_scroll_bar_right:
3067 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3068 break;
3070 default:
3071 break;
3074 width = flash_right - flash_left;
3076 /* If window is tall, flash top and bottom line. */
3077 if (height > 3 * FRAME_LINE_HEIGHT (f))
3079 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3080 flash_left,
3081 (FRAME_INTERNAL_BORDER_WIDTH (f)
3082 + FRAME_TOP_MARGIN_HEIGHT (f)),
3083 width, flash_height);
3084 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3085 flash_left,
3086 (height - flash_height
3087 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3088 width, flash_height);
3091 else
3092 /* If it is short, flash it all. */
3093 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3094 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3095 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3097 x_flush (f);
3100 struct timeval wakeup;
3102 EMACS_GET_TIME (wakeup);
3104 /* Compute time to wait until, propagating carry from usecs. */
3105 wakeup.tv_usec += 150000;
3106 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3107 wakeup.tv_usec %= 1000000;
3109 /* Keep waiting until past the time wakeup or any input gets
3110 available. */
3111 while (! detect_input_pending ())
3113 struct timeval current;
3114 struct timeval timeout;
3116 EMACS_GET_TIME (current);
3118 /* Break if result would be negative. */
3119 if (timeval_subtract (&current, wakeup, current))
3120 break;
3122 /* How long `select' should wait. */
3123 timeout.tv_sec = 0;
3124 timeout.tv_usec = 10000;
3126 /* Try to wait that long--but we might wake up sooner. */
3127 select (0, NULL, NULL, NULL, &timeout);
3131 /* If window is tall, flash top and bottom line. */
3132 if (height > 3 * FRAME_LINE_HEIGHT (f))
3134 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3135 flash_left,
3136 (FRAME_INTERNAL_BORDER_WIDTH (f)
3137 + FRAME_TOP_MARGIN_HEIGHT (f)),
3138 width, flash_height);
3139 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3140 flash_left,
3141 (height - flash_height
3142 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3143 width, flash_height);
3145 else
3146 /* If it is short, flash it all. */
3147 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3148 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3149 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3151 #ifdef USE_GTK
3152 g_object_unref (G_OBJECT (gc));
3153 #undef XFillRectangle
3154 #else
3155 XFreeGC (FRAME_X_DISPLAY (f), gc);
3156 #endif
3157 x_flush (f);
3161 UNBLOCK_INPUT;
3164 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3167 static void
3168 XTtoggle_invisible_pointer (f, invisible)
3169 FRAME_PTR f;
3170 int invisible;
3172 BLOCK_INPUT;
3173 if (invisible)
3175 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor != 0)
3176 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3177 FRAME_X_DISPLAY_INFO (f)->invisible_cursor);
3179 else
3180 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3181 f->output_data.x->current_cursor);
3182 f->pointer_invisible = invisible;
3183 UNBLOCK_INPUT;
3187 /* Make audible bell. */
3189 void
3190 XTring_bell ()
3192 struct frame *f = SELECTED_FRAME ();
3194 if (FRAME_X_DISPLAY (f))
3196 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3197 if (visible_bell)
3198 XTflash (f);
3199 else
3200 #endif
3202 BLOCK_INPUT;
3203 XBell (FRAME_X_DISPLAY (f), 0);
3204 XFlush (FRAME_X_DISPLAY (f));
3205 UNBLOCK_INPUT;
3211 /* Specify how many text lines, from the top of the window,
3212 should be affected by insert-lines and delete-lines operations.
3213 This, and those operations, are used only within an update
3214 that is bounded by calls to x_update_begin and x_update_end. */
3216 static void
3217 XTset_terminal_window (n)
3218 register int n;
3220 /* This function intentionally left blank. */
3225 /***********************************************************************
3226 Line Dance
3227 ***********************************************************************/
3229 /* Perform an insert-lines or delete-lines operation, inserting N
3230 lines or deleting -N lines at vertical position VPOS. */
3232 static void
3233 x_ins_del_lines (f, vpos, n)
3234 struct frame *f;
3235 int vpos, n;
3237 abort ();
3241 /* Scroll part of the display as described by RUN. */
3243 static void
3244 x_scroll_run (w, run)
3245 struct window *w;
3246 struct run *run;
3248 struct frame *f = XFRAME (w->frame);
3249 int x, y, width, height, from_y, to_y, bottom_y;
3251 /* Get frame-relative bounding box of the text display area of W,
3252 without mode lines. Include in this box the left and right
3253 fringe of W. */
3254 window_box (w, -1, &x, &y, &width, &height);
3256 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3257 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3258 bottom_y = y + height;
3260 if (to_y < from_y)
3262 /* Scrolling up. Make sure we don't copy part of the mode
3263 line at the bottom. */
3264 if (from_y + run->height > bottom_y)
3265 height = bottom_y - from_y;
3266 else
3267 height = run->height;
3269 else
3271 /* Scolling down. Make sure we don't copy over the mode line.
3272 at the bottom. */
3273 if (to_y + run->height > bottom_y)
3274 height = bottom_y - to_y;
3275 else
3276 height = run->height;
3279 BLOCK_INPUT;
3281 /* Cursor off. Will be switched on again in x_update_window_end. */
3282 updated_window = w;
3283 x_clear_cursor (w);
3285 XCopyArea (FRAME_X_DISPLAY (f),
3286 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3287 f->output_data.x->normal_gc,
3288 x, from_y,
3289 width, height,
3290 x, to_y);
3292 UNBLOCK_INPUT;
3297 /***********************************************************************
3298 Exposure Events
3299 ***********************************************************************/
3302 static void
3303 frame_highlight (f)
3304 struct frame *f;
3306 /* We used to only do this if Vx_no_window_manager was non-nil, but
3307 the ICCCM (section 4.1.6) says that the window's border pixmap
3308 and border pixel are window attributes which are "private to the
3309 client", so we can always change it to whatever we want. */
3310 BLOCK_INPUT;
3311 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3312 f->output_data.x->border_pixel);
3313 UNBLOCK_INPUT;
3314 x_update_cursor (f, 1);
3315 x_set_frame_alpha (f);
3318 static void
3319 frame_unhighlight (f)
3320 struct frame *f;
3322 /* We used to only do this if Vx_no_window_manager was non-nil, but
3323 the ICCCM (section 4.1.6) says that the window's border pixmap
3324 and border pixel are window attributes which are "private to the
3325 client", so we can always change it to whatever we want. */
3326 BLOCK_INPUT;
3327 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3328 f->output_data.x->border_tile);
3329 UNBLOCK_INPUT;
3330 x_update_cursor (f, 1);
3331 x_set_frame_alpha (f);
3334 /* The focus has changed. Update the frames as necessary to reflect
3335 the new situation. Note that we can't change the selected frame
3336 here, because the Lisp code we are interrupting might become confused.
3337 Each event gets marked with the frame in which it occurred, so the
3338 Lisp code can tell when the switch took place by examining the events. */
3340 static void
3341 x_new_focus_frame (dpyinfo, frame)
3342 struct x_display_info *dpyinfo;
3343 struct frame *frame;
3345 struct frame *old_focus = dpyinfo->x_focus_frame;
3347 if (frame != dpyinfo->x_focus_frame)
3349 /* Set this before calling other routines, so that they see
3350 the correct value of x_focus_frame. */
3351 dpyinfo->x_focus_frame = frame;
3353 if (old_focus && old_focus->auto_lower)
3354 x_lower_frame (old_focus);
3356 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3357 pending_autoraise_frame = dpyinfo->x_focus_frame;
3358 else
3359 pending_autoraise_frame = 0;
3362 x_frame_rehighlight (dpyinfo);
3365 /* Handle FocusIn and FocusOut state changes for FRAME.
3366 If FRAME has focus and there exists more than one frame, puts
3367 a FOCUS_IN_EVENT into *BUFP. */
3369 static void
3370 x_focus_changed (type, state, dpyinfo, frame, bufp)
3371 int type;
3372 int state;
3373 struct x_display_info *dpyinfo;
3374 struct frame *frame;
3375 struct input_event *bufp;
3377 if (type == FocusIn)
3379 if (dpyinfo->x_focus_event_frame != frame)
3381 x_new_focus_frame (dpyinfo, frame);
3382 dpyinfo->x_focus_event_frame = frame;
3384 /* Don't stop displaying the initial startup message
3385 for a switch-frame event we don't need. */
3386 if (NILP (Vterminal_frame)
3387 && CONSP (Vframe_list)
3388 && !NILP (XCDR (Vframe_list)))
3390 bufp->kind = FOCUS_IN_EVENT;
3391 XSETFRAME (bufp->frame_or_window, frame);
3395 frame->output_data.x->focus_state |= state;
3397 #ifdef HAVE_X_I18N
3398 if (FRAME_XIC (frame))
3399 XSetICFocus (FRAME_XIC (frame));
3400 #endif
3402 else if (type == FocusOut)
3404 frame->output_data.x->focus_state &= ~state;
3406 if (dpyinfo->x_focus_event_frame == frame)
3408 dpyinfo->x_focus_event_frame = 0;
3409 x_new_focus_frame (dpyinfo, 0);
3412 #ifdef HAVE_X_I18N
3413 if (FRAME_XIC (frame))
3414 XUnsetICFocus (FRAME_XIC (frame));
3415 #endif
3416 if (frame->pointer_invisible)
3417 XTtoggle_invisible_pointer (frame, 0);
3421 /* The focus may have changed. Figure out if it is a real focus change,
3422 by checking both FocusIn/Out and Enter/LeaveNotify events.
3424 Returns FOCUS_IN_EVENT event in *BUFP. */
3426 static void
3427 x_detect_focus_change (dpyinfo, event, bufp)
3428 struct x_display_info *dpyinfo;
3429 XEvent *event;
3430 struct input_event *bufp;
3432 struct frame *frame;
3434 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3435 if (! frame)
3436 return;
3438 switch (event->type)
3440 case EnterNotify:
3441 case LeaveNotify:
3443 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3444 int focus_state
3445 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3447 if (event->xcrossing.detail != NotifyInferior
3448 && event->xcrossing.focus
3449 && ! (focus_state & FOCUS_EXPLICIT))
3450 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3451 FOCUS_IMPLICIT,
3452 dpyinfo, frame, bufp);
3454 break;
3456 case FocusIn:
3457 case FocusOut:
3458 x_focus_changed (event->type,
3459 (event->xfocus.detail == NotifyPointer ?
3460 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3461 dpyinfo, frame, bufp);
3462 break;
3464 case ClientMessage:
3465 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3467 enum xembed_message msg = event->xclient.data.l[1];
3468 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3469 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3471 break;
3476 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3478 void
3479 x_mouse_leave (dpyinfo)
3480 struct x_display_info *dpyinfo;
3482 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3485 /* The focus has changed, or we have redirected a frame's focus to
3486 another frame (this happens when a frame uses a surrogate
3487 mini-buffer frame). Shift the highlight as appropriate.
3489 The FRAME argument doesn't necessarily have anything to do with which
3490 frame is being highlighted or un-highlighted; we only use it to find
3491 the appropriate X display info. */
3493 static void
3494 XTframe_rehighlight (frame)
3495 struct frame *frame;
3497 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3500 static void
3501 x_frame_rehighlight (dpyinfo)
3502 struct x_display_info *dpyinfo;
3504 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3506 if (dpyinfo->x_focus_frame)
3508 dpyinfo->x_highlight_frame
3509 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3510 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3511 : dpyinfo->x_focus_frame);
3512 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3514 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3515 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3518 else
3519 dpyinfo->x_highlight_frame = 0;
3521 if (dpyinfo->x_highlight_frame != old_highlight)
3523 if (old_highlight)
3524 frame_unhighlight (old_highlight);
3525 if (dpyinfo->x_highlight_frame)
3526 frame_highlight (dpyinfo->x_highlight_frame);
3532 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3534 /* Initialize mode_switch_bit and modifier_meaning. */
3535 static void
3536 x_find_modifier_meanings (dpyinfo)
3537 struct x_display_info *dpyinfo;
3539 int min_code, max_code;
3540 KeySym *syms;
3541 int syms_per_code;
3542 XModifierKeymap *mods;
3544 dpyinfo->meta_mod_mask = 0;
3545 dpyinfo->shift_lock_mask = 0;
3546 dpyinfo->alt_mod_mask = 0;
3547 dpyinfo->super_mod_mask = 0;
3548 dpyinfo->hyper_mod_mask = 0;
3550 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3552 syms = XGetKeyboardMapping (dpyinfo->display,
3553 min_code, max_code - min_code + 1,
3554 &syms_per_code);
3555 mods = XGetModifierMapping (dpyinfo->display);
3557 /* Scan the modifier table to see which modifier bits the Meta and
3558 Alt keysyms are on. */
3560 int row, col; /* The row and column in the modifier table. */
3561 int found_alt_or_meta;
3563 for (row = 3; row < 8; row++)
3565 found_alt_or_meta = 0;
3566 for (col = 0; col < mods->max_keypermod; col++)
3568 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3570 /* Zeroes are used for filler. Skip them. */
3571 if (code == 0)
3572 continue;
3574 /* Are any of this keycode's keysyms a meta key? */
3576 int code_col;
3578 for (code_col = 0; code_col < syms_per_code; code_col++)
3580 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3582 switch (sym)
3584 case XK_Meta_L:
3585 case XK_Meta_R:
3586 found_alt_or_meta = 1;
3587 dpyinfo->meta_mod_mask |= (1 << row);
3588 break;
3590 case XK_Alt_L:
3591 case XK_Alt_R:
3592 found_alt_or_meta = 1;
3593 dpyinfo->alt_mod_mask |= (1 << row);
3594 break;
3596 case XK_Hyper_L:
3597 case XK_Hyper_R:
3598 if (!found_alt_or_meta)
3599 dpyinfo->hyper_mod_mask |= (1 << row);
3600 code_col = syms_per_code;
3601 col = mods->max_keypermod;
3602 break;
3604 case XK_Super_L:
3605 case XK_Super_R:
3606 if (!found_alt_or_meta)
3607 dpyinfo->super_mod_mask |= (1 << row);
3608 code_col = syms_per_code;
3609 col = mods->max_keypermod;
3610 break;
3612 case XK_Shift_Lock:
3613 /* Ignore this if it's not on the lock modifier. */
3614 if (!found_alt_or_meta && ((1 << row) == LockMask))
3615 dpyinfo->shift_lock_mask = LockMask;
3616 code_col = syms_per_code;
3617 col = mods->max_keypermod;
3618 break;
3626 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3627 if (! dpyinfo->meta_mod_mask)
3629 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3630 dpyinfo->alt_mod_mask = 0;
3633 /* If some keys are both alt and meta,
3634 make them just meta, not alt. */
3635 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3637 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3640 XFree ((char *) syms);
3641 XFreeModifiermap (mods);
3644 /* Convert between the modifier bits X uses and the modifier bits
3645 Emacs uses. */
3647 unsigned int
3648 x_x_to_emacs_modifiers (dpyinfo, state)
3649 struct x_display_info *dpyinfo;
3650 unsigned int state;
3652 EMACS_UINT mod_meta = meta_modifier;
3653 EMACS_UINT mod_alt = alt_modifier;
3654 EMACS_UINT mod_hyper = hyper_modifier;
3655 EMACS_UINT mod_super = super_modifier;
3656 Lisp_Object tem;
3658 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3659 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3660 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3661 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3662 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3663 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3664 tem = Fget (Vx_super_keysym, Qmodifier_value);
3665 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3668 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3669 | ((state & ControlMask) ? ctrl_modifier : 0)
3670 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3671 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3672 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3673 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3676 static unsigned int
3677 x_emacs_to_x_modifiers (dpyinfo, state)
3678 struct x_display_info *dpyinfo;
3679 unsigned int state;
3681 EMACS_UINT mod_meta = meta_modifier;
3682 EMACS_UINT mod_alt = alt_modifier;
3683 EMACS_UINT mod_hyper = hyper_modifier;
3684 EMACS_UINT mod_super = super_modifier;
3686 Lisp_Object tem;
3688 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3689 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3690 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3691 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3692 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3693 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3694 tem = Fget (Vx_super_keysym, Qmodifier_value);
3695 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3698 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3699 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3700 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3701 | ((state & shift_modifier) ? ShiftMask : 0)
3702 | ((state & ctrl_modifier) ? ControlMask : 0)
3703 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3706 /* Convert a keysym to its name. */
3708 char *
3709 x_get_keysym_name (keysym)
3710 KeySym keysym;
3712 char *value;
3714 BLOCK_INPUT;
3715 value = XKeysymToString (keysym);
3716 UNBLOCK_INPUT;
3718 return value;
3723 /* Mouse clicks and mouse movement. Rah. */
3725 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3727 If the event is a button press, then note that we have grabbed
3728 the mouse. */
3730 static Lisp_Object
3731 construct_mouse_click (result, event, f)
3732 struct input_event *result;
3733 XButtonEvent *event;
3734 struct frame *f;
3736 /* Make the event type NO_EVENT; we'll change that when we decide
3737 otherwise. */
3738 result->kind = MOUSE_CLICK_EVENT;
3739 result->code = event->button - Button1;
3740 result->timestamp = event->time;
3741 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3742 event->state)
3743 | (event->type == ButtonRelease
3744 ? up_modifier
3745 : down_modifier));
3747 XSETINT (result->x, event->x);
3748 XSETINT (result->y, event->y);
3749 XSETFRAME (result->frame_or_window, f);
3750 result->arg = Qnil;
3751 return Qnil;
3755 /* Function to report a mouse movement to the mainstream Emacs code.
3756 The input handler calls this.
3758 We have received a mouse movement event, which is given in *event.
3759 If the mouse is over a different glyph than it was last time, tell
3760 the mainstream emacs code by setting mouse_moved. If not, ask for
3761 another motion event, so we can check again the next time it moves. */
3763 static XMotionEvent last_mouse_motion_event;
3764 static Lisp_Object last_mouse_motion_frame;
3766 static int
3767 note_mouse_movement (frame, event)
3768 FRAME_PTR frame;
3769 XMotionEvent *event;
3771 last_mouse_movement_time = event->time;
3772 last_mouse_motion_event = *event;
3773 XSETFRAME (last_mouse_motion_frame, frame);
3775 if (!FRAME_X_OUTPUT (frame))
3776 return 0;
3778 if (event->window != FRAME_X_WINDOW (frame))
3780 frame->mouse_moved = 1;
3781 last_mouse_scroll_bar = Qnil;
3782 note_mouse_highlight (frame, -1, -1);
3783 last_mouse_glyph_frame = 0;
3784 return 1;
3788 /* Has the mouse moved off the glyph it was on at the last sighting? */
3789 if (frame != last_mouse_glyph_frame
3790 || event->x < last_mouse_glyph.x
3791 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3792 || event->y < last_mouse_glyph.y
3793 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3795 frame->mouse_moved = 1;
3796 last_mouse_scroll_bar = Qnil;
3797 note_mouse_highlight (frame, event->x, event->y);
3798 /* Remember which glyph we're now on. */
3799 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3800 last_mouse_glyph_frame = frame;
3801 return 1;
3804 return 0;
3808 /************************************************************************
3809 Mouse Face
3810 ************************************************************************/
3812 static void
3813 redo_mouse_highlight ()
3815 if (!NILP (last_mouse_motion_frame)
3816 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3817 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3818 last_mouse_motion_event.x,
3819 last_mouse_motion_event.y);
3824 /* Return the current position of the mouse.
3825 *FP should be a frame which indicates which display to ask about.
3827 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3828 and *PART to the frame, window, and scroll bar part that the mouse
3829 is over. Set *X and *Y to the portion and whole of the mouse's
3830 position on the scroll bar.
3832 If the mouse movement started elsewhere, set *FP to the frame the
3833 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3834 the mouse is over.
3836 Set *TIME to the server time-stamp for the time at which the mouse
3837 was at this position.
3839 Don't store anything if we don't have a valid set of values to report.
3841 This clears the mouse_moved flag, so we can wait for the next mouse
3842 movement. */
3844 static void
3845 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3846 FRAME_PTR *fp;
3847 int insist;
3848 Lisp_Object *bar_window;
3849 enum scroll_bar_part *part;
3850 Lisp_Object *x, *y;
3851 unsigned long *time;
3853 FRAME_PTR f1;
3855 BLOCK_INPUT;
3857 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3858 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3859 else
3861 Window root;
3862 int root_x, root_y;
3864 Window dummy_window;
3865 int dummy;
3867 Lisp_Object frame, tail;
3869 /* Clear the mouse-moved flag for every frame on this display. */
3870 FOR_EACH_FRAME (tail, frame)
3871 if (FRAME_X_P (XFRAME (frame))
3872 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3873 XFRAME (frame)->mouse_moved = 0;
3875 last_mouse_scroll_bar = Qnil;
3877 /* Figure out which root window we're on. */
3878 XQueryPointer (FRAME_X_DISPLAY (*fp),
3879 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3881 /* The root window which contains the pointer. */
3882 &root,
3884 /* Trash which we can't trust if the pointer is on
3885 a different screen. */
3886 &dummy_window,
3888 /* The position on that root window. */
3889 &root_x, &root_y,
3891 /* More trash we can't trust. */
3892 &dummy, &dummy,
3894 /* Modifier keys and pointer buttons, about which
3895 we don't care. */
3896 (unsigned int *) &dummy);
3898 /* Now we have a position on the root; find the innermost window
3899 containing the pointer. */
3901 Window win, child;
3902 int win_x, win_y;
3903 int parent_x = 0, parent_y = 0;
3905 win = root;
3907 /* XTranslateCoordinates can get errors if the window
3908 structure is changing at the same time this function
3909 is running. So at least we must not crash from them. */
3911 x_catch_errors (FRAME_X_DISPLAY (*fp));
3913 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3914 && FRAME_LIVE_P (last_mouse_frame))
3916 /* If mouse was grabbed on a frame, give coords for that frame
3917 even if the mouse is now outside it. */
3918 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3920 /* From-window, to-window. */
3921 root, FRAME_X_WINDOW (last_mouse_frame),
3923 /* From-position, to-position. */
3924 root_x, root_y, &win_x, &win_y,
3926 /* Child of win. */
3927 &child);
3928 f1 = last_mouse_frame;
3930 else
3932 while (1)
3934 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3936 /* From-window, to-window. */
3937 root, win,
3939 /* From-position, to-position. */
3940 root_x, root_y, &win_x, &win_y,
3942 /* Child of win. */
3943 &child);
3945 if (child == None || child == win)
3946 break;
3947 #ifdef USE_GTK
3948 /* We don't wan't to know the innermost window. We
3949 want the edit window. For non-Gtk+ the innermost
3950 window is the edit window. For Gtk+ it might not
3951 be. It might be the tool bar for example. */
3952 if (x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win))
3953 break;
3954 #endif
3955 win = child;
3956 parent_x = win_x;
3957 parent_y = win_y;
3960 /* Now we know that:
3961 win is the innermost window containing the pointer
3962 (XTC says it has no child containing the pointer),
3963 win_x and win_y are the pointer's position in it
3964 (XTC did this the last time through), and
3965 parent_x and parent_y are the pointer's position in win's parent.
3966 (They are what win_x and win_y were when win was child.
3967 If win is the root window, it has no parent, and
3968 parent_{x,y} are invalid, but that's okay, because we'll
3969 never use them in that case.) */
3971 #ifdef USE_GTK
3972 /* We don't wan't to know the innermost window. We
3973 want the edit window. */
3974 f1 = x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3975 #else
3976 /* Is win one of our frames? */
3977 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3978 #endif
3980 #ifdef USE_X_TOOLKIT
3981 /* If we end up with the menu bar window, say it's not
3982 on the frame. */
3983 if (f1 != NULL
3984 && f1->output_data.x->menubar_widget
3985 && win == XtWindow (f1->output_data.x->menubar_widget))
3986 f1 = NULL;
3987 #endif /* USE_X_TOOLKIT */
3990 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3991 f1 = 0;
3993 x_uncatch_errors ();
3995 /* If not, is it one of our scroll bars? */
3996 if (! f1)
3998 struct scroll_bar *bar;
4000 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
4002 if (bar)
4004 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4005 win_x = parent_x;
4006 win_y = parent_y;
4010 if (f1 == 0 && insist > 0)
4011 f1 = SELECTED_FRAME ();
4013 if (f1)
4015 /* Ok, we found a frame. Store all the values.
4016 last_mouse_glyph is a rectangle used to reduce the
4017 generation of mouse events. To not miss any motion
4018 events, we must divide the frame into rectangles of the
4019 size of the smallest character that could be displayed
4020 on it, i.e. into the same rectangles that matrices on
4021 the frame are divided into. */
4023 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
4024 last_mouse_glyph_frame = f1;
4026 *bar_window = Qnil;
4027 *part = 0;
4028 *fp = f1;
4029 XSETINT (*x, win_x);
4030 XSETINT (*y, win_y);
4031 *time = last_mouse_movement_time;
4036 UNBLOCK_INPUT;
4041 /***********************************************************************
4042 Scroll bars
4043 ***********************************************************************/
4045 /* Scroll bar support. */
4047 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4048 manages it.
4049 This can be called in GC, so we have to make sure to strip off mark
4050 bits. */
4052 static struct scroll_bar *
4053 x_window_to_scroll_bar (display, window_id)
4054 Display *display;
4055 Window window_id;
4057 Lisp_Object tail;
4059 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4060 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4061 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4063 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4065 Lisp_Object frame, bar, condemned;
4067 frame = XCAR (tail);
4068 /* All elements of Vframe_list should be frames. */
4069 if (! FRAMEP (frame))
4070 abort ();
4072 if (! FRAME_X_P (XFRAME (frame)))
4073 continue;
4075 /* Scan this frame's scroll bar list for a scroll bar with the
4076 right window ID. */
4077 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4078 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4079 /* This trick allows us to search both the ordinary and
4080 condemned scroll bar lists with one loop. */
4081 ! NILP (bar) || (bar = condemned,
4082 condemned = Qnil,
4083 ! NILP (bar));
4084 bar = XSCROLL_BAR (bar)->next)
4085 if (XSCROLL_BAR (bar)->x_window == window_id &&
4086 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4087 return XSCROLL_BAR (bar);
4090 return 0;
4094 #if defined USE_LUCID
4096 /* Return the Lucid menu bar WINDOW is part of. Return null
4097 if WINDOW is not part of a menu bar. */
4099 static Widget
4100 x_window_to_menu_bar (window)
4101 Window window;
4103 Lisp_Object tail;
4105 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4107 if (FRAME_X_P (XFRAME (XCAR (tail))))
4109 Lisp_Object frame = XCAR (tail);
4110 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4112 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4113 return menu_bar;
4117 return NULL;
4120 #endif /* USE_LUCID */
4123 /************************************************************************
4124 Toolkit scroll bars
4125 ************************************************************************/
4127 #ifdef USE_TOOLKIT_SCROLL_BARS
4129 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4130 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4131 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4132 struct scroll_bar *));
4133 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4134 int, int, int));
4137 /* Lisp window being scrolled. Set when starting to interact with
4138 a toolkit scroll bar, reset to nil when ending the interaction. */
4140 static Lisp_Object window_being_scrolled;
4142 /* Last scroll bar part sent in xm_scroll_callback. */
4144 static int last_scroll_bar_part;
4146 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4147 that movements of 1/20 of the screen size are mapped to up/down. */
4149 #ifndef USE_GTK
4150 /* Id of action hook installed for scroll bars. */
4152 static XtActionHookId action_hook_id;
4154 static Boolean xaw3d_arrow_scroll;
4156 /* Whether the drag scrolling maintains the mouse at the top of the
4157 thumb. If not, resizing the thumb needs to be done more carefully
4158 to avoid jerkyness. */
4160 static Boolean xaw3d_pick_top;
4162 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4163 bars are used.. The hook is responsible for detecting when
4164 the user ends an interaction with the scroll bar, and generates
4165 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4167 static void
4168 xt_action_hook (widget, client_data, action_name, event, params,
4169 num_params)
4170 Widget widget;
4171 XtPointer client_data;
4172 String action_name;
4173 XEvent *event;
4174 String *params;
4175 Cardinal *num_params;
4177 int scroll_bar_p;
4178 char *end_action;
4180 #ifdef USE_MOTIF
4181 scroll_bar_p = XmIsScrollBar (widget);
4182 end_action = "Release";
4183 #else /* !USE_MOTIF i.e. use Xaw */
4184 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4185 end_action = "EndScroll";
4186 #endif /* USE_MOTIF */
4188 if (scroll_bar_p
4189 && strcmp (action_name, end_action) == 0
4190 && WINDOWP (window_being_scrolled))
4192 struct window *w;
4194 x_send_scroll_bar_event (window_being_scrolled,
4195 scroll_bar_end_scroll, 0, 0);
4196 w = XWINDOW (window_being_scrolled);
4198 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4200 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4201 /* The thumb size is incorrect while dragging: fix it. */
4202 set_vertical_scroll_bar (w);
4204 window_being_scrolled = Qnil;
4205 last_scroll_bar_part = -1;
4207 /* Xt timeouts no longer needed. */
4208 toolkit_scroll_bar_interaction = 0;
4211 #endif /* not USE_GTK */
4213 /* A vector of windows used for communication between
4214 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4216 static struct window **scroll_bar_windows;
4217 static int scroll_bar_windows_size;
4220 /* Send a client message with message type Xatom_Scrollbar for a
4221 scroll action to the frame of WINDOW. PART is a value identifying
4222 the part of the scroll bar that was clicked on. PORTION is the
4223 amount to scroll of a whole of WHOLE. */
4225 static void
4226 x_send_scroll_bar_event (window, part, portion, whole)
4227 Lisp_Object window;
4228 int part, portion, whole;
4230 XEvent event;
4231 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4232 struct window *w = XWINDOW (window);
4233 struct frame *f = XFRAME (w->frame);
4234 int i;
4236 BLOCK_INPUT;
4238 /* Construct a ClientMessage event to send to the frame. */
4239 ev->type = ClientMessage;
4240 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4241 ev->display = FRAME_X_DISPLAY (f);
4242 ev->window = FRAME_X_WINDOW (f);
4243 ev->format = 32;
4245 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4246 not enough to store a pointer or Lisp_Object on a 64 bit system.
4247 So, store the window in scroll_bar_windows and pass the index
4248 into that array in the event. */
4249 for (i = 0; i < scroll_bar_windows_size; ++i)
4250 if (scroll_bar_windows[i] == NULL)
4251 break;
4253 if (i == scroll_bar_windows_size)
4255 int new_size = max (10, 2 * scroll_bar_windows_size);
4256 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4257 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4259 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4260 nbytes);
4261 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4262 scroll_bar_windows_size = new_size;
4265 scroll_bar_windows[i] = w;
4266 ev->data.l[0] = (long) i;
4267 ev->data.l[1] = (long) part;
4268 ev->data.l[2] = (long) 0;
4269 ev->data.l[3] = (long) portion;
4270 ev->data.l[4] = (long) whole;
4272 /* Make Xt timeouts work while the scroll bar is active. */
4273 toolkit_scroll_bar_interaction = 1;
4274 #ifdef USE_X_TOOLKIT
4275 x_activate_timeout_atimer ();
4276 #endif
4278 /* Setting the event mask to zero means that the message will
4279 be sent to the client that created the window, and if that
4280 window no longer exists, no event will be sent. */
4281 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4282 UNBLOCK_INPUT;
4286 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4287 in *IEVENT. */
4289 static void
4290 x_scroll_bar_to_input_event (event, ievent)
4291 XEvent *event;
4292 struct input_event *ievent;
4294 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4295 Lisp_Object window;
4296 struct frame *f;
4297 struct window *w;
4299 w = scroll_bar_windows[ev->data.l[0]];
4300 scroll_bar_windows[ev->data.l[0]] = NULL;
4302 XSETWINDOW (window, w);
4303 f = XFRAME (w->frame);
4305 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4306 ievent->frame_or_window = window;
4307 ievent->arg = Qnil;
4308 #ifdef USE_GTK
4309 ievent->timestamp = CurrentTime;
4310 #else
4311 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4312 #endif
4313 ievent->part = ev->data.l[1];
4314 ievent->code = ev->data.l[2];
4315 ievent->x = make_number ((int) ev->data.l[3]);
4316 ievent->y = make_number ((int) ev->data.l[4]);
4317 ievent->modifiers = 0;
4321 #ifdef USE_MOTIF
4323 /* Minimum and maximum values used for Motif scroll bars. */
4325 #define XM_SB_MAX 10000000
4328 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4329 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4330 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4332 static void
4333 xm_scroll_callback (widget, client_data, call_data)
4334 Widget widget;
4335 XtPointer client_data, call_data;
4337 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4338 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4339 int part = -1, whole = 0, portion = 0;
4341 switch (cs->reason)
4343 case XmCR_DECREMENT:
4344 bar->dragging = Qnil;
4345 part = scroll_bar_up_arrow;
4346 break;
4348 case XmCR_INCREMENT:
4349 bar->dragging = Qnil;
4350 part = scroll_bar_down_arrow;
4351 break;
4353 case XmCR_PAGE_DECREMENT:
4354 bar->dragging = Qnil;
4355 part = scroll_bar_above_handle;
4356 break;
4358 case XmCR_PAGE_INCREMENT:
4359 bar->dragging = Qnil;
4360 part = scroll_bar_below_handle;
4361 break;
4363 case XmCR_TO_TOP:
4364 bar->dragging = Qnil;
4365 part = scroll_bar_to_top;
4366 break;
4368 case XmCR_TO_BOTTOM:
4369 bar->dragging = Qnil;
4370 part = scroll_bar_to_bottom;
4371 break;
4373 case XmCR_DRAG:
4375 int slider_size;
4377 /* Get the slider size. */
4378 BLOCK_INPUT;
4379 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4380 UNBLOCK_INPUT;
4382 whole = XM_SB_MAX - slider_size;
4383 portion = min (cs->value, whole);
4384 part = scroll_bar_handle;
4385 bar->dragging = make_number (cs->value);
4387 break;
4389 case XmCR_VALUE_CHANGED:
4390 break;
4393 if (part >= 0)
4395 window_being_scrolled = bar->window;
4396 last_scroll_bar_part = part;
4397 x_send_scroll_bar_event (bar->window, part, portion, whole);
4401 #elif defined USE_GTK
4403 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4404 bar widget. DATA is a pointer to the scroll_bar structure. */
4406 static gboolean
4407 xg_scroll_callback (GtkRange *range,
4408 GtkScrollType scroll,
4409 gdouble value,
4410 gpointer user_data)
4412 struct scroll_bar *bar = (struct scroll_bar *) user_data;
4413 gdouble position;
4414 int part = -1, whole = 0, portion = 0;
4415 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4416 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4418 if (xg_ignore_gtk_scrollbar) return FALSE;
4419 position = gtk_adjustment_get_value (adj);
4422 switch (scroll)
4424 case GTK_SCROLL_JUMP:
4425 /* Buttons 1 2 or 3 must be grabbed. */
4426 if (FRAME_X_DISPLAY_INFO (f)->grabbed != 0
4427 && FRAME_X_DISPLAY_INFO (f)->grabbed < (1 << 4))
4429 part = scroll_bar_handle;
4430 whole = adj->upper - adj->page_size;
4431 portion = min ((int)position, whole);
4432 bar->dragging = make_number ((int)portion);
4434 break;
4435 case GTK_SCROLL_STEP_BACKWARD:
4436 part = scroll_bar_up_arrow;
4437 bar->dragging = Qnil;
4438 break;
4439 case GTK_SCROLL_STEP_FORWARD:
4440 part = scroll_bar_down_arrow;
4441 bar->dragging = Qnil;
4442 break;
4443 case GTK_SCROLL_PAGE_BACKWARD:
4444 part = scroll_bar_above_handle;
4445 bar->dragging = Qnil;
4446 break;
4447 case GTK_SCROLL_PAGE_FORWARD:
4448 part = scroll_bar_below_handle;
4449 bar->dragging = Qnil;
4450 break;
4453 if (part >= 0)
4455 window_being_scrolled = bar->window;
4456 last_scroll_bar_part = part;
4457 x_send_scroll_bar_event (bar->window, part, portion, whole);
4460 return FALSE;
4463 /* Callback for button release. Sets dragging to Qnil when dragging is done. */
4465 static gboolean
4466 xg_end_scroll_callback (GtkWidget *widget,
4467 GdkEventButton *event,
4468 gpointer user_data)
4470 struct scroll_bar *bar = (struct scroll_bar *) user_data;
4471 bar->dragging = Qnil;
4472 if (WINDOWP (window_being_scrolled))
4474 x_send_scroll_bar_event (window_being_scrolled,
4475 scroll_bar_end_scroll, 0, 0);
4476 window_being_scrolled = Qnil;
4479 return FALSE;
4483 #else /* not USE_GTK and not USE_MOTIF */
4485 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4486 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4487 scroll bar struct. CALL_DATA is a pointer to a float saying where
4488 the thumb is. */
4490 static void
4491 xaw_jump_callback (widget, client_data, call_data)
4492 Widget widget;
4493 XtPointer client_data, call_data;
4495 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4496 float top = *(float *) call_data;
4497 float shown;
4498 int whole, portion, height;
4499 int part;
4501 /* Get the size of the thumb, a value between 0 and 1. */
4502 BLOCK_INPUT;
4503 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4504 UNBLOCK_INPUT;
4506 whole = 10000000;
4507 portion = shown < 1 ? top * whole : 0;
4509 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4510 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4511 the bottom, so we force the scrolling whenever we see that we're
4512 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4513 we try to ensure that we always stay two pixels away from the
4514 bottom). */
4515 part = scroll_bar_down_arrow;
4516 else
4517 part = scroll_bar_handle;
4519 window_being_scrolled = bar->window;
4520 bar->dragging = make_number (portion);
4521 last_scroll_bar_part = part;
4522 x_send_scroll_bar_event (bar->window, part, portion, whole);
4526 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4527 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4528 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4529 the scroll bar. CALL_DATA is an integer specifying the action that
4530 has taken place. Its magnitude is in the range 0..height of the
4531 scroll bar. Negative values mean scroll towards buffer start.
4532 Values < height of scroll bar mean line-wise movement. */
4534 static void
4535 xaw_scroll_callback (widget, client_data, call_data)
4536 Widget widget;
4537 XtPointer client_data, call_data;
4539 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4540 /* The position really is stored cast to a pointer. */
4541 int position = (long) call_data;
4542 Dimension height;
4543 int part;
4545 /* Get the height of the scroll bar. */
4546 BLOCK_INPUT;
4547 XtVaGetValues (widget, XtNheight, &height, NULL);
4548 UNBLOCK_INPUT;
4550 if (eabs (position) >= height)
4551 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4553 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4554 it maps line-movement to call_data = max(5, height/20). */
4555 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4556 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4557 else
4558 part = scroll_bar_move_ratio;
4560 window_being_scrolled = bar->window;
4561 bar->dragging = Qnil;
4562 last_scroll_bar_part = part;
4563 x_send_scroll_bar_event (bar->window, part, position, height);
4566 #endif /* not USE_GTK and not USE_MOTIF */
4568 #define SCROLL_BAR_NAME "verticalScrollBar"
4570 /* Create the widget for scroll bar BAR on frame F. Record the widget
4571 and X window of the scroll bar in BAR. */
4573 #ifdef USE_GTK
4574 static void
4575 x_create_toolkit_scroll_bar (f, bar)
4576 struct frame *f;
4577 struct scroll_bar *bar;
4579 char *scroll_bar_name = SCROLL_BAR_NAME;
4581 BLOCK_INPUT;
4582 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4583 G_CALLBACK (xg_end_scroll_callback),
4584 scroll_bar_name);
4585 UNBLOCK_INPUT;
4588 #else /* not USE_GTK */
4590 static void
4591 x_create_toolkit_scroll_bar (f, bar)
4592 struct frame *f;
4593 struct scroll_bar *bar;
4595 Window xwindow;
4596 Widget widget;
4597 Arg av[20];
4598 int ac = 0;
4599 char *scroll_bar_name = SCROLL_BAR_NAME;
4600 unsigned long pixel;
4602 BLOCK_INPUT;
4604 #ifdef USE_MOTIF
4605 /* Set resources. Create the widget. */
4606 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4607 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4608 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4609 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4610 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4611 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4612 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4614 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4615 if (pixel != -1)
4617 XtSetArg (av[ac], XmNforeground, pixel);
4618 ++ac;
4621 pixel = f->output_data.x->scroll_bar_background_pixel;
4622 if (pixel != -1)
4624 XtSetArg (av[ac], XmNbackground, pixel);
4625 ++ac;
4628 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4629 scroll_bar_name, av, ac);
4631 /* Add one callback for everything that can happen. */
4632 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4633 (XtPointer) bar);
4634 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4635 (XtPointer) bar);
4636 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4637 (XtPointer) bar);
4638 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4639 (XtPointer) bar);
4640 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4641 (XtPointer) bar);
4642 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4643 (XtPointer) bar);
4644 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4645 (XtPointer) bar);
4647 /* Realize the widget. Only after that is the X window created. */
4648 XtRealizeWidget (widget);
4650 /* Set the cursor to an arrow. I didn't find a resource to do that.
4651 And I'm wondering why it hasn't an arrow cursor by default. */
4652 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4653 f->output_data.x->nontext_cursor);
4655 #else /* !USE_MOTIF i.e. use Xaw */
4657 /* Set resources. Create the widget. The background of the
4658 Xaw3d scroll bar widget is a little bit light for my taste.
4659 We don't alter it here to let users change it according
4660 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4661 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4662 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4663 /* For smoother scrolling with Xaw3d -sm */
4664 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4666 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4667 if (pixel != -1)
4669 XtSetArg (av[ac], XtNforeground, pixel);
4670 ++ac;
4673 pixel = f->output_data.x->scroll_bar_background_pixel;
4674 if (pixel != -1)
4676 XtSetArg (av[ac], XtNbackground, pixel);
4677 ++ac;
4680 /* Top/bottom shadow colors. */
4682 /* Allocate them, if necessary. */
4683 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4685 pixel = f->output_data.x->scroll_bar_background_pixel;
4686 if (pixel != -1)
4688 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4689 FRAME_X_COLORMAP (f),
4690 &pixel, 1.2, 0x8000))
4691 pixel = -1;
4692 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4695 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4697 pixel = f->output_data.x->scroll_bar_background_pixel;
4698 if (pixel != -1)
4700 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4701 FRAME_X_COLORMAP (f),
4702 &pixel, 0.6, 0x4000))
4703 pixel = -1;
4704 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4708 #ifdef XtNbeNiceToColormap
4709 /* Tell the toolkit about them. */
4710 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4711 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4712 /* We tried to allocate a color for the top/bottom shadow, and
4713 failed, so tell Xaw3d to use dithering instead. */
4715 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4716 ++ac;
4718 else
4719 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4720 be more consistent with other emacs 3d colors, and since Xaw3d is
4721 not good at dealing with allocation failure. */
4723 /* This tells Xaw3d to use real colors instead of dithering for
4724 the shadows. */
4725 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4726 ++ac;
4728 /* Specify the colors. */
4729 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4730 if (pixel != -1)
4732 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4733 ++ac;
4735 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4736 if (pixel != -1)
4738 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4739 ++ac;
4742 #endif
4744 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4745 f->output_data.x->edit_widget, av, ac);
4748 char *initial = "";
4749 char *val = initial;
4750 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4751 #ifdef XtNarrowScrollbars
4752 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4753 #endif
4754 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4755 if (xaw3d_arrow_scroll || val == initial)
4756 { /* ARROW_SCROLL */
4757 xaw3d_arrow_scroll = True;
4758 /* Isn't that just a personal preference ? --Stef */
4759 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4763 /* Define callbacks. */
4764 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4765 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4766 (XtPointer) bar);
4768 /* Realize the widget. Only after that is the X window created. */
4769 XtRealizeWidget (widget);
4771 #endif /* !USE_MOTIF */
4773 /* Install an action hook that lets us detect when the user
4774 finishes interacting with a scroll bar. */
4775 if (action_hook_id == 0)
4776 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4778 /* Remember X window and widget in the scroll bar vector. */
4779 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4780 xwindow = XtWindow (widget);
4781 bar->x_window = xwindow;
4783 UNBLOCK_INPUT;
4785 #endif /* not USE_GTK */
4788 /* Set the thumb size and position of scroll bar BAR. We are currently
4789 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4791 #ifdef USE_GTK
4792 static void
4793 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4794 struct scroll_bar *bar;
4795 int portion, position, whole;
4797 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4800 #else /* not USE_GTK */
4801 static void
4802 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4803 struct scroll_bar *bar;
4804 int portion, position, whole;
4806 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4807 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4808 float top, shown;
4810 BLOCK_INPUT;
4812 #ifdef USE_MOTIF
4814 /* We use an estimate of 30 chars per line rather than the real
4815 `portion' value. This has the disadvantage that the thumb size
4816 is not very representative, but it makes our life a lot easier.
4817 Otherwise, we have to constantly adjust the thumb size, which
4818 we can't always do quickly enough: while dragging, the size of
4819 the thumb might prevent the user from dragging the thumb all the
4820 way to the end. but Motif and some versions of Xaw3d don't allow
4821 updating the thumb size while dragging. Also, even if we can update
4822 its size, the update will often happen too late.
4823 If you don't believe it, check out revision 1.650 of xterm.c to see
4824 what hoops we were going through and the still poor behavior we got. */
4825 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4826 /* When the thumb is at the bottom, position == whole.
4827 So we need to increase `whole' to make space for the thumb. */
4828 whole += portion;
4830 if (whole <= 0)
4831 top = 0, shown = 1;
4832 else
4834 top = (float) position / whole;
4835 shown = (float) portion / whole;
4838 if (NILP (bar->dragging))
4840 int size, value;
4842 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4843 is the scroll bar's maximum and MIN is the scroll bar's minimum
4844 value. */
4845 size = shown * XM_SB_MAX;
4846 size = min (size, XM_SB_MAX);
4847 size = max (size, 1);
4849 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4850 value = top * XM_SB_MAX;
4851 value = min (value, XM_SB_MAX - size);
4853 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4855 #else /* !USE_MOTIF i.e. use Xaw */
4857 if (whole == 0)
4858 top = 0, shown = 1;
4859 else
4861 top = (float) position / whole;
4862 shown = (float) portion / whole;
4866 float old_top, old_shown;
4867 Dimension height;
4868 XtVaGetValues (widget,
4869 XtNtopOfThumb, &old_top,
4870 XtNshown, &old_shown,
4871 XtNheight, &height,
4872 NULL);
4874 /* Massage the top+shown values. */
4875 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4876 top = max (0, min (1, top));
4877 else
4878 top = old_top;
4879 /* Keep two pixels available for moving the thumb down. */
4880 shown = max (0, min (1 - top - (2.0 / height), shown));
4882 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4883 check that your system's configuration file contains a define
4884 for `NARROWPROTO'. See s/freebsd.h for an example. */
4885 if (top != old_top || shown != old_shown)
4887 if (NILP (bar->dragging))
4888 XawScrollbarSetThumb (widget, top, shown);
4889 else
4891 /* Try to make the scrolling a tad smoother. */
4892 if (!xaw3d_pick_top)
4893 shown = min (shown, old_shown);
4895 XawScrollbarSetThumb (widget, top, shown);
4899 #endif /* !USE_MOTIF */
4901 UNBLOCK_INPUT;
4903 #endif /* not USE_GTK */
4905 #endif /* USE_TOOLKIT_SCROLL_BARS */
4909 /************************************************************************
4910 Scroll bars, general
4911 ************************************************************************/
4913 /* Create a scroll bar and return the scroll bar vector for it. W is
4914 the Emacs window on which to create the scroll bar. TOP, LEFT,
4915 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4916 scroll bar. */
4918 static struct scroll_bar *
4919 x_scroll_bar_create (w, top, left, width, height)
4920 struct window *w;
4921 int top, left, width, height;
4923 struct frame *f = XFRAME (w->frame);
4924 struct scroll_bar *bar
4925 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4927 BLOCK_INPUT;
4929 #ifdef USE_TOOLKIT_SCROLL_BARS
4930 x_create_toolkit_scroll_bar (f, bar);
4931 #else /* not USE_TOOLKIT_SCROLL_BARS */
4933 XSetWindowAttributes a;
4934 unsigned long mask;
4935 Window window;
4937 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4938 if (a.background_pixel == -1)
4939 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4941 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4942 | ButtonMotionMask | PointerMotionHintMask
4943 | ExposureMask);
4944 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4946 mask = (CWBackPixel | CWEventMask | CWCursor);
4948 /* Clear the area of W that will serve as a scroll bar. This is
4949 for the case that a window has been split horizontally. In
4950 this case, no clear_frame is generated to reduce flickering. */
4951 if (width > 0 && height > 0)
4952 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4953 left, top, width,
4954 window_box_height (w), False);
4956 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4957 /* Position and size of scroll bar. */
4958 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4959 top,
4960 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4961 height,
4962 /* Border width, depth, class, and visual. */
4964 CopyFromParent,
4965 CopyFromParent,
4966 CopyFromParent,
4967 /* Attributes. */
4968 mask, &a);
4969 bar->x_window = window;
4971 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4973 XSETWINDOW (bar->window, w);
4974 bar->top = top;
4975 bar->left = left;
4976 bar->width = width;
4977 bar->height = height;
4978 bar->start = 0;
4979 bar->end = 0;
4980 bar->dragging = Qnil;
4981 bar->fringe_extended_p = 0;
4983 /* Add bar to its frame's list of scroll bars. */
4984 bar->next = FRAME_SCROLL_BARS (f);
4985 bar->prev = Qnil;
4986 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4987 if (!NILP (bar->next))
4988 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4990 /* Map the window/widget. */
4991 #ifdef USE_TOOLKIT_SCROLL_BARS
4993 #ifdef USE_GTK
4994 xg_update_scrollbar_pos (f,
4995 bar->x_window,
4996 top,
4997 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4998 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4999 max (height, 1));
5000 xg_show_scroll_bar (bar->x_window);
5001 #else /* not USE_GTK */
5002 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5003 XtConfigureWidget (scroll_bar,
5004 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5005 top,
5006 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5007 max (height, 1), 0);
5008 XtMapWidget (scroll_bar);
5009 #endif /* not USE_GTK */
5011 #else /* not USE_TOOLKIT_SCROLL_BARS */
5012 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
5013 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5015 UNBLOCK_INPUT;
5016 return bar;
5020 #ifndef USE_TOOLKIT_SCROLL_BARS
5022 /* Draw BAR's handle in the proper position.
5024 If the handle is already drawn from START to END, don't bother
5025 redrawing it, unless REBUILD is non-zero; in that case, always
5026 redraw it. (REBUILD is handy for drawing the handle after expose
5027 events.)
5029 Normally, we want to constrain the start and end of the handle to
5030 fit inside its rectangle, but if the user is dragging the scroll
5031 bar handle, we want to let them drag it down all the way, so that
5032 the bar's top is as far down as it goes; otherwise, there's no way
5033 to move to the very end of the buffer. */
5035 static void
5036 x_scroll_bar_set_handle (bar, start, end, rebuild)
5037 struct scroll_bar *bar;
5038 int start, end;
5039 int rebuild;
5041 int dragging = ! NILP (bar->dragging);
5042 Window w = bar->x_window;
5043 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5044 GC gc = f->output_data.x->normal_gc;
5046 /* If the display is already accurate, do nothing. */
5047 if (! rebuild
5048 && start == bar->start
5049 && end == bar->end)
5050 return;
5052 BLOCK_INPUT;
5055 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5056 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5057 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5059 /* Make sure the values are reasonable, and try to preserve
5060 the distance between start and end. */
5062 int length = end - start;
5064 if (start < 0)
5065 start = 0;
5066 else if (start > top_range)
5067 start = top_range;
5068 end = start + length;
5070 if (end < start)
5071 end = start;
5072 else if (end > top_range && ! dragging)
5073 end = top_range;
5076 /* Store the adjusted setting in the scroll bar. */
5077 bar->start = start;
5078 bar->end = end;
5080 /* Clip the end position, just for display. */
5081 if (end > top_range)
5082 end = top_range;
5084 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5085 below top positions, to make sure the handle is always at least
5086 that many pixels tall. */
5087 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5089 /* Draw the empty space above the handle. Note that we can't clear
5090 zero-height areas; that means "clear to end of window." */
5091 if (0 < start)
5092 x_clear_area (FRAME_X_DISPLAY (f), w,
5093 /* x, y, width, height, and exposures. */
5094 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5095 VERTICAL_SCROLL_BAR_TOP_BORDER,
5096 inside_width, start,
5097 False);
5099 /* Change to proper foreground color if one is specified. */
5100 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5101 XSetForeground (FRAME_X_DISPLAY (f), gc,
5102 f->output_data.x->scroll_bar_foreground_pixel);
5104 /* Draw the handle itself. */
5105 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5106 /* x, y, width, height */
5107 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5108 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5109 inside_width, end - start);
5111 /* Restore the foreground color of the GC if we changed it above. */
5112 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5113 XSetForeground (FRAME_X_DISPLAY (f), gc,
5114 FRAME_FOREGROUND_PIXEL (f));
5116 /* Draw the empty space below the handle. Note that we can't
5117 clear zero-height areas; that means "clear to end of window." */
5118 if (end < inside_height)
5119 x_clear_area (FRAME_X_DISPLAY (f), w,
5120 /* x, y, width, height, and exposures. */
5121 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5122 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5123 inside_width, inside_height - end,
5124 False);
5128 UNBLOCK_INPUT;
5131 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5133 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5134 nil. */
5136 static void
5137 x_scroll_bar_remove (bar)
5138 struct scroll_bar *bar;
5140 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5141 BLOCK_INPUT;
5143 #ifdef USE_TOOLKIT_SCROLL_BARS
5144 #ifdef USE_GTK
5145 xg_remove_scroll_bar (f, bar->x_window);
5146 #else /* not USE_GTK */
5147 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5148 #endif /* not USE_GTK */
5149 #else
5150 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5151 #endif
5153 /* Disassociate this scroll bar from its window. */
5154 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5156 UNBLOCK_INPUT;
5160 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5161 that we are displaying PORTION characters out of a total of WHOLE
5162 characters, starting at POSITION. If WINDOW has no scroll bar,
5163 create one. */
5165 static void
5166 XTset_vertical_scroll_bar (w, portion, whole, position)
5167 struct window *w;
5168 int portion, whole, position;
5170 struct frame *f = XFRAME (w->frame);
5171 struct scroll_bar *bar;
5172 int top, height, left, sb_left, width, sb_width;
5173 int window_y, window_height;
5174 #ifdef USE_TOOLKIT_SCROLL_BARS
5175 int fringe_extended_p;
5176 #endif
5178 /* Get window dimensions. */
5179 window_box (w, -1, 0, &window_y, 0, &window_height);
5180 top = window_y;
5181 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5182 height = window_height;
5184 /* Compute the left edge of the scroll bar area. */
5185 left = WINDOW_SCROLL_BAR_AREA_X (w);
5187 /* Compute the width of the scroll bar which might be less than
5188 the width of the area reserved for the scroll bar. */
5189 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5190 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5191 else
5192 sb_width = width;
5194 /* Compute the left edge of the scroll bar. */
5195 #ifdef USE_TOOLKIT_SCROLL_BARS
5196 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5197 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5198 else
5199 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5200 #else
5201 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5202 sb_left = left + width - sb_width;
5203 else
5204 sb_left = left;
5205 #endif
5207 #ifdef USE_TOOLKIT_SCROLL_BARS
5208 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5209 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5210 && WINDOW_LEFT_FRINGE_WIDTH (w)
5211 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5212 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5213 else
5214 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5215 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5216 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5217 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5218 #endif
5220 /* Does the scroll bar exist yet? */
5221 if (NILP (w->vertical_scroll_bar))
5223 if (width > 0 && height > 0)
5225 BLOCK_INPUT;
5226 #ifdef USE_TOOLKIT_SCROLL_BARS
5227 if (fringe_extended_p)
5228 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5229 sb_left, top, sb_width, height, False);
5230 else
5231 #endif
5232 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5233 left, top, width, height, False);
5234 UNBLOCK_INPUT;
5237 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5239 else
5241 /* It may just need to be moved and resized. */
5242 unsigned int mask = 0;
5244 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5246 BLOCK_INPUT;
5248 if (sb_left != bar->left)
5249 mask |= CWX;
5250 if (top != bar->top)
5251 mask |= CWY;
5252 if (sb_width != bar->width)
5253 mask |= CWWidth;
5254 if (height != bar->height)
5255 mask |= CWHeight;
5257 #ifdef USE_TOOLKIT_SCROLL_BARS
5259 /* Move/size the scroll bar widget. */
5260 if (mask || bar->fringe_extended_p != fringe_extended_p)
5262 /* Since toolkit scroll bars are smaller than the space reserved
5263 for them on the frame, we have to clear "under" them. */
5264 if (width > 0 && height > 0)
5266 if (fringe_extended_p)
5267 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5268 sb_left, top, sb_width, height, False);
5269 else
5270 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5271 left, top, width, height, False);
5273 #ifdef USE_GTK
5274 xg_update_scrollbar_pos (f,
5275 bar->x_window,
5276 top,
5277 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5278 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5279 max (height, 1));
5280 #else /* not USE_GTK */
5281 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5282 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5283 top,
5284 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5285 max (height, 1), 0);
5286 #endif /* not USE_GTK */
5288 #else /* not USE_TOOLKIT_SCROLL_BARS */
5290 /* Clear areas not covered by the scroll bar because of
5291 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5292 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5294 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5295 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5296 height, False);
5297 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5298 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5299 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5300 height, False);
5303 /* Clear areas not covered by the scroll bar because it's not as
5304 wide as the area reserved for it. This makes sure a
5305 previous mode line display is cleared after C-x 2 C-x 1, for
5306 example. */
5308 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5309 int rest = area_width - sb_width;
5310 if (rest > 0 && height > 0)
5312 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5313 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5314 left + area_width - rest, top,
5315 rest, height, False);
5316 else
5317 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5318 left, top, rest, height, False);
5322 /* Move/size the scroll bar window. */
5323 if (mask)
5325 XWindowChanges wc;
5327 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5328 wc.y = top;
5329 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5330 wc.height = height;
5331 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5332 mask, &wc);
5335 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5337 /* Remember new settings. */
5338 bar->left = sb_left;
5339 bar->top = top;
5340 bar->width = sb_width;
5341 bar->height = height;
5343 UNBLOCK_INPUT;
5346 #ifdef USE_TOOLKIT_SCROLL_BARS
5347 bar->fringe_extended_p = fringe_extended_p;
5349 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5350 #else /* not USE_TOOLKIT_SCROLL_BARS */
5351 /* Set the scroll bar's current state, unless we're currently being
5352 dragged. */
5353 if (NILP (bar->dragging))
5355 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5357 if (whole == 0)
5358 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5359 else
5361 int start = ((double) position * top_range) / whole;
5362 int end = ((double) (position + portion) * top_range) / whole;
5363 x_scroll_bar_set_handle (bar, start, end, 0);
5366 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5368 XSETVECTOR (w->vertical_scroll_bar, bar);
5372 /* The following three hooks are used when we're doing a thorough
5373 redisplay of the frame. We don't explicitly know which scroll bars
5374 are going to be deleted, because keeping track of when windows go
5375 away is a real pain - "Can you say set-window-configuration, boys
5376 and girls?" Instead, we just assert at the beginning of redisplay
5377 that *all* scroll bars are to be removed, and then save a scroll bar
5378 from the fiery pit when we actually redisplay its window. */
5380 /* Arrange for all scroll bars on FRAME to be removed at the next call
5381 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5382 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5384 static void
5385 XTcondemn_scroll_bars (frame)
5386 FRAME_PTR frame;
5388 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5389 while (! NILP (FRAME_SCROLL_BARS (frame)))
5391 Lisp_Object bar;
5392 bar = FRAME_SCROLL_BARS (frame);
5393 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5394 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5395 XSCROLL_BAR (bar)->prev = Qnil;
5396 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5397 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5398 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5403 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5404 Note that WINDOW isn't necessarily condemned at all. */
5406 static void
5407 XTredeem_scroll_bar (window)
5408 struct window *window;
5410 struct scroll_bar *bar;
5411 struct frame *f;
5413 /* We can't redeem this window's scroll bar if it doesn't have one. */
5414 if (NILP (window->vertical_scroll_bar))
5415 abort ();
5417 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5419 /* Unlink it from the condemned list. */
5420 f = XFRAME (WINDOW_FRAME (window));
5421 if (NILP (bar->prev))
5423 /* If the prev pointer is nil, it must be the first in one of
5424 the lists. */
5425 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5426 /* It's not condemned. Everything's fine. */
5427 return;
5428 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5429 window->vertical_scroll_bar))
5430 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5431 else
5432 /* If its prev pointer is nil, it must be at the front of
5433 one or the other! */
5434 abort ();
5436 else
5437 XSCROLL_BAR (bar->prev)->next = bar->next;
5439 if (! NILP (bar->next))
5440 XSCROLL_BAR (bar->next)->prev = bar->prev;
5442 bar->next = FRAME_SCROLL_BARS (f);
5443 bar->prev = Qnil;
5444 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5445 if (! NILP (bar->next))
5446 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5449 /* Remove all scroll bars on FRAME that haven't been saved since the
5450 last call to `*condemn_scroll_bars_hook'. */
5452 static void
5453 XTjudge_scroll_bars (f)
5454 FRAME_PTR f;
5456 Lisp_Object bar, next;
5458 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5460 /* Clear out the condemned list now so we won't try to process any
5461 more events on the hapless scroll bars. */
5462 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5464 for (; ! NILP (bar); bar = next)
5466 struct scroll_bar *b = XSCROLL_BAR (bar);
5468 x_scroll_bar_remove (b);
5470 next = b->next;
5471 b->next = b->prev = Qnil;
5474 /* Now there should be no references to the condemned scroll bars,
5475 and they should get garbage-collected. */
5479 #ifndef USE_TOOLKIT_SCROLL_BARS
5480 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5481 is a no-op when using toolkit scroll bars.
5483 This may be called from a signal handler, so we have to ignore GC
5484 mark bits. */
5486 static void
5487 x_scroll_bar_expose (bar, event)
5488 struct scroll_bar *bar;
5489 XEvent *event;
5491 Window w = bar->x_window;
5492 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5493 GC gc = f->output_data.x->normal_gc;
5494 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5496 BLOCK_INPUT;
5498 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5500 /* Switch to scroll bar foreground color. */
5501 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5502 XSetForeground (FRAME_X_DISPLAY (f), gc,
5503 f->output_data.x->scroll_bar_foreground_pixel);
5505 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5506 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5508 /* x, y, width, height */
5509 0, 0,
5510 bar->width - 1 - width_trim - width_trim,
5511 bar->height - 1);
5513 /* Restore the foreground color of the GC if we changed it above. */
5514 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5515 XSetForeground (FRAME_X_DISPLAY (f), gc,
5516 FRAME_FOREGROUND_PIXEL (f));
5518 UNBLOCK_INPUT;
5521 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5523 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5524 is set to something other than NO_EVENT, it is enqueued.
5526 This may be called from a signal handler, so we have to ignore GC
5527 mark bits. */
5530 static void
5531 x_scroll_bar_handle_click (bar, event, emacs_event)
5532 struct scroll_bar *bar;
5533 XEvent *event;
5534 struct input_event *emacs_event;
5536 if (! WINDOWP (bar->window))
5537 abort ();
5539 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5540 emacs_event->code = event->xbutton.button - Button1;
5541 emacs_event->modifiers
5542 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5543 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5544 event->xbutton.state)
5545 | (event->type == ButtonRelease
5546 ? up_modifier
5547 : down_modifier));
5548 emacs_event->frame_or_window = bar->window;
5549 emacs_event->arg = Qnil;
5550 emacs_event->timestamp = event->xbutton.time;
5552 int top_range
5553 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5554 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5556 if (y < 0) y = 0;
5557 if (y > top_range) y = top_range;
5559 if (y < bar->start)
5560 emacs_event->part = scroll_bar_above_handle;
5561 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5562 emacs_event->part = scroll_bar_handle;
5563 else
5564 emacs_event->part = scroll_bar_below_handle;
5566 #ifndef USE_TOOLKIT_SCROLL_BARS
5567 /* If the user has released the handle, set it to its final position. */
5568 if (event->type == ButtonRelease
5569 && ! NILP (bar->dragging))
5571 int new_start = y - XINT (bar->dragging);
5572 int new_end = new_start + bar->end - bar->start;
5574 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5575 bar->dragging = Qnil;
5577 #endif
5579 XSETINT (emacs_event->x, y);
5580 XSETINT (emacs_event->y, top_range);
5584 #ifndef USE_TOOLKIT_SCROLL_BARS
5586 /* Handle some mouse motion while someone is dragging the scroll bar.
5588 This may be called from a signal handler, so we have to ignore GC
5589 mark bits. */
5591 static void
5592 x_scroll_bar_note_movement (bar, event)
5593 struct scroll_bar *bar;
5594 XEvent *event;
5596 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5598 last_mouse_movement_time = event->xmotion.time;
5600 f->mouse_moved = 1;
5601 XSETVECTOR (last_mouse_scroll_bar, bar);
5603 /* If we're dragging the bar, display it. */
5604 if (! NILP (bar->dragging))
5606 /* Where should the handle be now? */
5607 int new_start = event->xmotion.y - XINT (bar->dragging);
5609 if (new_start != bar->start)
5611 int new_end = new_start + bar->end - bar->start;
5613 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5618 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5620 /* Return information to the user about the current position of the mouse
5621 on the scroll bar. */
5623 static void
5624 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5625 FRAME_PTR *fp;
5626 Lisp_Object *bar_window;
5627 enum scroll_bar_part *part;
5628 Lisp_Object *x, *y;
5629 unsigned long *time;
5631 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5632 Window w = bar->x_window;
5633 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5634 int win_x, win_y;
5635 Window dummy_window;
5636 int dummy_coord;
5637 unsigned int dummy_mask;
5639 BLOCK_INPUT;
5641 /* Get the mouse's position relative to the scroll bar window, and
5642 report that. */
5643 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5645 /* Root, child, root x and root y. */
5646 &dummy_window, &dummy_window,
5647 &dummy_coord, &dummy_coord,
5649 /* Position relative to scroll bar. */
5650 &win_x, &win_y,
5652 /* Mouse buttons and modifier keys. */
5653 &dummy_mask))
5655 else
5657 int top_range
5658 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5660 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5662 if (! NILP (bar->dragging))
5663 win_y -= XINT (bar->dragging);
5665 if (win_y < 0)
5666 win_y = 0;
5667 if (win_y > top_range)
5668 win_y = top_range;
5670 *fp = f;
5671 *bar_window = bar->window;
5673 if (! NILP (bar->dragging))
5674 *part = scroll_bar_handle;
5675 else if (win_y < bar->start)
5676 *part = scroll_bar_above_handle;
5677 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5678 *part = scroll_bar_handle;
5679 else
5680 *part = scroll_bar_below_handle;
5682 XSETINT (*x, win_y);
5683 XSETINT (*y, top_range);
5685 f->mouse_moved = 0;
5686 last_mouse_scroll_bar = Qnil;
5689 *time = last_mouse_movement_time;
5691 UNBLOCK_INPUT;
5695 /* The screen has been cleared so we may have changed foreground or
5696 background colors, and the scroll bars may need to be redrawn.
5697 Clear out the scroll bars, and ask for expose events, so we can
5698 redraw them. */
5700 void
5701 x_scroll_bar_clear (f)
5702 FRAME_PTR f;
5704 #ifndef USE_TOOLKIT_SCROLL_BARS
5705 Lisp_Object bar;
5707 /* We can have scroll bars even if this is 0,
5708 if we just turned off scroll bar mode.
5709 But in that case we should not clear them. */
5710 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5711 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5712 bar = XSCROLL_BAR (bar)->next)
5713 XClearArea (FRAME_X_DISPLAY (f),
5714 XSCROLL_BAR (bar)->x_window,
5715 0, 0, 0, 0, True);
5716 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5720 /* The main X event-reading loop - XTread_socket. */
5722 /* This holds the state XLookupString needs to implement dead keys
5723 and other tricks known as "compose processing". _X Window System_
5724 says that a portable program can't use this, but Stephen Gildea assures
5725 me that letting the compiler initialize it to zeros will work okay.
5727 This must be defined outside of XTread_socket, for the same reasons
5728 given for enter_timestamp, above. */
5730 static XComposeStatus compose_status;
5732 /* Record the last 100 characters stored
5733 to help debug the loss-of-chars-during-GC problem. */
5735 static int temp_index;
5736 static short temp_buffer[100];
5738 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5739 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5740 temp_index = 0; \
5741 temp_buffer[temp_index++] = (keysym)
5743 /* Set this to nonzero to fake an "X I/O error"
5744 on a particular display. */
5746 struct x_display_info *XTread_socket_fake_io_error;
5748 /* When we find no input here, we occasionally do a no-op command
5749 to verify that the X server is still running and we can still talk with it.
5750 We try all the open displays, one by one.
5751 This variable is used for cycling thru the displays. */
5753 static struct x_display_info *next_noop_dpyinfo;
5755 #define SET_SAVED_MENU_EVENT(size) \
5756 do \
5758 if (f->output_data.x->saved_menu_event == 0) \
5759 f->output_data.x->saved_menu_event \
5760 = (XEvent *) xmalloc (sizeof (XEvent)); \
5761 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5762 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5763 XSETFRAME (inev.ie.frame_or_window, f); \
5765 while (0)
5767 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5768 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5771 enum
5773 X_EVENT_NORMAL,
5774 X_EVENT_GOTO_OUT,
5775 X_EVENT_DROP
5778 /* Filter events for the current X input method.
5779 DPYINFO is the display this event is for.
5780 EVENT is the X event to filter.
5782 Returns non-zero if the event was filtered, caller shall not process
5783 this event further.
5784 Returns zero if event is wasn't filtered. */
5786 #ifdef HAVE_X_I18N
5787 static int
5788 x_filter_event (dpyinfo, event)
5789 struct x_display_info *dpyinfo;
5790 XEvent *event;
5792 /* XFilterEvent returns non-zero if the input method has
5793 consumed the event. We pass the frame's X window to
5794 XFilterEvent because that's the one for which the IC
5795 was created. */
5797 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5798 event->xclient.window);
5800 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5802 #endif
5804 #ifdef USE_GTK
5805 static int current_count;
5806 static int current_finish;
5807 static struct input_event *current_hold_quit;
5809 /* This is the filter function invoked by the GTK event loop.
5810 It is invoked before the XEvent is translated to a GdkEvent,
5811 so we have a chance to act on the event before GTK. */
5812 static GdkFilterReturn
5813 event_handler_gdk (gxev, ev, data)
5814 GdkXEvent *gxev;
5815 GdkEvent *ev;
5816 gpointer data;
5818 XEvent *xev = (XEvent *) gxev;
5820 BLOCK_INPUT;
5821 if (current_count >= 0)
5823 struct x_display_info *dpyinfo;
5825 dpyinfo = x_display_info_for_display (xev->xany.display);
5827 #ifdef HAVE_X_I18N
5828 /* Filter events for the current X input method.
5829 GTK calls XFilterEvent but not for key press and release,
5830 so we do it here. */
5831 if ((xev->type == KeyPress || xev->type == KeyRelease)
5832 && dpyinfo
5833 && x_filter_event (dpyinfo, xev))
5835 UNBLOCK_INPUT;
5836 return GDK_FILTER_REMOVE;
5838 #endif
5840 if (! dpyinfo)
5841 current_finish = X_EVENT_NORMAL;
5842 else
5843 current_count +=
5844 handle_one_xevent (dpyinfo, xev, &current_finish,
5845 current_hold_quit);
5847 else
5848 current_finish = x_dispatch_event (xev, xev->xany.display);
5850 UNBLOCK_INPUT;
5852 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5853 return GDK_FILTER_REMOVE;
5855 return GDK_FILTER_CONTINUE;
5857 #endif /* USE_GTK */
5860 /* Handles the XEvent EVENT on display DPYINFO.
5862 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5863 *FINISH is zero if caller should continue reading events.
5864 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5866 We return the number of characters stored into the buffer. */
5868 static int
5869 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5870 struct x_display_info *dpyinfo;
5871 XEvent *eventp;
5872 int *finish;
5873 struct input_event *hold_quit;
5875 union {
5876 struct input_event ie;
5877 struct selection_input_event sie;
5878 } inev;
5879 int count = 0;
5880 int do_help = 0;
5881 int nbytes = 0;
5882 struct frame *f = NULL;
5883 struct coding_system coding;
5884 XEvent event = *eventp;
5886 *finish = X_EVENT_NORMAL;
5888 EVENT_INIT (inev.ie);
5889 inev.ie.kind = NO_EVENT;
5890 inev.ie.arg = Qnil;
5892 if (pending_event_wait.eventtype == event.type)
5893 pending_event_wait.eventtype = 0; /* Indicates we got it. */
5895 switch (event.type)
5897 case ClientMessage:
5899 if (event.xclient.message_type
5900 == dpyinfo->Xatom_wm_protocols
5901 && event.xclient.format == 32)
5903 if (event.xclient.data.l[0]
5904 == dpyinfo->Xatom_wm_take_focus)
5906 /* Use x_any_window_to_frame because this
5907 could be the shell widget window
5908 if the frame has no title bar. */
5909 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5910 #ifdef HAVE_X_I18N
5911 /* Not quite sure this is needed -pd */
5912 if (f && FRAME_XIC (f))
5913 XSetICFocus (FRAME_XIC (f));
5914 #endif
5915 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5916 instructs the WM to set the input focus automatically for
5917 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5918 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5919 it has set the focus. So, XSetInputFocus below is not
5920 needed.
5922 The call to XSetInputFocus below has also caused trouble. In
5923 cases where the XSetInputFocus done by the WM and the one
5924 below are temporally close (on a fast machine), the call
5925 below can generate additional FocusIn events which confuse
5926 Emacs. */
5928 /* Since we set WM_TAKE_FOCUS, we must call
5929 XSetInputFocus explicitly. But not if f is null,
5930 since that might be an event for a deleted frame. */
5931 if (f)
5933 Display *d = event.xclient.display;
5934 /* Catch and ignore errors, in case window has been
5935 iconified by a window manager such as GWM. */
5936 x_catch_errors (d);
5937 XSetInputFocus (d, event.xclient.window,
5938 /* The ICCCM says this is
5939 the only valid choice. */
5940 RevertToParent,
5941 event.xclient.data.l[1]);
5942 /* This is needed to detect the error
5943 if there is an error. */
5944 XSync (d, False);
5945 x_uncatch_errors ();
5947 /* Not certain about handling scroll bars here */
5948 #endif /* 0 */
5949 goto done;
5952 if (event.xclient.data.l[0]
5953 == dpyinfo->Xatom_wm_save_yourself)
5955 /* Save state modify the WM_COMMAND property to
5956 something which can reinstate us. This notifies
5957 the session manager, who's looking for such a
5958 PropertyNotify. Can restart processing when
5959 a keyboard or mouse event arrives. */
5960 /* If we have a session manager, don't set this.
5961 KDE will then start two Emacsen, one for the
5962 session manager and one for this. */
5963 #ifdef HAVE_X_SM
5964 if (! x_session_have_connection ())
5965 #endif
5967 f = x_top_window_to_frame (dpyinfo,
5968 event.xclient.window);
5969 /* This is just so we only give real data once
5970 for a single Emacs process. */
5971 if (f == SELECTED_FRAME ())
5972 XSetCommand (FRAME_X_DISPLAY (f),
5973 event.xclient.window,
5974 initial_argv, initial_argc);
5975 else if (f)
5976 XSetCommand (FRAME_X_DISPLAY (f),
5977 event.xclient.window,
5978 0, 0);
5980 goto done;
5983 if (event.xclient.data.l[0]
5984 == dpyinfo->Xatom_wm_delete_window)
5986 f = x_any_window_to_frame (dpyinfo,
5987 event.xclient.window);
5988 if (!f)
5989 goto OTHER; /* May be a dialog that is to be removed */
5991 inev.ie.kind = DELETE_WINDOW_EVENT;
5992 XSETFRAME (inev.ie.frame_or_window, f);
5993 goto done;
5996 goto done;
5999 if (event.xclient.message_type
6000 == dpyinfo->Xatom_wm_configure_denied)
6002 goto done;
6005 if (event.xclient.message_type
6006 == dpyinfo->Xatom_wm_window_moved)
6008 int new_x, new_y;
6009 f = x_window_to_frame (dpyinfo, event.xclient.window);
6011 new_x = event.xclient.data.s[0];
6012 new_y = event.xclient.data.s[1];
6014 if (f)
6016 f->left_pos = new_x;
6017 f->top_pos = new_y;
6019 goto done;
6022 #ifdef HACK_EDITRES
6023 if (event.xclient.message_type
6024 == dpyinfo->Xatom_editres)
6026 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6027 if (f)
6028 _XEditResCheckMessages (f->output_data.x->widget, NULL,
6029 &event, NULL);
6030 goto done;
6032 #endif /* HACK_EDITRES */
6034 if ((event.xclient.message_type
6035 == dpyinfo->Xatom_DONE)
6036 || (event.xclient.message_type
6037 == dpyinfo->Xatom_PAGE))
6039 /* Ghostview job completed. Kill it. We could
6040 reply with "Next" if we received "Page", but we
6041 currently never do because we are interested in
6042 images, only, which should have 1 page. */
6043 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
6044 f = x_window_to_frame (dpyinfo, event.xclient.window);
6045 if (!f)
6046 goto OTHER;
6047 x_kill_gs_process (pixmap, f);
6048 expose_frame (f, 0, 0, 0, 0);
6049 goto done;
6052 #ifdef USE_TOOLKIT_SCROLL_BARS
6053 /* Scroll bar callbacks send a ClientMessage from which
6054 we construct an input_event. */
6055 if (event.xclient.message_type
6056 == dpyinfo->Xatom_Scrollbar)
6058 x_scroll_bar_to_input_event (&event, &inev.ie);
6059 *finish = X_EVENT_GOTO_OUT;
6060 goto done;
6062 #endif /* USE_TOOLKIT_SCROLL_BARS */
6064 /* XEmbed messages from the embedder (if any). */
6065 if (event.xclient.message_type
6066 == dpyinfo->Xatom_XEMBED)
6068 enum xembed_message msg = event.xclient.data.l[1];
6069 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6070 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6072 *finish = X_EVENT_GOTO_OUT;
6073 goto done;
6076 xft_settings_event (dpyinfo, &event);
6078 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6079 if (!f)
6080 goto OTHER;
6081 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
6082 *finish = X_EVENT_DROP;
6084 break;
6086 case SelectionNotify:
6087 last_user_time = event.xselection.time;
6088 #ifdef USE_X_TOOLKIT
6089 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6090 goto OTHER;
6091 #endif /* not USE_X_TOOLKIT */
6092 x_handle_selection_notify (&event.xselection);
6093 break;
6095 case SelectionClear: /* Someone has grabbed ownership. */
6096 last_user_time = event.xselectionclear.time;
6097 #ifdef USE_X_TOOLKIT
6098 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6099 goto OTHER;
6100 #endif /* USE_X_TOOLKIT */
6102 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
6104 inev.ie.kind = SELECTION_CLEAR_EVENT;
6105 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6106 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6107 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6108 inev.ie.frame_or_window = Qnil;
6110 break;
6112 case SelectionRequest: /* Someone wants our selection. */
6113 last_user_time = event.xselectionrequest.time;
6114 #ifdef USE_X_TOOLKIT
6115 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6116 goto OTHER;
6117 #endif /* USE_X_TOOLKIT */
6119 XSelectionRequestEvent *eventp
6120 = (XSelectionRequestEvent *) &event;
6122 inev.ie.kind = SELECTION_REQUEST_EVENT;
6123 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6124 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6125 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6126 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6127 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6128 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6129 inev.ie.frame_or_window = Qnil;
6131 break;
6133 case PropertyNotify:
6134 last_user_time = event.xproperty.time;
6135 f = x_top_window_to_frame (dpyinfo, event.xproperty.window);
6136 if (f && event.xproperty.atom == dpyinfo->Xatom_net_wm_state)
6137 x_handle_net_wm_state (f, &event.xproperty);
6139 x_handle_property_notify (&event.xproperty);
6140 xft_settings_event (dpyinfo, &event);
6141 goto OTHER;
6143 case ReparentNotify:
6144 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6145 if (f)
6147 int x, y;
6148 f->output_data.x->parent_desc = event.xreparent.parent;
6149 x_real_positions (f, &x, &y);
6150 f->left_pos = x;
6151 f->top_pos = y;
6153 /* Perhaps reparented due to a WM restart. Reset this. */
6154 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6155 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6157 goto OTHER;
6159 case Expose:
6160 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6161 if (f)
6163 #ifdef USE_GTK
6164 /* This seems to be needed for GTK 2.6. */
6165 x_clear_area (event.xexpose.display,
6166 event.xexpose.window,
6167 event.xexpose.x, event.xexpose.y,
6168 event.xexpose.width, event.xexpose.height,
6169 FALSE);
6170 #endif
6171 if (f->async_visible == 0)
6173 f->async_visible = 1;
6174 f->async_iconified = 0;
6175 f->output_data.x->has_been_visible = 1;
6176 SET_FRAME_GARBAGED (f);
6178 else
6179 expose_frame (f,
6180 event.xexpose.x, event.xexpose.y,
6181 event.xexpose.width, event.xexpose.height);
6183 else
6185 #ifndef USE_TOOLKIT_SCROLL_BARS
6186 struct scroll_bar *bar;
6187 #endif
6188 #if defined USE_LUCID
6189 /* Submenus of the Lucid menu bar aren't widgets
6190 themselves, so there's no way to dispatch events
6191 to them. Recognize this case separately. */
6193 Widget widget
6194 = x_window_to_menu_bar (event.xexpose.window);
6195 if (widget)
6196 xlwmenu_redisplay (widget);
6198 #endif /* USE_LUCID */
6200 #ifdef USE_TOOLKIT_SCROLL_BARS
6201 /* Dispatch event to the widget. */
6202 goto OTHER;
6203 #else /* not USE_TOOLKIT_SCROLL_BARS */
6204 bar = x_window_to_scroll_bar (event.xexpose.display,
6205 event.xexpose.window);
6207 if (bar)
6208 x_scroll_bar_expose (bar, &event);
6209 #ifdef USE_X_TOOLKIT
6210 else
6211 goto OTHER;
6212 #endif /* USE_X_TOOLKIT */
6213 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6215 break;
6217 case GraphicsExpose: /* This occurs when an XCopyArea's
6218 source area was obscured or not
6219 available. */
6220 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6221 if (f)
6223 expose_frame (f,
6224 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6225 event.xgraphicsexpose.width,
6226 event.xgraphicsexpose.height);
6228 #ifdef USE_X_TOOLKIT
6229 else
6230 goto OTHER;
6231 #endif /* USE_X_TOOLKIT */
6232 break;
6234 case NoExpose: /* This occurs when an XCopyArea's
6235 source area was completely
6236 available. */
6237 break;
6239 case UnmapNotify:
6240 /* Redo the mouse-highlight after the tooltip has gone. */
6241 if (event.xmap.window == tip_window)
6243 tip_window = 0;
6244 redo_mouse_highlight ();
6247 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6248 if (f) /* F may no longer exist if
6249 the frame was deleted. */
6251 /* While a frame is unmapped, display generation is
6252 disabled; you don't want to spend time updating a
6253 display that won't ever be seen. */
6254 f->async_visible = 0;
6255 /* We can't distinguish, from the event, whether the window
6256 has become iconified or invisible. So assume, if it
6257 was previously visible, than now it is iconified.
6258 But x_make_frame_invisible clears both
6259 the visible flag and the iconified flag;
6260 and that way, we know the window is not iconified now. */
6261 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6263 f->async_iconified = 1;
6265 inev.ie.kind = ICONIFY_EVENT;
6266 XSETFRAME (inev.ie.frame_or_window, f);
6269 goto OTHER;
6271 case MapNotify:
6272 if (event.xmap.window == tip_window)
6273 /* The tooltip has been drawn already. Avoid
6274 the SET_FRAME_GARBAGED below. */
6275 goto OTHER;
6277 /* We use x_top_window_to_frame because map events can
6278 come for sub-windows and they don't mean that the
6279 frame is visible. */
6280 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6281 if (f)
6283 /* wait_reading_process_output will notice this and update
6284 the frame's display structures.
6285 If we where iconified, we should not set garbaged,
6286 because that stops redrawing on Expose events. This looks
6287 bad if we are called from a recursive event loop
6288 (x_dispatch_event), for example when a dialog is up. */
6289 if (! f->async_iconified)
6290 SET_FRAME_GARBAGED (f);
6292 /* Check if fullscreen was specified before we where mapped the
6293 first time, i.e. from the command line. */
6294 if (!f->output_data.x->has_been_visible)
6295 x_check_fullscreen (f);
6297 f->async_visible = 1;
6298 f->async_iconified = 0;
6299 f->output_data.x->has_been_visible = 1;
6301 if (f->iconified)
6303 inev.ie.kind = DEICONIFY_EVENT;
6304 XSETFRAME (inev.ie.frame_or_window, f);
6306 else if (! NILP (Vframe_list)
6307 && ! NILP (XCDR (Vframe_list)))
6308 /* Force a redisplay sooner or later
6309 to update the frame titles
6310 in case this is the second frame. */
6311 record_asynch_buffer_change ();
6313 #ifdef USE_GTK
6314 xg_frame_resized (f, -1, -1);
6315 #endif
6317 goto OTHER;
6319 case KeyPress:
6321 last_user_time = event.xkey.time;
6322 ignore_next_mouse_click_timeout = 0;
6324 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6325 /* Dispatch KeyPress events when in menu. */
6326 if (popup_activated ())
6327 goto OTHER;
6328 #endif
6330 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6332 /* If mouse-highlight is an integer, input clears out
6333 mouse highlighting. */
6334 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6335 && (f == 0
6336 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6338 clear_mouse_face (dpyinfo);
6339 dpyinfo->mouse_face_hidden = 1;
6342 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6343 if (f == 0)
6345 /* Scroll bars consume key events, but we want
6346 the keys to go to the scroll bar's frame. */
6347 Widget widget = XtWindowToWidget (dpyinfo->display,
6348 event.xkey.window);
6349 if (widget && XmIsScrollBar (widget))
6351 widget = XtParent (widget);
6352 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6355 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6357 if (f != 0)
6359 KeySym keysym, orig_keysym;
6360 /* al%imercury@uunet.uu.net says that making this 81
6361 instead of 80 fixed a bug whereby meta chars made
6362 his Emacs hang.
6364 It seems that some version of XmbLookupString has
6365 a bug of not returning XBufferOverflow in
6366 status_return even if the input is too long to
6367 fit in 81 bytes. So, we must prepare sufficient
6368 bytes for copy_buffer. 513 bytes (256 chars for
6369 two-byte character set) seems to be a fairly good
6370 approximation. -- 2000.8.10 handa@etl.go.jp */
6371 unsigned char copy_buffer[513];
6372 unsigned char *copy_bufptr = copy_buffer;
6373 int copy_bufsiz = sizeof (copy_buffer);
6374 int modifiers;
6375 Lisp_Object coding_system = Qlatin_1;
6376 Lisp_Object c;
6378 #ifdef USE_GTK
6379 /* Don't pass keys to GTK. A Tab will shift focus to the
6380 tool bar in GTK 2.4. Keys will still go to menus and
6381 dialogs because in that case popup_activated is TRUE
6382 (see above). */
6383 *finish = X_EVENT_DROP;
6384 #endif
6386 event.xkey.state
6387 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6388 extra_keyboard_modifiers);
6389 modifiers = event.xkey.state;
6391 /* This will have to go some day... */
6393 /* make_lispy_event turns chars into control chars.
6394 Don't do it here because XLookupString is too eager. */
6395 event.xkey.state &= ~ControlMask;
6396 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6397 | dpyinfo->super_mod_mask
6398 | dpyinfo->hyper_mod_mask
6399 | dpyinfo->alt_mod_mask);
6401 /* In case Meta is ComposeCharacter,
6402 clear its status. According to Markus Ehrnsperger
6403 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6404 this enables ComposeCharacter to work whether or
6405 not it is combined with Meta. */
6406 if (modifiers & dpyinfo->meta_mod_mask)
6407 bzero (&compose_status, sizeof (compose_status));
6409 #ifdef HAVE_X_I18N
6410 if (FRAME_XIC (f))
6412 Status status_return;
6414 coding_system = Vlocale_coding_system;
6415 nbytes = XmbLookupString (FRAME_XIC (f),
6416 &event.xkey, copy_bufptr,
6417 copy_bufsiz, &keysym,
6418 &status_return);
6419 if (status_return == XBufferOverflow)
6421 copy_bufsiz = nbytes + 1;
6422 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6423 nbytes = XmbLookupString (FRAME_XIC (f),
6424 &event.xkey, copy_bufptr,
6425 copy_bufsiz, &keysym,
6426 &status_return);
6428 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6429 if (status_return == XLookupNone)
6430 break;
6431 else if (status_return == XLookupChars)
6433 keysym = NoSymbol;
6434 modifiers = 0;
6436 else if (status_return != XLookupKeySym
6437 && status_return != XLookupBoth)
6438 abort ();
6440 else
6441 nbytes = XLookupString (&event.xkey, copy_bufptr,
6442 copy_bufsiz, &keysym,
6443 &compose_status);
6444 #else
6445 nbytes = XLookupString (&event.xkey, copy_bufptr,
6446 copy_bufsiz, &keysym,
6447 &compose_status);
6448 #endif
6450 /* If not using XIM/XIC, and a compose sequence is in progress,
6451 we break here. Otherwise, chars_matched is always 0. */
6452 if (compose_status.chars_matched > 0 && nbytes == 0)
6453 break;
6455 bzero (&compose_status, sizeof (compose_status));
6456 orig_keysym = keysym;
6458 /* Common for all keysym input events. */
6459 XSETFRAME (inev.ie.frame_or_window, f);
6460 inev.ie.modifiers
6461 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6462 inev.ie.timestamp = event.xkey.time;
6464 /* First deal with keysyms which have defined
6465 translations to characters. */
6466 if (keysym >= 32 && keysym < 128)
6467 /* Avoid explicitly decoding each ASCII character. */
6469 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6470 inev.ie.code = keysym;
6471 goto done_keysym;
6474 /* Keysyms directly mapped to Unicode characters. */
6475 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6477 if (keysym < 0x01000080)
6478 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6479 else
6480 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6481 inev.ie.code = keysym & 0xFFFFFF;
6482 goto done_keysym;
6485 /* Now non-ASCII. */
6486 if (HASH_TABLE_P (Vx_keysym_table)
6487 && (NATNUMP (c = Fgethash (make_number (keysym),
6488 Vx_keysym_table,
6489 Qnil))))
6491 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6492 ? ASCII_KEYSTROKE_EVENT
6493 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6494 inev.ie.code = XFASTINT (c);
6495 goto done_keysym;
6498 /* Random non-modifier sorts of keysyms. */
6499 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6500 || keysym == XK_Delete
6501 #ifdef XK_ISO_Left_Tab
6502 || (keysym >= XK_ISO_Left_Tab
6503 && keysym <= XK_ISO_Enter)
6504 #endif
6505 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6506 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6507 #ifdef HPUX
6508 /* This recognizes the "extended function
6509 keys". It seems there's no cleaner way.
6510 Test IsModifierKey to avoid handling
6511 mode_switch incorrectly. */
6512 || ((unsigned) (keysym) >= XK_Select
6513 && (unsigned)(keysym) < XK_KP_Space)
6514 #endif
6515 #ifdef XK_dead_circumflex
6516 || orig_keysym == XK_dead_circumflex
6517 #endif
6518 #ifdef XK_dead_grave
6519 || orig_keysym == XK_dead_grave
6520 #endif
6521 #ifdef XK_dead_tilde
6522 || orig_keysym == XK_dead_tilde
6523 #endif
6524 #ifdef XK_dead_diaeresis
6525 || orig_keysym == XK_dead_diaeresis
6526 #endif
6527 #ifdef XK_dead_macron
6528 || orig_keysym == XK_dead_macron
6529 #endif
6530 #ifdef XK_dead_degree
6531 || orig_keysym == XK_dead_degree
6532 #endif
6533 #ifdef XK_dead_acute
6534 || orig_keysym == XK_dead_acute
6535 #endif
6536 #ifdef XK_dead_cedilla
6537 || orig_keysym == XK_dead_cedilla
6538 #endif
6539 #ifdef XK_dead_breve
6540 || orig_keysym == XK_dead_breve
6541 #endif
6542 #ifdef XK_dead_ogonek
6543 || orig_keysym == XK_dead_ogonek
6544 #endif
6545 #ifdef XK_dead_caron
6546 || orig_keysym == XK_dead_caron
6547 #endif
6548 #ifdef XK_dead_doubleacute
6549 || orig_keysym == XK_dead_doubleacute
6550 #endif
6551 #ifdef XK_dead_abovedot
6552 || orig_keysym == XK_dead_abovedot
6553 #endif
6554 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6555 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6556 /* Any "vendor-specific" key is ok. */
6557 || (orig_keysym & (1 << 28))
6558 || (keysym != NoSymbol && nbytes == 0))
6559 && ! (IsModifierKey (orig_keysym)
6560 /* The symbols from XK_ISO_Lock
6561 to XK_ISO_Last_Group_Lock
6562 don't have real modifiers but
6563 should be treated similarly to
6564 Mode_switch by Emacs. */
6565 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6566 || ((unsigned)(orig_keysym)
6567 >= XK_ISO_Lock
6568 && (unsigned)(orig_keysym)
6569 <= XK_ISO_Last_Group_Lock)
6570 #endif
6573 STORE_KEYSYM_FOR_DEBUG (keysym);
6574 /* make_lispy_event will convert this to a symbolic
6575 key. */
6576 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6577 inev.ie.code = keysym;
6578 goto done_keysym;
6581 { /* Raw bytes, not keysym. */
6582 register int i;
6583 register int c;
6584 int nchars, len;
6586 for (i = 0, nchars = 0; i < nbytes; i++)
6588 if (ASCII_BYTE_P (copy_bufptr[i]))
6589 nchars++;
6590 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6593 if (nchars < nbytes)
6595 /* Decode the input data. */
6596 int require;
6597 unsigned char *p;
6599 /* The input should be decoded with `coding_system'
6600 which depends on which X*LookupString function
6601 we used just above and the locale. */
6602 setup_coding_system (coding_system, &coding);
6603 coding.src_multibyte = 0;
6604 coding.dst_multibyte = 1;
6605 /* The input is converted to events, thus we can't
6606 handle composition. Anyway, there's no XIM that
6607 gives us composition information. */
6608 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6610 require = MAX_MULTIBYTE_LENGTH * nbytes;
6611 coding.destination = alloca (require);
6612 coding.dst_bytes = require;
6613 coding.mode |= CODING_MODE_LAST_BLOCK;
6614 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6615 nbytes = coding.produced;
6616 nchars = coding.produced_char;
6617 copy_bufptr = coding.destination;
6620 /* Convert the input data to a sequence of
6621 character events. */
6622 for (i = 0; i < nbytes; i += len)
6624 if (nchars == nbytes)
6625 c = copy_bufptr[i], len = 1;
6626 else
6627 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
6628 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6629 ? ASCII_KEYSTROKE_EVENT
6630 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6631 inev.ie.code = c;
6632 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6635 count += nchars;
6637 inev.ie.kind = NO_EVENT; /* Already stored above. */
6639 if (keysym == NoSymbol)
6640 break;
6643 done_keysym:
6644 #ifdef HAVE_X_I18N
6645 /* Don't dispatch this event since XtDispatchEvent calls
6646 XFilterEvent, and two calls in a row may freeze the
6647 client. */
6648 break;
6649 #else
6650 goto OTHER;
6651 #endif
6653 case KeyRelease:
6654 last_user_time = event.xkey.time;
6655 #ifdef HAVE_X_I18N
6656 /* Don't dispatch this event since XtDispatchEvent calls
6657 XFilterEvent, and two calls in a row may freeze the
6658 client. */
6659 break;
6660 #else
6661 goto OTHER;
6662 #endif
6664 case EnterNotify:
6665 last_user_time = event.xcrossing.time;
6666 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6668 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6670 if (f && x_mouse_click_focus_ignore_position)
6671 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6673 /* EnterNotify counts as mouse movement,
6674 so update things that depend on mouse position. */
6675 if (f && !f->output_data.x->hourglass_p)
6676 note_mouse_movement (f, &event.xmotion);
6677 #ifdef USE_GTK
6678 /* We may get an EnterNotify on the buttons in the toolbar. In that
6679 case we moved out of any highlighted area and need to note this. */
6680 if (!f && last_mouse_glyph_frame)
6681 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6682 #endif
6683 goto OTHER;
6685 case FocusIn:
6686 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6687 goto OTHER;
6689 case LeaveNotify:
6690 last_user_time = event.xcrossing.time;
6691 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6693 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6694 if (f)
6696 if (f == dpyinfo->mouse_face_mouse_frame)
6698 /* If we move outside the frame, then we're
6699 certainly no longer on any text in the frame. */
6700 clear_mouse_face (dpyinfo);
6701 dpyinfo->mouse_face_mouse_frame = 0;
6704 /* Generate a nil HELP_EVENT to cancel a help-echo.
6705 Do it only if there's something to cancel.
6706 Otherwise, the startup message is cleared when
6707 the mouse leaves the frame. */
6708 if (any_help_event_p)
6709 do_help = -1;
6711 #ifdef USE_GTK
6712 /* See comment in EnterNotify above */
6713 else if (last_mouse_glyph_frame)
6714 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6715 #endif
6716 goto OTHER;
6718 case FocusOut:
6719 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6720 goto OTHER;
6722 case MotionNotify:
6724 last_user_time = event.xmotion.time;
6725 previous_help_echo_string = help_echo_string;
6726 help_echo_string = Qnil;
6728 if (dpyinfo->grabbed && last_mouse_frame
6729 && FRAME_LIVE_P (last_mouse_frame))
6730 f = last_mouse_frame;
6731 else
6732 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6734 if (dpyinfo->mouse_face_hidden)
6736 dpyinfo->mouse_face_hidden = 0;
6737 clear_mouse_face (dpyinfo);
6740 #ifdef USE_GTK
6741 if (f && xg_event_is_for_scrollbar (f, &event))
6742 f = 0;
6743 #endif
6744 if (f)
6747 /* Generate SELECT_WINDOW_EVENTs when needed.
6748 Don't let popup menus influence things (bug#1261). */
6749 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6751 Lisp_Object window;
6753 window = window_from_coordinates (f,
6754 event.xmotion.x, event.xmotion.y,
6755 0, 0, 0, 0);
6757 /* Window will be selected only when it is not selected now and
6758 last mouse movement event was not in it. Minibuffer window
6759 will be selected only when it is active. */
6760 if (WINDOWP (window)
6761 && !EQ (window, last_window)
6762 && !EQ (window, selected_window)
6763 /* For click-to-focus window managers
6764 create event iff we don't leave the
6765 selected frame. */
6766 && (focus_follows_mouse
6767 || (EQ (XWINDOW (window)->frame,
6768 XWINDOW (selected_window)->frame))))
6770 inev.ie.kind = SELECT_WINDOW_EVENT;
6771 inev.ie.frame_or_window = window;
6774 last_window=window;
6776 if (!note_mouse_movement (f, &event.xmotion))
6777 help_echo_string = previous_help_echo_string;
6779 else
6781 #ifndef USE_TOOLKIT_SCROLL_BARS
6782 struct scroll_bar *bar
6783 = x_window_to_scroll_bar (event.xmotion.display,
6784 event.xmotion.window);
6786 if (bar)
6787 x_scroll_bar_note_movement (bar, &event);
6788 #endif /* USE_TOOLKIT_SCROLL_BARS */
6790 /* If we move outside the frame, then we're
6791 certainly no longer on any text in the frame. */
6792 clear_mouse_face (dpyinfo);
6795 /* If the contents of the global variable help_echo_string
6796 has changed, generate a HELP_EVENT. */
6797 if (!NILP (help_echo_string)
6798 || !NILP (previous_help_echo_string))
6799 do_help = 1;
6800 goto OTHER;
6803 case ConfigureNotify:
6804 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6805 #ifdef USE_GTK
6806 if (!f
6807 && (f = x_any_window_to_frame (dpyinfo, event.xconfigure.window))
6808 && event.xconfigure.window == FRAME_X_WINDOW (f))
6810 xg_frame_resized (f, event.xconfigure.width,
6811 event.xconfigure.height);
6812 f = 0;
6814 #endif
6815 if (f)
6817 #ifndef USE_X_TOOLKIT
6818 #ifndef USE_GTK
6819 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6820 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6822 /* In the toolkit version, change_frame_size
6823 is called by the code that handles resizing
6824 of the EmacsFrame widget. */
6826 /* Even if the number of character rows and columns has
6827 not changed, the font size may have changed, so we need
6828 to check the pixel dimensions as well. */
6829 if (columns != FRAME_COLS (f)
6830 || rows != FRAME_LINES (f)
6831 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6832 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6834 change_frame_size (f, rows, columns, 0, 1, 0);
6835 SET_FRAME_GARBAGED (f);
6836 cancel_mouse_face (f);
6839 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6840 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6841 #endif /* not USE_GTK */
6842 #endif
6844 #ifdef USE_GTK
6845 /* GTK creates windows but doesn't map them.
6846 Only get real positions when mapped. */
6847 if (FRAME_GTK_OUTER_WIDGET (f)
6848 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6849 #endif
6851 x_real_positions (f, &f->left_pos, &f->top_pos);
6854 #ifdef HAVE_X_I18N
6855 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6856 xic_set_statusarea (f);
6857 #endif
6860 goto OTHER;
6862 case ButtonRelease:
6863 case ButtonPress:
6865 /* If we decide we want to generate an event to be seen
6866 by the rest of Emacs, we put it here. */
6867 int tool_bar_p = 0;
6869 bzero (&compose_status, sizeof (compose_status));
6870 last_mouse_glyph_frame = 0;
6871 last_user_time = event.xbutton.time;
6873 if (dpyinfo->grabbed
6874 && last_mouse_frame
6875 && FRAME_LIVE_P (last_mouse_frame))
6876 f = last_mouse_frame;
6877 else
6878 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6880 #ifdef USE_GTK
6881 if (f && xg_event_is_for_scrollbar (f, &event))
6882 f = 0;
6883 #endif
6884 if (f)
6886 /* Is this in the tool-bar? */
6887 if (WINDOWP (f->tool_bar_window)
6888 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6890 Lisp_Object window;
6891 int x = event.xbutton.x;
6892 int y = event.xbutton.y;
6894 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6895 tool_bar_p = EQ (window, f->tool_bar_window);
6897 if (tool_bar_p && event.xbutton.button < 4)
6899 handle_tool_bar_click (f, x, y,
6900 event.xbutton.type == ButtonPress,
6901 x_x_to_emacs_modifiers (dpyinfo,
6902 event.xbutton.state));
6906 if (!tool_bar_p)
6907 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6908 if (! popup_activated ())
6909 #endif
6911 if (ignore_next_mouse_click_timeout)
6913 if (event.type == ButtonPress
6914 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6916 ignore_next_mouse_click_timeout = 0;
6917 construct_mouse_click (&inev.ie, &event.xbutton, f);
6919 if (event.type == ButtonRelease)
6920 ignore_next_mouse_click_timeout = 0;
6922 else
6923 construct_mouse_click (&inev.ie, &event.xbutton, f);
6925 if (FRAME_X_EMBEDDED_P (f))
6926 xembed_send_message (f, event.xbutton.time,
6927 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6929 else
6931 struct scroll_bar *bar
6932 = x_window_to_scroll_bar (event.xbutton.display,
6933 event.xbutton.window);
6935 #ifdef USE_TOOLKIT_SCROLL_BARS
6936 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6937 scroll bars. */
6938 if (bar && event.xbutton.state & ControlMask)
6940 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6941 *finish = X_EVENT_DROP;
6943 #else /* not USE_TOOLKIT_SCROLL_BARS */
6944 if (bar)
6945 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6946 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6949 if (event.type == ButtonPress)
6951 dpyinfo->grabbed |= (1 << event.xbutton.button);
6952 last_mouse_frame = f;
6954 if (!tool_bar_p)
6955 last_tool_bar_item = -1;
6957 else
6958 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6960 /* Ignore any mouse motion that happened before this event;
6961 any subsequent mouse-movement Emacs events should reflect
6962 only motion after the ButtonPress/Release. */
6963 if (f != 0)
6964 f->mouse_moved = 0;
6966 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6967 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6968 /* For a down-event in the menu bar,
6969 don't pass it to Xt right now.
6970 Instead, save it away
6971 and we will pass it to Xt from kbd_buffer_get_event.
6972 That way, we can run some Lisp code first. */
6973 if (
6974 #ifdef USE_GTK
6975 ! popup_activated ()
6976 /* Gtk+ menus only react to the first three buttons. */
6977 && event.xbutton.button < 3
6979 #endif
6980 f && event.type == ButtonPress
6981 /* Verify the event is really within the menu bar
6982 and not just sent to it due to grabbing. */
6983 && event.xbutton.x >= 0
6984 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6985 && event.xbutton.y >= 0
6986 && event.xbutton.y < f->output_data.x->menubar_height
6987 && event.xbutton.same_screen)
6989 SET_SAVED_BUTTON_EVENT;
6990 XSETFRAME (last_mouse_press_frame, f);
6991 #ifdef USE_GTK
6992 *finish = X_EVENT_DROP;
6993 #endif
6995 else if (event.type == ButtonPress)
6997 last_mouse_press_frame = Qnil;
6998 goto OTHER;
7001 #ifdef USE_MOTIF /* This should do not harm for Lucid,
7002 but I am trying to be cautious. */
7003 else if (event.type == ButtonRelease)
7005 if (!NILP (last_mouse_press_frame))
7007 f = XFRAME (last_mouse_press_frame);
7008 if (f->output_data.x)
7009 SET_SAVED_BUTTON_EVENT;
7011 else
7012 goto OTHER;
7014 #endif /* USE_MOTIF */
7015 else
7016 goto OTHER;
7017 #endif /* USE_X_TOOLKIT || USE_GTK */
7019 break;
7021 case CirculateNotify:
7022 goto OTHER;
7024 case CirculateRequest:
7025 goto OTHER;
7027 case VisibilityNotify:
7028 goto OTHER;
7030 case MappingNotify:
7031 /* Someone has changed the keyboard mapping - update the
7032 local cache. */
7033 switch (event.xmapping.request)
7035 case MappingModifier:
7036 x_find_modifier_meanings (dpyinfo);
7037 /* This is meant to fall through. */
7038 case MappingKeyboard:
7039 XRefreshKeyboardMapping (&event.xmapping);
7041 goto OTHER;
7043 case DestroyNotify:
7044 xft_settings_event (dpyinfo, &event);
7045 break;
7047 default:
7048 OTHER:
7049 #ifdef USE_X_TOOLKIT
7050 BLOCK_INPUT;
7051 if (*finish != X_EVENT_DROP)
7052 XtDispatchEvent (&event);
7053 UNBLOCK_INPUT;
7054 #endif /* USE_X_TOOLKIT */
7055 break;
7058 done:
7059 if (inev.ie.kind != NO_EVENT)
7061 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7062 count++;
7065 if (do_help
7066 && !(hold_quit && hold_quit->kind != NO_EVENT))
7068 Lisp_Object frame;
7070 if (f)
7071 XSETFRAME (frame, f);
7072 else
7073 frame = Qnil;
7075 if (do_help > 0)
7077 any_help_event_p = 1;
7078 gen_help_event (help_echo_string, frame, help_echo_window,
7079 help_echo_object, help_echo_pos);
7081 else
7083 help_echo_string = Qnil;
7084 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7086 count++;
7089 *eventp = event;
7090 return count;
7094 /* Handles the XEvent EVENT on display DISPLAY.
7095 This is used for event loops outside the normal event handling,
7096 i.e. looping while a popup menu or a dialog is posted.
7098 Returns the value handle_one_xevent sets in the finish argument. */
7100 x_dispatch_event (event, display)
7101 XEvent *event;
7102 Display *display;
7104 struct x_display_info *dpyinfo;
7105 int finish = X_EVENT_NORMAL;
7107 dpyinfo = x_display_info_for_display (display);
7109 if (dpyinfo)
7110 handle_one_xevent (dpyinfo, event, &finish, 0);
7112 return finish;
7116 /* Read events coming from the X server.
7117 This routine is called by the SIGIO handler.
7118 We return as soon as there are no more events to be read.
7120 We return the number of characters stored into the buffer,
7121 thus pretending to be `read' (except the characters we store
7122 in the keyboard buffer can be multibyte, so are not necessarily
7123 C chars).
7125 EXPECTED is nonzero if the caller knows input is available. */
7127 static int
7128 XTread_socket (terminal, expected, hold_quit)
7129 struct terminal *terminal;
7130 int expected;
7131 struct input_event *hold_quit;
7133 int count = 0;
7134 XEvent event;
7135 int event_found = 0;
7137 if (interrupt_input_blocked)
7139 interrupt_input_pending = 1;
7140 #ifdef SYNC_INPUT
7141 pending_signals = 1;
7142 #endif
7143 return -1;
7146 interrupt_input_pending = 0;
7147 #ifdef SYNC_INPUT
7148 pending_signals = pending_atimers;
7149 #endif
7150 BLOCK_INPUT;
7152 /* So people can tell when we have read the available input. */
7153 input_signal_count++;
7155 ++handling_signal;
7157 #ifdef HAVE_X_SM
7158 /* Only check session manager input for the primary display. */
7159 if (terminal->id == 1 && x_session_have_connection ())
7161 struct input_event inev;
7162 BLOCK_INPUT;
7163 /* We don't need to EVENT_INIT (inev) here, as
7164 x_session_check_input copies an entire input_event. */
7165 if (x_session_check_input (&inev))
7167 kbd_buffer_store_event_hold (&inev, hold_quit);
7168 count++;
7170 UNBLOCK_INPUT;
7172 #endif
7174 /* For debugging, this gives a way to fake an I/O error. */
7175 if (terminal->display_info.x == XTread_socket_fake_io_error)
7177 XTread_socket_fake_io_error = 0;
7178 x_io_error_quitter (terminal->display_info.x->display);
7181 #ifndef USE_GTK
7182 while (XPending (terminal->display_info.x->display))
7184 int finish;
7186 XNextEvent (terminal->display_info.x->display, &event);
7188 #ifdef HAVE_X_I18N
7189 /* Filter events for the current X input method. */
7190 if (x_filter_event (terminal->display_info.x, &event))
7191 continue;
7192 #endif
7193 event_found = 1;
7195 count += handle_one_xevent (terminal->display_info.x,
7196 &event, &finish, hold_quit);
7198 if (finish == X_EVENT_GOTO_OUT)
7199 goto out;
7202 #else /* USE_GTK */
7204 /* For GTK we must use the GTK event loop. But XEvents gets passed
7205 to our filter function above, and then to the big event switch.
7206 We use a bunch of globals to communicate with our filter function,
7207 that is kind of ugly, but it works.
7209 There is no way to do one display at the time, GTK just does events
7210 from all displays. */
7212 while (gtk_events_pending ())
7214 current_count = count;
7215 current_hold_quit = hold_quit;
7217 gtk_main_iteration ();
7219 count = current_count;
7220 current_count = -1;
7221 current_hold_quit = 0;
7223 if (current_finish == X_EVENT_GOTO_OUT)
7224 break;
7226 #endif /* USE_GTK */
7228 out:;
7230 /* On some systems, an X bug causes Emacs to get no more events
7231 when the window is destroyed. Detect that. (1994.) */
7232 if (! event_found)
7234 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7235 One XNOOP in 100 loops will make Emacs terminate.
7236 B. Bretthauer, 1994 */
7237 x_noop_count++;
7238 if (x_noop_count >= 100)
7240 x_noop_count=0;
7242 if (next_noop_dpyinfo == 0)
7243 next_noop_dpyinfo = x_display_list;
7245 XNoOp (next_noop_dpyinfo->display);
7247 /* Each time we get here, cycle through the displays now open. */
7248 next_noop_dpyinfo = next_noop_dpyinfo->next;
7252 /* If the focus was just given to an auto-raising frame,
7253 raise it now. */
7254 /* ??? This ought to be able to handle more than one such frame. */
7255 if (pending_autoraise_frame)
7257 x_raise_frame (pending_autoraise_frame);
7258 pending_autoraise_frame = 0;
7261 --handling_signal;
7262 UNBLOCK_INPUT;
7264 return count;
7270 /***********************************************************************
7271 Text Cursor
7272 ***********************************************************************/
7274 /* Set clipping for output in glyph row ROW. W is the window in which
7275 we operate. GC is the graphics context to set clipping in.
7277 ROW may be a text row or, e.g., a mode line. Text rows must be
7278 clipped to the interior of the window dedicated to text display,
7279 mode lines must be clipped to the whole window. */
7281 static void
7282 x_clip_to_row (w, row, area, gc)
7283 struct window *w;
7284 struct glyph_row *row;
7285 int area;
7286 GC gc;
7288 struct frame *f = XFRAME (WINDOW_FRAME (w));
7289 XRectangle clip_rect;
7290 int window_x, window_y, window_width;
7292 window_box (w, area, &window_x, &window_y, &window_width, 0);
7294 clip_rect.x = window_x;
7295 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7296 clip_rect.y = max (clip_rect.y, window_y);
7297 clip_rect.width = window_width;
7298 clip_rect.height = row->visible_height;
7300 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7304 /* Draw a hollow box cursor on window W in glyph row ROW. */
7306 static void
7307 x_draw_hollow_cursor (w, row)
7308 struct window *w;
7309 struct glyph_row *row;
7311 struct frame *f = XFRAME (WINDOW_FRAME (w));
7312 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7313 Display *dpy = FRAME_X_DISPLAY (f);
7314 int x, y, wd, h;
7315 XGCValues xgcv;
7316 struct glyph *cursor_glyph;
7317 GC gc;
7319 /* Get the glyph the cursor is on. If we can't tell because
7320 the current matrix is invalid or such, give up. */
7321 cursor_glyph = get_phys_cursor_glyph (w);
7322 if (cursor_glyph == NULL)
7323 return;
7325 /* Compute frame-relative coordinates for phys cursor. */
7326 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7327 wd = w->phys_cursor_width;
7329 /* The foreground of cursor_gc is typically the same as the normal
7330 background color, which can cause the cursor box to be invisible. */
7331 xgcv.foreground = f->output_data.x->cursor_pixel;
7332 if (dpyinfo->scratch_cursor_gc)
7333 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7334 else
7335 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7336 GCForeground, &xgcv);
7337 gc = dpyinfo->scratch_cursor_gc;
7339 /* Set clipping, draw the rectangle, and reset clipping again. */
7340 x_clip_to_row (w, row, TEXT_AREA, gc);
7341 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7342 XSetClipMask (dpy, gc, None);
7346 /* Draw a bar cursor on window W in glyph row ROW.
7348 Implementation note: One would like to draw a bar cursor with an
7349 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7350 Unfortunately, I didn't find a font yet that has this property set.
7351 --gerd. */
7353 static void
7354 x_draw_bar_cursor (w, row, width, kind)
7355 struct window *w;
7356 struct glyph_row *row;
7357 int width;
7358 enum text_cursor_kinds kind;
7360 struct frame *f = XFRAME (w->frame);
7361 struct glyph *cursor_glyph;
7363 /* If cursor is out of bounds, don't draw garbage. This can happen
7364 in mini-buffer windows when switching between echo area glyphs
7365 and mini-buffer. */
7366 cursor_glyph = get_phys_cursor_glyph (w);
7367 if (cursor_glyph == NULL)
7368 return;
7370 /* If on an image, draw like a normal cursor. That's usually better
7371 visible than drawing a bar, esp. if the image is large so that
7372 the bar might not be in the window. */
7373 if (cursor_glyph->type == IMAGE_GLYPH)
7375 struct glyph_row *row;
7376 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7377 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7379 else
7381 Display *dpy = FRAME_X_DISPLAY (f);
7382 Window window = FRAME_X_WINDOW (f);
7383 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7384 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7385 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7386 XGCValues xgcv;
7388 /* If the glyph's background equals the color we normally draw
7389 the bars cursor in, the bar cursor in its normal color is
7390 invisible. Use the glyph's foreground color instead in this
7391 case, on the assumption that the glyph's colors are chosen so
7392 that the glyph is legible. */
7393 if (face->background == f->output_data.x->cursor_pixel)
7394 xgcv.background = xgcv.foreground = face->foreground;
7395 else
7396 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7397 xgcv.graphics_exposures = 0;
7399 if (gc)
7400 XChangeGC (dpy, gc, mask, &xgcv);
7401 else
7403 gc = XCreateGC (dpy, window, mask, &xgcv);
7404 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7407 x_clip_to_row (w, row, TEXT_AREA, gc);
7409 if (kind == BAR_CURSOR)
7411 if (width < 0)
7412 width = FRAME_CURSOR_WIDTH (f);
7413 width = min (cursor_glyph->pixel_width, width);
7415 w->phys_cursor_width = width;
7417 XFillRectangle (dpy, window, gc,
7418 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7419 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7420 width, row->height);
7422 else
7424 int dummy_x, dummy_y, dummy_h;
7426 if (width < 0)
7427 width = row->height;
7429 width = min (row->height, width);
7431 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
7432 &dummy_y, &dummy_h);
7434 XFillRectangle (dpy, window, gc,
7435 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7436 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7437 row->height - width),
7438 w->phys_cursor_width, width);
7441 XSetClipMask (dpy, gc, None);
7446 /* RIF: Define cursor CURSOR on frame F. */
7448 static void
7449 x_define_frame_cursor (f, cursor)
7450 struct frame *f;
7451 Cursor cursor;
7453 if (!f->pointer_invisible
7454 && f->output_data.x->current_cursor != cursor)
7455 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7456 f->output_data.x->current_cursor = cursor;
7460 /* RIF: Clear area on frame F. */
7462 static void
7463 x_clear_frame_area (f, x, y, width, height)
7464 struct frame *f;
7465 int x, y, width, height;
7467 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7468 x, y, width, height, False);
7469 #ifdef USE_GTK
7470 /* Must queue a redraw, because scroll bars might have been cleared. */
7471 if (FRAME_GTK_WIDGET (f))
7472 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
7473 #endif
7477 /* RIF: Draw cursor on window W. */
7479 static void
7480 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7481 struct window *w;
7482 struct glyph_row *glyph_row;
7483 int x, y;
7484 int cursor_type, cursor_width;
7485 int on_p, active_p;
7487 struct frame *f = XFRAME (WINDOW_FRAME (w));
7489 if (on_p)
7491 w->phys_cursor_type = cursor_type;
7492 w->phys_cursor_on_p = 1;
7494 if (glyph_row->exact_window_width_line_p
7495 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7497 glyph_row->cursor_in_fringe_p = 1;
7498 draw_fringe_bitmap (w, glyph_row, 0);
7500 else
7501 switch (cursor_type)
7503 case HOLLOW_BOX_CURSOR:
7504 x_draw_hollow_cursor (w, glyph_row);
7505 break;
7507 case FILLED_BOX_CURSOR:
7508 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7509 break;
7511 case BAR_CURSOR:
7512 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7513 break;
7515 case HBAR_CURSOR:
7516 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7517 break;
7519 case NO_CURSOR:
7520 w->phys_cursor_width = 0;
7521 break;
7523 default:
7524 abort ();
7527 #ifdef HAVE_X_I18N
7528 if (w == XWINDOW (f->selected_window))
7529 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7530 xic_set_preeditarea (w, x, y);
7531 #endif
7534 #ifndef XFlush
7535 XFlush (FRAME_X_DISPLAY (f));
7536 #endif
7540 /* Icons. */
7542 /* Make the x-window of frame F use the gnu icon bitmap. */
7545 x_bitmap_icon (f, file)
7546 struct frame *f;
7547 Lisp_Object file;
7549 int bitmap_id;
7551 if (FRAME_X_WINDOW (f) == 0)
7552 return 1;
7554 /* Free up our existing icon bitmap and mask if any. */
7555 if (f->output_data.x->icon_bitmap > 0)
7556 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7557 f->output_data.x->icon_bitmap = 0;
7559 if (STRINGP (file))
7561 #ifdef USE_GTK
7562 /* Use gtk_window_set_icon_from_file () if available,
7563 It's not restricted to bitmaps */
7564 if (xg_set_icon (f, file))
7565 return 0;
7566 #endif /* USE_GTK */
7567 bitmap_id = x_create_bitmap_from_file (f, file);
7568 x_create_bitmap_mask (f, bitmap_id);
7570 else
7572 /* Create the GNU bitmap and mask if necessary. */
7573 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7575 int rc = -1;
7577 #ifdef USE_GTK
7579 if (xg_set_icon (f, xg_default_icon_file)
7580 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7581 return 0;
7583 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7585 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7586 if (rc != -1)
7587 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7589 #endif
7591 /* If all else fails, use the (black and white) xbm image. */
7592 if (rc == -1)
7594 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7595 gnu_xbm_width, gnu_xbm_height);
7596 if (rc == -1)
7597 return 1;
7599 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7600 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7604 /* The first time we create the GNU bitmap and mask,
7605 this increments the ref-count one extra time.
7606 As a result, the GNU bitmap and mask are never freed.
7607 That way, we don't have to worry about allocating it again. */
7608 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7610 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7613 x_wm_set_icon_pixmap (f, bitmap_id);
7614 f->output_data.x->icon_bitmap = bitmap_id;
7616 return 0;
7620 /* Make the x-window of frame F use a rectangle with text.
7621 Use ICON_NAME as the text. */
7624 x_text_icon (f, icon_name)
7625 struct frame *f;
7626 char *icon_name;
7628 if (FRAME_X_WINDOW (f) == 0)
7629 return 1;
7632 XTextProperty text;
7633 text.value = (unsigned char *) icon_name;
7634 text.encoding = XA_STRING;
7635 text.format = 8;
7636 text.nitems = strlen (icon_name);
7637 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7640 if (f->output_data.x->icon_bitmap > 0)
7641 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7642 f->output_data.x->icon_bitmap = 0;
7643 x_wm_set_icon_pixmap (f, 0);
7645 return 0;
7648 #define X_ERROR_MESSAGE_SIZE 200
7650 /* If non-nil, this should be a string.
7651 It means catch X errors and store the error message in this string.
7653 The reason we use a stack is that x_catch_error/x_uncatch_error can
7654 be called from a signal handler.
7657 struct x_error_message_stack {
7658 char string[X_ERROR_MESSAGE_SIZE];
7659 Display *dpy;
7660 struct x_error_message_stack *prev;
7662 static struct x_error_message_stack *x_error_message;
7664 /* An X error handler which stores the error message in
7665 *x_error_message. This is called from x_error_handler if
7666 x_catch_errors is in effect. */
7668 static void
7669 x_error_catcher (display, error)
7670 Display *display;
7671 XErrorEvent *error;
7673 XGetErrorText (display, error->error_code,
7674 x_error_message->string,
7675 X_ERROR_MESSAGE_SIZE);
7678 /* Begin trapping X errors for display DPY. Actually we trap X errors
7679 for all displays, but DPY should be the display you are actually
7680 operating on.
7682 After calling this function, X protocol errors no longer cause
7683 Emacs to exit; instead, they are recorded in the string
7684 stored in *x_error_message.
7686 Calling x_check_errors signals an Emacs error if an X error has
7687 occurred since the last call to x_catch_errors or x_check_errors.
7689 Calling x_uncatch_errors resumes the normal error handling. */
7691 void x_check_errors ();
7693 void
7694 x_catch_errors (dpy)
7695 Display *dpy;
7697 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7699 /* Make sure any errors from previous requests have been dealt with. */
7700 XSync (dpy, False);
7702 data->dpy = dpy;
7703 data->string[0] = 0;
7704 data->prev = x_error_message;
7705 x_error_message = data;
7708 /* Undo the last x_catch_errors call.
7709 DPY should be the display that was passed to x_catch_errors. */
7711 void
7712 x_uncatch_errors ()
7714 struct x_error_message_stack *tmp;
7716 BLOCK_INPUT;
7718 /* The display may have been closed before this function is called.
7719 Check if it is still open before calling XSync. */
7720 if (x_display_info_for_display (x_error_message->dpy) != 0)
7721 XSync (x_error_message->dpy, False);
7723 tmp = x_error_message;
7724 x_error_message = x_error_message->prev;
7725 xfree (tmp);
7726 UNBLOCK_INPUT;
7729 /* If any X protocol errors have arrived since the last call to
7730 x_catch_errors or x_check_errors, signal an Emacs error using
7731 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7733 void
7734 x_check_errors (dpy, format)
7735 Display *dpy;
7736 char *format;
7738 /* Make sure to catch any errors incurred so far. */
7739 XSync (dpy, False);
7741 if (x_error_message->string[0])
7743 char string[X_ERROR_MESSAGE_SIZE];
7744 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7745 x_uncatch_errors ();
7746 error (format, string);
7750 /* Nonzero if we had any X protocol errors
7751 since we did x_catch_errors on DPY. */
7754 x_had_errors_p (dpy)
7755 Display *dpy;
7757 /* Make sure to catch any errors incurred so far. */
7758 XSync (dpy, False);
7760 return x_error_message->string[0] != 0;
7763 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7765 void
7766 x_clear_errors (dpy)
7767 Display *dpy;
7769 x_error_message->string[0] = 0;
7772 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7773 * idea. --lorentey */
7774 /* Close off all unclosed x_catch_errors calls. */
7776 void
7777 x_fully_uncatch_errors ()
7779 while (x_error_message)
7780 x_uncatch_errors ();
7782 #endif
7784 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7787 x_catching_errors ()
7789 return x_error_message != 0;
7792 #if 0
7793 static unsigned int x_wire_count;
7794 x_trace_wire ()
7796 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7798 #endif /* ! 0 */
7801 /* Handle SIGPIPE, which can happen when the connection to a server
7802 simply goes away. SIGPIPE is handled by x_connection_signal.
7803 Don't need to do anything, because the write which caused the
7804 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7805 which will do the appropriate cleanup for us. */
7807 static SIGTYPE
7808 x_connection_signal (signalnum) /* If we don't have an argument, */
7809 int signalnum; /* some compilers complain in signal calls. */
7811 #ifdef USG
7812 /* USG systems forget handlers when they are used;
7813 must reestablish each time */
7814 signal (signalnum, x_connection_signal);
7815 #endif /* USG */
7819 /************************************************************************
7820 Handling X errors
7821 ************************************************************************/
7823 /* Error message passed to x_connection_closed. */
7825 static char *error_msg;
7827 /* Function installed as fatal_error_signal_hook in
7828 x_connection_closed. Print the X error message, and exit normally,
7829 instead of dumping core when XtCloseDisplay fails. */
7831 static void
7832 x_fatal_error_signal ()
7834 fprintf (stderr, "%s\n", error_msg);
7835 exit (70);
7838 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7839 the text of an error message that lead to the connection loss. */
7841 static SIGTYPE
7842 x_connection_closed (dpy, error_message)
7843 Display *dpy;
7844 char *error_message;
7846 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7847 Lisp_Object frame, tail;
7848 int index = SPECPDL_INDEX ();
7850 error_msg = (char *) alloca (strlen (error_message) + 1);
7851 strcpy (error_msg, error_message);
7852 handling_signal = 0;
7854 /* Prevent being called recursively because of an error condition
7855 below. Otherwise, we might end up with printing ``can't find per
7856 display information'' in the recursive call instead of printing
7857 the original message here. */
7858 x_catch_errors (dpy);
7860 /* Inhibit redisplay while frames are being deleted. */
7861 specbind (Qinhibit_redisplay, Qt);
7863 if (dpyinfo)
7865 /* Protect display from being closed when we delete the last
7866 frame on it. */
7867 dpyinfo->reference_count++;
7868 dpyinfo->terminal->reference_count++;
7871 /* First delete frames whose mini-buffers are on frames
7872 that are on the dead display. */
7873 FOR_EACH_FRAME (tail, frame)
7875 Lisp_Object minibuf_frame;
7876 minibuf_frame
7877 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7878 if (FRAME_X_P (XFRAME (frame))
7879 && FRAME_X_P (XFRAME (minibuf_frame))
7880 && ! EQ (frame, minibuf_frame)
7881 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7882 delete_frame (frame, Qnoelisp);
7885 /* Now delete all remaining frames on the dead display.
7886 We are now sure none of these is used as the mini-buffer
7887 for another frame that we need to delete. */
7888 FOR_EACH_FRAME (tail, frame)
7889 if (FRAME_X_P (XFRAME (frame))
7890 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7892 /* Set this to t so that delete_frame won't get confused
7893 trying to find a replacement. */
7894 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7895 delete_frame (frame, Qnoelisp);
7898 /* We have to close the display to inform Xt that it doesn't
7899 exist anymore. If we don't, Xt will continue to wait for
7900 events from the display. As a consequence, a sequence of
7902 M-x make-frame-on-display RET :1 RET
7903 ...kill the new frame, so that we get an IO error...
7904 M-x make-frame-on-display RET :1 RET
7906 will indefinitely wait in Xt for events for display `:1', opened
7907 in the first call to make-frame-on-display.
7909 Closing the display is reported to lead to a bus error on
7910 OpenWindows in certain situations. I suspect that is a bug
7911 in OpenWindows. I don't know how to circumvent it here. */
7913 if (dpyinfo)
7915 #ifdef USE_X_TOOLKIT
7916 /* If DPYINFO is null, this means we didn't open the display
7917 in the first place, so don't try to close it. */
7919 extern void (*fatal_error_signal_hook) P_ ((void));
7920 fatal_error_signal_hook = x_fatal_error_signal;
7921 XtCloseDisplay (dpy);
7922 fatal_error_signal_hook = NULL;
7924 #endif
7926 #ifdef USE_GTK
7927 /* Due to bugs in some Gtk+ versions, just exit here if this
7928 is the last display/terminal. */
7929 if (terminal_list->next_terminal == NULL)
7931 fprintf (stderr, "%s\n", error_msg);
7932 shut_down_emacs (0, 0, Qnil);
7933 exit (70);
7935 xg_display_close (dpyinfo->display);
7936 #endif
7938 /* Indicate that this display is dead. */
7939 dpyinfo->display = 0;
7941 dpyinfo->reference_count--;
7942 dpyinfo->terminal->reference_count--;
7943 if (dpyinfo->reference_count != 0)
7944 /* We have just closed all frames on this display. */
7945 abort ();
7948 Lisp_Object tmp;
7949 XSETTERMINAL (tmp, dpyinfo->terminal);
7950 Fdelete_terminal (tmp, Qnoelisp);
7954 x_uncatch_errors ();
7956 if (terminal_list == 0)
7958 fprintf (stderr, "%s\n", error_msg);
7959 shut_down_emacs (0, 0, Qnil);
7960 exit (70);
7963 /* Ordinary stack unwind doesn't deal with these. */
7964 #ifdef SIGIO
7965 sigunblock (sigmask (SIGIO));
7966 #endif
7967 sigunblock (sigmask (SIGALRM));
7968 TOTALLY_UNBLOCK_INPUT;
7970 unbind_to (index, Qnil);
7971 clear_waiting_for_input ();
7972 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7973 longjmp), because returning from this function would get us back into
7974 Xlib's code which will directly call `exit'. */
7975 error ("%s", error_msg);
7978 /* We specifically use it before defining it, so that gcc doesn't inline it,
7979 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7980 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7982 /* This is the first-level handler for X protocol errors.
7983 It calls x_error_quitter or x_error_catcher. */
7985 static int
7986 x_error_handler (display, error)
7987 Display *display;
7988 XErrorEvent *error;
7990 if (x_error_message)
7991 x_error_catcher (display, error);
7992 else
7993 x_error_quitter (display, error);
7994 return 0;
7997 /* This is the usual handler for X protocol errors.
7998 It kills all frames on the display that we got the error for.
7999 If that was the only one, it prints an error message and kills Emacs. */
8001 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
8003 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
8004 #define NO_INLINE __attribute__((noinline))
8005 #else
8006 #define NO_INLINE
8007 #endif
8009 /* Some versions of GNU/Linux define noinline in their headers. */
8011 #ifdef noinline
8012 #undef noinline
8013 #endif
8015 /* On older GCC versions, just putting x_error_quitter
8016 after x_error_handler prevents inlining into the former. */
8018 static void NO_INLINE
8019 x_error_quitter (display, error)
8020 Display *display;
8021 XErrorEvent *error;
8023 char buf[256], buf1[356];
8025 /* Ignore BadName errors. They can happen because of fonts
8026 or colors that are not defined. */
8028 if (error->error_code == BadName)
8029 return;
8031 /* Note that there is no real way portable across R3/R4 to get the
8032 original error handler. */
8034 XGetErrorText (display, error->error_code, buf, sizeof (buf));
8035 sprintf (buf1, "X protocol error: %s on protocol request %d",
8036 buf, error->request_code);
8037 x_connection_closed (display, buf1);
8041 /* This is the handler for X IO errors, always.
8042 It kills all frames on the display that we lost touch with.
8043 If that was the only one, it prints an error message and kills Emacs. */
8045 static int
8046 x_io_error_quitter (display)
8047 Display *display;
8049 char buf[256];
8051 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
8052 x_connection_closed (display, buf);
8053 return 0;
8056 /* Changing the font of the frame. */
8058 /* Give frame F the font FONT-OBJECT as its default font. The return
8059 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
8060 frame. If it is negative, generate a new fontset from
8061 FONT-OBJECT. */
8063 Lisp_Object
8064 x_new_font (f, font_object, fontset)
8065 struct frame *f;
8066 Lisp_Object font_object;
8067 int fontset;
8069 struct font *font = XFONT_OBJECT (font_object);
8071 if (fontset < 0)
8072 fontset = fontset_from_font (font_object);
8073 FRAME_FONTSET (f) = fontset;
8074 if (FRAME_FONT (f) == font)
8075 /* This font is already set in frame F. There's nothing more to
8076 do. */
8077 return font_object;
8079 FRAME_FONT (f) = font;
8080 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8081 FRAME_COLUMN_WIDTH (f) = font->average_width;
8082 FRAME_SPACE_WIDTH (f) = font->space_width;
8083 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8085 compute_fringe_widths (f, 1);
8087 /* Compute the scroll bar width in character columns. */
8088 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8090 int wid = FRAME_COLUMN_WIDTH (f);
8091 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8092 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8094 else
8096 int wid = FRAME_COLUMN_WIDTH (f);
8097 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8100 if (FRAME_X_WINDOW (f) != 0)
8102 /* Don't change the size of a tip frame; there's no point in
8103 doing it because it's done in Fx_show_tip, and it leads to
8104 problems because the tip frame has no widget. */
8105 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8106 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8109 #ifdef HAVE_X_I18N
8110 if (FRAME_XIC (f)
8111 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8113 BLOCK_INPUT;
8114 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8115 UNBLOCK_INPUT;
8117 #endif
8119 return font_object;
8123 /***********************************************************************
8124 X Input Methods
8125 ***********************************************************************/
8127 #ifdef HAVE_X_I18N
8129 #ifdef HAVE_X11R6
8131 /* XIM destroy callback function, which is called whenever the
8132 connection to input method XIM dies. CLIENT_DATA contains a
8133 pointer to the x_display_info structure corresponding to XIM. */
8135 static void
8136 xim_destroy_callback (xim, client_data, call_data)
8137 XIM xim;
8138 XPointer client_data;
8139 XPointer call_data;
8141 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8142 Lisp_Object frame, tail;
8144 BLOCK_INPUT;
8146 /* No need to call XDestroyIC.. */
8147 FOR_EACH_FRAME (tail, frame)
8149 struct frame *f = XFRAME (frame);
8150 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8152 FRAME_XIC (f) = NULL;
8153 xic_free_xfontset (f);
8157 /* No need to call XCloseIM. */
8158 dpyinfo->xim = NULL;
8159 XFree (dpyinfo->xim_styles);
8160 UNBLOCK_INPUT;
8163 #endif /* HAVE_X11R6 */
8165 #ifdef HAVE_X11R6
8166 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8167 extern char *XSetIMValues P_ ((XIM, ...));
8168 #endif
8170 /* Open the connection to the XIM server on display DPYINFO.
8171 RESOURCE_NAME is the resource name Emacs uses. */
8173 static void
8174 xim_open_dpy (dpyinfo, resource_name)
8175 struct x_display_info *dpyinfo;
8176 char *resource_name;
8178 XIM xim;
8180 #ifdef HAVE_XIM
8181 if (use_xim)
8183 if (dpyinfo->xim)
8184 XCloseIM (dpyinfo->xim);
8185 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8186 EMACS_CLASS);
8187 dpyinfo->xim = xim;
8189 if (xim)
8191 #ifdef HAVE_X11R6
8192 XIMCallback destroy;
8193 #endif
8195 /* Get supported styles and XIM values. */
8196 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8198 #ifdef HAVE_X11R6
8199 destroy.callback = xim_destroy_callback;
8200 destroy.client_data = (XPointer)dpyinfo;
8201 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8202 #endif
8206 else
8207 #endif /* HAVE_XIM */
8208 dpyinfo->xim = NULL;
8212 #ifdef HAVE_X11R6_XIM
8214 /* XIM instantiate callback function, which is called whenever an XIM
8215 server is available. DISPLAY is the display of the XIM.
8216 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8217 when the callback was registered. */
8219 static void
8220 xim_instantiate_callback (display, client_data, call_data)
8221 Display *display;
8222 XPointer client_data;
8223 XPointer call_data;
8225 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8226 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8228 /* We don't support multiple XIM connections. */
8229 if (dpyinfo->xim)
8230 return;
8232 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8234 /* Create XIC for the existing frames on the same display, as long
8235 as they have no XIC. */
8236 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8238 Lisp_Object tail, frame;
8240 BLOCK_INPUT;
8241 FOR_EACH_FRAME (tail, frame)
8243 struct frame *f = XFRAME (frame);
8245 if (FRAME_X_P (f)
8246 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8247 if (FRAME_XIC (f) == NULL)
8249 create_frame_xic (f);
8250 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8251 xic_set_statusarea (f);
8252 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8254 struct window *w = XWINDOW (f->selected_window);
8255 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8260 UNBLOCK_INPUT;
8264 #endif /* HAVE_X11R6_XIM */
8267 /* Open a connection to the XIM server on display DPYINFO.
8268 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8269 connection only at the first time. On X11R6, open the connection
8270 in the XIM instantiate callback function. */
8272 static void
8273 xim_initialize (dpyinfo, resource_name)
8274 struct x_display_info *dpyinfo;
8275 char *resource_name;
8277 dpyinfo->xim = NULL;
8278 #ifdef HAVE_XIM
8279 if (use_xim)
8281 #ifdef HAVE_X11R6_XIM
8282 struct xim_inst_t *xim_inst;
8283 int len;
8285 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8286 dpyinfo->xim_callback_data = xim_inst;
8287 xim_inst->dpyinfo = dpyinfo;
8288 len = strlen (resource_name);
8289 xim_inst->resource_name = (char *) xmalloc (len + 1);
8290 bcopy (resource_name, xim_inst->resource_name, len + 1);
8291 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8292 resource_name, EMACS_CLASS,
8293 xim_instantiate_callback,
8294 /* This is XPointer in XFree86
8295 but (XPointer *) on Tru64, at
8296 least, hence the configure test. */
8297 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8298 #else /* not HAVE_X11R6_XIM */
8299 xim_open_dpy (dpyinfo, resource_name);
8300 #endif /* not HAVE_X11R6_XIM */
8302 #endif /* HAVE_XIM */
8306 /* Close the connection to the XIM server on display DPYINFO. */
8308 static void
8309 xim_close_dpy (dpyinfo)
8310 struct x_display_info *dpyinfo;
8312 #ifdef HAVE_XIM
8313 if (use_xim)
8315 #ifdef HAVE_X11R6_XIM
8316 if (dpyinfo->display)
8317 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8318 NULL, EMACS_CLASS,
8319 xim_instantiate_callback, NULL);
8320 xfree (dpyinfo->xim_callback_data->resource_name);
8321 xfree (dpyinfo->xim_callback_data);
8322 #endif /* HAVE_X11R6_XIM */
8323 if (dpyinfo->display)
8324 XCloseIM (dpyinfo->xim);
8325 dpyinfo->xim = NULL;
8326 XFree (dpyinfo->xim_styles);
8328 #endif /* HAVE_XIM */
8331 #endif /* not HAVE_X11R6_XIM */
8335 /* Calculate the absolute position in frame F
8336 from its current recorded position values and gravity. */
8338 void
8339 x_calc_absolute_position (f)
8340 struct frame *f;
8342 int flags = f->size_hint_flags;
8344 /* We have nothing to do if the current position
8345 is already for the top-left corner. */
8346 if (! ((flags & XNegative) || (flags & YNegative)))
8347 return;
8349 /* Treat negative positions as relative to the leftmost bottommost
8350 position that fits on the screen. */
8351 if (flags & XNegative)
8352 f->left_pos = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
8353 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8356 int height = FRAME_PIXEL_HEIGHT (f);
8358 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8359 /* Something is fishy here. When using Motif, starting Emacs with
8360 `-g -0-0', the frame appears too low by a few pixels.
8362 This seems to be so because initially, while Emacs is starting,
8363 the column widget's height and the frame's pixel height are
8364 different. The column widget's height is the right one. In
8365 later invocations, when Emacs is up, the frame's pixel height
8366 is right, though.
8368 It's not obvious where the initial small difference comes from.
8369 2000-12-01, gerd. */
8371 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8372 #endif
8374 if (flags & YNegative)
8375 f->top_pos = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
8376 - height + f->top_pos;
8379 /* The left_pos and top_pos
8380 are now relative to the top and left screen edges,
8381 so the flags should correspond. */
8382 f->size_hint_flags &= ~ (XNegative | YNegative);
8385 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8386 to really change the position, and 0 when calling from
8387 x_make_frame_visible (in that case, XOFF and YOFF are the current
8388 position values). It is -1 when calling from x_set_frame_parameters,
8389 which means, do adjust for borders but don't change the gravity. */
8391 void
8392 x_set_offset (f, xoff, yoff, change_gravity)
8393 struct frame *f;
8394 register int xoff, yoff;
8395 int change_gravity;
8397 int modified_top, modified_left;
8399 if (change_gravity > 0)
8401 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8402 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8404 f->top_pos = yoff;
8405 f->left_pos = xoff;
8406 f->size_hint_flags &= ~ (XNegative | YNegative);
8407 if (xoff < 0)
8408 f->size_hint_flags |= XNegative;
8409 if (yoff < 0)
8410 f->size_hint_flags |= YNegative;
8411 f->win_gravity = NorthWestGravity;
8413 x_calc_absolute_position (f);
8415 BLOCK_INPUT;
8416 x_wm_set_size_hint (f, (long) 0, 0);
8418 modified_left = f->left_pos;
8419 modified_top = f->top_pos;
8421 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8423 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8424 than the WM decorations. So we use the calculated offset instead
8425 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8426 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8427 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8430 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8431 modified_left, modified_top);
8433 x_sync_with_move (f, f->left_pos, f->top_pos,
8434 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8435 ? 1 : 0);
8437 /* change_gravity is non-zero when this function is called from Lisp to
8438 programmatically move a frame. In that case, we call
8439 x_check_expected_move to discover if we have a "Type A" or "Type B"
8440 window manager, and, for a "Type A" window manager, adjust the position
8441 of the frame.
8443 We call x_check_expected_move if a programmatic move occurred, and
8444 either the window manager type (A/B) is unknown or it is Type A but we
8445 need to compute the top/left offset adjustment for this frame. */
8447 if (change_gravity != 0 &&
8448 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8449 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8450 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8451 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8452 x_check_expected_move (f, modified_left, modified_top);
8454 UNBLOCK_INPUT;
8457 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8458 on the root window for frame F contains ATOMNAME.
8459 This is how a WM check shall be done according to the Window Manager
8460 Specification/Extended Window Manager Hints at
8461 http://freedesktop.org/wiki/Specifications/wm-spec. */
8463 static int
8464 wm_supports (f, atomname)
8465 struct frame *f;
8466 const char *atomname;
8468 Atom actual_type;
8469 unsigned long actual_size, bytes_remaining;
8470 int i, rc, actual_format;
8471 Atom prop_atom;
8472 Window wmcheck_window;
8473 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8474 Window target_window = dpyinfo->root_window;
8475 long max_len = 65536;
8476 Display *dpy = FRAME_X_DISPLAY (f);
8477 unsigned char *tmp_data = NULL;
8478 Atom target_type = XA_WINDOW;
8479 Atom want_atom;
8481 BLOCK_INPUT;
8483 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8485 x_catch_errors (dpy);
8486 rc = XGetWindowProperty (dpy, target_window,
8487 prop_atom, 0, max_len, False, target_type,
8488 &actual_type, &actual_format, &actual_size,
8489 &bytes_remaining, &tmp_data);
8491 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8493 if (tmp_data) XFree (tmp_data);
8494 x_uncatch_errors ();
8495 UNBLOCK_INPUT;
8496 return 0;
8499 wmcheck_window = *(Window *) tmp_data;
8500 XFree (tmp_data);
8502 /* Check if window exists. */
8503 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8504 x_sync (f);
8505 if (x_had_errors_p (dpy))
8507 x_uncatch_errors ();
8508 UNBLOCK_INPUT;
8509 return 0;
8512 if (dpyinfo->net_supported_window != wmcheck_window)
8514 /* Window changed, reload atoms */
8515 if (dpyinfo->net_supported_atoms != NULL)
8516 XFree (dpyinfo->net_supported_atoms);
8517 dpyinfo->net_supported_atoms = NULL;
8518 dpyinfo->nr_net_supported_atoms = 0;
8519 dpyinfo->net_supported_window = 0;
8521 target_type = XA_ATOM;
8522 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8523 tmp_data = NULL;
8524 rc = XGetWindowProperty (dpy, target_window,
8525 prop_atom, 0, max_len, False, target_type,
8526 &actual_type, &actual_format, &actual_size,
8527 &bytes_remaining, &tmp_data);
8529 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8531 if (tmp_data) XFree (tmp_data);
8532 x_uncatch_errors ();
8533 UNBLOCK_INPUT;
8534 return 0;
8537 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8538 dpyinfo->nr_net_supported_atoms = actual_size;
8539 dpyinfo->net_supported_window = wmcheck_window;
8542 rc = 0;
8543 want_atom = XInternAtom (dpy, atomname, False);
8545 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8546 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8548 x_uncatch_errors ();
8549 UNBLOCK_INPUT;
8551 return rc;
8554 static void
8555 set_wm_state (frame, add, what, what2)
8556 Lisp_Object frame;
8557 int add;
8558 const char *what;
8559 const char *what2;
8561 const char *atom = "_NET_WM_STATE";
8562 Fx_send_client_event (frame, make_number (0), frame,
8563 make_unibyte_string (atom, strlen (atom)),
8564 make_number (32),
8565 /* 1 = add, 0 = remove */
8566 Fcons
8567 (make_number (add ? 1 : 0),
8568 Fcons
8569 (make_unibyte_string (what, strlen (what)),
8570 what2 != 0
8571 ? Fcons (make_unibyte_string (what2, strlen (what2)),
8572 Qnil)
8573 : Qnil)));
8576 void
8577 x_set_sticky (f, new_value, old_value)
8578 struct frame *f;
8579 Lisp_Object new_value, old_value;
8581 Lisp_Object frame;
8583 XSETFRAME (frame, f);
8584 set_wm_state (frame, NILP (new_value) ? 0 : 1,
8585 "_NET_WM_STATE_STICKY", NULL);
8588 /* Do fullscreen as specified in extended window manager hints */
8590 static int
8591 do_ewmh_fullscreen (f)
8592 struct frame *f;
8594 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8596 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8597 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8598 if (!have_net_atom)
8599 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8601 if (have_net_atom)
8603 Lisp_Object frame;
8604 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8605 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8606 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8608 XSETFRAME (frame, f);
8610 set_wm_state (frame, 0, fs, NULL);
8611 set_wm_state (frame, 0, fh, NULL);
8612 set_wm_state (frame, 0, fw, NULL);
8614 /* If there are _NET_ atoms we assume we have extended window manager
8615 hints. */
8616 switch (f->want_fullscreen)
8618 case FULLSCREEN_BOTH:
8619 set_wm_state (frame, 1, fs, NULL);
8620 break;
8621 case FULLSCREEN_WIDTH:
8622 set_wm_state (frame, 1, fw, NULL);
8623 break;
8624 case FULLSCREEN_HEIGHT:
8625 set_wm_state (frame, 1, fh, NULL);
8626 break;
8627 case FULLSCREEN_MAXIMIZED:
8628 set_wm_state (frame, 1, fw, fh);
8629 break;
8632 f->want_fullscreen = FULLSCREEN_NONE;
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 x_check_fullscreen (f);
8647 x_sync (f);
8648 UNBLOCK_INPUT;
8653 static void
8654 x_handle_net_wm_state (f, event)
8655 struct frame *f;
8656 XPropertyEvent *event;
8658 Atom actual_type;
8659 unsigned long actual_size, bytes_remaining;
8660 int i, rc, actual_format, value = FULLSCREEN_NONE;
8661 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8662 long max_len = 65536;
8663 Display *dpy = FRAME_X_DISPLAY (f);
8664 unsigned char *tmp_data = NULL;
8665 Atom target_type = XA_ATOM;
8666 Lisp_Object lval;
8667 int sticky = 0;
8669 BLOCK_INPUT;
8670 x_catch_errors (dpy);
8671 rc = XGetWindowProperty (dpy, event->window,
8672 event->atom, 0, max_len, False, target_type,
8673 &actual_type, &actual_format, &actual_size,
8674 &bytes_remaining, &tmp_data);
8676 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
8678 if (tmp_data) XFree (tmp_data);
8679 x_uncatch_errors ();
8680 UNBLOCK_INPUT;
8681 return;
8684 x_uncatch_errors ();
8686 for (i = 0; i < actual_size; ++i)
8688 Atom a = ((Atom*)tmp_data)[i];
8689 if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
8691 if (value == FULLSCREEN_HEIGHT)
8692 value = FULLSCREEN_MAXIMIZED;
8693 else
8694 value = FULLSCREEN_WIDTH;
8696 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
8698 if (value == FULLSCREEN_WIDTH)
8699 value = FULLSCREEN_MAXIMIZED;
8700 else
8701 value = FULLSCREEN_HEIGHT;
8703 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen_atom)
8704 value = FULLSCREEN_BOTH;
8705 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
8706 sticky = 1;
8709 lval = Qnil;
8710 switch (value)
8712 case FULLSCREEN_WIDTH:
8713 lval = Qfullwidth;
8714 break;
8715 case FULLSCREEN_HEIGHT:
8716 lval = Qfullheight;
8717 break;
8718 case FULLSCREEN_BOTH:
8719 lval = Qfullboth;
8720 break;
8721 case FULLSCREEN_MAXIMIZED:
8722 lval = Qmaximized;
8723 break;
8726 store_frame_param (f, Qfullscreen, lval);
8727 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
8729 if (tmp_data) XFree (tmp_data);
8730 UNBLOCK_INPUT;
8733 /* Check if we need to resize the frame due to a fullscreen request.
8734 If so needed, resize the frame. */
8735 static void
8736 x_check_fullscreen (f)
8737 struct frame *f;
8739 if (do_ewmh_fullscreen (f))
8740 return;
8742 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8743 return; /* Only fullscreen without WM or with EWM hints (above). */
8745 if (f->want_fullscreen != FULLSCREEN_NONE)
8747 int width = FRAME_COLS (f), height = FRAME_LINES (f);
8748 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8750 switch (f->want_fullscreen)
8752 /* No difference between these two when there is no WM */
8753 case FULLSCREEN_BOTH:
8754 case FULLSCREEN_MAXIMIZED:
8755 width = x_display_pixel_width (dpyinfo);
8756 height = x_display_pixel_height (dpyinfo);
8757 break;
8758 case FULLSCREEN_WIDTH:
8759 width = x_display_pixel_width (dpyinfo);
8760 break;
8761 case FULLSCREEN_HEIGHT:
8762 height = x_display_pixel_height (dpyinfo);
8765 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8767 change_frame_size (f, height, width, 0, 1, 0);
8768 SET_FRAME_GARBAGED (f);
8769 cancel_mouse_face (f);
8774 /* This function is called by x_set_offset to determine whether the window
8775 manager interfered with the positioning of the frame. Type A window
8776 managers position the surrounding window manager decorations a small
8777 amount above and left of the user-supplied position. Type B window
8778 managers position the surrounding window manager decorations at the
8779 user-specified position. If we detect a Type A window manager, we
8780 compensate by moving the window right and down by the proper amount. */
8782 static void
8783 x_check_expected_move (f, expected_left, expected_top)
8784 struct frame *f;
8785 int expected_left;
8786 int expected_top;
8788 int current_left = 0, current_top = 0;
8790 /* x_real_positions returns the left and top offsets of the outermost
8791 window manager window around the frame. */
8793 x_real_positions (f, &current_left, &current_top);
8795 if (current_left != expected_left || current_top != expected_top)
8797 /* It's a "Type A" window manager. */
8799 int adjusted_left;
8800 int adjusted_top;
8802 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8803 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8804 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8806 /* Now fix the mispositioned frame's location. */
8808 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8809 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8811 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8812 adjusted_left, adjusted_top);
8814 x_sync_with_move (f, expected_left, expected_top, 0);
8816 else
8817 /* It's a "Type B" window manager. We don't have to adjust the
8818 frame's position. */
8820 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8824 /* Wait for XGetGeometry to return up-to-date position information for a
8825 recently-moved frame. Call this immediately after calling XMoveWindow.
8826 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8827 frame has been moved to, so we use a fuzzy position comparison instead
8828 of an exact comparison. */
8830 static void
8831 x_sync_with_move (f, left, top, fuzzy)
8832 struct frame *f;
8833 int left, top, fuzzy;
8835 int count = 0;
8837 while (count++ < 50)
8839 int current_left = 0, current_top = 0;
8841 /* In theory, this call to XSync only needs to happen once, but in
8842 practice, it doesn't seem to work, hence the need for the surrounding
8843 loop. */
8845 XSync (FRAME_X_DISPLAY (f), False);
8846 x_real_positions (f, &current_left, &current_top);
8848 if (fuzzy)
8850 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8851 pixels. */
8853 if (eabs (current_left - left) <= 10
8854 && eabs (current_top - top) <= 40)
8855 return;
8857 else if (current_left == left && current_top == top)
8858 return;
8861 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8862 will then return up-to-date position info. */
8864 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8868 /* Wait for an event on frame F matching EVENTTYPE. */
8869 void
8870 x_wait_for_event (f, eventtype)
8871 struct frame *f;
8872 int eventtype;
8874 int level = interrupt_input_blocked;
8876 SELECT_TYPE fds;
8877 EMACS_TIME tmo, tmo_at, time_now;
8878 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
8880 pending_event_wait.f = f;
8881 pending_event_wait.eventtype = eventtype;
8883 /* Set timeout to 0.1 second. Hopefully not noticable.
8884 Maybe it should be configurable. */
8885 EMACS_SET_SECS_USECS (tmo, 0, 100000);
8886 EMACS_GET_TIME (tmo_at);
8887 EMACS_ADD_TIME (tmo_at, tmo_at, tmo);
8889 while (pending_event_wait.eventtype)
8891 interrupt_input_pending = 1;
8892 TOTALLY_UNBLOCK_INPUT;
8893 /* XTread_socket is called after unblock. */
8894 BLOCK_INPUT;
8895 interrupt_input_blocked = level;
8897 FD_ZERO (&fds);
8898 FD_SET (fd, &fds);
8900 EMACS_GET_TIME (time_now);
8901 EMACS_SUB_TIME (tmo, tmo_at, time_now);
8903 if (EMACS_TIME_NEG_P (tmo) || select (fd+1, &fds, NULL, NULL, &tmo) == 0)
8904 break; /* Timeout */
8906 pending_event_wait.f = 0;
8907 pending_event_wait.eventtype = 0;
8911 /* Change the size of frame F's X window to COLS/ROWS in the case F
8912 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8913 top-left-corner window gravity for this size change and subsequent
8914 size changes. Otherwise we leave the window gravity unchanged. */
8916 static void
8917 x_set_window_size_1 (f, change_gravity, cols, rows)
8918 struct frame *f;
8919 int change_gravity;
8920 int cols, rows;
8922 int pixelwidth, pixelheight;
8924 check_frame_size (f, &rows, &cols);
8925 f->scroll_bar_actual_width
8926 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8928 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8929 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8930 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8932 compute_fringe_widths (f, 0);
8934 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
8935 + 2*f->border_width;
8936 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
8937 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f)
8938 + 2*f->border_width;
8940 if (change_gravity) f->win_gravity = NorthWestGravity;
8941 x_wm_set_size_hint (f, (long) 0, 0);
8942 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8943 pixelwidth, pixelheight);
8946 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8947 receive in the ConfigureNotify event; if we get what we asked
8948 for, then the event won't cause the screen to become garbaged, so
8949 we have to make sure to do it here. */
8950 SET_FRAME_GARBAGED (f);
8952 /* Now, strictly speaking, we can't be sure that this is accurate,
8953 but the window manager will get around to dealing with the size
8954 change request eventually, and we'll hear how it went when the
8955 ConfigureNotify event gets here.
8957 We could just not bother storing any of this information here,
8958 and let the ConfigureNotify event set everything up, but that
8959 might be kind of confusing to the Lisp code, since size changes
8960 wouldn't be reported in the frame parameters until some random
8961 point in the future when the ConfigureNotify event arrives.
8963 We pass 1 for DELAY since we can't run Lisp code inside of
8964 a BLOCK_INPUT. */
8966 /* But the ConfigureNotify may in fact never arrive, and then this is
8967 not right if the frame is visible. Instead wait (with timeout)
8968 for the ConfigureNotify. */
8969 if (f->async_visible)
8970 x_wait_for_event (f, ConfigureNotify);
8971 else
8973 change_frame_size (f, rows, cols, 0, 1, 0);
8974 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8975 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8976 x_sync (f);
8981 /* Call this to change the size of frame F's x-window.
8982 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8983 for this size change and subsequent size changes.
8984 Otherwise we leave the window gravity unchanged. */
8986 void
8987 x_set_window_size (f, change_gravity, cols, rows)
8988 struct frame *f;
8989 int change_gravity;
8990 int cols, rows;
8992 BLOCK_INPUT;
8994 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8996 int r, c;
8998 /* When the frame is maximized/fullscreen or running under for
8999 example Xmonad, x_set_window_size_1 will be a no-op.
9000 In that case, the right thing to do is extend rows/cols to
9001 the current frame size. We do that first if x_set_window_size_1
9002 turns out to not be a no-op (there is no way to know).
9003 The size will be adjusted again if the frame gets a
9004 ConfigureNotify event as a result of x_set_window_size. */
9005 int pixelh = FRAME_PIXEL_HEIGHT (f);
9006 #ifdef USE_X_TOOLKIT
9007 /* The menu bar is not part of text lines. The tool bar
9008 is however. */
9009 pixelh -= FRAME_MENUBAR_HEIGHT (f);
9010 #endif
9011 r = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelh);
9012 /* Update f->scroll_bar_actual_width because it is used in
9013 FRAME_PIXEL_WIDTH_TO_TEXT_COLS. */
9014 f->scroll_bar_actual_width
9015 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
9016 c = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, FRAME_PIXEL_WIDTH (f));
9017 change_frame_size (f, r, c, 0, 1, 0);
9020 #ifdef USE_GTK
9021 if (FRAME_GTK_WIDGET (f))
9022 xg_frame_set_char_size (f, cols, rows);
9023 else
9024 x_set_window_size_1 (f, change_gravity, cols, rows);
9025 #else /* not USE_GTK */
9027 x_set_window_size_1 (f, change_gravity, cols, rows);
9029 #endif /* not USE_GTK */
9031 /* If cursor was outside the new size, mark it as off. */
9032 mark_window_cursors_off (XWINDOW (f->root_window));
9034 /* Clear out any recollection of where the mouse highlighting was,
9035 since it might be in a place that's outside the new frame size.
9036 Actually checking whether it is outside is a pain in the neck,
9037 so don't try--just let the highlighting be done afresh with new size. */
9038 cancel_mouse_face (f);
9040 UNBLOCK_INPUT;
9043 /* Mouse warping. */
9045 void
9046 x_set_mouse_position (f, x, y)
9047 struct frame *f;
9048 int x, y;
9050 int pix_x, pix_y;
9052 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
9053 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
9055 if (pix_x < 0) pix_x = 0;
9056 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
9058 if (pix_y < 0) pix_y = 0;
9059 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
9061 BLOCK_INPUT;
9063 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9064 0, 0, 0, 0, pix_x, pix_y);
9065 UNBLOCK_INPUT;
9068 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9070 void
9071 x_set_mouse_pixel_position (f, pix_x, pix_y)
9072 struct frame *f;
9073 int pix_x, pix_y;
9075 BLOCK_INPUT;
9077 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9078 0, 0, 0, 0, pix_x, pix_y);
9079 UNBLOCK_INPUT;
9082 /* focus shifting, raising and lowering. */
9084 void
9085 x_focus_on_frame (f)
9086 struct frame *f;
9088 #if 0
9089 /* I don't think that the ICCCM allows programs to do things like this
9090 without the interaction of the window manager. Whatever you end up
9091 doing with this code, do it to x_unfocus_frame too. */
9092 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9093 RevertToPointerRoot, CurrentTime);
9094 #endif /* ! 0 */
9097 void
9098 x_unfocus_frame (f)
9099 struct frame *f;
9101 #if 0
9102 /* Look at the remarks in x_focus_on_frame. */
9103 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
9104 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
9105 RevertToPointerRoot, CurrentTime);
9106 #endif /* ! 0 */
9109 /* Raise frame F. */
9111 void
9112 x_raise_frame (f)
9113 struct frame *f;
9115 BLOCK_INPUT;
9116 if (f->async_visible)
9117 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9119 XFlush (FRAME_X_DISPLAY (f));
9120 UNBLOCK_INPUT;
9123 /* Lower frame F. */
9125 void
9126 x_lower_frame (f)
9127 struct frame *f;
9129 if (f->async_visible)
9131 BLOCK_INPUT;
9132 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9133 XFlush (FRAME_X_DISPLAY (f));
9134 UNBLOCK_INPUT;
9138 /* Activate frame with Extended Window Manager Hints */
9140 void
9141 x_ewmh_activate_frame (f)
9142 FRAME_PTR f;
9144 /* See Window Manager Specification/Extended Window Manager Hints at
9145 http://freedesktop.org/wiki/Specifications/wm-spec */
9147 const char *atom = "_NET_ACTIVE_WINDOW";
9148 if (f->async_visible && wm_supports (f, atom))
9150 Lisp_Object frame;
9151 XSETFRAME (frame, f);
9152 Fx_send_client_event (frame, make_number (0), frame,
9153 make_unibyte_string (atom, strlen (atom)),
9154 make_number (32),
9155 Fcons (make_number (1),
9156 Fcons (make_number (last_user_time),
9157 Qnil)));
9161 static void
9162 XTframe_raise_lower (f, raise_flag)
9163 FRAME_PTR f;
9164 int raise_flag;
9166 if (raise_flag)
9167 x_raise_frame (f);
9168 else
9169 x_lower_frame (f);
9172 /* XEmbed implementation. */
9174 void
9175 xembed_set_info (f, flags)
9176 struct frame *f;
9177 enum xembed_info flags;
9179 Atom atom;
9180 unsigned long data[2];
9182 atom = XInternAtom (FRAME_X_DISPLAY (f), "_XEMBED_INFO", False);
9184 data[0] = XEMBED_VERSION;
9185 data[1] = flags;
9187 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), atom, atom,
9188 32, PropModeReplace, (unsigned char *) data, 2);
9191 void
9192 xembed_send_message (f, time, message, detail, data1, data2)
9193 struct frame *f;
9194 Time time;
9195 enum xembed_message message;
9196 long detail;
9197 long data1;
9198 long data2;
9200 XEvent event;
9202 event.xclient.type = ClientMessage;
9203 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9204 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
9205 event.xclient.format = 32;
9206 event.xclient.data.l[0] = time;
9207 event.xclient.data.l[1] = message;
9208 event.xclient.data.l[2] = detail;
9209 event.xclient.data.l[3] = data1;
9210 event.xclient.data.l[4] = data2;
9212 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9213 False, NoEventMask, &event);
9214 XSync (FRAME_X_DISPLAY (f), False);
9217 /* Change of visibility. */
9219 /* This tries to wait until the frame is really visible.
9220 However, if the window manager asks the user where to position
9221 the frame, this will return before the user finishes doing that.
9222 The frame will not actually be visible at that time,
9223 but it will become visible later when the window manager
9224 finishes with it. */
9226 void
9227 x_make_frame_visible (f)
9228 struct frame *f;
9230 Lisp_Object type;
9231 int original_top, original_left;
9232 int retry_count = 2;
9234 retry:
9236 BLOCK_INPUT;
9238 type = x_icon_type (f);
9239 if (!NILP (type))
9240 x_bitmap_icon (f, type);
9242 if (! FRAME_VISIBLE_P (f))
9244 /* We test FRAME_GARBAGED_P here to make sure we don't
9245 call x_set_offset a second time
9246 if we get to x_make_frame_visible a second time
9247 before the window gets really visible. */
9248 if (! FRAME_ICONIFIED_P (f)
9249 && ! FRAME_X_EMBEDDED_P (f)
9250 && ! f->output_data.x->asked_for_visible)
9251 x_set_offset (f, f->left_pos, f->top_pos, 0);
9253 f->output_data.x->asked_for_visible = 1;
9255 if (! EQ (Vx_no_window_manager, Qt))
9256 x_wm_set_window_state (f, NormalState);
9257 #ifdef USE_X_TOOLKIT
9258 if (FRAME_X_EMBEDDED_P (f))
9259 xembed_set_info (f, XEMBED_MAPPED);
9260 else
9262 /* This was XtPopup, but that did nothing for an iconified frame. */
9263 XtMapWidget (f->output_data.x->widget);
9265 #else /* not USE_X_TOOLKIT */
9266 #ifdef USE_GTK
9267 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9268 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9269 #else
9270 if (FRAME_X_EMBEDDED_P (f))
9271 xembed_set_info (f, XEMBED_MAPPED);
9272 else
9273 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9274 #endif /* not USE_GTK */
9275 #endif /* not USE_X_TOOLKIT */
9278 XFlush (FRAME_X_DISPLAY (f));
9280 /* Synchronize to ensure Emacs knows the frame is visible
9281 before we do anything else. We do this loop with input not blocked
9282 so that incoming events are handled. */
9284 Lisp_Object frame;
9285 int count;
9286 /* This must be before UNBLOCK_INPUT
9287 since events that arrive in response to the actions above
9288 will set it when they are handled. */
9289 int previously_visible = f->output_data.x->has_been_visible;
9291 original_left = f->left_pos;
9292 original_top = f->top_pos;
9294 /* This must come after we set COUNT. */
9295 UNBLOCK_INPUT;
9297 /* We unblock here so that arriving X events are processed. */
9299 /* Now move the window back to where it was "supposed to be".
9300 But don't do it if the gravity is negative.
9301 When the gravity is negative, this uses a position
9302 that is 3 pixels too low. Perhaps that's really the border width.
9304 Don't do this if the window has never been visible before,
9305 because the window manager may choose the position
9306 and we don't want to override it. */
9308 if (! FRAME_VISIBLE_P (f)
9309 && ! FRAME_ICONIFIED_P (f)
9310 && ! FRAME_X_EMBEDDED_P (f)
9311 && f->win_gravity == NorthWestGravity
9312 && previously_visible)
9314 Drawable rootw;
9315 int x, y;
9316 unsigned int width, height, border, depth;
9318 BLOCK_INPUT;
9320 /* On some window managers (such as FVWM) moving an existing
9321 window, even to the same place, causes the window manager
9322 to introduce an offset. This can cause the window to move
9323 to an unexpected location. Check the geometry (a little
9324 slow here) and then verify that the window is in the right
9325 place. If the window is not in the right place, move it
9326 there, and take the potential window manager hit. */
9327 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9328 &rootw, &x, &y, &width, &height, &border, &depth);
9330 if (original_left != x || original_top != y)
9331 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9332 original_left, original_top);
9334 UNBLOCK_INPUT;
9337 XSETFRAME (frame, f);
9339 /* Wait until the frame is visible. Process X events until a
9340 MapNotify event has been seen, or until we think we won't get a
9341 MapNotify at all.. */
9342 for (count = input_signal_count + 10;
9343 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9345 /* Force processing of queued events. */
9346 x_sync (f);
9348 /* Machines that do polling rather than SIGIO have been
9349 observed to go into a busy-wait here. So we'll fake an
9350 alarm signal to let the handler know that there's something
9351 to be read. We used to raise a real alarm, but it seems
9352 that the handler isn't always enabled here. This is
9353 probably a bug. */
9354 if (input_polling_used ())
9356 /* It could be confusing if a real alarm arrives while
9357 processing the fake one. Turn it off and let the
9358 handler reset it. */
9359 extern void poll_for_input_1 P_ ((void));
9360 int old_poll_suppress_count = poll_suppress_count;
9361 poll_suppress_count = 1;
9362 poll_for_input_1 ();
9363 poll_suppress_count = old_poll_suppress_count;
9366 /* See if a MapNotify event has been processed. */
9367 FRAME_SAMPLE_VISIBILITY (f);
9370 /* 2000-09-28: In
9372 (let ((f (selected-frame)))
9373 (iconify-frame f)
9374 (raise-frame f))
9376 the frame is not raised with various window managers on
9377 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9378 unknown reason, the call to XtMapWidget is completely ignored.
9379 Mapping the widget a second time works. */
9381 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9382 goto retry;
9386 /* Change from mapped state to withdrawn state. */
9388 /* Make the frame visible (mapped and not iconified). */
9390 void
9391 x_make_frame_invisible (f)
9392 struct frame *f;
9394 Window window;
9396 /* Use the frame's outermost window, not the one we normally draw on. */
9397 window = FRAME_OUTER_WINDOW (f);
9399 /* Don't keep the highlight on an invisible frame. */
9400 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9401 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9403 BLOCK_INPUT;
9405 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9406 that the current position of the window is user-specified, rather than
9407 program-specified, so that when the window is mapped again, it will be
9408 placed at the same location, without forcing the user to position it
9409 by hand again (they have already done that once for this window.) */
9410 x_wm_set_size_hint (f, (long) 0, 1);
9412 #ifdef USE_GTK
9413 if (FRAME_GTK_OUTER_WIDGET (f))
9414 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9415 else
9416 #else
9417 if (FRAME_X_EMBEDDED_P (f))
9418 xembed_set_info (f, 0);
9419 else
9420 #endif
9423 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9424 DefaultScreen (FRAME_X_DISPLAY (f))))
9426 UNBLOCK_INPUT_RESIGNAL;
9427 error ("Can't notify window manager of window withdrawal");
9431 /* We can't distinguish this from iconification
9432 just by the event that we get from the server.
9433 So we can't win using the usual strategy of letting
9434 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9435 and synchronize with the server to make sure we agree. */
9436 f->visible = 0;
9437 FRAME_ICONIFIED_P (f) = 0;
9438 f->async_visible = 0;
9439 f->async_iconified = 0;
9441 x_sync (f);
9443 UNBLOCK_INPUT;
9446 /* Change window state from mapped to iconified. */
9448 void
9449 x_iconify_frame (f)
9450 struct frame *f;
9452 int result;
9453 Lisp_Object type;
9455 /* Don't keep the highlight on an invisible frame. */
9456 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9457 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9459 if (f->async_iconified)
9460 return;
9462 BLOCK_INPUT;
9464 FRAME_SAMPLE_VISIBILITY (f);
9466 type = x_icon_type (f);
9467 if (!NILP (type))
9468 x_bitmap_icon (f, type);
9470 #ifdef USE_GTK
9471 if (FRAME_GTK_OUTER_WIDGET (f))
9473 if (! FRAME_VISIBLE_P (f))
9474 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9476 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9477 f->iconified = 1;
9478 f->visible = 1;
9479 f->async_iconified = 1;
9480 f->async_visible = 0;
9481 UNBLOCK_INPUT;
9482 return;
9484 #endif
9486 #ifdef USE_X_TOOLKIT
9488 if (! FRAME_VISIBLE_P (f))
9490 if (! EQ (Vx_no_window_manager, Qt))
9491 x_wm_set_window_state (f, IconicState);
9492 /* This was XtPopup, but that did nothing for an iconified frame. */
9493 XtMapWidget (f->output_data.x->widget);
9494 /* The server won't give us any event to indicate
9495 that an invisible frame was changed to an icon,
9496 so we have to record it here. */
9497 f->iconified = 1;
9498 f->visible = 1;
9499 f->async_iconified = 1;
9500 f->async_visible = 0;
9501 UNBLOCK_INPUT;
9502 return;
9505 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9506 XtWindow (f->output_data.x->widget),
9507 DefaultScreen (FRAME_X_DISPLAY (f)));
9508 UNBLOCK_INPUT;
9510 if (!result)
9511 error ("Can't notify window manager of iconification");
9513 f->async_iconified = 1;
9514 f->async_visible = 0;
9517 BLOCK_INPUT;
9518 XFlush (FRAME_X_DISPLAY (f));
9519 UNBLOCK_INPUT;
9520 #else /* not USE_X_TOOLKIT */
9522 /* Make sure the X server knows where the window should be positioned,
9523 in case the user deiconifies with the window manager. */
9524 if (! FRAME_VISIBLE_P (f)
9525 && ! FRAME_ICONIFIED_P (f)
9526 && ! FRAME_X_EMBEDDED_P (f))
9527 x_set_offset (f, f->left_pos, f->top_pos, 0);
9529 /* Since we don't know which revision of X we're running, we'll use both
9530 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9532 /* X11R4: send a ClientMessage to the window manager using the
9533 WM_CHANGE_STATE type. */
9535 XEvent message;
9537 message.xclient.window = FRAME_X_WINDOW (f);
9538 message.xclient.type = ClientMessage;
9539 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9540 message.xclient.format = 32;
9541 message.xclient.data.l[0] = IconicState;
9543 if (! XSendEvent (FRAME_X_DISPLAY (f),
9544 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9545 False,
9546 SubstructureRedirectMask | SubstructureNotifyMask,
9547 &message))
9549 UNBLOCK_INPUT_RESIGNAL;
9550 error ("Can't notify window manager of iconification");
9554 /* X11R3: set the initial_state field of the window manager hints to
9555 IconicState. */
9556 x_wm_set_window_state (f, IconicState);
9558 if (!FRAME_VISIBLE_P (f))
9560 /* If the frame was withdrawn, before, we must map it. */
9561 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9564 f->async_iconified = 1;
9565 f->async_visible = 0;
9567 XFlush (FRAME_X_DISPLAY (f));
9568 UNBLOCK_INPUT;
9569 #endif /* not USE_X_TOOLKIT */
9573 /* Free X resources of frame F. */
9575 void
9576 x_free_frame_resources (f)
9577 struct frame *f;
9579 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9580 Lisp_Object bar;
9581 struct scroll_bar *b;
9583 BLOCK_INPUT;
9585 /* If a display connection is dead, don't try sending more
9586 commands to the X server. */
9587 if (dpyinfo->display)
9589 /* We must free faces before destroying windows because some
9590 font-driver (e.g. xft) access a window while finishing a
9591 face. */
9592 if (FRAME_FACE_CACHE (f))
9593 free_frame_faces (f);
9595 if (f->output_data.x->icon_desc)
9596 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9598 #ifdef USE_X_TOOLKIT
9599 /* Explicitly destroy the scroll bars of the frame. Without
9600 this, we get "BadDrawable" errors from the toolkit later on,
9601 presumably from expose events generated for the disappearing
9602 toolkit scroll bars. */
9603 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9605 b = XSCROLL_BAR (bar);
9606 x_scroll_bar_remove (b);
9608 #endif
9610 #ifdef HAVE_X_I18N
9611 if (FRAME_XIC (f))
9612 free_frame_xic (f);
9613 #endif
9615 #ifdef USE_X_TOOLKIT
9616 if (f->output_data.x->widget)
9618 XtDestroyWidget (f->output_data.x->widget);
9619 f->output_data.x->widget = NULL;
9621 /* Tooltips don't have widgets, only a simple X window, even if
9622 we are using a toolkit. */
9623 else if (FRAME_X_WINDOW (f))
9624 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9626 free_frame_menubar (f);
9627 #else /* !USE_X_TOOLKIT */
9629 #ifdef USE_GTK
9630 /* In the GTK version, tooltips are normal X
9631 frames. We must check and free both types. */
9632 if (FRAME_GTK_OUTER_WIDGET (f))
9634 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9635 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9636 FRAME_GTK_OUTER_WIDGET (f) = 0;
9638 #endif /* USE_GTK */
9640 if (FRAME_X_WINDOW (f))
9641 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9642 #endif /* !USE_X_TOOLKIT */
9644 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9645 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9646 unload_color (f, f->output_data.x->cursor_pixel);
9647 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9648 unload_color (f, f->output_data.x->border_pixel);
9649 unload_color (f, f->output_data.x->mouse_pixel);
9651 if (f->output_data.x->scroll_bar_background_pixel != -1)
9652 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9653 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9654 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9655 #ifdef USE_TOOLKIT_SCROLL_BARS
9656 /* Scrollbar shadow colors. */
9657 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9658 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9659 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9660 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9661 #endif /* USE_TOOLKIT_SCROLL_BARS */
9662 if (f->output_data.x->white_relief.allocated_p)
9663 unload_color (f, f->output_data.x->white_relief.pixel);
9664 if (f->output_data.x->black_relief.allocated_p)
9665 unload_color (f, f->output_data.x->black_relief.pixel);
9667 x_free_gcs (f);
9668 XFlush (FRAME_X_DISPLAY (f));
9671 xfree (f->output_data.x->saved_menu_event);
9672 xfree (f->output_data.x);
9673 f->output_data.x = NULL;
9675 if (f == dpyinfo->x_focus_frame)
9676 dpyinfo->x_focus_frame = 0;
9677 if (f == dpyinfo->x_focus_event_frame)
9678 dpyinfo->x_focus_event_frame = 0;
9679 if (f == dpyinfo->x_highlight_frame)
9680 dpyinfo->x_highlight_frame = 0;
9682 if (f == dpyinfo->mouse_face_mouse_frame)
9684 dpyinfo->mouse_face_beg_row
9685 = dpyinfo->mouse_face_beg_col = -1;
9686 dpyinfo->mouse_face_end_row
9687 = dpyinfo->mouse_face_end_col = -1;
9688 dpyinfo->mouse_face_window = Qnil;
9689 dpyinfo->mouse_face_deferred_gc = 0;
9690 dpyinfo->mouse_face_mouse_frame = 0;
9693 UNBLOCK_INPUT;
9697 /* Destroy the X window of frame F. */
9699 void
9700 x_destroy_window (f)
9701 struct frame *f;
9703 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9705 /* If a display connection is dead, don't try sending more
9706 commands to the X server. */
9707 if (dpyinfo->display != 0)
9708 x_free_frame_resources (f);
9710 dpyinfo->reference_count--;
9714 /* Setting window manager hints. */
9716 /* Set the normal size hints for the window manager, for frame F.
9717 FLAGS is the flags word to use--or 0 meaning preserve the flags
9718 that the window now has.
9719 If USER_POSITION is nonzero, we set the USPosition
9720 flag (this is useful when FLAGS is 0).
9721 The GTK version is in gtkutils.c */
9723 #ifndef USE_GTK
9724 void
9725 x_wm_set_size_hint (f, flags, user_position)
9726 struct frame *f;
9727 long flags;
9728 int user_position;
9730 XSizeHints size_hints;
9731 Window window = FRAME_OUTER_WINDOW (f);
9733 /* Setting PMaxSize caused various problems. */
9734 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9736 size_hints.x = f->left_pos;
9737 size_hints.y = f->top_pos;
9739 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9740 size_hints.width = FRAME_PIXEL_WIDTH (f);
9742 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9743 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9744 size_hints.max_width = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
9745 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9746 size_hints.max_height = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
9747 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9749 /* Calculate the base and minimum sizes. */
9751 int base_width, base_height;
9752 int min_rows = 0, min_cols = 0;
9754 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9755 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9757 check_frame_size (f, &min_rows, &min_cols);
9759 /* The window manager uses the base width hints to calculate the
9760 current number of rows and columns in the frame while
9761 resizing; min_width and min_height aren't useful for this
9762 purpose, since they might not give the dimensions for a
9763 zero-row, zero-column frame.
9765 We use the base_width and base_height members if we have
9766 them; otherwise, we set the min_width and min_height members
9767 to the size for a zero x zero frame. */
9769 size_hints.flags |= PBaseSize;
9770 size_hints.base_width = base_width;
9771 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
9772 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9773 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9776 /* If we don't need the old flags, we don't need the old hint at all. */
9777 if (flags)
9779 size_hints.flags |= flags;
9780 goto no_read;
9784 XSizeHints hints; /* Sometimes I hate X Windows... */
9785 long supplied_return;
9786 int value;
9788 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9789 &supplied_return);
9791 if (flags)
9792 size_hints.flags |= flags;
9793 else
9795 if (value == 0)
9796 hints.flags = 0;
9797 if (hints.flags & PSize)
9798 size_hints.flags |= PSize;
9799 if (hints.flags & PPosition)
9800 size_hints.flags |= PPosition;
9801 if (hints.flags & USPosition)
9802 size_hints.flags |= USPosition;
9803 if (hints.flags & USSize)
9804 size_hints.flags |= USSize;
9808 no_read:
9810 #ifdef PWinGravity
9811 size_hints.win_gravity = f->win_gravity;
9812 size_hints.flags |= PWinGravity;
9814 if (user_position)
9816 size_hints.flags &= ~ PPosition;
9817 size_hints.flags |= USPosition;
9819 #endif /* PWinGravity */
9821 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9823 #endif /* not USE_GTK */
9825 /* Used for IconicState or NormalState */
9827 void
9828 x_wm_set_window_state (f, state)
9829 struct frame *f;
9830 int state;
9832 #ifdef USE_X_TOOLKIT
9833 Arg al[1];
9835 XtSetArg (al[0], XtNinitialState, state);
9836 XtSetValues (f->output_data.x->widget, al, 1);
9837 #else /* not USE_X_TOOLKIT */
9838 Window window = FRAME_X_WINDOW (f);
9840 f->output_data.x->wm_hints.flags |= StateHint;
9841 f->output_data.x->wm_hints.initial_state = state;
9843 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9844 #endif /* not USE_X_TOOLKIT */
9847 void
9848 x_wm_set_icon_pixmap (f, pixmap_id)
9849 struct frame *f;
9850 int pixmap_id;
9852 Pixmap icon_pixmap, icon_mask;
9854 #if !defined USE_X_TOOLKIT && !defined USE_GTK
9855 Window window = FRAME_OUTER_WINDOW (f);
9856 #endif
9858 if (pixmap_id > 0)
9860 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9861 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9862 icon_mask = x_bitmap_mask (f, pixmap_id);
9863 f->output_data.x->wm_hints.icon_mask = icon_mask;
9865 else
9867 /* It seems there is no way to turn off use of an icon
9868 pixmap. */
9869 return;
9873 #ifdef USE_GTK
9875 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9876 return;
9879 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9882 Arg al[1];
9883 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9884 XtSetValues (f->output_data.x->widget, al, 1);
9885 XtSetArg (al[0], XtNiconMask, icon_mask);
9886 XtSetValues (f->output_data.x->widget, al, 1);
9889 #else /* not USE_X_TOOLKIT && not USE_GTK */
9891 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9892 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9894 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9897 void
9898 x_wm_set_icon_position (f, icon_x, icon_y)
9899 struct frame *f;
9900 int icon_x, icon_y;
9902 Window window = FRAME_OUTER_WINDOW (f);
9904 f->output_data.x->wm_hints.flags |= IconPositionHint;
9905 f->output_data.x->wm_hints.icon_x = icon_x;
9906 f->output_data.x->wm_hints.icon_y = icon_y;
9908 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9912 /***********************************************************************
9913 Fonts
9914 ***********************************************************************/
9916 #if GLYPH_DEBUG
9918 /* Check that FONT is valid on frame F. It is if it can be found in F's
9919 font table. */
9921 static void
9922 x_check_font (f, font)
9923 struct frame *f;
9924 struct font *font;
9926 Lisp_Object frame;
9928 xassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9929 if (font->driver->check)
9930 xassert (font->driver->check (f, font) == 0);
9933 #endif /* GLYPH_DEBUG != 0 */
9936 /***********************************************************************
9937 Initialization
9938 ***********************************************************************/
9940 #ifdef USE_X_TOOLKIT
9941 static XrmOptionDescRec emacs_options[] = {
9942 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9943 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9945 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9946 XrmoptionSepArg, NULL},
9947 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9949 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9950 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9951 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9952 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9953 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9954 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9955 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9958 /* Whether atimer for Xt timeouts is activated or not. */
9960 static int x_timeout_atimer_activated_flag;
9962 #endif /* USE_X_TOOLKIT */
9964 static int x_initialized;
9966 #ifdef HAVE_X_SM
9967 static int x_session_initialized;
9968 #endif
9970 /* Test whether two display-name strings agree up to the dot that separates
9971 the screen number from the server number. */
9972 static int
9973 same_x_server (name1, name2)
9974 const char *name1, *name2;
9976 int seen_colon = 0;
9977 const unsigned char *system_name = SDATA (Vsystem_name);
9978 int system_name_length = strlen (system_name);
9979 int length_until_period = 0;
9981 while (system_name[length_until_period] != 0
9982 && system_name[length_until_period] != '.')
9983 length_until_period++;
9985 /* Treat `unix' like an empty host name. */
9986 if (! strncmp (name1, "unix:", 5))
9987 name1 += 4;
9988 if (! strncmp (name2, "unix:", 5))
9989 name2 += 4;
9990 /* Treat this host's name like an empty host name. */
9991 if (! strncmp (name1, system_name, system_name_length)
9992 && name1[system_name_length] == ':')
9993 name1 += system_name_length;
9994 if (! strncmp (name2, system_name, system_name_length)
9995 && name2[system_name_length] == ':')
9996 name2 += system_name_length;
9997 /* Treat this host's domainless name like an empty host name. */
9998 if (! strncmp (name1, system_name, length_until_period)
9999 && name1[length_until_period] == ':')
10000 name1 += length_until_period;
10001 if (! strncmp (name2, system_name, length_until_period)
10002 && name2[length_until_period] == ':')
10003 name2 += length_until_period;
10005 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10007 if (*name1 == ':')
10008 seen_colon++;
10009 if (seen_colon && *name1 == '.')
10010 return 1;
10012 return (seen_colon
10013 && (*name1 == '.' || *name1 == '\0')
10014 && (*name2 == '.' || *name2 == '\0'));
10017 /* Count number of set bits in mask and number of bits to shift to
10018 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10019 to 5. */
10020 static void
10021 get_bits_and_offset (mask, bits, offset)
10022 unsigned long mask;
10023 int *bits;
10024 int *offset;
10026 int nr = 0;
10027 int off = 0;
10029 while (!(mask & 1))
10031 off++;
10032 mask >>= 1;
10035 while (mask & 1)
10037 nr++;
10038 mask >>= 1;
10041 *offset = off;
10042 *bits = nr;
10045 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
10046 But don't permanently open it, just test its availability. */
10049 x_display_ok (display)
10050 const char *display;
10052 int dpy_ok = 1;
10053 Display *dpy;
10055 dpy = XOpenDisplay (display);
10056 if (dpy)
10057 XCloseDisplay (dpy);
10058 else
10059 dpy_ok = 0;
10060 return dpy_ok;
10063 #ifdef USE_GTK
10064 static void
10065 my_log_handler (log_domain, log_level, message, user_data)
10066 const gchar *log_domain;
10067 GLogLevelFlags log_level;
10068 const gchar *message;
10069 gpointer user_data;
10071 if (!strstr (message, "g_set_prgname"))
10072 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, message);
10074 #endif
10076 /* Open a connection to X display DISPLAY_NAME, and return
10077 the structure that describes the open display.
10078 If we cannot contact the display, return null. */
10080 struct x_display_info *
10081 x_term_init (display_name, xrm_option, resource_name)
10082 Lisp_Object display_name;
10083 char *xrm_option;
10084 char *resource_name;
10086 int connection;
10087 Display *dpy;
10088 struct terminal *terminal;
10089 struct x_display_info *dpyinfo;
10090 XrmDatabase xrdb;
10092 BLOCK_INPUT;
10094 if (!x_initialized)
10096 x_initialize ();
10097 ++x_initialized;
10100 if (! x_display_ok (SDATA (display_name)))
10101 error ("Display %s can't be opened", SDATA (display_name));
10103 #ifdef USE_GTK
10105 #define NUM_ARGV 10
10106 int argc;
10107 char *argv[NUM_ARGV];
10108 char **argv2 = argv;
10109 guint id;
10110 #ifndef HAVE_GTK_MULTIDISPLAY
10111 if (!EQ (Vinitial_window_system, Qx))
10112 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10113 #endif
10115 if (x_initialized++ > 1)
10117 #ifdef HAVE_GTK_MULTIDISPLAY
10118 /* Opening another display. If xg_display_open returns less
10119 than zero, we are probably on GTK 2.0, which can only handle
10120 one display. GTK 2.2 or later can handle more than one. */
10121 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10122 #endif
10123 error ("Sorry, this version of GTK can only handle one display");
10125 else
10127 for (argc = 0; argc < NUM_ARGV; ++argc)
10128 argv[argc] = 0;
10130 argc = 0;
10131 argv[argc++] = initial_argv[0];
10133 if (! NILP (display_name))
10135 argv[argc++] = "--display";
10136 argv[argc++] = SDATA (display_name);
10139 argv[argc++] = "--name";
10140 argv[argc++] = resource_name;
10142 XSetLocaleModifiers ("");
10144 /* Work around GLib bug that outputs a faulty warning. See
10145 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
10146 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
10147 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
10148 gtk_init (&argc, &argv2);
10149 g_log_remove_handler ("GLib", id);
10151 /* gtk_init does set_locale. We must fix locale after calling it. */
10152 fixup_locale ();
10153 xg_initialize ();
10155 dpy = GDK_DISPLAY ();
10157 /* NULL window -> events for all windows go to our function */
10158 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10160 /* Load our own gtkrc if it exists. */
10162 char *file = "~/.emacs.d/gtkrc";
10163 Lisp_Object s, abs_file;
10165 s = make_string (file, strlen (file));
10166 abs_file = Fexpand_file_name (s, Qnil);
10168 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10169 gtk_rc_parse (SDATA (abs_file));
10172 XSetErrorHandler (x_error_handler);
10173 XSetIOErrorHandler (x_io_error_quitter);
10176 #else /* not USE_GTK */
10177 #ifdef USE_X_TOOLKIT
10178 /* weiner@footloose.sps.mot.com reports that this causes
10179 errors with X11R5:
10180 X protocol error: BadAtom (invalid Atom parameter)
10181 on protocol request 18skiloaf.
10182 So let's not use it until R6. */
10183 #ifdef HAVE_X11XTR6
10184 XtSetLanguageProc (NULL, NULL, NULL);
10185 #endif
10188 int argc = 0;
10189 char *argv[3];
10191 argv[0] = "";
10192 argc = 1;
10193 if (xrm_option)
10195 argv[argc++] = "-xrm";
10196 argv[argc++] = xrm_option;
10198 turn_on_atimers (0);
10199 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10200 resource_name, EMACS_CLASS,
10201 emacs_options, XtNumber (emacs_options),
10202 &argc, argv);
10203 turn_on_atimers (1);
10205 #ifdef HAVE_X11XTR6
10206 /* I think this is to compensate for XtSetLanguageProc. */
10207 fixup_locale ();
10208 #endif
10211 #else /* not USE_X_TOOLKIT */
10212 XSetLocaleModifiers ("");
10213 dpy = XOpenDisplay (SDATA (display_name));
10214 #endif /* not USE_X_TOOLKIT */
10215 #endif /* not USE_GTK*/
10217 /* Detect failure. */
10218 if (dpy == 0)
10220 UNBLOCK_INPUT;
10221 return 0;
10224 /* We have definitely succeeded. Record the new connection. */
10226 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10227 bzero (dpyinfo, sizeof *dpyinfo);
10229 terminal = x_create_terminal (dpyinfo);
10232 struct x_display_info *share;
10233 Lisp_Object tail;
10235 for (share = x_display_list, tail = x_display_name_list; share;
10236 share = share->next, tail = XCDR (tail))
10237 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10238 SDATA (display_name)))
10239 break;
10240 if (share)
10241 terminal->kboard = share->terminal->kboard;
10242 else
10244 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10245 init_kboard (terminal->kboard);
10246 terminal->kboard->Vwindow_system = Qx;
10248 /* Add the keyboard to the list before running Lisp code (via
10249 Qvendor_specific_keysyms below), since these are not traced
10250 via terminals but only through all_kboards. */
10251 terminal->kboard->next_kboard = all_kboards;
10252 all_kboards = terminal->kboard;
10254 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10256 char *vendor = ServerVendor (dpy);
10258 /* Protect terminal from GC before removing it from the
10259 list of terminals. */
10260 struct gcpro gcpro1;
10261 Lisp_Object gcpro_term;
10262 XSETTERMINAL (gcpro_term, terminal);
10263 GCPRO1 (gcpro_term);
10265 /* Temporarily hide the partially initialized terminal. */
10266 terminal_list = terminal->next_terminal;
10267 UNBLOCK_INPUT;
10268 terminal->kboard->Vsystem_key_alist
10269 = call1 (Qvendor_specific_keysyms,
10270 vendor ? build_string (vendor) : empty_unibyte_string);
10271 BLOCK_INPUT;
10272 terminal->next_terminal = terminal_list;
10273 terminal_list = terminal;
10274 UNGCPRO;
10277 /* Don't let the initial kboard remain current longer than necessary.
10278 That would cause problems if a file loaded on startup tries to
10279 prompt in the mini-buffer. */
10280 if (current_kboard == initial_kboard)
10281 current_kboard = terminal->kboard;
10283 terminal->kboard->reference_count++;
10286 /* Put this display on the chain. */
10287 dpyinfo->next = x_display_list;
10288 x_display_list = dpyinfo;
10290 /* Put it on x_display_name_list as well, to keep them parallel. */
10291 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10292 x_display_name_list);
10293 dpyinfo->name_list_element = XCAR (x_display_name_list);
10295 dpyinfo->display = dpy;
10297 /* Set the name of the terminal. */
10298 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10299 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10300 terminal->name[SBYTES (display_name)] = 0;
10302 #if 0
10303 XSetAfterFunction (x_current_display, x_trace_wire);
10304 #endif /* ! 0 */
10306 dpyinfo->x_id_name
10307 = (char *) xmalloc (SBYTES (Vinvocation_name)
10308 + SBYTES (Vsystem_name)
10309 + 2);
10310 sprintf (dpyinfo->x_id_name, "%s@%s",
10311 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10313 /* Figure out which modifier bits mean what. */
10314 x_find_modifier_meanings (dpyinfo);
10316 /* Get the scroll bar cursor. */
10317 #ifdef USE_GTK
10318 /* We must create a GTK cursor, it is required for GTK widgets. */
10319 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10320 #endif /* USE_GTK */
10322 dpyinfo->vertical_scroll_bar_cursor
10323 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10325 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10326 resource_name, EMACS_CLASS);
10327 #ifdef HAVE_XRMSETDATABASE
10328 XrmSetDatabase (dpyinfo->display, xrdb);
10329 #else
10330 dpyinfo->display->db = xrdb;
10331 #endif
10332 /* Put the rdb where we can find it in a way that works on
10333 all versions. */
10334 dpyinfo->xrdb = xrdb;
10336 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10337 DefaultScreen (dpyinfo->display));
10338 select_visual (dpyinfo);
10339 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10340 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10341 dpyinfo->client_leader_window = 0;
10342 dpyinfo->grabbed = 0;
10343 dpyinfo->reference_count = 0;
10344 dpyinfo->icon_bitmap_id = -1;
10345 dpyinfo->n_fonts = 0;
10346 dpyinfo->bitmaps = 0;
10347 dpyinfo->bitmaps_size = 0;
10348 dpyinfo->bitmaps_last = 0;
10349 dpyinfo->scratch_cursor_gc = 0;
10350 dpyinfo->mouse_face_mouse_frame = 0;
10351 dpyinfo->mouse_face_deferred_gc = 0;
10352 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10353 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10354 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10355 dpyinfo->mouse_face_window = Qnil;
10356 dpyinfo->mouse_face_overlay = Qnil;
10357 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10358 dpyinfo->mouse_face_defer = 0;
10359 dpyinfo->mouse_face_hidden = 0;
10360 dpyinfo->x_focus_frame = 0;
10361 dpyinfo->x_focus_event_frame = 0;
10362 dpyinfo->x_highlight_frame = 0;
10363 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10365 /* See if we can construct pixel values from RGB values. */
10366 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10367 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10369 if (dpyinfo->visual->class == TrueColor)
10371 get_bits_and_offset (dpyinfo->visual->red_mask,
10372 &dpyinfo->red_bits, &dpyinfo->red_offset);
10373 get_bits_and_offset (dpyinfo->visual->blue_mask,
10374 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10375 get_bits_and_offset (dpyinfo->visual->green_mask,
10376 &dpyinfo->green_bits, &dpyinfo->green_offset);
10379 /* See if a private colormap is requested. */
10380 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10382 if (dpyinfo->visual->class == PseudoColor)
10384 Lisp_Object value;
10385 value = display_x_get_resource (dpyinfo,
10386 build_string ("privateColormap"),
10387 build_string ("PrivateColormap"),
10388 Qnil, Qnil);
10389 if (STRINGP (value)
10390 && (!strcmp (SDATA (value), "true")
10391 || !strcmp (SDATA (value), "on")))
10392 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10395 else
10396 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10397 dpyinfo->visual, AllocNone);
10399 #ifdef HAVE_XFT
10401 /* If we are using Xft, check dpi value in X resources.
10402 It is better we use it as well, since Xft will use it, as will all
10403 Gnome applications. If our real DPI is smaller or larger than the
10404 one Xft uses, our font will look smaller or larger than other
10405 for other applications, even if it is the same font name (monospace-10
10406 for example). */
10407 char *v = XGetDefault (dpyinfo->display, "Xft", "dpi");
10408 double d;
10409 if (v != NULL && sscanf (v, "%lf", &d) == 1)
10410 dpyinfo->resy = dpyinfo->resx = d;
10412 #endif
10414 if (dpyinfo->resy < 1)
10416 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10417 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10418 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10419 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10420 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10421 pixels = DisplayWidth (dpyinfo->display, screen_number);
10422 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10423 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10424 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10427 dpyinfo->Xatom_wm_protocols
10428 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10429 dpyinfo->Xatom_wm_take_focus
10430 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10431 dpyinfo->Xatom_wm_save_yourself
10432 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10433 dpyinfo->Xatom_wm_delete_window
10434 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10435 dpyinfo->Xatom_wm_change_state
10436 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10437 dpyinfo->Xatom_wm_configure_denied
10438 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10439 dpyinfo->Xatom_wm_window_moved
10440 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10441 dpyinfo->Xatom_wm_client_leader
10442 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10443 dpyinfo->Xatom_editres
10444 = XInternAtom (dpyinfo->display, "Editres", False);
10445 dpyinfo->Xatom_CLIPBOARD
10446 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10447 dpyinfo->Xatom_TIMESTAMP
10448 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10449 dpyinfo->Xatom_TEXT
10450 = XInternAtom (dpyinfo->display, "TEXT", False);
10451 dpyinfo->Xatom_COMPOUND_TEXT
10452 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10453 dpyinfo->Xatom_UTF8_STRING
10454 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10455 dpyinfo->Xatom_DELETE
10456 = XInternAtom (dpyinfo->display, "DELETE", False);
10457 dpyinfo->Xatom_MULTIPLE
10458 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10459 dpyinfo->Xatom_INCR
10460 = XInternAtom (dpyinfo->display, "INCR", False);
10461 dpyinfo->Xatom_EMACS_TMP
10462 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10463 dpyinfo->Xatom_TARGETS
10464 = XInternAtom (dpyinfo->display, "TARGETS", False);
10465 dpyinfo->Xatom_NULL
10466 = XInternAtom (dpyinfo->display, "NULL", False);
10467 dpyinfo->Xatom_ATOM_PAIR
10468 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10469 /* For properties of font. */
10470 dpyinfo->Xatom_PIXEL_SIZE
10471 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10472 dpyinfo->Xatom_AVERAGE_WIDTH
10473 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10474 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10475 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10476 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10477 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10478 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10479 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10481 /* Ghostscript support. */
10482 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10483 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10485 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10486 False);
10488 dpyinfo->Xatom_XEMBED = XInternAtom (dpyinfo->display, "_XEMBED",
10489 False);
10491 dpyinfo->Xatom_net_wm_state
10492 = XInternAtom (dpyinfo->display, "_NET_WM_STATE", False);
10493 dpyinfo->Xatom_net_wm_state_fullscreen_atom
10494 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_FULLSCREEN", False);
10495 dpyinfo->Xatom_net_wm_state_maximized_horz
10496 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
10497 dpyinfo->Xatom_net_wm_state_maximized_vert
10498 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
10499 dpyinfo->Xatom_net_wm_state_sticky
10500 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_STICKY", False);
10501 dpyinfo->Xatom_net_window_type
10502 = XInternAtom (dpyinfo->display, "_NET_WM_WINDOW_TYPE", False);
10503 dpyinfo->Xatom_net_window_type_tooltip
10504 = XInternAtom (dpyinfo->display, "_NET_WM_WINDOW_TYPE_TOOLTIP", False);
10506 dpyinfo->cut_buffers_initialized = 0;
10508 dpyinfo->x_dnd_atoms_size = 8;
10509 dpyinfo->x_dnd_atoms_length = 0;
10510 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10511 * dpyinfo->x_dnd_atoms_size);
10513 dpyinfo->net_supported_atoms = NULL;
10514 dpyinfo->nr_net_supported_atoms = 0;
10515 dpyinfo->net_supported_window = 0;
10517 connection = ConnectionNumber (dpyinfo->display);
10518 dpyinfo->connection = connection;
10521 extern int gray_bitmap_width, gray_bitmap_height;
10522 extern char *gray_bitmap_bits;
10523 dpyinfo->gray
10524 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10525 gray_bitmap_bits,
10526 gray_bitmap_width, gray_bitmap_height,
10527 (unsigned long) 1, (unsigned long) 0, 1);
10530 #ifdef HAVE_X_I18N
10531 xim_initialize (dpyinfo, resource_name);
10532 #endif
10534 xsettings_initialize (dpyinfo);
10536 #ifdef subprocesses
10537 /* This is only needed for distinguishing keyboard and process input. */
10538 if (connection != 0)
10539 add_keyboard_wait_descriptor (connection);
10540 #endif
10542 #ifdef F_SETOWN
10543 fcntl (connection, F_SETOWN, getpid ());
10544 #endif /* ! defined (F_SETOWN) */
10546 #ifdef SIGIO
10547 if (interrupt_input)
10548 init_sigio (connection);
10549 #endif /* ! defined (SIGIO) */
10551 #ifdef USE_LUCID
10553 Display *dpy = dpyinfo->display;
10554 XrmValue d, fr, to;
10555 Font font;
10557 d.addr = (XPointer)&dpy;
10558 d.size = sizeof (Display *);
10559 fr.addr = XtDefaultFont;
10560 fr.size = sizeof (XtDefaultFont);
10561 to.size = sizeof (Font *);
10562 to.addr = (XPointer)&font;
10563 x_catch_errors (dpy);
10564 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10565 abort ();
10566 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10567 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10568 x_uncatch_errors ();
10570 #endif
10572 /* See if we should run in synchronous mode. This is useful
10573 for debugging X code. */
10575 Lisp_Object value;
10576 value = display_x_get_resource (dpyinfo,
10577 build_string ("synchronous"),
10578 build_string ("Synchronous"),
10579 Qnil, Qnil);
10580 if (STRINGP (value)
10581 && (!strcmp (SDATA (value), "true")
10582 || !strcmp (SDATA (value), "on")))
10583 XSynchronize (dpyinfo->display, True);
10587 Lisp_Object value;
10588 value = display_x_get_resource (dpyinfo,
10589 build_string ("useXIM"),
10590 build_string ("UseXIM"),
10591 Qnil, Qnil);
10592 #ifdef USE_XIM
10593 if (STRINGP (value)
10594 && (!strcmp (SDATA (value), "false")
10595 || !strcmp (SDATA (value), "off")))
10596 use_xim = 0;
10597 #else
10598 if (STRINGP (value)
10599 && (!strcmp (SDATA (value), "true")
10600 || !strcmp (SDATA (value), "on")))
10601 use_xim = 1;
10602 #endif
10605 #ifdef HAVE_X_SM
10606 /* Only do this for the very first display in the Emacs session.
10607 Ignore X session management when Emacs was first started on a
10608 tty. */
10609 if (terminal->id == 1)
10610 x_session_initialize (dpyinfo);
10611 #endif
10613 UNBLOCK_INPUT;
10615 return dpyinfo;
10618 /* Get rid of display DPYINFO, deleting all frames on it,
10619 and without sending any more commands to the X server. */
10621 void
10622 x_delete_display (dpyinfo)
10623 struct x_display_info *dpyinfo;
10625 struct terminal *t;
10627 /* Close all frames and delete the generic struct terminal for this
10628 X display. */
10629 for (t = terminal_list; t; t = t->next_terminal)
10630 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10632 #ifdef HAVE_X_SM
10633 /* Close X session management when we close its display. */
10634 if (t->id == 1 && x_session_have_connection ())
10635 x_session_close();
10636 #endif
10637 delete_terminal (t);
10638 break;
10641 delete_keyboard_wait_descriptor (dpyinfo->connection);
10643 /* Discard this display from x_display_name_list and x_display_list.
10644 We can't use Fdelq because that can quit. */
10645 if (! NILP (x_display_name_list)
10646 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10647 x_display_name_list = XCDR (x_display_name_list);
10648 else
10650 Lisp_Object tail;
10652 tail = x_display_name_list;
10653 while (CONSP (tail) && CONSP (XCDR (tail)))
10655 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10657 XSETCDR (tail, XCDR (XCDR (tail)));
10658 break;
10660 tail = XCDR (tail);
10664 if (next_noop_dpyinfo == dpyinfo)
10665 next_noop_dpyinfo = dpyinfo->next;
10667 if (x_display_list == dpyinfo)
10668 x_display_list = dpyinfo->next;
10669 else
10671 struct x_display_info *tail;
10673 for (tail = x_display_list; tail; tail = tail->next)
10674 if (tail->next == dpyinfo)
10675 tail->next = tail->next->next;
10678 xfree (dpyinfo->x_id_name);
10679 xfree (dpyinfo->x_dnd_atoms);
10680 xfree (dpyinfo->color_cells);
10681 xfree (dpyinfo);
10684 #ifdef USE_X_TOOLKIT
10686 /* Atimer callback function for TIMER. Called every 0.1s to process
10687 Xt timeouts, if needed. We must avoid calling XtAppPending as
10688 much as possible because that function does an implicit XFlush
10689 that slows us down. */
10691 static void
10692 x_process_timeouts (timer)
10693 struct atimer *timer;
10695 BLOCK_INPUT;
10696 x_timeout_atimer_activated_flag = 0;
10697 if (toolkit_scroll_bar_interaction || popup_activated ())
10699 while (XtAppPending (Xt_app_con) & XtIMTimer)
10700 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10701 /* Reactivate the atimer for next time. */
10702 x_activate_timeout_atimer ();
10704 UNBLOCK_INPUT;
10707 /* Install an asynchronous timer that processes Xt timeout events
10708 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10709 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10710 function whenever these variables are set. This is necessary
10711 because some widget sets use timeouts internally, for example the
10712 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10713 processed, these widgets don't behave normally. */
10715 void
10716 x_activate_timeout_atimer ()
10718 BLOCK_INPUT;
10719 if (!x_timeout_atimer_activated_flag)
10721 EMACS_TIME interval;
10723 EMACS_SET_SECS_USECS (interval, 0, 100000);
10724 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10725 x_timeout_atimer_activated_flag = 1;
10727 UNBLOCK_INPUT;
10730 #endif /* USE_X_TOOLKIT */
10733 /* Set up use of X before we make the first connection. */
10735 extern frame_parm_handler x_frame_parm_handlers[];
10737 static struct redisplay_interface x_redisplay_interface =
10739 x_frame_parm_handlers,
10740 x_produce_glyphs,
10741 x_write_glyphs,
10742 x_insert_glyphs,
10743 x_clear_end_of_line,
10744 x_scroll_run,
10745 x_after_update_window_line,
10746 x_update_window_begin,
10747 x_update_window_end,
10748 x_cursor_to,
10749 x_flush,
10750 #ifdef XFlush
10751 x_flush,
10752 #else
10753 0, /* flush_display_optional */
10754 #endif
10755 x_clear_window_mouse_face,
10756 x_get_glyph_overhangs,
10757 x_fix_overlapping_area,
10758 x_draw_fringe_bitmap,
10759 0, /* define_fringe_bitmap */
10760 0, /* destroy_fringe_bitmap */
10761 x_compute_glyph_string_overhangs,
10762 x_draw_glyph_string,
10763 x_define_frame_cursor,
10764 x_clear_frame_area,
10765 x_draw_window_cursor,
10766 x_draw_vertical_window_border,
10767 x_shift_glyphs_for_insert
10771 /* This function is called when the last frame on a display is deleted. */
10772 void
10773 x_delete_terminal (struct terminal *terminal)
10775 struct x_display_info *dpyinfo = terminal->display_info.x;
10777 /* Protect against recursive calls. delete_frame in
10778 delete_terminal calls us back when it deletes our last frame. */
10779 if (!terminal->name)
10780 return;
10782 BLOCK_INPUT;
10783 #ifdef HAVE_X_I18N
10784 /* We must close our connection to the XIM server before closing the
10785 X display. */
10786 if (dpyinfo->xim)
10787 xim_close_dpy (dpyinfo);
10788 #endif
10790 /* If called from x_connection_closed, the display may already be closed
10791 and dpyinfo->display was set to 0 to indicate that. */
10792 if (dpyinfo->display)
10794 x_destroy_all_bitmaps (dpyinfo);
10795 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10797 /* Whether or not XCloseDisplay destroys the associated resource
10798 database depends on the version of libX11. To avoid both
10799 crash and memory leak, we dissociate the database from the
10800 display and then destroy dpyinfo->xrdb ourselves.
10802 Unfortunately, the above strategy does not work in some
10803 situations due to a bug in newer versions of libX11: because
10804 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
10805 dpy->db is NULL, XCloseDisplay destroys the associated
10806 database whereas it has not been created by XGetDefault
10807 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
10808 don't destroy the database here in order to avoid the crash
10809 in the above situations for now, though that may cause memory
10810 leaks in other situations. */
10811 #if 0
10812 #ifdef HAVE_XRMSETDATABASE
10813 XrmSetDatabase (dpyinfo->display, NULL);
10814 #else
10815 dpyinfo->display->db = NULL;
10816 #endif
10817 /* We used to call XrmDestroyDatabase from x_delete_display, but
10818 some older versions of libX11 crash if we call it after
10819 closing all the displays. */
10820 XrmDestroyDatabase (dpyinfo->xrdb);
10821 #endif
10823 #ifdef USE_GTK
10824 xg_display_close (dpyinfo->display);
10825 #else
10826 #ifdef USE_X_TOOLKIT
10827 XtCloseDisplay (dpyinfo->display);
10828 #else
10829 XCloseDisplay (dpyinfo->display);
10830 #endif
10831 #endif /* ! USE_GTK */
10834 /* Mark as dead. */
10835 dpyinfo->display = NULL;
10836 x_delete_display (dpyinfo);
10837 UNBLOCK_INPUT;
10840 /* Create a struct terminal, initialize it with the X11 specific
10841 functions and make DISPLAY->TERMINAL point to it. */
10843 static struct terminal *
10844 x_create_terminal (struct x_display_info *dpyinfo)
10846 struct terminal *terminal;
10848 terminal = create_terminal ();
10850 terminal->type = output_x_window;
10851 terminal->display_info.x = dpyinfo;
10852 dpyinfo->terminal = terminal;
10854 /* kboard is initialized in x_term_init. */
10856 terminal->clear_frame_hook = x_clear_frame;
10857 terminal->ins_del_lines_hook = x_ins_del_lines;
10858 terminal->delete_glyphs_hook = x_delete_glyphs;
10859 terminal->ring_bell_hook = XTring_bell;
10860 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
10861 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
10862 terminal->set_terminal_modes_hook = XTset_terminal_modes;
10863 terminal->update_begin_hook = x_update_begin;
10864 terminal->update_end_hook = x_update_end;
10865 terminal->set_terminal_window_hook = XTset_terminal_window;
10866 terminal->read_socket_hook = XTread_socket;
10867 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10868 terminal->mouse_position_hook = XTmouse_position;
10869 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10870 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10871 terminal->fullscreen_hook = XTfullscreen_hook;
10872 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10873 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10874 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10875 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10877 terminal->delete_frame_hook = x_destroy_window;
10878 terminal->delete_terminal_hook = x_delete_terminal;
10880 terminal->rif = &x_redisplay_interface;
10881 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10882 terminal->char_ins_del_ok = 1;
10883 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10884 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10885 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10886 off the bottom. */
10888 return terminal;
10891 void
10892 x_initialize ()
10894 baud_rate = 19200;
10896 x_noop_count = 0;
10897 last_tool_bar_item = -1;
10898 any_help_event_p = 0;
10899 ignore_next_mouse_click_timeout = 0;
10900 #ifdef HAVE_X_SM
10901 x_session_initialized = 0;
10902 #endif
10904 #ifdef USE_GTK
10905 current_count = -1;
10906 #endif
10908 /* Try to use interrupt input; if we can't, then start polling. */
10909 Fset_input_interrupt_mode (Qt);
10911 #ifdef USE_X_TOOLKIT
10912 XtToolkitInitialize ();
10914 Xt_app_con = XtCreateApplicationContext ();
10916 /* Register a converter from strings to pixels, which uses
10917 Emacs' color allocation infrastructure. */
10918 XtAppSetTypeConverter (Xt_app_con,
10919 XtRString, XtRPixel, cvt_string_to_pixel,
10920 cvt_string_to_pixel_args,
10921 XtNumber (cvt_string_to_pixel_args),
10922 XtCacheByDisplay, cvt_pixel_dtor);
10924 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10925 #endif
10927 #ifdef USE_TOOLKIT_SCROLL_BARS
10928 #ifndef USE_GTK
10929 xaw3d_arrow_scroll = False;
10930 xaw3d_pick_top = True;
10931 #endif
10932 #endif
10934 pending_autoraise_frame = 0;
10935 pending_event_wait.f = 0;
10936 pending_event_wait.eventtype = 0;
10938 /* Note that there is no real way portable across R3/R4 to get the
10939 original error handler. */
10940 XSetErrorHandler (x_error_handler);
10941 XSetIOErrorHandler (x_io_error_quitter);
10943 signal (SIGPIPE, x_connection_signal);
10945 xgselect_initialize ();
10949 void
10950 syms_of_xterm ()
10952 x_error_message = NULL;
10954 staticpro (&x_display_name_list);
10955 x_display_name_list = Qnil;
10957 staticpro (&last_mouse_scroll_bar);
10958 last_mouse_scroll_bar = Qnil;
10960 staticpro (&Qvendor_specific_keysyms);
10961 Qvendor_specific_keysyms = intern_c_string ("vendor-specific-keysyms");
10963 staticpro (&Qlatin_1);
10964 Qlatin_1 = intern_c_string ("latin-1");
10966 staticpro (&last_mouse_press_frame);
10967 last_mouse_press_frame = Qnil;
10969 #ifdef USE_GTK
10970 xg_default_icon_file = make_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
10971 staticpro (&xg_default_icon_file);
10973 Qx_gtk_map_stock = intern_c_string ("x-gtk-map-stock");
10974 staticpro (&Qx_gtk_map_stock);
10975 #endif
10977 DEFVAR_BOOL ("x-use-underline-position-properties",
10978 &x_use_underline_position_properties,
10979 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10980 A value of nil means ignore them. If you encounter fonts with bogus
10981 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10982 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10983 to override the font's UNDERLINE_POSITION for small font display
10984 sizes. */);
10985 x_use_underline_position_properties = 1;
10987 DEFVAR_BOOL ("x-underline-at-descent-line",
10988 &x_underline_at_descent_line,
10989 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
10990 A value of nil means to draw the underline according to the value of the
10991 variable `x-use-underline-position-properties', which is usually at the
10992 baseline level. The default value is nil. */);
10993 x_underline_at_descent_line = 0;
10995 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10996 &x_mouse_click_focus_ignore_position,
10997 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10998 This variable is only used when the window manager requires that you
10999 click on a frame to select it (give it focus). In that case, a value
11000 of nil, means that the selected window and cursor position changes to
11001 reflect the mouse click position, while a non-nil value means that the
11002 selected window or cursor position is preserved. */);
11003 x_mouse_click_focus_ignore_position = 0;
11005 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
11006 doc: /* What X toolkit scroll bars Emacs uses.
11007 A value of nil means Emacs doesn't use X toolkit scroll bars.
11008 Otherwise, value is a symbol describing the X toolkit. */);
11009 #ifdef USE_TOOLKIT_SCROLL_BARS
11010 #ifdef USE_MOTIF
11011 Vx_toolkit_scroll_bars = intern_c_string ("motif");
11012 #elif defined HAVE_XAW3D
11013 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
11014 #elif USE_GTK
11015 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
11016 #else
11017 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
11018 #endif
11019 #else
11020 Vx_toolkit_scroll_bars = Qnil;
11021 #endif
11023 staticpro (&last_mouse_motion_frame);
11024 last_mouse_motion_frame = Qnil;
11026 Qmodifier_value = intern_c_string ("modifier-value");
11027 Qalt = intern_c_string ("alt");
11028 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
11029 Qhyper = intern_c_string ("hyper");
11030 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11031 Qmeta = intern_c_string ("meta");
11032 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11033 Qsuper = intern_c_string ("super");
11034 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11036 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
11037 doc: /* Which keys Emacs uses for the alt modifier.
11038 This should be one of the symbols `alt', `hyper', `meta', `super'.
11039 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11040 is nil, which is the same as `alt'. */);
11041 Vx_alt_keysym = Qnil;
11043 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
11044 doc: /* Which keys Emacs uses for the hyper modifier.
11045 This should be one of the symbols `alt', `hyper', `meta', `super'.
11046 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11047 default is nil, which is the same as `hyper'. */);
11048 Vx_hyper_keysym = Qnil;
11050 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
11051 doc: /* Which keys Emacs uses for the meta modifier.
11052 This should be one of the symbols `alt', `hyper', `meta', `super'.
11053 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11054 default is nil, which is the same as `meta'. */);
11055 Vx_meta_keysym = Qnil;
11057 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
11058 doc: /* Which keys Emacs uses for the super modifier.
11059 This should be one of the symbols `alt', `hyper', `meta', `super'.
11060 For example, `super' means use the Super_L and Super_R keysyms. The
11061 default is nil, which is the same as `super'. */);
11062 Vx_super_keysym = Qnil;
11064 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
11065 doc: /* Hash table of character codes indexed by X keysym codes. */);
11066 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
11067 make_float (DEFAULT_REHASH_SIZE),
11068 make_float (DEFAULT_REHASH_THRESHOLD),
11069 Qnil, Qnil, Qnil);
11072 #endif /* HAVE_X_WINDOWS */
11074 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
11075 (do not change this comment) */