* files.el (directory-files-no-dot-files-regexp): Doc fix (bug#6298).
[emacs.git] / src / xterm.c
blobf867632f22f74f55ea6d0eb6a5495043c94d70b1
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, extra;
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 extra = s->face->id == TOOL_BAR_FACE_ID
2326 ? XINT (Vtool_bar_button_margin) : 0;
2328 x0 = x - thick - extra;
2329 y0 = y - thick - extra;
2330 x1 = x + s->slice.width + thick - 1 + extra;
2331 y1 = y + s->slice.height + thick - 1 + extra;
2333 x_setup_relief_colors (s);
2334 get_glyph_string_clip_rect (s, &r);
2335 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2336 s->slice.y == 0,
2337 s->slice.y + s->slice.height == s->img->height,
2338 s->slice.x == 0,
2339 s->slice.x + s->slice.width == s->img->width,
2340 &r);
2344 /* Draw the foreground of image glyph string S to PIXMAP. */
2346 static void
2347 x_draw_image_foreground_1 (s, pixmap)
2348 struct glyph_string *s;
2349 Pixmap pixmap;
2351 int x = 0;
2352 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2354 /* If first glyph of S has a left box line, start drawing it to the
2355 right of that line. */
2356 if (s->face->box != FACE_NO_BOX
2357 && s->first_glyph->left_box_line_p
2358 && s->slice.x == 0)
2359 x += eabs (s->face->box_line_width);
2361 /* If there is a margin around the image, adjust x- and y-position
2362 by that margin. */
2363 if (s->slice.x == 0)
2364 x += s->img->hmargin;
2365 if (s->slice.y == 0)
2366 y += s->img->vmargin;
2368 if (s->img->pixmap)
2370 if (s->img->mask)
2372 /* We can't set both a clip mask and use XSetClipRectangles
2373 because the latter also sets a clip mask. We also can't
2374 trust on the shape extension to be available
2375 (XShapeCombineRegion). So, compute the rectangle to draw
2376 manually. */
2377 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2378 | GCFunction);
2379 XGCValues xgcv;
2381 xgcv.clip_mask = s->img->mask;
2382 xgcv.clip_x_origin = x - s->slice.x;
2383 xgcv.clip_y_origin = y - s->slice.y;
2384 xgcv.function = GXcopy;
2385 XChangeGC (s->display, s->gc, mask, &xgcv);
2387 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2388 s->slice.x, s->slice.y,
2389 s->slice.width, s->slice.height, x, y);
2390 XSetClipMask (s->display, s->gc, None);
2392 else
2394 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2395 s->slice.x, s->slice.y,
2396 s->slice.width, s->slice.height, x, y);
2398 /* When the image has a mask, we can expect that at
2399 least part of a mouse highlight or a block cursor will
2400 be visible. If the image doesn't have a mask, make
2401 a block cursor visible by drawing a rectangle around
2402 the image. I believe it's looking better if we do
2403 nothing here for mouse-face. */
2404 if (s->hl == DRAW_CURSOR)
2406 int r = s->img->relief;
2407 if (r < 0) r = -r;
2408 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2409 s->slice.width + r*2 - 1,
2410 s->slice.height + r*2 - 1);
2414 else
2415 /* Draw a rectangle if image could not be loaded. */
2416 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2417 s->slice.width - 1, s->slice.height - 1);
2421 /* Draw part of the background of glyph string S. X, Y, W, and H
2422 give the rectangle to draw. */
2424 static void
2425 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2426 struct glyph_string *s;
2427 int x, y, w, h;
2429 if (s->stippled_p)
2431 /* Fill background with a stipple pattern. */
2432 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2433 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2434 XSetFillStyle (s->display, s->gc, FillSolid);
2436 else
2437 x_clear_glyph_string_rect (s, x, y, w, h);
2441 /* Draw image glyph string S.
2443 s->y
2444 s->x +-------------------------
2445 | s->face->box
2447 | +-------------------------
2448 | | s->img->margin
2450 | | +-------------------
2451 | | | the image
2455 static void
2456 x_draw_image_glyph_string (s)
2457 struct glyph_string *s;
2459 int box_line_hwidth = eabs (s->face->box_line_width);
2460 int box_line_vwidth = max (s->face->box_line_width, 0);
2461 int height;
2462 Pixmap pixmap = None;
2464 height = s->height;
2465 if (s->slice.y == 0)
2466 height -= box_line_vwidth;
2467 if (s->slice.y + s->slice.height >= s->img->height)
2468 height -= box_line_vwidth;
2470 /* Fill background with face under the image. Do it only if row is
2471 taller than image or if image has a clip mask to reduce
2472 flickering. */
2473 s->stippled_p = s->face->stipple != 0;
2474 if (height > s->slice.height
2475 || s->img->hmargin
2476 || s->img->vmargin
2477 || s->img->mask
2478 || s->img->pixmap == 0
2479 || s->width != s->background_width)
2481 if (s->img->mask)
2483 /* Create a pixmap as large as the glyph string. Fill it
2484 with the background color. Copy the image to it, using
2485 its mask. Copy the temporary pixmap to the display. */
2486 Screen *screen = FRAME_X_SCREEN (s->f);
2487 int depth = DefaultDepthOfScreen (screen);
2489 /* Create a pixmap as large as the glyph string. */
2490 pixmap = XCreatePixmap (s->display, s->window,
2491 s->background_width,
2492 s->height, depth);
2494 /* Don't clip in the following because we're working on the
2495 pixmap. */
2496 XSetClipMask (s->display, s->gc, None);
2498 /* Fill the pixmap with the background color/stipple. */
2499 if (s->stippled_p)
2501 /* Fill background with a stipple pattern. */
2502 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2503 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2504 XFillRectangle (s->display, pixmap, s->gc,
2505 0, 0, s->background_width, s->height);
2506 XSetFillStyle (s->display, s->gc, FillSolid);
2507 XSetTSOrigin (s->display, s->gc, 0, 0);
2509 else
2511 XGCValues xgcv;
2512 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2513 &xgcv);
2514 XSetForeground (s->display, s->gc, xgcv.background);
2515 XFillRectangle (s->display, pixmap, s->gc,
2516 0, 0, s->background_width, s->height);
2517 XSetForeground (s->display, s->gc, xgcv.foreground);
2520 else
2522 int x = s->x;
2523 int y = s->y;
2525 if (s->first_glyph->left_box_line_p
2526 && s->slice.x == 0)
2527 x += box_line_hwidth;
2529 if (s->slice.y == 0)
2530 y += box_line_vwidth;
2532 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2535 s->background_filled_p = 1;
2538 /* Draw the foreground. */
2539 if (pixmap != None)
2541 x_draw_image_foreground_1 (s, pixmap);
2542 x_set_glyph_string_clipping (s);
2543 XCopyArea (s->display, pixmap, s->window, s->gc,
2544 0, 0, s->background_width, s->height, s->x, s->y);
2545 XFreePixmap (s->display, pixmap);
2547 else
2548 x_draw_image_foreground (s);
2550 /* If we must draw a relief around the image, do it. */
2551 if (s->img->relief
2552 || s->hl == DRAW_IMAGE_RAISED
2553 || s->hl == DRAW_IMAGE_SUNKEN)
2554 x_draw_image_relief (s);
2558 /* Draw stretch glyph string S. */
2560 static void
2561 x_draw_stretch_glyph_string (s)
2562 struct glyph_string *s;
2564 xassert (s->first_glyph->type == STRETCH_GLYPH);
2566 if (s->hl == DRAW_CURSOR
2567 && !x_stretch_cursor_p)
2569 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2570 as wide as the stretch glyph. */
2571 int width, background_width = s->background_width;
2572 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2574 if (x < left_x)
2576 background_width -= left_x - x;
2577 x = left_x;
2579 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2581 /* Draw cursor. */
2582 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2584 /* Clear rest using the GC of the original non-cursor face. */
2585 if (width < background_width)
2587 int y = s->y;
2588 int w = background_width - width, h = s->height;
2589 XRectangle r;
2590 GC gc;
2592 x += width;
2593 if (s->row->mouse_face_p
2594 && cursor_in_mouse_face_p (s->w))
2596 x_set_mouse_face_gc (s);
2597 gc = s->gc;
2599 else
2600 gc = s->face->gc;
2602 get_glyph_string_clip_rect (s, &r);
2603 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2605 if (s->face->stipple)
2607 /* Fill background with a stipple pattern. */
2608 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2609 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2610 XSetFillStyle (s->display, gc, FillSolid);
2612 else
2614 XGCValues xgcv;
2615 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2616 XSetForeground (s->display, gc, xgcv.background);
2617 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2618 XSetForeground (s->display, gc, xgcv.foreground);
2622 else if (!s->background_filled_p)
2624 int background_width = s->background_width;
2625 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2627 /* Don't draw into left margin, fringe or scrollbar area
2628 except for header line and mode line. */
2629 if (x < left_x && !s->row->mode_line_p)
2631 background_width -= left_x - x;
2632 x = left_x;
2634 if (background_width > 0)
2635 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2638 s->background_filled_p = 1;
2642 /* Draw glyph string S. */
2644 static void
2645 x_draw_glyph_string (s)
2646 struct glyph_string *s;
2648 int relief_drawn_p = 0;
2650 /* If S draws into the background of its successors, draw the
2651 background of the successors first so that S can draw into it.
2652 This makes S->next use XDrawString instead of XDrawImageString. */
2653 if (s->next && s->right_overhang && !s->for_overlaps)
2655 int width;
2656 struct glyph_string *next;
2658 for (width = 0, next = s->next;
2659 next && width < s->right_overhang;
2660 width += next->width, next = next->next)
2661 if (next->first_glyph->type != IMAGE_GLYPH)
2663 x_set_glyph_string_gc (next);
2664 x_set_glyph_string_clipping (next);
2665 if (next->first_glyph->type == STRETCH_GLYPH)
2666 x_draw_stretch_glyph_string (next);
2667 else
2668 x_draw_glyph_string_background (next, 1);
2669 next->num_clips = 0;
2673 /* Set up S->gc, set clipping and draw S. */
2674 x_set_glyph_string_gc (s);
2676 /* Draw relief (if any) in advance for char/composition so that the
2677 glyph string can be drawn over it. */
2678 if (!s->for_overlaps
2679 && s->face->box != FACE_NO_BOX
2680 && (s->first_glyph->type == CHAR_GLYPH
2681 || s->first_glyph->type == COMPOSITE_GLYPH))
2684 x_set_glyph_string_clipping (s);
2685 x_draw_glyph_string_background (s, 1);
2686 x_draw_glyph_string_box (s);
2687 x_set_glyph_string_clipping (s);
2688 relief_drawn_p = 1;
2690 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2691 && !s->clip_tail
2692 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2693 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2694 /* We must clip just this glyph. left_overhang part has already
2695 drawn when s->prev was drawn, and right_overhang part will be
2696 drawn later when s->next is drawn. */
2697 x_set_glyph_string_clipping_exactly (s, s);
2698 else
2699 x_set_glyph_string_clipping (s);
2701 switch (s->first_glyph->type)
2703 case IMAGE_GLYPH:
2704 x_draw_image_glyph_string (s);
2705 break;
2707 case STRETCH_GLYPH:
2708 x_draw_stretch_glyph_string (s);
2709 break;
2711 case CHAR_GLYPH:
2712 if (s->for_overlaps)
2713 s->background_filled_p = 1;
2714 else
2715 x_draw_glyph_string_background (s, 0);
2716 x_draw_glyph_string_foreground (s);
2717 break;
2719 case COMPOSITE_GLYPH:
2720 if (s->for_overlaps || (s->cmp_from > 0
2721 && ! s->first_glyph->u.cmp.automatic))
2722 s->background_filled_p = 1;
2723 else
2724 x_draw_glyph_string_background (s, 1);
2725 x_draw_composite_glyph_string_foreground (s);
2726 break;
2728 default:
2729 abort ();
2732 if (!s->for_overlaps)
2734 /* Draw underline. */
2735 if (s->face->underline_p)
2737 unsigned long thickness, position;
2738 int y;
2740 if (s->prev && s->prev->face->underline_p)
2742 /* We use the same underline style as the previous one. */
2743 thickness = s->prev->underline_thickness;
2744 position = s->prev->underline_position;
2746 else
2748 /* Get the underline thickness. Default is 1 pixel. */
2749 if (s->font && s->font->underline_thickness > 0)
2750 thickness = s->font->underline_thickness;
2751 else
2752 thickness = 1;
2753 if (x_underline_at_descent_line)
2754 position = (s->height - thickness) - (s->ybase - s->y);
2755 else
2757 /* Get the underline position. This is the recommended
2758 vertical offset in pixels from the baseline to the top of
2759 the underline. This is a signed value according to the
2760 specs, and its default is
2762 ROUND ((maximum descent) / 2), with
2763 ROUND(x) = floor (x + 0.5) */
2765 if (x_use_underline_position_properties
2766 && s->font && s->font->underline_position >= 0)
2767 position = s->font->underline_position;
2768 else if (s->font)
2769 position = (s->font->descent + 1) / 2;
2770 else
2771 position = underline_minimum_offset;
2773 position = max (position, underline_minimum_offset);
2775 /* Check the sanity of thickness and position. We should
2776 avoid drawing underline out of the current line area. */
2777 if (s->y + s->height <= s->ybase + position)
2778 position = (s->height - 1) - (s->ybase - s->y);
2779 if (s->y + s->height < s->ybase + position + thickness)
2780 thickness = (s->y + s->height) - (s->ybase + position);
2781 s->underline_thickness = thickness;
2782 s->underline_position = position;
2783 y = s->ybase + position;
2784 if (s->face->underline_defaulted_p)
2785 XFillRectangle (s->display, s->window, s->gc,
2786 s->x, y, s->width, thickness);
2787 else
2789 XGCValues xgcv;
2790 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2791 XSetForeground (s->display, s->gc, s->face->underline_color);
2792 XFillRectangle (s->display, s->window, s->gc,
2793 s->x, y, s->width, thickness);
2794 XSetForeground (s->display, s->gc, xgcv.foreground);
2798 /* Draw overline. */
2799 if (s->face->overline_p)
2801 unsigned long dy = 0, h = 1;
2803 if (s->face->overline_color_defaulted_p)
2804 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2805 s->width, h);
2806 else
2808 XGCValues xgcv;
2809 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2810 XSetForeground (s->display, s->gc, s->face->overline_color);
2811 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2812 s->width, h);
2813 XSetForeground (s->display, s->gc, xgcv.foreground);
2817 /* Draw strike-through. */
2818 if (s->face->strike_through_p)
2820 unsigned long h = 1;
2821 unsigned long dy = (s->height - h) / 2;
2823 if (s->face->strike_through_color_defaulted_p)
2824 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2825 s->width, h);
2826 else
2828 XGCValues xgcv;
2829 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2830 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2831 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2832 s->width, h);
2833 XSetForeground (s->display, s->gc, xgcv.foreground);
2837 /* Draw relief if not yet drawn. */
2838 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2839 x_draw_glyph_string_box (s);
2841 if (s->prev)
2843 struct glyph_string *prev;
2845 for (prev = s->prev; prev; prev = prev->prev)
2846 if (prev->hl != s->hl
2847 && prev->x + prev->width + prev->right_overhang > s->x)
2849 /* As prev was drawn while clipped to its own area, we
2850 must draw the right_overhang part using s->hl now. */
2851 enum draw_glyphs_face save = prev->hl;
2853 prev->hl = s->hl;
2854 x_set_glyph_string_gc (prev);
2855 x_set_glyph_string_clipping_exactly (s, prev);
2856 if (prev->first_glyph->type == CHAR_GLYPH)
2857 x_draw_glyph_string_foreground (prev);
2858 else
2859 x_draw_composite_glyph_string_foreground (prev);
2860 XSetClipMask (prev->display, prev->gc, None);
2861 prev->hl = save;
2862 prev->num_clips = 0;
2866 if (s->next)
2868 struct glyph_string *next;
2870 for (next = s->next; next; next = next->next)
2871 if (next->hl != s->hl
2872 && next->x - next->left_overhang < s->x + s->width)
2874 /* As next will be drawn while clipped to its own area,
2875 we must draw the left_overhang part using s->hl now. */
2876 enum draw_glyphs_face save = next->hl;
2878 next->hl = s->hl;
2879 x_set_glyph_string_gc (next);
2880 x_set_glyph_string_clipping_exactly (s, next);
2881 if (next->first_glyph->type == CHAR_GLYPH)
2882 x_draw_glyph_string_foreground (next);
2883 else
2884 x_draw_composite_glyph_string_foreground (next);
2885 XSetClipMask (next->display, next->gc, None);
2886 next->hl = save;
2887 next->num_clips = 0;
2892 /* Reset clipping. */
2893 XSetClipMask (s->display, s->gc, None);
2894 s->num_clips = 0;
2897 /* Shift display to make room for inserted glyphs. */
2899 void
2900 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2901 struct frame *f;
2902 int x, y, width, height, shift_by;
2904 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2905 f->output_data.x->normal_gc,
2906 x, y, width, height,
2907 x + shift_by, y);
2910 /* Delete N glyphs at the nominal cursor position. Not implemented
2911 for X frames. */
2913 static void
2914 x_delete_glyphs (f, n)
2915 struct frame *f;
2916 register int n;
2918 abort ();
2922 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2923 If they are <= 0, this is probably an error. */
2925 void
2926 x_clear_area (dpy, window, x, y, width, height, exposures)
2927 Display *dpy;
2928 Window window;
2929 int x, y;
2930 int width, height;
2931 int exposures;
2933 xassert (width > 0 && height > 0);
2934 XClearArea (dpy, window, x, y, width, height, exposures);
2938 /* Clear an entire frame. */
2940 static void
2941 x_clear_frame (struct frame *f)
2943 /* Clearing the frame will erase any cursor, so mark them all as no
2944 longer visible. */
2945 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2946 output_cursor.hpos = output_cursor.vpos = 0;
2947 output_cursor.x = -1;
2949 /* We don't set the output cursor here because there will always
2950 follow an explicit cursor_to. */
2951 BLOCK_INPUT;
2952 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2954 /* We have to clear the scroll bars, too. If we have changed
2955 colors or something like that, then they should be notified. */
2956 x_scroll_bar_clear (f);
2958 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2959 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2960 redisplay, do it here. */
2961 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2962 #endif
2964 XFlush (FRAME_X_DISPLAY (f));
2966 UNBLOCK_INPUT;
2971 /* Invert the middle quarter of the frame for .15 sec. */
2973 /* We use the select system call to do the waiting, so we have to make
2974 sure it's available. If it isn't, we just won't do visual bells. */
2976 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2979 /* Subtract the `struct timeval' values X and Y, storing the result in
2980 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2982 static int
2983 timeval_subtract (result, x, y)
2984 struct timeval *result, x, y;
2986 /* Perform the carry for the later subtraction by updating y. This
2987 is safer because on some systems the tv_sec member is unsigned. */
2988 if (x.tv_usec < y.tv_usec)
2990 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2991 y.tv_usec -= 1000000 * nsec;
2992 y.tv_sec += nsec;
2995 if (x.tv_usec - y.tv_usec > 1000000)
2997 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2998 y.tv_usec += 1000000 * nsec;
2999 y.tv_sec -= nsec;
3002 /* Compute the time remaining to wait. tv_usec is certainly
3003 positive. */
3004 result->tv_sec = x.tv_sec - y.tv_sec;
3005 result->tv_usec = x.tv_usec - y.tv_usec;
3007 /* Return indication of whether the result should be considered
3008 negative. */
3009 return x.tv_sec < y.tv_sec;
3012 void
3013 XTflash (f)
3014 struct frame *f;
3016 BLOCK_INPUT;
3019 #ifdef USE_GTK
3020 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3021 when the scroll bars and the edit widget share the same X window. */
3022 GdkGCValues vals;
3023 GdkGC *gc;
3024 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3025 ^ FRAME_BACKGROUND_PIXEL (f));
3026 vals.function = GDK_XOR;
3027 gc = gdk_gc_new_with_values (FRAME_GTK_WIDGET (f)->window,
3028 &vals,
3029 GDK_GC_FUNCTION
3030 | GDK_GC_FOREGROUND);
3031 #define XFillRectangle(d, win, gc, x, y, w, h) \
3032 gdk_draw_rectangle (FRAME_GTK_WIDGET (f)->window, \
3033 gc, TRUE, x, y, w, h)
3034 #else
3035 GC gc;
3037 /* Create a GC that will use the GXxor function to flip foreground
3038 pixels into background pixels. */
3040 XGCValues values;
3042 values.function = GXxor;
3043 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3044 ^ FRAME_BACKGROUND_PIXEL (f));
3046 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3047 GCFunction | GCForeground, &values);
3049 #endif
3051 /* Get the height not including a menu bar widget. */
3052 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
3053 /* Height of each line to flash. */
3054 int flash_height = FRAME_LINE_HEIGHT (f);
3055 /* These will be the left and right margins of the rectangles. */
3056 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3057 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3059 int width;
3061 /* Don't flash the area between a scroll bar and the frame
3062 edge it is next to. */
3063 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3065 case vertical_scroll_bar_left:
3066 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3067 break;
3069 case vertical_scroll_bar_right:
3070 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3071 break;
3073 default:
3074 break;
3077 width = flash_right - flash_left;
3079 /* If window is tall, flash top and bottom line. */
3080 if (height > 3 * FRAME_LINE_HEIGHT (f))
3082 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3083 flash_left,
3084 (FRAME_INTERNAL_BORDER_WIDTH (f)
3085 + FRAME_TOP_MARGIN_HEIGHT (f)),
3086 width, flash_height);
3087 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3088 flash_left,
3089 (height - flash_height
3090 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3091 width, flash_height);
3094 else
3095 /* If it is short, flash it all. */
3096 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3097 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3098 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3100 x_flush (f);
3103 struct timeval wakeup;
3105 EMACS_GET_TIME (wakeup);
3107 /* Compute time to wait until, propagating carry from usecs. */
3108 wakeup.tv_usec += 150000;
3109 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3110 wakeup.tv_usec %= 1000000;
3112 /* Keep waiting until past the time wakeup or any input gets
3113 available. */
3114 while (! detect_input_pending ())
3116 struct timeval current;
3117 struct timeval timeout;
3119 EMACS_GET_TIME (current);
3121 /* Break if result would be negative. */
3122 if (timeval_subtract (&current, wakeup, current))
3123 break;
3125 /* How long `select' should wait. */
3126 timeout.tv_sec = 0;
3127 timeout.tv_usec = 10000;
3129 /* Try to wait that long--but we might wake up sooner. */
3130 select (0, NULL, NULL, NULL, &timeout);
3134 /* If window is tall, flash top and bottom line. */
3135 if (height > 3 * FRAME_LINE_HEIGHT (f))
3137 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3138 flash_left,
3139 (FRAME_INTERNAL_BORDER_WIDTH (f)
3140 + FRAME_TOP_MARGIN_HEIGHT (f)),
3141 width, flash_height);
3142 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3143 flash_left,
3144 (height - flash_height
3145 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3146 width, flash_height);
3148 else
3149 /* If it is short, flash it all. */
3150 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3151 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3152 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3154 #ifdef USE_GTK
3155 g_object_unref (G_OBJECT (gc));
3156 #undef XFillRectangle
3157 #else
3158 XFreeGC (FRAME_X_DISPLAY (f), gc);
3159 #endif
3160 x_flush (f);
3164 UNBLOCK_INPUT;
3167 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3170 static void
3171 XTtoggle_invisible_pointer (f, invisible)
3172 FRAME_PTR f;
3173 int invisible;
3175 BLOCK_INPUT;
3176 if (invisible)
3178 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor != 0)
3179 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3180 FRAME_X_DISPLAY_INFO (f)->invisible_cursor);
3182 else
3183 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3184 f->output_data.x->current_cursor);
3185 f->pointer_invisible = invisible;
3186 UNBLOCK_INPUT;
3190 /* Make audible bell. */
3192 void
3193 XTring_bell ()
3195 struct frame *f = SELECTED_FRAME ();
3197 if (FRAME_X_DISPLAY (f))
3199 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3200 if (visible_bell)
3201 XTflash (f);
3202 else
3203 #endif
3205 BLOCK_INPUT;
3206 XBell (FRAME_X_DISPLAY (f), 0);
3207 XFlush (FRAME_X_DISPLAY (f));
3208 UNBLOCK_INPUT;
3214 /* Specify how many text lines, from the top of the window,
3215 should be affected by insert-lines and delete-lines operations.
3216 This, and those operations, are used only within an update
3217 that is bounded by calls to x_update_begin and x_update_end. */
3219 static void
3220 XTset_terminal_window (n)
3221 register int n;
3223 /* This function intentionally left blank. */
3228 /***********************************************************************
3229 Line Dance
3230 ***********************************************************************/
3232 /* Perform an insert-lines or delete-lines operation, inserting N
3233 lines or deleting -N lines at vertical position VPOS. */
3235 static void
3236 x_ins_del_lines (f, vpos, n)
3237 struct frame *f;
3238 int vpos, n;
3240 abort ();
3244 /* Scroll part of the display as described by RUN. */
3246 static void
3247 x_scroll_run (w, run)
3248 struct window *w;
3249 struct run *run;
3251 struct frame *f = XFRAME (w->frame);
3252 int x, y, width, height, from_y, to_y, bottom_y;
3254 /* Get frame-relative bounding box of the text display area of W,
3255 without mode lines. Include in this box the left and right
3256 fringe of W. */
3257 window_box (w, -1, &x, &y, &width, &height);
3259 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3260 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3261 bottom_y = y + height;
3263 if (to_y < from_y)
3265 /* Scrolling up. Make sure we don't copy part of the mode
3266 line at the bottom. */
3267 if (from_y + run->height > bottom_y)
3268 height = bottom_y - from_y;
3269 else
3270 height = run->height;
3272 else
3274 /* Scolling down. Make sure we don't copy over the mode line.
3275 at the bottom. */
3276 if (to_y + run->height > bottom_y)
3277 height = bottom_y - to_y;
3278 else
3279 height = run->height;
3282 BLOCK_INPUT;
3284 /* Cursor off. Will be switched on again in x_update_window_end. */
3285 updated_window = w;
3286 x_clear_cursor (w);
3288 XCopyArea (FRAME_X_DISPLAY (f),
3289 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3290 f->output_data.x->normal_gc,
3291 x, from_y,
3292 width, height,
3293 x, to_y);
3295 UNBLOCK_INPUT;
3300 /***********************************************************************
3301 Exposure Events
3302 ***********************************************************************/
3305 static void
3306 frame_highlight (f)
3307 struct frame *f;
3309 /* We used to only do this if Vx_no_window_manager was non-nil, but
3310 the ICCCM (section 4.1.6) says that the window's border pixmap
3311 and border pixel are window attributes which are "private to the
3312 client", so we can always change it to whatever we want. */
3313 BLOCK_INPUT;
3314 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3315 f->output_data.x->border_pixel);
3316 UNBLOCK_INPUT;
3317 x_update_cursor (f, 1);
3318 x_set_frame_alpha (f);
3321 static void
3322 frame_unhighlight (f)
3323 struct frame *f;
3325 /* We used to only do this if Vx_no_window_manager was non-nil, but
3326 the ICCCM (section 4.1.6) says that the window's border pixmap
3327 and border pixel are window attributes which are "private to the
3328 client", so we can always change it to whatever we want. */
3329 BLOCK_INPUT;
3330 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3331 f->output_data.x->border_tile);
3332 UNBLOCK_INPUT;
3333 x_update_cursor (f, 1);
3334 x_set_frame_alpha (f);
3337 /* The focus has changed. Update the frames as necessary to reflect
3338 the new situation. Note that we can't change the selected frame
3339 here, because the Lisp code we are interrupting might become confused.
3340 Each event gets marked with the frame in which it occurred, so the
3341 Lisp code can tell when the switch took place by examining the events. */
3343 static void
3344 x_new_focus_frame (dpyinfo, frame)
3345 struct x_display_info *dpyinfo;
3346 struct frame *frame;
3348 struct frame *old_focus = dpyinfo->x_focus_frame;
3350 if (frame != dpyinfo->x_focus_frame)
3352 /* Set this before calling other routines, so that they see
3353 the correct value of x_focus_frame. */
3354 dpyinfo->x_focus_frame = frame;
3356 if (old_focus && old_focus->auto_lower)
3357 x_lower_frame (old_focus);
3359 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3360 pending_autoraise_frame = dpyinfo->x_focus_frame;
3361 else
3362 pending_autoraise_frame = 0;
3365 x_frame_rehighlight (dpyinfo);
3368 /* Handle FocusIn and FocusOut state changes for FRAME.
3369 If FRAME has focus and there exists more than one frame, puts
3370 a FOCUS_IN_EVENT into *BUFP. */
3372 static void
3373 x_focus_changed (type, state, dpyinfo, frame, bufp)
3374 int type;
3375 int state;
3376 struct x_display_info *dpyinfo;
3377 struct frame *frame;
3378 struct input_event *bufp;
3380 if (type == FocusIn)
3382 if (dpyinfo->x_focus_event_frame != frame)
3384 x_new_focus_frame (dpyinfo, frame);
3385 dpyinfo->x_focus_event_frame = frame;
3387 /* Don't stop displaying the initial startup message
3388 for a switch-frame event we don't need. */
3389 if (NILP (Vterminal_frame)
3390 && CONSP (Vframe_list)
3391 && !NILP (XCDR (Vframe_list)))
3393 bufp->kind = FOCUS_IN_EVENT;
3394 XSETFRAME (bufp->frame_or_window, frame);
3398 frame->output_data.x->focus_state |= state;
3400 #ifdef HAVE_X_I18N
3401 if (FRAME_XIC (frame))
3402 XSetICFocus (FRAME_XIC (frame));
3403 #endif
3405 else if (type == FocusOut)
3407 frame->output_data.x->focus_state &= ~state;
3409 if (dpyinfo->x_focus_event_frame == frame)
3411 dpyinfo->x_focus_event_frame = 0;
3412 x_new_focus_frame (dpyinfo, 0);
3415 #ifdef HAVE_X_I18N
3416 if (FRAME_XIC (frame))
3417 XUnsetICFocus (FRAME_XIC (frame));
3418 #endif
3419 if (frame->pointer_invisible)
3420 XTtoggle_invisible_pointer (frame, 0);
3424 /* The focus may have changed. Figure out if it is a real focus change,
3425 by checking both FocusIn/Out and Enter/LeaveNotify events.
3427 Returns FOCUS_IN_EVENT event in *BUFP. */
3429 static void
3430 x_detect_focus_change (dpyinfo, event, bufp)
3431 struct x_display_info *dpyinfo;
3432 XEvent *event;
3433 struct input_event *bufp;
3435 struct frame *frame;
3437 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3438 if (! frame)
3439 return;
3441 switch (event->type)
3443 case EnterNotify:
3444 case LeaveNotify:
3446 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3447 int focus_state
3448 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3450 if (event->xcrossing.detail != NotifyInferior
3451 && event->xcrossing.focus
3452 && ! (focus_state & FOCUS_EXPLICIT))
3453 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3454 FOCUS_IMPLICIT,
3455 dpyinfo, frame, bufp);
3457 break;
3459 case FocusIn:
3460 case FocusOut:
3461 x_focus_changed (event->type,
3462 (event->xfocus.detail == NotifyPointer ?
3463 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3464 dpyinfo, frame, bufp);
3465 break;
3467 case ClientMessage:
3468 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3470 enum xembed_message msg = event->xclient.data.l[1];
3471 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3472 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3474 break;
3479 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3481 void
3482 x_mouse_leave (dpyinfo)
3483 struct x_display_info *dpyinfo;
3485 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3488 /* The focus has changed, or we have redirected a frame's focus to
3489 another frame (this happens when a frame uses a surrogate
3490 mini-buffer frame). Shift the highlight as appropriate.
3492 The FRAME argument doesn't necessarily have anything to do with which
3493 frame is being highlighted or un-highlighted; we only use it to find
3494 the appropriate X display info. */
3496 static void
3497 XTframe_rehighlight (frame)
3498 struct frame *frame;
3500 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3503 static void
3504 x_frame_rehighlight (dpyinfo)
3505 struct x_display_info *dpyinfo;
3507 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3509 if (dpyinfo->x_focus_frame)
3511 dpyinfo->x_highlight_frame
3512 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3513 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3514 : dpyinfo->x_focus_frame);
3515 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3517 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3518 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3521 else
3522 dpyinfo->x_highlight_frame = 0;
3524 if (dpyinfo->x_highlight_frame != old_highlight)
3526 if (old_highlight)
3527 frame_unhighlight (old_highlight);
3528 if (dpyinfo->x_highlight_frame)
3529 frame_highlight (dpyinfo->x_highlight_frame);
3535 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3537 /* Initialize mode_switch_bit and modifier_meaning. */
3538 static void
3539 x_find_modifier_meanings (dpyinfo)
3540 struct x_display_info *dpyinfo;
3542 int min_code, max_code;
3543 KeySym *syms;
3544 int syms_per_code;
3545 XModifierKeymap *mods;
3547 dpyinfo->meta_mod_mask = 0;
3548 dpyinfo->shift_lock_mask = 0;
3549 dpyinfo->alt_mod_mask = 0;
3550 dpyinfo->super_mod_mask = 0;
3551 dpyinfo->hyper_mod_mask = 0;
3553 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3555 syms = XGetKeyboardMapping (dpyinfo->display,
3556 min_code, max_code - min_code + 1,
3557 &syms_per_code);
3558 mods = XGetModifierMapping (dpyinfo->display);
3560 /* Scan the modifier table to see which modifier bits the Meta and
3561 Alt keysyms are on. */
3563 int row, col; /* The row and column in the modifier table. */
3564 int found_alt_or_meta;
3566 for (row = 3; row < 8; row++)
3568 found_alt_or_meta = 0;
3569 for (col = 0; col < mods->max_keypermod; col++)
3571 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3573 /* Zeroes are used for filler. Skip them. */
3574 if (code == 0)
3575 continue;
3577 /* Are any of this keycode's keysyms a meta key? */
3579 int code_col;
3581 for (code_col = 0; code_col < syms_per_code; code_col++)
3583 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3585 switch (sym)
3587 case XK_Meta_L:
3588 case XK_Meta_R:
3589 found_alt_or_meta = 1;
3590 dpyinfo->meta_mod_mask |= (1 << row);
3591 break;
3593 case XK_Alt_L:
3594 case XK_Alt_R:
3595 found_alt_or_meta = 1;
3596 dpyinfo->alt_mod_mask |= (1 << row);
3597 break;
3599 case XK_Hyper_L:
3600 case XK_Hyper_R:
3601 if (!found_alt_or_meta)
3602 dpyinfo->hyper_mod_mask |= (1 << row);
3603 code_col = syms_per_code;
3604 col = mods->max_keypermod;
3605 break;
3607 case XK_Super_L:
3608 case XK_Super_R:
3609 if (!found_alt_or_meta)
3610 dpyinfo->super_mod_mask |= (1 << row);
3611 code_col = syms_per_code;
3612 col = mods->max_keypermod;
3613 break;
3615 case XK_Shift_Lock:
3616 /* Ignore this if it's not on the lock modifier. */
3617 if (!found_alt_or_meta && ((1 << row) == LockMask))
3618 dpyinfo->shift_lock_mask = LockMask;
3619 code_col = syms_per_code;
3620 col = mods->max_keypermod;
3621 break;
3629 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3630 if (! dpyinfo->meta_mod_mask)
3632 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3633 dpyinfo->alt_mod_mask = 0;
3636 /* If some keys are both alt and meta,
3637 make them just meta, not alt. */
3638 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3640 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3643 XFree ((char *) syms);
3644 XFreeModifiermap (mods);
3647 /* Convert between the modifier bits X uses and the modifier bits
3648 Emacs uses. */
3650 unsigned int
3651 x_x_to_emacs_modifiers (dpyinfo, state)
3652 struct x_display_info *dpyinfo;
3653 unsigned int state;
3655 EMACS_UINT mod_meta = meta_modifier;
3656 EMACS_UINT mod_alt = alt_modifier;
3657 EMACS_UINT mod_hyper = hyper_modifier;
3658 EMACS_UINT mod_super = super_modifier;
3659 Lisp_Object tem;
3661 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3662 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3663 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3664 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3665 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3666 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3667 tem = Fget (Vx_super_keysym, Qmodifier_value);
3668 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3671 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3672 | ((state & ControlMask) ? ctrl_modifier : 0)
3673 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3674 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3675 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3676 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3679 static unsigned int
3680 x_emacs_to_x_modifiers (dpyinfo, state)
3681 struct x_display_info *dpyinfo;
3682 unsigned int state;
3684 EMACS_UINT mod_meta = meta_modifier;
3685 EMACS_UINT mod_alt = alt_modifier;
3686 EMACS_UINT mod_hyper = hyper_modifier;
3687 EMACS_UINT mod_super = super_modifier;
3689 Lisp_Object tem;
3691 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3692 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3693 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3694 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3695 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3696 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3697 tem = Fget (Vx_super_keysym, Qmodifier_value);
3698 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3701 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3702 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3703 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3704 | ((state & shift_modifier) ? ShiftMask : 0)
3705 | ((state & ctrl_modifier) ? ControlMask : 0)
3706 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3709 /* Convert a keysym to its name. */
3711 char *
3712 x_get_keysym_name (keysym)
3713 KeySym keysym;
3715 char *value;
3717 BLOCK_INPUT;
3718 value = XKeysymToString (keysym);
3719 UNBLOCK_INPUT;
3721 return value;
3726 /* Mouse clicks and mouse movement. Rah. */
3728 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3730 If the event is a button press, then note that we have grabbed
3731 the mouse. */
3733 static Lisp_Object
3734 construct_mouse_click (result, event, f)
3735 struct input_event *result;
3736 XButtonEvent *event;
3737 struct frame *f;
3739 /* Make the event type NO_EVENT; we'll change that when we decide
3740 otherwise. */
3741 result->kind = MOUSE_CLICK_EVENT;
3742 result->code = event->button - Button1;
3743 result->timestamp = event->time;
3744 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3745 event->state)
3746 | (event->type == ButtonRelease
3747 ? up_modifier
3748 : down_modifier));
3750 XSETINT (result->x, event->x);
3751 XSETINT (result->y, event->y);
3752 XSETFRAME (result->frame_or_window, f);
3753 result->arg = Qnil;
3754 return Qnil;
3758 /* Function to report a mouse movement to the mainstream Emacs code.
3759 The input handler calls this.
3761 We have received a mouse movement event, which is given in *event.
3762 If the mouse is over a different glyph than it was last time, tell
3763 the mainstream emacs code by setting mouse_moved. If not, ask for
3764 another motion event, so we can check again the next time it moves. */
3766 static XMotionEvent last_mouse_motion_event;
3767 static Lisp_Object last_mouse_motion_frame;
3769 static int
3770 note_mouse_movement (frame, event)
3771 FRAME_PTR frame;
3772 XMotionEvent *event;
3774 last_mouse_movement_time = event->time;
3775 last_mouse_motion_event = *event;
3776 XSETFRAME (last_mouse_motion_frame, frame);
3778 if (!FRAME_X_OUTPUT (frame))
3779 return 0;
3781 if (event->window != FRAME_X_WINDOW (frame))
3783 frame->mouse_moved = 1;
3784 last_mouse_scroll_bar = Qnil;
3785 note_mouse_highlight (frame, -1, -1);
3786 last_mouse_glyph_frame = 0;
3787 return 1;
3791 /* Has the mouse moved off the glyph it was on at the last sighting? */
3792 if (frame != last_mouse_glyph_frame
3793 || event->x < last_mouse_glyph.x
3794 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3795 || event->y < last_mouse_glyph.y
3796 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3798 frame->mouse_moved = 1;
3799 last_mouse_scroll_bar = Qnil;
3800 note_mouse_highlight (frame, event->x, event->y);
3801 /* Remember which glyph we're now on. */
3802 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3803 last_mouse_glyph_frame = frame;
3804 return 1;
3807 return 0;
3811 /************************************************************************
3812 Mouse Face
3813 ************************************************************************/
3815 static void
3816 redo_mouse_highlight ()
3818 if (!NILP (last_mouse_motion_frame)
3819 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3820 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3821 last_mouse_motion_event.x,
3822 last_mouse_motion_event.y);
3827 /* Return the current position of the mouse.
3828 *FP should be a frame which indicates which display to ask about.
3830 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3831 and *PART to the frame, window, and scroll bar part that the mouse
3832 is over. Set *X and *Y to the portion and whole of the mouse's
3833 position on the scroll bar.
3835 If the mouse movement started elsewhere, set *FP to the frame the
3836 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3837 the mouse is over.
3839 Set *TIME to the server time-stamp for the time at which the mouse
3840 was at this position.
3842 Don't store anything if we don't have a valid set of values to report.
3844 This clears the mouse_moved flag, so we can wait for the next mouse
3845 movement. */
3847 static void
3848 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3849 FRAME_PTR *fp;
3850 int insist;
3851 Lisp_Object *bar_window;
3852 enum scroll_bar_part *part;
3853 Lisp_Object *x, *y;
3854 unsigned long *time;
3856 FRAME_PTR f1;
3858 BLOCK_INPUT;
3860 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3861 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3862 else
3864 Window root;
3865 int root_x, root_y;
3867 Window dummy_window;
3868 int dummy;
3870 Lisp_Object frame, tail;
3872 /* Clear the mouse-moved flag for every frame on this display. */
3873 FOR_EACH_FRAME (tail, frame)
3874 if (FRAME_X_P (XFRAME (frame))
3875 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3876 XFRAME (frame)->mouse_moved = 0;
3878 last_mouse_scroll_bar = Qnil;
3880 /* Figure out which root window we're on. */
3881 XQueryPointer (FRAME_X_DISPLAY (*fp),
3882 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3884 /* The root window which contains the pointer. */
3885 &root,
3887 /* Trash which we can't trust if the pointer is on
3888 a different screen. */
3889 &dummy_window,
3891 /* The position on that root window. */
3892 &root_x, &root_y,
3894 /* More trash we can't trust. */
3895 &dummy, &dummy,
3897 /* Modifier keys and pointer buttons, about which
3898 we don't care. */
3899 (unsigned int *) &dummy);
3901 /* Now we have a position on the root; find the innermost window
3902 containing the pointer. */
3904 Window win, child;
3905 int win_x, win_y;
3906 int parent_x = 0, parent_y = 0;
3908 win = root;
3910 /* XTranslateCoordinates can get errors if the window
3911 structure is changing at the same time this function
3912 is running. So at least we must not crash from them. */
3914 x_catch_errors (FRAME_X_DISPLAY (*fp));
3916 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3917 && FRAME_LIVE_P (last_mouse_frame))
3919 /* If mouse was grabbed on a frame, give coords for that frame
3920 even if the mouse is now outside it. */
3921 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3923 /* From-window, to-window. */
3924 root, FRAME_X_WINDOW (last_mouse_frame),
3926 /* From-position, to-position. */
3927 root_x, root_y, &win_x, &win_y,
3929 /* Child of win. */
3930 &child);
3931 f1 = last_mouse_frame;
3933 else
3935 while (1)
3937 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3939 /* From-window, to-window. */
3940 root, win,
3942 /* From-position, to-position. */
3943 root_x, root_y, &win_x, &win_y,
3945 /* Child of win. */
3946 &child);
3948 if (child == None || child == win)
3949 break;
3950 #ifdef USE_GTK
3951 /* We don't wan't to know the innermost window. We
3952 want the edit window. For non-Gtk+ the innermost
3953 window is the edit window. For Gtk+ it might not
3954 be. It might be the tool bar for example. */
3955 if (x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win))
3956 break;
3957 #endif
3958 win = child;
3959 parent_x = win_x;
3960 parent_y = win_y;
3963 /* Now we know that:
3964 win is the innermost window containing the pointer
3965 (XTC says it has no child containing the pointer),
3966 win_x and win_y are the pointer's position in it
3967 (XTC did this the last time through), and
3968 parent_x and parent_y are the pointer's position in win's parent.
3969 (They are what win_x and win_y were when win was child.
3970 If win is the root window, it has no parent, and
3971 parent_{x,y} are invalid, but that's okay, because we'll
3972 never use them in that case.) */
3974 #ifdef USE_GTK
3975 /* We don't wan't to know the innermost window. We
3976 want the edit window. */
3977 f1 = x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3978 #else
3979 /* Is win one of our frames? */
3980 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3981 #endif
3983 #ifdef USE_X_TOOLKIT
3984 /* If we end up with the menu bar window, say it's not
3985 on the frame. */
3986 if (f1 != NULL
3987 && f1->output_data.x->menubar_widget
3988 && win == XtWindow (f1->output_data.x->menubar_widget))
3989 f1 = NULL;
3990 #endif /* USE_X_TOOLKIT */
3993 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3994 f1 = 0;
3996 x_uncatch_errors ();
3998 /* If not, is it one of our scroll bars? */
3999 if (! f1)
4001 struct scroll_bar *bar;
4003 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
4005 if (bar)
4007 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4008 win_x = parent_x;
4009 win_y = parent_y;
4013 if (f1 == 0 && insist > 0)
4014 f1 = SELECTED_FRAME ();
4016 if (f1)
4018 /* Ok, we found a frame. Store all the values.
4019 last_mouse_glyph is a rectangle used to reduce the
4020 generation of mouse events. To not miss any motion
4021 events, we must divide the frame into rectangles of the
4022 size of the smallest character that could be displayed
4023 on it, i.e. into the same rectangles that matrices on
4024 the frame are divided into. */
4026 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
4027 last_mouse_glyph_frame = f1;
4029 *bar_window = Qnil;
4030 *part = 0;
4031 *fp = f1;
4032 XSETINT (*x, win_x);
4033 XSETINT (*y, win_y);
4034 *time = last_mouse_movement_time;
4039 UNBLOCK_INPUT;
4044 /***********************************************************************
4045 Scroll bars
4046 ***********************************************************************/
4048 /* Scroll bar support. */
4050 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4051 manages it.
4052 This can be called in GC, so we have to make sure to strip off mark
4053 bits. */
4055 static struct scroll_bar *
4056 x_window_to_scroll_bar (display, window_id)
4057 Display *display;
4058 Window window_id;
4060 Lisp_Object tail;
4062 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4063 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4064 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4066 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4068 Lisp_Object frame, bar, condemned;
4070 frame = XCAR (tail);
4071 /* All elements of Vframe_list should be frames. */
4072 if (! FRAMEP (frame))
4073 abort ();
4075 if (! FRAME_X_P (XFRAME (frame)))
4076 continue;
4078 /* Scan this frame's scroll bar list for a scroll bar with the
4079 right window ID. */
4080 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4081 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4082 /* This trick allows us to search both the ordinary and
4083 condemned scroll bar lists with one loop. */
4084 ! NILP (bar) || (bar = condemned,
4085 condemned = Qnil,
4086 ! NILP (bar));
4087 bar = XSCROLL_BAR (bar)->next)
4088 if (XSCROLL_BAR (bar)->x_window == window_id &&
4089 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4090 return XSCROLL_BAR (bar);
4093 return 0;
4097 #if defined USE_LUCID
4099 /* Return the Lucid menu bar WINDOW is part of. Return null
4100 if WINDOW is not part of a menu bar. */
4102 static Widget
4103 x_window_to_menu_bar (window)
4104 Window window;
4106 Lisp_Object tail;
4108 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4110 if (FRAME_X_P (XFRAME (XCAR (tail))))
4112 Lisp_Object frame = XCAR (tail);
4113 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4115 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4116 return menu_bar;
4120 return NULL;
4123 #endif /* USE_LUCID */
4126 /************************************************************************
4127 Toolkit scroll bars
4128 ************************************************************************/
4130 #ifdef USE_TOOLKIT_SCROLL_BARS
4132 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4133 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4134 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4135 struct scroll_bar *));
4136 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4137 int, int, int));
4140 /* Lisp window being scrolled. Set when starting to interact with
4141 a toolkit scroll bar, reset to nil when ending the interaction. */
4143 static Lisp_Object window_being_scrolled;
4145 /* Last scroll bar part sent in xm_scroll_callback. */
4147 static int last_scroll_bar_part;
4149 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4150 that movements of 1/20 of the screen size are mapped to up/down. */
4152 #ifndef USE_GTK
4153 /* Id of action hook installed for scroll bars. */
4155 static XtActionHookId action_hook_id;
4157 static Boolean xaw3d_arrow_scroll;
4159 /* Whether the drag scrolling maintains the mouse at the top of the
4160 thumb. If not, resizing the thumb needs to be done more carefully
4161 to avoid jerkyness. */
4163 static Boolean xaw3d_pick_top;
4165 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4166 bars are used.. The hook is responsible for detecting when
4167 the user ends an interaction with the scroll bar, and generates
4168 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4170 static void
4171 xt_action_hook (widget, client_data, action_name, event, params,
4172 num_params)
4173 Widget widget;
4174 XtPointer client_data;
4175 String action_name;
4176 XEvent *event;
4177 String *params;
4178 Cardinal *num_params;
4180 int scroll_bar_p;
4181 char *end_action;
4183 #ifdef USE_MOTIF
4184 scroll_bar_p = XmIsScrollBar (widget);
4185 end_action = "Release";
4186 #else /* !USE_MOTIF i.e. use Xaw */
4187 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4188 end_action = "EndScroll";
4189 #endif /* USE_MOTIF */
4191 if (scroll_bar_p
4192 && strcmp (action_name, end_action) == 0
4193 && WINDOWP (window_being_scrolled))
4195 struct window *w;
4197 x_send_scroll_bar_event (window_being_scrolled,
4198 scroll_bar_end_scroll, 0, 0);
4199 w = XWINDOW (window_being_scrolled);
4201 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4203 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4204 /* The thumb size is incorrect while dragging: fix it. */
4205 set_vertical_scroll_bar (w);
4207 window_being_scrolled = Qnil;
4208 last_scroll_bar_part = -1;
4210 /* Xt timeouts no longer needed. */
4211 toolkit_scroll_bar_interaction = 0;
4214 #endif /* not USE_GTK */
4216 /* A vector of windows used for communication between
4217 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4219 static struct window **scroll_bar_windows;
4220 static int scroll_bar_windows_size;
4223 /* Send a client message with message type Xatom_Scrollbar for a
4224 scroll action to the frame of WINDOW. PART is a value identifying
4225 the part of the scroll bar that was clicked on. PORTION is the
4226 amount to scroll of a whole of WHOLE. */
4228 static void
4229 x_send_scroll_bar_event (window, part, portion, whole)
4230 Lisp_Object window;
4231 int part, portion, whole;
4233 XEvent event;
4234 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4235 struct window *w = XWINDOW (window);
4236 struct frame *f = XFRAME (w->frame);
4237 int i;
4239 BLOCK_INPUT;
4241 /* Construct a ClientMessage event to send to the frame. */
4242 ev->type = ClientMessage;
4243 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4244 ev->display = FRAME_X_DISPLAY (f);
4245 ev->window = FRAME_X_WINDOW (f);
4246 ev->format = 32;
4248 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4249 not enough to store a pointer or Lisp_Object on a 64 bit system.
4250 So, store the window in scroll_bar_windows and pass the index
4251 into that array in the event. */
4252 for (i = 0; i < scroll_bar_windows_size; ++i)
4253 if (scroll_bar_windows[i] == NULL)
4254 break;
4256 if (i == scroll_bar_windows_size)
4258 int new_size = max (10, 2 * scroll_bar_windows_size);
4259 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4260 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4262 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4263 nbytes);
4264 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4265 scroll_bar_windows_size = new_size;
4268 scroll_bar_windows[i] = w;
4269 ev->data.l[0] = (long) i;
4270 ev->data.l[1] = (long) part;
4271 ev->data.l[2] = (long) 0;
4272 ev->data.l[3] = (long) portion;
4273 ev->data.l[4] = (long) whole;
4275 /* Make Xt timeouts work while the scroll bar is active. */
4276 toolkit_scroll_bar_interaction = 1;
4277 #ifdef USE_X_TOOLKIT
4278 x_activate_timeout_atimer ();
4279 #endif
4281 /* Setting the event mask to zero means that the message will
4282 be sent to the client that created the window, and if that
4283 window no longer exists, no event will be sent. */
4284 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4285 UNBLOCK_INPUT;
4289 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4290 in *IEVENT. */
4292 static void
4293 x_scroll_bar_to_input_event (event, ievent)
4294 XEvent *event;
4295 struct input_event *ievent;
4297 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4298 Lisp_Object window;
4299 struct frame *f;
4300 struct window *w;
4302 w = scroll_bar_windows[ev->data.l[0]];
4303 scroll_bar_windows[ev->data.l[0]] = NULL;
4305 XSETWINDOW (window, w);
4306 f = XFRAME (w->frame);
4308 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4309 ievent->frame_or_window = window;
4310 ievent->arg = Qnil;
4311 #ifdef USE_GTK
4312 ievent->timestamp = CurrentTime;
4313 #else
4314 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4315 #endif
4316 ievent->part = ev->data.l[1];
4317 ievent->code = ev->data.l[2];
4318 ievent->x = make_number ((int) ev->data.l[3]);
4319 ievent->y = make_number ((int) ev->data.l[4]);
4320 ievent->modifiers = 0;
4324 #ifdef USE_MOTIF
4326 /* Minimum and maximum values used for Motif scroll bars. */
4328 #define XM_SB_MAX 10000000
4331 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4332 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4333 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4335 static void
4336 xm_scroll_callback (widget, client_data, call_data)
4337 Widget widget;
4338 XtPointer client_data, call_data;
4340 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4341 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4342 int part = -1, whole = 0, portion = 0;
4344 switch (cs->reason)
4346 case XmCR_DECREMENT:
4347 bar->dragging = Qnil;
4348 part = scroll_bar_up_arrow;
4349 break;
4351 case XmCR_INCREMENT:
4352 bar->dragging = Qnil;
4353 part = scroll_bar_down_arrow;
4354 break;
4356 case XmCR_PAGE_DECREMENT:
4357 bar->dragging = Qnil;
4358 part = scroll_bar_above_handle;
4359 break;
4361 case XmCR_PAGE_INCREMENT:
4362 bar->dragging = Qnil;
4363 part = scroll_bar_below_handle;
4364 break;
4366 case XmCR_TO_TOP:
4367 bar->dragging = Qnil;
4368 part = scroll_bar_to_top;
4369 break;
4371 case XmCR_TO_BOTTOM:
4372 bar->dragging = Qnil;
4373 part = scroll_bar_to_bottom;
4374 break;
4376 case XmCR_DRAG:
4378 int slider_size;
4380 /* Get the slider size. */
4381 BLOCK_INPUT;
4382 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4383 UNBLOCK_INPUT;
4385 whole = XM_SB_MAX - slider_size;
4386 portion = min (cs->value, whole);
4387 part = scroll_bar_handle;
4388 bar->dragging = make_number (cs->value);
4390 break;
4392 case XmCR_VALUE_CHANGED:
4393 break;
4396 if (part >= 0)
4398 window_being_scrolled = bar->window;
4399 last_scroll_bar_part = part;
4400 x_send_scroll_bar_event (bar->window, part, portion, whole);
4404 #elif defined USE_GTK
4406 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4407 bar widget. DATA is a pointer to the scroll_bar structure. */
4409 static gboolean
4410 xg_scroll_callback (GtkRange *range,
4411 GtkScrollType scroll,
4412 gdouble value,
4413 gpointer user_data)
4415 struct scroll_bar *bar = (struct scroll_bar *) user_data;
4416 gdouble position;
4417 int part = -1, whole = 0, portion = 0;
4418 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4419 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4421 if (xg_ignore_gtk_scrollbar) return FALSE;
4422 position = gtk_adjustment_get_value (adj);
4425 switch (scroll)
4427 case GTK_SCROLL_JUMP:
4428 /* Buttons 1 2 or 3 must be grabbed. */
4429 if (FRAME_X_DISPLAY_INFO (f)->grabbed != 0
4430 && FRAME_X_DISPLAY_INFO (f)->grabbed < (1 << 4))
4432 part = scroll_bar_handle;
4433 whole = adj->upper - adj->page_size;
4434 portion = min ((int)position, whole);
4435 bar->dragging = make_number ((int)portion);
4437 break;
4438 case GTK_SCROLL_STEP_BACKWARD:
4439 part = scroll_bar_up_arrow;
4440 bar->dragging = Qnil;
4441 break;
4442 case GTK_SCROLL_STEP_FORWARD:
4443 part = scroll_bar_down_arrow;
4444 bar->dragging = Qnil;
4445 break;
4446 case GTK_SCROLL_PAGE_BACKWARD:
4447 part = scroll_bar_above_handle;
4448 bar->dragging = Qnil;
4449 break;
4450 case GTK_SCROLL_PAGE_FORWARD:
4451 part = scroll_bar_below_handle;
4452 bar->dragging = Qnil;
4453 break;
4456 if (part >= 0)
4458 window_being_scrolled = bar->window;
4459 last_scroll_bar_part = part;
4460 x_send_scroll_bar_event (bar->window, part, portion, whole);
4463 return FALSE;
4466 /* Callback for button release. Sets dragging to Qnil when dragging is done. */
4468 static gboolean
4469 xg_end_scroll_callback (GtkWidget *widget,
4470 GdkEventButton *event,
4471 gpointer user_data)
4473 struct scroll_bar *bar = (struct scroll_bar *) user_data;
4474 bar->dragging = Qnil;
4475 if (WINDOWP (window_being_scrolled))
4477 x_send_scroll_bar_event (window_being_scrolled,
4478 scroll_bar_end_scroll, 0, 0);
4479 window_being_scrolled = Qnil;
4482 return FALSE;
4486 #else /* not USE_GTK and not USE_MOTIF */
4488 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4489 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4490 scroll bar struct. CALL_DATA is a pointer to a float saying where
4491 the thumb is. */
4493 static void
4494 xaw_jump_callback (widget, client_data, call_data)
4495 Widget widget;
4496 XtPointer client_data, call_data;
4498 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4499 float top = *(float *) call_data;
4500 float shown;
4501 int whole, portion, height;
4502 int part;
4504 /* Get the size of the thumb, a value between 0 and 1. */
4505 BLOCK_INPUT;
4506 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4507 UNBLOCK_INPUT;
4509 whole = 10000000;
4510 portion = shown < 1 ? top * whole : 0;
4512 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4513 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4514 the bottom, so we force the scrolling whenever we see that we're
4515 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4516 we try to ensure that we always stay two pixels away from the
4517 bottom). */
4518 part = scroll_bar_down_arrow;
4519 else
4520 part = scroll_bar_handle;
4522 window_being_scrolled = bar->window;
4523 bar->dragging = make_number (portion);
4524 last_scroll_bar_part = part;
4525 x_send_scroll_bar_event (bar->window, part, portion, whole);
4529 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4530 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4531 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4532 the scroll bar. CALL_DATA is an integer specifying the action that
4533 has taken place. Its magnitude is in the range 0..height of the
4534 scroll bar. Negative values mean scroll towards buffer start.
4535 Values < height of scroll bar mean line-wise movement. */
4537 static void
4538 xaw_scroll_callback (widget, client_data, call_data)
4539 Widget widget;
4540 XtPointer client_data, call_data;
4542 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4543 /* The position really is stored cast to a pointer. */
4544 int position = (long) call_data;
4545 Dimension height;
4546 int part;
4548 /* Get the height of the scroll bar. */
4549 BLOCK_INPUT;
4550 XtVaGetValues (widget, XtNheight, &height, NULL);
4551 UNBLOCK_INPUT;
4553 if (eabs (position) >= height)
4554 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4556 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4557 it maps line-movement to call_data = max(5, height/20). */
4558 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4559 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4560 else
4561 part = scroll_bar_move_ratio;
4563 window_being_scrolled = bar->window;
4564 bar->dragging = Qnil;
4565 last_scroll_bar_part = part;
4566 x_send_scroll_bar_event (bar->window, part, position, height);
4569 #endif /* not USE_GTK and not USE_MOTIF */
4571 #define SCROLL_BAR_NAME "verticalScrollBar"
4573 /* Create the widget for scroll bar BAR on frame F. Record the widget
4574 and X window of the scroll bar in BAR. */
4576 #ifdef USE_GTK
4577 static void
4578 x_create_toolkit_scroll_bar (f, bar)
4579 struct frame *f;
4580 struct scroll_bar *bar;
4582 char *scroll_bar_name = SCROLL_BAR_NAME;
4584 BLOCK_INPUT;
4585 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4586 G_CALLBACK (xg_end_scroll_callback),
4587 scroll_bar_name);
4588 UNBLOCK_INPUT;
4591 #else /* not USE_GTK */
4593 static void
4594 x_create_toolkit_scroll_bar (f, bar)
4595 struct frame *f;
4596 struct scroll_bar *bar;
4598 Window xwindow;
4599 Widget widget;
4600 Arg av[20];
4601 int ac = 0;
4602 char *scroll_bar_name = SCROLL_BAR_NAME;
4603 unsigned long pixel;
4605 BLOCK_INPUT;
4607 #ifdef USE_MOTIF
4608 /* Set resources. Create the widget. */
4609 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4610 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4611 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4612 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4613 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4614 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4615 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4617 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4618 if (pixel != -1)
4620 XtSetArg (av[ac], XmNforeground, pixel);
4621 ++ac;
4624 pixel = f->output_data.x->scroll_bar_background_pixel;
4625 if (pixel != -1)
4627 XtSetArg (av[ac], XmNbackground, pixel);
4628 ++ac;
4631 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4632 scroll_bar_name, av, ac);
4634 /* Add one callback for everything that can happen. */
4635 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4636 (XtPointer) bar);
4637 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4638 (XtPointer) bar);
4639 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4640 (XtPointer) bar);
4641 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4642 (XtPointer) bar);
4643 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4644 (XtPointer) bar);
4645 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4646 (XtPointer) bar);
4647 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4648 (XtPointer) bar);
4650 /* Realize the widget. Only after that is the X window created. */
4651 XtRealizeWidget (widget);
4653 /* Set the cursor to an arrow. I didn't find a resource to do that.
4654 And I'm wondering why it hasn't an arrow cursor by default. */
4655 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4656 f->output_data.x->nontext_cursor);
4658 #else /* !USE_MOTIF i.e. use Xaw */
4660 /* Set resources. Create the widget. The background of the
4661 Xaw3d scroll bar widget is a little bit light for my taste.
4662 We don't alter it here to let users change it according
4663 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4664 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4665 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4666 /* For smoother scrolling with Xaw3d -sm */
4667 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4669 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4670 if (pixel != -1)
4672 XtSetArg (av[ac], XtNforeground, pixel);
4673 ++ac;
4676 pixel = f->output_data.x->scroll_bar_background_pixel;
4677 if (pixel != -1)
4679 XtSetArg (av[ac], XtNbackground, pixel);
4680 ++ac;
4683 /* Top/bottom shadow colors. */
4685 /* Allocate them, if necessary. */
4686 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4688 pixel = f->output_data.x->scroll_bar_background_pixel;
4689 if (pixel != -1)
4691 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4692 FRAME_X_COLORMAP (f),
4693 &pixel, 1.2, 0x8000))
4694 pixel = -1;
4695 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4698 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4700 pixel = f->output_data.x->scroll_bar_background_pixel;
4701 if (pixel != -1)
4703 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4704 FRAME_X_COLORMAP (f),
4705 &pixel, 0.6, 0x4000))
4706 pixel = -1;
4707 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4711 #ifdef XtNbeNiceToColormap
4712 /* Tell the toolkit about them. */
4713 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4714 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4715 /* We tried to allocate a color for the top/bottom shadow, and
4716 failed, so tell Xaw3d to use dithering instead. */
4718 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4719 ++ac;
4721 else
4722 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4723 be more consistent with other emacs 3d colors, and since Xaw3d is
4724 not good at dealing with allocation failure. */
4726 /* This tells Xaw3d to use real colors instead of dithering for
4727 the shadows. */
4728 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4729 ++ac;
4731 /* Specify the colors. */
4732 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4733 if (pixel != -1)
4735 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4736 ++ac;
4738 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4739 if (pixel != -1)
4741 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4742 ++ac;
4745 #endif
4747 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4748 f->output_data.x->edit_widget, av, ac);
4751 char *initial = "";
4752 char *val = initial;
4753 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4754 #ifdef XtNarrowScrollbars
4755 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4756 #endif
4757 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4758 if (xaw3d_arrow_scroll || val == initial)
4759 { /* ARROW_SCROLL */
4760 xaw3d_arrow_scroll = True;
4761 /* Isn't that just a personal preference ? --Stef */
4762 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4766 /* Define callbacks. */
4767 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4768 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4769 (XtPointer) bar);
4771 /* Realize the widget. Only after that is the X window created. */
4772 XtRealizeWidget (widget);
4774 #endif /* !USE_MOTIF */
4776 /* Install an action hook that lets us detect when the user
4777 finishes interacting with a scroll bar. */
4778 if (action_hook_id == 0)
4779 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4781 /* Remember X window and widget in the scroll bar vector. */
4782 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4783 xwindow = XtWindow (widget);
4784 bar->x_window = xwindow;
4786 UNBLOCK_INPUT;
4788 #endif /* not USE_GTK */
4791 /* Set the thumb size and position of scroll bar BAR. We are currently
4792 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4794 #ifdef USE_GTK
4795 static void
4796 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4797 struct scroll_bar *bar;
4798 int portion, position, whole;
4800 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4803 #else /* not USE_GTK */
4804 static void
4805 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4806 struct scroll_bar *bar;
4807 int portion, position, whole;
4809 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4810 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4811 float top, shown;
4813 BLOCK_INPUT;
4815 #ifdef USE_MOTIF
4817 /* We use an estimate of 30 chars per line rather than the real
4818 `portion' value. This has the disadvantage that the thumb size
4819 is not very representative, but it makes our life a lot easier.
4820 Otherwise, we have to constantly adjust the thumb size, which
4821 we can't always do quickly enough: while dragging, the size of
4822 the thumb might prevent the user from dragging the thumb all the
4823 way to the end. but Motif and some versions of Xaw3d don't allow
4824 updating the thumb size while dragging. Also, even if we can update
4825 its size, the update will often happen too late.
4826 If you don't believe it, check out revision 1.650 of xterm.c to see
4827 what hoops we were going through and the still poor behavior we got. */
4828 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4829 /* When the thumb is at the bottom, position == whole.
4830 So we need to increase `whole' to make space for the thumb. */
4831 whole += portion;
4833 if (whole <= 0)
4834 top = 0, shown = 1;
4835 else
4837 top = (float) position / whole;
4838 shown = (float) portion / whole;
4841 if (NILP (bar->dragging))
4843 int size, value;
4845 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4846 is the scroll bar's maximum and MIN is the scroll bar's minimum
4847 value. */
4848 size = shown * XM_SB_MAX;
4849 size = min (size, XM_SB_MAX);
4850 size = max (size, 1);
4852 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4853 value = top * XM_SB_MAX;
4854 value = min (value, XM_SB_MAX - size);
4856 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4858 #else /* !USE_MOTIF i.e. use Xaw */
4860 if (whole == 0)
4861 top = 0, shown = 1;
4862 else
4864 top = (float) position / whole;
4865 shown = (float) portion / whole;
4869 float old_top, old_shown;
4870 Dimension height;
4871 XtVaGetValues (widget,
4872 XtNtopOfThumb, &old_top,
4873 XtNshown, &old_shown,
4874 XtNheight, &height,
4875 NULL);
4877 /* Massage the top+shown values. */
4878 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4879 top = max (0, min (1, top));
4880 else
4881 top = old_top;
4882 /* Keep two pixels available for moving the thumb down. */
4883 shown = max (0, min (1 - top - (2.0 / height), shown));
4885 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4886 check that your system's configuration file contains a define
4887 for `NARROWPROTO'. See s/freebsd.h for an example. */
4888 if (top != old_top || shown != old_shown)
4890 if (NILP (bar->dragging))
4891 XawScrollbarSetThumb (widget, top, shown);
4892 else
4894 /* Try to make the scrolling a tad smoother. */
4895 if (!xaw3d_pick_top)
4896 shown = min (shown, old_shown);
4898 XawScrollbarSetThumb (widget, top, shown);
4902 #endif /* !USE_MOTIF */
4904 UNBLOCK_INPUT;
4906 #endif /* not USE_GTK */
4908 #endif /* USE_TOOLKIT_SCROLL_BARS */
4912 /************************************************************************
4913 Scroll bars, general
4914 ************************************************************************/
4916 /* Create a scroll bar and return the scroll bar vector for it. W is
4917 the Emacs window on which to create the scroll bar. TOP, LEFT,
4918 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4919 scroll bar. */
4921 static struct scroll_bar *
4922 x_scroll_bar_create (w, top, left, width, height)
4923 struct window *w;
4924 int top, left, width, height;
4926 struct frame *f = XFRAME (w->frame);
4927 struct scroll_bar *bar
4928 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4930 BLOCK_INPUT;
4932 #ifdef USE_TOOLKIT_SCROLL_BARS
4933 x_create_toolkit_scroll_bar (f, bar);
4934 #else /* not USE_TOOLKIT_SCROLL_BARS */
4936 XSetWindowAttributes a;
4937 unsigned long mask;
4938 Window window;
4940 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4941 if (a.background_pixel == -1)
4942 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4944 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4945 | ButtonMotionMask | PointerMotionHintMask
4946 | ExposureMask);
4947 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4949 mask = (CWBackPixel | CWEventMask | CWCursor);
4951 /* Clear the area of W that will serve as a scroll bar. This is
4952 for the case that a window has been split horizontally. In
4953 this case, no clear_frame is generated to reduce flickering. */
4954 if (width > 0 && height > 0)
4955 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4956 left, top, width,
4957 window_box_height (w), False);
4959 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4960 /* Position and size of scroll bar. */
4961 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4962 top,
4963 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4964 height,
4965 /* Border width, depth, class, and visual. */
4967 CopyFromParent,
4968 CopyFromParent,
4969 CopyFromParent,
4970 /* Attributes. */
4971 mask, &a);
4972 bar->x_window = window;
4974 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4976 XSETWINDOW (bar->window, w);
4977 bar->top = top;
4978 bar->left = left;
4979 bar->width = width;
4980 bar->height = height;
4981 bar->start = 0;
4982 bar->end = 0;
4983 bar->dragging = Qnil;
4984 bar->fringe_extended_p = 0;
4986 /* Add bar to its frame's list of scroll bars. */
4987 bar->next = FRAME_SCROLL_BARS (f);
4988 bar->prev = Qnil;
4989 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4990 if (!NILP (bar->next))
4991 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4993 /* Map the window/widget. */
4994 #ifdef USE_TOOLKIT_SCROLL_BARS
4996 #ifdef USE_GTK
4997 xg_update_scrollbar_pos (f,
4998 bar->x_window,
4999 top,
5000 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5001 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5002 max (height, 1));
5003 xg_show_scroll_bar (bar->x_window);
5004 #else /* not USE_GTK */
5005 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5006 XtConfigureWidget (scroll_bar,
5007 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5008 top,
5009 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5010 max (height, 1), 0);
5011 XtMapWidget (scroll_bar);
5012 #endif /* not USE_GTK */
5014 #else /* not USE_TOOLKIT_SCROLL_BARS */
5015 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
5016 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5018 UNBLOCK_INPUT;
5019 return bar;
5023 #ifndef USE_TOOLKIT_SCROLL_BARS
5025 /* Draw BAR's handle in the proper position.
5027 If the handle is already drawn from START to END, don't bother
5028 redrawing it, unless REBUILD is non-zero; in that case, always
5029 redraw it. (REBUILD is handy for drawing the handle after expose
5030 events.)
5032 Normally, we want to constrain the start and end of the handle to
5033 fit inside its rectangle, but if the user is dragging the scroll
5034 bar handle, we want to let them drag it down all the way, so that
5035 the bar's top is as far down as it goes; otherwise, there's no way
5036 to move to the very end of the buffer. */
5038 static void
5039 x_scroll_bar_set_handle (bar, start, end, rebuild)
5040 struct scroll_bar *bar;
5041 int start, end;
5042 int rebuild;
5044 int dragging = ! NILP (bar->dragging);
5045 Window w = bar->x_window;
5046 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5047 GC gc = f->output_data.x->normal_gc;
5049 /* If the display is already accurate, do nothing. */
5050 if (! rebuild
5051 && start == bar->start
5052 && end == bar->end)
5053 return;
5055 BLOCK_INPUT;
5058 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5059 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5060 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5062 /* Make sure the values are reasonable, and try to preserve
5063 the distance between start and end. */
5065 int length = end - start;
5067 if (start < 0)
5068 start = 0;
5069 else if (start > top_range)
5070 start = top_range;
5071 end = start + length;
5073 if (end < start)
5074 end = start;
5075 else if (end > top_range && ! dragging)
5076 end = top_range;
5079 /* Store the adjusted setting in the scroll bar. */
5080 bar->start = start;
5081 bar->end = end;
5083 /* Clip the end position, just for display. */
5084 if (end > top_range)
5085 end = top_range;
5087 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5088 below top positions, to make sure the handle is always at least
5089 that many pixels tall. */
5090 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5092 /* Draw the empty space above the handle. Note that we can't clear
5093 zero-height areas; that means "clear to end of window." */
5094 if (0 < start)
5095 x_clear_area (FRAME_X_DISPLAY (f), w,
5096 /* x, y, width, height, and exposures. */
5097 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5098 VERTICAL_SCROLL_BAR_TOP_BORDER,
5099 inside_width, start,
5100 False);
5102 /* Change to proper foreground color if one is specified. */
5103 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5104 XSetForeground (FRAME_X_DISPLAY (f), gc,
5105 f->output_data.x->scroll_bar_foreground_pixel);
5107 /* Draw the handle itself. */
5108 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5109 /* x, y, width, height */
5110 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5111 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5112 inside_width, end - start);
5114 /* Restore the foreground color of the GC if we changed it above. */
5115 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5116 XSetForeground (FRAME_X_DISPLAY (f), gc,
5117 FRAME_FOREGROUND_PIXEL (f));
5119 /* Draw the empty space below the handle. Note that we can't
5120 clear zero-height areas; that means "clear to end of window." */
5121 if (end < inside_height)
5122 x_clear_area (FRAME_X_DISPLAY (f), w,
5123 /* x, y, width, height, and exposures. */
5124 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5125 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5126 inside_width, inside_height - end,
5127 False);
5131 UNBLOCK_INPUT;
5134 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5136 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5137 nil. */
5139 static void
5140 x_scroll_bar_remove (bar)
5141 struct scroll_bar *bar;
5143 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5144 BLOCK_INPUT;
5146 #ifdef USE_TOOLKIT_SCROLL_BARS
5147 #ifdef USE_GTK
5148 xg_remove_scroll_bar (f, bar->x_window);
5149 #else /* not USE_GTK */
5150 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5151 #endif /* not USE_GTK */
5152 #else
5153 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5154 #endif
5156 /* Disassociate this scroll bar from its window. */
5157 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5159 UNBLOCK_INPUT;
5163 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5164 that we are displaying PORTION characters out of a total of WHOLE
5165 characters, starting at POSITION. If WINDOW has no scroll bar,
5166 create one. */
5168 static void
5169 XTset_vertical_scroll_bar (w, portion, whole, position)
5170 struct window *w;
5171 int portion, whole, position;
5173 struct frame *f = XFRAME (w->frame);
5174 struct scroll_bar *bar;
5175 int top, height, left, sb_left, width, sb_width;
5176 int window_y, window_height;
5177 #ifdef USE_TOOLKIT_SCROLL_BARS
5178 int fringe_extended_p;
5179 #endif
5181 /* Get window dimensions. */
5182 window_box (w, -1, 0, &window_y, 0, &window_height);
5183 top = window_y;
5184 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5185 height = window_height;
5187 /* Compute the left edge of the scroll bar area. */
5188 left = WINDOW_SCROLL_BAR_AREA_X (w);
5190 /* Compute the width of the scroll bar which might be less than
5191 the width of the area reserved for the scroll bar. */
5192 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5193 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5194 else
5195 sb_width = width;
5197 /* Compute the left edge of the scroll bar. */
5198 #ifdef USE_TOOLKIT_SCROLL_BARS
5199 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5200 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5201 else
5202 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5203 #else
5204 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5205 sb_left = left + width - sb_width;
5206 else
5207 sb_left = left;
5208 #endif
5210 #ifdef USE_TOOLKIT_SCROLL_BARS
5211 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5212 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5213 && WINDOW_LEFT_FRINGE_WIDTH (w)
5214 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5215 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5216 else
5217 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5218 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5219 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5220 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5221 #endif
5223 /* Does the scroll bar exist yet? */
5224 if (NILP (w->vertical_scroll_bar))
5226 if (width > 0 && height > 0)
5228 BLOCK_INPUT;
5229 #ifdef USE_TOOLKIT_SCROLL_BARS
5230 if (fringe_extended_p)
5231 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5232 sb_left, top, sb_width, height, False);
5233 else
5234 #endif
5235 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5236 left, top, width, height, False);
5237 UNBLOCK_INPUT;
5240 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5242 else
5244 /* It may just need to be moved and resized. */
5245 unsigned int mask = 0;
5247 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5249 BLOCK_INPUT;
5251 if (sb_left != bar->left)
5252 mask |= CWX;
5253 if (top != bar->top)
5254 mask |= CWY;
5255 if (sb_width != bar->width)
5256 mask |= CWWidth;
5257 if (height != bar->height)
5258 mask |= CWHeight;
5260 #ifdef USE_TOOLKIT_SCROLL_BARS
5262 /* Move/size the scroll bar widget. */
5263 if (mask || bar->fringe_extended_p != fringe_extended_p)
5265 /* Since toolkit scroll bars are smaller than the space reserved
5266 for them on the frame, we have to clear "under" them. */
5267 if (width > 0 && height > 0)
5269 if (fringe_extended_p)
5270 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5271 sb_left, top, sb_width, height, False);
5272 else
5273 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5274 left, top, width, height, False);
5276 #ifdef USE_GTK
5277 xg_update_scrollbar_pos (f,
5278 bar->x_window,
5279 top,
5280 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5281 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5282 max (height, 1));
5283 #else /* not USE_GTK */
5284 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5285 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5286 top,
5287 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5288 max (height, 1), 0);
5289 #endif /* not USE_GTK */
5291 #else /* not USE_TOOLKIT_SCROLL_BARS */
5293 /* Clear areas not covered by the scroll bar because of
5294 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5295 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5297 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5298 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5299 height, False);
5300 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5301 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5302 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5303 height, False);
5306 /* Clear areas not covered by the scroll bar because it's not as
5307 wide as the area reserved for it. This makes sure a
5308 previous mode line display is cleared after C-x 2 C-x 1, for
5309 example. */
5311 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5312 int rest = area_width - sb_width;
5313 if (rest > 0 && height > 0)
5315 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5316 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5317 left + area_width - rest, top,
5318 rest, height, False);
5319 else
5320 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5321 left, top, rest, height, False);
5325 /* Move/size the scroll bar window. */
5326 if (mask)
5328 XWindowChanges wc;
5330 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5331 wc.y = top;
5332 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5333 wc.height = height;
5334 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5335 mask, &wc);
5338 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5340 /* Remember new settings. */
5341 bar->left = sb_left;
5342 bar->top = top;
5343 bar->width = sb_width;
5344 bar->height = height;
5346 UNBLOCK_INPUT;
5349 #ifdef USE_TOOLKIT_SCROLL_BARS
5350 bar->fringe_extended_p = fringe_extended_p;
5352 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5353 #else /* not USE_TOOLKIT_SCROLL_BARS */
5354 /* Set the scroll bar's current state, unless we're currently being
5355 dragged. */
5356 if (NILP (bar->dragging))
5358 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5360 if (whole == 0)
5361 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5362 else
5364 int start = ((double) position * top_range) / whole;
5365 int end = ((double) (position + portion) * top_range) / whole;
5366 x_scroll_bar_set_handle (bar, start, end, 0);
5369 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5371 XSETVECTOR (w->vertical_scroll_bar, bar);
5375 /* The following three hooks are used when we're doing a thorough
5376 redisplay of the frame. We don't explicitly know which scroll bars
5377 are going to be deleted, because keeping track of when windows go
5378 away is a real pain - "Can you say set-window-configuration, boys
5379 and girls?" Instead, we just assert at the beginning of redisplay
5380 that *all* scroll bars are to be removed, and then save a scroll bar
5381 from the fiery pit when we actually redisplay its window. */
5383 /* Arrange for all scroll bars on FRAME to be removed at the next call
5384 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5385 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5387 static void
5388 XTcondemn_scroll_bars (frame)
5389 FRAME_PTR frame;
5391 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5392 while (! NILP (FRAME_SCROLL_BARS (frame)))
5394 Lisp_Object bar;
5395 bar = FRAME_SCROLL_BARS (frame);
5396 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5397 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5398 XSCROLL_BAR (bar)->prev = Qnil;
5399 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5400 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5401 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5406 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5407 Note that WINDOW isn't necessarily condemned at all. */
5409 static void
5410 XTredeem_scroll_bar (window)
5411 struct window *window;
5413 struct scroll_bar *bar;
5414 struct frame *f;
5416 /* We can't redeem this window's scroll bar if it doesn't have one. */
5417 if (NILP (window->vertical_scroll_bar))
5418 abort ();
5420 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5422 /* Unlink it from the condemned list. */
5423 f = XFRAME (WINDOW_FRAME (window));
5424 if (NILP (bar->prev))
5426 /* If the prev pointer is nil, it must be the first in one of
5427 the lists. */
5428 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5429 /* It's not condemned. Everything's fine. */
5430 return;
5431 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5432 window->vertical_scroll_bar))
5433 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5434 else
5435 /* If its prev pointer is nil, it must be at the front of
5436 one or the other! */
5437 abort ();
5439 else
5440 XSCROLL_BAR (bar->prev)->next = bar->next;
5442 if (! NILP (bar->next))
5443 XSCROLL_BAR (bar->next)->prev = bar->prev;
5445 bar->next = FRAME_SCROLL_BARS (f);
5446 bar->prev = Qnil;
5447 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5448 if (! NILP (bar->next))
5449 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5452 /* Remove all scroll bars on FRAME that haven't been saved since the
5453 last call to `*condemn_scroll_bars_hook'. */
5455 static void
5456 XTjudge_scroll_bars (f)
5457 FRAME_PTR f;
5459 Lisp_Object bar, next;
5461 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5463 /* Clear out the condemned list now so we won't try to process any
5464 more events on the hapless scroll bars. */
5465 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5467 for (; ! NILP (bar); bar = next)
5469 struct scroll_bar *b = XSCROLL_BAR (bar);
5471 x_scroll_bar_remove (b);
5473 next = b->next;
5474 b->next = b->prev = Qnil;
5477 /* Now there should be no references to the condemned scroll bars,
5478 and they should get garbage-collected. */
5482 #ifndef USE_TOOLKIT_SCROLL_BARS
5483 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5484 is a no-op when using toolkit scroll bars.
5486 This may be called from a signal handler, so we have to ignore GC
5487 mark bits. */
5489 static void
5490 x_scroll_bar_expose (bar, event)
5491 struct scroll_bar *bar;
5492 XEvent *event;
5494 Window w = bar->x_window;
5495 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5496 GC gc = f->output_data.x->normal_gc;
5497 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5499 BLOCK_INPUT;
5501 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5503 /* Switch to scroll bar foreground color. */
5504 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5505 XSetForeground (FRAME_X_DISPLAY (f), gc,
5506 f->output_data.x->scroll_bar_foreground_pixel);
5508 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5509 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5511 /* x, y, width, height */
5512 0, 0,
5513 bar->width - 1 - width_trim - width_trim,
5514 bar->height - 1);
5516 /* Restore the foreground color of the GC if we changed it above. */
5517 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5518 XSetForeground (FRAME_X_DISPLAY (f), gc,
5519 FRAME_FOREGROUND_PIXEL (f));
5521 UNBLOCK_INPUT;
5524 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5526 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5527 is set to something other than NO_EVENT, it is enqueued.
5529 This may be called from a signal handler, so we have to ignore GC
5530 mark bits. */
5533 static void
5534 x_scroll_bar_handle_click (bar, event, emacs_event)
5535 struct scroll_bar *bar;
5536 XEvent *event;
5537 struct input_event *emacs_event;
5539 if (! WINDOWP (bar->window))
5540 abort ();
5542 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5543 emacs_event->code = event->xbutton.button - Button1;
5544 emacs_event->modifiers
5545 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5546 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5547 event->xbutton.state)
5548 | (event->type == ButtonRelease
5549 ? up_modifier
5550 : down_modifier));
5551 emacs_event->frame_or_window = bar->window;
5552 emacs_event->arg = Qnil;
5553 emacs_event->timestamp = event->xbutton.time;
5555 int top_range
5556 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5557 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5559 if (y < 0) y = 0;
5560 if (y > top_range) y = top_range;
5562 if (y < bar->start)
5563 emacs_event->part = scroll_bar_above_handle;
5564 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5565 emacs_event->part = scroll_bar_handle;
5566 else
5567 emacs_event->part = scroll_bar_below_handle;
5569 #ifndef USE_TOOLKIT_SCROLL_BARS
5570 /* If the user has released the handle, set it to its final position. */
5571 if (event->type == ButtonRelease
5572 && ! NILP (bar->dragging))
5574 int new_start = y - XINT (bar->dragging);
5575 int new_end = new_start + bar->end - bar->start;
5577 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5578 bar->dragging = Qnil;
5580 #endif
5582 XSETINT (emacs_event->x, y);
5583 XSETINT (emacs_event->y, top_range);
5587 #ifndef USE_TOOLKIT_SCROLL_BARS
5589 /* Handle some mouse motion while someone is dragging the scroll bar.
5591 This may be called from a signal handler, so we have to ignore GC
5592 mark bits. */
5594 static void
5595 x_scroll_bar_note_movement (bar, event)
5596 struct scroll_bar *bar;
5597 XEvent *event;
5599 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5601 last_mouse_movement_time = event->xmotion.time;
5603 f->mouse_moved = 1;
5604 XSETVECTOR (last_mouse_scroll_bar, bar);
5606 /* If we're dragging the bar, display it. */
5607 if (! NILP (bar->dragging))
5609 /* Where should the handle be now? */
5610 int new_start = event->xmotion.y - XINT (bar->dragging);
5612 if (new_start != bar->start)
5614 int new_end = new_start + bar->end - bar->start;
5616 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5621 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5623 /* Return information to the user about the current position of the mouse
5624 on the scroll bar. */
5626 static void
5627 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5628 FRAME_PTR *fp;
5629 Lisp_Object *bar_window;
5630 enum scroll_bar_part *part;
5631 Lisp_Object *x, *y;
5632 unsigned long *time;
5634 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5635 Window w = bar->x_window;
5636 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5637 int win_x, win_y;
5638 Window dummy_window;
5639 int dummy_coord;
5640 unsigned int dummy_mask;
5642 BLOCK_INPUT;
5644 /* Get the mouse's position relative to the scroll bar window, and
5645 report that. */
5646 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5648 /* Root, child, root x and root y. */
5649 &dummy_window, &dummy_window,
5650 &dummy_coord, &dummy_coord,
5652 /* Position relative to scroll bar. */
5653 &win_x, &win_y,
5655 /* Mouse buttons and modifier keys. */
5656 &dummy_mask))
5658 else
5660 int top_range
5661 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5663 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5665 if (! NILP (bar->dragging))
5666 win_y -= XINT (bar->dragging);
5668 if (win_y < 0)
5669 win_y = 0;
5670 if (win_y > top_range)
5671 win_y = top_range;
5673 *fp = f;
5674 *bar_window = bar->window;
5676 if (! NILP (bar->dragging))
5677 *part = scroll_bar_handle;
5678 else if (win_y < bar->start)
5679 *part = scroll_bar_above_handle;
5680 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5681 *part = scroll_bar_handle;
5682 else
5683 *part = scroll_bar_below_handle;
5685 XSETINT (*x, win_y);
5686 XSETINT (*y, top_range);
5688 f->mouse_moved = 0;
5689 last_mouse_scroll_bar = Qnil;
5692 *time = last_mouse_movement_time;
5694 UNBLOCK_INPUT;
5698 /* The screen has been cleared so we may have changed foreground or
5699 background colors, and the scroll bars may need to be redrawn.
5700 Clear out the scroll bars, and ask for expose events, so we can
5701 redraw them. */
5703 void
5704 x_scroll_bar_clear (f)
5705 FRAME_PTR f;
5707 #ifndef USE_TOOLKIT_SCROLL_BARS
5708 Lisp_Object bar;
5710 /* We can have scroll bars even if this is 0,
5711 if we just turned off scroll bar mode.
5712 But in that case we should not clear them. */
5713 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5714 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5715 bar = XSCROLL_BAR (bar)->next)
5716 XClearArea (FRAME_X_DISPLAY (f),
5717 XSCROLL_BAR (bar)->x_window,
5718 0, 0, 0, 0, True);
5719 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5723 /* The main X event-reading loop - XTread_socket. */
5725 /* This holds the state XLookupString needs to implement dead keys
5726 and other tricks known as "compose processing". _X Window System_
5727 says that a portable program can't use this, but Stephen Gildea assures
5728 me that letting the compiler initialize it to zeros will work okay.
5730 This must be defined outside of XTread_socket, for the same reasons
5731 given for enter_timestamp, above. */
5733 static XComposeStatus compose_status;
5735 /* Record the last 100 characters stored
5736 to help debug the loss-of-chars-during-GC problem. */
5738 static int temp_index;
5739 static short temp_buffer[100];
5741 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5742 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5743 temp_index = 0; \
5744 temp_buffer[temp_index++] = (keysym)
5746 /* Set this to nonzero to fake an "X I/O error"
5747 on a particular display. */
5749 struct x_display_info *XTread_socket_fake_io_error;
5751 /* When we find no input here, we occasionally do a no-op command
5752 to verify that the X server is still running and we can still talk with it.
5753 We try all the open displays, one by one.
5754 This variable is used for cycling thru the displays. */
5756 static struct x_display_info *next_noop_dpyinfo;
5758 #define SET_SAVED_MENU_EVENT(size) \
5759 do \
5761 if (f->output_data.x->saved_menu_event == 0) \
5762 f->output_data.x->saved_menu_event \
5763 = (XEvent *) xmalloc (sizeof (XEvent)); \
5764 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5765 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5766 XSETFRAME (inev.ie.frame_or_window, f); \
5768 while (0)
5770 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5771 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5774 enum
5776 X_EVENT_NORMAL,
5777 X_EVENT_GOTO_OUT,
5778 X_EVENT_DROP
5781 /* Filter events for the current X input method.
5782 DPYINFO is the display this event is for.
5783 EVENT is the X event to filter.
5785 Returns non-zero if the event was filtered, caller shall not process
5786 this event further.
5787 Returns zero if event is wasn't filtered. */
5789 #ifdef HAVE_X_I18N
5790 static int
5791 x_filter_event (dpyinfo, event)
5792 struct x_display_info *dpyinfo;
5793 XEvent *event;
5795 /* XFilterEvent returns non-zero if the input method has
5796 consumed the event. We pass the frame's X window to
5797 XFilterEvent because that's the one for which the IC
5798 was created. */
5800 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5801 event->xclient.window);
5803 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5805 #endif
5807 #ifdef USE_GTK
5808 static int current_count;
5809 static int current_finish;
5810 static struct input_event *current_hold_quit;
5812 /* This is the filter function invoked by the GTK event loop.
5813 It is invoked before the XEvent is translated to a GdkEvent,
5814 so we have a chance to act on the event before GTK. */
5815 static GdkFilterReturn
5816 event_handler_gdk (gxev, ev, data)
5817 GdkXEvent *gxev;
5818 GdkEvent *ev;
5819 gpointer data;
5821 XEvent *xev = (XEvent *) gxev;
5823 BLOCK_INPUT;
5824 if (current_count >= 0)
5826 struct x_display_info *dpyinfo;
5828 dpyinfo = x_display_info_for_display (xev->xany.display);
5830 #ifdef HAVE_X_I18N
5831 /* Filter events for the current X input method.
5832 GTK calls XFilterEvent but not for key press and release,
5833 so we do it here. */
5834 if ((xev->type == KeyPress || xev->type == KeyRelease)
5835 && dpyinfo
5836 && x_filter_event (dpyinfo, xev))
5838 UNBLOCK_INPUT;
5839 return GDK_FILTER_REMOVE;
5841 #endif
5843 if (! dpyinfo)
5844 current_finish = X_EVENT_NORMAL;
5845 else
5846 current_count +=
5847 handle_one_xevent (dpyinfo, xev, &current_finish,
5848 current_hold_quit);
5850 else
5851 current_finish = x_dispatch_event (xev, xev->xany.display);
5853 UNBLOCK_INPUT;
5855 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5856 return GDK_FILTER_REMOVE;
5858 return GDK_FILTER_CONTINUE;
5860 #endif /* USE_GTK */
5863 /* Handles the XEvent EVENT on display DPYINFO.
5865 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5866 *FINISH is zero if caller should continue reading events.
5867 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5869 We return the number of characters stored into the buffer. */
5871 static int
5872 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5873 struct x_display_info *dpyinfo;
5874 XEvent *eventp;
5875 int *finish;
5876 struct input_event *hold_quit;
5878 union {
5879 struct input_event ie;
5880 struct selection_input_event sie;
5881 } inev;
5882 int count = 0;
5883 int do_help = 0;
5884 int nbytes = 0;
5885 struct frame *f = NULL;
5886 struct coding_system coding;
5887 XEvent event = *eventp;
5889 *finish = X_EVENT_NORMAL;
5891 EVENT_INIT (inev.ie);
5892 inev.ie.kind = NO_EVENT;
5893 inev.ie.arg = Qnil;
5895 if (pending_event_wait.eventtype == event.type)
5896 pending_event_wait.eventtype = 0; /* Indicates we got it. */
5898 switch (event.type)
5900 case ClientMessage:
5902 if (event.xclient.message_type
5903 == dpyinfo->Xatom_wm_protocols
5904 && event.xclient.format == 32)
5906 if (event.xclient.data.l[0]
5907 == dpyinfo->Xatom_wm_take_focus)
5909 /* Use x_any_window_to_frame because this
5910 could be the shell widget window
5911 if the frame has no title bar. */
5912 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5913 #ifdef HAVE_X_I18N
5914 /* Not quite sure this is needed -pd */
5915 if (f && FRAME_XIC (f))
5916 XSetICFocus (FRAME_XIC (f));
5917 #endif
5918 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5919 instructs the WM to set the input focus automatically for
5920 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5921 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5922 it has set the focus. So, XSetInputFocus below is not
5923 needed.
5925 The call to XSetInputFocus below has also caused trouble. In
5926 cases where the XSetInputFocus done by the WM and the one
5927 below are temporally close (on a fast machine), the call
5928 below can generate additional FocusIn events which confuse
5929 Emacs. */
5931 /* Since we set WM_TAKE_FOCUS, we must call
5932 XSetInputFocus explicitly. But not if f is null,
5933 since that might be an event for a deleted frame. */
5934 if (f)
5936 Display *d = event.xclient.display;
5937 /* Catch and ignore errors, in case window has been
5938 iconified by a window manager such as GWM. */
5939 x_catch_errors (d);
5940 XSetInputFocus (d, event.xclient.window,
5941 /* The ICCCM says this is
5942 the only valid choice. */
5943 RevertToParent,
5944 event.xclient.data.l[1]);
5945 /* This is needed to detect the error
5946 if there is an error. */
5947 XSync (d, False);
5948 x_uncatch_errors ();
5950 /* Not certain about handling scroll bars here */
5951 #endif /* 0 */
5952 goto done;
5955 if (event.xclient.data.l[0]
5956 == dpyinfo->Xatom_wm_save_yourself)
5958 /* Save state modify the WM_COMMAND property to
5959 something which can reinstate us. This notifies
5960 the session manager, who's looking for such a
5961 PropertyNotify. Can restart processing when
5962 a keyboard or mouse event arrives. */
5963 /* If we have a session manager, don't set this.
5964 KDE will then start two Emacsen, one for the
5965 session manager and one for this. */
5966 #ifdef HAVE_X_SM
5967 if (! x_session_have_connection ())
5968 #endif
5970 f = x_top_window_to_frame (dpyinfo,
5971 event.xclient.window);
5972 /* This is just so we only give real data once
5973 for a single Emacs process. */
5974 if (f == SELECTED_FRAME ())
5975 XSetCommand (FRAME_X_DISPLAY (f),
5976 event.xclient.window,
5977 initial_argv, initial_argc);
5978 else if (f)
5979 XSetCommand (FRAME_X_DISPLAY (f),
5980 event.xclient.window,
5981 0, 0);
5983 goto done;
5986 if (event.xclient.data.l[0]
5987 == dpyinfo->Xatom_wm_delete_window)
5989 f = x_any_window_to_frame (dpyinfo,
5990 event.xclient.window);
5991 if (!f)
5992 goto OTHER; /* May be a dialog that is to be removed */
5994 inev.ie.kind = DELETE_WINDOW_EVENT;
5995 XSETFRAME (inev.ie.frame_or_window, f);
5996 goto done;
5999 goto done;
6002 if (event.xclient.message_type
6003 == dpyinfo->Xatom_wm_configure_denied)
6005 goto done;
6008 if (event.xclient.message_type
6009 == dpyinfo->Xatom_wm_window_moved)
6011 int new_x, new_y;
6012 f = x_window_to_frame (dpyinfo, event.xclient.window);
6014 new_x = event.xclient.data.s[0];
6015 new_y = event.xclient.data.s[1];
6017 if (f)
6019 f->left_pos = new_x;
6020 f->top_pos = new_y;
6022 goto done;
6025 #ifdef HACK_EDITRES
6026 if (event.xclient.message_type
6027 == dpyinfo->Xatom_editres)
6029 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6030 if (f)
6031 _XEditResCheckMessages (f->output_data.x->widget, NULL,
6032 &event, NULL);
6033 goto done;
6035 #endif /* HACK_EDITRES */
6037 if ((event.xclient.message_type
6038 == dpyinfo->Xatom_DONE)
6039 || (event.xclient.message_type
6040 == dpyinfo->Xatom_PAGE))
6042 /* Ghostview job completed. Kill it. We could
6043 reply with "Next" if we received "Page", but we
6044 currently never do because we are interested in
6045 images, only, which should have 1 page. */
6046 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
6047 f = x_window_to_frame (dpyinfo, event.xclient.window);
6048 if (!f)
6049 goto OTHER;
6050 x_kill_gs_process (pixmap, f);
6051 expose_frame (f, 0, 0, 0, 0);
6052 goto done;
6055 #ifdef USE_TOOLKIT_SCROLL_BARS
6056 /* Scroll bar callbacks send a ClientMessage from which
6057 we construct an input_event. */
6058 if (event.xclient.message_type
6059 == dpyinfo->Xatom_Scrollbar)
6061 x_scroll_bar_to_input_event (&event, &inev.ie);
6062 *finish = X_EVENT_GOTO_OUT;
6063 goto done;
6065 #endif /* USE_TOOLKIT_SCROLL_BARS */
6067 /* XEmbed messages from the embedder (if any). */
6068 if (event.xclient.message_type
6069 == dpyinfo->Xatom_XEMBED)
6071 enum xembed_message msg = event.xclient.data.l[1];
6072 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6073 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6075 *finish = X_EVENT_GOTO_OUT;
6076 goto done;
6079 xft_settings_event (dpyinfo, &event);
6081 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6082 if (!f)
6083 goto OTHER;
6084 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
6085 *finish = X_EVENT_DROP;
6087 break;
6089 case SelectionNotify:
6090 last_user_time = event.xselection.time;
6091 #ifdef USE_X_TOOLKIT
6092 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6093 goto OTHER;
6094 #endif /* not USE_X_TOOLKIT */
6095 x_handle_selection_notify (&event.xselection);
6096 break;
6098 case SelectionClear: /* Someone has grabbed ownership. */
6099 last_user_time = event.xselectionclear.time;
6100 #ifdef USE_X_TOOLKIT
6101 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6102 goto OTHER;
6103 #endif /* USE_X_TOOLKIT */
6105 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
6107 inev.ie.kind = SELECTION_CLEAR_EVENT;
6108 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6109 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6110 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6111 inev.ie.frame_or_window = Qnil;
6113 break;
6115 case SelectionRequest: /* Someone wants our selection. */
6116 last_user_time = event.xselectionrequest.time;
6117 #ifdef USE_X_TOOLKIT
6118 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6119 goto OTHER;
6120 #endif /* USE_X_TOOLKIT */
6122 XSelectionRequestEvent *eventp
6123 = (XSelectionRequestEvent *) &event;
6125 inev.ie.kind = SELECTION_REQUEST_EVENT;
6126 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6127 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6128 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6129 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6130 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6131 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6132 inev.ie.frame_or_window = Qnil;
6134 break;
6136 case PropertyNotify:
6137 last_user_time = event.xproperty.time;
6138 f = x_top_window_to_frame (dpyinfo, event.xproperty.window);
6139 if (f && event.xproperty.atom == dpyinfo->Xatom_net_wm_state)
6140 x_handle_net_wm_state (f, &event.xproperty);
6142 x_handle_property_notify (&event.xproperty);
6143 xft_settings_event (dpyinfo, &event);
6144 goto OTHER;
6146 case ReparentNotify:
6147 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6148 if (f)
6150 int x, y;
6151 f->output_data.x->parent_desc = event.xreparent.parent;
6152 x_real_positions (f, &x, &y);
6153 f->left_pos = x;
6154 f->top_pos = y;
6156 /* Perhaps reparented due to a WM restart. Reset this. */
6157 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6158 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6160 goto OTHER;
6162 case Expose:
6163 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6164 if (f)
6166 #ifdef USE_GTK
6167 /* This seems to be needed for GTK 2.6. */
6168 x_clear_area (event.xexpose.display,
6169 event.xexpose.window,
6170 event.xexpose.x, event.xexpose.y,
6171 event.xexpose.width, event.xexpose.height,
6172 FALSE);
6173 #endif
6174 if (f->async_visible == 0)
6176 f->async_visible = 1;
6177 f->async_iconified = 0;
6178 f->output_data.x->has_been_visible = 1;
6179 SET_FRAME_GARBAGED (f);
6181 else
6182 expose_frame (f,
6183 event.xexpose.x, event.xexpose.y,
6184 event.xexpose.width, event.xexpose.height);
6186 else
6188 #ifndef USE_TOOLKIT_SCROLL_BARS
6189 struct scroll_bar *bar;
6190 #endif
6191 #if defined USE_LUCID
6192 /* Submenus of the Lucid menu bar aren't widgets
6193 themselves, so there's no way to dispatch events
6194 to them. Recognize this case separately. */
6196 Widget widget
6197 = x_window_to_menu_bar (event.xexpose.window);
6198 if (widget)
6199 xlwmenu_redisplay (widget);
6201 #endif /* USE_LUCID */
6203 #ifdef USE_TOOLKIT_SCROLL_BARS
6204 /* Dispatch event to the widget. */
6205 goto OTHER;
6206 #else /* not USE_TOOLKIT_SCROLL_BARS */
6207 bar = x_window_to_scroll_bar (event.xexpose.display,
6208 event.xexpose.window);
6210 if (bar)
6211 x_scroll_bar_expose (bar, &event);
6212 #ifdef USE_X_TOOLKIT
6213 else
6214 goto OTHER;
6215 #endif /* USE_X_TOOLKIT */
6216 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6218 break;
6220 case GraphicsExpose: /* This occurs when an XCopyArea's
6221 source area was obscured or not
6222 available. */
6223 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6224 if (f)
6226 expose_frame (f,
6227 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6228 event.xgraphicsexpose.width,
6229 event.xgraphicsexpose.height);
6231 #ifdef USE_X_TOOLKIT
6232 else
6233 goto OTHER;
6234 #endif /* USE_X_TOOLKIT */
6235 break;
6237 case NoExpose: /* This occurs when an XCopyArea's
6238 source area was completely
6239 available. */
6240 break;
6242 case UnmapNotify:
6243 /* Redo the mouse-highlight after the tooltip has gone. */
6244 if (event.xmap.window == tip_window)
6246 tip_window = 0;
6247 redo_mouse_highlight ();
6250 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6251 if (f) /* F may no longer exist if
6252 the frame was deleted. */
6254 /* While a frame is unmapped, display generation is
6255 disabled; you don't want to spend time updating a
6256 display that won't ever be seen. */
6257 f->async_visible = 0;
6258 /* We can't distinguish, from the event, whether the window
6259 has become iconified or invisible. So assume, if it
6260 was previously visible, than now it is iconified.
6261 But x_make_frame_invisible clears both
6262 the visible flag and the iconified flag;
6263 and that way, we know the window is not iconified now. */
6264 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6266 f->async_iconified = 1;
6268 inev.ie.kind = ICONIFY_EVENT;
6269 XSETFRAME (inev.ie.frame_or_window, f);
6272 goto OTHER;
6274 case MapNotify:
6275 if (event.xmap.window == tip_window)
6276 /* The tooltip has been drawn already. Avoid
6277 the SET_FRAME_GARBAGED below. */
6278 goto OTHER;
6280 /* We use x_top_window_to_frame because map events can
6281 come for sub-windows and they don't mean that the
6282 frame is visible. */
6283 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6284 if (f)
6286 /* wait_reading_process_output will notice this and update
6287 the frame's display structures.
6288 If we where iconified, we should not set garbaged,
6289 because that stops redrawing on Expose events. This looks
6290 bad if we are called from a recursive event loop
6291 (x_dispatch_event), for example when a dialog is up. */
6292 if (! f->async_iconified)
6293 SET_FRAME_GARBAGED (f);
6295 /* Check if fullscreen was specified before we where mapped the
6296 first time, i.e. from the command line. */
6297 if (!f->output_data.x->has_been_visible)
6298 x_check_fullscreen (f);
6300 f->async_visible = 1;
6301 f->async_iconified = 0;
6302 f->output_data.x->has_been_visible = 1;
6304 if (f->iconified)
6306 inev.ie.kind = DEICONIFY_EVENT;
6307 XSETFRAME (inev.ie.frame_or_window, f);
6309 else if (! NILP (Vframe_list)
6310 && ! NILP (XCDR (Vframe_list)))
6311 /* Force a redisplay sooner or later
6312 to update the frame titles
6313 in case this is the second frame. */
6314 record_asynch_buffer_change ();
6316 #ifdef USE_GTK
6317 xg_frame_resized (f, -1, -1);
6318 #endif
6320 goto OTHER;
6322 case KeyPress:
6324 last_user_time = event.xkey.time;
6325 ignore_next_mouse_click_timeout = 0;
6327 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6328 /* Dispatch KeyPress events when in menu. */
6329 if (popup_activated ())
6330 goto OTHER;
6331 #endif
6333 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6335 /* If mouse-highlight is an integer, input clears out
6336 mouse highlighting. */
6337 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6338 && (f == 0
6339 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6341 clear_mouse_face (dpyinfo);
6342 dpyinfo->mouse_face_hidden = 1;
6345 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6346 if (f == 0)
6348 /* Scroll bars consume key events, but we want
6349 the keys to go to the scroll bar's frame. */
6350 Widget widget = XtWindowToWidget (dpyinfo->display,
6351 event.xkey.window);
6352 if (widget && XmIsScrollBar (widget))
6354 widget = XtParent (widget);
6355 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6358 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6360 if (f != 0)
6362 KeySym keysym, orig_keysym;
6363 /* al%imercury@uunet.uu.net says that making this 81
6364 instead of 80 fixed a bug whereby meta chars made
6365 his Emacs hang.
6367 It seems that some version of XmbLookupString has
6368 a bug of not returning XBufferOverflow in
6369 status_return even if the input is too long to
6370 fit in 81 bytes. So, we must prepare sufficient
6371 bytes for copy_buffer. 513 bytes (256 chars for
6372 two-byte character set) seems to be a fairly good
6373 approximation. -- 2000.8.10 handa@etl.go.jp */
6374 unsigned char copy_buffer[513];
6375 unsigned char *copy_bufptr = copy_buffer;
6376 int copy_bufsiz = sizeof (copy_buffer);
6377 int modifiers;
6378 Lisp_Object coding_system = Qlatin_1;
6379 Lisp_Object c;
6381 #ifdef USE_GTK
6382 /* Don't pass keys to GTK. A Tab will shift focus to the
6383 tool bar in GTK 2.4. Keys will still go to menus and
6384 dialogs because in that case popup_activated is TRUE
6385 (see above). */
6386 *finish = X_EVENT_DROP;
6387 #endif
6389 event.xkey.state
6390 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6391 extra_keyboard_modifiers);
6392 modifiers = event.xkey.state;
6394 /* This will have to go some day... */
6396 /* make_lispy_event turns chars into control chars.
6397 Don't do it here because XLookupString is too eager. */
6398 event.xkey.state &= ~ControlMask;
6399 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6400 | dpyinfo->super_mod_mask
6401 | dpyinfo->hyper_mod_mask
6402 | dpyinfo->alt_mod_mask);
6404 /* In case Meta is ComposeCharacter,
6405 clear its status. According to Markus Ehrnsperger
6406 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6407 this enables ComposeCharacter to work whether or
6408 not it is combined with Meta. */
6409 if (modifiers & dpyinfo->meta_mod_mask)
6410 bzero (&compose_status, sizeof (compose_status));
6412 #ifdef HAVE_X_I18N
6413 if (FRAME_XIC (f))
6415 Status status_return;
6417 coding_system = Vlocale_coding_system;
6418 nbytes = XmbLookupString (FRAME_XIC (f),
6419 &event.xkey, copy_bufptr,
6420 copy_bufsiz, &keysym,
6421 &status_return);
6422 if (status_return == XBufferOverflow)
6424 copy_bufsiz = nbytes + 1;
6425 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6426 nbytes = XmbLookupString (FRAME_XIC (f),
6427 &event.xkey, copy_bufptr,
6428 copy_bufsiz, &keysym,
6429 &status_return);
6431 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6432 if (status_return == XLookupNone)
6433 break;
6434 else if (status_return == XLookupChars)
6436 keysym = NoSymbol;
6437 modifiers = 0;
6439 else if (status_return != XLookupKeySym
6440 && status_return != XLookupBoth)
6441 abort ();
6443 else
6444 nbytes = XLookupString (&event.xkey, copy_bufptr,
6445 copy_bufsiz, &keysym,
6446 &compose_status);
6447 #else
6448 nbytes = XLookupString (&event.xkey, copy_bufptr,
6449 copy_bufsiz, &keysym,
6450 &compose_status);
6451 #endif
6453 /* If not using XIM/XIC, and a compose sequence is in progress,
6454 we break here. Otherwise, chars_matched is always 0. */
6455 if (compose_status.chars_matched > 0 && nbytes == 0)
6456 break;
6458 bzero (&compose_status, sizeof (compose_status));
6459 orig_keysym = keysym;
6461 /* Common for all keysym input events. */
6462 XSETFRAME (inev.ie.frame_or_window, f);
6463 inev.ie.modifiers
6464 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6465 inev.ie.timestamp = event.xkey.time;
6467 /* First deal with keysyms which have defined
6468 translations to characters. */
6469 if (keysym >= 32 && keysym < 128)
6470 /* Avoid explicitly decoding each ASCII character. */
6472 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6473 inev.ie.code = keysym;
6474 goto done_keysym;
6477 /* Keysyms directly mapped to Unicode characters. */
6478 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6480 if (keysym < 0x01000080)
6481 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6482 else
6483 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6484 inev.ie.code = keysym & 0xFFFFFF;
6485 goto done_keysym;
6488 /* Now non-ASCII. */
6489 if (HASH_TABLE_P (Vx_keysym_table)
6490 && (NATNUMP (c = Fgethash (make_number (keysym),
6491 Vx_keysym_table,
6492 Qnil))))
6494 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6495 ? ASCII_KEYSTROKE_EVENT
6496 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6497 inev.ie.code = XFASTINT (c);
6498 goto done_keysym;
6501 /* Random non-modifier sorts of keysyms. */
6502 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6503 || keysym == XK_Delete
6504 #ifdef XK_ISO_Left_Tab
6505 || (keysym >= XK_ISO_Left_Tab
6506 && keysym <= XK_ISO_Enter)
6507 #endif
6508 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6509 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6510 #ifdef HPUX
6511 /* This recognizes the "extended function
6512 keys". It seems there's no cleaner way.
6513 Test IsModifierKey to avoid handling
6514 mode_switch incorrectly. */
6515 || ((unsigned) (keysym) >= XK_Select
6516 && (unsigned)(keysym) < XK_KP_Space)
6517 #endif
6518 #ifdef XK_dead_circumflex
6519 || orig_keysym == XK_dead_circumflex
6520 #endif
6521 #ifdef XK_dead_grave
6522 || orig_keysym == XK_dead_grave
6523 #endif
6524 #ifdef XK_dead_tilde
6525 || orig_keysym == XK_dead_tilde
6526 #endif
6527 #ifdef XK_dead_diaeresis
6528 || orig_keysym == XK_dead_diaeresis
6529 #endif
6530 #ifdef XK_dead_macron
6531 || orig_keysym == XK_dead_macron
6532 #endif
6533 #ifdef XK_dead_degree
6534 || orig_keysym == XK_dead_degree
6535 #endif
6536 #ifdef XK_dead_acute
6537 || orig_keysym == XK_dead_acute
6538 #endif
6539 #ifdef XK_dead_cedilla
6540 || orig_keysym == XK_dead_cedilla
6541 #endif
6542 #ifdef XK_dead_breve
6543 || orig_keysym == XK_dead_breve
6544 #endif
6545 #ifdef XK_dead_ogonek
6546 || orig_keysym == XK_dead_ogonek
6547 #endif
6548 #ifdef XK_dead_caron
6549 || orig_keysym == XK_dead_caron
6550 #endif
6551 #ifdef XK_dead_doubleacute
6552 || orig_keysym == XK_dead_doubleacute
6553 #endif
6554 #ifdef XK_dead_abovedot
6555 || orig_keysym == XK_dead_abovedot
6556 #endif
6557 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6558 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6559 /* Any "vendor-specific" key is ok. */
6560 || (orig_keysym & (1 << 28))
6561 || (keysym != NoSymbol && nbytes == 0))
6562 && ! (IsModifierKey (orig_keysym)
6563 /* The symbols from XK_ISO_Lock
6564 to XK_ISO_Last_Group_Lock
6565 don't have real modifiers but
6566 should be treated similarly to
6567 Mode_switch by Emacs. */
6568 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6569 || ((unsigned)(orig_keysym)
6570 >= XK_ISO_Lock
6571 && (unsigned)(orig_keysym)
6572 <= XK_ISO_Last_Group_Lock)
6573 #endif
6576 STORE_KEYSYM_FOR_DEBUG (keysym);
6577 /* make_lispy_event will convert this to a symbolic
6578 key. */
6579 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6580 inev.ie.code = keysym;
6581 goto done_keysym;
6584 { /* Raw bytes, not keysym. */
6585 register int i;
6586 register int c;
6587 int nchars, len;
6589 for (i = 0, nchars = 0; i < nbytes; i++)
6591 if (ASCII_BYTE_P (copy_bufptr[i]))
6592 nchars++;
6593 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6596 if (nchars < nbytes)
6598 /* Decode the input data. */
6599 int require;
6600 unsigned char *p;
6602 /* The input should be decoded with `coding_system'
6603 which depends on which X*LookupString function
6604 we used just above and the locale. */
6605 setup_coding_system (coding_system, &coding);
6606 coding.src_multibyte = 0;
6607 coding.dst_multibyte = 1;
6608 /* The input is converted to events, thus we can't
6609 handle composition. Anyway, there's no XIM that
6610 gives us composition information. */
6611 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6613 require = MAX_MULTIBYTE_LENGTH * nbytes;
6614 coding.destination = alloca (require);
6615 coding.dst_bytes = require;
6616 coding.mode |= CODING_MODE_LAST_BLOCK;
6617 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6618 nbytes = coding.produced;
6619 nchars = coding.produced_char;
6620 copy_bufptr = coding.destination;
6623 /* Convert the input data to a sequence of
6624 character events. */
6625 for (i = 0; i < nbytes; i += len)
6627 if (nchars == nbytes)
6628 c = copy_bufptr[i], len = 1;
6629 else
6630 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
6631 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6632 ? ASCII_KEYSTROKE_EVENT
6633 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6634 inev.ie.code = c;
6635 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6638 count += nchars;
6640 inev.ie.kind = NO_EVENT; /* Already stored above. */
6642 if (keysym == NoSymbol)
6643 break;
6646 done_keysym:
6647 #ifdef HAVE_X_I18N
6648 /* Don't dispatch this event since XtDispatchEvent calls
6649 XFilterEvent, and two calls in a row may freeze the
6650 client. */
6651 break;
6652 #else
6653 goto OTHER;
6654 #endif
6656 case KeyRelease:
6657 last_user_time = event.xkey.time;
6658 #ifdef HAVE_X_I18N
6659 /* Don't dispatch this event since XtDispatchEvent calls
6660 XFilterEvent, and two calls in a row may freeze the
6661 client. */
6662 break;
6663 #else
6664 goto OTHER;
6665 #endif
6667 case EnterNotify:
6668 last_user_time = event.xcrossing.time;
6669 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6671 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6673 if (f && x_mouse_click_focus_ignore_position)
6674 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6676 /* EnterNotify counts as mouse movement,
6677 so update things that depend on mouse position. */
6678 if (f && !f->output_data.x->hourglass_p)
6679 note_mouse_movement (f, &event.xmotion);
6680 #ifdef USE_GTK
6681 /* We may get an EnterNotify on the buttons in the toolbar. In that
6682 case we moved out of any highlighted area and need to note this. */
6683 if (!f && last_mouse_glyph_frame)
6684 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6685 #endif
6686 goto OTHER;
6688 case FocusIn:
6689 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6690 goto OTHER;
6692 case LeaveNotify:
6693 last_user_time = event.xcrossing.time;
6694 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6696 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6697 if (f)
6699 if (f == dpyinfo->mouse_face_mouse_frame)
6701 /* If we move outside the frame, then we're
6702 certainly no longer on any text in the frame. */
6703 clear_mouse_face (dpyinfo);
6704 dpyinfo->mouse_face_mouse_frame = 0;
6707 /* Generate a nil HELP_EVENT to cancel a help-echo.
6708 Do it only if there's something to cancel.
6709 Otherwise, the startup message is cleared when
6710 the mouse leaves the frame. */
6711 if (any_help_event_p)
6712 do_help = -1;
6714 #ifdef USE_GTK
6715 /* See comment in EnterNotify above */
6716 else if (last_mouse_glyph_frame)
6717 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6718 #endif
6719 goto OTHER;
6721 case FocusOut:
6722 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6723 goto OTHER;
6725 case MotionNotify:
6727 last_user_time = event.xmotion.time;
6728 previous_help_echo_string = help_echo_string;
6729 help_echo_string = Qnil;
6731 if (dpyinfo->grabbed && last_mouse_frame
6732 && FRAME_LIVE_P (last_mouse_frame))
6733 f = last_mouse_frame;
6734 else
6735 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6737 if (dpyinfo->mouse_face_hidden)
6739 dpyinfo->mouse_face_hidden = 0;
6740 clear_mouse_face (dpyinfo);
6743 #ifdef USE_GTK
6744 if (f && xg_event_is_for_scrollbar (f, &event))
6745 f = 0;
6746 #endif
6747 if (f)
6750 /* Generate SELECT_WINDOW_EVENTs when needed.
6751 Don't let popup menus influence things (bug#1261). */
6752 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6754 Lisp_Object window;
6756 window = window_from_coordinates (f,
6757 event.xmotion.x, event.xmotion.y,
6758 0, 0, 0, 0);
6760 /* Window will be selected only when it is not selected now and
6761 last mouse movement event was not in it. Minibuffer window
6762 will be selected only when it is active. */
6763 if (WINDOWP (window)
6764 && !EQ (window, last_window)
6765 && !EQ (window, selected_window)
6766 /* For click-to-focus window managers
6767 create event iff we don't leave the
6768 selected frame. */
6769 && (focus_follows_mouse
6770 || (EQ (XWINDOW (window)->frame,
6771 XWINDOW (selected_window)->frame))))
6773 inev.ie.kind = SELECT_WINDOW_EVENT;
6774 inev.ie.frame_or_window = window;
6777 last_window=window;
6779 if (!note_mouse_movement (f, &event.xmotion))
6780 help_echo_string = previous_help_echo_string;
6782 else
6784 #ifndef USE_TOOLKIT_SCROLL_BARS
6785 struct scroll_bar *bar
6786 = x_window_to_scroll_bar (event.xmotion.display,
6787 event.xmotion.window);
6789 if (bar)
6790 x_scroll_bar_note_movement (bar, &event);
6791 #endif /* USE_TOOLKIT_SCROLL_BARS */
6793 /* If we move outside the frame, then we're
6794 certainly no longer on any text in the frame. */
6795 clear_mouse_face (dpyinfo);
6798 /* If the contents of the global variable help_echo_string
6799 has changed, generate a HELP_EVENT. */
6800 if (!NILP (help_echo_string)
6801 || !NILP (previous_help_echo_string))
6802 do_help = 1;
6803 goto OTHER;
6806 case ConfigureNotify:
6807 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6808 #ifdef USE_GTK
6809 if (!f
6810 && (f = x_any_window_to_frame (dpyinfo, event.xconfigure.window))
6811 && event.xconfigure.window == FRAME_X_WINDOW (f))
6813 xg_frame_resized (f, event.xconfigure.width,
6814 event.xconfigure.height);
6815 f = 0;
6817 #endif
6818 if (f)
6820 #ifndef USE_X_TOOLKIT
6821 #ifndef USE_GTK
6822 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6823 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6825 /* In the toolkit version, change_frame_size
6826 is called by the code that handles resizing
6827 of the EmacsFrame widget. */
6829 /* Even if the number of character rows and columns has
6830 not changed, the font size may have changed, so we need
6831 to check the pixel dimensions as well. */
6832 if (columns != FRAME_COLS (f)
6833 || rows != FRAME_LINES (f)
6834 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6835 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6837 change_frame_size (f, rows, columns, 0, 1, 0);
6838 SET_FRAME_GARBAGED (f);
6839 cancel_mouse_face (f);
6842 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6843 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6844 #endif /* not USE_GTK */
6845 #endif
6847 #ifdef USE_GTK
6848 /* GTK creates windows but doesn't map them.
6849 Only get real positions when mapped. */
6850 if (FRAME_GTK_OUTER_WIDGET (f)
6851 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6852 #endif
6854 x_real_positions (f, &f->left_pos, &f->top_pos);
6857 #ifdef HAVE_X_I18N
6858 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6859 xic_set_statusarea (f);
6860 #endif
6863 goto OTHER;
6865 case ButtonRelease:
6866 case ButtonPress:
6868 /* If we decide we want to generate an event to be seen
6869 by the rest of Emacs, we put it here. */
6870 int tool_bar_p = 0;
6872 bzero (&compose_status, sizeof (compose_status));
6873 last_mouse_glyph_frame = 0;
6874 last_user_time = event.xbutton.time;
6876 if (dpyinfo->grabbed
6877 && last_mouse_frame
6878 && FRAME_LIVE_P (last_mouse_frame))
6879 f = last_mouse_frame;
6880 else
6881 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6883 #ifdef USE_GTK
6884 if (f && xg_event_is_for_scrollbar (f, &event))
6885 f = 0;
6886 #endif
6887 if (f)
6889 /* Is this in the tool-bar? */
6890 if (WINDOWP (f->tool_bar_window)
6891 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6893 Lisp_Object window;
6894 int x = event.xbutton.x;
6895 int y = event.xbutton.y;
6897 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6898 tool_bar_p = EQ (window, f->tool_bar_window);
6900 if (tool_bar_p && event.xbutton.button < 4)
6902 handle_tool_bar_click (f, x, y,
6903 event.xbutton.type == ButtonPress,
6904 x_x_to_emacs_modifiers (dpyinfo,
6905 event.xbutton.state));
6909 if (!tool_bar_p)
6910 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6911 if (! popup_activated ())
6912 #endif
6914 if (ignore_next_mouse_click_timeout)
6916 if (event.type == ButtonPress
6917 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6919 ignore_next_mouse_click_timeout = 0;
6920 construct_mouse_click (&inev.ie, &event.xbutton, f);
6922 if (event.type == ButtonRelease)
6923 ignore_next_mouse_click_timeout = 0;
6925 else
6926 construct_mouse_click (&inev.ie, &event.xbutton, f);
6928 if (FRAME_X_EMBEDDED_P (f))
6929 xembed_send_message (f, event.xbutton.time,
6930 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6932 else
6934 struct scroll_bar *bar
6935 = x_window_to_scroll_bar (event.xbutton.display,
6936 event.xbutton.window);
6938 #ifdef USE_TOOLKIT_SCROLL_BARS
6939 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6940 scroll bars. */
6941 if (bar && event.xbutton.state & ControlMask)
6943 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6944 *finish = X_EVENT_DROP;
6946 #else /* not USE_TOOLKIT_SCROLL_BARS */
6947 if (bar)
6948 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6949 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6952 if (event.type == ButtonPress)
6954 dpyinfo->grabbed |= (1 << event.xbutton.button);
6955 last_mouse_frame = f;
6957 if (!tool_bar_p)
6958 last_tool_bar_item = -1;
6960 else
6961 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6963 /* Ignore any mouse motion that happened before this event;
6964 any subsequent mouse-movement Emacs events should reflect
6965 only motion after the ButtonPress/Release. */
6966 if (f != 0)
6967 f->mouse_moved = 0;
6969 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6970 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6971 /* For a down-event in the menu bar,
6972 don't pass it to Xt right now.
6973 Instead, save it away
6974 and we will pass it to Xt from kbd_buffer_get_event.
6975 That way, we can run some Lisp code first. */
6976 if (
6977 #ifdef USE_GTK
6978 ! popup_activated ()
6979 /* Gtk+ menus only react to the first three buttons. */
6980 && event.xbutton.button < 3
6982 #endif
6983 f && event.type == ButtonPress
6984 /* Verify the event is really within the menu bar
6985 and not just sent to it due to grabbing. */
6986 && event.xbutton.x >= 0
6987 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6988 && event.xbutton.y >= 0
6989 && event.xbutton.y < f->output_data.x->menubar_height
6990 && event.xbutton.same_screen)
6992 SET_SAVED_BUTTON_EVENT;
6993 XSETFRAME (last_mouse_press_frame, f);
6994 #ifdef USE_GTK
6995 *finish = X_EVENT_DROP;
6996 #endif
6998 else if (event.type == ButtonPress)
7000 last_mouse_press_frame = Qnil;
7001 goto OTHER;
7004 #ifdef USE_MOTIF /* This should do not harm for Lucid,
7005 but I am trying to be cautious. */
7006 else if (event.type == ButtonRelease)
7008 if (!NILP (last_mouse_press_frame))
7010 f = XFRAME (last_mouse_press_frame);
7011 if (f->output_data.x)
7012 SET_SAVED_BUTTON_EVENT;
7014 else
7015 goto OTHER;
7017 #endif /* USE_MOTIF */
7018 else
7019 goto OTHER;
7020 #endif /* USE_X_TOOLKIT || USE_GTK */
7022 break;
7024 case CirculateNotify:
7025 goto OTHER;
7027 case CirculateRequest:
7028 goto OTHER;
7030 case VisibilityNotify:
7031 goto OTHER;
7033 case MappingNotify:
7034 /* Someone has changed the keyboard mapping - update the
7035 local cache. */
7036 switch (event.xmapping.request)
7038 case MappingModifier:
7039 x_find_modifier_meanings (dpyinfo);
7040 /* This is meant to fall through. */
7041 case MappingKeyboard:
7042 XRefreshKeyboardMapping (&event.xmapping);
7044 goto OTHER;
7046 case DestroyNotify:
7047 xft_settings_event (dpyinfo, &event);
7048 break;
7050 default:
7051 OTHER:
7052 #ifdef USE_X_TOOLKIT
7053 BLOCK_INPUT;
7054 if (*finish != X_EVENT_DROP)
7055 XtDispatchEvent (&event);
7056 UNBLOCK_INPUT;
7057 #endif /* USE_X_TOOLKIT */
7058 break;
7061 done:
7062 if (inev.ie.kind != NO_EVENT)
7064 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7065 count++;
7068 if (do_help
7069 && !(hold_quit && hold_quit->kind != NO_EVENT))
7071 Lisp_Object frame;
7073 if (f)
7074 XSETFRAME (frame, f);
7075 else
7076 frame = Qnil;
7078 if (do_help > 0)
7080 any_help_event_p = 1;
7081 gen_help_event (help_echo_string, frame, help_echo_window,
7082 help_echo_object, help_echo_pos);
7084 else
7086 help_echo_string = Qnil;
7087 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7089 count++;
7092 *eventp = event;
7093 return count;
7097 /* Handles the XEvent EVENT on display DISPLAY.
7098 This is used for event loops outside the normal event handling,
7099 i.e. looping while a popup menu or a dialog is posted.
7101 Returns the value handle_one_xevent sets in the finish argument. */
7103 x_dispatch_event (event, display)
7104 XEvent *event;
7105 Display *display;
7107 struct x_display_info *dpyinfo;
7108 int finish = X_EVENT_NORMAL;
7110 dpyinfo = x_display_info_for_display (display);
7112 if (dpyinfo)
7113 handle_one_xevent (dpyinfo, event, &finish, 0);
7115 return finish;
7119 /* Read events coming from the X server.
7120 This routine is called by the SIGIO handler.
7121 We return as soon as there are no more events to be read.
7123 We return the number of characters stored into the buffer,
7124 thus pretending to be `read' (except the characters we store
7125 in the keyboard buffer can be multibyte, so are not necessarily
7126 C chars).
7128 EXPECTED is nonzero if the caller knows input is available. */
7130 static int
7131 XTread_socket (terminal, expected, hold_quit)
7132 struct terminal *terminal;
7133 int expected;
7134 struct input_event *hold_quit;
7136 int count = 0;
7137 XEvent event;
7138 int event_found = 0;
7140 if (interrupt_input_blocked)
7142 interrupt_input_pending = 1;
7143 #ifdef SYNC_INPUT
7144 pending_signals = 1;
7145 #endif
7146 return -1;
7149 interrupt_input_pending = 0;
7150 #ifdef SYNC_INPUT
7151 pending_signals = pending_atimers;
7152 #endif
7153 BLOCK_INPUT;
7155 /* So people can tell when we have read the available input. */
7156 input_signal_count++;
7158 ++handling_signal;
7160 #ifdef HAVE_X_SM
7161 /* Only check session manager input for the primary display. */
7162 if (terminal->id == 1 && x_session_have_connection ())
7164 struct input_event inev;
7165 BLOCK_INPUT;
7166 /* We don't need to EVENT_INIT (inev) here, as
7167 x_session_check_input copies an entire input_event. */
7168 if (x_session_check_input (&inev))
7170 kbd_buffer_store_event_hold (&inev, hold_quit);
7171 count++;
7173 UNBLOCK_INPUT;
7175 #endif
7177 /* For debugging, this gives a way to fake an I/O error. */
7178 if (terminal->display_info.x == XTread_socket_fake_io_error)
7180 XTread_socket_fake_io_error = 0;
7181 x_io_error_quitter (terminal->display_info.x->display);
7184 #ifndef USE_GTK
7185 while (XPending (terminal->display_info.x->display))
7187 int finish;
7189 XNextEvent (terminal->display_info.x->display, &event);
7191 #ifdef HAVE_X_I18N
7192 /* Filter events for the current X input method. */
7193 if (x_filter_event (terminal->display_info.x, &event))
7194 continue;
7195 #endif
7196 event_found = 1;
7198 count += handle_one_xevent (terminal->display_info.x,
7199 &event, &finish, hold_quit);
7201 if (finish == X_EVENT_GOTO_OUT)
7202 goto out;
7205 #else /* USE_GTK */
7207 /* For GTK we must use the GTK event loop. But XEvents gets passed
7208 to our filter function above, and then to the big event switch.
7209 We use a bunch of globals to communicate with our filter function,
7210 that is kind of ugly, but it works.
7212 There is no way to do one display at the time, GTK just does events
7213 from all displays. */
7215 while (gtk_events_pending ())
7217 current_count = count;
7218 current_hold_quit = hold_quit;
7220 gtk_main_iteration ();
7222 count = current_count;
7223 current_count = -1;
7224 current_hold_quit = 0;
7226 if (current_finish == X_EVENT_GOTO_OUT)
7227 break;
7229 #endif /* USE_GTK */
7231 out:;
7233 /* On some systems, an X bug causes Emacs to get no more events
7234 when the window is destroyed. Detect that. (1994.) */
7235 if (! event_found)
7237 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7238 One XNOOP in 100 loops will make Emacs terminate.
7239 B. Bretthauer, 1994 */
7240 x_noop_count++;
7241 if (x_noop_count >= 100)
7243 x_noop_count=0;
7245 if (next_noop_dpyinfo == 0)
7246 next_noop_dpyinfo = x_display_list;
7248 XNoOp (next_noop_dpyinfo->display);
7250 /* Each time we get here, cycle through the displays now open. */
7251 next_noop_dpyinfo = next_noop_dpyinfo->next;
7255 /* If the focus was just given to an auto-raising frame,
7256 raise it now. */
7257 /* ??? This ought to be able to handle more than one such frame. */
7258 if (pending_autoraise_frame)
7260 x_raise_frame (pending_autoraise_frame);
7261 pending_autoraise_frame = 0;
7264 --handling_signal;
7265 UNBLOCK_INPUT;
7267 return count;
7273 /***********************************************************************
7274 Text Cursor
7275 ***********************************************************************/
7277 /* Set clipping for output in glyph row ROW. W is the window in which
7278 we operate. GC is the graphics context to set clipping in.
7280 ROW may be a text row or, e.g., a mode line. Text rows must be
7281 clipped to the interior of the window dedicated to text display,
7282 mode lines must be clipped to the whole window. */
7284 static void
7285 x_clip_to_row (w, row, area, gc)
7286 struct window *w;
7287 struct glyph_row *row;
7288 int area;
7289 GC gc;
7291 struct frame *f = XFRAME (WINDOW_FRAME (w));
7292 XRectangle clip_rect;
7293 int window_x, window_y, window_width;
7295 window_box (w, area, &window_x, &window_y, &window_width, 0);
7297 clip_rect.x = window_x;
7298 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7299 clip_rect.y = max (clip_rect.y, window_y);
7300 clip_rect.width = window_width;
7301 clip_rect.height = row->visible_height;
7303 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7307 /* Draw a hollow box cursor on window W in glyph row ROW. */
7309 static void
7310 x_draw_hollow_cursor (w, row)
7311 struct window *w;
7312 struct glyph_row *row;
7314 struct frame *f = XFRAME (WINDOW_FRAME (w));
7315 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7316 Display *dpy = FRAME_X_DISPLAY (f);
7317 int x, y, wd, h;
7318 XGCValues xgcv;
7319 struct glyph *cursor_glyph;
7320 GC gc;
7322 /* Get the glyph the cursor is on. If we can't tell because
7323 the current matrix is invalid or such, give up. */
7324 cursor_glyph = get_phys_cursor_glyph (w);
7325 if (cursor_glyph == NULL)
7326 return;
7328 /* Compute frame-relative coordinates for phys cursor. */
7329 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7330 wd = w->phys_cursor_width;
7332 /* The foreground of cursor_gc is typically the same as the normal
7333 background color, which can cause the cursor box to be invisible. */
7334 xgcv.foreground = f->output_data.x->cursor_pixel;
7335 if (dpyinfo->scratch_cursor_gc)
7336 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7337 else
7338 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7339 GCForeground, &xgcv);
7340 gc = dpyinfo->scratch_cursor_gc;
7342 /* Set clipping, draw the rectangle, and reset clipping again. */
7343 x_clip_to_row (w, row, TEXT_AREA, gc);
7344 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7345 XSetClipMask (dpy, gc, None);
7349 /* Draw a bar cursor on window W in glyph row ROW.
7351 Implementation note: One would like to draw a bar cursor with an
7352 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7353 Unfortunately, I didn't find a font yet that has this property set.
7354 --gerd. */
7356 static void
7357 x_draw_bar_cursor (w, row, width, kind)
7358 struct window *w;
7359 struct glyph_row *row;
7360 int width;
7361 enum text_cursor_kinds kind;
7363 struct frame *f = XFRAME (w->frame);
7364 struct glyph *cursor_glyph;
7366 /* If cursor is out of bounds, don't draw garbage. This can happen
7367 in mini-buffer windows when switching between echo area glyphs
7368 and mini-buffer. */
7369 cursor_glyph = get_phys_cursor_glyph (w);
7370 if (cursor_glyph == NULL)
7371 return;
7373 /* If on an image, draw like a normal cursor. That's usually better
7374 visible than drawing a bar, esp. if the image is large so that
7375 the bar might not be in the window. */
7376 if (cursor_glyph->type == IMAGE_GLYPH)
7378 struct glyph_row *row;
7379 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7380 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7382 else
7384 Display *dpy = FRAME_X_DISPLAY (f);
7385 Window window = FRAME_X_WINDOW (f);
7386 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7387 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7388 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7389 XGCValues xgcv;
7391 /* If the glyph's background equals the color we normally draw
7392 the bars cursor in, the bar cursor in its normal color is
7393 invisible. Use the glyph's foreground color instead in this
7394 case, on the assumption that the glyph's colors are chosen so
7395 that the glyph is legible. */
7396 if (face->background == f->output_data.x->cursor_pixel)
7397 xgcv.background = xgcv.foreground = face->foreground;
7398 else
7399 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7400 xgcv.graphics_exposures = 0;
7402 if (gc)
7403 XChangeGC (dpy, gc, mask, &xgcv);
7404 else
7406 gc = XCreateGC (dpy, window, mask, &xgcv);
7407 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7410 x_clip_to_row (w, row, TEXT_AREA, gc);
7412 if (kind == BAR_CURSOR)
7414 if (width < 0)
7415 width = FRAME_CURSOR_WIDTH (f);
7416 width = min (cursor_glyph->pixel_width, width);
7418 w->phys_cursor_width = width;
7420 XFillRectangle (dpy, window, gc,
7421 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7422 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7423 width, row->height);
7425 else
7427 int dummy_x, dummy_y, dummy_h;
7429 if (width < 0)
7430 width = row->height;
7432 width = min (row->height, width);
7434 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
7435 &dummy_y, &dummy_h);
7437 XFillRectangle (dpy, window, gc,
7438 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7439 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7440 row->height - width),
7441 w->phys_cursor_width, width);
7444 XSetClipMask (dpy, gc, None);
7449 /* RIF: Define cursor CURSOR on frame F. */
7451 static void
7452 x_define_frame_cursor (f, cursor)
7453 struct frame *f;
7454 Cursor cursor;
7456 if (!f->pointer_invisible
7457 && f->output_data.x->current_cursor != cursor)
7458 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7459 f->output_data.x->current_cursor = cursor;
7463 /* RIF: Clear area on frame F. */
7465 static void
7466 x_clear_frame_area (f, x, y, width, height)
7467 struct frame *f;
7468 int x, y, width, height;
7470 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7471 x, y, width, height, False);
7472 #ifdef USE_GTK
7473 /* Must queue a redraw, because scroll bars might have been cleared. */
7474 if (FRAME_GTK_WIDGET (f))
7475 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
7476 #endif
7480 /* RIF: Draw cursor on window W. */
7482 static void
7483 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7484 struct window *w;
7485 struct glyph_row *glyph_row;
7486 int x, y;
7487 int cursor_type, cursor_width;
7488 int on_p, active_p;
7490 struct frame *f = XFRAME (WINDOW_FRAME (w));
7492 if (on_p)
7494 w->phys_cursor_type = cursor_type;
7495 w->phys_cursor_on_p = 1;
7497 if (glyph_row->exact_window_width_line_p
7498 && (glyph_row->reversed_p
7499 ? (w->phys_cursor.hpos < 0)
7500 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
7502 glyph_row->cursor_in_fringe_p = 1;
7503 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
7505 else
7507 switch (cursor_type)
7509 case HOLLOW_BOX_CURSOR:
7510 x_draw_hollow_cursor (w, glyph_row);
7511 break;
7513 case FILLED_BOX_CURSOR:
7514 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7515 break;
7517 case BAR_CURSOR:
7518 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7519 break;
7521 case HBAR_CURSOR:
7522 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7523 break;
7525 case NO_CURSOR:
7526 w->phys_cursor_width = 0;
7527 break;
7529 default:
7530 abort ();
7534 #ifdef HAVE_X_I18N
7535 if (w == XWINDOW (f->selected_window))
7536 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7537 xic_set_preeditarea (w, x, y);
7538 #endif
7541 #ifndef XFlush
7542 XFlush (FRAME_X_DISPLAY (f));
7543 #endif
7547 /* Icons. */
7549 /* Make the x-window of frame F use the gnu icon bitmap. */
7552 x_bitmap_icon (f, file)
7553 struct frame *f;
7554 Lisp_Object file;
7556 int bitmap_id;
7558 if (FRAME_X_WINDOW (f) == 0)
7559 return 1;
7561 /* Free up our existing icon bitmap and mask if any. */
7562 if (f->output_data.x->icon_bitmap > 0)
7563 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7564 f->output_data.x->icon_bitmap = 0;
7566 if (STRINGP (file))
7568 #ifdef USE_GTK
7569 /* Use gtk_window_set_icon_from_file () if available,
7570 It's not restricted to bitmaps */
7571 if (xg_set_icon (f, file))
7572 return 0;
7573 #endif /* USE_GTK */
7574 bitmap_id = x_create_bitmap_from_file (f, file);
7575 x_create_bitmap_mask (f, bitmap_id);
7577 else
7579 /* Create the GNU bitmap and mask if necessary. */
7580 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7582 int rc = -1;
7584 #ifdef USE_GTK
7586 if (xg_set_icon (f, xg_default_icon_file)
7587 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7588 return 0;
7590 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7592 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7593 if (rc != -1)
7594 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7596 #endif
7598 /* If all else fails, use the (black and white) xbm image. */
7599 if (rc == -1)
7601 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7602 gnu_xbm_width, gnu_xbm_height);
7603 if (rc == -1)
7604 return 1;
7606 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7607 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7611 /* The first time we create the GNU bitmap and mask,
7612 this increments the ref-count one extra time.
7613 As a result, the GNU bitmap and mask are never freed.
7614 That way, we don't have to worry about allocating it again. */
7615 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7617 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7620 x_wm_set_icon_pixmap (f, bitmap_id);
7621 f->output_data.x->icon_bitmap = bitmap_id;
7623 return 0;
7627 /* Make the x-window of frame F use a rectangle with text.
7628 Use ICON_NAME as the text. */
7631 x_text_icon (f, icon_name)
7632 struct frame *f;
7633 char *icon_name;
7635 if (FRAME_X_WINDOW (f) == 0)
7636 return 1;
7639 XTextProperty text;
7640 text.value = (unsigned char *) icon_name;
7641 text.encoding = XA_STRING;
7642 text.format = 8;
7643 text.nitems = strlen (icon_name);
7644 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7647 if (f->output_data.x->icon_bitmap > 0)
7648 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7649 f->output_data.x->icon_bitmap = 0;
7650 x_wm_set_icon_pixmap (f, 0);
7652 return 0;
7655 #define X_ERROR_MESSAGE_SIZE 200
7657 /* If non-nil, this should be a string.
7658 It means catch X errors and store the error message in this string.
7660 The reason we use a stack is that x_catch_error/x_uncatch_error can
7661 be called from a signal handler.
7664 struct x_error_message_stack {
7665 char string[X_ERROR_MESSAGE_SIZE];
7666 Display *dpy;
7667 struct x_error_message_stack *prev;
7669 static struct x_error_message_stack *x_error_message;
7671 /* An X error handler which stores the error message in
7672 *x_error_message. This is called from x_error_handler if
7673 x_catch_errors is in effect. */
7675 static void
7676 x_error_catcher (display, error)
7677 Display *display;
7678 XErrorEvent *error;
7680 XGetErrorText (display, error->error_code,
7681 x_error_message->string,
7682 X_ERROR_MESSAGE_SIZE);
7685 /* Begin trapping X errors for display DPY. Actually we trap X errors
7686 for all displays, but DPY should be the display you are actually
7687 operating on.
7689 After calling this function, X protocol errors no longer cause
7690 Emacs to exit; instead, they are recorded in the string
7691 stored in *x_error_message.
7693 Calling x_check_errors signals an Emacs error if an X error has
7694 occurred since the last call to x_catch_errors or x_check_errors.
7696 Calling x_uncatch_errors resumes the normal error handling. */
7698 void x_check_errors ();
7700 void
7701 x_catch_errors (dpy)
7702 Display *dpy;
7704 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7706 /* Make sure any errors from previous requests have been dealt with. */
7707 XSync (dpy, False);
7709 data->dpy = dpy;
7710 data->string[0] = 0;
7711 data->prev = x_error_message;
7712 x_error_message = data;
7715 /* Undo the last x_catch_errors call.
7716 DPY should be the display that was passed to x_catch_errors. */
7718 void
7719 x_uncatch_errors ()
7721 struct x_error_message_stack *tmp;
7723 BLOCK_INPUT;
7725 /* The display may have been closed before this function is called.
7726 Check if it is still open before calling XSync. */
7727 if (x_display_info_for_display (x_error_message->dpy) != 0)
7728 XSync (x_error_message->dpy, False);
7730 tmp = x_error_message;
7731 x_error_message = x_error_message->prev;
7732 xfree (tmp);
7733 UNBLOCK_INPUT;
7736 /* If any X protocol errors have arrived since the last call to
7737 x_catch_errors or x_check_errors, signal an Emacs error using
7738 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7740 void
7741 x_check_errors (dpy, format)
7742 Display *dpy;
7743 char *format;
7745 /* Make sure to catch any errors incurred so far. */
7746 XSync (dpy, False);
7748 if (x_error_message->string[0])
7750 char string[X_ERROR_MESSAGE_SIZE];
7751 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7752 x_uncatch_errors ();
7753 error (format, string);
7757 /* Nonzero if we had any X protocol errors
7758 since we did x_catch_errors on DPY. */
7761 x_had_errors_p (dpy)
7762 Display *dpy;
7764 /* Make sure to catch any errors incurred so far. */
7765 XSync (dpy, False);
7767 return x_error_message->string[0] != 0;
7770 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7772 void
7773 x_clear_errors (dpy)
7774 Display *dpy;
7776 x_error_message->string[0] = 0;
7779 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7780 * idea. --lorentey */
7781 /* Close off all unclosed x_catch_errors calls. */
7783 void
7784 x_fully_uncatch_errors ()
7786 while (x_error_message)
7787 x_uncatch_errors ();
7789 #endif
7791 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7794 x_catching_errors ()
7796 return x_error_message != 0;
7799 #if 0
7800 static unsigned int x_wire_count;
7801 x_trace_wire ()
7803 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7805 #endif /* ! 0 */
7808 /* Handle SIGPIPE, which can happen when the connection to a server
7809 simply goes away. SIGPIPE is handled by x_connection_signal.
7810 Don't need to do anything, because the write which caused the
7811 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7812 which will do the appropriate cleanup for us. */
7814 static SIGTYPE
7815 x_connection_signal (signalnum) /* If we don't have an argument, */
7816 int signalnum; /* some compilers complain in signal calls. */
7818 #ifdef USG
7819 /* USG systems forget handlers when they are used;
7820 must reestablish each time */
7821 signal (signalnum, x_connection_signal);
7822 #endif /* USG */
7826 /************************************************************************
7827 Handling X errors
7828 ************************************************************************/
7830 /* Error message passed to x_connection_closed. */
7832 static char *error_msg;
7834 /* Function installed as fatal_error_signal_hook in
7835 x_connection_closed. Print the X error message, and exit normally,
7836 instead of dumping core when XtCloseDisplay fails. */
7838 static void
7839 x_fatal_error_signal ()
7841 fprintf (stderr, "%s\n", error_msg);
7842 exit (70);
7845 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7846 the text of an error message that lead to the connection loss. */
7848 static SIGTYPE
7849 x_connection_closed (dpy, error_message)
7850 Display *dpy;
7851 char *error_message;
7853 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7854 Lisp_Object frame, tail;
7855 int index = SPECPDL_INDEX ();
7857 error_msg = (char *) alloca (strlen (error_message) + 1);
7858 strcpy (error_msg, error_message);
7859 handling_signal = 0;
7861 /* Prevent being called recursively because of an error condition
7862 below. Otherwise, we might end up with printing ``can't find per
7863 display information'' in the recursive call instead of printing
7864 the original message here. */
7865 x_catch_errors (dpy);
7867 /* Inhibit redisplay while frames are being deleted. */
7868 specbind (Qinhibit_redisplay, Qt);
7870 if (dpyinfo)
7872 /* Protect display from being closed when we delete the last
7873 frame on it. */
7874 dpyinfo->reference_count++;
7875 dpyinfo->terminal->reference_count++;
7878 /* First delete frames whose mini-buffers are on frames
7879 that are on the dead display. */
7880 FOR_EACH_FRAME (tail, frame)
7882 Lisp_Object minibuf_frame;
7883 minibuf_frame
7884 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7885 if (FRAME_X_P (XFRAME (frame))
7886 && FRAME_X_P (XFRAME (minibuf_frame))
7887 && ! EQ (frame, minibuf_frame)
7888 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7889 delete_frame (frame, Qnoelisp);
7892 /* Now delete all remaining frames on the dead display.
7893 We are now sure none of these is used as the mini-buffer
7894 for another frame that we need to delete. */
7895 FOR_EACH_FRAME (tail, frame)
7896 if (FRAME_X_P (XFRAME (frame))
7897 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7899 /* Set this to t so that delete_frame won't get confused
7900 trying to find a replacement. */
7901 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7902 delete_frame (frame, Qnoelisp);
7905 /* We have to close the display to inform Xt that it doesn't
7906 exist anymore. If we don't, Xt will continue to wait for
7907 events from the display. As a consequence, a sequence of
7909 M-x make-frame-on-display RET :1 RET
7910 ...kill the new frame, so that we get an IO error...
7911 M-x make-frame-on-display RET :1 RET
7913 will indefinitely wait in Xt for events for display `:1', opened
7914 in the first call to make-frame-on-display.
7916 Closing the display is reported to lead to a bus error on
7917 OpenWindows in certain situations. I suspect that is a bug
7918 in OpenWindows. I don't know how to circumvent it here. */
7920 if (dpyinfo)
7922 #ifdef USE_X_TOOLKIT
7923 /* If DPYINFO is null, this means we didn't open the display
7924 in the first place, so don't try to close it. */
7926 extern void (*fatal_error_signal_hook) P_ ((void));
7927 fatal_error_signal_hook = x_fatal_error_signal;
7928 XtCloseDisplay (dpy);
7929 fatal_error_signal_hook = NULL;
7931 #endif
7933 #ifdef USE_GTK
7934 /* Due to bugs in some Gtk+ versions, just exit here if this
7935 is the last display/terminal. */
7936 if (terminal_list->next_terminal == NULL)
7938 fprintf (stderr, "%s\n", error_msg);
7939 Fkill_emacs (make_number (70));
7940 /* NOTREACHED */
7942 xg_display_close (dpyinfo->display);
7943 #endif
7945 /* Indicate that this display is dead. */
7946 dpyinfo->display = 0;
7948 dpyinfo->reference_count--;
7949 dpyinfo->terminal->reference_count--;
7950 if (dpyinfo->reference_count != 0)
7951 /* We have just closed all frames on this display. */
7952 abort ();
7955 Lisp_Object tmp;
7956 XSETTERMINAL (tmp, dpyinfo->terminal);
7957 Fdelete_terminal (tmp, Qnoelisp);
7961 x_uncatch_errors ();
7963 if (terminal_list == 0)
7965 fprintf (stderr, "%s\n", error_msg);
7966 Fkill_emacs (make_number (70));
7967 /* NOTREACHED */
7970 /* Ordinary stack unwind doesn't deal with these. */
7971 #ifdef SIGIO
7972 sigunblock (sigmask (SIGIO));
7973 #endif
7974 sigunblock (sigmask (SIGALRM));
7975 TOTALLY_UNBLOCK_INPUT;
7977 unbind_to (index, Qnil);
7978 clear_waiting_for_input ();
7979 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7980 longjmp), because returning from this function would get us back into
7981 Xlib's code which will directly call `exit'. */
7982 error ("%s", error_msg);
7985 /* We specifically use it before defining it, so that gcc doesn't inline it,
7986 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7987 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7989 /* This is the first-level handler for X protocol errors.
7990 It calls x_error_quitter or x_error_catcher. */
7992 static int
7993 x_error_handler (display, error)
7994 Display *display;
7995 XErrorEvent *error;
7997 if (x_error_message)
7998 x_error_catcher (display, error);
7999 else
8000 x_error_quitter (display, error);
8001 return 0;
8004 /* This is the usual handler for X protocol errors.
8005 It kills all frames on the display that we got the error for.
8006 If that was the only one, it prints an error message and kills Emacs. */
8008 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
8010 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
8011 #define NO_INLINE __attribute__((noinline))
8012 #else
8013 #define NO_INLINE
8014 #endif
8016 /* Some versions of GNU/Linux define noinline in their headers. */
8018 #ifdef noinline
8019 #undef noinline
8020 #endif
8022 /* On older GCC versions, just putting x_error_quitter
8023 after x_error_handler prevents inlining into the former. */
8025 static void NO_INLINE
8026 x_error_quitter (display, error)
8027 Display *display;
8028 XErrorEvent *error;
8030 char buf[256], buf1[356];
8032 /* Ignore BadName errors. They can happen because of fonts
8033 or colors that are not defined. */
8035 if (error->error_code == BadName)
8036 return;
8038 /* Note that there is no real way portable across R3/R4 to get the
8039 original error handler. */
8041 XGetErrorText (display, error->error_code, buf, sizeof (buf));
8042 sprintf (buf1, "X protocol error: %s on protocol request %d",
8043 buf, error->request_code);
8044 x_connection_closed (display, buf1);
8048 /* This is the handler for X IO errors, always.
8049 It kills all frames on the display that we lost touch with.
8050 If that was the only one, it prints an error message and kills Emacs. */
8052 static int
8053 x_io_error_quitter (display)
8054 Display *display;
8056 char buf[256];
8058 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
8059 x_connection_closed (display, buf);
8060 return 0;
8063 /* Changing the font of the frame. */
8065 /* Give frame F the font FONT-OBJECT as its default font. The return
8066 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
8067 frame. If it is negative, generate a new fontset from
8068 FONT-OBJECT. */
8070 Lisp_Object
8071 x_new_font (f, font_object, fontset)
8072 struct frame *f;
8073 Lisp_Object font_object;
8074 int fontset;
8076 struct font *font = XFONT_OBJECT (font_object);
8078 if (fontset < 0)
8079 fontset = fontset_from_font (font_object);
8080 FRAME_FONTSET (f) = fontset;
8081 if (FRAME_FONT (f) == font)
8082 /* This font is already set in frame F. There's nothing more to
8083 do. */
8084 return font_object;
8086 FRAME_FONT (f) = font;
8087 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8088 FRAME_COLUMN_WIDTH (f) = font->average_width;
8089 FRAME_SPACE_WIDTH (f) = font->space_width;
8090 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8092 compute_fringe_widths (f, 1);
8094 /* Compute the scroll bar width in character columns. */
8095 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8097 int wid = FRAME_COLUMN_WIDTH (f);
8098 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8099 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8101 else
8103 int wid = FRAME_COLUMN_WIDTH (f);
8104 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8107 if (FRAME_X_WINDOW (f) != 0)
8109 /* Don't change the size of a tip frame; there's no point in
8110 doing it because it's done in Fx_show_tip, and it leads to
8111 problems because the tip frame has no widget. */
8112 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8113 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8116 #ifdef HAVE_X_I18N
8117 if (FRAME_XIC (f)
8118 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8120 BLOCK_INPUT;
8121 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8122 UNBLOCK_INPUT;
8124 #endif
8126 return font_object;
8130 /***********************************************************************
8131 X Input Methods
8132 ***********************************************************************/
8134 #ifdef HAVE_X_I18N
8136 #ifdef HAVE_X11R6
8138 /* XIM destroy callback function, which is called whenever the
8139 connection to input method XIM dies. CLIENT_DATA contains a
8140 pointer to the x_display_info structure corresponding to XIM. */
8142 static void
8143 xim_destroy_callback (xim, client_data, call_data)
8144 XIM xim;
8145 XPointer client_data;
8146 XPointer call_data;
8148 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8149 Lisp_Object frame, tail;
8151 BLOCK_INPUT;
8153 /* No need to call XDestroyIC.. */
8154 FOR_EACH_FRAME (tail, frame)
8156 struct frame *f = XFRAME (frame);
8157 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8159 FRAME_XIC (f) = NULL;
8160 xic_free_xfontset (f);
8164 /* No need to call XCloseIM. */
8165 dpyinfo->xim = NULL;
8166 XFree (dpyinfo->xim_styles);
8167 UNBLOCK_INPUT;
8170 #endif /* HAVE_X11R6 */
8172 #ifdef HAVE_X11R6
8173 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8174 extern char *XSetIMValues P_ ((XIM, ...));
8175 #endif
8177 /* Open the connection to the XIM server on display DPYINFO.
8178 RESOURCE_NAME is the resource name Emacs uses. */
8180 static void
8181 xim_open_dpy (dpyinfo, resource_name)
8182 struct x_display_info *dpyinfo;
8183 char *resource_name;
8185 XIM xim;
8187 #ifdef HAVE_XIM
8188 if (use_xim)
8190 if (dpyinfo->xim)
8191 XCloseIM (dpyinfo->xim);
8192 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8193 EMACS_CLASS);
8194 dpyinfo->xim = xim;
8196 if (xim)
8198 #ifdef HAVE_X11R6
8199 XIMCallback destroy;
8200 #endif
8202 /* Get supported styles and XIM values. */
8203 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8205 #ifdef HAVE_X11R6
8206 destroy.callback = xim_destroy_callback;
8207 destroy.client_data = (XPointer)dpyinfo;
8208 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8209 #endif
8213 else
8214 #endif /* HAVE_XIM */
8215 dpyinfo->xim = NULL;
8219 #ifdef HAVE_X11R6_XIM
8221 /* XIM instantiate callback function, which is called whenever an XIM
8222 server is available. DISPLAY is the display of the XIM.
8223 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8224 when the callback was registered. */
8226 static void
8227 xim_instantiate_callback (display, client_data, call_data)
8228 Display *display;
8229 XPointer client_data;
8230 XPointer call_data;
8232 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8233 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8235 /* We don't support multiple XIM connections. */
8236 if (dpyinfo->xim)
8237 return;
8239 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8241 /* Create XIC for the existing frames on the same display, as long
8242 as they have no XIC. */
8243 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8245 Lisp_Object tail, frame;
8247 BLOCK_INPUT;
8248 FOR_EACH_FRAME (tail, frame)
8250 struct frame *f = XFRAME (frame);
8252 if (FRAME_X_P (f)
8253 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8254 if (FRAME_XIC (f) == NULL)
8256 create_frame_xic (f);
8257 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8258 xic_set_statusarea (f);
8259 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8261 struct window *w = XWINDOW (f->selected_window);
8262 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8267 UNBLOCK_INPUT;
8271 #endif /* HAVE_X11R6_XIM */
8274 /* Open a connection to the XIM server on display DPYINFO.
8275 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8276 connection only at the first time. On X11R6, open the connection
8277 in the XIM instantiate callback function. */
8279 static void
8280 xim_initialize (dpyinfo, resource_name)
8281 struct x_display_info *dpyinfo;
8282 char *resource_name;
8284 dpyinfo->xim = NULL;
8285 #ifdef HAVE_XIM
8286 if (use_xim)
8288 #ifdef HAVE_X11R6_XIM
8289 struct xim_inst_t *xim_inst;
8290 int len;
8292 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8293 dpyinfo->xim_callback_data = xim_inst;
8294 xim_inst->dpyinfo = dpyinfo;
8295 len = strlen (resource_name);
8296 xim_inst->resource_name = (char *) xmalloc (len + 1);
8297 bcopy (resource_name, xim_inst->resource_name, len + 1);
8298 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8299 resource_name, EMACS_CLASS,
8300 xim_instantiate_callback,
8301 /* This is XPointer in XFree86
8302 but (XPointer *) on Tru64, at
8303 least, hence the configure test. */
8304 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8305 #else /* not HAVE_X11R6_XIM */
8306 xim_open_dpy (dpyinfo, resource_name);
8307 #endif /* not HAVE_X11R6_XIM */
8309 #endif /* HAVE_XIM */
8313 /* Close the connection to the XIM server on display DPYINFO. */
8315 static void
8316 xim_close_dpy (dpyinfo)
8317 struct x_display_info *dpyinfo;
8319 #ifdef HAVE_XIM
8320 if (use_xim)
8322 #ifdef HAVE_X11R6_XIM
8323 if (dpyinfo->display)
8324 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8325 NULL, EMACS_CLASS,
8326 xim_instantiate_callback, NULL);
8327 xfree (dpyinfo->xim_callback_data->resource_name);
8328 xfree (dpyinfo->xim_callback_data);
8329 #endif /* HAVE_X11R6_XIM */
8330 if (dpyinfo->display)
8331 XCloseIM (dpyinfo->xim);
8332 dpyinfo->xim = NULL;
8333 XFree (dpyinfo->xim_styles);
8335 #endif /* HAVE_XIM */
8338 #endif /* not HAVE_X11R6_XIM */
8342 /* Calculate the absolute position in frame F
8343 from its current recorded position values and gravity. */
8345 void
8346 x_calc_absolute_position (f)
8347 struct frame *f;
8349 int flags = f->size_hint_flags;
8351 /* We have nothing to do if the current position
8352 is already for the top-left corner. */
8353 if (! ((flags & XNegative) || (flags & YNegative)))
8354 return;
8356 /* Treat negative positions as relative to the leftmost bottommost
8357 position that fits on the screen. */
8358 if (flags & XNegative)
8359 f->left_pos = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
8360 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8363 int height = FRAME_PIXEL_HEIGHT (f);
8365 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8366 /* Something is fishy here. When using Motif, starting Emacs with
8367 `-g -0-0', the frame appears too low by a few pixels.
8369 This seems to be so because initially, while Emacs is starting,
8370 the column widget's height and the frame's pixel height are
8371 different. The column widget's height is the right one. In
8372 later invocations, when Emacs is up, the frame's pixel height
8373 is right, though.
8375 It's not obvious where the initial small difference comes from.
8376 2000-12-01, gerd. */
8378 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8379 #endif
8381 if (flags & YNegative)
8382 f->top_pos = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
8383 - height + f->top_pos;
8386 /* The left_pos and top_pos
8387 are now relative to the top and left screen edges,
8388 so the flags should correspond. */
8389 f->size_hint_flags &= ~ (XNegative | YNegative);
8392 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8393 to really change the position, and 0 when calling from
8394 x_make_frame_visible (in that case, XOFF and YOFF are the current
8395 position values). It is -1 when calling from x_set_frame_parameters,
8396 which means, do adjust for borders but don't change the gravity. */
8398 void
8399 x_set_offset (f, xoff, yoff, change_gravity)
8400 struct frame *f;
8401 register int xoff, yoff;
8402 int change_gravity;
8404 int modified_top, modified_left;
8406 if (change_gravity > 0)
8408 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8409 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8411 f->top_pos = yoff;
8412 f->left_pos = xoff;
8413 f->size_hint_flags &= ~ (XNegative | YNegative);
8414 if (xoff < 0)
8415 f->size_hint_flags |= XNegative;
8416 if (yoff < 0)
8417 f->size_hint_flags |= YNegative;
8418 f->win_gravity = NorthWestGravity;
8420 x_calc_absolute_position (f);
8422 BLOCK_INPUT;
8423 x_wm_set_size_hint (f, (long) 0, 0);
8425 modified_left = f->left_pos;
8426 modified_top = f->top_pos;
8428 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8430 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8431 than the WM decorations. So we use the calculated offset instead
8432 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8433 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8434 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8437 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8438 modified_left, modified_top);
8440 x_sync_with_move (f, f->left_pos, f->top_pos,
8441 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8442 ? 1 : 0);
8444 /* change_gravity is non-zero when this function is called from Lisp to
8445 programmatically move a frame. In that case, we call
8446 x_check_expected_move to discover if we have a "Type A" or "Type B"
8447 window manager, and, for a "Type A" window manager, adjust the position
8448 of the frame.
8450 We call x_check_expected_move if a programmatic move occurred, and
8451 either the window manager type (A/B) is unknown or it is Type A but we
8452 need to compute the top/left offset adjustment for this frame. */
8454 if (change_gravity != 0 &&
8455 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8456 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8457 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8458 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8459 x_check_expected_move (f, modified_left, modified_top);
8461 UNBLOCK_INPUT;
8464 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8465 on the root window for frame F contains ATOMNAME.
8466 This is how a WM check shall be done according to the Window Manager
8467 Specification/Extended Window Manager Hints at
8468 http://freedesktop.org/wiki/Specifications/wm-spec. */
8470 static int
8471 wm_supports (f, atomname)
8472 struct frame *f;
8473 const char *atomname;
8475 Atom actual_type;
8476 unsigned long actual_size, bytes_remaining;
8477 int i, rc, actual_format;
8478 Atom prop_atom;
8479 Window wmcheck_window;
8480 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8481 Window target_window = dpyinfo->root_window;
8482 long max_len = 65536;
8483 Display *dpy = FRAME_X_DISPLAY (f);
8484 unsigned char *tmp_data = NULL;
8485 Atom target_type = XA_WINDOW;
8486 Atom want_atom;
8488 BLOCK_INPUT;
8490 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8492 x_catch_errors (dpy);
8493 rc = XGetWindowProperty (dpy, target_window,
8494 prop_atom, 0, max_len, False, target_type,
8495 &actual_type, &actual_format, &actual_size,
8496 &bytes_remaining, &tmp_data);
8498 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8500 if (tmp_data) XFree (tmp_data);
8501 x_uncatch_errors ();
8502 UNBLOCK_INPUT;
8503 return 0;
8506 wmcheck_window = *(Window *) tmp_data;
8507 XFree (tmp_data);
8509 /* Check if window exists. */
8510 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8511 x_sync (f);
8512 if (x_had_errors_p (dpy))
8514 x_uncatch_errors ();
8515 UNBLOCK_INPUT;
8516 return 0;
8519 if (dpyinfo->net_supported_window != wmcheck_window)
8521 /* Window changed, reload atoms */
8522 if (dpyinfo->net_supported_atoms != NULL)
8523 XFree (dpyinfo->net_supported_atoms);
8524 dpyinfo->net_supported_atoms = NULL;
8525 dpyinfo->nr_net_supported_atoms = 0;
8526 dpyinfo->net_supported_window = 0;
8528 target_type = XA_ATOM;
8529 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8530 tmp_data = NULL;
8531 rc = XGetWindowProperty (dpy, target_window,
8532 prop_atom, 0, max_len, False, target_type,
8533 &actual_type, &actual_format, &actual_size,
8534 &bytes_remaining, &tmp_data);
8536 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8538 if (tmp_data) XFree (tmp_data);
8539 x_uncatch_errors ();
8540 UNBLOCK_INPUT;
8541 return 0;
8544 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8545 dpyinfo->nr_net_supported_atoms = actual_size;
8546 dpyinfo->net_supported_window = wmcheck_window;
8549 rc = 0;
8550 want_atom = XInternAtom (dpy, atomname, False);
8552 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8553 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8555 x_uncatch_errors ();
8556 UNBLOCK_INPUT;
8558 return rc;
8561 static void
8562 set_wm_state (frame, add, what, what2)
8563 Lisp_Object frame;
8564 int add;
8565 const char *what;
8566 const char *what2;
8568 const char *atom = "_NET_WM_STATE";
8569 Fx_send_client_event (frame, make_number (0), frame,
8570 make_unibyte_string (atom, strlen (atom)),
8571 make_number (32),
8572 /* 1 = add, 0 = remove */
8573 Fcons
8574 (make_number (add ? 1 : 0),
8575 Fcons
8576 (make_unibyte_string (what, strlen (what)),
8577 what2 != 0
8578 ? Fcons (make_unibyte_string (what2, strlen (what2)),
8579 Qnil)
8580 : Qnil)));
8583 void
8584 x_set_sticky (f, new_value, old_value)
8585 struct frame *f;
8586 Lisp_Object new_value, old_value;
8588 Lisp_Object frame;
8590 XSETFRAME (frame, f);
8591 set_wm_state (frame, NILP (new_value) ? 0 : 1,
8592 "_NET_WM_STATE_STICKY", NULL);
8595 /* Do fullscreen as specified in extended window manager hints */
8597 static int
8598 do_ewmh_fullscreen (f)
8599 struct frame *f;
8601 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8603 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8604 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8605 if (!have_net_atom)
8606 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8608 if (have_net_atom)
8610 Lisp_Object frame;
8611 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8612 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8613 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8615 XSETFRAME (frame, f);
8617 set_wm_state (frame, 0, fs, NULL);
8618 set_wm_state (frame, 0, fh, NULL);
8619 set_wm_state (frame, 0, fw, NULL);
8621 /* If there are _NET_ atoms we assume we have extended window manager
8622 hints. */
8623 switch (f->want_fullscreen)
8625 case FULLSCREEN_BOTH:
8626 set_wm_state (frame, 1, fs, NULL);
8627 break;
8628 case FULLSCREEN_WIDTH:
8629 set_wm_state (frame, 1, fw, NULL);
8630 break;
8631 case FULLSCREEN_HEIGHT:
8632 set_wm_state (frame, 1, fh, NULL);
8633 break;
8634 case FULLSCREEN_MAXIMIZED:
8635 set_wm_state (frame, 1, fw, fh);
8636 break;
8639 f->want_fullscreen = FULLSCREEN_NONE;
8643 return have_net_atom;
8646 static void
8647 XTfullscreen_hook (f)
8648 FRAME_PTR f;
8650 if (f->async_visible)
8652 BLOCK_INPUT;
8653 x_check_fullscreen (f);
8654 x_sync (f);
8655 UNBLOCK_INPUT;
8660 static void
8661 x_handle_net_wm_state (f, event)
8662 struct frame *f;
8663 XPropertyEvent *event;
8665 Atom actual_type;
8666 unsigned long actual_size, bytes_remaining;
8667 int i, rc, actual_format, value = FULLSCREEN_NONE;
8668 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8669 long max_len = 65536;
8670 Display *dpy = FRAME_X_DISPLAY (f);
8671 unsigned char *tmp_data = NULL;
8672 Atom target_type = XA_ATOM;
8673 Lisp_Object lval;
8674 int sticky = 0;
8676 BLOCK_INPUT;
8677 x_catch_errors (dpy);
8678 rc = XGetWindowProperty (dpy, event->window,
8679 event->atom, 0, max_len, False, target_type,
8680 &actual_type, &actual_format, &actual_size,
8681 &bytes_remaining, &tmp_data);
8683 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
8685 if (tmp_data) XFree (tmp_data);
8686 x_uncatch_errors ();
8687 UNBLOCK_INPUT;
8688 return;
8691 x_uncatch_errors ();
8693 for (i = 0; i < actual_size; ++i)
8695 Atom a = ((Atom*)tmp_data)[i];
8696 if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
8698 if (value == FULLSCREEN_HEIGHT)
8699 value = FULLSCREEN_MAXIMIZED;
8700 else
8701 value = FULLSCREEN_WIDTH;
8703 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
8705 if (value == FULLSCREEN_WIDTH)
8706 value = FULLSCREEN_MAXIMIZED;
8707 else
8708 value = FULLSCREEN_HEIGHT;
8710 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen_atom)
8711 value = FULLSCREEN_BOTH;
8712 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
8713 sticky = 1;
8716 lval = Qnil;
8717 switch (value)
8719 case FULLSCREEN_WIDTH:
8720 lval = Qfullwidth;
8721 break;
8722 case FULLSCREEN_HEIGHT:
8723 lval = Qfullheight;
8724 break;
8725 case FULLSCREEN_BOTH:
8726 lval = Qfullboth;
8727 break;
8728 case FULLSCREEN_MAXIMIZED:
8729 lval = Qmaximized;
8730 break;
8733 store_frame_param (f, Qfullscreen, lval);
8734 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
8736 if (tmp_data) XFree (tmp_data);
8737 UNBLOCK_INPUT;
8740 /* Check if we need to resize the frame due to a fullscreen request.
8741 If so needed, resize the frame. */
8742 static void
8743 x_check_fullscreen (f)
8744 struct frame *f;
8746 if (do_ewmh_fullscreen (f))
8747 return;
8749 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8750 return; /* Only fullscreen without WM or with EWM hints (above). */
8752 if (f->want_fullscreen != FULLSCREEN_NONE)
8754 int width = FRAME_COLS (f), height = FRAME_LINES (f);
8755 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8757 switch (f->want_fullscreen)
8759 /* No difference between these two when there is no WM */
8760 case FULLSCREEN_BOTH:
8761 case FULLSCREEN_MAXIMIZED:
8762 width = x_display_pixel_width (dpyinfo);
8763 height = x_display_pixel_height (dpyinfo);
8764 break;
8765 case FULLSCREEN_WIDTH:
8766 width = x_display_pixel_width (dpyinfo);
8767 break;
8768 case FULLSCREEN_HEIGHT:
8769 height = x_display_pixel_height (dpyinfo);
8772 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8774 change_frame_size (f, height, width, 0, 1, 0);
8775 SET_FRAME_GARBAGED (f);
8776 cancel_mouse_face (f);
8781 /* This function is called by x_set_offset to determine whether the window
8782 manager interfered with the positioning of the frame. Type A window
8783 managers position the surrounding window manager decorations a small
8784 amount above and left of the user-supplied position. Type B window
8785 managers position the surrounding window manager decorations at the
8786 user-specified position. If we detect a Type A window manager, we
8787 compensate by moving the window right and down by the proper amount. */
8789 static void
8790 x_check_expected_move (f, expected_left, expected_top)
8791 struct frame *f;
8792 int expected_left;
8793 int expected_top;
8795 int current_left = 0, current_top = 0;
8797 /* x_real_positions returns the left and top offsets of the outermost
8798 window manager window around the frame. */
8800 x_real_positions (f, &current_left, &current_top);
8802 if (current_left != expected_left || current_top != expected_top)
8804 /* It's a "Type A" window manager. */
8806 int adjusted_left;
8807 int adjusted_top;
8809 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8810 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8811 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8813 /* Now fix the mispositioned frame's location. */
8815 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8816 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8818 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8819 adjusted_left, adjusted_top);
8821 x_sync_with_move (f, expected_left, expected_top, 0);
8823 else
8824 /* It's a "Type B" window manager. We don't have to adjust the
8825 frame's position. */
8827 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8831 /* Wait for XGetGeometry to return up-to-date position information for a
8832 recently-moved frame. Call this immediately after calling XMoveWindow.
8833 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8834 frame has been moved to, so we use a fuzzy position comparison instead
8835 of an exact comparison. */
8837 static void
8838 x_sync_with_move (f, left, top, fuzzy)
8839 struct frame *f;
8840 int left, top, fuzzy;
8842 int count = 0;
8844 while (count++ < 50)
8846 int current_left = 0, current_top = 0;
8848 /* In theory, this call to XSync only needs to happen once, but in
8849 practice, it doesn't seem to work, hence the need for the surrounding
8850 loop. */
8852 XSync (FRAME_X_DISPLAY (f), False);
8853 x_real_positions (f, &current_left, &current_top);
8855 if (fuzzy)
8857 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8858 pixels. */
8860 if (eabs (current_left - left) <= 10
8861 && eabs (current_top - top) <= 40)
8862 return;
8864 else if (current_left == left && current_top == top)
8865 return;
8868 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8869 will then return up-to-date position info. */
8871 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8875 /* Wait for an event on frame F matching EVENTTYPE. */
8876 void
8877 x_wait_for_event (f, eventtype)
8878 struct frame *f;
8879 int eventtype;
8881 int level = interrupt_input_blocked;
8883 SELECT_TYPE fds;
8884 EMACS_TIME tmo, tmo_at, time_now;
8885 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
8887 pending_event_wait.f = f;
8888 pending_event_wait.eventtype = eventtype;
8890 /* Set timeout to 0.1 second. Hopefully not noticable.
8891 Maybe it should be configurable. */
8892 EMACS_SET_SECS_USECS (tmo, 0, 100000);
8893 EMACS_GET_TIME (tmo_at);
8894 EMACS_ADD_TIME (tmo_at, tmo_at, tmo);
8896 while (pending_event_wait.eventtype)
8898 interrupt_input_pending = 1;
8899 TOTALLY_UNBLOCK_INPUT;
8900 /* XTread_socket is called after unblock. */
8901 BLOCK_INPUT;
8902 interrupt_input_blocked = level;
8904 FD_ZERO (&fds);
8905 FD_SET (fd, &fds);
8907 EMACS_GET_TIME (time_now);
8908 EMACS_SUB_TIME (tmo, tmo_at, time_now);
8910 if (EMACS_TIME_NEG_P (tmo) || select (fd+1, &fds, NULL, NULL, &tmo) == 0)
8911 break; /* Timeout */
8913 pending_event_wait.f = 0;
8914 pending_event_wait.eventtype = 0;
8918 /* Change the size of frame F's X window to COLS/ROWS in the case F
8919 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8920 top-left-corner window gravity for this size change and subsequent
8921 size changes. Otherwise we leave the window gravity unchanged. */
8923 static void
8924 x_set_window_size_1 (f, change_gravity, cols, rows)
8925 struct frame *f;
8926 int change_gravity;
8927 int cols, rows;
8929 int pixelwidth, pixelheight;
8931 check_frame_size (f, &rows, &cols);
8932 f->scroll_bar_actual_width
8933 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8935 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8936 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8937 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8939 compute_fringe_widths (f, 0);
8941 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8942 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
8943 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
8945 if (change_gravity) f->win_gravity = NorthWestGravity;
8946 x_wm_set_size_hint (f, (long) 0, 0);
8947 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8948 pixelwidth, pixelheight);
8951 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8952 receive in the ConfigureNotify event; if we get what we asked
8953 for, then the event won't cause the screen to become garbaged, so
8954 we have to make sure to do it here. */
8955 SET_FRAME_GARBAGED (f);
8957 /* Now, strictly speaking, we can't be sure that this is accurate,
8958 but the window manager will get around to dealing with the size
8959 change request eventually, and we'll hear how it went when the
8960 ConfigureNotify event gets here.
8962 We could just not bother storing any of this information here,
8963 and let the ConfigureNotify event set everything up, but that
8964 might be kind of confusing to the Lisp code, since size changes
8965 wouldn't be reported in the frame parameters until some random
8966 point in the future when the ConfigureNotify event arrives.
8968 We pass 1 for DELAY since we can't run Lisp code inside of
8969 a BLOCK_INPUT. */
8971 /* But the ConfigureNotify may in fact never arrive, and then this is
8972 not right if the frame is visible. Instead wait (with timeout)
8973 for the ConfigureNotify. */
8974 if (f->async_visible)
8975 x_wait_for_event (f, ConfigureNotify);
8976 else
8978 change_frame_size (f, rows, cols, 0, 1, 0);
8979 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8980 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8981 x_sync (f);
8986 /* Call this to change the size of frame F's x-window.
8987 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8988 for this size change and subsequent size changes.
8989 Otherwise we leave the window gravity unchanged. */
8991 void
8992 x_set_window_size (f, change_gravity, cols, rows)
8993 struct frame *f;
8994 int change_gravity;
8995 int cols, rows;
8997 BLOCK_INPUT;
8999 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
9001 int r, c;
9003 /* When the frame is maximized/fullscreen or running under for
9004 example Xmonad, x_set_window_size_1 will be a no-op.
9005 In that case, the right thing to do is extend rows/cols to
9006 the current frame size. We do that first if x_set_window_size_1
9007 turns out to not be a no-op (there is no way to know).
9008 The size will be adjusted again if the frame gets a
9009 ConfigureNotify event as a result of x_set_window_size. */
9010 int pixelh = FRAME_PIXEL_HEIGHT (f);
9011 #ifdef USE_X_TOOLKIT
9012 /* The menu bar is not part of text lines. The tool bar
9013 is however. */
9014 pixelh -= FRAME_MENUBAR_HEIGHT (f);
9015 #endif
9016 r = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelh);
9017 /* Update f->scroll_bar_actual_width because it is used in
9018 FRAME_PIXEL_WIDTH_TO_TEXT_COLS. */
9019 f->scroll_bar_actual_width
9020 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
9021 c = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, FRAME_PIXEL_WIDTH (f));
9022 change_frame_size (f, r, c, 0, 1, 0);
9025 #ifdef USE_GTK
9026 if (FRAME_GTK_WIDGET (f))
9027 xg_frame_set_char_size (f, cols, rows);
9028 else
9029 x_set_window_size_1 (f, change_gravity, cols, rows);
9030 #else /* not USE_GTK */
9032 x_set_window_size_1 (f, change_gravity, cols, rows);
9034 #endif /* not USE_GTK */
9036 /* If cursor was outside the new size, mark it as off. */
9037 mark_window_cursors_off (XWINDOW (f->root_window));
9039 /* Clear out any recollection of where the mouse highlighting was,
9040 since it might be in a place that's outside the new frame size.
9041 Actually checking whether it is outside is a pain in the neck,
9042 so don't try--just let the highlighting be done afresh with new size. */
9043 cancel_mouse_face (f);
9045 UNBLOCK_INPUT;
9048 /* Mouse warping. */
9050 void
9051 x_set_mouse_position (f, x, y)
9052 struct frame *f;
9053 int x, y;
9055 int pix_x, pix_y;
9057 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
9058 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
9060 if (pix_x < 0) pix_x = 0;
9061 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
9063 if (pix_y < 0) pix_y = 0;
9064 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
9066 BLOCK_INPUT;
9068 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9069 0, 0, 0, 0, pix_x, pix_y);
9070 UNBLOCK_INPUT;
9073 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9075 void
9076 x_set_mouse_pixel_position (f, pix_x, pix_y)
9077 struct frame *f;
9078 int pix_x, pix_y;
9080 BLOCK_INPUT;
9082 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9083 0, 0, 0, 0, pix_x, pix_y);
9084 UNBLOCK_INPUT;
9087 /* focus shifting, raising and lowering. */
9089 void
9090 x_focus_on_frame (f)
9091 struct frame *f;
9093 #if 0
9094 /* I don't think that the ICCCM allows programs to do things like this
9095 without the interaction of the window manager. Whatever you end up
9096 doing with this code, do it to x_unfocus_frame too. */
9097 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9098 RevertToPointerRoot, CurrentTime);
9099 #endif /* ! 0 */
9102 void
9103 x_unfocus_frame (f)
9104 struct frame *f;
9106 #if 0
9107 /* Look at the remarks in x_focus_on_frame. */
9108 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
9109 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
9110 RevertToPointerRoot, CurrentTime);
9111 #endif /* ! 0 */
9114 /* Raise frame F. */
9116 void
9117 x_raise_frame (f)
9118 struct frame *f;
9120 BLOCK_INPUT;
9121 if (f->async_visible)
9122 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9124 XFlush (FRAME_X_DISPLAY (f));
9125 UNBLOCK_INPUT;
9128 /* Lower frame F. */
9130 void
9131 x_lower_frame (f)
9132 struct frame *f;
9134 if (f->async_visible)
9136 BLOCK_INPUT;
9137 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9138 XFlush (FRAME_X_DISPLAY (f));
9139 UNBLOCK_INPUT;
9143 /* Activate frame with Extended Window Manager Hints */
9145 void
9146 x_ewmh_activate_frame (f)
9147 FRAME_PTR f;
9149 /* See Window Manager Specification/Extended Window Manager Hints at
9150 http://freedesktop.org/wiki/Specifications/wm-spec */
9152 const char *atom = "_NET_ACTIVE_WINDOW";
9153 if (f->async_visible && wm_supports (f, atom))
9155 Lisp_Object frame;
9156 XSETFRAME (frame, f);
9157 Fx_send_client_event (frame, make_number (0), frame,
9158 make_unibyte_string (atom, strlen (atom)),
9159 make_number (32),
9160 Fcons (make_number (1),
9161 Fcons (make_number (last_user_time),
9162 Qnil)));
9166 static void
9167 XTframe_raise_lower (f, raise_flag)
9168 FRAME_PTR f;
9169 int raise_flag;
9171 if (raise_flag)
9172 x_raise_frame (f);
9173 else
9174 x_lower_frame (f);
9177 /* XEmbed implementation. */
9179 void
9180 xembed_set_info (f, flags)
9181 struct frame *f;
9182 enum xembed_info flags;
9184 Atom atom;
9185 unsigned long data[2];
9187 atom = XInternAtom (FRAME_X_DISPLAY (f), "_XEMBED_INFO", False);
9189 data[0] = XEMBED_VERSION;
9190 data[1] = flags;
9192 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), atom, atom,
9193 32, PropModeReplace, (unsigned char *) data, 2);
9196 void
9197 xembed_send_message (f, time, message, detail, data1, data2)
9198 struct frame *f;
9199 Time time;
9200 enum xembed_message message;
9201 long detail;
9202 long data1;
9203 long data2;
9205 XEvent event;
9207 event.xclient.type = ClientMessage;
9208 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9209 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
9210 event.xclient.format = 32;
9211 event.xclient.data.l[0] = time;
9212 event.xclient.data.l[1] = message;
9213 event.xclient.data.l[2] = detail;
9214 event.xclient.data.l[3] = data1;
9215 event.xclient.data.l[4] = data2;
9217 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9218 False, NoEventMask, &event);
9219 XSync (FRAME_X_DISPLAY (f), False);
9222 /* Change of visibility. */
9224 /* This tries to wait until the frame is really visible.
9225 However, if the window manager asks the user where to position
9226 the frame, this will return before the user finishes doing that.
9227 The frame will not actually be visible at that time,
9228 but it will become visible later when the window manager
9229 finishes with it. */
9231 void
9232 x_make_frame_visible (f)
9233 struct frame *f;
9235 Lisp_Object type;
9236 int original_top, original_left;
9237 int retry_count = 2;
9239 retry:
9241 BLOCK_INPUT;
9243 type = x_icon_type (f);
9244 if (!NILP (type))
9245 x_bitmap_icon (f, type);
9247 if (! FRAME_VISIBLE_P (f))
9249 /* We test FRAME_GARBAGED_P here to make sure we don't
9250 call x_set_offset a second time
9251 if we get to x_make_frame_visible a second time
9252 before the window gets really visible. */
9253 if (! FRAME_ICONIFIED_P (f)
9254 && ! FRAME_X_EMBEDDED_P (f)
9255 && ! f->output_data.x->asked_for_visible)
9256 x_set_offset (f, f->left_pos, f->top_pos, 0);
9258 f->output_data.x->asked_for_visible = 1;
9260 if (! EQ (Vx_no_window_manager, Qt))
9261 x_wm_set_window_state (f, NormalState);
9262 #ifdef USE_X_TOOLKIT
9263 if (FRAME_X_EMBEDDED_P (f))
9264 xembed_set_info (f, XEMBED_MAPPED);
9265 else
9267 /* This was XtPopup, but that did nothing for an iconified frame. */
9268 XtMapWidget (f->output_data.x->widget);
9270 #else /* not USE_X_TOOLKIT */
9271 #ifdef USE_GTK
9272 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9273 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9274 #else
9275 if (FRAME_X_EMBEDDED_P (f))
9276 xembed_set_info (f, XEMBED_MAPPED);
9277 else
9278 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9279 #endif /* not USE_GTK */
9280 #endif /* not USE_X_TOOLKIT */
9283 XFlush (FRAME_X_DISPLAY (f));
9285 /* Synchronize to ensure Emacs knows the frame is visible
9286 before we do anything else. We do this loop with input not blocked
9287 so that incoming events are handled. */
9289 Lisp_Object frame;
9290 int count;
9291 /* This must be before UNBLOCK_INPUT
9292 since events that arrive in response to the actions above
9293 will set it when they are handled. */
9294 int previously_visible = f->output_data.x->has_been_visible;
9296 original_left = f->left_pos;
9297 original_top = f->top_pos;
9299 /* This must come after we set COUNT. */
9300 UNBLOCK_INPUT;
9302 /* We unblock here so that arriving X events are processed. */
9304 /* Now move the window back to where it was "supposed to be".
9305 But don't do it if the gravity is negative.
9306 When the gravity is negative, this uses a position
9307 that is 3 pixels too low. Perhaps that's really the border width.
9309 Don't do this if the window has never been visible before,
9310 because the window manager may choose the position
9311 and we don't want to override it. */
9313 if (! FRAME_VISIBLE_P (f)
9314 && ! FRAME_ICONIFIED_P (f)
9315 && ! FRAME_X_EMBEDDED_P (f)
9316 && f->win_gravity == NorthWestGravity
9317 && previously_visible)
9319 Drawable rootw;
9320 int x, y;
9321 unsigned int width, height, border, depth;
9323 BLOCK_INPUT;
9325 /* On some window managers (such as FVWM) moving an existing
9326 window, even to the same place, causes the window manager
9327 to introduce an offset. This can cause the window to move
9328 to an unexpected location. Check the geometry (a little
9329 slow here) and then verify that the window is in the right
9330 place. If the window is not in the right place, move it
9331 there, and take the potential window manager hit. */
9332 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9333 &rootw, &x, &y, &width, &height, &border, &depth);
9335 if (original_left != x || original_top != y)
9336 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9337 original_left, original_top);
9339 UNBLOCK_INPUT;
9342 XSETFRAME (frame, f);
9344 /* Wait until the frame is visible. Process X events until a
9345 MapNotify event has been seen, or until we think we won't get a
9346 MapNotify at all.. */
9347 for (count = input_signal_count + 10;
9348 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9350 /* Force processing of queued events. */
9351 x_sync (f);
9353 /* Machines that do polling rather than SIGIO have been
9354 observed to go into a busy-wait here. So we'll fake an
9355 alarm signal to let the handler know that there's something
9356 to be read. We used to raise a real alarm, but it seems
9357 that the handler isn't always enabled here. This is
9358 probably a bug. */
9359 if (input_polling_used ())
9361 /* It could be confusing if a real alarm arrives while
9362 processing the fake one. Turn it off and let the
9363 handler reset it. */
9364 extern void poll_for_input_1 P_ ((void));
9365 int old_poll_suppress_count = poll_suppress_count;
9366 poll_suppress_count = 1;
9367 poll_for_input_1 ();
9368 poll_suppress_count = old_poll_suppress_count;
9371 /* See if a MapNotify event has been processed. */
9372 FRAME_SAMPLE_VISIBILITY (f);
9375 /* 2000-09-28: In
9377 (let ((f (selected-frame)))
9378 (iconify-frame f)
9379 (raise-frame f))
9381 the frame is not raised with various window managers on
9382 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9383 unknown reason, the call to XtMapWidget is completely ignored.
9384 Mapping the widget a second time works. */
9386 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9387 goto retry;
9391 /* Change from mapped state to withdrawn state. */
9393 /* Make the frame visible (mapped and not iconified). */
9395 void
9396 x_make_frame_invisible (f)
9397 struct frame *f;
9399 Window window;
9401 /* Use the frame's outermost window, not the one we normally draw on. */
9402 window = FRAME_OUTER_WINDOW (f);
9404 /* Don't keep the highlight on an invisible frame. */
9405 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9406 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9408 BLOCK_INPUT;
9410 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9411 that the current position of the window is user-specified, rather than
9412 program-specified, so that when the window is mapped again, it will be
9413 placed at the same location, without forcing the user to position it
9414 by hand again (they have already done that once for this window.) */
9415 x_wm_set_size_hint (f, (long) 0, 1);
9417 #ifdef USE_GTK
9418 if (FRAME_GTK_OUTER_WIDGET (f))
9419 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9420 else
9421 #else
9422 if (FRAME_X_EMBEDDED_P (f))
9423 xembed_set_info (f, 0);
9424 else
9425 #endif
9428 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9429 DefaultScreen (FRAME_X_DISPLAY (f))))
9431 UNBLOCK_INPUT_RESIGNAL;
9432 error ("Can't notify window manager of window withdrawal");
9436 /* We can't distinguish this from iconification
9437 just by the event that we get from the server.
9438 So we can't win using the usual strategy of letting
9439 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9440 and synchronize with the server to make sure we agree. */
9441 f->visible = 0;
9442 FRAME_ICONIFIED_P (f) = 0;
9443 f->async_visible = 0;
9444 f->async_iconified = 0;
9446 x_sync (f);
9448 UNBLOCK_INPUT;
9451 /* Change window state from mapped to iconified. */
9453 void
9454 x_iconify_frame (f)
9455 struct frame *f;
9457 int result;
9458 Lisp_Object type;
9460 /* Don't keep the highlight on an invisible frame. */
9461 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9462 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9464 if (f->async_iconified)
9465 return;
9467 BLOCK_INPUT;
9469 FRAME_SAMPLE_VISIBILITY (f);
9471 type = x_icon_type (f);
9472 if (!NILP (type))
9473 x_bitmap_icon (f, type);
9475 #ifdef USE_GTK
9476 if (FRAME_GTK_OUTER_WIDGET (f))
9478 if (! FRAME_VISIBLE_P (f))
9479 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9481 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9482 f->iconified = 1;
9483 f->visible = 1;
9484 f->async_iconified = 1;
9485 f->async_visible = 0;
9486 UNBLOCK_INPUT;
9487 return;
9489 #endif
9491 #ifdef USE_X_TOOLKIT
9493 if (! FRAME_VISIBLE_P (f))
9495 if (! EQ (Vx_no_window_manager, Qt))
9496 x_wm_set_window_state (f, IconicState);
9497 /* This was XtPopup, but that did nothing for an iconified frame. */
9498 XtMapWidget (f->output_data.x->widget);
9499 /* The server won't give us any event to indicate
9500 that an invisible frame was changed to an icon,
9501 so we have to record it here. */
9502 f->iconified = 1;
9503 f->visible = 1;
9504 f->async_iconified = 1;
9505 f->async_visible = 0;
9506 UNBLOCK_INPUT;
9507 return;
9510 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9511 XtWindow (f->output_data.x->widget),
9512 DefaultScreen (FRAME_X_DISPLAY (f)));
9513 UNBLOCK_INPUT;
9515 if (!result)
9516 error ("Can't notify window manager of iconification");
9518 f->async_iconified = 1;
9519 f->async_visible = 0;
9522 BLOCK_INPUT;
9523 XFlush (FRAME_X_DISPLAY (f));
9524 UNBLOCK_INPUT;
9525 #else /* not USE_X_TOOLKIT */
9527 /* Make sure the X server knows where the window should be positioned,
9528 in case the user deiconifies with the window manager. */
9529 if (! FRAME_VISIBLE_P (f)
9530 && ! FRAME_ICONIFIED_P (f)
9531 && ! FRAME_X_EMBEDDED_P (f))
9532 x_set_offset (f, f->left_pos, f->top_pos, 0);
9534 /* Since we don't know which revision of X we're running, we'll use both
9535 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9537 /* X11R4: send a ClientMessage to the window manager using the
9538 WM_CHANGE_STATE type. */
9540 XEvent message;
9542 message.xclient.window = FRAME_X_WINDOW (f);
9543 message.xclient.type = ClientMessage;
9544 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9545 message.xclient.format = 32;
9546 message.xclient.data.l[0] = IconicState;
9548 if (! XSendEvent (FRAME_X_DISPLAY (f),
9549 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9550 False,
9551 SubstructureRedirectMask | SubstructureNotifyMask,
9552 &message))
9554 UNBLOCK_INPUT_RESIGNAL;
9555 error ("Can't notify window manager of iconification");
9559 /* X11R3: set the initial_state field of the window manager hints to
9560 IconicState. */
9561 x_wm_set_window_state (f, IconicState);
9563 if (!FRAME_VISIBLE_P (f))
9565 /* If the frame was withdrawn, before, we must map it. */
9566 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9569 f->async_iconified = 1;
9570 f->async_visible = 0;
9572 XFlush (FRAME_X_DISPLAY (f));
9573 UNBLOCK_INPUT;
9574 #endif /* not USE_X_TOOLKIT */
9578 /* Free X resources of frame F. */
9580 void
9581 x_free_frame_resources (f)
9582 struct frame *f;
9584 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9585 Lisp_Object bar;
9586 struct scroll_bar *b;
9588 BLOCK_INPUT;
9590 /* If a display connection is dead, don't try sending more
9591 commands to the X server. */
9592 if (dpyinfo->display)
9594 /* We must free faces before destroying windows because some
9595 font-driver (e.g. xft) access a window while finishing a
9596 face. */
9597 if (FRAME_FACE_CACHE (f))
9598 free_frame_faces (f);
9600 if (f->output_data.x->icon_desc)
9601 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9603 #ifdef USE_X_TOOLKIT
9604 /* Explicitly destroy the scroll bars of the frame. Without
9605 this, we get "BadDrawable" errors from the toolkit later on,
9606 presumably from expose events generated for the disappearing
9607 toolkit scroll bars. */
9608 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9610 b = XSCROLL_BAR (bar);
9611 x_scroll_bar_remove (b);
9613 #endif
9615 #ifdef HAVE_X_I18N
9616 if (FRAME_XIC (f))
9617 free_frame_xic (f);
9618 #endif
9620 #ifdef USE_X_TOOLKIT
9621 if (f->output_data.x->widget)
9623 XtDestroyWidget (f->output_data.x->widget);
9624 f->output_data.x->widget = NULL;
9626 /* Tooltips don't have widgets, only a simple X window, even if
9627 we are using a toolkit. */
9628 else if (FRAME_X_WINDOW (f))
9629 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9631 free_frame_menubar (f);
9632 #else /* !USE_X_TOOLKIT */
9634 #ifdef USE_GTK
9635 /* In the GTK version, tooltips are normal X
9636 frames. We must check and free both types. */
9637 if (FRAME_GTK_OUTER_WIDGET (f))
9639 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9640 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9641 FRAME_GTK_OUTER_WIDGET (f) = 0;
9643 #endif /* USE_GTK */
9645 if (FRAME_X_WINDOW (f))
9646 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9647 #endif /* !USE_X_TOOLKIT */
9649 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9650 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9651 unload_color (f, f->output_data.x->cursor_pixel);
9652 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9653 unload_color (f, f->output_data.x->border_pixel);
9654 unload_color (f, f->output_data.x->mouse_pixel);
9656 if (f->output_data.x->scroll_bar_background_pixel != -1)
9657 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9658 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9659 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9660 #ifdef USE_TOOLKIT_SCROLL_BARS
9661 /* Scrollbar shadow colors. */
9662 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9663 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9664 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9665 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9666 #endif /* USE_TOOLKIT_SCROLL_BARS */
9667 if (f->output_data.x->white_relief.allocated_p)
9668 unload_color (f, f->output_data.x->white_relief.pixel);
9669 if (f->output_data.x->black_relief.allocated_p)
9670 unload_color (f, f->output_data.x->black_relief.pixel);
9672 x_free_gcs (f);
9673 XFlush (FRAME_X_DISPLAY (f));
9676 xfree (f->output_data.x->saved_menu_event);
9677 xfree (f->output_data.x);
9678 f->output_data.x = NULL;
9680 if (f == dpyinfo->x_focus_frame)
9681 dpyinfo->x_focus_frame = 0;
9682 if (f == dpyinfo->x_focus_event_frame)
9683 dpyinfo->x_focus_event_frame = 0;
9684 if (f == dpyinfo->x_highlight_frame)
9685 dpyinfo->x_highlight_frame = 0;
9687 if (f == dpyinfo->mouse_face_mouse_frame)
9689 dpyinfo->mouse_face_beg_row
9690 = dpyinfo->mouse_face_beg_col = -1;
9691 dpyinfo->mouse_face_end_row
9692 = dpyinfo->mouse_face_end_col = -1;
9693 dpyinfo->mouse_face_window = Qnil;
9694 dpyinfo->mouse_face_deferred_gc = 0;
9695 dpyinfo->mouse_face_mouse_frame = 0;
9698 UNBLOCK_INPUT;
9702 /* Destroy the X window of frame F. */
9704 void
9705 x_destroy_window (f)
9706 struct frame *f;
9708 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9710 /* If a display connection is dead, don't try sending more
9711 commands to the X server. */
9712 if (dpyinfo->display != 0)
9713 x_free_frame_resources (f);
9715 dpyinfo->reference_count--;
9719 /* Setting window manager hints. */
9721 /* Set the normal size hints for the window manager, for frame F.
9722 FLAGS is the flags word to use--or 0 meaning preserve the flags
9723 that the window now has.
9724 If USER_POSITION is nonzero, we set the USPosition
9725 flag (this is useful when FLAGS is 0).
9726 The GTK version is in gtkutils.c */
9728 #ifndef USE_GTK
9729 void
9730 x_wm_set_size_hint (f, flags, user_position)
9731 struct frame *f;
9732 long flags;
9733 int user_position;
9735 XSizeHints size_hints;
9736 Window window = FRAME_OUTER_WINDOW (f);
9738 /* Setting PMaxSize caused various problems. */
9739 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9741 size_hints.x = f->left_pos;
9742 size_hints.y = f->top_pos;
9744 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9745 size_hints.width = FRAME_PIXEL_WIDTH (f);
9747 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9748 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9749 size_hints.max_width = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
9750 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9751 size_hints.max_height = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
9752 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9754 /* Calculate the base and minimum sizes. */
9756 int base_width, base_height;
9757 int min_rows = 0, min_cols = 0;
9759 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9760 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9762 check_frame_size (f, &min_rows, &min_cols);
9764 /* The window manager uses the base width hints to calculate the
9765 current number of rows and columns in the frame while
9766 resizing; min_width and min_height aren't useful for this
9767 purpose, since they might not give the dimensions for a
9768 zero-row, zero-column frame.
9770 We use the base_width and base_height members if we have
9771 them; otherwise, we set the min_width and min_height members
9772 to the size for a zero x zero frame. */
9774 size_hints.flags |= PBaseSize;
9775 size_hints.base_width = base_width;
9776 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
9777 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9778 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9781 /* If we don't need the old flags, we don't need the old hint at all. */
9782 if (flags)
9784 size_hints.flags |= flags;
9785 goto no_read;
9789 XSizeHints hints; /* Sometimes I hate X Windows... */
9790 long supplied_return;
9791 int value;
9793 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9794 &supplied_return);
9796 if (flags)
9797 size_hints.flags |= flags;
9798 else
9800 if (value == 0)
9801 hints.flags = 0;
9802 if (hints.flags & PSize)
9803 size_hints.flags |= PSize;
9804 if (hints.flags & PPosition)
9805 size_hints.flags |= PPosition;
9806 if (hints.flags & USPosition)
9807 size_hints.flags |= USPosition;
9808 if (hints.flags & USSize)
9809 size_hints.flags |= USSize;
9813 no_read:
9815 #ifdef PWinGravity
9816 size_hints.win_gravity = f->win_gravity;
9817 size_hints.flags |= PWinGravity;
9819 if (user_position)
9821 size_hints.flags &= ~ PPosition;
9822 size_hints.flags |= USPosition;
9824 #endif /* PWinGravity */
9826 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9828 #endif /* not USE_GTK */
9830 /* Used for IconicState or NormalState */
9832 void
9833 x_wm_set_window_state (f, state)
9834 struct frame *f;
9835 int state;
9837 #ifdef USE_X_TOOLKIT
9838 Arg al[1];
9840 XtSetArg (al[0], XtNinitialState, state);
9841 XtSetValues (f->output_data.x->widget, al, 1);
9842 #else /* not USE_X_TOOLKIT */
9843 Window window = FRAME_X_WINDOW (f);
9845 f->output_data.x->wm_hints.flags |= StateHint;
9846 f->output_data.x->wm_hints.initial_state = state;
9848 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9849 #endif /* not USE_X_TOOLKIT */
9852 void
9853 x_wm_set_icon_pixmap (f, pixmap_id)
9854 struct frame *f;
9855 int pixmap_id;
9857 Pixmap icon_pixmap, icon_mask;
9859 #if !defined USE_X_TOOLKIT && !defined USE_GTK
9860 Window window = FRAME_OUTER_WINDOW (f);
9861 #endif
9863 if (pixmap_id > 0)
9865 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9866 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9867 icon_mask = x_bitmap_mask (f, pixmap_id);
9868 f->output_data.x->wm_hints.icon_mask = icon_mask;
9870 else
9872 /* It seems there is no way to turn off use of an icon
9873 pixmap. */
9874 return;
9878 #ifdef USE_GTK
9880 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9881 return;
9884 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9887 Arg al[1];
9888 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9889 XtSetValues (f->output_data.x->widget, al, 1);
9890 XtSetArg (al[0], XtNiconMask, icon_mask);
9891 XtSetValues (f->output_data.x->widget, al, 1);
9894 #else /* not USE_X_TOOLKIT && not USE_GTK */
9896 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9897 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9899 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9902 void
9903 x_wm_set_icon_position (f, icon_x, icon_y)
9904 struct frame *f;
9905 int icon_x, icon_y;
9907 Window window = FRAME_OUTER_WINDOW (f);
9909 f->output_data.x->wm_hints.flags |= IconPositionHint;
9910 f->output_data.x->wm_hints.icon_x = icon_x;
9911 f->output_data.x->wm_hints.icon_y = icon_y;
9913 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9917 /***********************************************************************
9918 Fonts
9919 ***********************************************************************/
9921 #if GLYPH_DEBUG
9923 /* Check that FONT is valid on frame F. It is if it can be found in F's
9924 font table. */
9926 static void
9927 x_check_font (f, font)
9928 struct frame *f;
9929 struct font *font;
9931 Lisp_Object frame;
9933 xassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9934 if (font->driver->check)
9935 xassert (font->driver->check (f, font) == 0);
9938 #endif /* GLYPH_DEBUG != 0 */
9941 /***********************************************************************
9942 Initialization
9943 ***********************************************************************/
9945 #ifdef USE_X_TOOLKIT
9946 static XrmOptionDescRec emacs_options[] = {
9947 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9948 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9950 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9951 XrmoptionSepArg, NULL},
9952 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9954 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9955 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9956 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9957 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9958 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9959 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9960 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9963 /* Whether atimer for Xt timeouts is activated or not. */
9965 static int x_timeout_atimer_activated_flag;
9967 #endif /* USE_X_TOOLKIT */
9969 static int x_initialized;
9971 #ifdef HAVE_X_SM
9972 static int x_session_initialized;
9973 #endif
9975 /* Test whether two display-name strings agree up to the dot that separates
9976 the screen number from the server number. */
9977 static int
9978 same_x_server (name1, name2)
9979 const char *name1, *name2;
9981 int seen_colon = 0;
9982 const unsigned char *system_name = SDATA (Vsystem_name);
9983 int system_name_length = strlen (system_name);
9984 int length_until_period = 0;
9986 while (system_name[length_until_period] != 0
9987 && system_name[length_until_period] != '.')
9988 length_until_period++;
9990 /* Treat `unix' like an empty host name. */
9991 if (! strncmp (name1, "unix:", 5))
9992 name1 += 4;
9993 if (! strncmp (name2, "unix:", 5))
9994 name2 += 4;
9995 /* Treat this host's name like an empty host name. */
9996 if (! strncmp (name1, system_name, system_name_length)
9997 && name1[system_name_length] == ':')
9998 name1 += system_name_length;
9999 if (! strncmp (name2, system_name, system_name_length)
10000 && name2[system_name_length] == ':')
10001 name2 += system_name_length;
10002 /* Treat this host's domainless name like an empty host name. */
10003 if (! strncmp (name1, system_name, length_until_period)
10004 && name1[length_until_period] == ':')
10005 name1 += length_until_period;
10006 if (! strncmp (name2, system_name, length_until_period)
10007 && name2[length_until_period] == ':')
10008 name2 += length_until_period;
10010 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10012 if (*name1 == ':')
10013 seen_colon++;
10014 if (seen_colon && *name1 == '.')
10015 return 1;
10017 return (seen_colon
10018 && (*name1 == '.' || *name1 == '\0')
10019 && (*name2 == '.' || *name2 == '\0'));
10022 /* Count number of set bits in mask and number of bits to shift to
10023 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10024 to 5. */
10025 static void
10026 get_bits_and_offset (mask, bits, offset)
10027 unsigned long mask;
10028 int *bits;
10029 int *offset;
10031 int nr = 0;
10032 int off = 0;
10034 while (!(mask & 1))
10036 off++;
10037 mask >>= 1;
10040 while (mask & 1)
10042 nr++;
10043 mask >>= 1;
10046 *offset = off;
10047 *bits = nr;
10050 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
10051 But don't permanently open it, just test its availability. */
10054 x_display_ok (display)
10055 const char *display;
10057 int dpy_ok = 1;
10058 Display *dpy;
10060 dpy = XOpenDisplay (display);
10061 if (dpy)
10062 XCloseDisplay (dpy);
10063 else
10064 dpy_ok = 0;
10065 return dpy_ok;
10068 #ifdef USE_GTK
10069 static void
10070 my_log_handler (log_domain, log_level, message, user_data)
10071 const gchar *log_domain;
10072 GLogLevelFlags log_level;
10073 const gchar *message;
10074 gpointer user_data;
10076 if (!strstr (message, "g_set_prgname"))
10077 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, message);
10079 #endif
10081 /* Open a connection to X display DISPLAY_NAME, and return
10082 the structure that describes the open display.
10083 If we cannot contact the display, return null. */
10085 struct x_display_info *
10086 x_term_init (display_name, xrm_option, resource_name)
10087 Lisp_Object display_name;
10088 char *xrm_option;
10089 char *resource_name;
10091 int connection;
10092 Display *dpy;
10093 struct terminal *terminal;
10094 struct x_display_info *dpyinfo;
10095 XrmDatabase xrdb;
10097 BLOCK_INPUT;
10099 if (!x_initialized)
10101 x_initialize ();
10102 ++x_initialized;
10105 if (! x_display_ok (SDATA (display_name)))
10106 error ("Display %s can't be opened", SDATA (display_name));
10108 #ifdef USE_GTK
10110 #define NUM_ARGV 10
10111 int argc;
10112 char *argv[NUM_ARGV];
10113 char **argv2 = argv;
10114 guint id;
10115 #ifndef HAVE_GTK_MULTIDISPLAY
10116 if (!EQ (Vinitial_window_system, Qx))
10117 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10118 #endif
10120 if (x_initialized++ > 1)
10122 #ifdef HAVE_GTK_MULTIDISPLAY
10123 /* Opening another display. If xg_display_open returns less
10124 than zero, we are probably on GTK 2.0, which can only handle
10125 one display. GTK 2.2 or later can handle more than one. */
10126 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10127 #endif
10128 error ("Sorry, this version of GTK can only handle one display");
10130 else
10132 for (argc = 0; argc < NUM_ARGV; ++argc)
10133 argv[argc] = 0;
10135 argc = 0;
10136 argv[argc++] = initial_argv[0];
10138 if (! NILP (display_name))
10140 argv[argc++] = "--display";
10141 argv[argc++] = SDATA (display_name);
10144 argv[argc++] = "--name";
10145 argv[argc++] = resource_name;
10147 XSetLocaleModifiers ("");
10149 /* Work around GLib bug that outputs a faulty warning. See
10150 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
10151 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
10152 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
10153 gtk_init (&argc, &argv2);
10154 g_log_remove_handler ("GLib", id);
10156 /* gtk_init does set_locale. We must fix locale after calling it. */
10157 fixup_locale ();
10158 xg_initialize ();
10160 dpy = GDK_DISPLAY ();
10162 /* NULL window -> events for all windows go to our function */
10163 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10165 /* Load our own gtkrc if it exists. */
10167 char *file = "~/.emacs.d/gtkrc";
10168 Lisp_Object s, abs_file;
10170 s = make_string (file, strlen (file));
10171 abs_file = Fexpand_file_name (s, Qnil);
10173 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10174 gtk_rc_parse (SDATA (abs_file));
10177 XSetErrorHandler (x_error_handler);
10178 XSetIOErrorHandler (x_io_error_quitter);
10181 #else /* not USE_GTK */
10182 #ifdef USE_X_TOOLKIT
10183 /* weiner@footloose.sps.mot.com reports that this causes
10184 errors with X11R5:
10185 X protocol error: BadAtom (invalid Atom parameter)
10186 on protocol request 18skiloaf.
10187 So let's not use it until R6. */
10188 #ifdef HAVE_X11XTR6
10189 XtSetLanguageProc (NULL, NULL, NULL);
10190 #endif
10193 int argc = 0;
10194 char *argv[3];
10196 argv[0] = "";
10197 argc = 1;
10198 if (xrm_option)
10200 argv[argc++] = "-xrm";
10201 argv[argc++] = xrm_option;
10203 turn_on_atimers (0);
10204 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10205 resource_name, EMACS_CLASS,
10206 emacs_options, XtNumber (emacs_options),
10207 &argc, argv);
10208 turn_on_atimers (1);
10210 #ifdef HAVE_X11XTR6
10211 /* I think this is to compensate for XtSetLanguageProc. */
10212 fixup_locale ();
10213 #endif
10216 #else /* not USE_X_TOOLKIT */
10217 XSetLocaleModifiers ("");
10218 dpy = XOpenDisplay (SDATA (display_name));
10219 #endif /* not USE_X_TOOLKIT */
10220 #endif /* not USE_GTK*/
10222 /* Detect failure. */
10223 if (dpy == 0)
10225 UNBLOCK_INPUT;
10226 return 0;
10229 /* We have definitely succeeded. Record the new connection. */
10231 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10232 bzero (dpyinfo, sizeof *dpyinfo);
10234 terminal = x_create_terminal (dpyinfo);
10237 struct x_display_info *share;
10238 Lisp_Object tail;
10240 for (share = x_display_list, tail = x_display_name_list; share;
10241 share = share->next, tail = XCDR (tail))
10242 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10243 SDATA (display_name)))
10244 break;
10245 if (share)
10246 terminal->kboard = share->terminal->kboard;
10247 else
10249 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10250 init_kboard (terminal->kboard);
10251 terminal->kboard->Vwindow_system = Qx;
10253 /* Add the keyboard to the list before running Lisp code (via
10254 Qvendor_specific_keysyms below), since these are not traced
10255 via terminals but only through all_kboards. */
10256 terminal->kboard->next_kboard = all_kboards;
10257 all_kboards = terminal->kboard;
10259 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10261 char *vendor = ServerVendor (dpy);
10263 /* Protect terminal from GC before removing it from the
10264 list of terminals. */
10265 struct gcpro gcpro1;
10266 Lisp_Object gcpro_term;
10267 XSETTERMINAL (gcpro_term, terminal);
10268 GCPRO1 (gcpro_term);
10270 /* Temporarily hide the partially initialized terminal. */
10271 terminal_list = terminal->next_terminal;
10272 UNBLOCK_INPUT;
10273 terminal->kboard->Vsystem_key_alist
10274 = call1 (Qvendor_specific_keysyms,
10275 vendor ? build_string (vendor) : empty_unibyte_string);
10276 BLOCK_INPUT;
10277 terminal->next_terminal = terminal_list;
10278 terminal_list = terminal;
10279 UNGCPRO;
10282 /* Don't let the initial kboard remain current longer than necessary.
10283 That would cause problems if a file loaded on startup tries to
10284 prompt in the mini-buffer. */
10285 if (current_kboard == initial_kboard)
10286 current_kboard = terminal->kboard;
10288 terminal->kboard->reference_count++;
10291 /* Put this display on the chain. */
10292 dpyinfo->next = x_display_list;
10293 x_display_list = dpyinfo;
10295 /* Put it on x_display_name_list as well, to keep them parallel. */
10296 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10297 x_display_name_list);
10298 dpyinfo->name_list_element = XCAR (x_display_name_list);
10300 dpyinfo->display = dpy;
10302 /* Set the name of the terminal. */
10303 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10304 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10305 terminal->name[SBYTES (display_name)] = 0;
10307 #if 0
10308 XSetAfterFunction (x_current_display, x_trace_wire);
10309 #endif /* ! 0 */
10311 dpyinfo->x_id_name
10312 = (char *) xmalloc (SBYTES (Vinvocation_name)
10313 + SBYTES (Vsystem_name)
10314 + 2);
10315 sprintf (dpyinfo->x_id_name, "%s@%s",
10316 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10318 /* Figure out which modifier bits mean what. */
10319 x_find_modifier_meanings (dpyinfo);
10321 /* Get the scroll bar cursor. */
10322 #ifdef USE_GTK
10323 /* We must create a GTK cursor, it is required for GTK widgets. */
10324 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10325 #endif /* USE_GTK */
10327 dpyinfo->vertical_scroll_bar_cursor
10328 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10330 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10331 resource_name, EMACS_CLASS);
10332 #ifdef HAVE_XRMSETDATABASE
10333 XrmSetDatabase (dpyinfo->display, xrdb);
10334 #else
10335 dpyinfo->display->db = xrdb;
10336 #endif
10337 /* Put the rdb where we can find it in a way that works on
10338 all versions. */
10339 dpyinfo->xrdb = xrdb;
10341 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10342 DefaultScreen (dpyinfo->display));
10343 select_visual (dpyinfo);
10344 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10345 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10346 dpyinfo->client_leader_window = 0;
10347 dpyinfo->grabbed = 0;
10348 dpyinfo->reference_count = 0;
10349 dpyinfo->icon_bitmap_id = -1;
10350 dpyinfo->n_fonts = 0;
10351 dpyinfo->bitmaps = 0;
10352 dpyinfo->bitmaps_size = 0;
10353 dpyinfo->bitmaps_last = 0;
10354 dpyinfo->scratch_cursor_gc = 0;
10355 dpyinfo->mouse_face_mouse_frame = 0;
10356 dpyinfo->mouse_face_deferred_gc = 0;
10357 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10358 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10359 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10360 dpyinfo->mouse_face_window = Qnil;
10361 dpyinfo->mouse_face_overlay = Qnil;
10362 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10363 dpyinfo->mouse_face_defer = 0;
10364 dpyinfo->mouse_face_hidden = 0;
10365 dpyinfo->x_focus_frame = 0;
10366 dpyinfo->x_focus_event_frame = 0;
10367 dpyinfo->x_highlight_frame = 0;
10368 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10370 /* See if we can construct pixel values from RGB values. */
10371 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10372 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10374 if (dpyinfo->visual->class == TrueColor)
10376 get_bits_and_offset (dpyinfo->visual->red_mask,
10377 &dpyinfo->red_bits, &dpyinfo->red_offset);
10378 get_bits_and_offset (dpyinfo->visual->blue_mask,
10379 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10380 get_bits_and_offset (dpyinfo->visual->green_mask,
10381 &dpyinfo->green_bits, &dpyinfo->green_offset);
10384 /* See if a private colormap is requested. */
10385 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10387 if (dpyinfo->visual->class == PseudoColor)
10389 Lisp_Object value;
10390 value = display_x_get_resource (dpyinfo,
10391 build_string ("privateColormap"),
10392 build_string ("PrivateColormap"),
10393 Qnil, Qnil);
10394 if (STRINGP (value)
10395 && (!strcmp (SDATA (value), "true")
10396 || !strcmp (SDATA (value), "on")))
10397 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10400 else
10401 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10402 dpyinfo->visual, AllocNone);
10404 #ifdef HAVE_XFT
10406 /* If we are using Xft, check dpi value in X resources.
10407 It is better we use it as well, since Xft will use it, as will all
10408 Gnome applications. If our real DPI is smaller or larger than the
10409 one Xft uses, our font will look smaller or larger than other
10410 for other applications, even if it is the same font name (monospace-10
10411 for example). */
10412 char *v = XGetDefault (dpyinfo->display, "Xft", "dpi");
10413 double d;
10414 if (v != NULL && sscanf (v, "%lf", &d) == 1)
10415 dpyinfo->resy = dpyinfo->resx = d;
10417 #endif
10419 if (dpyinfo->resy < 1)
10421 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10422 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10423 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10424 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10425 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10426 pixels = DisplayWidth (dpyinfo->display, screen_number);
10427 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10428 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10429 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10432 dpyinfo->Xatom_wm_protocols
10433 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10434 dpyinfo->Xatom_wm_take_focus
10435 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10436 dpyinfo->Xatom_wm_save_yourself
10437 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10438 dpyinfo->Xatom_wm_delete_window
10439 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10440 dpyinfo->Xatom_wm_change_state
10441 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10442 dpyinfo->Xatom_wm_configure_denied
10443 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10444 dpyinfo->Xatom_wm_window_moved
10445 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10446 dpyinfo->Xatom_wm_client_leader
10447 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10448 dpyinfo->Xatom_editres
10449 = XInternAtom (dpyinfo->display, "Editres", False);
10450 dpyinfo->Xatom_CLIPBOARD
10451 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10452 dpyinfo->Xatom_TIMESTAMP
10453 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10454 dpyinfo->Xatom_TEXT
10455 = XInternAtom (dpyinfo->display, "TEXT", False);
10456 dpyinfo->Xatom_COMPOUND_TEXT
10457 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10458 dpyinfo->Xatom_UTF8_STRING
10459 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10460 dpyinfo->Xatom_DELETE
10461 = XInternAtom (dpyinfo->display, "DELETE", False);
10462 dpyinfo->Xatom_MULTIPLE
10463 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10464 dpyinfo->Xatom_INCR
10465 = XInternAtom (dpyinfo->display, "INCR", False);
10466 dpyinfo->Xatom_EMACS_TMP
10467 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10468 dpyinfo->Xatom_TARGETS
10469 = XInternAtom (dpyinfo->display, "TARGETS", False);
10470 dpyinfo->Xatom_NULL
10471 = XInternAtom (dpyinfo->display, "NULL", False);
10472 dpyinfo->Xatom_ATOM_PAIR
10473 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10474 /* For properties of font. */
10475 dpyinfo->Xatom_PIXEL_SIZE
10476 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10477 dpyinfo->Xatom_AVERAGE_WIDTH
10478 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10479 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10480 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10481 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10482 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10483 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10484 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10486 /* Ghostscript support. */
10487 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10488 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10490 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10491 False);
10493 dpyinfo->Xatom_XEMBED = XInternAtom (dpyinfo->display, "_XEMBED",
10494 False);
10496 dpyinfo->Xatom_net_wm_state
10497 = XInternAtom (dpyinfo->display, "_NET_WM_STATE", False);
10498 dpyinfo->Xatom_net_wm_state_fullscreen_atom
10499 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_FULLSCREEN", False);
10500 dpyinfo->Xatom_net_wm_state_maximized_horz
10501 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
10502 dpyinfo->Xatom_net_wm_state_maximized_vert
10503 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
10504 dpyinfo->Xatom_net_wm_state_sticky
10505 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_STICKY", False);
10506 dpyinfo->Xatom_net_window_type
10507 = XInternAtom (dpyinfo->display, "_NET_WM_WINDOW_TYPE", False);
10508 dpyinfo->Xatom_net_window_type_tooltip
10509 = XInternAtom (dpyinfo->display, "_NET_WM_WINDOW_TYPE_TOOLTIP", False);
10511 dpyinfo->cut_buffers_initialized = 0;
10513 dpyinfo->x_dnd_atoms_size = 8;
10514 dpyinfo->x_dnd_atoms_length = 0;
10515 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10516 * dpyinfo->x_dnd_atoms_size);
10518 dpyinfo->net_supported_atoms = NULL;
10519 dpyinfo->nr_net_supported_atoms = 0;
10520 dpyinfo->net_supported_window = 0;
10522 connection = ConnectionNumber (dpyinfo->display);
10523 dpyinfo->connection = connection;
10526 extern int gray_bitmap_width, gray_bitmap_height;
10527 extern char *gray_bitmap_bits;
10528 dpyinfo->gray
10529 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10530 gray_bitmap_bits,
10531 gray_bitmap_width, gray_bitmap_height,
10532 (unsigned long) 1, (unsigned long) 0, 1);
10535 #ifdef HAVE_X_I18N
10536 xim_initialize (dpyinfo, resource_name);
10537 #endif
10539 xsettings_initialize (dpyinfo);
10541 #ifdef subprocesses
10542 /* This is only needed for distinguishing keyboard and process input. */
10543 if (connection != 0)
10544 add_keyboard_wait_descriptor (connection);
10545 #endif
10547 #ifdef F_SETOWN
10548 fcntl (connection, F_SETOWN, getpid ());
10549 #endif /* ! defined (F_SETOWN) */
10551 #ifdef SIGIO
10552 if (interrupt_input)
10553 init_sigio (connection);
10554 #endif /* ! defined (SIGIO) */
10556 #ifdef USE_LUCID
10558 Display *dpy = dpyinfo->display;
10559 XrmValue d, fr, to;
10560 Font font;
10562 d.addr = (XPointer)&dpy;
10563 d.size = sizeof (Display *);
10564 fr.addr = XtDefaultFont;
10565 fr.size = sizeof (XtDefaultFont);
10566 to.size = sizeof (Font *);
10567 to.addr = (XPointer)&font;
10568 x_catch_errors (dpy);
10569 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10570 abort ();
10571 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10572 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10573 x_uncatch_errors ();
10575 #endif
10577 /* See if we should run in synchronous mode. This is useful
10578 for debugging X code. */
10580 Lisp_Object value;
10581 value = display_x_get_resource (dpyinfo,
10582 build_string ("synchronous"),
10583 build_string ("Synchronous"),
10584 Qnil, Qnil);
10585 if (STRINGP (value)
10586 && (!strcmp (SDATA (value), "true")
10587 || !strcmp (SDATA (value), "on")))
10588 XSynchronize (dpyinfo->display, True);
10592 Lisp_Object value;
10593 value = display_x_get_resource (dpyinfo,
10594 build_string ("useXIM"),
10595 build_string ("UseXIM"),
10596 Qnil, Qnil);
10597 #ifdef USE_XIM
10598 if (STRINGP (value)
10599 && (!strcmp (SDATA (value), "false")
10600 || !strcmp (SDATA (value), "off")))
10601 use_xim = 0;
10602 #else
10603 if (STRINGP (value)
10604 && (!strcmp (SDATA (value), "true")
10605 || !strcmp (SDATA (value), "on")))
10606 use_xim = 1;
10607 #endif
10610 #ifdef HAVE_X_SM
10611 /* Only do this for the very first display in the Emacs session.
10612 Ignore X session management when Emacs was first started on a
10613 tty. */
10614 if (terminal->id == 1)
10615 x_session_initialize (dpyinfo);
10616 #endif
10618 UNBLOCK_INPUT;
10620 return dpyinfo;
10623 /* Get rid of display DPYINFO, deleting all frames on it,
10624 and without sending any more commands to the X server. */
10626 void
10627 x_delete_display (dpyinfo)
10628 struct x_display_info *dpyinfo;
10630 struct terminal *t;
10632 /* Close all frames and delete the generic struct terminal for this
10633 X display. */
10634 for (t = terminal_list; t; t = t->next_terminal)
10635 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10637 #ifdef HAVE_X_SM
10638 /* Close X session management when we close its display. */
10639 if (t->id == 1 && x_session_have_connection ())
10640 x_session_close();
10641 #endif
10642 delete_terminal (t);
10643 break;
10646 delete_keyboard_wait_descriptor (dpyinfo->connection);
10648 /* Discard this display from x_display_name_list and x_display_list.
10649 We can't use Fdelq because that can quit. */
10650 if (! NILP (x_display_name_list)
10651 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10652 x_display_name_list = XCDR (x_display_name_list);
10653 else
10655 Lisp_Object tail;
10657 tail = x_display_name_list;
10658 while (CONSP (tail) && CONSP (XCDR (tail)))
10660 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10662 XSETCDR (tail, XCDR (XCDR (tail)));
10663 break;
10665 tail = XCDR (tail);
10669 if (next_noop_dpyinfo == dpyinfo)
10670 next_noop_dpyinfo = dpyinfo->next;
10672 if (x_display_list == dpyinfo)
10673 x_display_list = dpyinfo->next;
10674 else
10676 struct x_display_info *tail;
10678 for (tail = x_display_list; tail; tail = tail->next)
10679 if (tail->next == dpyinfo)
10680 tail->next = tail->next->next;
10683 xfree (dpyinfo->x_id_name);
10684 xfree (dpyinfo->x_dnd_atoms);
10685 xfree (dpyinfo->color_cells);
10686 xfree (dpyinfo);
10689 #ifdef USE_X_TOOLKIT
10691 /* Atimer callback function for TIMER. Called every 0.1s to process
10692 Xt timeouts, if needed. We must avoid calling XtAppPending as
10693 much as possible because that function does an implicit XFlush
10694 that slows us down. */
10696 static void
10697 x_process_timeouts (timer)
10698 struct atimer *timer;
10700 BLOCK_INPUT;
10701 x_timeout_atimer_activated_flag = 0;
10702 if (toolkit_scroll_bar_interaction || popup_activated ())
10704 while (XtAppPending (Xt_app_con) & XtIMTimer)
10705 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10706 /* Reactivate the atimer for next time. */
10707 x_activate_timeout_atimer ();
10709 UNBLOCK_INPUT;
10712 /* Install an asynchronous timer that processes Xt timeout events
10713 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10714 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10715 function whenever these variables are set. This is necessary
10716 because some widget sets use timeouts internally, for example the
10717 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10718 processed, these widgets don't behave normally. */
10720 void
10721 x_activate_timeout_atimer ()
10723 BLOCK_INPUT;
10724 if (!x_timeout_atimer_activated_flag)
10726 EMACS_TIME interval;
10728 EMACS_SET_SECS_USECS (interval, 0, 100000);
10729 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10730 x_timeout_atimer_activated_flag = 1;
10732 UNBLOCK_INPUT;
10735 #endif /* USE_X_TOOLKIT */
10738 /* Set up use of X before we make the first connection. */
10740 extern frame_parm_handler x_frame_parm_handlers[];
10742 static struct redisplay_interface x_redisplay_interface =
10744 x_frame_parm_handlers,
10745 x_produce_glyphs,
10746 x_write_glyphs,
10747 x_insert_glyphs,
10748 x_clear_end_of_line,
10749 x_scroll_run,
10750 x_after_update_window_line,
10751 x_update_window_begin,
10752 x_update_window_end,
10753 x_cursor_to,
10754 x_flush,
10755 #ifdef XFlush
10756 x_flush,
10757 #else
10758 0, /* flush_display_optional */
10759 #endif
10760 x_clear_window_mouse_face,
10761 x_get_glyph_overhangs,
10762 x_fix_overlapping_area,
10763 x_draw_fringe_bitmap,
10764 0, /* define_fringe_bitmap */
10765 0, /* destroy_fringe_bitmap */
10766 x_compute_glyph_string_overhangs,
10767 x_draw_glyph_string,
10768 x_define_frame_cursor,
10769 x_clear_frame_area,
10770 x_draw_window_cursor,
10771 x_draw_vertical_window_border,
10772 x_shift_glyphs_for_insert
10776 /* This function is called when the last frame on a display is deleted. */
10777 void
10778 x_delete_terminal (struct terminal *terminal)
10780 struct x_display_info *dpyinfo = terminal->display_info.x;
10782 /* Protect against recursive calls. delete_frame in
10783 delete_terminal calls us back when it deletes our last frame. */
10784 if (!terminal->name)
10785 return;
10787 BLOCK_INPUT;
10788 #ifdef HAVE_X_I18N
10789 /* We must close our connection to the XIM server before closing the
10790 X display. */
10791 if (dpyinfo->xim)
10792 xim_close_dpy (dpyinfo);
10793 #endif
10795 /* If called from x_connection_closed, the display may already be closed
10796 and dpyinfo->display was set to 0 to indicate that. */
10797 if (dpyinfo->display)
10799 x_destroy_all_bitmaps (dpyinfo);
10800 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10802 /* Whether or not XCloseDisplay destroys the associated resource
10803 database depends on the version of libX11. To avoid both
10804 crash and memory leak, we dissociate the database from the
10805 display and then destroy dpyinfo->xrdb ourselves.
10807 Unfortunately, the above strategy does not work in some
10808 situations due to a bug in newer versions of libX11: because
10809 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
10810 dpy->db is NULL, XCloseDisplay destroys the associated
10811 database whereas it has not been created by XGetDefault
10812 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
10813 don't destroy the database here in order to avoid the crash
10814 in the above situations for now, though that may cause memory
10815 leaks in other situations. */
10816 #if 0
10817 #ifdef HAVE_XRMSETDATABASE
10818 XrmSetDatabase (dpyinfo->display, NULL);
10819 #else
10820 dpyinfo->display->db = NULL;
10821 #endif
10822 /* We used to call XrmDestroyDatabase from x_delete_display, but
10823 some older versions of libX11 crash if we call it after
10824 closing all the displays. */
10825 XrmDestroyDatabase (dpyinfo->xrdb);
10826 #endif
10828 #ifdef USE_GTK
10829 xg_display_close (dpyinfo->display);
10830 #else
10831 #ifdef USE_X_TOOLKIT
10832 XtCloseDisplay (dpyinfo->display);
10833 #else
10834 XCloseDisplay (dpyinfo->display);
10835 #endif
10836 #endif /* ! USE_GTK */
10839 /* Mark as dead. */
10840 dpyinfo->display = NULL;
10841 x_delete_display (dpyinfo);
10842 UNBLOCK_INPUT;
10845 /* Create a struct terminal, initialize it with the X11 specific
10846 functions and make DISPLAY->TERMINAL point to it. */
10848 static struct terminal *
10849 x_create_terminal (struct x_display_info *dpyinfo)
10851 struct terminal *terminal;
10853 terminal = create_terminal ();
10855 terminal->type = output_x_window;
10856 terminal->display_info.x = dpyinfo;
10857 dpyinfo->terminal = terminal;
10859 /* kboard is initialized in x_term_init. */
10861 terminal->clear_frame_hook = x_clear_frame;
10862 terminal->ins_del_lines_hook = x_ins_del_lines;
10863 terminal->delete_glyphs_hook = x_delete_glyphs;
10864 terminal->ring_bell_hook = XTring_bell;
10865 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
10866 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
10867 terminal->set_terminal_modes_hook = XTset_terminal_modes;
10868 terminal->update_begin_hook = x_update_begin;
10869 terminal->update_end_hook = x_update_end;
10870 terminal->set_terminal_window_hook = XTset_terminal_window;
10871 terminal->read_socket_hook = XTread_socket;
10872 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10873 terminal->mouse_position_hook = XTmouse_position;
10874 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10875 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10876 terminal->fullscreen_hook = XTfullscreen_hook;
10877 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10878 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10879 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10880 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10882 terminal->delete_frame_hook = x_destroy_window;
10883 terminal->delete_terminal_hook = x_delete_terminal;
10885 terminal->rif = &x_redisplay_interface;
10886 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10887 terminal->char_ins_del_ok = 1;
10888 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10889 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10890 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10891 off the bottom. */
10893 return terminal;
10896 void
10897 x_initialize ()
10899 baud_rate = 19200;
10901 x_noop_count = 0;
10902 last_tool_bar_item = -1;
10903 any_help_event_p = 0;
10904 ignore_next_mouse_click_timeout = 0;
10905 #ifdef HAVE_X_SM
10906 x_session_initialized = 0;
10907 #endif
10909 #ifdef USE_GTK
10910 current_count = -1;
10911 #endif
10913 /* Try to use interrupt input; if we can't, then start polling. */
10914 Fset_input_interrupt_mode (Qt);
10916 #ifdef USE_X_TOOLKIT
10917 XtToolkitInitialize ();
10919 Xt_app_con = XtCreateApplicationContext ();
10921 /* Register a converter from strings to pixels, which uses
10922 Emacs' color allocation infrastructure. */
10923 XtAppSetTypeConverter (Xt_app_con,
10924 XtRString, XtRPixel, cvt_string_to_pixel,
10925 cvt_string_to_pixel_args,
10926 XtNumber (cvt_string_to_pixel_args),
10927 XtCacheByDisplay, cvt_pixel_dtor);
10929 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10930 #endif
10932 #ifdef USE_TOOLKIT_SCROLL_BARS
10933 #ifndef USE_GTK
10934 xaw3d_arrow_scroll = False;
10935 xaw3d_pick_top = True;
10936 #endif
10937 #endif
10939 pending_autoraise_frame = 0;
10940 pending_event_wait.f = 0;
10941 pending_event_wait.eventtype = 0;
10943 /* Note that there is no real way portable across R3/R4 to get the
10944 original error handler. */
10945 XSetErrorHandler (x_error_handler);
10946 XSetIOErrorHandler (x_io_error_quitter);
10948 signal (SIGPIPE, x_connection_signal);
10950 xgselect_initialize ();
10954 void
10955 syms_of_xterm ()
10957 x_error_message = NULL;
10959 staticpro (&x_display_name_list);
10960 x_display_name_list = Qnil;
10962 staticpro (&last_mouse_scroll_bar);
10963 last_mouse_scroll_bar = Qnil;
10965 staticpro (&Qvendor_specific_keysyms);
10966 Qvendor_specific_keysyms = intern_c_string ("vendor-specific-keysyms");
10968 staticpro (&Qlatin_1);
10969 Qlatin_1 = intern_c_string ("latin-1");
10971 staticpro (&last_mouse_press_frame);
10972 last_mouse_press_frame = Qnil;
10974 #ifdef USE_GTK
10975 xg_default_icon_file = make_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
10976 staticpro (&xg_default_icon_file);
10978 Qx_gtk_map_stock = intern_c_string ("x-gtk-map-stock");
10979 staticpro (&Qx_gtk_map_stock);
10980 #endif
10982 DEFVAR_BOOL ("x-use-underline-position-properties",
10983 &x_use_underline_position_properties,
10984 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10985 A value of nil means ignore them. If you encounter fonts with bogus
10986 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10987 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10988 to override the font's UNDERLINE_POSITION for small font display
10989 sizes. */);
10990 x_use_underline_position_properties = 1;
10992 DEFVAR_BOOL ("x-underline-at-descent-line",
10993 &x_underline_at_descent_line,
10994 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
10995 A value of nil means to draw the underline according to the value of the
10996 variable `x-use-underline-position-properties', which is usually at the
10997 baseline level. The default value is nil. */);
10998 x_underline_at_descent_line = 0;
11000 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
11001 &x_mouse_click_focus_ignore_position,
11002 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
11003 This variable is only used when the window manager requires that you
11004 click on a frame to select it (give it focus). In that case, a value
11005 of nil, means that the selected window and cursor position changes to
11006 reflect the mouse click position, while a non-nil value means that the
11007 selected window or cursor position is preserved. */);
11008 x_mouse_click_focus_ignore_position = 0;
11010 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
11011 doc: /* What X toolkit scroll bars Emacs uses.
11012 A value of nil means Emacs doesn't use X toolkit scroll bars.
11013 Otherwise, value is a symbol describing the X toolkit. */);
11014 #ifdef USE_TOOLKIT_SCROLL_BARS
11015 #ifdef USE_MOTIF
11016 Vx_toolkit_scroll_bars = intern_c_string ("motif");
11017 #elif defined HAVE_XAW3D
11018 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
11019 #elif USE_GTK
11020 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
11021 #else
11022 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
11023 #endif
11024 #else
11025 Vx_toolkit_scroll_bars = Qnil;
11026 #endif
11028 staticpro (&last_mouse_motion_frame);
11029 last_mouse_motion_frame = Qnil;
11031 Qmodifier_value = intern_c_string ("modifier-value");
11032 Qalt = intern_c_string ("alt");
11033 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
11034 Qhyper = intern_c_string ("hyper");
11035 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11036 Qmeta = intern_c_string ("meta");
11037 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11038 Qsuper = intern_c_string ("super");
11039 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11041 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
11042 doc: /* Which keys Emacs uses for the alt modifier.
11043 This should be one of the symbols `alt', `hyper', `meta', `super'.
11044 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11045 is nil, which is the same as `alt'. */);
11046 Vx_alt_keysym = Qnil;
11048 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
11049 doc: /* Which keys Emacs uses for the hyper modifier.
11050 This should be one of the symbols `alt', `hyper', `meta', `super'.
11051 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11052 default is nil, which is the same as `hyper'. */);
11053 Vx_hyper_keysym = Qnil;
11055 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
11056 doc: /* Which keys Emacs uses for the meta modifier.
11057 This should be one of the symbols `alt', `hyper', `meta', `super'.
11058 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11059 default is nil, which is the same as `meta'. */);
11060 Vx_meta_keysym = Qnil;
11062 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
11063 doc: /* Which keys Emacs uses for the super modifier.
11064 This should be one of the symbols `alt', `hyper', `meta', `super'.
11065 For example, `super' means use the Super_L and Super_R keysyms. The
11066 default is nil, which is the same as `super'. */);
11067 Vx_super_keysym = Qnil;
11069 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
11070 doc: /* Hash table of character codes indexed by X keysym codes. */);
11071 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
11072 make_float (DEFAULT_REHASH_SIZE),
11073 make_float (DEFAULT_REHASH_THRESHOLD),
11074 Qnil, Qnil, Qnil);
11077 #endif /* HAVE_X_WINDOWS */
11079 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
11080 (do not change this comment) */