Recompute prev_stop and base_level_stop when the iterator is
[emacs.git] / src / xterm.c
blob2093b68c1102ec9a776a598d599f437405fbcfc6
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 "xsettings.h"
90 #include "xgselect.h"
91 #include "sysselect.h"
93 #ifdef USE_X_TOOLKIT
94 #include <X11/Shell.h>
95 #endif
97 #ifdef HAVE_SYS_TIME_H
98 #include <sys/time.h>
99 #endif
100 #ifdef HAVE_UNISTD_H
101 #include <unistd.h>
102 #endif
104 #ifdef USE_GTK
105 #include "gtkutil.h"
106 #endif
108 #ifdef USE_LUCID
109 extern int xlwmenu_window_p P_ ((Widget w, Window window));
110 extern void xlwmenu_redisplay P_ ((Widget));
111 #endif
113 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
115 extern void free_frame_menubar P_ ((struct frame *));
116 #endif
118 #ifdef USE_X_TOOLKIT
119 #if !defined(NO_EDITRES)
120 #define HACK_EDITRES
121 extern void _XEditResCheckMessages ();
122 #endif /* not NO_EDITRES */
124 /* Include toolkit specific headers for the scroll bar widget. */
126 #ifdef USE_TOOLKIT_SCROLL_BARS
127 #if defined USE_MOTIF
128 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
129 #include <Xm/ScrollBar.h>
130 #else /* !USE_MOTIF i.e. use Xaw */
132 #ifdef HAVE_XAW3D
133 #include <X11/Xaw3d/Simple.h>
134 #include <X11/Xaw3d/Scrollbar.h>
135 #include <X11/Xaw3d/ThreeD.h>
136 #else /* !HAVE_XAW3D */
137 #include <X11/Xaw/Simple.h>
138 #include <X11/Xaw/Scrollbar.h>
139 #endif /* !HAVE_XAW3D */
140 #ifndef XtNpickTop
141 #define XtNpickTop "pickTop"
142 #endif /* !XtNpickTop */
143 #endif /* !USE_MOTIF */
144 #endif /* USE_TOOLKIT_SCROLL_BARS */
146 #endif /* USE_X_TOOLKIT */
148 #ifdef USE_X_TOOLKIT
149 #include "widget.h"
150 #ifndef XtNinitialState
151 #define XtNinitialState "initialState"
152 #endif
153 #endif
155 /* Default to using XIM if available. */
156 #ifdef USE_XIM
157 int use_xim = 1;
158 #else
159 int use_xim = 0; /* configure --without-xim */
160 #endif
164 /* Non-nil means Emacs uses toolkit scroll bars. */
166 Lisp_Object Vx_toolkit_scroll_bars;
168 /* Non-zero means that a HELP_EVENT has been generated since Emacs
169 start. */
171 static int any_help_event_p;
173 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
174 static Lisp_Object last_window;
176 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
178 int x_use_underline_position_properties;
180 /* Non-zero means to draw the underline at the same place as the descent line. */
182 int x_underline_at_descent_line;
184 /* This is a chain of structures for all the X displays currently in
185 use. */
187 struct x_display_info *x_display_list;
189 /* This is a list of cons cells, each of the form (NAME
190 . FONT-LIST-CACHE), one for each element of x_display_list and in
191 the same order. NAME is the name of the frame. FONT-LIST-CACHE
192 records previous values returned by x-list-fonts. */
194 Lisp_Object x_display_name_list;
196 /* Frame being updated by update_frame. This is declared in term.c.
197 This is set by update_begin and looked at by all the XT functions.
198 It is zero while not inside an update. In that case, the XT
199 functions assume that `selected_frame' is the frame to apply to. */
201 extern struct frame *updating_frame;
203 /* This is a frame waiting to be auto-raised, within XTread_socket. */
205 static struct frame *pending_autoraise_frame;
207 /* This is a frame waiting for an event matching mask, within XTread_socket. */
209 static struct {
210 struct frame *f;
211 int eventtype;
212 } pending_event_wait;
214 #ifdef USE_X_TOOLKIT
215 /* The application context for Xt use. */
216 XtAppContext Xt_app_con;
217 static String Xt_default_resources[] = {0};
218 #endif /* USE_X_TOOLKIT */
220 /* Non-zero means user is interacting with a toolkit scroll bar. */
222 static int toolkit_scroll_bar_interaction;
224 /* Non-zero means to not move point as a result of clicking on a
225 frame to focus it (when focus-follows-mouse is nil). */
227 int x_mouse_click_focus_ignore_position;
229 /* Non-zero timeout value means ignore next mouse click if it arrives
230 before that timeout elapses (i.e. as part of the same sequence of
231 events resulting from clicking on a frame to select it). */
233 static unsigned long ignore_next_mouse_click_timeout;
235 /* Mouse movement.
237 Formerly, we used PointerMotionHintMask (in standard_event_mask)
238 so that we would have to call XQueryPointer after each MotionNotify
239 event to ask for another such event. However, this made mouse tracking
240 slow, and there was a bug that made it eventually stop.
242 Simply asking for MotionNotify all the time seems to work better.
244 In order to avoid asking for motion events and then throwing most
245 of them away or busy-polling the server for mouse positions, we ask
246 the server for pointer motion hints. This means that we get only
247 one event per group of mouse movements. "Groups" are delimited by
248 other kinds of events (focus changes and button clicks, for
249 example), or by XQueryPointer calls; when one of these happens, we
250 get another MotionNotify event the next time the mouse moves. This
251 is at least as efficient as getting motion events when mouse
252 tracking is on, and I suspect only negligibly worse when tracking
253 is off. */
255 /* Where the mouse was last time we reported a mouse event. */
257 static XRectangle last_mouse_glyph;
258 static FRAME_PTR last_mouse_glyph_frame;
259 static Lisp_Object last_mouse_press_frame;
261 /* The scroll bar in which the last X motion event occurred.
263 If the last X motion event occurred in a scroll bar, we set this so
264 XTmouse_position can know whether to report a scroll bar motion or
265 an ordinary motion.
267 If the last X motion event didn't occur in a scroll bar, we set
268 this to Qnil, to tell XTmouse_position to return an ordinary motion
269 event. */
271 static Lisp_Object last_mouse_scroll_bar;
273 /* This is a hack. We would really prefer that XTmouse_position would
274 return the time associated with the position it returns, but there
275 doesn't seem to be any way to wrest the time-stamp from the server
276 along with the position query. So, we just keep track of the time
277 of the last movement we received, and return that in hopes that
278 it's somewhat accurate. */
280 static Time last_mouse_movement_time;
282 /* Time for last user interaction as returned in X events. */
284 static Time last_user_time;
286 /* Incremented by XTread_socket whenever it really tries to read
287 events. */
289 #ifdef __STDC__
290 static int volatile input_signal_count;
291 #else
292 static int input_signal_count;
293 #endif
295 /* Used locally within XTread_socket. */
297 static int x_noop_count;
299 /* Initial values of argv and argc. */
301 extern char **initial_argv;
302 extern int initial_argc;
304 extern Lisp_Object Vcommand_line_args, Vsystem_name;
306 /* Tells if a window manager is present or not. */
308 extern Lisp_Object Vx_no_window_manager;
310 extern Lisp_Object Qeql;
312 extern int errno;
314 /* A mask of extra modifier bits to put into every keyboard char. */
316 extern EMACS_INT extra_keyboard_modifiers;
318 /* The keysyms to use for the various modifiers. */
320 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
321 Lisp_Object Vx_keysym_table;
322 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
324 static Lisp_Object Qvendor_specific_keysyms;
325 static Lisp_Object Qlatin_1;
327 #ifdef USE_GTK
328 /* The name of the Emacs icon file. */
329 static Lisp_Object xg_default_icon_file;
331 /* Used in gtkutil.c. */
332 Lisp_Object Qx_gtk_map_stock;
333 #endif
335 /* Used in x_flush. */
337 extern Lisp_Object Vinhibit_redisplay;
339 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
340 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
342 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
343 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
344 static const XColor *x_color_cells P_ ((Display *, int *));
345 static void x_update_window_end P_ ((struct window *, int, int));
347 static int x_io_error_quitter P_ ((Display *));
348 static struct terminal *x_create_terminal P_ ((struct x_display_info *));
349 void x_delete_terminal P_ ((struct terminal *));
350 static void x_update_end P_ ((struct frame *));
351 static void XTframe_up_to_date P_ ((struct frame *));
352 static void XTset_terminal_modes P_ ((struct terminal *));
353 static void XTreset_terminal_modes P_ ((struct terminal *));
354 static void x_clear_frame P_ ((struct frame *));
355 static void frame_highlight P_ ((struct frame *));
356 static void frame_unhighlight P_ ((struct frame *));
357 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
358 static void x_focus_changed P_ ((int, int, struct x_display_info *,
359 struct frame *, struct input_event *));
360 static void x_detect_focus_change P_ ((struct x_display_info *,
361 XEvent *, struct input_event *));
362 static void XTframe_rehighlight P_ ((struct frame *));
363 static void x_frame_rehighlight P_ ((struct x_display_info *));
364 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
365 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
366 enum text_cursor_kinds));
368 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
369 static void x_flush P_ ((struct frame *f));
370 static void x_update_begin P_ ((struct frame *));
371 static void x_update_window_begin P_ ((struct window *));
372 static void x_after_update_window_line P_ ((struct glyph_row *));
373 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
374 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
375 enum scroll_bar_part *,
376 Lisp_Object *, Lisp_Object *,
377 unsigned long *));
378 static void x_handle_net_wm_state P_ ((struct frame *, XPropertyEvent *));
379 static void x_check_fullscreen P_ ((struct frame *));
380 static void x_check_expected_move P_ ((struct frame *, int, int));
381 static void x_sync_with_move P_ ((struct frame *, int, int, int));
382 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
383 int *, struct input_event *));
384 /* Don't declare this NO_RETURN because we want no
385 interference with debugging failing X calls. */
386 static SIGTYPE x_connection_closed P_ ((Display *, char *));
389 /* Flush display of frame F, or of all frames if F is null. */
391 static void
392 x_flush (f)
393 struct frame *f;
395 /* Don't call XFlush when it is not safe to redisplay; the X
396 connection may be broken. */
397 if (!NILP (Vinhibit_redisplay))
398 return;
400 BLOCK_INPUT;
401 if (f == NULL)
403 Lisp_Object rest, frame;
404 FOR_EACH_FRAME (rest, frame)
405 if (FRAME_X_P (XFRAME (frame)))
406 x_flush (XFRAME (frame));
408 else if (FRAME_X_P (f))
409 XFlush (FRAME_X_DISPLAY (f));
410 UNBLOCK_INPUT;
414 /* Remove calls to XFlush by defining XFlush to an empty replacement.
415 Calls to XFlush should be unnecessary because the X output buffer
416 is flushed automatically as needed by calls to XPending,
417 XNextEvent, or XWindowEvent according to the XFlush man page.
418 XTread_socket calls XPending. Removing XFlush improves
419 performance. */
421 #define XFlush(DISPLAY) (void) 0
424 /***********************************************************************
425 Debugging
426 ***********************************************************************/
428 #if 0
430 /* This is a function useful for recording debugging information about
431 the sequence of occurrences in this file. */
433 struct record
435 char *locus;
436 int type;
439 struct record event_record[100];
441 int event_record_index;
443 record_event (locus, type)
444 char *locus;
445 int type;
447 if (event_record_index == sizeof (event_record) / sizeof (struct record))
448 event_record_index = 0;
450 event_record[event_record_index].locus = locus;
451 event_record[event_record_index].type = type;
452 event_record_index++;
455 #endif /* 0 */
459 /* Return the struct x_display_info corresponding to DPY. */
461 struct x_display_info *
462 x_display_info_for_display (dpy)
463 Display *dpy;
465 struct x_display_info *dpyinfo;
467 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
468 if (dpyinfo->display == dpy)
469 return dpyinfo;
471 return 0;
474 #define OPAQUE 0xffffffff
475 #define OPACITY "_NET_WM_WINDOW_OPACITY"
477 void
478 x_set_frame_alpha (f)
479 struct frame *f;
481 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
482 Display *dpy = FRAME_X_DISPLAY (f);
483 Window win = FRAME_OUTER_WINDOW (f);
484 double alpha = 1.0;
485 double alpha_min = 1.0;
486 unsigned long opac;
488 if (FRAME_X_DISPLAY_INFO (f)->root_window != FRAME_X_OUTPUT (f)->parent_desc)
489 /* Since the WM decoration lies under the FRAME_OUTER_WINDOW,
490 we must treat the former instead of the latter. */
491 win = FRAME_X_OUTPUT(f)->parent_desc;
493 if (dpyinfo->x_highlight_frame == f)
494 alpha = f->alpha[0];
495 else
496 alpha = f->alpha[1];
498 if (FLOATP (Vframe_alpha_lower_limit))
499 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
500 else if (INTEGERP (Vframe_alpha_lower_limit))
501 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
503 if (alpha < 0.0)
504 return;
505 else if (alpha > 1.0)
506 alpha = 1.0;
507 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
508 alpha = alpha_min;
510 opac = alpha * OPAQUE;
512 /* return unless necessary */
514 unsigned char *data;
515 Atom actual;
516 int rc, format;
517 unsigned long n, left;
519 x_catch_errors (dpy);
520 rc = XGetWindowProperty(dpy, win, XInternAtom(dpy, OPACITY, False),
521 0L, 1L, False, XA_CARDINAL,
522 &actual, &format, &n, &left,
523 &data);
525 if (rc == Success && actual != None)
526 if (*(unsigned long *)data == opac)
528 XFree ((void *) data);
529 x_uncatch_errors ();
530 return;
532 else
533 XFree ((void *) data);
534 x_uncatch_errors ();
537 x_catch_errors (dpy);
538 XChangeProperty (dpy, win, XInternAtom (dpy, OPACITY, False),
539 XA_CARDINAL, 32, PropModeReplace,
540 (unsigned char *) &opac, 1L);
541 x_uncatch_errors ();
545 x_display_pixel_height (dpyinfo)
546 struct x_display_info *dpyinfo;
548 return HeightOfScreen (dpyinfo->screen);
552 x_display_pixel_width (dpyinfo)
553 struct x_display_info *dpyinfo;
555 return WidthOfScreen (dpyinfo->screen);
559 /***********************************************************************
560 Starting and ending an update
561 ***********************************************************************/
563 /* Start an update of frame F. This function is installed as a hook
564 for update_begin, i.e. it is called when update_begin is called.
565 This function is called prior to calls to x_update_window_begin for
566 each window being updated. Currently, there is nothing to do here
567 because all interesting stuff is done on a window basis. */
569 static void
570 x_update_begin (f)
571 struct frame *f;
573 /* Nothing to do. */
577 /* Start update of window W. Set the global variable updated_window
578 to the window being updated and set output_cursor to the cursor
579 position of W. */
581 static void
582 x_update_window_begin (w)
583 struct window *w;
585 struct frame *f = XFRAME (WINDOW_FRAME (w));
586 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
588 updated_window = w;
589 set_output_cursor (&w->cursor);
591 BLOCK_INPUT;
593 if (f == display_info->mouse_face_mouse_frame)
595 /* Don't do highlighting for mouse motion during the update. */
596 display_info->mouse_face_defer = 1;
598 /* If F needs to be redrawn, simply forget about any prior mouse
599 highlighting. */
600 if (FRAME_GARBAGED_P (f))
601 display_info->mouse_face_window = Qnil;
604 UNBLOCK_INPUT;
608 /* Draw a vertical window border from (x,y0) to (x,y1) */
610 static void
611 x_draw_vertical_window_border (w, x, y0, y1)
612 struct window *w;
613 int x, y0, y1;
615 struct frame *f = XFRAME (WINDOW_FRAME (w));
616 struct face *face;
618 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
619 if (face)
620 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
621 face->foreground);
623 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
624 f->output_data.x->normal_gc, x, y0, x, y1);
627 /* End update of window W (which is equal to updated_window).
629 Draw vertical borders between horizontally adjacent windows, and
630 display W's cursor if CURSOR_ON_P is non-zero.
632 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
633 glyphs in mouse-face were overwritten. In that case we have to
634 make sure that the mouse-highlight is properly redrawn.
636 W may be a menu bar pseudo-window in case we don't have X toolkit
637 support. Such windows don't have a cursor, so don't display it
638 here. */
640 static void
641 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
642 struct window *w;
643 int cursor_on_p, mouse_face_overwritten_p;
645 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
647 if (!w->pseudo_window_p)
649 BLOCK_INPUT;
651 if (cursor_on_p)
652 display_and_set_cursor (w, 1, output_cursor.hpos,
653 output_cursor.vpos,
654 output_cursor.x, output_cursor.y);
656 if (draw_window_fringes (w, 1))
657 x_draw_vertical_border (w);
659 UNBLOCK_INPUT;
662 /* If a row with mouse-face was overwritten, arrange for
663 XTframe_up_to_date to redisplay the mouse highlight. */
664 if (mouse_face_overwritten_p)
666 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
667 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
668 dpyinfo->mouse_face_window = Qnil;
671 updated_window = NULL;
675 /* End update of frame F. This function is installed as a hook in
676 update_end. */
678 static void
679 x_update_end (f)
680 struct frame *f;
682 /* Mouse highlight may be displayed again. */
683 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
685 #ifndef XFlush
686 BLOCK_INPUT;
687 XFlush (FRAME_X_DISPLAY (f));
688 UNBLOCK_INPUT;
689 #endif
693 /* This function is called from various places in xdisp.c whenever a
694 complete update has been performed. The global variable
695 updated_window is not available here. */
697 static void
698 XTframe_up_to_date (f)
699 struct frame *f;
701 if (FRAME_X_P (f))
703 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
705 if (dpyinfo->mouse_face_deferred_gc
706 || f == dpyinfo->mouse_face_mouse_frame)
708 BLOCK_INPUT;
709 if (dpyinfo->mouse_face_mouse_frame)
710 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
711 dpyinfo->mouse_face_mouse_x,
712 dpyinfo->mouse_face_mouse_y);
713 dpyinfo->mouse_face_deferred_gc = 0;
714 UNBLOCK_INPUT;
720 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
721 arrow bitmaps, or clear the fringes if no bitmaps are required
722 before DESIRED_ROW is made current. The window being updated is
723 found in updated_window. This function It is called from
724 update_window_line only if it is known that there are differences
725 between bitmaps to be drawn between current row and DESIRED_ROW. */
727 static void
728 x_after_update_window_line (desired_row)
729 struct glyph_row *desired_row;
731 struct window *w = updated_window;
732 struct frame *f;
733 int width, height;
735 xassert (w);
737 if (!desired_row->mode_line_p && !w->pseudo_window_p)
738 desired_row->redraw_fringe_bitmaps_p = 1;
740 /* When a window has disappeared, make sure that no rest of
741 full-width rows stays visible in the internal border. Could
742 check here if updated_window is the leftmost/rightmost window,
743 but I guess it's not worth doing since vertically split windows
744 are almost never used, internal border is rarely set, and the
745 overhead is very small. */
746 if (windows_or_buffers_changed
747 && desired_row->full_width_p
748 && (f = XFRAME (w->frame),
749 width = FRAME_INTERNAL_BORDER_WIDTH (f),
750 width != 0)
751 && (height = desired_row->visible_height,
752 height > 0))
754 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
756 BLOCK_INPUT;
757 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
758 0, y, width, height, False);
759 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
760 FRAME_PIXEL_WIDTH (f) - width,
761 y, width, height, False);
762 UNBLOCK_INPUT;
766 static void
767 x_draw_fringe_bitmap (w, row, p)
768 struct window *w;
769 struct glyph_row *row;
770 struct draw_fringe_bitmap_params *p;
772 struct frame *f = XFRAME (WINDOW_FRAME (w));
773 Display *display = FRAME_X_DISPLAY (f);
774 Window window = FRAME_X_WINDOW (f);
775 GC gc = f->output_data.x->normal_gc;
776 struct face *face = p->face;
777 int rowY;
779 /* Must clip because of partially visible lines. */
780 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
781 if (p->y < rowY)
783 /* Adjust position of "bottom aligned" bitmap on partially
784 visible last row. */
785 int oldY = row->y;
786 int oldVH = row->visible_height;
787 row->visible_height = p->h;
788 row->y -= rowY - p->y;
789 x_clip_to_row (w, row, -1, gc);
790 row->y = oldY;
791 row->visible_height = oldVH;
793 else
794 x_clip_to_row (w, row, -1, gc);
796 if (!p->overlay_p)
798 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
800 /* In case the same realized face is used for fringes and
801 for something displayed in the text (e.g. face `region' on
802 mono-displays, the fill style may have been changed to
803 FillSolid in x_draw_glyph_string_background. */
804 if (face->stipple)
805 XSetFillStyle (display, face->gc, FillOpaqueStippled);
806 else
807 XSetForeground (display, face->gc, face->background);
809 #ifdef USE_TOOLKIT_SCROLL_BARS
810 /* If the fringe is adjacent to the left (right) scroll bar of a
811 leftmost (rightmost, respectively) window, then extend its
812 background to the gap between the fringe and the bar. */
813 if ((WINDOW_LEFTMOST_P (w)
814 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
815 || (WINDOW_RIGHTMOST_P (w)
816 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
818 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
820 if (sb_width > 0)
822 int left = WINDOW_SCROLL_BAR_AREA_X (w);
823 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
824 * FRAME_COLUMN_WIDTH (f));
826 if (bx < 0)
828 /* Bitmap fills the fringe. */
829 if (left + width == p->x)
830 bx = left + sb_width;
831 else if (p->x + p->wd == left)
832 bx = left;
833 if (bx >= 0)
835 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
837 nx = width - sb_width;
838 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
839 row->y));
840 ny = row->visible_height;
843 else
845 if (left + width == bx)
847 bx = left + sb_width;
848 nx += width - sb_width;
850 else if (bx + nx == left)
851 nx += width - sb_width;
855 #endif
856 if (bx >= 0 && nx > 0)
857 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
859 if (!face->stipple)
860 XSetForeground (display, face->gc, face->foreground);
863 if (p->which)
865 unsigned char *bits;
866 Pixmap pixmap, clipmask = (Pixmap) 0;
867 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
868 XGCValues gcv;
870 if (p->wd > 8)
871 bits = (unsigned char *)(p->bits + p->dh);
872 else
873 bits = (unsigned char *)p->bits + p->dh;
875 /* Draw the bitmap. I believe these small pixmaps can be cached
876 by the server. */
877 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
878 (p->cursor_p
879 ? (p->overlay_p ? face->background
880 : f->output_data.x->cursor_pixel)
881 : face->foreground),
882 face->background, depth);
884 if (p->overlay_p)
886 clipmask = XCreatePixmapFromBitmapData (display,
887 FRAME_X_DISPLAY_INFO (f)->root_window,
888 bits, p->wd, p->h,
889 1, 0, 1);
890 gcv.clip_mask = clipmask;
891 gcv.clip_x_origin = p->x;
892 gcv.clip_y_origin = p->y;
893 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
896 XCopyArea (display, pixmap, window, gc, 0, 0,
897 p->wd, p->h, p->x, p->y);
898 XFreePixmap (display, pixmap);
900 if (p->overlay_p)
902 gcv.clip_mask = (Pixmap) 0;
903 XChangeGC (display, gc, GCClipMask, &gcv);
904 XFreePixmap (display, clipmask);
908 XSetClipMask (display, gc, None);
913 /* This is called when starting Emacs and when restarting after
914 suspend. When starting Emacs, no X window is mapped. And nothing
915 must be done to Emacs's own window if it is suspended (though that
916 rarely happens). */
918 static void
919 XTset_terminal_modes (struct terminal *terminal)
923 /* This is called when exiting or suspending Emacs. Exiting will make
924 the X-windows go away, and suspending requires no action. */
926 static void
927 XTreset_terminal_modes (struct terminal *terminal)
932 /***********************************************************************
933 Glyph display
934 ***********************************************************************/
938 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
939 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
940 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
941 int));
942 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
943 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
944 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
945 static void x_draw_glyph_string P_ ((struct glyph_string *));
946 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
947 static void x_set_cursor_gc P_ ((struct glyph_string *));
948 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
949 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
950 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
951 unsigned long *, double, int));
952 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
953 double, int, unsigned long));
954 static void x_setup_relief_colors P_ ((struct glyph_string *));
955 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
956 static void x_draw_image_relief P_ ((struct glyph_string *));
957 static void x_draw_image_foreground P_ ((struct glyph_string *));
958 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
959 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
960 int, int, int));
961 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
962 int, int, int, int, int, int,
963 XRectangle *));
964 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
965 int, int, int, XRectangle *));
967 #if GLYPH_DEBUG
968 static void x_check_font P_ ((struct frame *, struct font *));
969 #endif
972 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
973 face. */
975 static void
976 x_set_cursor_gc (s)
977 struct glyph_string *s;
979 if (s->font == FRAME_FONT (s->f)
980 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
981 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
982 && !s->cmp)
983 s->gc = s->f->output_data.x->cursor_gc;
984 else
986 /* Cursor on non-default face: must merge. */
987 XGCValues xgcv;
988 unsigned long mask;
990 xgcv.background = s->f->output_data.x->cursor_pixel;
991 xgcv.foreground = s->face->background;
993 /* If the glyph would be invisible, try a different foreground. */
994 if (xgcv.foreground == xgcv.background)
995 xgcv.foreground = s->face->foreground;
996 if (xgcv.foreground == xgcv.background)
997 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
998 if (xgcv.foreground == xgcv.background)
999 xgcv.foreground = s->face->foreground;
1001 /* Make sure the cursor is distinct from text in this face. */
1002 if (xgcv.background == s->face->background
1003 && xgcv.foreground == s->face->foreground)
1005 xgcv.background = s->face->foreground;
1006 xgcv.foreground = s->face->background;
1009 IF_DEBUG (x_check_font (s->f, s->font));
1010 xgcv.graphics_exposures = False;
1011 mask = GCForeground | GCBackground | GCGraphicsExposures;
1013 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1014 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1015 mask, &xgcv);
1016 else
1017 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1018 = XCreateGC (s->display, s->window, mask, &xgcv);
1020 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1025 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1027 static void
1028 x_set_mouse_face_gc (s)
1029 struct glyph_string *s;
1031 int face_id;
1032 struct face *face;
1034 /* What face has to be used last for the mouse face? */
1035 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1036 face = FACE_FROM_ID (s->f, face_id);
1037 if (face == NULL)
1038 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1040 if (s->first_glyph->type == CHAR_GLYPH)
1041 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1042 else
1043 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1044 s->face = FACE_FROM_ID (s->f, face_id);
1045 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1047 if (s->font == s->face->font)
1048 s->gc = s->face->gc;
1049 else
1051 /* Otherwise construct scratch_cursor_gc with values from FACE
1052 except for FONT. */
1053 XGCValues xgcv;
1054 unsigned long mask;
1056 xgcv.background = s->face->background;
1057 xgcv.foreground = s->face->foreground;
1058 xgcv.graphics_exposures = False;
1059 mask = GCForeground | GCBackground | GCGraphicsExposures;
1061 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1062 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1063 mask, &xgcv);
1064 else
1065 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1066 = XCreateGC (s->display, s->window, mask, &xgcv);
1068 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1071 xassert (s->gc != 0);
1075 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1076 Faces to use in the mode line have already been computed when the
1077 matrix was built, so there isn't much to do, here. */
1079 static INLINE void
1080 x_set_mode_line_face_gc (s)
1081 struct glyph_string *s;
1083 s->gc = s->face->gc;
1087 /* Set S->gc of glyph string S for drawing that glyph string. Set
1088 S->stippled_p to a non-zero value if the face of S has a stipple
1089 pattern. */
1091 static INLINE void
1092 x_set_glyph_string_gc (s)
1093 struct glyph_string *s;
1095 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1097 if (s->hl == DRAW_NORMAL_TEXT)
1099 s->gc = s->face->gc;
1100 s->stippled_p = s->face->stipple != 0;
1102 else if (s->hl == DRAW_INVERSE_VIDEO)
1104 x_set_mode_line_face_gc (s);
1105 s->stippled_p = s->face->stipple != 0;
1107 else if (s->hl == DRAW_CURSOR)
1109 x_set_cursor_gc (s);
1110 s->stippled_p = 0;
1112 else if (s->hl == DRAW_MOUSE_FACE)
1114 x_set_mouse_face_gc (s);
1115 s->stippled_p = s->face->stipple != 0;
1117 else if (s->hl == DRAW_IMAGE_RAISED
1118 || s->hl == DRAW_IMAGE_SUNKEN)
1120 s->gc = s->face->gc;
1121 s->stippled_p = s->face->stipple != 0;
1123 else
1125 s->gc = s->face->gc;
1126 s->stippled_p = s->face->stipple != 0;
1129 /* GC must have been set. */
1130 xassert (s->gc != 0);
1134 /* Set clipping for output of glyph string S. S may be part of a mode
1135 line or menu if we don't have X toolkit support. */
1137 static INLINE void
1138 x_set_glyph_string_clipping (s)
1139 struct glyph_string *s;
1141 XRectangle *r = s->clip;
1142 int n = get_glyph_string_clip_rects (s, r, 2);
1144 if (n > 0)
1145 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
1146 s->num_clips = n;
1150 /* Set SRC's clipping for output of glyph string DST. This is called
1151 when we are drawing DST's left_overhang or right_overhang only in
1152 the area of SRC. */
1154 static void
1155 x_set_glyph_string_clipping_exactly (src, dst)
1156 struct glyph_string *src, *dst;
1158 XRectangle r;
1160 r.x = src->x;
1161 r.width = src->width;
1162 r.y = src->y;
1163 r.height = src->height;
1164 dst->clip[0] = r;
1165 dst->num_clips = 1;
1166 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1170 /* RIF:
1171 Compute left and right overhang of glyph string S. */
1173 static void
1174 x_compute_glyph_string_overhangs (s)
1175 struct glyph_string *s;
1177 if (s->cmp == NULL
1178 && (s->first_glyph->type == CHAR_GLYPH
1179 || s->first_glyph->type == COMPOSITE_GLYPH))
1181 struct font_metrics metrics;
1183 if (s->first_glyph->type == CHAR_GLYPH)
1185 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1186 struct font *font = s->font;
1187 int i;
1189 for (i = 0; i < s->nchars; i++)
1190 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1191 font->driver->text_extents (font, code, s->nchars, &metrics);
1193 else
1195 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1197 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1199 s->right_overhang = (metrics.rbearing > metrics.width
1200 ? metrics.rbearing - metrics.width : 0);
1201 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1203 else if (s->cmp)
1205 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1206 s->left_overhang = - s->cmp->lbearing;
1211 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1213 static INLINE void
1214 x_clear_glyph_string_rect (s, x, y, w, h)
1215 struct glyph_string *s;
1216 int x, y, w, h;
1218 XGCValues xgcv;
1219 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1220 XSetForeground (s->display, s->gc, xgcv.background);
1221 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1222 XSetForeground (s->display, s->gc, xgcv.foreground);
1226 /* Draw the background of glyph_string S. If S->background_filled_p
1227 is non-zero don't draw it. FORCE_P non-zero means draw the
1228 background even if it wouldn't be drawn normally. This is used
1229 when a string preceding S draws into the background of S, or S
1230 contains the first component of a composition. */
1232 static void
1233 x_draw_glyph_string_background (s, force_p)
1234 struct glyph_string *s;
1235 int force_p;
1237 /* Nothing to do if background has already been drawn or if it
1238 shouldn't be drawn in the first place. */
1239 if (!s->background_filled_p)
1241 int box_line_width = max (s->face->box_line_width, 0);
1243 if (s->stippled_p)
1245 /* Fill background with a stipple pattern. */
1246 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1247 XFillRectangle (s->display, s->window, s->gc, s->x,
1248 s->y + box_line_width,
1249 s->background_width,
1250 s->height - 2 * box_line_width);
1251 XSetFillStyle (s->display, s->gc, FillSolid);
1252 s->background_filled_p = 1;
1254 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1255 || s->font_not_found_p
1256 || s->extends_to_end_of_line_p
1257 || force_p)
1259 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1260 s->background_width,
1261 s->height - 2 * box_line_width);
1262 s->background_filled_p = 1;
1268 /* Draw the foreground of glyph string S. */
1270 static void
1271 x_draw_glyph_string_foreground (s)
1272 struct glyph_string *s;
1274 int i, x;
1276 /* If first glyph of S has a left box line, start drawing the text
1277 of S to the right of that box line. */
1278 if (s->face->box != FACE_NO_BOX
1279 && s->first_glyph->left_box_line_p)
1280 x = s->x + eabs (s->face->box_line_width);
1281 else
1282 x = s->x;
1284 /* Draw characters of S as rectangles if S's font could not be
1285 loaded. */
1286 if (s->font_not_found_p)
1288 for (i = 0; i < s->nchars; ++i)
1290 struct glyph *g = s->first_glyph + i;
1291 XDrawRectangle (s->display, s->window,
1292 s->gc, x, s->y, g->pixel_width - 1,
1293 s->height - 1);
1294 x += g->pixel_width;
1297 else
1299 struct font *font = s->font;
1300 int boff = font->baseline_offset;
1301 int y;
1303 if (font->vertical_centering)
1304 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1306 y = s->ybase - boff;
1307 if (s->for_overlaps
1308 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1309 font->driver->draw (s, 0, s->nchars, x, y, 0);
1310 else
1311 font->driver->draw (s, 0, s->nchars, x, y, 1);
1312 if (s->face->overstrike)
1313 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1317 /* Draw the foreground of composite glyph string S. */
1319 static void
1320 x_draw_composite_glyph_string_foreground (s)
1321 struct glyph_string *s;
1323 int i, j, x;
1324 struct font *font = s->font;
1326 /* If first glyph of S has a left box line, start drawing the text
1327 of S to the right of that box line. */
1328 if (s->face && s->face->box != FACE_NO_BOX
1329 && s->first_glyph->left_box_line_p)
1330 x = s->x + eabs (s->face->box_line_width);
1331 else
1332 x = s->x;
1334 /* S is a glyph string for a composition. S->cmp_from is the index
1335 of the first character drawn for glyphs of this composition.
1336 S->cmp_from == 0 means we are drawing the very first character of
1337 this composition. */
1339 /* Draw a rectangle for the composition if the font for the very
1340 first character of the composition could not be loaded. */
1341 if (s->font_not_found_p)
1343 if (s->cmp_from == 0)
1344 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1345 s->width - 1, s->height - 1);
1347 else if (! s->first_glyph->u.cmp.automatic)
1349 int y = s->ybase;
1351 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1352 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1354 int xx = x + s->cmp->offsets[j * 2];
1355 int yy = y - s->cmp->offsets[j * 2 + 1];
1357 font->driver->draw (s, j, j + 1, xx, yy, 0);
1358 if (s->face->overstrike)
1359 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1362 else
1364 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1365 Lisp_Object glyph;
1366 int y = s->ybase;
1367 int width = 0;
1369 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1371 glyph = LGSTRING_GLYPH (gstring, i);
1372 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1373 width += LGLYPH_WIDTH (glyph);
1374 else
1376 int xoff, yoff, wadjust;
1378 if (j < i)
1380 font->driver->draw (s, j, i, x, y, 0);
1381 x += width;
1383 xoff = LGLYPH_XOFF (glyph);
1384 yoff = LGLYPH_YOFF (glyph);
1385 wadjust = LGLYPH_WADJUST (glyph);
1386 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1387 x += wadjust;
1388 j = i + 1;
1389 width = 0;
1392 if (j < i)
1393 font->driver->draw (s, j, i, x, y, 0);
1398 #ifdef USE_X_TOOLKIT
1400 static struct frame *x_frame_of_widget P_ ((Widget));
1401 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1402 XrmValue *, XrmValue *, XtPointer *));
1403 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1404 XrmValue *, Cardinal *));
1407 /* Return the frame on which widget WIDGET is used.. Abort if frame
1408 cannot be determined. */
1410 static struct frame *
1411 x_frame_of_widget (widget)
1412 Widget widget;
1414 struct x_display_info *dpyinfo;
1415 Lisp_Object tail;
1416 struct frame *f;
1418 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1420 /* Find the top-level shell of the widget. Note that this function
1421 can be called when the widget is not yet realized, so XtWindow
1422 (widget) == 0. That's the reason we can't simply use
1423 x_any_window_to_frame. */
1424 while (!XtIsTopLevelShell (widget))
1425 widget = XtParent (widget);
1427 /* Look for a frame with that top-level widget. Allocate the color
1428 on that frame to get the right gamma correction value. */
1429 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1430 if (FRAMEP (XCAR (tail))
1431 && (f = XFRAME (XCAR (tail)),
1432 (FRAME_X_P (f)
1433 && f->output_data.nothing != 1
1434 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1435 && f->output_data.x->widget == widget)
1436 return f;
1438 abort ();
1442 /* Allocate the color COLOR->pixel on the screen and display of
1443 widget WIDGET in colormap CMAP. If an exact match cannot be
1444 allocated, try the nearest color available. Value is non-zero
1445 if successful. This is called from lwlib. */
1448 x_alloc_nearest_color_for_widget (widget, cmap, color)
1449 Widget widget;
1450 Colormap cmap;
1451 XColor *color;
1453 struct frame *f = x_frame_of_widget (widget);
1454 return x_alloc_nearest_color (f, cmap, color);
1458 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1459 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1460 If this produces the same color as PIXEL, try a color where all RGB
1461 values have DELTA added. Return the allocated color in *PIXEL.
1462 DISPLAY is the X display, CMAP is the colormap to operate on.
1463 Value is non-zero if successful. */
1466 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1467 Widget widget;
1468 Display *display;
1469 Colormap cmap;
1470 unsigned long *pixel;
1471 double factor;
1472 int delta;
1474 struct frame *f = x_frame_of_widget (widget);
1475 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1479 /* Structure specifying which arguments should be passed by Xt to
1480 cvt_string_to_pixel. We want the widget's screen and colormap. */
1482 static XtConvertArgRec cvt_string_to_pixel_args[] =
1484 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1485 sizeof (Screen *)},
1486 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1487 sizeof (Colormap)}
1491 /* The address of this variable is returned by
1492 cvt_string_to_pixel. */
1494 static Pixel cvt_string_to_pixel_value;
1497 /* Convert a color name to a pixel color.
1499 DPY is the display we are working on.
1501 ARGS is an array of *NARGS XrmValue structures holding additional
1502 information about the widget for which the conversion takes place.
1503 The contents of this array are determined by the specification
1504 in cvt_string_to_pixel_args.
1506 FROM is a pointer to an XrmValue which points to the color name to
1507 convert. TO is an XrmValue in which to return the pixel color.
1509 CLOSURE_RET is a pointer to user-data, in which we record if
1510 we allocated the color or not.
1512 Value is True if successful, False otherwise. */
1514 static Boolean
1515 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1516 Display *dpy;
1517 XrmValue *args;
1518 Cardinal *nargs;
1519 XrmValue *from, *to;
1520 XtPointer *closure_ret;
1522 Screen *screen;
1523 Colormap cmap;
1524 Pixel pixel;
1525 String color_name;
1526 XColor color;
1528 if (*nargs != 2)
1530 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1531 "wrongParameters", "cvt_string_to_pixel",
1532 "XtToolkitError",
1533 "Screen and colormap args required", NULL, NULL);
1534 return False;
1537 screen = *(Screen **) args[0].addr;
1538 cmap = *(Colormap *) args[1].addr;
1539 color_name = (String) from->addr;
1541 if (strcmp (color_name, XtDefaultBackground) == 0)
1543 *closure_ret = (XtPointer) False;
1544 pixel = WhitePixelOfScreen (screen);
1546 else if (strcmp (color_name, XtDefaultForeground) == 0)
1548 *closure_ret = (XtPointer) False;
1549 pixel = BlackPixelOfScreen (screen);
1551 else if (XParseColor (dpy, cmap, color_name, &color)
1552 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1554 pixel = color.pixel;
1555 *closure_ret = (XtPointer) True;
1557 else
1559 String params[1];
1560 Cardinal nparams = 1;
1562 params[0] = color_name;
1563 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1564 "badValue", "cvt_string_to_pixel",
1565 "XtToolkitError", "Invalid color `%s'",
1566 params, &nparams);
1567 return False;
1570 if (to->addr != NULL)
1572 if (to->size < sizeof (Pixel))
1574 to->size = sizeof (Pixel);
1575 return False;
1578 *(Pixel *) to->addr = pixel;
1580 else
1582 cvt_string_to_pixel_value = pixel;
1583 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1586 to->size = sizeof (Pixel);
1587 return True;
1591 /* Free a pixel color which was previously allocated via
1592 cvt_string_to_pixel. This is registered as the destructor
1593 for this type of resource via XtSetTypeConverter.
1595 APP is the application context in which we work.
1597 TO is a pointer to an XrmValue holding the color to free.
1598 CLOSURE is the value we stored in CLOSURE_RET for this color
1599 in cvt_string_to_pixel.
1601 ARGS and NARGS are like for cvt_string_to_pixel. */
1603 static void
1604 cvt_pixel_dtor (app, to, closure, args, nargs)
1605 XtAppContext app;
1606 XrmValuePtr to;
1607 XtPointer closure;
1608 XrmValuePtr args;
1609 Cardinal *nargs;
1611 if (*nargs != 2)
1613 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1614 "XtToolkitError",
1615 "Screen and colormap arguments required",
1616 NULL, NULL);
1618 else if (closure != NULL)
1620 /* We did allocate the pixel, so free it. */
1621 Screen *screen = *(Screen **) args[0].addr;
1622 Colormap cmap = *(Colormap *) args[1].addr;
1623 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1624 (Pixel *) to->addr, 1);
1629 #endif /* USE_X_TOOLKIT */
1632 /* Value is an array of XColor structures for the contents of the
1633 color map of display DPY. Set *NCELLS to the size of the array.
1634 Note that this probably shouldn't be called for large color maps,
1635 say a 24-bit TrueColor map. */
1637 static const XColor *
1638 x_color_cells (dpy, ncells)
1639 Display *dpy;
1640 int *ncells;
1642 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1644 if (dpyinfo->color_cells == NULL)
1646 Screen *screen = dpyinfo->screen;
1647 int i;
1649 dpyinfo->ncolor_cells
1650 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1651 dpyinfo->color_cells
1652 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1653 * sizeof *dpyinfo->color_cells);
1655 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1656 dpyinfo->color_cells[i].pixel = i;
1658 XQueryColors (dpy, dpyinfo->cmap,
1659 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1662 *ncells = dpyinfo->ncolor_cells;
1663 return dpyinfo->color_cells;
1667 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1668 colors in COLORS. Use cached information, if available. */
1670 void
1671 x_query_colors (f, colors, ncolors)
1672 struct frame *f;
1673 XColor *colors;
1674 int ncolors;
1676 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1678 if (dpyinfo->color_cells)
1680 int i;
1681 for (i = 0; i < ncolors; ++i)
1683 unsigned long pixel = colors[i].pixel;
1684 xassert (pixel < dpyinfo->ncolor_cells);
1685 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1686 colors[i] = dpyinfo->color_cells[pixel];
1689 else
1690 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1694 /* On frame F, translate pixel color to RGB values for the color in
1695 COLOR. Use cached information, if available. */
1697 void
1698 x_query_color (f, color)
1699 struct frame *f;
1700 XColor *color;
1702 x_query_colors (f, color, 1);
1706 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1707 exact match can't be allocated, try the nearest color available.
1708 Value is non-zero if successful. Set *COLOR to the color
1709 allocated. */
1711 static int
1712 x_alloc_nearest_color_1 (dpy, cmap, color)
1713 Display *dpy;
1714 Colormap cmap;
1715 XColor *color;
1717 int rc;
1719 rc = XAllocColor (dpy, cmap, color);
1720 if (rc == 0)
1722 /* If we got to this point, the colormap is full, so we're going
1723 to try to get the next closest color. The algorithm used is
1724 a least-squares matching, which is what X uses for closest
1725 color matching with StaticColor visuals. */
1726 int nearest, i;
1727 unsigned long nearest_delta = ~0;
1728 int ncells;
1729 const XColor *cells = x_color_cells (dpy, &ncells);
1731 for (nearest = i = 0; i < ncells; ++i)
1733 long dred = (color->red >> 8) - (cells[i].red >> 8);
1734 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1735 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1736 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1738 if (delta < nearest_delta)
1740 nearest = i;
1741 nearest_delta = delta;
1745 color->red = cells[nearest].red;
1746 color->green = cells[nearest].green;
1747 color->blue = cells[nearest].blue;
1748 rc = XAllocColor (dpy, cmap, color);
1750 else
1752 /* If allocation succeeded, and the allocated pixel color is not
1753 equal to a cached pixel color recorded earlier, there was a
1754 change in the colormap, so clear the color cache. */
1755 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1756 XColor *cached_color;
1758 if (dpyinfo->color_cells
1759 && (cached_color = &dpyinfo->color_cells[color->pixel],
1760 (cached_color->red != color->red
1761 || cached_color->blue != color->blue
1762 || cached_color->green != color->green)))
1764 xfree (dpyinfo->color_cells);
1765 dpyinfo->color_cells = NULL;
1766 dpyinfo->ncolor_cells = 0;
1770 #ifdef DEBUG_X_COLORS
1771 if (rc)
1772 register_color (color->pixel);
1773 #endif /* DEBUG_X_COLORS */
1775 return rc;
1779 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1780 exact match can't be allocated, try the nearest color available.
1781 Value is non-zero if successful. Set *COLOR to the color
1782 allocated. */
1785 x_alloc_nearest_color (f, cmap, color)
1786 struct frame *f;
1787 Colormap cmap;
1788 XColor *color;
1790 gamma_correct (f, color);
1791 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1795 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1796 It's necessary to do this instead of just using PIXEL directly to
1797 get color reference counts right. */
1799 unsigned long
1800 x_copy_color (f, pixel)
1801 struct frame *f;
1802 unsigned long pixel;
1804 XColor color;
1806 color.pixel = pixel;
1807 BLOCK_INPUT;
1808 x_query_color (f, &color);
1809 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1810 UNBLOCK_INPUT;
1811 #ifdef DEBUG_X_COLORS
1812 register_color (pixel);
1813 #endif
1814 return color.pixel;
1818 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1819 It's necessary to do this instead of just using PIXEL directly to
1820 get color reference counts right. */
1822 unsigned long
1823 x_copy_dpy_color (dpy, cmap, pixel)
1824 Display *dpy;
1825 Colormap cmap;
1826 unsigned long pixel;
1828 XColor color;
1830 color.pixel = pixel;
1831 BLOCK_INPUT;
1832 XQueryColor (dpy, cmap, &color);
1833 XAllocColor (dpy, cmap, &color);
1834 UNBLOCK_INPUT;
1835 #ifdef DEBUG_X_COLORS
1836 register_color (pixel);
1837 #endif
1838 return color.pixel;
1842 /* Brightness beyond which a color won't have its highlight brightness
1843 boosted.
1845 Nominally, highlight colors for `3d' faces are calculated by
1846 brightening an object's color by a constant scale factor, but this
1847 doesn't yield good results for dark colors, so for colors who's
1848 brightness is less than this value (on a scale of 0-65535) have an
1849 use an additional additive factor.
1851 The value here is set so that the default menu-bar/mode-line color
1852 (grey75) will not have its highlights changed at all. */
1853 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1856 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1857 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1858 If this produces the same color as PIXEL, try a color where all RGB
1859 values have DELTA added. Return the allocated color in *PIXEL.
1860 DISPLAY is the X display, CMAP is the colormap to operate on.
1861 Value is non-zero if successful. */
1863 static int
1864 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1865 struct frame *f;
1866 Display *display;
1867 Colormap cmap;
1868 unsigned long *pixel;
1869 double factor;
1870 int delta;
1872 XColor color, new;
1873 long bright;
1874 int success_p;
1876 /* Get RGB color values. */
1877 color.pixel = *pixel;
1878 x_query_color (f, &color);
1880 /* Change RGB values by specified FACTOR. Avoid overflow! */
1881 xassert (factor >= 0);
1882 new.red = min (0xffff, factor * color.red);
1883 new.green = min (0xffff, factor * color.green);
1884 new.blue = min (0xffff, factor * color.blue);
1886 /* Calculate brightness of COLOR. */
1887 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1889 /* We only boost colors that are darker than
1890 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1891 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1892 /* Make an additive adjustment to NEW, because it's dark enough so
1893 that scaling by FACTOR alone isn't enough. */
1895 /* How far below the limit this color is (0 - 1, 1 being darker). */
1896 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1897 /* The additive adjustment. */
1898 int min_delta = delta * dimness * factor / 2;
1900 if (factor < 1)
1902 new.red = max (0, new.red - min_delta);
1903 new.green = max (0, new.green - min_delta);
1904 new.blue = max (0, new.blue - min_delta);
1906 else
1908 new.red = min (0xffff, min_delta + new.red);
1909 new.green = min (0xffff, min_delta + new.green);
1910 new.blue = min (0xffff, min_delta + new.blue);
1914 /* Try to allocate the color. */
1915 success_p = x_alloc_nearest_color (f, cmap, &new);
1916 if (success_p)
1918 if (new.pixel == *pixel)
1920 /* If we end up with the same color as before, try adding
1921 delta to the RGB values. */
1922 x_free_colors (f, &new.pixel, 1);
1924 new.red = min (0xffff, delta + color.red);
1925 new.green = min (0xffff, delta + color.green);
1926 new.blue = min (0xffff, delta + color.blue);
1927 success_p = x_alloc_nearest_color (f, cmap, &new);
1929 else
1930 success_p = 1;
1931 *pixel = new.pixel;
1934 return success_p;
1938 /* Set up the foreground color for drawing relief lines of glyph
1939 string S. RELIEF is a pointer to a struct relief containing the GC
1940 with which lines will be drawn. Use a color that is FACTOR or
1941 DELTA lighter or darker than the relief's background which is found
1942 in S->f->output_data.x->relief_background. If such a color cannot
1943 be allocated, use DEFAULT_PIXEL, instead. */
1945 static void
1946 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1947 struct frame *f;
1948 struct relief *relief;
1949 double factor;
1950 int delta;
1951 unsigned long default_pixel;
1953 XGCValues xgcv;
1954 struct x_output *di = f->output_data.x;
1955 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1956 unsigned long pixel;
1957 unsigned long background = di->relief_background;
1958 Colormap cmap = FRAME_X_COLORMAP (f);
1959 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1960 Display *dpy = FRAME_X_DISPLAY (f);
1962 xgcv.graphics_exposures = False;
1963 xgcv.line_width = 1;
1965 /* Free previously allocated color. The color cell will be reused
1966 when it has been freed as many times as it was allocated, so this
1967 doesn't affect faces using the same colors. */
1968 if (relief->gc
1969 && relief->allocated_p)
1971 x_free_colors (f, &relief->pixel, 1);
1972 relief->allocated_p = 0;
1975 /* Allocate new color. */
1976 xgcv.foreground = default_pixel;
1977 pixel = background;
1978 if (dpyinfo->n_planes != 1
1979 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1981 relief->allocated_p = 1;
1982 xgcv.foreground = relief->pixel = pixel;
1985 if (relief->gc == 0)
1987 xgcv.stipple = dpyinfo->gray;
1988 mask |= GCStipple;
1989 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1991 else
1992 XChangeGC (dpy, relief->gc, mask, &xgcv);
1996 /* Set up colors for the relief lines around glyph string S. */
1998 static void
1999 x_setup_relief_colors (s)
2000 struct glyph_string *s;
2002 struct x_output *di = s->f->output_data.x;
2003 unsigned long color;
2005 if (s->face->use_box_color_for_shadows_p)
2006 color = s->face->box_color;
2007 else if (s->first_glyph->type == IMAGE_GLYPH
2008 && s->img->pixmap
2009 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2010 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2011 else
2013 XGCValues xgcv;
2015 /* Get the background color of the face. */
2016 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2017 color = xgcv.background;
2020 if (di->white_relief.gc == 0
2021 || color != di->relief_background)
2023 di->relief_background = color;
2024 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2025 WHITE_PIX_DEFAULT (s->f));
2026 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2027 BLACK_PIX_DEFAULT (s->f));
2032 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2033 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2034 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2035 relief. LEFT_P non-zero means draw a relief on the left side of
2036 the rectangle. RIGHT_P non-zero means draw a relief on the right
2037 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2038 when drawing. */
2040 static void
2041 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2042 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2043 struct frame *f;
2044 int left_x, top_y, right_x, bottom_y, width;
2045 int top_p, bot_p, left_p, right_p, raised_p;
2046 XRectangle *clip_rect;
2048 Display *dpy = FRAME_X_DISPLAY (f);
2049 Window window = FRAME_X_WINDOW (f);
2050 int i;
2051 GC gc;
2053 if (raised_p)
2054 gc = f->output_data.x->white_relief.gc;
2055 else
2056 gc = f->output_data.x->black_relief.gc;
2057 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2059 /* Top. */
2060 if (top_p)
2061 for (i = 0; i < width; ++i)
2062 XDrawLine (dpy, window, gc,
2063 left_x + i * left_p, top_y + i,
2064 right_x + 1 - i * right_p, top_y + i);
2066 /* Left. */
2067 if (left_p)
2068 for (i = 0; i < width; ++i)
2069 XDrawLine (dpy, window, gc,
2070 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2072 XSetClipMask (dpy, gc, None);
2073 if (raised_p)
2074 gc = f->output_data.x->black_relief.gc;
2075 else
2076 gc = f->output_data.x->white_relief.gc;
2077 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2079 /* Bottom. */
2080 if (bot_p)
2081 for (i = 0; i < width; ++i)
2082 XDrawLine (dpy, window, gc,
2083 left_x + i * left_p, bottom_y - i,
2084 right_x + 1 - i * right_p, bottom_y - i);
2086 /* Right. */
2087 if (right_p)
2088 for (i = 0; i < width; ++i)
2089 XDrawLine (dpy, window, gc,
2090 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2092 XSetClipMask (dpy, gc, None);
2096 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2097 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2098 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2099 left side of the rectangle. RIGHT_P non-zero means draw a line
2100 on the right side of the rectangle. CLIP_RECT is the clipping
2101 rectangle to use when drawing. */
2103 static void
2104 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2105 left_p, right_p, clip_rect)
2106 struct glyph_string *s;
2107 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2108 XRectangle *clip_rect;
2110 XGCValues xgcv;
2112 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2113 XSetForeground (s->display, s->gc, s->face->box_color);
2114 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2116 /* Top. */
2117 XFillRectangle (s->display, s->window, s->gc,
2118 left_x, top_y, right_x - left_x + 1, width);
2120 /* Left. */
2121 if (left_p)
2122 XFillRectangle (s->display, s->window, s->gc,
2123 left_x, top_y, width, bottom_y - top_y + 1);
2125 /* Bottom. */
2126 XFillRectangle (s->display, s->window, s->gc,
2127 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2129 /* Right. */
2130 if (right_p)
2131 XFillRectangle (s->display, s->window, s->gc,
2132 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2134 XSetForeground (s->display, s->gc, xgcv.foreground);
2135 XSetClipMask (s->display, s->gc, None);
2139 /* Draw a box around glyph string S. */
2141 static void
2142 x_draw_glyph_string_box (s)
2143 struct glyph_string *s;
2145 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2146 int left_p, right_p;
2147 struct glyph *last_glyph;
2148 XRectangle clip_rect;
2150 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2151 ? WINDOW_RIGHT_EDGE_X (s->w)
2152 : window_box_right (s->w, s->area));
2154 /* The glyph that may have a right box line. */
2155 last_glyph = (s->cmp || s->img
2156 ? s->first_glyph
2157 : s->first_glyph + s->nchars - 1);
2159 width = eabs (s->face->box_line_width);
2160 raised_p = s->face->box == FACE_RAISED_BOX;
2161 left_x = s->x;
2162 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2163 ? last_x - 1
2164 : min (last_x, s->x + s->background_width) - 1);
2165 top_y = s->y;
2166 bottom_y = top_y + s->height - 1;
2168 left_p = (s->first_glyph->left_box_line_p
2169 || (s->hl == DRAW_MOUSE_FACE
2170 && (s->prev == NULL
2171 || s->prev->hl != s->hl)));
2172 right_p = (last_glyph->right_box_line_p
2173 || (s->hl == DRAW_MOUSE_FACE
2174 && (s->next == NULL
2175 || s->next->hl != s->hl)));
2177 get_glyph_string_clip_rect (s, &clip_rect);
2179 if (s->face->box == FACE_SIMPLE_BOX)
2180 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2181 left_p, right_p, &clip_rect);
2182 else
2184 x_setup_relief_colors (s);
2185 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2186 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2191 /* Draw foreground of image glyph string S. */
2193 static void
2194 x_draw_image_foreground (s)
2195 struct glyph_string *s;
2197 int x = s->x;
2198 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2200 /* If first glyph of S has a left box line, start drawing it to the
2201 right of that line. */
2202 if (s->face->box != FACE_NO_BOX
2203 && s->first_glyph->left_box_line_p
2204 && s->slice.x == 0)
2205 x += eabs (s->face->box_line_width);
2207 /* If there is a margin around the image, adjust x- and y-position
2208 by that margin. */
2209 if (s->slice.x == 0)
2210 x += s->img->hmargin;
2211 if (s->slice.y == 0)
2212 y += s->img->vmargin;
2214 if (s->img->pixmap)
2216 if (s->img->mask)
2218 /* We can't set both a clip mask and use XSetClipRectangles
2219 because the latter also sets a clip mask. We also can't
2220 trust on the shape extension to be available
2221 (XShapeCombineRegion). So, compute the rectangle to draw
2222 manually. */
2223 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2224 | GCFunction);
2225 XGCValues xgcv;
2226 XRectangle clip_rect, image_rect, r;
2228 xgcv.clip_mask = s->img->mask;
2229 xgcv.clip_x_origin = x;
2230 xgcv.clip_y_origin = y;
2231 xgcv.function = GXcopy;
2232 XChangeGC (s->display, s->gc, mask, &xgcv);
2234 get_glyph_string_clip_rect (s, &clip_rect);
2235 image_rect.x = x;
2236 image_rect.y = y;
2237 image_rect.width = s->slice.width;
2238 image_rect.height = s->slice.height;
2239 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2240 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2241 s->slice.x + r.x - x, s->slice.y + r.y - y,
2242 r.width, r.height, r.x, r.y);
2244 else
2246 XRectangle clip_rect, image_rect, r;
2248 get_glyph_string_clip_rect (s, &clip_rect);
2249 image_rect.x = x;
2250 image_rect.y = y;
2251 image_rect.width = s->slice.width;
2252 image_rect.height = s->slice.height;
2253 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2254 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2255 s->slice.x + r.x - x, s->slice.y + r.y - y,
2256 r.width, r.height, r.x, r.y);
2258 /* When the image has a mask, we can expect that at
2259 least part of a mouse highlight or a block cursor will
2260 be visible. If the image doesn't have a mask, make
2261 a block cursor visible by drawing a rectangle around
2262 the image. I believe it's looking better if we do
2263 nothing here for mouse-face. */
2264 if (s->hl == DRAW_CURSOR)
2266 int r = s->img->relief;
2267 if (r < 0) r = -r;
2268 XDrawRectangle (s->display, s->window, s->gc,
2269 x - r, y - r,
2270 s->slice.width + r*2 - 1,
2271 s->slice.height + r*2 - 1);
2275 else
2276 /* Draw a rectangle if image could not be loaded. */
2277 XDrawRectangle (s->display, s->window, s->gc, x, y,
2278 s->slice.width - 1, s->slice.height - 1);
2282 /* Draw a relief around the image glyph string S. */
2284 static void
2285 x_draw_image_relief (s)
2286 struct glyph_string *s;
2288 int x0, y0, x1, y1, thick, raised_p;
2289 XRectangle r;
2290 int x = s->x;
2291 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2293 /* If first glyph of S has a left box line, start drawing it to the
2294 right of that line. */
2295 if (s->face->box != FACE_NO_BOX
2296 && s->first_glyph->left_box_line_p
2297 && s->slice.x == 0)
2298 x += eabs (s->face->box_line_width);
2300 /* If there is a margin around the image, adjust x- and y-position
2301 by that margin. */
2302 if (s->slice.x == 0)
2303 x += s->img->hmargin;
2304 if (s->slice.y == 0)
2305 y += s->img->vmargin;
2307 if (s->hl == DRAW_IMAGE_SUNKEN
2308 || s->hl == DRAW_IMAGE_RAISED)
2310 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2311 raised_p = s->hl == DRAW_IMAGE_RAISED;
2313 else
2315 thick = eabs (s->img->relief);
2316 raised_p = s->img->relief > 0;
2319 x0 = x - thick;
2320 y0 = y - thick;
2321 x1 = x + s->slice.width + thick - 1;
2322 y1 = y + s->slice.height + thick - 1;
2324 x_setup_relief_colors (s);
2325 get_glyph_string_clip_rect (s, &r);
2326 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2327 s->slice.y == 0,
2328 s->slice.y + s->slice.height == s->img->height,
2329 s->slice.x == 0,
2330 s->slice.x + s->slice.width == s->img->width,
2331 &r);
2335 /* Draw the foreground of image glyph string S to PIXMAP. */
2337 static void
2338 x_draw_image_foreground_1 (s, pixmap)
2339 struct glyph_string *s;
2340 Pixmap pixmap;
2342 int x = 0;
2343 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2345 /* If first glyph of S has a left box line, start drawing it to the
2346 right of that line. */
2347 if (s->face->box != FACE_NO_BOX
2348 && s->first_glyph->left_box_line_p
2349 && s->slice.x == 0)
2350 x += eabs (s->face->box_line_width);
2352 /* If there is a margin around the image, adjust x- and y-position
2353 by that margin. */
2354 if (s->slice.x == 0)
2355 x += s->img->hmargin;
2356 if (s->slice.y == 0)
2357 y += s->img->vmargin;
2359 if (s->img->pixmap)
2361 if (s->img->mask)
2363 /* We can't set both a clip mask and use XSetClipRectangles
2364 because the latter also sets a clip mask. We also can't
2365 trust on the shape extension to be available
2366 (XShapeCombineRegion). So, compute the rectangle to draw
2367 manually. */
2368 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2369 | GCFunction);
2370 XGCValues xgcv;
2372 xgcv.clip_mask = s->img->mask;
2373 xgcv.clip_x_origin = x - s->slice.x;
2374 xgcv.clip_y_origin = y - s->slice.y;
2375 xgcv.function = GXcopy;
2376 XChangeGC (s->display, s->gc, mask, &xgcv);
2378 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2379 s->slice.x, s->slice.y,
2380 s->slice.width, s->slice.height, x, y);
2381 XSetClipMask (s->display, s->gc, None);
2383 else
2385 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2386 s->slice.x, s->slice.y,
2387 s->slice.width, s->slice.height, x, y);
2389 /* When the image has a mask, we can expect that at
2390 least part of a mouse highlight or a block cursor will
2391 be visible. If the image doesn't have a mask, make
2392 a block cursor visible by drawing a rectangle around
2393 the image. I believe it's looking better if we do
2394 nothing here for mouse-face. */
2395 if (s->hl == DRAW_CURSOR)
2397 int r = s->img->relief;
2398 if (r < 0) r = -r;
2399 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2400 s->slice.width + r*2 - 1,
2401 s->slice.height + r*2 - 1);
2405 else
2406 /* Draw a rectangle if image could not be loaded. */
2407 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2408 s->slice.width - 1, s->slice.height - 1);
2412 /* Draw part of the background of glyph string S. X, Y, W, and H
2413 give the rectangle to draw. */
2415 static void
2416 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2417 struct glyph_string *s;
2418 int x, y, w, h;
2420 if (s->stippled_p)
2422 /* Fill background with a stipple pattern. */
2423 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2424 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2425 XSetFillStyle (s->display, s->gc, FillSolid);
2427 else
2428 x_clear_glyph_string_rect (s, x, y, w, h);
2432 /* Draw image glyph string S.
2434 s->y
2435 s->x +-------------------------
2436 | s->face->box
2438 | +-------------------------
2439 | | s->img->margin
2441 | | +-------------------
2442 | | | the image
2446 static void
2447 x_draw_image_glyph_string (s)
2448 struct glyph_string *s;
2450 int box_line_hwidth = eabs (s->face->box_line_width);
2451 int box_line_vwidth = max (s->face->box_line_width, 0);
2452 int height;
2453 Pixmap pixmap = None;
2455 height = s->height;
2456 if (s->slice.y == 0)
2457 height -= box_line_vwidth;
2458 if (s->slice.y + s->slice.height >= s->img->height)
2459 height -= box_line_vwidth;
2461 /* Fill background with face under the image. Do it only if row is
2462 taller than image or if image has a clip mask to reduce
2463 flickering. */
2464 s->stippled_p = s->face->stipple != 0;
2465 if (height > s->slice.height
2466 || s->img->hmargin
2467 || s->img->vmargin
2468 || s->img->mask
2469 || s->img->pixmap == 0
2470 || s->width != s->background_width)
2472 if (s->img->mask)
2474 /* Create a pixmap as large as the glyph string. Fill it
2475 with the background color. Copy the image to it, using
2476 its mask. Copy the temporary pixmap to the display. */
2477 Screen *screen = FRAME_X_SCREEN (s->f);
2478 int depth = DefaultDepthOfScreen (screen);
2480 /* Create a pixmap as large as the glyph string. */
2481 pixmap = XCreatePixmap (s->display, s->window,
2482 s->background_width,
2483 s->height, depth);
2485 /* Don't clip in the following because we're working on the
2486 pixmap. */
2487 XSetClipMask (s->display, s->gc, None);
2489 /* Fill the pixmap with the background color/stipple. */
2490 if (s->stippled_p)
2492 /* Fill background with a stipple pattern. */
2493 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2494 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2495 XFillRectangle (s->display, pixmap, s->gc,
2496 0, 0, s->background_width, s->height);
2497 XSetFillStyle (s->display, s->gc, FillSolid);
2498 XSetTSOrigin (s->display, s->gc, 0, 0);
2500 else
2502 XGCValues xgcv;
2503 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2504 &xgcv);
2505 XSetForeground (s->display, s->gc, xgcv.background);
2506 XFillRectangle (s->display, pixmap, s->gc,
2507 0, 0, s->background_width, s->height);
2508 XSetForeground (s->display, s->gc, xgcv.foreground);
2511 else
2513 int x = s->x;
2514 int y = s->y;
2516 if (s->first_glyph->left_box_line_p
2517 && s->slice.x == 0)
2518 x += box_line_hwidth;
2520 if (s->slice.y == 0)
2521 y += box_line_vwidth;
2523 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2526 s->background_filled_p = 1;
2529 /* Draw the foreground. */
2530 if (pixmap != None)
2532 x_draw_image_foreground_1 (s, pixmap);
2533 x_set_glyph_string_clipping (s);
2534 XCopyArea (s->display, pixmap, s->window, s->gc,
2535 0, 0, s->background_width, s->height, s->x, s->y);
2536 XFreePixmap (s->display, pixmap);
2538 else
2539 x_draw_image_foreground (s);
2541 /* If we must draw a relief around the image, do it. */
2542 if (s->img->relief
2543 || s->hl == DRAW_IMAGE_RAISED
2544 || s->hl == DRAW_IMAGE_SUNKEN)
2545 x_draw_image_relief (s);
2549 /* Draw stretch glyph string S. */
2551 static void
2552 x_draw_stretch_glyph_string (s)
2553 struct glyph_string *s;
2555 xassert (s->first_glyph->type == STRETCH_GLYPH);
2557 if (s->hl == DRAW_CURSOR
2558 && !x_stretch_cursor_p)
2560 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2561 as wide as the stretch glyph. */
2562 int width, background_width = s->background_width;
2563 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2565 if (x < left_x)
2567 background_width -= left_x - x;
2568 x = left_x;
2570 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2572 /* Draw cursor. */
2573 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2575 /* Clear rest using the GC of the original non-cursor face. */
2576 if (width < background_width)
2578 int y = s->y;
2579 int w = background_width - width, h = s->height;
2580 XRectangle r;
2581 GC gc;
2583 x += width;
2584 if (s->row->mouse_face_p
2585 && cursor_in_mouse_face_p (s->w))
2587 x_set_mouse_face_gc (s);
2588 gc = s->gc;
2590 else
2591 gc = s->face->gc;
2593 get_glyph_string_clip_rect (s, &r);
2594 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2596 if (s->face->stipple)
2598 /* Fill background with a stipple pattern. */
2599 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2600 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2601 XSetFillStyle (s->display, gc, FillSolid);
2603 else
2605 XGCValues xgcv;
2606 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2607 XSetForeground (s->display, gc, xgcv.background);
2608 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2609 XSetForeground (s->display, gc, xgcv.foreground);
2613 else if (!s->background_filled_p)
2615 int background_width = s->background_width;
2616 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2618 /* Don't draw into left margin, fringe or scrollbar area
2619 except for header line and mode line. */
2620 if (x < left_x && !s->row->mode_line_p)
2622 background_width -= left_x - x;
2623 x = left_x;
2625 if (background_width > 0)
2626 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2629 s->background_filled_p = 1;
2633 /* Draw glyph string S. */
2635 static void
2636 x_draw_glyph_string (s)
2637 struct glyph_string *s;
2639 int relief_drawn_p = 0;
2641 /* If S draws into the background of its successors, draw the
2642 background of the successors first so that S can draw into it.
2643 This makes S->next use XDrawString instead of XDrawImageString. */
2644 if (s->next && s->right_overhang && !s->for_overlaps)
2646 int width;
2647 struct glyph_string *next;
2649 for (width = 0, next = s->next;
2650 next && width < s->right_overhang;
2651 width += next->width, next = next->next)
2652 if (next->first_glyph->type != IMAGE_GLYPH)
2654 x_set_glyph_string_gc (next);
2655 x_set_glyph_string_clipping (next);
2656 if (next->first_glyph->type == STRETCH_GLYPH)
2657 x_draw_stretch_glyph_string (next);
2658 else
2659 x_draw_glyph_string_background (next, 1);
2660 next->num_clips = 0;
2664 /* Set up S->gc, set clipping and draw S. */
2665 x_set_glyph_string_gc (s);
2667 /* Draw relief (if any) in advance for char/composition so that the
2668 glyph string can be drawn over it. */
2669 if (!s->for_overlaps
2670 && s->face->box != FACE_NO_BOX
2671 && (s->first_glyph->type == CHAR_GLYPH
2672 || s->first_glyph->type == COMPOSITE_GLYPH))
2675 x_set_glyph_string_clipping (s);
2676 x_draw_glyph_string_background (s, 1);
2677 x_draw_glyph_string_box (s);
2678 x_set_glyph_string_clipping (s);
2679 relief_drawn_p = 1;
2681 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2682 && !s->clip_tail
2683 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2684 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2685 /* We must clip just this glyph. left_overhang part has already
2686 drawn when s->prev was drawn, and right_overhang part will be
2687 drawn later when s->next is drawn. */
2688 x_set_glyph_string_clipping_exactly (s, s);
2689 else
2690 x_set_glyph_string_clipping (s);
2692 switch (s->first_glyph->type)
2694 case IMAGE_GLYPH:
2695 x_draw_image_glyph_string (s);
2696 break;
2698 case STRETCH_GLYPH:
2699 x_draw_stretch_glyph_string (s);
2700 break;
2702 case CHAR_GLYPH:
2703 if (s->for_overlaps)
2704 s->background_filled_p = 1;
2705 else
2706 x_draw_glyph_string_background (s, 0);
2707 x_draw_glyph_string_foreground (s);
2708 break;
2710 case COMPOSITE_GLYPH:
2711 if (s->for_overlaps || (s->cmp_from > 0
2712 && ! s->first_glyph->u.cmp.automatic))
2713 s->background_filled_p = 1;
2714 else
2715 x_draw_glyph_string_background (s, 1);
2716 x_draw_composite_glyph_string_foreground (s);
2717 break;
2719 default:
2720 abort ();
2723 if (!s->for_overlaps)
2725 /* Draw underline. */
2726 if (s->face->underline_p)
2728 unsigned long thickness, position;
2729 int y;
2731 if (s->prev && s->prev->face->underline_p)
2733 /* We use the same underline style as the previous one. */
2734 thickness = s->prev->underline_thickness;
2735 position = s->prev->underline_position;
2737 else
2739 /* Get the underline thickness. Default is 1 pixel. */
2740 if (s->font && s->font->underline_thickness > 0)
2741 thickness = s->font->underline_thickness;
2742 else
2743 thickness = 1;
2744 if (x_underline_at_descent_line)
2745 position = (s->height - thickness) - (s->ybase - s->y);
2746 else
2748 /* Get the underline position. This is the recommended
2749 vertical offset in pixels from the baseline to the top of
2750 the underline. This is a signed value according to the
2751 specs, and its default is
2753 ROUND ((maximum descent) / 2), with
2754 ROUND(x) = floor (x + 0.5) */
2756 if (x_use_underline_position_properties
2757 && s->font && s->font->underline_position >= 0)
2758 position = s->font->underline_position;
2759 else if (s->font)
2760 position = (s->font->descent + 1) / 2;
2761 else
2762 position = underline_minimum_offset;
2764 position = max (position, underline_minimum_offset);
2766 /* Check the sanity of thickness and position. We should
2767 avoid drawing underline out of the current line area. */
2768 if (s->y + s->height <= s->ybase + position)
2769 position = (s->height - 1) - (s->ybase - s->y);
2770 if (s->y + s->height < s->ybase + position + thickness)
2771 thickness = (s->y + s->height) - (s->ybase + position);
2772 s->underline_thickness = thickness;
2773 s->underline_position = position;
2774 y = s->ybase + position;
2775 if (s->face->underline_defaulted_p)
2776 XFillRectangle (s->display, s->window, s->gc,
2777 s->x, y, s->width, thickness);
2778 else
2780 XGCValues xgcv;
2781 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2782 XSetForeground (s->display, s->gc, s->face->underline_color);
2783 XFillRectangle (s->display, s->window, s->gc,
2784 s->x, y, s->width, thickness);
2785 XSetForeground (s->display, s->gc, xgcv.foreground);
2789 /* Draw overline. */
2790 if (s->face->overline_p)
2792 unsigned long dy = 0, h = 1;
2794 if (s->face->overline_color_defaulted_p)
2795 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2796 s->width, h);
2797 else
2799 XGCValues xgcv;
2800 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2801 XSetForeground (s->display, s->gc, s->face->overline_color);
2802 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2803 s->width, h);
2804 XSetForeground (s->display, s->gc, xgcv.foreground);
2808 /* Draw strike-through. */
2809 if (s->face->strike_through_p)
2811 unsigned long h = 1;
2812 unsigned long dy = (s->height - h) / 2;
2814 if (s->face->strike_through_color_defaulted_p)
2815 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2816 s->width, h);
2817 else
2819 XGCValues xgcv;
2820 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2821 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2822 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2823 s->width, h);
2824 XSetForeground (s->display, s->gc, xgcv.foreground);
2828 /* Draw relief if not yet drawn. */
2829 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2830 x_draw_glyph_string_box (s);
2832 if (s->prev)
2834 struct glyph_string *prev;
2836 for (prev = s->prev; prev; prev = prev->prev)
2837 if (prev->hl != s->hl
2838 && prev->x + prev->width + prev->right_overhang > s->x)
2840 /* As prev was drawn while clipped to its own area, we
2841 must draw the right_overhang part using s->hl now. */
2842 enum draw_glyphs_face save = prev->hl;
2844 prev->hl = s->hl;
2845 x_set_glyph_string_gc (prev);
2846 x_set_glyph_string_clipping_exactly (s, prev);
2847 if (prev->first_glyph->type == CHAR_GLYPH)
2848 x_draw_glyph_string_foreground (prev);
2849 else
2850 x_draw_composite_glyph_string_foreground (prev);
2851 XSetClipMask (prev->display, prev->gc, None);
2852 prev->hl = save;
2853 prev->num_clips = 0;
2857 if (s->next)
2859 struct glyph_string *next;
2861 for (next = s->next; next; next = next->next)
2862 if (next->hl != s->hl
2863 && next->x - next->left_overhang < s->x + s->width)
2865 /* As next will be drawn while clipped to its own area,
2866 we must draw the left_overhang part using s->hl now. */
2867 enum draw_glyphs_face save = next->hl;
2869 next->hl = s->hl;
2870 x_set_glyph_string_gc (next);
2871 x_set_glyph_string_clipping_exactly (s, next);
2872 if (next->first_glyph->type == CHAR_GLYPH)
2873 x_draw_glyph_string_foreground (next);
2874 else
2875 x_draw_composite_glyph_string_foreground (next);
2876 XSetClipMask (next->display, next->gc, None);
2877 next->hl = save;
2878 next->num_clips = 0;
2883 /* Reset clipping. */
2884 XSetClipMask (s->display, s->gc, None);
2885 s->num_clips = 0;
2888 /* Shift display to make room for inserted glyphs. */
2890 void
2891 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2892 struct frame *f;
2893 int x, y, width, height, shift_by;
2895 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2896 f->output_data.x->normal_gc,
2897 x, y, width, height,
2898 x + shift_by, y);
2901 /* Delete N glyphs at the nominal cursor position. Not implemented
2902 for X frames. */
2904 static void
2905 x_delete_glyphs (f, n)
2906 struct frame *f;
2907 register int n;
2909 abort ();
2913 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2914 If they are <= 0, this is probably an error. */
2916 void
2917 x_clear_area (dpy, window, x, y, width, height, exposures)
2918 Display *dpy;
2919 Window window;
2920 int x, y;
2921 int width, height;
2922 int exposures;
2924 xassert (width > 0 && height > 0);
2925 XClearArea (dpy, window, x, y, width, height, exposures);
2929 /* Clear an entire frame. */
2931 static void
2932 x_clear_frame (struct frame *f)
2934 /* Clearing the frame will erase any cursor, so mark them all as no
2935 longer visible. */
2936 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2937 output_cursor.hpos = output_cursor.vpos = 0;
2938 output_cursor.x = -1;
2940 /* We don't set the output cursor here because there will always
2941 follow an explicit cursor_to. */
2942 BLOCK_INPUT;
2943 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2945 /* We have to clear the scroll bars, too. If we have changed
2946 colors or something like that, then they should be notified. */
2947 x_scroll_bar_clear (f);
2949 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2950 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2951 redisplay, do it here. */
2952 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2953 #endif
2955 XFlush (FRAME_X_DISPLAY (f));
2957 UNBLOCK_INPUT;
2962 /* Invert the middle quarter of the frame for .15 sec. */
2964 /* We use the select system call to do the waiting, so we have to make
2965 sure it's available. If it isn't, we just won't do visual bells. */
2967 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2970 /* Subtract the `struct timeval' values X and Y, storing the result in
2971 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2973 static int
2974 timeval_subtract (result, x, y)
2975 struct timeval *result, x, y;
2977 /* Perform the carry for the later subtraction by updating y. This
2978 is safer because on some systems the tv_sec member is unsigned. */
2979 if (x.tv_usec < y.tv_usec)
2981 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2982 y.tv_usec -= 1000000 * nsec;
2983 y.tv_sec += nsec;
2986 if (x.tv_usec - y.tv_usec > 1000000)
2988 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2989 y.tv_usec += 1000000 * nsec;
2990 y.tv_sec -= nsec;
2993 /* Compute the time remaining to wait. tv_usec is certainly
2994 positive. */
2995 result->tv_sec = x.tv_sec - y.tv_sec;
2996 result->tv_usec = x.tv_usec - y.tv_usec;
2998 /* Return indication of whether the result should be considered
2999 negative. */
3000 return x.tv_sec < y.tv_sec;
3003 void
3004 XTflash (f)
3005 struct frame *f;
3007 BLOCK_INPUT;
3010 GC gc;
3012 /* Create a GC that will use the GXxor function to flip foreground
3013 pixels into background pixels. */
3015 XGCValues values;
3017 values.function = GXxor;
3018 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3019 ^ FRAME_BACKGROUND_PIXEL (f));
3021 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3022 GCFunction | GCForeground, &values);
3026 /* Get the height not including a menu bar widget. */
3027 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
3028 /* Height of each line to flash. */
3029 int flash_height = FRAME_LINE_HEIGHT (f);
3030 /* These will be the left and right margins of the rectangles. */
3031 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3032 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3034 int width;
3036 /* Don't flash the area between a scroll bar and the frame
3037 edge it is next to. */
3038 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3040 case vertical_scroll_bar_left:
3041 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3042 break;
3044 case vertical_scroll_bar_right:
3045 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3046 break;
3048 default:
3049 break;
3052 width = flash_right - flash_left;
3054 /* If window is tall, flash top and bottom line. */
3055 if (height > 3 * FRAME_LINE_HEIGHT (f))
3057 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3058 flash_left,
3059 (FRAME_INTERNAL_BORDER_WIDTH (f)
3060 + FRAME_TOP_MARGIN_HEIGHT (f)),
3061 width, flash_height);
3062 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3063 flash_left,
3064 (height - flash_height
3065 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3066 width, flash_height);
3068 else
3069 /* If it is short, flash it all. */
3070 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3071 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3072 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3074 x_flush (f);
3077 struct timeval wakeup;
3079 EMACS_GET_TIME (wakeup);
3081 /* Compute time to wait until, propagating carry from usecs. */
3082 wakeup.tv_usec += 150000;
3083 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3084 wakeup.tv_usec %= 1000000;
3086 /* Keep waiting until past the time wakeup or any input gets
3087 available. */
3088 while (! detect_input_pending ())
3090 struct timeval current;
3091 struct timeval timeout;
3093 EMACS_GET_TIME (current);
3095 /* Break if result would be negative. */
3096 if (timeval_subtract (&current, wakeup, current))
3097 break;
3099 /* How long `select' should wait. */
3100 timeout.tv_sec = 0;
3101 timeout.tv_usec = 10000;
3103 /* Try to wait that long--but we might wake up sooner. */
3104 select (0, NULL, NULL, NULL, &timeout);
3108 /* If window is tall, flash top and bottom line. */
3109 if (height > 3 * FRAME_LINE_HEIGHT (f))
3111 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3112 flash_left,
3113 (FRAME_INTERNAL_BORDER_WIDTH (f)
3114 + FRAME_TOP_MARGIN_HEIGHT (f)),
3115 width, flash_height);
3116 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3117 flash_left,
3118 (height - flash_height
3119 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3120 width, flash_height);
3122 else
3123 /* If it is short, flash it all. */
3124 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3125 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3126 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3128 XFreeGC (FRAME_X_DISPLAY (f), gc);
3129 x_flush (f);
3133 UNBLOCK_INPUT;
3136 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3139 static void
3140 XTtoggle_invisible_pointer (f, invisible)
3141 FRAME_PTR f;
3142 int invisible;
3144 BLOCK_INPUT;
3145 if (invisible)
3147 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor != 0)
3148 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3149 FRAME_X_DISPLAY_INFO (f)->invisible_cursor);
3151 else
3152 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3153 f->output_data.x->current_cursor);
3154 f->pointer_invisible = invisible;
3155 UNBLOCK_INPUT;
3159 /* Make audible bell. */
3161 void
3162 XTring_bell ()
3164 struct frame *f = SELECTED_FRAME ();
3166 if (FRAME_X_DISPLAY (f))
3168 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3169 if (visible_bell)
3170 XTflash (f);
3171 else
3172 #endif
3174 BLOCK_INPUT;
3175 XBell (FRAME_X_DISPLAY (f), 0);
3176 XFlush (FRAME_X_DISPLAY (f));
3177 UNBLOCK_INPUT;
3183 /* Specify how many text lines, from the top of the window,
3184 should be affected by insert-lines and delete-lines operations.
3185 This, and those operations, are used only within an update
3186 that is bounded by calls to x_update_begin and x_update_end. */
3188 static void
3189 XTset_terminal_window (n)
3190 register int n;
3192 /* This function intentionally left blank. */
3197 /***********************************************************************
3198 Line Dance
3199 ***********************************************************************/
3201 /* Perform an insert-lines or delete-lines operation, inserting N
3202 lines or deleting -N lines at vertical position VPOS. */
3204 static void
3205 x_ins_del_lines (f, vpos, n)
3206 struct frame *f;
3207 int vpos, n;
3209 abort ();
3213 /* Scroll part of the display as described by RUN. */
3215 static void
3216 x_scroll_run (w, run)
3217 struct window *w;
3218 struct run *run;
3220 struct frame *f = XFRAME (w->frame);
3221 int x, y, width, height, from_y, to_y, bottom_y;
3223 /* Get frame-relative bounding box of the text display area of W,
3224 without mode lines. Include in this box the left and right
3225 fringe of W. */
3226 window_box (w, -1, &x, &y, &width, &height);
3228 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3229 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3230 bottom_y = y + height;
3232 if (to_y < from_y)
3234 /* Scrolling up. Make sure we don't copy part of the mode
3235 line at the bottom. */
3236 if (from_y + run->height > bottom_y)
3237 height = bottom_y - from_y;
3238 else
3239 height = run->height;
3241 else
3243 /* Scolling down. Make sure we don't copy over the mode line.
3244 at the bottom. */
3245 if (to_y + run->height > bottom_y)
3246 height = bottom_y - to_y;
3247 else
3248 height = run->height;
3251 BLOCK_INPUT;
3253 /* Cursor off. Will be switched on again in x_update_window_end. */
3254 updated_window = w;
3255 x_clear_cursor (w);
3257 XCopyArea (FRAME_X_DISPLAY (f),
3258 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3259 f->output_data.x->normal_gc,
3260 x, from_y,
3261 width, height,
3262 x, to_y);
3264 UNBLOCK_INPUT;
3269 /***********************************************************************
3270 Exposure Events
3271 ***********************************************************************/
3274 static void
3275 frame_highlight (f)
3276 struct frame *f;
3278 /* We used to only do this if Vx_no_window_manager was non-nil, but
3279 the ICCCM (section 4.1.6) says that the window's border pixmap
3280 and border pixel are window attributes which are "private to the
3281 client", so we can always change it to whatever we want. */
3282 BLOCK_INPUT;
3283 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3284 f->output_data.x->border_pixel);
3285 UNBLOCK_INPUT;
3286 x_update_cursor (f, 1);
3287 x_set_frame_alpha (f);
3290 static void
3291 frame_unhighlight (f)
3292 struct frame *f;
3294 /* We used to only do this if Vx_no_window_manager was non-nil, but
3295 the ICCCM (section 4.1.6) says that the window's border pixmap
3296 and border pixel are window attributes which are "private to the
3297 client", so we can always change it to whatever we want. */
3298 BLOCK_INPUT;
3299 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3300 f->output_data.x->border_tile);
3301 UNBLOCK_INPUT;
3302 x_update_cursor (f, 1);
3303 x_set_frame_alpha (f);
3306 /* The focus has changed. Update the frames as necessary to reflect
3307 the new situation. Note that we can't change the selected frame
3308 here, because the Lisp code we are interrupting might become confused.
3309 Each event gets marked with the frame in which it occurred, so the
3310 Lisp code can tell when the switch took place by examining the events. */
3312 static void
3313 x_new_focus_frame (dpyinfo, frame)
3314 struct x_display_info *dpyinfo;
3315 struct frame *frame;
3317 struct frame *old_focus = dpyinfo->x_focus_frame;
3319 if (frame != dpyinfo->x_focus_frame)
3321 /* Set this before calling other routines, so that they see
3322 the correct value of x_focus_frame. */
3323 dpyinfo->x_focus_frame = frame;
3325 if (old_focus && old_focus->auto_lower)
3326 x_lower_frame (old_focus);
3328 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3329 pending_autoraise_frame = dpyinfo->x_focus_frame;
3330 else
3331 pending_autoraise_frame = 0;
3334 x_frame_rehighlight (dpyinfo);
3337 /* Handle FocusIn and FocusOut state changes for FRAME.
3338 If FRAME has focus and there exists more than one frame, puts
3339 a FOCUS_IN_EVENT into *BUFP. */
3341 static void
3342 x_focus_changed (type, state, dpyinfo, frame, bufp)
3343 int type;
3344 int state;
3345 struct x_display_info *dpyinfo;
3346 struct frame *frame;
3347 struct input_event *bufp;
3349 if (type == FocusIn)
3351 if (dpyinfo->x_focus_event_frame != frame)
3353 x_new_focus_frame (dpyinfo, frame);
3354 dpyinfo->x_focus_event_frame = frame;
3356 /* Don't stop displaying the initial startup message
3357 for a switch-frame event we don't need. */
3358 if (NILP (Vterminal_frame)
3359 && CONSP (Vframe_list)
3360 && !NILP (XCDR (Vframe_list)))
3362 bufp->kind = FOCUS_IN_EVENT;
3363 XSETFRAME (bufp->frame_or_window, frame);
3367 frame->output_data.x->focus_state |= state;
3369 #ifdef HAVE_X_I18N
3370 if (FRAME_XIC (frame))
3371 XSetICFocus (FRAME_XIC (frame));
3372 #endif
3374 else if (type == FocusOut)
3376 frame->output_data.x->focus_state &= ~state;
3378 if (dpyinfo->x_focus_event_frame == frame)
3380 dpyinfo->x_focus_event_frame = 0;
3381 x_new_focus_frame (dpyinfo, 0);
3384 #ifdef HAVE_X_I18N
3385 if (FRAME_XIC (frame))
3386 XUnsetICFocus (FRAME_XIC (frame));
3387 #endif
3388 if (frame->pointer_invisible)
3389 XTtoggle_invisible_pointer (frame, 0);
3393 /* The focus may have changed. Figure out if it is a real focus change,
3394 by checking both FocusIn/Out and Enter/LeaveNotify events.
3396 Returns FOCUS_IN_EVENT event in *BUFP. */
3398 static void
3399 x_detect_focus_change (dpyinfo, event, bufp)
3400 struct x_display_info *dpyinfo;
3401 XEvent *event;
3402 struct input_event *bufp;
3404 struct frame *frame;
3406 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3407 if (! frame)
3408 return;
3410 switch (event->type)
3412 case EnterNotify:
3413 case LeaveNotify:
3415 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3416 int focus_state
3417 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3419 if (event->xcrossing.detail != NotifyInferior
3420 && event->xcrossing.focus
3421 && ! (focus_state & FOCUS_EXPLICIT))
3422 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3423 FOCUS_IMPLICIT,
3424 dpyinfo, frame, bufp);
3426 break;
3428 case FocusIn:
3429 case FocusOut:
3430 x_focus_changed (event->type,
3431 (event->xfocus.detail == NotifyPointer ?
3432 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3433 dpyinfo, frame, bufp);
3434 break;
3436 case ClientMessage:
3437 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3439 enum xembed_message msg = event->xclient.data.l[1];
3440 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3441 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3443 break;
3448 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3450 void
3451 x_mouse_leave (dpyinfo)
3452 struct x_display_info *dpyinfo;
3454 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3457 /* The focus has changed, or we have redirected a frame's focus to
3458 another frame (this happens when a frame uses a surrogate
3459 mini-buffer frame). Shift the highlight as appropriate.
3461 The FRAME argument doesn't necessarily have anything to do with which
3462 frame is being highlighted or un-highlighted; we only use it to find
3463 the appropriate X display info. */
3465 static void
3466 XTframe_rehighlight (frame)
3467 struct frame *frame;
3469 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3472 static void
3473 x_frame_rehighlight (dpyinfo)
3474 struct x_display_info *dpyinfo;
3476 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3478 if (dpyinfo->x_focus_frame)
3480 dpyinfo->x_highlight_frame
3481 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3482 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3483 : dpyinfo->x_focus_frame);
3484 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3486 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3487 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3490 else
3491 dpyinfo->x_highlight_frame = 0;
3493 if (dpyinfo->x_highlight_frame != old_highlight)
3495 if (old_highlight)
3496 frame_unhighlight (old_highlight);
3497 if (dpyinfo->x_highlight_frame)
3498 frame_highlight (dpyinfo->x_highlight_frame);
3504 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3506 /* Initialize mode_switch_bit and modifier_meaning. */
3507 static void
3508 x_find_modifier_meanings (dpyinfo)
3509 struct x_display_info *dpyinfo;
3511 int min_code, max_code;
3512 KeySym *syms;
3513 int syms_per_code;
3514 XModifierKeymap *mods;
3516 dpyinfo->meta_mod_mask = 0;
3517 dpyinfo->shift_lock_mask = 0;
3518 dpyinfo->alt_mod_mask = 0;
3519 dpyinfo->super_mod_mask = 0;
3520 dpyinfo->hyper_mod_mask = 0;
3522 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3524 syms = XGetKeyboardMapping (dpyinfo->display,
3525 min_code, max_code - min_code + 1,
3526 &syms_per_code);
3527 mods = XGetModifierMapping (dpyinfo->display);
3529 /* Scan the modifier table to see which modifier bits the Meta and
3530 Alt keysyms are on. */
3532 int row, col; /* The row and column in the modifier table. */
3533 int found_alt_or_meta;
3535 for (row = 3; row < 8; row++)
3537 found_alt_or_meta = 0;
3538 for (col = 0; col < mods->max_keypermod; col++)
3540 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3542 /* Zeroes are used for filler. Skip them. */
3543 if (code == 0)
3544 continue;
3546 /* Are any of this keycode's keysyms a meta key? */
3548 int code_col;
3550 for (code_col = 0; code_col < syms_per_code; code_col++)
3552 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3554 switch (sym)
3556 case XK_Meta_L:
3557 case XK_Meta_R:
3558 found_alt_or_meta = 1;
3559 dpyinfo->meta_mod_mask |= (1 << row);
3560 break;
3562 case XK_Alt_L:
3563 case XK_Alt_R:
3564 found_alt_or_meta = 1;
3565 dpyinfo->alt_mod_mask |= (1 << row);
3566 break;
3568 case XK_Hyper_L:
3569 case XK_Hyper_R:
3570 if (!found_alt_or_meta)
3571 dpyinfo->hyper_mod_mask |= (1 << row);
3572 code_col = syms_per_code;
3573 col = mods->max_keypermod;
3574 break;
3576 case XK_Super_L:
3577 case XK_Super_R:
3578 if (!found_alt_or_meta)
3579 dpyinfo->super_mod_mask |= (1 << row);
3580 code_col = syms_per_code;
3581 col = mods->max_keypermod;
3582 break;
3584 case XK_Shift_Lock:
3585 /* Ignore this if it's not on the lock modifier. */
3586 if (!found_alt_or_meta && ((1 << row) == LockMask))
3587 dpyinfo->shift_lock_mask = LockMask;
3588 code_col = syms_per_code;
3589 col = mods->max_keypermod;
3590 break;
3598 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3599 if (! dpyinfo->meta_mod_mask)
3601 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3602 dpyinfo->alt_mod_mask = 0;
3605 /* If some keys are both alt and meta,
3606 make them just meta, not alt. */
3607 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3609 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3612 XFree ((char *) syms);
3613 XFreeModifiermap (mods);
3616 /* Convert between the modifier bits X uses and the modifier bits
3617 Emacs uses. */
3619 unsigned int
3620 x_x_to_emacs_modifiers (dpyinfo, state)
3621 struct x_display_info *dpyinfo;
3622 unsigned int state;
3624 EMACS_UINT mod_meta = meta_modifier;
3625 EMACS_UINT mod_alt = alt_modifier;
3626 EMACS_UINT mod_hyper = hyper_modifier;
3627 EMACS_UINT mod_super = super_modifier;
3628 Lisp_Object tem;
3630 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3631 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3632 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3633 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3634 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3635 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3636 tem = Fget (Vx_super_keysym, Qmodifier_value);
3637 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3640 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3641 | ((state & ControlMask) ? ctrl_modifier : 0)
3642 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3643 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3644 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3645 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3648 static unsigned int
3649 x_emacs_to_x_modifiers (dpyinfo, state)
3650 struct x_display_info *dpyinfo;
3651 unsigned int state;
3653 EMACS_UINT mod_meta = meta_modifier;
3654 EMACS_UINT mod_alt = alt_modifier;
3655 EMACS_UINT mod_hyper = hyper_modifier;
3656 EMACS_UINT mod_super = super_modifier;
3658 Lisp_Object tem;
3660 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3661 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3662 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3663 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3664 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3665 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3666 tem = Fget (Vx_super_keysym, Qmodifier_value);
3667 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3670 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3671 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3672 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3673 | ((state & shift_modifier) ? ShiftMask : 0)
3674 | ((state & ctrl_modifier) ? ControlMask : 0)
3675 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3678 /* Convert a keysym to its name. */
3680 char *
3681 x_get_keysym_name (keysym)
3682 KeySym keysym;
3684 char *value;
3686 BLOCK_INPUT;
3687 value = XKeysymToString (keysym);
3688 UNBLOCK_INPUT;
3690 return value;
3695 /* Mouse clicks and mouse movement. Rah. */
3697 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3699 If the event is a button press, then note that we have grabbed
3700 the mouse. */
3702 static Lisp_Object
3703 construct_mouse_click (result, event, f)
3704 struct input_event *result;
3705 XButtonEvent *event;
3706 struct frame *f;
3708 /* Make the event type NO_EVENT; we'll change that when we decide
3709 otherwise. */
3710 result->kind = MOUSE_CLICK_EVENT;
3711 result->code = event->button - Button1;
3712 result->timestamp = event->time;
3713 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3714 event->state)
3715 | (event->type == ButtonRelease
3716 ? up_modifier
3717 : down_modifier));
3719 XSETINT (result->x, event->x);
3720 XSETINT (result->y, event->y);
3721 XSETFRAME (result->frame_or_window, f);
3722 result->arg = Qnil;
3723 return Qnil;
3727 /* Function to report a mouse movement to the mainstream Emacs code.
3728 The input handler calls this.
3730 We have received a mouse movement event, which is given in *event.
3731 If the mouse is over a different glyph than it was last time, tell
3732 the mainstream emacs code by setting mouse_moved. If not, ask for
3733 another motion event, so we can check again the next time it moves. */
3735 static XMotionEvent last_mouse_motion_event;
3736 static Lisp_Object last_mouse_motion_frame;
3738 static int
3739 note_mouse_movement (frame, event)
3740 FRAME_PTR frame;
3741 XMotionEvent *event;
3743 last_mouse_movement_time = event->time;
3744 last_mouse_motion_event = *event;
3745 XSETFRAME (last_mouse_motion_frame, frame);
3747 if (!FRAME_X_OUTPUT (frame))
3748 return 0;
3750 if (event->window != FRAME_X_WINDOW (frame))
3752 frame->mouse_moved = 1;
3753 last_mouse_scroll_bar = Qnil;
3754 note_mouse_highlight (frame, -1, -1);
3755 last_mouse_glyph_frame = 0;
3756 return 1;
3760 /* Has the mouse moved off the glyph it was on at the last sighting? */
3761 if (frame != last_mouse_glyph_frame
3762 || event->x < last_mouse_glyph.x
3763 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3764 || event->y < last_mouse_glyph.y
3765 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3767 frame->mouse_moved = 1;
3768 last_mouse_scroll_bar = Qnil;
3769 note_mouse_highlight (frame, event->x, event->y);
3770 /* Remember which glyph we're now on. */
3771 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3772 last_mouse_glyph_frame = frame;
3773 return 1;
3776 return 0;
3780 /************************************************************************
3781 Mouse Face
3782 ************************************************************************/
3784 static void
3785 redo_mouse_highlight ()
3787 if (!NILP (last_mouse_motion_frame)
3788 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3789 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3790 last_mouse_motion_event.x,
3791 last_mouse_motion_event.y);
3796 /* Return the current position of the mouse.
3797 *FP should be a frame which indicates which display to ask about.
3799 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3800 and *PART to the frame, window, and scroll bar part that the mouse
3801 is over. Set *X and *Y to the portion and whole of the mouse's
3802 position on the scroll bar.
3804 If the mouse movement started elsewhere, set *FP to the frame the
3805 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3806 the mouse is over.
3808 Set *TIME to the server time-stamp for the time at which the mouse
3809 was at this position.
3811 Don't store anything if we don't have a valid set of values to report.
3813 This clears the mouse_moved flag, so we can wait for the next mouse
3814 movement. */
3816 static void
3817 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3818 FRAME_PTR *fp;
3819 int insist;
3820 Lisp_Object *bar_window;
3821 enum scroll_bar_part *part;
3822 Lisp_Object *x, *y;
3823 unsigned long *time;
3825 FRAME_PTR f1;
3827 BLOCK_INPUT;
3829 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3830 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3831 else
3833 Window root;
3834 int root_x, root_y;
3836 Window dummy_window;
3837 int dummy;
3839 Lisp_Object frame, tail;
3841 /* Clear the mouse-moved flag for every frame on this display. */
3842 FOR_EACH_FRAME (tail, frame)
3843 if (FRAME_X_P (XFRAME (frame))
3844 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3845 XFRAME (frame)->mouse_moved = 0;
3847 last_mouse_scroll_bar = Qnil;
3849 /* Figure out which root window we're on. */
3850 XQueryPointer (FRAME_X_DISPLAY (*fp),
3851 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3853 /* The root window which contains the pointer. */
3854 &root,
3856 /* Trash which we can't trust if the pointer is on
3857 a different screen. */
3858 &dummy_window,
3860 /* The position on that root window. */
3861 &root_x, &root_y,
3863 /* More trash we can't trust. */
3864 &dummy, &dummy,
3866 /* Modifier keys and pointer buttons, about which
3867 we don't care. */
3868 (unsigned int *) &dummy);
3870 /* Now we have a position on the root; find the innermost window
3871 containing the pointer. */
3873 Window win, child;
3874 int win_x, win_y;
3875 int parent_x = 0, parent_y = 0;
3877 win = root;
3879 /* XTranslateCoordinates can get errors if the window
3880 structure is changing at the same time this function
3881 is running. So at least we must not crash from them. */
3883 x_catch_errors (FRAME_X_DISPLAY (*fp));
3885 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3886 && FRAME_LIVE_P (last_mouse_frame))
3888 /* If mouse was grabbed on a frame, give coords for that frame
3889 even if the mouse is now outside it. */
3890 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3892 /* From-window, to-window. */
3893 root, FRAME_X_WINDOW (last_mouse_frame),
3895 /* From-position, to-position. */
3896 root_x, root_y, &win_x, &win_y,
3898 /* Child of win. */
3899 &child);
3900 f1 = last_mouse_frame;
3902 else
3904 while (1)
3906 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3908 /* From-window, to-window. */
3909 root, win,
3911 /* From-position, to-position. */
3912 root_x, root_y, &win_x, &win_y,
3914 /* Child of win. */
3915 &child);
3917 if (child == None || child == win)
3918 break;
3919 #ifdef USE_GTK
3920 /* We don't wan't to know the innermost window. We
3921 want the edit window. For non-Gtk+ the innermost
3922 window is the edit window. For Gtk+ it might not
3923 be. It might be the tool bar for example. */
3924 if (x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win))
3925 break;
3926 #endif
3927 win = child;
3928 parent_x = win_x;
3929 parent_y = win_y;
3932 /* Now we know that:
3933 win is the innermost window containing the pointer
3934 (XTC says it has no child containing the pointer),
3935 win_x and win_y are the pointer's position in it
3936 (XTC did this the last time through), and
3937 parent_x and parent_y are the pointer's position in win's parent.
3938 (They are what win_x and win_y were when win was child.
3939 If win is the root window, it has no parent, and
3940 parent_{x,y} are invalid, but that's okay, because we'll
3941 never use them in that case.) */
3943 #ifdef USE_GTK
3944 /* We don't wan't to know the innermost window. We
3945 want the edit window. */
3946 f1 = x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3947 #else
3948 /* Is win one of our frames? */
3949 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3950 #endif
3952 #ifdef USE_X_TOOLKIT
3953 /* If we end up with the menu bar window, say it's not
3954 on the frame. */
3955 if (f1 != NULL
3956 && f1->output_data.x->menubar_widget
3957 && win == XtWindow (f1->output_data.x->menubar_widget))
3958 f1 = NULL;
3959 #endif /* USE_X_TOOLKIT */
3962 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3963 f1 = 0;
3965 x_uncatch_errors ();
3967 /* If not, is it one of our scroll bars? */
3968 if (! f1)
3970 struct scroll_bar *bar;
3972 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3974 if (bar)
3976 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3977 win_x = parent_x;
3978 win_y = parent_y;
3982 if (f1 == 0 && insist > 0)
3983 f1 = SELECTED_FRAME ();
3985 if (f1)
3987 /* Ok, we found a frame. Store all the values.
3988 last_mouse_glyph is a rectangle used to reduce the
3989 generation of mouse events. To not miss any motion
3990 events, we must divide the frame into rectangles of the
3991 size of the smallest character that could be displayed
3992 on it, i.e. into the same rectangles that matrices on
3993 the frame are divided into. */
3995 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
3996 last_mouse_glyph_frame = f1;
3998 *bar_window = Qnil;
3999 *part = 0;
4000 *fp = f1;
4001 XSETINT (*x, win_x);
4002 XSETINT (*y, win_y);
4003 *time = last_mouse_movement_time;
4008 UNBLOCK_INPUT;
4013 /***********************************************************************
4014 Scroll bars
4015 ***********************************************************************/
4017 /* Scroll bar support. */
4019 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4020 manages it.
4021 This can be called in GC, so we have to make sure to strip off mark
4022 bits. */
4024 static struct scroll_bar *
4025 x_window_to_scroll_bar (display, window_id)
4026 Display *display;
4027 Window window_id;
4029 Lisp_Object tail;
4031 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4032 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4033 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4035 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4037 Lisp_Object frame, bar, condemned;
4039 frame = XCAR (tail);
4040 /* All elements of Vframe_list should be frames. */
4041 if (! FRAMEP (frame))
4042 abort ();
4044 if (! FRAME_X_P (XFRAME (frame)))
4045 continue;
4047 /* Scan this frame's scroll bar list for a scroll bar with the
4048 right window ID. */
4049 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4050 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4051 /* This trick allows us to search both the ordinary and
4052 condemned scroll bar lists with one loop. */
4053 ! NILP (bar) || (bar = condemned,
4054 condemned = Qnil,
4055 ! NILP (bar));
4056 bar = XSCROLL_BAR (bar)->next)
4057 if (XSCROLL_BAR (bar)->x_window == window_id &&
4058 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4059 return XSCROLL_BAR (bar);
4062 return 0;
4066 #if defined USE_LUCID
4068 /* Return the Lucid menu bar WINDOW is part of. Return null
4069 if WINDOW is not part of a menu bar. */
4071 static Widget
4072 x_window_to_menu_bar (window)
4073 Window window;
4075 Lisp_Object tail;
4077 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4079 if (FRAME_X_P (XFRAME (XCAR (tail))))
4081 Lisp_Object frame = XCAR (tail);
4082 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4084 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4085 return menu_bar;
4089 return NULL;
4092 #endif /* USE_LUCID */
4095 /************************************************************************
4096 Toolkit scroll bars
4097 ************************************************************************/
4099 #ifdef USE_TOOLKIT_SCROLL_BARS
4101 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4102 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4103 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4104 struct scroll_bar *));
4105 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4106 int, int, int));
4109 /* Lisp window being scrolled. Set when starting to interact with
4110 a toolkit scroll bar, reset to nil when ending the interaction. */
4112 static Lisp_Object window_being_scrolled;
4114 /* Last scroll bar part sent in xm_scroll_callback. */
4116 static int last_scroll_bar_part;
4118 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4119 that movements of 1/20 of the screen size are mapped to up/down. */
4121 #ifndef USE_GTK
4122 /* Id of action hook installed for scroll bars. */
4124 static XtActionHookId action_hook_id;
4126 static Boolean xaw3d_arrow_scroll;
4128 /* Whether the drag scrolling maintains the mouse at the top of the
4129 thumb. If not, resizing the thumb needs to be done more carefully
4130 to avoid jerkyness. */
4132 static Boolean xaw3d_pick_top;
4134 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4135 bars are used.. The hook is responsible for detecting when
4136 the user ends an interaction with the scroll bar, and generates
4137 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4139 static void
4140 xt_action_hook (widget, client_data, action_name, event, params,
4141 num_params)
4142 Widget widget;
4143 XtPointer client_data;
4144 String action_name;
4145 XEvent *event;
4146 String *params;
4147 Cardinal *num_params;
4149 int scroll_bar_p;
4150 char *end_action;
4152 #ifdef USE_MOTIF
4153 scroll_bar_p = XmIsScrollBar (widget);
4154 end_action = "Release";
4155 #else /* !USE_MOTIF i.e. use Xaw */
4156 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4157 end_action = "EndScroll";
4158 #endif /* USE_MOTIF */
4160 if (scroll_bar_p
4161 && strcmp (action_name, end_action) == 0
4162 && WINDOWP (window_being_scrolled))
4164 struct window *w;
4166 x_send_scroll_bar_event (window_being_scrolled,
4167 scroll_bar_end_scroll, 0, 0);
4168 w = XWINDOW (window_being_scrolled);
4170 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4172 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4173 /* The thumb size is incorrect while dragging: fix it. */
4174 set_vertical_scroll_bar (w);
4176 window_being_scrolled = Qnil;
4177 last_scroll_bar_part = -1;
4179 /* Xt timeouts no longer needed. */
4180 toolkit_scroll_bar_interaction = 0;
4183 #endif /* not USE_GTK */
4185 /* A vector of windows used for communication between
4186 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4188 static struct window **scroll_bar_windows;
4189 static int scroll_bar_windows_size;
4192 /* Send a client message with message type Xatom_Scrollbar for a
4193 scroll action to the frame of WINDOW. PART is a value identifying
4194 the part of the scroll bar that was clicked on. PORTION is the
4195 amount to scroll of a whole of WHOLE. */
4197 static void
4198 x_send_scroll_bar_event (window, part, portion, whole)
4199 Lisp_Object window;
4200 int part, portion, whole;
4202 XEvent event;
4203 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4204 struct window *w = XWINDOW (window);
4205 struct frame *f = XFRAME (w->frame);
4206 int i;
4208 BLOCK_INPUT;
4210 /* Construct a ClientMessage event to send to the frame. */
4211 ev->type = ClientMessage;
4212 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4213 ev->display = FRAME_X_DISPLAY (f);
4214 ev->window = FRAME_X_WINDOW (f);
4215 ev->format = 32;
4217 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4218 not enough to store a pointer or Lisp_Object on a 64 bit system.
4219 So, store the window in scroll_bar_windows and pass the index
4220 into that array in the event. */
4221 for (i = 0; i < scroll_bar_windows_size; ++i)
4222 if (scroll_bar_windows[i] == NULL)
4223 break;
4225 if (i == scroll_bar_windows_size)
4227 int new_size = max (10, 2 * scroll_bar_windows_size);
4228 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4229 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4231 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4232 nbytes);
4233 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4234 scroll_bar_windows_size = new_size;
4237 scroll_bar_windows[i] = w;
4238 ev->data.l[0] = (long) i;
4239 ev->data.l[1] = (long) part;
4240 ev->data.l[2] = (long) 0;
4241 ev->data.l[3] = (long) portion;
4242 ev->data.l[4] = (long) whole;
4244 /* Make Xt timeouts work while the scroll bar is active. */
4245 toolkit_scroll_bar_interaction = 1;
4246 #ifdef USE_X_TOOLKIT
4247 x_activate_timeout_atimer ();
4248 #endif
4250 /* Setting the event mask to zero means that the message will
4251 be sent to the client that created the window, and if that
4252 window no longer exists, no event will be sent. */
4253 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4254 UNBLOCK_INPUT;
4258 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4259 in *IEVENT. */
4261 static void
4262 x_scroll_bar_to_input_event (event, ievent)
4263 XEvent *event;
4264 struct input_event *ievent;
4266 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4267 Lisp_Object window;
4268 struct frame *f;
4269 struct window *w;
4271 w = scroll_bar_windows[ev->data.l[0]];
4272 scroll_bar_windows[ev->data.l[0]] = NULL;
4274 XSETWINDOW (window, w);
4275 f = XFRAME (w->frame);
4277 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4278 ievent->frame_or_window = window;
4279 ievent->arg = Qnil;
4280 #ifdef USE_GTK
4281 ievent->timestamp = CurrentTime;
4282 #else
4283 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4284 #endif
4285 ievent->part = ev->data.l[1];
4286 ievent->code = ev->data.l[2];
4287 ievent->x = make_number ((int) ev->data.l[3]);
4288 ievent->y = make_number ((int) ev->data.l[4]);
4289 ievent->modifiers = 0;
4293 #ifdef USE_MOTIF
4295 /* Minimum and maximum values used for Motif scroll bars. */
4297 #define XM_SB_MAX 10000000
4300 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4301 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4302 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4304 static void
4305 xm_scroll_callback (widget, client_data, call_data)
4306 Widget widget;
4307 XtPointer client_data, call_data;
4309 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4310 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4311 int part = -1, whole = 0, portion = 0;
4313 switch (cs->reason)
4315 case XmCR_DECREMENT:
4316 bar->dragging = Qnil;
4317 part = scroll_bar_up_arrow;
4318 break;
4320 case XmCR_INCREMENT:
4321 bar->dragging = Qnil;
4322 part = scroll_bar_down_arrow;
4323 break;
4325 case XmCR_PAGE_DECREMENT:
4326 bar->dragging = Qnil;
4327 part = scroll_bar_above_handle;
4328 break;
4330 case XmCR_PAGE_INCREMENT:
4331 bar->dragging = Qnil;
4332 part = scroll_bar_below_handle;
4333 break;
4335 case XmCR_TO_TOP:
4336 bar->dragging = Qnil;
4337 part = scroll_bar_to_top;
4338 break;
4340 case XmCR_TO_BOTTOM:
4341 bar->dragging = Qnil;
4342 part = scroll_bar_to_bottom;
4343 break;
4345 case XmCR_DRAG:
4347 int slider_size;
4349 /* Get the slider size. */
4350 BLOCK_INPUT;
4351 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4352 UNBLOCK_INPUT;
4354 whole = XM_SB_MAX - slider_size;
4355 portion = min (cs->value, whole);
4356 part = scroll_bar_handle;
4357 bar->dragging = make_number (cs->value);
4359 break;
4361 case XmCR_VALUE_CHANGED:
4362 break;
4365 if (part >= 0)
4367 window_being_scrolled = bar->window;
4368 last_scroll_bar_part = part;
4369 x_send_scroll_bar_event (bar->window, part, portion, whole);
4373 #elif defined USE_GTK
4375 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4376 bar widget. DATA is a pointer to the scroll_bar structure. */
4378 static gboolean
4379 xg_scroll_callback (GtkRange *range,
4380 GtkScrollType scroll,
4381 gdouble value,
4382 gpointer user_data)
4384 struct scroll_bar *bar = (struct scroll_bar *) user_data;
4385 gdouble position;
4386 int part = -1, whole = 0, portion = 0;
4387 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4388 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4390 if (xg_ignore_gtk_scrollbar) return FALSE;
4391 position = gtk_adjustment_get_value (adj);
4394 switch (scroll)
4396 case GTK_SCROLL_JUMP:
4397 /* Buttons 1 2 or 3 must be grabbed. */
4398 if (FRAME_X_DISPLAY_INFO (f)->grabbed != 0
4399 && FRAME_X_DISPLAY_INFO (f)->grabbed < (1 << 4))
4401 part = scroll_bar_handle;
4402 whole = adj->upper - adj->page_size;
4403 portion = min ((int)position, whole);
4404 bar->dragging = make_number ((int)portion);
4406 break;
4407 case GTK_SCROLL_STEP_BACKWARD:
4408 part = scroll_bar_up_arrow;
4409 bar->dragging = Qnil;
4410 break;
4411 case GTK_SCROLL_STEP_FORWARD:
4412 part = scroll_bar_down_arrow;
4413 bar->dragging = Qnil;
4414 break;
4415 case GTK_SCROLL_PAGE_BACKWARD:
4416 part = scroll_bar_above_handle;
4417 bar->dragging = Qnil;
4418 break;
4419 case GTK_SCROLL_PAGE_FORWARD:
4420 part = scroll_bar_below_handle;
4421 bar->dragging = Qnil;
4422 break;
4425 if (part >= 0)
4427 window_being_scrolled = bar->window;
4428 last_scroll_bar_part = part;
4429 x_send_scroll_bar_event (bar->window, part, portion, whole);
4432 return FALSE;
4435 /* Callback for button release. Sets dragging to Qnil when dragging is done. */
4437 static gboolean
4438 xg_end_scroll_callback (GtkWidget *widget,
4439 GdkEventButton *event,
4440 gpointer user_data)
4442 struct scroll_bar *bar = (struct scroll_bar *) user_data;
4443 bar->dragging = Qnil;
4444 if (WINDOWP (window_being_scrolled))
4446 x_send_scroll_bar_event (window_being_scrolled,
4447 scroll_bar_end_scroll, 0, 0);
4448 window_being_scrolled = Qnil;
4451 return FALSE;
4455 #else /* not USE_GTK and not USE_MOTIF */
4457 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4458 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4459 scroll bar struct. CALL_DATA is a pointer to a float saying where
4460 the thumb is. */
4462 static void
4463 xaw_jump_callback (widget, client_data, call_data)
4464 Widget widget;
4465 XtPointer client_data, call_data;
4467 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4468 float top = *(float *) call_data;
4469 float shown;
4470 int whole, portion, height;
4471 int part;
4473 /* Get the size of the thumb, a value between 0 and 1. */
4474 BLOCK_INPUT;
4475 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4476 UNBLOCK_INPUT;
4478 whole = 10000000;
4479 portion = shown < 1 ? top * whole : 0;
4481 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4482 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4483 the bottom, so we force the scrolling whenever we see that we're
4484 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4485 we try to ensure that we always stay two pixels away from the
4486 bottom). */
4487 part = scroll_bar_down_arrow;
4488 else
4489 part = scroll_bar_handle;
4491 window_being_scrolled = bar->window;
4492 bar->dragging = make_number (portion);
4493 last_scroll_bar_part = part;
4494 x_send_scroll_bar_event (bar->window, part, portion, whole);
4498 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4499 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4500 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4501 the scroll bar. CALL_DATA is an integer specifying the action that
4502 has taken place. Its magnitude is in the range 0..height of the
4503 scroll bar. Negative values mean scroll towards buffer start.
4504 Values < height of scroll bar mean line-wise movement. */
4506 static void
4507 xaw_scroll_callback (widget, client_data, call_data)
4508 Widget widget;
4509 XtPointer client_data, call_data;
4511 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4512 /* The position really is stored cast to a pointer. */
4513 int position = (long) call_data;
4514 Dimension height;
4515 int part;
4517 /* Get the height of the scroll bar. */
4518 BLOCK_INPUT;
4519 XtVaGetValues (widget, XtNheight, &height, NULL);
4520 UNBLOCK_INPUT;
4522 if (eabs (position) >= height)
4523 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4525 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4526 it maps line-movement to call_data = max(5, height/20). */
4527 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4528 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4529 else
4530 part = scroll_bar_move_ratio;
4532 window_being_scrolled = bar->window;
4533 bar->dragging = Qnil;
4534 last_scroll_bar_part = part;
4535 x_send_scroll_bar_event (bar->window, part, position, height);
4538 #endif /* not USE_GTK and not USE_MOTIF */
4540 #define SCROLL_BAR_NAME "verticalScrollBar"
4542 /* Create the widget for scroll bar BAR on frame F. Record the widget
4543 and X window of the scroll bar in BAR. */
4545 #ifdef USE_GTK
4546 static void
4547 x_create_toolkit_scroll_bar (f, bar)
4548 struct frame *f;
4549 struct scroll_bar *bar;
4551 char *scroll_bar_name = SCROLL_BAR_NAME;
4553 BLOCK_INPUT;
4554 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4555 G_CALLBACK (xg_end_scroll_callback),
4556 scroll_bar_name);
4557 UNBLOCK_INPUT;
4560 #else /* not USE_GTK */
4562 static void
4563 x_create_toolkit_scroll_bar (f, bar)
4564 struct frame *f;
4565 struct scroll_bar *bar;
4567 Window xwindow;
4568 Widget widget;
4569 Arg av[20];
4570 int ac = 0;
4571 char *scroll_bar_name = SCROLL_BAR_NAME;
4572 unsigned long pixel;
4574 BLOCK_INPUT;
4576 #ifdef USE_MOTIF
4577 /* Set resources. Create the widget. */
4578 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4579 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4580 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4581 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4582 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4583 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4584 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4586 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4587 if (pixel != -1)
4589 XtSetArg (av[ac], XmNforeground, pixel);
4590 ++ac;
4593 pixel = f->output_data.x->scroll_bar_background_pixel;
4594 if (pixel != -1)
4596 XtSetArg (av[ac], XmNbackground, pixel);
4597 ++ac;
4600 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4601 scroll_bar_name, av, ac);
4603 /* Add one callback for everything that can happen. */
4604 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4605 (XtPointer) bar);
4606 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4607 (XtPointer) bar);
4608 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4609 (XtPointer) bar);
4610 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4611 (XtPointer) bar);
4612 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4613 (XtPointer) bar);
4614 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4615 (XtPointer) bar);
4616 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4617 (XtPointer) bar);
4619 /* Realize the widget. Only after that is the X window created. */
4620 XtRealizeWidget (widget);
4622 /* Set the cursor to an arrow. I didn't find a resource to do that.
4623 And I'm wondering why it hasn't an arrow cursor by default. */
4624 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4625 f->output_data.x->nontext_cursor);
4627 #else /* !USE_MOTIF i.e. use Xaw */
4629 /* Set resources. Create the widget. The background of the
4630 Xaw3d scroll bar widget is a little bit light for my taste.
4631 We don't alter it here to let users change it according
4632 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4633 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4634 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4635 /* For smoother scrolling with Xaw3d -sm */
4636 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4638 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4639 if (pixel != -1)
4641 XtSetArg (av[ac], XtNforeground, pixel);
4642 ++ac;
4645 pixel = f->output_data.x->scroll_bar_background_pixel;
4646 if (pixel != -1)
4648 XtSetArg (av[ac], XtNbackground, pixel);
4649 ++ac;
4652 /* Top/bottom shadow colors. */
4654 /* Allocate them, if necessary. */
4655 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4657 pixel = f->output_data.x->scroll_bar_background_pixel;
4658 if (pixel != -1)
4660 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4661 FRAME_X_COLORMAP (f),
4662 &pixel, 1.2, 0x8000))
4663 pixel = -1;
4664 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4667 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4669 pixel = f->output_data.x->scroll_bar_background_pixel;
4670 if (pixel != -1)
4672 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4673 FRAME_X_COLORMAP (f),
4674 &pixel, 0.6, 0x4000))
4675 pixel = -1;
4676 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4680 #ifdef XtNbeNiceToColormap
4681 /* Tell the toolkit about them. */
4682 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4683 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4684 /* We tried to allocate a color for the top/bottom shadow, and
4685 failed, so tell Xaw3d to use dithering instead. */
4687 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4688 ++ac;
4690 else
4691 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4692 be more consistent with other emacs 3d colors, and since Xaw3d is
4693 not good at dealing with allocation failure. */
4695 /* This tells Xaw3d to use real colors instead of dithering for
4696 the shadows. */
4697 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4698 ++ac;
4700 /* Specify the colors. */
4701 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4702 if (pixel != -1)
4704 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4705 ++ac;
4707 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4708 if (pixel != -1)
4710 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4711 ++ac;
4714 #endif
4716 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4717 f->output_data.x->edit_widget, av, ac);
4720 char *initial = "";
4721 char *val = initial;
4722 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4723 #ifdef XtNarrowScrollbars
4724 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4725 #endif
4726 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4727 if (xaw3d_arrow_scroll || val == initial)
4728 { /* ARROW_SCROLL */
4729 xaw3d_arrow_scroll = True;
4730 /* Isn't that just a personal preference ? --Stef */
4731 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4735 /* Define callbacks. */
4736 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4737 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4738 (XtPointer) bar);
4740 /* Realize the widget. Only after that is the X window created. */
4741 XtRealizeWidget (widget);
4743 #endif /* !USE_MOTIF */
4745 /* Install an action hook that lets us detect when the user
4746 finishes interacting with a scroll bar. */
4747 if (action_hook_id == 0)
4748 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4750 /* Remember X window and widget in the scroll bar vector. */
4751 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4752 xwindow = XtWindow (widget);
4753 bar->x_window = xwindow;
4755 UNBLOCK_INPUT;
4757 #endif /* not USE_GTK */
4760 /* Set the thumb size and position of scroll bar BAR. We are currently
4761 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4763 #ifdef USE_GTK
4764 static void
4765 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4766 struct scroll_bar *bar;
4767 int portion, position, whole;
4769 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4772 #else /* not USE_GTK */
4773 static void
4774 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4775 struct scroll_bar *bar;
4776 int portion, position, whole;
4778 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4779 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4780 float top, shown;
4782 BLOCK_INPUT;
4784 #ifdef USE_MOTIF
4786 /* We use an estimate of 30 chars per line rather than the real
4787 `portion' value. This has the disadvantage that the thumb size
4788 is not very representative, but it makes our life a lot easier.
4789 Otherwise, we have to constantly adjust the thumb size, which
4790 we can't always do quickly enough: while dragging, the size of
4791 the thumb might prevent the user from dragging the thumb all the
4792 way to the end. but Motif and some versions of Xaw3d don't allow
4793 updating the thumb size while dragging. Also, even if we can update
4794 its size, the update will often happen too late.
4795 If you don't believe it, check out revision 1.650 of xterm.c to see
4796 what hoops we were going through and the still poor behavior we got. */
4797 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4798 /* When the thumb is at the bottom, position == whole.
4799 So we need to increase `whole' to make space for the thumb. */
4800 whole += portion;
4802 if (whole <= 0)
4803 top = 0, shown = 1;
4804 else
4806 top = (float) position / whole;
4807 shown = (float) portion / whole;
4810 if (NILP (bar->dragging))
4812 int size, value;
4814 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4815 is the scroll bar's maximum and MIN is the scroll bar's minimum
4816 value. */
4817 size = shown * XM_SB_MAX;
4818 size = min (size, XM_SB_MAX);
4819 size = max (size, 1);
4821 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4822 value = top * XM_SB_MAX;
4823 value = min (value, XM_SB_MAX - size);
4825 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4827 #else /* !USE_MOTIF i.e. use Xaw */
4829 if (whole == 0)
4830 top = 0, shown = 1;
4831 else
4833 top = (float) position / whole;
4834 shown = (float) portion / whole;
4838 float old_top, old_shown;
4839 Dimension height;
4840 XtVaGetValues (widget,
4841 XtNtopOfThumb, &old_top,
4842 XtNshown, &old_shown,
4843 XtNheight, &height,
4844 NULL);
4846 /* Massage the top+shown values. */
4847 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4848 top = max (0, min (1, top));
4849 else
4850 top = old_top;
4851 /* Keep two pixels available for moving the thumb down. */
4852 shown = max (0, min (1 - top - (2.0 / height), shown));
4854 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4855 check that your system's configuration file contains a define
4856 for `NARROWPROTO'. See s/freebsd.h for an example. */
4857 if (top != old_top || shown != old_shown)
4859 if (NILP (bar->dragging))
4860 XawScrollbarSetThumb (widget, top, shown);
4861 else
4863 /* Try to make the scrolling a tad smoother. */
4864 if (!xaw3d_pick_top)
4865 shown = min (shown, old_shown);
4867 XawScrollbarSetThumb (widget, top, shown);
4871 #endif /* !USE_MOTIF */
4873 UNBLOCK_INPUT;
4875 #endif /* not USE_GTK */
4877 #endif /* USE_TOOLKIT_SCROLL_BARS */
4881 /************************************************************************
4882 Scroll bars, general
4883 ************************************************************************/
4885 /* Create a scroll bar and return the scroll bar vector for it. W is
4886 the Emacs window on which to create the scroll bar. TOP, LEFT,
4887 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4888 scroll bar. */
4890 static struct scroll_bar *
4891 x_scroll_bar_create (w, top, left, width, height)
4892 struct window *w;
4893 int top, left, width, height;
4895 struct frame *f = XFRAME (w->frame);
4896 struct scroll_bar *bar
4897 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4899 BLOCK_INPUT;
4901 #ifdef USE_TOOLKIT_SCROLL_BARS
4902 x_create_toolkit_scroll_bar (f, bar);
4903 #else /* not USE_TOOLKIT_SCROLL_BARS */
4905 XSetWindowAttributes a;
4906 unsigned long mask;
4907 Window window;
4909 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4910 if (a.background_pixel == -1)
4911 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4913 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4914 | ButtonMotionMask | PointerMotionHintMask
4915 | ExposureMask);
4916 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4918 mask = (CWBackPixel | CWEventMask | CWCursor);
4920 /* Clear the area of W that will serve as a scroll bar. This is
4921 for the case that a window has been split horizontally. In
4922 this case, no clear_frame is generated to reduce flickering. */
4923 if (width > 0 && height > 0)
4924 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4925 left, top, width,
4926 window_box_height (w), False);
4928 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4929 /* Position and size of scroll bar. */
4930 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4931 top,
4932 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4933 height,
4934 /* Border width, depth, class, and visual. */
4936 CopyFromParent,
4937 CopyFromParent,
4938 CopyFromParent,
4939 /* Attributes. */
4940 mask, &a);
4941 bar->x_window = window;
4943 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4945 XSETWINDOW (bar->window, w);
4946 bar->top = top;
4947 bar->left = left;
4948 bar->width = width;
4949 bar->height = height;
4950 bar->start = 0;
4951 bar->end = 0;
4952 bar->dragging = Qnil;
4953 bar->fringe_extended_p = 0;
4955 /* Add bar to its frame's list of scroll bars. */
4956 bar->next = FRAME_SCROLL_BARS (f);
4957 bar->prev = Qnil;
4958 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4959 if (!NILP (bar->next))
4960 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4962 /* Map the window/widget. */
4963 #ifdef USE_TOOLKIT_SCROLL_BARS
4965 #ifdef USE_GTK
4966 xg_update_scrollbar_pos (f,
4967 bar->x_window,
4968 top,
4969 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4970 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4971 max (height, 1));
4972 xg_show_scroll_bar (bar->x_window);
4973 #else /* not USE_GTK */
4974 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4975 XtConfigureWidget (scroll_bar,
4976 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4977 top,
4978 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4979 max (height, 1), 0);
4980 XtMapWidget (scroll_bar);
4981 #endif /* not USE_GTK */
4983 #else /* not USE_TOOLKIT_SCROLL_BARS */
4984 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
4985 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4987 UNBLOCK_INPUT;
4988 return bar;
4992 #ifndef USE_TOOLKIT_SCROLL_BARS
4994 /* Draw BAR's handle in the proper position.
4996 If the handle is already drawn from START to END, don't bother
4997 redrawing it, unless REBUILD is non-zero; in that case, always
4998 redraw it. (REBUILD is handy for drawing the handle after expose
4999 events.)
5001 Normally, we want to constrain the start and end of the handle to
5002 fit inside its rectangle, but if the user is dragging the scroll
5003 bar handle, we want to let them drag it down all the way, so that
5004 the bar's top is as far down as it goes; otherwise, there's no way
5005 to move to the very end of the buffer. */
5007 static void
5008 x_scroll_bar_set_handle (bar, start, end, rebuild)
5009 struct scroll_bar *bar;
5010 int start, end;
5011 int rebuild;
5013 int dragging = ! NILP (bar->dragging);
5014 Window w = bar->x_window;
5015 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5016 GC gc = f->output_data.x->normal_gc;
5018 /* If the display is already accurate, do nothing. */
5019 if (! rebuild
5020 && start == bar->start
5021 && end == bar->end)
5022 return;
5024 BLOCK_INPUT;
5027 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5028 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5029 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5031 /* Make sure the values are reasonable, and try to preserve
5032 the distance between start and end. */
5034 int length = end - start;
5036 if (start < 0)
5037 start = 0;
5038 else if (start > top_range)
5039 start = top_range;
5040 end = start + length;
5042 if (end < start)
5043 end = start;
5044 else if (end > top_range && ! dragging)
5045 end = top_range;
5048 /* Store the adjusted setting in the scroll bar. */
5049 bar->start = start;
5050 bar->end = end;
5052 /* Clip the end position, just for display. */
5053 if (end > top_range)
5054 end = top_range;
5056 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5057 below top positions, to make sure the handle is always at least
5058 that many pixels tall. */
5059 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5061 /* Draw the empty space above the handle. Note that we can't clear
5062 zero-height areas; that means "clear to end of window." */
5063 if (0 < start)
5064 x_clear_area (FRAME_X_DISPLAY (f), w,
5065 /* x, y, width, height, and exposures. */
5066 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5067 VERTICAL_SCROLL_BAR_TOP_BORDER,
5068 inside_width, start,
5069 False);
5071 /* Change to proper foreground color if one is specified. */
5072 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5073 XSetForeground (FRAME_X_DISPLAY (f), gc,
5074 f->output_data.x->scroll_bar_foreground_pixel);
5076 /* Draw the handle itself. */
5077 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5078 /* x, y, width, height */
5079 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5080 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5081 inside_width, end - start);
5083 /* Restore the foreground color of the GC if we changed it above. */
5084 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5085 XSetForeground (FRAME_X_DISPLAY (f), gc,
5086 FRAME_FOREGROUND_PIXEL (f));
5088 /* Draw the empty space below the handle. Note that we can't
5089 clear zero-height areas; that means "clear to end of window." */
5090 if (end < inside_height)
5091 x_clear_area (FRAME_X_DISPLAY (f), w,
5092 /* x, y, width, height, and exposures. */
5093 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5094 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5095 inside_width, inside_height - end,
5096 False);
5100 UNBLOCK_INPUT;
5103 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5105 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5106 nil. */
5108 static void
5109 x_scroll_bar_remove (bar)
5110 struct scroll_bar *bar;
5112 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5113 BLOCK_INPUT;
5115 #ifdef USE_TOOLKIT_SCROLL_BARS
5116 #ifdef USE_GTK
5117 xg_remove_scroll_bar (f, bar->x_window);
5118 #else /* not USE_GTK */
5119 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5120 #endif /* not USE_GTK */
5121 #else
5122 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5123 #endif
5125 /* Disassociate this scroll bar from its window. */
5126 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5128 UNBLOCK_INPUT;
5132 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5133 that we are displaying PORTION characters out of a total of WHOLE
5134 characters, starting at POSITION. If WINDOW has no scroll bar,
5135 create one. */
5137 static void
5138 XTset_vertical_scroll_bar (w, portion, whole, position)
5139 struct window *w;
5140 int portion, whole, position;
5142 struct frame *f = XFRAME (w->frame);
5143 struct scroll_bar *bar;
5144 int top, height, left, sb_left, width, sb_width;
5145 int window_y, window_height;
5146 #ifdef USE_TOOLKIT_SCROLL_BARS
5147 int fringe_extended_p;
5148 #endif
5150 /* Get window dimensions. */
5151 window_box (w, -1, 0, &window_y, 0, &window_height);
5152 top = window_y;
5153 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5154 height = window_height;
5156 /* Compute the left edge of the scroll bar area. */
5157 left = WINDOW_SCROLL_BAR_AREA_X (w);
5159 /* Compute the width of the scroll bar which might be less than
5160 the width of the area reserved for the scroll bar. */
5161 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5162 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5163 else
5164 sb_width = width;
5166 /* Compute the left edge of the scroll bar. */
5167 #ifdef USE_TOOLKIT_SCROLL_BARS
5168 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5169 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5170 else
5171 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5172 #else
5173 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5174 sb_left = left + width - sb_width;
5175 else
5176 sb_left = left;
5177 #endif
5179 #ifdef USE_TOOLKIT_SCROLL_BARS
5180 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5181 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5182 && WINDOW_LEFT_FRINGE_WIDTH (w)
5183 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5184 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5185 else
5186 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5187 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5188 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5189 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5190 #endif
5192 /* Does the scroll bar exist yet? */
5193 if (NILP (w->vertical_scroll_bar))
5195 if (width > 0 && height > 0)
5197 BLOCK_INPUT;
5198 #ifdef USE_TOOLKIT_SCROLL_BARS
5199 if (fringe_extended_p)
5200 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5201 sb_left, top, sb_width, height, False);
5202 else
5203 #endif
5204 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5205 left, top, width, height, False);
5206 UNBLOCK_INPUT;
5209 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5211 else
5213 /* It may just need to be moved and resized. */
5214 unsigned int mask = 0;
5216 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5218 BLOCK_INPUT;
5220 if (sb_left != bar->left)
5221 mask |= CWX;
5222 if (top != bar->top)
5223 mask |= CWY;
5224 if (sb_width != bar->width)
5225 mask |= CWWidth;
5226 if (height != bar->height)
5227 mask |= CWHeight;
5229 #ifdef USE_TOOLKIT_SCROLL_BARS
5231 /* Move/size the scroll bar widget. */
5232 if (mask || bar->fringe_extended_p != fringe_extended_p)
5234 /* Since toolkit scroll bars are smaller than the space reserved
5235 for them on the frame, we have to clear "under" them. */
5236 if (width > 0 && height > 0)
5238 if (fringe_extended_p)
5239 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5240 sb_left, top, sb_width, height, False);
5241 else
5242 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5243 left, top, width, height, False);
5245 #ifdef USE_GTK
5246 xg_update_scrollbar_pos (f,
5247 bar->x_window,
5248 top,
5249 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5250 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5251 max (height, 1));
5252 #else /* not USE_GTK */
5253 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5254 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5255 top,
5256 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5257 max (height, 1), 0);
5258 #endif /* not USE_GTK */
5260 #else /* not USE_TOOLKIT_SCROLL_BARS */
5262 /* Clear areas not covered by the scroll bar because of
5263 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5264 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5266 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5267 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5268 height, False);
5269 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5270 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5271 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5272 height, False);
5275 /* Clear areas not covered by the scroll bar because it's not as
5276 wide as the area reserved for it. This makes sure a
5277 previous mode line display is cleared after C-x 2 C-x 1, for
5278 example. */
5280 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5281 int rest = area_width - sb_width;
5282 if (rest > 0 && height > 0)
5284 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5285 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5286 left + area_width - rest, top,
5287 rest, height, False);
5288 else
5289 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5290 left, top, rest, height, False);
5294 /* Move/size the scroll bar window. */
5295 if (mask)
5297 XWindowChanges wc;
5299 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5300 wc.y = top;
5301 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5302 wc.height = height;
5303 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5304 mask, &wc);
5307 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5309 /* Remember new settings. */
5310 bar->left = sb_left;
5311 bar->top = top;
5312 bar->width = sb_width;
5313 bar->height = height;
5315 UNBLOCK_INPUT;
5318 #ifdef USE_TOOLKIT_SCROLL_BARS
5319 bar->fringe_extended_p = fringe_extended_p;
5321 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5322 #else /* not USE_TOOLKIT_SCROLL_BARS */
5323 /* Set the scroll bar's current state, unless we're currently being
5324 dragged. */
5325 if (NILP (bar->dragging))
5327 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5329 if (whole == 0)
5330 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5331 else
5333 int start = ((double) position * top_range) / whole;
5334 int end = ((double) (position + portion) * top_range) / whole;
5335 x_scroll_bar_set_handle (bar, start, end, 0);
5338 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5340 XSETVECTOR (w->vertical_scroll_bar, bar);
5344 /* The following three hooks are used when we're doing a thorough
5345 redisplay of the frame. We don't explicitly know which scroll bars
5346 are going to be deleted, because keeping track of when windows go
5347 away is a real pain - "Can you say set-window-configuration, boys
5348 and girls?" Instead, we just assert at the beginning of redisplay
5349 that *all* scroll bars are to be removed, and then save a scroll bar
5350 from the fiery pit when we actually redisplay its window. */
5352 /* Arrange for all scroll bars on FRAME to be removed at the next call
5353 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5354 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5356 static void
5357 XTcondemn_scroll_bars (frame)
5358 FRAME_PTR frame;
5360 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5361 while (! NILP (FRAME_SCROLL_BARS (frame)))
5363 Lisp_Object bar;
5364 bar = FRAME_SCROLL_BARS (frame);
5365 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5366 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5367 XSCROLL_BAR (bar)->prev = Qnil;
5368 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5369 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5370 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5375 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5376 Note that WINDOW isn't necessarily condemned at all. */
5378 static void
5379 XTredeem_scroll_bar (window)
5380 struct window *window;
5382 struct scroll_bar *bar;
5383 struct frame *f;
5385 /* We can't redeem this window's scroll bar if it doesn't have one. */
5386 if (NILP (window->vertical_scroll_bar))
5387 abort ();
5389 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5391 /* Unlink it from the condemned list. */
5392 f = XFRAME (WINDOW_FRAME (window));
5393 if (NILP (bar->prev))
5395 /* If the prev pointer is nil, it must be the first in one of
5396 the lists. */
5397 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5398 /* It's not condemned. Everything's fine. */
5399 return;
5400 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5401 window->vertical_scroll_bar))
5402 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5403 else
5404 /* If its prev pointer is nil, it must be at the front of
5405 one or the other! */
5406 abort ();
5408 else
5409 XSCROLL_BAR (bar->prev)->next = bar->next;
5411 if (! NILP (bar->next))
5412 XSCROLL_BAR (bar->next)->prev = bar->prev;
5414 bar->next = FRAME_SCROLL_BARS (f);
5415 bar->prev = Qnil;
5416 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5417 if (! NILP (bar->next))
5418 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5421 /* Remove all scroll bars on FRAME that haven't been saved since the
5422 last call to `*condemn_scroll_bars_hook'. */
5424 static void
5425 XTjudge_scroll_bars (f)
5426 FRAME_PTR f;
5428 Lisp_Object bar, next;
5430 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5432 /* Clear out the condemned list now so we won't try to process any
5433 more events on the hapless scroll bars. */
5434 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5436 for (; ! NILP (bar); bar = next)
5438 struct scroll_bar *b = XSCROLL_BAR (bar);
5440 x_scroll_bar_remove (b);
5442 next = b->next;
5443 b->next = b->prev = Qnil;
5446 /* Now there should be no references to the condemned scroll bars,
5447 and they should get garbage-collected. */
5451 #ifndef USE_TOOLKIT_SCROLL_BARS
5452 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5453 is a no-op when using toolkit scroll bars.
5455 This may be called from a signal handler, so we have to ignore GC
5456 mark bits. */
5458 static void
5459 x_scroll_bar_expose (bar, event)
5460 struct scroll_bar *bar;
5461 XEvent *event;
5463 Window w = bar->x_window;
5464 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5465 GC gc = f->output_data.x->normal_gc;
5466 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5468 BLOCK_INPUT;
5470 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5472 /* Switch to scroll bar foreground color. */
5473 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5474 XSetForeground (FRAME_X_DISPLAY (f), gc,
5475 f->output_data.x->scroll_bar_foreground_pixel);
5477 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5478 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5480 /* x, y, width, height */
5481 0, 0,
5482 bar->width - 1 - width_trim - width_trim,
5483 bar->height - 1);
5485 /* Restore the foreground color of the GC if we changed it above. */
5486 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5487 XSetForeground (FRAME_X_DISPLAY (f), gc,
5488 FRAME_FOREGROUND_PIXEL (f));
5490 UNBLOCK_INPUT;
5493 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5495 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5496 is set to something other than NO_EVENT, it is enqueued.
5498 This may be called from a signal handler, so we have to ignore GC
5499 mark bits. */
5502 static void
5503 x_scroll_bar_handle_click (bar, event, emacs_event)
5504 struct scroll_bar *bar;
5505 XEvent *event;
5506 struct input_event *emacs_event;
5508 if (! WINDOWP (bar->window))
5509 abort ();
5511 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5512 emacs_event->code = event->xbutton.button - Button1;
5513 emacs_event->modifiers
5514 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5515 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5516 event->xbutton.state)
5517 | (event->type == ButtonRelease
5518 ? up_modifier
5519 : down_modifier));
5520 emacs_event->frame_or_window = bar->window;
5521 emacs_event->arg = Qnil;
5522 emacs_event->timestamp = event->xbutton.time;
5524 int top_range
5525 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5526 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5528 if (y < 0) y = 0;
5529 if (y > top_range) y = top_range;
5531 if (y < bar->start)
5532 emacs_event->part = scroll_bar_above_handle;
5533 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5534 emacs_event->part = scroll_bar_handle;
5535 else
5536 emacs_event->part = scroll_bar_below_handle;
5538 #ifndef USE_TOOLKIT_SCROLL_BARS
5539 /* If the user has released the handle, set it to its final position. */
5540 if (event->type == ButtonRelease
5541 && ! NILP (bar->dragging))
5543 int new_start = y - XINT (bar->dragging);
5544 int new_end = new_start + bar->end - bar->start;
5546 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5547 bar->dragging = Qnil;
5549 #endif
5551 XSETINT (emacs_event->x, y);
5552 XSETINT (emacs_event->y, top_range);
5556 #ifndef USE_TOOLKIT_SCROLL_BARS
5558 /* Handle some mouse motion while someone is dragging the scroll bar.
5560 This may be called from a signal handler, so we have to ignore GC
5561 mark bits. */
5563 static void
5564 x_scroll_bar_note_movement (bar, event)
5565 struct scroll_bar *bar;
5566 XEvent *event;
5568 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5570 last_mouse_movement_time = event->xmotion.time;
5572 f->mouse_moved = 1;
5573 XSETVECTOR (last_mouse_scroll_bar, bar);
5575 /* If we're dragging the bar, display it. */
5576 if (! NILP (bar->dragging))
5578 /* Where should the handle be now? */
5579 int new_start = event->xmotion.y - XINT (bar->dragging);
5581 if (new_start != bar->start)
5583 int new_end = new_start + bar->end - bar->start;
5585 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5590 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5592 /* Return information to the user about the current position of the mouse
5593 on the scroll bar. */
5595 static void
5596 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5597 FRAME_PTR *fp;
5598 Lisp_Object *bar_window;
5599 enum scroll_bar_part *part;
5600 Lisp_Object *x, *y;
5601 unsigned long *time;
5603 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5604 Window w = bar->x_window;
5605 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5606 int win_x, win_y;
5607 Window dummy_window;
5608 int dummy_coord;
5609 unsigned int dummy_mask;
5611 BLOCK_INPUT;
5613 /* Get the mouse's position relative to the scroll bar window, and
5614 report that. */
5615 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5617 /* Root, child, root x and root y. */
5618 &dummy_window, &dummy_window,
5619 &dummy_coord, &dummy_coord,
5621 /* Position relative to scroll bar. */
5622 &win_x, &win_y,
5624 /* Mouse buttons and modifier keys. */
5625 &dummy_mask))
5627 else
5629 int top_range
5630 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5632 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5634 if (! NILP (bar->dragging))
5635 win_y -= XINT (bar->dragging);
5637 if (win_y < 0)
5638 win_y = 0;
5639 if (win_y > top_range)
5640 win_y = top_range;
5642 *fp = f;
5643 *bar_window = bar->window;
5645 if (! NILP (bar->dragging))
5646 *part = scroll_bar_handle;
5647 else if (win_y < bar->start)
5648 *part = scroll_bar_above_handle;
5649 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5650 *part = scroll_bar_handle;
5651 else
5652 *part = scroll_bar_below_handle;
5654 XSETINT (*x, win_y);
5655 XSETINT (*y, top_range);
5657 f->mouse_moved = 0;
5658 last_mouse_scroll_bar = Qnil;
5661 *time = last_mouse_movement_time;
5663 UNBLOCK_INPUT;
5667 /* The screen has been cleared so we may have changed foreground or
5668 background colors, and the scroll bars may need to be redrawn.
5669 Clear out the scroll bars, and ask for expose events, so we can
5670 redraw them. */
5672 void
5673 x_scroll_bar_clear (f)
5674 FRAME_PTR f;
5676 #ifndef USE_TOOLKIT_SCROLL_BARS
5677 Lisp_Object bar;
5679 /* We can have scroll bars even if this is 0,
5680 if we just turned off scroll bar mode.
5681 But in that case we should not clear them. */
5682 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5683 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5684 bar = XSCROLL_BAR (bar)->next)
5685 XClearArea (FRAME_X_DISPLAY (f),
5686 XSCROLL_BAR (bar)->x_window,
5687 0, 0, 0, 0, True);
5688 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5692 /* The main X event-reading loop - XTread_socket. */
5694 /* This holds the state XLookupString needs to implement dead keys
5695 and other tricks known as "compose processing". _X Window System_
5696 says that a portable program can't use this, but Stephen Gildea assures
5697 me that letting the compiler initialize it to zeros will work okay.
5699 This must be defined outside of XTread_socket, for the same reasons
5700 given for enter_timestamp, above. */
5702 static XComposeStatus compose_status;
5704 /* Record the last 100 characters stored
5705 to help debug the loss-of-chars-during-GC problem. */
5707 static int temp_index;
5708 static short temp_buffer[100];
5710 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5711 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5712 temp_index = 0; \
5713 temp_buffer[temp_index++] = (keysym)
5715 /* Set this to nonzero to fake an "X I/O error"
5716 on a particular display. */
5718 struct x_display_info *XTread_socket_fake_io_error;
5720 /* When we find no input here, we occasionally do a no-op command
5721 to verify that the X server is still running and we can still talk with it.
5722 We try all the open displays, one by one.
5723 This variable is used for cycling thru the displays. */
5725 static struct x_display_info *next_noop_dpyinfo;
5727 #define SET_SAVED_MENU_EVENT(size) \
5728 do \
5730 if (f->output_data.x->saved_menu_event == 0) \
5731 f->output_data.x->saved_menu_event \
5732 = (XEvent *) xmalloc (sizeof (XEvent)); \
5733 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5734 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5735 XSETFRAME (inev.ie.frame_or_window, f); \
5737 while (0)
5739 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5740 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5743 enum
5745 X_EVENT_NORMAL,
5746 X_EVENT_GOTO_OUT,
5747 X_EVENT_DROP
5750 /* Filter events for the current X input method.
5751 DPYINFO is the display this event is for.
5752 EVENT is the X event to filter.
5754 Returns non-zero if the event was filtered, caller shall not process
5755 this event further.
5756 Returns zero if event is wasn't filtered. */
5758 #ifdef HAVE_X_I18N
5759 static int
5760 x_filter_event (dpyinfo, event)
5761 struct x_display_info *dpyinfo;
5762 XEvent *event;
5764 /* XFilterEvent returns non-zero if the input method has
5765 consumed the event. We pass the frame's X window to
5766 XFilterEvent because that's the one for which the IC
5767 was created. */
5769 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5770 event->xclient.window);
5772 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5774 #endif
5776 #ifdef USE_GTK
5777 static int current_count;
5778 static int current_finish;
5779 static struct input_event *current_hold_quit;
5781 /* This is the filter function invoked by the GTK event loop.
5782 It is invoked before the XEvent is translated to a GdkEvent,
5783 so we have a chance to act on the event before GTK. */
5784 static GdkFilterReturn
5785 event_handler_gdk (gxev, ev, data)
5786 GdkXEvent *gxev;
5787 GdkEvent *ev;
5788 gpointer data;
5790 XEvent *xev = (XEvent *) gxev;
5792 if (current_count >= 0)
5794 struct x_display_info *dpyinfo;
5796 dpyinfo = x_display_info_for_display (xev->xany.display);
5798 #ifdef HAVE_X_I18N
5799 /* Filter events for the current X input method.
5800 GTK calls XFilterEvent but not for key press and release,
5801 so we do it here. */
5802 if (xev->type == KeyPress || xev->type == KeyRelease)
5803 if (dpyinfo && x_filter_event (dpyinfo, xev))
5804 return GDK_FILTER_REMOVE;
5805 #endif
5807 if (! dpyinfo)
5808 current_finish = X_EVENT_NORMAL;
5809 else
5811 current_count +=
5812 handle_one_xevent (dpyinfo, xev, &current_finish,
5813 current_hold_quit);
5816 else
5817 current_finish = x_dispatch_event (xev, xev->xany.display);
5819 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5820 return GDK_FILTER_REMOVE;
5822 return GDK_FILTER_CONTINUE;
5824 #endif /* USE_GTK */
5827 /* Handles the XEvent EVENT on display DPYINFO.
5829 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5830 *FINISH is zero if caller should continue reading events.
5831 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5833 We return the number of characters stored into the buffer. */
5835 static int
5836 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5837 struct x_display_info *dpyinfo;
5838 XEvent *eventp;
5839 int *finish;
5840 struct input_event *hold_quit;
5842 union {
5843 struct input_event ie;
5844 struct selection_input_event sie;
5845 } inev;
5846 int count = 0;
5847 int do_help = 0;
5848 int nbytes = 0;
5849 struct frame *f = NULL;
5850 struct coding_system coding;
5851 XEvent event = *eventp;
5853 *finish = X_EVENT_NORMAL;
5855 EVENT_INIT (inev.ie);
5856 inev.ie.kind = NO_EVENT;
5857 inev.ie.arg = Qnil;
5859 if (pending_event_wait.eventtype == event.type)
5860 pending_event_wait.eventtype = 0; /* Indicates we got it. */
5862 switch (event.type)
5864 case ClientMessage:
5866 if (event.xclient.message_type
5867 == dpyinfo->Xatom_wm_protocols
5868 && event.xclient.format == 32)
5870 if (event.xclient.data.l[0]
5871 == dpyinfo->Xatom_wm_take_focus)
5873 /* Use x_any_window_to_frame because this
5874 could be the shell widget window
5875 if the frame has no title bar. */
5876 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5877 #ifdef HAVE_X_I18N
5878 /* Not quite sure this is needed -pd */
5879 if (f && FRAME_XIC (f))
5880 XSetICFocus (FRAME_XIC (f));
5881 #endif
5882 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5883 instructs the WM to set the input focus automatically for
5884 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5885 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5886 it has set the focus. So, XSetInputFocus below is not
5887 needed.
5889 The call to XSetInputFocus below has also caused trouble. In
5890 cases where the XSetInputFocus done by the WM and the one
5891 below are temporally close (on a fast machine), the call
5892 below can generate additional FocusIn events which confuse
5893 Emacs. */
5895 /* Since we set WM_TAKE_FOCUS, we must call
5896 XSetInputFocus explicitly. But not if f is null,
5897 since that might be an event for a deleted frame. */
5898 if (f)
5900 Display *d = event.xclient.display;
5901 /* Catch and ignore errors, in case window has been
5902 iconified by a window manager such as GWM. */
5903 x_catch_errors (d);
5904 XSetInputFocus (d, event.xclient.window,
5905 /* The ICCCM says this is
5906 the only valid choice. */
5907 RevertToParent,
5908 event.xclient.data.l[1]);
5909 /* This is needed to detect the error
5910 if there is an error. */
5911 XSync (d, False);
5912 x_uncatch_errors ();
5914 /* Not certain about handling scroll bars here */
5915 #endif /* 0 */
5916 goto done;
5919 if (event.xclient.data.l[0]
5920 == dpyinfo->Xatom_wm_save_yourself)
5922 /* Save state modify the WM_COMMAND property to
5923 something which can reinstate us. This notifies
5924 the session manager, who's looking for such a
5925 PropertyNotify. Can restart processing when
5926 a keyboard or mouse event arrives. */
5927 /* If we have a session manager, don't set this.
5928 KDE will then start two Emacsen, one for the
5929 session manager and one for this. */
5930 #ifdef HAVE_X_SM
5931 if (! x_session_have_connection ())
5932 #endif
5934 f = x_top_window_to_frame (dpyinfo,
5935 event.xclient.window);
5936 /* This is just so we only give real data once
5937 for a single Emacs process. */
5938 if (f == SELECTED_FRAME ())
5939 XSetCommand (FRAME_X_DISPLAY (f),
5940 event.xclient.window,
5941 initial_argv, initial_argc);
5942 else if (f)
5943 XSetCommand (FRAME_X_DISPLAY (f),
5944 event.xclient.window,
5945 0, 0);
5947 goto done;
5950 if (event.xclient.data.l[0]
5951 == dpyinfo->Xatom_wm_delete_window)
5953 f = x_any_window_to_frame (dpyinfo,
5954 event.xclient.window);
5955 if (!f)
5956 goto OTHER; /* May be a dialog that is to be removed */
5958 inev.ie.kind = DELETE_WINDOW_EVENT;
5959 XSETFRAME (inev.ie.frame_or_window, f);
5960 goto done;
5963 goto done;
5966 if (event.xclient.message_type
5967 == dpyinfo->Xatom_wm_configure_denied)
5969 goto done;
5972 if (event.xclient.message_type
5973 == dpyinfo->Xatom_wm_window_moved)
5975 int new_x, new_y;
5976 f = x_window_to_frame (dpyinfo, event.xclient.window);
5978 new_x = event.xclient.data.s[0];
5979 new_y = event.xclient.data.s[1];
5981 if (f)
5983 f->left_pos = new_x;
5984 f->top_pos = new_y;
5986 goto done;
5989 #ifdef HACK_EDITRES
5990 if (event.xclient.message_type
5991 == dpyinfo->Xatom_editres)
5993 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5994 if (f)
5995 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5996 &event, NULL);
5997 goto done;
5999 #endif /* HACK_EDITRES */
6001 if ((event.xclient.message_type
6002 == dpyinfo->Xatom_DONE)
6003 || (event.xclient.message_type
6004 == dpyinfo->Xatom_PAGE))
6006 /* Ghostview job completed. Kill it. We could
6007 reply with "Next" if we received "Page", but we
6008 currently never do because we are interested in
6009 images, only, which should have 1 page. */
6010 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
6011 f = x_window_to_frame (dpyinfo, event.xclient.window);
6012 if (!f)
6013 goto OTHER;
6014 x_kill_gs_process (pixmap, f);
6015 expose_frame (f, 0, 0, 0, 0);
6016 goto done;
6019 #ifdef USE_TOOLKIT_SCROLL_BARS
6020 /* Scroll bar callbacks send a ClientMessage from which
6021 we construct an input_event. */
6022 if (event.xclient.message_type
6023 == dpyinfo->Xatom_Scrollbar)
6025 x_scroll_bar_to_input_event (&event, &inev.ie);
6026 *finish = X_EVENT_GOTO_OUT;
6027 goto done;
6029 #endif /* USE_TOOLKIT_SCROLL_BARS */
6031 /* XEmbed messages from the embedder (if any). */
6032 if (event.xclient.message_type
6033 == dpyinfo->Xatom_XEMBED)
6035 enum xembed_message msg = event.xclient.data.l[1];
6036 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6037 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6039 *finish = X_EVENT_GOTO_OUT;
6040 goto done;
6043 xft_settings_event (dpyinfo, &event);
6045 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6046 if (!f)
6047 goto OTHER;
6048 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
6049 *finish = X_EVENT_DROP;
6051 break;
6053 case SelectionNotify:
6054 last_user_time = event.xselection.time;
6055 #ifdef USE_X_TOOLKIT
6056 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6057 goto OTHER;
6058 #endif /* not USE_X_TOOLKIT */
6059 x_handle_selection_notify (&event.xselection);
6060 break;
6062 case SelectionClear: /* Someone has grabbed ownership. */
6063 last_user_time = event.xselectionclear.time;
6064 #ifdef USE_X_TOOLKIT
6065 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6066 goto OTHER;
6067 #endif /* USE_X_TOOLKIT */
6069 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
6071 inev.ie.kind = SELECTION_CLEAR_EVENT;
6072 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6073 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6074 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6075 inev.ie.frame_or_window = Qnil;
6077 break;
6079 case SelectionRequest: /* Someone wants our selection. */
6080 last_user_time = event.xselectionrequest.time;
6081 #ifdef USE_X_TOOLKIT
6082 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6083 goto OTHER;
6084 #endif /* USE_X_TOOLKIT */
6086 XSelectionRequestEvent *eventp
6087 = (XSelectionRequestEvent *) &event;
6089 inev.ie.kind = SELECTION_REQUEST_EVENT;
6090 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6091 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6092 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6093 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6094 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6095 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6096 inev.ie.frame_or_window = Qnil;
6098 break;
6100 case PropertyNotify:
6101 last_user_time = event.xproperty.time;
6102 f = x_top_window_to_frame (dpyinfo, event.xproperty.window);
6103 if (f && event.xproperty.atom == dpyinfo->Xatom_net_wm_state)
6104 x_handle_net_wm_state (f, &event.xproperty);
6106 x_handle_property_notify (&event.xproperty);
6107 xft_settings_event (dpyinfo, &event);
6108 goto OTHER;
6110 case ReparentNotify:
6111 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6112 if (f)
6114 int x, y;
6115 f->output_data.x->parent_desc = event.xreparent.parent;
6116 x_real_positions (f, &x, &y);
6117 f->left_pos = x;
6118 f->top_pos = y;
6120 /* Perhaps reparented due to a WM restart. Reset this. */
6121 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6122 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6124 goto OTHER;
6126 case Expose:
6127 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6128 if (f)
6130 #ifdef USE_GTK
6131 /* This seems to be needed for GTK 2.6. */
6132 x_clear_area (event.xexpose.display,
6133 event.xexpose.window,
6134 event.xexpose.x, event.xexpose.y,
6135 event.xexpose.width, event.xexpose.height,
6136 FALSE);
6137 #endif
6138 if (f->async_visible == 0)
6140 f->async_visible = 1;
6141 f->async_iconified = 0;
6142 f->output_data.x->has_been_visible = 1;
6143 SET_FRAME_GARBAGED (f);
6145 else
6146 expose_frame (f,
6147 event.xexpose.x, event.xexpose.y,
6148 event.xexpose.width, event.xexpose.height);
6150 else
6152 #ifndef USE_TOOLKIT_SCROLL_BARS
6153 struct scroll_bar *bar;
6154 #endif
6155 #if defined USE_LUCID
6156 /* Submenus of the Lucid menu bar aren't widgets
6157 themselves, so there's no way to dispatch events
6158 to them. Recognize this case separately. */
6160 Widget widget
6161 = x_window_to_menu_bar (event.xexpose.window);
6162 if (widget)
6163 xlwmenu_redisplay (widget);
6165 #endif /* USE_LUCID */
6167 #ifdef USE_TOOLKIT_SCROLL_BARS
6168 /* Dispatch event to the widget. */
6169 goto OTHER;
6170 #else /* not USE_TOOLKIT_SCROLL_BARS */
6171 bar = x_window_to_scroll_bar (event.xexpose.display,
6172 event.xexpose.window);
6174 if (bar)
6175 x_scroll_bar_expose (bar, &event);
6176 #ifdef USE_X_TOOLKIT
6177 else
6178 goto OTHER;
6179 #endif /* USE_X_TOOLKIT */
6180 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6182 break;
6184 case GraphicsExpose: /* This occurs when an XCopyArea's
6185 source area was obscured or not
6186 available. */
6187 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6188 if (f)
6190 expose_frame (f,
6191 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6192 event.xgraphicsexpose.width,
6193 event.xgraphicsexpose.height);
6195 #ifdef USE_X_TOOLKIT
6196 else
6197 goto OTHER;
6198 #endif /* USE_X_TOOLKIT */
6199 break;
6201 case NoExpose: /* This occurs when an XCopyArea's
6202 source area was completely
6203 available. */
6204 break;
6206 case UnmapNotify:
6207 /* Redo the mouse-highlight after the tooltip has gone. */
6208 if (event.xmap.window == tip_window)
6210 tip_window = 0;
6211 redo_mouse_highlight ();
6214 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6215 if (f) /* F may no longer exist if
6216 the frame was deleted. */
6218 /* While a frame is unmapped, display generation is
6219 disabled; you don't want to spend time updating a
6220 display that won't ever be seen. */
6221 f->async_visible = 0;
6222 /* We can't distinguish, from the event, whether the window
6223 has become iconified or invisible. So assume, if it
6224 was previously visible, than now it is iconified.
6225 But x_make_frame_invisible clears both
6226 the visible flag and the iconified flag;
6227 and that way, we know the window is not iconified now. */
6228 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6230 f->async_iconified = 1;
6232 inev.ie.kind = ICONIFY_EVENT;
6233 XSETFRAME (inev.ie.frame_or_window, f);
6236 goto OTHER;
6238 case MapNotify:
6239 if (event.xmap.window == tip_window)
6240 /* The tooltip has been drawn already. Avoid
6241 the SET_FRAME_GARBAGED below. */
6242 goto OTHER;
6244 /* We use x_top_window_to_frame because map events can
6245 come for sub-windows and they don't mean that the
6246 frame is visible. */
6247 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6248 if (f)
6250 /* wait_reading_process_output will notice this and update
6251 the frame's display structures.
6252 If we where iconified, we should not set garbaged,
6253 because that stops redrawing on Expose events. This looks
6254 bad if we are called from a recursive event loop
6255 (x_dispatch_event), for example when a dialog is up. */
6256 if (! f->async_iconified)
6257 SET_FRAME_GARBAGED (f);
6259 /* Check if fullscreen was specified before we where mapped the
6260 first time, i.e. from the command line. */
6261 if (!f->output_data.x->has_been_visible)
6262 x_check_fullscreen (f);
6264 f->async_visible = 1;
6265 f->async_iconified = 0;
6266 f->output_data.x->has_been_visible = 1;
6268 if (f->iconified)
6270 inev.ie.kind = DEICONIFY_EVENT;
6271 XSETFRAME (inev.ie.frame_or_window, f);
6273 else if (! NILP (Vframe_list)
6274 && ! NILP (XCDR (Vframe_list)))
6275 /* Force a redisplay sooner or later
6276 to update the frame titles
6277 in case this is the second frame. */
6278 record_asynch_buffer_change ();
6280 #ifdef USE_GTK
6281 xg_frame_resized (f, -1, -1);
6282 #endif
6284 goto OTHER;
6286 case KeyPress:
6288 last_user_time = event.xkey.time;
6289 ignore_next_mouse_click_timeout = 0;
6291 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6292 /* Dispatch KeyPress events when in menu. */
6293 if (popup_activated ())
6294 goto OTHER;
6295 #endif
6297 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6299 /* If mouse-highlight is an integer, input clears out
6300 mouse highlighting. */
6301 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6302 && (f == 0
6303 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6305 clear_mouse_face (dpyinfo);
6306 dpyinfo->mouse_face_hidden = 1;
6309 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6310 if (f == 0)
6312 /* Scroll bars consume key events, but we want
6313 the keys to go to the scroll bar's frame. */
6314 Widget widget = XtWindowToWidget (dpyinfo->display,
6315 event.xkey.window);
6316 if (widget && XmIsScrollBar (widget))
6318 widget = XtParent (widget);
6319 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6322 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6324 if (f != 0)
6326 KeySym keysym, orig_keysym;
6327 /* al%imercury@uunet.uu.net says that making this 81
6328 instead of 80 fixed a bug whereby meta chars made
6329 his Emacs hang.
6331 It seems that some version of XmbLookupString has
6332 a bug of not returning XBufferOverflow in
6333 status_return even if the input is too long to
6334 fit in 81 bytes. So, we must prepare sufficient
6335 bytes for copy_buffer. 513 bytes (256 chars for
6336 two-byte character set) seems to be a fairly good
6337 approximation. -- 2000.8.10 handa@etl.go.jp */
6338 unsigned char copy_buffer[513];
6339 unsigned char *copy_bufptr = copy_buffer;
6340 int copy_bufsiz = sizeof (copy_buffer);
6341 int modifiers;
6342 Lisp_Object coding_system = Qlatin_1;
6343 Lisp_Object c;
6345 #ifdef USE_GTK
6346 /* Don't pass keys to GTK. A Tab will shift focus to the
6347 tool bar in GTK 2.4. Keys will still go to menus and
6348 dialogs because in that case popup_activated is TRUE
6349 (see above). */
6350 *finish = X_EVENT_DROP;
6351 #endif
6353 event.xkey.state
6354 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6355 extra_keyboard_modifiers);
6356 modifiers = event.xkey.state;
6358 /* This will have to go some day... */
6360 /* make_lispy_event turns chars into control chars.
6361 Don't do it here because XLookupString is too eager. */
6362 event.xkey.state &= ~ControlMask;
6363 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6364 | dpyinfo->super_mod_mask
6365 | dpyinfo->hyper_mod_mask
6366 | dpyinfo->alt_mod_mask);
6368 /* In case Meta is ComposeCharacter,
6369 clear its status. According to Markus Ehrnsperger
6370 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6371 this enables ComposeCharacter to work whether or
6372 not it is combined with Meta. */
6373 if (modifiers & dpyinfo->meta_mod_mask)
6374 bzero (&compose_status, sizeof (compose_status));
6376 #ifdef HAVE_X_I18N
6377 if (FRAME_XIC (f))
6379 Status status_return;
6381 coding_system = Vlocale_coding_system;
6382 nbytes = XmbLookupString (FRAME_XIC (f),
6383 &event.xkey, copy_bufptr,
6384 copy_bufsiz, &keysym,
6385 &status_return);
6386 if (status_return == XBufferOverflow)
6388 copy_bufsiz = nbytes + 1;
6389 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6390 nbytes = XmbLookupString (FRAME_XIC (f),
6391 &event.xkey, copy_bufptr,
6392 copy_bufsiz, &keysym,
6393 &status_return);
6395 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6396 if (status_return == XLookupNone)
6397 break;
6398 else if (status_return == XLookupChars)
6400 keysym = NoSymbol;
6401 modifiers = 0;
6403 else if (status_return != XLookupKeySym
6404 && status_return != XLookupBoth)
6405 abort ();
6407 else
6408 nbytes = XLookupString (&event.xkey, copy_bufptr,
6409 copy_bufsiz, &keysym,
6410 &compose_status);
6411 #else
6412 nbytes = XLookupString (&event.xkey, copy_bufptr,
6413 copy_bufsiz, &keysym,
6414 &compose_status);
6415 #endif
6417 /* If not using XIM/XIC, and a compose sequence is in progress,
6418 we break here. Otherwise, chars_matched is always 0. */
6419 if (compose_status.chars_matched > 0 && nbytes == 0)
6420 break;
6422 bzero (&compose_status, sizeof (compose_status));
6423 orig_keysym = keysym;
6425 /* Common for all keysym input events. */
6426 XSETFRAME (inev.ie.frame_or_window, f);
6427 inev.ie.modifiers
6428 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6429 inev.ie.timestamp = event.xkey.time;
6431 /* First deal with keysyms which have defined
6432 translations to characters. */
6433 if (keysym >= 32 && keysym < 128)
6434 /* Avoid explicitly decoding each ASCII character. */
6436 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6437 inev.ie.code = keysym;
6438 goto done_keysym;
6441 /* Keysyms directly mapped to Unicode characters. */
6442 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6444 if (keysym < 0x01000080)
6445 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6446 else
6447 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6448 inev.ie.code = keysym & 0xFFFFFF;
6449 goto done_keysym;
6452 /* Now non-ASCII. */
6453 if (HASH_TABLE_P (Vx_keysym_table)
6454 && (NATNUMP (c = Fgethash (make_number (keysym),
6455 Vx_keysym_table,
6456 Qnil))))
6458 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6459 ? ASCII_KEYSTROKE_EVENT
6460 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6461 inev.ie.code = XFASTINT (c);
6462 goto done_keysym;
6465 /* Random non-modifier sorts of keysyms. */
6466 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6467 || keysym == XK_Delete
6468 #ifdef XK_ISO_Left_Tab
6469 || (keysym >= XK_ISO_Left_Tab
6470 && keysym <= XK_ISO_Enter)
6471 #endif
6472 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6473 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6474 #ifdef HPUX
6475 /* This recognizes the "extended function
6476 keys". It seems there's no cleaner way.
6477 Test IsModifierKey to avoid handling
6478 mode_switch incorrectly. */
6479 || ((unsigned) (keysym) >= XK_Select
6480 && (unsigned)(keysym) < XK_KP_Space)
6481 #endif
6482 #ifdef XK_dead_circumflex
6483 || orig_keysym == XK_dead_circumflex
6484 #endif
6485 #ifdef XK_dead_grave
6486 || orig_keysym == XK_dead_grave
6487 #endif
6488 #ifdef XK_dead_tilde
6489 || orig_keysym == XK_dead_tilde
6490 #endif
6491 #ifdef XK_dead_diaeresis
6492 || orig_keysym == XK_dead_diaeresis
6493 #endif
6494 #ifdef XK_dead_macron
6495 || orig_keysym == XK_dead_macron
6496 #endif
6497 #ifdef XK_dead_degree
6498 || orig_keysym == XK_dead_degree
6499 #endif
6500 #ifdef XK_dead_acute
6501 || orig_keysym == XK_dead_acute
6502 #endif
6503 #ifdef XK_dead_cedilla
6504 || orig_keysym == XK_dead_cedilla
6505 #endif
6506 #ifdef XK_dead_breve
6507 || orig_keysym == XK_dead_breve
6508 #endif
6509 #ifdef XK_dead_ogonek
6510 || orig_keysym == XK_dead_ogonek
6511 #endif
6512 #ifdef XK_dead_caron
6513 || orig_keysym == XK_dead_caron
6514 #endif
6515 #ifdef XK_dead_doubleacute
6516 || orig_keysym == XK_dead_doubleacute
6517 #endif
6518 #ifdef XK_dead_abovedot
6519 || orig_keysym == XK_dead_abovedot
6520 #endif
6521 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6522 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6523 /* Any "vendor-specific" key is ok. */
6524 || (orig_keysym & (1 << 28))
6525 || (keysym != NoSymbol && nbytes == 0))
6526 && ! (IsModifierKey (orig_keysym)
6527 /* The symbols from XK_ISO_Lock
6528 to XK_ISO_Last_Group_Lock
6529 don't have real modifiers but
6530 should be treated similarly to
6531 Mode_switch by Emacs. */
6532 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6533 || ((unsigned)(orig_keysym)
6534 >= XK_ISO_Lock
6535 && (unsigned)(orig_keysym)
6536 <= XK_ISO_Last_Group_Lock)
6537 #endif
6540 STORE_KEYSYM_FOR_DEBUG (keysym);
6541 /* make_lispy_event will convert this to a symbolic
6542 key. */
6543 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6544 inev.ie.code = keysym;
6545 goto done_keysym;
6548 { /* Raw bytes, not keysym. */
6549 register int i;
6550 register int c;
6551 int nchars, len;
6553 for (i = 0, nchars = 0; i < nbytes; i++)
6555 if (ASCII_BYTE_P (copy_bufptr[i]))
6556 nchars++;
6557 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6560 if (nchars < nbytes)
6562 /* Decode the input data. */
6563 int require;
6564 unsigned char *p;
6566 /* The input should be decoded with `coding_system'
6567 which depends on which X*LookupString function
6568 we used just above and the locale. */
6569 setup_coding_system (coding_system, &coding);
6570 coding.src_multibyte = 0;
6571 coding.dst_multibyte = 1;
6572 /* The input is converted to events, thus we can't
6573 handle composition. Anyway, there's no XIM that
6574 gives us composition information. */
6575 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6577 require = MAX_MULTIBYTE_LENGTH * nbytes;
6578 coding.destination = alloca (require);
6579 coding.dst_bytes = require;
6580 coding.mode |= CODING_MODE_LAST_BLOCK;
6581 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6582 nbytes = coding.produced;
6583 nchars = coding.produced_char;
6584 copy_bufptr = coding.destination;
6587 /* Convert the input data to a sequence of
6588 character events. */
6589 for (i = 0; i < nbytes; i += len)
6591 if (nchars == nbytes)
6592 c = copy_bufptr[i], len = 1;
6593 else
6594 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
6595 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6596 ? ASCII_KEYSTROKE_EVENT
6597 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6598 inev.ie.code = c;
6599 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6602 count += nchars;
6604 inev.ie.kind = NO_EVENT; /* Already stored above. */
6606 if (keysym == NoSymbol)
6607 break;
6610 done_keysym:
6611 #ifdef HAVE_X_I18N
6612 /* Don't dispatch this event since XtDispatchEvent calls
6613 XFilterEvent, and two calls in a row may freeze the
6614 client. */
6615 break;
6616 #else
6617 goto OTHER;
6618 #endif
6620 case KeyRelease:
6621 last_user_time = event.xkey.time;
6622 #ifdef HAVE_X_I18N
6623 /* Don't dispatch this event since XtDispatchEvent calls
6624 XFilterEvent, and two calls in a row may freeze the
6625 client. */
6626 break;
6627 #else
6628 goto OTHER;
6629 #endif
6631 case EnterNotify:
6632 last_user_time = event.xcrossing.time;
6633 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6635 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6637 if (f && x_mouse_click_focus_ignore_position)
6638 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6640 /* EnterNotify counts as mouse movement,
6641 so update things that depend on mouse position. */
6642 if (f && !f->output_data.x->hourglass_p)
6643 note_mouse_movement (f, &event.xmotion);
6644 #ifdef USE_GTK
6645 /* We may get an EnterNotify on the buttons in the toolbar. In that
6646 case we moved out of any highlighted area and need to note this. */
6647 if (!f && last_mouse_glyph_frame)
6648 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6649 #endif
6650 goto OTHER;
6652 case FocusIn:
6653 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6654 goto OTHER;
6656 case LeaveNotify:
6657 last_user_time = event.xcrossing.time;
6658 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6660 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6661 if (f)
6663 if (f == dpyinfo->mouse_face_mouse_frame)
6665 /* If we move outside the frame, then we're
6666 certainly no longer on any text in the frame. */
6667 clear_mouse_face (dpyinfo);
6668 dpyinfo->mouse_face_mouse_frame = 0;
6671 /* Generate a nil HELP_EVENT to cancel a help-echo.
6672 Do it only if there's something to cancel.
6673 Otherwise, the startup message is cleared when
6674 the mouse leaves the frame. */
6675 if (any_help_event_p)
6676 do_help = -1;
6678 #ifdef USE_GTK
6679 /* See comment in EnterNotify above */
6680 else if (last_mouse_glyph_frame)
6681 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6682 #endif
6683 goto OTHER;
6685 case FocusOut:
6686 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6687 goto OTHER;
6689 case MotionNotify:
6691 last_user_time = event.xmotion.time;
6692 previous_help_echo_string = help_echo_string;
6693 help_echo_string = Qnil;
6695 if (dpyinfo->grabbed && last_mouse_frame
6696 && FRAME_LIVE_P (last_mouse_frame))
6697 f = last_mouse_frame;
6698 else
6699 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6701 if (dpyinfo->mouse_face_hidden)
6703 dpyinfo->mouse_face_hidden = 0;
6704 clear_mouse_face (dpyinfo);
6707 #ifdef USE_GTK
6708 if (f && xg_event_is_for_scrollbar (f, &event))
6709 f = 0;
6710 #endif
6711 if (f)
6714 /* Generate SELECT_WINDOW_EVENTs when needed.
6715 Don't let popup menus influence things (bug#1261). */
6716 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6718 Lisp_Object window;
6720 window = window_from_coordinates (f,
6721 event.xmotion.x, event.xmotion.y,
6722 0, 0, 0, 0);
6724 /* Window will be selected only when it is not selected now and
6725 last mouse movement event was not in it. Minibuffer window
6726 will be selected only when it is active. */
6727 if (WINDOWP (window)
6728 && !EQ (window, last_window)
6729 && !EQ (window, selected_window)
6730 /* For click-to-focus window managers
6731 create event iff we don't leave the
6732 selected frame. */
6733 && (focus_follows_mouse
6734 || (EQ (XWINDOW (window)->frame,
6735 XWINDOW (selected_window)->frame))))
6737 inev.ie.kind = SELECT_WINDOW_EVENT;
6738 inev.ie.frame_or_window = window;
6741 last_window=window;
6743 if (!note_mouse_movement (f, &event.xmotion))
6744 help_echo_string = previous_help_echo_string;
6746 else
6748 #ifndef USE_TOOLKIT_SCROLL_BARS
6749 struct scroll_bar *bar
6750 = x_window_to_scroll_bar (event.xmotion.display,
6751 event.xmotion.window);
6753 if (bar)
6754 x_scroll_bar_note_movement (bar, &event);
6755 #endif /* USE_TOOLKIT_SCROLL_BARS */
6757 /* If we move outside the frame, then we're
6758 certainly no longer on any text in the frame. */
6759 clear_mouse_face (dpyinfo);
6762 /* If the contents of the global variable help_echo_string
6763 has changed, generate a HELP_EVENT. */
6764 if (!NILP (help_echo_string)
6765 || !NILP (previous_help_echo_string))
6766 do_help = 1;
6767 goto OTHER;
6770 case ConfigureNotify:
6771 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6772 #ifdef USE_GTK
6773 if (!f
6774 && (f = x_any_window_to_frame (dpyinfo, event.xconfigure.window))
6775 && event.xconfigure.window == FRAME_X_WINDOW (f))
6777 xg_frame_resized (f, event.xconfigure.width,
6778 event.xconfigure.height);
6779 f = 0;
6781 #endif
6782 if (f)
6784 #ifndef USE_X_TOOLKIT
6785 #ifndef USE_GTK
6786 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6787 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6789 /* In the toolkit version, change_frame_size
6790 is called by the code that handles resizing
6791 of the EmacsFrame widget. */
6793 /* Even if the number of character rows and columns has
6794 not changed, the font size may have changed, so we need
6795 to check the pixel dimensions as well. */
6796 if (columns != FRAME_COLS (f)
6797 || rows != FRAME_LINES (f)
6798 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6799 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6801 change_frame_size (f, rows, columns, 0, 1, 0);
6802 SET_FRAME_GARBAGED (f);
6803 cancel_mouse_face (f);
6806 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6807 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6808 #endif /* not USE_GTK */
6809 #endif
6811 #ifdef USE_GTK
6812 /* GTK creates windows but doesn't map them.
6813 Only get real positions when mapped. */
6814 if (FRAME_GTK_OUTER_WIDGET (f)
6815 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6816 #endif
6818 x_real_positions (f, &f->left_pos, &f->top_pos);
6821 #ifdef HAVE_X_I18N
6822 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6823 xic_set_statusarea (f);
6824 #endif
6827 goto OTHER;
6829 case ButtonRelease:
6830 case ButtonPress:
6832 /* If we decide we want to generate an event to be seen
6833 by the rest of Emacs, we put it here. */
6834 int tool_bar_p = 0;
6836 bzero (&compose_status, sizeof (compose_status));
6837 last_mouse_glyph_frame = 0;
6838 last_user_time = event.xbutton.time;
6840 if (dpyinfo->grabbed
6841 && last_mouse_frame
6842 && FRAME_LIVE_P (last_mouse_frame))
6843 f = last_mouse_frame;
6844 else
6845 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6847 #ifdef USE_GTK
6848 if (f && xg_event_is_for_scrollbar (f, &event))
6849 f = 0;
6850 #endif
6851 if (f)
6853 /* Is this in the tool-bar? */
6854 if (WINDOWP (f->tool_bar_window)
6855 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6857 Lisp_Object window;
6858 int x = event.xbutton.x;
6859 int y = event.xbutton.y;
6861 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6862 tool_bar_p = EQ (window, f->tool_bar_window);
6864 if (tool_bar_p && event.xbutton.button < 4)
6866 handle_tool_bar_click (f, x, y,
6867 event.xbutton.type == ButtonPress,
6868 x_x_to_emacs_modifiers (dpyinfo,
6869 event.xbutton.state));
6873 if (!tool_bar_p)
6874 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6875 if (! popup_activated ())
6876 #endif
6878 if (ignore_next_mouse_click_timeout)
6880 if (event.type == ButtonPress
6881 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6883 ignore_next_mouse_click_timeout = 0;
6884 construct_mouse_click (&inev.ie, &event.xbutton, f);
6886 if (event.type == ButtonRelease)
6887 ignore_next_mouse_click_timeout = 0;
6889 else
6890 construct_mouse_click (&inev.ie, &event.xbutton, f);
6892 if (FRAME_X_EMBEDDED_P (f))
6893 xembed_send_message (f, event.xbutton.time,
6894 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6896 else
6898 struct scroll_bar *bar
6899 = x_window_to_scroll_bar (event.xbutton.display,
6900 event.xbutton.window);
6902 #ifdef USE_TOOLKIT_SCROLL_BARS
6903 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6904 scroll bars. */
6905 if (bar && event.xbutton.state & ControlMask)
6907 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6908 *finish = X_EVENT_DROP;
6910 #else /* not USE_TOOLKIT_SCROLL_BARS */
6911 if (bar)
6912 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6913 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6916 if (event.type == ButtonPress)
6918 dpyinfo->grabbed |= (1 << event.xbutton.button);
6919 last_mouse_frame = f;
6921 if (!tool_bar_p)
6922 last_tool_bar_item = -1;
6924 else
6925 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6927 /* Ignore any mouse motion that happened before this event;
6928 any subsequent mouse-movement Emacs events should reflect
6929 only motion after the ButtonPress/Release. */
6930 if (f != 0)
6931 f->mouse_moved = 0;
6933 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6934 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6935 /* For a down-event in the menu bar,
6936 don't pass it to Xt right now.
6937 Instead, save it away
6938 and we will pass it to Xt from kbd_buffer_get_event.
6939 That way, we can run some Lisp code first. */
6940 if (
6941 #ifdef USE_GTK
6942 ! popup_activated ()
6943 /* Gtk+ menus only react to the first three buttons. */
6944 && event.xbutton.button < 3
6946 #endif
6947 f && event.type == ButtonPress
6948 /* Verify the event is really within the menu bar
6949 and not just sent to it due to grabbing. */
6950 && event.xbutton.x >= 0
6951 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6952 && event.xbutton.y >= 0
6953 && event.xbutton.y < f->output_data.x->menubar_height
6954 && event.xbutton.same_screen)
6956 SET_SAVED_BUTTON_EVENT;
6957 XSETFRAME (last_mouse_press_frame, f);
6958 #ifdef USE_GTK
6959 *finish = X_EVENT_DROP;
6960 #endif
6962 else if (event.type == ButtonPress)
6964 last_mouse_press_frame = Qnil;
6965 goto OTHER;
6968 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6969 but I am trying to be cautious. */
6970 else if (event.type == ButtonRelease)
6972 if (!NILP (last_mouse_press_frame))
6974 f = XFRAME (last_mouse_press_frame);
6975 if (f->output_data.x)
6976 SET_SAVED_BUTTON_EVENT;
6978 else
6979 goto OTHER;
6981 #endif /* USE_MOTIF */
6982 else
6983 goto OTHER;
6984 #endif /* USE_X_TOOLKIT || USE_GTK */
6986 break;
6988 case CirculateNotify:
6989 goto OTHER;
6991 case CirculateRequest:
6992 goto OTHER;
6994 case VisibilityNotify:
6995 goto OTHER;
6997 case MappingNotify:
6998 /* Someone has changed the keyboard mapping - update the
6999 local cache. */
7000 switch (event.xmapping.request)
7002 case MappingModifier:
7003 x_find_modifier_meanings (dpyinfo);
7004 /* This is meant to fall through. */
7005 case MappingKeyboard:
7006 XRefreshKeyboardMapping (&event.xmapping);
7008 goto OTHER;
7010 case DestroyNotify:
7011 xft_settings_event (dpyinfo, &event);
7012 break;
7014 default:
7015 OTHER:
7016 #ifdef USE_X_TOOLKIT
7017 BLOCK_INPUT;
7018 if (*finish != X_EVENT_DROP)
7019 XtDispatchEvent (&event);
7020 UNBLOCK_INPUT;
7021 #endif /* USE_X_TOOLKIT */
7022 break;
7025 done:
7026 if (inev.ie.kind != NO_EVENT)
7028 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7029 count++;
7032 if (do_help
7033 && !(hold_quit && hold_quit->kind != NO_EVENT))
7035 Lisp_Object frame;
7037 if (f)
7038 XSETFRAME (frame, f);
7039 else
7040 frame = Qnil;
7042 if (do_help > 0)
7044 any_help_event_p = 1;
7045 gen_help_event (help_echo_string, frame, help_echo_window,
7046 help_echo_object, help_echo_pos);
7048 else
7050 help_echo_string = Qnil;
7051 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7053 count++;
7056 *eventp = event;
7057 return count;
7061 /* Handles the XEvent EVENT on display DISPLAY.
7062 This is used for event loops outside the normal event handling,
7063 i.e. looping while a popup menu or a dialog is posted.
7065 Returns the value handle_one_xevent sets in the finish argument. */
7067 x_dispatch_event (event, display)
7068 XEvent *event;
7069 Display *display;
7071 struct x_display_info *dpyinfo;
7072 int finish = X_EVENT_NORMAL;
7074 dpyinfo = x_display_info_for_display (display);
7076 if (dpyinfo)
7077 handle_one_xevent (dpyinfo, event, &finish, 0);
7079 return finish;
7083 /* Read events coming from the X server.
7084 This routine is called by the SIGIO handler.
7085 We return as soon as there are no more events to be read.
7087 We return the number of characters stored into the buffer,
7088 thus pretending to be `read' (except the characters we store
7089 in the keyboard buffer can be multibyte, so are not necessarily
7090 C chars).
7092 EXPECTED is nonzero if the caller knows input is available. */
7094 static int
7095 XTread_socket (terminal, expected, hold_quit)
7096 struct terminal *terminal;
7097 int expected;
7098 struct input_event *hold_quit;
7100 int count = 0;
7101 XEvent event;
7102 int event_found = 0;
7104 if (interrupt_input_blocked)
7106 interrupt_input_pending = 1;
7107 #ifdef SYNC_INPUT
7108 pending_signals = 1;
7109 #endif
7110 return -1;
7113 interrupt_input_pending = 0;
7114 #ifdef SYNC_INPUT
7115 pending_signals = pending_atimers;
7116 #endif
7117 BLOCK_INPUT;
7119 /* So people can tell when we have read the available input. */
7120 input_signal_count++;
7122 ++handling_signal;
7124 #ifdef HAVE_X_SM
7125 /* Only check session manager input for the primary display. */
7126 if (terminal->id == 1 && x_session_have_connection ())
7128 struct input_event inev;
7129 BLOCK_INPUT;
7130 /* We don't need to EVENT_INIT (inev) here, as
7131 x_session_check_input copies an entire input_event. */
7132 if (x_session_check_input (&inev))
7134 kbd_buffer_store_event_hold (&inev, hold_quit);
7135 count++;
7137 UNBLOCK_INPUT;
7139 #endif
7141 /* For debugging, this gives a way to fake an I/O error. */
7142 if (terminal->display_info.x == XTread_socket_fake_io_error)
7144 XTread_socket_fake_io_error = 0;
7145 x_io_error_quitter (terminal->display_info.x->display);
7148 #ifndef USE_GTK
7149 while (XPending (terminal->display_info.x->display))
7151 int finish;
7153 XNextEvent (terminal->display_info.x->display, &event);
7155 #ifdef HAVE_X_I18N
7156 /* Filter events for the current X input method. */
7157 if (x_filter_event (terminal->display_info.x, &event))
7158 continue;
7159 #endif
7160 event_found = 1;
7162 count += handle_one_xevent (terminal->display_info.x,
7163 &event, &finish, hold_quit);
7165 if (finish == X_EVENT_GOTO_OUT)
7166 goto out;
7169 #else /* USE_GTK */
7171 /* For GTK we must use the GTK event loop. But XEvents gets passed
7172 to our filter function above, and then to the big event switch.
7173 We use a bunch of globals to communicate with our filter function,
7174 that is kind of ugly, but it works.
7176 There is no way to do one display at the time, GTK just does events
7177 from all displays. */
7179 while (gtk_events_pending ())
7181 current_count = count;
7182 current_hold_quit = hold_quit;
7184 gtk_main_iteration ();
7186 count = current_count;
7187 current_count = -1;
7188 current_hold_quit = 0;
7190 if (current_finish == X_EVENT_GOTO_OUT)
7191 break;
7193 #endif /* USE_GTK */
7195 out:;
7197 /* On some systems, an X bug causes Emacs to get no more events
7198 when the window is destroyed. Detect that. (1994.) */
7199 if (! event_found)
7201 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7202 One XNOOP in 100 loops will make Emacs terminate.
7203 B. Bretthauer, 1994 */
7204 x_noop_count++;
7205 if (x_noop_count >= 100)
7207 x_noop_count=0;
7209 if (next_noop_dpyinfo == 0)
7210 next_noop_dpyinfo = x_display_list;
7212 XNoOp (next_noop_dpyinfo->display);
7214 /* Each time we get here, cycle through the displays now open. */
7215 next_noop_dpyinfo = next_noop_dpyinfo->next;
7219 /* If the focus was just given to an auto-raising frame,
7220 raise it now. */
7221 /* ??? This ought to be able to handle more than one such frame. */
7222 if (pending_autoraise_frame)
7224 x_raise_frame (pending_autoraise_frame);
7225 pending_autoraise_frame = 0;
7228 --handling_signal;
7229 UNBLOCK_INPUT;
7231 return count;
7237 /***********************************************************************
7238 Text Cursor
7239 ***********************************************************************/
7241 /* Set clipping for output in glyph row ROW. W is the window in which
7242 we operate. GC is the graphics context to set clipping in.
7244 ROW may be a text row or, e.g., a mode line. Text rows must be
7245 clipped to the interior of the window dedicated to text display,
7246 mode lines must be clipped to the whole window. */
7248 static void
7249 x_clip_to_row (w, row, area, gc)
7250 struct window *w;
7251 struct glyph_row *row;
7252 int area;
7253 GC gc;
7255 struct frame *f = XFRAME (WINDOW_FRAME (w));
7256 XRectangle clip_rect;
7257 int window_x, window_y, window_width;
7259 window_box (w, area, &window_x, &window_y, &window_width, 0);
7261 clip_rect.x = window_x;
7262 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7263 clip_rect.y = max (clip_rect.y, window_y);
7264 clip_rect.width = window_width;
7265 clip_rect.height = row->visible_height;
7267 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7271 /* Draw a hollow box cursor on window W in glyph row ROW. */
7273 static void
7274 x_draw_hollow_cursor (w, row)
7275 struct window *w;
7276 struct glyph_row *row;
7278 struct frame *f = XFRAME (WINDOW_FRAME (w));
7279 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7280 Display *dpy = FRAME_X_DISPLAY (f);
7281 int x, y, wd, h;
7282 XGCValues xgcv;
7283 struct glyph *cursor_glyph;
7284 GC gc;
7286 /* Get the glyph the cursor is on. If we can't tell because
7287 the current matrix is invalid or such, give up. */
7288 cursor_glyph = get_phys_cursor_glyph (w);
7289 if (cursor_glyph == NULL)
7290 return;
7292 /* Compute frame-relative coordinates for phys cursor. */
7293 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7294 wd = w->phys_cursor_width;
7296 /* The foreground of cursor_gc is typically the same as the normal
7297 background color, which can cause the cursor box to be invisible. */
7298 xgcv.foreground = f->output_data.x->cursor_pixel;
7299 if (dpyinfo->scratch_cursor_gc)
7300 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7301 else
7302 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7303 GCForeground, &xgcv);
7304 gc = dpyinfo->scratch_cursor_gc;
7306 /* Set clipping, draw the rectangle, and reset clipping again. */
7307 x_clip_to_row (w, row, TEXT_AREA, gc);
7308 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7309 XSetClipMask (dpy, gc, None);
7313 /* Draw a bar cursor on window W in glyph row ROW.
7315 Implementation note: One would like to draw a bar cursor with an
7316 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7317 Unfortunately, I didn't find a font yet that has this property set.
7318 --gerd. */
7320 static void
7321 x_draw_bar_cursor (w, row, width, kind)
7322 struct window *w;
7323 struct glyph_row *row;
7324 int width;
7325 enum text_cursor_kinds kind;
7327 struct frame *f = XFRAME (w->frame);
7328 struct glyph *cursor_glyph;
7330 /* If cursor is out of bounds, don't draw garbage. This can happen
7331 in mini-buffer windows when switching between echo area glyphs
7332 and mini-buffer. */
7333 cursor_glyph = get_phys_cursor_glyph (w);
7334 if (cursor_glyph == NULL)
7335 return;
7337 /* If on an image, draw like a normal cursor. That's usually better
7338 visible than drawing a bar, esp. if the image is large so that
7339 the bar might not be in the window. */
7340 if (cursor_glyph->type == IMAGE_GLYPH)
7342 struct glyph_row *row;
7343 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7344 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7346 else
7348 Display *dpy = FRAME_X_DISPLAY (f);
7349 Window window = FRAME_X_WINDOW (f);
7350 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7351 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7352 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7353 XGCValues xgcv;
7355 /* If the glyph's background equals the color we normally draw
7356 the bars cursor in, the bar cursor in its normal color is
7357 invisible. Use the glyph's foreground color instead in this
7358 case, on the assumption that the glyph's colors are chosen so
7359 that the glyph is legible. */
7360 if (face->background == f->output_data.x->cursor_pixel)
7361 xgcv.background = xgcv.foreground = face->foreground;
7362 else
7363 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7364 xgcv.graphics_exposures = 0;
7366 if (gc)
7367 XChangeGC (dpy, gc, mask, &xgcv);
7368 else
7370 gc = XCreateGC (dpy, window, mask, &xgcv);
7371 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7374 x_clip_to_row (w, row, TEXT_AREA, gc);
7376 if (kind == BAR_CURSOR)
7378 if (width < 0)
7379 width = FRAME_CURSOR_WIDTH (f);
7380 width = min (cursor_glyph->pixel_width, width);
7382 w->phys_cursor_width = width;
7384 XFillRectangle (dpy, window, gc,
7385 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7386 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7387 width, row->height);
7389 else
7391 int dummy_x, dummy_y, dummy_h;
7393 if (width < 0)
7394 width = row->height;
7396 width = min (row->height, width);
7398 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
7399 &dummy_y, &dummy_h);
7401 XFillRectangle (dpy, window, gc,
7402 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7403 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7404 row->height - width),
7405 w->phys_cursor_width, width);
7408 XSetClipMask (dpy, gc, None);
7413 /* RIF: Define cursor CURSOR on frame F. */
7415 static void
7416 x_define_frame_cursor (f, cursor)
7417 struct frame *f;
7418 Cursor cursor;
7420 if (!f->pointer_invisible
7421 && f->output_data.x->current_cursor != cursor)
7422 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7423 f->output_data.x->current_cursor = cursor;
7427 /* RIF: Clear area on frame F. */
7429 static void
7430 x_clear_frame_area (f, x, y, width, height)
7431 struct frame *f;
7432 int x, y, width, height;
7434 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7435 x, y, width, height, False);
7439 /* RIF: Draw cursor on window W. */
7441 static void
7442 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7443 struct window *w;
7444 struct glyph_row *glyph_row;
7445 int x, y;
7446 int cursor_type, cursor_width;
7447 int on_p, active_p;
7449 struct frame *f = XFRAME (WINDOW_FRAME (w));
7451 if (on_p)
7453 w->phys_cursor_type = cursor_type;
7454 w->phys_cursor_on_p = 1;
7456 if (glyph_row->exact_window_width_line_p
7457 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7459 glyph_row->cursor_in_fringe_p = 1;
7460 draw_fringe_bitmap (w, glyph_row, 0);
7462 else
7463 switch (cursor_type)
7465 case HOLLOW_BOX_CURSOR:
7466 x_draw_hollow_cursor (w, glyph_row);
7467 break;
7469 case FILLED_BOX_CURSOR:
7470 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7471 break;
7473 case BAR_CURSOR:
7474 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7475 break;
7477 case HBAR_CURSOR:
7478 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7479 break;
7481 case NO_CURSOR:
7482 w->phys_cursor_width = 0;
7483 break;
7485 default:
7486 abort ();
7489 #ifdef HAVE_X_I18N
7490 if (w == XWINDOW (f->selected_window))
7491 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7492 xic_set_preeditarea (w, x, y);
7493 #endif
7496 #ifndef XFlush
7497 XFlush (FRAME_X_DISPLAY (f));
7498 #endif
7502 /* Icons. */
7504 /* Make the x-window of frame F use the gnu icon bitmap. */
7507 x_bitmap_icon (f, file)
7508 struct frame *f;
7509 Lisp_Object file;
7511 int bitmap_id;
7513 if (FRAME_X_WINDOW (f) == 0)
7514 return 1;
7516 /* Free up our existing icon bitmap and mask if any. */
7517 if (f->output_data.x->icon_bitmap > 0)
7518 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7519 f->output_data.x->icon_bitmap = 0;
7521 if (STRINGP (file))
7523 #ifdef USE_GTK
7524 /* Use gtk_window_set_icon_from_file () if available,
7525 It's not restricted to bitmaps */
7526 if (xg_set_icon (f, file))
7527 return 0;
7528 #endif /* USE_GTK */
7529 bitmap_id = x_create_bitmap_from_file (f, file);
7530 x_create_bitmap_mask (f, bitmap_id);
7532 else
7534 /* Create the GNU bitmap and mask if necessary. */
7535 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7537 int rc = -1;
7539 #ifdef USE_GTK
7541 if (xg_set_icon (f, xg_default_icon_file)
7542 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7543 return 0;
7545 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7547 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7548 if (rc != -1)
7549 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7551 #endif
7553 /* If all else fails, use the (black and white) xbm image. */
7554 if (rc == -1)
7556 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7557 gnu_xbm_width, gnu_xbm_height);
7558 if (rc == -1)
7559 return 1;
7561 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7562 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7566 /* The first time we create the GNU bitmap and mask,
7567 this increments the ref-count one extra time.
7568 As a result, the GNU bitmap and mask are never freed.
7569 That way, we don't have to worry about allocating it again. */
7570 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7572 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7575 x_wm_set_icon_pixmap (f, bitmap_id);
7576 f->output_data.x->icon_bitmap = bitmap_id;
7578 return 0;
7582 /* Make the x-window of frame F use a rectangle with text.
7583 Use ICON_NAME as the text. */
7586 x_text_icon (f, icon_name)
7587 struct frame *f;
7588 char *icon_name;
7590 if (FRAME_X_WINDOW (f) == 0)
7591 return 1;
7594 XTextProperty text;
7595 text.value = (unsigned char *) icon_name;
7596 text.encoding = XA_STRING;
7597 text.format = 8;
7598 text.nitems = strlen (icon_name);
7599 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7602 if (f->output_data.x->icon_bitmap > 0)
7603 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7604 f->output_data.x->icon_bitmap = 0;
7605 x_wm_set_icon_pixmap (f, 0);
7607 return 0;
7610 #define X_ERROR_MESSAGE_SIZE 200
7612 /* If non-nil, this should be a string.
7613 It means catch X errors and store the error message in this string.
7615 The reason we use a stack is that x_catch_error/x_uncatch_error can
7616 be called from a signal handler.
7619 struct x_error_message_stack {
7620 char string[X_ERROR_MESSAGE_SIZE];
7621 Display *dpy;
7622 struct x_error_message_stack *prev;
7624 static struct x_error_message_stack *x_error_message;
7626 /* An X error handler which stores the error message in
7627 *x_error_message. This is called from x_error_handler if
7628 x_catch_errors is in effect. */
7630 static void
7631 x_error_catcher (display, error)
7632 Display *display;
7633 XErrorEvent *error;
7635 XGetErrorText (display, error->error_code,
7636 x_error_message->string,
7637 X_ERROR_MESSAGE_SIZE);
7640 /* Begin trapping X errors for display DPY. Actually we trap X errors
7641 for all displays, but DPY should be the display you are actually
7642 operating on.
7644 After calling this function, X protocol errors no longer cause
7645 Emacs to exit; instead, they are recorded in the string
7646 stored in *x_error_message.
7648 Calling x_check_errors signals an Emacs error if an X error has
7649 occurred since the last call to x_catch_errors or x_check_errors.
7651 Calling x_uncatch_errors resumes the normal error handling. */
7653 void x_check_errors ();
7655 void
7656 x_catch_errors (dpy)
7657 Display *dpy;
7659 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7661 /* Make sure any errors from previous requests have been dealt with. */
7662 XSync (dpy, False);
7664 data->dpy = dpy;
7665 data->string[0] = 0;
7666 data->prev = x_error_message;
7667 x_error_message = data;
7670 /* Undo the last x_catch_errors call.
7671 DPY should be the display that was passed to x_catch_errors. */
7673 void
7674 x_uncatch_errors ()
7676 struct x_error_message_stack *tmp;
7678 BLOCK_INPUT;
7680 /* The display may have been closed before this function is called.
7681 Check if it is still open before calling XSync. */
7682 if (x_display_info_for_display (x_error_message->dpy) != 0)
7683 XSync (x_error_message->dpy, False);
7685 tmp = x_error_message;
7686 x_error_message = x_error_message->prev;
7687 xfree (tmp);
7688 UNBLOCK_INPUT;
7691 /* If any X protocol errors have arrived since the last call to
7692 x_catch_errors or x_check_errors, signal an Emacs error using
7693 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7695 void
7696 x_check_errors (dpy, format)
7697 Display *dpy;
7698 char *format;
7700 /* Make sure to catch any errors incurred so far. */
7701 XSync (dpy, False);
7703 if (x_error_message->string[0])
7705 char string[X_ERROR_MESSAGE_SIZE];
7706 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7707 x_uncatch_errors ();
7708 error (format, string);
7712 /* Nonzero if we had any X protocol errors
7713 since we did x_catch_errors on DPY. */
7716 x_had_errors_p (dpy)
7717 Display *dpy;
7719 /* Make sure to catch any errors incurred so far. */
7720 XSync (dpy, False);
7722 return x_error_message->string[0] != 0;
7725 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7727 void
7728 x_clear_errors (dpy)
7729 Display *dpy;
7731 x_error_message->string[0] = 0;
7734 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7735 * idea. --lorentey */
7736 /* Close off all unclosed x_catch_errors calls. */
7738 void
7739 x_fully_uncatch_errors ()
7741 while (x_error_message)
7742 x_uncatch_errors ();
7744 #endif
7746 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7749 x_catching_errors ()
7751 return x_error_message != 0;
7754 #if 0
7755 static unsigned int x_wire_count;
7756 x_trace_wire ()
7758 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7760 #endif /* ! 0 */
7763 /* Handle SIGPIPE, which can happen when the connection to a server
7764 simply goes away. SIGPIPE is handled by x_connection_signal.
7765 Don't need to do anything, because the write which caused the
7766 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7767 which will do the appropriate cleanup for us. */
7769 static SIGTYPE
7770 x_connection_signal (signalnum) /* If we don't have an argument, */
7771 int signalnum; /* some compilers complain in signal calls. */
7773 #ifdef USG
7774 /* USG systems forget handlers when they are used;
7775 must reestablish each time */
7776 signal (signalnum, x_connection_signal);
7777 #endif /* USG */
7781 /************************************************************************
7782 Handling X errors
7783 ************************************************************************/
7785 /* Error message passed to x_connection_closed. */
7787 static char *error_msg;
7789 /* Function installed as fatal_error_signal_hook in
7790 x_connection_closed. Print the X error message, and exit normally,
7791 instead of dumping core when XtCloseDisplay fails. */
7793 static void
7794 x_fatal_error_signal ()
7796 fprintf (stderr, "%s\n", error_msg);
7797 exit (70);
7800 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7801 the text of an error message that lead to the connection loss. */
7803 static SIGTYPE
7804 x_connection_closed (dpy, error_message)
7805 Display *dpy;
7806 char *error_message;
7808 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7809 Lisp_Object frame, tail;
7810 int index = SPECPDL_INDEX ();
7812 error_msg = (char *) alloca (strlen (error_message) + 1);
7813 strcpy (error_msg, error_message);
7814 handling_signal = 0;
7816 /* Prevent being called recursively because of an error condition
7817 below. Otherwise, we might end up with printing ``can't find per
7818 display information'' in the recursive call instead of printing
7819 the original message here. */
7820 x_catch_errors (dpy);
7822 /* Inhibit redisplay while frames are being deleted. */
7823 specbind (Qinhibit_redisplay, Qt);
7825 if (dpyinfo)
7827 /* Protect display from being closed when we delete the last
7828 frame on it. */
7829 dpyinfo->reference_count++;
7830 dpyinfo->terminal->reference_count++;
7833 /* First delete frames whose mini-buffers are on frames
7834 that are on the dead display. */
7835 FOR_EACH_FRAME (tail, frame)
7837 Lisp_Object minibuf_frame;
7838 minibuf_frame
7839 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7840 if (FRAME_X_P (XFRAME (frame))
7841 && FRAME_X_P (XFRAME (minibuf_frame))
7842 && ! EQ (frame, minibuf_frame)
7843 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7844 delete_frame (frame, Qnoelisp);
7847 /* Now delete all remaining frames on the dead display.
7848 We are now sure none of these is used as the mini-buffer
7849 for another frame that we need to delete. */
7850 FOR_EACH_FRAME (tail, frame)
7851 if (FRAME_X_P (XFRAME (frame))
7852 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7854 /* Set this to t so that delete_frame won't get confused
7855 trying to find a replacement. */
7856 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7857 delete_frame (frame, Qnoelisp);
7860 /* We have to close the display to inform Xt that it doesn't
7861 exist anymore. If we don't, Xt will continue to wait for
7862 events from the display. As a consequence, a sequence of
7864 M-x make-frame-on-display RET :1 RET
7865 ...kill the new frame, so that we get an IO error...
7866 M-x make-frame-on-display RET :1 RET
7868 will indefinitely wait in Xt for events for display `:1', opened
7869 in the first call to make-frame-on-display.
7871 Closing the display is reported to lead to a bus error on
7872 OpenWindows in certain situations. I suspect that is a bug
7873 in OpenWindows. I don't know how to circumvent it here. */
7875 if (dpyinfo)
7877 #ifdef USE_X_TOOLKIT
7878 /* If DPYINFO is null, this means we didn't open the display
7879 in the first place, so don't try to close it. */
7881 extern void (*fatal_error_signal_hook) P_ ((void));
7882 fatal_error_signal_hook = x_fatal_error_signal;
7883 XtCloseDisplay (dpy);
7884 fatal_error_signal_hook = NULL;
7886 #endif
7888 #ifdef USE_GTK
7889 /* Due to bugs in some Gtk+ versions, just exit here if this
7890 is the last display/terminal. */
7891 if (terminal_list->next_terminal == NULL)
7893 fprintf (stderr, "%s\n", error_msg);
7894 shut_down_emacs (0, 0, Qnil);
7895 exit (70);
7897 xg_display_close (dpyinfo->display);
7898 #endif
7900 /* Indicate that this display is dead. */
7901 dpyinfo->display = 0;
7903 dpyinfo->reference_count--;
7904 dpyinfo->terminal->reference_count--;
7905 if (dpyinfo->reference_count != 0)
7906 /* We have just closed all frames on this display. */
7907 abort ();
7910 Lisp_Object tmp;
7911 XSETTERMINAL (tmp, dpyinfo->terminal);
7912 Fdelete_terminal (tmp, Qnoelisp);
7916 x_uncatch_errors ();
7918 if (terminal_list == 0)
7920 fprintf (stderr, "%s\n", error_msg);
7921 shut_down_emacs (0, 0, Qnil);
7922 exit (70);
7925 /* Ordinary stack unwind doesn't deal with these. */
7926 #ifdef SIGIO
7927 sigunblock (sigmask (SIGIO));
7928 #endif
7929 sigunblock (sigmask (SIGALRM));
7930 TOTALLY_UNBLOCK_INPUT;
7932 unbind_to (index, Qnil);
7933 clear_waiting_for_input ();
7934 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7935 longjmp), because returning from this function would get us back into
7936 Xlib's code which will directly call `exit'. */
7937 error ("%s", error_msg);
7940 /* We specifically use it before defining it, so that gcc doesn't inline it,
7941 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7942 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7944 /* This is the first-level handler for X protocol errors.
7945 It calls x_error_quitter or x_error_catcher. */
7947 static int
7948 x_error_handler (display, error)
7949 Display *display;
7950 XErrorEvent *error;
7952 if (x_error_message)
7953 x_error_catcher (display, error);
7954 else
7955 x_error_quitter (display, error);
7956 return 0;
7959 /* This is the usual handler for X protocol errors.
7960 It kills all frames on the display that we got the error for.
7961 If that was the only one, it prints an error message and kills Emacs. */
7963 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7965 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
7966 #define NO_INLINE __attribute__((noinline))
7967 #else
7968 #define NO_INLINE
7969 #endif
7971 /* Some versions of GNU/Linux define noinline in their headers. */
7973 #ifdef noinline
7974 #undef noinline
7975 #endif
7977 /* On older GCC versions, just putting x_error_quitter
7978 after x_error_handler prevents inlining into the former. */
7980 static void NO_INLINE
7981 x_error_quitter (display, error)
7982 Display *display;
7983 XErrorEvent *error;
7985 char buf[256], buf1[356];
7987 /* Ignore BadName errors. They can happen because of fonts
7988 or colors that are not defined. */
7990 if (error->error_code == BadName)
7991 return;
7993 /* Note that there is no real way portable across R3/R4 to get the
7994 original error handler. */
7996 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7997 sprintf (buf1, "X protocol error: %s on protocol request %d",
7998 buf, error->request_code);
7999 x_connection_closed (display, buf1);
8003 /* This is the handler for X IO errors, always.
8004 It kills all frames on the display that we lost touch with.
8005 If that was the only one, it prints an error message and kills Emacs. */
8007 static int
8008 x_io_error_quitter (display)
8009 Display *display;
8011 char buf[256];
8013 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
8014 x_connection_closed (display, buf);
8015 return 0;
8018 /* Changing the font of the frame. */
8020 /* Give frame F the font FONT-OBJECT as its default font. The return
8021 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
8022 frame. If it is negative, generate a new fontset from
8023 FONT-OBJECT. */
8025 Lisp_Object
8026 x_new_font (f, font_object, fontset)
8027 struct frame *f;
8028 Lisp_Object font_object;
8029 int fontset;
8031 struct font *font = XFONT_OBJECT (font_object);
8033 if (fontset < 0)
8034 fontset = fontset_from_font (font_object);
8035 FRAME_FONTSET (f) = fontset;
8036 if (FRAME_FONT (f) == font)
8037 /* This font is already set in frame F. There's nothing more to
8038 do. */
8039 return font_object;
8041 FRAME_FONT (f) = font;
8042 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8043 FRAME_COLUMN_WIDTH (f) = font->average_width;
8044 FRAME_SPACE_WIDTH (f) = font->space_width;
8045 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8047 compute_fringe_widths (f, 1);
8049 /* Compute the scroll bar width in character columns. */
8050 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8052 int wid = FRAME_COLUMN_WIDTH (f);
8053 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8054 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8056 else
8058 int wid = FRAME_COLUMN_WIDTH (f);
8059 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8062 if (FRAME_X_WINDOW (f) != 0)
8064 /* Don't change the size of a tip frame; there's no point in
8065 doing it because it's done in Fx_show_tip, and it leads to
8066 problems because the tip frame has no widget. */
8067 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8068 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8071 #ifdef HAVE_X_I18N
8072 if (FRAME_XIC (f)
8073 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8075 BLOCK_INPUT;
8076 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8077 UNBLOCK_INPUT;
8079 #endif
8081 return font_object;
8085 /***********************************************************************
8086 X Input Methods
8087 ***********************************************************************/
8089 #ifdef HAVE_X_I18N
8091 #ifdef HAVE_X11R6
8093 /* XIM destroy callback function, which is called whenever the
8094 connection to input method XIM dies. CLIENT_DATA contains a
8095 pointer to the x_display_info structure corresponding to XIM. */
8097 static void
8098 xim_destroy_callback (xim, client_data, call_data)
8099 XIM xim;
8100 XPointer client_data;
8101 XPointer call_data;
8103 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8104 Lisp_Object frame, tail;
8106 BLOCK_INPUT;
8108 /* No need to call XDestroyIC.. */
8109 FOR_EACH_FRAME (tail, frame)
8111 struct frame *f = XFRAME (frame);
8112 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8114 FRAME_XIC (f) = NULL;
8115 xic_free_xfontset (f);
8119 /* No need to call XCloseIM. */
8120 dpyinfo->xim = NULL;
8121 XFree (dpyinfo->xim_styles);
8122 UNBLOCK_INPUT;
8125 #endif /* HAVE_X11R6 */
8127 #ifdef HAVE_X11R6
8128 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8129 extern char *XSetIMValues P_ ((XIM, ...));
8130 #endif
8132 /* Open the connection to the XIM server on display DPYINFO.
8133 RESOURCE_NAME is the resource name Emacs uses. */
8135 static void
8136 xim_open_dpy (dpyinfo, resource_name)
8137 struct x_display_info *dpyinfo;
8138 char *resource_name;
8140 XIM xim;
8142 #ifdef HAVE_XIM
8143 if (use_xim)
8145 if (dpyinfo->xim)
8146 XCloseIM (dpyinfo->xim);
8147 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8148 EMACS_CLASS);
8149 dpyinfo->xim = xim;
8151 if (xim)
8153 #ifdef HAVE_X11R6
8154 XIMCallback destroy;
8155 #endif
8157 /* Get supported styles and XIM values. */
8158 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8160 #ifdef HAVE_X11R6
8161 destroy.callback = xim_destroy_callback;
8162 destroy.client_data = (XPointer)dpyinfo;
8163 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8164 #endif
8168 else
8169 #endif /* HAVE_XIM */
8170 dpyinfo->xim = NULL;
8174 #ifdef HAVE_X11R6_XIM
8176 /* XIM instantiate callback function, which is called whenever an XIM
8177 server is available. DISPLAY is the display of the XIM.
8178 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8179 when the callback was registered. */
8181 static void
8182 xim_instantiate_callback (display, client_data, call_data)
8183 Display *display;
8184 XPointer client_data;
8185 XPointer call_data;
8187 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8188 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8190 /* We don't support multiple XIM connections. */
8191 if (dpyinfo->xim)
8192 return;
8194 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8196 /* Create XIC for the existing frames on the same display, as long
8197 as they have no XIC. */
8198 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8200 Lisp_Object tail, frame;
8202 BLOCK_INPUT;
8203 FOR_EACH_FRAME (tail, frame)
8205 struct frame *f = XFRAME (frame);
8207 if (FRAME_X_P (f)
8208 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8209 if (FRAME_XIC (f) == NULL)
8211 create_frame_xic (f);
8212 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8213 xic_set_statusarea (f);
8214 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8216 struct window *w = XWINDOW (f->selected_window);
8217 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8222 UNBLOCK_INPUT;
8226 #endif /* HAVE_X11R6_XIM */
8229 /* Open a connection to the XIM server on display DPYINFO.
8230 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8231 connection only at the first time. On X11R6, open the connection
8232 in the XIM instantiate callback function. */
8234 static void
8235 xim_initialize (dpyinfo, resource_name)
8236 struct x_display_info *dpyinfo;
8237 char *resource_name;
8239 dpyinfo->xim = NULL;
8240 #ifdef HAVE_XIM
8241 if (use_xim)
8243 #ifdef HAVE_X11R6_XIM
8244 struct xim_inst_t *xim_inst;
8245 int len;
8247 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8248 dpyinfo->xim_callback_data = xim_inst;
8249 xim_inst->dpyinfo = dpyinfo;
8250 len = strlen (resource_name);
8251 xim_inst->resource_name = (char *) xmalloc (len + 1);
8252 bcopy (resource_name, xim_inst->resource_name, len + 1);
8253 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8254 resource_name, EMACS_CLASS,
8255 xim_instantiate_callback,
8256 /* This is XPointer in XFree86
8257 but (XPointer *) on Tru64, at
8258 least, hence the configure test. */
8259 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8260 #else /* not HAVE_X11R6_XIM */
8261 xim_open_dpy (dpyinfo, resource_name);
8262 #endif /* not HAVE_X11R6_XIM */
8264 #endif /* HAVE_XIM */
8268 /* Close the connection to the XIM server on display DPYINFO. */
8270 static void
8271 xim_close_dpy (dpyinfo)
8272 struct x_display_info *dpyinfo;
8274 #ifdef HAVE_XIM
8275 if (use_xim)
8277 #ifdef HAVE_X11R6_XIM
8278 if (dpyinfo->display)
8279 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8280 NULL, EMACS_CLASS,
8281 xim_instantiate_callback, NULL);
8282 xfree (dpyinfo->xim_callback_data->resource_name);
8283 xfree (dpyinfo->xim_callback_data);
8284 #endif /* HAVE_X11R6_XIM */
8285 if (dpyinfo->display)
8286 XCloseIM (dpyinfo->xim);
8287 dpyinfo->xim = NULL;
8288 XFree (dpyinfo->xim_styles);
8290 #endif /* HAVE_XIM */
8293 #endif /* not HAVE_X11R6_XIM */
8297 /* Calculate the absolute position in frame F
8298 from its current recorded position values and gravity. */
8300 void
8301 x_calc_absolute_position (f)
8302 struct frame *f;
8304 int flags = f->size_hint_flags;
8306 /* We have nothing to do if the current position
8307 is already for the top-left corner. */
8308 if (! ((flags & XNegative) || (flags & YNegative)))
8309 return;
8311 /* Treat negative positions as relative to the leftmost bottommost
8312 position that fits on the screen. */
8313 if (flags & XNegative)
8314 f->left_pos = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
8315 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8318 int height = FRAME_PIXEL_HEIGHT (f);
8320 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8321 /* Something is fishy here. When using Motif, starting Emacs with
8322 `-g -0-0', the frame appears too low by a few pixels.
8324 This seems to be so because initially, while Emacs is starting,
8325 the column widget's height and the frame's pixel height are
8326 different. The column widget's height is the right one. In
8327 later invocations, when Emacs is up, the frame's pixel height
8328 is right, though.
8330 It's not obvious where the initial small difference comes from.
8331 2000-12-01, gerd. */
8333 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8334 #endif
8336 if (flags & YNegative)
8337 f->top_pos = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
8338 - height + f->top_pos;
8341 /* The left_pos and top_pos
8342 are now relative to the top and left screen edges,
8343 so the flags should correspond. */
8344 f->size_hint_flags &= ~ (XNegative | YNegative);
8347 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8348 to really change the position, and 0 when calling from
8349 x_make_frame_visible (in that case, XOFF and YOFF are the current
8350 position values). It is -1 when calling from x_set_frame_parameters,
8351 which means, do adjust for borders but don't change the gravity. */
8353 void
8354 x_set_offset (f, xoff, yoff, change_gravity)
8355 struct frame *f;
8356 register int xoff, yoff;
8357 int change_gravity;
8359 int modified_top, modified_left;
8361 if (change_gravity > 0)
8363 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8364 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8366 f->top_pos = yoff;
8367 f->left_pos = xoff;
8368 f->size_hint_flags &= ~ (XNegative | YNegative);
8369 if (xoff < 0)
8370 f->size_hint_flags |= XNegative;
8371 if (yoff < 0)
8372 f->size_hint_flags |= YNegative;
8373 f->win_gravity = NorthWestGravity;
8375 x_calc_absolute_position (f);
8377 BLOCK_INPUT;
8378 x_wm_set_size_hint (f, (long) 0, 0);
8380 modified_left = f->left_pos;
8381 modified_top = f->top_pos;
8383 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8385 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8386 than the WM decorations. So we use the calculated offset instead
8387 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8388 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8389 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8392 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8393 modified_left, modified_top);
8395 x_sync_with_move (f, f->left_pos, f->top_pos,
8396 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8397 ? 1 : 0);
8399 /* change_gravity is non-zero when this function is called from Lisp to
8400 programmatically move a frame. In that case, we call
8401 x_check_expected_move to discover if we have a "Type A" or "Type B"
8402 window manager, and, for a "Type A" window manager, adjust the position
8403 of the frame.
8405 We call x_check_expected_move if a programmatic move occurred, and
8406 either the window manager type (A/B) is unknown or it is Type A but we
8407 need to compute the top/left offset adjustment for this frame. */
8409 if (change_gravity != 0 &&
8410 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8411 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8412 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8413 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8414 x_check_expected_move (f, modified_left, modified_top);
8416 UNBLOCK_INPUT;
8419 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8420 on the root window for frame F contains ATOMNAME.
8421 This is how a WM check shall be done according to the Window Manager
8422 Specification/Extended Window Manager Hints at
8423 http://freedesktop.org/wiki/Specifications/wm-spec. */
8425 static int
8426 wm_supports (f, atomname)
8427 struct frame *f;
8428 const char *atomname;
8430 Atom actual_type;
8431 unsigned long actual_size, bytes_remaining;
8432 int i, rc, actual_format;
8433 Atom prop_atom;
8434 Window wmcheck_window;
8435 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8436 Window target_window = dpyinfo->root_window;
8437 long max_len = 65536;
8438 Display *dpy = FRAME_X_DISPLAY (f);
8439 unsigned char *tmp_data = NULL;
8440 Atom target_type = XA_WINDOW;
8441 Atom want_atom;
8443 BLOCK_INPUT;
8445 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8447 x_catch_errors (dpy);
8448 rc = XGetWindowProperty (dpy, target_window,
8449 prop_atom, 0, max_len, False, target_type,
8450 &actual_type, &actual_format, &actual_size,
8451 &bytes_remaining, &tmp_data);
8453 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8455 if (tmp_data) XFree (tmp_data);
8456 x_uncatch_errors ();
8457 UNBLOCK_INPUT;
8458 return 0;
8461 wmcheck_window = *(Window *) tmp_data;
8462 XFree (tmp_data);
8464 /* Check if window exists. */
8465 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8466 x_sync (f);
8467 if (x_had_errors_p (dpy))
8469 x_uncatch_errors ();
8470 UNBLOCK_INPUT;
8471 return 0;
8474 if (dpyinfo->net_supported_window != wmcheck_window)
8476 /* Window changed, reload atoms */
8477 if (dpyinfo->net_supported_atoms != NULL)
8478 XFree (dpyinfo->net_supported_atoms);
8479 dpyinfo->net_supported_atoms = NULL;
8480 dpyinfo->nr_net_supported_atoms = 0;
8481 dpyinfo->net_supported_window = 0;
8483 target_type = XA_ATOM;
8484 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8485 tmp_data = NULL;
8486 rc = XGetWindowProperty (dpy, target_window,
8487 prop_atom, 0, max_len, False, target_type,
8488 &actual_type, &actual_format, &actual_size,
8489 &bytes_remaining, &tmp_data);
8491 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8493 if (tmp_data) XFree (tmp_data);
8494 x_uncatch_errors ();
8495 UNBLOCK_INPUT;
8496 return 0;
8499 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8500 dpyinfo->nr_net_supported_atoms = actual_size;
8501 dpyinfo->net_supported_window = wmcheck_window;
8504 rc = 0;
8505 want_atom = XInternAtom (dpy, atomname, False);
8507 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8508 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8510 x_uncatch_errors ();
8511 UNBLOCK_INPUT;
8513 return rc;
8516 static void
8517 set_wm_state (frame, add, what, what2)
8518 Lisp_Object frame;
8519 int add;
8520 const char *what;
8521 const char *what2;
8523 const char *atom = "_NET_WM_STATE";
8524 Fx_send_client_event (frame, make_number (0), frame,
8525 make_unibyte_string (atom, strlen (atom)),
8526 make_number (32),
8527 /* 1 = add, 0 = remove */
8528 Fcons
8529 (make_number (add ? 1 : 0),
8530 Fcons
8531 (make_unibyte_string (what, strlen (what)),
8532 what2 != 0
8533 ? Fcons (make_unibyte_string (what2, strlen (what2)),
8534 Qnil)
8535 : Qnil)));
8538 void
8539 x_set_sticky (f, new_value, old_value)
8540 struct frame *f;
8541 Lisp_Object new_value, old_value;
8543 Lisp_Object frame;
8545 XSETFRAME (frame, f);
8546 set_wm_state (frame, NILP (new_value) ? 0 : 1,
8547 "_NET_WM_STATE_STICKY", NULL);
8550 /* Do fullscreen as specified in extended window manager hints */
8552 static int
8553 do_ewmh_fullscreen (f)
8554 struct frame *f;
8556 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8558 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8559 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8560 if (!have_net_atom)
8561 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8563 if (have_net_atom)
8565 Lisp_Object frame;
8566 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8567 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8568 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8570 XSETFRAME (frame, f);
8572 set_wm_state (frame, 0, fs, NULL);
8573 set_wm_state (frame, 0, fh, NULL);
8574 set_wm_state (frame, 0, fw, NULL);
8576 /* If there are _NET_ atoms we assume we have extended window manager
8577 hints. */
8578 switch (f->want_fullscreen)
8580 case FULLSCREEN_BOTH:
8581 set_wm_state (frame, 1, fs, NULL);
8582 break;
8583 case FULLSCREEN_WIDTH:
8584 set_wm_state (frame, 1, fw, NULL);
8585 break;
8586 case FULLSCREEN_HEIGHT:
8587 set_wm_state (frame, 1, fh, NULL);
8588 break;
8589 case FULLSCREEN_MAXIMIZED:
8590 set_wm_state (frame, 1, fw, fh);
8591 break;
8594 f->want_fullscreen = FULLSCREEN_NONE;
8598 return have_net_atom;
8601 static void
8602 XTfullscreen_hook (f)
8603 FRAME_PTR f;
8605 if (f->async_visible)
8607 BLOCK_INPUT;
8608 x_check_fullscreen (f);
8609 x_sync (f);
8610 UNBLOCK_INPUT;
8615 static void
8616 x_handle_net_wm_state (f, event)
8617 struct frame *f;
8618 XPropertyEvent *event;
8620 Atom actual_type;
8621 unsigned long actual_size, bytes_remaining;
8622 int i, rc, actual_format, value = FULLSCREEN_NONE;
8623 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8624 long max_len = 65536;
8625 Display *dpy = FRAME_X_DISPLAY (f);
8626 unsigned char *tmp_data = NULL;
8627 Atom target_type = XA_ATOM;
8628 Lisp_Object lval;
8629 int sticky = 0;
8631 BLOCK_INPUT;
8632 x_catch_errors (dpy);
8633 rc = XGetWindowProperty (dpy, event->window,
8634 event->atom, 0, max_len, False, target_type,
8635 &actual_type, &actual_format, &actual_size,
8636 &bytes_remaining, &tmp_data);
8638 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
8640 if (tmp_data) XFree (tmp_data);
8641 x_uncatch_errors ();
8642 UNBLOCK_INPUT;
8643 return;
8646 x_uncatch_errors ();
8648 for (i = 0; i < actual_size; ++i)
8650 Atom a = ((Atom*)tmp_data)[i];
8651 if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
8653 if (value == FULLSCREEN_HEIGHT)
8654 value = FULLSCREEN_MAXIMIZED;
8655 else
8656 value = FULLSCREEN_WIDTH;
8658 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
8660 if (value == FULLSCREEN_WIDTH)
8661 value = FULLSCREEN_MAXIMIZED;
8662 else
8663 value = FULLSCREEN_HEIGHT;
8665 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen_atom)
8666 value = FULLSCREEN_BOTH;
8667 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
8668 sticky = 1;
8671 lval = Qnil;
8672 switch (value)
8674 case FULLSCREEN_WIDTH:
8675 lval = Qfullwidth;
8676 break;
8677 case FULLSCREEN_HEIGHT:
8678 lval = Qfullheight;
8679 break;
8680 case FULLSCREEN_BOTH:
8681 lval = Qfullboth;
8682 break;
8683 case FULLSCREEN_MAXIMIZED:
8684 lval = Qmaximized;
8685 break;
8688 store_frame_param (f, Qfullscreen, lval);
8689 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
8691 if (tmp_data) XFree (tmp_data);
8692 UNBLOCK_INPUT;
8695 /* Check if we need to resize the frame due to a fullscreen request.
8696 If so needed, resize the frame. */
8697 static void
8698 x_check_fullscreen (f)
8699 struct frame *f;
8701 if (do_ewmh_fullscreen (f))
8702 return;
8704 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8705 return; /* Only fullscreen without WM or with EWM hints (above). */
8707 if (f->want_fullscreen != FULLSCREEN_NONE)
8709 int width = FRAME_COLS (f), height = FRAME_LINES (f);
8710 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8712 switch (f->want_fullscreen)
8714 /* No difference between these two when there is no WM */
8715 case FULLSCREEN_BOTH:
8716 case FULLSCREEN_MAXIMIZED:
8717 width = x_display_pixel_width (dpyinfo);
8718 height = x_display_pixel_height (dpyinfo);
8719 break;
8720 case FULLSCREEN_WIDTH:
8721 width = x_display_pixel_width (dpyinfo);
8722 break;
8723 case FULLSCREEN_HEIGHT:
8724 height = x_display_pixel_height (dpyinfo);
8727 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8729 change_frame_size (f, height, width, 0, 1, 0);
8730 SET_FRAME_GARBAGED (f);
8731 cancel_mouse_face (f);
8736 /* This function is called by x_set_offset to determine whether the window
8737 manager interfered with the positioning of the frame. Type A window
8738 managers position the surrounding window manager decorations a small
8739 amount above and left of the user-supplied position. Type B window
8740 managers position the surrounding window manager decorations at the
8741 user-specified position. If we detect a Type A window manager, we
8742 compensate by moving the window right and down by the proper amount. */
8744 static void
8745 x_check_expected_move (f, expected_left, expected_top)
8746 struct frame *f;
8747 int expected_left;
8748 int expected_top;
8750 int current_left = 0, current_top = 0;
8752 /* x_real_positions returns the left and top offsets of the outermost
8753 window manager window around the frame. */
8755 x_real_positions (f, &current_left, &current_top);
8757 if (current_left != expected_left || current_top != expected_top)
8759 /* It's a "Type A" window manager. */
8761 int adjusted_left;
8762 int adjusted_top;
8764 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8765 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8766 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8768 /* Now fix the mispositioned frame's location. */
8770 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8771 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8773 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8774 adjusted_left, adjusted_top);
8776 x_sync_with_move (f, expected_left, expected_top, 0);
8778 else
8779 /* It's a "Type B" window manager. We don't have to adjust the
8780 frame's position. */
8782 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8786 /* Wait for XGetGeometry to return up-to-date position information for a
8787 recently-moved frame. Call this immediately after calling XMoveWindow.
8788 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8789 frame has been moved to, so we use a fuzzy position comparison instead
8790 of an exact comparison. */
8792 static void
8793 x_sync_with_move (f, left, top, fuzzy)
8794 struct frame *f;
8795 int left, top, fuzzy;
8797 int count = 0;
8799 while (count++ < 50)
8801 int current_left = 0, current_top = 0;
8803 /* In theory, this call to XSync only needs to happen once, but in
8804 practice, it doesn't seem to work, hence the need for the surrounding
8805 loop. */
8807 XSync (FRAME_X_DISPLAY (f), False);
8808 x_real_positions (f, &current_left, &current_top);
8810 if (fuzzy)
8812 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8813 pixels. */
8815 if (eabs (current_left - left) <= 10
8816 && eabs (current_top - top) <= 40)
8817 return;
8819 else if (current_left == left && current_top == top)
8820 return;
8823 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8824 will then return up-to-date position info. */
8826 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8830 /* Wait for an event on frame F matching EVENTTYPE. */
8831 void
8832 x_wait_for_event (f, eventtype)
8833 struct frame *f;
8834 int eventtype;
8836 int level = interrupt_input_blocked;
8838 SELECT_TYPE fds;
8839 EMACS_TIME tmo, tmo_at, time_now;
8840 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
8842 pending_event_wait.f = f;
8843 pending_event_wait.eventtype = eventtype;
8845 /* Set timeout to 0.1 second. Hopefully not noticable.
8846 Maybe it should be configurable. */
8847 EMACS_SET_SECS_USECS (tmo, 0, 100000);
8848 EMACS_GET_TIME (tmo_at);
8849 EMACS_ADD_TIME (tmo_at, tmo_at, tmo);
8851 while (pending_event_wait.eventtype)
8853 interrupt_input_pending = 1;
8854 TOTALLY_UNBLOCK_INPUT;
8855 /* XTread_socket is called after unblock. */
8856 BLOCK_INPUT;
8857 interrupt_input_blocked = level;
8859 FD_ZERO (&fds);
8860 FD_SET (fd, &fds);
8862 EMACS_GET_TIME (time_now);
8863 EMACS_SUB_TIME (tmo, tmo_at, time_now);
8865 if (EMACS_TIME_NEG_P (tmo) || select (fd+1, &fds, NULL, NULL, &tmo) == 0)
8866 break; /* Timeout */
8868 pending_event_wait.f = 0;
8869 pending_event_wait.eventtype = 0;
8873 /* Change the size of frame F's X window to COLS/ROWS in the case F
8874 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8875 top-left-corner window gravity for this size change and subsequent
8876 size changes. Otherwise we leave the window gravity unchanged. */
8878 static void
8879 x_set_window_size_1 (f, change_gravity, cols, rows)
8880 struct frame *f;
8881 int change_gravity;
8882 int cols, rows;
8884 int pixelwidth, pixelheight;
8886 check_frame_size (f, &rows, &cols);
8887 f->scroll_bar_actual_width
8888 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8890 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8891 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8892 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8894 compute_fringe_widths (f, 0);
8896 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
8897 + 2*f->border_width;
8898 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
8899 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f)
8900 + 2*f->border_width;
8902 if (change_gravity) f->win_gravity = NorthWestGravity;
8903 x_wm_set_size_hint (f, (long) 0, 0);
8904 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8905 pixelwidth, pixelheight);
8908 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8909 receive in the ConfigureNotify event; if we get what we asked
8910 for, then the event won't cause the screen to become garbaged, so
8911 we have to make sure to do it here. */
8912 SET_FRAME_GARBAGED (f);
8914 /* Now, strictly speaking, we can't be sure that this is accurate,
8915 but the window manager will get around to dealing with the size
8916 change request eventually, and we'll hear how it went when the
8917 ConfigureNotify event gets here.
8919 We could just not bother storing any of this information here,
8920 and let the ConfigureNotify event set everything up, but that
8921 might be kind of confusing to the Lisp code, since size changes
8922 wouldn't be reported in the frame parameters until some random
8923 point in the future when the ConfigureNotify event arrives.
8925 We pass 1 for DELAY since we can't run Lisp code inside of
8926 a BLOCK_INPUT. */
8928 /* But the ConfigureNotify may in fact never arrive, and then this is
8929 not right if the frame is visible. Instead wait (with timeout)
8930 for the ConfigureNotify. */
8931 if (f->async_visible)
8932 x_wait_for_event (f, ConfigureNotify);
8933 else
8935 change_frame_size (f, rows, cols, 0, 1, 0);
8936 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8937 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8938 x_sync (f);
8943 /* Call this to change the size of frame F's x-window.
8944 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8945 for this size change and subsequent size changes.
8946 Otherwise we leave the window gravity unchanged. */
8948 void
8949 x_set_window_size (f, change_gravity, cols, rows)
8950 struct frame *f;
8951 int change_gravity;
8952 int cols, rows;
8954 BLOCK_INPUT;
8956 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8958 int r, c;
8960 /* When the frame is maximized/fullscreen or running under for
8961 example Xmonad, x_set_window_size_1 will be a no-op.
8962 In that case, the right thing to do is extend rows/cols to
8963 the current frame size. We do that first if x_set_window_size_1
8964 turns out to not be a no-op (there is no way to know).
8965 The size will be adjusted again if the frame gets a
8966 ConfigureNotify event as a result of x_set_window_size. */
8967 int pixelh = FRAME_PIXEL_HEIGHT (f);
8968 #ifdef USE_X_TOOLKIT
8969 /* The menu bar is not part of text lines. The tool bar
8970 is however. */
8971 pixelh -= FRAME_MENUBAR_HEIGHT (f);
8972 #endif
8973 r = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelh);
8974 /* Update f->scroll_bar_actual_width because it is used in
8975 FRAME_PIXEL_WIDTH_TO_TEXT_COLS. */
8976 f->scroll_bar_actual_width
8977 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
8978 c = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, FRAME_PIXEL_WIDTH (f));
8979 change_frame_size (f, r, c, 0, 1, 0);
8982 #ifdef USE_GTK
8983 if (FRAME_GTK_WIDGET (f))
8984 xg_frame_set_char_size (f, cols, rows);
8985 else
8986 x_set_window_size_1 (f, change_gravity, cols, rows);
8987 #else /* not USE_GTK */
8989 x_set_window_size_1 (f, change_gravity, cols, rows);
8991 #endif /* not USE_GTK */
8993 /* If cursor was outside the new size, mark it as off. */
8994 mark_window_cursors_off (XWINDOW (f->root_window));
8996 /* Clear out any recollection of where the mouse highlighting was,
8997 since it might be in a place that's outside the new frame size.
8998 Actually checking whether it is outside is a pain in the neck,
8999 so don't try--just let the highlighting be done afresh with new size. */
9000 cancel_mouse_face (f);
9002 UNBLOCK_INPUT;
9005 /* Mouse warping. */
9007 void
9008 x_set_mouse_position (f, x, y)
9009 struct frame *f;
9010 int x, y;
9012 int pix_x, pix_y;
9014 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
9015 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
9017 if (pix_x < 0) pix_x = 0;
9018 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
9020 if (pix_y < 0) pix_y = 0;
9021 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
9023 BLOCK_INPUT;
9025 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9026 0, 0, 0, 0, pix_x, pix_y);
9027 UNBLOCK_INPUT;
9030 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9032 void
9033 x_set_mouse_pixel_position (f, pix_x, pix_y)
9034 struct frame *f;
9035 int pix_x, pix_y;
9037 BLOCK_INPUT;
9039 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9040 0, 0, 0, 0, pix_x, pix_y);
9041 UNBLOCK_INPUT;
9044 /* focus shifting, raising and lowering. */
9046 void
9047 x_focus_on_frame (f)
9048 struct frame *f;
9050 #if 0
9051 /* I don't think that the ICCCM allows programs to do things like this
9052 without the interaction of the window manager. Whatever you end up
9053 doing with this code, do it to x_unfocus_frame too. */
9054 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9055 RevertToPointerRoot, CurrentTime);
9056 #endif /* ! 0 */
9059 void
9060 x_unfocus_frame (f)
9061 struct frame *f;
9063 #if 0
9064 /* Look at the remarks in x_focus_on_frame. */
9065 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
9066 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
9067 RevertToPointerRoot, CurrentTime);
9068 #endif /* ! 0 */
9071 /* Raise frame F. */
9073 void
9074 x_raise_frame (f)
9075 struct frame *f;
9077 BLOCK_INPUT;
9078 if (f->async_visible)
9079 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9081 XFlush (FRAME_X_DISPLAY (f));
9082 UNBLOCK_INPUT;
9085 /* Lower frame F. */
9087 void
9088 x_lower_frame (f)
9089 struct frame *f;
9091 if (f->async_visible)
9093 BLOCK_INPUT;
9094 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9095 XFlush (FRAME_X_DISPLAY (f));
9096 UNBLOCK_INPUT;
9100 /* Activate frame with Extended Window Manager Hints */
9102 void
9103 x_ewmh_activate_frame (f)
9104 FRAME_PTR f;
9106 /* See Window Manager Specification/Extended Window Manager Hints at
9107 http://freedesktop.org/wiki/Specifications/wm-spec */
9109 const char *atom = "_NET_ACTIVE_WINDOW";
9110 if (f->async_visible && wm_supports (f, atom))
9112 Lisp_Object frame;
9113 XSETFRAME (frame, f);
9114 Fx_send_client_event (frame, make_number (0), frame,
9115 make_unibyte_string (atom, strlen (atom)),
9116 make_number (32),
9117 Fcons (make_number (1),
9118 Fcons (make_number (last_user_time),
9119 Qnil)));
9123 static void
9124 XTframe_raise_lower (f, raise_flag)
9125 FRAME_PTR f;
9126 int raise_flag;
9128 if (raise_flag)
9129 x_raise_frame (f);
9130 else
9131 x_lower_frame (f);
9134 /* XEmbed implementation. */
9136 void
9137 xembed_set_info (f, flags)
9138 struct frame *f;
9139 enum xembed_info flags;
9141 Atom atom;
9142 unsigned long data[2];
9144 atom = XInternAtom (FRAME_X_DISPLAY (f), "_XEMBED_INFO", False);
9146 data[0] = XEMBED_VERSION;
9147 data[1] = flags;
9149 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), atom, atom,
9150 32, PropModeReplace, (unsigned char *) data, 2);
9153 void
9154 xembed_send_message (f, time, message, detail, data1, data2)
9155 struct frame *f;
9156 Time time;
9157 enum xembed_message message;
9158 long detail;
9159 long data1;
9160 long data2;
9162 XEvent event;
9164 event.xclient.type = ClientMessage;
9165 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9166 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
9167 event.xclient.format = 32;
9168 event.xclient.data.l[0] = time;
9169 event.xclient.data.l[1] = message;
9170 event.xclient.data.l[2] = detail;
9171 event.xclient.data.l[3] = data1;
9172 event.xclient.data.l[4] = data2;
9174 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9175 False, NoEventMask, &event);
9176 XSync (FRAME_X_DISPLAY (f), False);
9179 /* Change of visibility. */
9181 /* This tries to wait until the frame is really visible.
9182 However, if the window manager asks the user where to position
9183 the frame, this will return before the user finishes doing that.
9184 The frame will not actually be visible at that time,
9185 but it will become visible later when the window manager
9186 finishes with it. */
9188 void
9189 x_make_frame_visible (f)
9190 struct frame *f;
9192 Lisp_Object type;
9193 int original_top, original_left;
9194 int retry_count = 2;
9196 retry:
9198 BLOCK_INPUT;
9200 type = x_icon_type (f);
9201 if (!NILP (type))
9202 x_bitmap_icon (f, type);
9204 if (! FRAME_VISIBLE_P (f))
9206 /* We test FRAME_GARBAGED_P here to make sure we don't
9207 call x_set_offset a second time
9208 if we get to x_make_frame_visible a second time
9209 before the window gets really visible. */
9210 if (! FRAME_ICONIFIED_P (f)
9211 && ! FRAME_X_EMBEDDED_P (f)
9212 && ! f->output_data.x->asked_for_visible)
9213 x_set_offset (f, f->left_pos, f->top_pos, 0);
9215 f->output_data.x->asked_for_visible = 1;
9217 if (! EQ (Vx_no_window_manager, Qt))
9218 x_wm_set_window_state (f, NormalState);
9219 #ifdef USE_X_TOOLKIT
9220 if (FRAME_X_EMBEDDED_P (f))
9221 xembed_set_info (f, XEMBED_MAPPED);
9222 else
9224 /* This was XtPopup, but that did nothing for an iconified frame. */
9225 XtMapWidget (f->output_data.x->widget);
9227 #else /* not USE_X_TOOLKIT */
9228 #ifdef USE_GTK
9229 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9230 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9231 #else
9232 if (FRAME_X_EMBEDDED_P (f))
9233 xembed_set_info (f, XEMBED_MAPPED);
9234 else
9235 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9236 #endif /* not USE_GTK */
9237 #endif /* not USE_X_TOOLKIT */
9240 XFlush (FRAME_X_DISPLAY (f));
9242 /* Synchronize to ensure Emacs knows the frame is visible
9243 before we do anything else. We do this loop with input not blocked
9244 so that incoming events are handled. */
9246 Lisp_Object frame;
9247 int count;
9248 /* This must be before UNBLOCK_INPUT
9249 since events that arrive in response to the actions above
9250 will set it when they are handled. */
9251 int previously_visible = f->output_data.x->has_been_visible;
9253 original_left = f->left_pos;
9254 original_top = f->top_pos;
9256 /* This must come after we set COUNT. */
9257 UNBLOCK_INPUT;
9259 /* We unblock here so that arriving X events are processed. */
9261 /* Now move the window back to where it was "supposed to be".
9262 But don't do it if the gravity is negative.
9263 When the gravity is negative, this uses a position
9264 that is 3 pixels too low. Perhaps that's really the border width.
9266 Don't do this if the window has never been visible before,
9267 because the window manager may choose the position
9268 and we don't want to override it. */
9270 if (! FRAME_VISIBLE_P (f)
9271 && ! FRAME_ICONIFIED_P (f)
9272 && ! FRAME_X_EMBEDDED_P (f)
9273 && f->win_gravity == NorthWestGravity
9274 && previously_visible)
9276 Drawable rootw;
9277 int x, y;
9278 unsigned int width, height, border, depth;
9280 BLOCK_INPUT;
9282 /* On some window managers (such as FVWM) moving an existing
9283 window, even to the same place, causes the window manager
9284 to introduce an offset. This can cause the window to move
9285 to an unexpected location. Check the geometry (a little
9286 slow here) and then verify that the window is in the right
9287 place. If the window is not in the right place, move it
9288 there, and take the potential window manager hit. */
9289 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9290 &rootw, &x, &y, &width, &height, &border, &depth);
9292 if (original_left != x || original_top != y)
9293 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9294 original_left, original_top);
9296 UNBLOCK_INPUT;
9299 XSETFRAME (frame, f);
9301 /* Wait until the frame is visible. Process X events until a
9302 MapNotify event has been seen, or until we think we won't get a
9303 MapNotify at all.. */
9304 for (count = input_signal_count + 10;
9305 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9307 /* Force processing of queued events. */
9308 x_sync (f);
9310 /* Machines that do polling rather than SIGIO have been
9311 observed to go into a busy-wait here. So we'll fake an
9312 alarm signal to let the handler know that there's something
9313 to be read. We used to raise a real alarm, but it seems
9314 that the handler isn't always enabled here. This is
9315 probably a bug. */
9316 if (input_polling_used ())
9318 /* It could be confusing if a real alarm arrives while
9319 processing the fake one. Turn it off and let the
9320 handler reset it. */
9321 extern void poll_for_input_1 P_ ((void));
9322 int old_poll_suppress_count = poll_suppress_count;
9323 poll_suppress_count = 1;
9324 poll_for_input_1 ();
9325 poll_suppress_count = old_poll_suppress_count;
9328 /* See if a MapNotify event has been processed. */
9329 FRAME_SAMPLE_VISIBILITY (f);
9332 /* 2000-09-28: In
9334 (let ((f (selected-frame)))
9335 (iconify-frame f)
9336 (raise-frame f))
9338 the frame is not raised with various window managers on
9339 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9340 unknown reason, the call to XtMapWidget is completely ignored.
9341 Mapping the widget a second time works. */
9343 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9344 goto retry;
9348 /* Change from mapped state to withdrawn state. */
9350 /* Make the frame visible (mapped and not iconified). */
9352 void
9353 x_make_frame_invisible (f)
9354 struct frame *f;
9356 Window window;
9358 /* Use the frame's outermost window, not the one we normally draw on. */
9359 window = FRAME_OUTER_WINDOW (f);
9361 /* Don't keep the highlight on an invisible frame. */
9362 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9363 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9365 BLOCK_INPUT;
9367 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9368 that the current position of the window is user-specified, rather than
9369 program-specified, so that when the window is mapped again, it will be
9370 placed at the same location, without forcing the user to position it
9371 by hand again (they have already done that once for this window.) */
9372 x_wm_set_size_hint (f, (long) 0, 1);
9374 #ifdef USE_GTK
9375 if (FRAME_GTK_OUTER_WIDGET (f))
9376 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9377 else
9378 #else
9379 if (FRAME_X_EMBEDDED_P (f))
9380 xembed_set_info (f, 0);
9381 else
9382 #endif
9385 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9386 DefaultScreen (FRAME_X_DISPLAY (f))))
9388 UNBLOCK_INPUT_RESIGNAL;
9389 error ("Can't notify window manager of window withdrawal");
9393 /* We can't distinguish this from iconification
9394 just by the event that we get from the server.
9395 So we can't win using the usual strategy of letting
9396 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9397 and synchronize with the server to make sure we agree. */
9398 f->visible = 0;
9399 FRAME_ICONIFIED_P (f) = 0;
9400 f->async_visible = 0;
9401 f->async_iconified = 0;
9403 x_sync (f);
9405 UNBLOCK_INPUT;
9408 /* Change window state from mapped to iconified. */
9410 void
9411 x_iconify_frame (f)
9412 struct frame *f;
9414 int result;
9415 Lisp_Object type;
9417 /* Don't keep the highlight on an invisible frame. */
9418 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9419 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9421 if (f->async_iconified)
9422 return;
9424 BLOCK_INPUT;
9426 FRAME_SAMPLE_VISIBILITY (f);
9428 type = x_icon_type (f);
9429 if (!NILP (type))
9430 x_bitmap_icon (f, type);
9432 #ifdef USE_GTK
9433 if (FRAME_GTK_OUTER_WIDGET (f))
9435 if (! FRAME_VISIBLE_P (f))
9436 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9438 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9439 f->iconified = 1;
9440 f->visible = 1;
9441 f->async_iconified = 1;
9442 f->async_visible = 0;
9443 UNBLOCK_INPUT;
9444 return;
9446 #endif
9448 #ifdef USE_X_TOOLKIT
9450 if (! FRAME_VISIBLE_P (f))
9452 if (! EQ (Vx_no_window_manager, Qt))
9453 x_wm_set_window_state (f, IconicState);
9454 /* This was XtPopup, but that did nothing for an iconified frame. */
9455 XtMapWidget (f->output_data.x->widget);
9456 /* The server won't give us any event to indicate
9457 that an invisible frame was changed to an icon,
9458 so we have to record it here. */
9459 f->iconified = 1;
9460 f->visible = 1;
9461 f->async_iconified = 1;
9462 f->async_visible = 0;
9463 UNBLOCK_INPUT;
9464 return;
9467 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9468 XtWindow (f->output_data.x->widget),
9469 DefaultScreen (FRAME_X_DISPLAY (f)));
9470 UNBLOCK_INPUT;
9472 if (!result)
9473 error ("Can't notify window manager of iconification");
9475 f->async_iconified = 1;
9476 f->async_visible = 0;
9479 BLOCK_INPUT;
9480 XFlush (FRAME_X_DISPLAY (f));
9481 UNBLOCK_INPUT;
9482 #else /* not USE_X_TOOLKIT */
9484 /* Make sure the X server knows where the window should be positioned,
9485 in case the user deiconifies with the window manager. */
9486 if (! FRAME_VISIBLE_P (f)
9487 && ! FRAME_ICONIFIED_P (f)
9488 && ! FRAME_X_EMBEDDED_P (f))
9489 x_set_offset (f, f->left_pos, f->top_pos, 0);
9491 /* Since we don't know which revision of X we're running, we'll use both
9492 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9494 /* X11R4: send a ClientMessage to the window manager using the
9495 WM_CHANGE_STATE type. */
9497 XEvent message;
9499 message.xclient.window = FRAME_X_WINDOW (f);
9500 message.xclient.type = ClientMessage;
9501 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9502 message.xclient.format = 32;
9503 message.xclient.data.l[0] = IconicState;
9505 if (! XSendEvent (FRAME_X_DISPLAY (f),
9506 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9507 False,
9508 SubstructureRedirectMask | SubstructureNotifyMask,
9509 &message))
9511 UNBLOCK_INPUT_RESIGNAL;
9512 error ("Can't notify window manager of iconification");
9516 /* X11R3: set the initial_state field of the window manager hints to
9517 IconicState. */
9518 x_wm_set_window_state (f, IconicState);
9520 if (!FRAME_VISIBLE_P (f))
9522 /* If the frame was withdrawn, before, we must map it. */
9523 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9526 f->async_iconified = 1;
9527 f->async_visible = 0;
9529 XFlush (FRAME_X_DISPLAY (f));
9530 UNBLOCK_INPUT;
9531 #endif /* not USE_X_TOOLKIT */
9535 /* Free X resources of frame F. */
9537 void
9538 x_free_frame_resources (f)
9539 struct frame *f;
9541 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9542 Lisp_Object bar;
9543 struct scroll_bar *b;
9545 BLOCK_INPUT;
9547 /* If a display connection is dead, don't try sending more
9548 commands to the X server. */
9549 if (dpyinfo->display)
9551 /* We must free faces before destroying windows because some
9552 font-driver (e.g. xft) access a window while finishing a
9553 face. */
9554 if (FRAME_FACE_CACHE (f))
9555 free_frame_faces (f);
9557 if (f->output_data.x->icon_desc)
9558 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9560 #ifdef USE_X_TOOLKIT
9561 /* Explicitly destroy the scroll bars of the frame. Without
9562 this, we get "BadDrawable" errors from the toolkit later on,
9563 presumably from expose events generated for the disappearing
9564 toolkit scroll bars. */
9565 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9567 b = XSCROLL_BAR (bar);
9568 x_scroll_bar_remove (b);
9570 #endif
9572 #ifdef HAVE_X_I18N
9573 if (FRAME_XIC (f))
9574 free_frame_xic (f);
9575 #endif
9577 #ifdef USE_X_TOOLKIT
9578 if (f->output_data.x->widget)
9580 XtDestroyWidget (f->output_data.x->widget);
9581 f->output_data.x->widget = NULL;
9583 /* Tooltips don't have widgets, only a simple X window, even if
9584 we are using a toolkit. */
9585 else if (FRAME_X_WINDOW (f))
9586 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9588 free_frame_menubar (f);
9589 #else /* !USE_X_TOOLKIT */
9591 #ifdef USE_GTK
9592 /* In the GTK version, tooltips are normal X
9593 frames. We must check and free both types. */
9594 if (FRAME_GTK_OUTER_WIDGET (f))
9596 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9597 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9598 FRAME_GTK_OUTER_WIDGET (f) = 0;
9600 #endif /* USE_GTK */
9602 if (FRAME_X_WINDOW (f))
9603 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9604 #endif /* !USE_X_TOOLKIT */
9606 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9607 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9608 unload_color (f, f->output_data.x->cursor_pixel);
9609 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9610 unload_color (f, f->output_data.x->border_pixel);
9611 unload_color (f, f->output_data.x->mouse_pixel);
9613 if (f->output_data.x->scroll_bar_background_pixel != -1)
9614 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9615 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9616 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9617 #ifdef USE_TOOLKIT_SCROLL_BARS
9618 /* Scrollbar shadow colors. */
9619 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9620 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9621 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9622 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9623 #endif /* USE_TOOLKIT_SCROLL_BARS */
9624 if (f->output_data.x->white_relief.allocated_p)
9625 unload_color (f, f->output_data.x->white_relief.pixel);
9626 if (f->output_data.x->black_relief.allocated_p)
9627 unload_color (f, f->output_data.x->black_relief.pixel);
9629 x_free_gcs (f);
9630 XFlush (FRAME_X_DISPLAY (f));
9633 xfree (f->output_data.x->saved_menu_event);
9634 xfree (f->output_data.x);
9635 f->output_data.x = NULL;
9637 if (f == dpyinfo->x_focus_frame)
9638 dpyinfo->x_focus_frame = 0;
9639 if (f == dpyinfo->x_focus_event_frame)
9640 dpyinfo->x_focus_event_frame = 0;
9641 if (f == dpyinfo->x_highlight_frame)
9642 dpyinfo->x_highlight_frame = 0;
9644 if (f == dpyinfo->mouse_face_mouse_frame)
9646 dpyinfo->mouse_face_beg_row
9647 = dpyinfo->mouse_face_beg_col = -1;
9648 dpyinfo->mouse_face_end_row
9649 = dpyinfo->mouse_face_end_col = -1;
9650 dpyinfo->mouse_face_window = Qnil;
9651 dpyinfo->mouse_face_deferred_gc = 0;
9652 dpyinfo->mouse_face_mouse_frame = 0;
9655 UNBLOCK_INPUT;
9659 /* Destroy the X window of frame F. */
9661 void
9662 x_destroy_window (f)
9663 struct frame *f;
9665 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9667 /* If a display connection is dead, don't try sending more
9668 commands to the X server. */
9669 if (dpyinfo->display != 0)
9670 x_free_frame_resources (f);
9672 dpyinfo->reference_count--;
9676 /* Setting window manager hints. */
9678 /* Set the normal size hints for the window manager, for frame F.
9679 FLAGS is the flags word to use--or 0 meaning preserve the flags
9680 that the window now has.
9681 If USER_POSITION is nonzero, we set the USPosition
9682 flag (this is useful when FLAGS is 0).
9683 The GTK version is in gtkutils.c */
9685 #ifndef USE_GTK
9686 void
9687 x_wm_set_size_hint (f, flags, user_position)
9688 struct frame *f;
9689 long flags;
9690 int user_position;
9692 XSizeHints size_hints;
9693 Window window = FRAME_OUTER_WINDOW (f);
9695 /* Setting PMaxSize caused various problems. */
9696 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9698 size_hints.x = f->left_pos;
9699 size_hints.y = f->top_pos;
9701 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9702 size_hints.width = FRAME_PIXEL_WIDTH (f);
9704 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9705 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9706 size_hints.max_width = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
9707 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9708 size_hints.max_height = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
9709 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9711 /* Calculate the base and minimum sizes. */
9713 int base_width, base_height;
9714 int min_rows = 0, min_cols = 0;
9716 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9717 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9719 check_frame_size (f, &min_rows, &min_cols);
9721 /* The window manager uses the base width hints to calculate the
9722 current number of rows and columns in the frame while
9723 resizing; min_width and min_height aren't useful for this
9724 purpose, since they might not give the dimensions for a
9725 zero-row, zero-column frame.
9727 We use the base_width and base_height members if we have
9728 them; otherwise, we set the min_width and min_height members
9729 to the size for a zero x zero frame. */
9731 size_hints.flags |= PBaseSize;
9732 size_hints.base_width = base_width;
9733 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
9734 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9735 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9738 /* If we don't need the old flags, we don't need the old hint at all. */
9739 if (flags)
9741 size_hints.flags |= flags;
9742 goto no_read;
9746 XSizeHints hints; /* Sometimes I hate X Windows... */
9747 long supplied_return;
9748 int value;
9750 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9751 &supplied_return);
9753 if (flags)
9754 size_hints.flags |= flags;
9755 else
9757 if (value == 0)
9758 hints.flags = 0;
9759 if (hints.flags & PSize)
9760 size_hints.flags |= PSize;
9761 if (hints.flags & PPosition)
9762 size_hints.flags |= PPosition;
9763 if (hints.flags & USPosition)
9764 size_hints.flags |= USPosition;
9765 if (hints.flags & USSize)
9766 size_hints.flags |= USSize;
9770 no_read:
9772 #ifdef PWinGravity
9773 size_hints.win_gravity = f->win_gravity;
9774 size_hints.flags |= PWinGravity;
9776 if (user_position)
9778 size_hints.flags &= ~ PPosition;
9779 size_hints.flags |= USPosition;
9781 #endif /* PWinGravity */
9783 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9785 #endif /* not USE_GTK */
9787 /* Used for IconicState or NormalState */
9789 void
9790 x_wm_set_window_state (f, state)
9791 struct frame *f;
9792 int state;
9794 #ifdef USE_X_TOOLKIT
9795 Arg al[1];
9797 XtSetArg (al[0], XtNinitialState, state);
9798 XtSetValues (f->output_data.x->widget, al, 1);
9799 #else /* not USE_X_TOOLKIT */
9800 Window window = FRAME_X_WINDOW (f);
9802 f->output_data.x->wm_hints.flags |= StateHint;
9803 f->output_data.x->wm_hints.initial_state = state;
9805 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9806 #endif /* not USE_X_TOOLKIT */
9809 void
9810 x_wm_set_icon_pixmap (f, pixmap_id)
9811 struct frame *f;
9812 int pixmap_id;
9814 Pixmap icon_pixmap, icon_mask;
9816 #ifndef USE_X_TOOLKIT
9817 Window window = FRAME_OUTER_WINDOW (f);
9818 #endif
9820 if (pixmap_id > 0)
9822 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9823 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9824 icon_mask = x_bitmap_mask (f, pixmap_id);
9825 f->output_data.x->wm_hints.icon_mask = icon_mask;
9827 else
9829 /* It seems there is no way to turn off use of an icon
9830 pixmap. */
9831 return;
9835 #ifdef USE_GTK
9837 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9838 return;
9841 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9844 Arg al[1];
9845 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9846 XtSetValues (f->output_data.x->widget, al, 1);
9847 XtSetArg (al[0], XtNiconMask, icon_mask);
9848 XtSetValues (f->output_data.x->widget, al, 1);
9851 #else /* not USE_X_TOOLKIT && not USE_GTK */
9853 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9854 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9856 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9859 void
9860 x_wm_set_icon_position (f, icon_x, icon_y)
9861 struct frame *f;
9862 int icon_x, icon_y;
9864 Window window = FRAME_OUTER_WINDOW (f);
9866 f->output_data.x->wm_hints.flags |= IconPositionHint;
9867 f->output_data.x->wm_hints.icon_x = icon_x;
9868 f->output_data.x->wm_hints.icon_y = icon_y;
9870 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9874 /***********************************************************************
9875 Fonts
9876 ***********************************************************************/
9878 #if GLYPH_DEBUG
9880 /* Check that FONT is valid on frame F. It is if it can be found in F's
9881 font table. */
9883 static void
9884 x_check_font (f, font)
9885 struct frame *f;
9886 struct font *font;
9888 Lisp_Object frame;
9890 xassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9891 if (font->driver->check)
9892 xassert (font->driver->check (f, font) == 0);
9895 #endif /* GLYPH_DEBUG != 0 */
9898 /***********************************************************************
9899 Initialization
9900 ***********************************************************************/
9902 #ifdef USE_X_TOOLKIT
9903 static XrmOptionDescRec emacs_options[] = {
9904 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9905 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9907 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9908 XrmoptionSepArg, NULL},
9909 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9911 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9912 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9913 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9914 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9915 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9916 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9917 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9920 /* Whether atimer for Xt timeouts is activated or not. */
9922 static int x_timeout_atimer_activated_flag;
9924 #endif /* USE_X_TOOLKIT */
9926 static int x_initialized;
9928 #ifdef HAVE_X_SM
9929 static int x_session_initialized;
9930 #endif
9932 /* Test whether two display-name strings agree up to the dot that separates
9933 the screen number from the server number. */
9934 static int
9935 same_x_server (name1, name2)
9936 const char *name1, *name2;
9938 int seen_colon = 0;
9939 const unsigned char *system_name = SDATA (Vsystem_name);
9940 int system_name_length = strlen (system_name);
9941 int length_until_period = 0;
9943 while (system_name[length_until_period] != 0
9944 && system_name[length_until_period] != '.')
9945 length_until_period++;
9947 /* Treat `unix' like an empty host name. */
9948 if (! strncmp (name1, "unix:", 5))
9949 name1 += 4;
9950 if (! strncmp (name2, "unix:", 5))
9951 name2 += 4;
9952 /* Treat this host's name like an empty host name. */
9953 if (! strncmp (name1, system_name, system_name_length)
9954 && name1[system_name_length] == ':')
9955 name1 += system_name_length;
9956 if (! strncmp (name2, system_name, system_name_length)
9957 && name2[system_name_length] == ':')
9958 name2 += system_name_length;
9959 /* Treat this host's domainless name like an empty host name. */
9960 if (! strncmp (name1, system_name, length_until_period)
9961 && name1[length_until_period] == ':')
9962 name1 += length_until_period;
9963 if (! strncmp (name2, system_name, length_until_period)
9964 && name2[length_until_period] == ':')
9965 name2 += length_until_period;
9967 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
9969 if (*name1 == ':')
9970 seen_colon++;
9971 if (seen_colon && *name1 == '.')
9972 return 1;
9974 return (seen_colon
9975 && (*name1 == '.' || *name1 == '\0')
9976 && (*name2 == '.' || *name2 == '\0'));
9979 /* Count number of set bits in mask and number of bits to shift to
9980 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
9981 to 5. */
9982 static void
9983 get_bits_and_offset (mask, bits, offset)
9984 unsigned long mask;
9985 int *bits;
9986 int *offset;
9988 int nr = 0;
9989 int off = 0;
9991 while (!(mask & 1))
9993 off++;
9994 mask >>= 1;
9997 while (mask & 1)
9999 nr++;
10000 mask >>= 1;
10003 *offset = off;
10004 *bits = nr;
10007 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
10008 But don't permanently open it, just test its availability. */
10011 x_display_ok (display)
10012 const char *display;
10014 int dpy_ok = 1;
10015 Display *dpy;
10017 dpy = XOpenDisplay (display);
10018 if (dpy)
10019 XCloseDisplay (dpy);
10020 else
10021 dpy_ok = 0;
10022 return dpy_ok;
10025 #ifdef USE_GTK
10026 static void
10027 my_log_handler (log_domain, log_level, message, user_data)
10028 const gchar *log_domain;
10029 GLogLevelFlags log_level;
10030 const gchar *message;
10031 gpointer user_data;
10033 if (!strstr (message, "g_set_prgname"))
10034 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, message);
10036 #endif
10038 /* Open a connection to X display DISPLAY_NAME, and return
10039 the structure that describes the open display.
10040 If we cannot contact the display, return null. */
10042 struct x_display_info *
10043 x_term_init (display_name, xrm_option, resource_name)
10044 Lisp_Object display_name;
10045 char *xrm_option;
10046 char *resource_name;
10048 int connection;
10049 Display *dpy;
10050 struct terminal *terminal;
10051 struct x_display_info *dpyinfo;
10052 XrmDatabase xrdb;
10054 BLOCK_INPUT;
10056 if (!x_initialized)
10058 x_initialize ();
10059 ++x_initialized;
10062 if (! x_display_ok (SDATA (display_name)))
10063 error ("Display %s can't be opened", SDATA (display_name));
10065 #ifdef USE_GTK
10067 #define NUM_ARGV 10
10068 int argc;
10069 char *argv[NUM_ARGV];
10070 char **argv2 = argv;
10071 GdkAtom atom;
10072 guint id;
10073 #ifndef HAVE_GTK_MULTIDISPLAY
10074 if (!EQ (Vinitial_window_system, Qx))
10075 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10076 #endif
10078 if (x_initialized++ > 1)
10080 #ifdef HAVE_GTK_MULTIDISPLAY
10081 /* Opening another display. If xg_display_open returns less
10082 than zero, we are probably on GTK 2.0, which can only handle
10083 one display. GTK 2.2 or later can handle more than one. */
10084 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10085 #endif
10086 error ("Sorry, this version of GTK can only handle one display");
10088 else
10090 for (argc = 0; argc < NUM_ARGV; ++argc)
10091 argv[argc] = 0;
10093 argc = 0;
10094 argv[argc++] = initial_argv[0];
10096 if (! NILP (display_name))
10098 argv[argc++] = "--display";
10099 argv[argc++] = SDATA (display_name);
10102 argv[argc++] = "--name";
10103 argv[argc++] = resource_name;
10105 XSetLocaleModifiers ("");
10107 /* Work around GLib bug that outputs a faulty warning. See
10108 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
10109 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
10110 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
10111 gtk_init (&argc, &argv2);
10112 g_log_remove_handler ("GLib", id);
10114 /* gtk_init does set_locale. We must fix locale after calling it. */
10115 fixup_locale ();
10116 xg_initialize ();
10118 dpy = GDK_DISPLAY ();
10120 /* NULL window -> events for all windows go to our function */
10121 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10123 /* Load our own gtkrc if it exists. */
10125 char *file = "~/.emacs.d/gtkrc";
10126 Lisp_Object s, abs_file;
10128 s = make_string (file, strlen (file));
10129 abs_file = Fexpand_file_name (s, Qnil);
10131 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10132 gtk_rc_parse (SDATA (abs_file));
10135 XSetErrorHandler (x_error_handler);
10136 XSetIOErrorHandler (x_io_error_quitter);
10139 #else /* not USE_GTK */
10140 #ifdef USE_X_TOOLKIT
10141 /* weiner@footloose.sps.mot.com reports that this causes
10142 errors with X11R5:
10143 X protocol error: BadAtom (invalid Atom parameter)
10144 on protocol request 18skiloaf.
10145 So let's not use it until R6. */
10146 #ifdef HAVE_X11XTR6
10147 XtSetLanguageProc (NULL, NULL, NULL);
10148 #endif
10151 int argc = 0;
10152 char *argv[3];
10154 argv[0] = "";
10155 argc = 1;
10156 if (xrm_option)
10158 argv[argc++] = "-xrm";
10159 argv[argc++] = xrm_option;
10161 turn_on_atimers (0);
10162 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10163 resource_name, EMACS_CLASS,
10164 emacs_options, XtNumber (emacs_options),
10165 &argc, argv);
10166 turn_on_atimers (1);
10168 #ifdef HAVE_X11XTR6
10169 /* I think this is to compensate for XtSetLanguageProc. */
10170 fixup_locale ();
10171 #endif
10174 #else /* not USE_X_TOOLKIT */
10175 XSetLocaleModifiers ("");
10176 dpy = XOpenDisplay (SDATA (display_name));
10177 #endif /* not USE_X_TOOLKIT */
10178 #endif /* not USE_GTK*/
10180 /* Detect failure. */
10181 if (dpy == 0)
10183 UNBLOCK_INPUT;
10184 return 0;
10187 /* We have definitely succeeded. Record the new connection. */
10189 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10190 bzero (dpyinfo, sizeof *dpyinfo);
10192 terminal = x_create_terminal (dpyinfo);
10195 struct x_display_info *share;
10196 Lisp_Object tail;
10198 for (share = x_display_list, tail = x_display_name_list; share;
10199 share = share->next, tail = XCDR (tail))
10200 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10201 SDATA (display_name)))
10202 break;
10203 if (share)
10204 terminal->kboard = share->terminal->kboard;
10205 else
10207 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10208 init_kboard (terminal->kboard);
10209 terminal->kboard->Vwindow_system = Qx;
10210 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10212 char *vendor = ServerVendor (dpy);
10213 /* Temporarily hide the partially initialized terminal */
10214 terminal_list = terminal->next_terminal;
10215 UNBLOCK_INPUT;
10216 terminal->kboard->Vsystem_key_alist
10217 = call1 (Qvendor_specific_keysyms,
10218 vendor ? build_string (vendor) : empty_unibyte_string);
10219 BLOCK_INPUT;
10220 terminal->next_terminal = terminal_list;
10221 terminal_list = terminal;
10224 terminal->kboard->next_kboard = all_kboards;
10225 all_kboards = terminal->kboard;
10226 /* Don't let the initial kboard remain current longer than necessary.
10227 That would cause problems if a file loaded on startup tries to
10228 prompt in the mini-buffer. */
10229 if (current_kboard == initial_kboard)
10230 current_kboard = terminal->kboard;
10232 terminal->kboard->reference_count++;
10235 /* Put this display on the chain. */
10236 dpyinfo->next = x_display_list;
10237 x_display_list = dpyinfo;
10239 /* Put it on x_display_name_list as well, to keep them parallel. */
10240 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10241 x_display_name_list);
10242 dpyinfo->name_list_element = XCAR (x_display_name_list);
10244 dpyinfo->display = dpy;
10246 /* Set the name of the terminal. */
10247 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10248 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10249 terminal->name[SBYTES (display_name)] = 0;
10251 #if 0
10252 XSetAfterFunction (x_current_display, x_trace_wire);
10253 #endif /* ! 0 */
10255 dpyinfo->x_id_name
10256 = (char *) xmalloc (SBYTES (Vinvocation_name)
10257 + SBYTES (Vsystem_name)
10258 + 2);
10259 sprintf (dpyinfo->x_id_name, "%s@%s",
10260 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10262 /* Figure out which modifier bits mean what. */
10263 x_find_modifier_meanings (dpyinfo);
10265 /* Get the scroll bar cursor. */
10266 #ifdef USE_GTK
10267 /* We must create a GTK cursor, it is required for GTK widgets. */
10268 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10269 #endif /* USE_GTK */
10271 dpyinfo->vertical_scroll_bar_cursor
10272 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10274 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10275 resource_name, EMACS_CLASS);
10276 #ifdef HAVE_XRMSETDATABASE
10277 XrmSetDatabase (dpyinfo->display, xrdb);
10278 #else
10279 dpyinfo->display->db = xrdb;
10280 #endif
10281 /* Put the rdb where we can find it in a way that works on
10282 all versions. */
10283 dpyinfo->xrdb = xrdb;
10285 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10286 DefaultScreen (dpyinfo->display));
10287 select_visual (dpyinfo);
10288 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10289 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10290 dpyinfo->client_leader_window = 0;
10291 dpyinfo->grabbed = 0;
10292 dpyinfo->reference_count = 0;
10293 dpyinfo->icon_bitmap_id = -1;
10294 dpyinfo->n_fonts = 0;
10295 dpyinfo->bitmaps = 0;
10296 dpyinfo->bitmaps_size = 0;
10297 dpyinfo->bitmaps_last = 0;
10298 dpyinfo->scratch_cursor_gc = 0;
10299 dpyinfo->mouse_face_mouse_frame = 0;
10300 dpyinfo->mouse_face_deferred_gc = 0;
10301 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10302 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10303 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10304 dpyinfo->mouse_face_window = Qnil;
10305 dpyinfo->mouse_face_overlay = Qnil;
10306 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10307 dpyinfo->mouse_face_defer = 0;
10308 dpyinfo->mouse_face_hidden = 0;
10309 dpyinfo->x_focus_frame = 0;
10310 dpyinfo->x_focus_event_frame = 0;
10311 dpyinfo->x_highlight_frame = 0;
10312 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10314 /* See if we can construct pixel values from RGB values. */
10315 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10316 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10318 if (dpyinfo->visual->class == TrueColor)
10320 get_bits_and_offset (dpyinfo->visual->red_mask,
10321 &dpyinfo->red_bits, &dpyinfo->red_offset);
10322 get_bits_and_offset (dpyinfo->visual->blue_mask,
10323 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10324 get_bits_and_offset (dpyinfo->visual->green_mask,
10325 &dpyinfo->green_bits, &dpyinfo->green_offset);
10328 /* See if a private colormap is requested. */
10329 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10331 if (dpyinfo->visual->class == PseudoColor)
10333 Lisp_Object value;
10334 value = display_x_get_resource (dpyinfo,
10335 build_string ("privateColormap"),
10336 build_string ("PrivateColormap"),
10337 Qnil, Qnil);
10338 if (STRINGP (value)
10339 && (!strcmp (SDATA (value), "true")
10340 || !strcmp (SDATA (value), "on")))
10341 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10344 else
10345 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10346 dpyinfo->visual, AllocNone);
10348 #ifdef HAVE_XFT
10350 /* If we are using Xft, check dpi value in X resources.
10351 It is better we use it as well, since Xft will use it, as will all
10352 Gnome applications. If our real DPI is smaller or larger than the
10353 one Xft uses, our font will look smaller or larger than other
10354 for other applications, even if it is the same font name (monospace-10
10355 for example). */
10356 char *v = XGetDefault (dpyinfo->display, "Xft", "dpi");
10357 double d;
10358 if (v != NULL && sscanf (v, "%lf", &d) == 1)
10359 dpyinfo->resy = dpyinfo->resx = d;
10361 #endif
10363 if (dpyinfo->resy < 1)
10365 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10366 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10367 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10368 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10369 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10370 pixels = DisplayWidth (dpyinfo->display, screen_number);
10371 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10372 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10373 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10376 dpyinfo->Xatom_wm_protocols
10377 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10378 dpyinfo->Xatom_wm_take_focus
10379 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10380 dpyinfo->Xatom_wm_save_yourself
10381 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10382 dpyinfo->Xatom_wm_delete_window
10383 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10384 dpyinfo->Xatom_wm_change_state
10385 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10386 dpyinfo->Xatom_wm_configure_denied
10387 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10388 dpyinfo->Xatom_wm_window_moved
10389 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10390 dpyinfo->Xatom_wm_client_leader
10391 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10392 dpyinfo->Xatom_editres
10393 = XInternAtom (dpyinfo->display, "Editres", False);
10394 dpyinfo->Xatom_CLIPBOARD
10395 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10396 dpyinfo->Xatom_TIMESTAMP
10397 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10398 dpyinfo->Xatom_TEXT
10399 = XInternAtom (dpyinfo->display, "TEXT", False);
10400 dpyinfo->Xatom_COMPOUND_TEXT
10401 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10402 dpyinfo->Xatom_UTF8_STRING
10403 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10404 dpyinfo->Xatom_DELETE
10405 = XInternAtom (dpyinfo->display, "DELETE", False);
10406 dpyinfo->Xatom_MULTIPLE
10407 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10408 dpyinfo->Xatom_INCR
10409 = XInternAtom (dpyinfo->display, "INCR", False);
10410 dpyinfo->Xatom_EMACS_TMP
10411 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10412 dpyinfo->Xatom_TARGETS
10413 = XInternAtom (dpyinfo->display, "TARGETS", False);
10414 dpyinfo->Xatom_NULL
10415 = XInternAtom (dpyinfo->display, "NULL", False);
10416 dpyinfo->Xatom_ATOM_PAIR
10417 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10418 /* For properties of font. */
10419 dpyinfo->Xatom_PIXEL_SIZE
10420 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10421 dpyinfo->Xatom_AVERAGE_WIDTH
10422 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10423 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10424 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10425 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10426 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10427 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10428 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10430 /* Ghostscript support. */
10431 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10432 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10434 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10435 False);
10437 dpyinfo->Xatom_XEMBED = XInternAtom (dpyinfo->display, "_XEMBED",
10438 False);
10440 dpyinfo->Xatom_net_wm_state
10441 = XInternAtom (dpyinfo->display, "_NET_WM_STATE", False);
10442 dpyinfo->Xatom_net_wm_state_fullscreen_atom
10443 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_FULLSCREEN", False);
10444 dpyinfo->Xatom_net_wm_state_maximized_horz
10445 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
10446 dpyinfo->Xatom_net_wm_state_maximized_vert
10447 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
10448 dpyinfo->Xatom_net_wm_state_sticky
10449 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_STICKY", False);
10450 dpyinfo->Xatom_net_window_type
10451 = XInternAtom (dpyinfo->display, "_NET_WM_WINDOW_TYPE", False);
10452 dpyinfo->Xatom_net_window_type_tooltip
10453 = XInternAtom (dpyinfo->display, "_NET_WM_WINDOW_TYPE_TOOLTIP", False);
10455 dpyinfo->cut_buffers_initialized = 0;
10457 dpyinfo->x_dnd_atoms_size = 8;
10458 dpyinfo->x_dnd_atoms_length = 0;
10459 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10460 * dpyinfo->x_dnd_atoms_size);
10462 dpyinfo->net_supported_atoms = NULL;
10463 dpyinfo->nr_net_supported_atoms = 0;
10464 dpyinfo->net_supported_window = 0;
10466 connection = ConnectionNumber (dpyinfo->display);
10467 dpyinfo->connection = connection;
10470 extern int gray_bitmap_width, gray_bitmap_height;
10471 extern char *gray_bitmap_bits;
10472 dpyinfo->gray
10473 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10474 gray_bitmap_bits,
10475 gray_bitmap_width, gray_bitmap_height,
10476 (unsigned long) 1, (unsigned long) 0, 1);
10479 #ifdef HAVE_X_I18N
10480 xim_initialize (dpyinfo, resource_name);
10481 #endif
10483 xsettings_initialize (dpyinfo);
10485 #ifdef subprocesses
10486 /* This is only needed for distinguishing keyboard and process input. */
10487 if (connection != 0)
10488 add_keyboard_wait_descriptor (connection);
10489 #endif
10491 #ifdef F_SETOWN
10492 fcntl (connection, F_SETOWN, getpid ());
10493 #endif /* ! defined (F_SETOWN) */
10495 #ifdef SIGIO
10496 if (interrupt_input)
10497 init_sigio (connection);
10498 #endif /* ! defined (SIGIO) */
10500 #ifdef USE_LUCID
10502 Display *dpy = dpyinfo->display;
10503 XrmValue d, fr, to;
10504 Font font;
10506 d.addr = (XPointer)&dpy;
10507 d.size = sizeof (Display *);
10508 fr.addr = XtDefaultFont;
10509 fr.size = sizeof (XtDefaultFont);
10510 to.size = sizeof (Font *);
10511 to.addr = (XPointer)&font;
10512 x_catch_errors (dpy);
10513 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10514 abort ();
10515 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10516 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10517 x_uncatch_errors ();
10519 #endif
10521 /* See if we should run in synchronous mode. This is useful
10522 for debugging X code. */
10524 Lisp_Object value;
10525 value = display_x_get_resource (dpyinfo,
10526 build_string ("synchronous"),
10527 build_string ("Synchronous"),
10528 Qnil, Qnil);
10529 if (STRINGP (value)
10530 && (!strcmp (SDATA (value), "true")
10531 || !strcmp (SDATA (value), "on")))
10532 XSynchronize (dpyinfo->display, True);
10536 Lisp_Object value;
10537 value = display_x_get_resource (dpyinfo,
10538 build_string ("useXIM"),
10539 build_string ("UseXIM"),
10540 Qnil, Qnil);
10541 #ifdef USE_XIM
10542 if (STRINGP (value)
10543 && (!strcmp (SDATA (value), "false")
10544 || !strcmp (SDATA (value), "off")))
10545 use_xim = 0;
10546 #else
10547 if (STRINGP (value)
10548 && (!strcmp (SDATA (value), "true")
10549 || !strcmp (SDATA (value), "on")))
10550 use_xim = 1;
10551 #endif
10554 #ifdef HAVE_X_SM
10555 /* Only do this for the very first display in the Emacs session.
10556 Ignore X session management when Emacs was first started on a
10557 tty. */
10558 if (terminal->id == 1)
10559 x_session_initialize (dpyinfo);
10560 #endif
10562 UNBLOCK_INPUT;
10564 return dpyinfo;
10567 /* Get rid of display DPYINFO, deleting all frames on it,
10568 and without sending any more commands to the X server. */
10570 void
10571 x_delete_display (dpyinfo)
10572 struct x_display_info *dpyinfo;
10574 int i;
10575 struct terminal *t;
10577 /* Close all frames and delete the generic struct terminal for this
10578 X display. */
10579 for (t = terminal_list; t; t = t->next_terminal)
10580 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10582 #ifdef HAVE_X_SM
10583 /* Close X session management when we close its display. */
10584 if (t->id == 1 && x_session_have_connection ())
10585 x_session_close();
10586 #endif
10587 delete_terminal (t);
10588 break;
10591 delete_keyboard_wait_descriptor (dpyinfo->connection);
10593 /* Discard this display from x_display_name_list and x_display_list.
10594 We can't use Fdelq because that can quit. */
10595 if (! NILP (x_display_name_list)
10596 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10597 x_display_name_list = XCDR (x_display_name_list);
10598 else
10600 Lisp_Object tail;
10602 tail = x_display_name_list;
10603 while (CONSP (tail) && CONSP (XCDR (tail)))
10605 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10607 XSETCDR (tail, XCDR (XCDR (tail)));
10608 break;
10610 tail = XCDR (tail);
10614 if (next_noop_dpyinfo == dpyinfo)
10615 next_noop_dpyinfo = dpyinfo->next;
10617 if (x_display_list == dpyinfo)
10618 x_display_list = dpyinfo->next;
10619 else
10621 struct x_display_info *tail;
10623 for (tail = x_display_list; tail; tail = tail->next)
10624 if (tail->next == dpyinfo)
10625 tail->next = tail->next->next;
10628 xfree (dpyinfo->x_id_name);
10629 xfree (dpyinfo->x_dnd_atoms);
10630 xfree (dpyinfo->color_cells);
10631 xfree (dpyinfo);
10634 #ifdef USE_X_TOOLKIT
10636 /* Atimer callback function for TIMER. Called every 0.1s to process
10637 Xt timeouts, if needed. We must avoid calling XtAppPending as
10638 much as possible because that function does an implicit XFlush
10639 that slows us down. */
10641 static void
10642 x_process_timeouts (timer)
10643 struct atimer *timer;
10645 BLOCK_INPUT;
10646 x_timeout_atimer_activated_flag = 0;
10647 if (toolkit_scroll_bar_interaction || popup_activated ())
10649 while (XtAppPending (Xt_app_con) & XtIMTimer)
10650 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10651 /* Reactivate the atimer for next time. */
10652 x_activate_timeout_atimer ();
10654 UNBLOCK_INPUT;
10657 /* Install an asynchronous timer that processes Xt timeout events
10658 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10659 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10660 function whenever these variables are set. This is necessary
10661 because some widget sets use timeouts internally, for example the
10662 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10663 processed, these widgets don't behave normally. */
10665 void
10666 x_activate_timeout_atimer ()
10668 BLOCK_INPUT;
10669 if (!x_timeout_atimer_activated_flag)
10671 EMACS_TIME interval;
10673 EMACS_SET_SECS_USECS (interval, 0, 100000);
10674 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10675 x_timeout_atimer_activated_flag = 1;
10677 UNBLOCK_INPUT;
10680 #endif /* USE_X_TOOLKIT */
10683 /* Set up use of X before we make the first connection. */
10685 extern frame_parm_handler x_frame_parm_handlers[];
10687 static struct redisplay_interface x_redisplay_interface =
10689 x_frame_parm_handlers,
10690 x_produce_glyphs,
10691 x_write_glyphs,
10692 x_insert_glyphs,
10693 x_clear_end_of_line,
10694 x_scroll_run,
10695 x_after_update_window_line,
10696 x_update_window_begin,
10697 x_update_window_end,
10698 x_cursor_to,
10699 x_flush,
10700 #ifdef XFlush
10701 x_flush,
10702 #else
10703 0, /* flush_display_optional */
10704 #endif
10705 x_clear_window_mouse_face,
10706 x_get_glyph_overhangs,
10707 x_fix_overlapping_area,
10708 x_draw_fringe_bitmap,
10709 0, /* define_fringe_bitmap */
10710 0, /* destroy_fringe_bitmap */
10711 x_compute_glyph_string_overhangs,
10712 x_draw_glyph_string,
10713 x_define_frame_cursor,
10714 x_clear_frame_area,
10715 x_draw_window_cursor,
10716 x_draw_vertical_window_border,
10717 x_shift_glyphs_for_insert
10721 /* This function is called when the last frame on a display is deleted. */
10722 void
10723 x_delete_terminal (struct terminal *terminal)
10725 struct x_display_info *dpyinfo = terminal->display_info.x;
10726 int i;
10728 /* Protect against recursive calls. delete_frame in
10729 delete_terminal calls us back when it deletes our last frame. */
10730 if (!terminal->name)
10731 return;
10733 BLOCK_INPUT;
10734 #ifdef HAVE_X_I18N
10735 /* We must close our connection to the XIM server before closing the
10736 X display. */
10737 if (dpyinfo->xim)
10738 xim_close_dpy (dpyinfo);
10739 #endif
10741 /* If called from x_connection_closed, the display may already be closed
10742 and dpyinfo->display was set to 0 to indicate that. */
10743 if (dpyinfo->display)
10745 x_destroy_all_bitmaps (dpyinfo);
10746 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10748 /* Whether or not XCloseDisplay destroys the associated resource
10749 database depends on the version of libX11. To avoid both
10750 crash and memory leak, we dissociate the database from the
10751 display and then destroy dpyinfo->xrdb ourselves.
10753 Unfortunately, the above strategy does not work in some
10754 situations due to a bug in newer versions of libX11: because
10755 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
10756 dpy->db is NULL, XCloseDisplay destroys the associated
10757 database whereas it has not been created by XGetDefault
10758 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
10759 don't destroy the database here in order to avoid the crash
10760 in the above situations for now, though that may cause memory
10761 leaks in other situations. */
10762 #if 0
10763 #ifdef HAVE_XRMSETDATABASE
10764 XrmSetDatabase (dpyinfo->display, NULL);
10765 #else
10766 dpyinfo->display->db = NULL;
10767 #endif
10768 /* We used to call XrmDestroyDatabase from x_delete_display, but
10769 some older versions of libX11 crash if we call it after
10770 closing all the displays. */
10771 XrmDestroyDatabase (dpyinfo->xrdb);
10772 #endif
10774 #ifdef USE_GTK
10775 xg_display_close (dpyinfo->display);
10776 #else
10777 #ifdef USE_X_TOOLKIT
10778 XtCloseDisplay (dpyinfo->display);
10779 #else
10780 XCloseDisplay (dpyinfo->display);
10781 #endif
10782 #endif /* ! USE_GTK */
10785 /* Mark as dead. */
10786 dpyinfo->display = NULL;
10787 x_delete_display (dpyinfo);
10788 UNBLOCK_INPUT;
10791 /* Create a struct terminal, initialize it with the X11 specific
10792 functions and make DISPLAY->TERMINAL point to it. */
10794 static struct terminal *
10795 x_create_terminal (struct x_display_info *dpyinfo)
10797 struct terminal *terminal;
10799 terminal = create_terminal ();
10801 terminal->type = output_x_window;
10802 terminal->display_info.x = dpyinfo;
10803 dpyinfo->terminal = terminal;
10805 /* kboard is initialized in x_term_init. */
10807 terminal->clear_frame_hook = x_clear_frame;
10808 terminal->ins_del_lines_hook = x_ins_del_lines;
10809 terminal->delete_glyphs_hook = x_delete_glyphs;
10810 terminal->ring_bell_hook = XTring_bell;
10811 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
10812 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
10813 terminal->set_terminal_modes_hook = XTset_terminal_modes;
10814 terminal->update_begin_hook = x_update_begin;
10815 terminal->update_end_hook = x_update_end;
10816 terminal->set_terminal_window_hook = XTset_terminal_window;
10817 terminal->read_socket_hook = XTread_socket;
10818 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10819 terminal->mouse_position_hook = XTmouse_position;
10820 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10821 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10822 terminal->fullscreen_hook = XTfullscreen_hook;
10823 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10824 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10825 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10826 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10828 terminal->delete_frame_hook = x_destroy_window;
10829 terminal->delete_terminal_hook = x_delete_terminal;
10831 terminal->rif = &x_redisplay_interface;
10832 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10833 terminal->char_ins_del_ok = 1;
10834 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10835 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10836 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10837 off the bottom. */
10839 return terminal;
10842 void
10843 x_initialize ()
10845 baud_rate = 19200;
10847 x_noop_count = 0;
10848 last_tool_bar_item = -1;
10849 any_help_event_p = 0;
10850 ignore_next_mouse_click_timeout = 0;
10851 #ifdef HAVE_X_SM
10852 x_session_initialized = 0;
10853 #endif
10855 #ifdef USE_GTK
10856 current_count = -1;
10857 #endif
10859 /* Try to use interrupt input; if we can't, then start polling. */
10860 Fset_input_interrupt_mode (Qt);
10862 #ifdef USE_X_TOOLKIT
10863 XtToolkitInitialize ();
10865 Xt_app_con = XtCreateApplicationContext ();
10867 /* Register a converter from strings to pixels, which uses
10868 Emacs' color allocation infrastructure. */
10869 XtAppSetTypeConverter (Xt_app_con,
10870 XtRString, XtRPixel, cvt_string_to_pixel,
10871 cvt_string_to_pixel_args,
10872 XtNumber (cvt_string_to_pixel_args),
10873 XtCacheByDisplay, cvt_pixel_dtor);
10875 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10876 #endif
10878 #ifdef USE_TOOLKIT_SCROLL_BARS
10879 #ifndef USE_GTK
10880 xaw3d_arrow_scroll = False;
10881 xaw3d_pick_top = True;
10882 #endif
10883 #endif
10885 pending_autoraise_frame = 0;
10886 pending_event_wait.f = 0;
10887 pending_event_wait.eventtype = 0;
10889 /* Note that there is no real way portable across R3/R4 to get the
10890 original error handler. */
10891 XSetErrorHandler (x_error_handler);
10892 XSetIOErrorHandler (x_io_error_quitter);
10894 signal (SIGPIPE, x_connection_signal);
10896 xgselect_initialize ();
10900 void
10901 syms_of_xterm ()
10903 x_error_message = NULL;
10905 staticpro (&x_display_name_list);
10906 x_display_name_list = Qnil;
10908 staticpro (&last_mouse_scroll_bar);
10909 last_mouse_scroll_bar = Qnil;
10911 staticpro (&Qvendor_specific_keysyms);
10912 Qvendor_specific_keysyms = intern_c_string ("vendor-specific-keysyms");
10914 staticpro (&Qlatin_1);
10915 Qlatin_1 = intern_c_string ("latin-1");
10917 staticpro (&last_mouse_press_frame);
10918 last_mouse_press_frame = Qnil;
10920 #ifdef USE_GTK
10921 xg_default_icon_file = make_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
10922 staticpro (&xg_default_icon_file);
10924 Qx_gtk_map_stock = intern_c_string ("x-gtk-map-stock");
10925 staticpro (&Qx_gtk_map_stock);
10926 #endif
10928 DEFVAR_BOOL ("x-use-underline-position-properties",
10929 &x_use_underline_position_properties,
10930 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10931 A value of nil means ignore them. If you encounter fonts with bogus
10932 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10933 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10934 to override the font's UNDERLINE_POSITION for small font display
10935 sizes. */);
10936 x_use_underline_position_properties = 1;
10938 DEFVAR_BOOL ("x-underline-at-descent-line",
10939 &x_underline_at_descent_line,
10940 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
10941 A value of nil means to draw the underline according to the value of the
10942 variable `x-use-underline-position-properties', which is usually at the
10943 baseline level. The default value is nil. */);
10944 x_underline_at_descent_line = 0;
10946 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10947 &x_mouse_click_focus_ignore_position,
10948 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10949 This variable is only used when the window manager requires that you
10950 click on a frame to select it (give it focus). In that case, a value
10951 of nil, means that the selected window and cursor position changes to
10952 reflect the mouse click position, while a non-nil value means that the
10953 selected window or cursor position is preserved. */);
10954 x_mouse_click_focus_ignore_position = 0;
10956 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10957 doc: /* What X toolkit scroll bars Emacs uses.
10958 A value of nil means Emacs doesn't use X toolkit scroll bars.
10959 Otherwise, value is a symbol describing the X toolkit. */);
10960 #ifdef USE_TOOLKIT_SCROLL_BARS
10961 #ifdef USE_MOTIF
10962 Vx_toolkit_scroll_bars = intern_c_string ("motif");
10963 #elif defined HAVE_XAW3D
10964 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
10965 #elif USE_GTK
10966 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
10967 #else
10968 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
10969 #endif
10970 #else
10971 Vx_toolkit_scroll_bars = Qnil;
10972 #endif
10974 staticpro (&last_mouse_motion_frame);
10975 last_mouse_motion_frame = Qnil;
10977 Qmodifier_value = intern_c_string ("modifier-value");
10978 Qalt = intern_c_string ("alt");
10979 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10980 Qhyper = intern_c_string ("hyper");
10981 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10982 Qmeta = intern_c_string ("meta");
10983 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10984 Qsuper = intern_c_string ("super");
10985 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10987 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10988 doc: /* Which keys Emacs uses for the alt modifier.
10989 This should be one of the symbols `alt', `hyper', `meta', `super'.
10990 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10991 is nil, which is the same as `alt'. */);
10992 Vx_alt_keysym = Qnil;
10994 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
10995 doc: /* Which keys Emacs uses for the hyper modifier.
10996 This should be one of the symbols `alt', `hyper', `meta', `super'.
10997 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10998 default is nil, which is the same as `hyper'. */);
10999 Vx_hyper_keysym = Qnil;
11001 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
11002 doc: /* Which keys Emacs uses for the meta modifier.
11003 This should be one of the symbols `alt', `hyper', `meta', `super'.
11004 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11005 default is nil, which is the same as `meta'. */);
11006 Vx_meta_keysym = Qnil;
11008 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
11009 doc: /* Which keys Emacs uses for the super modifier.
11010 This should be one of the symbols `alt', `hyper', `meta', `super'.
11011 For example, `super' means use the Super_L and Super_R keysyms. The
11012 default is nil, which is the same as `super'. */);
11013 Vx_super_keysym = Qnil;
11015 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
11016 doc: /* Hash table of character codes indexed by X keysym codes. */);
11017 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
11018 make_float (DEFAULT_REHASH_SIZE),
11019 make_float (DEFAULT_REHASH_THRESHOLD),
11020 Qnil, Qnil, Qnil);
11023 #endif /* HAVE_X_WINDOWS */
11025 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
11026 (do not change this comment) */