(pcomplete-common-suffix, pcomplete-table-subvert): New funs.
[emacs.git] / src / xterm.c
blob16aa12abbaa0d0874c38dceb0f204f9820dbd5fe
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>
31 #include <setjmp.h>
33 #ifdef HAVE_X_WINDOWS
35 #include "lisp.h"
36 #include "blockinput.h"
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
57 #include "systime.h"
59 #ifndef INCLUDED_FCNTL
60 #include <fcntl.h>
61 #endif
62 #include <ctype.h>
63 #include <errno.h>
64 #include <setjmp.h>
65 #include <sys/stat.h>
66 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
67 /* #include <sys/param.h> */
69 #include "charset.h"
70 #include "character.h"
71 #include "coding.h"
72 #include "frame.h"
73 #include "dispextern.h"
74 #include "fontset.h"
75 #include "termhooks.h"
76 #include "termopts.h"
77 #include "termchar.h"
78 #include "emacs-icon.h"
79 #include "disptab.h"
80 #include "buffer.h"
81 #include "window.h"
82 #include "keyboard.h"
83 #include "intervals.h"
84 #include "process.h"
85 #include "atimer.h"
86 #include "keymap.h"
87 #include "font.h"
88 #include "fontset.h"
89 #include "sysselect.h"
91 #ifdef USE_X_TOOLKIT
92 #include <X11/Shell.h>
93 #endif
95 #ifdef HAVE_SYS_TIME_H
96 #include <sys/time.h>
97 #endif
98 #ifdef HAVE_UNISTD_H
99 #include <unistd.h>
100 #endif
102 #ifdef USE_GTK
103 #include "gtkutil.h"
104 #endif
106 #ifdef USE_LUCID
107 extern int xlwmenu_window_p P_ ((Widget w, Window window));
108 extern void xlwmenu_redisplay P_ ((Widget));
109 #endif
111 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
113 extern void free_frame_menubar P_ ((struct frame *));
114 #endif
116 #ifdef USE_X_TOOLKIT
117 #if !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 #ifdef USE_X_TOOLKIT
147 #include "widget.h"
148 #ifndef XtNinitialState
149 #define XtNinitialState "initialState"
150 #endif
151 #endif
153 /* Default to using XIM if available. */
154 #ifdef USE_XIM
155 int use_xim = 1;
156 #else
157 int use_xim = 0; /* configure --without-xim */
158 #endif
162 /* Non-nil means Emacs uses toolkit scroll bars. */
164 Lisp_Object Vx_toolkit_scroll_bars;
166 /* Non-zero means that a HELP_EVENT has been generated since Emacs
167 start. */
169 static int any_help_event_p;
171 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
172 static Lisp_Object last_window;
174 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
176 int x_use_underline_position_properties;
178 /* Non-zero means to draw the underline at the same place as the descent line. */
180 int x_underline_at_descent_line;
182 /* This is a chain of structures for all the X displays currently in
183 use. */
185 struct x_display_info *x_display_list;
187 /* This is a list of cons cells, each of the form (NAME
188 . FONT-LIST-CACHE), one for each element of x_display_list and in
189 the same order. NAME is the name of the frame. FONT-LIST-CACHE
190 records previous values returned by x-list-fonts. */
192 Lisp_Object x_display_name_list;
194 /* Frame being updated by update_frame. This is declared in term.c.
195 This is set by update_begin and looked at by all the XT functions.
196 It is zero while not inside an update. In that case, the XT
197 functions assume that `selected_frame' is the frame to apply to. */
199 extern struct frame *updating_frame;
201 /* This is a frame waiting to be auto-raised, within XTread_socket. */
203 static struct frame *pending_autoraise_frame;
205 /* This is a frame waiting for an event matching mask, within XTread_socket. */
207 static struct {
208 struct frame *f;
209 int eventtype;
210 } pending_event_wait;
212 #ifdef USE_X_TOOLKIT
213 /* The application context for Xt use. */
214 XtAppContext Xt_app_con;
215 static String Xt_default_resources[] = {0};
216 #endif /* USE_X_TOOLKIT */
218 /* Non-zero means user is interacting with a toolkit scroll bar. */
220 static int toolkit_scroll_bar_interaction;
222 /* Non-zero means to not move point as a result of clicking on a
223 frame to focus it (when focus-follows-mouse is nil). */
225 int x_mouse_click_focus_ignore_position;
227 /* Non-zero timeout value means ignore next mouse click if it arrives
228 before that timeout elapses (i.e. as part of the same sequence of
229 events resulting from clicking on a frame to select it). */
231 static unsigned long ignore_next_mouse_click_timeout;
233 /* Mouse movement.
235 Formerly, we used PointerMotionHintMask (in standard_event_mask)
236 so that we would have to call XQueryPointer after each MotionNotify
237 event to ask for another such event. However, this made mouse tracking
238 slow, and there was a bug that made it eventually stop.
240 Simply asking for MotionNotify all the time seems to work better.
242 In order to avoid asking for motion events and then throwing most
243 of them away or busy-polling the server for mouse positions, we ask
244 the server for pointer motion hints. This means that we get only
245 one event per group of mouse movements. "Groups" are delimited by
246 other kinds of events (focus changes and button clicks, for
247 example), or by XQueryPointer calls; when one of these happens, we
248 get another MotionNotify event the next time the mouse moves. This
249 is at least as efficient as getting motion events when mouse
250 tracking is on, and I suspect only negligibly worse when tracking
251 is off. */
253 /* Where the mouse was last time we reported a mouse event. */
255 static XRectangle last_mouse_glyph;
256 static FRAME_PTR last_mouse_glyph_frame;
257 static Lisp_Object last_mouse_press_frame;
259 /* The scroll bar in which the last X motion event occurred.
261 If the last X motion event occurred in a scroll bar, we set this so
262 XTmouse_position can know whether to report a scroll bar motion or
263 an ordinary motion.
265 If the last X motion event didn't occur in a scroll bar, we set
266 this to Qnil, to tell XTmouse_position to return an ordinary motion
267 event. */
269 static Lisp_Object last_mouse_scroll_bar;
271 /* This is a hack. We would really prefer that XTmouse_position would
272 return the time associated with the position it returns, but there
273 doesn't seem to be any way to wrest the time-stamp from the server
274 along with the position query. So, we just keep track of the time
275 of the last movement we received, and return that in hopes that
276 it's somewhat accurate. */
278 static Time last_mouse_movement_time;
280 /* Time for last user interaction as returned in X events. */
282 static Time last_user_time;
284 /* Incremented by XTread_socket whenever it really tries to read
285 events. */
287 #ifdef __STDC__
288 static int volatile input_signal_count;
289 #else
290 static int input_signal_count;
291 #endif
293 /* Used locally within XTread_socket. */
295 static int x_noop_count;
297 /* Initial values of argv and argc. */
299 extern char **initial_argv;
300 extern int initial_argc;
302 extern Lisp_Object Vcommand_line_args, Vsystem_name;
304 /* Tells if a window manager is present or not. */
306 extern Lisp_Object Vx_no_window_manager;
308 extern Lisp_Object Qeql;
310 extern int errno;
312 /* A mask of extra modifier bits to put into every keyboard char. */
314 extern EMACS_INT extra_keyboard_modifiers;
316 /* The keysyms to use for the various modifiers. */
318 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
319 Lisp_Object Vx_keysym_table;
320 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
322 static Lisp_Object Qvendor_specific_keysyms;
323 static Lisp_Object Qlatin_1;
325 #ifdef USE_GTK
326 /* The name of the Emacs icon file. */
327 static Lisp_Object xg_default_icon_file;
329 /* Used in gtkutil.c. */
330 Lisp_Object Qx_gtk_map_stock;
331 #endif
333 /* Used in x_flush. */
335 extern Lisp_Object Vinhibit_redisplay;
337 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
338 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
340 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
341 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
342 static const XColor *x_color_cells P_ ((Display *, int *));
343 static void x_update_window_end P_ ((struct window *, int, int));
345 static int x_io_error_quitter P_ ((Display *));
346 static struct terminal *x_create_terminal P_ ((struct x_display_info *));
347 void x_delete_terminal P_ ((struct terminal *));
348 static void x_update_end P_ ((struct frame *));
349 static void XTframe_up_to_date P_ ((struct frame *));
350 static void XTset_terminal_modes P_ ((struct terminal *));
351 static void XTreset_terminal_modes P_ ((struct terminal *));
352 static void x_clear_frame P_ ((struct frame *));
353 static void frame_highlight P_ ((struct frame *));
354 static void frame_unhighlight P_ ((struct frame *));
355 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
356 static void x_focus_changed P_ ((int, int, struct x_display_info *,
357 struct frame *, struct input_event *));
358 static void x_detect_focus_change P_ ((struct x_display_info *,
359 XEvent *, struct input_event *));
360 static void XTframe_rehighlight P_ ((struct frame *));
361 static void x_frame_rehighlight P_ ((struct x_display_info *));
362 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
363 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
364 enum text_cursor_kinds));
366 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
367 static void x_flush P_ ((struct frame *f));
368 static void x_update_begin P_ ((struct frame *));
369 static void x_update_window_begin P_ ((struct window *));
370 static void x_after_update_window_line P_ ((struct glyph_row *));
371 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
372 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
373 enum scroll_bar_part *,
374 Lisp_Object *, Lisp_Object *,
375 unsigned long *));
376 static void x_handle_net_wm_state P_ ((struct frame *, XPropertyEvent *));
377 static void x_check_fullscreen P_ ((struct frame *));
378 static void x_check_expected_move P_ ((struct frame *, int, int));
379 static void x_sync_with_move P_ ((struct frame *, int, int, int));
380 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
381 int *, struct input_event *));
382 /* Don't declare this NO_RETURN because we want no
383 interference with debugging failing X calls. */
384 static SIGTYPE x_connection_closed P_ ((Display *, char *));
387 /* Flush display of frame F, or of all frames if F is null. */
389 static void
390 x_flush (f)
391 struct frame *f;
393 /* Don't call XFlush when it is not safe to redisplay; the X
394 connection may be broken. */
395 if (!NILP (Vinhibit_redisplay))
396 return;
398 BLOCK_INPUT;
399 if (f == NULL)
401 Lisp_Object rest, frame;
402 FOR_EACH_FRAME (rest, frame)
403 if (FRAME_X_P (XFRAME (frame)))
404 x_flush (XFRAME (frame));
406 else if (FRAME_X_P (f))
407 XFlush (FRAME_X_DISPLAY (f));
408 UNBLOCK_INPUT;
412 /* Remove calls to XFlush by defining XFlush to an empty replacement.
413 Calls to XFlush should be unnecessary because the X output buffer
414 is flushed automatically as needed by calls to XPending,
415 XNextEvent, or XWindowEvent according to the XFlush man page.
416 XTread_socket calls XPending. Removing XFlush improves
417 performance. */
419 #define XFlush(DISPLAY) (void) 0
422 /***********************************************************************
423 Debugging
424 ***********************************************************************/
426 #if 0
428 /* This is a function useful for recording debugging information about
429 the sequence of occurrences in this file. */
431 struct record
433 char *locus;
434 int type;
437 struct record event_record[100];
439 int event_record_index;
441 record_event (locus, type)
442 char *locus;
443 int type;
445 if (event_record_index == sizeof (event_record) / sizeof (struct record))
446 event_record_index = 0;
448 event_record[event_record_index].locus = locus;
449 event_record[event_record_index].type = type;
450 event_record_index++;
453 #endif /* 0 */
457 /* Return the struct x_display_info corresponding to DPY. */
459 struct x_display_info *
460 x_display_info_for_display (dpy)
461 Display *dpy;
463 struct x_display_info *dpyinfo;
465 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
466 if (dpyinfo->display == dpy)
467 return dpyinfo;
469 return 0;
472 #define OPAQUE 0xffffffff
473 #define OPACITY "_NET_WM_WINDOW_OPACITY"
475 void
476 x_set_frame_alpha (f)
477 struct frame *f;
479 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
480 Display *dpy = FRAME_X_DISPLAY (f);
481 Window win = FRAME_OUTER_WINDOW (f);
482 double alpha = 1.0;
483 double alpha_min = 1.0;
484 unsigned long opac;
486 if (FRAME_X_DISPLAY_INFO (f)->root_window != FRAME_X_OUTPUT (f)->parent_desc)
487 /* Since the WM decoration lies under the FRAME_OUTER_WINDOW,
488 we must treat the former instead of the latter. */
489 win = FRAME_X_OUTPUT(f)->parent_desc;
491 if (dpyinfo->x_highlight_frame == f)
492 alpha = f->alpha[0];
493 else
494 alpha = f->alpha[1];
496 if (FLOATP (Vframe_alpha_lower_limit))
497 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
498 else if (INTEGERP (Vframe_alpha_lower_limit))
499 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
501 if (alpha < 0.0)
502 return;
503 else if (alpha > 1.0)
504 alpha = 1.0;
505 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
506 alpha = alpha_min;
508 opac = alpha * OPAQUE;
510 /* return unless necessary */
512 unsigned char *data;
513 Atom actual;
514 int rc, format;
515 unsigned long n, left;
517 x_catch_errors (dpy);
518 rc = XGetWindowProperty(dpy, win, XInternAtom(dpy, OPACITY, False),
519 0L, 1L, False, XA_CARDINAL,
520 &actual, &format, &n, &left,
521 &data);
523 if (rc == Success && actual != None)
524 if (*(unsigned long *)data == opac)
526 XFree ((void *) data);
527 x_uncatch_errors ();
528 return;
530 else
531 XFree ((void *) data);
532 x_uncatch_errors ();
535 x_catch_errors (dpy);
536 XChangeProperty (dpy, win, XInternAtom (dpy, OPACITY, False),
537 XA_CARDINAL, 32, PropModeReplace,
538 (unsigned char *) &opac, 1L);
539 x_uncatch_errors ();
543 x_display_pixel_height (dpyinfo)
544 struct x_display_info *dpyinfo;
546 return HeightOfScreen (dpyinfo->screen);
550 x_display_pixel_width (dpyinfo)
551 struct x_display_info *dpyinfo;
553 return WidthOfScreen (dpyinfo->screen);
557 /***********************************************************************
558 Starting and ending an update
559 ***********************************************************************/
561 /* Start an update of frame F. This function is installed as a hook
562 for update_begin, i.e. it is called when update_begin is called.
563 This function is called prior to calls to x_update_window_begin for
564 each window being updated. Currently, there is nothing to do here
565 because all interesting stuff is done on a window basis. */
567 static void
568 x_update_begin (f)
569 struct frame *f;
571 /* Nothing to do. */
575 /* Start update of window W. Set the global variable updated_window
576 to the window being updated and set output_cursor to the cursor
577 position of W. */
579 static void
580 x_update_window_begin (w)
581 struct window *w;
583 struct frame *f = XFRAME (WINDOW_FRAME (w));
584 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
586 updated_window = w;
587 set_output_cursor (&w->cursor);
589 BLOCK_INPUT;
591 if (f == display_info->mouse_face_mouse_frame)
593 /* Don't do highlighting for mouse motion during the update. */
594 display_info->mouse_face_defer = 1;
596 /* If F needs to be redrawn, simply forget about any prior mouse
597 highlighting. */
598 if (FRAME_GARBAGED_P (f))
599 display_info->mouse_face_window = Qnil;
602 UNBLOCK_INPUT;
606 /* Draw a vertical window border from (x,y0) to (x,y1) */
608 static void
609 x_draw_vertical_window_border (w, x, y0, y1)
610 struct window *w;
611 int x, y0, y1;
613 struct frame *f = XFRAME (WINDOW_FRAME (w));
614 struct face *face;
616 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
617 if (face)
618 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
619 face->foreground);
621 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
622 f->output_data.x->normal_gc, x, y0, x, y1);
625 /* End update of window W (which is equal to updated_window).
627 Draw vertical borders between horizontally adjacent windows, and
628 display W's cursor if CURSOR_ON_P is non-zero.
630 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
631 glyphs in mouse-face were overwritten. In that case we have to
632 make sure that the mouse-highlight is properly redrawn.
634 W may be a menu bar pseudo-window in case we don't have X toolkit
635 support. Such windows don't have a cursor, so don't display it
636 here. */
638 static void
639 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
640 struct window *w;
641 int cursor_on_p, mouse_face_overwritten_p;
643 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
645 if (!w->pseudo_window_p)
647 BLOCK_INPUT;
649 if (cursor_on_p)
650 display_and_set_cursor (w, 1, output_cursor.hpos,
651 output_cursor.vpos,
652 output_cursor.x, output_cursor.y);
654 if (draw_window_fringes (w, 1))
655 x_draw_vertical_border (w);
657 UNBLOCK_INPUT;
660 /* If a row with mouse-face was overwritten, arrange for
661 XTframe_up_to_date to redisplay the mouse highlight. */
662 if (mouse_face_overwritten_p)
664 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
665 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
666 dpyinfo->mouse_face_window = Qnil;
669 updated_window = NULL;
673 /* End update of frame F. This function is installed as a hook in
674 update_end. */
676 static void
677 x_update_end (f)
678 struct frame *f;
680 /* Mouse highlight may be displayed again. */
681 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
683 #ifndef XFlush
684 BLOCK_INPUT;
685 XFlush (FRAME_X_DISPLAY (f));
686 UNBLOCK_INPUT;
687 #endif
691 /* This function is called from various places in xdisp.c whenever a
692 complete update has been performed. The global variable
693 updated_window is not available here. */
695 static void
696 XTframe_up_to_date (f)
697 struct frame *f;
699 if (FRAME_X_P (f))
701 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
703 if (dpyinfo->mouse_face_deferred_gc
704 || f == dpyinfo->mouse_face_mouse_frame)
706 BLOCK_INPUT;
707 if (dpyinfo->mouse_face_mouse_frame)
708 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
709 dpyinfo->mouse_face_mouse_x,
710 dpyinfo->mouse_face_mouse_y);
711 dpyinfo->mouse_face_deferred_gc = 0;
712 UNBLOCK_INPUT;
718 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
719 arrow bitmaps, or clear the fringes if no bitmaps are required
720 before DESIRED_ROW is made current. The window being updated is
721 found in updated_window. This function It is called from
722 update_window_line only if it is known that there are differences
723 between bitmaps to be drawn between current row and DESIRED_ROW. */
725 static void
726 x_after_update_window_line (desired_row)
727 struct glyph_row *desired_row;
729 struct window *w = updated_window;
730 struct frame *f;
731 int width, height;
733 xassert (w);
735 if (!desired_row->mode_line_p && !w->pseudo_window_p)
736 desired_row->redraw_fringe_bitmaps_p = 1;
738 /* When a window has disappeared, make sure that no rest of
739 full-width rows stays visible in the internal border. Could
740 check here if updated_window is the leftmost/rightmost window,
741 but I guess it's not worth doing since vertically split windows
742 are almost never used, internal border is rarely set, and the
743 overhead is very small. */
744 if (windows_or_buffers_changed
745 && desired_row->full_width_p
746 && (f = XFRAME (w->frame),
747 width = FRAME_INTERNAL_BORDER_WIDTH (f),
748 width != 0)
749 && (height = desired_row->visible_height,
750 height > 0))
752 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
754 /* Internal border is drawn below the tool bar. */
755 if (WINDOWP (f->tool_bar_window)
756 && w == XWINDOW (f->tool_bar_window))
757 y -= width;
759 BLOCK_INPUT;
760 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
761 0, y, width, height, False);
762 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
763 FRAME_PIXEL_WIDTH (f) - width,
764 y, width, height, False);
765 UNBLOCK_INPUT;
769 static void
770 x_draw_fringe_bitmap (w, row, p)
771 struct window *w;
772 struct glyph_row *row;
773 struct draw_fringe_bitmap_params *p;
775 struct frame *f = XFRAME (WINDOW_FRAME (w));
776 Display *display = FRAME_X_DISPLAY (f);
777 Window window = FRAME_X_WINDOW (f);
778 GC gc = f->output_data.x->normal_gc;
779 struct face *face = p->face;
780 int rowY;
782 /* Must clip because of partially visible lines. */
783 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
784 if (p->y < rowY)
786 /* Adjust position of "bottom aligned" bitmap on partially
787 visible last row. */
788 int oldY = row->y;
789 int oldVH = row->visible_height;
790 row->visible_height = p->h;
791 row->y -= rowY - p->y;
792 x_clip_to_row (w, row, -1, gc);
793 row->y = oldY;
794 row->visible_height = oldVH;
796 else
797 x_clip_to_row (w, row, -1, gc);
799 if (!p->overlay_p)
801 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
803 /* In case the same realized face is used for fringes and
804 for something displayed in the text (e.g. face `region' on
805 mono-displays, the fill style may have been changed to
806 FillSolid in x_draw_glyph_string_background. */
807 if (face->stipple)
808 XSetFillStyle (display, face->gc, FillOpaqueStippled);
809 else
810 XSetForeground (display, face->gc, face->background);
812 #ifdef USE_TOOLKIT_SCROLL_BARS
813 /* If the fringe is adjacent to the left (right) scroll bar of a
814 leftmost (rightmost, respectively) window, then extend its
815 background to the gap between the fringe and the bar. */
816 if ((WINDOW_LEFTMOST_P (w)
817 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
818 || (WINDOW_RIGHTMOST_P (w)
819 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
821 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
823 if (sb_width > 0)
825 int left = WINDOW_SCROLL_BAR_AREA_X (w);
826 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
827 * FRAME_COLUMN_WIDTH (f));
829 if (bx < 0)
831 /* Bitmap fills the fringe. */
832 if (left + width == p->x)
833 bx = left + sb_width;
834 else if (p->x + p->wd == left)
835 bx = left;
836 if (bx >= 0)
838 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
840 nx = width - sb_width;
841 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
842 row->y));
843 ny = row->visible_height;
846 else
848 if (left + width == bx)
850 bx = left + sb_width;
851 nx += width - sb_width;
853 else if (bx + nx == left)
854 nx += width - sb_width;
858 #endif
859 if (bx >= 0 && nx > 0)
860 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
862 if (!face->stipple)
863 XSetForeground (display, face->gc, face->foreground);
866 if (p->which)
868 unsigned char *bits;
869 Pixmap pixmap, clipmask = (Pixmap) 0;
870 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
871 XGCValues gcv;
873 if (p->wd > 8)
874 bits = (unsigned char *)(p->bits + p->dh);
875 else
876 bits = (unsigned char *)p->bits + p->dh;
878 /* Draw the bitmap. I believe these small pixmaps can be cached
879 by the server. */
880 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
881 (p->cursor_p
882 ? (p->overlay_p ? face->background
883 : f->output_data.x->cursor_pixel)
884 : face->foreground),
885 face->background, depth);
887 if (p->overlay_p)
889 clipmask = XCreatePixmapFromBitmapData (display,
890 FRAME_X_DISPLAY_INFO (f)->root_window,
891 bits, p->wd, p->h,
892 1, 0, 1);
893 gcv.clip_mask = clipmask;
894 gcv.clip_x_origin = p->x;
895 gcv.clip_y_origin = p->y;
896 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
899 XCopyArea (display, pixmap, window, gc, 0, 0,
900 p->wd, p->h, p->x, p->y);
901 XFreePixmap (display, pixmap);
903 if (p->overlay_p)
905 gcv.clip_mask = (Pixmap) 0;
906 XChangeGC (display, gc, GCClipMask, &gcv);
907 XFreePixmap (display, clipmask);
911 XSetClipMask (display, gc, None);
916 /* This is called when starting Emacs and when restarting after
917 suspend. When starting Emacs, no X window is mapped. And nothing
918 must be done to Emacs's own window if it is suspended (though that
919 rarely happens). */
921 static void
922 XTset_terminal_modes (struct terminal *terminal)
926 /* This is called when exiting or suspending Emacs. Exiting will make
927 the X-windows go away, and suspending requires no action. */
929 static void
930 XTreset_terminal_modes (struct terminal *terminal)
935 /***********************************************************************
936 Glyph display
937 ***********************************************************************/
941 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
942 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
943 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
944 int));
945 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
946 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
947 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
948 static void x_draw_glyph_string P_ ((struct glyph_string *));
949 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
950 static void x_set_cursor_gc P_ ((struct glyph_string *));
951 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
952 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
953 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
954 unsigned long *, double, int));
955 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
956 double, int, unsigned long));
957 static void x_setup_relief_colors P_ ((struct glyph_string *));
958 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
959 static void x_draw_image_relief P_ ((struct glyph_string *));
960 static void x_draw_image_foreground P_ ((struct glyph_string *));
961 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
962 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
963 int, int, int));
964 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
965 int, int, int, int, int, int,
966 XRectangle *));
967 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
968 int, int, int, XRectangle *));
970 #if GLYPH_DEBUG
971 static void x_check_font P_ ((struct frame *, struct font *));
972 #endif
975 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
976 face. */
978 static void
979 x_set_cursor_gc (s)
980 struct glyph_string *s;
982 if (s->font == FRAME_FONT (s->f)
983 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
984 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
985 && !s->cmp)
986 s->gc = s->f->output_data.x->cursor_gc;
987 else
989 /* Cursor on non-default face: must merge. */
990 XGCValues xgcv;
991 unsigned long mask;
993 xgcv.background = s->f->output_data.x->cursor_pixel;
994 xgcv.foreground = s->face->background;
996 /* If the glyph would be invisible, try a different foreground. */
997 if (xgcv.foreground == xgcv.background)
998 xgcv.foreground = s->face->foreground;
999 if (xgcv.foreground == xgcv.background)
1000 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1001 if (xgcv.foreground == xgcv.background)
1002 xgcv.foreground = s->face->foreground;
1004 /* Make sure the cursor is distinct from text in this face. */
1005 if (xgcv.background == s->face->background
1006 && xgcv.foreground == s->face->foreground)
1008 xgcv.background = s->face->foreground;
1009 xgcv.foreground = s->face->background;
1012 IF_DEBUG (x_check_font (s->f, s->font));
1013 xgcv.graphics_exposures = False;
1014 mask = GCForeground | GCBackground | GCGraphicsExposures;
1016 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1017 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1018 mask, &xgcv);
1019 else
1020 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1021 = XCreateGC (s->display, s->window, mask, &xgcv);
1023 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1028 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1030 static void
1031 x_set_mouse_face_gc (s)
1032 struct glyph_string *s;
1034 int face_id;
1035 struct face *face;
1037 /* What face has to be used last for the mouse face? */
1038 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1039 face = FACE_FROM_ID (s->f, face_id);
1040 if (face == NULL)
1041 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1043 if (s->first_glyph->type == CHAR_GLYPH)
1044 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1045 else
1046 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1047 s->face = FACE_FROM_ID (s->f, face_id);
1048 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1050 if (s->font == s->face->font)
1051 s->gc = s->face->gc;
1052 else
1054 /* Otherwise construct scratch_cursor_gc with values from FACE
1055 except for FONT. */
1056 XGCValues xgcv;
1057 unsigned long mask;
1059 xgcv.background = s->face->background;
1060 xgcv.foreground = s->face->foreground;
1061 xgcv.graphics_exposures = False;
1062 mask = GCForeground | GCBackground | GCGraphicsExposures;
1064 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1065 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1066 mask, &xgcv);
1067 else
1068 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1069 = XCreateGC (s->display, s->window, mask, &xgcv);
1071 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1074 xassert (s->gc != 0);
1078 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1079 Faces to use in the mode line have already been computed when the
1080 matrix was built, so there isn't much to do, here. */
1082 static INLINE void
1083 x_set_mode_line_face_gc (s)
1084 struct glyph_string *s;
1086 s->gc = s->face->gc;
1090 /* Set S->gc of glyph string S for drawing that glyph string. Set
1091 S->stippled_p to a non-zero value if the face of S has a stipple
1092 pattern. */
1094 static INLINE void
1095 x_set_glyph_string_gc (s)
1096 struct glyph_string *s;
1098 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1100 if (s->hl == DRAW_NORMAL_TEXT)
1102 s->gc = s->face->gc;
1103 s->stippled_p = s->face->stipple != 0;
1105 else if (s->hl == DRAW_INVERSE_VIDEO)
1107 x_set_mode_line_face_gc (s);
1108 s->stippled_p = s->face->stipple != 0;
1110 else if (s->hl == DRAW_CURSOR)
1112 x_set_cursor_gc (s);
1113 s->stippled_p = 0;
1115 else if (s->hl == DRAW_MOUSE_FACE)
1117 x_set_mouse_face_gc (s);
1118 s->stippled_p = s->face->stipple != 0;
1120 else if (s->hl == DRAW_IMAGE_RAISED
1121 || s->hl == DRAW_IMAGE_SUNKEN)
1123 s->gc = s->face->gc;
1124 s->stippled_p = s->face->stipple != 0;
1126 else
1128 s->gc = s->face->gc;
1129 s->stippled_p = s->face->stipple != 0;
1132 /* GC must have been set. */
1133 xassert (s->gc != 0);
1137 /* Set clipping for output of glyph string S. S may be part of a mode
1138 line or menu if we don't have X toolkit support. */
1140 static INLINE void
1141 x_set_glyph_string_clipping (s)
1142 struct glyph_string *s;
1144 XRectangle *r = s->clip;
1145 int n = get_glyph_string_clip_rects (s, r, 2);
1147 if (n > 0)
1148 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
1149 s->num_clips = n;
1153 /* Set SRC's clipping for output of glyph string DST. This is called
1154 when we are drawing DST's left_overhang or right_overhang only in
1155 the area of SRC. */
1157 static void
1158 x_set_glyph_string_clipping_exactly (src, dst)
1159 struct glyph_string *src, *dst;
1161 XRectangle r;
1163 r.x = src->x;
1164 r.width = src->width;
1165 r.y = src->y;
1166 r.height = src->height;
1167 dst->clip[0] = r;
1168 dst->num_clips = 1;
1169 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1173 /* RIF:
1174 Compute left and right overhang of glyph string S. */
1176 static void
1177 x_compute_glyph_string_overhangs (s)
1178 struct glyph_string *s;
1180 if (s->cmp == NULL
1181 && (s->first_glyph->type == CHAR_GLYPH
1182 || s->first_glyph->type == COMPOSITE_GLYPH))
1184 struct font_metrics metrics;
1186 if (s->first_glyph->type == CHAR_GLYPH)
1188 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1189 struct font *font = s->font;
1190 int i;
1192 for (i = 0; i < s->nchars; i++)
1193 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1194 font->driver->text_extents (font, code, s->nchars, &metrics);
1196 else
1198 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1200 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1202 s->right_overhang = (metrics.rbearing > metrics.width
1203 ? metrics.rbearing - metrics.width : 0);
1204 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1206 else if (s->cmp)
1208 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1209 s->left_overhang = - s->cmp->lbearing;
1214 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1216 static INLINE void
1217 x_clear_glyph_string_rect (s, x, y, w, h)
1218 struct glyph_string *s;
1219 int x, y, w, h;
1221 XGCValues xgcv;
1222 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1223 XSetForeground (s->display, s->gc, xgcv.background);
1224 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1225 XSetForeground (s->display, s->gc, xgcv.foreground);
1229 /* Draw the background of glyph_string S. If S->background_filled_p
1230 is non-zero don't draw it. FORCE_P non-zero means draw the
1231 background even if it wouldn't be drawn normally. This is used
1232 when a string preceding S draws into the background of S, or S
1233 contains the first component of a composition. */
1235 static void
1236 x_draw_glyph_string_background (s, force_p)
1237 struct glyph_string *s;
1238 int force_p;
1240 /* Nothing to do if background has already been drawn or if it
1241 shouldn't be drawn in the first place. */
1242 if (!s->background_filled_p)
1244 int box_line_width = max (s->face->box_line_width, 0);
1246 if (s->stippled_p)
1248 /* Fill background with a stipple pattern. */
1249 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1250 XFillRectangle (s->display, s->window, s->gc, s->x,
1251 s->y + box_line_width,
1252 s->background_width,
1253 s->height - 2 * box_line_width);
1254 XSetFillStyle (s->display, s->gc, FillSolid);
1255 s->background_filled_p = 1;
1257 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1258 || s->font_not_found_p
1259 || s->extends_to_end_of_line_p
1260 || force_p)
1262 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1263 s->background_width,
1264 s->height - 2 * box_line_width);
1265 s->background_filled_p = 1;
1271 /* Draw the foreground of glyph string S. */
1273 static void
1274 x_draw_glyph_string_foreground (s)
1275 struct glyph_string *s;
1277 int i, x;
1279 /* If first glyph of S has a left box line, start drawing the text
1280 of S to the right of that box line. */
1281 if (s->face->box != FACE_NO_BOX
1282 && s->first_glyph->left_box_line_p)
1283 x = s->x + eabs (s->face->box_line_width);
1284 else
1285 x = s->x;
1287 /* Draw characters of S as rectangles if S's font could not be
1288 loaded. */
1289 if (s->font_not_found_p)
1291 for (i = 0; i < s->nchars; ++i)
1293 struct glyph *g = s->first_glyph + i;
1294 XDrawRectangle (s->display, s->window,
1295 s->gc, x, s->y, g->pixel_width - 1,
1296 s->height - 1);
1297 x += g->pixel_width;
1300 else
1302 struct font *font = s->font;
1303 int boff = font->baseline_offset;
1304 int y;
1306 if (font->vertical_centering)
1307 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1309 y = s->ybase - boff;
1310 if (s->for_overlaps
1311 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1312 font->driver->draw (s, 0, s->nchars, x, y, 0);
1313 else
1314 font->driver->draw (s, 0, s->nchars, x, y, 1);
1315 if (s->face->overstrike)
1316 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1320 /* Draw the foreground of composite glyph string S. */
1322 static void
1323 x_draw_composite_glyph_string_foreground (s)
1324 struct glyph_string *s;
1326 int i, j, x;
1327 struct font *font = s->font;
1329 /* If first glyph of S has a left box line, start drawing the text
1330 of S to the right of that box line. */
1331 if (s->face && s->face->box != FACE_NO_BOX
1332 && s->first_glyph->left_box_line_p)
1333 x = s->x + eabs (s->face->box_line_width);
1334 else
1335 x = s->x;
1337 /* S is a glyph string for a composition. S->cmp_from is the index
1338 of the first character drawn for glyphs of this composition.
1339 S->cmp_from == 0 means we are drawing the very first character of
1340 this composition. */
1342 /* Draw a rectangle for the composition if the font for the very
1343 first character of the composition could not be loaded. */
1344 if (s->font_not_found_p)
1346 if (s->cmp_from == 0)
1347 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1348 s->width - 1, s->height - 1);
1350 else if (! s->first_glyph->u.cmp.automatic)
1352 int y = s->ybase;
1354 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1355 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1357 int xx = x + s->cmp->offsets[j * 2];
1358 int yy = y - s->cmp->offsets[j * 2 + 1];
1360 font->driver->draw (s, j, j + 1, xx, yy, 0);
1361 if (s->face->overstrike)
1362 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1365 else
1367 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1368 Lisp_Object glyph;
1369 int y = s->ybase;
1370 int width = 0;
1372 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1374 glyph = LGSTRING_GLYPH (gstring, i);
1375 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1376 width += LGLYPH_WIDTH (glyph);
1377 else
1379 int xoff, yoff, wadjust;
1381 if (j < i)
1383 font->driver->draw (s, j, i, x, y, 0);
1384 x += width;
1386 xoff = LGLYPH_XOFF (glyph);
1387 yoff = LGLYPH_YOFF (glyph);
1388 wadjust = LGLYPH_WADJUST (glyph);
1389 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1390 x += wadjust;
1391 j = i + 1;
1392 width = 0;
1395 if (j < i)
1396 font->driver->draw (s, j, i, x, y, 0);
1401 #ifdef USE_X_TOOLKIT
1403 static struct frame *x_frame_of_widget P_ ((Widget));
1404 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1405 XrmValue *, XrmValue *, XtPointer *));
1406 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1407 XrmValue *, Cardinal *));
1410 /* Return the frame on which widget WIDGET is used.. Abort if frame
1411 cannot be determined. */
1413 static struct frame *
1414 x_frame_of_widget (widget)
1415 Widget widget;
1417 struct x_display_info *dpyinfo;
1418 Lisp_Object tail;
1419 struct frame *f;
1421 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1423 /* Find the top-level shell of the widget. Note that this function
1424 can be called when the widget is not yet realized, so XtWindow
1425 (widget) == 0. That's the reason we can't simply use
1426 x_any_window_to_frame. */
1427 while (!XtIsTopLevelShell (widget))
1428 widget = XtParent (widget);
1430 /* Look for a frame with that top-level widget. Allocate the color
1431 on that frame to get the right gamma correction value. */
1432 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1433 if (FRAMEP (XCAR (tail))
1434 && (f = XFRAME (XCAR (tail)),
1435 (FRAME_X_P (f)
1436 && f->output_data.nothing != 1
1437 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1438 && f->output_data.x->widget == widget)
1439 return f;
1441 abort ();
1445 /* Allocate the color COLOR->pixel on the screen and display of
1446 widget WIDGET in colormap CMAP. If an exact match cannot be
1447 allocated, try the nearest color available. Value is non-zero
1448 if successful. This is called from lwlib. */
1451 x_alloc_nearest_color_for_widget (widget, cmap, color)
1452 Widget widget;
1453 Colormap cmap;
1454 XColor *color;
1456 struct frame *f = x_frame_of_widget (widget);
1457 return x_alloc_nearest_color (f, cmap, color);
1461 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1462 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1463 If this produces the same color as PIXEL, try a color where all RGB
1464 values have DELTA added. Return the allocated color in *PIXEL.
1465 DISPLAY is the X display, CMAP is the colormap to operate on.
1466 Value is non-zero if successful. */
1469 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1470 Widget widget;
1471 Display *display;
1472 Colormap cmap;
1473 unsigned long *pixel;
1474 double factor;
1475 int delta;
1477 struct frame *f = x_frame_of_widget (widget);
1478 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1482 /* Structure specifying which arguments should be passed by Xt to
1483 cvt_string_to_pixel. We want the widget's screen and colormap. */
1485 static XtConvertArgRec cvt_string_to_pixel_args[] =
1487 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1488 sizeof (Screen *)},
1489 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1490 sizeof (Colormap)}
1494 /* The address of this variable is returned by
1495 cvt_string_to_pixel. */
1497 static Pixel cvt_string_to_pixel_value;
1500 /* Convert a color name to a pixel color.
1502 DPY is the display we are working on.
1504 ARGS is an array of *NARGS XrmValue structures holding additional
1505 information about the widget for which the conversion takes place.
1506 The contents of this array are determined by the specification
1507 in cvt_string_to_pixel_args.
1509 FROM is a pointer to an XrmValue which points to the color name to
1510 convert. TO is an XrmValue in which to return the pixel color.
1512 CLOSURE_RET is a pointer to user-data, in which we record if
1513 we allocated the color or not.
1515 Value is True if successful, False otherwise. */
1517 static Boolean
1518 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1519 Display *dpy;
1520 XrmValue *args;
1521 Cardinal *nargs;
1522 XrmValue *from, *to;
1523 XtPointer *closure_ret;
1525 Screen *screen;
1526 Colormap cmap;
1527 Pixel pixel;
1528 String color_name;
1529 XColor color;
1531 if (*nargs != 2)
1533 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1534 "wrongParameters", "cvt_string_to_pixel",
1535 "XtToolkitError",
1536 "Screen and colormap args required", NULL, NULL);
1537 return False;
1540 screen = *(Screen **) args[0].addr;
1541 cmap = *(Colormap *) args[1].addr;
1542 color_name = (String) from->addr;
1544 if (strcmp (color_name, XtDefaultBackground) == 0)
1546 *closure_ret = (XtPointer) False;
1547 pixel = WhitePixelOfScreen (screen);
1549 else if (strcmp (color_name, XtDefaultForeground) == 0)
1551 *closure_ret = (XtPointer) False;
1552 pixel = BlackPixelOfScreen (screen);
1554 else if (XParseColor (dpy, cmap, color_name, &color)
1555 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1557 pixel = color.pixel;
1558 *closure_ret = (XtPointer) True;
1560 else
1562 String params[1];
1563 Cardinal nparams = 1;
1565 params[0] = color_name;
1566 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1567 "badValue", "cvt_string_to_pixel",
1568 "XtToolkitError", "Invalid color `%s'",
1569 params, &nparams);
1570 return False;
1573 if (to->addr != NULL)
1575 if (to->size < sizeof (Pixel))
1577 to->size = sizeof (Pixel);
1578 return False;
1581 *(Pixel *) to->addr = pixel;
1583 else
1585 cvt_string_to_pixel_value = pixel;
1586 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1589 to->size = sizeof (Pixel);
1590 return True;
1594 /* Free a pixel color which was previously allocated via
1595 cvt_string_to_pixel. This is registered as the destructor
1596 for this type of resource via XtSetTypeConverter.
1598 APP is the application context in which we work.
1600 TO is a pointer to an XrmValue holding the color to free.
1601 CLOSURE is the value we stored in CLOSURE_RET for this color
1602 in cvt_string_to_pixel.
1604 ARGS and NARGS are like for cvt_string_to_pixel. */
1606 static void
1607 cvt_pixel_dtor (app, to, closure, args, nargs)
1608 XtAppContext app;
1609 XrmValuePtr to;
1610 XtPointer closure;
1611 XrmValuePtr args;
1612 Cardinal *nargs;
1614 if (*nargs != 2)
1616 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1617 "XtToolkitError",
1618 "Screen and colormap arguments required",
1619 NULL, NULL);
1621 else if (closure != NULL)
1623 /* We did allocate the pixel, so free it. */
1624 Screen *screen = *(Screen **) args[0].addr;
1625 Colormap cmap = *(Colormap *) args[1].addr;
1626 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1627 (Pixel *) to->addr, 1);
1632 #endif /* USE_X_TOOLKIT */
1635 /* Value is an array of XColor structures for the contents of the
1636 color map of display DPY. Set *NCELLS to the size of the array.
1637 Note that this probably shouldn't be called for large color maps,
1638 say a 24-bit TrueColor map. */
1640 static const XColor *
1641 x_color_cells (dpy, ncells)
1642 Display *dpy;
1643 int *ncells;
1645 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1647 if (dpyinfo->color_cells == NULL)
1649 Screen *screen = dpyinfo->screen;
1650 int i;
1652 dpyinfo->ncolor_cells
1653 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1654 dpyinfo->color_cells
1655 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1656 * sizeof *dpyinfo->color_cells);
1658 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1659 dpyinfo->color_cells[i].pixel = i;
1661 XQueryColors (dpy, dpyinfo->cmap,
1662 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1665 *ncells = dpyinfo->ncolor_cells;
1666 return dpyinfo->color_cells;
1670 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1671 colors in COLORS. Use cached information, if available. */
1673 void
1674 x_query_colors (f, colors, ncolors)
1675 struct frame *f;
1676 XColor *colors;
1677 int ncolors;
1679 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1681 if (dpyinfo->color_cells)
1683 int i;
1684 for (i = 0; i < ncolors; ++i)
1686 unsigned long pixel = colors[i].pixel;
1687 xassert (pixel < dpyinfo->ncolor_cells);
1688 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1689 colors[i] = dpyinfo->color_cells[pixel];
1692 else
1693 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1697 /* On frame F, translate pixel color to RGB values for the color in
1698 COLOR. Use cached information, if available. */
1700 void
1701 x_query_color (f, color)
1702 struct frame *f;
1703 XColor *color;
1705 x_query_colors (f, color, 1);
1709 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1710 exact match can't be allocated, try the nearest color available.
1711 Value is non-zero if successful. Set *COLOR to the color
1712 allocated. */
1714 static int
1715 x_alloc_nearest_color_1 (dpy, cmap, color)
1716 Display *dpy;
1717 Colormap cmap;
1718 XColor *color;
1720 int rc;
1722 rc = XAllocColor (dpy, cmap, color);
1723 if (rc == 0)
1725 /* If we got to this point, the colormap is full, so we're going
1726 to try to get the next closest color. The algorithm used is
1727 a least-squares matching, which is what X uses for closest
1728 color matching with StaticColor visuals. */
1729 int nearest, i;
1730 unsigned long nearest_delta = ~0;
1731 int ncells;
1732 const XColor *cells = x_color_cells (dpy, &ncells);
1734 for (nearest = i = 0; i < ncells; ++i)
1736 long dred = (color->red >> 8) - (cells[i].red >> 8);
1737 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1738 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1739 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1741 if (delta < nearest_delta)
1743 nearest = i;
1744 nearest_delta = delta;
1748 color->red = cells[nearest].red;
1749 color->green = cells[nearest].green;
1750 color->blue = cells[nearest].blue;
1751 rc = XAllocColor (dpy, cmap, color);
1753 else
1755 /* If allocation succeeded, and the allocated pixel color is not
1756 equal to a cached pixel color recorded earlier, there was a
1757 change in the colormap, so clear the color cache. */
1758 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1759 XColor *cached_color;
1761 if (dpyinfo->color_cells
1762 && (cached_color = &dpyinfo->color_cells[color->pixel],
1763 (cached_color->red != color->red
1764 || cached_color->blue != color->blue
1765 || cached_color->green != color->green)))
1767 xfree (dpyinfo->color_cells);
1768 dpyinfo->color_cells = NULL;
1769 dpyinfo->ncolor_cells = 0;
1773 #ifdef DEBUG_X_COLORS
1774 if (rc)
1775 register_color (color->pixel);
1776 #endif /* DEBUG_X_COLORS */
1778 return rc;
1782 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1783 exact match can't be allocated, try the nearest color available.
1784 Value is non-zero if successful. Set *COLOR to the color
1785 allocated. */
1788 x_alloc_nearest_color (f, cmap, color)
1789 struct frame *f;
1790 Colormap cmap;
1791 XColor *color;
1793 gamma_correct (f, color);
1794 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1798 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1799 It's necessary to do this instead of just using PIXEL directly to
1800 get color reference counts right. */
1802 unsigned long
1803 x_copy_color (f, pixel)
1804 struct frame *f;
1805 unsigned long pixel;
1807 XColor color;
1809 color.pixel = pixel;
1810 BLOCK_INPUT;
1811 x_query_color (f, &color);
1812 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1813 UNBLOCK_INPUT;
1814 #ifdef DEBUG_X_COLORS
1815 register_color (pixel);
1816 #endif
1817 return color.pixel;
1821 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1822 It's necessary to do this instead of just using PIXEL directly to
1823 get color reference counts right. */
1825 unsigned long
1826 x_copy_dpy_color (dpy, cmap, pixel)
1827 Display *dpy;
1828 Colormap cmap;
1829 unsigned long pixel;
1831 XColor color;
1833 color.pixel = pixel;
1834 BLOCK_INPUT;
1835 XQueryColor (dpy, cmap, &color);
1836 XAllocColor (dpy, cmap, &color);
1837 UNBLOCK_INPUT;
1838 #ifdef DEBUG_X_COLORS
1839 register_color (pixel);
1840 #endif
1841 return color.pixel;
1845 /* Brightness beyond which a color won't have its highlight brightness
1846 boosted.
1848 Nominally, highlight colors for `3d' faces are calculated by
1849 brightening an object's color by a constant scale factor, but this
1850 doesn't yield good results for dark colors, so for colors who's
1851 brightness is less than this value (on a scale of 0-65535) have an
1852 use an additional additive factor.
1854 The value here is set so that the default menu-bar/mode-line color
1855 (grey75) will not have its highlights changed at all. */
1856 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1859 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1860 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1861 If this produces the same color as PIXEL, try a color where all RGB
1862 values have DELTA added. Return the allocated color in *PIXEL.
1863 DISPLAY is the X display, CMAP is the colormap to operate on.
1864 Value is non-zero if successful. */
1866 static int
1867 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1868 struct frame *f;
1869 Display *display;
1870 Colormap cmap;
1871 unsigned long *pixel;
1872 double factor;
1873 int delta;
1875 XColor color, new;
1876 long bright;
1877 int success_p;
1879 /* Get RGB color values. */
1880 color.pixel = *pixel;
1881 x_query_color (f, &color);
1883 /* Change RGB values by specified FACTOR. Avoid overflow! */
1884 xassert (factor >= 0);
1885 new.red = min (0xffff, factor * color.red);
1886 new.green = min (0xffff, factor * color.green);
1887 new.blue = min (0xffff, factor * color.blue);
1889 /* Calculate brightness of COLOR. */
1890 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1892 /* We only boost colors that are darker than
1893 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1894 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1895 /* Make an additive adjustment to NEW, because it's dark enough so
1896 that scaling by FACTOR alone isn't enough. */
1898 /* How far below the limit this color is (0 - 1, 1 being darker). */
1899 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1900 /* The additive adjustment. */
1901 int min_delta = delta * dimness * factor / 2;
1903 if (factor < 1)
1905 new.red = max (0, new.red - min_delta);
1906 new.green = max (0, new.green - min_delta);
1907 new.blue = max (0, new.blue - min_delta);
1909 else
1911 new.red = min (0xffff, min_delta + new.red);
1912 new.green = min (0xffff, min_delta + new.green);
1913 new.blue = min (0xffff, min_delta + new.blue);
1917 /* Try to allocate the color. */
1918 success_p = x_alloc_nearest_color (f, cmap, &new);
1919 if (success_p)
1921 if (new.pixel == *pixel)
1923 /* If we end up with the same color as before, try adding
1924 delta to the RGB values. */
1925 x_free_colors (f, &new.pixel, 1);
1927 new.red = min (0xffff, delta + color.red);
1928 new.green = min (0xffff, delta + color.green);
1929 new.blue = min (0xffff, delta + color.blue);
1930 success_p = x_alloc_nearest_color (f, cmap, &new);
1932 else
1933 success_p = 1;
1934 *pixel = new.pixel;
1937 return success_p;
1941 /* Set up the foreground color for drawing relief lines of glyph
1942 string S. RELIEF is a pointer to a struct relief containing the GC
1943 with which lines will be drawn. Use a color that is FACTOR or
1944 DELTA lighter or darker than the relief's background which is found
1945 in S->f->output_data.x->relief_background. If such a color cannot
1946 be allocated, use DEFAULT_PIXEL, instead. */
1948 static void
1949 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1950 struct frame *f;
1951 struct relief *relief;
1952 double factor;
1953 int delta;
1954 unsigned long default_pixel;
1956 XGCValues xgcv;
1957 struct x_output *di = f->output_data.x;
1958 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1959 unsigned long pixel;
1960 unsigned long background = di->relief_background;
1961 Colormap cmap = FRAME_X_COLORMAP (f);
1962 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1963 Display *dpy = FRAME_X_DISPLAY (f);
1965 xgcv.graphics_exposures = False;
1966 xgcv.line_width = 1;
1968 /* Free previously allocated color. The color cell will be reused
1969 when it has been freed as many times as it was allocated, so this
1970 doesn't affect faces using the same colors. */
1971 if (relief->gc
1972 && relief->allocated_p)
1974 x_free_colors (f, &relief->pixel, 1);
1975 relief->allocated_p = 0;
1978 /* Allocate new color. */
1979 xgcv.foreground = default_pixel;
1980 pixel = background;
1981 if (dpyinfo->n_planes != 1
1982 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1984 relief->allocated_p = 1;
1985 xgcv.foreground = relief->pixel = pixel;
1988 if (relief->gc == 0)
1990 xgcv.stipple = dpyinfo->gray;
1991 mask |= GCStipple;
1992 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1994 else
1995 XChangeGC (dpy, relief->gc, mask, &xgcv);
1999 /* Set up colors for the relief lines around glyph string S. */
2001 static void
2002 x_setup_relief_colors (s)
2003 struct glyph_string *s;
2005 struct x_output *di = s->f->output_data.x;
2006 unsigned long color;
2008 if (s->face->use_box_color_for_shadows_p)
2009 color = s->face->box_color;
2010 else if (s->first_glyph->type == IMAGE_GLYPH
2011 && s->img->pixmap
2012 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2013 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2014 else
2016 XGCValues xgcv;
2018 /* Get the background color of the face. */
2019 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2020 color = xgcv.background;
2023 if (di->white_relief.gc == 0
2024 || color != di->relief_background)
2026 di->relief_background = color;
2027 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2028 WHITE_PIX_DEFAULT (s->f));
2029 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2030 BLACK_PIX_DEFAULT (s->f));
2035 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2036 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2037 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2038 relief. LEFT_P non-zero means draw a relief on the left side of
2039 the rectangle. RIGHT_P non-zero means draw a relief on the right
2040 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2041 when drawing. */
2043 static void
2044 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2045 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2046 struct frame *f;
2047 int left_x, top_y, right_x, bottom_y, width;
2048 int top_p, bot_p, left_p, right_p, raised_p;
2049 XRectangle *clip_rect;
2051 Display *dpy = FRAME_X_DISPLAY (f);
2052 Window window = FRAME_X_WINDOW (f);
2053 int i;
2054 GC gc;
2056 if (raised_p)
2057 gc = f->output_data.x->white_relief.gc;
2058 else
2059 gc = f->output_data.x->black_relief.gc;
2060 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2062 /* Top. */
2063 if (top_p)
2064 for (i = 0; i < width; ++i)
2065 XDrawLine (dpy, window, gc,
2066 left_x + i * left_p, top_y + i,
2067 right_x + 1 - i * right_p, top_y + i);
2069 /* Left. */
2070 if (left_p)
2071 for (i = 0; i < width; ++i)
2072 XDrawLine (dpy, window, gc,
2073 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2075 XSetClipMask (dpy, gc, None);
2076 if (raised_p)
2077 gc = f->output_data.x->black_relief.gc;
2078 else
2079 gc = f->output_data.x->white_relief.gc;
2080 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2082 /* Bottom. */
2083 if (bot_p)
2084 for (i = 0; i < width; ++i)
2085 XDrawLine (dpy, window, gc,
2086 left_x + i * left_p, bottom_y - i,
2087 right_x + 1 - i * right_p, bottom_y - i);
2089 /* Right. */
2090 if (right_p)
2091 for (i = 0; i < width; ++i)
2092 XDrawLine (dpy, window, gc,
2093 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2095 XSetClipMask (dpy, gc, None);
2099 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2100 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2101 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2102 left side of the rectangle. RIGHT_P non-zero means draw a line
2103 on the right side of the rectangle. CLIP_RECT is the clipping
2104 rectangle to use when drawing. */
2106 static void
2107 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2108 left_p, right_p, clip_rect)
2109 struct glyph_string *s;
2110 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2111 XRectangle *clip_rect;
2113 XGCValues xgcv;
2115 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2116 XSetForeground (s->display, s->gc, s->face->box_color);
2117 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2119 /* Top. */
2120 XFillRectangle (s->display, s->window, s->gc,
2121 left_x, top_y, right_x - left_x + 1, width);
2123 /* Left. */
2124 if (left_p)
2125 XFillRectangle (s->display, s->window, s->gc,
2126 left_x, top_y, width, bottom_y - top_y + 1);
2128 /* Bottom. */
2129 XFillRectangle (s->display, s->window, s->gc,
2130 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2132 /* Right. */
2133 if (right_p)
2134 XFillRectangle (s->display, s->window, s->gc,
2135 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2137 XSetForeground (s->display, s->gc, xgcv.foreground);
2138 XSetClipMask (s->display, s->gc, None);
2142 /* Draw a box around glyph string S. */
2144 static void
2145 x_draw_glyph_string_box (s)
2146 struct glyph_string *s;
2148 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2149 int left_p, right_p;
2150 struct glyph *last_glyph;
2151 XRectangle clip_rect;
2153 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2154 ? WINDOW_RIGHT_EDGE_X (s->w)
2155 : window_box_right (s->w, s->area));
2157 /* The glyph that may have a right box line. */
2158 last_glyph = (s->cmp || s->img
2159 ? s->first_glyph
2160 : s->first_glyph + s->nchars - 1);
2162 width = eabs (s->face->box_line_width);
2163 raised_p = s->face->box == FACE_RAISED_BOX;
2164 left_x = s->x;
2165 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2166 ? last_x - 1
2167 : min (last_x, s->x + s->background_width) - 1);
2168 top_y = s->y;
2169 bottom_y = top_y + s->height - 1;
2171 left_p = (s->first_glyph->left_box_line_p
2172 || (s->hl == DRAW_MOUSE_FACE
2173 && (s->prev == NULL
2174 || s->prev->hl != s->hl)));
2175 right_p = (last_glyph->right_box_line_p
2176 || (s->hl == DRAW_MOUSE_FACE
2177 && (s->next == NULL
2178 || s->next->hl != s->hl)));
2180 get_glyph_string_clip_rect (s, &clip_rect);
2182 if (s->face->box == FACE_SIMPLE_BOX)
2183 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2184 left_p, right_p, &clip_rect);
2185 else
2187 x_setup_relief_colors (s);
2188 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2189 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2194 /* Draw foreground of image glyph string S. */
2196 static void
2197 x_draw_image_foreground (s)
2198 struct glyph_string *s;
2200 int x = s->x;
2201 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2203 /* If first glyph of S has a left box line, start drawing it to the
2204 right of that line. */
2205 if (s->face->box != FACE_NO_BOX
2206 && s->first_glyph->left_box_line_p
2207 && s->slice.x == 0)
2208 x += eabs (s->face->box_line_width);
2210 /* If there is a margin around the image, adjust x- and y-position
2211 by that margin. */
2212 if (s->slice.x == 0)
2213 x += s->img->hmargin;
2214 if (s->slice.y == 0)
2215 y += s->img->vmargin;
2217 if (s->img->pixmap)
2219 if (s->img->mask)
2221 /* We can't set both a clip mask and use XSetClipRectangles
2222 because the latter also sets a clip mask. We also can't
2223 trust on the shape extension to be available
2224 (XShapeCombineRegion). So, compute the rectangle to draw
2225 manually. */
2226 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2227 | GCFunction);
2228 XGCValues xgcv;
2229 XRectangle clip_rect, image_rect, r;
2231 xgcv.clip_mask = s->img->mask;
2232 xgcv.clip_x_origin = x;
2233 xgcv.clip_y_origin = y;
2234 xgcv.function = GXcopy;
2235 XChangeGC (s->display, s->gc, mask, &xgcv);
2237 get_glyph_string_clip_rect (s, &clip_rect);
2238 image_rect.x = x;
2239 image_rect.y = y;
2240 image_rect.width = s->slice.width;
2241 image_rect.height = s->slice.height;
2242 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2243 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2244 s->slice.x + r.x - x, s->slice.y + r.y - y,
2245 r.width, r.height, r.x, r.y);
2247 else
2249 XRectangle clip_rect, image_rect, r;
2251 get_glyph_string_clip_rect (s, &clip_rect);
2252 image_rect.x = x;
2253 image_rect.y = y;
2254 image_rect.width = s->slice.width;
2255 image_rect.height = s->slice.height;
2256 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2257 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2258 s->slice.x + r.x - x, s->slice.y + r.y - y,
2259 r.width, r.height, r.x, r.y);
2261 /* When the image has a mask, we can expect that at
2262 least part of a mouse highlight or a block cursor will
2263 be visible. If the image doesn't have a mask, make
2264 a block cursor visible by drawing a rectangle around
2265 the image. I believe it's looking better if we do
2266 nothing here for mouse-face. */
2267 if (s->hl == DRAW_CURSOR)
2269 int r = s->img->relief;
2270 if (r < 0) r = -r;
2271 XDrawRectangle (s->display, s->window, s->gc,
2272 x - r, y - r,
2273 s->slice.width + r*2 - 1,
2274 s->slice.height + r*2 - 1);
2278 else
2279 /* Draw a rectangle if image could not be loaded. */
2280 XDrawRectangle (s->display, s->window, s->gc, x, y,
2281 s->slice.width - 1, s->slice.height - 1);
2285 /* Draw a relief around the image glyph string S. */
2287 static void
2288 x_draw_image_relief (s)
2289 struct glyph_string *s;
2291 int x0, y0, x1, y1, thick, raised_p;
2292 XRectangle r;
2293 int x = s->x;
2294 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2296 /* If first glyph of S has a left box line, start drawing it to the
2297 right of that line. */
2298 if (s->face->box != FACE_NO_BOX
2299 && s->first_glyph->left_box_line_p
2300 && s->slice.x == 0)
2301 x += eabs (s->face->box_line_width);
2303 /* If there is a margin around the image, adjust x- and y-position
2304 by that margin. */
2305 if (s->slice.x == 0)
2306 x += s->img->hmargin;
2307 if (s->slice.y == 0)
2308 y += s->img->vmargin;
2310 if (s->hl == DRAW_IMAGE_SUNKEN
2311 || s->hl == DRAW_IMAGE_RAISED)
2313 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2314 raised_p = s->hl == DRAW_IMAGE_RAISED;
2316 else
2318 thick = eabs (s->img->relief);
2319 raised_p = s->img->relief > 0;
2322 x0 = x - thick;
2323 y0 = y - thick;
2324 x1 = x + s->slice.width + thick - 1;
2325 y1 = y + s->slice.height + thick - 1;
2327 x_setup_relief_colors (s);
2328 get_glyph_string_clip_rect (s, &r);
2329 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2330 s->slice.y == 0,
2331 s->slice.y + s->slice.height == s->img->height,
2332 s->slice.x == 0,
2333 s->slice.x + s->slice.width == s->img->width,
2334 &r);
2338 /* Draw the foreground of image glyph string S to PIXMAP. */
2340 static void
2341 x_draw_image_foreground_1 (s, pixmap)
2342 struct glyph_string *s;
2343 Pixmap pixmap;
2345 int x = 0;
2346 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2348 /* If first glyph of S has a left box line, start drawing it to the
2349 right of that line. */
2350 if (s->face->box != FACE_NO_BOX
2351 && s->first_glyph->left_box_line_p
2352 && s->slice.x == 0)
2353 x += eabs (s->face->box_line_width);
2355 /* If there is a margin around the image, adjust x- and y-position
2356 by that margin. */
2357 if (s->slice.x == 0)
2358 x += s->img->hmargin;
2359 if (s->slice.y == 0)
2360 y += s->img->vmargin;
2362 if (s->img->pixmap)
2364 if (s->img->mask)
2366 /* We can't set both a clip mask and use XSetClipRectangles
2367 because the latter also sets a clip mask. We also can't
2368 trust on the shape extension to be available
2369 (XShapeCombineRegion). So, compute the rectangle to draw
2370 manually. */
2371 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2372 | GCFunction);
2373 XGCValues xgcv;
2375 xgcv.clip_mask = s->img->mask;
2376 xgcv.clip_x_origin = x - s->slice.x;
2377 xgcv.clip_y_origin = y - s->slice.y;
2378 xgcv.function = GXcopy;
2379 XChangeGC (s->display, s->gc, mask, &xgcv);
2381 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2382 s->slice.x, s->slice.y,
2383 s->slice.width, s->slice.height, x, y);
2384 XSetClipMask (s->display, s->gc, None);
2386 else
2388 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2389 s->slice.x, s->slice.y,
2390 s->slice.width, s->slice.height, x, y);
2392 /* When the image has a mask, we can expect that at
2393 least part of a mouse highlight or a block cursor will
2394 be visible. If the image doesn't have a mask, make
2395 a block cursor visible by drawing a rectangle around
2396 the image. I believe it's looking better if we do
2397 nothing here for mouse-face. */
2398 if (s->hl == DRAW_CURSOR)
2400 int r = s->img->relief;
2401 if (r < 0) r = -r;
2402 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2403 s->slice.width + r*2 - 1,
2404 s->slice.height + r*2 - 1);
2408 else
2409 /* Draw a rectangle if image could not be loaded. */
2410 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2411 s->slice.width - 1, s->slice.height - 1);
2415 /* Draw part of the background of glyph string S. X, Y, W, and H
2416 give the rectangle to draw. */
2418 static void
2419 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2420 struct glyph_string *s;
2421 int x, y, w, h;
2423 if (s->stippled_p)
2425 /* Fill background with a stipple pattern. */
2426 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2427 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2428 XSetFillStyle (s->display, s->gc, FillSolid);
2430 else
2431 x_clear_glyph_string_rect (s, x, y, w, h);
2435 /* Draw image glyph string S.
2437 s->y
2438 s->x +-------------------------
2439 | s->face->box
2441 | +-------------------------
2442 | | s->img->margin
2444 | | +-------------------
2445 | | | the image
2449 static void
2450 x_draw_image_glyph_string (s)
2451 struct glyph_string *s;
2453 int box_line_hwidth = eabs (s->face->box_line_width);
2454 int box_line_vwidth = max (s->face->box_line_width, 0);
2455 int height;
2456 Pixmap pixmap = None;
2458 height = s->height;
2459 if (s->slice.y == 0)
2460 height -= box_line_vwidth;
2461 if (s->slice.y + s->slice.height >= s->img->height)
2462 height -= box_line_vwidth;
2464 /* Fill background with face under the image. Do it only if row is
2465 taller than image or if image has a clip mask to reduce
2466 flickering. */
2467 s->stippled_p = s->face->stipple != 0;
2468 if (height > s->slice.height
2469 || s->img->hmargin
2470 || s->img->vmargin
2471 || s->img->mask
2472 || s->img->pixmap == 0
2473 || s->width != s->background_width)
2475 if (s->img->mask)
2477 /* Create a pixmap as large as the glyph string. Fill it
2478 with the background color. Copy the image to it, using
2479 its mask. Copy the temporary pixmap to the display. */
2480 Screen *screen = FRAME_X_SCREEN (s->f);
2481 int depth = DefaultDepthOfScreen (screen);
2483 /* Create a pixmap as large as the glyph string. */
2484 pixmap = XCreatePixmap (s->display, s->window,
2485 s->background_width,
2486 s->height, depth);
2488 /* Don't clip in the following because we're working on the
2489 pixmap. */
2490 XSetClipMask (s->display, s->gc, None);
2492 /* Fill the pixmap with the background color/stipple. */
2493 if (s->stippled_p)
2495 /* Fill background with a stipple pattern. */
2496 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2497 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2498 XFillRectangle (s->display, pixmap, s->gc,
2499 0, 0, s->background_width, s->height);
2500 XSetFillStyle (s->display, s->gc, FillSolid);
2501 XSetTSOrigin (s->display, s->gc, 0, 0);
2503 else
2505 XGCValues xgcv;
2506 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2507 &xgcv);
2508 XSetForeground (s->display, s->gc, xgcv.background);
2509 XFillRectangle (s->display, pixmap, s->gc,
2510 0, 0, s->background_width, s->height);
2511 XSetForeground (s->display, s->gc, xgcv.foreground);
2514 else
2516 int x = s->x;
2517 int y = s->y;
2519 if (s->first_glyph->left_box_line_p
2520 && s->slice.x == 0)
2521 x += box_line_hwidth;
2523 if (s->slice.y == 0)
2524 y += box_line_vwidth;
2526 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2529 s->background_filled_p = 1;
2532 /* Draw the foreground. */
2533 if (pixmap != None)
2535 x_draw_image_foreground_1 (s, pixmap);
2536 x_set_glyph_string_clipping (s);
2537 XCopyArea (s->display, pixmap, s->window, s->gc,
2538 0, 0, s->background_width, s->height, s->x, s->y);
2539 XFreePixmap (s->display, pixmap);
2541 else
2542 x_draw_image_foreground (s);
2544 /* If we must draw a relief around the image, do it. */
2545 if (s->img->relief
2546 || s->hl == DRAW_IMAGE_RAISED
2547 || s->hl == DRAW_IMAGE_SUNKEN)
2548 x_draw_image_relief (s);
2552 /* Draw stretch glyph string S. */
2554 static void
2555 x_draw_stretch_glyph_string (s)
2556 struct glyph_string *s;
2558 xassert (s->first_glyph->type == STRETCH_GLYPH);
2560 if (s->hl == DRAW_CURSOR
2561 && !x_stretch_cursor_p)
2563 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2564 as wide as the stretch glyph. */
2565 int width, background_width = s->background_width;
2566 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2568 if (x < left_x)
2570 background_width -= left_x - x;
2571 x = left_x;
2573 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2575 /* Draw cursor. */
2576 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2578 /* Clear rest using the GC of the original non-cursor face. */
2579 if (width < background_width)
2581 int y = s->y;
2582 int w = background_width - width, h = s->height;
2583 XRectangle r;
2584 GC gc;
2586 x += width;
2587 if (s->row->mouse_face_p
2588 && cursor_in_mouse_face_p (s->w))
2590 x_set_mouse_face_gc (s);
2591 gc = s->gc;
2593 else
2594 gc = s->face->gc;
2596 get_glyph_string_clip_rect (s, &r);
2597 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2599 if (s->face->stipple)
2601 /* Fill background with a stipple pattern. */
2602 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2603 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2604 XSetFillStyle (s->display, gc, FillSolid);
2606 else
2608 XGCValues xgcv;
2609 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2610 XSetForeground (s->display, gc, xgcv.background);
2611 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2612 XSetForeground (s->display, gc, xgcv.foreground);
2616 else if (!s->background_filled_p)
2618 int background_width = s->background_width;
2619 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2621 /* Don't draw into left margin, fringe or scrollbar area
2622 except for header line and mode line. */
2623 if (x < left_x && !s->row->mode_line_p)
2625 background_width -= left_x - x;
2626 x = left_x;
2628 if (background_width > 0)
2629 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2632 s->background_filled_p = 1;
2636 /* Draw glyph string S. */
2638 static void
2639 x_draw_glyph_string (s)
2640 struct glyph_string *s;
2642 int relief_drawn_p = 0;
2644 /* If S draws into the background of its successors, draw the
2645 background of the successors first so that S can draw into it.
2646 This makes S->next use XDrawString instead of XDrawImageString. */
2647 if (s->next && s->right_overhang && !s->for_overlaps)
2649 int width;
2650 struct glyph_string *next;
2652 for (width = 0, next = s->next;
2653 next && width < s->right_overhang;
2654 width += next->width, next = next->next)
2655 if (next->first_glyph->type != IMAGE_GLYPH)
2657 x_set_glyph_string_gc (next);
2658 x_set_glyph_string_clipping (next);
2659 if (next->first_glyph->type == STRETCH_GLYPH)
2660 x_draw_stretch_glyph_string (next);
2661 else
2662 x_draw_glyph_string_background (next, 1);
2663 next->num_clips = 0;
2667 /* Set up S->gc, set clipping and draw S. */
2668 x_set_glyph_string_gc (s);
2670 /* Draw relief (if any) in advance for char/composition so that the
2671 glyph string can be drawn over it. */
2672 if (!s->for_overlaps
2673 && s->face->box != FACE_NO_BOX
2674 && (s->first_glyph->type == CHAR_GLYPH
2675 || s->first_glyph->type == COMPOSITE_GLYPH))
2678 x_set_glyph_string_clipping (s);
2679 x_draw_glyph_string_background (s, 1);
2680 x_draw_glyph_string_box (s);
2681 x_set_glyph_string_clipping (s);
2682 relief_drawn_p = 1;
2684 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2685 && !s->clip_tail
2686 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2687 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2688 /* We must clip just this glyph. left_overhang part has already
2689 drawn when s->prev was drawn, and right_overhang part will be
2690 drawn later when s->next is drawn. */
2691 x_set_glyph_string_clipping_exactly (s, s);
2692 else
2693 x_set_glyph_string_clipping (s);
2695 switch (s->first_glyph->type)
2697 case IMAGE_GLYPH:
2698 x_draw_image_glyph_string (s);
2699 break;
2701 case STRETCH_GLYPH:
2702 x_draw_stretch_glyph_string (s);
2703 break;
2705 case CHAR_GLYPH:
2706 if (s->for_overlaps)
2707 s->background_filled_p = 1;
2708 else
2709 x_draw_glyph_string_background (s, 0);
2710 x_draw_glyph_string_foreground (s);
2711 break;
2713 case COMPOSITE_GLYPH:
2714 if (s->for_overlaps || (s->cmp_from > 0
2715 && ! s->first_glyph->u.cmp.automatic))
2716 s->background_filled_p = 1;
2717 else
2718 x_draw_glyph_string_background (s, 1);
2719 x_draw_composite_glyph_string_foreground (s);
2720 break;
2722 default:
2723 abort ();
2726 if (!s->for_overlaps)
2728 /* Draw underline. */
2729 if (s->face->underline_p)
2731 unsigned long thickness, position;
2732 int y;
2734 if (s->prev && s->prev->face->underline_p)
2736 /* We use the same underline style as the previous one. */
2737 thickness = s->prev->underline_thickness;
2738 position = s->prev->underline_position;
2740 else
2742 /* Get the underline thickness. Default is 1 pixel. */
2743 if (s->font && s->font->underline_thickness > 0)
2744 thickness = s->font->underline_thickness;
2745 else
2746 thickness = 1;
2747 if (x_underline_at_descent_line)
2748 position = (s->height - thickness) - (s->ybase - s->y);
2749 else
2751 /* Get the underline position. This is the recommended
2752 vertical offset in pixels from the baseline to the top of
2753 the underline. This is a signed value according to the
2754 specs, and its default is
2756 ROUND ((maximum descent) / 2), with
2757 ROUND(x) = floor (x + 0.5) */
2759 if (x_use_underline_position_properties
2760 && s->font && s->font->underline_position >= 0)
2761 position = s->font->underline_position;
2762 else if (s->font)
2763 position = (s->font->descent + 1) / 2;
2764 else
2765 position = underline_minimum_offset;
2767 position = max (position, underline_minimum_offset);
2769 /* Check the sanity of thickness and position. We should
2770 avoid drawing underline out of the current line area. */
2771 if (s->y + s->height <= s->ybase + position)
2772 position = (s->height - 1) - (s->ybase - s->y);
2773 if (s->y + s->height < s->ybase + position + thickness)
2774 thickness = (s->y + s->height) - (s->ybase + position);
2775 s->underline_thickness = thickness;
2776 s->underline_position = position;
2777 y = s->ybase + position;
2778 if (s->face->underline_defaulted_p)
2779 XFillRectangle (s->display, s->window, s->gc,
2780 s->x, y, s->width, thickness);
2781 else
2783 XGCValues xgcv;
2784 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2785 XSetForeground (s->display, s->gc, s->face->underline_color);
2786 XFillRectangle (s->display, s->window, s->gc,
2787 s->x, y, s->width, thickness);
2788 XSetForeground (s->display, s->gc, xgcv.foreground);
2792 /* Draw overline. */
2793 if (s->face->overline_p)
2795 unsigned long dy = 0, h = 1;
2797 if (s->face->overline_color_defaulted_p)
2798 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2799 s->width, h);
2800 else
2802 XGCValues xgcv;
2803 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2804 XSetForeground (s->display, s->gc, s->face->overline_color);
2805 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2806 s->width, h);
2807 XSetForeground (s->display, s->gc, xgcv.foreground);
2811 /* Draw strike-through. */
2812 if (s->face->strike_through_p)
2814 unsigned long h = 1;
2815 unsigned long dy = (s->height - h) / 2;
2817 if (s->face->strike_through_color_defaulted_p)
2818 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2819 s->width, h);
2820 else
2822 XGCValues xgcv;
2823 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2824 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2825 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2826 s->width, h);
2827 XSetForeground (s->display, s->gc, xgcv.foreground);
2831 /* Draw relief if not yet drawn. */
2832 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2833 x_draw_glyph_string_box (s);
2835 if (s->prev)
2837 struct glyph_string *prev;
2839 for (prev = s->prev; prev; prev = prev->prev)
2840 if (prev->hl != s->hl
2841 && prev->x + prev->width + prev->right_overhang > s->x)
2843 /* As prev was drawn while clipped to its own area, we
2844 must draw the right_overhang part using s->hl now. */
2845 enum draw_glyphs_face save = prev->hl;
2847 prev->hl = s->hl;
2848 x_set_glyph_string_gc (prev);
2849 x_set_glyph_string_clipping_exactly (s, prev);
2850 if (prev->first_glyph->type == CHAR_GLYPH)
2851 x_draw_glyph_string_foreground (prev);
2852 else
2853 x_draw_composite_glyph_string_foreground (prev);
2854 XSetClipMask (prev->display, prev->gc, None);
2855 prev->hl = save;
2856 prev->num_clips = 0;
2860 if (s->next)
2862 struct glyph_string *next;
2864 for (next = s->next; next; next = next->next)
2865 if (next->hl != s->hl
2866 && next->x - next->left_overhang < s->x + s->width)
2868 /* As next will be drawn while clipped to its own area,
2869 we must draw the left_overhang part using s->hl now. */
2870 enum draw_glyphs_face save = next->hl;
2872 next->hl = s->hl;
2873 x_set_glyph_string_gc (next);
2874 x_set_glyph_string_clipping_exactly (s, next);
2875 if (next->first_glyph->type == CHAR_GLYPH)
2876 x_draw_glyph_string_foreground (next);
2877 else
2878 x_draw_composite_glyph_string_foreground (next);
2879 XSetClipMask (next->display, next->gc, None);
2880 next->hl = save;
2881 next->num_clips = 0;
2886 /* Reset clipping. */
2887 XSetClipMask (s->display, s->gc, None);
2888 s->num_clips = 0;
2891 /* Shift display to make room for inserted glyphs. */
2893 void
2894 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2895 struct frame *f;
2896 int x, y, width, height, shift_by;
2898 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2899 f->output_data.x->normal_gc,
2900 x, y, width, height,
2901 x + shift_by, y);
2904 /* Delete N glyphs at the nominal cursor position. Not implemented
2905 for X frames. */
2907 static void
2908 x_delete_glyphs (f, n)
2909 struct frame *f;
2910 register int n;
2912 abort ();
2916 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2917 If they are <= 0, this is probably an error. */
2919 void
2920 x_clear_area (dpy, window, x, y, width, height, exposures)
2921 Display *dpy;
2922 Window window;
2923 int x, y;
2924 int width, height;
2925 int exposures;
2927 xassert (width > 0 && height > 0);
2928 XClearArea (dpy, window, x, y, width, height, exposures);
2932 /* Clear an entire frame. */
2934 static void
2935 x_clear_frame (struct frame *f)
2937 /* Clearing the frame will erase any cursor, so mark them all as no
2938 longer visible. */
2939 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2940 output_cursor.hpos = output_cursor.vpos = 0;
2941 output_cursor.x = -1;
2943 /* We don't set the output cursor here because there will always
2944 follow an explicit cursor_to. */
2945 BLOCK_INPUT;
2946 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2948 /* We have to clear the scroll bars, too. If we have changed
2949 colors or something like that, then they should be notified. */
2950 x_scroll_bar_clear (f);
2952 XFlush (FRAME_X_DISPLAY (f));
2954 UNBLOCK_INPUT;
2959 /* Invert the middle quarter of the frame for .15 sec. */
2961 /* We use the select system call to do the waiting, so we have to make
2962 sure it's available. If it isn't, we just won't do visual bells. */
2964 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2967 /* Subtract the `struct timeval' values X and Y, storing the result in
2968 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2970 static int
2971 timeval_subtract (result, x, y)
2972 struct timeval *result, x, y;
2974 /* Perform the carry for the later subtraction by updating y. This
2975 is safer because on some systems the tv_sec member is unsigned. */
2976 if (x.tv_usec < y.tv_usec)
2978 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2979 y.tv_usec -= 1000000 * nsec;
2980 y.tv_sec += nsec;
2983 if (x.tv_usec - y.tv_usec > 1000000)
2985 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2986 y.tv_usec += 1000000 * nsec;
2987 y.tv_sec -= nsec;
2990 /* Compute the time remaining to wait. tv_usec is certainly
2991 positive. */
2992 result->tv_sec = x.tv_sec - y.tv_sec;
2993 result->tv_usec = x.tv_usec - y.tv_usec;
2995 /* Return indication of whether the result should be considered
2996 negative. */
2997 return x.tv_sec < y.tv_sec;
3000 void
3001 XTflash (f)
3002 struct frame *f;
3004 BLOCK_INPUT;
3007 GC gc;
3009 /* Create a GC that will use the GXxor function to flip foreground
3010 pixels into background pixels. */
3012 XGCValues values;
3014 values.function = GXxor;
3015 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3016 ^ FRAME_BACKGROUND_PIXEL (f));
3018 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3019 GCFunction | GCForeground, &values);
3023 /* Get the height not including a menu bar widget. */
3024 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
3025 /* Height of each line to flash. */
3026 int flash_height = FRAME_LINE_HEIGHT (f);
3027 /* These will be the left and right margins of the rectangles. */
3028 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3029 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3031 int width;
3033 /* Don't flash the area between a scroll bar and the frame
3034 edge it is next to. */
3035 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3037 case vertical_scroll_bar_left:
3038 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3039 break;
3041 case vertical_scroll_bar_right:
3042 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3043 break;
3045 default:
3046 break;
3049 width = flash_right - flash_left;
3051 /* If window is tall, flash top and bottom line. */
3052 if (height > 3 * FRAME_LINE_HEIGHT (f))
3054 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3055 flash_left,
3056 (FRAME_INTERNAL_BORDER_WIDTH (f)
3057 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3058 width, flash_height);
3059 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3060 flash_left,
3061 (height - flash_height
3062 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3063 width, flash_height);
3065 else
3066 /* If it is short, flash it all. */
3067 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3068 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3069 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3071 x_flush (f);
3074 struct timeval wakeup;
3076 EMACS_GET_TIME (wakeup);
3078 /* Compute time to wait until, propagating carry from usecs. */
3079 wakeup.tv_usec += 150000;
3080 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3081 wakeup.tv_usec %= 1000000;
3083 /* Keep waiting until past the time wakeup or any input gets
3084 available. */
3085 while (! detect_input_pending ())
3087 struct timeval current;
3088 struct timeval timeout;
3090 EMACS_GET_TIME (current);
3092 /* Break if result would be negative. */
3093 if (timeval_subtract (&current, wakeup, current))
3094 break;
3096 /* How long `select' should wait. */
3097 timeout.tv_sec = 0;
3098 timeout.tv_usec = 10000;
3100 /* Try to wait that long--but we might wake up sooner. */
3101 select (0, NULL, NULL, NULL, &timeout);
3105 /* If window is tall, flash top and bottom line. */
3106 if (height > 3 * FRAME_LINE_HEIGHT (f))
3108 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3109 flash_left,
3110 (FRAME_INTERNAL_BORDER_WIDTH (f)
3111 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3112 width, flash_height);
3113 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3114 flash_left,
3115 (height - flash_height
3116 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3117 width, flash_height);
3119 else
3120 /* If it is short, flash it all. */
3121 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3122 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3123 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3125 XFreeGC (FRAME_X_DISPLAY (f), gc);
3126 x_flush (f);
3130 UNBLOCK_INPUT;
3133 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3136 static void
3137 XTtoggle_invisible_pointer (f, invisible)
3138 FRAME_PTR f;
3139 int invisible;
3141 BLOCK_INPUT;
3142 if (invisible)
3144 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor != 0)
3145 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3146 FRAME_X_DISPLAY_INFO (f)->invisible_cursor);
3148 else
3149 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3150 f->output_data.x->current_cursor);
3151 f->pointer_invisible = invisible;
3152 UNBLOCK_INPUT;
3156 /* Make audible bell. */
3158 void
3159 XTring_bell ()
3161 struct frame *f = SELECTED_FRAME ();
3163 if (FRAME_X_DISPLAY (f))
3165 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3166 if (visible_bell)
3167 XTflash (f);
3168 else
3169 #endif
3171 BLOCK_INPUT;
3172 XBell (FRAME_X_DISPLAY (f), 0);
3173 XFlush (FRAME_X_DISPLAY (f));
3174 UNBLOCK_INPUT;
3180 /* Specify how many text lines, from the top of the window,
3181 should be affected by insert-lines and delete-lines operations.
3182 This, and those operations, are used only within an update
3183 that is bounded by calls to x_update_begin and x_update_end. */
3185 static void
3186 XTset_terminal_window (n)
3187 register int n;
3189 /* This function intentionally left blank. */
3194 /***********************************************************************
3195 Line Dance
3196 ***********************************************************************/
3198 /* Perform an insert-lines or delete-lines operation, inserting N
3199 lines or deleting -N lines at vertical position VPOS. */
3201 static void
3202 x_ins_del_lines (f, vpos, n)
3203 struct frame *f;
3204 int vpos, n;
3206 abort ();
3210 /* Scroll part of the display as described by RUN. */
3212 static void
3213 x_scroll_run (w, run)
3214 struct window *w;
3215 struct run *run;
3217 struct frame *f = XFRAME (w->frame);
3218 int x, y, width, height, from_y, to_y, bottom_y;
3220 /* Get frame-relative bounding box of the text display area of W,
3221 without mode lines. Include in this box the left and right
3222 fringe of W. */
3223 window_box (w, -1, &x, &y, &width, &height);
3225 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3226 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3227 bottom_y = y + height;
3229 if (to_y < from_y)
3231 /* Scrolling up. Make sure we don't copy part of the mode
3232 line at the bottom. */
3233 if (from_y + run->height > bottom_y)
3234 height = bottom_y - from_y;
3235 else
3236 height = run->height;
3238 else
3240 /* Scolling down. Make sure we don't copy over the mode line.
3241 at the bottom. */
3242 if (to_y + run->height > bottom_y)
3243 height = bottom_y - to_y;
3244 else
3245 height = run->height;
3248 BLOCK_INPUT;
3250 /* Cursor off. Will be switched on again in x_update_window_end. */
3251 updated_window = w;
3252 x_clear_cursor (w);
3254 XCopyArea (FRAME_X_DISPLAY (f),
3255 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3256 f->output_data.x->normal_gc,
3257 x, from_y,
3258 width, height,
3259 x, to_y);
3261 UNBLOCK_INPUT;
3266 /***********************************************************************
3267 Exposure Events
3268 ***********************************************************************/
3271 static void
3272 frame_highlight (f)
3273 struct frame *f;
3275 /* We used to only do this if Vx_no_window_manager was non-nil, but
3276 the ICCCM (section 4.1.6) says that the window's border pixmap
3277 and border pixel are window attributes which are "private to the
3278 client", so we can always change it to whatever we want. */
3279 BLOCK_INPUT;
3280 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3281 f->output_data.x->border_pixel);
3282 UNBLOCK_INPUT;
3283 x_update_cursor (f, 1);
3284 x_set_frame_alpha (f);
3287 static void
3288 frame_unhighlight (f)
3289 struct frame *f;
3291 /* We used to only do this if Vx_no_window_manager was non-nil, but
3292 the ICCCM (section 4.1.6) says that the window's border pixmap
3293 and border pixel are window attributes which are "private to the
3294 client", so we can always change it to whatever we want. */
3295 BLOCK_INPUT;
3296 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3297 f->output_data.x->border_tile);
3298 UNBLOCK_INPUT;
3299 x_update_cursor (f, 1);
3300 x_set_frame_alpha (f);
3303 /* The focus has changed. Update the frames as necessary to reflect
3304 the new situation. Note that we can't change the selected frame
3305 here, because the Lisp code we are interrupting might become confused.
3306 Each event gets marked with the frame in which it occurred, so the
3307 Lisp code can tell when the switch took place by examining the events. */
3309 static void
3310 x_new_focus_frame (dpyinfo, frame)
3311 struct x_display_info *dpyinfo;
3312 struct frame *frame;
3314 struct frame *old_focus = dpyinfo->x_focus_frame;
3316 if (frame != dpyinfo->x_focus_frame)
3318 /* Set this before calling other routines, so that they see
3319 the correct value of x_focus_frame. */
3320 dpyinfo->x_focus_frame = frame;
3322 if (old_focus && old_focus->auto_lower)
3323 x_lower_frame (old_focus);
3325 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3326 pending_autoraise_frame = dpyinfo->x_focus_frame;
3327 else
3328 pending_autoraise_frame = 0;
3331 x_frame_rehighlight (dpyinfo);
3334 /* Handle FocusIn and FocusOut state changes for FRAME.
3335 If FRAME has focus and there exists more than one frame, puts
3336 a FOCUS_IN_EVENT into *BUFP. */
3338 static void
3339 x_focus_changed (type, state, dpyinfo, frame, bufp)
3340 int type;
3341 int state;
3342 struct x_display_info *dpyinfo;
3343 struct frame *frame;
3344 struct input_event *bufp;
3346 if (type == FocusIn)
3348 if (dpyinfo->x_focus_event_frame != frame)
3350 x_new_focus_frame (dpyinfo, frame);
3351 dpyinfo->x_focus_event_frame = frame;
3353 /* Don't stop displaying the initial startup message
3354 for a switch-frame event we don't need. */
3355 if (NILP (Vterminal_frame)
3356 && CONSP (Vframe_list)
3357 && !NILP (XCDR (Vframe_list)))
3359 bufp->kind = FOCUS_IN_EVENT;
3360 XSETFRAME (bufp->frame_or_window, frame);
3364 frame->output_data.x->focus_state |= state;
3366 #ifdef HAVE_X_I18N
3367 if (FRAME_XIC (frame))
3368 XSetICFocus (FRAME_XIC (frame));
3369 #endif
3371 else if (type == FocusOut)
3373 frame->output_data.x->focus_state &= ~state;
3375 if (dpyinfo->x_focus_event_frame == frame)
3377 dpyinfo->x_focus_event_frame = 0;
3378 x_new_focus_frame (dpyinfo, 0);
3381 #ifdef HAVE_X_I18N
3382 if (FRAME_XIC (frame))
3383 XUnsetICFocus (FRAME_XIC (frame));
3384 #endif
3385 if (frame->pointer_invisible)
3386 XTtoggle_invisible_pointer (frame, 0);
3390 /* The focus may have changed. Figure out if it is a real focus change,
3391 by checking both FocusIn/Out and Enter/LeaveNotify events.
3393 Returns FOCUS_IN_EVENT event in *BUFP. */
3395 static void
3396 x_detect_focus_change (dpyinfo, event, bufp)
3397 struct x_display_info *dpyinfo;
3398 XEvent *event;
3399 struct input_event *bufp;
3401 struct frame *frame;
3403 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3404 if (! frame)
3405 return;
3407 switch (event->type)
3409 case EnterNotify:
3410 case LeaveNotify:
3412 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3413 int focus_state
3414 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3416 if (event->xcrossing.detail != NotifyInferior
3417 && event->xcrossing.focus
3418 && ! (focus_state & FOCUS_EXPLICIT))
3419 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3420 FOCUS_IMPLICIT,
3421 dpyinfo, frame, bufp);
3423 break;
3425 case FocusIn:
3426 case FocusOut:
3427 x_focus_changed (event->type,
3428 (event->xfocus.detail == NotifyPointer ?
3429 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3430 dpyinfo, frame, bufp);
3431 break;
3433 case ClientMessage:
3434 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3436 enum xembed_message msg = event->xclient.data.l[1];
3437 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3438 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3440 break;
3445 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3447 void
3448 x_mouse_leave (dpyinfo)
3449 struct x_display_info *dpyinfo;
3451 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3454 /* The focus has changed, or we have redirected a frame's focus to
3455 another frame (this happens when a frame uses a surrogate
3456 mini-buffer frame). Shift the highlight as appropriate.
3458 The FRAME argument doesn't necessarily have anything to do with which
3459 frame is being highlighted or un-highlighted; we only use it to find
3460 the appropriate X display info. */
3462 static void
3463 XTframe_rehighlight (frame)
3464 struct frame *frame;
3466 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3469 static void
3470 x_frame_rehighlight (dpyinfo)
3471 struct x_display_info *dpyinfo;
3473 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3475 if (dpyinfo->x_focus_frame)
3477 dpyinfo->x_highlight_frame
3478 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3479 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3480 : dpyinfo->x_focus_frame);
3481 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3483 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3484 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3487 else
3488 dpyinfo->x_highlight_frame = 0;
3490 if (dpyinfo->x_highlight_frame != old_highlight)
3492 if (old_highlight)
3493 frame_unhighlight (old_highlight);
3494 if (dpyinfo->x_highlight_frame)
3495 frame_highlight (dpyinfo->x_highlight_frame);
3501 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3503 /* Initialize mode_switch_bit and modifier_meaning. */
3504 static void
3505 x_find_modifier_meanings (dpyinfo)
3506 struct x_display_info *dpyinfo;
3508 int min_code, max_code;
3509 KeySym *syms;
3510 int syms_per_code;
3511 XModifierKeymap *mods;
3513 dpyinfo->meta_mod_mask = 0;
3514 dpyinfo->shift_lock_mask = 0;
3515 dpyinfo->alt_mod_mask = 0;
3516 dpyinfo->super_mod_mask = 0;
3517 dpyinfo->hyper_mod_mask = 0;
3519 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3521 syms = XGetKeyboardMapping (dpyinfo->display,
3522 min_code, max_code - min_code + 1,
3523 &syms_per_code);
3524 mods = XGetModifierMapping (dpyinfo->display);
3526 /* Scan the modifier table to see which modifier bits the Meta and
3527 Alt keysyms are on. */
3529 int row, col; /* The row and column in the modifier table. */
3530 int found_alt_or_meta;
3532 for (row = 3; row < 8; row++)
3534 found_alt_or_meta = 0;
3535 for (col = 0; col < mods->max_keypermod; col++)
3537 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3539 /* Zeroes are used for filler. Skip them. */
3540 if (code == 0)
3541 continue;
3543 /* Are any of this keycode's keysyms a meta key? */
3545 int code_col;
3547 for (code_col = 0; code_col < syms_per_code; code_col++)
3549 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3551 switch (sym)
3553 case XK_Meta_L:
3554 case XK_Meta_R:
3555 found_alt_or_meta = 1;
3556 dpyinfo->meta_mod_mask |= (1 << row);
3557 break;
3559 case XK_Alt_L:
3560 case XK_Alt_R:
3561 found_alt_or_meta = 1;
3562 dpyinfo->alt_mod_mask |= (1 << row);
3563 break;
3565 case XK_Hyper_L:
3566 case XK_Hyper_R:
3567 if (!found_alt_or_meta)
3568 dpyinfo->hyper_mod_mask |= (1 << row);
3569 code_col = syms_per_code;
3570 col = mods->max_keypermod;
3571 break;
3573 case XK_Super_L:
3574 case XK_Super_R:
3575 if (!found_alt_or_meta)
3576 dpyinfo->super_mod_mask |= (1 << row);
3577 code_col = syms_per_code;
3578 col = mods->max_keypermod;
3579 break;
3581 case XK_Shift_Lock:
3582 /* Ignore this if it's not on the lock modifier. */
3583 if (!found_alt_or_meta && ((1 << row) == LockMask))
3584 dpyinfo->shift_lock_mask = LockMask;
3585 code_col = syms_per_code;
3586 col = mods->max_keypermod;
3587 break;
3595 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3596 if (! dpyinfo->meta_mod_mask)
3598 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3599 dpyinfo->alt_mod_mask = 0;
3602 /* If some keys are both alt and meta,
3603 make them just meta, not alt. */
3604 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3606 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3609 XFree ((char *) syms);
3610 XFreeModifiermap (mods);
3613 /* Convert between the modifier bits X uses and the modifier bits
3614 Emacs uses. */
3616 unsigned int
3617 x_x_to_emacs_modifiers (dpyinfo, state)
3618 struct x_display_info *dpyinfo;
3619 unsigned int state;
3621 EMACS_UINT mod_meta = meta_modifier;
3622 EMACS_UINT mod_alt = alt_modifier;
3623 EMACS_UINT mod_hyper = hyper_modifier;
3624 EMACS_UINT mod_super = super_modifier;
3625 Lisp_Object tem;
3627 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3628 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3629 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3630 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3631 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3632 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3633 tem = Fget (Vx_super_keysym, Qmodifier_value);
3634 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3637 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3638 | ((state & ControlMask) ? ctrl_modifier : 0)
3639 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3640 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3641 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3642 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3645 static unsigned int
3646 x_emacs_to_x_modifiers (dpyinfo, state)
3647 struct x_display_info *dpyinfo;
3648 unsigned int state;
3650 EMACS_UINT mod_meta = meta_modifier;
3651 EMACS_UINT mod_alt = alt_modifier;
3652 EMACS_UINT mod_hyper = hyper_modifier;
3653 EMACS_UINT mod_super = super_modifier;
3655 Lisp_Object tem;
3657 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3658 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3659 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3660 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3661 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3662 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3663 tem = Fget (Vx_super_keysym, Qmodifier_value);
3664 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3667 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3668 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3669 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3670 | ((state & shift_modifier) ? ShiftMask : 0)
3671 | ((state & ctrl_modifier) ? ControlMask : 0)
3672 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3675 /* Convert a keysym to its name. */
3677 char *
3678 x_get_keysym_name (keysym)
3679 KeySym keysym;
3681 char *value;
3683 BLOCK_INPUT;
3684 value = XKeysymToString (keysym);
3685 UNBLOCK_INPUT;
3687 return value;
3692 /* Mouse clicks and mouse movement. Rah. */
3694 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3696 If the event is a button press, then note that we have grabbed
3697 the mouse. */
3699 static Lisp_Object
3700 construct_mouse_click (result, event, f)
3701 struct input_event *result;
3702 XButtonEvent *event;
3703 struct frame *f;
3705 /* Make the event type NO_EVENT; we'll change that when we decide
3706 otherwise. */
3707 result->kind = MOUSE_CLICK_EVENT;
3708 result->code = event->button - Button1;
3709 result->timestamp = event->time;
3710 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3711 event->state)
3712 | (event->type == ButtonRelease
3713 ? up_modifier
3714 : down_modifier));
3716 XSETINT (result->x, event->x);
3717 XSETINT (result->y, event->y);
3718 XSETFRAME (result->frame_or_window, f);
3719 result->arg = Qnil;
3720 return Qnil;
3724 /* Function to report a mouse movement to the mainstream Emacs code.
3725 The input handler calls this.
3727 We have received a mouse movement event, which is given in *event.
3728 If the mouse is over a different glyph than it was last time, tell
3729 the mainstream emacs code by setting mouse_moved. If not, ask for
3730 another motion event, so we can check again the next time it moves. */
3732 static XMotionEvent last_mouse_motion_event;
3733 static Lisp_Object last_mouse_motion_frame;
3735 static int
3736 note_mouse_movement (frame, event)
3737 FRAME_PTR frame;
3738 XMotionEvent *event;
3740 last_mouse_movement_time = event->time;
3741 last_mouse_motion_event = *event;
3742 XSETFRAME (last_mouse_motion_frame, frame);
3744 if (!FRAME_X_OUTPUT (frame))
3745 return 0;
3747 if (event->window != FRAME_X_WINDOW (frame))
3749 frame->mouse_moved = 1;
3750 last_mouse_scroll_bar = Qnil;
3751 note_mouse_highlight (frame, -1, -1);
3752 last_mouse_glyph_frame = 0;
3753 return 1;
3757 /* Has the mouse moved off the glyph it was on at the last sighting? */
3758 if (frame != last_mouse_glyph_frame
3759 || event->x < last_mouse_glyph.x
3760 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3761 || event->y < last_mouse_glyph.y
3762 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3764 frame->mouse_moved = 1;
3765 last_mouse_scroll_bar = Qnil;
3766 note_mouse_highlight (frame, event->x, event->y);
3767 /* Remember which glyph we're now on. */
3768 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3769 last_mouse_glyph_frame = frame;
3770 return 1;
3773 return 0;
3777 /************************************************************************
3778 Mouse Face
3779 ************************************************************************/
3781 static void
3782 redo_mouse_highlight ()
3784 if (!NILP (last_mouse_motion_frame)
3785 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3786 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3787 last_mouse_motion_event.x,
3788 last_mouse_motion_event.y);
3793 /* Return the current position of the mouse.
3794 *FP should be a frame which indicates which display to ask about.
3796 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3797 and *PART to the frame, window, and scroll bar part that the mouse
3798 is over. Set *X and *Y to the portion and whole of the mouse's
3799 position on the scroll bar.
3801 If the mouse movement started elsewhere, set *FP to the frame the
3802 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3803 the mouse is over.
3805 Set *TIME to the server time-stamp for the time at which the mouse
3806 was at this position.
3808 Don't store anything if we don't have a valid set of values to report.
3810 This clears the mouse_moved flag, so we can wait for the next mouse
3811 movement. */
3813 static void
3814 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3815 FRAME_PTR *fp;
3816 int insist;
3817 Lisp_Object *bar_window;
3818 enum scroll_bar_part *part;
3819 Lisp_Object *x, *y;
3820 unsigned long *time;
3822 FRAME_PTR f1;
3824 BLOCK_INPUT;
3826 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3827 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3828 else
3830 Window root;
3831 int root_x, root_y;
3833 Window dummy_window;
3834 int dummy;
3836 Lisp_Object frame, tail;
3838 /* Clear the mouse-moved flag for every frame on this display. */
3839 FOR_EACH_FRAME (tail, frame)
3840 if (FRAME_X_P (XFRAME (frame))
3841 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3842 XFRAME (frame)->mouse_moved = 0;
3844 last_mouse_scroll_bar = Qnil;
3846 /* Figure out which root window we're on. */
3847 XQueryPointer (FRAME_X_DISPLAY (*fp),
3848 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3850 /* The root window which contains the pointer. */
3851 &root,
3853 /* Trash which we can't trust if the pointer is on
3854 a different screen. */
3855 &dummy_window,
3857 /* The position on that root window. */
3858 &root_x, &root_y,
3860 /* More trash we can't trust. */
3861 &dummy, &dummy,
3863 /* Modifier keys and pointer buttons, about which
3864 we don't care. */
3865 (unsigned int *) &dummy);
3867 /* Now we have a position on the root; find the innermost window
3868 containing the pointer. */
3870 Window win, child;
3871 int win_x, win_y;
3872 int parent_x = 0, parent_y = 0;
3874 win = root;
3876 /* XTranslateCoordinates can get errors if the window
3877 structure is changing at the same time this function
3878 is running. So at least we must not crash from them. */
3880 x_catch_errors (FRAME_X_DISPLAY (*fp));
3882 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3883 && FRAME_LIVE_P (last_mouse_frame))
3885 /* If mouse was grabbed on a frame, give coords for that frame
3886 even if the mouse is now outside it. */
3887 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3889 /* From-window, to-window. */
3890 root, FRAME_X_WINDOW (last_mouse_frame),
3892 /* From-position, to-position. */
3893 root_x, root_y, &win_x, &win_y,
3895 /* Child of win. */
3896 &child);
3897 f1 = last_mouse_frame;
3899 else
3901 while (1)
3903 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3905 /* From-window, to-window. */
3906 root, win,
3908 /* From-position, to-position. */
3909 root_x, root_y, &win_x, &win_y,
3911 /* Child of win. */
3912 &child);
3914 if (child == None || child == win)
3915 break;
3916 #ifdef USE_GTK
3917 /* We don't wan't to know the innermost window. We
3918 want the edit window. For non-Gtk+ the innermost
3919 window is the edit window. For Gtk+ it might not
3920 be. It might be the tool bar for example. */
3921 if (x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win))
3922 break;
3923 #endif
3924 win = child;
3925 parent_x = win_x;
3926 parent_y = win_y;
3929 /* Now we know that:
3930 win is the innermost window containing the pointer
3931 (XTC says it has no child containing the pointer),
3932 win_x and win_y are the pointer's position in it
3933 (XTC did this the last time through), and
3934 parent_x and parent_y are the pointer's position in win's parent.
3935 (They are what win_x and win_y were when win was child.
3936 If win is the root window, it has no parent, and
3937 parent_{x,y} are invalid, but that's okay, because we'll
3938 never use them in that case.) */
3940 #ifdef USE_GTK
3941 /* We don't wan't to know the innermost window. We
3942 want the edit window. */
3943 f1 = x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3944 #else
3945 /* Is win one of our frames? */
3946 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3947 #endif
3949 #ifdef USE_X_TOOLKIT
3950 /* If we end up with the menu bar window, say it's not
3951 on the frame. */
3952 if (f1 != NULL
3953 && f1->output_data.x->menubar_widget
3954 && win == XtWindow (f1->output_data.x->menubar_widget))
3955 f1 = NULL;
3956 #endif /* USE_X_TOOLKIT */
3959 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3960 f1 = 0;
3962 x_uncatch_errors ();
3964 /* If not, is it one of our scroll bars? */
3965 if (! f1)
3967 struct scroll_bar *bar;
3969 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3971 if (bar)
3973 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3974 win_x = parent_x;
3975 win_y = parent_y;
3979 if (f1 == 0 && insist > 0)
3980 f1 = SELECTED_FRAME ();
3982 if (f1)
3984 /* Ok, we found a frame. Store all the values.
3985 last_mouse_glyph is a rectangle used to reduce the
3986 generation of mouse events. To not miss any motion
3987 events, we must divide the frame into rectangles of the
3988 size of the smallest character that could be displayed
3989 on it, i.e. into the same rectangles that matrices on
3990 the frame are divided into. */
3992 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
3993 last_mouse_glyph_frame = f1;
3995 *bar_window = Qnil;
3996 *part = 0;
3997 *fp = f1;
3998 XSETINT (*x, win_x);
3999 XSETINT (*y, win_y);
4000 *time = last_mouse_movement_time;
4005 UNBLOCK_INPUT;
4010 /***********************************************************************
4011 Scroll bars
4012 ***********************************************************************/
4014 /* Scroll bar support. */
4016 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4017 manages it.
4018 This can be called in GC, so we have to make sure to strip off mark
4019 bits. */
4021 static struct scroll_bar *
4022 x_window_to_scroll_bar (display, window_id)
4023 Display *display;
4024 Window window_id;
4026 Lisp_Object tail;
4028 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4029 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4030 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4032 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4034 Lisp_Object frame, bar, condemned;
4036 frame = XCAR (tail);
4037 /* All elements of Vframe_list should be frames. */
4038 if (! FRAMEP (frame))
4039 abort ();
4041 if (! FRAME_X_P (XFRAME (frame)))
4042 continue;
4044 /* Scan this frame's scroll bar list for a scroll bar with the
4045 right window ID. */
4046 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4047 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4048 /* This trick allows us to search both the ordinary and
4049 condemned scroll bar lists with one loop. */
4050 ! NILP (bar) || (bar = condemned,
4051 condemned = Qnil,
4052 ! NILP (bar));
4053 bar = XSCROLL_BAR (bar)->next)
4054 if (XSCROLL_BAR (bar)->x_window == window_id &&
4055 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4056 return XSCROLL_BAR (bar);
4059 return 0;
4063 #if defined USE_LUCID
4065 /* Return the Lucid menu bar WINDOW is part of. Return null
4066 if WINDOW is not part of a menu bar. */
4068 static Widget
4069 x_window_to_menu_bar (window)
4070 Window window;
4072 Lisp_Object tail;
4074 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4076 if (FRAME_X_P (XFRAME (XCAR (tail))))
4078 Lisp_Object frame = XCAR (tail);
4079 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4081 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4082 return menu_bar;
4086 return NULL;
4089 #endif /* USE_LUCID */
4092 /************************************************************************
4093 Toolkit scroll bars
4094 ************************************************************************/
4096 #ifdef USE_TOOLKIT_SCROLL_BARS
4098 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4099 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4100 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4101 struct scroll_bar *));
4102 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4103 int, int, int));
4106 /* Lisp window being scrolled. Set when starting to interact with
4107 a toolkit scroll bar, reset to nil when ending the interaction. */
4109 static Lisp_Object window_being_scrolled;
4111 /* Last scroll bar part sent in xm_scroll_callback. */
4113 static int last_scroll_bar_part;
4115 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4116 that movements of 1/20 of the screen size are mapped to up/down. */
4118 #ifndef USE_GTK
4119 /* Id of action hook installed for scroll bars. */
4121 static XtActionHookId action_hook_id;
4123 static Boolean xaw3d_arrow_scroll;
4125 /* Whether the drag scrolling maintains the mouse at the top of the
4126 thumb. If not, resizing the thumb needs to be done more carefully
4127 to avoid jerkyness. */
4129 static Boolean xaw3d_pick_top;
4131 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4132 bars are used.. The hook is responsible for detecting when
4133 the user ends an interaction with the scroll bar, and generates
4134 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4136 static void
4137 xt_action_hook (widget, client_data, action_name, event, params,
4138 num_params)
4139 Widget widget;
4140 XtPointer client_data;
4141 String action_name;
4142 XEvent *event;
4143 String *params;
4144 Cardinal *num_params;
4146 int scroll_bar_p;
4147 char *end_action;
4149 #ifdef USE_MOTIF
4150 scroll_bar_p = XmIsScrollBar (widget);
4151 end_action = "Release";
4152 #else /* !USE_MOTIF i.e. use Xaw */
4153 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4154 end_action = "EndScroll";
4155 #endif /* USE_MOTIF */
4157 if (scroll_bar_p
4158 && strcmp (action_name, end_action) == 0
4159 && WINDOWP (window_being_scrolled))
4161 struct window *w;
4163 x_send_scroll_bar_event (window_being_scrolled,
4164 scroll_bar_end_scroll, 0, 0);
4165 w = XWINDOW (window_being_scrolled);
4167 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4169 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4170 /* The thumb size is incorrect while dragging: fix it. */
4171 set_vertical_scroll_bar (w);
4173 window_being_scrolled = Qnil;
4174 last_scroll_bar_part = -1;
4176 /* Xt timeouts no longer needed. */
4177 toolkit_scroll_bar_interaction = 0;
4180 #endif /* not USE_GTK */
4182 /* A vector of windows used for communication between
4183 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4185 static struct window **scroll_bar_windows;
4186 static int scroll_bar_windows_size;
4189 /* Send a client message with message type Xatom_Scrollbar for a
4190 scroll action to the frame of WINDOW. PART is a value identifying
4191 the part of the scroll bar that was clicked on. PORTION is the
4192 amount to scroll of a whole of WHOLE. */
4194 static void
4195 x_send_scroll_bar_event (window, part, portion, whole)
4196 Lisp_Object window;
4197 int part, portion, whole;
4199 XEvent event;
4200 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4201 struct window *w = XWINDOW (window);
4202 struct frame *f = XFRAME (w->frame);
4203 int i;
4205 BLOCK_INPUT;
4207 /* Construct a ClientMessage event to send to the frame. */
4208 ev->type = ClientMessage;
4209 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4210 ev->display = FRAME_X_DISPLAY (f);
4211 ev->window = FRAME_X_WINDOW (f);
4212 ev->format = 32;
4214 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4215 not enough to store a pointer or Lisp_Object on a 64 bit system.
4216 So, store the window in scroll_bar_windows and pass the index
4217 into that array in the event. */
4218 for (i = 0; i < scroll_bar_windows_size; ++i)
4219 if (scroll_bar_windows[i] == NULL)
4220 break;
4222 if (i == scroll_bar_windows_size)
4224 int new_size = max (10, 2 * scroll_bar_windows_size);
4225 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4226 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4228 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4229 nbytes);
4230 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4231 scroll_bar_windows_size = new_size;
4234 scroll_bar_windows[i] = w;
4235 ev->data.l[0] = (long) i;
4236 ev->data.l[1] = (long) part;
4237 ev->data.l[2] = (long) 0;
4238 ev->data.l[3] = (long) portion;
4239 ev->data.l[4] = (long) whole;
4241 /* Make Xt timeouts work while the scroll bar is active. */
4242 toolkit_scroll_bar_interaction = 1;
4243 #ifdef USE_X_TOOLKIT
4244 x_activate_timeout_atimer ();
4245 #endif
4247 /* Setting the event mask to zero means that the message will
4248 be sent to the client that created the window, and if that
4249 window no longer exists, no event will be sent. */
4250 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4251 UNBLOCK_INPUT;
4255 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4256 in *IEVENT. */
4258 static void
4259 x_scroll_bar_to_input_event (event, ievent)
4260 XEvent *event;
4261 struct input_event *ievent;
4263 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4264 Lisp_Object window;
4265 struct frame *f;
4266 struct window *w;
4268 w = scroll_bar_windows[ev->data.l[0]];
4269 scroll_bar_windows[ev->data.l[0]] = NULL;
4271 XSETWINDOW (window, w);
4272 f = XFRAME (w->frame);
4274 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4275 ievent->frame_or_window = window;
4276 ievent->arg = Qnil;
4277 #ifdef USE_GTK
4278 ievent->timestamp = CurrentTime;
4279 #else
4280 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4281 #endif
4282 ievent->part = ev->data.l[1];
4283 ievent->code = ev->data.l[2];
4284 ievent->x = make_number ((int) ev->data.l[3]);
4285 ievent->y = make_number ((int) ev->data.l[4]);
4286 ievent->modifiers = 0;
4290 #ifdef USE_MOTIF
4292 /* Minimum and maximum values used for Motif scroll bars. */
4294 #define XM_SB_MAX 10000000
4297 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4298 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4299 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4301 static void
4302 xm_scroll_callback (widget, client_data, call_data)
4303 Widget widget;
4304 XtPointer client_data, call_data;
4306 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4307 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4308 int part = -1, whole = 0, portion = 0;
4310 switch (cs->reason)
4312 case XmCR_DECREMENT:
4313 bar->dragging = Qnil;
4314 part = scroll_bar_up_arrow;
4315 break;
4317 case XmCR_INCREMENT:
4318 bar->dragging = Qnil;
4319 part = scroll_bar_down_arrow;
4320 break;
4322 case XmCR_PAGE_DECREMENT:
4323 bar->dragging = Qnil;
4324 part = scroll_bar_above_handle;
4325 break;
4327 case XmCR_PAGE_INCREMENT:
4328 bar->dragging = Qnil;
4329 part = scroll_bar_below_handle;
4330 break;
4332 case XmCR_TO_TOP:
4333 bar->dragging = Qnil;
4334 part = scroll_bar_to_top;
4335 break;
4337 case XmCR_TO_BOTTOM:
4338 bar->dragging = Qnil;
4339 part = scroll_bar_to_bottom;
4340 break;
4342 case XmCR_DRAG:
4344 int slider_size;
4346 /* Get the slider size. */
4347 BLOCK_INPUT;
4348 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4349 UNBLOCK_INPUT;
4351 whole = XM_SB_MAX - slider_size;
4352 portion = min (cs->value, whole);
4353 part = scroll_bar_handle;
4354 bar->dragging = make_number (cs->value);
4356 break;
4358 case XmCR_VALUE_CHANGED:
4359 break;
4362 if (part >= 0)
4364 window_being_scrolled = bar->window;
4365 last_scroll_bar_part = part;
4366 x_send_scroll_bar_event (bar->window, part, portion, whole);
4370 #elif defined USE_GTK
4372 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4373 bar widget. DATA is a pointer to the scroll_bar structure. */
4375 static void
4376 xg_scroll_callback (widget, data)
4377 GtkRange *widget;
4378 gpointer data;
4380 struct scroll_bar *bar = (struct scroll_bar *) data;
4381 gdouble previous;
4382 gdouble position;
4383 gdouble *p;
4384 int diff;
4386 int part = -1, whole = 0, portion = 0;
4387 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4389 position = gtk_adjustment_get_value (adj);
4391 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4392 if (! p)
4394 p = (gdouble*) xmalloc (sizeof (gdouble));
4395 *p = XG_SB_MIN;
4396 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4399 previous = *p;
4400 *p = position;
4402 if (xg_ignore_gtk_scrollbar) return;
4404 diff = (int) (position - previous);
4406 if (diff == (int) adj->step_increment)
4408 part = scroll_bar_down_arrow;
4409 bar->dragging = Qnil;
4411 else if (-diff == (int) adj->step_increment)
4413 part = scroll_bar_up_arrow;
4414 bar->dragging = Qnil;
4416 else if (diff == (int) adj->page_increment)
4418 part = scroll_bar_below_handle;
4419 bar->dragging = Qnil;
4421 else if (-diff == (int) adj->page_increment)
4423 part = scroll_bar_above_handle;
4424 bar->dragging = Qnil;
4426 else
4428 part = scroll_bar_handle;
4429 whole = adj->upper - adj->page_size;
4430 portion = min ((int)position, whole);
4431 bar->dragging = make_number ((int)portion);
4434 if (part >= 0)
4436 window_being_scrolled = bar->window;
4437 last_scroll_bar_part = part;
4438 x_send_scroll_bar_event (bar->window, part, portion, whole);
4442 #else /* not USE_GTK and not USE_MOTIF */
4444 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4445 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4446 scroll bar struct. CALL_DATA is a pointer to a float saying where
4447 the thumb is. */
4449 static void
4450 xaw_jump_callback (widget, client_data, call_data)
4451 Widget widget;
4452 XtPointer client_data, call_data;
4454 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4455 float top = *(float *) call_data;
4456 float shown;
4457 int whole, portion, height;
4458 int part;
4460 /* Get the size of the thumb, a value between 0 and 1. */
4461 BLOCK_INPUT;
4462 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4463 UNBLOCK_INPUT;
4465 whole = 10000000;
4466 portion = shown < 1 ? top * whole : 0;
4468 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4469 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4470 the bottom, so we force the scrolling whenever we see that we're
4471 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4472 we try to ensure that we always stay two pixels away from the
4473 bottom). */
4474 part = scroll_bar_down_arrow;
4475 else
4476 part = scroll_bar_handle;
4478 window_being_scrolled = bar->window;
4479 bar->dragging = make_number (portion);
4480 last_scroll_bar_part = part;
4481 x_send_scroll_bar_event (bar->window, part, portion, whole);
4485 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4486 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4487 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4488 the scroll bar. CALL_DATA is an integer specifying the action that
4489 has taken place. Its magnitude is in the range 0..height of the
4490 scroll bar. Negative values mean scroll towards buffer start.
4491 Values < height of scroll bar mean line-wise movement. */
4493 static void
4494 xaw_scroll_callback (widget, client_data, call_data)
4495 Widget widget;
4496 XtPointer client_data, call_data;
4498 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4499 /* The position really is stored cast to a pointer. */
4500 int position = (long) call_data;
4501 Dimension height;
4502 int part;
4504 /* Get the height of the scroll bar. */
4505 BLOCK_INPUT;
4506 XtVaGetValues (widget, XtNheight, &height, NULL);
4507 UNBLOCK_INPUT;
4509 if (eabs (position) >= height)
4510 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4512 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4513 it maps line-movement to call_data = max(5, height/20). */
4514 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4515 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4516 else
4517 part = scroll_bar_move_ratio;
4519 window_being_scrolled = bar->window;
4520 bar->dragging = Qnil;
4521 last_scroll_bar_part = part;
4522 x_send_scroll_bar_event (bar->window, part, position, height);
4525 #endif /* not USE_GTK and not USE_MOTIF */
4527 #define SCROLL_BAR_NAME "verticalScrollBar"
4529 /* Create the widget for scroll bar BAR on frame F. Record the widget
4530 and X window of the scroll bar in BAR. */
4532 #ifdef USE_GTK
4533 static void
4534 x_create_toolkit_scroll_bar (f, bar)
4535 struct frame *f;
4536 struct scroll_bar *bar;
4538 char *scroll_bar_name = SCROLL_BAR_NAME;
4540 BLOCK_INPUT;
4541 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4542 scroll_bar_name);
4543 UNBLOCK_INPUT;
4546 #else /* not USE_GTK */
4548 static void
4549 x_create_toolkit_scroll_bar (f, bar)
4550 struct frame *f;
4551 struct scroll_bar *bar;
4553 Window xwindow;
4554 Widget widget;
4555 Arg av[20];
4556 int ac = 0;
4557 char *scroll_bar_name = SCROLL_BAR_NAME;
4558 unsigned long pixel;
4560 BLOCK_INPUT;
4562 #ifdef USE_MOTIF
4563 /* Set resources. Create the widget. */
4564 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4565 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4566 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4567 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4568 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4569 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4570 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4572 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4573 if (pixel != -1)
4575 XtSetArg (av[ac], XmNforeground, pixel);
4576 ++ac;
4579 pixel = f->output_data.x->scroll_bar_background_pixel;
4580 if (pixel != -1)
4582 XtSetArg (av[ac], XmNbackground, pixel);
4583 ++ac;
4586 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4587 scroll_bar_name, av, ac);
4589 /* Add one callback for everything that can happen. */
4590 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4591 (XtPointer) bar);
4592 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4593 (XtPointer) bar);
4594 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4595 (XtPointer) bar);
4596 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4597 (XtPointer) bar);
4598 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4599 (XtPointer) bar);
4600 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4601 (XtPointer) bar);
4602 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4603 (XtPointer) bar);
4605 /* Realize the widget. Only after that is the X window created. */
4606 XtRealizeWidget (widget);
4608 /* Set the cursor to an arrow. I didn't find a resource to do that.
4609 And I'm wondering why it hasn't an arrow cursor by default. */
4610 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4611 f->output_data.x->nontext_cursor);
4613 #else /* !USE_MOTIF i.e. use Xaw */
4615 /* Set resources. Create the widget. The background of the
4616 Xaw3d scroll bar widget is a little bit light for my taste.
4617 We don't alter it here to let users change it according
4618 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4619 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4620 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4621 /* For smoother scrolling with Xaw3d -sm */
4622 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4624 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4625 if (pixel != -1)
4627 XtSetArg (av[ac], XtNforeground, pixel);
4628 ++ac;
4631 pixel = f->output_data.x->scroll_bar_background_pixel;
4632 if (pixel != -1)
4634 XtSetArg (av[ac], XtNbackground, pixel);
4635 ++ac;
4638 /* Top/bottom shadow colors. */
4640 /* Allocate them, if necessary. */
4641 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4643 pixel = f->output_data.x->scroll_bar_background_pixel;
4644 if (pixel != -1)
4646 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4647 FRAME_X_COLORMAP (f),
4648 &pixel, 1.2, 0x8000))
4649 pixel = -1;
4650 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4653 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4655 pixel = f->output_data.x->scroll_bar_background_pixel;
4656 if (pixel != -1)
4658 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4659 FRAME_X_COLORMAP (f),
4660 &pixel, 0.6, 0x4000))
4661 pixel = -1;
4662 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4666 #ifdef XtNbeNiceToColormap
4667 /* Tell the toolkit about them. */
4668 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4669 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4670 /* We tried to allocate a color for the top/bottom shadow, and
4671 failed, so tell Xaw3d to use dithering instead. */
4673 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4674 ++ac;
4676 else
4677 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4678 be more consistent with other emacs 3d colors, and since Xaw3d is
4679 not good at dealing with allocation failure. */
4681 /* This tells Xaw3d to use real colors instead of dithering for
4682 the shadows. */
4683 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4684 ++ac;
4686 /* Specify the colors. */
4687 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4688 if (pixel != -1)
4690 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4691 ++ac;
4693 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4694 if (pixel != -1)
4696 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4697 ++ac;
4700 #endif
4702 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4703 f->output_data.x->edit_widget, av, ac);
4706 char *initial = "";
4707 char *val = initial;
4708 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4709 #ifdef XtNarrowScrollbars
4710 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4711 #endif
4712 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4713 if (xaw3d_arrow_scroll || val == initial)
4714 { /* ARROW_SCROLL */
4715 xaw3d_arrow_scroll = True;
4716 /* Isn't that just a personal preference ? --Stef */
4717 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4721 /* Define callbacks. */
4722 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4723 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4724 (XtPointer) bar);
4726 /* Realize the widget. Only after that is the X window created. */
4727 XtRealizeWidget (widget);
4729 #endif /* !USE_MOTIF */
4731 /* Install an action hook that lets us detect when the user
4732 finishes interacting with a scroll bar. */
4733 if (action_hook_id == 0)
4734 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4736 /* Remember X window and widget in the scroll bar vector. */
4737 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4738 xwindow = XtWindow (widget);
4739 bar->x_window = xwindow;
4741 UNBLOCK_INPUT;
4743 #endif /* not USE_GTK */
4746 /* Set the thumb size and position of scroll bar BAR. We are currently
4747 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4749 #ifdef USE_GTK
4750 static void
4751 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4752 struct scroll_bar *bar;
4753 int portion, position, whole;
4755 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4758 #else /* not USE_GTK */
4759 static void
4760 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4761 struct scroll_bar *bar;
4762 int portion, position, whole;
4764 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4765 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4766 float top, shown;
4768 BLOCK_INPUT;
4770 #ifdef USE_MOTIF
4772 /* We use an estimate of 30 chars per line rather than the real
4773 `portion' value. This has the disadvantage that the thumb size
4774 is not very representative, but it makes our life a lot easier.
4775 Otherwise, we have to constantly adjust the thumb size, which
4776 we can't always do quickly enough: while dragging, the size of
4777 the thumb might prevent the user from dragging the thumb all the
4778 way to the end. but Motif and some versions of Xaw3d don't allow
4779 updating the thumb size while dragging. Also, even if we can update
4780 its size, the update will often happen too late.
4781 If you don't believe it, check out revision 1.650 of xterm.c to see
4782 what hoops we were going through and the still poor behavior we got. */
4783 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4784 /* When the thumb is at the bottom, position == whole.
4785 So we need to increase `whole' to make space for the thumb. */
4786 whole += portion;
4788 if (whole <= 0)
4789 top = 0, shown = 1;
4790 else
4792 top = (float) position / whole;
4793 shown = (float) portion / whole;
4796 if (NILP (bar->dragging))
4798 int size, value;
4800 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4801 is the scroll bar's maximum and MIN is the scroll bar's minimum
4802 value. */
4803 size = shown * XM_SB_MAX;
4804 size = min (size, XM_SB_MAX);
4805 size = max (size, 1);
4807 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4808 value = top * XM_SB_MAX;
4809 value = min (value, XM_SB_MAX - size);
4811 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4813 #else /* !USE_MOTIF i.e. use Xaw */
4815 if (whole == 0)
4816 top = 0, shown = 1;
4817 else
4819 top = (float) position / whole;
4820 shown = (float) portion / whole;
4824 float old_top, old_shown;
4825 Dimension height;
4826 XtVaGetValues (widget,
4827 XtNtopOfThumb, &old_top,
4828 XtNshown, &old_shown,
4829 XtNheight, &height,
4830 NULL);
4832 /* Massage the top+shown values. */
4833 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4834 top = max (0, min (1, top));
4835 else
4836 top = old_top;
4837 /* Keep two pixels available for moving the thumb down. */
4838 shown = max (0, min (1 - top - (2.0 / height), shown));
4840 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4841 check that your system's configuration file contains a define
4842 for `NARROWPROTO'. See s/freebsd.h for an example. */
4843 if (top != old_top || shown != old_shown)
4845 if (NILP (bar->dragging))
4846 XawScrollbarSetThumb (widget, top, shown);
4847 else
4849 /* Try to make the scrolling a tad smoother. */
4850 if (!xaw3d_pick_top)
4851 shown = min (shown, old_shown);
4853 XawScrollbarSetThumb (widget, top, shown);
4857 #endif /* !USE_MOTIF */
4859 UNBLOCK_INPUT;
4861 #endif /* not USE_GTK */
4863 #endif /* USE_TOOLKIT_SCROLL_BARS */
4867 /************************************************************************
4868 Scroll bars, general
4869 ************************************************************************/
4871 /* Create a scroll bar and return the scroll bar vector for it. W is
4872 the Emacs window on which to create the scroll bar. TOP, LEFT,
4873 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4874 scroll bar. */
4876 static struct scroll_bar *
4877 x_scroll_bar_create (w, top, left, width, height)
4878 struct window *w;
4879 int top, left, width, height;
4881 struct frame *f = XFRAME (w->frame);
4882 struct scroll_bar *bar
4883 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4885 BLOCK_INPUT;
4887 #ifdef USE_TOOLKIT_SCROLL_BARS
4888 x_create_toolkit_scroll_bar (f, bar);
4889 #else /* not USE_TOOLKIT_SCROLL_BARS */
4891 XSetWindowAttributes a;
4892 unsigned long mask;
4893 Window window;
4895 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4896 if (a.background_pixel == -1)
4897 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4899 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4900 | ButtonMotionMask | PointerMotionHintMask
4901 | ExposureMask);
4902 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4904 mask = (CWBackPixel | CWEventMask | CWCursor);
4906 /* Clear the area of W that will serve as a scroll bar. This is
4907 for the case that a window has been split horizontally. In
4908 this case, no clear_frame is generated to reduce flickering. */
4909 if (width > 0 && height > 0)
4910 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4911 left, top, width,
4912 window_box_height (w), False);
4914 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4915 /* Position and size of scroll bar. */
4916 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4917 top,
4918 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4919 height,
4920 /* Border width, depth, class, and visual. */
4922 CopyFromParent,
4923 CopyFromParent,
4924 CopyFromParent,
4925 /* Attributes. */
4926 mask, &a);
4927 bar->x_window = window;
4929 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4931 XSETWINDOW (bar->window, w);
4932 bar->top = top;
4933 bar->left = left;
4934 bar->width = width;
4935 bar->height = height;
4936 bar->start = 0;
4937 bar->end = 0;
4938 bar->dragging = Qnil;
4939 bar->fringe_extended_p = 0;
4941 /* Add bar to its frame's list of scroll bars. */
4942 bar->next = FRAME_SCROLL_BARS (f);
4943 bar->prev = Qnil;
4944 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4945 if (!NILP (bar->next))
4946 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4948 /* Map the window/widget. */
4949 #ifdef USE_TOOLKIT_SCROLL_BARS
4951 #ifdef USE_GTK
4952 xg_update_scrollbar_pos (f,
4953 bar->x_window,
4954 top,
4955 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4956 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4957 max (height, 1));
4958 xg_show_scroll_bar (bar->x_window);
4959 #else /* not USE_GTK */
4960 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4961 XtConfigureWidget (scroll_bar,
4962 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4963 top,
4964 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4965 max (height, 1), 0);
4966 XtMapWidget (scroll_bar);
4967 #endif /* not USE_GTK */
4969 #else /* not USE_TOOLKIT_SCROLL_BARS */
4970 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
4971 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4973 UNBLOCK_INPUT;
4974 return bar;
4978 #ifndef USE_TOOLKIT_SCROLL_BARS
4980 /* Draw BAR's handle in the proper position.
4982 If the handle is already drawn from START to END, don't bother
4983 redrawing it, unless REBUILD is non-zero; in that case, always
4984 redraw it. (REBUILD is handy for drawing the handle after expose
4985 events.)
4987 Normally, we want to constrain the start and end of the handle to
4988 fit inside its rectangle, but if the user is dragging the scroll
4989 bar handle, we want to let them drag it down all the way, so that
4990 the bar's top is as far down as it goes; otherwise, there's no way
4991 to move to the very end of the buffer. */
4993 static void
4994 x_scroll_bar_set_handle (bar, start, end, rebuild)
4995 struct scroll_bar *bar;
4996 int start, end;
4997 int rebuild;
4999 int dragging = ! NILP (bar->dragging);
5000 Window w = bar->x_window;
5001 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5002 GC gc = f->output_data.x->normal_gc;
5004 /* If the display is already accurate, do nothing. */
5005 if (! rebuild
5006 && start == bar->start
5007 && end == bar->end)
5008 return;
5010 BLOCK_INPUT;
5013 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5014 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5015 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5017 /* Make sure the values are reasonable, and try to preserve
5018 the distance between start and end. */
5020 int length = end - start;
5022 if (start < 0)
5023 start = 0;
5024 else if (start > top_range)
5025 start = top_range;
5026 end = start + length;
5028 if (end < start)
5029 end = start;
5030 else if (end > top_range && ! dragging)
5031 end = top_range;
5034 /* Store the adjusted setting in the scroll bar. */
5035 bar->start = start;
5036 bar->end = end;
5038 /* Clip the end position, just for display. */
5039 if (end > top_range)
5040 end = top_range;
5042 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5043 below top positions, to make sure the handle is always at least
5044 that many pixels tall. */
5045 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5047 /* Draw the empty space above the handle. Note that we can't clear
5048 zero-height areas; that means "clear to end of window." */
5049 if (0 < start)
5050 x_clear_area (FRAME_X_DISPLAY (f), w,
5051 /* x, y, width, height, and exposures. */
5052 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5053 VERTICAL_SCROLL_BAR_TOP_BORDER,
5054 inside_width, start,
5055 False);
5057 /* Change to proper foreground color if one is specified. */
5058 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5059 XSetForeground (FRAME_X_DISPLAY (f), gc,
5060 f->output_data.x->scroll_bar_foreground_pixel);
5062 /* Draw the handle itself. */
5063 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5064 /* x, y, width, height */
5065 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5066 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5067 inside_width, end - start);
5069 /* Restore the foreground color of the GC if we changed it above. */
5070 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5071 XSetForeground (FRAME_X_DISPLAY (f), gc,
5072 FRAME_FOREGROUND_PIXEL (f));
5074 /* Draw the empty space below the handle. Note that we can't
5075 clear zero-height areas; that means "clear to end of window." */
5076 if (end < inside_height)
5077 x_clear_area (FRAME_X_DISPLAY (f), w,
5078 /* x, y, width, height, and exposures. */
5079 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5080 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5081 inside_width, inside_height - end,
5082 False);
5086 UNBLOCK_INPUT;
5089 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5091 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5092 nil. */
5094 static void
5095 x_scroll_bar_remove (bar)
5096 struct scroll_bar *bar;
5098 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5099 BLOCK_INPUT;
5101 #ifdef USE_TOOLKIT_SCROLL_BARS
5102 #ifdef USE_GTK
5103 xg_remove_scroll_bar (f, bar->x_window);
5104 #else /* not USE_GTK */
5105 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5106 #endif /* not USE_GTK */
5107 #else
5108 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5109 #endif
5111 /* Disassociate this scroll bar from its window. */
5112 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5114 UNBLOCK_INPUT;
5118 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5119 that we are displaying PORTION characters out of a total of WHOLE
5120 characters, starting at POSITION. If WINDOW has no scroll bar,
5121 create one. */
5123 static void
5124 XTset_vertical_scroll_bar (w, portion, whole, position)
5125 struct window *w;
5126 int portion, whole, position;
5128 struct frame *f = XFRAME (w->frame);
5129 struct scroll_bar *bar;
5130 int top, height, left, sb_left, width, sb_width;
5131 int window_y, window_height;
5132 #ifdef USE_TOOLKIT_SCROLL_BARS
5133 int fringe_extended_p;
5134 #endif
5136 /* Get window dimensions. */
5137 window_box (w, -1, 0, &window_y, 0, &window_height);
5138 top = window_y;
5139 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5140 height = window_height;
5142 /* Compute the left edge of the scroll bar area. */
5143 left = WINDOW_SCROLL_BAR_AREA_X (w);
5145 /* Compute the width of the scroll bar which might be less than
5146 the width of the area reserved for the scroll bar. */
5147 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5148 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5149 else
5150 sb_width = width;
5152 /* Compute the left edge of the scroll bar. */
5153 #ifdef USE_TOOLKIT_SCROLL_BARS
5154 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5155 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5156 else
5157 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5158 #else
5159 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5160 sb_left = left + width - sb_width;
5161 else
5162 sb_left = left;
5163 #endif
5165 #ifdef USE_TOOLKIT_SCROLL_BARS
5166 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5167 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5168 && WINDOW_LEFT_FRINGE_WIDTH (w)
5169 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5170 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5171 else
5172 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5173 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5174 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5175 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5176 #endif
5178 /* Does the scroll bar exist yet? */
5179 if (NILP (w->vertical_scroll_bar))
5181 if (width > 0 && height > 0)
5183 BLOCK_INPUT;
5184 #ifdef USE_TOOLKIT_SCROLL_BARS
5185 if (fringe_extended_p)
5186 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5187 sb_left, top, sb_width, height, False);
5188 else
5189 #endif
5190 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5191 left, top, width, height, False);
5192 UNBLOCK_INPUT;
5195 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5197 else
5199 /* It may just need to be moved and resized. */
5200 unsigned int mask = 0;
5202 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5204 BLOCK_INPUT;
5206 if (sb_left != bar->left)
5207 mask |= CWX;
5208 if (top != bar->top)
5209 mask |= CWY;
5210 if (sb_width != bar->width)
5211 mask |= CWWidth;
5212 if (height != bar->height)
5213 mask |= CWHeight;
5215 #ifdef USE_TOOLKIT_SCROLL_BARS
5217 /* Move/size the scroll bar widget. */
5218 if (mask || bar->fringe_extended_p != fringe_extended_p)
5220 /* Since toolkit scroll bars are smaller than the space reserved
5221 for them on the frame, we have to clear "under" them. */
5222 if (width > 0 && height > 0)
5224 if (fringe_extended_p)
5225 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5226 sb_left, top, sb_width, height, False);
5227 else
5228 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5229 left, top, width, height, False);
5231 #ifdef USE_GTK
5232 xg_update_scrollbar_pos (f,
5233 bar->x_window,
5234 top,
5235 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5236 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5237 max (height, 1));
5238 #else /* not USE_GTK */
5239 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5240 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5241 top,
5242 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5243 max (height, 1), 0);
5244 #endif /* not USE_GTK */
5246 #else /* not USE_TOOLKIT_SCROLL_BARS */
5248 /* Clear areas not covered by the scroll bar because of
5249 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5250 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5252 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5253 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5254 height, False);
5255 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5256 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5257 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5258 height, False);
5261 /* Clear areas not covered by the scroll bar because it's not as
5262 wide as the area reserved for it. This makes sure a
5263 previous mode line display is cleared after C-x 2 C-x 1, for
5264 example. */
5266 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5267 int rest = area_width - sb_width;
5268 if (rest > 0 && height > 0)
5270 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5271 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5272 left + area_width - rest, top,
5273 rest, height, False);
5274 else
5275 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5276 left, top, rest, height, False);
5280 /* Move/size the scroll bar window. */
5281 if (mask)
5283 XWindowChanges wc;
5285 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5286 wc.y = top;
5287 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5288 wc.height = height;
5289 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5290 mask, &wc);
5293 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5295 /* Remember new settings. */
5296 bar->left = sb_left;
5297 bar->top = top;
5298 bar->width = sb_width;
5299 bar->height = height;
5301 UNBLOCK_INPUT;
5304 #ifdef USE_TOOLKIT_SCROLL_BARS
5305 bar->fringe_extended_p = fringe_extended_p;
5307 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5308 #else /* not USE_TOOLKIT_SCROLL_BARS */
5309 /* Set the scroll bar's current state, unless we're currently being
5310 dragged. */
5311 if (NILP (bar->dragging))
5313 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5315 if (whole == 0)
5316 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5317 else
5319 int start = ((double) position * top_range) / whole;
5320 int end = ((double) (position + portion) * top_range) / whole;
5321 x_scroll_bar_set_handle (bar, start, end, 0);
5324 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5326 XSETVECTOR (w->vertical_scroll_bar, bar);
5330 /* The following three hooks are used when we're doing a thorough
5331 redisplay of the frame. We don't explicitly know which scroll bars
5332 are going to be deleted, because keeping track of when windows go
5333 away is a real pain - "Can you say set-window-configuration, boys
5334 and girls?" Instead, we just assert at the beginning of redisplay
5335 that *all* scroll bars are to be removed, and then save a scroll bar
5336 from the fiery pit when we actually redisplay its window. */
5338 /* Arrange for all scroll bars on FRAME to be removed at the next call
5339 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5340 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5342 static void
5343 XTcondemn_scroll_bars (frame)
5344 FRAME_PTR frame;
5346 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5347 while (! NILP (FRAME_SCROLL_BARS (frame)))
5349 Lisp_Object bar;
5350 bar = FRAME_SCROLL_BARS (frame);
5351 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5352 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5353 XSCROLL_BAR (bar)->prev = Qnil;
5354 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5355 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5356 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5361 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5362 Note that WINDOW isn't necessarily condemned at all. */
5364 static void
5365 XTredeem_scroll_bar (window)
5366 struct window *window;
5368 struct scroll_bar *bar;
5369 struct frame *f;
5371 /* We can't redeem this window's scroll bar if it doesn't have one. */
5372 if (NILP (window->vertical_scroll_bar))
5373 abort ();
5375 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5377 /* Unlink it from the condemned list. */
5378 f = XFRAME (WINDOW_FRAME (window));
5379 if (NILP (bar->prev))
5381 /* If the prev pointer is nil, it must be the first in one of
5382 the lists. */
5383 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5384 /* It's not condemned. Everything's fine. */
5385 return;
5386 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5387 window->vertical_scroll_bar))
5388 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5389 else
5390 /* If its prev pointer is nil, it must be at the front of
5391 one or the other! */
5392 abort ();
5394 else
5395 XSCROLL_BAR (bar->prev)->next = bar->next;
5397 if (! NILP (bar->next))
5398 XSCROLL_BAR (bar->next)->prev = bar->prev;
5400 bar->next = FRAME_SCROLL_BARS (f);
5401 bar->prev = Qnil;
5402 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5403 if (! NILP (bar->next))
5404 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5407 /* Remove all scroll bars on FRAME that haven't been saved since the
5408 last call to `*condemn_scroll_bars_hook'. */
5410 static void
5411 XTjudge_scroll_bars (f)
5412 FRAME_PTR f;
5414 Lisp_Object bar, next;
5416 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5418 /* Clear out the condemned list now so we won't try to process any
5419 more events on the hapless scroll bars. */
5420 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5422 for (; ! NILP (bar); bar = next)
5424 struct scroll_bar *b = XSCROLL_BAR (bar);
5426 x_scroll_bar_remove (b);
5428 next = b->next;
5429 b->next = b->prev = Qnil;
5432 /* Now there should be no references to the condemned scroll bars,
5433 and they should get garbage-collected. */
5437 #ifndef USE_TOOLKIT_SCROLL_BARS
5438 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5439 is a no-op when using toolkit scroll bars.
5441 This may be called from a signal handler, so we have to ignore GC
5442 mark bits. */
5444 static void
5445 x_scroll_bar_expose (bar, event)
5446 struct scroll_bar *bar;
5447 XEvent *event;
5449 Window w = bar->x_window;
5450 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5451 GC gc = f->output_data.x->normal_gc;
5452 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5454 BLOCK_INPUT;
5456 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5458 /* Switch to scroll bar foreground color. */
5459 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5460 XSetForeground (FRAME_X_DISPLAY (f), gc,
5461 f->output_data.x->scroll_bar_foreground_pixel);
5463 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5464 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5466 /* x, y, width, height */
5467 0, 0,
5468 bar->width - 1 - width_trim - width_trim,
5469 bar->height - 1);
5471 /* Restore the foreground color of the GC if we changed it above. */
5472 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5473 XSetForeground (FRAME_X_DISPLAY (f), gc,
5474 FRAME_FOREGROUND_PIXEL (f));
5476 UNBLOCK_INPUT;
5479 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5481 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5482 is set to something other than NO_EVENT, it is enqueued.
5484 This may be called from a signal handler, so we have to ignore GC
5485 mark bits. */
5488 static void
5489 x_scroll_bar_handle_click (bar, event, emacs_event)
5490 struct scroll_bar *bar;
5491 XEvent *event;
5492 struct input_event *emacs_event;
5494 if (! WINDOWP (bar->window))
5495 abort ();
5497 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5498 emacs_event->code = event->xbutton.button - Button1;
5499 emacs_event->modifiers
5500 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5501 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5502 event->xbutton.state)
5503 | (event->type == ButtonRelease
5504 ? up_modifier
5505 : down_modifier));
5506 emacs_event->frame_or_window = bar->window;
5507 emacs_event->arg = Qnil;
5508 emacs_event->timestamp = event->xbutton.time;
5510 int top_range
5511 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5512 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5514 if (y < 0) y = 0;
5515 if (y > top_range) y = top_range;
5517 if (y < bar->start)
5518 emacs_event->part = scroll_bar_above_handle;
5519 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5520 emacs_event->part = scroll_bar_handle;
5521 else
5522 emacs_event->part = scroll_bar_below_handle;
5524 #ifndef USE_TOOLKIT_SCROLL_BARS
5525 /* If the user has released the handle, set it to its final position. */
5526 if (event->type == ButtonRelease
5527 && ! NILP (bar->dragging))
5529 int new_start = y - XINT (bar->dragging);
5530 int new_end = new_start + bar->end - bar->start;
5532 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5533 bar->dragging = Qnil;
5535 #endif
5537 XSETINT (emacs_event->x, y);
5538 XSETINT (emacs_event->y, top_range);
5542 #ifndef USE_TOOLKIT_SCROLL_BARS
5544 /* Handle some mouse motion while someone is dragging the scroll bar.
5546 This may be called from a signal handler, so we have to ignore GC
5547 mark bits. */
5549 static void
5550 x_scroll_bar_note_movement (bar, event)
5551 struct scroll_bar *bar;
5552 XEvent *event;
5554 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5556 last_mouse_movement_time = event->xmotion.time;
5558 f->mouse_moved = 1;
5559 XSETVECTOR (last_mouse_scroll_bar, bar);
5561 /* If we're dragging the bar, display it. */
5562 if (! NILP (bar->dragging))
5564 /* Where should the handle be now? */
5565 int new_start = event->xmotion.y - XINT (bar->dragging);
5567 if (new_start != bar->start)
5569 int new_end = new_start + bar->end - bar->start;
5571 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5576 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5578 /* Return information to the user about the current position of the mouse
5579 on the scroll bar. */
5581 static void
5582 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5583 FRAME_PTR *fp;
5584 Lisp_Object *bar_window;
5585 enum scroll_bar_part *part;
5586 Lisp_Object *x, *y;
5587 unsigned long *time;
5589 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5590 Window w = bar->x_window;
5591 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5592 int win_x, win_y;
5593 Window dummy_window;
5594 int dummy_coord;
5595 unsigned int dummy_mask;
5597 BLOCK_INPUT;
5599 /* Get the mouse's position relative to the scroll bar window, and
5600 report that. */
5601 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5603 /* Root, child, root x and root y. */
5604 &dummy_window, &dummy_window,
5605 &dummy_coord, &dummy_coord,
5607 /* Position relative to scroll bar. */
5608 &win_x, &win_y,
5610 /* Mouse buttons and modifier keys. */
5611 &dummy_mask))
5613 else
5615 int top_range
5616 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5618 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5620 if (! NILP (bar->dragging))
5621 win_y -= XINT (bar->dragging);
5623 if (win_y < 0)
5624 win_y = 0;
5625 if (win_y > top_range)
5626 win_y = top_range;
5628 *fp = f;
5629 *bar_window = bar->window;
5631 if (! NILP (bar->dragging))
5632 *part = scroll_bar_handle;
5633 else if (win_y < bar->start)
5634 *part = scroll_bar_above_handle;
5635 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5636 *part = scroll_bar_handle;
5637 else
5638 *part = scroll_bar_below_handle;
5640 XSETINT (*x, win_y);
5641 XSETINT (*y, top_range);
5643 f->mouse_moved = 0;
5644 last_mouse_scroll_bar = Qnil;
5647 *time = last_mouse_movement_time;
5649 UNBLOCK_INPUT;
5653 /* The screen has been cleared so we may have changed foreground or
5654 background colors, and the scroll bars may need to be redrawn.
5655 Clear out the scroll bars, and ask for expose events, so we can
5656 redraw them. */
5658 void
5659 x_scroll_bar_clear (f)
5660 FRAME_PTR f;
5662 #ifndef USE_TOOLKIT_SCROLL_BARS
5663 Lisp_Object bar;
5665 /* We can have scroll bars even if this is 0,
5666 if we just turned off scroll bar mode.
5667 But in that case we should not clear them. */
5668 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5669 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5670 bar = XSCROLL_BAR (bar)->next)
5671 XClearArea (FRAME_X_DISPLAY (f),
5672 XSCROLL_BAR (bar)->x_window,
5673 0, 0, 0, 0, True);
5674 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5678 /* The main X event-reading loop - XTread_socket. */
5680 /* This holds the state XLookupString needs to implement dead keys
5681 and other tricks known as "compose processing". _X Window System_
5682 says that a portable program can't use this, but Stephen Gildea assures
5683 me that letting the compiler initialize it to zeros will work okay.
5685 This must be defined outside of XTread_socket, for the same reasons
5686 given for enter_timestamp, above. */
5688 static XComposeStatus compose_status;
5690 /* Record the last 100 characters stored
5691 to help debug the loss-of-chars-during-GC problem. */
5693 static int temp_index;
5694 static short temp_buffer[100];
5696 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5697 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5698 temp_index = 0; \
5699 temp_buffer[temp_index++] = (keysym)
5701 /* Set this to nonzero to fake an "X I/O error"
5702 on a particular display. */
5704 struct x_display_info *XTread_socket_fake_io_error;
5706 /* When we find no input here, we occasionally do a no-op command
5707 to verify that the X server is still running and we can still talk with it.
5708 We try all the open displays, one by one.
5709 This variable is used for cycling thru the displays. */
5711 static struct x_display_info *next_noop_dpyinfo;
5713 #define SET_SAVED_MENU_EVENT(size) \
5714 do \
5716 if (f->output_data.x->saved_menu_event == 0) \
5717 f->output_data.x->saved_menu_event \
5718 = (XEvent *) xmalloc (sizeof (XEvent)); \
5719 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5720 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5721 XSETFRAME (inev.ie.frame_or_window, f); \
5723 while (0)
5725 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5726 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5729 enum
5731 X_EVENT_NORMAL,
5732 X_EVENT_GOTO_OUT,
5733 X_EVENT_DROP
5736 /* Filter events for the current X input method.
5737 DPYINFO is the display this event is for.
5738 EVENT is the X event to filter.
5740 Returns non-zero if the event was filtered, caller shall not process
5741 this event further.
5742 Returns zero if event is wasn't filtered. */
5744 #ifdef HAVE_X_I18N
5745 static int
5746 x_filter_event (dpyinfo, event)
5747 struct x_display_info *dpyinfo;
5748 XEvent *event;
5750 /* XFilterEvent returns non-zero if the input method has
5751 consumed the event. We pass the frame's X window to
5752 XFilterEvent because that's the one for which the IC
5753 was created. */
5755 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5756 event->xclient.window);
5758 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5760 #endif
5762 #ifdef USE_GTK
5763 static int current_count;
5764 static int current_finish;
5765 static struct input_event *current_hold_quit;
5767 /* This is the filter function invoked by the GTK event loop.
5768 It is invoked before the XEvent is translated to a GdkEvent,
5769 so we have a chance to act on the event before GTK. */
5770 static GdkFilterReturn
5771 event_handler_gdk (gxev, ev, data)
5772 GdkXEvent *gxev;
5773 GdkEvent *ev;
5774 gpointer data;
5776 XEvent *xev = (XEvent *) gxev;
5778 if (current_count >= 0)
5780 struct x_display_info *dpyinfo;
5782 dpyinfo = x_display_info_for_display (xev->xany.display);
5784 #ifdef HAVE_X_I18N
5785 /* Filter events for the current X input method.
5786 GTK calls XFilterEvent but not for key press and release,
5787 so we do it here. */
5788 if (xev->type == KeyPress || xev->type == KeyRelease)
5789 if (dpyinfo && x_filter_event (dpyinfo, xev))
5790 return GDK_FILTER_REMOVE;
5791 #endif
5793 if (! dpyinfo)
5794 current_finish = X_EVENT_NORMAL;
5795 else
5797 current_count +=
5798 handle_one_xevent (dpyinfo, xev, &current_finish,
5799 current_hold_quit);
5802 else
5803 current_finish = x_dispatch_event (xev, xev->xany.display);
5805 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5806 return GDK_FILTER_REMOVE;
5808 return GDK_FILTER_CONTINUE;
5810 #endif /* USE_GTK */
5813 /* Handles the XEvent EVENT on display DPYINFO.
5815 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5816 *FINISH is zero if caller should continue reading events.
5817 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5819 We return the number of characters stored into the buffer. */
5821 static int
5822 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5823 struct x_display_info *dpyinfo;
5824 XEvent *eventp;
5825 int *finish;
5826 struct input_event *hold_quit;
5828 union {
5829 struct input_event ie;
5830 struct selection_input_event sie;
5831 } inev;
5832 int count = 0;
5833 int do_help = 0;
5834 int nbytes = 0;
5835 struct frame *f = NULL;
5836 struct coding_system coding;
5837 XEvent event = *eventp;
5839 *finish = X_EVENT_NORMAL;
5841 EVENT_INIT (inev.ie);
5842 inev.ie.kind = NO_EVENT;
5843 inev.ie.arg = Qnil;
5845 if (pending_event_wait.eventtype == event.type)
5846 pending_event_wait.eventtype = 0; /* Indicates we got it. */
5848 switch (event.type)
5850 case ClientMessage:
5852 if (event.xclient.message_type
5853 == dpyinfo->Xatom_wm_protocols
5854 && event.xclient.format == 32)
5856 if (event.xclient.data.l[0]
5857 == dpyinfo->Xatom_wm_take_focus)
5859 /* Use x_any_window_to_frame because this
5860 could be the shell widget window
5861 if the frame has no title bar. */
5862 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5863 #ifdef HAVE_X_I18N
5864 /* Not quite sure this is needed -pd */
5865 if (f && FRAME_XIC (f))
5866 XSetICFocus (FRAME_XIC (f));
5867 #endif
5868 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5869 instructs the WM to set the input focus automatically for
5870 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5871 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5872 it has set the focus. So, XSetInputFocus below is not
5873 needed.
5875 The call to XSetInputFocus below has also caused trouble. In
5876 cases where the XSetInputFocus done by the WM and the one
5877 below are temporally close (on a fast machine), the call
5878 below can generate additional FocusIn events which confuse
5879 Emacs. */
5881 /* Since we set WM_TAKE_FOCUS, we must call
5882 XSetInputFocus explicitly. But not if f is null,
5883 since that might be an event for a deleted frame. */
5884 if (f)
5886 Display *d = event.xclient.display;
5887 /* Catch and ignore errors, in case window has been
5888 iconified by a window manager such as GWM. */
5889 x_catch_errors (d);
5890 XSetInputFocus (d, event.xclient.window,
5891 /* The ICCCM says this is
5892 the only valid choice. */
5893 RevertToParent,
5894 event.xclient.data.l[1]);
5895 /* This is needed to detect the error
5896 if there is an error. */
5897 XSync (d, False);
5898 x_uncatch_errors ();
5900 /* Not certain about handling scroll bars here */
5901 #endif /* 0 */
5902 goto done;
5905 if (event.xclient.data.l[0]
5906 == dpyinfo->Xatom_wm_save_yourself)
5908 /* Save state modify the WM_COMMAND property to
5909 something which can reinstate us. This notifies
5910 the session manager, who's looking for such a
5911 PropertyNotify. Can restart processing when
5912 a keyboard or mouse event arrives. */
5913 /* If we have a session manager, don't set this.
5914 KDE will then start two Emacsen, one for the
5915 session manager and one for this. */
5916 #ifdef HAVE_X_SM
5917 if (! x_session_have_connection ())
5918 #endif
5920 f = x_top_window_to_frame (dpyinfo,
5921 event.xclient.window);
5922 /* This is just so we only give real data once
5923 for a single Emacs process. */
5924 if (f == SELECTED_FRAME ())
5925 XSetCommand (FRAME_X_DISPLAY (f),
5926 event.xclient.window,
5927 initial_argv, initial_argc);
5928 else if (f)
5929 XSetCommand (FRAME_X_DISPLAY (f),
5930 event.xclient.window,
5931 0, 0);
5933 goto done;
5936 if (event.xclient.data.l[0]
5937 == dpyinfo->Xatom_wm_delete_window)
5939 f = x_any_window_to_frame (dpyinfo,
5940 event.xclient.window);
5941 if (!f)
5942 goto OTHER; /* May be a dialog that is to be removed */
5944 inev.ie.kind = DELETE_WINDOW_EVENT;
5945 XSETFRAME (inev.ie.frame_or_window, f);
5946 goto done;
5949 goto done;
5952 if (event.xclient.message_type
5953 == dpyinfo->Xatom_wm_configure_denied)
5955 goto done;
5958 if (event.xclient.message_type
5959 == dpyinfo->Xatom_wm_window_moved)
5961 int new_x, new_y;
5962 f = x_window_to_frame (dpyinfo, event.xclient.window);
5964 new_x = event.xclient.data.s[0];
5965 new_y = event.xclient.data.s[1];
5967 if (f)
5969 f->left_pos = new_x;
5970 f->top_pos = new_y;
5972 goto done;
5975 #ifdef HACK_EDITRES
5976 if (event.xclient.message_type
5977 == dpyinfo->Xatom_editres)
5979 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5980 if (f)
5981 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5982 &event, NULL);
5983 goto done;
5985 #endif /* HACK_EDITRES */
5987 if ((event.xclient.message_type
5988 == dpyinfo->Xatom_DONE)
5989 || (event.xclient.message_type
5990 == dpyinfo->Xatom_PAGE))
5992 /* Ghostview job completed. Kill it. We could
5993 reply with "Next" if we received "Page", but we
5994 currently never do because we are interested in
5995 images, only, which should have 1 page. */
5996 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5997 f = x_window_to_frame (dpyinfo, event.xclient.window);
5998 if (!f)
5999 goto OTHER;
6000 x_kill_gs_process (pixmap, f);
6001 expose_frame (f, 0, 0, 0, 0);
6002 goto done;
6005 #ifdef USE_TOOLKIT_SCROLL_BARS
6006 /* Scroll bar callbacks send a ClientMessage from which
6007 we construct an input_event. */
6008 if (event.xclient.message_type
6009 == dpyinfo->Xatom_Scrollbar)
6011 x_scroll_bar_to_input_event (&event, &inev.ie);
6012 *finish = X_EVENT_GOTO_OUT;
6013 goto done;
6015 #endif /* USE_TOOLKIT_SCROLL_BARS */
6017 /* XEmbed messages from the embedder (if any). */
6018 if (event.xclient.message_type
6019 == dpyinfo->Xatom_XEMBED)
6021 enum xembed_message msg = event.xclient.data.l[1];
6022 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6023 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6025 *finish = X_EVENT_GOTO_OUT;
6026 goto done;
6029 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6030 if (!f)
6031 goto OTHER;
6032 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
6033 *finish = X_EVENT_DROP;
6035 break;
6037 case SelectionNotify:
6038 last_user_time = event.xselection.time;
6039 #ifdef USE_X_TOOLKIT
6040 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6041 goto OTHER;
6042 #endif /* not USE_X_TOOLKIT */
6043 x_handle_selection_notify (&event.xselection);
6044 break;
6046 case SelectionClear: /* Someone has grabbed ownership. */
6047 last_user_time = event.xselectionclear.time;
6048 #ifdef USE_X_TOOLKIT
6049 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6050 goto OTHER;
6051 #endif /* USE_X_TOOLKIT */
6053 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
6055 inev.ie.kind = SELECTION_CLEAR_EVENT;
6056 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6057 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6058 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6059 inev.ie.frame_or_window = Qnil;
6061 break;
6063 case SelectionRequest: /* Someone wants our selection. */
6064 last_user_time = event.xselectionrequest.time;
6065 #ifdef USE_X_TOOLKIT
6066 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6067 goto OTHER;
6068 #endif /* USE_X_TOOLKIT */
6070 XSelectionRequestEvent *eventp
6071 = (XSelectionRequestEvent *) &event;
6073 inev.ie.kind = SELECTION_REQUEST_EVENT;
6074 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6075 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6076 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6077 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6078 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6079 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6080 inev.ie.frame_or_window = Qnil;
6082 break;
6084 case PropertyNotify:
6085 last_user_time = event.xproperty.time;
6086 f = x_top_window_to_frame (dpyinfo, event.xproperty.window);
6087 if (f && event.xproperty.atom == dpyinfo->Xatom_net_wm_state)
6088 x_handle_net_wm_state (f, &event.xproperty);
6090 x_handle_property_notify (&event.xproperty);
6091 goto OTHER;
6093 case ReparentNotify:
6094 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6095 if (f)
6097 int x, y;
6098 f->output_data.x->parent_desc = event.xreparent.parent;
6099 x_real_positions (f, &x, &y);
6100 f->left_pos = x;
6101 f->top_pos = y;
6103 /* Perhaps reparented due to a WM restart. Reset this. */
6104 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6105 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6107 goto OTHER;
6109 case Expose:
6110 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6111 if (f)
6113 #ifdef USE_GTK
6114 /* This seems to be needed for GTK 2.6. */
6115 x_clear_area (event.xexpose.display,
6116 event.xexpose.window,
6117 event.xexpose.x, event.xexpose.y,
6118 event.xexpose.width, event.xexpose.height,
6119 FALSE);
6120 #endif
6121 if (f->async_visible == 0)
6123 f->async_visible = 1;
6124 f->async_iconified = 0;
6125 f->output_data.x->has_been_visible = 1;
6126 SET_FRAME_GARBAGED (f);
6128 else
6129 expose_frame (f,
6130 event.xexpose.x, event.xexpose.y,
6131 event.xexpose.width, event.xexpose.height);
6133 else
6135 #ifndef USE_TOOLKIT_SCROLL_BARS
6136 struct scroll_bar *bar;
6137 #endif
6138 #if defined USE_LUCID
6139 /* Submenus of the Lucid menu bar aren't widgets
6140 themselves, so there's no way to dispatch events
6141 to them. Recognize this case separately. */
6143 Widget widget
6144 = x_window_to_menu_bar (event.xexpose.window);
6145 if (widget)
6146 xlwmenu_redisplay (widget);
6148 #endif /* USE_LUCID */
6150 #ifdef USE_TOOLKIT_SCROLL_BARS
6151 /* Dispatch event to the widget. */
6152 goto OTHER;
6153 #else /* not USE_TOOLKIT_SCROLL_BARS */
6154 bar = x_window_to_scroll_bar (event.xexpose.display,
6155 event.xexpose.window);
6157 if (bar)
6158 x_scroll_bar_expose (bar, &event);
6159 #ifdef USE_X_TOOLKIT
6160 else
6161 goto OTHER;
6162 #endif /* USE_X_TOOLKIT */
6163 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6165 break;
6167 case GraphicsExpose: /* This occurs when an XCopyArea's
6168 source area was obscured or not
6169 available. */
6170 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6171 if (f)
6173 expose_frame (f,
6174 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6175 event.xgraphicsexpose.width,
6176 event.xgraphicsexpose.height);
6178 #ifdef USE_X_TOOLKIT
6179 else
6180 goto OTHER;
6181 #endif /* USE_X_TOOLKIT */
6182 break;
6184 case NoExpose: /* This occurs when an XCopyArea's
6185 source area was completely
6186 available. */
6187 break;
6189 case UnmapNotify:
6190 /* Redo the mouse-highlight after the tooltip has gone. */
6191 if (event.xmap.window == tip_window)
6193 tip_window = 0;
6194 redo_mouse_highlight ();
6197 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6198 if (f) /* F may no longer exist if
6199 the frame was deleted. */
6201 /* While a frame is unmapped, display generation is
6202 disabled; you don't want to spend time updating a
6203 display that won't ever be seen. */
6204 f->async_visible = 0;
6205 /* We can't distinguish, from the event, whether the window
6206 has become iconified or invisible. So assume, if it
6207 was previously visible, than now it is iconified.
6208 But x_make_frame_invisible clears both
6209 the visible flag and the iconified flag;
6210 and that way, we know the window is not iconified now. */
6211 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6213 f->async_iconified = 1;
6215 inev.ie.kind = ICONIFY_EVENT;
6216 XSETFRAME (inev.ie.frame_or_window, f);
6219 goto OTHER;
6221 case MapNotify:
6222 if (event.xmap.window == tip_window)
6223 /* The tooltip has been drawn already. Avoid
6224 the SET_FRAME_GARBAGED below. */
6225 goto OTHER;
6227 /* We use x_top_window_to_frame because map events can
6228 come for sub-windows and they don't mean that the
6229 frame is visible. */
6230 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6231 if (f)
6233 /* wait_reading_process_output will notice this and update
6234 the frame's display structures.
6235 If we where iconified, we should not set garbaged,
6236 because that stops redrawing on Expose events. This looks
6237 bad if we are called from a recursive event loop
6238 (x_dispatch_event), for example when a dialog is up. */
6239 if (! f->async_iconified)
6240 SET_FRAME_GARBAGED (f);
6242 /* Check if fullscreen was specified before we where mapped the
6243 first time, i.e. from the command line. */
6244 if (!f->output_data.x->has_been_visible)
6245 x_check_fullscreen (f);
6247 f->async_visible = 1;
6248 f->async_iconified = 0;
6249 f->output_data.x->has_been_visible = 1;
6251 if (f->iconified)
6253 inev.ie.kind = DEICONIFY_EVENT;
6254 XSETFRAME (inev.ie.frame_or_window, f);
6256 else if (! NILP (Vframe_list)
6257 && ! NILP (XCDR (Vframe_list)))
6258 /* Force a redisplay sooner or later
6259 to update the frame titles
6260 in case this is the second frame. */
6261 record_asynch_buffer_change ();
6263 #ifdef USE_GTK
6264 xg_frame_resized (f, -1, -1);
6265 #endif
6267 goto OTHER;
6269 case KeyPress:
6271 last_user_time = event.xkey.time;
6272 ignore_next_mouse_click_timeout = 0;
6274 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6275 /* Dispatch KeyPress events when in menu. */
6276 if (popup_activated ())
6277 goto OTHER;
6278 #endif
6280 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6282 /* If mouse-highlight is an integer, input clears out
6283 mouse highlighting. */
6284 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6285 && (f == 0
6286 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6288 clear_mouse_face (dpyinfo);
6289 dpyinfo->mouse_face_hidden = 1;
6292 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6293 if (f == 0)
6295 /* Scroll bars consume key events, but we want
6296 the keys to go to the scroll bar's frame. */
6297 Widget widget = XtWindowToWidget (dpyinfo->display,
6298 event.xkey.window);
6299 if (widget && XmIsScrollBar (widget))
6301 widget = XtParent (widget);
6302 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6305 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6307 if (f != 0)
6309 KeySym keysym, orig_keysym;
6310 /* al%imercury@uunet.uu.net says that making this 81
6311 instead of 80 fixed a bug whereby meta chars made
6312 his Emacs hang.
6314 It seems that some version of XmbLookupString has
6315 a bug of not returning XBufferOverflow in
6316 status_return even if the input is too long to
6317 fit in 81 bytes. So, we must prepare sufficient
6318 bytes for copy_buffer. 513 bytes (256 chars for
6319 two-byte character set) seems to be a fairly good
6320 approximation. -- 2000.8.10 handa@etl.go.jp */
6321 unsigned char copy_buffer[513];
6322 unsigned char *copy_bufptr = copy_buffer;
6323 int copy_bufsiz = sizeof (copy_buffer);
6324 int modifiers;
6325 Lisp_Object coding_system = Qlatin_1;
6326 Lisp_Object c;
6328 #ifdef USE_GTK
6329 /* Don't pass keys to GTK. A Tab will shift focus to the
6330 tool bar in GTK 2.4. Keys will still go to menus and
6331 dialogs because in that case popup_activated is TRUE
6332 (see above). */
6333 *finish = X_EVENT_DROP;
6334 #endif
6336 event.xkey.state
6337 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6338 extra_keyboard_modifiers);
6339 modifiers = event.xkey.state;
6341 /* This will have to go some day... */
6343 /* make_lispy_event turns chars into control chars.
6344 Don't do it here because XLookupString is too eager. */
6345 event.xkey.state &= ~ControlMask;
6346 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6347 | dpyinfo->super_mod_mask
6348 | dpyinfo->hyper_mod_mask
6349 | dpyinfo->alt_mod_mask);
6351 /* In case Meta is ComposeCharacter,
6352 clear its status. According to Markus Ehrnsperger
6353 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6354 this enables ComposeCharacter to work whether or
6355 not it is combined with Meta. */
6356 if (modifiers & dpyinfo->meta_mod_mask)
6357 bzero (&compose_status, sizeof (compose_status));
6359 #ifdef HAVE_X_I18N
6360 if (FRAME_XIC (f))
6362 Status status_return;
6364 coding_system = Vlocale_coding_system;
6365 nbytes = XmbLookupString (FRAME_XIC (f),
6366 &event.xkey, copy_bufptr,
6367 copy_bufsiz, &keysym,
6368 &status_return);
6369 if (status_return == XBufferOverflow)
6371 copy_bufsiz = nbytes + 1;
6372 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6373 nbytes = XmbLookupString (FRAME_XIC (f),
6374 &event.xkey, copy_bufptr,
6375 copy_bufsiz, &keysym,
6376 &status_return);
6378 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6379 if (status_return == XLookupNone)
6380 break;
6381 else if (status_return == XLookupChars)
6383 keysym = NoSymbol;
6384 modifiers = 0;
6386 else if (status_return != XLookupKeySym
6387 && status_return != XLookupBoth)
6388 abort ();
6390 else
6391 nbytes = XLookupString (&event.xkey, copy_bufptr,
6392 copy_bufsiz, &keysym,
6393 &compose_status);
6394 #else
6395 nbytes = XLookupString (&event.xkey, copy_bufptr,
6396 copy_bufsiz, &keysym,
6397 &compose_status);
6398 #endif
6400 /* If not using XIM/XIC, and a compose sequence is in progress,
6401 we break here. Otherwise, chars_matched is always 0. */
6402 if (compose_status.chars_matched > 0 && nbytes == 0)
6403 break;
6405 bzero (&compose_status, sizeof (compose_status));
6406 orig_keysym = keysym;
6408 /* Common for all keysym input events. */
6409 XSETFRAME (inev.ie.frame_or_window, f);
6410 inev.ie.modifiers
6411 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6412 inev.ie.timestamp = event.xkey.time;
6414 /* First deal with keysyms which have defined
6415 translations to characters. */
6416 if (keysym >= 32 && keysym < 128)
6417 /* Avoid explicitly decoding each ASCII character. */
6419 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6420 inev.ie.code = keysym;
6421 goto done_keysym;
6424 /* Keysyms directly mapped to Unicode characters. */
6425 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6427 if (keysym < 0x01000080)
6428 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6429 else
6430 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6431 inev.ie.code = keysym & 0xFFFFFF;
6432 goto done_keysym;
6435 /* Now non-ASCII. */
6436 if (HASH_TABLE_P (Vx_keysym_table)
6437 && (NATNUMP (c = Fgethash (make_number (keysym),
6438 Vx_keysym_table,
6439 Qnil))))
6441 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6442 ? ASCII_KEYSTROKE_EVENT
6443 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6444 inev.ie.code = XFASTINT (c);
6445 goto done_keysym;
6448 /* Random non-modifier sorts of keysyms. */
6449 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6450 || keysym == XK_Delete
6451 #ifdef XK_ISO_Left_Tab
6452 || (keysym >= XK_ISO_Left_Tab
6453 && keysym <= XK_ISO_Enter)
6454 #endif
6455 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6456 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6457 #ifdef HPUX
6458 /* This recognizes the "extended function
6459 keys". It seems there's no cleaner way.
6460 Test IsModifierKey to avoid handling
6461 mode_switch incorrectly. */
6462 || ((unsigned) (keysym) >= XK_Select
6463 && (unsigned)(keysym) < XK_KP_Space)
6464 #endif
6465 #ifdef XK_dead_circumflex
6466 || orig_keysym == XK_dead_circumflex
6467 #endif
6468 #ifdef XK_dead_grave
6469 || orig_keysym == XK_dead_grave
6470 #endif
6471 #ifdef XK_dead_tilde
6472 || orig_keysym == XK_dead_tilde
6473 #endif
6474 #ifdef XK_dead_diaeresis
6475 || orig_keysym == XK_dead_diaeresis
6476 #endif
6477 #ifdef XK_dead_macron
6478 || orig_keysym == XK_dead_macron
6479 #endif
6480 #ifdef XK_dead_degree
6481 || orig_keysym == XK_dead_degree
6482 #endif
6483 #ifdef XK_dead_acute
6484 || orig_keysym == XK_dead_acute
6485 #endif
6486 #ifdef XK_dead_cedilla
6487 || orig_keysym == XK_dead_cedilla
6488 #endif
6489 #ifdef XK_dead_breve
6490 || orig_keysym == XK_dead_breve
6491 #endif
6492 #ifdef XK_dead_ogonek
6493 || orig_keysym == XK_dead_ogonek
6494 #endif
6495 #ifdef XK_dead_caron
6496 || orig_keysym == XK_dead_caron
6497 #endif
6498 #ifdef XK_dead_doubleacute
6499 || orig_keysym == XK_dead_doubleacute
6500 #endif
6501 #ifdef XK_dead_abovedot
6502 || orig_keysym == XK_dead_abovedot
6503 #endif
6504 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6505 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6506 /* Any "vendor-specific" key is ok. */
6507 || (orig_keysym & (1 << 28))
6508 || (keysym != NoSymbol && nbytes == 0))
6509 && ! (IsModifierKey (orig_keysym)
6510 /* The symbols from XK_ISO_Lock
6511 to XK_ISO_Last_Group_Lock
6512 don't have real modifiers but
6513 should be treated similarly to
6514 Mode_switch by Emacs. */
6515 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6516 || ((unsigned)(orig_keysym)
6517 >= XK_ISO_Lock
6518 && (unsigned)(orig_keysym)
6519 <= XK_ISO_Last_Group_Lock)
6520 #endif
6523 STORE_KEYSYM_FOR_DEBUG (keysym);
6524 /* make_lispy_event will convert this to a symbolic
6525 key. */
6526 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6527 inev.ie.code = keysym;
6528 goto done_keysym;
6531 { /* Raw bytes, not keysym. */
6532 register int i;
6533 register int c;
6534 int nchars, len;
6536 for (i = 0, nchars = 0; i < nbytes; i++)
6538 if (ASCII_BYTE_P (copy_bufptr[i]))
6539 nchars++;
6540 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6543 if (nchars < nbytes)
6545 /* Decode the input data. */
6546 int require;
6547 unsigned char *p;
6549 /* The input should be decoded with `coding_system'
6550 which depends on which X*LookupString function
6551 we used just above and the locale. */
6552 setup_coding_system (coding_system, &coding);
6553 coding.src_multibyte = 0;
6554 coding.dst_multibyte = 1;
6555 /* The input is converted to events, thus we can't
6556 handle composition. Anyway, there's no XIM that
6557 gives us composition information. */
6558 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6560 require = MAX_MULTIBYTE_LENGTH * nbytes;
6561 coding.destination = alloca (require);
6562 coding.dst_bytes = require;
6563 coding.mode |= CODING_MODE_LAST_BLOCK;
6564 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6565 nbytes = coding.produced;
6566 nchars = coding.produced_char;
6567 copy_bufptr = coding.destination;
6570 /* Convert the input data to a sequence of
6571 character events. */
6572 for (i = 0; i < nbytes; i += len)
6574 if (nchars == nbytes)
6575 c = copy_bufptr[i], len = 1;
6576 else
6577 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6578 nbytes - i, len);
6579 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6580 ? ASCII_KEYSTROKE_EVENT
6581 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6582 inev.ie.code = c;
6583 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6586 count += nchars;
6588 inev.ie.kind = NO_EVENT; /* Already stored above. */
6590 if (keysym == NoSymbol)
6591 break;
6594 done_keysym:
6595 #ifdef HAVE_X_I18N
6596 /* Don't dispatch this event since XtDispatchEvent calls
6597 XFilterEvent, and two calls in a row may freeze the
6598 client. */
6599 break;
6600 #else
6601 goto OTHER;
6602 #endif
6604 case KeyRelease:
6605 last_user_time = event.xkey.time;
6606 #ifdef HAVE_X_I18N
6607 /* Don't dispatch this event since XtDispatchEvent calls
6608 XFilterEvent, and two calls in a row may freeze the
6609 client. */
6610 break;
6611 #else
6612 goto OTHER;
6613 #endif
6615 case EnterNotify:
6616 last_user_time = event.xcrossing.time;
6617 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6619 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6621 if (f && x_mouse_click_focus_ignore_position)
6622 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6624 /* EnterNotify counts as mouse movement,
6625 so update things that depend on mouse position. */
6626 if (f && !f->output_data.x->hourglass_p)
6627 note_mouse_movement (f, &event.xmotion);
6628 #ifdef USE_GTK
6629 /* We may get an EnterNotify on the buttons in the toolbar. In that
6630 case we moved out of any highlighted area and need to note this. */
6631 if (!f && last_mouse_glyph_frame)
6632 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6633 #endif
6634 goto OTHER;
6636 case FocusIn:
6637 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6638 goto OTHER;
6640 case LeaveNotify:
6641 last_user_time = event.xcrossing.time;
6642 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6644 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6645 if (f)
6647 if (f == dpyinfo->mouse_face_mouse_frame)
6649 /* If we move outside the frame, then we're
6650 certainly no longer on any text in the frame. */
6651 clear_mouse_face (dpyinfo);
6652 dpyinfo->mouse_face_mouse_frame = 0;
6655 /* Generate a nil HELP_EVENT to cancel a help-echo.
6656 Do it only if there's something to cancel.
6657 Otherwise, the startup message is cleared when
6658 the mouse leaves the frame. */
6659 if (any_help_event_p)
6660 do_help = -1;
6662 #ifdef USE_GTK
6663 /* See comment in EnterNotify above */
6664 else if (last_mouse_glyph_frame)
6665 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6666 #endif
6667 goto OTHER;
6669 case FocusOut:
6670 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6671 goto OTHER;
6673 case MotionNotify:
6675 last_user_time = event.xmotion.time;
6676 previous_help_echo_string = help_echo_string;
6677 help_echo_string = Qnil;
6679 if (dpyinfo->grabbed && last_mouse_frame
6680 && FRAME_LIVE_P (last_mouse_frame))
6681 f = last_mouse_frame;
6682 else
6683 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6685 if (dpyinfo->mouse_face_hidden)
6687 dpyinfo->mouse_face_hidden = 0;
6688 clear_mouse_face (dpyinfo);
6691 if (f)
6694 /* Generate SELECT_WINDOW_EVENTs when needed.
6695 Don't let popup menus influence things (bug#1261). */
6696 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6698 Lisp_Object window;
6700 window = window_from_coordinates (f,
6701 event.xmotion.x, event.xmotion.y,
6702 0, 0, 0, 0);
6704 /* Window will be selected only when it is not selected now and
6705 last mouse movement event was not in it. Minibuffer window
6706 will be selected only when it is active. */
6707 if (WINDOWP (window)
6708 && !EQ (window, last_window)
6709 && !EQ (window, selected_window)
6710 /* For click-to-focus window managers
6711 create event iff we don't leave the
6712 selected frame. */
6713 && (focus_follows_mouse
6714 || (EQ (XWINDOW (window)->frame,
6715 XWINDOW (selected_window)->frame))))
6717 inev.ie.kind = SELECT_WINDOW_EVENT;
6718 inev.ie.frame_or_window = window;
6721 last_window=window;
6723 if (!note_mouse_movement (f, &event.xmotion))
6724 help_echo_string = previous_help_echo_string;
6726 else
6728 #ifndef USE_TOOLKIT_SCROLL_BARS
6729 struct scroll_bar *bar
6730 = x_window_to_scroll_bar (event.xmotion.display,
6731 event.xmotion.window);
6733 if (bar)
6734 x_scroll_bar_note_movement (bar, &event);
6735 #endif /* USE_TOOLKIT_SCROLL_BARS */
6737 /* If we move outside the frame, then we're
6738 certainly no longer on any text in the frame. */
6739 clear_mouse_face (dpyinfo);
6742 /* If the contents of the global variable help_echo_string
6743 has changed, generate a HELP_EVENT. */
6744 if (!NILP (help_echo_string)
6745 || !NILP (previous_help_echo_string))
6746 do_help = 1;
6747 goto OTHER;
6750 case ConfigureNotify:
6751 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6752 #ifdef USE_GTK
6753 if (!f
6754 && (f = x_any_window_to_frame (dpyinfo, event.xconfigure.window))
6755 && event.xconfigure.window == FRAME_X_WINDOW (f))
6757 xg_frame_resized (f, event.xconfigure.width,
6758 event.xconfigure.height);
6759 f = 0;
6761 #endif
6762 if (f)
6764 #ifndef USE_X_TOOLKIT
6765 #ifndef USE_GTK
6766 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6767 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6769 /* In the toolkit version, change_frame_size
6770 is called by the code that handles resizing
6771 of the EmacsFrame widget. */
6773 /* Even if the number of character rows and columns has
6774 not changed, the font size may have changed, so we need
6775 to check the pixel dimensions as well. */
6776 if (columns != FRAME_COLS (f)
6777 || rows != FRAME_LINES (f)
6778 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6779 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6781 change_frame_size (f, rows, columns, 0, 1, 0);
6782 SET_FRAME_GARBAGED (f);
6783 cancel_mouse_face (f);
6786 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6787 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6788 #endif /* not USE_GTK */
6789 #endif
6791 #ifdef USE_GTK
6792 /* GTK creates windows but doesn't map them.
6793 Only get real positions when mapped. */
6794 if (FRAME_GTK_OUTER_WIDGET (f)
6795 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6796 #endif
6798 x_real_positions (f, &f->left_pos, &f->top_pos);
6801 #ifdef HAVE_X_I18N
6802 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6803 xic_set_statusarea (f);
6804 #endif
6807 goto OTHER;
6809 case ButtonRelease:
6810 case ButtonPress:
6812 /* If we decide we want to generate an event to be seen
6813 by the rest of Emacs, we put it here. */
6814 int tool_bar_p = 0;
6816 bzero (&compose_status, sizeof (compose_status));
6817 last_mouse_glyph_frame = 0;
6818 last_user_time = event.xbutton.time;
6820 if (dpyinfo->grabbed
6821 && last_mouse_frame
6822 && FRAME_LIVE_P (last_mouse_frame))
6823 f = last_mouse_frame;
6824 else
6825 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6827 if (f)
6829 /* Is this in the tool-bar? */
6830 if (WINDOWP (f->tool_bar_window)
6831 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6833 Lisp_Object window;
6834 int x = event.xbutton.x;
6835 int y = event.xbutton.y;
6837 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6838 tool_bar_p = EQ (window, f->tool_bar_window);
6840 if (tool_bar_p && event.xbutton.button < 4)
6842 handle_tool_bar_click (f, x, y,
6843 event.xbutton.type == ButtonPress,
6844 x_x_to_emacs_modifiers (dpyinfo,
6845 event.xbutton.state));
6849 if (!tool_bar_p)
6850 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6851 if (! popup_activated ())
6852 #endif
6854 if (ignore_next_mouse_click_timeout)
6856 if (event.type == ButtonPress
6857 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6859 ignore_next_mouse_click_timeout = 0;
6860 construct_mouse_click (&inev.ie, &event.xbutton, f);
6862 if (event.type == ButtonRelease)
6863 ignore_next_mouse_click_timeout = 0;
6865 else
6866 construct_mouse_click (&inev.ie, &event.xbutton, f);
6868 if (FRAME_X_EMBEDDED_P (f))
6869 xembed_send_message (f, event.xbutton.time,
6870 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6872 else
6874 struct scroll_bar *bar
6875 = x_window_to_scroll_bar (event.xbutton.display,
6876 event.xbutton.window);
6878 #ifdef USE_TOOLKIT_SCROLL_BARS
6879 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6880 scroll bars. */
6881 if (bar && event.xbutton.state & ControlMask)
6883 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6884 *finish = X_EVENT_DROP;
6886 #else /* not USE_TOOLKIT_SCROLL_BARS */
6887 if (bar)
6888 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6889 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6892 if (event.type == ButtonPress)
6894 dpyinfo->grabbed |= (1 << event.xbutton.button);
6895 last_mouse_frame = f;
6897 if (!tool_bar_p)
6898 last_tool_bar_item = -1;
6900 else
6901 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6903 /* Ignore any mouse motion that happened before this event;
6904 any subsequent mouse-movement Emacs events should reflect
6905 only motion after the ButtonPress/Release. */
6906 if (f != 0)
6907 f->mouse_moved = 0;
6909 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6910 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6911 /* For a down-event in the menu bar,
6912 don't pass it to Xt right now.
6913 Instead, save it away
6914 and we will pass it to Xt from kbd_buffer_get_event.
6915 That way, we can run some Lisp code first. */
6916 if (
6917 #ifdef USE_GTK
6918 ! popup_activated ()
6919 /* Gtk+ menus only react to the first three buttons. */
6920 && event.xbutton.button < 3
6922 #endif
6923 f && event.type == ButtonPress
6924 /* Verify the event is really within the menu bar
6925 and not just sent to it due to grabbing. */
6926 && event.xbutton.x >= 0
6927 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6928 && event.xbutton.y >= 0
6929 && event.xbutton.y < f->output_data.x->menubar_height
6930 && event.xbutton.same_screen)
6932 SET_SAVED_BUTTON_EVENT;
6933 XSETFRAME (last_mouse_press_frame, f);
6934 #ifdef USE_GTK
6935 *finish = X_EVENT_DROP;
6936 #endif
6938 else if (event.type == ButtonPress)
6940 last_mouse_press_frame = Qnil;
6941 goto OTHER;
6944 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6945 but I am trying to be cautious. */
6946 else if (event.type == ButtonRelease)
6948 if (!NILP (last_mouse_press_frame))
6950 f = XFRAME (last_mouse_press_frame);
6951 if (f->output_data.x)
6952 SET_SAVED_BUTTON_EVENT;
6954 else
6955 goto OTHER;
6957 #endif /* USE_MOTIF */
6958 else
6959 goto OTHER;
6960 #endif /* USE_X_TOOLKIT || USE_GTK */
6962 break;
6964 case CirculateNotify:
6965 goto OTHER;
6967 case CirculateRequest:
6968 goto OTHER;
6970 case VisibilityNotify:
6971 goto OTHER;
6973 case MappingNotify:
6974 /* Someone has changed the keyboard mapping - update the
6975 local cache. */
6976 switch (event.xmapping.request)
6978 case MappingModifier:
6979 x_find_modifier_meanings (dpyinfo);
6980 /* This is meant to fall through. */
6981 case MappingKeyboard:
6982 XRefreshKeyboardMapping (&event.xmapping);
6984 goto OTHER;
6986 default:
6987 OTHER:
6988 #ifdef USE_X_TOOLKIT
6989 BLOCK_INPUT;
6990 if (*finish != X_EVENT_DROP)
6991 XtDispatchEvent (&event);
6992 UNBLOCK_INPUT;
6993 #endif /* USE_X_TOOLKIT */
6994 break;
6997 done:
6998 if (inev.ie.kind != NO_EVENT)
7000 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7001 count++;
7004 if (do_help
7005 && !(hold_quit && hold_quit->kind != NO_EVENT))
7007 Lisp_Object frame;
7009 if (f)
7010 XSETFRAME (frame, f);
7011 else
7012 frame = Qnil;
7014 if (do_help > 0)
7016 any_help_event_p = 1;
7017 gen_help_event (help_echo_string, frame, help_echo_window,
7018 help_echo_object, help_echo_pos);
7020 else
7022 help_echo_string = Qnil;
7023 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7025 count++;
7028 *eventp = event;
7029 return count;
7033 /* Handles the XEvent EVENT on display DISPLAY.
7034 This is used for event loops outside the normal event handling,
7035 i.e. looping while a popup menu or a dialog is posted.
7037 Returns the value handle_one_xevent sets in the finish argument. */
7039 x_dispatch_event (event, display)
7040 XEvent *event;
7041 Display *display;
7043 struct x_display_info *dpyinfo;
7044 int finish = X_EVENT_NORMAL;
7046 dpyinfo = x_display_info_for_display (display);
7048 if (dpyinfo)
7049 handle_one_xevent (dpyinfo, event, &finish, 0);
7051 return finish;
7055 /* Read events coming from the X server.
7056 This routine is called by the SIGIO handler.
7057 We return as soon as there are no more events to be read.
7059 We return the number of characters stored into the buffer,
7060 thus pretending to be `read' (except the characters we store
7061 in the keyboard buffer can be multibyte, so are not necessarily
7062 C chars).
7064 EXPECTED is nonzero if the caller knows input is available. */
7066 static int
7067 XTread_socket (terminal, expected, hold_quit)
7068 struct terminal *terminal;
7069 int expected;
7070 struct input_event *hold_quit;
7072 int count = 0;
7073 XEvent event;
7074 int event_found = 0;
7076 if (interrupt_input_blocked)
7078 interrupt_input_pending = 1;
7079 #ifdef SYNC_INPUT
7080 pending_signals = 1;
7081 #endif
7082 return -1;
7085 interrupt_input_pending = 0;
7086 #ifdef SYNC_INPUT
7087 pending_signals = pending_atimers;
7088 #endif
7089 BLOCK_INPUT;
7091 /* So people can tell when we have read the available input. */
7092 input_signal_count++;
7094 ++handling_signal;
7096 #ifdef HAVE_X_SM
7097 /* Only check session manager input for the primary display. */
7098 if (terminal->id == 1 && x_session_have_connection ())
7100 struct input_event inev;
7101 BLOCK_INPUT;
7102 /* We don't need to EVENT_INIT (inev) here, as
7103 x_session_check_input copies an entire input_event. */
7104 if (x_session_check_input (&inev))
7106 kbd_buffer_store_event_hold (&inev, hold_quit);
7107 count++;
7109 UNBLOCK_INPUT;
7111 #endif
7113 /* For debugging, this gives a way to fake an I/O error. */
7114 if (terminal->display_info.x == XTread_socket_fake_io_error)
7116 XTread_socket_fake_io_error = 0;
7117 x_io_error_quitter (terminal->display_info.x->display);
7120 #ifndef USE_GTK
7121 while (XPending (terminal->display_info.x->display))
7123 int finish;
7125 XNextEvent (terminal->display_info.x->display, &event);
7127 #ifdef HAVE_X_I18N
7128 /* Filter events for the current X input method. */
7129 if (x_filter_event (terminal->display_info.x, &event))
7130 continue;
7131 #endif
7132 event_found = 1;
7134 count += handle_one_xevent (terminal->display_info.x,
7135 &event, &finish, hold_quit);
7137 if (finish == X_EVENT_GOTO_OUT)
7138 goto out;
7141 #else /* USE_GTK */
7143 /* For GTK we must use the GTK event loop. But XEvents gets passed
7144 to our filter function above, and then to the big event switch.
7145 We use a bunch of globals to communicate with our filter function,
7146 that is kind of ugly, but it works.
7148 There is no way to do one display at the time, GTK just does events
7149 from all displays. */
7151 while (gtk_events_pending ())
7153 current_count = count;
7154 current_hold_quit = hold_quit;
7156 gtk_main_iteration ();
7158 count = current_count;
7159 current_count = -1;
7160 current_hold_quit = 0;
7162 if (current_finish == X_EVENT_GOTO_OUT)
7163 break;
7165 #endif /* USE_GTK */
7167 out:;
7169 /* On some systems, an X bug causes Emacs to get no more events
7170 when the window is destroyed. Detect that. (1994.) */
7171 if (! event_found)
7173 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7174 One XNOOP in 100 loops will make Emacs terminate.
7175 B. Bretthauer, 1994 */
7176 x_noop_count++;
7177 if (x_noop_count >= 100)
7179 x_noop_count=0;
7181 if (next_noop_dpyinfo == 0)
7182 next_noop_dpyinfo = x_display_list;
7184 XNoOp (next_noop_dpyinfo->display);
7186 /* Each time we get here, cycle through the displays now open. */
7187 next_noop_dpyinfo = next_noop_dpyinfo->next;
7191 /* If the focus was just given to an auto-raising frame,
7192 raise it now. */
7193 /* ??? This ought to be able to handle more than one such frame. */
7194 if (pending_autoraise_frame)
7196 x_raise_frame (pending_autoraise_frame);
7197 pending_autoraise_frame = 0;
7200 --handling_signal;
7201 UNBLOCK_INPUT;
7203 return count;
7209 /***********************************************************************
7210 Text Cursor
7211 ***********************************************************************/
7213 /* Set clipping for output in glyph row ROW. W is the window in which
7214 we operate. GC is the graphics context to set clipping in.
7216 ROW may be a text row or, e.g., a mode line. Text rows must be
7217 clipped to the interior of the window dedicated to text display,
7218 mode lines must be clipped to the whole window. */
7220 static void
7221 x_clip_to_row (w, row, area, gc)
7222 struct window *w;
7223 struct glyph_row *row;
7224 int area;
7225 GC gc;
7227 struct frame *f = XFRAME (WINDOW_FRAME (w));
7228 XRectangle clip_rect;
7229 int window_x, window_y, window_width;
7231 window_box (w, area, &window_x, &window_y, &window_width, 0);
7233 clip_rect.x = window_x;
7234 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7235 clip_rect.y = max (clip_rect.y, window_y);
7236 clip_rect.width = window_width;
7237 clip_rect.height = row->visible_height;
7239 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7243 /* Draw a hollow box cursor on window W in glyph row ROW. */
7245 static void
7246 x_draw_hollow_cursor (w, row)
7247 struct window *w;
7248 struct glyph_row *row;
7250 struct frame *f = XFRAME (WINDOW_FRAME (w));
7251 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7252 Display *dpy = FRAME_X_DISPLAY (f);
7253 int x, y, wd, h;
7254 XGCValues xgcv;
7255 struct glyph *cursor_glyph;
7256 GC gc;
7258 /* Get the glyph the cursor is on. If we can't tell because
7259 the current matrix is invalid or such, give up. */
7260 cursor_glyph = get_phys_cursor_glyph (w);
7261 if (cursor_glyph == NULL)
7262 return;
7264 /* Compute frame-relative coordinates for phys cursor. */
7265 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7266 wd = w->phys_cursor_width;
7268 /* The foreground of cursor_gc is typically the same as the normal
7269 background color, which can cause the cursor box to be invisible. */
7270 xgcv.foreground = f->output_data.x->cursor_pixel;
7271 if (dpyinfo->scratch_cursor_gc)
7272 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7273 else
7274 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7275 GCForeground, &xgcv);
7276 gc = dpyinfo->scratch_cursor_gc;
7278 /* Set clipping, draw the rectangle, and reset clipping again. */
7279 x_clip_to_row (w, row, TEXT_AREA, gc);
7280 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7281 XSetClipMask (dpy, gc, None);
7285 /* Draw a bar cursor on window W in glyph row ROW.
7287 Implementation note: One would like to draw a bar cursor with an
7288 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7289 Unfortunately, I didn't find a font yet that has this property set.
7290 --gerd. */
7292 static void
7293 x_draw_bar_cursor (w, row, width, kind)
7294 struct window *w;
7295 struct glyph_row *row;
7296 int width;
7297 enum text_cursor_kinds kind;
7299 struct frame *f = XFRAME (w->frame);
7300 struct glyph *cursor_glyph;
7302 /* If cursor is out of bounds, don't draw garbage. This can happen
7303 in mini-buffer windows when switching between echo area glyphs
7304 and mini-buffer. */
7305 cursor_glyph = get_phys_cursor_glyph (w);
7306 if (cursor_glyph == NULL)
7307 return;
7309 /* If on an image, draw like a normal cursor. That's usually better
7310 visible than drawing a bar, esp. if the image is large so that
7311 the bar might not be in the window. */
7312 if (cursor_glyph->type == IMAGE_GLYPH)
7314 struct glyph_row *row;
7315 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7316 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7318 else
7320 Display *dpy = FRAME_X_DISPLAY (f);
7321 Window window = FRAME_X_WINDOW (f);
7322 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7323 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7324 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7325 XGCValues xgcv;
7327 /* If the glyph's background equals the color we normally draw
7328 the bars cursor in, the bar cursor in its normal color is
7329 invisible. Use the glyph's foreground color instead in this
7330 case, on the assumption that the glyph's colors are chosen so
7331 that the glyph is legible. */
7332 if (face->background == f->output_data.x->cursor_pixel)
7333 xgcv.background = xgcv.foreground = face->foreground;
7334 else
7335 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7336 xgcv.graphics_exposures = 0;
7338 if (gc)
7339 XChangeGC (dpy, gc, mask, &xgcv);
7340 else
7342 gc = XCreateGC (dpy, window, mask, &xgcv);
7343 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7346 x_clip_to_row (w, row, TEXT_AREA, gc);
7348 if (kind == BAR_CURSOR)
7350 if (width < 0)
7351 width = FRAME_CURSOR_WIDTH (f);
7352 width = min (cursor_glyph->pixel_width, width);
7354 w->phys_cursor_width = width;
7356 XFillRectangle (dpy, window, gc,
7357 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7358 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7359 width, row->height);
7361 else
7363 int dummy_x, dummy_y, dummy_h;
7365 if (width < 0)
7366 width = row->height;
7368 width = min (row->height, width);
7370 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
7371 &dummy_y, &dummy_h);
7373 XFillRectangle (dpy, window, gc,
7374 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7375 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7376 row->height - width),
7377 w->phys_cursor_width, width);
7380 XSetClipMask (dpy, gc, None);
7385 /* RIF: Define cursor CURSOR on frame F. */
7387 static void
7388 x_define_frame_cursor (f, cursor)
7389 struct frame *f;
7390 Cursor cursor;
7392 if (!f->pointer_invisible
7393 && f->output_data.x->current_cursor != cursor)
7394 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7395 f->output_data.x->current_cursor = cursor;
7399 /* RIF: Clear area on frame F. */
7401 static void
7402 x_clear_frame_area (f, x, y, width, height)
7403 struct frame *f;
7404 int x, y, width, height;
7406 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7407 x, y, width, height, False);
7411 /* RIF: Draw cursor on window W. */
7413 static void
7414 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7415 struct window *w;
7416 struct glyph_row *glyph_row;
7417 int x, y;
7418 int cursor_type, cursor_width;
7419 int on_p, active_p;
7421 struct frame *f = XFRAME (WINDOW_FRAME (w));
7423 if (on_p)
7425 w->phys_cursor_type = cursor_type;
7426 w->phys_cursor_on_p = 1;
7428 if (glyph_row->exact_window_width_line_p
7429 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7431 glyph_row->cursor_in_fringe_p = 1;
7432 draw_fringe_bitmap (w, glyph_row, 0);
7434 else
7435 switch (cursor_type)
7437 case HOLLOW_BOX_CURSOR:
7438 x_draw_hollow_cursor (w, glyph_row);
7439 break;
7441 case FILLED_BOX_CURSOR:
7442 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7443 break;
7445 case BAR_CURSOR:
7446 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7447 break;
7449 case HBAR_CURSOR:
7450 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7451 break;
7453 case NO_CURSOR:
7454 w->phys_cursor_width = 0;
7455 break;
7457 default:
7458 abort ();
7461 #ifdef HAVE_X_I18N
7462 if (w == XWINDOW (f->selected_window))
7463 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7464 xic_set_preeditarea (w, x, y);
7465 #endif
7468 #ifndef XFlush
7469 XFlush (FRAME_X_DISPLAY (f));
7470 #endif
7474 /* Icons. */
7476 /* Make the x-window of frame F use the gnu icon bitmap. */
7479 x_bitmap_icon (f, file)
7480 struct frame *f;
7481 Lisp_Object file;
7483 int bitmap_id;
7485 if (FRAME_X_WINDOW (f) == 0)
7486 return 1;
7488 /* Free up our existing icon bitmap and mask if any. */
7489 if (f->output_data.x->icon_bitmap > 0)
7490 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7491 f->output_data.x->icon_bitmap = 0;
7493 if (STRINGP (file))
7495 #ifdef USE_GTK
7496 /* Use gtk_window_set_icon_from_file () if available,
7497 It's not restricted to bitmaps */
7498 if (xg_set_icon (f, file))
7499 return 0;
7500 #endif /* USE_GTK */
7501 bitmap_id = x_create_bitmap_from_file (f, file);
7502 x_create_bitmap_mask (f, bitmap_id);
7504 else
7506 /* Create the GNU bitmap and mask if necessary. */
7507 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7509 int rc = -1;
7511 #ifdef USE_GTK
7513 if (xg_set_icon (f, xg_default_icon_file)
7514 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7515 return 0;
7517 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7519 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7520 if (rc != -1)
7521 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7523 #endif
7525 /* If all else fails, use the (black and white) xbm image. */
7526 if (rc == -1)
7528 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7529 gnu_xbm_width, gnu_xbm_height);
7530 if (rc == -1)
7531 return 1;
7533 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7534 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7538 /* The first time we create the GNU bitmap and mask,
7539 this increments the ref-count one extra time.
7540 As a result, the GNU bitmap and mask are never freed.
7541 That way, we don't have to worry about allocating it again. */
7542 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7544 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7547 x_wm_set_icon_pixmap (f, bitmap_id);
7548 f->output_data.x->icon_bitmap = bitmap_id;
7550 return 0;
7554 /* Make the x-window of frame F use a rectangle with text.
7555 Use ICON_NAME as the text. */
7558 x_text_icon (f, icon_name)
7559 struct frame *f;
7560 char *icon_name;
7562 if (FRAME_X_WINDOW (f) == 0)
7563 return 1;
7566 XTextProperty text;
7567 text.value = (unsigned char *) icon_name;
7568 text.encoding = XA_STRING;
7569 text.format = 8;
7570 text.nitems = strlen (icon_name);
7571 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7574 if (f->output_data.x->icon_bitmap > 0)
7575 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7576 f->output_data.x->icon_bitmap = 0;
7577 x_wm_set_icon_pixmap (f, 0);
7579 return 0;
7582 #define X_ERROR_MESSAGE_SIZE 200
7584 /* If non-nil, this should be a string.
7585 It means catch X errors and store the error message in this string.
7587 The reason we use a stack is that x_catch_error/x_uncatch_error can
7588 be called from a signal handler.
7591 struct x_error_message_stack {
7592 char string[X_ERROR_MESSAGE_SIZE];
7593 Display *dpy;
7594 struct x_error_message_stack *prev;
7596 static struct x_error_message_stack *x_error_message;
7598 /* An X error handler which stores the error message in
7599 *x_error_message. This is called from x_error_handler if
7600 x_catch_errors is in effect. */
7602 static void
7603 x_error_catcher (display, error)
7604 Display *display;
7605 XErrorEvent *error;
7607 XGetErrorText (display, error->error_code,
7608 x_error_message->string,
7609 X_ERROR_MESSAGE_SIZE);
7612 /* Begin trapping X errors for display DPY. Actually we trap X errors
7613 for all displays, but DPY should be the display you are actually
7614 operating on.
7616 After calling this function, X protocol errors no longer cause
7617 Emacs to exit; instead, they are recorded in the string
7618 stored in *x_error_message.
7620 Calling x_check_errors signals an Emacs error if an X error has
7621 occurred since the last call to x_catch_errors or x_check_errors.
7623 Calling x_uncatch_errors resumes the normal error handling. */
7625 void x_check_errors ();
7627 void
7628 x_catch_errors (dpy)
7629 Display *dpy;
7631 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7633 /* Make sure any errors from previous requests have been dealt with. */
7634 XSync (dpy, False);
7636 data->dpy = dpy;
7637 data->string[0] = 0;
7638 data->prev = x_error_message;
7639 x_error_message = data;
7642 /* Undo the last x_catch_errors call.
7643 DPY should be the display that was passed to x_catch_errors. */
7645 void
7646 x_uncatch_errors ()
7648 struct x_error_message_stack *tmp;
7650 BLOCK_INPUT;
7652 /* The display may have been closed before this function is called.
7653 Check if it is still open before calling XSync. */
7654 if (x_display_info_for_display (x_error_message->dpy) != 0)
7655 XSync (x_error_message->dpy, False);
7657 tmp = x_error_message;
7658 x_error_message = x_error_message->prev;
7659 xfree (tmp);
7660 UNBLOCK_INPUT;
7663 /* If any X protocol errors have arrived since the last call to
7664 x_catch_errors or x_check_errors, signal an Emacs error using
7665 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7667 void
7668 x_check_errors (dpy, format)
7669 Display *dpy;
7670 char *format;
7672 /* Make sure to catch any errors incurred so far. */
7673 XSync (dpy, False);
7675 if (x_error_message->string[0])
7677 char string[X_ERROR_MESSAGE_SIZE];
7678 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7679 x_uncatch_errors ();
7680 error (format, string);
7684 /* Nonzero if we had any X protocol errors
7685 since we did x_catch_errors on DPY. */
7688 x_had_errors_p (dpy)
7689 Display *dpy;
7691 /* Make sure to catch any errors incurred so far. */
7692 XSync (dpy, False);
7694 return x_error_message->string[0] != 0;
7697 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7699 void
7700 x_clear_errors (dpy)
7701 Display *dpy;
7703 x_error_message->string[0] = 0;
7706 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7707 * idea. --lorentey */
7708 /* Close off all unclosed x_catch_errors calls. */
7710 void
7711 x_fully_uncatch_errors ()
7713 while (x_error_message)
7714 x_uncatch_errors ();
7716 #endif
7718 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7721 x_catching_errors ()
7723 return x_error_message != 0;
7726 #if 0
7727 static unsigned int x_wire_count;
7728 x_trace_wire ()
7730 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7732 #endif /* ! 0 */
7735 /* Handle SIGPIPE, which can happen when the connection to a server
7736 simply goes away. SIGPIPE is handled by x_connection_signal.
7737 Don't need to do anything, because the write which caused the
7738 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7739 which will do the appropriate cleanup for us. */
7741 static SIGTYPE
7742 x_connection_signal (signalnum) /* If we don't have an argument, */
7743 int signalnum; /* some compilers complain in signal calls. */
7745 #ifdef USG
7746 /* USG systems forget handlers when they are used;
7747 must reestablish each time */
7748 signal (signalnum, x_connection_signal);
7749 #endif /* USG */
7753 /************************************************************************
7754 Handling X errors
7755 ************************************************************************/
7757 /* Error message passed to x_connection_closed. */
7759 static char *error_msg;
7761 /* Function installed as fatal_error_signal_hook in
7762 x_connection_closed. Print the X error message, and exit normally,
7763 instead of dumping core when XtCloseDisplay fails. */
7765 static void
7766 x_fatal_error_signal ()
7768 fprintf (stderr, "%s\n", error_msg);
7769 exit (70);
7772 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7773 the text of an error message that lead to the connection loss. */
7775 static SIGTYPE
7776 x_connection_closed (dpy, error_message)
7777 Display *dpy;
7778 char *error_message;
7780 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7781 Lisp_Object frame, tail;
7782 int index = SPECPDL_INDEX ();
7784 error_msg = (char *) alloca (strlen (error_message) + 1);
7785 strcpy (error_msg, error_message);
7786 handling_signal = 0;
7788 /* Prevent being called recursively because of an error condition
7789 below. Otherwise, we might end up with printing ``can't find per
7790 display information'' in the recursive call instead of printing
7791 the original message here. */
7792 x_catch_errors (dpy);
7794 /* Inhibit redisplay while frames are being deleted. */
7795 specbind (Qinhibit_redisplay, Qt);
7797 if (dpyinfo)
7799 /* Protect display from being closed when we delete the last
7800 frame on it. */
7801 dpyinfo->reference_count++;
7802 dpyinfo->terminal->reference_count++;
7805 /* First delete frames whose mini-buffers are on frames
7806 that are on the dead display. */
7807 FOR_EACH_FRAME (tail, frame)
7809 Lisp_Object minibuf_frame;
7810 minibuf_frame
7811 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7812 if (FRAME_X_P (XFRAME (frame))
7813 && FRAME_X_P (XFRAME (minibuf_frame))
7814 && ! EQ (frame, minibuf_frame)
7815 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7816 delete_frame (frame, Qnoelisp);
7819 /* Now delete all remaining frames on the dead display.
7820 We are now sure none of these is used as the mini-buffer
7821 for another frame that we need to delete. */
7822 FOR_EACH_FRAME (tail, frame)
7823 if (FRAME_X_P (XFRAME (frame))
7824 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7826 /* Set this to t so that delete_frame won't get confused
7827 trying to find a replacement. */
7828 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7829 delete_frame (frame, Qnoelisp);
7832 /* We have to close the display to inform Xt that it doesn't
7833 exist anymore. If we don't, Xt will continue to wait for
7834 events from the display. As a consequence, a sequence of
7836 M-x make-frame-on-display RET :1 RET
7837 ...kill the new frame, so that we get an IO error...
7838 M-x make-frame-on-display RET :1 RET
7840 will indefinitely wait in Xt for events for display `:1', opened
7841 in the first call to make-frame-on-display.
7843 Closing the display is reported to lead to a bus error on
7844 OpenWindows in certain situations. I suspect that is a bug
7845 in OpenWindows. I don't know how to circumvent it here. */
7847 if (dpyinfo)
7849 #ifdef USE_X_TOOLKIT
7850 /* If DPYINFO is null, this means we didn't open the display
7851 in the first place, so don't try to close it. */
7853 extern void (*fatal_error_signal_hook) P_ ((void));
7854 fatal_error_signal_hook = x_fatal_error_signal;
7855 XtCloseDisplay (dpy);
7856 fatal_error_signal_hook = NULL;
7858 #endif
7860 #ifdef USE_GTK
7861 /* Due to bugs in some Gtk+ versions, just exit here if this
7862 is the last display/terminal. */
7863 if (terminal_list->next_terminal == NULL)
7865 fprintf (stderr, "%s\n", error_msg);
7866 shut_down_emacs (0, 0, Qnil);
7867 exit (70);
7869 xg_display_close (dpyinfo->display);
7870 #endif
7872 /* Indicate that this display is dead. */
7873 dpyinfo->display = 0;
7875 dpyinfo->reference_count--;
7876 dpyinfo->terminal->reference_count--;
7877 if (dpyinfo->reference_count != 0)
7878 /* We have just closed all frames on this display. */
7879 abort ();
7882 Lisp_Object tmp;
7883 XSETTERMINAL (tmp, dpyinfo->terminal);
7884 Fdelete_terminal (tmp, Qnoelisp);
7888 x_uncatch_errors ();
7890 if (terminal_list == 0)
7892 fprintf (stderr, "%s\n", error_msg);
7893 shut_down_emacs (0, 0, Qnil);
7894 exit (70);
7897 /* Ordinary stack unwind doesn't deal with these. */
7898 #ifdef SIGIO
7899 sigunblock (sigmask (SIGIO));
7900 #endif
7901 sigunblock (sigmask (SIGALRM));
7902 TOTALLY_UNBLOCK_INPUT;
7904 unbind_to (index, Qnil);
7905 clear_waiting_for_input ();
7906 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7907 longjmp), because returning from this function would get us back into
7908 Xlib's code which will directly call `exit'. */
7909 error ("%s", error_msg);
7912 /* We specifically use it before defining it, so that gcc doesn't inline it,
7913 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7914 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7916 /* This is the first-level handler for X protocol errors.
7917 It calls x_error_quitter or x_error_catcher. */
7919 static int
7920 x_error_handler (display, error)
7921 Display *display;
7922 XErrorEvent *error;
7924 if (x_error_message)
7925 x_error_catcher (display, error);
7926 else
7927 x_error_quitter (display, error);
7928 return 0;
7931 /* This is the usual handler for X protocol errors.
7932 It kills all frames on the display that we got the error for.
7933 If that was the only one, it prints an error message and kills Emacs. */
7935 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7937 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
7938 #define NO_INLINE __attribute__((noinline))
7939 #else
7940 #define NO_INLINE
7941 #endif
7943 /* Some versions of GNU/Linux define noinline in their headers. */
7945 #ifdef noinline
7946 #undef noinline
7947 #endif
7949 /* On older GCC versions, just putting x_error_quitter
7950 after x_error_handler prevents inlining into the former. */
7952 static void NO_INLINE
7953 x_error_quitter (display, error)
7954 Display *display;
7955 XErrorEvent *error;
7957 char buf[256], buf1[356];
7959 /* Ignore BadName errors. They can happen because of fonts
7960 or colors that are not defined. */
7962 if (error->error_code == BadName)
7963 return;
7965 /* Note that there is no real way portable across R3/R4 to get the
7966 original error handler. */
7968 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7969 sprintf (buf1, "X protocol error: %s on protocol request %d",
7970 buf, error->request_code);
7971 x_connection_closed (display, buf1);
7975 /* This is the handler for X IO errors, always.
7976 It kills all frames on the display that we lost touch with.
7977 If that was the only one, it prints an error message and kills Emacs. */
7979 static int
7980 x_io_error_quitter (display)
7981 Display *display;
7983 char buf[256];
7985 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7986 x_connection_closed (display, buf);
7987 return 0;
7990 /* Changing the font of the frame. */
7992 /* Give frame F the font FONT-OBJECT as its default font. The return
7993 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
7994 frame. If it is negative, generate a new fontset from
7995 FONT-OBJECT. */
7997 Lisp_Object
7998 x_new_font (f, font_object, fontset)
7999 struct frame *f;
8000 Lisp_Object font_object;
8001 int fontset;
8003 struct font *font = XFONT_OBJECT (font_object);
8005 if (fontset < 0)
8006 fontset = fontset_from_font (font_object);
8007 FRAME_FONTSET (f) = fontset;
8008 if (FRAME_FONT (f) == font)
8009 /* This font is already set in frame F. There's nothing more to
8010 do. */
8011 return font_object;
8013 FRAME_FONT (f) = font;
8014 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8015 FRAME_COLUMN_WIDTH (f) = font->average_width;
8016 FRAME_SPACE_WIDTH (f) = font->space_width;
8017 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8019 compute_fringe_widths (f, 1);
8021 /* Compute the scroll bar width in character columns. */
8022 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8024 int wid = FRAME_COLUMN_WIDTH (f);
8025 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8026 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8028 else
8030 int wid = FRAME_COLUMN_WIDTH (f);
8031 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8034 if (FRAME_X_WINDOW (f) != 0)
8036 /* Don't change the size of a tip frame; there's no point in
8037 doing it because it's done in Fx_show_tip, and it leads to
8038 problems because the tip frame has no widget. */
8039 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8041 /* When the frame is maximized/fullscreen or running under for
8042 example Xmonad, x_set_window_size will be a no-op.
8043 In that case, the right thing to do is extend rows/cols to
8044 the current frame size. We do that first if x_set_window_size
8045 turns out to not be a no-op (there is no way to know).
8046 The size will be adjusted again if the frame gets a
8047 ConfigureNotify event as a result of x_set_window_size. */
8048 int pixelh = FRAME_PIXEL_HEIGHT (f);
8049 #ifdef USE_X_TOOLKIT
8050 /* The menu bar is not part of text lines. The tool bar
8051 is however. */
8052 pixelh -= FRAME_MENUBAR_HEIGHT (f);
8053 #endif
8054 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelh);
8055 int cols = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, FRAME_PIXEL_WIDTH (f));
8057 change_frame_size (f, rows, cols, 0, 1, 0);
8058 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8062 #ifdef HAVE_X_I18N
8063 if (FRAME_XIC (f)
8064 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8066 BLOCK_INPUT;
8067 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8068 UNBLOCK_INPUT;
8070 #endif
8072 return font_object;
8076 /***********************************************************************
8077 X Input Methods
8078 ***********************************************************************/
8080 #ifdef HAVE_X_I18N
8082 #ifdef HAVE_X11R6
8084 /* XIM destroy callback function, which is called whenever the
8085 connection to input method XIM dies. CLIENT_DATA contains a
8086 pointer to the x_display_info structure corresponding to XIM. */
8088 static void
8089 xim_destroy_callback (xim, client_data, call_data)
8090 XIM xim;
8091 XPointer client_data;
8092 XPointer call_data;
8094 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8095 Lisp_Object frame, tail;
8097 BLOCK_INPUT;
8099 /* No need to call XDestroyIC.. */
8100 FOR_EACH_FRAME (tail, frame)
8102 struct frame *f = XFRAME (frame);
8103 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8105 FRAME_XIC (f) = NULL;
8106 xic_free_xfontset (f);
8110 /* No need to call XCloseIM. */
8111 dpyinfo->xim = NULL;
8112 XFree (dpyinfo->xim_styles);
8113 UNBLOCK_INPUT;
8116 #endif /* HAVE_X11R6 */
8118 #ifdef HAVE_X11R6
8119 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8120 extern char *XSetIMValues P_ ((XIM, ...));
8121 #endif
8123 /* Open the connection to the XIM server on display DPYINFO.
8124 RESOURCE_NAME is the resource name Emacs uses. */
8126 static void
8127 xim_open_dpy (dpyinfo, resource_name)
8128 struct x_display_info *dpyinfo;
8129 char *resource_name;
8131 XIM xim;
8133 #ifdef HAVE_XIM
8134 if (use_xim)
8136 if (dpyinfo->xim)
8137 XCloseIM (dpyinfo->xim);
8138 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8139 EMACS_CLASS);
8140 dpyinfo->xim = xim;
8142 if (xim)
8144 #ifdef HAVE_X11R6
8145 XIMCallback destroy;
8146 #endif
8148 /* Get supported styles and XIM values. */
8149 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8151 #ifdef HAVE_X11R6
8152 destroy.callback = xim_destroy_callback;
8153 destroy.client_data = (XPointer)dpyinfo;
8154 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8155 #endif
8159 else
8160 #endif /* HAVE_XIM */
8161 dpyinfo->xim = NULL;
8165 #ifdef HAVE_X11R6_XIM
8167 /* XIM instantiate callback function, which is called whenever an XIM
8168 server is available. DISPLAY is the display of the XIM.
8169 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8170 when the callback was registered. */
8172 static void
8173 xim_instantiate_callback (display, client_data, call_data)
8174 Display *display;
8175 XPointer client_data;
8176 XPointer call_data;
8178 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8179 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8181 /* We don't support multiple XIM connections. */
8182 if (dpyinfo->xim)
8183 return;
8185 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8187 /* Create XIC for the existing frames on the same display, as long
8188 as they have no XIC. */
8189 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8191 Lisp_Object tail, frame;
8193 BLOCK_INPUT;
8194 FOR_EACH_FRAME (tail, frame)
8196 struct frame *f = XFRAME (frame);
8198 if (FRAME_X_P (f)
8199 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8200 if (FRAME_XIC (f) == NULL)
8202 create_frame_xic (f);
8203 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8204 xic_set_statusarea (f);
8205 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8207 struct window *w = XWINDOW (f->selected_window);
8208 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8213 UNBLOCK_INPUT;
8217 #endif /* HAVE_X11R6_XIM */
8220 /* Open a connection to the XIM server on display DPYINFO.
8221 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8222 connection only at the first time. On X11R6, open the connection
8223 in the XIM instantiate callback function. */
8225 static void
8226 xim_initialize (dpyinfo, resource_name)
8227 struct x_display_info *dpyinfo;
8228 char *resource_name;
8230 dpyinfo->xim = NULL;
8231 #ifdef HAVE_XIM
8232 if (use_xim)
8234 #ifdef HAVE_X11R6_XIM
8235 struct xim_inst_t *xim_inst;
8236 int len;
8238 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8239 dpyinfo->xim_callback_data = xim_inst;
8240 xim_inst->dpyinfo = dpyinfo;
8241 len = strlen (resource_name);
8242 xim_inst->resource_name = (char *) xmalloc (len + 1);
8243 bcopy (resource_name, xim_inst->resource_name, len + 1);
8244 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8245 resource_name, EMACS_CLASS,
8246 xim_instantiate_callback,
8247 /* This is XPointer in XFree86
8248 but (XPointer *) on Tru64, at
8249 least, hence the configure test. */
8250 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8251 #else /* not HAVE_X11R6_XIM */
8252 xim_open_dpy (dpyinfo, resource_name);
8253 #endif /* not HAVE_X11R6_XIM */
8255 #endif /* HAVE_XIM */
8259 /* Close the connection to the XIM server on display DPYINFO. */
8261 static void
8262 xim_close_dpy (dpyinfo)
8263 struct x_display_info *dpyinfo;
8265 #ifdef HAVE_XIM
8266 if (use_xim)
8268 #ifdef HAVE_X11R6_XIM
8269 if (dpyinfo->display)
8270 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8271 NULL, EMACS_CLASS,
8272 xim_instantiate_callback, NULL);
8273 xfree (dpyinfo->xim_callback_data->resource_name);
8274 xfree (dpyinfo->xim_callback_data);
8275 #endif /* HAVE_X11R6_XIM */
8276 if (dpyinfo->display)
8277 XCloseIM (dpyinfo->xim);
8278 dpyinfo->xim = NULL;
8279 XFree (dpyinfo->xim_styles);
8281 #endif /* HAVE_XIM */
8284 #endif /* not HAVE_X11R6_XIM */
8288 /* Calculate the absolute position in frame F
8289 from its current recorded position values and gravity. */
8291 void
8292 x_calc_absolute_position (f)
8293 struct frame *f;
8295 int flags = f->size_hint_flags;
8297 /* We have nothing to do if the current position
8298 is already for the top-left corner. */
8299 if (! ((flags & XNegative) || (flags & YNegative)))
8300 return;
8302 /* Treat negative positions as relative to the leftmost bottommost
8303 position that fits on the screen. */
8304 if (flags & XNegative)
8305 f->left_pos = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
8306 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8309 int height = FRAME_PIXEL_HEIGHT (f);
8311 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8312 /* Something is fishy here. When using Motif, starting Emacs with
8313 `-g -0-0', the frame appears too low by a few pixels.
8315 This seems to be so because initially, while Emacs is starting,
8316 the column widget's height and the frame's pixel height are
8317 different. The column widget's height is the right one. In
8318 later invocations, when Emacs is up, the frame's pixel height
8319 is right, though.
8321 It's not obvious where the initial small difference comes from.
8322 2000-12-01, gerd. */
8324 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8325 #endif
8327 if (flags & YNegative)
8328 f->top_pos = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
8329 - height + f->top_pos;
8332 /* The left_pos and top_pos
8333 are now relative to the top and left screen edges,
8334 so the flags should correspond. */
8335 f->size_hint_flags &= ~ (XNegative | YNegative);
8338 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8339 to really change the position, and 0 when calling from
8340 x_make_frame_visible (in that case, XOFF and YOFF are the current
8341 position values). It is -1 when calling from x_set_frame_parameters,
8342 which means, do adjust for borders but don't change the gravity. */
8344 void
8345 x_set_offset (f, xoff, yoff, change_gravity)
8346 struct frame *f;
8347 register int xoff, yoff;
8348 int change_gravity;
8350 int modified_top, modified_left;
8352 if (change_gravity > 0)
8354 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8355 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8357 f->top_pos = yoff;
8358 f->left_pos = xoff;
8359 f->size_hint_flags &= ~ (XNegative | YNegative);
8360 if (xoff < 0)
8361 f->size_hint_flags |= XNegative;
8362 if (yoff < 0)
8363 f->size_hint_flags |= YNegative;
8364 f->win_gravity = NorthWestGravity;
8366 x_calc_absolute_position (f);
8368 BLOCK_INPUT;
8369 x_wm_set_size_hint (f, (long) 0, 0);
8371 modified_left = f->left_pos;
8372 modified_top = f->top_pos;
8374 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8376 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8377 than the WM decorations. So we use the calculated offset instead
8378 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8379 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8380 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8383 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8384 modified_left, modified_top);
8386 x_sync_with_move (f, f->left_pos, f->top_pos,
8387 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8388 ? 1 : 0);
8390 /* change_gravity is non-zero when this function is called from Lisp to
8391 programmatically move a frame. In that case, we call
8392 x_check_expected_move to discover if we have a "Type A" or "Type B"
8393 window manager, and, for a "Type A" window manager, adjust the position
8394 of the frame.
8396 We call x_check_expected_move if a programmatic move occurred, and
8397 either the window manager type (A/B) is unknown or it is Type A but we
8398 need to compute the top/left offset adjustment for this frame. */
8400 if (change_gravity != 0 &&
8401 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8402 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8403 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8404 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8405 x_check_expected_move (f, modified_left, modified_top);
8407 UNBLOCK_INPUT;
8410 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8411 on the root window for frame F contains ATOMNAME.
8412 This is how a WM check shall be done according to the Window Manager
8413 Specification/Extended Window Manager Hints at
8414 http://freedesktop.org/wiki/Specifications/wm-spec. */
8416 static int
8417 wm_supports (f, atomname)
8418 struct frame *f;
8419 const char *atomname;
8421 Atom actual_type;
8422 unsigned long actual_size, bytes_remaining;
8423 int i, rc, actual_format;
8424 Atom prop_atom;
8425 Window wmcheck_window;
8426 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8427 Window target_window = dpyinfo->root_window;
8428 long max_len = 65536;
8429 Display *dpy = FRAME_X_DISPLAY (f);
8430 unsigned char *tmp_data = NULL;
8431 Atom target_type = XA_WINDOW;
8432 Atom want_atom;
8434 BLOCK_INPUT;
8436 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8438 x_catch_errors (dpy);
8439 rc = XGetWindowProperty (dpy, target_window,
8440 prop_atom, 0, max_len, False, target_type,
8441 &actual_type, &actual_format, &actual_size,
8442 &bytes_remaining, &tmp_data);
8444 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8446 if (tmp_data) XFree (tmp_data);
8447 x_uncatch_errors ();
8448 UNBLOCK_INPUT;
8449 return 0;
8452 wmcheck_window = *(Window *) tmp_data;
8453 XFree (tmp_data);
8455 /* Check if window exists. */
8456 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8457 x_sync (f);
8458 if (x_had_errors_p (dpy))
8460 x_uncatch_errors ();
8461 UNBLOCK_INPUT;
8462 return 0;
8465 if (dpyinfo->net_supported_window != wmcheck_window)
8467 /* Window changed, reload atoms */
8468 if (dpyinfo->net_supported_atoms != NULL)
8469 XFree (dpyinfo->net_supported_atoms);
8470 dpyinfo->net_supported_atoms = NULL;
8471 dpyinfo->nr_net_supported_atoms = 0;
8472 dpyinfo->net_supported_window = 0;
8474 target_type = XA_ATOM;
8475 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8476 tmp_data = NULL;
8477 rc = XGetWindowProperty (dpy, target_window,
8478 prop_atom, 0, max_len, False, target_type,
8479 &actual_type, &actual_format, &actual_size,
8480 &bytes_remaining, &tmp_data);
8482 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8484 if (tmp_data) XFree (tmp_data);
8485 x_uncatch_errors ();
8486 UNBLOCK_INPUT;
8487 return 0;
8490 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8491 dpyinfo->nr_net_supported_atoms = actual_size;
8492 dpyinfo->net_supported_window = wmcheck_window;
8495 rc = 0;
8496 want_atom = XInternAtom (dpy, atomname, False);
8498 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8499 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8501 x_uncatch_errors ();
8502 UNBLOCK_INPUT;
8504 return rc;
8507 static void
8508 set_wm_state (frame, add, what, what2)
8509 Lisp_Object frame;
8510 int add;
8511 const char *what;
8512 const char *what2;
8514 const char *atom = "_NET_WM_STATE";
8515 Fx_send_client_event (frame, make_number (0), frame,
8516 make_unibyte_string (atom, strlen (atom)),
8517 make_number (32),
8518 /* 1 = add, 0 = remove */
8519 Fcons
8520 (make_number (add ? 1 : 0),
8521 Fcons
8522 (make_unibyte_string (what, strlen (what)),
8523 what2 != 0
8524 ? Fcons (make_unibyte_string (what2, strlen (what2)),
8525 Qnil)
8526 : Qnil)));
8529 void
8530 x_set_sticky (f, new_value, old_value)
8531 struct frame *f;
8532 Lisp_Object new_value, old_value;
8534 Lisp_Object frame;
8536 XSETFRAME (frame, f);
8537 set_wm_state (frame, NILP (new_value) ? 0 : 1,
8538 "_NET_WM_STATE_STICKY", NULL);
8541 /* Do fullscreen as specified in extended window manager hints */
8543 static int
8544 do_ewmh_fullscreen (f)
8545 struct frame *f;
8547 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8549 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8550 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8551 if (!have_net_atom)
8552 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8554 if (have_net_atom)
8556 Lisp_Object frame;
8557 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8558 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8559 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8561 XSETFRAME (frame, f);
8563 set_wm_state (frame, 0, fs, NULL);
8564 set_wm_state (frame, 0, fh, NULL);
8565 set_wm_state (frame, 0, fw, NULL);
8567 /* If there are _NET_ atoms we assume we have extended window manager
8568 hints. */
8569 switch (f->want_fullscreen)
8571 case FULLSCREEN_BOTH:
8572 set_wm_state (frame, 1, fs, NULL);
8573 break;
8574 case FULLSCREEN_WIDTH:
8575 set_wm_state (frame, 1, fw, NULL);
8576 break;
8577 case FULLSCREEN_HEIGHT:
8578 set_wm_state (frame, 1, fh, NULL);
8579 break;
8580 case FULLSCREEN_MAXIMIZED:
8581 set_wm_state (frame, 1, fw, fh);
8582 break;
8585 f->want_fullscreen = FULLSCREEN_NONE;
8589 return have_net_atom;
8592 static void
8593 XTfullscreen_hook (f)
8594 FRAME_PTR f;
8596 if (f->async_visible)
8598 BLOCK_INPUT;
8599 x_check_fullscreen (f);
8600 x_sync (f);
8601 UNBLOCK_INPUT;
8606 static void
8607 x_handle_net_wm_state (f, event)
8608 struct frame *f;
8609 XPropertyEvent *event;
8611 Atom actual_type;
8612 unsigned long actual_size, bytes_remaining;
8613 int i, rc, actual_format, value = FULLSCREEN_NONE;
8614 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8615 long max_len = 65536;
8616 Display *dpy = FRAME_X_DISPLAY (f);
8617 unsigned char *tmp_data = NULL;
8618 Atom target_type = XA_ATOM;
8619 Lisp_Object lval;
8620 int sticky = 0;
8622 BLOCK_INPUT;
8623 x_catch_errors (dpy);
8624 rc = XGetWindowProperty (dpy, event->window,
8625 event->atom, 0, max_len, False, target_type,
8626 &actual_type, &actual_format, &actual_size,
8627 &bytes_remaining, &tmp_data);
8629 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
8631 if (tmp_data) XFree (tmp_data);
8632 x_uncatch_errors ();
8633 UNBLOCK_INPUT;
8634 return;
8637 x_uncatch_errors ();
8639 for (i = 0; i < actual_size; ++i)
8641 Atom a = ((Atom*)tmp_data)[i];
8642 if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
8644 if (value == FULLSCREEN_HEIGHT)
8645 value = FULLSCREEN_MAXIMIZED;
8646 else
8647 value = FULLSCREEN_WIDTH;
8649 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
8651 if (value == FULLSCREEN_WIDTH)
8652 value = FULLSCREEN_MAXIMIZED;
8653 else
8654 value = FULLSCREEN_HEIGHT;
8656 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen_atom)
8657 value = FULLSCREEN_BOTH;
8658 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
8659 sticky = 1;
8662 lval = Qnil;
8663 switch (value)
8665 case FULLSCREEN_WIDTH:
8666 lval = Qfullwidth;
8667 break;
8668 case FULLSCREEN_HEIGHT:
8669 lval = Qfullheight;
8670 break;
8671 case FULLSCREEN_BOTH:
8672 lval = Qfullboth;
8673 break;
8674 case FULLSCREEN_MAXIMIZED:
8675 lval = Qmaximized;
8676 break;
8679 store_frame_param (f, Qfullscreen, lval);
8680 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
8682 if (tmp_data) XFree (tmp_data);
8683 UNBLOCK_INPUT;
8686 /* Check if we need to resize the frame due to a fullscreen request.
8687 If so needed, resize the frame. */
8688 static void
8689 x_check_fullscreen (f)
8690 struct frame *f;
8692 if (do_ewmh_fullscreen (f))
8693 return;
8695 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8696 return; /* Only fullscreen without WM or with EWM hints (above). */
8698 if (f->want_fullscreen != FULLSCREEN_NONE)
8700 int width = FRAME_COLS (f), height = FRAME_LINES (f);
8701 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8703 switch (f->want_fullscreen)
8705 /* No difference between these two when there is no WM */
8706 case FULLSCREEN_BOTH:
8707 case FULLSCREEN_MAXIMIZED:
8708 width = x_display_pixel_width (dpyinfo);
8709 height = x_display_pixel_height (dpyinfo);
8710 break;
8711 case FULLSCREEN_WIDTH:
8712 width = x_display_pixel_width (dpyinfo);
8713 break;
8714 case FULLSCREEN_HEIGHT:
8715 height = x_display_pixel_height (dpyinfo);
8718 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8720 change_frame_size (f, height, width, 0, 1, 0);
8721 SET_FRAME_GARBAGED (f);
8722 cancel_mouse_face (f);
8727 /* This function is called by x_set_offset to determine whether the window
8728 manager interfered with the positioning of the frame. Type A window
8729 managers position the surrounding window manager decorations a small
8730 amount above and left of the user-supplied position. Type B window
8731 managers position the surrounding window manager decorations at the
8732 user-specified position. If we detect a Type A window manager, we
8733 compensate by moving the window right and down by the proper amount. */
8735 static void
8736 x_check_expected_move (f, expected_left, expected_top)
8737 struct frame *f;
8738 int expected_left;
8739 int expected_top;
8741 int current_left = 0, current_top = 0;
8743 /* x_real_positions returns the left and top offsets of the outermost
8744 window manager window around the frame. */
8746 x_real_positions (f, &current_left, &current_top);
8748 if (current_left != expected_left || current_top != expected_top)
8750 /* It's a "Type A" window manager. */
8752 int adjusted_left;
8753 int adjusted_top;
8755 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8756 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8757 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8759 /* Now fix the mispositioned frame's location. */
8761 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8762 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8764 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8765 adjusted_left, adjusted_top);
8767 x_sync_with_move (f, expected_left, expected_top, 0);
8769 else
8770 /* It's a "Type B" window manager. We don't have to adjust the
8771 frame's position. */
8773 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8777 /* Wait for XGetGeometry to return up-to-date position information for a
8778 recently-moved frame. Call this immediately after calling XMoveWindow.
8779 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8780 frame has been moved to, so we use a fuzzy position comparison instead
8781 of an exact comparison. */
8783 static void
8784 x_sync_with_move (f, left, top, fuzzy)
8785 struct frame *f;
8786 int left, top, fuzzy;
8788 int count = 0;
8790 while (count++ < 50)
8792 int current_left = 0, current_top = 0;
8794 /* In theory, this call to XSync only needs to happen once, but in
8795 practice, it doesn't seem to work, hence the need for the surrounding
8796 loop. */
8798 XSync (FRAME_X_DISPLAY (f), False);
8799 x_real_positions (f, &current_left, &current_top);
8801 if (fuzzy)
8803 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8804 pixels. */
8806 if (eabs (current_left - left) <= 10
8807 && eabs (current_top - top) <= 40)
8808 return;
8810 else if (current_left == left && current_top == top)
8811 return;
8814 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8815 will then return up-to-date position info. */
8817 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8821 /* Wait for an event on frame F matching EVENTTYPE. */
8822 void
8823 x_wait_for_event (f, eventtype)
8824 struct frame *f;
8825 int eventtype;
8827 int level = interrupt_input_blocked;
8829 SELECT_TYPE fds;
8830 EMACS_TIME tmo, tmo_at, time_now;
8831 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
8833 pending_event_wait.f = f;
8834 pending_event_wait.eventtype = eventtype;
8836 /* Set timeout to 0.1 second. Hopefully not noticable.
8837 Maybe it should be configurable. */
8838 EMACS_SET_SECS_USECS (tmo, 0, 100000);
8839 EMACS_GET_TIME (tmo_at);
8840 EMACS_ADD_TIME (tmo_at, tmo_at, tmo);
8842 while (pending_event_wait.eventtype)
8844 interrupt_input_pending = 1;
8845 TOTALLY_UNBLOCK_INPUT;
8846 /* XTread_socket is called after unblock. */
8847 BLOCK_INPUT;
8848 interrupt_input_blocked = level;
8850 FD_ZERO (&fds);
8851 FD_SET (fd, &fds);
8853 EMACS_GET_TIME (time_now);
8854 EMACS_SUB_TIME (tmo, tmo_at, time_now);
8856 if (EMACS_TIME_NEG_P (tmo) || select (fd+1, &fds, NULL, NULL, &tmo) == 0)
8857 break; /* Timeout */
8859 pending_event_wait.f = 0;
8860 pending_event_wait.eventtype = 0;
8864 /* Change the size of frame F's X window to COLS/ROWS in the case F
8865 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8866 top-left-corner window gravity for this size change and subsequent
8867 size changes. Otherwise we leave the window gravity unchanged. */
8869 static void
8870 x_set_window_size_1 (f, change_gravity, cols, rows)
8871 struct frame *f;
8872 int change_gravity;
8873 int cols, rows;
8875 int pixelwidth, pixelheight;
8877 check_frame_size (f, &rows, &cols);
8878 f->scroll_bar_actual_width
8879 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8881 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8882 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8883 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8885 compute_fringe_widths (f, 0);
8887 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
8888 + 2*f->border_width;
8889 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
8890 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f)
8891 + 2*f->border_width;
8893 if (change_gravity) f->win_gravity = NorthWestGravity;
8894 x_wm_set_size_hint (f, (long) 0, 0);
8895 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8896 pixelwidth, pixelheight);
8899 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8900 receive in the ConfigureNotify event; if we get what we asked
8901 for, then the event won't cause the screen to become garbaged, so
8902 we have to make sure to do it here. */
8903 SET_FRAME_GARBAGED (f);
8905 /* Now, strictly speaking, we can't be sure that this is accurate,
8906 but the window manager will get around to dealing with the size
8907 change request eventually, and we'll hear how it went when the
8908 ConfigureNotify event gets here.
8910 We could just not bother storing any of this information here,
8911 and let the ConfigureNotify event set everything up, but that
8912 might be kind of confusing to the Lisp code, since size changes
8913 wouldn't be reported in the frame parameters until some random
8914 point in the future when the ConfigureNotify event arrives.
8916 We pass 1 for DELAY since we can't run Lisp code inside of
8917 a BLOCK_INPUT. */
8919 /* But the ConfigureNotify may in fact never arrive, and then this is
8920 not right if the frame is visible. Instead wait (with timeout)
8921 for the ConfigureNotify. */
8922 if (f->async_visible)
8923 x_wait_for_event (f, ConfigureNotify);
8924 else
8926 change_frame_size (f, rows, cols, 0, 1, 0);
8927 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8928 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8929 x_sync (f);
8934 /* Call this to change the size of frame F's x-window.
8935 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8936 for this size change and subsequent size changes.
8937 Otherwise we leave the window gravity unchanged. */
8939 void
8940 x_set_window_size (f, change_gravity, cols, rows)
8941 struct frame *f;
8942 int change_gravity;
8943 int cols, rows;
8945 BLOCK_INPUT;
8947 #ifdef USE_GTK
8948 if (FRAME_GTK_WIDGET (f))
8949 xg_frame_set_char_size (f, cols, rows);
8950 else
8951 x_set_window_size_1 (f, change_gravity, cols, rows);
8952 #else /* not USE_GTK */
8954 x_set_window_size_1 (f, change_gravity, cols, rows);
8956 #endif /* not USE_GTK */
8958 /* If cursor was outside the new size, mark it as off. */
8959 mark_window_cursors_off (XWINDOW (f->root_window));
8961 /* Clear out any recollection of where the mouse highlighting was,
8962 since it might be in a place that's outside the new frame size.
8963 Actually checking whether it is outside is a pain in the neck,
8964 so don't try--just let the highlighting be done afresh with new size. */
8965 cancel_mouse_face (f);
8967 UNBLOCK_INPUT;
8970 /* Mouse warping. */
8972 void
8973 x_set_mouse_position (f, x, y)
8974 struct frame *f;
8975 int x, y;
8977 int pix_x, pix_y;
8979 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8980 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8982 if (pix_x < 0) pix_x = 0;
8983 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8985 if (pix_y < 0) pix_y = 0;
8986 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8988 BLOCK_INPUT;
8990 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8991 0, 0, 0, 0, pix_x, pix_y);
8992 UNBLOCK_INPUT;
8995 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8997 void
8998 x_set_mouse_pixel_position (f, pix_x, pix_y)
8999 struct frame *f;
9000 int pix_x, pix_y;
9002 BLOCK_INPUT;
9004 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9005 0, 0, 0, 0, pix_x, pix_y);
9006 UNBLOCK_INPUT;
9009 /* focus shifting, raising and lowering. */
9011 void
9012 x_focus_on_frame (f)
9013 struct frame *f;
9015 #if 0
9016 /* I don't think that the ICCCM allows programs to do things like this
9017 without the interaction of the window manager. Whatever you end up
9018 doing with this code, do it to x_unfocus_frame too. */
9019 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9020 RevertToPointerRoot, CurrentTime);
9021 #endif /* ! 0 */
9024 void
9025 x_unfocus_frame (f)
9026 struct frame *f;
9028 #if 0
9029 /* Look at the remarks in x_focus_on_frame. */
9030 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
9031 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
9032 RevertToPointerRoot, CurrentTime);
9033 #endif /* ! 0 */
9036 /* Raise frame F. */
9038 void
9039 x_raise_frame (f)
9040 struct frame *f;
9042 BLOCK_INPUT;
9043 if (f->async_visible)
9044 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9046 XFlush (FRAME_X_DISPLAY (f));
9047 UNBLOCK_INPUT;
9050 /* Lower frame F. */
9052 void
9053 x_lower_frame (f)
9054 struct frame *f;
9056 if (f->async_visible)
9058 BLOCK_INPUT;
9059 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9060 XFlush (FRAME_X_DISPLAY (f));
9061 UNBLOCK_INPUT;
9065 /* Activate frame with Extended Window Manager Hints */
9067 void
9068 x_ewmh_activate_frame (f)
9069 FRAME_PTR f;
9071 /* See Window Manager Specification/Extended Window Manager Hints at
9072 http://freedesktop.org/wiki/Specifications/wm-spec */
9074 const char *atom = "_NET_ACTIVE_WINDOW";
9075 if (f->async_visible && wm_supports (f, atom))
9077 Lisp_Object frame;
9078 XSETFRAME (frame, f);
9079 Fx_send_client_event (frame, make_number (0), frame,
9080 make_unibyte_string (atom, strlen (atom)),
9081 make_number (32),
9082 Fcons (make_number (1),
9083 Fcons (make_number (last_user_time),
9084 Qnil)));
9088 static void
9089 XTframe_raise_lower (f, raise_flag)
9090 FRAME_PTR f;
9091 int raise_flag;
9093 if (raise_flag)
9094 x_raise_frame (f);
9095 else
9096 x_lower_frame (f);
9099 /* XEmbed implementation. */
9101 void
9102 xembed_set_info (f, flags)
9103 struct frame *f;
9104 enum xembed_info flags;
9106 Atom atom;
9107 unsigned long data[2];
9109 atom = XInternAtom (FRAME_X_DISPLAY (f), "_XEMBED_INFO", False);
9111 data[0] = XEMBED_VERSION;
9112 data[1] = flags;
9114 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), atom, atom,
9115 32, PropModeReplace, (unsigned char *) data, 2);
9118 void
9119 xembed_send_message (f, time, message, detail, data1, data2)
9120 struct frame *f;
9121 Time time;
9122 enum xembed_message message;
9123 long detail;
9124 long data1;
9125 long data2;
9127 XEvent event;
9129 event.xclient.type = ClientMessage;
9130 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9131 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
9132 event.xclient.format = 32;
9133 event.xclient.data.l[0] = time;
9134 event.xclient.data.l[1] = message;
9135 event.xclient.data.l[2] = detail;
9136 event.xclient.data.l[3] = data1;
9137 event.xclient.data.l[4] = data2;
9139 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9140 False, NoEventMask, &event);
9141 XSync (FRAME_X_DISPLAY (f), False);
9144 /* Change of visibility. */
9146 /* This tries to wait until the frame is really visible.
9147 However, if the window manager asks the user where to position
9148 the frame, this will return before the user finishes doing that.
9149 The frame will not actually be visible at that time,
9150 but it will become visible later when the window manager
9151 finishes with it. */
9153 void
9154 x_make_frame_visible (f)
9155 struct frame *f;
9157 Lisp_Object type;
9158 int original_top, original_left;
9159 int retry_count = 2;
9161 retry:
9163 BLOCK_INPUT;
9165 type = x_icon_type (f);
9166 if (!NILP (type))
9167 x_bitmap_icon (f, type);
9169 if (! FRAME_VISIBLE_P (f))
9171 /* We test FRAME_GARBAGED_P here to make sure we don't
9172 call x_set_offset a second time
9173 if we get to x_make_frame_visible a second time
9174 before the window gets really visible. */
9175 if (! FRAME_ICONIFIED_P (f)
9176 && ! FRAME_X_EMBEDDED_P (f)
9177 && ! f->output_data.x->asked_for_visible)
9178 x_set_offset (f, f->left_pos, f->top_pos, 0);
9180 f->output_data.x->asked_for_visible = 1;
9182 if (! EQ (Vx_no_window_manager, Qt))
9183 x_wm_set_window_state (f, NormalState);
9184 #ifdef USE_X_TOOLKIT
9185 if (FRAME_X_EMBEDDED_P (f))
9186 xembed_set_info (f, XEMBED_MAPPED);
9187 else
9189 /* This was XtPopup, but that did nothing for an iconified frame. */
9190 XtMapWidget (f->output_data.x->widget);
9192 #else /* not USE_X_TOOLKIT */
9193 #ifdef USE_GTK
9194 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9195 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9196 #else
9197 if (FRAME_X_EMBEDDED_P (f))
9198 xembed_set_info (f, XEMBED_MAPPED);
9199 else
9200 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9201 #endif /* not USE_GTK */
9202 #endif /* not USE_X_TOOLKIT */
9205 XFlush (FRAME_X_DISPLAY (f));
9207 /* Synchronize to ensure Emacs knows the frame is visible
9208 before we do anything else. We do this loop with input not blocked
9209 so that incoming events are handled. */
9211 Lisp_Object frame;
9212 int count;
9213 /* This must be before UNBLOCK_INPUT
9214 since events that arrive in response to the actions above
9215 will set it when they are handled. */
9216 int previously_visible = f->output_data.x->has_been_visible;
9218 original_left = f->left_pos;
9219 original_top = f->top_pos;
9221 /* This must come after we set COUNT. */
9222 UNBLOCK_INPUT;
9224 /* We unblock here so that arriving X events are processed. */
9226 /* Now move the window back to where it was "supposed to be".
9227 But don't do it if the gravity is negative.
9228 When the gravity is negative, this uses a position
9229 that is 3 pixels too low. Perhaps that's really the border width.
9231 Don't do this if the window has never been visible before,
9232 because the window manager may choose the position
9233 and we don't want to override it. */
9235 if (! FRAME_VISIBLE_P (f)
9236 && ! FRAME_ICONIFIED_P (f)
9237 && ! FRAME_X_EMBEDDED_P (f)
9238 && f->win_gravity == NorthWestGravity
9239 && previously_visible)
9241 Drawable rootw;
9242 int x, y;
9243 unsigned int width, height, border, depth;
9245 BLOCK_INPUT;
9247 /* On some window managers (such as FVWM) moving an existing
9248 window, even to the same place, causes the window manager
9249 to introduce an offset. This can cause the window to move
9250 to an unexpected location. Check the geometry (a little
9251 slow here) and then verify that the window is in the right
9252 place. If the window is not in the right place, move it
9253 there, and take the potential window manager hit. */
9254 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9255 &rootw, &x, &y, &width, &height, &border, &depth);
9257 if (original_left != x || original_top != y)
9258 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9259 original_left, original_top);
9261 UNBLOCK_INPUT;
9264 XSETFRAME (frame, f);
9266 /* Wait until the frame is visible. Process X events until a
9267 MapNotify event has been seen, or until we think we won't get a
9268 MapNotify at all.. */
9269 for (count = input_signal_count + 10;
9270 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9272 /* Force processing of queued events. */
9273 x_sync (f);
9275 /* Machines that do polling rather than SIGIO have been
9276 observed to go into a busy-wait here. So we'll fake an
9277 alarm signal to let the handler know that there's something
9278 to be read. We used to raise a real alarm, but it seems
9279 that the handler isn't always enabled here. This is
9280 probably a bug. */
9281 if (input_polling_used ())
9283 /* It could be confusing if a real alarm arrives while
9284 processing the fake one. Turn it off and let the
9285 handler reset it. */
9286 extern void poll_for_input_1 P_ ((void));
9287 int old_poll_suppress_count = poll_suppress_count;
9288 poll_suppress_count = 1;
9289 poll_for_input_1 ();
9290 poll_suppress_count = old_poll_suppress_count;
9293 /* See if a MapNotify event has been processed. */
9294 FRAME_SAMPLE_VISIBILITY (f);
9297 /* 2000-09-28: In
9299 (let ((f (selected-frame)))
9300 (iconify-frame f)
9301 (raise-frame f))
9303 the frame is not raised with various window managers on
9304 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9305 unknown reason, the call to XtMapWidget is completely ignored.
9306 Mapping the widget a second time works. */
9308 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9309 goto retry;
9313 /* Change from mapped state to withdrawn state. */
9315 /* Make the frame visible (mapped and not iconified). */
9317 void
9318 x_make_frame_invisible (f)
9319 struct frame *f;
9321 Window window;
9323 /* Use the frame's outermost window, not the one we normally draw on. */
9324 window = FRAME_OUTER_WINDOW (f);
9326 /* Don't keep the highlight on an invisible frame. */
9327 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9328 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9330 BLOCK_INPUT;
9332 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9333 that the current position of the window is user-specified, rather than
9334 program-specified, so that when the window is mapped again, it will be
9335 placed at the same location, without forcing the user to position it
9336 by hand again (they have already done that once for this window.) */
9337 x_wm_set_size_hint (f, (long) 0, 1);
9339 #ifdef USE_GTK
9340 if (FRAME_GTK_OUTER_WIDGET (f))
9341 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9342 else
9343 #else
9344 if (FRAME_X_EMBEDDED_P (f))
9345 xembed_set_info (f, 0);
9346 else
9347 #endif
9350 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9351 DefaultScreen (FRAME_X_DISPLAY (f))))
9353 UNBLOCK_INPUT_RESIGNAL;
9354 error ("Can't notify window manager of window withdrawal");
9358 /* We can't distinguish this from iconification
9359 just by the event that we get from the server.
9360 So we can't win using the usual strategy of letting
9361 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9362 and synchronize with the server to make sure we agree. */
9363 f->visible = 0;
9364 FRAME_ICONIFIED_P (f) = 0;
9365 f->async_visible = 0;
9366 f->async_iconified = 0;
9368 x_sync (f);
9370 UNBLOCK_INPUT;
9373 /* Change window state from mapped to iconified. */
9375 void
9376 x_iconify_frame (f)
9377 struct frame *f;
9379 int result;
9380 Lisp_Object type;
9382 /* Don't keep the highlight on an invisible frame. */
9383 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9384 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9386 if (f->async_iconified)
9387 return;
9389 BLOCK_INPUT;
9391 FRAME_SAMPLE_VISIBILITY (f);
9393 type = x_icon_type (f);
9394 if (!NILP (type))
9395 x_bitmap_icon (f, type);
9397 #ifdef USE_GTK
9398 if (FRAME_GTK_OUTER_WIDGET (f))
9400 if (! FRAME_VISIBLE_P (f))
9401 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9403 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9404 f->iconified = 1;
9405 f->visible = 1;
9406 f->async_iconified = 1;
9407 f->async_visible = 0;
9408 UNBLOCK_INPUT;
9409 return;
9411 #endif
9413 #ifdef USE_X_TOOLKIT
9415 if (! FRAME_VISIBLE_P (f))
9417 if (! EQ (Vx_no_window_manager, Qt))
9418 x_wm_set_window_state (f, IconicState);
9419 /* This was XtPopup, but that did nothing for an iconified frame. */
9420 XtMapWidget (f->output_data.x->widget);
9421 /* The server won't give us any event to indicate
9422 that an invisible frame was changed to an icon,
9423 so we have to record it here. */
9424 f->iconified = 1;
9425 f->visible = 1;
9426 f->async_iconified = 1;
9427 f->async_visible = 0;
9428 UNBLOCK_INPUT;
9429 return;
9432 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9433 XtWindow (f->output_data.x->widget),
9434 DefaultScreen (FRAME_X_DISPLAY (f)));
9435 UNBLOCK_INPUT;
9437 if (!result)
9438 error ("Can't notify window manager of iconification");
9440 f->async_iconified = 1;
9441 f->async_visible = 0;
9444 BLOCK_INPUT;
9445 XFlush (FRAME_X_DISPLAY (f));
9446 UNBLOCK_INPUT;
9447 #else /* not USE_X_TOOLKIT */
9449 /* Make sure the X server knows where the window should be positioned,
9450 in case the user deiconifies with the window manager. */
9451 if (! FRAME_VISIBLE_P (f)
9452 && ! FRAME_ICONIFIED_P (f)
9453 && ! FRAME_X_EMBEDDED_P (f))
9454 x_set_offset (f, f->left_pos, f->top_pos, 0);
9456 /* Since we don't know which revision of X we're running, we'll use both
9457 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9459 /* X11R4: send a ClientMessage to the window manager using the
9460 WM_CHANGE_STATE type. */
9462 XEvent message;
9464 message.xclient.window = FRAME_X_WINDOW (f);
9465 message.xclient.type = ClientMessage;
9466 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9467 message.xclient.format = 32;
9468 message.xclient.data.l[0] = IconicState;
9470 if (! XSendEvent (FRAME_X_DISPLAY (f),
9471 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9472 False,
9473 SubstructureRedirectMask | SubstructureNotifyMask,
9474 &message))
9476 UNBLOCK_INPUT_RESIGNAL;
9477 error ("Can't notify window manager of iconification");
9481 /* X11R3: set the initial_state field of the window manager hints to
9482 IconicState. */
9483 x_wm_set_window_state (f, IconicState);
9485 if (!FRAME_VISIBLE_P (f))
9487 /* If the frame was withdrawn, before, we must map it. */
9488 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9491 f->async_iconified = 1;
9492 f->async_visible = 0;
9494 XFlush (FRAME_X_DISPLAY (f));
9495 UNBLOCK_INPUT;
9496 #endif /* not USE_X_TOOLKIT */
9500 /* Free X resources of frame F. */
9502 void
9503 x_free_frame_resources (f)
9504 struct frame *f;
9506 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9507 Lisp_Object bar;
9508 struct scroll_bar *b;
9510 BLOCK_INPUT;
9512 /* If a display connection is dead, don't try sending more
9513 commands to the X server. */
9514 if (dpyinfo->display)
9516 /* We must free faces before destroying windows because some
9517 font-driver (e.g. xft) access a window while finishing a
9518 face. */
9519 if (FRAME_FACE_CACHE (f))
9520 free_frame_faces (f);
9522 if (f->output_data.x->icon_desc)
9523 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9525 #ifdef USE_X_TOOLKIT
9526 /* Explicitly destroy the scroll bars of the frame. Without
9527 this, we get "BadDrawable" errors from the toolkit later on,
9528 presumably from expose events generated for the disappearing
9529 toolkit scroll bars. */
9530 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9532 b = XSCROLL_BAR (bar);
9533 x_scroll_bar_remove (b);
9535 #endif
9537 #ifdef HAVE_X_I18N
9538 if (FRAME_XIC (f))
9539 free_frame_xic (f);
9540 #endif
9542 #ifdef USE_X_TOOLKIT
9543 if (f->output_data.x->widget)
9545 XtDestroyWidget (f->output_data.x->widget);
9546 f->output_data.x->widget = NULL;
9548 /* Tooltips don't have widgets, only a simple X window, even if
9549 we are using a toolkit. */
9550 else if (FRAME_X_WINDOW (f))
9551 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9553 free_frame_menubar (f);
9554 #else /* !USE_X_TOOLKIT */
9556 #ifdef USE_GTK
9557 /* In the GTK version, tooltips are normal X
9558 frames. We must check and free both types. */
9559 if (FRAME_GTK_OUTER_WIDGET (f))
9561 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9562 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9563 FRAME_GTK_OUTER_WIDGET (f) = 0;
9565 #endif /* USE_GTK */
9567 if (FRAME_X_WINDOW (f))
9568 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9569 #endif /* !USE_X_TOOLKIT */
9571 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9572 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9573 unload_color (f, f->output_data.x->cursor_pixel);
9574 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9575 unload_color (f, f->output_data.x->border_pixel);
9576 unload_color (f, f->output_data.x->mouse_pixel);
9578 if (f->output_data.x->scroll_bar_background_pixel != -1)
9579 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9580 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9581 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9582 #ifdef USE_TOOLKIT_SCROLL_BARS
9583 /* Scrollbar shadow colors. */
9584 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9585 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9586 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9587 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9588 #endif /* USE_TOOLKIT_SCROLL_BARS */
9589 if (f->output_data.x->white_relief.allocated_p)
9590 unload_color (f, f->output_data.x->white_relief.pixel);
9591 if (f->output_data.x->black_relief.allocated_p)
9592 unload_color (f, f->output_data.x->black_relief.pixel);
9594 x_free_gcs (f);
9595 XFlush (FRAME_X_DISPLAY (f));
9598 xfree (f->output_data.x->saved_menu_event);
9599 xfree (f->output_data.x);
9600 f->output_data.x = NULL;
9602 if (f == dpyinfo->x_focus_frame)
9603 dpyinfo->x_focus_frame = 0;
9604 if (f == dpyinfo->x_focus_event_frame)
9605 dpyinfo->x_focus_event_frame = 0;
9606 if (f == dpyinfo->x_highlight_frame)
9607 dpyinfo->x_highlight_frame = 0;
9609 if (f == dpyinfo->mouse_face_mouse_frame)
9611 dpyinfo->mouse_face_beg_row
9612 = dpyinfo->mouse_face_beg_col = -1;
9613 dpyinfo->mouse_face_end_row
9614 = dpyinfo->mouse_face_end_col = -1;
9615 dpyinfo->mouse_face_window = Qnil;
9616 dpyinfo->mouse_face_deferred_gc = 0;
9617 dpyinfo->mouse_face_mouse_frame = 0;
9620 UNBLOCK_INPUT;
9624 /* Destroy the X window of frame F. */
9626 void
9627 x_destroy_window (f)
9628 struct frame *f;
9630 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9632 /* If a display connection is dead, don't try sending more
9633 commands to the X server. */
9634 if (dpyinfo->display != 0)
9635 x_free_frame_resources (f);
9637 dpyinfo->reference_count--;
9641 /* Setting window manager hints. */
9643 /* Set the normal size hints for the window manager, for frame F.
9644 FLAGS is the flags word to use--or 0 meaning preserve the flags
9645 that the window now has.
9646 If USER_POSITION is nonzero, we set the USPosition
9647 flag (this is useful when FLAGS is 0).
9648 The GTK version is in gtkutils.c */
9650 #ifndef USE_GTK
9651 void
9652 x_wm_set_size_hint (f, flags, user_position)
9653 struct frame *f;
9654 long flags;
9655 int user_position;
9657 XSizeHints size_hints;
9658 Window window = FRAME_OUTER_WINDOW (f);
9660 /* Setting PMaxSize caused various problems. */
9661 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9663 size_hints.x = f->left_pos;
9664 size_hints.y = f->top_pos;
9666 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9667 size_hints.width = FRAME_PIXEL_WIDTH (f);
9669 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9670 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9671 size_hints.max_width = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
9672 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9673 size_hints.max_height = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
9674 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9676 /* Calculate the base and minimum sizes. */
9678 int base_width, base_height;
9679 int min_rows = 0, min_cols = 0;
9681 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9682 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9684 check_frame_size (f, &min_rows, &min_cols);
9686 /* The window manager uses the base width hints to calculate the
9687 current number of rows and columns in the frame while
9688 resizing; min_width and min_height aren't useful for this
9689 purpose, since they might not give the dimensions for a
9690 zero-row, zero-column frame.
9692 We use the base_width and base_height members if we have
9693 them; otherwise, we set the min_width and min_height members
9694 to the size for a zero x zero frame. */
9696 size_hints.flags |= PBaseSize;
9697 size_hints.base_width = base_width;
9698 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
9699 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9700 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9703 /* If we don't need the old flags, we don't need the old hint at all. */
9704 if (flags)
9706 size_hints.flags |= flags;
9707 goto no_read;
9711 XSizeHints hints; /* Sometimes I hate X Windows... */
9712 long supplied_return;
9713 int value;
9715 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9716 &supplied_return);
9718 if (flags)
9719 size_hints.flags |= flags;
9720 else
9722 if (value == 0)
9723 hints.flags = 0;
9724 if (hints.flags & PSize)
9725 size_hints.flags |= PSize;
9726 if (hints.flags & PPosition)
9727 size_hints.flags |= PPosition;
9728 if (hints.flags & USPosition)
9729 size_hints.flags |= USPosition;
9730 if (hints.flags & USSize)
9731 size_hints.flags |= USSize;
9735 no_read:
9737 #ifdef PWinGravity
9738 size_hints.win_gravity = f->win_gravity;
9739 size_hints.flags |= PWinGravity;
9741 if (user_position)
9743 size_hints.flags &= ~ PPosition;
9744 size_hints.flags |= USPosition;
9746 #endif /* PWinGravity */
9748 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9750 #endif /* not USE_GTK */
9752 /* Used for IconicState or NormalState */
9754 void
9755 x_wm_set_window_state (f, state)
9756 struct frame *f;
9757 int state;
9759 #ifdef USE_X_TOOLKIT
9760 Arg al[1];
9762 XtSetArg (al[0], XtNinitialState, state);
9763 XtSetValues (f->output_data.x->widget, al, 1);
9764 #else /* not USE_X_TOOLKIT */
9765 Window window = FRAME_X_WINDOW (f);
9767 f->output_data.x->wm_hints.flags |= StateHint;
9768 f->output_data.x->wm_hints.initial_state = state;
9770 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9771 #endif /* not USE_X_TOOLKIT */
9774 void
9775 x_wm_set_icon_pixmap (f, pixmap_id)
9776 struct frame *f;
9777 int pixmap_id;
9779 Pixmap icon_pixmap, icon_mask;
9781 #ifndef USE_X_TOOLKIT
9782 Window window = FRAME_OUTER_WINDOW (f);
9783 #endif
9785 if (pixmap_id > 0)
9787 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9788 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9789 icon_mask = x_bitmap_mask (f, pixmap_id);
9790 f->output_data.x->wm_hints.icon_mask = icon_mask;
9792 else
9794 /* It seems there is no way to turn off use of an icon
9795 pixmap. */
9796 return;
9800 #ifdef USE_GTK
9802 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9803 return;
9806 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9809 Arg al[1];
9810 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9811 XtSetValues (f->output_data.x->widget, al, 1);
9812 XtSetArg (al[0], XtNiconMask, icon_mask);
9813 XtSetValues (f->output_data.x->widget, al, 1);
9816 #else /* not USE_X_TOOLKIT && not USE_GTK */
9818 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9819 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9821 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9824 void
9825 x_wm_set_icon_position (f, icon_x, icon_y)
9826 struct frame *f;
9827 int icon_x, icon_y;
9829 Window window = FRAME_OUTER_WINDOW (f);
9831 f->output_data.x->wm_hints.flags |= IconPositionHint;
9832 f->output_data.x->wm_hints.icon_x = icon_x;
9833 f->output_data.x->wm_hints.icon_y = icon_y;
9835 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9839 /***********************************************************************
9840 Fonts
9841 ***********************************************************************/
9843 #if GLYPH_DEBUG
9845 /* Check that FONT is valid on frame F. It is if it can be found in F's
9846 font table. */
9848 static void
9849 x_check_font (f, font)
9850 struct frame *f;
9851 struct font *font;
9853 Lisp_Object frame;
9855 xassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9856 if (font->driver->check)
9857 xassert (font->driver->check (f, font) == 0);
9860 #endif /* GLYPH_DEBUG != 0 */
9863 /***********************************************************************
9864 Initialization
9865 ***********************************************************************/
9867 #ifdef USE_X_TOOLKIT
9868 static XrmOptionDescRec emacs_options[] = {
9869 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9870 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9872 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9873 XrmoptionSepArg, NULL},
9874 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9876 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9877 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9878 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9879 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9880 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9881 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9882 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9885 /* Whether atimer for Xt timeouts is activated or not. */
9887 static int x_timeout_atimer_activated_flag;
9889 #endif /* USE_X_TOOLKIT */
9891 static int x_initialized;
9893 #ifdef HAVE_X_SM
9894 static int x_session_initialized;
9895 #endif
9897 /* Test whether two display-name strings agree up to the dot that separates
9898 the screen number from the server number. */
9899 static int
9900 same_x_server (name1, name2)
9901 const char *name1, *name2;
9903 int seen_colon = 0;
9904 const unsigned char *system_name = SDATA (Vsystem_name);
9905 int system_name_length = strlen (system_name);
9906 int length_until_period = 0;
9908 while (system_name[length_until_period] != 0
9909 && system_name[length_until_period] != '.')
9910 length_until_period++;
9912 /* Treat `unix' like an empty host name. */
9913 if (! strncmp (name1, "unix:", 5))
9914 name1 += 4;
9915 if (! strncmp (name2, "unix:", 5))
9916 name2 += 4;
9917 /* Treat this host's name like an empty host name. */
9918 if (! strncmp (name1, system_name, system_name_length)
9919 && name1[system_name_length] == ':')
9920 name1 += system_name_length;
9921 if (! strncmp (name2, system_name, system_name_length)
9922 && name2[system_name_length] == ':')
9923 name2 += system_name_length;
9924 /* Treat this host's domainless name like an empty host name. */
9925 if (! strncmp (name1, system_name, length_until_period)
9926 && name1[length_until_period] == ':')
9927 name1 += length_until_period;
9928 if (! strncmp (name2, system_name, length_until_period)
9929 && name2[length_until_period] == ':')
9930 name2 += length_until_period;
9932 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
9934 if (*name1 == ':')
9935 seen_colon++;
9936 if (seen_colon && *name1 == '.')
9937 return 1;
9939 return (seen_colon
9940 && (*name1 == '.' || *name1 == '\0')
9941 && (*name2 == '.' || *name2 == '\0'));
9944 /* Count number of set bits in mask and number of bits to shift to
9945 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
9946 to 5. */
9947 static void
9948 get_bits_and_offset (mask, bits, offset)
9949 unsigned long mask;
9950 int *bits;
9951 int *offset;
9953 int nr = 0;
9954 int off = 0;
9956 while (!(mask & 1))
9958 off++;
9959 mask >>= 1;
9962 while (mask & 1)
9964 nr++;
9965 mask >>= 1;
9968 *offset = off;
9969 *bits = nr;
9972 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
9973 But don't permanently open it, just test its availability. */
9976 x_display_ok (display)
9977 const char *display;
9979 int dpy_ok = 1;
9980 Display *dpy;
9982 dpy = XOpenDisplay (display);
9983 if (dpy)
9984 XCloseDisplay (dpy);
9985 else
9986 dpy_ok = 0;
9987 return dpy_ok;
9990 /* Open a connection to X display DISPLAY_NAME, and return
9991 the structure that describes the open display.
9992 If we cannot contact the display, return null. */
9994 struct x_display_info *
9995 x_term_init (display_name, xrm_option, resource_name)
9996 Lisp_Object display_name;
9997 char *xrm_option;
9998 char *resource_name;
10000 int connection;
10001 Display *dpy;
10002 struct terminal *terminal;
10003 struct x_display_info *dpyinfo;
10004 XrmDatabase xrdb;
10006 BLOCK_INPUT;
10008 if (!x_initialized)
10010 x_initialize ();
10011 ++x_initialized;
10014 if (! x_display_ok (SDATA (display_name)))
10015 error ("Display %s can't be opened", SDATA (display_name));
10017 #ifdef USE_GTK
10019 #define NUM_ARGV 10
10020 int argc;
10021 char *argv[NUM_ARGV];
10022 char **argv2 = argv;
10023 GdkAtom atom;
10025 #ifndef HAVE_GTK_MULTIDISPLAY
10026 if (!EQ (Vinitial_window_system, Qx))
10027 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10028 #endif
10030 if (x_initialized++ > 1)
10032 #ifdef HAVE_GTK_MULTIDISPLAY
10033 /* Opening another display. If xg_display_open returns less
10034 than zero, we are probably on GTK 2.0, which can only handle
10035 one display. GTK 2.2 or later can handle more than one. */
10036 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10037 #endif
10038 error ("Sorry, this version of GTK can only handle one display");
10040 else
10042 for (argc = 0; argc < NUM_ARGV; ++argc)
10043 argv[argc] = 0;
10045 argc = 0;
10046 argv[argc++] = initial_argv[0];
10048 if (! NILP (display_name))
10050 argv[argc++] = "--display";
10051 argv[argc++] = SDATA (display_name);
10054 argv[argc++] = "--name";
10055 argv[argc++] = resource_name;
10057 XSetLocaleModifiers ("");
10059 gtk_init (&argc, &argv2);
10061 /* gtk_init does set_locale. We must fix locale after calling it. */
10062 fixup_locale ();
10063 xg_initialize ();
10065 dpy = GDK_DISPLAY ();
10067 /* NULL window -> events for all windows go to our function */
10068 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10070 /* Load our own gtkrc if it exists. */
10072 char *file = "~/.emacs.d/gtkrc";
10073 Lisp_Object s, abs_file;
10075 s = make_string (file, strlen (file));
10076 abs_file = Fexpand_file_name (s, Qnil);
10078 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10079 gtk_rc_parse (SDATA (abs_file));
10082 XSetErrorHandler (x_error_handler);
10083 XSetIOErrorHandler (x_io_error_quitter);
10086 #else /* not USE_GTK */
10087 #ifdef USE_X_TOOLKIT
10088 /* weiner@footloose.sps.mot.com reports that this causes
10089 errors with X11R5:
10090 X protocol error: BadAtom (invalid Atom parameter)
10091 on protocol request 18skiloaf.
10092 So let's not use it until R6. */
10093 #ifdef HAVE_X11XTR6
10094 XtSetLanguageProc (NULL, NULL, NULL);
10095 #endif
10098 int argc = 0;
10099 char *argv[3];
10101 argv[0] = "";
10102 argc = 1;
10103 if (xrm_option)
10105 argv[argc++] = "-xrm";
10106 argv[argc++] = xrm_option;
10108 turn_on_atimers (0);
10109 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10110 resource_name, EMACS_CLASS,
10111 emacs_options, XtNumber (emacs_options),
10112 &argc, argv);
10113 turn_on_atimers (1);
10115 #ifdef HAVE_X11XTR6
10116 /* I think this is to compensate for XtSetLanguageProc. */
10117 fixup_locale ();
10118 #endif
10121 #else /* not USE_X_TOOLKIT */
10122 XSetLocaleModifiers ("");
10123 dpy = XOpenDisplay (SDATA (display_name));
10124 #endif /* not USE_X_TOOLKIT */
10125 #endif /* not USE_GTK*/
10127 /* Detect failure. */
10128 if (dpy == 0)
10130 UNBLOCK_INPUT;
10131 return 0;
10134 /* We have definitely succeeded. Record the new connection. */
10136 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10137 bzero (dpyinfo, sizeof *dpyinfo);
10139 terminal = x_create_terminal (dpyinfo);
10142 struct x_display_info *share;
10143 Lisp_Object tail;
10145 for (share = x_display_list, tail = x_display_name_list; share;
10146 share = share->next, tail = XCDR (tail))
10147 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10148 SDATA (display_name)))
10149 break;
10150 if (share)
10151 terminal->kboard = share->terminal->kboard;
10152 else
10154 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10155 init_kboard (terminal->kboard);
10156 terminal->kboard->Vwindow_system = Qx;
10157 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10159 char *vendor = ServerVendor (dpy);
10160 /* Temporarily hide the partially initialized terminal */
10161 terminal_list = terminal->next_terminal;
10162 UNBLOCK_INPUT;
10163 terminal->kboard->Vsystem_key_alist
10164 = call1 (Qvendor_specific_keysyms,
10165 vendor ? build_string (vendor) : empty_unibyte_string);
10166 BLOCK_INPUT;
10167 terminal->next_terminal = terminal_list;
10168 terminal_list = terminal;
10171 terminal->kboard->next_kboard = all_kboards;
10172 all_kboards = terminal->kboard;
10173 /* Don't let the initial kboard remain current longer than necessary.
10174 That would cause problems if a file loaded on startup tries to
10175 prompt in the mini-buffer. */
10176 if (current_kboard == initial_kboard)
10177 current_kboard = terminal->kboard;
10179 terminal->kboard->reference_count++;
10182 /* Put this display on the chain. */
10183 dpyinfo->next = x_display_list;
10184 x_display_list = dpyinfo;
10186 /* Put it on x_display_name_list as well, to keep them parallel. */
10187 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10188 x_display_name_list);
10189 dpyinfo->name_list_element = XCAR (x_display_name_list);
10191 dpyinfo->display = dpy;
10193 /* Set the name of the terminal. */
10194 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10195 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10196 terminal->name[SBYTES (display_name)] = 0;
10198 #if 0
10199 XSetAfterFunction (x_current_display, x_trace_wire);
10200 #endif /* ! 0 */
10202 dpyinfo->x_id_name
10203 = (char *) xmalloc (SBYTES (Vinvocation_name)
10204 + SBYTES (Vsystem_name)
10205 + 2);
10206 sprintf (dpyinfo->x_id_name, "%s@%s",
10207 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10209 /* Figure out which modifier bits mean what. */
10210 x_find_modifier_meanings (dpyinfo);
10212 /* Get the scroll bar cursor. */
10213 #ifdef USE_GTK
10214 /* We must create a GTK cursor, it is required for GTK widgets. */
10215 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10216 #endif /* USE_GTK */
10218 dpyinfo->vertical_scroll_bar_cursor
10219 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10221 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10222 resource_name, EMACS_CLASS);
10223 #ifdef HAVE_XRMSETDATABASE
10224 XrmSetDatabase (dpyinfo->display, xrdb);
10225 #else
10226 dpyinfo->display->db = xrdb;
10227 #endif
10228 /* Put the rdb where we can find it in a way that works on
10229 all versions. */
10230 dpyinfo->xrdb = xrdb;
10232 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10233 DefaultScreen (dpyinfo->display));
10234 select_visual (dpyinfo);
10235 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10236 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10237 dpyinfo->client_leader_window = 0;
10238 dpyinfo->grabbed = 0;
10239 dpyinfo->reference_count = 0;
10240 dpyinfo->icon_bitmap_id = -1;
10241 dpyinfo->n_fonts = 0;
10242 dpyinfo->bitmaps = 0;
10243 dpyinfo->bitmaps_size = 0;
10244 dpyinfo->bitmaps_last = 0;
10245 dpyinfo->scratch_cursor_gc = 0;
10246 dpyinfo->mouse_face_mouse_frame = 0;
10247 dpyinfo->mouse_face_deferred_gc = 0;
10248 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10249 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10250 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10251 dpyinfo->mouse_face_window = Qnil;
10252 dpyinfo->mouse_face_overlay = Qnil;
10253 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10254 dpyinfo->mouse_face_defer = 0;
10255 dpyinfo->mouse_face_hidden = 0;
10256 dpyinfo->x_focus_frame = 0;
10257 dpyinfo->x_focus_event_frame = 0;
10258 dpyinfo->x_highlight_frame = 0;
10259 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10261 /* See if we can construct pixel values from RGB values. */
10262 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10263 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10265 if (dpyinfo->visual->class == TrueColor)
10267 get_bits_and_offset (dpyinfo->visual->red_mask,
10268 &dpyinfo->red_bits, &dpyinfo->red_offset);
10269 get_bits_and_offset (dpyinfo->visual->blue_mask,
10270 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10271 get_bits_and_offset (dpyinfo->visual->green_mask,
10272 &dpyinfo->green_bits, &dpyinfo->green_offset);
10275 /* See if a private colormap is requested. */
10276 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10278 if (dpyinfo->visual->class == PseudoColor)
10280 Lisp_Object value;
10281 value = display_x_get_resource (dpyinfo,
10282 build_string ("privateColormap"),
10283 build_string ("PrivateColormap"),
10284 Qnil, Qnil);
10285 if (STRINGP (value)
10286 && (!strcmp (SDATA (value), "true")
10287 || !strcmp (SDATA (value), "on")))
10288 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10291 else
10292 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10293 dpyinfo->visual, AllocNone);
10296 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10297 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10298 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10299 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10300 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10301 pixels = DisplayWidth (dpyinfo->display, screen_number);
10302 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10303 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10304 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10307 dpyinfo->Xatom_wm_protocols
10308 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10309 dpyinfo->Xatom_wm_take_focus
10310 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10311 dpyinfo->Xatom_wm_save_yourself
10312 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10313 dpyinfo->Xatom_wm_delete_window
10314 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10315 dpyinfo->Xatom_wm_change_state
10316 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10317 dpyinfo->Xatom_wm_configure_denied
10318 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10319 dpyinfo->Xatom_wm_window_moved
10320 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10321 dpyinfo->Xatom_wm_client_leader
10322 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10323 dpyinfo->Xatom_editres
10324 = XInternAtom (dpyinfo->display, "Editres", False);
10325 dpyinfo->Xatom_CLIPBOARD
10326 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10327 dpyinfo->Xatom_TIMESTAMP
10328 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10329 dpyinfo->Xatom_TEXT
10330 = XInternAtom (dpyinfo->display, "TEXT", False);
10331 dpyinfo->Xatom_COMPOUND_TEXT
10332 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10333 dpyinfo->Xatom_UTF8_STRING
10334 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10335 dpyinfo->Xatom_DELETE
10336 = XInternAtom (dpyinfo->display, "DELETE", False);
10337 dpyinfo->Xatom_MULTIPLE
10338 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10339 dpyinfo->Xatom_INCR
10340 = XInternAtom (dpyinfo->display, "INCR", False);
10341 dpyinfo->Xatom_EMACS_TMP
10342 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10343 dpyinfo->Xatom_TARGETS
10344 = XInternAtom (dpyinfo->display, "TARGETS", False);
10345 dpyinfo->Xatom_NULL
10346 = XInternAtom (dpyinfo->display, "NULL", False);
10347 dpyinfo->Xatom_ATOM_PAIR
10348 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10349 /* For properties of font. */
10350 dpyinfo->Xatom_PIXEL_SIZE
10351 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10352 dpyinfo->Xatom_AVERAGE_WIDTH
10353 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10354 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10355 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10356 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10357 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10358 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10359 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10361 /* Ghostscript support. */
10362 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10363 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10365 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10366 False);
10368 dpyinfo->Xatom_XEMBED = XInternAtom (dpyinfo->display, "_XEMBED",
10369 False);
10371 dpyinfo->Xatom_net_wm_state
10372 = XInternAtom (dpyinfo->display, "_NET_WM_STATE", False);
10373 dpyinfo->Xatom_net_wm_state_fullscreen_atom
10374 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_FULLSCREEN", False);
10375 dpyinfo->Xatom_net_wm_state_maximized_horz
10376 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
10377 dpyinfo->Xatom_net_wm_state_maximized_vert
10378 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
10379 dpyinfo->Xatom_net_wm_state_sticky
10380 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_STICKY", False);
10382 dpyinfo->cut_buffers_initialized = 0;
10384 dpyinfo->x_dnd_atoms_size = 8;
10385 dpyinfo->x_dnd_atoms_length = 0;
10386 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10387 * dpyinfo->x_dnd_atoms_size);
10389 dpyinfo->net_supported_atoms = NULL;
10390 dpyinfo->nr_net_supported_atoms = 0;
10391 dpyinfo->net_supported_window = 0;
10393 connection = ConnectionNumber (dpyinfo->display);
10394 dpyinfo->connection = connection;
10397 extern int gray_bitmap_width, gray_bitmap_height;
10398 extern char *gray_bitmap_bits;
10399 dpyinfo->gray
10400 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10401 gray_bitmap_bits,
10402 gray_bitmap_width, gray_bitmap_height,
10403 (unsigned long) 1, (unsigned long) 0, 1);
10406 #ifdef HAVE_X_I18N
10407 xim_initialize (dpyinfo, resource_name);
10408 #endif
10410 #ifdef subprocesses
10411 /* This is only needed for distinguishing keyboard and process input. */
10412 if (connection != 0)
10413 add_keyboard_wait_descriptor (connection);
10414 #endif
10416 #ifdef F_SETOWN
10417 fcntl (connection, F_SETOWN, getpid ());
10418 #endif /* ! defined (F_SETOWN) */
10420 #ifdef SIGIO
10421 if (interrupt_input)
10422 init_sigio (connection);
10423 #endif /* ! defined (SIGIO) */
10425 #ifdef USE_LUCID
10427 Display *dpy = dpyinfo->display;
10428 XrmValue d, fr, to;
10429 Font font;
10431 d.addr = (XPointer)&dpy;
10432 d.size = sizeof (Display *);
10433 fr.addr = XtDefaultFont;
10434 fr.size = sizeof (XtDefaultFont);
10435 to.size = sizeof (Font *);
10436 to.addr = (XPointer)&font;
10437 x_catch_errors (dpy);
10438 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10439 abort ();
10440 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10441 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10442 x_uncatch_errors ();
10444 #endif
10446 /* See if we should run in synchronous mode. This is useful
10447 for debugging X code. */
10449 Lisp_Object value;
10450 value = display_x_get_resource (dpyinfo,
10451 build_string ("synchronous"),
10452 build_string ("Synchronous"),
10453 Qnil, Qnil);
10454 if (STRINGP (value)
10455 && (!strcmp (SDATA (value), "true")
10456 || !strcmp (SDATA (value), "on")))
10457 XSynchronize (dpyinfo->display, True);
10461 Lisp_Object value;
10462 value = display_x_get_resource (dpyinfo,
10463 build_string ("useXIM"),
10464 build_string ("UseXIM"),
10465 Qnil, Qnil);
10466 #ifdef USE_XIM
10467 if (STRINGP (value)
10468 && (!strcmp (SDATA (value), "false")
10469 || !strcmp (SDATA (value), "off")))
10470 use_xim = 0;
10471 #else
10472 if (STRINGP (value)
10473 && (!strcmp (SDATA (value), "true")
10474 || !strcmp (SDATA (value), "on")))
10475 use_xim = 1;
10476 #endif
10479 #ifdef HAVE_X_SM
10480 /* Only do this for the very first display in the Emacs session.
10481 Ignore X session management when Emacs was first started on a
10482 tty. */
10483 if (terminal->id == 1)
10484 x_session_initialize (dpyinfo);
10485 #endif
10487 UNBLOCK_INPUT;
10489 return dpyinfo;
10492 /* Get rid of display DPYINFO, deleting all frames on it,
10493 and without sending any more commands to the X server. */
10495 void
10496 x_delete_display (dpyinfo)
10497 struct x_display_info *dpyinfo;
10499 int i;
10500 struct terminal *t;
10502 /* Close all frames and delete the generic struct terminal for this
10503 X display. */
10504 for (t = terminal_list; t; t = t->next_terminal)
10505 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10507 #ifdef HAVE_X_SM
10508 /* Close X session management when we close its display. */
10509 if (t->id == 1 && x_session_have_connection ())
10510 x_session_close();
10511 #endif
10512 delete_terminal (t);
10513 break;
10516 delete_keyboard_wait_descriptor (dpyinfo->connection);
10518 /* Discard this display from x_display_name_list and x_display_list.
10519 We can't use Fdelq because that can quit. */
10520 if (! NILP (x_display_name_list)
10521 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10522 x_display_name_list = XCDR (x_display_name_list);
10523 else
10525 Lisp_Object tail;
10527 tail = x_display_name_list;
10528 while (CONSP (tail) && CONSP (XCDR (tail)))
10530 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10532 XSETCDR (tail, XCDR (XCDR (tail)));
10533 break;
10535 tail = XCDR (tail);
10539 if (next_noop_dpyinfo == dpyinfo)
10540 next_noop_dpyinfo = dpyinfo->next;
10542 if (x_display_list == dpyinfo)
10543 x_display_list = dpyinfo->next;
10544 else
10546 struct x_display_info *tail;
10548 for (tail = x_display_list; tail; tail = tail->next)
10549 if (tail->next == dpyinfo)
10550 tail->next = tail->next->next;
10553 xfree (dpyinfo->x_id_name);
10554 xfree (dpyinfo->x_dnd_atoms);
10555 xfree (dpyinfo->color_cells);
10556 xfree (dpyinfo);
10559 #ifdef USE_X_TOOLKIT
10561 /* Atimer callback function for TIMER. Called every 0.1s to process
10562 Xt timeouts, if needed. We must avoid calling XtAppPending as
10563 much as possible because that function does an implicit XFlush
10564 that slows us down. */
10566 static void
10567 x_process_timeouts (timer)
10568 struct atimer *timer;
10570 BLOCK_INPUT;
10571 x_timeout_atimer_activated_flag = 0;
10572 if (toolkit_scroll_bar_interaction || popup_activated ())
10574 while (XtAppPending (Xt_app_con) & XtIMTimer)
10575 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10576 /* Reactivate the atimer for next time. */
10577 x_activate_timeout_atimer ();
10579 UNBLOCK_INPUT;
10582 /* Install an asynchronous timer that processes Xt timeout events
10583 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10584 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10585 function whenever these variables are set. This is necessary
10586 because some widget sets use timeouts internally, for example the
10587 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10588 processed, these widgets don't behave normally. */
10590 void
10591 x_activate_timeout_atimer ()
10593 BLOCK_INPUT;
10594 if (!x_timeout_atimer_activated_flag)
10596 EMACS_TIME interval;
10598 EMACS_SET_SECS_USECS (interval, 0, 100000);
10599 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10600 x_timeout_atimer_activated_flag = 1;
10602 UNBLOCK_INPUT;
10605 #endif /* USE_X_TOOLKIT */
10608 /* Set up use of X before we make the first connection. */
10610 extern frame_parm_handler x_frame_parm_handlers[];
10612 static struct redisplay_interface x_redisplay_interface =
10614 x_frame_parm_handlers,
10615 x_produce_glyphs,
10616 x_write_glyphs,
10617 x_insert_glyphs,
10618 x_clear_end_of_line,
10619 x_scroll_run,
10620 x_after_update_window_line,
10621 x_update_window_begin,
10622 x_update_window_end,
10623 x_cursor_to,
10624 x_flush,
10625 #ifdef XFlush
10626 x_flush,
10627 #else
10628 0, /* flush_display_optional */
10629 #endif
10630 x_clear_window_mouse_face,
10631 x_get_glyph_overhangs,
10632 x_fix_overlapping_area,
10633 x_draw_fringe_bitmap,
10634 0, /* define_fringe_bitmap */
10635 0, /* destroy_fringe_bitmap */
10636 x_compute_glyph_string_overhangs,
10637 x_draw_glyph_string,
10638 x_define_frame_cursor,
10639 x_clear_frame_area,
10640 x_draw_window_cursor,
10641 x_draw_vertical_window_border,
10642 x_shift_glyphs_for_insert
10646 /* This function is called when the last frame on a display is deleted. */
10647 void
10648 x_delete_terminal (struct terminal *terminal)
10650 struct x_display_info *dpyinfo = terminal->display_info.x;
10651 int i;
10653 /* Protect against recursive calls. delete_frame in
10654 delete_terminal calls us back when it deletes our last frame. */
10655 if (!terminal->name)
10656 return;
10658 BLOCK_INPUT;
10659 #ifdef HAVE_X_I18N
10660 /* We must close our connection to the XIM server before closing the
10661 X display. */
10662 if (dpyinfo->xim)
10663 xim_close_dpy (dpyinfo);
10664 #endif
10666 /* If called from x_connection_closed, the display may already be closed
10667 and dpyinfo->display was set to 0 to indicate that. */
10668 if (dpyinfo->display)
10670 x_destroy_all_bitmaps (dpyinfo);
10671 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10673 /* Whether or not XCloseDisplay destroys the associated resource
10674 database depends on the version of libX11. To avoid both
10675 crash and memory leak, we dissociate the database from the
10676 display and then destroy dpyinfo->xrdb ourselves.
10678 Unfortunately, the above strategy does not work in some
10679 situations due to a bug in newer versions of libX11: because
10680 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
10681 dpy->db is NULL, XCloseDisplay destroys the associated
10682 database whereas it has not been created by XGetDefault
10683 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
10684 don't destroy the database here in order to avoid the crash
10685 in the above situations for now, though that may cause memory
10686 leaks in other situations. */
10687 #if 0
10688 #ifdef HAVE_XRMSETDATABASE
10689 XrmSetDatabase (dpyinfo->display, NULL);
10690 #else
10691 dpyinfo->display->db = NULL;
10692 #endif
10693 /* We used to call XrmDestroyDatabase from x_delete_display, but
10694 some older versions of libX11 crash if we call it after
10695 closing all the displays. */
10696 XrmDestroyDatabase (dpyinfo->xrdb);
10697 #endif
10699 #ifdef USE_GTK
10700 xg_display_close (dpyinfo->display);
10701 #else
10702 #ifdef USE_X_TOOLKIT
10703 XtCloseDisplay (dpyinfo->display);
10704 #else
10705 XCloseDisplay (dpyinfo->display);
10706 #endif
10707 #endif /* ! USE_GTK */
10710 x_delete_display (dpyinfo);
10711 UNBLOCK_INPUT;
10714 /* Create a struct terminal, initialize it with the X11 specific
10715 functions and make DISPLAY->TERMINAL point to it. */
10717 static struct terminal *
10718 x_create_terminal (struct x_display_info *dpyinfo)
10720 struct terminal *terminal;
10722 terminal = create_terminal ();
10724 terminal->type = output_x_window;
10725 terminal->display_info.x = dpyinfo;
10726 dpyinfo->terminal = terminal;
10728 /* kboard is initialized in x_term_init. */
10730 terminal->clear_frame_hook = x_clear_frame;
10731 terminal->ins_del_lines_hook = x_ins_del_lines;
10732 terminal->delete_glyphs_hook = x_delete_glyphs;
10733 terminal->ring_bell_hook = XTring_bell;
10734 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
10735 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
10736 terminal->set_terminal_modes_hook = XTset_terminal_modes;
10737 terminal->update_begin_hook = x_update_begin;
10738 terminal->update_end_hook = x_update_end;
10739 terminal->set_terminal_window_hook = XTset_terminal_window;
10740 terminal->read_socket_hook = XTread_socket;
10741 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10742 terminal->mouse_position_hook = XTmouse_position;
10743 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10744 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10745 terminal->fullscreen_hook = XTfullscreen_hook;
10746 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10747 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10748 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10749 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10751 terminal->delete_frame_hook = x_destroy_window;
10752 terminal->delete_terminal_hook = x_delete_terminal;
10754 terminal->rif = &x_redisplay_interface;
10755 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10756 terminal->char_ins_del_ok = 1;
10757 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10758 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10759 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10760 off the bottom. */
10762 return terminal;
10765 void
10766 x_initialize ()
10768 baud_rate = 19200;
10770 x_noop_count = 0;
10771 last_tool_bar_item = -1;
10772 any_help_event_p = 0;
10773 ignore_next_mouse_click_timeout = 0;
10774 #ifdef HAVE_X_SM
10775 x_session_initialized = 0;
10776 #endif
10778 #ifdef USE_GTK
10779 current_count = -1;
10780 #endif
10782 /* Try to use interrupt input; if we can't, then start polling. */
10783 Fset_input_interrupt_mode (Qt);
10785 #ifdef USE_X_TOOLKIT
10786 XtToolkitInitialize ();
10788 Xt_app_con = XtCreateApplicationContext ();
10790 /* Register a converter from strings to pixels, which uses
10791 Emacs' color allocation infrastructure. */
10792 XtAppSetTypeConverter (Xt_app_con,
10793 XtRString, XtRPixel, cvt_string_to_pixel,
10794 cvt_string_to_pixel_args,
10795 XtNumber (cvt_string_to_pixel_args),
10796 XtCacheByDisplay, cvt_pixel_dtor);
10798 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10799 #endif
10801 #ifdef USE_TOOLKIT_SCROLL_BARS
10802 #ifndef USE_GTK
10803 xaw3d_arrow_scroll = False;
10804 xaw3d_pick_top = True;
10805 #endif
10806 #endif
10808 pending_autoraise_frame = 0;
10809 pending_event_wait.f = 0;
10810 pending_event_wait.eventtype = 0;
10812 /* Note that there is no real way portable across R3/R4 to get the
10813 original error handler. */
10814 XSetErrorHandler (x_error_handler);
10815 XSetIOErrorHandler (x_io_error_quitter);
10817 signal (SIGPIPE, x_connection_signal);
10821 void
10822 syms_of_xterm ()
10824 x_error_message = NULL;
10826 staticpro (&x_display_name_list);
10827 x_display_name_list = Qnil;
10829 staticpro (&last_mouse_scroll_bar);
10830 last_mouse_scroll_bar = Qnil;
10832 staticpro (&Qvendor_specific_keysyms);
10833 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
10835 staticpro (&Qlatin_1);
10836 Qlatin_1 = intern ("latin-1");
10838 staticpro (&last_mouse_press_frame);
10839 last_mouse_press_frame = Qnil;
10841 #ifdef USE_GTK
10842 xg_default_icon_file = build_string ("icons/hicolor/scalable/apps/emacs.svg");
10843 staticpro (&xg_default_icon_file);
10845 Qx_gtk_map_stock = intern ("x-gtk-map-stock");
10846 staticpro (&Qx_gtk_map_stock);
10847 #endif
10849 DEFVAR_BOOL ("x-use-underline-position-properties",
10850 &x_use_underline_position_properties,
10851 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10852 A value of nil means ignore them. If you encounter fonts with bogus
10853 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10854 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10855 to override the font's UNDERLINE_POSITION for small font display
10856 sizes. */);
10857 x_use_underline_position_properties = 1;
10859 DEFVAR_BOOL ("x-underline-at-descent-line",
10860 &x_underline_at_descent_line,
10861 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
10862 A value of nil means to draw the underline according to the value of the
10863 variable `x-use-underline-position-properties', which is usually at the
10864 baseline level. The default value is nil. */);
10865 x_underline_at_descent_line = 0;
10867 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10868 &x_mouse_click_focus_ignore_position,
10869 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10870 This variable is only used when the window manager requires that you
10871 click on a frame to select it (give it focus). In that case, a value
10872 of nil, means that the selected window and cursor position changes to
10873 reflect the mouse click position, while a non-nil value means that the
10874 selected window or cursor position is preserved. */);
10875 x_mouse_click_focus_ignore_position = 0;
10877 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10878 doc: /* What X toolkit scroll bars Emacs uses.
10879 A value of nil means Emacs doesn't use X toolkit scroll bars.
10880 Otherwise, value is a symbol describing the X toolkit. */);
10881 #ifdef USE_TOOLKIT_SCROLL_BARS
10882 #ifdef USE_MOTIF
10883 Vx_toolkit_scroll_bars = intern ("motif");
10884 #elif defined HAVE_XAW3D
10885 Vx_toolkit_scroll_bars = intern ("xaw3d");
10886 #elif USE_GTK
10887 Vx_toolkit_scroll_bars = intern ("gtk");
10888 #else
10889 Vx_toolkit_scroll_bars = intern ("xaw");
10890 #endif
10891 #else
10892 Vx_toolkit_scroll_bars = Qnil;
10893 #endif
10895 staticpro (&last_mouse_motion_frame);
10896 last_mouse_motion_frame = Qnil;
10898 Qmodifier_value = intern ("modifier-value");
10899 Qalt = intern ("alt");
10900 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10901 Qhyper = intern ("hyper");
10902 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10903 Qmeta = intern ("meta");
10904 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10905 Qsuper = intern ("super");
10906 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10908 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10909 doc: /* Which keys Emacs uses for the alt modifier.
10910 This should be one of the symbols `alt', `hyper', `meta', `super'.
10911 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10912 is nil, which is the same as `alt'. */);
10913 Vx_alt_keysym = Qnil;
10915 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
10916 doc: /* Which keys Emacs uses for the hyper modifier.
10917 This should be one of the symbols `alt', `hyper', `meta', `super'.
10918 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10919 default is nil, which is the same as `hyper'. */);
10920 Vx_hyper_keysym = Qnil;
10922 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
10923 doc: /* Which keys Emacs uses for the meta modifier.
10924 This should be one of the symbols `alt', `hyper', `meta', `super'.
10925 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10926 default is nil, which is the same as `meta'. */);
10927 Vx_meta_keysym = Qnil;
10929 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
10930 doc: /* Which keys Emacs uses for the super modifier.
10931 This should be one of the symbols `alt', `hyper', `meta', `super'.
10932 For example, `super' means use the Super_L and Super_R keysyms. The
10933 default is nil, which is the same as `super'. */);
10934 Vx_super_keysym = Qnil;
10936 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
10937 doc: /* Hash table of character codes indexed by X keysym codes. */);
10938 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
10939 make_float (DEFAULT_REHASH_SIZE),
10940 make_float (DEFAULT_REHASH_THRESHOLD),
10941 Qnil, Qnil, Qnil);
10944 #endif /* HAVE_X_WINDOWS */
10946 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
10947 (do not change this comment) */