* mark.texi (Mark): Clarify introduction.
[emacs.git] / src / xterm.c
blobec6c4d4011f9ad785461efee0b421d983ebce53d
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
22 /* Xt features made by Fred Pierresteguy. */
24 #include <config.h>
26 /* On 4.3 these lose if they come after xterm.h. */
27 /* Putting these at the beginning seems to be standard for other .c files. */
28 #include <signal.h>
30 #include <stdio.h>
32 #ifdef HAVE_X_WINDOWS
34 #include "lisp.h"
35 #include "blockinput.h"
37 /* Need syssignal.h for various externs and definitions that may be required
38 by some configurations for calls to signal later in this source file. */
39 #include "syssignal.h"
41 /* This may include sys/types.h, and that somehow loses
42 if this is not done before the other system files. */
43 #include "xterm.h"
44 #include <X11/cursorfont.h>
46 /* Load sys/types.h if not already loaded.
47 In some systems loading it twice is suicidal. */
48 #ifndef makedev
49 #include <sys/types.h>
50 #endif /* makedev */
52 #ifdef BSD_SYSTEM
53 #include <sys/ioctl.h>
54 #endif /* ! defined (BSD_SYSTEM) */
56 #include "systime.h"
58 #ifndef INCLUDED_FCNTL
59 #include <fcntl.h>
60 #endif
61 #include <ctype.h>
62 #include <errno.h>
63 #include <setjmp.h>
64 #include <sys/stat.h>
65 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
66 /* #include <sys/param.h> */
68 #include "charset.h"
69 #include "character.h"
70 #include "coding.h"
71 #include "frame.h"
72 #include "dispextern.h"
73 #include "fontset.h"
74 #include "termhooks.h"
75 #include "termopts.h"
76 #include "termchar.h"
77 #include "emacs-icon.h"
78 #include "disptab.h"
79 #include "buffer.h"
80 #include "window.h"
81 #include "keyboard.h"
82 #include "intervals.h"
83 #include "process.h"
84 #include "atimer.h"
85 #include "keymap.h"
86 #include "font.h"
87 #include "fontset.h"
89 #ifdef USE_X_TOOLKIT
90 #include <X11/Shell.h>
91 #endif
93 #ifdef HAVE_SYS_TIME_H
94 #include <sys/time.h>
95 #endif
96 #ifdef HAVE_UNISTD_H
97 #include <unistd.h>
98 #endif
100 #ifdef USE_GTK
101 #include "gtkutil.h"
102 #endif
104 #ifdef USE_LUCID
105 extern int xlwmenu_window_p P_ ((Widget w, Window window));
106 extern void xlwmenu_redisplay P_ ((Widget));
107 #endif
109 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
111 extern void free_frame_menubar P_ ((struct frame *));
112 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
113 int));
114 #endif
116 #ifdef USE_X_TOOLKIT
117 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
118 #define HACK_EDITRES
119 extern void _XEditResCheckMessages ();
120 #endif /* not NO_EDITRES */
122 /* Include toolkit specific headers for the scroll bar widget. */
124 #ifdef USE_TOOLKIT_SCROLL_BARS
125 #if defined USE_MOTIF
126 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
127 #include <Xm/ScrollBar.h>
128 #else /* !USE_MOTIF i.e. use Xaw */
130 #ifdef HAVE_XAW3D
131 #include <X11/Xaw3d/Simple.h>
132 #include <X11/Xaw3d/Scrollbar.h>
133 #include <X11/Xaw3d/ThreeD.h>
134 #else /* !HAVE_XAW3D */
135 #include <X11/Xaw/Simple.h>
136 #include <X11/Xaw/Scrollbar.h>
137 #endif /* !HAVE_XAW3D */
138 #ifndef XtNpickTop
139 #define XtNpickTop "pickTop"
140 #endif /* !XtNpickTop */
141 #endif /* !USE_MOTIF */
142 #endif /* USE_TOOLKIT_SCROLL_BARS */
144 #endif /* USE_X_TOOLKIT */
146 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
147 #define x_any_window_to_frame x_window_to_frame
148 #define x_top_window_to_frame x_window_to_frame
149 #endif
151 #ifdef USE_X_TOOLKIT
152 #include "widget.h"
153 #ifndef XtNinitialState
154 #define XtNinitialState "initialState"
155 #endif
156 #endif
158 /* Default to using XIM if available. */
159 #ifdef USE_XIM
160 int use_xim = 1;
161 #else
162 int use_xim = 0; /* configure --without-xim */
163 #endif
167 /* Non-nil means Emacs uses toolkit scroll bars. */
169 Lisp_Object Vx_toolkit_scroll_bars;
171 /* Non-zero means that a HELP_EVENT has been generated since Emacs
172 start. */
174 static int any_help_event_p;
176 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
177 static Lisp_Object last_window;
179 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
181 int x_use_underline_position_properties;
183 /* Non-zero means to draw the underline at the same place as the descent line. */
185 int x_underline_at_descent_line;
187 /* This is a chain of structures for all the X displays currently in
188 use. */
190 struct x_display_info *x_display_list;
192 /* This is a list of cons cells, each of the form (NAME
193 . FONT-LIST-CACHE), one for each element of x_display_list and in
194 the same order. NAME is the name of the frame. FONT-LIST-CACHE
195 records previous values returned by x-list-fonts. */
197 Lisp_Object x_display_name_list;
199 /* Frame being updated by update_frame. This is declared in term.c.
200 This is set by update_begin and looked at by all the XT functions.
201 It is zero while not inside an update. In that case, the XT
202 functions assume that `selected_frame' is the frame to apply to. */
204 extern struct frame *updating_frame;
206 /* This is a frame waiting to be auto-raised, within XTread_socket. */
208 struct frame *pending_autoraise_frame;
210 #ifdef USE_X_TOOLKIT
211 /* The application context for Xt use. */
212 XtAppContext Xt_app_con;
213 static String Xt_default_resources[] = {0};
214 #endif /* USE_X_TOOLKIT */
216 /* Non-zero means user is interacting with a toolkit scroll bar. */
218 static int toolkit_scroll_bar_interaction;
220 /* Non-zero means to not move point as a result of clicking on a
221 frame to focus it (when focus-follows-mouse is nil). */
223 int x_mouse_click_focus_ignore_position;
225 /* Non-zero timeout value means ignore next mouse click if it arrives
226 before that timeout elapses (i.e. as part of the same sequence of
227 events resulting from clicking on a frame to select it). */
229 static unsigned long ignore_next_mouse_click_timeout;
231 /* Mouse movement.
233 Formerly, we used PointerMotionHintMask (in standard_event_mask)
234 so that we would have to call XQueryPointer after each MotionNotify
235 event to ask for another such event. However, this made mouse tracking
236 slow, and there was a bug that made it eventually stop.
238 Simply asking for MotionNotify all the time seems to work better.
240 In order to avoid asking for motion events and then throwing most
241 of them away or busy-polling the server for mouse positions, we ask
242 the server for pointer motion hints. This means that we get only
243 one event per group of mouse movements. "Groups" are delimited by
244 other kinds of events (focus changes and button clicks, for
245 example), or by XQueryPointer calls; when one of these happens, we
246 get another MotionNotify event the next time the mouse moves. This
247 is at least as efficient as getting motion events when mouse
248 tracking is on, and I suspect only negligibly worse when tracking
249 is off. */
251 /* Where the mouse was last time we reported a mouse event. */
253 static XRectangle last_mouse_glyph;
254 static FRAME_PTR last_mouse_glyph_frame;
255 static Lisp_Object last_mouse_press_frame;
257 /* The scroll bar in which the last X motion event occurred.
259 If the last X motion event occurred in a scroll bar, we set this so
260 XTmouse_position can know whether to report a scroll bar motion or
261 an ordinary motion.
263 If the last X motion event didn't occur in a scroll bar, we set
264 this to Qnil, to tell XTmouse_position to return an ordinary motion
265 event. */
267 static Lisp_Object last_mouse_scroll_bar;
269 /* This is a hack. We would really prefer that XTmouse_position would
270 return the time associated with the position it returns, but there
271 doesn't seem to be any way to wrest the time-stamp from the server
272 along with the position query. So, we just keep track of the time
273 of the last movement we received, and return that in hopes that
274 it's somewhat accurate. */
276 static Time last_mouse_movement_time;
278 /* Time for last user interaction as returned in X events. */
280 static Time last_user_time;
282 /* Incremented by XTread_socket whenever it really tries to read
283 events. */
285 #ifdef __STDC__
286 static int volatile input_signal_count;
287 #else
288 static int input_signal_count;
289 #endif
291 /* Used locally within XTread_socket. */
293 static int x_noop_count;
295 /* Initial values of argv and argc. */
297 extern char **initial_argv;
298 extern int initial_argc;
300 extern Lisp_Object Vcommand_line_args, Vsystem_name;
302 /* Tells if a window manager is present or not. */
304 extern Lisp_Object Vx_no_window_manager;
306 extern Lisp_Object Qeql;
308 extern int errno;
310 /* A mask of extra modifier bits to put into every keyboard char. */
312 extern EMACS_INT extra_keyboard_modifiers;
314 /* The keysyms to use for the various modifiers. */
316 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
317 Lisp_Object Vx_keysym_table;
318 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
320 static Lisp_Object Qvendor_specific_keysyms;
321 static Lisp_Object Qlatin_1;
323 /* Used in x_flush. */
325 extern Lisp_Object Vinhibit_redisplay;
327 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
328 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
330 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
331 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
332 static const XColor *x_color_cells P_ ((Display *, int *));
333 static void x_update_window_end P_ ((struct window *, int, int));
335 static int x_io_error_quitter P_ ((Display *));
336 static struct terminal *x_create_terminal P_ ((struct x_display_info *));
337 void x_delete_terminal P_ ((struct terminal *));
338 static void x_update_end P_ ((struct frame *));
339 static void XTframe_up_to_date P_ ((struct frame *));
340 static void XTset_terminal_modes P_ ((struct terminal *));
341 static void XTreset_terminal_modes P_ ((struct terminal *));
342 static void x_clear_frame P_ ((struct frame *));
343 static void frame_highlight P_ ((struct frame *));
344 static void frame_unhighlight P_ ((struct frame *));
345 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
346 static void x_focus_changed P_ ((int, int, struct x_display_info *,
347 struct frame *, struct input_event *));
348 static void x_detect_focus_change P_ ((struct x_display_info *,
349 XEvent *, struct input_event *));
350 static void XTframe_rehighlight P_ ((struct frame *));
351 static void x_frame_rehighlight P_ ((struct x_display_info *));
352 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
353 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
354 enum text_cursor_kinds));
356 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
357 static void x_flush P_ ((struct frame *f));
358 static void x_update_begin P_ ((struct frame *));
359 static void x_update_window_begin P_ ((struct window *));
360 static void x_after_update_window_line P_ ((struct glyph_row *));
361 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
362 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
363 enum scroll_bar_part *,
364 Lisp_Object *, Lisp_Object *,
365 unsigned long *));
366 static void x_handle_net_wm_state P_ ((struct frame *, XPropertyEvent *));
367 static void x_check_fullscreen P_ ((struct frame *));
368 static void x_check_expected_move P_ ((struct frame *, int, int));
369 static void x_sync_with_move P_ ((struct frame *, int, int, int));
370 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
371 int *, struct input_event *));
372 /* Don't declare this NO_RETURN because we want no
373 interference with debugging failing X calls. */
374 static SIGTYPE x_connection_closed P_ ((Display *, char *));
377 /* Flush display of frame F, or of all frames if F is null. */
379 static void
380 x_flush (f)
381 struct frame *f;
383 /* Don't call XFlush when it is not safe to redisplay; the X
384 connection may be broken. */
385 if (!NILP (Vinhibit_redisplay))
386 return;
388 BLOCK_INPUT;
389 if (f == NULL)
391 Lisp_Object rest, frame;
392 FOR_EACH_FRAME (rest, frame)
393 if (FRAME_X_P (XFRAME (frame)))
394 x_flush (XFRAME (frame));
396 else if (FRAME_X_P (f))
397 XFlush (FRAME_X_DISPLAY (f));
398 UNBLOCK_INPUT;
402 /* Remove calls to XFlush by defining XFlush to an empty replacement.
403 Calls to XFlush should be unnecessary because the X output buffer
404 is flushed automatically as needed by calls to XPending,
405 XNextEvent, or XWindowEvent according to the XFlush man page.
406 XTread_socket calls XPending. Removing XFlush improves
407 performance. */
409 #define XFlush(DISPLAY) (void) 0
412 /***********************************************************************
413 Debugging
414 ***********************************************************************/
416 #if 0
418 /* This is a function useful for recording debugging information about
419 the sequence of occurrences in this file. */
421 struct record
423 char *locus;
424 int type;
427 struct record event_record[100];
429 int event_record_index;
431 record_event (locus, type)
432 char *locus;
433 int type;
435 if (event_record_index == sizeof (event_record) / sizeof (struct record))
436 event_record_index = 0;
438 event_record[event_record_index].locus = locus;
439 event_record[event_record_index].type = type;
440 event_record_index++;
443 #endif /* 0 */
447 /* Return the struct x_display_info corresponding to DPY. */
449 struct x_display_info *
450 x_display_info_for_display (dpy)
451 Display *dpy;
453 struct x_display_info *dpyinfo;
455 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
456 if (dpyinfo->display == dpy)
457 return dpyinfo;
459 return 0;
462 #define OPAQUE 0xffffffff
463 #define OPACITY "_NET_WM_WINDOW_OPACITY"
465 void
466 x_set_frame_alpha (f)
467 struct frame *f;
469 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
470 Display *dpy = FRAME_X_DISPLAY (f);
471 Window win = FRAME_OUTER_WINDOW (f);
472 double alpha = 1.0;
473 double alpha_min = 1.0;
474 unsigned long opac;
476 if (FRAME_X_DISPLAY_INFO (f)->root_window != FRAME_X_OUTPUT (f)->parent_desc)
477 /* Since the WM decoration lies under the FRAME_OUTER_WINDOW,
478 we must treat the former instead of the latter. */
479 win = FRAME_X_OUTPUT(f)->parent_desc;
481 if (dpyinfo->x_highlight_frame == f)
482 alpha = f->alpha[0];
483 else
484 alpha = f->alpha[1];
486 if (FLOATP (Vframe_alpha_lower_limit))
487 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
488 else if (INTEGERP (Vframe_alpha_lower_limit))
489 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
491 if (alpha < 0.0)
492 return;
493 else if (alpha > 1.0)
494 alpha = 1.0;
495 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
496 alpha = alpha_min;
498 opac = alpha * OPAQUE;
500 /* return unless necessary */
502 unsigned char *data;
503 Atom actual;
504 int rc, format;
505 unsigned long n, left;
507 x_catch_errors (dpy);
508 rc = XGetWindowProperty(dpy, win, XInternAtom(dpy, OPACITY, False),
509 0L, 1L, False, XA_CARDINAL,
510 &actual, &format, &n, &left,
511 &data);
513 if (rc == Success && actual != None)
514 if (*(unsigned long *)data == opac)
516 XFree ((void *) data);
517 x_uncatch_errors ();
518 return;
520 else
521 XFree ((void *) data);
522 x_uncatch_errors ();
525 x_catch_errors (dpy);
526 XChangeProperty (dpy, win, XInternAtom (dpy, OPACITY, False),
527 XA_CARDINAL, 32, PropModeReplace,
528 (unsigned char *) &opac, 1L);
529 x_uncatch_errors ();
533 x_display_pixel_height (dpyinfo)
534 struct x_display_info *dpyinfo;
536 return HeightOfScreen (dpyinfo->screen);
540 x_display_pixel_width (dpyinfo)
541 struct x_display_info *dpyinfo;
543 return WidthOfScreen (dpyinfo->screen);
547 /***********************************************************************
548 Starting and ending an update
549 ***********************************************************************/
551 /* Start an update of frame F. This function is installed as a hook
552 for update_begin, i.e. it is called when update_begin is called.
553 This function is called prior to calls to x_update_window_begin for
554 each window being updated. Currently, there is nothing to do here
555 because all interesting stuff is done on a window basis. */
557 static void
558 x_update_begin (f)
559 struct frame *f;
561 /* Nothing to do. */
565 /* Start update of window W. Set the global variable updated_window
566 to the window being updated and set output_cursor to the cursor
567 position of W. */
569 static void
570 x_update_window_begin (w)
571 struct window *w;
573 struct frame *f = XFRAME (WINDOW_FRAME (w));
574 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
576 updated_window = w;
577 set_output_cursor (&w->cursor);
579 BLOCK_INPUT;
581 if (f == display_info->mouse_face_mouse_frame)
583 /* Don't do highlighting for mouse motion during the update. */
584 display_info->mouse_face_defer = 1;
586 /* If F needs to be redrawn, simply forget about any prior mouse
587 highlighting. */
588 if (FRAME_GARBAGED_P (f))
589 display_info->mouse_face_window = Qnil;
591 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
592 their mouse_face_p flag set, which means that they are always
593 unequal to rows in a desired matrix which never have that
594 flag set. So, rows containing mouse-face glyphs are never
595 scrolled, and we don't have to switch the mouse highlight off
596 here to prevent it from being scrolled. */
598 /* Can we tell that this update does not affect the window
599 where the mouse highlight is? If so, no need to turn off.
600 Likewise, don't do anything if the frame is garbaged;
601 in that case, the frame's current matrix that we would use
602 is all wrong, and we will redisplay that line anyway. */
603 if (!NILP (display_info->mouse_face_window)
604 && w == XWINDOW (display_info->mouse_face_window))
606 int i;
608 for (i = 0; i < w->desired_matrix->nrows; ++i)
609 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
610 break;
612 if (i < w->desired_matrix->nrows)
613 clear_mouse_face (display_info);
615 #endif /* 0 */
618 UNBLOCK_INPUT;
622 /* Draw a vertical window border from (x,y0) to (x,y1) */
624 static void
625 x_draw_vertical_window_border (w, x, y0, y1)
626 struct window *w;
627 int x, y0, y1;
629 struct frame *f = XFRAME (WINDOW_FRAME (w));
630 struct face *face;
632 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
633 if (face)
634 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
635 face->foreground);
637 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
638 f->output_data.x->normal_gc, x, y0, x, y1);
641 /* End update of window W (which is equal to updated_window).
643 Draw vertical borders between horizontally adjacent windows, and
644 display W's cursor if CURSOR_ON_P is non-zero.
646 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
647 glyphs in mouse-face were overwritten. In that case we have to
648 make sure that the mouse-highlight is properly redrawn.
650 W may be a menu bar pseudo-window in case we don't have X toolkit
651 support. Such windows don't have a cursor, so don't display it
652 here. */
654 static void
655 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
656 struct window *w;
657 int cursor_on_p, mouse_face_overwritten_p;
659 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
661 if (!w->pseudo_window_p)
663 BLOCK_INPUT;
665 if (cursor_on_p)
666 display_and_set_cursor (w, 1, output_cursor.hpos,
667 output_cursor.vpos,
668 output_cursor.x, output_cursor.y);
670 if (draw_window_fringes (w, 1))
671 x_draw_vertical_border (w);
673 UNBLOCK_INPUT;
676 /* If a row with mouse-face was overwritten, arrange for
677 XTframe_up_to_date to redisplay the mouse highlight. */
678 if (mouse_face_overwritten_p)
680 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
681 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
682 dpyinfo->mouse_face_window = Qnil;
685 updated_window = NULL;
689 /* End update of frame F. This function is installed as a hook in
690 update_end. */
692 static void
693 x_update_end (f)
694 struct frame *f;
696 /* Mouse highlight may be displayed again. */
697 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
699 #ifndef XFlush
700 BLOCK_INPUT;
701 XFlush (FRAME_X_DISPLAY (f));
702 UNBLOCK_INPUT;
703 #endif
707 /* This function is called from various places in xdisp.c whenever a
708 complete update has been performed. The global variable
709 updated_window is not available here. */
711 static void
712 XTframe_up_to_date (f)
713 struct frame *f;
715 if (FRAME_X_P (f))
717 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
719 if (dpyinfo->mouse_face_deferred_gc
720 || f == dpyinfo->mouse_face_mouse_frame)
722 BLOCK_INPUT;
723 if (dpyinfo->mouse_face_mouse_frame)
724 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
725 dpyinfo->mouse_face_mouse_x,
726 dpyinfo->mouse_face_mouse_y);
727 dpyinfo->mouse_face_deferred_gc = 0;
728 UNBLOCK_INPUT;
734 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
735 arrow bitmaps, or clear the fringes if no bitmaps are required
736 before DESIRED_ROW is made current. The window being updated is
737 found in updated_window. This function It is called from
738 update_window_line only if it is known that there are differences
739 between bitmaps to be drawn between current row and DESIRED_ROW. */
741 static void
742 x_after_update_window_line (desired_row)
743 struct glyph_row *desired_row;
745 struct window *w = updated_window;
746 struct frame *f;
747 int width, height;
749 xassert (w);
751 if (!desired_row->mode_line_p && !w->pseudo_window_p)
752 desired_row->redraw_fringe_bitmaps_p = 1;
754 /* When a window has disappeared, make sure that no rest of
755 full-width rows stays visible in the internal border. Could
756 check here if updated_window is the leftmost/rightmost window,
757 but I guess it's not worth doing since vertically split windows
758 are almost never used, internal border is rarely set, and the
759 overhead is very small. */
760 if (windows_or_buffers_changed
761 && desired_row->full_width_p
762 && (f = XFRAME (w->frame),
763 width = FRAME_INTERNAL_BORDER_WIDTH (f),
764 width != 0)
765 && (height = desired_row->visible_height,
766 height > 0))
768 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
770 /* Internal border is drawn below the tool bar. */
771 if (WINDOWP (f->tool_bar_window)
772 && w == XWINDOW (f->tool_bar_window))
773 y -= width;
775 BLOCK_INPUT;
776 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
777 0, y, width, height, False);
778 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
779 FRAME_PIXEL_WIDTH (f) - width,
780 y, width, height, False);
781 UNBLOCK_INPUT;
785 static void
786 x_draw_fringe_bitmap (w, row, p)
787 struct window *w;
788 struct glyph_row *row;
789 struct draw_fringe_bitmap_params *p;
791 struct frame *f = XFRAME (WINDOW_FRAME (w));
792 Display *display = FRAME_X_DISPLAY (f);
793 Window window = FRAME_X_WINDOW (f);
794 GC gc = f->output_data.x->normal_gc;
795 struct face *face = p->face;
796 int rowY;
798 /* Must clip because of partially visible lines. */
799 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
800 if (p->y < rowY)
802 /* Adjust position of "bottom aligned" bitmap on partially
803 visible last row. */
804 int oldY = row->y;
805 int oldVH = row->visible_height;
806 row->visible_height = p->h;
807 row->y -= rowY - p->y;
808 x_clip_to_row (w, row, -1, gc);
809 row->y = oldY;
810 row->visible_height = oldVH;
812 else
813 x_clip_to_row (w, row, -1, gc);
815 if (!p->overlay_p)
817 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
819 /* In case the same realized face is used for fringes and
820 for something displayed in the text (e.g. face `region' on
821 mono-displays, the fill style may have been changed to
822 FillSolid in x_draw_glyph_string_background. */
823 if (face->stipple)
824 XSetFillStyle (display, face->gc, FillOpaqueStippled);
825 else
826 XSetForeground (display, face->gc, face->background);
828 #ifdef USE_TOOLKIT_SCROLL_BARS
829 /* If the fringe is adjacent to the left (right) scroll bar of a
830 leftmost (rightmost, respectively) window, then extend its
831 background to the gap between the fringe and the bar. */
832 if ((WINDOW_LEFTMOST_P (w)
833 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
834 || (WINDOW_RIGHTMOST_P (w)
835 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
837 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
839 if (sb_width > 0)
841 int left = WINDOW_SCROLL_BAR_AREA_X (w);
842 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
843 * FRAME_COLUMN_WIDTH (f));
845 if (bx < 0)
847 /* Bitmap fills the fringe. */
848 if (left + width == p->x)
849 bx = left + sb_width;
850 else if (p->x + p->wd == left)
851 bx = left;
852 if (bx >= 0)
854 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
856 nx = width - sb_width;
857 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
858 row->y));
859 ny = row->visible_height;
862 else
864 if (left + width == bx)
866 bx = left + sb_width;
867 nx += width - sb_width;
869 else if (bx + nx == left)
870 nx += width - sb_width;
874 #endif
875 if (bx >= 0 && nx > 0)
876 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
878 if (!face->stipple)
879 XSetForeground (display, face->gc, face->foreground);
882 if (p->which)
884 unsigned char *bits;
885 Pixmap pixmap, clipmask = (Pixmap) 0;
886 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
887 XGCValues gcv;
889 if (p->wd > 8)
890 bits = (unsigned char *)(p->bits + p->dh);
891 else
892 bits = (unsigned char *)p->bits + p->dh;
894 /* Draw the bitmap. I believe these small pixmaps can be cached
895 by the server. */
896 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
897 (p->cursor_p
898 ? (p->overlay_p ? face->background
899 : f->output_data.x->cursor_pixel)
900 : face->foreground),
901 face->background, depth);
903 if (p->overlay_p)
905 clipmask = XCreatePixmapFromBitmapData (display,
906 FRAME_X_DISPLAY_INFO (f)->root_window,
907 bits, p->wd, p->h,
908 1, 0, 1);
909 gcv.clip_mask = clipmask;
910 gcv.clip_x_origin = p->x;
911 gcv.clip_y_origin = p->y;
912 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
915 XCopyArea (display, pixmap, window, gc, 0, 0,
916 p->wd, p->h, p->x, p->y);
917 XFreePixmap (display, pixmap);
919 if (p->overlay_p)
921 gcv.clip_mask = (Pixmap) 0;
922 XChangeGC (display, gc, GCClipMask, &gcv);
923 XFreePixmap (display, clipmask);
927 XSetClipMask (display, gc, None);
932 /* This is called when starting Emacs and when restarting after
933 suspend. When starting Emacs, no X window is mapped. And nothing
934 must be done to Emacs's own window if it is suspended (though that
935 rarely happens). */
937 static void
938 XTset_terminal_modes (struct terminal *terminal)
942 /* This is called when exiting or suspending Emacs. Exiting will make
943 the X-windows go away, and suspending requires no action. */
945 static void
946 XTreset_terminal_modes (struct terminal *terminal)
951 /***********************************************************************
952 Glyph display
953 ***********************************************************************/
957 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
958 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
959 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
960 int));
961 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
962 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
963 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
964 static void x_draw_glyph_string P_ ((struct glyph_string *));
965 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
966 static void x_set_cursor_gc P_ ((struct glyph_string *));
967 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
968 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
969 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
970 unsigned long *, double, int));
971 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
972 double, int, unsigned long));
973 static void x_setup_relief_colors P_ ((struct glyph_string *));
974 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
975 static void x_draw_image_relief P_ ((struct glyph_string *));
976 static void x_draw_image_foreground P_ ((struct glyph_string *));
977 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
978 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
979 int, int, int));
980 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
981 int, int, int, int, int, int,
982 XRectangle *));
983 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
984 int, int, int, XRectangle *));
986 #if GLYPH_DEBUG
987 static void x_check_font P_ ((struct frame *, struct font *));
988 #endif
991 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
992 face. */
994 static void
995 x_set_cursor_gc (s)
996 struct glyph_string *s;
998 if (s->font == FRAME_FONT (s->f)
999 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1000 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1001 && !s->cmp)
1002 s->gc = s->f->output_data.x->cursor_gc;
1003 else
1005 /* Cursor on non-default face: must merge. */
1006 XGCValues xgcv;
1007 unsigned long mask;
1009 xgcv.background = s->f->output_data.x->cursor_pixel;
1010 xgcv.foreground = s->face->background;
1012 /* If the glyph would be invisible, try a different foreground. */
1013 if (xgcv.foreground == xgcv.background)
1014 xgcv.foreground = s->face->foreground;
1015 if (xgcv.foreground == xgcv.background)
1016 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1017 if (xgcv.foreground == xgcv.background)
1018 xgcv.foreground = s->face->foreground;
1020 /* Make sure the cursor is distinct from text in this face. */
1021 if (xgcv.background == s->face->background
1022 && xgcv.foreground == s->face->foreground)
1024 xgcv.background = s->face->foreground;
1025 xgcv.foreground = s->face->background;
1028 IF_DEBUG (x_check_font (s->f, s->font));
1029 xgcv.graphics_exposures = False;
1030 mask = GCForeground | GCBackground | GCGraphicsExposures;
1032 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1033 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1034 mask, &xgcv);
1035 else
1036 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1037 = XCreateGC (s->display, s->window, mask, &xgcv);
1039 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1044 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1046 static void
1047 x_set_mouse_face_gc (s)
1048 struct glyph_string *s;
1050 int face_id;
1051 struct face *face;
1053 /* What face has to be used last for the mouse face? */
1054 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1055 face = FACE_FROM_ID (s->f, face_id);
1056 if (face == NULL)
1057 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1059 if (s->first_glyph->type == CHAR_GLYPH)
1060 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1061 else
1062 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1063 s->face = FACE_FROM_ID (s->f, face_id);
1064 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1066 if (s->font == s->face->font)
1067 s->gc = s->face->gc;
1068 else
1070 /* Otherwise construct scratch_cursor_gc with values from FACE
1071 except for FONT. */
1072 XGCValues xgcv;
1073 unsigned long mask;
1075 xgcv.background = s->face->background;
1076 xgcv.foreground = s->face->foreground;
1077 xgcv.graphics_exposures = False;
1078 mask = GCForeground | GCBackground | GCGraphicsExposures;
1080 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1081 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1082 mask, &xgcv);
1083 else
1084 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1085 = XCreateGC (s->display, s->window, mask, &xgcv);
1087 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1090 xassert (s->gc != 0);
1094 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1095 Faces to use in the mode line have already been computed when the
1096 matrix was built, so there isn't much to do, here. */
1098 static INLINE void
1099 x_set_mode_line_face_gc (s)
1100 struct glyph_string *s;
1102 s->gc = s->face->gc;
1106 /* Set S->gc of glyph string S for drawing that glyph string. Set
1107 S->stippled_p to a non-zero value if the face of S has a stipple
1108 pattern. */
1110 static INLINE void
1111 x_set_glyph_string_gc (s)
1112 struct glyph_string *s;
1114 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1116 if (s->hl == DRAW_NORMAL_TEXT)
1118 s->gc = s->face->gc;
1119 s->stippled_p = s->face->stipple != 0;
1121 else if (s->hl == DRAW_INVERSE_VIDEO)
1123 x_set_mode_line_face_gc (s);
1124 s->stippled_p = s->face->stipple != 0;
1126 else if (s->hl == DRAW_CURSOR)
1128 x_set_cursor_gc (s);
1129 s->stippled_p = 0;
1131 else if (s->hl == DRAW_MOUSE_FACE)
1133 x_set_mouse_face_gc (s);
1134 s->stippled_p = s->face->stipple != 0;
1136 else if (s->hl == DRAW_IMAGE_RAISED
1137 || s->hl == DRAW_IMAGE_SUNKEN)
1139 s->gc = s->face->gc;
1140 s->stippled_p = s->face->stipple != 0;
1142 else
1144 s->gc = s->face->gc;
1145 s->stippled_p = s->face->stipple != 0;
1148 /* GC must have been set. */
1149 xassert (s->gc != 0);
1153 /* Set clipping for output of glyph string S. S may be part of a mode
1154 line or menu if we don't have X toolkit support. */
1156 static INLINE void
1157 x_set_glyph_string_clipping (s)
1158 struct glyph_string *s;
1160 XRectangle *r = s->clip;
1161 int n = get_glyph_string_clip_rects (s, r, 2);
1163 if (n > 0)
1164 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
1165 s->num_clips = n;
1169 /* Set SRC's clipping for output of glyph string DST. This is called
1170 when we are drawing DST's left_overhang or right_overhang only in
1171 the area of SRC. */
1173 static void
1174 x_set_glyph_string_clipping_exactly (src, dst)
1175 struct glyph_string *src, *dst;
1177 XRectangle r;
1179 r.x = src->x;
1180 r.width = src->width;
1181 r.y = src->y;
1182 r.height = src->height;
1183 dst->clip[0] = r;
1184 dst->num_clips = 1;
1185 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1189 /* RIF:
1190 Compute left and right overhang of glyph string S. */
1192 static void
1193 x_compute_glyph_string_overhangs (s)
1194 struct glyph_string *s;
1196 if (s->cmp == NULL
1197 && s->first_glyph->type == CHAR_GLYPH)
1199 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1200 struct font *font = s->font;
1201 struct font_metrics metrics;
1202 int i;
1204 for (i = 0; i < s->nchars; i++)
1205 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1206 font->driver->text_extents (font, code, s->nchars, &metrics);
1207 s->right_overhang = (metrics.rbearing > metrics.width
1208 ? metrics.rbearing - metrics.width : 0);
1209 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1211 else if (s->cmp)
1213 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1214 s->left_overhang = - s->cmp->lbearing;
1219 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1221 static INLINE void
1222 x_clear_glyph_string_rect (s, x, y, w, h)
1223 struct glyph_string *s;
1224 int x, y, w, h;
1226 XGCValues xgcv;
1227 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1228 XSetForeground (s->display, s->gc, xgcv.background);
1229 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1230 XSetForeground (s->display, s->gc, xgcv.foreground);
1234 /* Draw the background of glyph_string S. If S->background_filled_p
1235 is non-zero don't draw it. FORCE_P non-zero means draw the
1236 background even if it wouldn't be drawn normally. This is used
1237 when a string preceding S draws into the background of S, or S
1238 contains the first component of a composition. */
1240 static void
1241 x_draw_glyph_string_background (s, force_p)
1242 struct glyph_string *s;
1243 int force_p;
1245 /* Nothing to do if background has already been drawn or if it
1246 shouldn't be drawn in the first place. */
1247 if (!s->background_filled_p)
1249 int box_line_width = max (s->face->box_line_width, 0);
1251 if (s->stippled_p)
1253 /* Fill background with a stipple pattern. */
1254 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1255 XFillRectangle (s->display, s->window, s->gc, s->x,
1256 s->y + box_line_width,
1257 s->background_width,
1258 s->height - 2 * box_line_width);
1259 XSetFillStyle (s->display, s->gc, FillSolid);
1260 s->background_filled_p = 1;
1262 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1263 || s->font_not_found_p
1264 || s->extends_to_end_of_line_p
1265 || force_p)
1267 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1268 s->background_width,
1269 s->height - 2 * box_line_width);
1270 s->background_filled_p = 1;
1276 /* Draw the foreground of glyph string S. */
1278 static void
1279 x_draw_glyph_string_foreground (s)
1280 struct glyph_string *s;
1282 int i, x;
1284 /* If first glyph of S has a left box line, start drawing the text
1285 of S to the right of that box line. */
1286 if (s->face->box != FACE_NO_BOX
1287 && s->first_glyph->left_box_line_p)
1288 x = s->x + eabs (s->face->box_line_width);
1289 else
1290 x = s->x;
1292 /* Draw characters of S as rectangles if S's font could not be
1293 loaded. */
1294 if (s->font_not_found_p)
1296 for (i = 0; i < s->nchars; ++i)
1298 struct glyph *g = s->first_glyph + i;
1299 XDrawRectangle (s->display, s->window,
1300 s->gc, x, s->y, g->pixel_width - 1,
1301 s->height - 1);
1302 x += g->pixel_width;
1305 else
1307 struct font *font = s->font;
1308 int boff = font->baseline_offset;
1309 int y;
1311 if (font->vertical_centering)
1312 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1314 y = s->ybase - boff;
1315 if (s->for_overlaps
1316 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1317 font->driver->draw (s, 0, s->nchars, x, y, 0);
1318 else
1319 font->driver->draw (s, 0, s->nchars, x, y, 1);
1320 if (s->face->overstrike)
1321 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1325 /* Draw the foreground of composite glyph string S. */
1327 static void
1328 x_draw_composite_glyph_string_foreground (s)
1329 struct glyph_string *s;
1331 int i, j, x;
1332 struct font *font = s->font;
1334 /* If first glyph of S has a left box line, start drawing the text
1335 of S to the right of that box line. */
1336 if (s->face && s->face->box != FACE_NO_BOX
1337 && s->first_glyph->left_box_line_p)
1338 x = s->x + eabs (s->face->box_line_width);
1339 else
1340 x = s->x;
1342 /* S is a glyph string for a composition. S->cmp_from is the index
1343 of the first character drawn for glyphs of this composition.
1344 S->cmp_from == 0 means we are drawing the very first character of
1345 this composition. */
1347 /* Draw a rectangle for the composition if the font for the very
1348 first character of the composition could not be loaded. */
1349 if (s->font_not_found_p)
1351 if (s->cmp_from == 0)
1352 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1353 s->width - 1, s->height - 1);
1355 else if (! s->first_glyph->u.cmp.automatic)
1357 int y = s->ybase;
1359 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1360 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1362 int xx = x + s->cmp->offsets[j * 2];
1363 int yy = y - s->cmp->offsets[j * 2 + 1];
1365 font->driver->draw (s, j, j + 1, xx, yy, 0);
1366 if (s->face->overstrike)
1367 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1370 else
1372 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1373 Lisp_Object glyph;
1374 int y = s->ybase;
1375 int width = 0;
1377 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1379 glyph = LGSTRING_GLYPH (gstring, i);
1380 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1381 width += LGLYPH_WIDTH (glyph);
1382 else
1384 int xoff, yoff, wadjust;
1386 if (j < i)
1388 font->driver->draw (s, j, i, x, y, 0);
1389 x += width;
1391 xoff = LGLYPH_XOFF (glyph);
1392 yoff = LGLYPH_YOFF (glyph);
1393 wadjust = LGLYPH_WADJUST (glyph);
1394 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1395 x += wadjust;
1396 j = i + 1;
1397 width = 0;
1400 if (j < i)
1401 font->driver->draw (s, j, i, x, y, 0);
1406 #ifdef USE_X_TOOLKIT
1408 static struct frame *x_frame_of_widget P_ ((Widget));
1409 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1410 XrmValue *, XrmValue *, XtPointer *));
1411 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1412 XrmValue *, Cardinal *));
1415 /* Return the frame on which widget WIDGET is used.. Abort if frame
1416 cannot be determined. */
1418 static struct frame *
1419 x_frame_of_widget (widget)
1420 Widget widget;
1422 struct x_display_info *dpyinfo;
1423 Lisp_Object tail;
1424 struct frame *f;
1426 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1428 /* Find the top-level shell of the widget. Note that this function
1429 can be called when the widget is not yet realized, so XtWindow
1430 (widget) == 0. That's the reason we can't simply use
1431 x_any_window_to_frame. */
1432 while (!XtIsTopLevelShell (widget))
1433 widget = XtParent (widget);
1435 /* Look for a frame with that top-level widget. Allocate the color
1436 on that frame to get the right gamma correction value. */
1437 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1438 if (FRAMEP (XCAR (tail))
1439 && (f = XFRAME (XCAR (tail)),
1440 (FRAME_X_P (f)
1441 && f->output_data.nothing != 1
1442 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1443 && f->output_data.x->widget == widget)
1444 return f;
1446 abort ();
1450 /* Allocate the color COLOR->pixel on the screen and display of
1451 widget WIDGET in colormap CMAP. If an exact match cannot be
1452 allocated, try the nearest color available. Value is non-zero
1453 if successful. This is called from lwlib. */
1456 x_alloc_nearest_color_for_widget (widget, cmap, color)
1457 Widget widget;
1458 Colormap cmap;
1459 XColor *color;
1461 struct frame *f = x_frame_of_widget (widget);
1462 return x_alloc_nearest_color (f, cmap, color);
1466 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1467 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1468 If this produces the same color as PIXEL, try a color where all RGB
1469 values have DELTA added. Return the allocated color in *PIXEL.
1470 DISPLAY is the X display, CMAP is the colormap to operate on.
1471 Value is non-zero if successful. */
1474 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1475 Widget widget;
1476 Display *display;
1477 Colormap cmap;
1478 unsigned long *pixel;
1479 double factor;
1480 int delta;
1482 struct frame *f = x_frame_of_widget (widget);
1483 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1487 /* Structure specifying which arguments should be passed by Xt to
1488 cvt_string_to_pixel. We want the widget's screen and colormap. */
1490 static XtConvertArgRec cvt_string_to_pixel_args[] =
1492 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1493 sizeof (Screen *)},
1494 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1495 sizeof (Colormap)}
1499 /* The address of this variable is returned by
1500 cvt_string_to_pixel. */
1502 static Pixel cvt_string_to_pixel_value;
1505 /* Convert a color name to a pixel color.
1507 DPY is the display we are working on.
1509 ARGS is an array of *NARGS XrmValue structures holding additional
1510 information about the widget for which the conversion takes place.
1511 The contents of this array are determined by the specification
1512 in cvt_string_to_pixel_args.
1514 FROM is a pointer to an XrmValue which points to the color name to
1515 convert. TO is an XrmValue in which to return the pixel color.
1517 CLOSURE_RET is a pointer to user-data, in which we record if
1518 we allocated the color or not.
1520 Value is True if successful, False otherwise. */
1522 static Boolean
1523 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1524 Display *dpy;
1525 XrmValue *args;
1526 Cardinal *nargs;
1527 XrmValue *from, *to;
1528 XtPointer *closure_ret;
1530 Screen *screen;
1531 Colormap cmap;
1532 Pixel pixel;
1533 String color_name;
1534 XColor color;
1536 if (*nargs != 2)
1538 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1539 "wrongParameters", "cvt_string_to_pixel",
1540 "XtToolkitError",
1541 "Screen and colormap args required", NULL, NULL);
1542 return False;
1545 screen = *(Screen **) args[0].addr;
1546 cmap = *(Colormap *) args[1].addr;
1547 color_name = (String) from->addr;
1549 if (strcmp (color_name, XtDefaultBackground) == 0)
1551 *closure_ret = (XtPointer) False;
1552 pixel = WhitePixelOfScreen (screen);
1554 else if (strcmp (color_name, XtDefaultForeground) == 0)
1556 *closure_ret = (XtPointer) False;
1557 pixel = BlackPixelOfScreen (screen);
1559 else if (XParseColor (dpy, cmap, color_name, &color)
1560 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1562 pixel = color.pixel;
1563 *closure_ret = (XtPointer) True;
1565 else
1567 String params[1];
1568 Cardinal nparams = 1;
1570 params[0] = color_name;
1571 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1572 "badValue", "cvt_string_to_pixel",
1573 "XtToolkitError", "Invalid color `%s'",
1574 params, &nparams);
1575 return False;
1578 if (to->addr != NULL)
1580 if (to->size < sizeof (Pixel))
1582 to->size = sizeof (Pixel);
1583 return False;
1586 *(Pixel *) to->addr = pixel;
1588 else
1590 cvt_string_to_pixel_value = pixel;
1591 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1594 to->size = sizeof (Pixel);
1595 return True;
1599 /* Free a pixel color which was previously allocated via
1600 cvt_string_to_pixel. This is registered as the destructor
1601 for this type of resource via XtSetTypeConverter.
1603 APP is the application context in which we work.
1605 TO is a pointer to an XrmValue holding the color to free.
1606 CLOSURE is the value we stored in CLOSURE_RET for this color
1607 in cvt_string_to_pixel.
1609 ARGS and NARGS are like for cvt_string_to_pixel. */
1611 static void
1612 cvt_pixel_dtor (app, to, closure, args, nargs)
1613 XtAppContext app;
1614 XrmValuePtr to;
1615 XtPointer closure;
1616 XrmValuePtr args;
1617 Cardinal *nargs;
1619 if (*nargs != 2)
1621 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1622 "XtToolkitError",
1623 "Screen and colormap arguments required",
1624 NULL, NULL);
1626 else if (closure != NULL)
1628 /* We did allocate the pixel, so free it. */
1629 Screen *screen = *(Screen **) args[0].addr;
1630 Colormap cmap = *(Colormap *) args[1].addr;
1631 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1632 (Pixel *) to->addr, 1);
1637 #endif /* USE_X_TOOLKIT */
1640 /* Value is an array of XColor structures for the contents of the
1641 color map of display DPY. Set *NCELLS to the size of the array.
1642 Note that this probably shouldn't be called for large color maps,
1643 say a 24-bit TrueColor map. */
1645 static const XColor *
1646 x_color_cells (dpy, ncells)
1647 Display *dpy;
1648 int *ncells;
1650 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1652 if (dpyinfo->color_cells == NULL)
1654 Screen *screen = dpyinfo->screen;
1655 int i;
1657 dpyinfo->ncolor_cells
1658 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1659 dpyinfo->color_cells
1660 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1661 * sizeof *dpyinfo->color_cells);
1663 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1664 dpyinfo->color_cells[i].pixel = i;
1666 XQueryColors (dpy, dpyinfo->cmap,
1667 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1670 *ncells = dpyinfo->ncolor_cells;
1671 return dpyinfo->color_cells;
1675 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1676 colors in COLORS. Use cached information, if available. */
1678 void
1679 x_query_colors (f, colors, ncolors)
1680 struct frame *f;
1681 XColor *colors;
1682 int ncolors;
1684 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1686 if (dpyinfo->color_cells)
1688 int i;
1689 for (i = 0; i < ncolors; ++i)
1691 unsigned long pixel = colors[i].pixel;
1692 xassert (pixel < dpyinfo->ncolor_cells);
1693 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1694 colors[i] = dpyinfo->color_cells[pixel];
1697 else
1698 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1702 /* On frame F, translate pixel color to RGB values for the color in
1703 COLOR. Use cached information, if available. */
1705 void
1706 x_query_color (f, color)
1707 struct frame *f;
1708 XColor *color;
1710 x_query_colors (f, color, 1);
1714 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1715 exact match can't be allocated, try the nearest color available.
1716 Value is non-zero if successful. Set *COLOR to the color
1717 allocated. */
1719 static int
1720 x_alloc_nearest_color_1 (dpy, cmap, color)
1721 Display *dpy;
1722 Colormap cmap;
1723 XColor *color;
1725 int rc;
1727 rc = XAllocColor (dpy, cmap, color);
1728 if (rc == 0)
1730 /* If we got to this point, the colormap is full, so we're going
1731 to try to get the next closest color. The algorithm used is
1732 a least-squares matching, which is what X uses for closest
1733 color matching with StaticColor visuals. */
1734 int nearest, i;
1735 unsigned long nearest_delta = ~0;
1736 int ncells;
1737 const XColor *cells = x_color_cells (dpy, &ncells);
1739 for (nearest = i = 0; i < ncells; ++i)
1741 long dred = (color->red >> 8) - (cells[i].red >> 8);
1742 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1743 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1744 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1746 if (delta < nearest_delta)
1748 nearest = i;
1749 nearest_delta = delta;
1753 color->red = cells[nearest].red;
1754 color->green = cells[nearest].green;
1755 color->blue = cells[nearest].blue;
1756 rc = XAllocColor (dpy, cmap, color);
1758 else
1760 /* If allocation succeeded, and the allocated pixel color is not
1761 equal to a cached pixel color recorded earlier, there was a
1762 change in the colormap, so clear the color cache. */
1763 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1764 XColor *cached_color;
1766 if (dpyinfo->color_cells
1767 && (cached_color = &dpyinfo->color_cells[color->pixel],
1768 (cached_color->red != color->red
1769 || cached_color->blue != color->blue
1770 || cached_color->green != color->green)))
1772 xfree (dpyinfo->color_cells);
1773 dpyinfo->color_cells = NULL;
1774 dpyinfo->ncolor_cells = 0;
1778 #ifdef DEBUG_X_COLORS
1779 if (rc)
1780 register_color (color->pixel);
1781 #endif /* DEBUG_X_COLORS */
1783 return rc;
1787 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1788 exact match can't be allocated, try the nearest color available.
1789 Value is non-zero if successful. Set *COLOR to the color
1790 allocated. */
1793 x_alloc_nearest_color (f, cmap, color)
1794 struct frame *f;
1795 Colormap cmap;
1796 XColor *color;
1798 gamma_correct (f, color);
1799 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1803 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1804 It's necessary to do this instead of just using PIXEL directly to
1805 get color reference counts right. */
1807 unsigned long
1808 x_copy_color (f, pixel)
1809 struct frame *f;
1810 unsigned long pixel;
1812 XColor color;
1814 color.pixel = pixel;
1815 BLOCK_INPUT;
1816 x_query_color (f, &color);
1817 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1818 UNBLOCK_INPUT;
1819 #ifdef DEBUG_X_COLORS
1820 register_color (pixel);
1821 #endif
1822 return color.pixel;
1826 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1827 It's necessary to do this instead of just using PIXEL directly to
1828 get color reference counts right. */
1830 unsigned long
1831 x_copy_dpy_color (dpy, cmap, pixel)
1832 Display *dpy;
1833 Colormap cmap;
1834 unsigned long pixel;
1836 XColor color;
1838 color.pixel = pixel;
1839 BLOCK_INPUT;
1840 XQueryColor (dpy, cmap, &color);
1841 XAllocColor (dpy, cmap, &color);
1842 UNBLOCK_INPUT;
1843 #ifdef DEBUG_X_COLORS
1844 register_color (pixel);
1845 #endif
1846 return color.pixel;
1850 /* Brightness beyond which a color won't have its highlight brightness
1851 boosted.
1853 Nominally, highlight colors for `3d' faces are calculated by
1854 brightening an object's color by a constant scale factor, but this
1855 doesn't yield good results for dark colors, so for colors who's
1856 brightness is less than this value (on a scale of 0-65535) have an
1857 use an additional additive factor.
1859 The value here is set so that the default menu-bar/mode-line color
1860 (grey75) will not have its highlights changed at all. */
1861 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1864 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1865 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1866 If this produces the same color as PIXEL, try a color where all RGB
1867 values have DELTA added. Return the allocated color in *PIXEL.
1868 DISPLAY is the X display, CMAP is the colormap to operate on.
1869 Value is non-zero if successful. */
1871 static int
1872 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1873 struct frame *f;
1874 Display *display;
1875 Colormap cmap;
1876 unsigned long *pixel;
1877 double factor;
1878 int delta;
1880 XColor color, new;
1881 long bright;
1882 int success_p;
1884 /* Get RGB color values. */
1885 color.pixel = *pixel;
1886 x_query_color (f, &color);
1888 /* Change RGB values by specified FACTOR. Avoid overflow! */
1889 xassert (factor >= 0);
1890 new.red = min (0xffff, factor * color.red);
1891 new.green = min (0xffff, factor * color.green);
1892 new.blue = min (0xffff, factor * color.blue);
1894 /* Calculate brightness of COLOR. */
1895 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1897 /* We only boost colors that are darker than
1898 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1899 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1900 /* Make an additive adjustment to NEW, because it's dark enough so
1901 that scaling by FACTOR alone isn't enough. */
1903 /* How far below the limit this color is (0 - 1, 1 being darker). */
1904 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1905 /* The additive adjustment. */
1906 int min_delta = delta * dimness * factor / 2;
1908 if (factor < 1)
1910 new.red = max (0, new.red - min_delta);
1911 new.green = max (0, new.green - min_delta);
1912 new.blue = max (0, new.blue - min_delta);
1914 else
1916 new.red = min (0xffff, min_delta + new.red);
1917 new.green = min (0xffff, min_delta + new.green);
1918 new.blue = min (0xffff, min_delta + new.blue);
1922 /* Try to allocate the color. */
1923 success_p = x_alloc_nearest_color (f, cmap, &new);
1924 if (success_p)
1926 if (new.pixel == *pixel)
1928 /* If we end up with the same color as before, try adding
1929 delta to the RGB values. */
1930 x_free_colors (f, &new.pixel, 1);
1932 new.red = min (0xffff, delta + color.red);
1933 new.green = min (0xffff, delta + color.green);
1934 new.blue = min (0xffff, delta + color.blue);
1935 success_p = x_alloc_nearest_color (f, cmap, &new);
1937 else
1938 success_p = 1;
1939 *pixel = new.pixel;
1942 return success_p;
1946 /* Set up the foreground color for drawing relief lines of glyph
1947 string S. RELIEF is a pointer to a struct relief containing the GC
1948 with which lines will be drawn. Use a color that is FACTOR or
1949 DELTA lighter or darker than the relief's background which is found
1950 in S->f->output_data.x->relief_background. If such a color cannot
1951 be allocated, use DEFAULT_PIXEL, instead. */
1953 static void
1954 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1955 struct frame *f;
1956 struct relief *relief;
1957 double factor;
1958 int delta;
1959 unsigned long default_pixel;
1961 XGCValues xgcv;
1962 struct x_output *di = f->output_data.x;
1963 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1964 unsigned long pixel;
1965 unsigned long background = di->relief_background;
1966 Colormap cmap = FRAME_X_COLORMAP (f);
1967 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1968 Display *dpy = FRAME_X_DISPLAY (f);
1970 xgcv.graphics_exposures = False;
1971 xgcv.line_width = 1;
1973 /* Free previously allocated color. The color cell will be reused
1974 when it has been freed as many times as it was allocated, so this
1975 doesn't affect faces using the same colors. */
1976 if (relief->gc
1977 && relief->allocated_p)
1979 x_free_colors (f, &relief->pixel, 1);
1980 relief->allocated_p = 0;
1983 /* Allocate new color. */
1984 xgcv.foreground = default_pixel;
1985 pixel = background;
1986 if (dpyinfo->n_planes != 1
1987 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1989 relief->allocated_p = 1;
1990 xgcv.foreground = relief->pixel = pixel;
1993 if (relief->gc == 0)
1995 xgcv.stipple = dpyinfo->gray;
1996 mask |= GCStipple;
1997 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1999 else
2000 XChangeGC (dpy, relief->gc, mask, &xgcv);
2004 /* Set up colors for the relief lines around glyph string S. */
2006 static void
2007 x_setup_relief_colors (s)
2008 struct glyph_string *s;
2010 struct x_output *di = s->f->output_data.x;
2011 unsigned long color;
2013 if (s->face->use_box_color_for_shadows_p)
2014 color = s->face->box_color;
2015 else if (s->first_glyph->type == IMAGE_GLYPH
2016 && s->img->pixmap
2017 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2018 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2019 else
2021 XGCValues xgcv;
2023 /* Get the background color of the face. */
2024 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2025 color = xgcv.background;
2028 if (di->white_relief.gc == 0
2029 || color != di->relief_background)
2031 di->relief_background = color;
2032 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2033 WHITE_PIX_DEFAULT (s->f));
2034 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2035 BLACK_PIX_DEFAULT (s->f));
2040 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2041 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2042 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2043 relief. LEFT_P non-zero means draw a relief on the left side of
2044 the rectangle. RIGHT_P non-zero means draw a relief on the right
2045 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2046 when drawing. */
2048 static void
2049 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2050 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2051 struct frame *f;
2052 int left_x, top_y, right_x, bottom_y, width;
2053 int top_p, bot_p, left_p, right_p, raised_p;
2054 XRectangle *clip_rect;
2056 Display *dpy = FRAME_X_DISPLAY (f);
2057 Window window = FRAME_X_WINDOW (f);
2058 int i;
2059 GC gc;
2061 if (raised_p)
2062 gc = f->output_data.x->white_relief.gc;
2063 else
2064 gc = f->output_data.x->black_relief.gc;
2065 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2067 /* Top. */
2068 if (top_p)
2069 for (i = 0; i < width; ++i)
2070 XDrawLine (dpy, window, gc,
2071 left_x + i * left_p, top_y + i,
2072 right_x + 1 - i * right_p, top_y + i);
2074 /* Left. */
2075 if (left_p)
2076 for (i = 0; i < width; ++i)
2077 XDrawLine (dpy, window, gc,
2078 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2080 XSetClipMask (dpy, gc, None);
2081 if (raised_p)
2082 gc = f->output_data.x->black_relief.gc;
2083 else
2084 gc = f->output_data.x->white_relief.gc;
2085 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2087 /* Bottom. */
2088 if (bot_p)
2089 for (i = 0; i < width; ++i)
2090 XDrawLine (dpy, window, gc,
2091 left_x + i * left_p, bottom_y - i,
2092 right_x + 1 - i * right_p, bottom_y - i);
2094 /* Right. */
2095 if (right_p)
2096 for (i = 0; i < width; ++i)
2097 XDrawLine (dpy, window, gc,
2098 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2100 XSetClipMask (dpy, gc, None);
2104 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2105 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2106 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2107 left side of the rectangle. RIGHT_P non-zero means draw a line
2108 on the right side of the rectangle. CLIP_RECT is the clipping
2109 rectangle to use when drawing. */
2111 static void
2112 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2113 left_p, right_p, clip_rect)
2114 struct glyph_string *s;
2115 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2116 XRectangle *clip_rect;
2118 XGCValues xgcv;
2120 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2121 XSetForeground (s->display, s->gc, s->face->box_color);
2122 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2124 /* Top. */
2125 XFillRectangle (s->display, s->window, s->gc,
2126 left_x, top_y, right_x - left_x + 1, width);
2128 /* Left. */
2129 if (left_p)
2130 XFillRectangle (s->display, s->window, s->gc,
2131 left_x, top_y, width, bottom_y - top_y + 1);
2133 /* Bottom. */
2134 XFillRectangle (s->display, s->window, s->gc,
2135 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2137 /* Right. */
2138 if (right_p)
2139 XFillRectangle (s->display, s->window, s->gc,
2140 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2142 XSetForeground (s->display, s->gc, xgcv.foreground);
2143 XSetClipMask (s->display, s->gc, None);
2147 /* Draw a box around glyph string S. */
2149 static void
2150 x_draw_glyph_string_box (s)
2151 struct glyph_string *s;
2153 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2154 int left_p, right_p;
2155 struct glyph *last_glyph;
2156 XRectangle clip_rect;
2158 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2159 ? WINDOW_RIGHT_EDGE_X (s->w)
2160 : window_box_right (s->w, s->area));
2162 /* The glyph that may have a right box line. */
2163 last_glyph = (s->cmp || s->img
2164 ? s->first_glyph
2165 : s->first_glyph + s->nchars - 1);
2167 width = eabs (s->face->box_line_width);
2168 raised_p = s->face->box == FACE_RAISED_BOX;
2169 left_x = s->x;
2170 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2171 ? last_x - 1
2172 : min (last_x, s->x + s->background_width) - 1);
2173 top_y = s->y;
2174 bottom_y = top_y + s->height - 1;
2176 left_p = (s->first_glyph->left_box_line_p
2177 || (s->hl == DRAW_MOUSE_FACE
2178 && (s->prev == NULL
2179 || s->prev->hl != s->hl)));
2180 right_p = (last_glyph->right_box_line_p
2181 || (s->hl == DRAW_MOUSE_FACE
2182 && (s->next == NULL
2183 || s->next->hl != s->hl)));
2185 get_glyph_string_clip_rect (s, &clip_rect);
2187 if (s->face->box == FACE_SIMPLE_BOX)
2188 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2189 left_p, right_p, &clip_rect);
2190 else
2192 x_setup_relief_colors (s);
2193 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2194 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2199 /* Draw foreground of image glyph string S. */
2201 static void
2202 x_draw_image_foreground (s)
2203 struct glyph_string *s;
2205 int x = s->x;
2206 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2208 /* If first glyph of S has a left box line, start drawing it to the
2209 right of that line. */
2210 if (s->face->box != FACE_NO_BOX
2211 && s->first_glyph->left_box_line_p
2212 && s->slice.x == 0)
2213 x += eabs (s->face->box_line_width);
2215 /* If there is a margin around the image, adjust x- and y-position
2216 by that margin. */
2217 if (s->slice.x == 0)
2218 x += s->img->hmargin;
2219 if (s->slice.y == 0)
2220 y += s->img->vmargin;
2222 if (s->img->pixmap)
2224 if (s->img->mask)
2226 /* We can't set both a clip mask and use XSetClipRectangles
2227 because the latter also sets a clip mask. We also can't
2228 trust on the shape extension to be available
2229 (XShapeCombineRegion). So, compute the rectangle to draw
2230 manually. */
2231 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2232 | GCFunction);
2233 XGCValues xgcv;
2234 XRectangle clip_rect, image_rect, r;
2236 xgcv.clip_mask = s->img->mask;
2237 xgcv.clip_x_origin = x;
2238 xgcv.clip_y_origin = y;
2239 xgcv.function = GXcopy;
2240 XChangeGC (s->display, s->gc, mask, &xgcv);
2242 get_glyph_string_clip_rect (s, &clip_rect);
2243 image_rect.x = x;
2244 image_rect.y = y;
2245 image_rect.width = s->slice.width;
2246 image_rect.height = s->slice.height;
2247 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2248 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2249 s->slice.x + r.x - x, s->slice.y + r.y - y,
2250 r.width, r.height, r.x, r.y);
2252 else
2254 XRectangle clip_rect, image_rect, r;
2256 get_glyph_string_clip_rect (s, &clip_rect);
2257 image_rect.x = x;
2258 image_rect.y = y;
2259 image_rect.width = s->slice.width;
2260 image_rect.height = s->slice.height;
2261 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2262 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2263 s->slice.x + r.x - x, s->slice.y + r.y - y,
2264 r.width, r.height, r.x, r.y);
2266 /* When the image has a mask, we can expect that at
2267 least part of a mouse highlight or a block cursor will
2268 be visible. If the image doesn't have a mask, make
2269 a block cursor visible by drawing a rectangle around
2270 the image. I believe it's looking better if we do
2271 nothing here for mouse-face. */
2272 if (s->hl == DRAW_CURSOR)
2274 int r = s->img->relief;
2275 if (r < 0) r = -r;
2276 XDrawRectangle (s->display, s->window, s->gc,
2277 x - r, y - r,
2278 s->slice.width + r*2 - 1,
2279 s->slice.height + r*2 - 1);
2283 else
2284 /* Draw a rectangle if image could not be loaded. */
2285 XDrawRectangle (s->display, s->window, s->gc, x, y,
2286 s->slice.width - 1, s->slice.height - 1);
2290 /* Draw a relief around the image glyph string S. */
2292 static void
2293 x_draw_image_relief (s)
2294 struct glyph_string *s;
2296 int x0, y0, x1, y1, thick, raised_p;
2297 XRectangle r;
2298 int x = s->x;
2299 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2301 /* If first glyph of S has a left box line, start drawing it to the
2302 right of that line. */
2303 if (s->face->box != FACE_NO_BOX
2304 && s->first_glyph->left_box_line_p
2305 && s->slice.x == 0)
2306 x += eabs (s->face->box_line_width);
2308 /* If there is a margin around the image, adjust x- and y-position
2309 by that margin. */
2310 if (s->slice.x == 0)
2311 x += s->img->hmargin;
2312 if (s->slice.y == 0)
2313 y += s->img->vmargin;
2315 if (s->hl == DRAW_IMAGE_SUNKEN
2316 || s->hl == DRAW_IMAGE_RAISED)
2318 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2319 raised_p = s->hl == DRAW_IMAGE_RAISED;
2321 else
2323 thick = eabs (s->img->relief);
2324 raised_p = s->img->relief > 0;
2327 x0 = x - thick;
2328 y0 = y - thick;
2329 x1 = x + s->slice.width + thick - 1;
2330 y1 = y + s->slice.height + thick - 1;
2332 x_setup_relief_colors (s);
2333 get_glyph_string_clip_rect (s, &r);
2334 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2335 s->slice.y == 0,
2336 s->slice.y + s->slice.height == s->img->height,
2337 s->slice.x == 0,
2338 s->slice.x + s->slice.width == s->img->width,
2339 &r);
2343 /* Draw the foreground of image glyph string S to PIXMAP. */
2345 static void
2346 x_draw_image_foreground_1 (s, pixmap)
2347 struct glyph_string *s;
2348 Pixmap pixmap;
2350 int x = 0;
2351 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2353 /* If first glyph of S has a left box line, start drawing it to the
2354 right of that line. */
2355 if (s->face->box != FACE_NO_BOX
2356 && s->first_glyph->left_box_line_p
2357 && s->slice.x == 0)
2358 x += eabs (s->face->box_line_width);
2360 /* If there is a margin around the image, adjust x- and y-position
2361 by that margin. */
2362 if (s->slice.x == 0)
2363 x += s->img->hmargin;
2364 if (s->slice.y == 0)
2365 y += s->img->vmargin;
2367 if (s->img->pixmap)
2369 if (s->img->mask)
2371 /* We can't set both a clip mask and use XSetClipRectangles
2372 because the latter also sets a clip mask. We also can't
2373 trust on the shape extension to be available
2374 (XShapeCombineRegion). So, compute the rectangle to draw
2375 manually. */
2376 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2377 | GCFunction);
2378 XGCValues xgcv;
2380 xgcv.clip_mask = s->img->mask;
2381 xgcv.clip_x_origin = x - s->slice.x;
2382 xgcv.clip_y_origin = y - s->slice.y;
2383 xgcv.function = GXcopy;
2384 XChangeGC (s->display, s->gc, mask, &xgcv);
2386 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2387 s->slice.x, s->slice.y,
2388 s->slice.width, s->slice.height, x, y);
2389 XSetClipMask (s->display, s->gc, None);
2391 else
2393 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2394 s->slice.x, s->slice.y,
2395 s->slice.width, s->slice.height, x, y);
2397 /* When the image has a mask, we can expect that at
2398 least part of a mouse highlight or a block cursor will
2399 be visible. If the image doesn't have a mask, make
2400 a block cursor visible by drawing a rectangle around
2401 the image. I believe it's looking better if we do
2402 nothing here for mouse-face. */
2403 if (s->hl == DRAW_CURSOR)
2405 int r = s->img->relief;
2406 if (r < 0) r = -r;
2407 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2408 s->slice.width + r*2 - 1,
2409 s->slice.height + r*2 - 1);
2413 else
2414 /* Draw a rectangle if image could not be loaded. */
2415 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2416 s->slice.width - 1, s->slice.height - 1);
2420 /* Draw part of the background of glyph string S. X, Y, W, and H
2421 give the rectangle to draw. */
2423 static void
2424 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2425 struct glyph_string *s;
2426 int x, y, w, h;
2428 if (s->stippled_p)
2430 /* Fill background with a stipple pattern. */
2431 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2432 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2433 XSetFillStyle (s->display, s->gc, FillSolid);
2435 else
2436 x_clear_glyph_string_rect (s, x, y, w, h);
2440 /* Draw image glyph string S.
2442 s->y
2443 s->x +-------------------------
2444 | s->face->box
2446 | +-------------------------
2447 | | s->img->margin
2449 | | +-------------------
2450 | | | the image
2454 static void
2455 x_draw_image_glyph_string (s)
2456 struct glyph_string *s;
2458 int box_line_hwidth = eabs (s->face->box_line_width);
2459 int box_line_vwidth = max (s->face->box_line_width, 0);
2460 int height;
2461 Pixmap pixmap = None;
2463 height = s->height;
2464 if (s->slice.y == 0)
2465 height -= box_line_vwidth;
2466 if (s->slice.y + s->slice.height >= s->img->height)
2467 height -= box_line_vwidth;
2469 /* Fill background with face under the image. Do it only if row is
2470 taller than image or if image has a clip mask to reduce
2471 flickering. */
2472 s->stippled_p = s->face->stipple != 0;
2473 if (height > s->slice.height
2474 || s->img->hmargin
2475 || s->img->vmargin
2476 || s->img->mask
2477 || s->img->pixmap == 0
2478 || s->width != s->background_width)
2480 if (s->img->mask)
2482 /* Create a pixmap as large as the glyph string. Fill it
2483 with the background color. Copy the image to it, using
2484 its mask. Copy the temporary pixmap to the display. */
2485 Screen *screen = FRAME_X_SCREEN (s->f);
2486 int depth = DefaultDepthOfScreen (screen);
2488 /* Create a pixmap as large as the glyph string. */
2489 pixmap = XCreatePixmap (s->display, s->window,
2490 s->background_width,
2491 s->height, depth);
2493 /* Don't clip in the following because we're working on the
2494 pixmap. */
2495 XSetClipMask (s->display, s->gc, None);
2497 /* Fill the pixmap with the background color/stipple. */
2498 if (s->stippled_p)
2500 /* Fill background with a stipple pattern. */
2501 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2502 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2503 XFillRectangle (s->display, pixmap, s->gc,
2504 0, 0, s->background_width, s->height);
2505 XSetFillStyle (s->display, s->gc, FillSolid);
2506 XSetTSOrigin (s->display, s->gc, 0, 0);
2508 else
2510 XGCValues xgcv;
2511 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2512 &xgcv);
2513 XSetForeground (s->display, s->gc, xgcv.background);
2514 XFillRectangle (s->display, pixmap, s->gc,
2515 0, 0, s->background_width, s->height);
2516 XSetForeground (s->display, s->gc, xgcv.foreground);
2519 else
2521 int x = s->x;
2522 int y = s->y;
2524 if (s->first_glyph->left_box_line_p
2525 && s->slice.x == 0)
2526 x += box_line_hwidth;
2528 if (s->slice.y == 0)
2529 y += box_line_vwidth;
2531 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2534 s->background_filled_p = 1;
2537 /* Draw the foreground. */
2538 if (pixmap != None)
2540 x_draw_image_foreground_1 (s, pixmap);
2541 x_set_glyph_string_clipping (s);
2542 XCopyArea (s->display, pixmap, s->window, s->gc,
2543 0, 0, s->background_width, s->height, s->x, s->y);
2544 XFreePixmap (s->display, pixmap);
2546 else
2547 x_draw_image_foreground (s);
2549 /* If we must draw a relief around the image, do it. */
2550 if (s->img->relief
2551 || s->hl == DRAW_IMAGE_RAISED
2552 || s->hl == DRAW_IMAGE_SUNKEN)
2553 x_draw_image_relief (s);
2557 /* Draw stretch glyph string S. */
2559 static void
2560 x_draw_stretch_glyph_string (s)
2561 struct glyph_string *s;
2563 xassert (s->first_glyph->type == STRETCH_GLYPH);
2565 if (s->hl == DRAW_CURSOR
2566 && !x_stretch_cursor_p)
2568 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2569 as wide as the stretch glyph. */
2570 int width, background_width = s->background_width;
2571 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2573 if (x < left_x)
2575 background_width -= left_x - x;
2576 x = left_x;
2578 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2580 /* Draw cursor. */
2581 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2583 /* Clear rest using the GC of the original non-cursor face. */
2584 if (width < background_width)
2586 int y = s->y;
2587 int w = background_width - width, h = s->height;
2588 XRectangle r;
2589 GC gc;
2591 x += width;
2592 if (s->row->mouse_face_p
2593 && cursor_in_mouse_face_p (s->w))
2595 x_set_mouse_face_gc (s);
2596 gc = s->gc;
2598 else
2599 gc = s->face->gc;
2601 get_glyph_string_clip_rect (s, &r);
2602 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2604 if (s->face->stipple)
2606 /* Fill background with a stipple pattern. */
2607 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2608 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2609 XSetFillStyle (s->display, gc, FillSolid);
2611 else
2613 XGCValues xgcv;
2614 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2615 XSetForeground (s->display, gc, xgcv.background);
2616 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2617 XSetForeground (s->display, gc, xgcv.foreground);
2621 else if (!s->background_filled_p)
2623 int background_width = s->background_width;
2624 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2626 /* Don't draw into left margin, fringe or scrollbar area
2627 except for header line and mode line. */
2628 if (x < left_x && !s->row->mode_line_p)
2630 background_width -= left_x - x;
2631 x = left_x;
2633 if (background_width > 0)
2634 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2637 s->background_filled_p = 1;
2641 /* Draw glyph string S. */
2643 static void
2644 x_draw_glyph_string (s)
2645 struct glyph_string *s;
2647 int relief_drawn_p = 0;
2649 /* If S draws into the background of its successors, draw the
2650 background of the successors first so that S can draw into it.
2651 This makes S->next use XDrawString instead of XDrawImageString. */
2652 if (s->next && s->right_overhang && !s->for_overlaps)
2654 int width;
2655 struct glyph_string *next;
2657 for (width = 0, next = s->next;
2658 next && width < s->right_overhang;
2659 width += next->width, next = next->next)
2660 if (next->first_glyph->type != IMAGE_GLYPH)
2662 x_set_glyph_string_gc (next);
2663 x_set_glyph_string_clipping (next);
2664 if (next->first_glyph->type == STRETCH_GLYPH)
2665 x_draw_stretch_glyph_string (next);
2666 else
2667 x_draw_glyph_string_background (next, 1);
2668 next->num_clips = 0;
2672 /* Set up S->gc, set clipping and draw S. */
2673 x_set_glyph_string_gc (s);
2675 /* Draw relief (if any) in advance for char/composition so that the
2676 glyph string can be drawn over it. */
2677 if (!s->for_overlaps
2678 && s->face->box != FACE_NO_BOX
2679 && (s->first_glyph->type == CHAR_GLYPH
2680 || s->first_glyph->type == COMPOSITE_GLYPH))
2683 x_set_glyph_string_clipping (s);
2684 x_draw_glyph_string_background (s, 1);
2685 x_draw_glyph_string_box (s);
2686 x_set_glyph_string_clipping (s);
2687 relief_drawn_p = 1;
2689 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2690 && !s->clip_tail
2691 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2692 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2693 /* We must clip just this glyph. left_overhang part has already
2694 drawn when s->prev was drawn, and right_overhang part will be
2695 drawn later when s->next is drawn. */
2696 x_set_glyph_string_clipping_exactly (s, s);
2697 else
2698 x_set_glyph_string_clipping (s);
2700 switch (s->first_glyph->type)
2702 case IMAGE_GLYPH:
2703 x_draw_image_glyph_string (s);
2704 break;
2706 case STRETCH_GLYPH:
2707 x_draw_stretch_glyph_string (s);
2708 break;
2710 case CHAR_GLYPH:
2711 if (s->for_overlaps)
2712 s->background_filled_p = 1;
2713 else
2714 x_draw_glyph_string_background (s, 0);
2715 x_draw_glyph_string_foreground (s);
2716 break;
2718 case COMPOSITE_GLYPH:
2719 if (s->for_overlaps || (s->cmp_from > 0
2720 && ! s->first_glyph->u.cmp.automatic))
2721 s->background_filled_p = 1;
2722 else
2723 x_draw_glyph_string_background (s, 1);
2724 x_draw_composite_glyph_string_foreground (s);
2725 break;
2727 default:
2728 abort ();
2731 if (!s->for_overlaps)
2733 /* Draw underline. */
2734 if (s->face->underline_p)
2736 unsigned long thickness, position;
2737 int y;
2739 if (s->prev && s->prev->face->underline_p)
2741 /* We use the same underline style as the previous one. */
2742 thickness = s->prev->underline_thickness;
2743 position = s->prev->underline_position;
2745 else
2747 /* Get the underline thickness. Default is 1 pixel. */
2748 if (s->font && s->font->underline_thickness > 0)
2749 thickness = s->font->underline_thickness;
2750 else
2751 thickness = 1;
2752 if (x_underline_at_descent_line)
2753 position = (s->height - thickness) - (s->ybase - s->y);
2754 else
2756 /* Get the underline position. This is the recommended
2757 vertical offset in pixels from the baseline to the top of
2758 the underline. This is a signed value according to the
2759 specs, and its default is
2761 ROUND ((maximum descent) / 2), with
2762 ROUND(x) = floor (x + 0.5) */
2764 if (x_use_underline_position_properties
2765 && s->font && s->font->underline_position >= 0)
2766 position = s->font->underline_position;
2767 else if (s->font)
2768 position = (s->font->descent + 1) / 2;
2769 else
2770 position = underline_minimum_offset;
2772 position = max (position, underline_minimum_offset);
2774 /* Check the sanity of thickness and position. We should
2775 avoid drawing underline out of the current line area. */
2776 if (s->y + s->height <= s->ybase + position)
2777 position = (s->height - 1) - (s->ybase - s->y);
2778 if (s->y + s->height < s->ybase + position + thickness)
2779 thickness = (s->y + s->height) - (s->ybase + position);
2780 s->underline_thickness = thickness;
2781 s->underline_position = position;
2782 y = s->ybase + position;
2783 if (s->face->underline_defaulted_p)
2784 XFillRectangle (s->display, s->window, s->gc,
2785 s->x, y, s->background_width, thickness);
2786 else
2788 XGCValues xgcv;
2789 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2790 XSetForeground (s->display, s->gc, s->face->underline_color);
2791 XFillRectangle (s->display, s->window, s->gc,
2792 s->x, y, s->background_width, thickness);
2793 XSetForeground (s->display, s->gc, xgcv.foreground);
2797 /* Draw overline. */
2798 if (s->face->overline_p)
2800 unsigned long dy = 0, h = 1;
2802 if (s->face->overline_color_defaulted_p)
2803 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2804 s->background_width, h);
2805 else
2807 XGCValues xgcv;
2808 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2809 XSetForeground (s->display, s->gc, s->face->overline_color);
2810 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2811 s->background_width, h);
2812 XSetForeground (s->display, s->gc, xgcv.foreground);
2816 /* Draw strike-through. */
2817 if (s->face->strike_through_p)
2819 unsigned long h = 1;
2820 unsigned long dy = (s->height - h) / 2;
2822 if (s->face->strike_through_color_defaulted_p)
2823 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2824 s->width, h);
2825 else
2827 XGCValues xgcv;
2828 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2829 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2830 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2831 s->width, h);
2832 XSetForeground (s->display, s->gc, xgcv.foreground);
2836 /* Draw relief if not yet drawn. */
2837 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2838 x_draw_glyph_string_box (s);
2840 if (s->prev)
2842 struct glyph_string *prev;
2844 for (prev = s->prev; prev; prev = prev->prev)
2845 if (prev->hl != s->hl
2846 && prev->x + prev->width + prev->right_overhang > s->x)
2848 /* As prev was drawn while clipped to its own area, we
2849 must draw the right_overhang part using s->hl now. */
2850 enum draw_glyphs_face save = prev->hl;
2852 prev->hl = s->hl;
2853 x_set_glyph_string_gc (prev);
2854 x_set_glyph_string_clipping_exactly (s, prev);
2855 if (prev->first_glyph->type == CHAR_GLYPH)
2856 x_draw_glyph_string_foreground (prev);
2857 else
2858 x_draw_composite_glyph_string_foreground (prev);
2859 XSetClipMask (prev->display, prev->gc, None);
2860 prev->hl = save;
2861 prev->num_clips = 0;
2865 if (s->next)
2867 struct glyph_string *next;
2869 for (next = s->next; next; next = next->next)
2870 if (next->hl != s->hl
2871 && next->x - next->left_overhang < s->x + s->width)
2873 /* As next will be drawn while clipped to its own area,
2874 we must draw the left_overhang part using s->hl now. */
2875 enum draw_glyphs_face save = next->hl;
2877 next->hl = s->hl;
2878 x_set_glyph_string_gc (next);
2879 x_set_glyph_string_clipping_exactly (s, next);
2880 if (next->first_glyph->type == CHAR_GLYPH)
2881 x_draw_glyph_string_foreground (next);
2882 else
2883 x_draw_composite_glyph_string_foreground (next);
2884 XSetClipMask (next->display, next->gc, None);
2885 next->hl = save;
2886 next->num_clips = 0;
2891 /* Reset clipping. */
2892 XSetClipMask (s->display, s->gc, None);
2893 s->num_clips = 0;
2896 /* Shift display to make room for inserted glyphs. */
2898 void
2899 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2900 struct frame *f;
2901 int x, y, width, height, shift_by;
2903 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2904 f->output_data.x->normal_gc,
2905 x, y, width, height,
2906 x + shift_by, y);
2909 /* Delete N glyphs at the nominal cursor position. Not implemented
2910 for X frames. */
2912 static void
2913 x_delete_glyphs (f, n)
2914 struct frame *f;
2915 register int n;
2917 abort ();
2921 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2922 If they are <= 0, this is probably an error. */
2924 void
2925 x_clear_area (dpy, window, x, y, width, height, exposures)
2926 Display *dpy;
2927 Window window;
2928 int x, y;
2929 int width, height;
2930 int exposures;
2932 xassert (width > 0 && height > 0);
2933 XClearArea (dpy, window, x, y, width, height, exposures);
2937 /* Clear an entire frame. */
2939 static void
2940 x_clear_frame (struct frame *f)
2942 /* Clearing the frame will erase any cursor, so mark them all as no
2943 longer visible. */
2944 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2945 output_cursor.hpos = output_cursor.vpos = 0;
2946 output_cursor.x = -1;
2948 /* We don't set the output cursor here because there will always
2949 follow an explicit cursor_to. */
2950 BLOCK_INPUT;
2951 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2953 /* We have to clear the scroll bars, too. If we have changed
2954 colors or something like that, then they should be notified. */
2955 x_scroll_bar_clear (f);
2957 XFlush (FRAME_X_DISPLAY (f));
2959 UNBLOCK_INPUT;
2964 /* Invert the middle quarter of the frame for .15 sec. */
2966 /* We use the select system call to do the waiting, so we have to make
2967 sure it's available. If it isn't, we just won't do visual bells. */
2969 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2972 /* Subtract the `struct timeval' values X and Y, storing the result in
2973 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2975 static int
2976 timeval_subtract (result, x, y)
2977 struct timeval *result, x, y;
2979 /* Perform the carry for the later subtraction by updating y. This
2980 is safer because on some systems the tv_sec member is unsigned. */
2981 if (x.tv_usec < y.tv_usec)
2983 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2984 y.tv_usec -= 1000000 * nsec;
2985 y.tv_sec += nsec;
2988 if (x.tv_usec - y.tv_usec > 1000000)
2990 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2991 y.tv_usec += 1000000 * nsec;
2992 y.tv_sec -= nsec;
2995 /* Compute the time remaining to wait. tv_usec is certainly
2996 positive. */
2997 result->tv_sec = x.tv_sec - y.tv_sec;
2998 result->tv_usec = x.tv_usec - y.tv_usec;
3000 /* Return indication of whether the result should be considered
3001 negative. */
3002 return x.tv_sec < y.tv_sec;
3005 void
3006 XTflash (f)
3007 struct frame *f;
3009 BLOCK_INPUT;
3012 GC gc;
3014 /* Create a GC that will use the GXxor function to flip foreground
3015 pixels into background pixels. */
3017 XGCValues values;
3019 values.function = GXxor;
3020 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3021 ^ FRAME_BACKGROUND_PIXEL (f));
3023 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3024 GCFunction | GCForeground, &values);
3028 /* Get the height not including a menu bar widget. */
3029 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
3030 /* Height of each line to flash. */
3031 int flash_height = FRAME_LINE_HEIGHT (f);
3032 /* These will be the left and right margins of the rectangles. */
3033 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3034 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3036 int width;
3038 /* Don't flash the area between a scroll bar and the frame
3039 edge it is next to. */
3040 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3042 case vertical_scroll_bar_left:
3043 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3044 break;
3046 case vertical_scroll_bar_right:
3047 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3048 break;
3050 default:
3051 break;
3054 width = flash_right - flash_left;
3056 /* If window is tall, flash top and bottom line. */
3057 if (height > 3 * FRAME_LINE_HEIGHT (f))
3059 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3060 flash_left,
3061 (FRAME_INTERNAL_BORDER_WIDTH (f)
3062 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3063 width, flash_height);
3064 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3065 flash_left,
3066 (height - flash_height
3067 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3068 width, flash_height);
3070 else
3071 /* If it is short, flash it all. */
3072 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3073 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3074 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3076 x_flush (f);
3079 struct timeval wakeup;
3081 EMACS_GET_TIME (wakeup);
3083 /* Compute time to wait until, propagating carry from usecs. */
3084 wakeup.tv_usec += 150000;
3085 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3086 wakeup.tv_usec %= 1000000;
3088 /* Keep waiting until past the time wakeup or any input gets
3089 available. */
3090 while (! detect_input_pending ())
3092 struct timeval current;
3093 struct timeval timeout;
3095 EMACS_GET_TIME (current);
3097 /* Break if result would be negative. */
3098 if (timeval_subtract (&current, wakeup, current))
3099 break;
3101 /* How long `select' should wait. */
3102 timeout.tv_sec = 0;
3103 timeout.tv_usec = 10000;
3105 /* Try to wait that long--but we might wake up sooner. */
3106 select (0, NULL, NULL, NULL, &timeout);
3110 /* If window is tall, flash top and bottom line. */
3111 if (height > 3 * FRAME_LINE_HEIGHT (f))
3113 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3114 flash_left,
3115 (FRAME_INTERNAL_BORDER_WIDTH (f)
3116 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3117 width, flash_height);
3118 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3119 flash_left,
3120 (height - flash_height
3121 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3122 width, flash_height);
3124 else
3125 /* If it is short, flash it all. */
3126 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3127 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3128 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3130 XFreeGC (FRAME_X_DISPLAY (f), gc);
3131 x_flush (f);
3135 UNBLOCK_INPUT;
3138 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3141 /* Make audible bell. */
3143 void
3144 XTring_bell ()
3146 struct frame *f = SELECTED_FRAME ();
3148 if (FRAME_X_DISPLAY (f))
3150 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3151 if (visible_bell)
3152 XTflash (f);
3153 else
3154 #endif
3156 BLOCK_INPUT;
3157 XBell (FRAME_X_DISPLAY (f), 0);
3158 XFlush (FRAME_X_DISPLAY (f));
3159 UNBLOCK_INPUT;
3165 /* Specify how many text lines, from the top of the window,
3166 should be affected by insert-lines and delete-lines operations.
3167 This, and those operations, are used only within an update
3168 that is bounded by calls to x_update_begin and x_update_end. */
3170 static void
3171 XTset_terminal_window (n)
3172 register int n;
3174 /* This function intentionally left blank. */
3179 /***********************************************************************
3180 Line Dance
3181 ***********************************************************************/
3183 /* Perform an insert-lines or delete-lines operation, inserting N
3184 lines or deleting -N lines at vertical position VPOS. */
3186 static void
3187 x_ins_del_lines (f, vpos, n)
3188 struct frame *f;
3189 int vpos, n;
3191 abort ();
3195 /* Scroll part of the display as described by RUN. */
3197 static void
3198 x_scroll_run (w, run)
3199 struct window *w;
3200 struct run *run;
3202 struct frame *f = XFRAME (w->frame);
3203 int x, y, width, height, from_y, to_y, bottom_y;
3205 /* Get frame-relative bounding box of the text display area of W,
3206 without mode lines. Include in this box the left and right
3207 fringe of W. */
3208 window_box (w, -1, &x, &y, &width, &height);
3210 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3211 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3212 bottom_y = y + height;
3214 if (to_y < from_y)
3216 /* Scrolling up. Make sure we don't copy part of the mode
3217 line at the bottom. */
3218 if (from_y + run->height > bottom_y)
3219 height = bottom_y - from_y;
3220 else
3221 height = run->height;
3223 else
3225 /* Scolling down. Make sure we don't copy over the mode line.
3226 at the bottom. */
3227 if (to_y + run->height > bottom_y)
3228 height = bottom_y - to_y;
3229 else
3230 height = run->height;
3233 BLOCK_INPUT;
3235 /* Cursor off. Will be switched on again in x_update_window_end. */
3236 updated_window = w;
3237 x_clear_cursor (w);
3239 XCopyArea (FRAME_X_DISPLAY (f),
3240 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3241 f->output_data.x->normal_gc,
3242 x, from_y,
3243 width, height,
3244 x, to_y);
3246 UNBLOCK_INPUT;
3251 /***********************************************************************
3252 Exposure Events
3253 ***********************************************************************/
3256 static void
3257 frame_highlight (f)
3258 struct frame *f;
3260 /* We used to only do this if Vx_no_window_manager was non-nil, but
3261 the ICCCM (section 4.1.6) says that the window's border pixmap
3262 and border pixel are window attributes which are "private to the
3263 client", so we can always change it to whatever we want. */
3264 BLOCK_INPUT;
3265 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3266 f->output_data.x->border_pixel);
3267 UNBLOCK_INPUT;
3268 x_update_cursor (f, 1);
3269 x_set_frame_alpha (f);
3272 static void
3273 frame_unhighlight (f)
3274 struct frame *f;
3276 /* We used to only do this if Vx_no_window_manager was non-nil, but
3277 the ICCCM (section 4.1.6) says that the window's border pixmap
3278 and border pixel are window attributes which are "private to the
3279 client", so we can always change it to whatever we want. */
3280 BLOCK_INPUT;
3281 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3282 f->output_data.x->border_tile);
3283 UNBLOCK_INPUT;
3284 x_update_cursor (f, 1);
3285 x_set_frame_alpha (f);
3288 /* The focus has changed. Update the frames as necessary to reflect
3289 the new situation. Note that we can't change the selected frame
3290 here, because the Lisp code we are interrupting might become confused.
3291 Each event gets marked with the frame in which it occurred, so the
3292 Lisp code can tell when the switch took place by examining the events. */
3294 static void
3295 x_new_focus_frame (dpyinfo, frame)
3296 struct x_display_info *dpyinfo;
3297 struct frame *frame;
3299 struct frame *old_focus = dpyinfo->x_focus_frame;
3301 if (frame != dpyinfo->x_focus_frame)
3303 /* Set this before calling other routines, so that they see
3304 the correct value of x_focus_frame. */
3305 dpyinfo->x_focus_frame = frame;
3307 if (old_focus && old_focus->auto_lower)
3308 x_lower_frame (old_focus);
3310 #if 0
3311 selected_frame = frame;
3312 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3313 selected_frame);
3314 Fselect_window (selected_frame->selected_window, Qnil);
3315 choose_minibuf_frame ();
3316 #endif /* ! 0 */
3318 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3319 pending_autoraise_frame = dpyinfo->x_focus_frame;
3320 else
3321 pending_autoraise_frame = 0;
3324 x_frame_rehighlight (dpyinfo);
3327 /* Handle FocusIn and FocusOut state changes for FRAME.
3328 If FRAME has focus and there exists more than one frame, puts
3329 a FOCUS_IN_EVENT into *BUFP. */
3331 static void
3332 x_focus_changed (type, state, dpyinfo, frame, bufp)
3333 int type;
3334 int state;
3335 struct x_display_info *dpyinfo;
3336 struct frame *frame;
3337 struct input_event *bufp;
3339 if (type == FocusIn)
3341 if (dpyinfo->x_focus_event_frame != frame)
3343 x_new_focus_frame (dpyinfo, frame);
3344 dpyinfo->x_focus_event_frame = frame;
3346 /* Don't stop displaying the initial startup message
3347 for a switch-frame event we don't need. */
3348 if (NILP (Vterminal_frame)
3349 && CONSP (Vframe_list)
3350 && !NILP (XCDR (Vframe_list)))
3352 bufp->kind = FOCUS_IN_EVENT;
3353 XSETFRAME (bufp->frame_or_window, frame);
3357 frame->output_data.x->focus_state |= state;
3359 #ifdef HAVE_X_I18N
3360 if (FRAME_XIC (frame))
3361 XSetICFocus (FRAME_XIC (frame));
3362 #endif
3364 else if (type == FocusOut)
3366 frame->output_data.x->focus_state &= ~state;
3368 if (dpyinfo->x_focus_event_frame == frame)
3370 dpyinfo->x_focus_event_frame = 0;
3371 x_new_focus_frame (dpyinfo, 0);
3374 #ifdef HAVE_X_I18N
3375 if (FRAME_XIC (frame))
3376 XUnsetICFocus (FRAME_XIC (frame));
3377 #endif
3381 /* The focus may have changed. Figure out if it is a real focus change,
3382 by checking both FocusIn/Out and Enter/LeaveNotify events.
3384 Returns FOCUS_IN_EVENT event in *BUFP. */
3386 static void
3387 x_detect_focus_change (dpyinfo, event, bufp)
3388 struct x_display_info *dpyinfo;
3389 XEvent *event;
3390 struct input_event *bufp;
3392 struct frame *frame;
3394 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3395 if (! frame)
3396 return;
3398 switch (event->type)
3400 case EnterNotify:
3401 case LeaveNotify:
3403 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3404 int focus_state
3405 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3407 if (event->xcrossing.detail != NotifyInferior
3408 && event->xcrossing.focus
3409 && ! (focus_state & FOCUS_EXPLICIT))
3410 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3411 FOCUS_IMPLICIT,
3412 dpyinfo, frame, bufp);
3414 break;
3416 case FocusIn:
3417 case FocusOut:
3418 x_focus_changed (event->type,
3419 (event->xfocus.detail == NotifyPointer ?
3420 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3421 dpyinfo, frame, bufp);
3422 break;
3424 case ClientMessage:
3425 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3427 enum xembed_message msg = event->xclient.data.l[1];
3428 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3429 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3431 break;
3436 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3438 void
3439 x_mouse_leave (dpyinfo)
3440 struct x_display_info *dpyinfo;
3442 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3445 /* The focus has changed, or we have redirected a frame's focus to
3446 another frame (this happens when a frame uses a surrogate
3447 mini-buffer frame). Shift the highlight as appropriate.
3449 The FRAME argument doesn't necessarily have anything to do with which
3450 frame is being highlighted or un-highlighted; we only use it to find
3451 the appropriate X display info. */
3453 static void
3454 XTframe_rehighlight (frame)
3455 struct frame *frame;
3457 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3460 static void
3461 x_frame_rehighlight (dpyinfo)
3462 struct x_display_info *dpyinfo;
3464 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3466 if (dpyinfo->x_focus_frame)
3468 dpyinfo->x_highlight_frame
3469 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3470 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3471 : dpyinfo->x_focus_frame);
3472 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3474 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3475 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3478 else
3479 dpyinfo->x_highlight_frame = 0;
3481 if (dpyinfo->x_highlight_frame != old_highlight)
3483 if (old_highlight)
3484 frame_unhighlight (old_highlight);
3485 if (dpyinfo->x_highlight_frame)
3486 frame_highlight (dpyinfo->x_highlight_frame);
3492 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3494 /* Initialize mode_switch_bit and modifier_meaning. */
3495 static void
3496 x_find_modifier_meanings (dpyinfo)
3497 struct x_display_info *dpyinfo;
3499 int min_code, max_code;
3500 KeySym *syms;
3501 int syms_per_code;
3502 XModifierKeymap *mods;
3504 dpyinfo->meta_mod_mask = 0;
3505 dpyinfo->shift_lock_mask = 0;
3506 dpyinfo->alt_mod_mask = 0;
3507 dpyinfo->super_mod_mask = 0;
3508 dpyinfo->hyper_mod_mask = 0;
3510 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3512 syms = XGetKeyboardMapping (dpyinfo->display,
3513 min_code, max_code - min_code + 1,
3514 &syms_per_code);
3515 mods = XGetModifierMapping (dpyinfo->display);
3517 /* Scan the modifier table to see which modifier bits the Meta and
3518 Alt keysyms are on. */
3520 int row, col; /* The row and column in the modifier table. */
3521 int found_alt_or_meta;
3523 for (row = 3; row < 8; row++)
3525 found_alt_or_meta = 0;
3526 for (col = 0; col < mods->max_keypermod; col++)
3528 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3530 /* Zeroes are used for filler. Skip them. */
3531 if (code == 0)
3532 continue;
3534 /* Are any of this keycode's keysyms a meta key? */
3536 int code_col;
3538 for (code_col = 0; code_col < syms_per_code; code_col++)
3540 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3542 switch (sym)
3544 case XK_Meta_L:
3545 case XK_Meta_R:
3546 found_alt_or_meta = 1;
3547 dpyinfo->meta_mod_mask |= (1 << row);
3548 break;
3550 case XK_Alt_L:
3551 case XK_Alt_R:
3552 found_alt_or_meta = 1;
3553 dpyinfo->alt_mod_mask |= (1 << row);
3554 break;
3556 case XK_Hyper_L:
3557 case XK_Hyper_R:
3558 if (!found_alt_or_meta)
3559 dpyinfo->hyper_mod_mask |= (1 << row);
3560 code_col = syms_per_code;
3561 col = mods->max_keypermod;
3562 break;
3564 case XK_Super_L:
3565 case XK_Super_R:
3566 if (!found_alt_or_meta)
3567 dpyinfo->super_mod_mask |= (1 << row);
3568 code_col = syms_per_code;
3569 col = mods->max_keypermod;
3570 break;
3572 case XK_Shift_Lock:
3573 /* Ignore this if it's not on the lock modifier. */
3574 if (!found_alt_or_meta && ((1 << row) == LockMask))
3575 dpyinfo->shift_lock_mask = LockMask;
3576 code_col = syms_per_code;
3577 col = mods->max_keypermod;
3578 break;
3586 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3587 if (! dpyinfo->meta_mod_mask)
3589 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3590 dpyinfo->alt_mod_mask = 0;
3593 /* If some keys are both alt and meta,
3594 make them just meta, not alt. */
3595 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3597 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3600 XFree ((char *) syms);
3601 XFreeModifiermap (mods);
3604 /* Convert between the modifier bits X uses and the modifier bits
3605 Emacs uses. */
3607 unsigned int
3608 x_x_to_emacs_modifiers (dpyinfo, state)
3609 struct x_display_info *dpyinfo;
3610 unsigned int state;
3612 EMACS_UINT mod_meta = meta_modifier;
3613 EMACS_UINT mod_alt = alt_modifier;
3614 EMACS_UINT mod_hyper = hyper_modifier;
3615 EMACS_UINT mod_super = super_modifier;
3616 Lisp_Object tem;
3618 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3619 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3620 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3621 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3622 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3623 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3624 tem = Fget (Vx_super_keysym, Qmodifier_value);
3625 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3628 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3629 | ((state & ControlMask) ? ctrl_modifier : 0)
3630 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3631 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3632 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3633 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3636 static unsigned int
3637 x_emacs_to_x_modifiers (dpyinfo, state)
3638 struct x_display_info *dpyinfo;
3639 unsigned int state;
3641 EMACS_UINT mod_meta = meta_modifier;
3642 EMACS_UINT mod_alt = alt_modifier;
3643 EMACS_UINT mod_hyper = hyper_modifier;
3644 EMACS_UINT mod_super = super_modifier;
3646 Lisp_Object tem;
3648 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3649 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3650 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3651 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3652 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3653 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3654 tem = Fget (Vx_super_keysym, Qmodifier_value);
3655 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3658 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3659 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3660 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3661 | ((state & shift_modifier) ? ShiftMask : 0)
3662 | ((state & ctrl_modifier) ? ControlMask : 0)
3663 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3666 /* Convert a keysym to its name. */
3668 char *
3669 x_get_keysym_name (keysym)
3670 KeySym keysym;
3672 char *value;
3674 BLOCK_INPUT;
3675 value = XKeysymToString (keysym);
3676 UNBLOCK_INPUT;
3678 return value;
3683 /* Mouse clicks and mouse movement. Rah. */
3685 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3687 If the event is a button press, then note that we have grabbed
3688 the mouse. */
3690 static Lisp_Object
3691 construct_mouse_click (result, event, f)
3692 struct input_event *result;
3693 XButtonEvent *event;
3694 struct frame *f;
3696 /* Make the event type NO_EVENT; we'll change that when we decide
3697 otherwise. */
3698 result->kind = MOUSE_CLICK_EVENT;
3699 result->code = event->button - Button1;
3700 result->timestamp = event->time;
3701 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3702 event->state)
3703 | (event->type == ButtonRelease
3704 ? up_modifier
3705 : down_modifier));
3707 XSETINT (result->x, event->x);
3708 XSETINT (result->y, event->y);
3709 XSETFRAME (result->frame_or_window, f);
3710 result->arg = Qnil;
3711 return Qnil;
3715 /* Function to report a mouse movement to the mainstream Emacs code.
3716 The input handler calls this.
3718 We have received a mouse movement event, which is given in *event.
3719 If the mouse is over a different glyph than it was last time, tell
3720 the mainstream emacs code by setting mouse_moved. If not, ask for
3721 another motion event, so we can check again the next time it moves. */
3723 static XMotionEvent last_mouse_motion_event;
3724 static Lisp_Object last_mouse_motion_frame;
3726 static int
3727 note_mouse_movement (frame, event)
3728 FRAME_PTR frame;
3729 XMotionEvent *event;
3731 last_mouse_movement_time = event->time;
3732 last_mouse_motion_event = *event;
3733 XSETFRAME (last_mouse_motion_frame, frame);
3735 if (!FRAME_X_OUTPUT (frame))
3736 return 0;
3738 if (event->window != FRAME_X_WINDOW (frame))
3740 frame->mouse_moved = 1;
3741 last_mouse_scroll_bar = Qnil;
3742 note_mouse_highlight (frame, -1, -1);
3743 last_mouse_glyph_frame = 0;
3744 return 1;
3748 /* Has the mouse moved off the glyph it was on at the last sighting? */
3749 if (frame != last_mouse_glyph_frame
3750 || event->x < last_mouse_glyph.x
3751 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3752 || event->y < last_mouse_glyph.y
3753 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3755 frame->mouse_moved = 1;
3756 last_mouse_scroll_bar = Qnil;
3757 note_mouse_highlight (frame, event->x, event->y);
3758 /* Remember which glyph we're now on. */
3759 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3760 last_mouse_glyph_frame = frame;
3761 return 1;
3764 return 0;
3768 /************************************************************************
3769 Mouse Face
3770 ************************************************************************/
3772 static void
3773 redo_mouse_highlight ()
3775 if (!NILP (last_mouse_motion_frame)
3776 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3777 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3778 last_mouse_motion_event.x,
3779 last_mouse_motion_event.y);
3784 /* Return the current position of the mouse.
3785 *FP should be a frame which indicates which display to ask about.
3787 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3788 and *PART to the frame, window, and scroll bar part that the mouse
3789 is over. Set *X and *Y to the portion and whole of the mouse's
3790 position on the scroll bar.
3792 If the mouse movement started elsewhere, set *FP to the frame the
3793 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3794 the mouse is over.
3796 Set *TIME to the server time-stamp for the time at which the mouse
3797 was at this position.
3799 Don't store anything if we don't have a valid set of values to report.
3801 This clears the mouse_moved flag, so we can wait for the next mouse
3802 movement. */
3804 static void
3805 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3806 FRAME_PTR *fp;
3807 int insist;
3808 Lisp_Object *bar_window;
3809 enum scroll_bar_part *part;
3810 Lisp_Object *x, *y;
3811 unsigned long *time;
3813 FRAME_PTR f1;
3815 BLOCK_INPUT;
3817 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3818 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3819 else
3821 Window root;
3822 int root_x, root_y;
3824 Window dummy_window;
3825 int dummy;
3827 Lisp_Object frame, tail;
3829 /* Clear the mouse-moved flag for every frame on this display. */
3830 FOR_EACH_FRAME (tail, frame)
3831 if (FRAME_X_P (XFRAME (frame))
3832 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3833 XFRAME (frame)->mouse_moved = 0;
3835 last_mouse_scroll_bar = Qnil;
3837 /* Figure out which root window we're on. */
3838 XQueryPointer (FRAME_X_DISPLAY (*fp),
3839 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3841 /* The root window which contains the pointer. */
3842 &root,
3844 /* Trash which we can't trust if the pointer is on
3845 a different screen. */
3846 &dummy_window,
3848 /* The position on that root window. */
3849 &root_x, &root_y,
3851 /* More trash we can't trust. */
3852 &dummy, &dummy,
3854 /* Modifier keys and pointer buttons, about which
3855 we don't care. */
3856 (unsigned int *) &dummy);
3858 /* Now we have a position on the root; find the innermost window
3859 containing the pointer. */
3861 Window win, child;
3862 int win_x, win_y;
3863 int parent_x = 0, parent_y = 0;
3865 win = root;
3867 /* XTranslateCoordinates can get errors if the window
3868 structure is changing at the same time this function
3869 is running. So at least we must not crash from them. */
3871 x_catch_errors (FRAME_X_DISPLAY (*fp));
3873 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3874 && FRAME_LIVE_P (last_mouse_frame))
3876 /* If mouse was grabbed on a frame, give coords for that frame
3877 even if the mouse is now outside it. */
3878 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3880 /* From-window, to-window. */
3881 root, FRAME_X_WINDOW (last_mouse_frame),
3883 /* From-position, to-position. */
3884 root_x, root_y, &win_x, &win_y,
3886 /* Child of win. */
3887 &child);
3888 f1 = last_mouse_frame;
3890 else
3892 while (1)
3894 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3896 /* From-window, to-window. */
3897 root, win,
3899 /* From-position, to-position. */
3900 root_x, root_y, &win_x, &win_y,
3902 /* Child of win. */
3903 &child);
3905 if (child == None || child == win)
3906 break;
3908 win = child;
3909 parent_x = win_x;
3910 parent_y = win_y;
3913 /* Now we know that:
3914 win is the innermost window containing the pointer
3915 (XTC says it has no child containing the pointer),
3916 win_x and win_y are the pointer's position in it
3917 (XTC did this the last time through), and
3918 parent_x and parent_y are the pointer's position in win's parent.
3919 (They are what win_x and win_y were when win was child.
3920 If win is the root window, it has no parent, and
3921 parent_{x,y} are invalid, but that's okay, because we'll
3922 never use them in that case.) */
3924 /* Is win one of our frames? */
3925 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3927 #ifdef USE_X_TOOLKIT
3928 /* If we end up with the menu bar window, say it's not
3929 on the frame. */
3930 if (f1 != NULL
3931 && f1->output_data.x->menubar_widget
3932 && win == XtWindow (f1->output_data.x->menubar_widget))
3933 f1 = NULL;
3934 #endif /* USE_X_TOOLKIT */
3937 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3938 f1 = 0;
3940 x_uncatch_errors ();
3942 /* If not, is it one of our scroll bars? */
3943 if (! f1)
3945 struct scroll_bar *bar;
3947 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3949 if (bar)
3951 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3952 win_x = parent_x;
3953 win_y = parent_y;
3957 if (f1 == 0 && insist > 0)
3958 f1 = SELECTED_FRAME ();
3960 if (f1)
3962 /* Ok, we found a frame. Store all the values.
3963 last_mouse_glyph is a rectangle used to reduce the
3964 generation of mouse events. To not miss any motion
3965 events, we must divide the frame into rectangles of the
3966 size of the smallest character that could be displayed
3967 on it, i.e. into the same rectangles that matrices on
3968 the frame are divided into. */
3970 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
3971 last_mouse_glyph_frame = f1;
3973 *bar_window = Qnil;
3974 *part = 0;
3975 *fp = f1;
3976 XSETINT (*x, win_x);
3977 XSETINT (*y, win_y);
3978 *time = last_mouse_movement_time;
3983 UNBLOCK_INPUT;
3988 /***********************************************************************
3989 Scroll bars
3990 ***********************************************************************/
3992 /* Scroll bar support. */
3994 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
3995 manages it.
3996 This can be called in GC, so we have to make sure to strip off mark
3997 bits. */
3999 static struct scroll_bar *
4000 x_window_to_scroll_bar (display, window_id)
4001 Display *display;
4002 Window window_id;
4004 Lisp_Object tail;
4006 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4007 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4008 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4010 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4012 Lisp_Object frame, bar, condemned;
4014 frame = XCAR (tail);
4015 /* All elements of Vframe_list should be frames. */
4016 if (! FRAMEP (frame))
4017 abort ();
4019 if (! FRAME_X_P (XFRAME (frame)))
4020 continue;
4022 /* Scan this frame's scroll bar list for a scroll bar with the
4023 right window ID. */
4024 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4025 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4026 /* This trick allows us to search both the ordinary and
4027 condemned scroll bar lists with one loop. */
4028 ! NILP (bar) || (bar = condemned,
4029 condemned = Qnil,
4030 ! NILP (bar));
4031 bar = XSCROLL_BAR (bar)->next)
4032 if (XSCROLL_BAR (bar)->x_window == window_id &&
4033 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4034 return XSCROLL_BAR (bar);
4037 return 0;
4041 #if defined USE_LUCID
4043 /* Return the Lucid menu bar WINDOW is part of. Return null
4044 if WINDOW is not part of a menu bar. */
4046 static Widget
4047 x_window_to_menu_bar (window)
4048 Window window;
4050 Lisp_Object tail;
4052 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4054 if (FRAME_X_P (XFRAME (XCAR (tail))))
4056 Lisp_Object frame = XCAR (tail);
4057 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4059 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4060 return menu_bar;
4064 return NULL;
4067 #endif /* USE_LUCID */
4070 /************************************************************************
4071 Toolkit scroll bars
4072 ************************************************************************/
4074 #ifdef USE_TOOLKIT_SCROLL_BARS
4076 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4077 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4078 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4079 struct scroll_bar *));
4080 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4081 int, int, int));
4084 /* Lisp window being scrolled. Set when starting to interact with
4085 a toolkit scroll bar, reset to nil when ending the interaction. */
4087 static Lisp_Object window_being_scrolled;
4089 /* Last scroll bar part sent in xm_scroll_callback. */
4091 static int last_scroll_bar_part;
4093 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4094 that movements of 1/20 of the screen size are mapped to up/down. */
4096 #ifndef USE_GTK
4097 /* Id of action hook installed for scroll bars. */
4099 static XtActionHookId action_hook_id;
4101 static Boolean xaw3d_arrow_scroll;
4103 /* Whether the drag scrolling maintains the mouse at the top of the
4104 thumb. If not, resizing the thumb needs to be done more carefully
4105 to avoid jerkyness. */
4107 static Boolean xaw3d_pick_top;
4109 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4110 bars are used.. The hook is responsible for detecting when
4111 the user ends an interaction with the scroll bar, and generates
4112 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4114 static void
4115 xt_action_hook (widget, client_data, action_name, event, params,
4116 num_params)
4117 Widget widget;
4118 XtPointer client_data;
4119 String action_name;
4120 XEvent *event;
4121 String *params;
4122 Cardinal *num_params;
4124 int scroll_bar_p;
4125 char *end_action;
4127 #ifdef USE_MOTIF
4128 scroll_bar_p = XmIsScrollBar (widget);
4129 end_action = "Release";
4130 #else /* !USE_MOTIF i.e. use Xaw */
4131 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4132 end_action = "EndScroll";
4133 #endif /* USE_MOTIF */
4135 if (scroll_bar_p
4136 && strcmp (action_name, end_action) == 0
4137 && WINDOWP (window_being_scrolled))
4139 struct window *w;
4141 x_send_scroll_bar_event (window_being_scrolled,
4142 scroll_bar_end_scroll, 0, 0);
4143 w = XWINDOW (window_being_scrolled);
4145 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4147 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4148 /* The thumb size is incorrect while dragging: fix it. */
4149 set_vertical_scroll_bar (w);
4151 window_being_scrolled = Qnil;
4152 last_scroll_bar_part = -1;
4154 /* Xt timeouts no longer needed. */
4155 toolkit_scroll_bar_interaction = 0;
4158 #endif /* not USE_GTK */
4160 /* A vector of windows used for communication between
4161 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4163 static struct window **scroll_bar_windows;
4164 static int scroll_bar_windows_size;
4167 /* Send a client message with message type Xatom_Scrollbar for a
4168 scroll action to the frame of WINDOW. PART is a value identifying
4169 the part of the scroll bar that was clicked on. PORTION is the
4170 amount to scroll of a whole of WHOLE. */
4172 static void
4173 x_send_scroll_bar_event (window, part, portion, whole)
4174 Lisp_Object window;
4175 int part, portion, whole;
4177 XEvent event;
4178 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4179 struct window *w = XWINDOW (window);
4180 struct frame *f = XFRAME (w->frame);
4181 int i;
4183 BLOCK_INPUT;
4185 /* Construct a ClientMessage event to send to the frame. */
4186 ev->type = ClientMessage;
4187 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4188 ev->display = FRAME_X_DISPLAY (f);
4189 ev->window = FRAME_X_WINDOW (f);
4190 ev->format = 32;
4192 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4193 not enough to store a pointer or Lisp_Object on a 64 bit system.
4194 So, store the window in scroll_bar_windows and pass the index
4195 into that array in the event. */
4196 for (i = 0; i < scroll_bar_windows_size; ++i)
4197 if (scroll_bar_windows[i] == NULL)
4198 break;
4200 if (i == scroll_bar_windows_size)
4202 int new_size = max (10, 2 * scroll_bar_windows_size);
4203 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4204 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4206 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4207 nbytes);
4208 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4209 scroll_bar_windows_size = new_size;
4212 scroll_bar_windows[i] = w;
4213 ev->data.l[0] = (long) i;
4214 ev->data.l[1] = (long) part;
4215 ev->data.l[2] = (long) 0;
4216 ev->data.l[3] = (long) portion;
4217 ev->data.l[4] = (long) whole;
4219 /* Make Xt timeouts work while the scroll bar is active. */
4220 toolkit_scroll_bar_interaction = 1;
4221 #ifdef USE_X_TOOLKIT
4222 x_activate_timeout_atimer ();
4223 #endif
4225 /* Setting the event mask to zero means that the message will
4226 be sent to the client that created the window, and if that
4227 window no longer exists, no event will be sent. */
4228 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4229 UNBLOCK_INPUT;
4233 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4234 in *IEVENT. */
4236 static void
4237 x_scroll_bar_to_input_event (event, ievent)
4238 XEvent *event;
4239 struct input_event *ievent;
4241 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4242 Lisp_Object window;
4243 struct frame *f;
4244 struct window *w;
4246 w = scroll_bar_windows[ev->data.l[0]];
4247 scroll_bar_windows[ev->data.l[0]] = NULL;
4249 XSETWINDOW (window, w);
4250 f = XFRAME (w->frame);
4252 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4253 ievent->frame_or_window = window;
4254 ievent->arg = Qnil;
4255 #ifdef USE_GTK
4256 ievent->timestamp = CurrentTime;
4257 #else
4258 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4259 #endif
4260 ievent->part = ev->data.l[1];
4261 ievent->code = ev->data.l[2];
4262 ievent->x = make_number ((int) ev->data.l[3]);
4263 ievent->y = make_number ((int) ev->data.l[4]);
4264 ievent->modifiers = 0;
4268 #ifdef USE_MOTIF
4270 /* Minimum and maximum values used for Motif scroll bars. */
4272 #define XM_SB_MAX 10000000
4275 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4276 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4277 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4279 static void
4280 xm_scroll_callback (widget, client_data, call_data)
4281 Widget widget;
4282 XtPointer client_data, call_data;
4284 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4285 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4286 int part = -1, whole = 0, portion = 0;
4288 switch (cs->reason)
4290 case XmCR_DECREMENT:
4291 bar->dragging = Qnil;
4292 part = scroll_bar_up_arrow;
4293 break;
4295 case XmCR_INCREMENT:
4296 bar->dragging = Qnil;
4297 part = scroll_bar_down_arrow;
4298 break;
4300 case XmCR_PAGE_DECREMENT:
4301 bar->dragging = Qnil;
4302 part = scroll_bar_above_handle;
4303 break;
4305 case XmCR_PAGE_INCREMENT:
4306 bar->dragging = Qnil;
4307 part = scroll_bar_below_handle;
4308 break;
4310 case XmCR_TO_TOP:
4311 bar->dragging = Qnil;
4312 part = scroll_bar_to_top;
4313 break;
4315 case XmCR_TO_BOTTOM:
4316 bar->dragging = Qnil;
4317 part = scroll_bar_to_bottom;
4318 break;
4320 case XmCR_DRAG:
4322 int slider_size;
4324 /* Get the slider size. */
4325 BLOCK_INPUT;
4326 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4327 UNBLOCK_INPUT;
4329 whole = XM_SB_MAX - slider_size;
4330 portion = min (cs->value, whole);
4331 part = scroll_bar_handle;
4332 bar->dragging = make_number (cs->value);
4334 break;
4336 case XmCR_VALUE_CHANGED:
4337 break;
4340 if (part >= 0)
4342 window_being_scrolled = bar->window;
4343 last_scroll_bar_part = part;
4344 x_send_scroll_bar_event (bar->window, part, portion, whole);
4348 #elif defined USE_GTK
4350 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4351 bar widget. DATA is a pointer to the scroll_bar structure. */
4353 static void
4354 xg_scroll_callback (widget, data)
4355 GtkRange *widget;
4356 gpointer data;
4358 struct scroll_bar *bar = (struct scroll_bar *) data;
4359 gdouble previous;
4360 gdouble position;
4361 gdouble *p;
4362 int diff;
4364 int part = -1, whole = 0, portion = 0;
4365 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4367 position = gtk_adjustment_get_value (adj);
4369 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4370 if (! p)
4372 p = (gdouble*) xmalloc (sizeof (gdouble));
4373 *p = XG_SB_MIN;
4374 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4377 previous = *p;
4378 *p = position;
4380 if (xg_ignore_gtk_scrollbar) return;
4382 diff = (int) (position - previous);
4384 if (diff == (int) adj->step_increment)
4386 part = scroll_bar_down_arrow;
4387 bar->dragging = Qnil;
4389 else if (-diff == (int) adj->step_increment)
4391 part = scroll_bar_up_arrow;
4392 bar->dragging = Qnil;
4394 else if (diff == (int) adj->page_increment)
4396 part = scroll_bar_below_handle;
4397 bar->dragging = Qnil;
4399 else if (-diff == (int) adj->page_increment)
4401 part = scroll_bar_above_handle;
4402 bar->dragging = Qnil;
4404 else
4406 part = scroll_bar_handle;
4407 whole = adj->upper - adj->page_size;
4408 portion = min ((int)position, whole);
4409 bar->dragging = make_number ((int)portion);
4412 if (part >= 0)
4414 window_being_scrolled = bar->window;
4415 last_scroll_bar_part = part;
4416 x_send_scroll_bar_event (bar->window, part, portion, whole);
4420 #else /* not USE_GTK and not USE_MOTIF */
4422 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4423 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4424 scroll bar struct. CALL_DATA is a pointer to a float saying where
4425 the thumb is. */
4427 static void
4428 xaw_jump_callback (widget, client_data, call_data)
4429 Widget widget;
4430 XtPointer client_data, call_data;
4432 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4433 float top = *(float *) call_data;
4434 float shown;
4435 int whole, portion, height;
4436 int part;
4438 /* Get the size of the thumb, a value between 0 and 1. */
4439 BLOCK_INPUT;
4440 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4441 UNBLOCK_INPUT;
4443 whole = 10000000;
4444 portion = shown < 1 ? top * whole : 0;
4446 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4447 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4448 the bottom, so we force the scrolling whenever we see that we're
4449 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4450 we try to ensure that we always stay two pixels away from the
4451 bottom). */
4452 part = scroll_bar_down_arrow;
4453 else
4454 part = scroll_bar_handle;
4456 window_being_scrolled = bar->window;
4457 bar->dragging = make_number (portion);
4458 last_scroll_bar_part = part;
4459 x_send_scroll_bar_event (bar->window, part, portion, whole);
4463 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4464 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4465 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4466 the scroll bar. CALL_DATA is an integer specifying the action that
4467 has taken place. Its magnitude is in the range 0..height of the
4468 scroll bar. Negative values mean scroll towards buffer start.
4469 Values < height of scroll bar mean line-wise movement. */
4471 static void
4472 xaw_scroll_callback (widget, client_data, call_data)
4473 Widget widget;
4474 XtPointer client_data, call_data;
4476 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4477 /* The position really is stored cast to a pointer. */
4478 int position = (long) call_data;
4479 Dimension height;
4480 int part;
4482 /* Get the height of the scroll bar. */
4483 BLOCK_INPUT;
4484 XtVaGetValues (widget, XtNheight, &height, NULL);
4485 UNBLOCK_INPUT;
4487 if (eabs (position) >= height)
4488 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4490 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4491 it maps line-movement to call_data = max(5, height/20). */
4492 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4493 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4494 else
4495 part = scroll_bar_move_ratio;
4497 window_being_scrolled = bar->window;
4498 bar->dragging = Qnil;
4499 last_scroll_bar_part = part;
4500 x_send_scroll_bar_event (bar->window, part, position, height);
4503 #endif /* not USE_GTK and not USE_MOTIF */
4505 #define SCROLL_BAR_NAME "verticalScrollBar"
4507 /* Create the widget for scroll bar BAR on frame F. Record the widget
4508 and X window of the scroll bar in BAR. */
4510 #ifdef USE_GTK
4511 static void
4512 x_create_toolkit_scroll_bar (f, bar)
4513 struct frame *f;
4514 struct scroll_bar *bar;
4516 char *scroll_bar_name = SCROLL_BAR_NAME;
4518 BLOCK_INPUT;
4519 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4520 scroll_bar_name);
4521 UNBLOCK_INPUT;
4524 #else /* not USE_GTK */
4526 static void
4527 x_create_toolkit_scroll_bar (f, bar)
4528 struct frame *f;
4529 struct scroll_bar *bar;
4531 Window xwindow;
4532 Widget widget;
4533 Arg av[20];
4534 int ac = 0;
4535 char *scroll_bar_name = SCROLL_BAR_NAME;
4536 unsigned long pixel;
4538 BLOCK_INPUT;
4540 #ifdef USE_MOTIF
4541 /* Set resources. Create the widget. */
4542 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4543 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4544 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4545 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4546 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4547 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4548 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4550 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4551 if (pixel != -1)
4553 XtSetArg (av[ac], XmNforeground, pixel);
4554 ++ac;
4557 pixel = f->output_data.x->scroll_bar_background_pixel;
4558 if (pixel != -1)
4560 XtSetArg (av[ac], XmNbackground, pixel);
4561 ++ac;
4564 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4565 scroll_bar_name, av, ac);
4567 /* Add one callback for everything that can happen. */
4568 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4569 (XtPointer) bar);
4570 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4571 (XtPointer) bar);
4572 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4573 (XtPointer) bar);
4574 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4575 (XtPointer) bar);
4576 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4577 (XtPointer) bar);
4578 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4579 (XtPointer) bar);
4580 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4581 (XtPointer) bar);
4583 /* Realize the widget. Only after that is the X window created. */
4584 XtRealizeWidget (widget);
4586 /* Set the cursor to an arrow. I didn't find a resource to do that.
4587 And I'm wondering why it hasn't an arrow cursor by default. */
4588 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4589 f->output_data.x->nontext_cursor);
4591 #else /* !USE_MOTIF i.e. use Xaw */
4593 /* Set resources. Create the widget. The background of the
4594 Xaw3d scroll bar widget is a little bit light for my taste.
4595 We don't alter it here to let users change it according
4596 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4597 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4598 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4599 /* For smoother scrolling with Xaw3d -sm */
4600 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4602 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4603 if (pixel != -1)
4605 XtSetArg (av[ac], XtNforeground, pixel);
4606 ++ac;
4609 pixel = f->output_data.x->scroll_bar_background_pixel;
4610 if (pixel != -1)
4612 XtSetArg (av[ac], XtNbackground, pixel);
4613 ++ac;
4616 /* Top/bottom shadow colors. */
4618 /* Allocate them, if necessary. */
4619 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4621 pixel = f->output_data.x->scroll_bar_background_pixel;
4622 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4623 &pixel, 1.2, 0x8000))
4624 pixel = -1;
4625 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4627 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4629 pixel = f->output_data.x->scroll_bar_background_pixel;
4630 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4631 &pixel, 0.6, 0x4000))
4632 pixel = -1;
4633 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4636 #ifdef XtNbeNiceToColormap
4637 /* Tell the toolkit about them. */
4638 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4639 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4640 /* We tried to allocate a color for the top/bottom shadow, and
4641 failed, so tell Xaw3d to use dithering instead. */
4643 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4644 ++ac;
4646 else
4647 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4648 be more consistent with other emacs 3d colors, and since Xaw3d is
4649 not good at dealing with allocation failure. */
4651 /* This tells Xaw3d to use real colors instead of dithering for
4652 the shadows. */
4653 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4654 ++ac;
4656 /* Specify the colors. */
4657 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4658 if (pixel != -1)
4660 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4661 ++ac;
4663 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4664 if (pixel != -1)
4666 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4667 ++ac;
4670 #endif
4672 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4673 f->output_data.x->edit_widget, av, ac);
4676 char *initial = "";
4677 char *val = initial;
4678 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4679 #ifdef XtNarrowScrollbars
4680 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4681 #endif
4682 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4683 if (xaw3d_arrow_scroll || val == initial)
4684 { /* ARROW_SCROLL */
4685 xaw3d_arrow_scroll = True;
4686 /* Isn't that just a personal preference ? --Stef */
4687 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4691 /* Define callbacks. */
4692 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4693 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4694 (XtPointer) bar);
4696 /* Realize the widget. Only after that is the X window created. */
4697 XtRealizeWidget (widget);
4699 #endif /* !USE_MOTIF */
4701 /* Install an action hook that lets us detect when the user
4702 finishes interacting with a scroll bar. */
4703 if (action_hook_id == 0)
4704 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4706 /* Remember X window and widget in the scroll bar vector. */
4707 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4708 xwindow = XtWindow (widget);
4709 bar->x_window = xwindow;
4711 UNBLOCK_INPUT;
4713 #endif /* not USE_GTK */
4716 /* Set the thumb size and position of scroll bar BAR. We are currently
4717 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4719 #ifdef USE_GTK
4720 static void
4721 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4722 struct scroll_bar *bar;
4723 int portion, position, whole;
4725 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4728 #else /* not USE_GTK */
4729 static void
4730 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4731 struct scroll_bar *bar;
4732 int portion, position, whole;
4734 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4735 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4736 float top, shown;
4738 BLOCK_INPUT;
4740 #ifdef USE_MOTIF
4742 /* We use an estimate of 30 chars per line rather than the real
4743 `portion' value. This has the disadvantage that the thumb size
4744 is not very representative, but it makes our life a lot easier.
4745 Otherwise, we have to constantly adjust the thumb size, which
4746 we can't always do quickly enough: while dragging, the size of
4747 the thumb might prevent the user from dragging the thumb all the
4748 way to the end. but Motif and some versions of Xaw3d don't allow
4749 updating the thumb size while dragging. Also, even if we can update
4750 its size, the update will often happen too late.
4751 If you don't believe it, check out revision 1.650 of xterm.c to see
4752 what hoops we were going through and the still poor behavior we got. */
4753 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4754 /* When the thumb is at the bottom, position == whole.
4755 So we need to increase `whole' to make space for the thumb. */
4756 whole += portion;
4758 if (whole <= 0)
4759 top = 0, shown = 1;
4760 else
4762 top = (float) position / whole;
4763 shown = (float) portion / whole;
4766 if (NILP (bar->dragging))
4768 int size, value;
4770 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4771 is the scroll bar's maximum and MIN is the scroll bar's minimum
4772 value. */
4773 size = shown * XM_SB_MAX;
4774 size = min (size, XM_SB_MAX);
4775 size = max (size, 1);
4777 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4778 value = top * XM_SB_MAX;
4779 value = min (value, XM_SB_MAX - size);
4781 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4783 #else /* !USE_MOTIF i.e. use Xaw */
4785 if (whole == 0)
4786 top = 0, shown = 1;
4787 else
4789 top = (float) position / whole;
4790 shown = (float) portion / whole;
4794 float old_top, old_shown;
4795 Dimension height;
4796 XtVaGetValues (widget,
4797 XtNtopOfThumb, &old_top,
4798 XtNshown, &old_shown,
4799 XtNheight, &height,
4800 NULL);
4802 /* Massage the top+shown values. */
4803 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4804 top = max (0, min (1, top));
4805 else
4806 top = old_top;
4807 /* Keep two pixels available for moving the thumb down. */
4808 shown = max (0, min (1 - top - (2.0 / height), shown));
4810 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4811 check that your system's configuration file contains a define
4812 for `NARROWPROTO'. See s/freebsd.h for an example. */
4813 if (top != old_top || shown != old_shown)
4815 if (NILP (bar->dragging))
4816 XawScrollbarSetThumb (widget, top, shown);
4817 else
4819 /* Try to make the scrolling a tad smoother. */
4820 if (!xaw3d_pick_top)
4821 shown = min (shown, old_shown);
4823 XawScrollbarSetThumb (widget, top, shown);
4827 #endif /* !USE_MOTIF */
4829 UNBLOCK_INPUT;
4831 #endif /* not USE_GTK */
4833 #endif /* USE_TOOLKIT_SCROLL_BARS */
4837 /************************************************************************
4838 Scroll bars, general
4839 ************************************************************************/
4841 /* Create a scroll bar and return the scroll bar vector for it. W is
4842 the Emacs window on which to create the scroll bar. TOP, LEFT,
4843 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4844 scroll bar. */
4846 static struct scroll_bar *
4847 x_scroll_bar_create (w, top, left, width, height)
4848 struct window *w;
4849 int top, left, width, height;
4851 struct frame *f = XFRAME (w->frame);
4852 struct scroll_bar *bar
4853 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4855 BLOCK_INPUT;
4857 #ifdef USE_TOOLKIT_SCROLL_BARS
4858 x_create_toolkit_scroll_bar (f, bar);
4859 #else /* not USE_TOOLKIT_SCROLL_BARS */
4861 XSetWindowAttributes a;
4862 unsigned long mask;
4863 Window window;
4865 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4866 if (a.background_pixel == -1)
4867 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4869 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4870 | ButtonMotionMask | PointerMotionHintMask
4871 | ExposureMask);
4872 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4874 mask = (CWBackPixel | CWEventMask | CWCursor);
4876 /* Clear the area of W that will serve as a scroll bar. This is
4877 for the case that a window has been split horizontally. In
4878 this case, no clear_frame is generated to reduce flickering. */
4879 if (width > 0 && height > 0)
4880 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4881 left, top, width,
4882 window_box_height (w), False);
4884 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4885 /* Position and size of scroll bar. */
4886 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4887 top,
4888 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4889 height,
4890 /* Border width, depth, class, and visual. */
4892 CopyFromParent,
4893 CopyFromParent,
4894 CopyFromParent,
4895 /* Attributes. */
4896 mask, &a);
4897 bar->x_window = window;
4899 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4901 XSETWINDOW (bar->window, w);
4902 bar->top = top;
4903 bar->left = left;
4904 bar->width = width;
4905 bar->height = height;
4906 bar->start = 0;
4907 bar->end = 0;
4908 bar->dragging = Qnil;
4909 bar->fringe_extended_p = 0;
4911 /* Add bar to its frame's list of scroll bars. */
4912 bar->next = FRAME_SCROLL_BARS (f);
4913 bar->prev = Qnil;
4914 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4915 if (!NILP (bar->next))
4916 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4918 /* Map the window/widget. */
4919 #ifdef USE_TOOLKIT_SCROLL_BARS
4921 #ifdef USE_GTK
4922 xg_update_scrollbar_pos (f,
4923 bar->x_window,
4924 top,
4925 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4926 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4927 max (height, 1));
4928 xg_show_scroll_bar (bar->x_window);
4929 #else /* not USE_GTK */
4930 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4931 XtConfigureWidget (scroll_bar,
4932 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4933 top,
4934 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4935 max (height, 1), 0);
4936 XtMapWidget (scroll_bar);
4937 #endif /* not USE_GTK */
4939 #else /* not USE_TOOLKIT_SCROLL_BARS */
4940 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
4941 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4943 UNBLOCK_INPUT;
4944 return bar;
4948 #ifndef USE_TOOLKIT_SCROLL_BARS
4950 /* Draw BAR's handle in the proper position.
4952 If the handle is already drawn from START to END, don't bother
4953 redrawing it, unless REBUILD is non-zero; in that case, always
4954 redraw it. (REBUILD is handy for drawing the handle after expose
4955 events.)
4957 Normally, we want to constrain the start and end of the handle to
4958 fit inside its rectangle, but if the user is dragging the scroll
4959 bar handle, we want to let them drag it down all the way, so that
4960 the bar's top is as far down as it goes; otherwise, there's no way
4961 to move to the very end of the buffer. */
4963 static void
4964 x_scroll_bar_set_handle (bar, start, end, rebuild)
4965 struct scroll_bar *bar;
4966 int start, end;
4967 int rebuild;
4969 int dragging = ! NILP (bar->dragging);
4970 Window w = bar->x_window;
4971 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4972 GC gc = f->output_data.x->normal_gc;
4974 /* If the display is already accurate, do nothing. */
4975 if (! rebuild
4976 && start == bar->start
4977 && end == bar->end)
4978 return;
4980 BLOCK_INPUT;
4983 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
4984 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
4985 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
4987 /* Make sure the values are reasonable, and try to preserve
4988 the distance between start and end. */
4990 int length = end - start;
4992 if (start < 0)
4993 start = 0;
4994 else if (start > top_range)
4995 start = top_range;
4996 end = start + length;
4998 if (end < start)
4999 end = start;
5000 else if (end > top_range && ! dragging)
5001 end = top_range;
5004 /* Store the adjusted setting in the scroll bar. */
5005 bar->start = start;
5006 bar->end = end;
5008 /* Clip the end position, just for display. */
5009 if (end > top_range)
5010 end = top_range;
5012 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5013 below top positions, to make sure the handle is always at least
5014 that many pixels tall. */
5015 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5017 /* Draw the empty space above the handle. Note that we can't clear
5018 zero-height areas; that means "clear to end of window." */
5019 if (0 < start)
5020 x_clear_area (FRAME_X_DISPLAY (f), w,
5021 /* x, y, width, height, and exposures. */
5022 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5023 VERTICAL_SCROLL_BAR_TOP_BORDER,
5024 inside_width, start,
5025 False);
5027 /* Change to proper foreground color if one is specified. */
5028 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5029 XSetForeground (FRAME_X_DISPLAY (f), gc,
5030 f->output_data.x->scroll_bar_foreground_pixel);
5032 /* Draw the handle itself. */
5033 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5034 /* x, y, width, height */
5035 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5036 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5037 inside_width, end - start);
5039 /* Restore the foreground color of the GC if we changed it above. */
5040 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5041 XSetForeground (FRAME_X_DISPLAY (f), gc,
5042 FRAME_FOREGROUND_PIXEL (f));
5044 /* Draw the empty space below the handle. Note that we can't
5045 clear zero-height areas; that means "clear to end of window." */
5046 if (end < inside_height)
5047 x_clear_area (FRAME_X_DISPLAY (f), w,
5048 /* x, y, width, height, and exposures. */
5049 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5050 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5051 inside_width, inside_height - end,
5052 False);
5056 UNBLOCK_INPUT;
5059 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5061 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5062 nil. */
5064 static void
5065 x_scroll_bar_remove (bar)
5066 struct scroll_bar *bar;
5068 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5069 BLOCK_INPUT;
5071 #ifdef USE_TOOLKIT_SCROLL_BARS
5072 #ifdef USE_GTK
5073 xg_remove_scroll_bar (f, bar->x_window);
5074 #else /* not USE_GTK */
5075 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5076 #endif /* not USE_GTK */
5077 #else
5078 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5079 #endif
5081 /* Disassociate this scroll bar from its window. */
5082 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5084 UNBLOCK_INPUT;
5088 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5089 that we are displaying PORTION characters out of a total of WHOLE
5090 characters, starting at POSITION. If WINDOW has no scroll bar,
5091 create one. */
5093 static void
5094 XTset_vertical_scroll_bar (w, portion, whole, position)
5095 struct window *w;
5096 int portion, whole, position;
5098 struct frame *f = XFRAME (w->frame);
5099 struct scroll_bar *bar;
5100 int top, height, left, sb_left, width, sb_width;
5101 int window_y, window_height;
5102 #ifdef USE_TOOLKIT_SCROLL_BARS
5103 int fringe_extended_p;
5104 #endif
5106 /* Get window dimensions. */
5107 window_box (w, -1, 0, &window_y, 0, &window_height);
5108 top = window_y;
5109 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5110 height = window_height;
5112 /* Compute the left edge of the scroll bar area. */
5113 left = WINDOW_SCROLL_BAR_AREA_X (w);
5115 /* Compute the width of the scroll bar which might be less than
5116 the width of the area reserved for the scroll bar. */
5117 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5118 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5119 else
5120 sb_width = width;
5122 /* Compute the left edge of the scroll bar. */
5123 #ifdef USE_TOOLKIT_SCROLL_BARS
5124 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5125 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5126 else
5127 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5128 #else
5129 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5130 sb_left = left + width - sb_width;
5131 else
5132 sb_left = left;
5133 #endif
5135 #ifdef USE_TOOLKIT_SCROLL_BARS
5136 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5137 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5138 && WINDOW_LEFT_FRINGE_WIDTH (w)
5139 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5140 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5141 else
5142 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5143 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5144 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5145 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5146 #endif
5148 /* Does the scroll bar exist yet? */
5149 if (NILP (w->vertical_scroll_bar))
5151 if (width > 0 && height > 0)
5153 BLOCK_INPUT;
5154 #ifdef USE_TOOLKIT_SCROLL_BARS
5155 if (fringe_extended_p)
5156 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5157 sb_left, top, sb_width, height, False);
5158 else
5159 #endif
5160 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5161 left, top, width, height, False);
5162 UNBLOCK_INPUT;
5165 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5167 else
5169 /* It may just need to be moved and resized. */
5170 unsigned int mask = 0;
5172 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5174 BLOCK_INPUT;
5176 if (sb_left != bar->left)
5177 mask |= CWX;
5178 if (top != bar->top)
5179 mask |= CWY;
5180 if (sb_width != bar->width)
5181 mask |= CWWidth;
5182 if (height != bar->height)
5183 mask |= CWHeight;
5185 #ifdef USE_TOOLKIT_SCROLL_BARS
5187 /* Move/size the scroll bar widget. */
5188 if (mask || bar->fringe_extended_p != fringe_extended_p)
5190 /* Since toolkit scroll bars are smaller than the space reserved
5191 for them on the frame, we have to clear "under" them. */
5192 if (width > 0 && height > 0)
5194 if (fringe_extended_p)
5195 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5196 sb_left, top, sb_width, height, False);
5197 else
5198 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5199 left, top, width, height, False);
5201 #ifdef USE_GTK
5202 xg_update_scrollbar_pos (f,
5203 bar->x_window,
5204 top,
5205 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5206 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5207 max (height, 1));
5208 #else /* not USE_GTK */
5209 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5210 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5211 top,
5212 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5213 max (height, 1), 0);
5214 #endif /* not USE_GTK */
5216 #else /* not USE_TOOLKIT_SCROLL_BARS */
5218 /* Clear areas not covered by the scroll bar because of
5219 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5220 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5222 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5223 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5224 height, False);
5225 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5226 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5227 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5228 height, False);
5231 /* Clear areas not covered by the scroll bar because it's not as
5232 wide as the area reserved for it. This makes sure a
5233 previous mode line display is cleared after C-x 2 C-x 1, for
5234 example. */
5236 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5237 int rest = area_width - sb_width;
5238 if (rest > 0 && height > 0)
5240 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5241 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5242 left + area_width - rest, top,
5243 rest, height, False);
5244 else
5245 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5246 left, top, rest, height, False);
5250 /* Move/size the scroll bar window. */
5251 if (mask)
5253 XWindowChanges wc;
5255 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5256 wc.y = top;
5257 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5258 wc.height = height;
5259 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5260 mask, &wc);
5263 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5265 /* Remember new settings. */
5266 bar->left = sb_left;
5267 bar->top = top;
5268 bar->width = sb_width;
5269 bar->height = height;
5271 UNBLOCK_INPUT;
5274 #ifdef USE_TOOLKIT_SCROLL_BARS
5275 bar->fringe_extended_p = fringe_extended_p;
5277 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5278 #else /* not USE_TOOLKIT_SCROLL_BARS */
5279 /* Set the scroll bar's current state, unless we're currently being
5280 dragged. */
5281 if (NILP (bar->dragging))
5283 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5285 if (whole == 0)
5286 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5287 else
5289 int start = ((double) position * top_range) / whole;
5290 int end = ((double) (position + portion) * top_range) / whole;
5291 x_scroll_bar_set_handle (bar, start, end, 0);
5294 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5296 XSETVECTOR (w->vertical_scroll_bar, bar);
5300 /* The following three hooks are used when we're doing a thorough
5301 redisplay of the frame. We don't explicitly know which scroll bars
5302 are going to be deleted, because keeping track of when windows go
5303 away is a real pain - "Can you say set-window-configuration, boys
5304 and girls?" Instead, we just assert at the beginning of redisplay
5305 that *all* scroll bars are to be removed, and then save a scroll bar
5306 from the fiery pit when we actually redisplay its window. */
5308 /* Arrange for all scroll bars on FRAME to be removed at the next call
5309 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5310 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5312 static void
5313 XTcondemn_scroll_bars (frame)
5314 FRAME_PTR frame;
5316 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5317 while (! NILP (FRAME_SCROLL_BARS (frame)))
5319 Lisp_Object bar;
5320 bar = FRAME_SCROLL_BARS (frame);
5321 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5322 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5323 XSCROLL_BAR (bar)->prev = Qnil;
5324 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5325 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5326 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5331 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5332 Note that WINDOW isn't necessarily condemned at all. */
5334 static void
5335 XTredeem_scroll_bar (window)
5336 struct window *window;
5338 struct scroll_bar *bar;
5339 struct frame *f;
5341 /* We can't redeem this window's scroll bar if it doesn't have one. */
5342 if (NILP (window->vertical_scroll_bar))
5343 abort ();
5345 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5347 /* Unlink it from the condemned list. */
5348 f = XFRAME (WINDOW_FRAME (window));
5349 if (NILP (bar->prev))
5351 /* If the prev pointer is nil, it must be the first in one of
5352 the lists. */
5353 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5354 /* It's not condemned. Everything's fine. */
5355 return;
5356 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5357 window->vertical_scroll_bar))
5358 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5359 else
5360 /* If its prev pointer is nil, it must be at the front of
5361 one or the other! */
5362 abort ();
5364 else
5365 XSCROLL_BAR (bar->prev)->next = bar->next;
5367 if (! NILP (bar->next))
5368 XSCROLL_BAR (bar->next)->prev = bar->prev;
5370 bar->next = FRAME_SCROLL_BARS (f);
5371 bar->prev = Qnil;
5372 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5373 if (! NILP (bar->next))
5374 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5377 /* Remove all scroll bars on FRAME that haven't been saved since the
5378 last call to `*condemn_scroll_bars_hook'. */
5380 static void
5381 XTjudge_scroll_bars (f)
5382 FRAME_PTR f;
5384 Lisp_Object bar, next;
5386 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5388 /* Clear out the condemned list now so we won't try to process any
5389 more events on the hapless scroll bars. */
5390 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5392 for (; ! NILP (bar); bar = next)
5394 struct scroll_bar *b = XSCROLL_BAR (bar);
5396 x_scroll_bar_remove (b);
5398 next = b->next;
5399 b->next = b->prev = Qnil;
5402 /* Now there should be no references to the condemned scroll bars,
5403 and they should get garbage-collected. */
5407 #ifndef USE_TOOLKIT_SCROLL_BARS
5408 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5409 is a no-op when using toolkit scroll bars.
5411 This may be called from a signal handler, so we have to ignore GC
5412 mark bits. */
5414 static void
5415 x_scroll_bar_expose (bar, event)
5416 struct scroll_bar *bar;
5417 XEvent *event;
5419 Window w = bar->x_window;
5420 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5421 GC gc = f->output_data.x->normal_gc;
5422 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5424 BLOCK_INPUT;
5426 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5428 /* Switch to scroll bar foreground color. */
5429 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5430 XSetForeground (FRAME_X_DISPLAY (f), gc,
5431 f->output_data.x->scroll_bar_foreground_pixel);
5433 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5434 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5436 /* x, y, width, height */
5437 0, 0,
5438 bar->width - 1 - width_trim - width_trim,
5439 bar->height - 1);
5441 /* Restore the foreground color of the GC if we changed it above. */
5442 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5443 XSetForeground (FRAME_X_DISPLAY (f), gc,
5444 FRAME_FOREGROUND_PIXEL (f));
5446 UNBLOCK_INPUT;
5449 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5451 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5452 is set to something other than NO_EVENT, it is enqueued.
5454 This may be called from a signal handler, so we have to ignore GC
5455 mark bits. */
5458 static void
5459 x_scroll_bar_handle_click (bar, event, emacs_event)
5460 struct scroll_bar *bar;
5461 XEvent *event;
5462 struct input_event *emacs_event;
5464 if (! WINDOWP (bar->window))
5465 abort ();
5467 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5468 emacs_event->code = event->xbutton.button - Button1;
5469 emacs_event->modifiers
5470 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5471 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5472 event->xbutton.state)
5473 | (event->type == ButtonRelease
5474 ? up_modifier
5475 : down_modifier));
5476 emacs_event->frame_or_window = bar->window;
5477 emacs_event->arg = Qnil;
5478 emacs_event->timestamp = event->xbutton.time;
5480 #if 0
5481 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5482 int internal_height
5483 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5484 #endif
5485 int top_range
5486 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5487 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5489 if (y < 0) y = 0;
5490 if (y > top_range) y = top_range;
5492 if (y < bar->start)
5493 emacs_event->part = scroll_bar_above_handle;
5494 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5495 emacs_event->part = scroll_bar_handle;
5496 else
5497 emacs_event->part = scroll_bar_below_handle;
5499 /* Just because the user has clicked on the handle doesn't mean
5500 they want to drag it. Lisp code needs to be able to decide
5501 whether or not we're dragging. */
5502 #if 0
5503 /* If the user has just clicked on the handle, record where they're
5504 holding it. */
5505 if (event->type == ButtonPress
5506 && emacs_event->part == scroll_bar_handle)
5507 XSETINT (bar->dragging, y - bar->start);
5508 #endif
5510 #ifndef USE_TOOLKIT_SCROLL_BARS
5511 /* If the user has released the handle, set it to its final position. */
5512 if (event->type == ButtonRelease
5513 && ! NILP (bar->dragging))
5515 int new_start = y - XINT (bar->dragging);
5516 int new_end = new_start + bar->end - bar->start;
5518 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5519 bar->dragging = Qnil;
5521 #endif
5523 /* Same deal here as the other #if 0. */
5524 #if 0
5525 /* Clicks on the handle are always reported as occurring at the top of
5526 the handle. */
5527 if (emacs_event->part == scroll_bar_handle)
5528 emacs_event->x = bar->start;
5529 else
5530 XSETINT (emacs_event->x, y);
5531 #else
5532 XSETINT (emacs_event->x, y);
5533 #endif
5535 XSETINT (emacs_event->y, top_range);
5539 #ifndef USE_TOOLKIT_SCROLL_BARS
5541 /* Handle some mouse motion while someone is dragging the scroll bar.
5543 This may be called from a signal handler, so we have to ignore GC
5544 mark bits. */
5546 static void
5547 x_scroll_bar_note_movement (bar, event)
5548 struct scroll_bar *bar;
5549 XEvent *event;
5551 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5553 last_mouse_movement_time = event->xmotion.time;
5555 f->mouse_moved = 1;
5556 XSETVECTOR (last_mouse_scroll_bar, bar);
5558 /* If we're dragging the bar, display it. */
5559 if (! NILP (bar->dragging))
5561 /* Where should the handle be now? */
5562 int new_start = event->xmotion.y - XINT (bar->dragging);
5564 if (new_start != bar->start)
5566 int new_end = new_start + bar->end - bar->start;
5568 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5573 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5575 /* Return information to the user about the current position of the mouse
5576 on the scroll bar. */
5578 static void
5579 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5580 FRAME_PTR *fp;
5581 Lisp_Object *bar_window;
5582 enum scroll_bar_part *part;
5583 Lisp_Object *x, *y;
5584 unsigned long *time;
5586 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5587 Window w = bar->x_window;
5588 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5589 int win_x, win_y;
5590 Window dummy_window;
5591 int dummy_coord;
5592 unsigned int dummy_mask;
5594 BLOCK_INPUT;
5596 /* Get the mouse's position relative to the scroll bar window, and
5597 report that. */
5598 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5600 /* Root, child, root x and root y. */
5601 &dummy_window, &dummy_window,
5602 &dummy_coord, &dummy_coord,
5604 /* Position relative to scroll bar. */
5605 &win_x, &win_y,
5607 /* Mouse buttons and modifier keys. */
5608 &dummy_mask))
5610 else
5612 #if 0
5613 int inside_height
5614 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5615 #endif
5616 int top_range
5617 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5619 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5621 if (! NILP (bar->dragging))
5622 win_y -= XINT (bar->dragging);
5624 if (win_y < 0)
5625 win_y = 0;
5626 if (win_y > top_range)
5627 win_y = top_range;
5629 *fp = f;
5630 *bar_window = bar->window;
5632 if (! NILP (bar->dragging))
5633 *part = scroll_bar_handle;
5634 else if (win_y < bar->start)
5635 *part = scroll_bar_above_handle;
5636 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5637 *part = scroll_bar_handle;
5638 else
5639 *part = scroll_bar_below_handle;
5641 XSETINT (*x, win_y);
5642 XSETINT (*y, top_range);
5644 f->mouse_moved = 0;
5645 last_mouse_scroll_bar = Qnil;
5648 *time = last_mouse_movement_time;
5650 UNBLOCK_INPUT;
5654 /* The screen has been cleared so we may have changed foreground or
5655 background colors, and the scroll bars may need to be redrawn.
5656 Clear out the scroll bars, and ask for expose events, so we can
5657 redraw them. */
5659 void
5660 x_scroll_bar_clear (f)
5661 FRAME_PTR f;
5663 #ifndef USE_TOOLKIT_SCROLL_BARS
5664 Lisp_Object bar;
5666 /* We can have scroll bars even if this is 0,
5667 if we just turned off scroll bar mode.
5668 But in that case we should not clear them. */
5669 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5670 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5671 bar = XSCROLL_BAR (bar)->next)
5672 XClearArea (FRAME_X_DISPLAY (f),
5673 XSCROLL_BAR (bar)->x_window,
5674 0, 0, 0, 0, True);
5675 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5679 /* The main X event-reading loop - XTread_socket. */
5681 #if 0
5682 /* Time stamp of enter window event. This is only used by XTread_socket,
5683 but we have to put it out here, since static variables within functions
5684 sometimes don't work. */
5686 static Time enter_timestamp;
5687 #endif
5689 /* This holds the state XLookupString needs to implement dead keys
5690 and other tricks known as "compose processing". _X Window System_
5691 says that a portable program can't use this, but Stephen Gildea assures
5692 me that letting the compiler initialize it to zeros will work okay.
5694 This must be defined outside of XTread_socket, for the same reasons
5695 given for enter_timestamp, above. */
5697 static XComposeStatus compose_status;
5699 /* Record the last 100 characters stored
5700 to help debug the loss-of-chars-during-GC problem. */
5702 static int temp_index;
5703 static short temp_buffer[100];
5705 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5706 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5707 temp_index = 0; \
5708 temp_buffer[temp_index++] = (keysym)
5710 /* Set this to nonzero to fake an "X I/O error"
5711 on a particular display. */
5713 struct x_display_info *XTread_socket_fake_io_error;
5715 /* When we find no input here, we occasionally do a no-op command
5716 to verify that the X server is still running and we can still talk with it.
5717 We try all the open displays, one by one.
5718 This variable is used for cycling thru the displays. */
5720 static struct x_display_info *next_noop_dpyinfo;
5722 #define SET_SAVED_MENU_EVENT(size) \
5723 do \
5725 if (f->output_data.x->saved_menu_event == 0) \
5726 f->output_data.x->saved_menu_event \
5727 = (XEvent *) xmalloc (sizeof (XEvent)); \
5728 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5729 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5730 XSETFRAME (inev.ie.frame_or_window, f); \
5732 while (0)
5734 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5735 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5738 enum
5740 X_EVENT_NORMAL,
5741 X_EVENT_GOTO_OUT,
5742 X_EVENT_DROP
5745 /* Filter events for the current X input method.
5746 DPYINFO is the display this event is for.
5747 EVENT is the X event to filter.
5749 Returns non-zero if the event was filtered, caller shall not process
5750 this event further.
5751 Returns zero if event is wasn't filtered. */
5753 #ifdef HAVE_X_I18N
5754 static int
5755 x_filter_event (dpyinfo, event)
5756 struct x_display_info *dpyinfo;
5757 XEvent *event;
5759 /* XFilterEvent returns non-zero if the input method has
5760 consumed the event. We pass the frame's X window to
5761 XFilterEvent because that's the one for which the IC
5762 was created. */
5764 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5765 event->xclient.window);
5767 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5769 #endif
5771 #ifdef USE_GTK
5772 static int current_count;
5773 static int current_finish;
5774 static struct input_event *current_hold_quit;
5776 /* This is the filter function invoked by the GTK event loop.
5777 It is invoked before the XEvent is translated to a GdkEvent,
5778 so we have a chance to act on the event before GTK. */
5779 static GdkFilterReturn
5780 event_handler_gdk (gxev, ev, data)
5781 GdkXEvent *gxev;
5782 GdkEvent *ev;
5783 gpointer data;
5785 XEvent *xev = (XEvent *) gxev;
5787 if (current_count >= 0)
5789 struct x_display_info *dpyinfo;
5791 dpyinfo = x_display_info_for_display (xev->xany.display);
5793 #ifdef HAVE_X_I18N
5794 /* Filter events for the current X input method.
5795 GTK calls XFilterEvent but not for key press and release,
5796 so we do it here. */
5797 if (xev->type == KeyPress || xev->type == KeyRelease)
5798 if (dpyinfo && x_filter_event (dpyinfo, xev))
5799 return GDK_FILTER_REMOVE;
5800 #endif
5802 if (! dpyinfo)
5803 current_finish = X_EVENT_NORMAL;
5804 else
5806 current_count +=
5807 handle_one_xevent (dpyinfo, xev, &current_finish,
5808 current_hold_quit);
5811 else
5812 current_finish = x_dispatch_event (xev, xev->xany.display);
5814 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5815 return GDK_FILTER_REMOVE;
5817 return GDK_FILTER_CONTINUE;
5819 #endif /* USE_GTK */
5822 /* Handles the XEvent EVENT on display DPYINFO.
5824 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5825 *FINISH is zero if caller should continue reading events.
5826 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5828 We return the number of characters stored into the buffer. */
5830 static int
5831 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5832 struct x_display_info *dpyinfo;
5833 XEvent *eventp;
5834 int *finish;
5835 struct input_event *hold_quit;
5837 union {
5838 struct input_event ie;
5839 struct selection_input_event sie;
5840 } inev;
5841 int count = 0;
5842 int do_help = 0;
5843 int nbytes = 0;
5844 struct frame *f = NULL;
5845 struct coding_system coding;
5846 XEvent event = *eventp;
5848 *finish = X_EVENT_NORMAL;
5850 EVENT_INIT (inev.ie);
5851 inev.ie.kind = NO_EVENT;
5852 inev.ie.arg = Qnil;
5854 switch (event.type)
5856 case ClientMessage:
5858 if (event.xclient.message_type
5859 == dpyinfo->Xatom_wm_protocols
5860 && event.xclient.format == 32)
5862 if (event.xclient.data.l[0]
5863 == dpyinfo->Xatom_wm_take_focus)
5865 /* Use x_any_window_to_frame because this
5866 could be the shell widget window
5867 if the frame has no title bar. */
5868 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5869 #ifdef HAVE_X_I18N
5870 /* Not quite sure this is needed -pd */
5871 if (f && FRAME_XIC (f))
5872 XSetICFocus (FRAME_XIC (f));
5873 #endif
5874 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5875 instructs the WM to set the input focus automatically for
5876 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5877 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5878 it has set the focus. So, XSetInputFocus below is not
5879 needed.
5881 The call to XSetInputFocus below has also caused trouble. In
5882 cases where the XSetInputFocus done by the WM and the one
5883 below are temporally close (on a fast machine), the call
5884 below can generate additional FocusIn events which confuse
5885 Emacs. */
5887 /* Since we set WM_TAKE_FOCUS, we must call
5888 XSetInputFocus explicitly. But not if f is null,
5889 since that might be an event for a deleted frame. */
5890 if (f)
5892 Display *d = event.xclient.display;
5893 /* Catch and ignore errors, in case window has been
5894 iconified by a window manager such as GWM. */
5895 x_catch_errors (d);
5896 XSetInputFocus (d, event.xclient.window,
5897 /* The ICCCM says this is
5898 the only valid choice. */
5899 RevertToParent,
5900 event.xclient.data.l[1]);
5901 /* This is needed to detect the error
5902 if there is an error. */
5903 XSync (d, False);
5904 x_uncatch_errors ();
5906 /* Not certain about handling scroll bars here */
5907 #endif /* 0 */
5908 goto done;
5911 if (event.xclient.data.l[0]
5912 == dpyinfo->Xatom_wm_save_yourself)
5914 /* Save state modify the WM_COMMAND property to
5915 something which can reinstate us. This notifies
5916 the session manager, who's looking for such a
5917 PropertyNotify. Can restart processing when
5918 a keyboard or mouse event arrives. */
5919 /* If we have a session manager, don't set this.
5920 KDE will then start two Emacsen, one for the
5921 session manager and one for this. */
5922 #ifdef HAVE_X_SM
5923 if (! x_session_have_connection ())
5924 #endif
5926 f = x_top_window_to_frame (dpyinfo,
5927 event.xclient.window);
5928 /* This is just so we only give real data once
5929 for a single Emacs process. */
5930 if (f == SELECTED_FRAME ())
5931 XSetCommand (FRAME_X_DISPLAY (f),
5932 event.xclient.window,
5933 initial_argv, initial_argc);
5934 else if (f)
5935 XSetCommand (FRAME_X_DISPLAY (f),
5936 event.xclient.window,
5937 0, 0);
5939 goto done;
5942 if (event.xclient.data.l[0]
5943 == dpyinfo->Xatom_wm_delete_window)
5945 f = x_any_window_to_frame (dpyinfo,
5946 event.xclient.window);
5947 if (!f)
5948 goto OTHER; /* May be a dialog that is to be removed */
5950 inev.ie.kind = DELETE_WINDOW_EVENT;
5951 XSETFRAME (inev.ie.frame_or_window, f);
5952 goto done;
5955 goto done;
5958 if (event.xclient.message_type
5959 == dpyinfo->Xatom_wm_configure_denied)
5961 goto done;
5964 if (event.xclient.message_type
5965 == dpyinfo->Xatom_wm_window_moved)
5967 int new_x, new_y;
5968 f = x_window_to_frame (dpyinfo, event.xclient.window);
5970 new_x = event.xclient.data.s[0];
5971 new_y = event.xclient.data.s[1];
5973 if (f)
5975 f->left_pos = new_x;
5976 f->top_pos = new_y;
5978 goto done;
5981 #ifdef HACK_EDITRES
5982 if (event.xclient.message_type
5983 == dpyinfo->Xatom_editres)
5985 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5986 if (f)
5987 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5988 &event, NULL);
5989 goto done;
5991 #endif /* HACK_EDITRES */
5993 if ((event.xclient.message_type
5994 == dpyinfo->Xatom_DONE)
5995 || (event.xclient.message_type
5996 == dpyinfo->Xatom_PAGE))
5998 /* Ghostview job completed. Kill it. We could
5999 reply with "Next" if we received "Page", but we
6000 currently never do because we are interested in
6001 images, only, which should have 1 page. */
6002 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
6003 f = x_window_to_frame (dpyinfo, event.xclient.window);
6004 if (!f)
6005 goto OTHER;
6006 x_kill_gs_process (pixmap, f);
6007 expose_frame (f, 0, 0, 0, 0);
6008 goto done;
6011 #ifdef USE_TOOLKIT_SCROLL_BARS
6012 /* Scroll bar callbacks send a ClientMessage from which
6013 we construct an input_event. */
6014 if (event.xclient.message_type
6015 == dpyinfo->Xatom_Scrollbar)
6017 x_scroll_bar_to_input_event (&event, &inev.ie);
6018 *finish = X_EVENT_GOTO_OUT;
6019 goto done;
6021 #endif /* USE_TOOLKIT_SCROLL_BARS */
6023 /* XEmbed messages from the embedder (if any). */
6024 if (event.xclient.message_type
6025 == dpyinfo->Xatom_XEMBED)
6027 enum xembed_message msg = event.xclient.data.l[1];
6028 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6029 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6031 *finish = X_EVENT_GOTO_OUT;
6032 goto done;
6035 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6036 if (!f)
6037 goto OTHER;
6038 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
6039 *finish = X_EVENT_DROP;
6041 break;
6043 case SelectionNotify:
6044 last_user_time = event.xselection.time;
6045 #ifdef USE_X_TOOLKIT
6046 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6047 goto OTHER;
6048 #endif /* not USE_X_TOOLKIT */
6049 x_handle_selection_notify (&event.xselection);
6050 break;
6052 case SelectionClear: /* Someone has grabbed ownership. */
6053 last_user_time = event.xselectionclear.time;
6054 #ifdef USE_X_TOOLKIT
6055 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6056 goto OTHER;
6057 #endif /* USE_X_TOOLKIT */
6059 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
6061 inev.ie.kind = SELECTION_CLEAR_EVENT;
6062 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6063 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6064 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6065 inev.ie.frame_or_window = Qnil;
6067 break;
6069 case SelectionRequest: /* Someone wants our selection. */
6070 last_user_time = event.xselectionrequest.time;
6071 #ifdef USE_X_TOOLKIT
6072 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6073 goto OTHER;
6074 #endif /* USE_X_TOOLKIT */
6076 XSelectionRequestEvent *eventp
6077 = (XSelectionRequestEvent *) &event;
6079 inev.ie.kind = SELECTION_REQUEST_EVENT;
6080 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6081 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6082 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6083 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6084 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6085 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6086 inev.ie.frame_or_window = Qnil;
6088 break;
6090 case PropertyNotify:
6091 last_user_time = event.xproperty.time;
6092 #if 0 /* This is plain wrong. In the case that we are waiting for a
6093 PropertyNotify used as an ACK in incremental selection
6094 transfer, the property will be on the receiver's window. */
6095 #if defined USE_X_TOOLKIT
6096 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
6097 goto OTHER;
6098 #endif
6099 #endif
6100 f = x_top_window_to_frame (dpyinfo, event.xproperty.window);
6101 if (f && event.xproperty.atom == dpyinfo->Xatom_net_wm_state)
6102 x_handle_net_wm_state (f, &event.xproperty);
6104 x_handle_property_notify (&event.xproperty);
6105 goto OTHER;
6107 case ReparentNotify:
6108 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6109 if (f)
6111 int x, y;
6112 f->output_data.x->parent_desc = event.xreparent.parent;
6113 x_real_positions (f, &x, &y);
6114 f->left_pos = x;
6115 f->top_pos = y;
6117 /* Perhaps reparented due to a WM restart. Reset this. */
6118 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6119 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6121 goto OTHER;
6123 case Expose:
6124 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6125 if (f)
6127 x_check_fullscreen (f);
6129 #ifdef USE_GTK
6130 /* This seems to be needed for GTK 2.6. */
6131 x_clear_area (event.xexpose.display,
6132 event.xexpose.window,
6133 event.xexpose.x, event.xexpose.y,
6134 event.xexpose.width, event.xexpose.height,
6135 FALSE);
6136 #endif
6137 if (f->async_visible == 0)
6139 f->async_visible = 1;
6140 f->async_iconified = 0;
6141 f->output_data.x->has_been_visible = 1;
6142 SET_FRAME_GARBAGED (f);
6144 else
6145 expose_frame (f,
6146 event.xexpose.x, event.xexpose.y,
6147 event.xexpose.width, event.xexpose.height);
6149 else
6151 #ifndef USE_TOOLKIT_SCROLL_BARS
6152 struct scroll_bar *bar;
6153 #endif
6154 #if defined USE_LUCID
6155 /* Submenus of the Lucid menu bar aren't widgets
6156 themselves, so there's no way to dispatch events
6157 to them. Recognize this case separately. */
6159 Widget widget
6160 = x_window_to_menu_bar (event.xexpose.window);
6161 if (widget)
6162 xlwmenu_redisplay (widget);
6164 #endif /* USE_LUCID */
6166 #ifdef USE_TOOLKIT_SCROLL_BARS
6167 /* Dispatch event to the widget. */
6168 goto OTHER;
6169 #else /* not USE_TOOLKIT_SCROLL_BARS */
6170 bar = x_window_to_scroll_bar (event.xexpose.display,
6171 event.xexpose.window);
6173 if (bar)
6174 x_scroll_bar_expose (bar, &event);
6175 #ifdef USE_X_TOOLKIT
6176 else
6177 goto OTHER;
6178 #endif /* USE_X_TOOLKIT */
6179 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6181 break;
6183 case GraphicsExpose: /* This occurs when an XCopyArea's
6184 source area was obscured or not
6185 available. */
6186 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6187 if (f)
6189 expose_frame (f,
6190 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6191 event.xgraphicsexpose.width,
6192 event.xgraphicsexpose.height);
6194 #ifdef USE_X_TOOLKIT
6195 else
6196 goto OTHER;
6197 #endif /* USE_X_TOOLKIT */
6198 break;
6200 case NoExpose: /* This occurs when an XCopyArea's
6201 source area was completely
6202 available. */
6203 break;
6205 case UnmapNotify:
6206 /* Redo the mouse-highlight after the tooltip has gone. */
6207 if (event.xmap.window == tip_window)
6209 tip_window = 0;
6210 redo_mouse_highlight ();
6213 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6214 if (f) /* F may no longer exist if
6215 the frame was deleted. */
6217 /* While a frame is unmapped, display generation is
6218 disabled; you don't want to spend time updating a
6219 display that won't ever be seen. */
6220 f->async_visible = 0;
6221 /* We can't distinguish, from the event, whether the window
6222 has become iconified or invisible. So assume, if it
6223 was previously visible, than now it is iconified.
6224 But x_make_frame_invisible clears both
6225 the visible flag and the iconified flag;
6226 and that way, we know the window is not iconified now. */
6227 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6229 f->async_iconified = 1;
6231 inev.ie.kind = ICONIFY_EVENT;
6232 XSETFRAME (inev.ie.frame_or_window, f);
6235 goto OTHER;
6237 case MapNotify:
6238 if (event.xmap.window == tip_window)
6239 /* The tooltip has been drawn already. Avoid
6240 the SET_FRAME_GARBAGED below. */
6241 goto OTHER;
6243 /* We use x_top_window_to_frame because map events can
6244 come for sub-windows and they don't mean that the
6245 frame is visible. */
6246 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6247 if (f)
6249 /* wait_reading_process_output will notice this and update
6250 the frame's display structures.
6251 If we where iconified, we should not set garbaged,
6252 because that stops redrawing on Expose events. This looks
6253 bad if we are called from a recursive event loop
6254 (x_dispatch_event), for example when a dialog is up. */
6255 if (! f->async_iconified)
6256 SET_FRAME_GARBAGED (f);
6258 f->async_visible = 1;
6259 f->async_iconified = 0;
6260 f->output_data.x->has_been_visible = 1;
6262 if (f->iconified)
6264 inev.ie.kind = DEICONIFY_EVENT;
6265 XSETFRAME (inev.ie.frame_or_window, f);
6267 else if (! NILP (Vframe_list)
6268 && ! NILP (XCDR (Vframe_list)))
6269 /* Force a redisplay sooner or later
6270 to update the frame titles
6271 in case this is the second frame. */
6272 record_asynch_buffer_change ();
6274 goto OTHER;
6276 case KeyPress:
6278 last_user_time = event.xkey.time;
6279 ignore_next_mouse_click_timeout = 0;
6281 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6282 /* Dispatch KeyPress events when in menu. */
6283 if (popup_activated ())
6284 goto OTHER;
6285 #endif
6287 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6289 /* If mouse-highlight is an integer, input clears out
6290 mouse highlighting. */
6291 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6292 && (f == 0
6293 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6295 clear_mouse_face (dpyinfo);
6296 dpyinfo->mouse_face_hidden = 1;
6299 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6300 if (f == 0)
6302 /* Scroll bars consume key events, but we want
6303 the keys to go to the scroll bar's frame. */
6304 Widget widget = XtWindowToWidget (dpyinfo->display,
6305 event.xkey.window);
6306 if (widget && XmIsScrollBar (widget))
6308 widget = XtParent (widget);
6309 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6312 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6314 if (f != 0)
6316 KeySym keysym, orig_keysym;
6317 /* al%imercury@uunet.uu.net says that making this 81
6318 instead of 80 fixed a bug whereby meta chars made
6319 his Emacs hang.
6321 It seems that some version of XmbLookupString has
6322 a bug of not returning XBufferOverflow in
6323 status_return even if the input is too long to
6324 fit in 81 bytes. So, we must prepare sufficient
6325 bytes for copy_buffer. 513 bytes (256 chars for
6326 two-byte character set) seems to be a fairly good
6327 approximation. -- 2000.8.10 handa@etl.go.jp */
6328 unsigned char copy_buffer[513];
6329 unsigned char *copy_bufptr = copy_buffer;
6330 int copy_bufsiz = sizeof (copy_buffer);
6331 int modifiers;
6332 Lisp_Object coding_system = Qlatin_1;
6333 Lisp_Object c;
6335 #ifdef USE_GTK
6336 /* Don't pass keys to GTK. A Tab will shift focus to the
6337 tool bar in GTK 2.4. Keys will still go to menus and
6338 dialogs because in that case popup_activated is TRUE
6339 (see above). */
6340 *finish = X_EVENT_DROP;
6341 #endif
6343 event.xkey.state
6344 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6345 extra_keyboard_modifiers);
6346 modifiers = event.xkey.state;
6348 /* This will have to go some day... */
6350 /* make_lispy_event turns chars into control chars.
6351 Don't do it here because XLookupString is too eager. */
6352 event.xkey.state &= ~ControlMask;
6353 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6354 | dpyinfo->super_mod_mask
6355 | dpyinfo->hyper_mod_mask
6356 | dpyinfo->alt_mod_mask);
6358 /* In case Meta is ComposeCharacter,
6359 clear its status. According to Markus Ehrnsperger
6360 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6361 this enables ComposeCharacter to work whether or
6362 not it is combined with Meta. */
6363 if (modifiers & dpyinfo->meta_mod_mask)
6364 bzero (&compose_status, sizeof (compose_status));
6366 #ifdef HAVE_X_I18N
6367 if (FRAME_XIC (f))
6369 Status status_return;
6371 coding_system = Vlocale_coding_system;
6372 nbytes = XmbLookupString (FRAME_XIC (f),
6373 &event.xkey, copy_bufptr,
6374 copy_bufsiz, &keysym,
6375 &status_return);
6376 if (status_return == XBufferOverflow)
6378 copy_bufsiz = nbytes + 1;
6379 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6380 nbytes = XmbLookupString (FRAME_XIC (f),
6381 &event.xkey, copy_bufptr,
6382 copy_bufsiz, &keysym,
6383 &status_return);
6385 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6386 #if 0 && defined X_HAVE_UTF8_STRING
6387 else if (status_return == XLookupKeySym)
6388 { /* Try again but with utf-8. */
6389 coding_system = Qutf_8;
6390 nbytes = Xutf8LookupString (FRAME_XIC (f),
6391 &event.xkey, copy_bufptr,
6392 copy_bufsiz, &keysym,
6393 &status_return);
6394 if (status_return == XBufferOverflow)
6396 copy_bufsiz = nbytes + 1;
6397 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6398 nbytes = Xutf8LookupString (FRAME_XIC (f),
6399 &event.xkey,
6400 copy_bufptr,
6401 copy_bufsiz, &keysym,
6402 &status_return);
6405 #endif
6407 if (status_return == XLookupNone)
6408 break;
6409 else if (status_return == XLookupChars)
6411 keysym = NoSymbol;
6412 modifiers = 0;
6414 else if (status_return != XLookupKeySym
6415 && status_return != XLookupBoth)
6416 abort ();
6418 else
6419 nbytes = XLookupString (&event.xkey, copy_bufptr,
6420 copy_bufsiz, &keysym,
6421 &compose_status);
6422 #else
6423 nbytes = XLookupString (&event.xkey, copy_bufptr,
6424 copy_bufsiz, &keysym,
6425 &compose_status);
6426 #endif
6428 /* If not using XIM/XIC, and a compose sequence is in progress,
6429 we break here. Otherwise, chars_matched is always 0. */
6430 if (compose_status.chars_matched > 0 && nbytes == 0)
6431 break;
6433 bzero (&compose_status, sizeof (compose_status));
6434 orig_keysym = keysym;
6436 /* Common for all keysym input events. */
6437 XSETFRAME (inev.ie.frame_or_window, f);
6438 inev.ie.modifiers
6439 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6440 inev.ie.timestamp = event.xkey.time;
6442 /* First deal with keysyms which have defined
6443 translations to characters. */
6444 if (keysym >= 32 && keysym < 128)
6445 /* Avoid explicitly decoding each ASCII character. */
6447 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6448 inev.ie.code = keysym;
6449 goto done_keysym;
6452 /* Keysyms directly mapped to Unicode characters. */
6453 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6455 if (keysym < 0x01000080)
6456 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6457 else
6458 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6459 inev.ie.code = keysym & 0xFFFFFF;
6460 goto done_keysym;
6463 /* Now non-ASCII. */
6464 if (HASH_TABLE_P (Vx_keysym_table)
6465 && (NATNUMP (c = Fgethash (make_number (keysym),
6466 Vx_keysym_table,
6467 Qnil))))
6469 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6470 ? ASCII_KEYSTROKE_EVENT
6471 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6472 inev.ie.code = XFASTINT (c);
6473 goto done_keysym;
6476 /* Random non-modifier sorts of keysyms. */
6477 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6478 || keysym == XK_Delete
6479 #ifdef XK_ISO_Left_Tab
6480 || (keysym >= XK_ISO_Left_Tab
6481 && keysym <= XK_ISO_Enter)
6482 #endif
6483 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6484 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6485 #ifdef HPUX
6486 /* This recognizes the "extended function
6487 keys". It seems there's no cleaner way.
6488 Test IsModifierKey to avoid handling
6489 mode_switch incorrectly. */
6490 || ((unsigned) (keysym) >= XK_Select
6491 && (unsigned)(keysym) < XK_KP_Space)
6492 #endif
6493 #ifdef XK_dead_circumflex
6494 || orig_keysym == XK_dead_circumflex
6495 #endif
6496 #ifdef XK_dead_grave
6497 || orig_keysym == XK_dead_grave
6498 #endif
6499 #ifdef XK_dead_tilde
6500 || orig_keysym == XK_dead_tilde
6501 #endif
6502 #ifdef XK_dead_diaeresis
6503 || orig_keysym == XK_dead_diaeresis
6504 #endif
6505 #ifdef XK_dead_macron
6506 || orig_keysym == XK_dead_macron
6507 #endif
6508 #ifdef XK_dead_degree
6509 || orig_keysym == XK_dead_degree
6510 #endif
6511 #ifdef XK_dead_acute
6512 || orig_keysym == XK_dead_acute
6513 #endif
6514 #ifdef XK_dead_cedilla
6515 || orig_keysym == XK_dead_cedilla
6516 #endif
6517 #ifdef XK_dead_breve
6518 || orig_keysym == XK_dead_breve
6519 #endif
6520 #ifdef XK_dead_ogonek
6521 || orig_keysym == XK_dead_ogonek
6522 #endif
6523 #ifdef XK_dead_caron
6524 || orig_keysym == XK_dead_caron
6525 #endif
6526 #ifdef XK_dead_doubleacute
6527 || orig_keysym == XK_dead_doubleacute
6528 #endif
6529 #ifdef XK_dead_abovedot
6530 || orig_keysym == XK_dead_abovedot
6531 #endif
6532 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6533 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6534 /* Any "vendor-specific" key is ok. */
6535 || (orig_keysym & (1 << 28))
6536 || (keysym != NoSymbol && nbytes == 0))
6537 && ! (IsModifierKey (orig_keysym)
6538 /* The symbols from XK_ISO_Lock
6539 to XK_ISO_Last_Group_Lock
6540 don't have real modifiers but
6541 should be treated similarly to
6542 Mode_switch by Emacs. */
6543 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6544 || ((unsigned)(orig_keysym)
6545 >= XK_ISO_Lock
6546 && (unsigned)(orig_keysym)
6547 <= XK_ISO_Last_Group_Lock)
6548 #endif
6551 STORE_KEYSYM_FOR_DEBUG (keysym);
6552 /* make_lispy_event will convert this to a symbolic
6553 key. */
6554 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6555 inev.ie.code = keysym;
6556 goto done_keysym;
6559 { /* Raw bytes, not keysym. */
6560 register int i;
6561 register int c;
6562 int nchars, len;
6564 for (i = 0, nchars = 0; i < nbytes; i++)
6566 if (ASCII_BYTE_P (copy_bufptr[i]))
6567 nchars++;
6568 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6571 if (nchars < nbytes)
6573 /* Decode the input data. */
6574 int require;
6575 unsigned char *p;
6577 /* The input should be decoded with `coding_system'
6578 which depends on which X*LookupString function
6579 we used just above and the locale. */
6580 setup_coding_system (coding_system, &coding);
6581 coding.src_multibyte = 0;
6582 coding.dst_multibyte = 1;
6583 /* The input is converted to events, thus we can't
6584 handle composition. Anyway, there's no XIM that
6585 gives us composition information. */
6586 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6588 require = MAX_MULTIBYTE_LENGTH * nbytes;
6589 coding.destination = alloca (require);
6590 coding.dst_bytes = require;
6591 coding.mode |= CODING_MODE_LAST_BLOCK;
6592 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6593 nbytes = coding.produced;
6594 nchars = coding.produced_char;
6595 copy_bufptr = coding.destination;
6598 /* Convert the input data to a sequence of
6599 character events. */
6600 for (i = 0; i < nbytes; i += len)
6602 if (nchars == nbytes)
6603 c = copy_bufptr[i], len = 1;
6604 else
6605 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6606 nbytes - i, len);
6607 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6608 ? ASCII_KEYSTROKE_EVENT
6609 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6610 inev.ie.code = c;
6611 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6614 count += nchars;
6616 inev.ie.kind = NO_EVENT; /* Already stored above. */
6618 if (keysym == NoSymbol)
6619 break;
6622 done_keysym:
6623 #ifdef HAVE_X_I18N
6624 /* Don't dispatch this event since XtDispatchEvent calls
6625 XFilterEvent, and two calls in a row may freeze the
6626 client. */
6627 break;
6628 #else
6629 goto OTHER;
6630 #endif
6632 case KeyRelease:
6633 last_user_time = event.xkey.time;
6634 #ifdef HAVE_X_I18N
6635 /* Don't dispatch this event since XtDispatchEvent calls
6636 XFilterEvent, and two calls in a row may freeze the
6637 client. */
6638 break;
6639 #else
6640 goto OTHER;
6641 #endif
6643 case EnterNotify:
6644 last_user_time = event.xcrossing.time;
6645 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6647 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6649 if (f && x_mouse_click_focus_ignore_position)
6650 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6652 #if 0
6653 if (event.xcrossing.focus)
6655 /* Avoid nasty pop/raise loops. */
6656 if (f && (!(f->auto_raise)
6657 || !(f->auto_lower)
6658 || (event.xcrossing.time - enter_timestamp) > 500))
6660 x_new_focus_frame (dpyinfo, f);
6661 enter_timestamp = event.xcrossing.time;
6664 else if (f == dpyinfo->x_focus_frame)
6665 x_new_focus_frame (dpyinfo, 0);
6666 #endif
6668 /* EnterNotify counts as mouse movement,
6669 so update things that depend on mouse position. */
6670 if (f && !f->output_data.x->hourglass_p)
6671 note_mouse_movement (f, &event.xmotion);
6672 #ifdef USE_GTK
6673 /* We may get an EnterNotify on the buttons in the toolbar. In that
6674 case we moved out of any highlighted area and need to note this. */
6675 if (!f && last_mouse_glyph_frame)
6676 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6677 #endif
6678 goto OTHER;
6680 case FocusIn:
6681 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6682 goto OTHER;
6684 case LeaveNotify:
6685 last_user_time = event.xcrossing.time;
6686 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6688 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6689 if (f)
6691 if (f == dpyinfo->mouse_face_mouse_frame)
6693 /* If we move outside the frame, then we're
6694 certainly no longer on any text in the frame. */
6695 clear_mouse_face (dpyinfo);
6696 dpyinfo->mouse_face_mouse_frame = 0;
6699 /* Generate a nil HELP_EVENT to cancel a help-echo.
6700 Do it only if there's something to cancel.
6701 Otherwise, the startup message is cleared when
6702 the mouse leaves the frame. */
6703 if (any_help_event_p)
6704 do_help = -1;
6706 #ifdef USE_GTK
6707 /* See comment in EnterNotify above */
6708 else if (last_mouse_glyph_frame)
6709 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6710 #endif
6711 goto OTHER;
6713 case FocusOut:
6714 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6715 goto OTHER;
6717 case MotionNotify:
6719 last_user_time = event.xmotion.time;
6720 previous_help_echo_string = help_echo_string;
6721 help_echo_string = Qnil;
6723 if (dpyinfo->grabbed && last_mouse_frame
6724 && FRAME_LIVE_P (last_mouse_frame))
6725 f = last_mouse_frame;
6726 else
6727 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6729 if (dpyinfo->mouse_face_hidden)
6731 dpyinfo->mouse_face_hidden = 0;
6732 clear_mouse_face (dpyinfo);
6735 if (f)
6738 /* Generate SELECT_WINDOW_EVENTs when needed.
6739 Don't let popup menus influence things (bug#1261). */
6740 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6742 Lisp_Object window;
6744 window = window_from_coordinates (f,
6745 event.xmotion.x, event.xmotion.y,
6746 0, 0, 0, 0);
6748 /* Window will be selected only when it is not selected now and
6749 last mouse movement event was not in it. Minibuffer window
6750 will be selected only when it is active. */
6751 if (WINDOWP (window)
6752 && !EQ (window, last_window)
6753 && !EQ (window, selected_window)
6754 /* For click-to-focus window managers
6755 create event iff we don't leave the
6756 selected frame. */
6757 && (focus_follows_mouse
6758 || (EQ (XWINDOW (window)->frame,
6759 XWINDOW (selected_window)->frame))))
6761 inev.ie.kind = SELECT_WINDOW_EVENT;
6762 inev.ie.frame_or_window = window;
6765 last_window=window;
6767 if (!note_mouse_movement (f, &event.xmotion))
6768 help_echo_string = previous_help_echo_string;
6770 else
6772 #ifndef USE_TOOLKIT_SCROLL_BARS
6773 struct scroll_bar *bar
6774 = x_window_to_scroll_bar (event.xmotion.display,
6775 event.xmotion.window);
6777 if (bar)
6778 x_scroll_bar_note_movement (bar, &event);
6779 #endif /* USE_TOOLKIT_SCROLL_BARS */
6781 /* If we move outside the frame, then we're
6782 certainly no longer on any text in the frame. */
6783 clear_mouse_face (dpyinfo);
6786 /* If the contents of the global variable help_echo_string
6787 has changed, generate a HELP_EVENT. */
6788 if (!NILP (help_echo_string)
6789 || !NILP (previous_help_echo_string))
6790 do_help = 1;
6791 goto OTHER;
6794 case ConfigureNotify:
6795 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6796 #ifdef USE_GTK
6797 if (!f
6798 && (f = x_any_window_to_frame (dpyinfo, event.xconfigure.window))
6799 && event.xconfigure.window == FRAME_X_WINDOW (f))
6801 xg_frame_resized (f, event.xconfigure.width,
6802 event.xconfigure.height);
6803 f = 0;
6805 #endif
6806 if (f)
6808 #ifndef USE_X_TOOLKIT
6809 #ifndef USE_GTK
6810 /* If there is a pending resize for fullscreen, don't
6811 do this one, the right one will come later.
6812 The toolkit version doesn't seem to need this, but we
6813 need to reset it below. */
6814 int dont_resize
6815 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6816 && f->new_text_cols != 0);
6817 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6818 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6820 if (dont_resize)
6821 goto OTHER;
6823 /* In the toolkit version, change_frame_size
6824 is called by the code that handles resizing
6825 of the EmacsFrame widget. */
6827 /* Even if the number of character rows and columns has
6828 not changed, the font size may have changed, so we need
6829 to check the pixel dimensions as well. */
6830 if (columns != FRAME_COLS (f)
6831 || rows != FRAME_LINES (f)
6832 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6833 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6835 change_frame_size (f, rows, columns, 0, 1, 0);
6836 SET_FRAME_GARBAGED (f);
6837 cancel_mouse_face (f);
6840 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6841 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6842 #endif /* not USE_GTK */
6843 #endif
6845 #ifdef USE_GTK
6846 /* GTK creates windows but doesn't map them.
6847 Only get real positions and check fullscreen when mapped. */
6848 if (FRAME_GTK_OUTER_WIDGET (f)
6849 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6850 #endif
6852 x_real_positions (f, &f->left_pos, &f->top_pos);
6854 if (f->want_fullscreen & FULLSCREEN_WAIT)
6855 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6858 #ifdef HAVE_X_I18N
6859 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6860 xic_set_statusarea (f);
6861 #endif
6863 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6865 /* Since the WM decorations come below top_pos now,
6866 we must put them below top_pos in the future. */
6867 f->win_gravity = NorthWestGravity;
6868 x_wm_set_size_hint (f, (long) 0, 0);
6871 goto OTHER;
6873 case ButtonRelease:
6874 case ButtonPress:
6876 /* If we decide we want to generate an event to be seen
6877 by the rest of Emacs, we put it here. */
6878 int tool_bar_p = 0;
6880 bzero (&compose_status, sizeof (compose_status));
6881 last_mouse_glyph_frame = 0;
6882 last_user_time = event.xbutton.time;
6884 if (dpyinfo->grabbed
6885 && last_mouse_frame
6886 && FRAME_LIVE_P (last_mouse_frame))
6887 f = last_mouse_frame;
6888 else
6889 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6891 if (f)
6893 /* Is this in the tool-bar? */
6894 if (WINDOWP (f->tool_bar_window)
6895 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6897 Lisp_Object window;
6898 int x = event.xbutton.x;
6899 int y = event.xbutton.y;
6901 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6902 tool_bar_p = EQ (window, f->tool_bar_window);
6904 if (tool_bar_p && event.xbutton.button < 4)
6906 handle_tool_bar_click (f, x, y,
6907 event.xbutton.type == ButtonPress,
6908 x_x_to_emacs_modifiers (dpyinfo,
6909 event.xbutton.state));
6913 if (!tool_bar_p)
6914 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6915 if (! popup_activated ())
6916 #endif
6918 if (ignore_next_mouse_click_timeout)
6920 if (event.type == ButtonPress
6921 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6923 ignore_next_mouse_click_timeout = 0;
6924 construct_mouse_click (&inev.ie, &event.xbutton, f);
6926 if (event.type == ButtonRelease)
6927 ignore_next_mouse_click_timeout = 0;
6929 else
6930 construct_mouse_click (&inev.ie, &event.xbutton, f);
6932 if (FRAME_X_EMBEDDED_P (f))
6933 xembed_send_message (f, event.xbutton.time,
6934 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6936 else
6938 struct scroll_bar *bar
6939 = x_window_to_scroll_bar (event.xbutton.display,
6940 event.xbutton.window);
6942 #ifdef USE_TOOLKIT_SCROLL_BARS
6943 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6944 scroll bars. */
6945 if (bar && event.xbutton.state & ControlMask)
6947 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6948 *finish = X_EVENT_DROP;
6950 #else /* not USE_TOOLKIT_SCROLL_BARS */
6951 if (bar)
6952 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6953 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6956 if (event.type == ButtonPress)
6958 dpyinfo->grabbed |= (1 << event.xbutton.button);
6959 last_mouse_frame = f;
6961 if (!tool_bar_p)
6962 last_tool_bar_item = -1;
6964 else
6965 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6967 /* Ignore any mouse motion that happened before this event;
6968 any subsequent mouse-movement Emacs events should reflect
6969 only motion after the ButtonPress/Release. */
6970 if (f != 0)
6971 f->mouse_moved = 0;
6973 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6974 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6975 /* For a down-event in the menu bar,
6976 don't pass it to Xt right now.
6977 Instead, save it away
6978 and we will pass it to Xt from kbd_buffer_get_event.
6979 That way, we can run some Lisp code first. */
6980 if (
6981 #ifdef USE_GTK
6982 ! popup_activated ()
6983 /* Gtk+ menus only react to the first three buttons. */
6984 && event.xbutton.button < 3
6986 #endif
6987 f && event.type == ButtonPress
6988 /* Verify the event is really within the menu bar
6989 and not just sent to it due to grabbing. */
6990 && event.xbutton.x >= 0
6991 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6992 && event.xbutton.y >= 0
6993 && event.xbutton.y < f->output_data.x->menubar_height
6994 && event.xbutton.same_screen)
6996 SET_SAVED_BUTTON_EVENT;
6997 XSETFRAME (last_mouse_press_frame, f);
6998 #ifdef USE_GTK
6999 *finish = X_EVENT_DROP;
7000 #endif
7002 else if (event.type == ButtonPress)
7004 last_mouse_press_frame = Qnil;
7005 goto OTHER;
7008 #ifdef USE_MOTIF /* This should do not harm for Lucid,
7009 but I am trying to be cautious. */
7010 else if (event.type == ButtonRelease)
7012 if (!NILP (last_mouse_press_frame))
7014 f = XFRAME (last_mouse_press_frame);
7015 if (f->output_data.x)
7016 SET_SAVED_BUTTON_EVENT;
7018 else
7019 goto OTHER;
7021 #endif /* USE_MOTIF */
7022 else
7023 goto OTHER;
7024 #endif /* USE_X_TOOLKIT || USE_GTK */
7026 break;
7028 case CirculateNotify:
7029 goto OTHER;
7031 case CirculateRequest:
7032 goto OTHER;
7034 case VisibilityNotify:
7035 goto OTHER;
7037 case MappingNotify:
7038 /* Someone has changed the keyboard mapping - update the
7039 local cache. */
7040 switch (event.xmapping.request)
7042 case MappingModifier:
7043 x_find_modifier_meanings (dpyinfo);
7044 /* This is meant to fall through. */
7045 case MappingKeyboard:
7046 XRefreshKeyboardMapping (&event.xmapping);
7048 goto OTHER;
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;
7139 #if 0
7140 struct x_display_info *dpyinfo;
7141 #endif
7143 if (interrupt_input_blocked)
7145 interrupt_input_pending = 1;
7146 #ifdef SYNC_INPUT
7147 pending_signals = 1;
7148 #endif
7149 return -1;
7152 interrupt_input_pending = 0;
7153 #ifdef SYNC_INPUT
7154 pending_signals = pending_atimers;
7155 #endif
7156 BLOCK_INPUT;
7158 /* So people can tell when we have read the available input. */
7159 input_signal_count++;
7161 ++handling_signal;
7163 #ifdef HAVE_X_SM
7164 /* Only check session manager input for the primary display. */
7165 if (terminal->id == 1 && x_session_have_connection ())
7167 struct input_event inev;
7168 BLOCK_INPUT;
7169 /* We don't need to EVENT_INIT (inev) here, as
7170 x_session_check_input copies an entire input_event. */
7171 if (x_session_check_input (&inev))
7173 kbd_buffer_store_event_hold (&inev, hold_quit);
7174 count++;
7176 UNBLOCK_INPUT;
7178 #endif
7180 /* For debugging, this gives a way to fake an I/O error. */
7181 if (terminal->display_info.x == XTread_socket_fake_io_error)
7183 XTread_socket_fake_io_error = 0;
7184 x_io_error_quitter (terminal->display_info.x->display);
7187 #if 0 /* This loop is a noop now. */
7188 /* Find the display we are supposed to read input for.
7189 It's the one communicating on descriptor SD. */
7190 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7192 #if 0 /* This ought to be unnecessary; let's verify it. */
7193 #ifdef FIOSNBIO
7194 /* If available, Xlib uses FIOSNBIO to make the socket
7195 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7196 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7197 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7198 fcntl (dpyinfo->connection, F_SETFL, 0);
7199 #endif /* ! defined (FIOSNBIO) */
7200 #endif
7202 #if 0 /* This code can't be made to work, with multiple displays,
7203 and appears not to be used on any system any more.
7204 Also keyboard.c doesn't turn O_NDELAY on and off
7205 for X connections. */
7206 #ifndef SIGIO
7207 #ifndef HAVE_SELECT
7208 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7210 extern int read_alarm_should_throw;
7211 read_alarm_should_throw = 1;
7212 XPeekEvent (dpyinfo->display, &event);
7213 read_alarm_should_throw = 0;
7215 #endif /* HAVE_SELECT */
7216 #endif /* SIGIO */
7217 #endif
7219 #endif
7221 #ifndef USE_GTK
7222 while (XPending (terminal->display_info.x->display))
7224 int finish;
7226 XNextEvent (terminal->display_info.x->display, &event);
7228 #ifdef HAVE_X_I18N
7229 /* Filter events for the current X input method. */
7230 if (x_filter_event (terminal->display_info.x, &event))
7231 break;
7232 #endif
7233 event_found = 1;
7235 count += handle_one_xevent (terminal->display_info.x,
7236 &event, &finish, hold_quit);
7238 if (finish == X_EVENT_GOTO_OUT)
7239 goto out;
7242 #else /* USE_GTK */
7244 /* For GTK we must use the GTK event loop. But XEvents gets passed
7245 to our filter function above, and then to the big event switch.
7246 We use a bunch of globals to communicate with our filter function,
7247 that is kind of ugly, but it works.
7249 There is no way to do one display at the time, GTK just does events
7250 from all displays. */
7252 while (gtk_events_pending ())
7254 current_count = count;
7255 current_hold_quit = hold_quit;
7257 gtk_main_iteration ();
7259 count = current_count;
7260 current_count = -1;
7261 current_hold_quit = 0;
7263 if (current_finish == X_EVENT_GOTO_OUT)
7264 break;
7266 #endif /* USE_GTK */
7268 out:;
7270 /* On some systems, an X bug causes Emacs to get no more events
7271 when the window is destroyed. Detect that. (1994.) */
7272 if (! event_found)
7274 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7275 One XNOOP in 100 loops will make Emacs terminate.
7276 B. Bretthauer, 1994 */
7277 x_noop_count++;
7278 if (x_noop_count >= 100)
7280 x_noop_count=0;
7282 if (next_noop_dpyinfo == 0)
7283 next_noop_dpyinfo = x_display_list;
7285 XNoOp (next_noop_dpyinfo->display);
7287 /* Each time we get here, cycle through the displays now open. */
7288 next_noop_dpyinfo = next_noop_dpyinfo->next;
7292 /* If the focus was just given to an auto-raising frame,
7293 raise it now. */
7294 /* ??? This ought to be able to handle more than one such frame. */
7295 if (pending_autoraise_frame)
7297 x_raise_frame (pending_autoraise_frame);
7298 pending_autoraise_frame = 0;
7301 --handling_signal;
7302 UNBLOCK_INPUT;
7304 return count;
7310 /***********************************************************************
7311 Text Cursor
7312 ***********************************************************************/
7314 /* Set clipping for output in glyph row ROW. W is the window in which
7315 we operate. GC is the graphics context to set clipping in.
7317 ROW may be a text row or, e.g., a mode line. Text rows must be
7318 clipped to the interior of the window dedicated to text display,
7319 mode lines must be clipped to the whole window. */
7321 static void
7322 x_clip_to_row (w, row, area, gc)
7323 struct window *w;
7324 struct glyph_row *row;
7325 int area;
7326 GC gc;
7328 struct frame *f = XFRAME (WINDOW_FRAME (w));
7329 XRectangle clip_rect;
7330 int window_x, window_y, window_width;
7332 window_box (w, area, &window_x, &window_y, &window_width, 0);
7334 clip_rect.x = window_x;
7335 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7336 clip_rect.y = max (clip_rect.y, window_y);
7337 clip_rect.width = window_width;
7338 clip_rect.height = row->visible_height;
7340 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7344 /* Draw a hollow box cursor on window W in glyph row ROW. */
7346 static void
7347 x_draw_hollow_cursor (w, row)
7348 struct window *w;
7349 struct glyph_row *row;
7351 struct frame *f = XFRAME (WINDOW_FRAME (w));
7352 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7353 Display *dpy = FRAME_X_DISPLAY (f);
7354 int x, y, wd, h;
7355 XGCValues xgcv;
7356 struct glyph *cursor_glyph;
7357 GC gc;
7359 /* Get the glyph the cursor is on. If we can't tell because
7360 the current matrix is invalid or such, give up. */
7361 cursor_glyph = get_phys_cursor_glyph (w);
7362 if (cursor_glyph == NULL)
7363 return;
7365 /* Compute frame-relative coordinates for phys cursor. */
7366 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7367 wd = w->phys_cursor_width;
7369 /* The foreground of cursor_gc is typically the same as the normal
7370 background color, which can cause the cursor box to be invisible. */
7371 xgcv.foreground = f->output_data.x->cursor_pixel;
7372 if (dpyinfo->scratch_cursor_gc)
7373 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7374 else
7375 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7376 GCForeground, &xgcv);
7377 gc = dpyinfo->scratch_cursor_gc;
7379 /* Set clipping, draw the rectangle, and reset clipping again. */
7380 x_clip_to_row (w, row, TEXT_AREA, gc);
7381 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7382 XSetClipMask (dpy, gc, None);
7386 /* Draw a bar cursor on window W in glyph row ROW.
7388 Implementation note: One would like to draw a bar cursor with an
7389 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7390 Unfortunately, I didn't find a font yet that has this property set.
7391 --gerd. */
7393 static void
7394 x_draw_bar_cursor (w, row, width, kind)
7395 struct window *w;
7396 struct glyph_row *row;
7397 int width;
7398 enum text_cursor_kinds kind;
7400 struct frame *f = XFRAME (w->frame);
7401 struct glyph *cursor_glyph;
7403 /* If cursor is out of bounds, don't draw garbage. This can happen
7404 in mini-buffer windows when switching between echo area glyphs
7405 and mini-buffer. */
7406 cursor_glyph = get_phys_cursor_glyph (w);
7407 if (cursor_glyph == NULL)
7408 return;
7410 /* If on an image, draw like a normal cursor. That's usually better
7411 visible than drawing a bar, esp. if the image is large so that
7412 the bar might not be in the window. */
7413 if (cursor_glyph->type == IMAGE_GLYPH)
7415 struct glyph_row *row;
7416 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7417 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7419 else
7421 Display *dpy = FRAME_X_DISPLAY (f);
7422 Window window = FRAME_X_WINDOW (f);
7423 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7424 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7425 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7426 XGCValues xgcv;
7428 /* If the glyph's background equals the color we normally draw
7429 the bars cursor in, the bar cursor in its normal color is
7430 invisible. Use the glyph's foreground color instead in this
7431 case, on the assumption that the glyph's colors are chosen so
7432 that the glyph is legible. */
7433 if (face->background == f->output_data.x->cursor_pixel)
7434 xgcv.background = xgcv.foreground = face->foreground;
7435 else
7436 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7437 xgcv.graphics_exposures = 0;
7439 if (gc)
7440 XChangeGC (dpy, gc, mask, &xgcv);
7441 else
7443 gc = XCreateGC (dpy, window, mask, &xgcv);
7444 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7447 x_clip_to_row (w, row, TEXT_AREA, gc);
7449 if (kind == BAR_CURSOR)
7451 if (width < 0)
7452 width = FRAME_CURSOR_WIDTH (f);
7453 width = min (cursor_glyph->pixel_width, width);
7455 w->phys_cursor_width = width;
7457 XFillRectangle (dpy, window, gc,
7458 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7459 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7460 width, row->height);
7462 else
7464 int dummy_x, dummy_y, dummy_h;
7466 if (width < 0)
7467 width = row->height;
7469 width = min (row->height, width);
7471 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
7472 &dummy_y, &dummy_h);
7474 XFillRectangle (dpy, window, gc,
7475 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7476 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7477 row->height - width),
7478 w->phys_cursor_width, width);
7481 XSetClipMask (dpy, gc, None);
7486 /* RIF: Define cursor CURSOR on frame F. */
7488 static void
7489 x_define_frame_cursor (f, cursor)
7490 struct frame *f;
7491 Cursor cursor;
7493 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7497 /* RIF: Clear area on frame F. */
7499 static void
7500 x_clear_frame_area (f, x, y, width, height)
7501 struct frame *f;
7502 int x, y, width, height;
7504 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7505 x, y, width, height, False);
7509 /* RIF: Draw cursor on window W. */
7511 static void
7512 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7513 struct window *w;
7514 struct glyph_row *glyph_row;
7515 int x, y;
7516 int cursor_type, cursor_width;
7517 int on_p, active_p;
7519 struct frame *f = XFRAME (WINDOW_FRAME (w));
7521 if (on_p)
7523 w->phys_cursor_type = cursor_type;
7524 w->phys_cursor_on_p = 1;
7526 if (glyph_row->exact_window_width_line_p
7527 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7529 glyph_row->cursor_in_fringe_p = 1;
7530 draw_fringe_bitmap (w, glyph_row, 0);
7532 else
7533 switch (cursor_type)
7535 case HOLLOW_BOX_CURSOR:
7536 x_draw_hollow_cursor (w, glyph_row);
7537 break;
7539 case FILLED_BOX_CURSOR:
7540 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7541 break;
7543 case BAR_CURSOR:
7544 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7545 break;
7547 case HBAR_CURSOR:
7548 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7549 break;
7551 case NO_CURSOR:
7552 w->phys_cursor_width = 0;
7553 break;
7555 default:
7556 abort ();
7559 #ifdef HAVE_X_I18N
7560 if (w == XWINDOW (f->selected_window))
7561 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7562 xic_set_preeditarea (w, x, y);
7563 #endif
7566 #ifndef XFlush
7567 XFlush (FRAME_X_DISPLAY (f));
7568 #endif
7572 /* Icons. */
7574 /* Make the x-window of frame F use the gnu icon bitmap. */
7577 x_bitmap_icon (f, file)
7578 struct frame *f;
7579 Lisp_Object file;
7581 int bitmap_id;
7583 if (FRAME_X_WINDOW (f) == 0)
7584 return 1;
7586 /* Free up our existing icon bitmap and mask if any. */
7587 if (f->output_data.x->icon_bitmap > 0)
7588 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7589 f->output_data.x->icon_bitmap = 0;
7591 if (STRINGP (file))
7593 #ifdef USE_GTK
7594 /* Use gtk_window_set_icon_from_file () if available,
7595 It's not restricted to bitmaps */
7596 if (xg_set_icon (f, file))
7597 return 0;
7598 #endif /* USE_GTK */
7599 bitmap_id = x_create_bitmap_from_file (f, file);
7600 x_create_bitmap_mask (f, bitmap_id);
7602 else
7604 /* Create the GNU bitmap and mask if necessary. */
7605 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7607 int rc = -1;
7609 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7610 #ifdef USE_GTK
7611 if (xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7612 return 0;
7613 #else
7614 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7615 if (rc != -1)
7616 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7617 #endif /* USE_GTK */
7618 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
7620 /* If all else fails, use the (black and white) xbm image. */
7621 if (rc == -1)
7623 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7624 gnu_xbm_width, gnu_xbm_height);
7625 if (rc == -1)
7626 return 1;
7628 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7629 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7633 /* The first time we create the GNU bitmap and mask,
7634 this increments the ref-count one extra time.
7635 As a result, the GNU bitmap and mask are never freed.
7636 That way, we don't have to worry about allocating it again. */
7637 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7639 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7642 x_wm_set_icon_pixmap (f, bitmap_id);
7643 f->output_data.x->icon_bitmap = bitmap_id;
7645 return 0;
7649 /* Make the x-window of frame F use a rectangle with text.
7650 Use ICON_NAME as the text. */
7653 x_text_icon (f, icon_name)
7654 struct frame *f;
7655 char *icon_name;
7657 if (FRAME_X_WINDOW (f) == 0)
7658 return 1;
7661 XTextProperty text;
7662 text.value = (unsigned char *) icon_name;
7663 text.encoding = XA_STRING;
7664 text.format = 8;
7665 text.nitems = strlen (icon_name);
7666 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7669 if (f->output_data.x->icon_bitmap > 0)
7670 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7671 f->output_data.x->icon_bitmap = 0;
7672 x_wm_set_icon_pixmap (f, 0);
7674 return 0;
7677 #define X_ERROR_MESSAGE_SIZE 200
7679 /* If non-nil, this should be a string.
7680 It means catch X errors and store the error message in this string.
7682 The reason we use a stack is that x_catch_error/x_uncatch_error can
7683 be called from a signal handler.
7686 struct x_error_message_stack {
7687 char string[X_ERROR_MESSAGE_SIZE];
7688 Display *dpy;
7689 struct x_error_message_stack *prev;
7691 static struct x_error_message_stack *x_error_message;
7693 /* An X error handler which stores the error message in
7694 *x_error_message. This is called from x_error_handler if
7695 x_catch_errors is in effect. */
7697 static void
7698 x_error_catcher (display, error)
7699 Display *display;
7700 XErrorEvent *error;
7702 XGetErrorText (display, error->error_code,
7703 x_error_message->string,
7704 X_ERROR_MESSAGE_SIZE);
7707 /* Begin trapping X errors for display DPY. Actually we trap X errors
7708 for all displays, but DPY should be the display you are actually
7709 operating on.
7711 After calling this function, X protocol errors no longer cause
7712 Emacs to exit; instead, they are recorded in the string
7713 stored in *x_error_message.
7715 Calling x_check_errors signals an Emacs error if an X error has
7716 occurred since the last call to x_catch_errors or x_check_errors.
7718 Calling x_uncatch_errors resumes the normal error handling. */
7720 void x_check_errors ();
7722 void
7723 x_catch_errors (dpy)
7724 Display *dpy;
7726 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7728 /* Make sure any errors from previous requests have been dealt with. */
7729 XSync (dpy, False);
7731 data->dpy = dpy;
7732 data->string[0] = 0;
7733 data->prev = x_error_message;
7734 x_error_message = data;
7737 /* Undo the last x_catch_errors call.
7738 DPY should be the display that was passed to x_catch_errors. */
7740 void
7741 x_uncatch_errors ()
7743 struct x_error_message_stack *tmp;
7745 BLOCK_INPUT;
7747 /* The display may have been closed before this function is called.
7748 Check if it is still open before calling XSync. */
7749 if (x_display_info_for_display (x_error_message->dpy) != 0)
7750 XSync (x_error_message->dpy, False);
7752 tmp = x_error_message;
7753 x_error_message = x_error_message->prev;
7754 xfree (tmp);
7755 UNBLOCK_INPUT;
7758 /* If any X protocol errors have arrived since the last call to
7759 x_catch_errors or x_check_errors, signal an Emacs error using
7760 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7762 void
7763 x_check_errors (dpy, format)
7764 Display *dpy;
7765 char *format;
7767 /* Make sure to catch any errors incurred so far. */
7768 XSync (dpy, False);
7770 if (x_error_message->string[0])
7772 char string[X_ERROR_MESSAGE_SIZE];
7773 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7774 x_uncatch_errors ();
7775 error (format, string);
7779 /* Nonzero if we had any X protocol errors
7780 since we did x_catch_errors on DPY. */
7783 x_had_errors_p (dpy)
7784 Display *dpy;
7786 /* Make sure to catch any errors incurred so far. */
7787 XSync (dpy, False);
7789 return x_error_message->string[0] != 0;
7792 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7794 void
7795 x_clear_errors (dpy)
7796 Display *dpy;
7798 x_error_message->string[0] = 0;
7801 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7802 * idea. --lorentey */
7803 /* Close off all unclosed x_catch_errors calls. */
7805 void
7806 x_fully_uncatch_errors ()
7808 while (x_error_message)
7809 x_uncatch_errors ();
7811 #endif
7813 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7816 x_catching_errors ()
7818 return x_error_message != 0;
7821 #if 0
7822 static unsigned int x_wire_count;
7823 x_trace_wire ()
7825 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7827 #endif /* ! 0 */
7830 /* Handle SIGPIPE, which can happen when the connection to a server
7831 simply goes away. SIGPIPE is handled by x_connection_signal.
7832 Don't need to do anything, because the write which caused the
7833 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7834 which will do the appropriate cleanup for us. */
7836 static SIGTYPE
7837 x_connection_signal (signalnum) /* If we don't have an argument, */
7838 int signalnum; /* some compilers complain in signal calls. */
7840 #ifdef USG
7841 /* USG systems forget handlers when they are used;
7842 must reestablish each time */
7843 signal (signalnum, x_connection_signal);
7844 #endif /* USG */
7848 /************************************************************************
7849 Handling X errors
7850 ************************************************************************/
7852 /* Error message passed to x_connection_closed. */
7854 static char *error_msg;
7856 /* Function installed as fatal_error_signal_hook in
7857 x_connection_closed. Print the X error message, and exit normally,
7858 instead of dumping core when XtCloseDisplay fails. */
7860 static void
7861 x_fatal_error_signal ()
7863 fprintf (stderr, "%s\n", error_msg);
7864 exit (70);
7867 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7868 the text of an error message that lead to the connection loss. */
7870 static SIGTYPE
7871 x_connection_closed (dpy, error_message)
7872 Display *dpy;
7873 char *error_message;
7875 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7876 Lisp_Object frame, tail;
7877 int index = SPECPDL_INDEX ();
7879 error_msg = (char *) alloca (strlen (error_message) + 1);
7880 strcpy (error_msg, error_message);
7881 handling_signal = 0;
7883 /* Prevent being called recursively because of an error condition
7884 below. Otherwise, we might end up with printing ``can't find per
7885 display information'' in the recursive call instead of printing
7886 the original message here. */
7887 x_catch_errors (dpy);
7889 /* Inhibit redisplay while frames are being deleted. */
7890 specbind (Qinhibit_redisplay, Qt);
7892 if (dpyinfo)
7894 /* Protect display from being closed when we delete the last
7895 frame on it. */
7896 dpyinfo->reference_count++;
7897 dpyinfo->terminal->reference_count++;
7900 /* First delete frames whose mini-buffers are on frames
7901 that are on the dead display. */
7902 FOR_EACH_FRAME (tail, frame)
7904 Lisp_Object minibuf_frame;
7905 minibuf_frame
7906 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7907 if (FRAME_X_P (XFRAME (frame))
7908 && FRAME_X_P (XFRAME (minibuf_frame))
7909 && ! EQ (frame, minibuf_frame)
7910 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7911 delete_frame (frame, Qnoelisp);
7914 /* Now delete all remaining frames on the dead display.
7915 We are now sure none of these is used as the mini-buffer
7916 for another frame that we need to delete. */
7917 FOR_EACH_FRAME (tail, frame)
7918 if (FRAME_X_P (XFRAME (frame))
7919 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7921 /* Set this to t so that delete_frame won't get confused
7922 trying to find a replacement. */
7923 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7924 delete_frame (frame, Qnoelisp);
7927 /* We have to close the display to inform Xt that it doesn't
7928 exist anymore. If we don't, Xt will continue to wait for
7929 events from the display. As a consequence, a sequence of
7931 M-x make-frame-on-display RET :1 RET
7932 ...kill the new frame, so that we get an IO error...
7933 M-x make-frame-on-display RET :1 RET
7935 will indefinitely wait in Xt for events for display `:1', opened
7936 in the first call to make-frame-on-display.
7938 Closing the display is reported to lead to a bus error on
7939 OpenWindows in certain situations. I suspect that is a bug
7940 in OpenWindows. I don't know how to circumvent it here. */
7942 if (dpyinfo)
7944 #ifdef USE_X_TOOLKIT
7945 /* If DPYINFO is null, this means we didn't open the display
7946 in the first place, so don't try to close it. */
7948 extern void (*fatal_error_signal_hook) P_ ((void));
7949 fatal_error_signal_hook = x_fatal_error_signal;
7950 XtCloseDisplay (dpy);
7951 fatal_error_signal_hook = NULL;
7953 #endif
7955 #ifdef USE_GTK
7956 /* Due to bugs in some Gtk+ versions, just exit here if this
7957 is the last display/terminal. */
7958 if (terminal_list->next_terminal == NULL)
7960 fprintf (stderr, "%s\n", error_msg);
7961 shut_down_emacs (0, 0, Qnil);
7962 exit (70);
7964 xg_display_close (dpyinfo->display);
7965 #endif
7967 /* Indicate that this display is dead. */
7968 dpyinfo->display = 0;
7970 dpyinfo->reference_count--;
7971 dpyinfo->terminal->reference_count--;
7972 if (dpyinfo->reference_count != 0)
7973 /* We have just closed all frames on this display. */
7974 abort ();
7977 Lisp_Object tmp;
7978 XSETTERMINAL (tmp, dpyinfo->terminal);
7979 Fdelete_terminal (tmp, Qnoelisp);
7983 x_uncatch_errors ();
7985 if (terminal_list == 0)
7987 fprintf (stderr, "%s\n", error_msg);
7988 shut_down_emacs (0, 0, Qnil);
7989 exit (70);
7992 /* Ordinary stack unwind doesn't deal with these. */
7993 #ifdef SIGIO
7994 sigunblock (sigmask (SIGIO));
7995 #endif
7996 sigunblock (sigmask (SIGALRM));
7997 TOTALLY_UNBLOCK_INPUT;
7999 unbind_to (index, Qnil);
8000 clear_waiting_for_input ();
8001 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
8002 longjmp), because returning from this function would get us back into
8003 Xlib's code which will directly call `exit'. */
8004 error ("%s", error_msg);
8007 /* We specifically use it before defining it, so that gcc doesn't inline it,
8008 otherwise gdb doesn't know how to properly put a breakpoint on it. */
8009 static void x_error_quitter P_ ((Display *, XErrorEvent *));
8011 /* This is the first-level handler for X protocol errors.
8012 It calls x_error_quitter or x_error_catcher. */
8014 static int
8015 x_error_handler (display, error)
8016 Display *display;
8017 XErrorEvent *error;
8019 if (x_error_message)
8020 x_error_catcher (display, error);
8021 else
8022 x_error_quitter (display, error);
8023 return 0;
8026 /* This is the usual handler for X protocol errors.
8027 It kills all frames on the display that we got the error for.
8028 If that was the only one, it prints an error message and kills Emacs. */
8030 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
8032 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
8033 #define NO_INLINE __attribute__((noinline))
8034 #else
8035 #define NO_INLINE
8036 #endif
8038 /* Some versions of GNU/Linux define noinline in their headers. */
8040 #ifdef noinline
8041 #undef noinline
8042 #endif
8044 /* On older GCC versions, just putting x_error_quitter
8045 after x_error_handler prevents inlining into the former. */
8047 static void NO_INLINE
8048 x_error_quitter (display, error)
8049 Display *display;
8050 XErrorEvent *error;
8052 char buf[256], buf1[356];
8054 /* Ignore BadName errors. They can happen because of fonts
8055 or colors that are not defined. */
8057 if (error->error_code == BadName)
8058 return;
8060 /* Note that there is no real way portable across R3/R4 to get the
8061 original error handler. */
8063 XGetErrorText (display, error->error_code, buf, sizeof (buf));
8064 sprintf (buf1, "X protocol error: %s on protocol request %d",
8065 buf, error->request_code);
8066 x_connection_closed (display, buf1);
8070 /* This is the handler for X IO errors, always.
8071 It kills all frames on the display that we lost touch with.
8072 If that was the only one, it prints an error message and kills Emacs. */
8074 static int
8075 x_io_error_quitter (display)
8076 Display *display;
8078 char buf[256];
8080 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
8081 x_connection_closed (display, buf);
8082 return 0;
8085 /* Changing the font of the frame. */
8087 /* Give frame F the font FONT-OBJECT as its default font. The return
8088 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
8089 frame. If it is negative, generate a new fontset from
8090 FONT-OBJECT. */
8092 Lisp_Object
8093 x_new_font (f, font_object, fontset)
8094 struct frame *f;
8095 Lisp_Object font_object;
8096 int fontset;
8098 struct font *font = XFONT_OBJECT (font_object);
8100 if (fontset < 0)
8101 fontset = fontset_from_font (font_object);
8102 FRAME_FONTSET (f) = fontset;
8103 if (FRAME_FONT (f) == font)
8104 /* This font is already set in frame F. There's nothing more to
8105 do. */
8106 return font_object;
8108 FRAME_FONT (f) = font;
8109 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8110 FRAME_COLUMN_WIDTH (f) = font->average_width;
8111 FRAME_SPACE_WIDTH (f) = font->space_width;
8112 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8114 compute_fringe_widths (f, 1);
8116 /* Compute the scroll bar width in character columns. */
8117 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8119 int wid = FRAME_COLUMN_WIDTH (f);
8120 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8121 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8123 else
8125 int wid = FRAME_COLUMN_WIDTH (f);
8126 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8129 if (FRAME_X_WINDOW (f) != 0)
8131 /* Don't change the size of a tip frame; there's no point in
8132 doing it because it's done in Fx_show_tip, and it leads to
8133 problems because the tip frame has no widget. */
8134 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8135 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8138 #ifdef HAVE_X_I18N
8139 if (FRAME_XIC (f)
8140 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8142 BLOCK_INPUT;
8143 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8144 UNBLOCK_INPUT;
8146 #endif
8148 return font_object;
8152 /***********************************************************************
8153 X Input Methods
8154 ***********************************************************************/
8156 #ifdef HAVE_X_I18N
8158 #ifdef HAVE_X11R6
8160 /* XIM destroy callback function, which is called whenever the
8161 connection to input method XIM dies. CLIENT_DATA contains a
8162 pointer to the x_display_info structure corresponding to XIM. */
8164 static void
8165 xim_destroy_callback (xim, client_data, call_data)
8166 XIM xim;
8167 XPointer client_data;
8168 XPointer call_data;
8170 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8171 Lisp_Object frame, tail;
8173 BLOCK_INPUT;
8175 /* No need to call XDestroyIC.. */
8176 FOR_EACH_FRAME (tail, frame)
8178 struct frame *f = XFRAME (frame);
8179 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8181 FRAME_XIC (f) = NULL;
8182 xic_free_xfontset (f);
8186 /* No need to call XCloseIM. */
8187 dpyinfo->xim = NULL;
8188 XFree (dpyinfo->xim_styles);
8189 UNBLOCK_INPUT;
8192 #endif /* HAVE_X11R6 */
8194 #ifdef HAVE_X11R6
8195 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8196 extern char *XSetIMValues P_ ((XIM, ...));
8197 #endif
8199 /* Open the connection to the XIM server on display DPYINFO.
8200 RESOURCE_NAME is the resource name Emacs uses. */
8202 static void
8203 xim_open_dpy (dpyinfo, resource_name)
8204 struct x_display_info *dpyinfo;
8205 char *resource_name;
8207 XIM xim;
8209 #ifdef HAVE_XIM
8210 if (use_xim)
8212 if (dpyinfo->xim)
8213 XCloseIM (dpyinfo->xim);
8214 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8215 EMACS_CLASS);
8216 dpyinfo->xim = xim;
8218 if (xim)
8220 #ifdef HAVE_X11R6
8221 XIMCallback destroy;
8222 #endif
8224 /* Get supported styles and XIM values. */
8225 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8227 #ifdef HAVE_X11R6
8228 destroy.callback = xim_destroy_callback;
8229 destroy.client_data = (XPointer)dpyinfo;
8230 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8231 #endif
8235 else
8236 #endif /* HAVE_XIM */
8237 dpyinfo->xim = NULL;
8241 #ifdef HAVE_X11R6_XIM
8243 /* XIM instantiate callback function, which is called whenever an XIM
8244 server is available. DISPLAY is the display of the XIM.
8245 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8246 when the callback was registered. */
8248 static void
8249 xim_instantiate_callback (display, client_data, call_data)
8250 Display *display;
8251 XPointer client_data;
8252 XPointer call_data;
8254 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8255 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8257 /* We don't support multiple XIM connections. */
8258 if (dpyinfo->xim)
8259 return;
8261 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8263 /* Create XIC for the existing frames on the same display, as long
8264 as they have no XIC. */
8265 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8267 Lisp_Object tail, frame;
8269 BLOCK_INPUT;
8270 FOR_EACH_FRAME (tail, frame)
8272 struct frame *f = XFRAME (frame);
8274 if (FRAME_X_P (f)
8275 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8276 if (FRAME_XIC (f) == NULL)
8278 create_frame_xic (f);
8279 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8280 xic_set_statusarea (f);
8281 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8283 struct window *w = XWINDOW (f->selected_window);
8284 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8289 UNBLOCK_INPUT;
8293 #endif /* HAVE_X11R6_XIM */
8296 /* Open a connection to the XIM server on display DPYINFO.
8297 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8298 connection only at the first time. On X11R6, open the connection
8299 in the XIM instantiate callback function. */
8301 static void
8302 xim_initialize (dpyinfo, resource_name)
8303 struct x_display_info *dpyinfo;
8304 char *resource_name;
8306 dpyinfo->xim = NULL;
8307 #ifdef HAVE_XIM
8308 if (use_xim)
8310 #ifdef HAVE_X11R6_XIM
8311 struct xim_inst_t *xim_inst;
8312 int len;
8314 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8315 dpyinfo->xim_callback_data = xim_inst;
8316 xim_inst->dpyinfo = dpyinfo;
8317 len = strlen (resource_name);
8318 xim_inst->resource_name = (char *) xmalloc (len + 1);
8319 bcopy (resource_name, xim_inst->resource_name, len + 1);
8320 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8321 resource_name, EMACS_CLASS,
8322 xim_instantiate_callback,
8323 /* This is XPointer in XFree86
8324 but (XPointer *) on Tru64, at
8325 least, hence the configure test. */
8326 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8327 #else /* not HAVE_X11R6_XIM */
8328 xim_open_dpy (dpyinfo, resource_name);
8329 #endif /* not HAVE_X11R6_XIM */
8331 #endif /* HAVE_XIM */
8335 /* Close the connection to the XIM server on display DPYINFO. */
8337 static void
8338 xim_close_dpy (dpyinfo)
8339 struct x_display_info *dpyinfo;
8341 #ifdef HAVE_XIM
8342 if (use_xim)
8344 #ifdef HAVE_X11R6_XIM
8345 if (dpyinfo->display)
8346 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8347 NULL, EMACS_CLASS,
8348 xim_instantiate_callback, NULL);
8349 xfree (dpyinfo->xim_callback_data->resource_name);
8350 xfree (dpyinfo->xim_callback_data);
8351 #endif /* HAVE_X11R6_XIM */
8352 if (dpyinfo->display)
8353 XCloseIM (dpyinfo->xim);
8354 dpyinfo->xim = NULL;
8355 XFree (dpyinfo->xim_styles);
8357 #endif /* HAVE_XIM */
8360 #endif /* not HAVE_X11R6_XIM */
8364 /* Calculate the absolute position in frame F
8365 from its current recorded position values and gravity. */
8367 void
8368 x_calc_absolute_position (f)
8369 struct frame *f;
8371 int flags = f->size_hint_flags;
8373 /* We have nothing to do if the current position
8374 is already for the top-left corner. */
8375 if (! ((flags & XNegative) || (flags & YNegative)))
8376 return;
8378 /* Treat negative positions as relative to the leftmost bottommost
8379 position that fits on the screen. */
8380 if (flags & XNegative)
8381 f->left_pos = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
8382 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8385 int height = FRAME_PIXEL_HEIGHT (f);
8387 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8388 /* Something is fishy here. When using Motif, starting Emacs with
8389 `-g -0-0', the frame appears too low by a few pixels.
8391 This seems to be so because initially, while Emacs is starting,
8392 the column widget's height and the frame's pixel height are
8393 different. The column widget's height is the right one. In
8394 later invocations, when Emacs is up, the frame's pixel height
8395 is right, though.
8397 It's not obvious where the initial small difference comes from.
8398 2000-12-01, gerd. */
8400 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8401 #endif
8403 if (flags & YNegative)
8404 f->top_pos = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
8405 - height + f->top_pos;
8408 /* The left_pos and top_pos
8409 are now relative to the top and left screen edges,
8410 so the flags should correspond. */
8411 f->size_hint_flags &= ~ (XNegative | YNegative);
8414 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8415 to really change the position, and 0 when calling from
8416 x_make_frame_visible (in that case, XOFF and YOFF are the current
8417 position values). It is -1 when calling from x_set_frame_parameters,
8418 which means, do adjust for borders but don't change the gravity. */
8420 void
8421 x_set_offset (f, xoff, yoff, change_gravity)
8422 struct frame *f;
8423 register int xoff, yoff;
8424 int change_gravity;
8426 int modified_top, modified_left;
8428 if (change_gravity > 0)
8430 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8431 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8433 f->top_pos = yoff;
8434 f->left_pos = xoff;
8435 f->size_hint_flags &= ~ (XNegative | YNegative);
8436 if (xoff < 0)
8437 f->size_hint_flags |= XNegative;
8438 if (yoff < 0)
8439 f->size_hint_flags |= YNegative;
8440 f->win_gravity = NorthWestGravity;
8442 x_calc_absolute_position (f);
8444 BLOCK_INPUT;
8445 x_wm_set_size_hint (f, (long) 0, 0);
8447 modified_left = f->left_pos;
8448 modified_top = f->top_pos;
8450 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8452 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8453 than the WM decorations. So we use the calculated offset instead
8454 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8455 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8456 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8459 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8460 modified_left, modified_top);
8462 x_sync_with_move (f, f->left_pos, f->top_pos,
8463 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8464 ? 1 : 0);
8466 /* change_gravity is non-zero when this function is called from Lisp to
8467 programmatically move a frame. In that case, we call
8468 x_check_expected_move to discover if we have a "Type A" or "Type B"
8469 window manager, and, for a "Type A" window manager, adjust the position
8470 of the frame.
8472 We call x_check_expected_move if a programmatic move occurred, and
8473 either the window manager type (A/B) is unknown or it is Type A but we
8474 need to compute the top/left offset adjustment for this frame. */
8476 if (change_gravity != 0 &&
8477 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8478 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8479 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8480 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8481 x_check_expected_move (f, modified_left, modified_top);
8483 UNBLOCK_INPUT;
8486 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8487 on the root window for frame F contains ATOMNAME.
8488 This is how a WM check shall be done according to the Window Manager
8489 Specification/Extended Window Manager Hints at
8490 http://freedesktop.org/wiki/Specifications/wm-spec. */
8492 static int
8493 wm_supports (f, atomname)
8494 struct frame *f;
8495 const char *atomname;
8497 Atom actual_type;
8498 unsigned long actual_size, bytes_remaining;
8499 int i, rc, actual_format;
8500 Atom prop_atom;
8501 Window wmcheck_window;
8502 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8503 Window target_window = dpyinfo->root_window;
8504 long max_len = 65536;
8505 Display *dpy = FRAME_X_DISPLAY (f);
8506 unsigned char *tmp_data = NULL;
8507 Atom target_type = XA_WINDOW;
8508 Atom want_atom;
8510 BLOCK_INPUT;
8512 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8514 x_catch_errors (dpy);
8515 rc = XGetWindowProperty (dpy, target_window,
8516 prop_atom, 0, max_len, False, target_type,
8517 &actual_type, &actual_format, &actual_size,
8518 &bytes_remaining, &tmp_data);
8520 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8522 if (tmp_data) XFree (tmp_data);
8523 x_uncatch_errors ();
8524 UNBLOCK_INPUT;
8525 return 0;
8528 wmcheck_window = *(Window *) tmp_data;
8529 XFree (tmp_data);
8531 /* Check if window exists. */
8532 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8533 x_sync (f);
8534 if (x_had_errors_p (dpy))
8536 x_uncatch_errors ();
8537 UNBLOCK_INPUT;
8538 return 0;
8541 if (dpyinfo->net_supported_window != wmcheck_window)
8543 /* Window changed, reload atoms */
8544 if (dpyinfo->net_supported_atoms != NULL)
8545 XFree (dpyinfo->net_supported_atoms);
8546 dpyinfo->net_supported_atoms = NULL;
8547 dpyinfo->nr_net_supported_atoms = 0;
8548 dpyinfo->net_supported_window = 0;
8550 target_type = XA_ATOM;
8551 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8552 tmp_data = NULL;
8553 rc = XGetWindowProperty (dpy, target_window,
8554 prop_atom, 0, max_len, False, target_type,
8555 &actual_type, &actual_format, &actual_size,
8556 &bytes_remaining, &tmp_data);
8558 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8560 if (tmp_data) XFree (tmp_data);
8561 x_uncatch_errors ();
8562 UNBLOCK_INPUT;
8563 return 0;
8566 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8567 dpyinfo->nr_net_supported_atoms = actual_size;
8568 dpyinfo->net_supported_window = wmcheck_window;
8571 rc = 0;
8572 want_atom = XInternAtom (dpy, atomname, False);
8574 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8575 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8577 x_uncatch_errors ();
8578 UNBLOCK_INPUT;
8580 return rc;
8583 /* Do fullscreen as specified in extended window manager hints */
8585 static int
8586 do_ewmh_fullscreen (f)
8587 struct frame *f;
8589 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8591 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8592 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8593 if (!have_net_atom)
8594 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8596 if (have_net_atom)
8598 Lisp_Object frame;
8599 const char *atom = "_NET_WM_STATE";
8600 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8601 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8602 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8603 const char *what = NULL;
8605 XSETFRAME (frame, f);
8607 /* If there are _NET_ atoms we assume we have extended window manager
8608 hints. */
8609 switch (f->want_fullscreen)
8611 case FULLSCREEN_BOTH:
8612 what = fs;
8613 break;
8614 case FULLSCREEN_WIDTH:
8615 what = fw;
8616 break;
8617 case FULLSCREEN_HEIGHT:
8618 what = fh;
8619 break;
8622 if (what != NULL && !wm_supports (f, what)) return 0;
8625 Fx_send_client_event (frame, make_number (0), frame,
8626 make_unibyte_string (atom, strlen (atom)),
8627 make_number (32),
8628 Fcons (make_number (0), /* Remove */
8629 Fcons
8630 (make_unibyte_string (fs,
8631 strlen (fs)),
8632 Qnil)));
8633 Fx_send_client_event (frame, make_number (0), frame,
8634 make_unibyte_string (atom, strlen (atom)),
8635 make_number (32),
8636 Fcons (make_number (0), /* Remove */
8637 Fcons
8638 (make_unibyte_string (fh,
8639 strlen (fh)),
8640 Qnil)));
8641 Fx_send_client_event (frame, make_number (0), frame,
8642 make_unibyte_string (atom, strlen (atom)),
8643 make_number (32),
8644 Fcons (make_number (0), /* Remove */
8645 Fcons
8646 (make_unibyte_string (fw,
8647 strlen (fw)),
8648 Qnil)));
8649 f->want_fullscreen = FULLSCREEN_NONE;
8650 if (what != NULL)
8651 Fx_send_client_event (frame, make_number (0), frame,
8652 make_unibyte_string (atom, strlen (atom)),
8653 make_number (32),
8654 Fcons (make_number (1), /* Add */
8655 Fcons
8656 (make_unibyte_string (what,
8657 strlen (what)),
8658 Qnil)));
8661 return have_net_atom;
8664 static void
8665 XTfullscreen_hook (f)
8666 FRAME_PTR f;
8668 if (f->async_visible)
8670 BLOCK_INPUT;
8671 do_ewmh_fullscreen (f);
8672 x_sync (f);
8673 UNBLOCK_INPUT;
8678 extern Lisp_Object Qfullwidth, Qfullheight, Qfullboth;
8679 static void
8680 x_handle_net_wm_state (f, event)
8681 struct frame *f;
8682 XPropertyEvent *event;
8684 Atom actual_type;
8685 unsigned long actual_size, bytes_remaining;
8686 int i, rc, actual_format, value = 0;
8687 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8688 long max_len = 65536;
8689 Display *dpy = FRAME_X_DISPLAY (f);
8690 unsigned char *tmp_data = NULL;
8691 Atom target_type = XA_ATOM;
8692 Lisp_Object lval;
8694 BLOCK_INPUT;
8695 x_catch_errors (dpy);
8696 rc = XGetWindowProperty (dpy, event->window,
8697 event->atom, 0, max_len, False, target_type,
8698 &actual_type, &actual_format, &actual_size,
8699 &bytes_remaining, &tmp_data);
8701 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
8703 if (tmp_data) XFree (tmp_data);
8704 x_uncatch_errors ();
8705 UNBLOCK_INPUT;
8706 return;
8709 x_uncatch_errors ();
8711 for (i = 0; i < actual_size; ++i)
8713 Atom a = ((Atom*)tmp_data)[i];
8714 if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
8715 value |= FULLSCREEN_WIDTH;
8716 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
8717 value |= FULLSCREEN_HEIGHT;
8718 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen_atom)
8719 value |= FULLSCREEN_BOTH;
8722 lval = Qnil;
8723 switch (value)
8725 case FULLSCREEN_WIDTH:
8726 lval = Qfullwidth;
8727 break;
8728 case FULLSCREEN_HEIGHT:
8729 lval = Qfullheight;
8730 break;
8731 case FULLSCREEN_BOTH:
8732 lval = Qfullboth;
8733 break;
8736 store_frame_param (f, Qfullscreen, lval);
8738 if (tmp_data) XFree (tmp_data);
8739 UNBLOCK_INPUT;
8742 /* Check if we need to resize the frame due to a fullscreen request.
8743 If so needed, resize the frame. */
8744 static void
8745 x_check_fullscreen (f)
8746 struct frame *f;
8748 if (f->want_fullscreen & FULLSCREEN_BOTH)
8750 int width, height, ign;
8752 if (do_ewmh_fullscreen (f))
8753 return;
8755 x_real_positions (f, &f->left_pos, &f->top_pos);
8757 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8759 /* We do not need to move the window, it shall be taken care of
8760 when setting WM manager hints.
8761 If the frame is visible already, the position is checked by
8762 x_check_expected_move. */
8763 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8765 change_frame_size (f, height, width, 0, 1, 0);
8766 SET_FRAME_GARBAGED (f);
8767 cancel_mouse_face (f);
8769 /* Wait for the change of frame size to occur */
8770 f->want_fullscreen |= FULLSCREEN_WAIT;
8775 /* This function is called by x_set_offset to determine whether the window
8776 manager interfered with the positioning of the frame. Type A window
8777 managers position the surrounding window manager decorations a small
8778 amount above and left of the user-supplied position. Type B window
8779 managers position the surrounding window manager decorations at the
8780 user-specified position. If we detect a Type A window manager, we
8781 compensate by moving the window right and down by the proper amount. */
8783 static void
8784 x_check_expected_move (f, expected_left, expected_top)
8785 struct frame *f;
8786 int expected_left;
8787 int expected_top;
8789 int current_left = 0, current_top = 0;
8791 /* x_real_positions returns the left and top offsets of the outermost
8792 window manager window around the frame. */
8794 x_real_positions (f, &current_left, &current_top);
8796 if (current_left != expected_left || current_top != expected_top)
8798 /* It's a "Type A" window manager. */
8800 int adjusted_left;
8801 int adjusted_top;
8803 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8804 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8805 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8807 /* Now fix the mispositioned frame's location. */
8809 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8810 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8812 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8813 adjusted_left, adjusted_top);
8815 x_sync_with_move (f, expected_left, expected_top, 0);
8817 else
8818 /* It's a "Type B" window manager. We don't have to adjust the
8819 frame's position. */
8821 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8825 /* Wait for XGetGeometry to return up-to-date position information for a
8826 recently-moved frame. Call this immediately after calling XMoveWindow.
8827 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8828 frame has been moved to, so we use a fuzzy position comparison instead
8829 of an exact comparison. */
8831 static void
8832 x_sync_with_move (f, left, top, fuzzy)
8833 struct frame *f;
8834 int left, top, fuzzy;
8836 int count = 0;
8838 while (count++ < 50)
8840 int current_left = 0, current_top = 0;
8842 /* In theory, this call to XSync only needs to happen once, but in
8843 practice, it doesn't seem to work, hence the need for the surrounding
8844 loop. */
8846 XSync (FRAME_X_DISPLAY (f), False);
8847 x_real_positions (f, &current_left, &current_top);
8849 if (fuzzy)
8851 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8852 pixels. */
8854 if (eabs (current_left - left) <= 10
8855 && eabs (current_top - top) <= 40)
8856 return;
8858 else if (current_left == left && current_top == top)
8859 return;
8862 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8863 will then return up-to-date position info. */
8865 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8869 /* Change the size of frame F's X window to COLS/ROWS in the case F
8870 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8871 top-left-corner window gravity for this size change and subsequent
8872 size changes. Otherwise we leave the window gravity unchanged. */
8874 static void
8875 x_set_window_size_1 (f, change_gravity, cols, rows)
8876 struct frame *f;
8877 int change_gravity;
8878 int cols, rows;
8880 int pixelwidth, pixelheight;
8882 check_frame_size (f, &rows, &cols);
8883 f->scroll_bar_actual_width
8884 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8886 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8887 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8888 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8890 compute_fringe_widths (f, 0);
8892 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8893 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8895 f->win_gravity = NorthWestGravity;
8896 x_wm_set_size_hint (f, (long) 0, 0);
8898 XSync (FRAME_X_DISPLAY (f), False);
8899 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8900 pixelwidth, pixelheight);
8902 /* Now, strictly speaking, we can't be sure that this is accurate,
8903 but the window manager will get around to dealing with the size
8904 change request eventually, and we'll hear how it went when the
8905 ConfigureNotify event gets here.
8907 We could just not bother storing any of this information here,
8908 and let the ConfigureNotify event set everything up, but that
8909 might be kind of confusing to the Lisp code, since size changes
8910 wouldn't be reported in the frame parameters until some random
8911 point in the future when the ConfigureNotify event arrives.
8913 We pass 1 for DELAY since we can't run Lisp code inside of
8914 a BLOCK_INPUT. */
8915 change_frame_size (f, rows, cols, 0, 1, 0);
8916 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8917 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8919 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8920 receive in the ConfigureNotify event; if we get what we asked
8921 for, then the event won't cause the screen to become garbaged, so
8922 we have to make sure to do it here. */
8923 SET_FRAME_GARBAGED (f);
8925 XFlush (FRAME_X_DISPLAY (f));
8929 /* Call this to change the size of frame F's x-window.
8930 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8931 for this size change and subsequent size changes.
8932 Otherwise we leave the window gravity unchanged. */
8934 void
8935 x_set_window_size (f, change_gravity, cols, rows)
8936 struct frame *f;
8937 int change_gravity;
8938 int cols, rows;
8940 BLOCK_INPUT;
8942 #ifdef USE_GTK
8943 if (FRAME_GTK_WIDGET (f))
8944 xg_frame_set_char_size (f, cols, rows);
8945 else
8946 x_set_window_size_1 (f, change_gravity, cols, rows);
8947 #elif USE_X_TOOLKIT
8949 if (f->output_data.x->widget != NULL)
8951 /* The x and y position of the widget is clobbered by the
8952 call to XtSetValues within EmacsFrameSetCharSize.
8953 This is a real kludge, but I don't understand Xt so I can't
8954 figure out a correct fix. Can anyone else tell me? -- rms. */
8955 int xpos = f->output_data.x->widget->core.x;
8956 int ypos = f->output_data.x->widget->core.y;
8957 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8958 f->output_data.x->widget->core.x = xpos;
8959 f->output_data.x->widget->core.y = ypos;
8961 else
8962 x_set_window_size_1 (f, change_gravity, cols, rows);
8964 #else /* not USE_X_TOOLKIT */
8966 x_set_window_size_1 (f, change_gravity, cols, rows);
8968 #endif /* not USE_X_TOOLKIT */
8970 /* If cursor was outside the new size, mark it as off. */
8971 mark_window_cursors_off (XWINDOW (f->root_window));
8973 /* Clear out any recollection of where the mouse highlighting was,
8974 since it might be in a place that's outside the new frame size.
8975 Actually checking whether it is outside is a pain in the neck,
8976 so don't try--just let the highlighting be done afresh with new size. */
8977 cancel_mouse_face (f);
8979 UNBLOCK_INPUT;
8982 /* Mouse warping. */
8984 void
8985 x_set_mouse_position (f, x, y)
8986 struct frame *f;
8987 int x, y;
8989 int pix_x, pix_y;
8991 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8992 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8994 if (pix_x < 0) pix_x = 0;
8995 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8997 if (pix_y < 0) pix_y = 0;
8998 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
9000 BLOCK_INPUT;
9002 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9003 0, 0, 0, 0, pix_x, pix_y);
9004 UNBLOCK_INPUT;
9007 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9009 void
9010 x_set_mouse_pixel_position (f, pix_x, pix_y)
9011 struct frame *f;
9012 int pix_x, pix_y;
9014 BLOCK_INPUT;
9016 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9017 0, 0, 0, 0, pix_x, pix_y);
9018 UNBLOCK_INPUT;
9021 /* focus shifting, raising and lowering. */
9023 void
9024 x_focus_on_frame (f)
9025 struct frame *f;
9027 #if 0 /* This proves to be unpleasant. */
9028 x_raise_frame (f);
9029 #endif
9030 #if 0
9031 /* I don't think that the ICCCM allows programs to do things like this
9032 without the interaction of the window manager. Whatever you end up
9033 doing with this code, do it to x_unfocus_frame too. */
9034 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9035 RevertToPointerRoot, CurrentTime);
9036 #endif /* ! 0 */
9039 void
9040 x_unfocus_frame (f)
9041 struct frame *f;
9043 #if 0
9044 /* Look at the remarks in x_focus_on_frame. */
9045 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
9046 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
9047 RevertToPointerRoot, CurrentTime);
9048 #endif /* ! 0 */
9051 /* Raise frame F. */
9053 void
9054 x_raise_frame (f)
9055 struct frame *f;
9057 BLOCK_INPUT;
9058 if (f->async_visible)
9059 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9061 XFlush (FRAME_X_DISPLAY (f));
9062 UNBLOCK_INPUT;
9065 /* Lower frame F. */
9067 void
9068 x_lower_frame (f)
9069 struct frame *f;
9071 if (f->async_visible)
9073 BLOCK_INPUT;
9074 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9075 XFlush (FRAME_X_DISPLAY (f));
9076 UNBLOCK_INPUT;
9080 /* Activate frame with Extended Window Manager Hints */
9082 void
9083 x_ewmh_activate_frame (f)
9084 FRAME_PTR f;
9086 /* See Window Manager Specification/Extended Window Manager Hints at
9087 http://freedesktop.org/wiki/Specifications/wm-spec */
9089 const char *atom = "_NET_ACTIVE_WINDOW";
9090 if (f->async_visible && wm_supports (f, atom))
9092 Lisp_Object frame;
9093 XSETFRAME (frame, f);
9094 Fx_send_client_event (frame, make_number (0), frame,
9095 make_unibyte_string (atom, strlen (atom)),
9096 make_number (32),
9097 Fcons (make_number (1),
9098 Fcons (make_number (last_user_time),
9099 Qnil)));
9103 static void
9104 XTframe_raise_lower (f, raise_flag)
9105 FRAME_PTR f;
9106 int raise_flag;
9108 if (raise_flag)
9109 x_raise_frame (f);
9110 else
9111 x_lower_frame (f);
9114 /* XEmbed implementation. */
9116 void
9117 xembed_set_info (f, flags)
9118 struct frame *f;
9119 enum xembed_info flags;
9121 Atom atom;
9122 unsigned long data[2];
9124 atom = XInternAtom (FRAME_X_DISPLAY (f), "_XEMBED_INFO", False);
9126 data[0] = XEMBED_VERSION;
9127 data[1] = flags;
9129 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), atom, atom,
9130 32, PropModeReplace, (unsigned char *) data, 2);
9133 void
9134 xembed_send_message (f, time, message, detail, data1, data2)
9135 struct frame *f;
9136 Time time;
9137 enum xembed_message message;
9138 long detail;
9139 long data1;
9140 long data2;
9142 XEvent event;
9144 event.xclient.type = ClientMessage;
9145 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9146 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
9147 event.xclient.format = 32;
9148 event.xclient.data.l[0] = time;
9149 event.xclient.data.l[1] = message;
9150 event.xclient.data.l[2] = detail;
9151 event.xclient.data.l[3] = data1;
9152 event.xclient.data.l[4] = data2;
9154 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9155 False, NoEventMask, &event);
9156 XSync (FRAME_X_DISPLAY (f), False);
9159 /* Change of visibility. */
9161 /* This tries to wait until the frame is really visible.
9162 However, if the window manager asks the user where to position
9163 the frame, this will return before the user finishes doing that.
9164 The frame will not actually be visible at that time,
9165 but it will become visible later when the window manager
9166 finishes with it. */
9168 void
9169 x_make_frame_visible (f)
9170 struct frame *f;
9172 Lisp_Object type;
9173 int original_top, original_left;
9174 int retry_count = 2;
9176 retry:
9178 BLOCK_INPUT;
9180 type = x_icon_type (f);
9181 if (!NILP (type))
9182 x_bitmap_icon (f, type);
9184 if (! FRAME_VISIBLE_P (f))
9186 /* We test FRAME_GARBAGED_P here to make sure we don't
9187 call x_set_offset a second time
9188 if we get to x_make_frame_visible a second time
9189 before the window gets really visible. */
9190 if (! FRAME_ICONIFIED_P (f)
9191 && ! FRAME_X_EMBEDDED_P (f)
9192 && ! f->output_data.x->asked_for_visible)
9193 x_set_offset (f, f->left_pos, f->top_pos, 0);
9195 f->output_data.x->asked_for_visible = 1;
9197 if (! EQ (Vx_no_window_manager, Qt))
9198 x_wm_set_window_state (f, NormalState);
9199 #ifdef USE_X_TOOLKIT
9200 if (FRAME_X_EMBEDDED_P (f))
9201 xembed_set_info (f, XEMBED_MAPPED);
9202 else
9204 /* This was XtPopup, but that did nothing for an iconified frame. */
9205 XtMapWidget (f->output_data.x->widget);
9207 #else /* not USE_X_TOOLKIT */
9208 #ifdef USE_GTK
9209 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9210 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9211 #else
9212 if (FRAME_X_EMBEDDED_P (f))
9213 xembed_set_info (f, XEMBED_MAPPED);
9214 else
9215 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9216 #endif /* not USE_GTK */
9217 #endif /* not USE_X_TOOLKIT */
9218 #if 0 /* This seems to bring back scroll bars in the wrong places
9219 if the window configuration has changed. They seem
9220 to come back ok without this. */
9221 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9222 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9223 #endif
9226 XFlush (FRAME_X_DISPLAY (f));
9228 /* Synchronize to ensure Emacs knows the frame is visible
9229 before we do anything else. We do this loop with input not blocked
9230 so that incoming events are handled. */
9232 Lisp_Object frame;
9233 int count;
9234 /* This must be before UNBLOCK_INPUT
9235 since events that arrive in response to the actions above
9236 will set it when they are handled. */
9237 int previously_visible = f->output_data.x->has_been_visible;
9239 original_left = f->left_pos;
9240 original_top = f->top_pos;
9242 /* This must come after we set COUNT. */
9243 UNBLOCK_INPUT;
9245 /* We unblock here so that arriving X events are processed. */
9247 /* Now move the window back to where it was "supposed to be".
9248 But don't do it if the gravity is negative.
9249 When the gravity is negative, this uses a position
9250 that is 3 pixels too low. Perhaps that's really the border width.
9252 Don't do this if the window has never been visible before,
9253 because the window manager may choose the position
9254 and we don't want to override it. */
9256 if (! FRAME_VISIBLE_P (f)
9257 && ! FRAME_ICONIFIED_P (f)
9258 && ! FRAME_X_EMBEDDED_P (f)
9259 && f->win_gravity == NorthWestGravity
9260 && previously_visible)
9262 Drawable rootw;
9263 int x, y;
9264 unsigned int width, height, border, depth;
9266 BLOCK_INPUT;
9268 /* On some window managers (such as FVWM) moving an existing
9269 window, even to the same place, causes the window manager
9270 to introduce an offset. This can cause the window to move
9271 to an unexpected location. Check the geometry (a little
9272 slow here) and then verify that the window is in the right
9273 place. If the window is not in the right place, move it
9274 there, and take the potential window manager hit. */
9275 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9276 &rootw, &x, &y, &width, &height, &border, &depth);
9278 if (original_left != x || original_top != y)
9279 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9280 original_left, original_top);
9282 UNBLOCK_INPUT;
9285 XSETFRAME (frame, f);
9287 /* Wait until the frame is visible. Process X events until a
9288 MapNotify event has been seen, or until we think we won't get a
9289 MapNotify at all.. */
9290 for (count = input_signal_count + 10;
9291 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9293 /* Force processing of queued events. */
9294 x_sync (f);
9296 /* Machines that do polling rather than SIGIO have been
9297 observed to go into a busy-wait here. So we'll fake an
9298 alarm signal to let the handler know that there's something
9299 to be read. We used to raise a real alarm, but it seems
9300 that the handler isn't always enabled here. This is
9301 probably a bug. */
9302 if (input_polling_used ())
9304 /* It could be confusing if a real alarm arrives while
9305 processing the fake one. Turn it off and let the
9306 handler reset it. */
9307 extern void poll_for_input_1 P_ ((void));
9308 int old_poll_suppress_count = poll_suppress_count;
9309 poll_suppress_count = 1;
9310 poll_for_input_1 ();
9311 poll_suppress_count = old_poll_suppress_count;
9314 /* See if a MapNotify event has been processed. */
9315 FRAME_SAMPLE_VISIBILITY (f);
9318 /* 2000-09-28: In
9320 (let ((f (selected-frame)))
9321 (iconify-frame f)
9322 (raise-frame f))
9324 the frame is not raised with various window managers on
9325 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9326 unknown reason, the call to XtMapWidget is completely ignored.
9327 Mapping the widget a second time works. */
9329 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9330 goto retry;
9334 /* Change from mapped state to withdrawn state. */
9336 /* Make the frame visible (mapped and not iconified). */
9338 void
9339 x_make_frame_invisible (f)
9340 struct frame *f;
9342 Window window;
9344 /* Use the frame's outermost window, not the one we normally draw on. */
9345 window = FRAME_OUTER_WINDOW (f);
9347 /* Don't keep the highlight on an invisible frame. */
9348 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9349 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9351 #if 0/* This might add unreliability; I don't trust it -- rms. */
9352 if (! f->async_visible && ! f->async_iconified)
9353 return;
9354 #endif
9356 BLOCK_INPUT;
9358 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9359 that the current position of the window is user-specified, rather than
9360 program-specified, so that when the window is mapped again, it will be
9361 placed at the same location, without forcing the user to position it
9362 by hand again (they have already done that once for this window.) */
9363 x_wm_set_size_hint (f, (long) 0, 1);
9365 #ifdef USE_GTK
9366 if (FRAME_GTK_OUTER_WIDGET (f))
9367 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9368 else
9369 #else
9370 if (FRAME_X_EMBEDDED_P (f))
9371 xembed_set_info (f, 0);
9372 else
9373 #endif
9376 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9377 DefaultScreen (FRAME_X_DISPLAY (f))))
9379 UNBLOCK_INPUT_RESIGNAL;
9380 error ("Can't notify window manager of window withdrawal");
9384 /* We can't distinguish this from iconification
9385 just by the event that we get from the server.
9386 So we can't win using the usual strategy of letting
9387 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9388 and synchronize with the server to make sure we agree. */
9389 f->visible = 0;
9390 FRAME_ICONIFIED_P (f) = 0;
9391 f->async_visible = 0;
9392 f->async_iconified = 0;
9394 x_sync (f);
9396 UNBLOCK_INPUT;
9399 /* Change window state from mapped to iconified. */
9401 void
9402 x_iconify_frame (f)
9403 struct frame *f;
9405 int result;
9406 Lisp_Object type;
9408 /* Don't keep the highlight on an invisible frame. */
9409 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9410 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9412 if (f->async_iconified)
9413 return;
9415 BLOCK_INPUT;
9417 FRAME_SAMPLE_VISIBILITY (f);
9419 type = x_icon_type (f);
9420 if (!NILP (type))
9421 x_bitmap_icon (f, type);
9423 #ifdef USE_GTK
9424 if (FRAME_GTK_OUTER_WIDGET (f))
9426 if (! FRAME_VISIBLE_P (f))
9427 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9429 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9430 f->iconified = 1;
9431 f->visible = 1;
9432 f->async_iconified = 1;
9433 f->async_visible = 0;
9434 UNBLOCK_INPUT;
9435 return;
9437 #endif
9439 #ifdef USE_X_TOOLKIT
9441 if (! FRAME_VISIBLE_P (f))
9443 if (! EQ (Vx_no_window_manager, Qt))
9444 x_wm_set_window_state (f, IconicState);
9445 /* This was XtPopup, but that did nothing for an iconified frame. */
9446 XtMapWidget (f->output_data.x->widget);
9447 /* The server won't give us any event to indicate
9448 that an invisible frame was changed to an icon,
9449 so we have to record it here. */
9450 f->iconified = 1;
9451 f->visible = 1;
9452 f->async_iconified = 1;
9453 f->async_visible = 0;
9454 UNBLOCK_INPUT;
9455 return;
9458 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9459 XtWindow (f->output_data.x->widget),
9460 DefaultScreen (FRAME_X_DISPLAY (f)));
9461 UNBLOCK_INPUT;
9463 if (!result)
9464 error ("Can't notify window manager of iconification");
9466 f->async_iconified = 1;
9467 f->async_visible = 0;
9470 BLOCK_INPUT;
9471 XFlush (FRAME_X_DISPLAY (f));
9472 UNBLOCK_INPUT;
9473 #else /* not USE_X_TOOLKIT */
9475 /* Make sure the X server knows where the window should be positioned,
9476 in case the user deiconifies with the window manager. */
9477 if (! FRAME_VISIBLE_P (f)
9478 && ! FRAME_ICONIFIED_P (f)
9479 && ! FRAME_X_EMBEDDED_P (f))
9480 x_set_offset (f, f->left_pos, f->top_pos, 0);
9482 /* Since we don't know which revision of X we're running, we'll use both
9483 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9485 /* X11R4: send a ClientMessage to the window manager using the
9486 WM_CHANGE_STATE type. */
9488 XEvent message;
9490 message.xclient.window = FRAME_X_WINDOW (f);
9491 message.xclient.type = ClientMessage;
9492 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9493 message.xclient.format = 32;
9494 message.xclient.data.l[0] = IconicState;
9496 if (! XSendEvent (FRAME_X_DISPLAY (f),
9497 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9498 False,
9499 SubstructureRedirectMask | SubstructureNotifyMask,
9500 &message))
9502 UNBLOCK_INPUT_RESIGNAL;
9503 error ("Can't notify window manager of iconification");
9507 /* X11R3: set the initial_state field of the window manager hints to
9508 IconicState. */
9509 x_wm_set_window_state (f, IconicState);
9511 if (!FRAME_VISIBLE_P (f))
9513 /* If the frame was withdrawn, before, we must map it. */
9514 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9517 f->async_iconified = 1;
9518 f->async_visible = 0;
9520 XFlush (FRAME_X_DISPLAY (f));
9521 UNBLOCK_INPUT;
9522 #endif /* not USE_X_TOOLKIT */
9526 /* Free X resources of frame F. */
9528 void
9529 x_free_frame_resources (f)
9530 struct frame *f;
9532 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9533 Lisp_Object bar;
9534 struct scroll_bar *b;
9536 BLOCK_INPUT;
9538 /* If a display connection is dead, don't try sending more
9539 commands to the X server. */
9540 if (dpyinfo->display)
9542 /* We must free faces before destroying windows because some
9543 font-driver (e.g. xft) access a window while finishing a
9544 face. */
9545 if (FRAME_FACE_CACHE (f))
9546 free_frame_faces (f);
9548 if (f->output_data.x->icon_desc)
9549 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9551 #ifdef USE_X_TOOLKIT
9552 /* Explicitly destroy the scroll bars of the frame. Without
9553 this, we get "BadDrawable" errors from the toolkit later on,
9554 presumably from expose events generated for the disappearing
9555 toolkit scroll bars. */
9556 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9558 b = XSCROLL_BAR (bar);
9559 x_scroll_bar_remove (b);
9561 #endif
9563 #ifdef HAVE_X_I18N
9564 if (FRAME_XIC (f))
9565 free_frame_xic (f);
9566 #endif
9568 #ifdef USE_X_TOOLKIT
9569 if (f->output_data.x->widget)
9571 XtDestroyWidget (f->output_data.x->widget);
9572 f->output_data.x->widget = NULL;
9574 /* Tooltips don't have widgets, only a simple X window, even if
9575 we are using a toolkit. */
9576 else if (FRAME_X_WINDOW (f))
9577 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9579 free_frame_menubar (f);
9580 #else /* !USE_X_TOOLKIT */
9582 #ifdef USE_GTK
9583 /* In the GTK version, tooltips are normal X
9584 frames. We must check and free both types. */
9585 if (FRAME_GTK_OUTER_WIDGET (f))
9587 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9588 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9589 FRAME_GTK_OUTER_WIDGET (f) = 0;
9591 #endif /* USE_GTK */
9593 if (FRAME_X_WINDOW (f))
9594 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9595 #endif /* !USE_X_TOOLKIT */
9597 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9598 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9599 unload_color (f, f->output_data.x->cursor_pixel);
9600 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9601 unload_color (f, f->output_data.x->border_pixel);
9602 unload_color (f, f->output_data.x->mouse_pixel);
9604 if (f->output_data.x->scroll_bar_background_pixel != -1)
9605 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9606 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9607 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9608 #ifdef USE_TOOLKIT_SCROLL_BARS
9609 /* Scrollbar shadow colors. */
9610 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9611 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9612 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9613 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9614 #endif /* USE_TOOLKIT_SCROLL_BARS */
9615 if (f->output_data.x->white_relief.allocated_p)
9616 unload_color (f, f->output_data.x->white_relief.pixel);
9617 if (f->output_data.x->black_relief.allocated_p)
9618 unload_color (f, f->output_data.x->black_relief.pixel);
9620 x_free_gcs (f);
9621 XFlush (FRAME_X_DISPLAY (f));
9624 xfree (f->output_data.x->saved_menu_event);
9625 xfree (f->output_data.x);
9626 f->output_data.x = NULL;
9628 if (f == dpyinfo->x_focus_frame)
9629 dpyinfo->x_focus_frame = 0;
9630 if (f == dpyinfo->x_focus_event_frame)
9631 dpyinfo->x_focus_event_frame = 0;
9632 if (f == dpyinfo->x_highlight_frame)
9633 dpyinfo->x_highlight_frame = 0;
9635 if (f == dpyinfo->mouse_face_mouse_frame)
9637 dpyinfo->mouse_face_beg_row
9638 = dpyinfo->mouse_face_beg_col = -1;
9639 dpyinfo->mouse_face_end_row
9640 = dpyinfo->mouse_face_end_col = -1;
9641 dpyinfo->mouse_face_window = Qnil;
9642 dpyinfo->mouse_face_deferred_gc = 0;
9643 dpyinfo->mouse_face_mouse_frame = 0;
9646 UNBLOCK_INPUT;
9650 /* Destroy the X window of frame F. */
9652 void
9653 x_destroy_window (f)
9654 struct frame *f;
9656 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9658 /* If a display connection is dead, don't try sending more
9659 commands to the X server. */
9660 if (dpyinfo->display != 0)
9661 x_free_frame_resources (f);
9663 dpyinfo->reference_count--;
9667 /* Setting window manager hints. */
9669 /* Set the normal size hints for the window manager, for frame F.
9670 FLAGS is the flags word to use--or 0 meaning preserve the flags
9671 that the window now has.
9672 If USER_POSITION is nonzero, we set the USPosition
9673 flag (this is useful when FLAGS is 0).
9674 The GTK version is in gtkutils.c */
9676 #ifndef USE_GTK
9677 void
9678 x_wm_set_size_hint (f, flags, user_position)
9679 struct frame *f;
9680 long flags;
9681 int user_position;
9683 XSizeHints size_hints;
9685 #ifdef USE_X_TOOLKIT
9686 Arg al[2];
9687 int ac = 0;
9688 Dimension widget_width, widget_height;
9689 #endif
9691 Window window = FRAME_OUTER_WINDOW (f);
9693 /* Setting PMaxSize caused various problems. */
9694 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9696 size_hints.x = f->left_pos;
9697 size_hints.y = f->top_pos;
9699 #ifdef USE_X_TOOLKIT
9700 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9701 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9702 XtGetValues (f->output_data.x->widget, al, ac);
9703 size_hints.height = widget_height;
9704 size_hints.width = widget_width;
9705 #else /* not USE_X_TOOLKIT */
9706 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9707 size_hints.width = FRAME_PIXEL_WIDTH (f);
9708 #endif /* not USE_X_TOOLKIT */
9710 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9711 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9712 size_hints.max_width = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
9713 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9714 size_hints.max_height = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
9715 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9717 /* Calculate the base and minimum sizes.
9719 (When we use the X toolkit, we don't do it here.
9720 Instead we copy the values that the widgets are using, below.) */
9721 #ifndef USE_X_TOOLKIT
9723 int base_width, base_height;
9724 int min_rows = 0, min_cols = 0;
9726 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9727 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9729 check_frame_size (f, &min_rows, &min_cols);
9731 /* The window manager uses the base width hints to calculate the
9732 current number of rows and columns in the frame while
9733 resizing; min_width and min_height aren't useful for this
9734 purpose, since they might not give the dimensions for a
9735 zero-row, zero-column frame.
9737 We use the base_width and base_height members if we have
9738 them; otherwise, we set the min_width and min_height members
9739 to the size for a zero x zero frame. */
9741 size_hints.flags |= PBaseSize;
9742 size_hints.base_width = base_width;
9743 size_hints.base_height = base_height;
9744 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9745 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9748 /* If we don't need the old flags, we don't need the old hint at all. */
9749 if (flags)
9751 size_hints.flags |= flags;
9752 goto no_read;
9754 #endif /* not USE_X_TOOLKIT */
9757 XSizeHints hints; /* Sometimes I hate X Windows... */
9758 long supplied_return;
9759 int value;
9761 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9762 &supplied_return);
9764 #ifdef USE_X_TOOLKIT
9765 size_hints.base_height = hints.base_height;
9766 size_hints.base_width = hints.base_width;
9767 size_hints.min_height = hints.min_height;
9768 size_hints.min_width = hints.min_width;
9769 #endif
9771 if (flags)
9772 size_hints.flags |= flags;
9773 else
9775 if (value == 0)
9776 hints.flags = 0;
9777 if (hints.flags & PSize)
9778 size_hints.flags |= PSize;
9779 if (hints.flags & PPosition)
9780 size_hints.flags |= PPosition;
9781 if (hints.flags & USPosition)
9782 size_hints.flags |= USPosition;
9783 if (hints.flags & USSize)
9784 size_hints.flags |= USSize;
9788 #ifndef USE_X_TOOLKIT
9789 no_read:
9790 #endif
9792 #ifdef PWinGravity
9793 size_hints.win_gravity = f->win_gravity;
9794 size_hints.flags |= PWinGravity;
9796 if (user_position)
9798 size_hints.flags &= ~ PPosition;
9799 size_hints.flags |= USPosition;
9801 #endif /* PWinGravity */
9803 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9805 #endif /* not USE_GTK */
9807 /* Used for IconicState or NormalState */
9809 void
9810 x_wm_set_window_state (f, state)
9811 struct frame *f;
9812 int state;
9814 #ifdef USE_X_TOOLKIT
9815 Arg al[1];
9817 XtSetArg (al[0], XtNinitialState, state);
9818 XtSetValues (f->output_data.x->widget, al, 1);
9819 #else /* not USE_X_TOOLKIT */
9820 Window window = FRAME_X_WINDOW (f);
9822 f->output_data.x->wm_hints.flags |= StateHint;
9823 f->output_data.x->wm_hints.initial_state = state;
9825 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9826 #endif /* not USE_X_TOOLKIT */
9829 void
9830 x_wm_set_icon_pixmap (f, pixmap_id)
9831 struct frame *f;
9832 int pixmap_id;
9834 Pixmap icon_pixmap, icon_mask;
9836 #ifndef USE_X_TOOLKIT
9837 Window window = FRAME_OUTER_WINDOW (f);
9838 #endif
9840 if (pixmap_id > 0)
9842 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9843 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9844 icon_mask = x_bitmap_mask (f, pixmap_id);
9845 f->output_data.x->wm_hints.icon_mask = icon_mask;
9847 else
9849 /* It seems there is no way to turn off use of an icon pixmap.
9850 The following line does it, only if no icon has yet been created,
9851 for some window managers. But with mwm it crashes.
9852 Some people say it should clear the IconPixmapHint bit in this case,
9853 but that doesn't work, and the X consortium said it isn't the
9854 right thing at all. Since there is no way to win,
9855 best to explicitly give up. */
9856 #if 0
9857 f->output_data.x->wm_hints.icon_pixmap = None;
9858 f->output_data.x->wm_hints.icon_mask = None;
9859 #else
9860 return;
9861 #endif
9865 #ifdef USE_GTK
9867 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9868 return;
9871 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9874 Arg al[1];
9875 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9876 XtSetValues (f->output_data.x->widget, al, 1);
9877 XtSetArg (al[0], XtNiconMask, icon_mask);
9878 XtSetValues (f->output_data.x->widget, al, 1);
9881 #else /* not USE_X_TOOLKIT && not USE_GTK */
9883 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9884 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9886 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9889 void
9890 x_wm_set_icon_position (f, icon_x, icon_y)
9891 struct frame *f;
9892 int icon_x, icon_y;
9894 Window window = FRAME_OUTER_WINDOW (f);
9896 f->output_data.x->wm_hints.flags |= IconPositionHint;
9897 f->output_data.x->wm_hints.icon_x = icon_x;
9898 f->output_data.x->wm_hints.icon_y = icon_y;
9900 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9904 /***********************************************************************
9905 Fonts
9906 ***********************************************************************/
9908 #if GLYPH_DEBUG
9910 /* Check that FONT is valid on frame F. It is if it can be found in F's
9911 font table. */
9913 static void
9914 x_check_font (f, font)
9915 struct frame *f;
9916 struct font *font;
9918 Lisp_Object frame;
9920 xassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9921 if (font->driver->check)
9922 xassert (font->driver->check (f, font) == 0);
9925 #endif /* GLYPH_DEBUG != 0 */
9928 /***********************************************************************
9929 Initialization
9930 ***********************************************************************/
9932 #ifdef USE_X_TOOLKIT
9933 static XrmOptionDescRec emacs_options[] = {
9934 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9935 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9937 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9938 XrmoptionSepArg, NULL},
9939 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9941 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9942 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9943 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9944 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9945 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9946 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9947 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9950 /* Whether atimer for Xt timeouts is activated or not. */
9952 static int x_timeout_atimer_activated_flag;
9954 #endif /* USE_X_TOOLKIT */
9956 static int x_initialized;
9958 #ifdef HAVE_X_SM
9959 static int x_session_initialized;
9960 #endif
9962 /* Test whether two display-name strings agree up to the dot that separates
9963 the screen number from the server number. */
9964 static int
9965 same_x_server (name1, name2)
9966 const char *name1, *name2;
9968 int seen_colon = 0;
9969 const unsigned char *system_name = SDATA (Vsystem_name);
9970 int system_name_length = strlen (system_name);
9971 int length_until_period = 0;
9973 while (system_name[length_until_period] != 0
9974 && system_name[length_until_period] != '.')
9975 length_until_period++;
9977 /* Treat `unix' like an empty host name. */
9978 if (! strncmp (name1, "unix:", 5))
9979 name1 += 4;
9980 if (! strncmp (name2, "unix:", 5))
9981 name2 += 4;
9982 /* Treat this host's name like an empty host name. */
9983 if (! strncmp (name1, system_name, system_name_length)
9984 && name1[system_name_length] == ':')
9985 name1 += system_name_length;
9986 if (! strncmp (name2, system_name, system_name_length)
9987 && name2[system_name_length] == ':')
9988 name2 += system_name_length;
9989 /* Treat this host's domainless name like an empty host name. */
9990 if (! strncmp (name1, system_name, length_until_period)
9991 && name1[length_until_period] == ':')
9992 name1 += length_until_period;
9993 if (! strncmp (name2, system_name, length_until_period)
9994 && name2[length_until_period] == ':')
9995 name2 += length_until_period;
9997 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
9999 if (*name1 == ':')
10000 seen_colon++;
10001 if (seen_colon && *name1 == '.')
10002 return 1;
10004 return (seen_colon
10005 && (*name1 == '.' || *name1 == '\0')
10006 && (*name2 == '.' || *name2 == '\0'));
10009 /* Count number of set bits in mask and number of bits to shift to
10010 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10011 to 5. */
10012 static void
10013 get_bits_and_offset (mask, bits, offset)
10014 unsigned long mask;
10015 int *bits;
10016 int *offset;
10018 int nr = 0;
10019 int off = 0;
10021 while (!(mask & 1))
10023 off++;
10024 mask >>= 1;
10027 while (mask & 1)
10029 nr++;
10030 mask >>= 1;
10033 *offset = off;
10034 *bits = nr;
10037 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
10038 But don't permanently open it, just test its availability. */
10041 x_display_ok (display)
10042 const char *display;
10044 int dpy_ok = 1;
10045 Display *dpy;
10047 dpy = XOpenDisplay (display);
10048 if (dpy)
10049 XCloseDisplay (dpy);
10050 else
10051 dpy_ok = 0;
10052 return dpy_ok;
10055 /* Open a connection to X display DISPLAY_NAME, and return
10056 the structure that describes the open display.
10057 If we cannot contact the display, return null. */
10059 struct x_display_info *
10060 x_term_init (display_name, xrm_option, resource_name)
10061 Lisp_Object display_name;
10062 char *xrm_option;
10063 char *resource_name;
10065 int connection;
10066 Display *dpy;
10067 struct terminal *terminal;
10068 struct x_display_info *dpyinfo;
10069 XrmDatabase xrdb;
10071 BLOCK_INPUT;
10073 if (!x_initialized)
10075 x_initialize ();
10076 ++x_initialized;
10079 if (! x_display_ok (SDATA (display_name)))
10080 error ("Display %s can't be opened", SDATA (display_name));
10082 #ifdef USE_GTK
10084 #define NUM_ARGV 10
10085 int argc;
10086 char *argv[NUM_ARGV];
10087 char **argv2 = argv;
10088 GdkAtom atom;
10090 #ifndef HAVE_GTK_MULTIDISPLAY
10091 if (!EQ (Vinitial_window_system, intern ("x")))
10092 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10093 #endif
10095 if (x_initialized++ > 1)
10097 #ifdef HAVE_GTK_MULTIDISPLAY
10098 /* Opening another display. If xg_display_open returns less
10099 than zero, we are probably on GTK 2.0, which can only handle
10100 one display. GTK 2.2 or later can handle more than one. */
10101 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10102 #endif
10103 error ("Sorry, this version of GTK can only handle one display");
10105 else
10107 for (argc = 0; argc < NUM_ARGV; ++argc)
10108 argv[argc] = 0;
10110 argc = 0;
10111 argv[argc++] = initial_argv[0];
10113 if (! NILP (display_name))
10115 argv[argc++] = "--display";
10116 argv[argc++] = SDATA (display_name);
10119 argv[argc++] = "--name";
10120 argv[argc++] = resource_name;
10122 XSetLocaleModifiers ("");
10124 gtk_init (&argc, &argv2);
10126 /* gtk_init does set_locale. We must fix locale after calling it. */
10127 fixup_locale ();
10128 xg_initialize ();
10130 dpy = GDK_DISPLAY ();
10132 /* NULL window -> events for all windows go to our function */
10133 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10135 /* Load our own gtkrc if it exists. */
10137 char *file = "~/.emacs.d/gtkrc";
10138 Lisp_Object s, abs_file;
10140 s = make_string (file, strlen (file));
10141 abs_file = Fexpand_file_name (s, Qnil);
10143 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10144 gtk_rc_parse (SDATA (abs_file));
10147 XSetErrorHandler (x_error_handler);
10148 XSetIOErrorHandler (x_io_error_quitter);
10151 #else /* not USE_GTK */
10152 #ifdef USE_X_TOOLKIT
10153 /* weiner@footloose.sps.mot.com reports that this causes
10154 errors with X11R5:
10155 X protocol error: BadAtom (invalid Atom parameter)
10156 on protocol request 18skiloaf.
10157 So let's not use it until R6. */
10158 #ifdef HAVE_X11XTR6
10159 XtSetLanguageProc (NULL, NULL, NULL);
10160 #endif
10163 int argc = 0;
10164 char *argv[3];
10166 argv[0] = "";
10167 argc = 1;
10168 if (xrm_option)
10170 argv[argc++] = "-xrm";
10171 argv[argc++] = xrm_option;
10173 turn_on_atimers (0);
10174 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10175 resource_name, EMACS_CLASS,
10176 emacs_options, XtNumber (emacs_options),
10177 &argc, argv);
10178 turn_on_atimers (1);
10180 #ifdef HAVE_X11XTR6
10181 /* I think this is to compensate for XtSetLanguageProc. */
10182 fixup_locale ();
10183 #endif
10186 #else /* not USE_X_TOOLKIT */
10187 XSetLocaleModifiers ("");
10188 dpy = XOpenDisplay (SDATA (display_name));
10189 #endif /* not USE_X_TOOLKIT */
10190 #endif /* not USE_GTK*/
10192 /* Detect failure. */
10193 if (dpy == 0)
10195 UNBLOCK_INPUT;
10196 return 0;
10199 /* We have definitely succeeded. Record the new connection. */
10201 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10202 bzero (dpyinfo, sizeof *dpyinfo);
10204 terminal = x_create_terminal (dpyinfo);
10207 struct x_display_info *share;
10208 Lisp_Object tail;
10210 for (share = x_display_list, tail = x_display_name_list; share;
10211 share = share->next, tail = XCDR (tail))
10212 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10213 SDATA (display_name)))
10214 break;
10215 if (share)
10216 terminal->kboard = share->terminal->kboard;
10217 else
10219 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10220 init_kboard (terminal->kboard);
10221 terminal->kboard->Vwindow_system = intern ("x");
10222 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10224 char *vendor = ServerVendor (dpy);
10225 /* Temporarily hide the partially initialized terminal */
10226 terminal_list = terminal->next_terminal;
10227 UNBLOCK_INPUT;
10228 terminal->kboard->Vsystem_key_alist
10229 = call1 (Qvendor_specific_keysyms,
10230 vendor ? build_string (vendor) : empty_unibyte_string);
10231 BLOCK_INPUT;
10232 terminal->next_terminal = terminal_list;
10233 terminal_list = terminal;
10236 terminal->kboard->next_kboard = all_kboards;
10237 all_kboards = terminal->kboard;
10238 /* Don't let the initial kboard remain current longer than necessary.
10239 That would cause problems if a file loaded on startup tries to
10240 prompt in the mini-buffer. */
10241 if (current_kboard == initial_kboard)
10242 current_kboard = terminal->kboard;
10244 terminal->kboard->reference_count++;
10247 /* Put this display on the chain. */
10248 dpyinfo->next = x_display_list;
10249 x_display_list = dpyinfo;
10251 /* Put it on x_display_name_list as well, to keep them parallel. */
10252 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10253 x_display_name_list);
10254 dpyinfo->name_list_element = XCAR (x_display_name_list);
10256 dpyinfo->display = dpy;
10258 /* Set the name of the terminal. */
10259 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10260 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10261 terminal->name[SBYTES (display_name)] = 0;
10263 #if 0
10264 XSetAfterFunction (x_current_display, x_trace_wire);
10265 #endif /* ! 0 */
10267 dpyinfo->x_id_name
10268 = (char *) xmalloc (SBYTES (Vinvocation_name)
10269 + SBYTES (Vsystem_name)
10270 + 2);
10271 sprintf (dpyinfo->x_id_name, "%s@%s",
10272 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10274 /* Figure out which modifier bits mean what. */
10275 x_find_modifier_meanings (dpyinfo);
10277 /* Get the scroll bar cursor. */
10278 #ifdef USE_GTK
10279 /* We must create a GTK cursor, it is required for GTK widgets. */
10280 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10281 #endif /* USE_GTK */
10283 dpyinfo->vertical_scroll_bar_cursor
10284 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10286 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10287 resource_name, EMACS_CLASS);
10288 #ifdef HAVE_XRMSETDATABASE
10289 XrmSetDatabase (dpyinfo->display, xrdb);
10290 #else
10291 dpyinfo->display->db = xrdb;
10292 #endif
10293 /* Put the rdb where we can find it in a way that works on
10294 all versions. */
10295 dpyinfo->xrdb = xrdb;
10297 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10298 DefaultScreen (dpyinfo->display));
10299 select_visual (dpyinfo);
10300 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10301 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10302 dpyinfo->client_leader_window = 0;
10303 dpyinfo->grabbed = 0;
10304 dpyinfo->reference_count = 0;
10305 dpyinfo->icon_bitmap_id = -1;
10306 dpyinfo->n_fonts = 0;
10307 dpyinfo->bitmaps = 0;
10308 dpyinfo->bitmaps_size = 0;
10309 dpyinfo->bitmaps_last = 0;
10310 dpyinfo->scratch_cursor_gc = 0;
10311 dpyinfo->mouse_face_mouse_frame = 0;
10312 dpyinfo->mouse_face_deferred_gc = 0;
10313 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10314 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10315 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10316 dpyinfo->mouse_face_window = Qnil;
10317 dpyinfo->mouse_face_overlay = Qnil;
10318 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10319 dpyinfo->mouse_face_defer = 0;
10320 dpyinfo->mouse_face_hidden = 0;
10321 dpyinfo->x_focus_frame = 0;
10322 dpyinfo->x_focus_event_frame = 0;
10323 dpyinfo->x_highlight_frame = 0;
10324 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10326 /* See if we can construct pixel values from RGB values. */
10327 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10328 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10330 if (dpyinfo->visual->class == TrueColor)
10332 get_bits_and_offset (dpyinfo->visual->red_mask,
10333 &dpyinfo->red_bits, &dpyinfo->red_offset);
10334 get_bits_and_offset (dpyinfo->visual->blue_mask,
10335 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10336 get_bits_and_offset (dpyinfo->visual->green_mask,
10337 &dpyinfo->green_bits, &dpyinfo->green_offset);
10340 /* See if a private colormap is requested. */
10341 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10343 if (dpyinfo->visual->class == PseudoColor)
10345 Lisp_Object value;
10346 value = display_x_get_resource (dpyinfo,
10347 build_string ("privateColormap"),
10348 build_string ("PrivateColormap"),
10349 Qnil, Qnil);
10350 if (STRINGP (value)
10351 && (!strcmp (SDATA (value), "true")
10352 || !strcmp (SDATA (value), "on")))
10353 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10356 else
10357 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10358 dpyinfo->visual, AllocNone);
10361 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10362 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10363 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10364 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10365 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10366 pixels = DisplayWidth (dpyinfo->display, screen_number);
10367 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10368 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10369 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10372 dpyinfo->Xatom_wm_protocols
10373 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10374 dpyinfo->Xatom_wm_take_focus
10375 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10376 dpyinfo->Xatom_wm_save_yourself
10377 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10378 dpyinfo->Xatom_wm_delete_window
10379 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10380 dpyinfo->Xatom_wm_change_state
10381 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10382 dpyinfo->Xatom_wm_configure_denied
10383 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10384 dpyinfo->Xatom_wm_window_moved
10385 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10386 dpyinfo->Xatom_wm_client_leader
10387 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10388 dpyinfo->Xatom_editres
10389 = XInternAtom (dpyinfo->display, "Editres", False);
10390 dpyinfo->Xatom_CLIPBOARD
10391 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10392 dpyinfo->Xatom_TIMESTAMP
10393 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10394 dpyinfo->Xatom_TEXT
10395 = XInternAtom (dpyinfo->display, "TEXT", False);
10396 dpyinfo->Xatom_COMPOUND_TEXT
10397 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10398 dpyinfo->Xatom_UTF8_STRING
10399 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10400 dpyinfo->Xatom_DELETE
10401 = XInternAtom (dpyinfo->display, "DELETE", False);
10402 dpyinfo->Xatom_MULTIPLE
10403 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10404 dpyinfo->Xatom_INCR
10405 = XInternAtom (dpyinfo->display, "INCR", False);
10406 dpyinfo->Xatom_EMACS_TMP
10407 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10408 dpyinfo->Xatom_TARGETS
10409 = XInternAtom (dpyinfo->display, "TARGETS", False);
10410 dpyinfo->Xatom_NULL
10411 = XInternAtom (dpyinfo->display, "NULL", False);
10412 dpyinfo->Xatom_ATOM_PAIR
10413 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10414 /* For properties of font. */
10415 dpyinfo->Xatom_PIXEL_SIZE
10416 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10417 dpyinfo->Xatom_AVERAGE_WIDTH
10418 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10419 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10420 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10421 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10422 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10423 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10424 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10426 /* Ghostscript support. */
10427 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10428 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10430 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10431 False);
10433 dpyinfo->Xatom_XEMBED = XInternAtom (dpyinfo->display, "_XEMBED",
10434 False);
10436 dpyinfo->Xatom_net_wm_state
10437 = XInternAtom (dpyinfo->display, "_NET_WM_STATE", False);
10438 dpyinfo->Xatom_net_wm_state_fullscreen_atom
10439 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_FULLSCREEN", False);
10440 dpyinfo->Xatom_net_wm_state_maximized_horz
10441 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
10442 dpyinfo->Xatom_net_wm_state_maximized_vert
10443 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
10445 dpyinfo->cut_buffers_initialized = 0;
10447 dpyinfo->x_dnd_atoms_size = 8;
10448 dpyinfo->x_dnd_atoms_length = 0;
10449 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10450 * dpyinfo->x_dnd_atoms_size);
10452 dpyinfo->net_supported_atoms = NULL;
10453 dpyinfo->nr_net_supported_atoms = 0;
10454 dpyinfo->net_supported_window = 0;
10456 connection = ConnectionNumber (dpyinfo->display);
10457 dpyinfo->connection = connection;
10460 extern int gray_bitmap_width, gray_bitmap_height;
10461 extern char *gray_bitmap_bits;
10462 dpyinfo->gray
10463 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10464 gray_bitmap_bits,
10465 gray_bitmap_width, gray_bitmap_height,
10466 (unsigned long) 1, (unsigned long) 0, 1);
10469 #ifdef HAVE_X_I18N
10470 xim_initialize (dpyinfo, resource_name);
10471 #endif
10473 #ifdef subprocesses
10474 /* This is only needed for distinguishing keyboard and process input. */
10475 if (connection != 0)
10476 add_keyboard_wait_descriptor (connection);
10477 #endif
10479 #ifdef F_SETOWN
10480 fcntl (connection, F_SETOWN, getpid ());
10481 #endif /* ! defined (F_SETOWN) */
10483 #ifdef SIGIO
10484 if (interrupt_input)
10485 init_sigio (connection);
10486 #endif /* ! defined (SIGIO) */
10488 #ifdef USE_LUCID
10490 Display *dpy = dpyinfo->display;
10491 XrmValue d, fr, to;
10492 Font font;
10494 d.addr = (XPointer)&dpy;
10495 d.size = sizeof (Display *);
10496 fr.addr = XtDefaultFont;
10497 fr.size = sizeof (XtDefaultFont);
10498 to.size = sizeof (Font *);
10499 to.addr = (XPointer)&font;
10500 x_catch_errors (dpy);
10501 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10502 abort ();
10503 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10504 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10505 x_uncatch_errors ();
10507 #endif
10509 /* See if we should run in synchronous mode. This is useful
10510 for debugging X code. */
10512 Lisp_Object value;
10513 value = display_x_get_resource (dpyinfo,
10514 build_string ("synchronous"),
10515 build_string ("Synchronous"),
10516 Qnil, Qnil);
10517 if (STRINGP (value)
10518 && (!strcmp (SDATA (value), "true")
10519 || !strcmp (SDATA (value), "on")))
10520 XSynchronize (dpyinfo->display, True);
10524 Lisp_Object value;
10525 value = display_x_get_resource (dpyinfo,
10526 build_string ("useXIM"),
10527 build_string ("UseXIM"),
10528 Qnil, Qnil);
10529 #ifdef USE_XIM
10530 if (STRINGP (value)
10531 && (!strcmp (SDATA (value), "false")
10532 || !strcmp (SDATA (value), "off")))
10533 use_xim = 0;
10534 #else
10535 if (STRINGP (value)
10536 && (!strcmp (SDATA (value), "true")
10537 || !strcmp (SDATA (value), "on")))
10538 use_xim = 1;
10539 #endif
10542 #ifdef HAVE_X_SM
10543 /* Only do this for the very first display in the Emacs session.
10544 Ignore X session management when Emacs was first started on a
10545 tty. */
10546 if (terminal->id == 1)
10547 x_session_initialize (dpyinfo);
10548 #endif
10550 UNBLOCK_INPUT;
10552 return dpyinfo;
10555 /* Get rid of display DPYINFO, deleting all frames on it,
10556 and without sending any more commands to the X server. */
10558 void
10559 x_delete_display (dpyinfo)
10560 struct x_display_info *dpyinfo;
10562 int i;
10563 struct terminal *t;
10565 /* Close all frames and delete the generic struct terminal for this
10566 X display. */
10567 for (t = terminal_list; t; t = t->next_terminal)
10568 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10570 #ifdef HAVE_X_SM
10571 /* Close X session management when we close its display. */
10572 if (t->id == 1 && x_session_have_connection ())
10573 x_session_close();
10574 #endif
10575 delete_terminal (t);
10576 break;
10579 delete_keyboard_wait_descriptor (dpyinfo->connection);
10581 /* Discard this display from x_display_name_list and x_display_list.
10582 We can't use Fdelq because that can quit. */
10583 if (! NILP (x_display_name_list)
10584 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10585 x_display_name_list = XCDR (x_display_name_list);
10586 else
10588 Lisp_Object tail;
10590 tail = x_display_name_list;
10591 while (CONSP (tail) && CONSP (XCDR (tail)))
10593 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10595 XSETCDR (tail, XCDR (XCDR (tail)));
10596 break;
10598 tail = XCDR (tail);
10602 if (next_noop_dpyinfo == dpyinfo)
10603 next_noop_dpyinfo = dpyinfo->next;
10605 if (x_display_list == dpyinfo)
10606 x_display_list = dpyinfo->next;
10607 else
10609 struct x_display_info *tail;
10611 for (tail = x_display_list; tail; tail = tail->next)
10612 if (tail->next == dpyinfo)
10613 tail->next = tail->next->next;
10616 xfree (dpyinfo->x_id_name);
10617 xfree (dpyinfo->x_dnd_atoms);
10618 xfree (dpyinfo->color_cells);
10619 xfree (dpyinfo);
10622 #ifdef USE_X_TOOLKIT
10624 /* Atimer callback function for TIMER. Called every 0.1s to process
10625 Xt timeouts, if needed. We must avoid calling XtAppPending as
10626 much as possible because that function does an implicit XFlush
10627 that slows us down. */
10629 static void
10630 x_process_timeouts (timer)
10631 struct atimer *timer;
10633 BLOCK_INPUT;
10634 x_timeout_atimer_activated_flag = 0;
10635 if (toolkit_scroll_bar_interaction || popup_activated ())
10637 while (XtAppPending (Xt_app_con) & XtIMTimer)
10638 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10639 /* Reactivate the atimer for next time. */
10640 x_activate_timeout_atimer ();
10642 UNBLOCK_INPUT;
10645 /* Install an asynchronous timer that processes Xt timeout events
10646 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10647 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10648 function whenever these variables are set. This is necessary
10649 because some widget sets use timeouts internally, for example the
10650 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10651 processed, these widgets don't behave normally. */
10653 void
10654 x_activate_timeout_atimer ()
10656 BLOCK_INPUT;
10657 if (!x_timeout_atimer_activated_flag)
10659 EMACS_TIME interval;
10661 EMACS_SET_SECS_USECS (interval, 0, 100000);
10662 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10663 x_timeout_atimer_activated_flag = 1;
10665 UNBLOCK_INPUT;
10668 #endif /* USE_X_TOOLKIT */
10671 /* Set up use of X before we make the first connection. */
10673 extern frame_parm_handler x_frame_parm_handlers[];
10675 static struct redisplay_interface x_redisplay_interface =
10677 x_frame_parm_handlers,
10678 x_produce_glyphs,
10679 x_write_glyphs,
10680 x_insert_glyphs,
10681 x_clear_end_of_line,
10682 x_scroll_run,
10683 x_after_update_window_line,
10684 x_update_window_begin,
10685 x_update_window_end,
10686 x_cursor_to,
10687 x_flush,
10688 #ifdef XFlush
10689 x_flush,
10690 #else
10691 0, /* flush_display_optional */
10692 #endif
10693 x_clear_window_mouse_face,
10694 x_get_glyph_overhangs,
10695 x_fix_overlapping_area,
10696 x_draw_fringe_bitmap,
10697 0, /* define_fringe_bitmap */
10698 0, /* destroy_fringe_bitmap */
10699 x_compute_glyph_string_overhangs,
10700 x_draw_glyph_string,
10701 x_define_frame_cursor,
10702 x_clear_frame_area,
10703 x_draw_window_cursor,
10704 x_draw_vertical_window_border,
10705 x_shift_glyphs_for_insert
10709 /* This function is called when the last frame on a display is deleted. */
10710 void
10711 x_delete_terminal (struct terminal *terminal)
10713 struct x_display_info *dpyinfo = terminal->display_info.x;
10714 int i;
10716 /* Protect against recursive calls. delete_frame in
10717 delete_terminal calls us back when it deletes our last frame. */
10718 if (!terminal->name)
10719 return;
10721 BLOCK_INPUT;
10722 #ifdef HAVE_X_I18N
10723 /* We must close our connection to the XIM server before closing the
10724 X display. */
10725 if (dpyinfo->xim)
10726 xim_close_dpy (dpyinfo);
10727 #endif
10729 /* If called from x_connection_closed, the display may already be closed
10730 and dpyinfo->display was set to 0 to indicate that. */
10731 if (dpyinfo->display)
10733 x_destroy_all_bitmaps (dpyinfo);
10734 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10736 /* Whether or not XCloseDisplay destroys the associated resource
10737 database depends on the version of libX11. To avoid both
10738 crash and memory leak, we dissociate the database from the
10739 display and then destroy dpyinfo->xrdb ourselves. */
10740 #ifdef HAVE_XRMSETDATABASE
10741 XrmSetDatabase (dpyinfo->display, NULL);
10742 #else
10743 dpyinfo->display->db = NULL;
10744 #endif
10745 /* We used to call XrmDestroyDatabase from x_delete_display, but
10746 some older versions of libX11 crash if we call it after
10747 closing all the displays. */
10748 XrmDestroyDatabase (dpyinfo->xrdb);
10750 #ifdef USE_GTK
10751 xg_display_close (dpyinfo->display);
10752 #else
10753 #ifdef USE_X_TOOLKIT
10754 XtCloseDisplay (dpyinfo->display);
10755 #else
10756 XCloseDisplay (dpyinfo->display);
10757 #endif
10758 #endif /* ! USE_GTK */
10761 x_delete_display (dpyinfo);
10762 UNBLOCK_INPUT;
10765 /* Create a struct terminal, initialize it with the X11 specific
10766 functions and make DISPLAY->TERMINAL point to it. */
10768 static struct terminal *
10769 x_create_terminal (struct x_display_info *dpyinfo)
10771 struct terminal *terminal;
10773 terminal = create_terminal ();
10775 terminal->type = output_x_window;
10776 terminal->display_info.x = dpyinfo;
10777 dpyinfo->terminal = terminal;
10779 /* kboard is initialized in x_term_init. */
10781 terminal->clear_frame_hook = x_clear_frame;
10782 terminal->ins_del_lines_hook = x_ins_del_lines;
10783 terminal->delete_glyphs_hook = x_delete_glyphs;
10784 terminal->ring_bell_hook = XTring_bell;
10785 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
10786 terminal->set_terminal_modes_hook = XTset_terminal_modes;
10787 terminal->update_begin_hook = x_update_begin;
10788 terminal->update_end_hook = x_update_end;
10789 terminal->set_terminal_window_hook = XTset_terminal_window;
10790 terminal->read_socket_hook = XTread_socket;
10791 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10792 terminal->mouse_position_hook = XTmouse_position;
10793 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10794 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10795 terminal->fullscreen_hook = XTfullscreen_hook;
10796 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10797 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10798 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10799 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10801 terminal->delete_frame_hook = x_destroy_window;
10802 terminal->delete_terminal_hook = x_delete_terminal;
10804 terminal->rif = &x_redisplay_interface;
10805 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10806 terminal->char_ins_del_ok = 1;
10807 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10808 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10809 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10810 off the bottom. */
10812 return terminal;
10815 void
10816 x_initialize ()
10818 baud_rate = 19200;
10820 x_noop_count = 0;
10821 last_tool_bar_item = -1;
10822 any_help_event_p = 0;
10823 ignore_next_mouse_click_timeout = 0;
10824 #ifdef HAVE_X_SM
10825 x_session_initialized = 0;
10826 #endif
10828 #ifdef USE_GTK
10829 current_count = -1;
10830 #endif
10832 /* Try to use interrupt input; if we can't, then start polling. */
10833 Fset_input_interrupt_mode (Qt);
10835 #ifdef USE_X_TOOLKIT
10836 XtToolkitInitialize ();
10838 Xt_app_con = XtCreateApplicationContext ();
10840 /* Register a converter from strings to pixels, which uses
10841 Emacs' color allocation infrastructure. */
10842 XtAppSetTypeConverter (Xt_app_con,
10843 XtRString, XtRPixel, cvt_string_to_pixel,
10844 cvt_string_to_pixel_args,
10845 XtNumber (cvt_string_to_pixel_args),
10846 XtCacheByDisplay, cvt_pixel_dtor);
10848 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10849 #endif
10851 #ifdef USE_TOOLKIT_SCROLL_BARS
10852 #ifndef USE_GTK
10853 xaw3d_arrow_scroll = False;
10854 xaw3d_pick_top = True;
10855 #endif
10856 #endif
10858 /* Note that there is no real way portable across R3/R4 to get the
10859 original error handler. */
10860 XSetErrorHandler (x_error_handler);
10861 XSetIOErrorHandler (x_io_error_quitter);
10863 /* Disable Window Change signals; they are handled by X events. */
10864 #if 0 /* Don't. We may want to open tty frames later. */
10865 #ifdef SIGWINCH
10866 signal (SIGWINCH, SIG_DFL);
10867 #endif /* SIGWINCH */
10868 #endif
10870 signal (SIGPIPE, x_connection_signal);
10874 void
10875 syms_of_xterm ()
10877 x_error_message = NULL;
10879 staticpro (&x_display_name_list);
10880 x_display_name_list = Qnil;
10882 staticpro (&last_mouse_scroll_bar);
10883 last_mouse_scroll_bar = Qnil;
10885 staticpro (&Qvendor_specific_keysyms);
10886 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10888 staticpro (&Qlatin_1);
10889 Qlatin_1 = intern ("latin-1");
10891 staticpro (&last_mouse_press_frame);
10892 last_mouse_press_frame = Qnil;
10894 DEFVAR_BOOL ("x-use-underline-position-properties",
10895 &x_use_underline_position_properties,
10896 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10897 A value of nil means ignore them. If you encounter fonts with bogus
10898 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10899 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10900 to override the font's UNDERLINE_POSITION for small font display
10901 sizes. */);
10902 x_use_underline_position_properties = 1;
10904 DEFVAR_BOOL ("x-underline-at-descent-line",
10905 &x_underline_at_descent_line,
10906 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
10907 A value of nil means to draw the underline according to the value of the
10908 variable `x-use-underline-position-properties', which is usually at the
10909 baseline level. The default value is nil. */);
10910 x_underline_at_descent_line = 0;
10912 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10913 &x_mouse_click_focus_ignore_position,
10914 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10915 This variable is only used when the window manager requires that you
10916 click on a frame to select it (give it focus). In that case, a value
10917 of nil, means that the selected window and cursor position changes to
10918 reflect the mouse click position, while a non-nil value means that the
10919 selected window or cursor position is preserved. */);
10920 x_mouse_click_focus_ignore_position = 0;
10922 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10923 doc: /* What X toolkit scroll bars Emacs uses.
10924 A value of nil means Emacs doesn't use X toolkit scroll bars.
10925 Otherwise, value is a symbol describing the X toolkit. */);
10926 #ifdef USE_TOOLKIT_SCROLL_BARS
10927 #ifdef USE_MOTIF
10928 Vx_toolkit_scroll_bars = intern ("motif");
10929 #elif defined HAVE_XAW3D
10930 Vx_toolkit_scroll_bars = intern ("xaw3d");
10931 #elif USE_GTK
10932 Vx_toolkit_scroll_bars = intern ("gtk");
10933 #else
10934 Vx_toolkit_scroll_bars = intern ("xaw");
10935 #endif
10936 #else
10937 Vx_toolkit_scroll_bars = Qnil;
10938 #endif
10940 staticpro (&last_mouse_motion_frame);
10941 last_mouse_motion_frame = Qnil;
10943 Qmodifier_value = intern ("modifier-value");
10944 Qalt = intern ("alt");
10945 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10946 Qhyper = intern ("hyper");
10947 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10948 Qmeta = intern ("meta");
10949 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10950 Qsuper = intern ("super");
10951 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10953 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10954 doc: /* Which keys Emacs uses for the alt modifier.
10955 This should be one of the symbols `alt', `hyper', `meta', `super'.
10956 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10957 is nil, which is the same as `alt'. */);
10958 Vx_alt_keysym = Qnil;
10960 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
10961 doc: /* Which keys Emacs uses for the hyper modifier.
10962 This should be one of the symbols `alt', `hyper', `meta', `super'.
10963 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10964 default is nil, which is the same as `hyper'. */);
10965 Vx_hyper_keysym = Qnil;
10967 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
10968 doc: /* Which keys Emacs uses for the meta modifier.
10969 This should be one of the symbols `alt', `hyper', `meta', `super'.
10970 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10971 default is nil, which is the same as `meta'. */);
10972 Vx_meta_keysym = Qnil;
10974 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
10975 doc: /* Which keys Emacs uses for the super modifier.
10976 This should be one of the symbols `alt', `hyper', `meta', `super'.
10977 For example, `super' means use the Super_L and Super_R keysyms. The
10978 default is nil, which is the same as `super'. */);
10979 Vx_super_keysym = Qnil;
10981 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
10982 doc: /* Hash table of character codes indexed by X keysym codes. */);
10983 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
10984 make_float (DEFAULT_REHASH_SIZE),
10985 make_float (DEFAULT_REHASH_THRESHOLD),
10986 Qnil, Qnil, Qnil);
10989 #endif /* HAVE_X_WINDOWS */
10991 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
10992 (do not change this comment) */