make the minibuffer mutex recursive.
[emacs.git] / src / xterm.c
blob57c67078580ca0135a65c509e5a4dbdac3456052
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
22 /* Xt features made by Fred Pierresteguy. */
24 #include <config.h>
26 /* On 4.3 these lose if they come after xterm.h. */
27 /* Putting these at the beginning seems to be standard for other .c files. */
28 #include <signal.h>
30 #include <stdio.h>
31 #include <setjmp.h>
33 #ifdef HAVE_X_WINDOWS
35 #include "lisp.h"
36 #include "blockinput.h"
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef 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 impl_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 impl_Vcommand_line_args, impl_Vsystem_name;
306 /* Tells if a window manager is present or not. */
308 extern Lisp_Object impl_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 impl_Vx_alt_keysym, impl_Vx_hyper_keysym, impl_Vx_meta_keysym, impl_Vx_super_keysym;
321 Lisp_Object impl_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 impl_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 if (s->face->overstrike)
1382 font->driver->draw (s, j, i, x + 1, y, 0);
1383 x += width;
1385 xoff = LGLYPH_XOFF (glyph);
1386 yoff = LGLYPH_YOFF (glyph);
1387 wadjust = LGLYPH_WADJUST (glyph);
1388 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1389 if (s->face->overstrike)
1390 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff, 0);
1391 x += wadjust;
1392 j = i + 1;
1393 width = 0;
1396 if (j < i)
1398 font->driver->draw (s, j, i, x, y, 0);
1399 if (s->face->overstrike)
1400 font->driver->draw (s, j, i, x + 1, y, 0);
1406 #ifdef USE_X_TOOLKIT
1408 static struct frame *x_frame_of_widget P_ ((Widget));
1409 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1410 XrmValue *, XrmValue *, XtPointer *));
1411 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1412 XrmValue *, Cardinal *));
1415 /* Return the frame on which widget WIDGET is used.. Abort if frame
1416 cannot be determined. */
1418 static struct frame *
1419 x_frame_of_widget (widget)
1420 Widget widget;
1422 struct x_display_info *dpyinfo;
1423 Lisp_Object tail;
1424 struct frame *f;
1426 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1428 /* Find the top-level shell of the widget. Note that this function
1429 can be called when the widget is not yet realized, so XtWindow
1430 (widget) == 0. That's the reason we can't simply use
1431 x_any_window_to_frame. */
1432 while (!XtIsTopLevelShell (widget))
1433 widget = XtParent (widget);
1435 /* Look for a frame with that top-level widget. Allocate the color
1436 on that frame to get the right gamma correction value. */
1437 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1438 if (FRAMEP (XCAR (tail))
1439 && (f = XFRAME (XCAR (tail)),
1440 (FRAME_X_P (f)
1441 && f->output_data.nothing != 1
1442 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1443 && f->output_data.x->widget == widget)
1444 return f;
1446 abort ();
1450 /* Allocate the color COLOR->pixel on the screen and display of
1451 widget WIDGET in colormap CMAP. If an exact match cannot be
1452 allocated, try the nearest color available. Value is non-zero
1453 if successful. This is called from lwlib. */
1456 x_alloc_nearest_color_for_widget (widget, cmap, color)
1457 Widget widget;
1458 Colormap cmap;
1459 XColor *color;
1461 struct frame *f = x_frame_of_widget (widget);
1462 return x_alloc_nearest_color (f, cmap, color);
1466 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1467 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1468 If this produces the same color as PIXEL, try a color where all RGB
1469 values have DELTA added. Return the allocated color in *PIXEL.
1470 DISPLAY is the X display, CMAP is the colormap to operate on.
1471 Value is non-zero if successful. */
1474 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1475 Widget widget;
1476 Display *display;
1477 Colormap cmap;
1478 unsigned long *pixel;
1479 double factor;
1480 int delta;
1482 struct frame *f = x_frame_of_widget (widget);
1483 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1487 /* Structure specifying which arguments should be passed by Xt to
1488 cvt_string_to_pixel. We want the widget's screen and colormap. */
1490 static XtConvertArgRec cvt_string_to_pixel_args[] =
1492 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1493 sizeof (Screen *)},
1494 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1495 sizeof (Colormap)}
1499 /* The address of this variable is returned by
1500 cvt_string_to_pixel. */
1502 static Pixel cvt_string_to_pixel_value;
1505 /* Convert a color name to a pixel color.
1507 DPY is the display we are working on.
1509 ARGS is an array of *NARGS XrmValue structures holding additional
1510 information about the widget for which the conversion takes place.
1511 The contents of this array are determined by the specification
1512 in cvt_string_to_pixel_args.
1514 FROM is a pointer to an XrmValue which points to the color name to
1515 convert. TO is an XrmValue in which to return the pixel color.
1517 CLOSURE_RET is a pointer to user-data, in which we record if
1518 we allocated the color or not.
1520 Value is True if successful, False otherwise. */
1522 static Boolean
1523 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1524 Display *dpy;
1525 XrmValue *args;
1526 Cardinal *nargs;
1527 XrmValue *from, *to;
1528 XtPointer *closure_ret;
1530 Screen *screen;
1531 Colormap cmap;
1532 Pixel pixel;
1533 String color_name;
1534 XColor color;
1536 if (*nargs != 2)
1538 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1539 "wrongParameters", "cvt_string_to_pixel",
1540 "XtToolkitError",
1541 "Screen and colormap args required", NULL, NULL);
1542 return False;
1545 screen = *(Screen **) args[0].addr;
1546 cmap = *(Colormap *) args[1].addr;
1547 color_name = (String) from->addr;
1549 if (strcmp (color_name, XtDefaultBackground) == 0)
1551 *closure_ret = (XtPointer) False;
1552 pixel = WhitePixelOfScreen (screen);
1554 else if (strcmp (color_name, XtDefaultForeground) == 0)
1556 *closure_ret = (XtPointer) False;
1557 pixel = BlackPixelOfScreen (screen);
1559 else if (XParseColor (dpy, cmap, color_name, &color)
1560 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1562 pixel = color.pixel;
1563 *closure_ret = (XtPointer) True;
1565 else
1567 String params[1];
1568 Cardinal nparams = 1;
1570 params[0] = color_name;
1571 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1572 "badValue", "cvt_string_to_pixel",
1573 "XtToolkitError", "Invalid color `%s'",
1574 params, &nparams);
1575 return False;
1578 if (to->addr != NULL)
1580 if (to->size < sizeof (Pixel))
1582 to->size = sizeof (Pixel);
1583 return False;
1586 *(Pixel *) to->addr = pixel;
1588 else
1590 cvt_string_to_pixel_value = pixel;
1591 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1594 to->size = sizeof (Pixel);
1595 return True;
1599 /* Free a pixel color which was previously allocated via
1600 cvt_string_to_pixel. This is registered as the destructor
1601 for this type of resource via XtSetTypeConverter.
1603 APP is the application context in which we work.
1605 TO is a pointer to an XrmValue holding the color to free.
1606 CLOSURE is the value we stored in CLOSURE_RET for this color
1607 in cvt_string_to_pixel.
1609 ARGS and NARGS are like for cvt_string_to_pixel. */
1611 static void
1612 cvt_pixel_dtor (app, to, closure, args, nargs)
1613 XtAppContext app;
1614 XrmValuePtr to;
1615 XtPointer closure;
1616 XrmValuePtr args;
1617 Cardinal *nargs;
1619 if (*nargs != 2)
1621 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1622 "XtToolkitError",
1623 "Screen and colormap arguments required",
1624 NULL, NULL);
1626 else if (closure != NULL)
1628 /* We did allocate the pixel, so free it. */
1629 Screen *screen = *(Screen **) args[0].addr;
1630 Colormap cmap = *(Colormap *) args[1].addr;
1631 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1632 (Pixel *) to->addr, 1);
1637 #endif /* USE_X_TOOLKIT */
1640 /* Value is an array of XColor structures for the contents of the
1641 color map of display DPY. Set *NCELLS to the size of the array.
1642 Note that this probably shouldn't be called for large color maps,
1643 say a 24-bit TrueColor map. */
1645 static const XColor *
1646 x_color_cells (dpy, ncells)
1647 Display *dpy;
1648 int *ncells;
1650 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1652 if (dpyinfo->color_cells == NULL)
1654 Screen *screen = dpyinfo->screen;
1655 int i;
1657 dpyinfo->ncolor_cells
1658 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1659 dpyinfo->color_cells
1660 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1661 * sizeof *dpyinfo->color_cells);
1663 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1664 dpyinfo->color_cells[i].pixel = i;
1666 XQueryColors (dpy, dpyinfo->cmap,
1667 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1670 *ncells = dpyinfo->ncolor_cells;
1671 return dpyinfo->color_cells;
1675 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1676 colors in COLORS. Use cached information, if available. */
1678 void
1679 x_query_colors (f, colors, ncolors)
1680 struct frame *f;
1681 XColor *colors;
1682 int ncolors;
1684 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1686 if (dpyinfo->color_cells)
1688 int i;
1689 for (i = 0; i < ncolors; ++i)
1691 unsigned long pixel = colors[i].pixel;
1692 xassert (pixel < dpyinfo->ncolor_cells);
1693 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1694 colors[i] = dpyinfo->color_cells[pixel];
1697 else
1698 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1702 /* On frame F, translate pixel color to RGB values for the color in
1703 COLOR. Use cached information, if available. */
1705 void
1706 x_query_color (f, color)
1707 struct frame *f;
1708 XColor *color;
1710 x_query_colors (f, color, 1);
1714 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1715 exact match can't be allocated, try the nearest color available.
1716 Value is non-zero if successful. Set *COLOR to the color
1717 allocated. */
1719 static int
1720 x_alloc_nearest_color_1 (dpy, cmap, color)
1721 Display *dpy;
1722 Colormap cmap;
1723 XColor *color;
1725 int rc;
1727 rc = XAllocColor (dpy, cmap, color);
1728 if (rc == 0)
1730 /* If we got to this point, the colormap is full, so we're going
1731 to try to get the next closest color. The algorithm used is
1732 a least-squares matching, which is what X uses for closest
1733 color matching with StaticColor visuals. */
1734 int nearest, i;
1735 unsigned long nearest_delta = ~0;
1736 int ncells;
1737 const XColor *cells = x_color_cells (dpy, &ncells);
1739 for (nearest = i = 0; i < ncells; ++i)
1741 long dred = (color->red >> 8) - (cells[i].red >> 8);
1742 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1743 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1744 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1746 if (delta < nearest_delta)
1748 nearest = i;
1749 nearest_delta = delta;
1753 color->red = cells[nearest].red;
1754 color->green = cells[nearest].green;
1755 color->blue = cells[nearest].blue;
1756 rc = XAllocColor (dpy, cmap, color);
1758 else
1760 /* If allocation succeeded, and the allocated pixel color is not
1761 equal to a cached pixel color recorded earlier, there was a
1762 change in the colormap, so clear the color cache. */
1763 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1764 XColor *cached_color;
1766 if (dpyinfo->color_cells
1767 && (cached_color = &dpyinfo->color_cells[color->pixel],
1768 (cached_color->red != color->red
1769 || cached_color->blue != color->blue
1770 || cached_color->green != color->green)))
1772 xfree (dpyinfo->color_cells);
1773 dpyinfo->color_cells = NULL;
1774 dpyinfo->ncolor_cells = 0;
1778 #ifdef DEBUG_X_COLORS
1779 if (rc)
1780 register_color (color->pixel);
1781 #endif /* DEBUG_X_COLORS */
1783 return rc;
1787 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1788 exact match can't be allocated, try the nearest color available.
1789 Value is non-zero if successful. Set *COLOR to the color
1790 allocated. */
1793 x_alloc_nearest_color (f, cmap, color)
1794 struct frame *f;
1795 Colormap cmap;
1796 XColor *color;
1798 gamma_correct (f, color);
1799 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1803 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1804 It's necessary to do this instead of just using PIXEL directly to
1805 get color reference counts right. */
1807 unsigned long
1808 x_copy_color (f, pixel)
1809 struct frame *f;
1810 unsigned long pixel;
1812 XColor color;
1814 color.pixel = pixel;
1815 BLOCK_INPUT;
1816 x_query_color (f, &color);
1817 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1818 UNBLOCK_INPUT;
1819 #ifdef DEBUG_X_COLORS
1820 register_color (pixel);
1821 #endif
1822 return color.pixel;
1826 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1827 It's necessary to do this instead of just using PIXEL directly to
1828 get color reference counts right. */
1830 unsigned long
1831 x_copy_dpy_color (dpy, cmap, pixel)
1832 Display *dpy;
1833 Colormap cmap;
1834 unsigned long pixel;
1836 XColor color;
1838 color.pixel = pixel;
1839 BLOCK_INPUT;
1840 XQueryColor (dpy, cmap, &color);
1841 XAllocColor (dpy, cmap, &color);
1842 UNBLOCK_INPUT;
1843 #ifdef DEBUG_X_COLORS
1844 register_color (pixel);
1845 #endif
1846 return color.pixel;
1850 /* Brightness beyond which a color won't have its highlight brightness
1851 boosted.
1853 Nominally, highlight colors for `3d' faces are calculated by
1854 brightening an object's color by a constant scale factor, but this
1855 doesn't yield good results for dark colors, so for colors who's
1856 brightness is less than this value (on a scale of 0-65535) have an
1857 use an additional additive factor.
1859 The value here is set so that the default menu-bar/mode-line color
1860 (grey75) will not have its highlights changed at all. */
1861 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1864 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1865 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1866 If this produces the same color as PIXEL, try a color where all RGB
1867 values have DELTA added. Return the allocated color in *PIXEL.
1868 DISPLAY is the X display, CMAP is the colormap to operate on.
1869 Value is non-zero if successful. */
1871 static int
1872 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1873 struct frame *f;
1874 Display *display;
1875 Colormap cmap;
1876 unsigned long *pixel;
1877 double factor;
1878 int delta;
1880 XColor color, new;
1881 long bright;
1882 int success_p;
1884 /* Get RGB color values. */
1885 color.pixel = *pixel;
1886 x_query_color (f, &color);
1888 /* Change RGB values by specified FACTOR. Avoid overflow! */
1889 xassert (factor >= 0);
1890 new.red = min (0xffff, factor * color.red);
1891 new.green = min (0xffff, factor * color.green);
1892 new.blue = min (0xffff, factor * color.blue);
1894 /* Calculate brightness of COLOR. */
1895 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1897 /* We only boost colors that are darker than
1898 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1899 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1900 /* Make an additive adjustment to NEW, because it's dark enough so
1901 that scaling by FACTOR alone isn't enough. */
1903 /* How far below the limit this color is (0 - 1, 1 being darker). */
1904 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1905 /* The additive adjustment. */
1906 int min_delta = delta * dimness * factor / 2;
1908 if (factor < 1)
1910 new.red = max (0, new.red - min_delta);
1911 new.green = max (0, new.green - min_delta);
1912 new.blue = max (0, new.blue - min_delta);
1914 else
1916 new.red = min (0xffff, min_delta + new.red);
1917 new.green = min (0xffff, min_delta + new.green);
1918 new.blue = min (0xffff, min_delta + new.blue);
1922 /* Try to allocate the color. */
1923 success_p = x_alloc_nearest_color (f, cmap, &new);
1924 if (success_p)
1926 if (new.pixel == *pixel)
1928 /* If we end up with the same color as before, try adding
1929 delta to the RGB values. */
1930 x_free_colors (f, &new.pixel, 1);
1932 new.red = min (0xffff, delta + color.red);
1933 new.green = min (0xffff, delta + color.green);
1934 new.blue = min (0xffff, delta + color.blue);
1935 success_p = x_alloc_nearest_color (f, cmap, &new);
1937 else
1938 success_p = 1;
1939 *pixel = new.pixel;
1942 return success_p;
1946 /* Set up the foreground color for drawing relief lines of glyph
1947 string S. RELIEF is a pointer to a struct relief containing the GC
1948 with which lines will be drawn. Use a color that is FACTOR or
1949 DELTA lighter or darker than the relief's background which is found
1950 in S->f->output_data.x->relief_background. If such a color cannot
1951 be allocated, use DEFAULT_PIXEL, instead. */
1953 static void
1954 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1955 struct frame *f;
1956 struct relief *relief;
1957 double factor;
1958 int delta;
1959 unsigned long default_pixel;
1961 XGCValues xgcv;
1962 struct x_output *di = f->output_data.x;
1963 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1964 unsigned long pixel;
1965 unsigned long background = di->relief_background;
1966 Colormap cmap = FRAME_X_COLORMAP (f);
1967 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1968 Display *dpy = FRAME_X_DISPLAY (f);
1970 xgcv.graphics_exposures = False;
1971 xgcv.line_width = 1;
1973 /* Free previously allocated color. The color cell will be reused
1974 when it has been freed as many times as it was allocated, so this
1975 doesn't affect faces using the same colors. */
1976 if (relief->gc
1977 && relief->allocated_p)
1979 x_free_colors (f, &relief->pixel, 1);
1980 relief->allocated_p = 0;
1983 /* Allocate new color. */
1984 xgcv.foreground = default_pixel;
1985 pixel = background;
1986 if (dpyinfo->n_planes != 1
1987 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1989 relief->allocated_p = 1;
1990 xgcv.foreground = relief->pixel = pixel;
1993 if (relief->gc == 0)
1995 xgcv.stipple = dpyinfo->gray;
1996 mask |= GCStipple;
1997 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1999 else
2000 XChangeGC (dpy, relief->gc, mask, &xgcv);
2004 /* Set up colors for the relief lines around glyph string S. */
2006 static void
2007 x_setup_relief_colors (s)
2008 struct glyph_string *s;
2010 struct x_output *di = s->f->output_data.x;
2011 unsigned long color;
2013 if (s->face->use_box_color_for_shadows_p)
2014 color = s->face->box_color;
2015 else if (s->first_glyph->type == IMAGE_GLYPH
2016 && s->img->pixmap
2017 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2018 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2019 else
2021 XGCValues xgcv;
2023 /* Get the background color of the face. */
2024 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2025 color = xgcv.background;
2028 if (di->white_relief.gc == 0
2029 || color != di->relief_background)
2031 di->relief_background = color;
2032 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2033 WHITE_PIX_DEFAULT (s->f));
2034 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2035 BLACK_PIX_DEFAULT (s->f));
2040 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2041 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2042 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2043 relief. LEFT_P non-zero means draw a relief on the left side of
2044 the rectangle. RIGHT_P non-zero means draw a relief on the right
2045 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2046 when drawing. */
2048 static void
2049 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2050 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2051 struct frame *f;
2052 int left_x, top_y, right_x, bottom_y, width;
2053 int top_p, bot_p, left_p, right_p, raised_p;
2054 XRectangle *clip_rect;
2056 Display *dpy = FRAME_X_DISPLAY (f);
2057 Window window = FRAME_X_WINDOW (f);
2058 int i;
2059 GC gc;
2061 if (raised_p)
2062 gc = f->output_data.x->white_relief.gc;
2063 else
2064 gc = f->output_data.x->black_relief.gc;
2065 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2067 /* Top. */
2068 if (top_p)
2069 for (i = 0; i < width; ++i)
2070 XDrawLine (dpy, window, gc,
2071 left_x + i * left_p, top_y + i,
2072 right_x + 1 - i * right_p, top_y + i);
2074 /* Left. */
2075 if (left_p)
2076 for (i = 0; i < width; ++i)
2077 XDrawLine (dpy, window, gc,
2078 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2080 XSetClipMask (dpy, gc, None);
2081 if (raised_p)
2082 gc = f->output_data.x->black_relief.gc;
2083 else
2084 gc = f->output_data.x->white_relief.gc;
2085 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2087 /* Bottom. */
2088 if (bot_p)
2089 for (i = 0; i < width; ++i)
2090 XDrawLine (dpy, window, gc,
2091 left_x + i * left_p, bottom_y - i,
2092 right_x + 1 - i * right_p, bottom_y - i);
2094 /* Right. */
2095 if (right_p)
2096 for (i = 0; i < width; ++i)
2097 XDrawLine (dpy, window, gc,
2098 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2100 XSetClipMask (dpy, gc, None);
2104 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2105 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2106 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2107 left side of the rectangle. RIGHT_P non-zero means draw a line
2108 on the right side of the rectangle. CLIP_RECT is the clipping
2109 rectangle to use when drawing. */
2111 static void
2112 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2113 left_p, right_p, clip_rect)
2114 struct glyph_string *s;
2115 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2116 XRectangle *clip_rect;
2118 XGCValues xgcv;
2120 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2121 XSetForeground (s->display, s->gc, s->face->box_color);
2122 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2124 /* Top. */
2125 XFillRectangle (s->display, s->window, s->gc,
2126 left_x, top_y, right_x - left_x + 1, width);
2128 /* Left. */
2129 if (left_p)
2130 XFillRectangle (s->display, s->window, s->gc,
2131 left_x, top_y, width, bottom_y - top_y + 1);
2133 /* Bottom. */
2134 XFillRectangle (s->display, s->window, s->gc,
2135 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2137 /* Right. */
2138 if (right_p)
2139 XFillRectangle (s->display, s->window, s->gc,
2140 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2142 XSetForeground (s->display, s->gc, xgcv.foreground);
2143 XSetClipMask (s->display, s->gc, None);
2147 /* Draw a box around glyph string S. */
2149 static void
2150 x_draw_glyph_string_box (s)
2151 struct glyph_string *s;
2153 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2154 int left_p, right_p;
2155 struct glyph *last_glyph;
2156 XRectangle clip_rect;
2158 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2159 ? WINDOW_RIGHT_EDGE_X (s->w)
2160 : window_box_right (s->w, s->area));
2162 /* The glyph that may have a right box line. */
2163 last_glyph = (s->cmp || s->img
2164 ? s->first_glyph
2165 : s->first_glyph + s->nchars - 1);
2167 width = eabs (s->face->box_line_width);
2168 raised_p = s->face->box == FACE_RAISED_BOX;
2169 left_x = s->x;
2170 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2171 ? last_x - 1
2172 : min (last_x, s->x + s->background_width) - 1);
2173 top_y = s->y;
2174 bottom_y = top_y + s->height - 1;
2176 left_p = (s->first_glyph->left_box_line_p
2177 || (s->hl == DRAW_MOUSE_FACE
2178 && (s->prev == NULL
2179 || s->prev->hl != s->hl)));
2180 right_p = (last_glyph->right_box_line_p
2181 || (s->hl == DRAW_MOUSE_FACE
2182 && (s->next == NULL
2183 || s->next->hl != s->hl)));
2185 get_glyph_string_clip_rect (s, &clip_rect);
2187 if (s->face->box == FACE_SIMPLE_BOX)
2188 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2189 left_p, right_p, &clip_rect);
2190 else
2192 x_setup_relief_colors (s);
2193 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2194 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2199 /* Draw foreground of image glyph string S. */
2201 static void
2202 x_draw_image_foreground (s)
2203 struct glyph_string *s;
2205 int x = s->x;
2206 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2208 /* If first glyph of S has a left box line, start drawing it to the
2209 right of that line. */
2210 if (s->face->box != FACE_NO_BOX
2211 && s->first_glyph->left_box_line_p
2212 && s->slice.x == 0)
2213 x += eabs (s->face->box_line_width);
2215 /* If there is a margin around the image, adjust x- and y-position
2216 by that margin. */
2217 if (s->slice.x == 0)
2218 x += s->img->hmargin;
2219 if (s->slice.y == 0)
2220 y += s->img->vmargin;
2222 if (s->img->pixmap)
2224 if (s->img->mask)
2226 /* We can't set both a clip mask and use XSetClipRectangles
2227 because the latter also sets a clip mask. We also can't
2228 trust on the shape extension to be available
2229 (XShapeCombineRegion). So, compute the rectangle to draw
2230 manually. */
2231 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2232 | GCFunction);
2233 XGCValues xgcv;
2234 XRectangle clip_rect, image_rect, r;
2236 xgcv.clip_mask = s->img->mask;
2237 xgcv.clip_x_origin = x;
2238 xgcv.clip_y_origin = y;
2239 xgcv.function = GXcopy;
2240 XChangeGC (s->display, s->gc, mask, &xgcv);
2242 get_glyph_string_clip_rect (s, &clip_rect);
2243 image_rect.x = x;
2244 image_rect.y = y;
2245 image_rect.width = s->slice.width;
2246 image_rect.height = s->slice.height;
2247 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2248 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2249 s->slice.x + r.x - x, s->slice.y + r.y - y,
2250 r.width, r.height, r.x, r.y);
2252 else
2254 XRectangle clip_rect, image_rect, r;
2256 get_glyph_string_clip_rect (s, &clip_rect);
2257 image_rect.x = x;
2258 image_rect.y = y;
2259 image_rect.width = s->slice.width;
2260 image_rect.height = s->slice.height;
2261 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2262 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2263 s->slice.x + r.x - x, s->slice.y + r.y - y,
2264 r.width, r.height, r.x, r.y);
2266 /* When the image has a mask, we can expect that at
2267 least part of a mouse highlight or a block cursor will
2268 be visible. If the image doesn't have a mask, make
2269 a block cursor visible by drawing a rectangle around
2270 the image. I believe it's looking better if we do
2271 nothing here for mouse-face. */
2272 if (s->hl == DRAW_CURSOR)
2274 int r = s->img->relief;
2275 if (r < 0) r = -r;
2276 XDrawRectangle (s->display, s->window, s->gc,
2277 x - r, y - r,
2278 s->slice.width + r*2 - 1,
2279 s->slice.height + r*2 - 1);
2283 else
2284 /* Draw a rectangle if image could not be loaded. */
2285 XDrawRectangle (s->display, s->window, s->gc, x, y,
2286 s->slice.width - 1, s->slice.height - 1);
2290 /* Draw a relief around the image glyph string S. */
2292 static void
2293 x_draw_image_relief (s)
2294 struct glyph_string *s;
2296 int x0, y0, x1, y1, thick, raised_p;
2297 XRectangle r;
2298 int x = s->x;
2299 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2301 /* If first glyph of S has a left box line, start drawing it to the
2302 right of that line. */
2303 if (s->face->box != FACE_NO_BOX
2304 && s->first_glyph->left_box_line_p
2305 && s->slice.x == 0)
2306 x += eabs (s->face->box_line_width);
2308 /* If there is a margin around the image, adjust x- and y-position
2309 by that margin. */
2310 if (s->slice.x == 0)
2311 x += s->img->hmargin;
2312 if (s->slice.y == 0)
2313 y += s->img->vmargin;
2315 if (s->hl == DRAW_IMAGE_SUNKEN
2316 || s->hl == DRAW_IMAGE_RAISED)
2318 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2319 raised_p = s->hl == DRAW_IMAGE_RAISED;
2321 else
2323 thick = eabs (s->img->relief);
2324 raised_p = s->img->relief > 0;
2327 x0 = x - thick;
2328 y0 = y - thick;
2329 x1 = x + s->slice.width + thick - 1;
2330 y1 = y + s->slice.height + thick - 1;
2332 x_setup_relief_colors (s);
2333 get_glyph_string_clip_rect (s, &r);
2334 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2335 s->slice.y == 0,
2336 s->slice.y + s->slice.height == s->img->height,
2337 s->slice.x == 0,
2338 s->slice.x + s->slice.width == s->img->width,
2339 &r);
2343 /* Draw the foreground of image glyph string S to PIXMAP. */
2345 static void
2346 x_draw_image_foreground_1 (s, pixmap)
2347 struct glyph_string *s;
2348 Pixmap pixmap;
2350 int x = 0;
2351 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2353 /* If first glyph of S has a left box line, start drawing it to the
2354 right of that line. */
2355 if (s->face->box != FACE_NO_BOX
2356 && s->first_glyph->left_box_line_p
2357 && s->slice.x == 0)
2358 x += eabs (s->face->box_line_width);
2360 /* If there is a margin around the image, adjust x- and y-position
2361 by that margin. */
2362 if (s->slice.x == 0)
2363 x += s->img->hmargin;
2364 if (s->slice.y == 0)
2365 y += s->img->vmargin;
2367 if (s->img->pixmap)
2369 if (s->img->mask)
2371 /* We can't set both a clip mask and use XSetClipRectangles
2372 because the latter also sets a clip mask. We also can't
2373 trust on the shape extension to be available
2374 (XShapeCombineRegion). So, compute the rectangle to draw
2375 manually. */
2376 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2377 | GCFunction);
2378 XGCValues xgcv;
2380 xgcv.clip_mask = s->img->mask;
2381 xgcv.clip_x_origin = x - s->slice.x;
2382 xgcv.clip_y_origin = y - s->slice.y;
2383 xgcv.function = GXcopy;
2384 XChangeGC (s->display, s->gc, mask, &xgcv);
2386 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2387 s->slice.x, s->slice.y,
2388 s->slice.width, s->slice.height, x, y);
2389 XSetClipMask (s->display, s->gc, None);
2391 else
2393 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2394 s->slice.x, s->slice.y,
2395 s->slice.width, s->slice.height, x, y);
2397 /* When the image has a mask, we can expect that at
2398 least part of a mouse highlight or a block cursor will
2399 be visible. If the image doesn't have a mask, make
2400 a block cursor visible by drawing a rectangle around
2401 the image. I believe it's looking better if we do
2402 nothing here for mouse-face. */
2403 if (s->hl == DRAW_CURSOR)
2405 int r = s->img->relief;
2406 if (r < 0) r = -r;
2407 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2408 s->slice.width + r*2 - 1,
2409 s->slice.height + r*2 - 1);
2413 else
2414 /* Draw a rectangle if image could not be loaded. */
2415 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2416 s->slice.width - 1, s->slice.height - 1);
2420 /* Draw part of the background of glyph string S. X, Y, W, and H
2421 give the rectangle to draw. */
2423 static void
2424 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2425 struct glyph_string *s;
2426 int x, y, w, h;
2428 if (s->stippled_p)
2430 /* Fill background with a stipple pattern. */
2431 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2432 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2433 XSetFillStyle (s->display, s->gc, FillSolid);
2435 else
2436 x_clear_glyph_string_rect (s, x, y, w, h);
2440 /* Draw image glyph string S.
2442 s->y
2443 s->x +-------------------------
2444 | s->face->box
2446 | +-------------------------
2447 | | s->img->margin
2449 | | +-------------------
2450 | | | the image
2454 static void
2455 x_draw_image_glyph_string (s)
2456 struct glyph_string *s;
2458 int box_line_hwidth = eabs (s->face->box_line_width);
2459 int box_line_vwidth = max (s->face->box_line_width, 0);
2460 int height;
2461 Pixmap pixmap = None;
2463 height = s->height;
2464 if (s->slice.y == 0)
2465 height -= box_line_vwidth;
2466 if (s->slice.y + s->slice.height >= s->img->height)
2467 height -= box_line_vwidth;
2469 /* Fill background with face under the image. Do it only if row is
2470 taller than image or if image has a clip mask to reduce
2471 flickering. */
2472 s->stippled_p = s->face->stipple != 0;
2473 if (height > s->slice.height
2474 || s->img->hmargin
2475 || s->img->vmargin
2476 || s->img->mask
2477 || s->img->pixmap == 0
2478 || s->width != s->background_width)
2480 if (s->img->mask)
2482 /* Create a pixmap as large as the glyph string. Fill it
2483 with the background color. Copy the image to it, using
2484 its mask. Copy the temporary pixmap to the display. */
2485 Screen *screen = FRAME_X_SCREEN (s->f);
2486 int depth = DefaultDepthOfScreen (screen);
2488 /* Create a pixmap as large as the glyph string. */
2489 pixmap = XCreatePixmap (s->display, s->window,
2490 s->background_width,
2491 s->height, depth);
2493 /* Don't clip in the following because we're working on the
2494 pixmap. */
2495 XSetClipMask (s->display, s->gc, None);
2497 /* Fill the pixmap with the background color/stipple. */
2498 if (s->stippled_p)
2500 /* Fill background with a stipple pattern. */
2501 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2502 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2503 XFillRectangle (s->display, pixmap, s->gc,
2504 0, 0, s->background_width, s->height);
2505 XSetFillStyle (s->display, s->gc, FillSolid);
2506 XSetTSOrigin (s->display, s->gc, 0, 0);
2508 else
2510 XGCValues xgcv;
2511 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2512 &xgcv);
2513 XSetForeground (s->display, s->gc, xgcv.background);
2514 XFillRectangle (s->display, pixmap, s->gc,
2515 0, 0, s->background_width, s->height);
2516 XSetForeground (s->display, s->gc, xgcv.foreground);
2519 else
2521 int x = s->x;
2522 int y = s->y;
2524 if (s->first_glyph->left_box_line_p
2525 && s->slice.x == 0)
2526 x += box_line_hwidth;
2528 if (s->slice.y == 0)
2529 y += box_line_vwidth;
2531 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2534 s->background_filled_p = 1;
2537 /* Draw the foreground. */
2538 if (pixmap != None)
2540 x_draw_image_foreground_1 (s, pixmap);
2541 x_set_glyph_string_clipping (s);
2542 XCopyArea (s->display, pixmap, s->window, s->gc,
2543 0, 0, s->background_width, s->height, s->x, s->y);
2544 XFreePixmap (s->display, pixmap);
2546 else
2547 x_draw_image_foreground (s);
2549 /* If we must draw a relief around the image, do it. */
2550 if (s->img->relief
2551 || s->hl == DRAW_IMAGE_RAISED
2552 || s->hl == DRAW_IMAGE_SUNKEN)
2553 x_draw_image_relief (s);
2557 /* Draw stretch glyph string S. */
2559 static void
2560 x_draw_stretch_glyph_string (s)
2561 struct glyph_string *s;
2563 xassert (s->first_glyph->type == STRETCH_GLYPH);
2565 if (s->hl == DRAW_CURSOR
2566 && !x_stretch_cursor_p)
2568 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2569 as wide as the stretch glyph. */
2570 int width, background_width = s->background_width;
2571 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2573 if (x < left_x)
2575 background_width -= left_x - x;
2576 x = left_x;
2578 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2580 /* Draw cursor. */
2581 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2583 /* Clear rest using the GC of the original non-cursor face. */
2584 if (width < background_width)
2586 int y = s->y;
2587 int w = background_width - width, h = s->height;
2588 XRectangle r;
2589 GC gc;
2591 x += width;
2592 if (s->row->mouse_face_p
2593 && cursor_in_mouse_face_p (s->w))
2595 x_set_mouse_face_gc (s);
2596 gc = s->gc;
2598 else
2599 gc = s->face->gc;
2601 get_glyph_string_clip_rect (s, &r);
2602 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2604 if (s->face->stipple)
2606 /* Fill background with a stipple pattern. */
2607 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2608 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2609 XSetFillStyle (s->display, gc, FillSolid);
2611 else
2613 XGCValues xgcv;
2614 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2615 XSetForeground (s->display, gc, xgcv.background);
2616 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2617 XSetForeground (s->display, gc, xgcv.foreground);
2621 else if (!s->background_filled_p)
2623 int background_width = s->background_width;
2624 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2626 /* Don't draw into left margin, fringe or scrollbar area
2627 except for header line and mode line. */
2628 if (x < left_x && !s->row->mode_line_p)
2630 background_width -= left_x - x;
2631 x = left_x;
2633 if (background_width > 0)
2634 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2637 s->background_filled_p = 1;
2641 /* Draw glyph string S. */
2643 static void
2644 x_draw_glyph_string (s)
2645 struct glyph_string *s;
2647 int relief_drawn_p = 0;
2649 /* If S draws into the background of its successors, draw the
2650 background of the successors first so that S can draw into it.
2651 This makes S->next use XDrawString instead of XDrawImageString. */
2652 if (s->next && s->right_overhang && !s->for_overlaps)
2654 int width;
2655 struct glyph_string *next;
2657 for (width = 0, next = s->next;
2658 next && width < s->right_overhang;
2659 width += next->width, next = next->next)
2660 if (next->first_glyph->type != IMAGE_GLYPH)
2662 x_set_glyph_string_gc (next);
2663 x_set_glyph_string_clipping (next);
2664 if (next->first_glyph->type == STRETCH_GLYPH)
2665 x_draw_stretch_glyph_string (next);
2666 else
2667 x_draw_glyph_string_background (next, 1);
2668 next->num_clips = 0;
2672 /* Set up S->gc, set clipping and draw S. */
2673 x_set_glyph_string_gc (s);
2675 /* Draw relief (if any) in advance for char/composition so that the
2676 glyph string can be drawn over it. */
2677 if (!s->for_overlaps
2678 && s->face->box != FACE_NO_BOX
2679 && (s->first_glyph->type == CHAR_GLYPH
2680 || s->first_glyph->type == COMPOSITE_GLYPH))
2683 x_set_glyph_string_clipping (s);
2684 x_draw_glyph_string_background (s, 1);
2685 x_draw_glyph_string_box (s);
2686 x_set_glyph_string_clipping (s);
2687 relief_drawn_p = 1;
2689 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2690 && !s->clip_tail
2691 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2692 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2693 /* We must clip just this glyph. left_overhang part has already
2694 drawn when s->prev was drawn, and right_overhang part will be
2695 drawn later when s->next is drawn. */
2696 x_set_glyph_string_clipping_exactly (s, s);
2697 else
2698 x_set_glyph_string_clipping (s);
2700 switch (s->first_glyph->type)
2702 case IMAGE_GLYPH:
2703 x_draw_image_glyph_string (s);
2704 break;
2706 case STRETCH_GLYPH:
2707 x_draw_stretch_glyph_string (s);
2708 break;
2710 case CHAR_GLYPH:
2711 if (s->for_overlaps)
2712 s->background_filled_p = 1;
2713 else
2714 x_draw_glyph_string_background (s, 0);
2715 x_draw_glyph_string_foreground (s);
2716 break;
2718 case COMPOSITE_GLYPH:
2719 if (s->for_overlaps || (s->cmp_from > 0
2720 && ! s->first_glyph->u.cmp.automatic))
2721 s->background_filled_p = 1;
2722 else
2723 x_draw_glyph_string_background (s, 1);
2724 x_draw_composite_glyph_string_foreground (s);
2725 break;
2727 default:
2728 abort ();
2731 if (!s->for_overlaps)
2733 /* Draw underline. */
2734 if (s->face->underline_p)
2736 unsigned long thickness, position;
2737 int y;
2739 if (s->prev && s->prev->face->underline_p)
2741 /* We use the same underline style as the previous one. */
2742 thickness = s->prev->underline_thickness;
2743 position = s->prev->underline_position;
2745 else
2747 /* Get the underline thickness. Default is 1 pixel. */
2748 if (s->font && s->font->underline_thickness > 0)
2749 thickness = s->font->underline_thickness;
2750 else
2751 thickness = 1;
2752 if (x_underline_at_descent_line)
2753 position = (s->height - thickness) - (s->ybase - s->y);
2754 else
2756 /* Get the underline position. This is the recommended
2757 vertical offset in pixels from the baseline to the top of
2758 the underline. This is a signed value according to the
2759 specs, and its default is
2761 ROUND ((maximum descent) / 2), with
2762 ROUND(x) = floor (x + 0.5) */
2764 if (x_use_underline_position_properties
2765 && s->font && s->font->underline_position >= 0)
2766 position = s->font->underline_position;
2767 else if (s->font)
2768 position = (s->font->descent + 1) / 2;
2769 else
2770 position = underline_minimum_offset;
2772 position = max (position, underline_minimum_offset);
2774 /* Check the sanity of thickness and position. We should
2775 avoid drawing underline out of the current line area. */
2776 if (s->y + s->height <= s->ybase + position)
2777 position = (s->height - 1) - (s->ybase - s->y);
2778 if (s->y + s->height < s->ybase + position + thickness)
2779 thickness = (s->y + s->height) - (s->ybase + position);
2780 s->underline_thickness = thickness;
2781 s->underline_position = position;
2782 y = s->ybase + position;
2783 if (s->face->underline_defaulted_p)
2784 XFillRectangle (s->display, s->window, s->gc,
2785 s->x, y, s->width, thickness);
2786 else
2788 XGCValues xgcv;
2789 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2790 XSetForeground (s->display, s->gc, s->face->underline_color);
2791 XFillRectangle (s->display, s->window, s->gc,
2792 s->x, y, s->width, thickness);
2793 XSetForeground (s->display, s->gc, xgcv.foreground);
2797 /* Draw overline. */
2798 if (s->face->overline_p)
2800 unsigned long dy = 0, h = 1;
2802 if (s->face->overline_color_defaulted_p)
2803 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2804 s->width, h);
2805 else
2807 XGCValues xgcv;
2808 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2809 XSetForeground (s->display, s->gc, s->face->overline_color);
2810 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2811 s->width, h);
2812 XSetForeground (s->display, s->gc, xgcv.foreground);
2816 /* Draw strike-through. */
2817 if (s->face->strike_through_p)
2819 unsigned long h = 1;
2820 unsigned long dy = (s->height - h) / 2;
2822 if (s->face->strike_through_color_defaulted_p)
2823 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2824 s->width, h);
2825 else
2827 XGCValues xgcv;
2828 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2829 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2830 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2831 s->width, h);
2832 XSetForeground (s->display, s->gc, xgcv.foreground);
2836 /* Draw relief if not yet drawn. */
2837 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2838 x_draw_glyph_string_box (s);
2840 if (s->prev)
2842 struct glyph_string *prev;
2844 for (prev = s->prev; prev; prev = prev->prev)
2845 if (prev->hl != s->hl
2846 && prev->x + prev->width + prev->right_overhang > s->x)
2848 /* As prev was drawn while clipped to its own area, we
2849 must draw the right_overhang part using s->hl now. */
2850 enum draw_glyphs_face save = prev->hl;
2852 prev->hl = s->hl;
2853 x_set_glyph_string_gc (prev);
2854 x_set_glyph_string_clipping_exactly (s, prev);
2855 if (prev->first_glyph->type == CHAR_GLYPH)
2856 x_draw_glyph_string_foreground (prev);
2857 else
2858 x_draw_composite_glyph_string_foreground (prev);
2859 XSetClipMask (prev->display, prev->gc, None);
2860 prev->hl = save;
2861 prev->num_clips = 0;
2865 if (s->next)
2867 struct glyph_string *next;
2869 for (next = s->next; next; next = next->next)
2870 if (next->hl != s->hl
2871 && next->x - next->left_overhang < s->x + s->width)
2873 /* As next will be drawn while clipped to its own area,
2874 we must draw the left_overhang part using s->hl now. */
2875 enum draw_glyphs_face save = next->hl;
2877 next->hl = s->hl;
2878 x_set_glyph_string_gc (next);
2879 x_set_glyph_string_clipping_exactly (s, next);
2880 if (next->first_glyph->type == CHAR_GLYPH)
2881 x_draw_glyph_string_foreground (next);
2882 else
2883 x_draw_composite_glyph_string_foreground (next);
2884 XSetClipMask (next->display, next->gc, None);
2885 next->hl = save;
2886 next->num_clips = 0;
2891 /* Reset clipping. */
2892 XSetClipMask (s->display, s->gc, None);
2893 s->num_clips = 0;
2896 /* Shift display to make room for inserted glyphs. */
2898 void
2899 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2900 struct frame *f;
2901 int x, y, width, height, shift_by;
2903 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2904 f->output_data.x->normal_gc,
2905 x, y, width, height,
2906 x + shift_by, y);
2909 /* Delete N glyphs at the nominal cursor position. Not implemented
2910 for X frames. */
2912 static void
2913 x_delete_glyphs (f, n)
2914 struct frame *f;
2915 register int n;
2917 abort ();
2921 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2922 If they are <= 0, this is probably an error. */
2924 void
2925 x_clear_area (dpy, window, x, y, width, height, exposures)
2926 Display *dpy;
2927 Window window;
2928 int x, y;
2929 int width, height;
2930 int exposures;
2932 xassert (width > 0 && height > 0);
2933 XClearArea (dpy, window, x, y, width, height, exposures);
2937 /* Clear an entire frame. */
2939 static void
2940 x_clear_frame (struct frame *f)
2942 /* Clearing the frame will erase any cursor, so mark them all as no
2943 longer visible. */
2944 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2945 output_cursor.hpos = output_cursor.vpos = 0;
2946 output_cursor.x = -1;
2948 /* We don't set the output cursor here because there will always
2949 follow an explicit cursor_to. */
2950 BLOCK_INPUT;
2951 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2953 /* We have to clear the scroll bars, too. If we have changed
2954 colors or something like that, then they should be notified. */
2955 x_scroll_bar_clear (f);
2957 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2958 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2959 redisplay, do it here. */
2960 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2961 #endif
2963 XFlush (FRAME_X_DISPLAY (f));
2965 UNBLOCK_INPUT;
2970 /* Invert the middle quarter of the frame for .15 sec. */
2972 /* We use the select system call to do the waiting, so we have to make
2973 sure it's available. If it isn't, we just won't do visual bells. */
2975 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2978 /* Subtract the `struct timeval' values X and Y, storing the result in
2979 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2981 static int
2982 timeval_subtract (result, x, y)
2983 struct timeval *result, x, y;
2985 /* Perform the carry for the later subtraction by updating y. This
2986 is safer because on some systems the tv_sec member is unsigned. */
2987 if (x.tv_usec < y.tv_usec)
2989 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2990 y.tv_usec -= 1000000 * nsec;
2991 y.tv_sec += nsec;
2994 if (x.tv_usec - y.tv_usec > 1000000)
2996 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2997 y.tv_usec += 1000000 * nsec;
2998 y.tv_sec -= nsec;
3001 /* Compute the time remaining to wait. tv_usec is certainly
3002 positive. */
3003 result->tv_sec = x.tv_sec - y.tv_sec;
3004 result->tv_usec = x.tv_usec - y.tv_usec;
3006 /* Return indication of whether the result should be considered
3007 negative. */
3008 return x.tv_sec < y.tv_sec;
3011 void
3012 XTflash (f)
3013 struct frame *f;
3015 BLOCK_INPUT;
3018 #ifdef USE_GTK
3019 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3020 when the scroll bars and the edit widget share the same X window. */
3021 GdkGCValues vals;
3022 GdkGC *gc;
3023 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3024 ^ FRAME_BACKGROUND_PIXEL (f));
3025 vals.function = GDK_XOR;
3026 gc = gdk_gc_new_with_values (FRAME_GTK_WIDGET (f)->window,
3027 &vals,
3028 GDK_GC_FUNCTION
3029 | GDK_GC_FOREGROUND);
3030 #define XFillRectangle(d, win, gc, x, y, w, h) \
3031 gdk_draw_rectangle (FRAME_GTK_WIDGET (f)->window, \
3032 gc, TRUE, x, y, w, h)
3033 #else
3034 GC gc;
3036 /* Create a GC that will use the GXxor function to flip foreground
3037 pixels into background pixels. */
3039 XGCValues values;
3041 values.function = GXxor;
3042 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3043 ^ FRAME_BACKGROUND_PIXEL (f));
3045 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3046 GCFunction | GCForeground, &values);
3048 #endif
3050 /* Get the height not including a menu bar widget. */
3051 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
3052 /* Height of each line to flash. */
3053 int flash_height = FRAME_LINE_HEIGHT (f);
3054 /* These will be the left and right margins of the rectangles. */
3055 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3056 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3058 int width;
3060 /* Don't flash the area between a scroll bar and the frame
3061 edge it is next to. */
3062 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3064 case vertical_scroll_bar_left:
3065 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3066 break;
3068 case vertical_scroll_bar_right:
3069 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3070 break;
3072 default:
3073 break;
3076 width = flash_right - flash_left;
3078 /* If window is tall, flash top and bottom line. */
3079 if (height > 3 * FRAME_LINE_HEIGHT (f))
3081 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3082 flash_left,
3083 (FRAME_INTERNAL_BORDER_WIDTH (f)
3084 + FRAME_TOP_MARGIN_HEIGHT (f)),
3085 width, flash_height);
3086 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3087 flash_left,
3088 (height - flash_height
3089 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3090 width, flash_height);
3093 else
3094 /* If it is short, flash it all. */
3095 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3096 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3097 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3099 x_flush (f);
3102 struct timeval wakeup;
3104 EMACS_GET_TIME (wakeup);
3106 /* Compute time to wait until, propagating carry from usecs. */
3107 wakeup.tv_usec += 150000;
3108 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3109 wakeup.tv_usec %= 1000000;
3111 /* Keep waiting until past the time wakeup or any input gets
3112 available. */
3113 while (! detect_input_pending ())
3115 struct timeval current;
3116 struct timeval timeout;
3118 EMACS_GET_TIME (current);
3120 /* Break if result would be negative. */
3121 if (timeval_subtract (&current, wakeup, current))
3122 break;
3124 /* How long `select' should wait. */
3125 timeout.tv_sec = 0;
3126 timeout.tv_usec = 10000;
3128 /* Try to wait that long--but we might wake up sooner. */
3129 select (0, NULL, NULL, NULL, &timeout);
3133 /* If window is tall, flash top and bottom line. */
3134 if (height > 3 * FRAME_LINE_HEIGHT (f))
3136 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3137 flash_left,
3138 (FRAME_INTERNAL_BORDER_WIDTH (f)
3139 + FRAME_TOP_MARGIN_HEIGHT (f)),
3140 width, flash_height);
3141 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3142 flash_left,
3143 (height - flash_height
3144 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3145 width, flash_height);
3147 else
3148 /* If it is short, flash it all. */
3149 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3150 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3151 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3153 #ifdef USE_GTK
3154 g_object_unref (G_OBJECT (gc));
3155 #undef XFillRectangle
3156 #else
3157 XFreeGC (FRAME_X_DISPLAY (f), gc);
3158 #endif
3159 x_flush (f);
3163 UNBLOCK_INPUT;
3166 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3169 static void
3170 XTtoggle_invisible_pointer (f, invisible)
3171 FRAME_PTR f;
3172 int invisible;
3174 BLOCK_INPUT;
3175 if (invisible)
3177 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor != 0)
3178 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3179 FRAME_X_DISPLAY_INFO (f)->invisible_cursor);
3181 else
3182 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3183 f->output_data.x->current_cursor);
3184 f->pointer_invisible = invisible;
3185 UNBLOCK_INPUT;
3189 /* Make audible bell. */
3191 void
3192 XTring_bell ()
3194 struct frame *f = SELECTED_FRAME ();
3196 if (FRAME_X_DISPLAY (f))
3198 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3199 if (visible_bell)
3200 XTflash (f);
3201 else
3202 #endif
3204 BLOCK_INPUT;
3205 XBell (FRAME_X_DISPLAY (f), 0);
3206 XFlush (FRAME_X_DISPLAY (f));
3207 UNBLOCK_INPUT;
3213 /* Specify how many text lines, from the top of the window,
3214 should be affected by insert-lines and delete-lines operations.
3215 This, and those operations, are used only within an update
3216 that is bounded by calls to x_update_begin and x_update_end. */
3218 static void
3219 XTset_terminal_window (n)
3220 register int n;
3222 /* This function intentionally left blank. */
3227 /***********************************************************************
3228 Line Dance
3229 ***********************************************************************/
3231 /* Perform an insert-lines or delete-lines operation, inserting N
3232 lines or deleting -N lines at vertical position VPOS. */
3234 static void
3235 x_ins_del_lines (f, vpos, n)
3236 struct frame *f;
3237 int vpos, n;
3239 abort ();
3243 /* Scroll part of the display as described by RUN. */
3245 static void
3246 x_scroll_run (w, run)
3247 struct window *w;
3248 struct run *run;
3250 struct frame *f = XFRAME (w->frame);
3251 int x, y, width, height, from_y, to_y, bottom_y;
3253 /* Get frame-relative bounding box of the text display area of W,
3254 without mode lines. Include in this box the left and right
3255 fringe of W. */
3256 window_box (w, -1, &x, &y, &width, &height);
3258 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3259 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3260 bottom_y = y + height;
3262 if (to_y < from_y)
3264 /* Scrolling up. Make sure we don't copy part of the mode
3265 line at the bottom. */
3266 if (from_y + run->height > bottom_y)
3267 height = bottom_y - from_y;
3268 else
3269 height = run->height;
3271 else
3273 /* Scolling down. Make sure we don't copy over the mode line.
3274 at the bottom. */
3275 if (to_y + run->height > bottom_y)
3276 height = bottom_y - to_y;
3277 else
3278 height = run->height;
3281 BLOCK_INPUT;
3283 /* Cursor off. Will be switched on again in x_update_window_end. */
3284 updated_window = w;
3285 x_clear_cursor (w);
3287 XCopyArea (FRAME_X_DISPLAY (f),
3288 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3289 f->output_data.x->normal_gc,
3290 x, from_y,
3291 width, height,
3292 x, to_y);
3294 UNBLOCK_INPUT;
3299 /***********************************************************************
3300 Exposure Events
3301 ***********************************************************************/
3304 static void
3305 frame_highlight (f)
3306 struct frame *f;
3308 /* We used to only do this if Vx_no_window_manager was non-nil, but
3309 the ICCCM (section 4.1.6) says that the window's border pixmap
3310 and border pixel are window attributes which are "private to the
3311 client", so we can always change it to whatever we want. */
3312 BLOCK_INPUT;
3313 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3314 f->output_data.x->border_pixel);
3315 UNBLOCK_INPUT;
3316 x_update_cursor (f, 1);
3317 x_set_frame_alpha (f);
3320 static void
3321 frame_unhighlight (f)
3322 struct frame *f;
3324 /* We used to only do this if Vx_no_window_manager was non-nil, but
3325 the ICCCM (section 4.1.6) says that the window's border pixmap
3326 and border pixel are window attributes which are "private to the
3327 client", so we can always change it to whatever we want. */
3328 BLOCK_INPUT;
3329 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3330 f->output_data.x->border_tile);
3331 UNBLOCK_INPUT;
3332 x_update_cursor (f, 1);
3333 x_set_frame_alpha (f);
3336 /* The focus has changed. Update the frames as necessary to reflect
3337 the new situation. Note that we can't change the selected frame
3338 here, because the Lisp code we are interrupting might become confused.
3339 Each event gets marked with the frame in which it occurred, so the
3340 Lisp code can tell when the switch took place by examining the events. */
3342 static void
3343 x_new_focus_frame (dpyinfo, frame)
3344 struct x_display_info *dpyinfo;
3345 struct frame *frame;
3347 struct frame *old_focus = dpyinfo->x_focus_frame;
3349 if (frame != dpyinfo->x_focus_frame)
3351 /* Set this before calling other routines, so that they see
3352 the correct value of x_focus_frame. */
3353 dpyinfo->x_focus_frame = frame;
3355 if (old_focus && old_focus->auto_lower)
3356 x_lower_frame (old_focus);
3358 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3359 pending_autoraise_frame = dpyinfo->x_focus_frame;
3360 else
3361 pending_autoraise_frame = 0;
3364 x_frame_rehighlight (dpyinfo);
3367 /* Handle FocusIn and FocusOut state changes for FRAME.
3368 If FRAME has focus and there exists more than one frame, puts
3369 a FOCUS_IN_EVENT into *BUFP. */
3371 static void
3372 x_focus_changed (type, state, dpyinfo, frame, bufp)
3373 int type;
3374 int state;
3375 struct x_display_info *dpyinfo;
3376 struct frame *frame;
3377 struct input_event *bufp;
3379 if (type == FocusIn)
3381 if (dpyinfo->x_focus_event_frame != frame)
3383 x_new_focus_frame (dpyinfo, frame);
3384 dpyinfo->x_focus_event_frame = frame;
3386 /* Don't stop displaying the initial startup message
3387 for a switch-frame event we don't need. */
3388 if (NILP (Vterminal_frame)
3389 && CONSP (Vframe_list)
3390 && !NILP (XCDR (Vframe_list)))
3392 bufp->kind = FOCUS_IN_EVENT;
3393 XSETFRAME (bufp->frame_or_window, frame);
3397 frame->output_data.x->focus_state |= state;
3399 #ifdef HAVE_X_I18N
3400 if (FRAME_XIC (frame))
3401 XSetICFocus (FRAME_XIC (frame));
3402 #endif
3404 else if (type == FocusOut)
3406 frame->output_data.x->focus_state &= ~state;
3408 if (dpyinfo->x_focus_event_frame == frame)
3410 dpyinfo->x_focus_event_frame = 0;
3411 x_new_focus_frame (dpyinfo, 0);
3414 #ifdef HAVE_X_I18N
3415 if (FRAME_XIC (frame))
3416 XUnsetICFocus (FRAME_XIC (frame));
3417 #endif
3418 if (frame->pointer_invisible)
3419 XTtoggle_invisible_pointer (frame, 0);
3423 /* The focus may have changed. Figure out if it is a real focus change,
3424 by checking both FocusIn/Out and Enter/LeaveNotify events.
3426 Returns FOCUS_IN_EVENT event in *BUFP. */
3428 static void
3429 x_detect_focus_change (dpyinfo, event, bufp)
3430 struct x_display_info *dpyinfo;
3431 XEvent *event;
3432 struct input_event *bufp;
3434 struct frame *frame;
3436 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3437 if (! frame)
3438 return;
3440 switch (event->type)
3442 case EnterNotify:
3443 case LeaveNotify:
3445 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3446 int focus_state
3447 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3449 if (event->xcrossing.detail != NotifyInferior
3450 && event->xcrossing.focus
3451 && ! (focus_state & FOCUS_EXPLICIT))
3452 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3453 FOCUS_IMPLICIT,
3454 dpyinfo, frame, bufp);
3456 break;
3458 case FocusIn:
3459 case FocusOut:
3460 x_focus_changed (event->type,
3461 (event->xfocus.detail == NotifyPointer ?
3462 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3463 dpyinfo, frame, bufp);
3464 break;
3466 case ClientMessage:
3467 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3469 enum xembed_message msg = event->xclient.data.l[1];
3470 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3471 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3473 break;
3478 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3480 void
3481 x_mouse_leave (dpyinfo)
3482 struct x_display_info *dpyinfo;
3484 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3487 /* The focus has changed, or we have redirected a frame's focus to
3488 another frame (this happens when a frame uses a surrogate
3489 mini-buffer frame). Shift the highlight as appropriate.
3491 The FRAME argument doesn't necessarily have anything to do with which
3492 frame is being highlighted or un-highlighted; we only use it to find
3493 the appropriate X display info. */
3495 static void
3496 XTframe_rehighlight (frame)
3497 struct frame *frame;
3499 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3502 static void
3503 x_frame_rehighlight (dpyinfo)
3504 struct x_display_info *dpyinfo;
3506 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3508 if (dpyinfo->x_focus_frame)
3510 dpyinfo->x_highlight_frame
3511 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3512 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3513 : dpyinfo->x_focus_frame);
3514 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3516 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3517 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3520 else
3521 dpyinfo->x_highlight_frame = 0;
3523 if (dpyinfo->x_highlight_frame != old_highlight)
3525 if (old_highlight)
3526 frame_unhighlight (old_highlight);
3527 if (dpyinfo->x_highlight_frame)
3528 frame_highlight (dpyinfo->x_highlight_frame);
3534 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3536 /* Initialize mode_switch_bit and modifier_meaning. */
3537 static void
3538 x_find_modifier_meanings (dpyinfo)
3539 struct x_display_info *dpyinfo;
3541 int min_code, max_code;
3542 KeySym *syms;
3543 int syms_per_code;
3544 XModifierKeymap *mods;
3546 dpyinfo->meta_mod_mask = 0;
3547 dpyinfo->shift_lock_mask = 0;
3548 dpyinfo->alt_mod_mask = 0;
3549 dpyinfo->super_mod_mask = 0;
3550 dpyinfo->hyper_mod_mask = 0;
3552 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3554 syms = XGetKeyboardMapping (dpyinfo->display,
3555 min_code, max_code - min_code + 1,
3556 &syms_per_code);
3557 mods = XGetModifierMapping (dpyinfo->display);
3559 /* Scan the modifier table to see which modifier bits the Meta and
3560 Alt keysyms are on. */
3562 int row, col; /* The row and column in the modifier table. */
3563 int found_alt_or_meta;
3565 for (row = 3; row < 8; row++)
3567 found_alt_or_meta = 0;
3568 for (col = 0; col < mods->max_keypermod; col++)
3570 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3572 /* Zeroes are used for filler. Skip them. */
3573 if (code == 0)
3574 continue;
3576 /* Are any of this keycode's keysyms a meta key? */
3578 int code_col;
3580 for (code_col = 0; code_col < syms_per_code; code_col++)
3582 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3584 switch (sym)
3586 case XK_Meta_L:
3587 case XK_Meta_R:
3588 found_alt_or_meta = 1;
3589 dpyinfo->meta_mod_mask |= (1 << row);
3590 break;
3592 case XK_Alt_L:
3593 case XK_Alt_R:
3594 found_alt_or_meta = 1;
3595 dpyinfo->alt_mod_mask |= (1 << row);
3596 break;
3598 case XK_Hyper_L:
3599 case XK_Hyper_R:
3600 if (!found_alt_or_meta)
3601 dpyinfo->hyper_mod_mask |= (1 << row);
3602 code_col = syms_per_code;
3603 col = mods->max_keypermod;
3604 break;
3606 case XK_Super_L:
3607 case XK_Super_R:
3608 if (!found_alt_or_meta)
3609 dpyinfo->super_mod_mask |= (1 << row);
3610 code_col = syms_per_code;
3611 col = mods->max_keypermod;
3612 break;
3614 case XK_Shift_Lock:
3615 /* Ignore this if it's not on the lock modifier. */
3616 if (!found_alt_or_meta && ((1 << row) == LockMask))
3617 dpyinfo->shift_lock_mask = LockMask;
3618 code_col = syms_per_code;
3619 col = mods->max_keypermod;
3620 break;
3628 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3629 if (! dpyinfo->meta_mod_mask)
3631 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3632 dpyinfo->alt_mod_mask = 0;
3635 /* If some keys are both alt and meta,
3636 make them just meta, not alt. */
3637 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3639 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3642 XFree ((char *) syms);
3643 XFreeModifiermap (mods);
3646 /* Convert between the modifier bits X uses and the modifier bits
3647 Emacs uses. */
3649 unsigned int
3650 x_x_to_emacs_modifiers (dpyinfo, state)
3651 struct x_display_info *dpyinfo;
3652 unsigned int state;
3654 EMACS_UINT mod_meta = meta_modifier;
3655 EMACS_UINT mod_alt = alt_modifier;
3656 EMACS_UINT mod_hyper = hyper_modifier;
3657 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 & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3671 | ((state & ControlMask) ? ctrl_modifier : 0)
3672 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3673 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3674 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3675 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3678 static unsigned int
3679 x_emacs_to_x_modifiers (dpyinfo, state)
3680 struct x_display_info *dpyinfo;
3681 unsigned int state;
3683 EMACS_UINT mod_meta = meta_modifier;
3684 EMACS_UINT mod_alt = alt_modifier;
3685 EMACS_UINT mod_hyper = hyper_modifier;
3686 EMACS_UINT mod_super = super_modifier;
3688 Lisp_Object tem;
3690 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3691 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3692 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3693 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3694 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3695 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3696 tem = Fget (Vx_super_keysym, Qmodifier_value);
3697 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3700 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3701 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3702 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3703 | ((state & shift_modifier) ? ShiftMask : 0)
3704 | ((state & ctrl_modifier) ? ControlMask : 0)
3705 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3708 /* Convert a keysym to its name. */
3710 char *
3711 x_get_keysym_name (keysym)
3712 KeySym keysym;
3714 char *value;
3716 BLOCK_INPUT;
3717 value = XKeysymToString (keysym);
3718 UNBLOCK_INPUT;
3720 return value;
3725 /* Mouse clicks and mouse movement. Rah. */
3727 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3729 If the event is a button press, then note that we have grabbed
3730 the mouse. */
3732 static Lisp_Object
3733 construct_mouse_click (result, event, f)
3734 struct input_event *result;
3735 XButtonEvent *event;
3736 struct frame *f;
3738 /* Make the event type NO_EVENT; we'll change that when we decide
3739 otherwise. */
3740 result->kind = MOUSE_CLICK_EVENT;
3741 result->code = event->button - Button1;
3742 result->timestamp = event->time;
3743 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3744 event->state)
3745 | (event->type == ButtonRelease
3746 ? up_modifier
3747 : down_modifier));
3749 XSETINT (result->x, event->x);
3750 XSETINT (result->y, event->y);
3751 XSETFRAME (result->frame_or_window, f);
3752 result->arg = Qnil;
3753 return Qnil;
3757 /* Function to report a mouse movement to the mainstream Emacs code.
3758 The input handler calls this.
3760 We have received a mouse movement event, which is given in *event.
3761 If the mouse is over a different glyph than it was last time, tell
3762 the mainstream emacs code by setting mouse_moved. If not, ask for
3763 another motion event, so we can check again the next time it moves. */
3765 static XMotionEvent last_mouse_motion_event;
3766 static Lisp_Object last_mouse_motion_frame;
3768 static int
3769 note_mouse_movement (frame, event)
3770 FRAME_PTR frame;
3771 XMotionEvent *event;
3773 last_mouse_movement_time = event->time;
3774 last_mouse_motion_event = *event;
3775 XSETFRAME (last_mouse_motion_frame, frame);
3777 if (!FRAME_X_OUTPUT (frame))
3778 return 0;
3780 if (event->window != FRAME_X_WINDOW (frame))
3782 frame->mouse_moved = 1;
3783 last_mouse_scroll_bar = Qnil;
3784 note_mouse_highlight (frame, -1, -1);
3785 last_mouse_glyph_frame = 0;
3786 return 1;
3790 /* Has the mouse moved off the glyph it was on at the last sighting? */
3791 if (frame != last_mouse_glyph_frame
3792 || event->x < last_mouse_glyph.x
3793 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3794 || event->y < last_mouse_glyph.y
3795 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3797 frame->mouse_moved = 1;
3798 last_mouse_scroll_bar = Qnil;
3799 note_mouse_highlight (frame, event->x, event->y);
3800 /* Remember which glyph we're now on. */
3801 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3802 last_mouse_glyph_frame = frame;
3803 return 1;
3806 return 0;
3810 /************************************************************************
3811 Mouse Face
3812 ************************************************************************/
3814 static void
3815 redo_mouse_highlight ()
3817 if (!NILP (last_mouse_motion_frame)
3818 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3819 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3820 last_mouse_motion_event.x,
3821 last_mouse_motion_event.y);
3826 /* Return the current position of the mouse.
3827 *FP should be a frame which indicates which display to ask about.
3829 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3830 and *PART to the frame, window, and scroll bar part that the mouse
3831 is over. Set *X and *Y to the portion and whole of the mouse's
3832 position on the scroll bar.
3834 If the mouse movement started elsewhere, set *FP to the frame the
3835 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3836 the mouse is over.
3838 Set *TIME to the server time-stamp for the time at which the mouse
3839 was at this position.
3841 Don't store anything if we don't have a valid set of values to report.
3843 This clears the mouse_moved flag, so we can wait for the next mouse
3844 movement. */
3846 static void
3847 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3848 FRAME_PTR *fp;
3849 int insist;
3850 Lisp_Object *bar_window;
3851 enum scroll_bar_part *part;
3852 Lisp_Object *x, *y;
3853 unsigned long *time;
3855 FRAME_PTR f1;
3857 BLOCK_INPUT;
3859 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3860 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3861 else
3863 Window root;
3864 int root_x, root_y;
3866 Window dummy_window;
3867 int dummy;
3869 Lisp_Object frame, tail;
3871 /* Clear the mouse-moved flag for every frame on this display. */
3872 FOR_EACH_FRAME (tail, frame)
3873 if (FRAME_X_P (XFRAME (frame))
3874 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3875 XFRAME (frame)->mouse_moved = 0;
3877 last_mouse_scroll_bar = Qnil;
3879 /* Figure out which root window we're on. */
3880 XQueryPointer (FRAME_X_DISPLAY (*fp),
3881 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3883 /* The root window which contains the pointer. */
3884 &root,
3886 /* Trash which we can't trust if the pointer is on
3887 a different screen. */
3888 &dummy_window,
3890 /* The position on that root window. */
3891 &root_x, &root_y,
3893 /* More trash we can't trust. */
3894 &dummy, &dummy,
3896 /* Modifier keys and pointer buttons, about which
3897 we don't care. */
3898 (unsigned int *) &dummy);
3900 /* Now we have a position on the root; find the innermost window
3901 containing the pointer. */
3903 Window win, child;
3904 int win_x, win_y;
3905 int parent_x = 0, parent_y = 0;
3907 win = root;
3909 /* XTranslateCoordinates can get errors if the window
3910 structure is changing at the same time this function
3911 is running. So at least we must not crash from them. */
3913 x_catch_errors (FRAME_X_DISPLAY (*fp));
3915 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3916 && FRAME_LIVE_P (last_mouse_frame))
3918 /* If mouse was grabbed on a frame, give coords for that frame
3919 even if the mouse is now outside it. */
3920 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3922 /* From-window, to-window. */
3923 root, FRAME_X_WINDOW (last_mouse_frame),
3925 /* From-position, to-position. */
3926 root_x, root_y, &win_x, &win_y,
3928 /* Child of win. */
3929 &child);
3930 f1 = last_mouse_frame;
3932 else
3934 while (1)
3936 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3938 /* From-window, to-window. */
3939 root, win,
3941 /* From-position, to-position. */
3942 root_x, root_y, &win_x, &win_y,
3944 /* Child of win. */
3945 &child);
3947 if (child == None || child == win)
3948 break;
3949 #ifdef USE_GTK
3950 /* We don't wan't to know the innermost window. We
3951 want the edit window. For non-Gtk+ the innermost
3952 window is the edit window. For Gtk+ it might not
3953 be. It might be the tool bar for example. */
3954 if (x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win))
3955 break;
3956 #endif
3957 win = child;
3958 parent_x = win_x;
3959 parent_y = win_y;
3962 /* Now we know that:
3963 win is the innermost window containing the pointer
3964 (XTC says it has no child containing the pointer),
3965 win_x and win_y are the pointer's position in it
3966 (XTC did this the last time through), and
3967 parent_x and parent_y are the pointer's position in win's parent.
3968 (They are what win_x and win_y were when win was child.
3969 If win is the root window, it has no parent, and
3970 parent_{x,y} are invalid, but that's okay, because we'll
3971 never use them in that case.) */
3973 #ifdef USE_GTK
3974 /* We don't wan't to know the innermost window. We
3975 want the edit window. */
3976 f1 = x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3977 #else
3978 /* Is win one of our frames? */
3979 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3980 #endif
3982 #ifdef USE_X_TOOLKIT
3983 /* If we end up with the menu bar window, say it's not
3984 on the frame. */
3985 if (f1 != NULL
3986 && f1->output_data.x->menubar_widget
3987 && win == XtWindow (f1->output_data.x->menubar_widget))
3988 f1 = NULL;
3989 #endif /* USE_X_TOOLKIT */
3992 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3993 f1 = 0;
3995 x_uncatch_errors ();
3997 /* If not, is it one of our scroll bars? */
3998 if (! f1)
4000 struct scroll_bar *bar;
4002 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
4004 if (bar)
4006 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4007 win_x = parent_x;
4008 win_y = parent_y;
4012 if (f1 == 0 && insist > 0)
4013 f1 = SELECTED_FRAME ();
4015 if (f1)
4017 /* Ok, we found a frame. Store all the values.
4018 last_mouse_glyph is a rectangle used to reduce the
4019 generation of mouse events. To not miss any motion
4020 events, we must divide the frame into rectangles of the
4021 size of the smallest character that could be displayed
4022 on it, i.e. into the same rectangles that matrices on
4023 the frame are divided into. */
4025 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
4026 last_mouse_glyph_frame = f1;
4028 *bar_window = Qnil;
4029 *part = 0;
4030 *fp = f1;
4031 XSETINT (*x, win_x);
4032 XSETINT (*y, win_y);
4033 *time = last_mouse_movement_time;
4038 UNBLOCK_INPUT;
4043 /***********************************************************************
4044 Scroll bars
4045 ***********************************************************************/
4047 /* Scroll bar support. */
4049 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4050 manages it.
4051 This can be called in GC, so we have to make sure to strip off mark
4052 bits. */
4054 static struct scroll_bar *
4055 x_window_to_scroll_bar (display, window_id)
4056 Display *display;
4057 Window window_id;
4059 Lisp_Object tail;
4061 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4062 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4063 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4065 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4067 Lisp_Object frame, bar, condemned;
4069 frame = XCAR (tail);
4070 /* All elements of Vframe_list should be frames. */
4071 if (! FRAMEP (frame))
4072 abort ();
4074 if (! FRAME_X_P (XFRAME (frame)))
4075 continue;
4077 /* Scan this frame's scroll bar list for a scroll bar with the
4078 right window ID. */
4079 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4080 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4081 /* This trick allows us to search both the ordinary and
4082 condemned scroll bar lists with one loop. */
4083 ! NILP (bar) || (bar = condemned,
4084 condemned = Qnil,
4085 ! NILP (bar));
4086 bar = XSCROLL_BAR (bar)->next)
4087 if (XSCROLL_BAR (bar)->x_window == window_id &&
4088 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4089 return XSCROLL_BAR (bar);
4092 return 0;
4096 #if defined USE_LUCID
4098 /* Return the Lucid menu bar WINDOW is part of. Return null
4099 if WINDOW is not part of a menu bar. */
4101 static Widget
4102 x_window_to_menu_bar (window)
4103 Window window;
4105 Lisp_Object tail;
4107 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4109 if (FRAME_X_P (XFRAME (XCAR (tail))))
4111 Lisp_Object frame = XCAR (tail);
4112 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4114 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4115 return menu_bar;
4119 return NULL;
4122 #endif /* USE_LUCID */
4125 /************************************************************************
4126 Toolkit scroll bars
4127 ************************************************************************/
4129 #ifdef USE_TOOLKIT_SCROLL_BARS
4131 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4132 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4133 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4134 struct scroll_bar *));
4135 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4136 int, int, int));
4139 /* Lisp window being scrolled. Set when starting to interact with
4140 a toolkit scroll bar, reset to nil when ending the interaction. */
4142 static Lisp_Object window_being_scrolled;
4144 /* Last scroll bar part sent in xm_scroll_callback. */
4146 static int last_scroll_bar_part;
4148 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4149 that movements of 1/20 of the screen size are mapped to up/down. */
4151 #ifndef USE_GTK
4152 /* Id of action hook installed for scroll bars. */
4154 static XtActionHookId action_hook_id;
4156 static Boolean xaw3d_arrow_scroll;
4158 /* Whether the drag scrolling maintains the mouse at the top of the
4159 thumb. If not, resizing the thumb needs to be done more carefully
4160 to avoid jerkyness. */
4162 static Boolean xaw3d_pick_top;
4164 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4165 bars are used.. The hook is responsible for detecting when
4166 the user ends an interaction with the scroll bar, and generates
4167 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4169 static void
4170 xt_action_hook (widget, client_data, action_name, event, params,
4171 num_params)
4172 Widget widget;
4173 XtPointer client_data;
4174 String action_name;
4175 XEvent *event;
4176 String *params;
4177 Cardinal *num_params;
4179 int scroll_bar_p;
4180 char *end_action;
4182 #ifdef USE_MOTIF
4183 scroll_bar_p = XmIsScrollBar (widget);
4184 end_action = "Release";
4185 #else /* !USE_MOTIF i.e. use Xaw */
4186 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4187 end_action = "EndScroll";
4188 #endif /* USE_MOTIF */
4190 if (scroll_bar_p
4191 && strcmp (action_name, end_action) == 0
4192 && WINDOWP (window_being_scrolled))
4194 struct window *w;
4196 x_send_scroll_bar_event (window_being_scrolled,
4197 scroll_bar_end_scroll, 0, 0);
4198 w = XWINDOW (window_being_scrolled);
4200 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4202 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4203 /* The thumb size is incorrect while dragging: fix it. */
4204 set_vertical_scroll_bar (w);
4206 window_being_scrolled = Qnil;
4207 last_scroll_bar_part = -1;
4209 /* Xt timeouts no longer needed. */
4210 toolkit_scroll_bar_interaction = 0;
4213 #endif /* not USE_GTK */
4215 /* A vector of windows used for communication between
4216 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4218 static struct window **scroll_bar_windows;
4219 static int scroll_bar_windows_size;
4222 /* Send a client message with message type Xatom_Scrollbar for a
4223 scroll action to the frame of WINDOW. PART is a value identifying
4224 the part of the scroll bar that was clicked on. PORTION is the
4225 amount to scroll of a whole of WHOLE. */
4227 static void
4228 x_send_scroll_bar_event (window, part, portion, whole)
4229 Lisp_Object window;
4230 int part, portion, whole;
4232 XEvent event;
4233 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4234 struct window *w = XWINDOW (window);
4235 struct frame *f = XFRAME (w->frame);
4236 int i;
4238 BLOCK_INPUT;
4240 /* Construct a ClientMessage event to send to the frame. */
4241 ev->type = ClientMessage;
4242 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4243 ev->display = FRAME_X_DISPLAY (f);
4244 ev->window = FRAME_X_WINDOW (f);
4245 ev->format = 32;
4247 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4248 not enough to store a pointer or Lisp_Object on a 64 bit system.
4249 So, store the window in scroll_bar_windows and pass the index
4250 into that array in the event. */
4251 for (i = 0; i < scroll_bar_windows_size; ++i)
4252 if (scroll_bar_windows[i] == NULL)
4253 break;
4255 if (i == scroll_bar_windows_size)
4257 int new_size = max (10, 2 * scroll_bar_windows_size);
4258 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4259 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4261 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4262 nbytes);
4263 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4264 scroll_bar_windows_size = new_size;
4267 scroll_bar_windows[i] = w;
4268 ev->data.l[0] = (long) i;
4269 ev->data.l[1] = (long) part;
4270 ev->data.l[2] = (long) 0;
4271 ev->data.l[3] = (long) portion;
4272 ev->data.l[4] = (long) whole;
4274 /* Make Xt timeouts work while the scroll bar is active. */
4275 toolkit_scroll_bar_interaction = 1;
4276 #ifdef USE_X_TOOLKIT
4277 x_activate_timeout_atimer ();
4278 #endif
4280 /* Setting the event mask to zero means that the message will
4281 be sent to the client that created the window, and if that
4282 window no longer exists, no event will be sent. */
4283 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4284 UNBLOCK_INPUT;
4288 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4289 in *IEVENT. */
4291 static void
4292 x_scroll_bar_to_input_event (event, ievent)
4293 XEvent *event;
4294 struct input_event *ievent;
4296 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4297 Lisp_Object window;
4298 struct frame *f;
4299 struct window *w;
4301 w = scroll_bar_windows[ev->data.l[0]];
4302 scroll_bar_windows[ev->data.l[0]] = NULL;
4304 XSETWINDOW (window, w);
4305 f = XFRAME (w->frame);
4307 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4308 ievent->frame_or_window = window;
4309 ievent->arg = Qnil;
4310 #ifdef USE_GTK
4311 ievent->timestamp = CurrentTime;
4312 #else
4313 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4314 #endif
4315 ievent->part = ev->data.l[1];
4316 ievent->code = ev->data.l[2];
4317 ievent->x = make_number ((int) ev->data.l[3]);
4318 ievent->y = make_number ((int) ev->data.l[4]);
4319 ievent->modifiers = 0;
4323 #ifdef USE_MOTIF
4325 /* Minimum and maximum values used for Motif scroll bars. */
4327 #define XM_SB_MAX 10000000
4330 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4331 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4332 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4334 static void
4335 xm_scroll_callback (widget, client_data, call_data)
4336 Widget widget;
4337 XtPointer client_data, call_data;
4339 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4340 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4341 int part = -1, whole = 0, portion = 0;
4343 switch (cs->reason)
4345 case XmCR_DECREMENT:
4346 bar->dragging = Qnil;
4347 part = scroll_bar_up_arrow;
4348 break;
4350 case XmCR_INCREMENT:
4351 bar->dragging = Qnil;
4352 part = scroll_bar_down_arrow;
4353 break;
4355 case XmCR_PAGE_DECREMENT:
4356 bar->dragging = Qnil;
4357 part = scroll_bar_above_handle;
4358 break;
4360 case XmCR_PAGE_INCREMENT:
4361 bar->dragging = Qnil;
4362 part = scroll_bar_below_handle;
4363 break;
4365 case XmCR_TO_TOP:
4366 bar->dragging = Qnil;
4367 part = scroll_bar_to_top;
4368 break;
4370 case XmCR_TO_BOTTOM:
4371 bar->dragging = Qnil;
4372 part = scroll_bar_to_bottom;
4373 break;
4375 case XmCR_DRAG:
4377 int slider_size;
4379 /* Get the slider size. */
4380 BLOCK_INPUT;
4381 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4382 UNBLOCK_INPUT;
4384 whole = XM_SB_MAX - slider_size;
4385 portion = min (cs->value, whole);
4386 part = scroll_bar_handle;
4387 bar->dragging = make_number (cs->value);
4389 break;
4391 case XmCR_VALUE_CHANGED:
4392 break;
4395 if (part >= 0)
4397 window_being_scrolled = bar->window;
4398 last_scroll_bar_part = part;
4399 x_send_scroll_bar_event (bar->window, part, portion, whole);
4403 #elif defined USE_GTK
4405 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4406 bar widget. DATA is a pointer to the scroll_bar structure. */
4408 static gboolean
4409 xg_scroll_callback (GtkRange *range,
4410 GtkScrollType scroll,
4411 gdouble value,
4412 gpointer user_data)
4414 struct scroll_bar *bar = (struct scroll_bar *) user_data;
4415 gdouble position;
4416 int part = -1, whole = 0, portion = 0;
4417 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4418 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4420 if (xg_ignore_gtk_scrollbar) return FALSE;
4421 position = gtk_adjustment_get_value (adj);
4424 switch (scroll)
4426 case GTK_SCROLL_JUMP:
4427 /* Buttons 1 2 or 3 must be grabbed. */
4428 if (FRAME_X_DISPLAY_INFO (f)->grabbed != 0
4429 && FRAME_X_DISPLAY_INFO (f)->grabbed < (1 << 4))
4431 part = scroll_bar_handle;
4432 whole = adj->upper - adj->page_size;
4433 portion = min ((int)position, whole);
4434 bar->dragging = make_number ((int)portion);
4436 break;
4437 case GTK_SCROLL_STEP_BACKWARD:
4438 part = scroll_bar_up_arrow;
4439 bar->dragging = Qnil;
4440 break;
4441 case GTK_SCROLL_STEP_FORWARD:
4442 part = scroll_bar_down_arrow;
4443 bar->dragging = Qnil;
4444 break;
4445 case GTK_SCROLL_PAGE_BACKWARD:
4446 part = scroll_bar_above_handle;
4447 bar->dragging = Qnil;
4448 break;
4449 case GTK_SCROLL_PAGE_FORWARD:
4450 part = scroll_bar_below_handle;
4451 bar->dragging = Qnil;
4452 break;
4455 if (part >= 0)
4457 window_being_scrolled = bar->window;
4458 last_scroll_bar_part = part;
4459 x_send_scroll_bar_event (bar->window, part, portion, whole);
4462 return FALSE;
4465 /* Callback for button release. Sets dragging to Qnil when dragging is done. */
4467 static gboolean
4468 xg_end_scroll_callback (GtkWidget *widget,
4469 GdkEventButton *event,
4470 gpointer user_data)
4472 struct scroll_bar *bar = (struct scroll_bar *) user_data;
4473 bar->dragging = Qnil;
4474 if (WINDOWP (window_being_scrolled))
4476 x_send_scroll_bar_event (window_being_scrolled,
4477 scroll_bar_end_scroll, 0, 0);
4478 window_being_scrolled = Qnil;
4481 return FALSE;
4485 #else /* not USE_GTK and not USE_MOTIF */
4487 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4488 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4489 scroll bar struct. CALL_DATA is a pointer to a float saying where
4490 the thumb is. */
4492 static void
4493 xaw_jump_callback (widget, client_data, call_data)
4494 Widget widget;
4495 XtPointer client_data, call_data;
4497 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4498 float top = *(float *) call_data;
4499 float shown;
4500 int whole, portion, height;
4501 int part;
4503 /* Get the size of the thumb, a value between 0 and 1. */
4504 BLOCK_INPUT;
4505 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4506 UNBLOCK_INPUT;
4508 whole = 10000000;
4509 portion = shown < 1 ? top * whole : 0;
4511 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4512 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4513 the bottom, so we force the scrolling whenever we see that we're
4514 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4515 we try to ensure that we always stay two pixels away from the
4516 bottom). */
4517 part = scroll_bar_down_arrow;
4518 else
4519 part = scroll_bar_handle;
4521 window_being_scrolled = bar->window;
4522 bar->dragging = make_number (portion);
4523 last_scroll_bar_part = part;
4524 x_send_scroll_bar_event (bar->window, part, portion, whole);
4528 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4529 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4530 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4531 the scroll bar. CALL_DATA is an integer specifying the action that
4532 has taken place. Its magnitude is in the range 0..height of the
4533 scroll bar. Negative values mean scroll towards buffer start.
4534 Values < height of scroll bar mean line-wise movement. */
4536 static void
4537 xaw_scroll_callback (widget, client_data, call_data)
4538 Widget widget;
4539 XtPointer client_data, call_data;
4541 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4542 /* The position really is stored cast to a pointer. */
4543 int position = (long) call_data;
4544 Dimension height;
4545 int part;
4547 /* Get the height of the scroll bar. */
4548 BLOCK_INPUT;
4549 XtVaGetValues (widget, XtNheight, &height, NULL);
4550 UNBLOCK_INPUT;
4552 if (eabs (position) >= height)
4553 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4555 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4556 it maps line-movement to call_data = max(5, height/20). */
4557 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4558 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4559 else
4560 part = scroll_bar_move_ratio;
4562 window_being_scrolled = bar->window;
4563 bar->dragging = Qnil;
4564 last_scroll_bar_part = part;
4565 x_send_scroll_bar_event (bar->window, part, position, height);
4568 #endif /* not USE_GTK and not USE_MOTIF */
4570 #define SCROLL_BAR_NAME "verticalScrollBar"
4572 /* Create the widget for scroll bar BAR on frame F. Record the widget
4573 and X window of the scroll bar in BAR. */
4575 #ifdef USE_GTK
4576 static void
4577 x_create_toolkit_scroll_bar (f, bar)
4578 struct frame *f;
4579 struct scroll_bar *bar;
4581 char *scroll_bar_name = SCROLL_BAR_NAME;
4583 BLOCK_INPUT;
4584 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4585 G_CALLBACK (xg_end_scroll_callback),
4586 scroll_bar_name);
4587 UNBLOCK_INPUT;
4590 #else /* not USE_GTK */
4592 static void
4593 x_create_toolkit_scroll_bar (f, bar)
4594 struct frame *f;
4595 struct scroll_bar *bar;
4597 Window xwindow;
4598 Widget widget;
4599 Arg av[20];
4600 int ac = 0;
4601 char *scroll_bar_name = SCROLL_BAR_NAME;
4602 unsigned long pixel;
4604 BLOCK_INPUT;
4606 #ifdef USE_MOTIF
4607 /* Set resources. Create the widget. */
4608 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4609 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4610 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4611 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4612 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4613 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4614 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4616 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4617 if (pixel != -1)
4619 XtSetArg (av[ac], XmNforeground, pixel);
4620 ++ac;
4623 pixel = f->output_data.x->scroll_bar_background_pixel;
4624 if (pixel != -1)
4626 XtSetArg (av[ac], XmNbackground, pixel);
4627 ++ac;
4630 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4631 scroll_bar_name, av, ac);
4633 /* Add one callback for everything that can happen. */
4634 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4635 (XtPointer) bar);
4636 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4637 (XtPointer) bar);
4638 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4639 (XtPointer) bar);
4640 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4641 (XtPointer) bar);
4642 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4643 (XtPointer) bar);
4644 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4645 (XtPointer) bar);
4646 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4647 (XtPointer) bar);
4649 /* Realize the widget. Only after that is the X window created. */
4650 XtRealizeWidget (widget);
4652 /* Set the cursor to an arrow. I didn't find a resource to do that.
4653 And I'm wondering why it hasn't an arrow cursor by default. */
4654 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4655 f->output_data.x->nontext_cursor);
4657 #else /* !USE_MOTIF i.e. use Xaw */
4659 /* Set resources. Create the widget. The background of the
4660 Xaw3d scroll bar widget is a little bit light for my taste.
4661 We don't alter it here to let users change it according
4662 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4663 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4664 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4665 /* For smoother scrolling with Xaw3d -sm */
4666 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4668 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4669 if (pixel != -1)
4671 XtSetArg (av[ac], XtNforeground, pixel);
4672 ++ac;
4675 pixel = f->output_data.x->scroll_bar_background_pixel;
4676 if (pixel != -1)
4678 XtSetArg (av[ac], XtNbackground, pixel);
4679 ++ac;
4682 /* Top/bottom shadow colors. */
4684 /* Allocate them, if necessary. */
4685 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4687 pixel = f->output_data.x->scroll_bar_background_pixel;
4688 if (pixel != -1)
4690 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4691 FRAME_X_COLORMAP (f),
4692 &pixel, 1.2, 0x8000))
4693 pixel = -1;
4694 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4697 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4699 pixel = f->output_data.x->scroll_bar_background_pixel;
4700 if (pixel != -1)
4702 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4703 FRAME_X_COLORMAP (f),
4704 &pixel, 0.6, 0x4000))
4705 pixel = -1;
4706 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4710 #ifdef XtNbeNiceToColormap
4711 /* Tell the toolkit about them. */
4712 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4713 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4714 /* We tried to allocate a color for the top/bottom shadow, and
4715 failed, so tell Xaw3d to use dithering instead. */
4717 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4718 ++ac;
4720 else
4721 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4722 be more consistent with other emacs 3d colors, and since Xaw3d is
4723 not good at dealing with allocation failure. */
4725 /* This tells Xaw3d to use real colors instead of dithering for
4726 the shadows. */
4727 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4728 ++ac;
4730 /* Specify the colors. */
4731 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4732 if (pixel != -1)
4734 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4735 ++ac;
4737 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4738 if (pixel != -1)
4740 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4741 ++ac;
4744 #endif
4746 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4747 f->output_data.x->edit_widget, av, ac);
4750 char *initial = "";
4751 char *val = initial;
4752 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4753 #ifdef XtNarrowScrollbars
4754 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4755 #endif
4756 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4757 if (xaw3d_arrow_scroll || val == initial)
4758 { /* ARROW_SCROLL */
4759 xaw3d_arrow_scroll = True;
4760 /* Isn't that just a personal preference ? --Stef */
4761 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4765 /* Define callbacks. */
4766 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4767 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4768 (XtPointer) bar);
4770 /* Realize the widget. Only after that is the X window created. */
4771 XtRealizeWidget (widget);
4773 #endif /* !USE_MOTIF */
4775 /* Install an action hook that lets us detect when the user
4776 finishes interacting with a scroll bar. */
4777 if (action_hook_id == 0)
4778 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4780 /* Remember X window and widget in the scroll bar vector. */
4781 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4782 xwindow = XtWindow (widget);
4783 bar->x_window = xwindow;
4785 UNBLOCK_INPUT;
4787 #endif /* not USE_GTK */
4790 /* Set the thumb size and position of scroll bar BAR. We are currently
4791 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4793 #ifdef USE_GTK
4794 static void
4795 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4796 struct scroll_bar *bar;
4797 int portion, position, whole;
4799 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4802 #else /* not USE_GTK */
4803 static void
4804 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4805 struct scroll_bar *bar;
4806 int portion, position, whole;
4808 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4809 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4810 float top, shown;
4812 BLOCK_INPUT;
4814 #ifdef USE_MOTIF
4816 /* We use an estimate of 30 chars per line rather than the real
4817 `portion' value. This has the disadvantage that the thumb size
4818 is not very representative, but it makes our life a lot easier.
4819 Otherwise, we have to constantly adjust the thumb size, which
4820 we can't always do quickly enough: while dragging, the size of
4821 the thumb might prevent the user from dragging the thumb all the
4822 way to the end. but Motif and some versions of Xaw3d don't allow
4823 updating the thumb size while dragging. Also, even if we can update
4824 its size, the update will often happen too late.
4825 If you don't believe it, check out revision 1.650 of xterm.c to see
4826 what hoops we were going through and the still poor behavior we got. */
4827 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4828 /* When the thumb is at the bottom, position == whole.
4829 So we need to increase `whole' to make space for the thumb. */
4830 whole += portion;
4832 if (whole <= 0)
4833 top = 0, shown = 1;
4834 else
4836 top = (float) position / whole;
4837 shown = (float) portion / whole;
4840 if (NILP (bar->dragging))
4842 int size, value;
4844 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4845 is the scroll bar's maximum and MIN is the scroll bar's minimum
4846 value. */
4847 size = shown * XM_SB_MAX;
4848 size = min (size, XM_SB_MAX);
4849 size = max (size, 1);
4851 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4852 value = top * XM_SB_MAX;
4853 value = min (value, XM_SB_MAX - size);
4855 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4857 #else /* !USE_MOTIF i.e. use Xaw */
4859 if (whole == 0)
4860 top = 0, shown = 1;
4861 else
4863 top = (float) position / whole;
4864 shown = (float) portion / whole;
4868 float old_top, old_shown;
4869 Dimension height;
4870 XtVaGetValues (widget,
4871 XtNtopOfThumb, &old_top,
4872 XtNshown, &old_shown,
4873 XtNheight, &height,
4874 NULL);
4876 /* Massage the top+shown values. */
4877 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4878 top = max (0, min (1, top));
4879 else
4880 top = old_top;
4881 /* Keep two pixels available for moving the thumb down. */
4882 shown = max (0, min (1 - top - (2.0 / height), shown));
4884 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4885 check that your system's configuration file contains a define
4886 for `NARROWPROTO'. See s/freebsd.h for an example. */
4887 if (top != old_top || shown != old_shown)
4889 if (NILP (bar->dragging))
4890 XawScrollbarSetThumb (widget, top, shown);
4891 else
4893 /* Try to make the scrolling a tad smoother. */
4894 if (!xaw3d_pick_top)
4895 shown = min (shown, old_shown);
4897 XawScrollbarSetThumb (widget, top, shown);
4901 #endif /* !USE_MOTIF */
4903 UNBLOCK_INPUT;
4905 #endif /* not USE_GTK */
4907 #endif /* USE_TOOLKIT_SCROLL_BARS */
4911 /************************************************************************
4912 Scroll bars, general
4913 ************************************************************************/
4915 /* Create a scroll bar and return the scroll bar vector for it. W is
4916 the Emacs window on which to create the scroll bar. TOP, LEFT,
4917 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4918 scroll bar. */
4920 static struct scroll_bar *
4921 x_scroll_bar_create (w, top, left, width, height)
4922 struct window *w;
4923 int top, left, width, height;
4925 struct frame *f = XFRAME (w->frame);
4926 struct scroll_bar *bar
4927 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4929 BLOCK_INPUT;
4931 #ifdef USE_TOOLKIT_SCROLL_BARS
4932 x_create_toolkit_scroll_bar (f, bar);
4933 #else /* not USE_TOOLKIT_SCROLL_BARS */
4935 XSetWindowAttributes a;
4936 unsigned long mask;
4937 Window window;
4939 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4940 if (a.background_pixel == -1)
4941 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4943 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4944 | ButtonMotionMask | PointerMotionHintMask
4945 | ExposureMask);
4946 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4948 mask = (CWBackPixel | CWEventMask | CWCursor);
4950 /* Clear the area of W that will serve as a scroll bar. This is
4951 for the case that a window has been split horizontally. In
4952 this case, no clear_frame is generated to reduce flickering. */
4953 if (width > 0 && height > 0)
4954 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4955 left, top, width,
4956 window_box_height (w), False);
4958 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4959 /* Position and size of scroll bar. */
4960 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4961 top,
4962 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4963 height,
4964 /* Border width, depth, class, and visual. */
4966 CopyFromParent,
4967 CopyFromParent,
4968 CopyFromParent,
4969 /* Attributes. */
4970 mask, &a);
4971 bar->x_window = window;
4973 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4975 XSETWINDOW (bar->window, w);
4976 bar->top = top;
4977 bar->left = left;
4978 bar->width = width;
4979 bar->height = height;
4980 bar->start = 0;
4981 bar->end = 0;
4982 bar->dragging = Qnil;
4983 bar->fringe_extended_p = 0;
4985 /* Add bar to its frame's list of scroll bars. */
4986 bar->next = FRAME_SCROLL_BARS (f);
4987 bar->prev = Qnil;
4988 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4989 if (!NILP (bar->next))
4990 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4992 /* Map the window/widget. */
4993 #ifdef USE_TOOLKIT_SCROLL_BARS
4995 #ifdef USE_GTK
4996 xg_update_scrollbar_pos (f,
4997 bar->x_window,
4998 top,
4999 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5000 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5001 max (height, 1));
5002 xg_show_scroll_bar (bar->x_window);
5003 #else /* not USE_GTK */
5004 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5005 XtConfigureWidget (scroll_bar,
5006 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5007 top,
5008 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5009 max (height, 1), 0);
5010 XtMapWidget (scroll_bar);
5011 #endif /* not USE_GTK */
5013 #else /* not USE_TOOLKIT_SCROLL_BARS */
5014 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
5015 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5017 UNBLOCK_INPUT;
5018 return bar;
5022 #ifndef USE_TOOLKIT_SCROLL_BARS
5024 /* Draw BAR's handle in the proper position.
5026 If the handle is already drawn from START to END, don't bother
5027 redrawing it, unless REBUILD is non-zero; in that case, always
5028 redraw it. (REBUILD is handy for drawing the handle after expose
5029 events.)
5031 Normally, we want to constrain the start and end of the handle to
5032 fit inside its rectangle, but if the user is dragging the scroll
5033 bar handle, we want to let them drag it down all the way, so that
5034 the bar's top is as far down as it goes; otherwise, there's no way
5035 to move to the very end of the buffer. */
5037 static void
5038 x_scroll_bar_set_handle (bar, start, end, rebuild)
5039 struct scroll_bar *bar;
5040 int start, end;
5041 int rebuild;
5043 int dragging = ! NILP (bar->dragging);
5044 Window w = bar->x_window;
5045 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5046 GC gc = f->output_data.x->normal_gc;
5048 /* If the display is already accurate, do nothing. */
5049 if (! rebuild
5050 && start == bar->start
5051 && end == bar->end)
5052 return;
5054 BLOCK_INPUT;
5057 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5058 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5059 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5061 /* Make sure the values are reasonable, and try to preserve
5062 the distance between start and end. */
5064 int length = end - start;
5066 if (start < 0)
5067 start = 0;
5068 else if (start > top_range)
5069 start = top_range;
5070 end = start + length;
5072 if (end < start)
5073 end = start;
5074 else if (end > top_range && ! dragging)
5075 end = top_range;
5078 /* Store the adjusted setting in the scroll bar. */
5079 bar->start = start;
5080 bar->end = end;
5082 /* Clip the end position, just for display. */
5083 if (end > top_range)
5084 end = top_range;
5086 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5087 below top positions, to make sure the handle is always at least
5088 that many pixels tall. */
5089 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5091 /* Draw the empty space above the handle. Note that we can't clear
5092 zero-height areas; that means "clear to end of window." */
5093 if (0 < start)
5094 x_clear_area (FRAME_X_DISPLAY (f), w,
5095 /* x, y, width, height, and exposures. */
5096 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5097 VERTICAL_SCROLL_BAR_TOP_BORDER,
5098 inside_width, start,
5099 False);
5101 /* Change to proper foreground color if one is specified. */
5102 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5103 XSetForeground (FRAME_X_DISPLAY (f), gc,
5104 f->output_data.x->scroll_bar_foreground_pixel);
5106 /* Draw the handle itself. */
5107 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5108 /* x, y, width, height */
5109 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5110 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5111 inside_width, end - start);
5113 /* Restore the foreground color of the GC if we changed it above. */
5114 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5115 XSetForeground (FRAME_X_DISPLAY (f), gc,
5116 FRAME_FOREGROUND_PIXEL (f));
5118 /* Draw the empty space below the handle. Note that we can't
5119 clear zero-height areas; that means "clear to end of window." */
5120 if (end < inside_height)
5121 x_clear_area (FRAME_X_DISPLAY (f), w,
5122 /* x, y, width, height, and exposures. */
5123 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5124 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5125 inside_width, inside_height - end,
5126 False);
5130 UNBLOCK_INPUT;
5133 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5135 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5136 nil. */
5138 static void
5139 x_scroll_bar_remove (bar)
5140 struct scroll_bar *bar;
5142 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5143 BLOCK_INPUT;
5145 #ifdef USE_TOOLKIT_SCROLL_BARS
5146 #ifdef USE_GTK
5147 xg_remove_scroll_bar (f, bar->x_window);
5148 #else /* not USE_GTK */
5149 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5150 #endif /* not USE_GTK */
5151 #else
5152 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5153 #endif
5155 /* Disassociate this scroll bar from its window. */
5156 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5158 UNBLOCK_INPUT;
5162 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5163 that we are displaying PORTION characters out of a total of WHOLE
5164 characters, starting at POSITION. If WINDOW has no scroll bar,
5165 create one. */
5167 static void
5168 XTset_vertical_scroll_bar (w, portion, whole, position)
5169 struct window *w;
5170 int portion, whole, position;
5172 struct frame *f = XFRAME (w->frame);
5173 struct scroll_bar *bar;
5174 int top, height, left, sb_left, width, sb_width;
5175 int window_y, window_height;
5176 #ifdef USE_TOOLKIT_SCROLL_BARS
5177 int fringe_extended_p;
5178 #endif
5180 /* Get window dimensions. */
5181 window_box (w, -1, 0, &window_y, 0, &window_height);
5182 top = window_y;
5183 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5184 height = window_height;
5186 /* Compute the left edge of the scroll bar area. */
5187 left = WINDOW_SCROLL_BAR_AREA_X (w);
5189 /* Compute the width of the scroll bar which might be less than
5190 the width of the area reserved for the scroll bar. */
5191 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5192 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5193 else
5194 sb_width = width;
5196 /* Compute the left edge of the scroll bar. */
5197 #ifdef USE_TOOLKIT_SCROLL_BARS
5198 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5199 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5200 else
5201 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5202 #else
5203 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5204 sb_left = left + width - sb_width;
5205 else
5206 sb_left = left;
5207 #endif
5209 #ifdef USE_TOOLKIT_SCROLL_BARS
5210 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5211 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5212 && WINDOW_LEFT_FRINGE_WIDTH (w)
5213 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5214 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5215 else
5216 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5217 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5218 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5219 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5220 #endif
5222 /* Does the scroll bar exist yet? */
5223 if (NILP (w->vertical_scroll_bar))
5225 if (width > 0 && height > 0)
5227 BLOCK_INPUT;
5228 #ifdef USE_TOOLKIT_SCROLL_BARS
5229 if (fringe_extended_p)
5230 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5231 sb_left, top, sb_width, height, False);
5232 else
5233 #endif
5234 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5235 left, top, width, height, False);
5236 UNBLOCK_INPUT;
5239 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5241 else
5243 /* It may just need to be moved and resized. */
5244 unsigned int mask = 0;
5246 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5248 BLOCK_INPUT;
5250 if (sb_left != bar->left)
5251 mask |= CWX;
5252 if (top != bar->top)
5253 mask |= CWY;
5254 if (sb_width != bar->width)
5255 mask |= CWWidth;
5256 if (height != bar->height)
5257 mask |= CWHeight;
5259 #ifdef USE_TOOLKIT_SCROLL_BARS
5261 /* Move/size the scroll bar widget. */
5262 if (mask || bar->fringe_extended_p != fringe_extended_p)
5264 /* Since toolkit scroll bars are smaller than the space reserved
5265 for them on the frame, we have to clear "under" them. */
5266 if (width > 0 && height > 0)
5268 if (fringe_extended_p)
5269 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5270 sb_left, top, sb_width, height, False);
5271 else
5272 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5273 left, top, width, height, False);
5275 #ifdef USE_GTK
5276 xg_update_scrollbar_pos (f,
5277 bar->x_window,
5278 top,
5279 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5280 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5281 max (height, 1));
5282 #else /* not USE_GTK */
5283 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5284 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5285 top,
5286 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5287 max (height, 1), 0);
5288 #endif /* not USE_GTK */
5290 #else /* not USE_TOOLKIT_SCROLL_BARS */
5292 /* Clear areas not covered by the scroll bar because of
5293 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5294 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5296 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5297 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5298 height, False);
5299 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5300 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5301 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5302 height, False);
5305 /* Clear areas not covered by the scroll bar because it's not as
5306 wide as the area reserved for it. This makes sure a
5307 previous mode line display is cleared after C-x 2 C-x 1, for
5308 example. */
5310 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5311 int rest = area_width - sb_width;
5312 if (rest > 0 && height > 0)
5314 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5315 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5316 left + area_width - rest, top,
5317 rest, height, False);
5318 else
5319 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5320 left, top, rest, height, False);
5324 /* Move/size the scroll bar window. */
5325 if (mask)
5327 XWindowChanges wc;
5329 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5330 wc.y = top;
5331 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5332 wc.height = height;
5333 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5334 mask, &wc);
5337 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5339 /* Remember new settings. */
5340 bar->left = sb_left;
5341 bar->top = top;
5342 bar->width = sb_width;
5343 bar->height = height;
5345 UNBLOCK_INPUT;
5348 #ifdef USE_TOOLKIT_SCROLL_BARS
5349 bar->fringe_extended_p = fringe_extended_p;
5351 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5352 #else /* not USE_TOOLKIT_SCROLL_BARS */
5353 /* Set the scroll bar's current state, unless we're currently being
5354 dragged. */
5355 if (NILP (bar->dragging))
5357 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5359 if (whole == 0)
5360 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5361 else
5363 int start = ((double) position * top_range) / whole;
5364 int end = ((double) (position + portion) * top_range) / whole;
5365 x_scroll_bar_set_handle (bar, start, end, 0);
5368 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5370 XSETVECTOR (w->vertical_scroll_bar, bar);
5374 /* The following three hooks are used when we're doing a thorough
5375 redisplay of the frame. We don't explicitly know which scroll bars
5376 are going to be deleted, because keeping track of when windows go
5377 away is a real pain - "Can you say set-window-configuration, boys
5378 and girls?" Instead, we just assert at the beginning of redisplay
5379 that *all* scroll bars are to be removed, and then save a scroll bar
5380 from the fiery pit when we actually redisplay its window. */
5382 /* Arrange for all scroll bars on FRAME to be removed at the next call
5383 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5384 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5386 static void
5387 XTcondemn_scroll_bars (frame)
5388 FRAME_PTR frame;
5390 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5391 while (! NILP (FRAME_SCROLL_BARS (frame)))
5393 Lisp_Object bar;
5394 bar = FRAME_SCROLL_BARS (frame);
5395 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5396 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5397 XSCROLL_BAR (bar)->prev = Qnil;
5398 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5399 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5400 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5405 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5406 Note that WINDOW isn't necessarily condemned at all. */
5408 static void
5409 XTredeem_scroll_bar (window)
5410 struct window *window;
5412 struct scroll_bar *bar;
5413 struct frame *f;
5415 /* We can't redeem this window's scroll bar if it doesn't have one. */
5416 if (NILP (window->vertical_scroll_bar))
5417 abort ();
5419 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5421 /* Unlink it from the condemned list. */
5422 f = XFRAME (WINDOW_FRAME (window));
5423 if (NILP (bar->prev))
5425 /* If the prev pointer is nil, it must be the first in one of
5426 the lists. */
5427 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5428 /* It's not condemned. Everything's fine. */
5429 return;
5430 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5431 window->vertical_scroll_bar))
5432 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5433 else
5434 /* If its prev pointer is nil, it must be at the front of
5435 one or the other! */
5436 abort ();
5438 else
5439 XSCROLL_BAR (bar->prev)->next = bar->next;
5441 if (! NILP (bar->next))
5442 XSCROLL_BAR (bar->next)->prev = bar->prev;
5444 bar->next = FRAME_SCROLL_BARS (f);
5445 bar->prev = Qnil;
5446 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5447 if (! NILP (bar->next))
5448 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5451 /* Remove all scroll bars on FRAME that haven't been saved since the
5452 last call to `*condemn_scroll_bars_hook'. */
5454 static void
5455 XTjudge_scroll_bars (f)
5456 FRAME_PTR f;
5458 Lisp_Object bar, next;
5460 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5462 /* Clear out the condemned list now so we won't try to process any
5463 more events on the hapless scroll bars. */
5464 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5466 for (; ! NILP (bar); bar = next)
5468 struct scroll_bar *b = XSCROLL_BAR (bar);
5470 x_scroll_bar_remove (b);
5472 next = b->next;
5473 b->next = b->prev = Qnil;
5476 /* Now there should be no references to the condemned scroll bars,
5477 and they should get garbage-collected. */
5481 #ifndef USE_TOOLKIT_SCROLL_BARS
5482 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5483 is a no-op when using toolkit scroll bars.
5485 This may be called from a signal handler, so we have to ignore GC
5486 mark bits. */
5488 static void
5489 x_scroll_bar_expose (bar, event)
5490 struct scroll_bar *bar;
5491 XEvent *event;
5493 Window w = bar->x_window;
5494 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5495 GC gc = f->output_data.x->normal_gc;
5496 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5498 BLOCK_INPUT;
5500 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5502 /* Switch to scroll bar foreground color. */
5503 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5504 XSetForeground (FRAME_X_DISPLAY (f), gc,
5505 f->output_data.x->scroll_bar_foreground_pixel);
5507 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5508 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5510 /* x, y, width, height */
5511 0, 0,
5512 bar->width - 1 - width_trim - width_trim,
5513 bar->height - 1);
5515 /* Restore the foreground color of the GC if we changed it above. */
5516 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5517 XSetForeground (FRAME_X_DISPLAY (f), gc,
5518 FRAME_FOREGROUND_PIXEL (f));
5520 UNBLOCK_INPUT;
5523 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5525 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5526 is set to something other than NO_EVENT, it is enqueued.
5528 This may be called from a signal handler, so we have to ignore GC
5529 mark bits. */
5532 static void
5533 x_scroll_bar_handle_click (bar, event, emacs_event)
5534 struct scroll_bar *bar;
5535 XEvent *event;
5536 struct input_event *emacs_event;
5538 if (! WINDOWP (bar->window))
5539 abort ();
5541 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5542 emacs_event->code = event->xbutton.button - Button1;
5543 emacs_event->modifiers
5544 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5545 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5546 event->xbutton.state)
5547 | (event->type == ButtonRelease
5548 ? up_modifier
5549 : down_modifier));
5550 emacs_event->frame_or_window = bar->window;
5551 emacs_event->arg = Qnil;
5552 emacs_event->timestamp = event->xbutton.time;
5554 int top_range
5555 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5556 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5558 if (y < 0) y = 0;
5559 if (y > top_range) y = top_range;
5561 if (y < bar->start)
5562 emacs_event->part = scroll_bar_above_handle;
5563 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5564 emacs_event->part = scroll_bar_handle;
5565 else
5566 emacs_event->part = scroll_bar_below_handle;
5568 #ifndef USE_TOOLKIT_SCROLL_BARS
5569 /* If the user has released the handle, set it to its final position. */
5570 if (event->type == ButtonRelease
5571 && ! NILP (bar->dragging))
5573 int new_start = y - XINT (bar->dragging);
5574 int new_end = new_start + bar->end - bar->start;
5576 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5577 bar->dragging = Qnil;
5579 #endif
5581 XSETINT (emacs_event->x, y);
5582 XSETINT (emacs_event->y, top_range);
5586 #ifndef USE_TOOLKIT_SCROLL_BARS
5588 /* Handle some mouse motion while someone is dragging the scroll bar.
5590 This may be called from a signal handler, so we have to ignore GC
5591 mark bits. */
5593 static void
5594 x_scroll_bar_note_movement (bar, event)
5595 struct scroll_bar *bar;
5596 XEvent *event;
5598 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5600 last_mouse_movement_time = event->xmotion.time;
5602 f->mouse_moved = 1;
5603 XSETVECTOR (last_mouse_scroll_bar, bar);
5605 /* If we're dragging the bar, display it. */
5606 if (! NILP (bar->dragging))
5608 /* Where should the handle be now? */
5609 int new_start = event->xmotion.y - XINT (bar->dragging);
5611 if (new_start != bar->start)
5613 int new_end = new_start + bar->end - bar->start;
5615 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5620 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5622 /* Return information to the user about the current position of the mouse
5623 on the scroll bar. */
5625 static void
5626 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5627 FRAME_PTR *fp;
5628 Lisp_Object *bar_window;
5629 enum scroll_bar_part *part;
5630 Lisp_Object *x, *y;
5631 unsigned long *time;
5633 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5634 Window w = bar->x_window;
5635 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5636 int win_x, win_y;
5637 Window dummy_window;
5638 int dummy_coord;
5639 unsigned int dummy_mask;
5641 BLOCK_INPUT;
5643 /* Get the mouse's position relative to the scroll bar window, and
5644 report that. */
5645 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5647 /* Root, child, root x and root y. */
5648 &dummy_window, &dummy_window,
5649 &dummy_coord, &dummy_coord,
5651 /* Position relative to scroll bar. */
5652 &win_x, &win_y,
5654 /* Mouse buttons and modifier keys. */
5655 &dummy_mask))
5657 else
5659 int top_range
5660 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5662 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5664 if (! NILP (bar->dragging))
5665 win_y -= XINT (bar->dragging);
5667 if (win_y < 0)
5668 win_y = 0;
5669 if (win_y > top_range)
5670 win_y = top_range;
5672 *fp = f;
5673 *bar_window = bar->window;
5675 if (! NILP (bar->dragging))
5676 *part = scroll_bar_handle;
5677 else if (win_y < bar->start)
5678 *part = scroll_bar_above_handle;
5679 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5680 *part = scroll_bar_handle;
5681 else
5682 *part = scroll_bar_below_handle;
5684 XSETINT (*x, win_y);
5685 XSETINT (*y, top_range);
5687 f->mouse_moved = 0;
5688 last_mouse_scroll_bar = Qnil;
5691 *time = last_mouse_movement_time;
5693 UNBLOCK_INPUT;
5697 /* The screen has been cleared so we may have changed foreground or
5698 background colors, and the scroll bars may need to be redrawn.
5699 Clear out the scroll bars, and ask for expose events, so we can
5700 redraw them. */
5702 void
5703 x_scroll_bar_clear (f)
5704 FRAME_PTR f;
5706 #ifndef USE_TOOLKIT_SCROLL_BARS
5707 Lisp_Object bar;
5709 /* We can have scroll bars even if this is 0,
5710 if we just turned off scroll bar mode.
5711 But in that case we should not clear them. */
5712 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5713 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5714 bar = XSCROLL_BAR (bar)->next)
5715 XClearArea (FRAME_X_DISPLAY (f),
5716 XSCROLL_BAR (bar)->x_window,
5717 0, 0, 0, 0, True);
5718 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5722 /* The main X event-reading loop - XTread_socket. */
5724 /* This holds the state XLookupString needs to implement dead keys
5725 and other tricks known as "compose processing". _X Window System_
5726 says that a portable program can't use this, but Stephen Gildea assures
5727 me that letting the compiler initialize it to zeros will work okay.
5729 This must be defined outside of XTread_socket, for the same reasons
5730 given for enter_timestamp, above. */
5732 static XComposeStatus compose_status;
5734 /* Record the last 100 characters stored
5735 to help debug the loss-of-chars-during-GC problem. */
5737 static int temp_index;
5738 static short temp_buffer[100];
5740 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5741 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5742 temp_index = 0; \
5743 temp_buffer[temp_index++] = (keysym)
5745 /* Set this to nonzero to fake an "X I/O error"
5746 on a particular display. */
5748 struct x_display_info *XTread_socket_fake_io_error;
5750 /* When we find no input here, we occasionally do a no-op command
5751 to verify that the X server is still running and we can still talk with it.
5752 We try all the open displays, one by one.
5753 This variable is used for cycling thru the displays. */
5755 static struct x_display_info *next_noop_dpyinfo;
5757 #define SET_SAVED_MENU_EVENT(size) \
5758 do \
5760 if (f->output_data.x->saved_menu_event == 0) \
5761 f->output_data.x->saved_menu_event \
5762 = (XEvent *) xmalloc (sizeof (XEvent)); \
5763 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5764 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5765 XSETFRAME (inev.ie.frame_or_window, f); \
5767 while (0)
5769 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5770 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5773 enum
5775 X_EVENT_NORMAL,
5776 X_EVENT_GOTO_OUT,
5777 X_EVENT_DROP
5780 /* Filter events for the current X input method.
5781 DPYINFO is the display this event is for.
5782 EVENT is the X event to filter.
5784 Returns non-zero if the event was filtered, caller shall not process
5785 this event further.
5786 Returns zero if event is wasn't filtered. */
5788 #ifdef HAVE_X_I18N
5789 static int
5790 x_filter_event (dpyinfo, event)
5791 struct x_display_info *dpyinfo;
5792 XEvent *event;
5794 /* XFilterEvent returns non-zero if the input method has
5795 consumed the event. We pass the frame's X window to
5796 XFilterEvent because that's the one for which the IC
5797 was created. */
5799 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5800 event->xclient.window);
5802 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5804 #endif
5806 #ifdef USE_GTK
5807 static int current_count;
5808 static int current_finish;
5809 static struct input_event *current_hold_quit;
5811 /* This is the filter function invoked by the GTK event loop.
5812 It is invoked before the XEvent is translated to a GdkEvent,
5813 so we have a chance to act on the event before GTK. */
5814 static GdkFilterReturn
5815 event_handler_gdk (gxev, ev, data)
5816 GdkXEvent *gxev;
5817 GdkEvent *ev;
5818 gpointer data;
5820 XEvent *xev = (XEvent *) gxev;
5822 BLOCK_INPUT;
5823 if (current_count >= 0)
5825 struct x_display_info *dpyinfo;
5827 dpyinfo = x_display_info_for_display (xev->xany.display);
5829 #ifdef HAVE_X_I18N
5830 /* Filter events for the current X input method.
5831 GTK calls XFilterEvent but not for key press and release,
5832 so we do it here. */
5833 if ((xev->type == KeyPress || xev->type == KeyRelease)
5834 && dpyinfo
5835 && x_filter_event (dpyinfo, xev))
5837 UNBLOCK_INPUT;
5838 return GDK_FILTER_REMOVE;
5840 #endif
5842 if (! dpyinfo)
5843 current_finish = X_EVENT_NORMAL;
5844 else
5845 current_count +=
5846 handle_one_xevent (dpyinfo, xev, &current_finish,
5847 current_hold_quit);
5849 else
5850 current_finish = x_dispatch_event (xev, xev->xany.display);
5852 UNBLOCK_INPUT;
5854 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5855 return GDK_FILTER_REMOVE;
5857 return GDK_FILTER_CONTINUE;
5859 #endif /* USE_GTK */
5862 /* Handles the XEvent EVENT on display DPYINFO.
5864 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5865 *FINISH is zero if caller should continue reading events.
5866 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5868 We return the number of characters stored into the buffer. */
5870 static int
5871 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5872 struct x_display_info *dpyinfo;
5873 XEvent *eventp;
5874 int *finish;
5875 struct input_event *hold_quit;
5877 union {
5878 struct input_event ie;
5879 struct selection_input_event sie;
5880 } inev;
5881 int count = 0;
5882 int do_help = 0;
5883 int nbytes = 0;
5884 struct frame *f = NULL;
5885 struct coding_system coding;
5886 XEvent event = *eventp;
5888 *finish = X_EVENT_NORMAL;
5890 EVENT_INIT (inev.ie);
5891 inev.ie.kind = NO_EVENT;
5892 inev.ie.arg = Qnil;
5894 if (pending_event_wait.eventtype == event.type)
5895 pending_event_wait.eventtype = 0; /* Indicates we got it. */
5897 switch (event.type)
5899 case ClientMessage:
5901 if (event.xclient.message_type
5902 == dpyinfo->Xatom_wm_protocols
5903 && event.xclient.format == 32)
5905 if (event.xclient.data.l[0]
5906 == dpyinfo->Xatom_wm_take_focus)
5908 /* Use x_any_window_to_frame because this
5909 could be the shell widget window
5910 if the frame has no title bar. */
5911 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5912 #ifdef HAVE_X_I18N
5913 /* Not quite sure this is needed -pd */
5914 if (f && FRAME_XIC (f))
5915 XSetICFocus (FRAME_XIC (f));
5916 #endif
5917 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5918 instructs the WM to set the input focus automatically for
5919 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5920 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5921 it has set the focus. So, XSetInputFocus below is not
5922 needed.
5924 The call to XSetInputFocus below has also caused trouble. In
5925 cases where the XSetInputFocus done by the WM and the one
5926 below are temporally close (on a fast machine), the call
5927 below can generate additional FocusIn events which confuse
5928 Emacs. */
5930 /* Since we set WM_TAKE_FOCUS, we must call
5931 XSetInputFocus explicitly. But not if f is null,
5932 since that might be an event for a deleted frame. */
5933 if (f)
5935 Display *d = event.xclient.display;
5936 /* Catch and ignore errors, in case window has been
5937 iconified by a window manager such as GWM. */
5938 x_catch_errors (d);
5939 XSetInputFocus (d, event.xclient.window,
5940 /* The ICCCM says this is
5941 the only valid choice. */
5942 RevertToParent,
5943 event.xclient.data.l[1]);
5944 /* This is needed to detect the error
5945 if there is an error. */
5946 XSync (d, False);
5947 x_uncatch_errors ();
5949 /* Not certain about handling scroll bars here */
5950 #endif /* 0 */
5951 goto done;
5954 if (event.xclient.data.l[0]
5955 == dpyinfo->Xatom_wm_save_yourself)
5957 /* Save state modify the WM_COMMAND property to
5958 something which can reinstate us. This notifies
5959 the session manager, who's looking for such a
5960 PropertyNotify. Can restart processing when
5961 a keyboard or mouse event arrives. */
5962 /* If we have a session manager, don't set this.
5963 KDE will then start two Emacsen, one for the
5964 session manager and one for this. */
5965 #ifdef HAVE_X_SM
5966 if (! x_session_have_connection ())
5967 #endif
5969 f = x_top_window_to_frame (dpyinfo,
5970 event.xclient.window);
5971 /* This is just so we only give real data once
5972 for a single Emacs process. */
5973 if (f == SELECTED_FRAME ())
5974 XSetCommand (FRAME_X_DISPLAY (f),
5975 event.xclient.window,
5976 initial_argv, initial_argc);
5977 else if (f)
5978 XSetCommand (FRAME_X_DISPLAY (f),
5979 event.xclient.window,
5980 0, 0);
5982 goto done;
5985 if (event.xclient.data.l[0]
5986 == dpyinfo->Xatom_wm_delete_window)
5988 f = x_any_window_to_frame (dpyinfo,
5989 event.xclient.window);
5990 if (!f)
5991 goto OTHER; /* May be a dialog that is to be removed */
5993 inev.ie.kind = DELETE_WINDOW_EVENT;
5994 XSETFRAME (inev.ie.frame_or_window, f);
5995 goto done;
5998 goto done;
6001 if (event.xclient.message_type
6002 == dpyinfo->Xatom_wm_configure_denied)
6004 goto done;
6007 if (event.xclient.message_type
6008 == dpyinfo->Xatom_wm_window_moved)
6010 int new_x, new_y;
6011 f = x_window_to_frame (dpyinfo, event.xclient.window);
6013 new_x = event.xclient.data.s[0];
6014 new_y = event.xclient.data.s[1];
6016 if (f)
6018 f->left_pos = new_x;
6019 f->top_pos = new_y;
6021 goto done;
6024 #ifdef HACK_EDITRES
6025 if (event.xclient.message_type
6026 == dpyinfo->Xatom_editres)
6028 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6029 if (f)
6030 _XEditResCheckMessages (f->output_data.x->widget, NULL,
6031 &event, NULL);
6032 goto done;
6034 #endif /* HACK_EDITRES */
6036 if ((event.xclient.message_type
6037 == dpyinfo->Xatom_DONE)
6038 || (event.xclient.message_type
6039 == dpyinfo->Xatom_PAGE))
6041 /* Ghostview job completed. Kill it. We could
6042 reply with "Next" if we received "Page", but we
6043 currently never do because we are interested in
6044 images, only, which should have 1 page. */
6045 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
6046 f = x_window_to_frame (dpyinfo, event.xclient.window);
6047 if (!f)
6048 goto OTHER;
6049 x_kill_gs_process (pixmap, f);
6050 expose_frame (f, 0, 0, 0, 0);
6051 goto done;
6054 #ifdef USE_TOOLKIT_SCROLL_BARS
6055 /* Scroll bar callbacks send a ClientMessage from which
6056 we construct an input_event. */
6057 if (event.xclient.message_type
6058 == dpyinfo->Xatom_Scrollbar)
6060 x_scroll_bar_to_input_event (&event, &inev.ie);
6061 *finish = X_EVENT_GOTO_OUT;
6062 goto done;
6064 #endif /* USE_TOOLKIT_SCROLL_BARS */
6066 /* XEmbed messages from the embedder (if any). */
6067 if (event.xclient.message_type
6068 == dpyinfo->Xatom_XEMBED)
6070 enum xembed_message msg = event.xclient.data.l[1];
6071 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6072 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6074 *finish = X_EVENT_GOTO_OUT;
6075 goto done;
6078 xft_settings_event (dpyinfo, &event);
6080 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6081 if (!f)
6082 goto OTHER;
6083 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
6084 *finish = X_EVENT_DROP;
6086 break;
6088 case SelectionNotify:
6089 last_user_time = event.xselection.time;
6090 #ifdef USE_X_TOOLKIT
6091 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6092 goto OTHER;
6093 #endif /* not USE_X_TOOLKIT */
6094 x_handle_selection_notify (&event.xselection);
6095 break;
6097 case SelectionClear: /* Someone has grabbed ownership. */
6098 last_user_time = event.xselectionclear.time;
6099 #ifdef USE_X_TOOLKIT
6100 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6101 goto OTHER;
6102 #endif /* USE_X_TOOLKIT */
6104 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
6106 inev.ie.kind = SELECTION_CLEAR_EVENT;
6107 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6108 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6109 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6110 inev.ie.frame_or_window = Qnil;
6112 break;
6114 case SelectionRequest: /* Someone wants our selection. */
6115 last_user_time = event.xselectionrequest.time;
6116 #ifdef USE_X_TOOLKIT
6117 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6118 goto OTHER;
6119 #endif /* USE_X_TOOLKIT */
6121 XSelectionRequestEvent *eventp
6122 = (XSelectionRequestEvent *) &event;
6124 inev.ie.kind = SELECTION_REQUEST_EVENT;
6125 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6126 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6127 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6128 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6129 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6130 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6131 inev.ie.frame_or_window = Qnil;
6133 break;
6135 case PropertyNotify:
6136 last_user_time = event.xproperty.time;
6137 f = x_top_window_to_frame (dpyinfo, event.xproperty.window);
6138 if (f && event.xproperty.atom == dpyinfo->Xatom_net_wm_state)
6139 x_handle_net_wm_state (f, &event.xproperty);
6141 x_handle_property_notify (&event.xproperty);
6142 xft_settings_event (dpyinfo, &event);
6143 goto OTHER;
6145 case ReparentNotify:
6146 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6147 if (f)
6149 int x, y;
6150 f->output_data.x->parent_desc = event.xreparent.parent;
6151 x_real_positions (f, &x, &y);
6152 f->left_pos = x;
6153 f->top_pos = y;
6155 /* Perhaps reparented due to a WM restart. Reset this. */
6156 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6157 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6159 goto OTHER;
6161 case Expose:
6162 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6163 if (f)
6165 #ifdef USE_GTK
6166 /* This seems to be needed for GTK 2.6. */
6167 x_clear_area (event.xexpose.display,
6168 event.xexpose.window,
6169 event.xexpose.x, event.xexpose.y,
6170 event.xexpose.width, event.xexpose.height,
6171 FALSE);
6172 #endif
6173 if (f->async_visible == 0)
6175 f->async_visible = 1;
6176 f->async_iconified = 0;
6177 f->output_data.x->has_been_visible = 1;
6178 SET_FRAME_GARBAGED (f);
6180 else
6181 expose_frame (f,
6182 event.xexpose.x, event.xexpose.y,
6183 event.xexpose.width, event.xexpose.height);
6185 else
6187 #ifndef USE_TOOLKIT_SCROLL_BARS
6188 struct scroll_bar *bar;
6189 #endif
6190 #if defined USE_LUCID
6191 /* Submenus of the Lucid menu bar aren't widgets
6192 themselves, so there's no way to dispatch events
6193 to them. Recognize this case separately. */
6195 Widget widget
6196 = x_window_to_menu_bar (event.xexpose.window);
6197 if (widget)
6198 xlwmenu_redisplay (widget);
6200 #endif /* USE_LUCID */
6202 #ifdef USE_TOOLKIT_SCROLL_BARS
6203 /* Dispatch event to the widget. */
6204 goto OTHER;
6205 #else /* not USE_TOOLKIT_SCROLL_BARS */
6206 bar = x_window_to_scroll_bar (event.xexpose.display,
6207 event.xexpose.window);
6209 if (bar)
6210 x_scroll_bar_expose (bar, &event);
6211 #ifdef USE_X_TOOLKIT
6212 else
6213 goto OTHER;
6214 #endif /* USE_X_TOOLKIT */
6215 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6217 break;
6219 case GraphicsExpose: /* This occurs when an XCopyArea's
6220 source area was obscured or not
6221 available. */
6222 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6223 if (f)
6225 expose_frame (f,
6226 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6227 event.xgraphicsexpose.width,
6228 event.xgraphicsexpose.height);
6230 #ifdef USE_X_TOOLKIT
6231 else
6232 goto OTHER;
6233 #endif /* USE_X_TOOLKIT */
6234 break;
6236 case NoExpose: /* This occurs when an XCopyArea's
6237 source area was completely
6238 available. */
6239 break;
6241 case UnmapNotify:
6242 /* Redo the mouse-highlight after the tooltip has gone. */
6243 if (event.xmap.window == tip_window)
6245 tip_window = 0;
6246 redo_mouse_highlight ();
6249 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6250 if (f) /* F may no longer exist if
6251 the frame was deleted. */
6253 /* While a frame is unmapped, display generation is
6254 disabled; you don't want to spend time updating a
6255 display that won't ever be seen. */
6256 f->async_visible = 0;
6257 /* We can't distinguish, from the event, whether the window
6258 has become iconified or invisible. So assume, if it
6259 was previously visible, than now it is iconified.
6260 But x_make_frame_invisible clears both
6261 the visible flag and the iconified flag;
6262 and that way, we know the window is not iconified now. */
6263 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6265 f->async_iconified = 1;
6267 inev.ie.kind = ICONIFY_EVENT;
6268 XSETFRAME (inev.ie.frame_or_window, f);
6271 goto OTHER;
6273 case MapNotify:
6274 if (event.xmap.window == tip_window)
6275 /* The tooltip has been drawn already. Avoid
6276 the SET_FRAME_GARBAGED below. */
6277 goto OTHER;
6279 /* We use x_top_window_to_frame because map events can
6280 come for sub-windows and they don't mean that the
6281 frame is visible. */
6282 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6283 if (f)
6285 /* wait_reading_process_output will notice this and update
6286 the frame's display structures.
6287 If we where iconified, we should not set garbaged,
6288 because that stops redrawing on Expose events. This looks
6289 bad if we are called from a recursive event loop
6290 (x_dispatch_event), for example when a dialog is up. */
6291 if (! f->async_iconified)
6292 SET_FRAME_GARBAGED (f);
6294 /* Check if fullscreen was specified before we where mapped the
6295 first time, i.e. from the command line. */
6296 if (!f->output_data.x->has_been_visible)
6297 x_check_fullscreen (f);
6299 f->async_visible = 1;
6300 f->async_iconified = 0;
6301 f->output_data.x->has_been_visible = 1;
6303 if (f->iconified)
6305 inev.ie.kind = DEICONIFY_EVENT;
6306 XSETFRAME (inev.ie.frame_or_window, f);
6308 else if (! NILP (Vframe_list)
6309 && ! NILP (XCDR (Vframe_list)))
6310 /* Force a redisplay sooner or later
6311 to update the frame titles
6312 in case this is the second frame. */
6313 record_asynch_buffer_change ();
6315 #ifdef USE_GTK
6316 xg_frame_resized (f, -1, -1);
6317 #endif
6319 goto OTHER;
6321 case KeyPress:
6323 last_user_time = event.xkey.time;
6324 ignore_next_mouse_click_timeout = 0;
6326 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6327 /* Dispatch KeyPress events when in menu. */
6328 if (popup_activated ())
6329 goto OTHER;
6330 #endif
6332 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6334 /* If mouse-highlight is an integer, input clears out
6335 mouse highlighting. */
6336 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6337 && (f == 0
6338 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6340 clear_mouse_face (dpyinfo);
6341 dpyinfo->mouse_face_hidden = 1;
6344 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6345 if (f == 0)
6347 /* Scroll bars consume key events, but we want
6348 the keys to go to the scroll bar's frame. */
6349 Widget widget = XtWindowToWidget (dpyinfo->display,
6350 event.xkey.window);
6351 if (widget && XmIsScrollBar (widget))
6353 widget = XtParent (widget);
6354 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6357 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6359 if (f != 0)
6361 KeySym keysym, orig_keysym;
6362 /* al%imercury@uunet.uu.net says that making this 81
6363 instead of 80 fixed a bug whereby meta chars made
6364 his Emacs hang.
6366 It seems that some version of XmbLookupString has
6367 a bug of not returning XBufferOverflow in
6368 status_return even if the input is too long to
6369 fit in 81 bytes. So, we must prepare sufficient
6370 bytes for copy_buffer. 513 bytes (256 chars for
6371 two-byte character set) seems to be a fairly good
6372 approximation. -- 2000.8.10 handa@etl.go.jp */
6373 unsigned char copy_buffer[513];
6374 unsigned char *copy_bufptr = copy_buffer;
6375 int copy_bufsiz = sizeof (copy_buffer);
6376 int modifiers;
6377 Lisp_Object coding_system = Qlatin_1;
6378 Lisp_Object c;
6380 #ifdef USE_GTK
6381 /* Don't pass keys to GTK. A Tab will shift focus to the
6382 tool bar in GTK 2.4. Keys will still go to menus and
6383 dialogs because in that case popup_activated is TRUE
6384 (see above). */
6385 *finish = X_EVENT_DROP;
6386 #endif
6388 event.xkey.state
6389 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6390 extra_keyboard_modifiers);
6391 modifiers = event.xkey.state;
6393 /* This will have to go some day... */
6395 /* make_lispy_event turns chars into control chars.
6396 Don't do it here because XLookupString is too eager. */
6397 event.xkey.state &= ~ControlMask;
6398 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6399 | dpyinfo->super_mod_mask
6400 | dpyinfo->hyper_mod_mask
6401 | dpyinfo->alt_mod_mask);
6403 /* In case Meta is ComposeCharacter,
6404 clear its status. According to Markus Ehrnsperger
6405 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6406 this enables ComposeCharacter to work whether or
6407 not it is combined with Meta. */
6408 if (modifiers & dpyinfo->meta_mod_mask)
6409 bzero (&compose_status, sizeof (compose_status));
6411 #ifdef HAVE_X_I18N
6412 if (FRAME_XIC (f))
6414 Status status_return;
6416 coding_system = Vlocale_coding_system;
6417 nbytes = XmbLookupString (FRAME_XIC (f),
6418 &event.xkey, copy_bufptr,
6419 copy_bufsiz, &keysym,
6420 &status_return);
6421 if (status_return == XBufferOverflow)
6423 copy_bufsiz = nbytes + 1;
6424 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6425 nbytes = XmbLookupString (FRAME_XIC (f),
6426 &event.xkey, copy_bufptr,
6427 copy_bufsiz, &keysym,
6428 &status_return);
6430 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6431 if (status_return == XLookupNone)
6432 break;
6433 else if (status_return == XLookupChars)
6435 keysym = NoSymbol;
6436 modifiers = 0;
6438 else if (status_return != XLookupKeySym
6439 && status_return != XLookupBoth)
6440 abort ();
6442 else
6443 nbytes = XLookupString (&event.xkey, copy_bufptr,
6444 copy_bufsiz, &keysym,
6445 &compose_status);
6446 #else
6447 nbytes = XLookupString (&event.xkey, copy_bufptr,
6448 copy_bufsiz, &keysym,
6449 &compose_status);
6450 #endif
6452 /* If not using XIM/XIC, and a compose sequence is in progress,
6453 we break here. Otherwise, chars_matched is always 0. */
6454 if (compose_status.chars_matched > 0 && nbytes == 0)
6455 break;
6457 bzero (&compose_status, sizeof (compose_status));
6458 orig_keysym = keysym;
6460 /* Common for all keysym input events. */
6461 XSETFRAME (inev.ie.frame_or_window, f);
6462 inev.ie.modifiers
6463 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6464 inev.ie.timestamp = event.xkey.time;
6466 /* First deal with keysyms which have defined
6467 translations to characters. */
6468 if (keysym >= 32 && keysym < 128)
6469 /* Avoid explicitly decoding each ASCII character. */
6471 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6472 inev.ie.code = keysym;
6473 goto done_keysym;
6476 /* Keysyms directly mapped to Unicode characters. */
6477 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6479 if (keysym < 0x01000080)
6480 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6481 else
6482 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6483 inev.ie.code = keysym & 0xFFFFFF;
6484 goto done_keysym;
6487 /* Now non-ASCII. */
6488 if (HASH_TABLE_P (Vx_keysym_table)
6489 && (NATNUMP (c = Fgethash (make_number (keysym),
6490 Vx_keysym_table,
6491 Qnil))))
6493 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6494 ? ASCII_KEYSTROKE_EVENT
6495 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6496 inev.ie.code = XFASTINT (c);
6497 goto done_keysym;
6500 /* Random non-modifier sorts of keysyms. */
6501 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6502 || keysym == XK_Delete
6503 #ifdef XK_ISO_Left_Tab
6504 || (keysym >= XK_ISO_Left_Tab
6505 && keysym <= XK_ISO_Enter)
6506 #endif
6507 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6508 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6509 #ifdef HPUX
6510 /* This recognizes the "extended function
6511 keys". It seems there's no cleaner way.
6512 Test IsModifierKey to avoid handling
6513 mode_switch incorrectly. */
6514 || ((unsigned) (keysym) >= XK_Select
6515 && (unsigned)(keysym) < XK_KP_Space)
6516 #endif
6517 #ifdef XK_dead_circumflex
6518 || orig_keysym == XK_dead_circumflex
6519 #endif
6520 #ifdef XK_dead_grave
6521 || orig_keysym == XK_dead_grave
6522 #endif
6523 #ifdef XK_dead_tilde
6524 || orig_keysym == XK_dead_tilde
6525 #endif
6526 #ifdef XK_dead_diaeresis
6527 || orig_keysym == XK_dead_diaeresis
6528 #endif
6529 #ifdef XK_dead_macron
6530 || orig_keysym == XK_dead_macron
6531 #endif
6532 #ifdef XK_dead_degree
6533 || orig_keysym == XK_dead_degree
6534 #endif
6535 #ifdef XK_dead_acute
6536 || orig_keysym == XK_dead_acute
6537 #endif
6538 #ifdef XK_dead_cedilla
6539 || orig_keysym == XK_dead_cedilla
6540 #endif
6541 #ifdef XK_dead_breve
6542 || orig_keysym == XK_dead_breve
6543 #endif
6544 #ifdef XK_dead_ogonek
6545 || orig_keysym == XK_dead_ogonek
6546 #endif
6547 #ifdef XK_dead_caron
6548 || orig_keysym == XK_dead_caron
6549 #endif
6550 #ifdef XK_dead_doubleacute
6551 || orig_keysym == XK_dead_doubleacute
6552 #endif
6553 #ifdef XK_dead_abovedot
6554 || orig_keysym == XK_dead_abovedot
6555 #endif
6556 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6557 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6558 /* Any "vendor-specific" key is ok. */
6559 || (orig_keysym & (1 << 28))
6560 || (keysym != NoSymbol && nbytes == 0))
6561 && ! (IsModifierKey (orig_keysym)
6562 /* The symbols from XK_ISO_Lock
6563 to XK_ISO_Last_Group_Lock
6564 don't have real modifiers but
6565 should be treated similarly to
6566 Mode_switch by Emacs. */
6567 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6568 || ((unsigned)(orig_keysym)
6569 >= XK_ISO_Lock
6570 && (unsigned)(orig_keysym)
6571 <= XK_ISO_Last_Group_Lock)
6572 #endif
6575 STORE_KEYSYM_FOR_DEBUG (keysym);
6576 /* make_lispy_event will convert this to a symbolic
6577 key. */
6578 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6579 inev.ie.code = keysym;
6580 goto done_keysym;
6583 { /* Raw bytes, not keysym. */
6584 register int i;
6585 register int c;
6586 int nchars, len;
6588 for (i = 0, nchars = 0; i < nbytes; i++)
6590 if (ASCII_BYTE_P (copy_bufptr[i]))
6591 nchars++;
6592 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6595 if (nchars < nbytes)
6597 /* Decode the input data. */
6598 int require;
6599 unsigned char *p;
6601 /* The input should be decoded with `coding_system'
6602 which depends on which X*LookupString function
6603 we used just above and the locale. */
6604 setup_coding_system (coding_system, &coding);
6605 coding.src_multibyte = 0;
6606 coding.dst_multibyte = 1;
6607 /* The input is converted to events, thus we can't
6608 handle composition. Anyway, there's no XIM that
6609 gives us composition information. */
6610 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6612 require = MAX_MULTIBYTE_LENGTH * nbytes;
6613 coding.destination = alloca (require);
6614 coding.dst_bytes = require;
6615 coding.mode |= CODING_MODE_LAST_BLOCK;
6616 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6617 nbytes = coding.produced;
6618 nchars = coding.produced_char;
6619 copy_bufptr = coding.destination;
6622 /* Convert the input data to a sequence of
6623 character events. */
6624 for (i = 0; i < nbytes; i += len)
6626 if (nchars == nbytes)
6627 c = copy_bufptr[i], len = 1;
6628 else
6629 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
6630 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6631 ? ASCII_KEYSTROKE_EVENT
6632 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6633 inev.ie.code = c;
6634 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6637 count += nchars;
6639 inev.ie.kind = NO_EVENT; /* Already stored above. */
6641 if (keysym == NoSymbol)
6642 break;
6645 done_keysym:
6646 #ifdef HAVE_X_I18N
6647 /* Don't dispatch this event since XtDispatchEvent calls
6648 XFilterEvent, and two calls in a row may freeze the
6649 client. */
6650 break;
6651 #else
6652 goto OTHER;
6653 #endif
6655 case KeyRelease:
6656 last_user_time = event.xkey.time;
6657 #ifdef HAVE_X_I18N
6658 /* Don't dispatch this event since XtDispatchEvent calls
6659 XFilterEvent, and two calls in a row may freeze the
6660 client. */
6661 break;
6662 #else
6663 goto OTHER;
6664 #endif
6666 case EnterNotify:
6667 last_user_time = event.xcrossing.time;
6668 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6670 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6672 if (f && x_mouse_click_focus_ignore_position)
6673 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6675 /* EnterNotify counts as mouse movement,
6676 so update things that depend on mouse position. */
6677 if (f && !f->output_data.x->hourglass_p)
6678 note_mouse_movement (f, &event.xmotion);
6679 #ifdef USE_GTK
6680 /* We may get an EnterNotify on the buttons in the toolbar. In that
6681 case we moved out of any highlighted area and need to note this. */
6682 if (!f && last_mouse_glyph_frame)
6683 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6684 #endif
6685 goto OTHER;
6687 case FocusIn:
6688 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6689 goto OTHER;
6691 case LeaveNotify:
6692 last_user_time = event.xcrossing.time;
6693 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6695 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6696 if (f)
6698 if (f == dpyinfo->mouse_face_mouse_frame)
6700 /* If we move outside the frame, then we're
6701 certainly no longer on any text in the frame. */
6702 clear_mouse_face (dpyinfo);
6703 dpyinfo->mouse_face_mouse_frame = 0;
6706 /* Generate a nil HELP_EVENT to cancel a help-echo.
6707 Do it only if there's something to cancel.
6708 Otherwise, the startup message is cleared when
6709 the mouse leaves the frame. */
6710 if (any_help_event_p)
6711 do_help = -1;
6713 #ifdef USE_GTK
6714 /* See comment in EnterNotify above */
6715 else if (last_mouse_glyph_frame)
6716 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6717 #endif
6718 goto OTHER;
6720 case FocusOut:
6721 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6722 goto OTHER;
6724 case MotionNotify:
6726 last_user_time = event.xmotion.time;
6727 previous_help_echo_string = help_echo_string;
6728 help_echo_string = Qnil;
6730 if (dpyinfo->grabbed && last_mouse_frame
6731 && FRAME_LIVE_P (last_mouse_frame))
6732 f = last_mouse_frame;
6733 else
6734 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6736 if (dpyinfo->mouse_face_hidden)
6738 dpyinfo->mouse_face_hidden = 0;
6739 clear_mouse_face (dpyinfo);
6742 #ifdef USE_GTK
6743 if (f && xg_event_is_for_scrollbar (f, &event))
6744 f = 0;
6745 #endif
6746 if (f)
6749 /* Generate SELECT_WINDOW_EVENTs when needed.
6750 Don't let popup menus influence things (bug#1261). */
6751 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6753 Lisp_Object window;
6755 window = window_from_coordinates (f,
6756 event.xmotion.x, event.xmotion.y,
6757 0, 0, 0, 0);
6759 /* Window will be selected only when it is not selected now and
6760 last mouse movement event was not in it. Minibuffer window
6761 will be selected only when it is active. */
6762 if (WINDOWP (window)
6763 && !EQ (window, last_window)
6764 && !EQ (window, selected_window)
6765 /* For click-to-focus window managers
6766 create event iff we don't leave the
6767 selected frame. */
6768 && (focus_follows_mouse
6769 || (EQ (XWINDOW (window)->frame,
6770 XWINDOW (selected_window)->frame))))
6772 inev.ie.kind = SELECT_WINDOW_EVENT;
6773 inev.ie.frame_or_window = window;
6776 last_window=window;
6778 if (!note_mouse_movement (f, &event.xmotion))
6779 help_echo_string = previous_help_echo_string;
6781 else
6783 #ifndef USE_TOOLKIT_SCROLL_BARS
6784 struct scroll_bar *bar
6785 = x_window_to_scroll_bar (event.xmotion.display,
6786 event.xmotion.window);
6788 if (bar)
6789 x_scroll_bar_note_movement (bar, &event);
6790 #endif /* USE_TOOLKIT_SCROLL_BARS */
6792 /* If we move outside the frame, then we're
6793 certainly no longer on any text in the frame. */
6794 clear_mouse_face (dpyinfo);
6797 /* If the contents of the global variable help_echo_string
6798 has changed, generate a HELP_EVENT. */
6799 if (!NILP (help_echo_string)
6800 || !NILP (previous_help_echo_string))
6801 do_help = 1;
6802 goto OTHER;
6805 case ConfigureNotify:
6806 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6807 #ifdef USE_GTK
6808 if (!f
6809 && (f = x_any_window_to_frame (dpyinfo, event.xconfigure.window))
6810 && event.xconfigure.window == FRAME_X_WINDOW (f))
6812 xg_frame_resized (f, event.xconfigure.width,
6813 event.xconfigure.height);
6814 f = 0;
6816 #endif
6817 if (f)
6819 #ifndef USE_X_TOOLKIT
6820 #ifndef USE_GTK
6821 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6822 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6824 /* In the toolkit version, change_frame_size
6825 is called by the code that handles resizing
6826 of the EmacsFrame widget. */
6828 /* Even if the number of character rows and columns has
6829 not changed, the font size may have changed, so we need
6830 to check the pixel dimensions as well. */
6831 if (columns != FRAME_COLS (f)
6832 || rows != FRAME_LINES (f)
6833 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6834 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6836 change_frame_size (f, rows, columns, 0, 1, 0);
6837 SET_FRAME_GARBAGED (f);
6838 cancel_mouse_face (f);
6841 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6842 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6843 #endif /* not USE_GTK */
6844 #endif
6846 #ifdef USE_GTK
6847 /* GTK creates windows but doesn't map them.
6848 Only get real positions when mapped. */
6849 if (FRAME_GTK_OUTER_WIDGET (f)
6850 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6851 #endif
6853 x_real_positions (f, &f->left_pos, &f->top_pos);
6856 #ifdef HAVE_X_I18N
6857 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6858 xic_set_statusarea (f);
6859 #endif
6862 goto OTHER;
6864 case ButtonRelease:
6865 case ButtonPress:
6867 /* If we decide we want to generate an event to be seen
6868 by the rest of Emacs, we put it here. */
6869 int tool_bar_p = 0;
6871 bzero (&compose_status, sizeof (compose_status));
6872 last_mouse_glyph_frame = 0;
6873 last_user_time = event.xbutton.time;
6875 if (dpyinfo->grabbed
6876 && last_mouse_frame
6877 && FRAME_LIVE_P (last_mouse_frame))
6878 f = last_mouse_frame;
6879 else
6880 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6882 #ifdef USE_GTK
6883 if (f && xg_event_is_for_scrollbar (f, &event))
6884 f = 0;
6885 #endif
6886 if (f)
6888 /* Is this in the tool-bar? */
6889 if (WINDOWP (f->tool_bar_window)
6890 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6892 Lisp_Object window;
6893 int x = event.xbutton.x;
6894 int y = event.xbutton.y;
6896 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6897 tool_bar_p = EQ (window, f->tool_bar_window);
6899 if (tool_bar_p && event.xbutton.button < 4)
6901 handle_tool_bar_click (f, x, y,
6902 event.xbutton.type == ButtonPress,
6903 x_x_to_emacs_modifiers (dpyinfo,
6904 event.xbutton.state));
6908 if (!tool_bar_p)
6909 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6910 if (! popup_activated ())
6911 #endif
6913 if (ignore_next_mouse_click_timeout)
6915 if (event.type == ButtonPress
6916 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6918 ignore_next_mouse_click_timeout = 0;
6919 construct_mouse_click (&inev.ie, &event.xbutton, f);
6921 if (event.type == ButtonRelease)
6922 ignore_next_mouse_click_timeout = 0;
6924 else
6925 construct_mouse_click (&inev.ie, &event.xbutton, f);
6927 if (FRAME_X_EMBEDDED_P (f))
6928 xembed_send_message (f, event.xbutton.time,
6929 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6931 else
6933 struct scroll_bar *bar
6934 = x_window_to_scroll_bar (event.xbutton.display,
6935 event.xbutton.window);
6937 #ifdef USE_TOOLKIT_SCROLL_BARS
6938 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6939 scroll bars. */
6940 if (bar && event.xbutton.state & ControlMask)
6942 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6943 *finish = X_EVENT_DROP;
6945 #else /* not USE_TOOLKIT_SCROLL_BARS */
6946 if (bar)
6947 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6948 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6951 if (event.type == ButtonPress)
6953 dpyinfo->grabbed |= (1 << event.xbutton.button);
6954 last_mouse_frame = f;
6956 if (!tool_bar_p)
6957 last_tool_bar_item = -1;
6959 else
6960 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6962 /* Ignore any mouse motion that happened before this event;
6963 any subsequent mouse-movement Emacs events should reflect
6964 only motion after the ButtonPress/Release. */
6965 if (f != 0)
6966 f->mouse_moved = 0;
6968 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6969 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6970 /* For a down-event in the menu bar,
6971 don't pass it to Xt right now.
6972 Instead, save it away
6973 and we will pass it to Xt from kbd_buffer_get_event.
6974 That way, we can run some Lisp code first. */
6975 if (
6976 #ifdef USE_GTK
6977 ! popup_activated ()
6978 /* Gtk+ menus only react to the first three buttons. */
6979 && event.xbutton.button < 3
6981 #endif
6982 f && event.type == ButtonPress
6983 /* Verify the event is really within the menu bar
6984 and not just sent to it due to grabbing. */
6985 && event.xbutton.x >= 0
6986 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6987 && event.xbutton.y >= 0
6988 && event.xbutton.y < f->output_data.x->menubar_height
6989 && event.xbutton.same_screen)
6991 SET_SAVED_BUTTON_EVENT;
6992 XSETFRAME (last_mouse_press_frame, f);
6993 #ifdef USE_GTK
6994 *finish = X_EVENT_DROP;
6995 #endif
6997 else if (event.type == ButtonPress)
6999 last_mouse_press_frame = Qnil;
7000 goto OTHER;
7003 #ifdef USE_MOTIF /* This should do not harm for Lucid,
7004 but I am trying to be cautious. */
7005 else if (event.type == ButtonRelease)
7007 if (!NILP (last_mouse_press_frame))
7009 f = XFRAME (last_mouse_press_frame);
7010 if (f->output_data.x)
7011 SET_SAVED_BUTTON_EVENT;
7013 else
7014 goto OTHER;
7016 #endif /* USE_MOTIF */
7017 else
7018 goto OTHER;
7019 #endif /* USE_X_TOOLKIT || USE_GTK */
7021 break;
7023 case CirculateNotify:
7024 goto OTHER;
7026 case CirculateRequest:
7027 goto OTHER;
7029 case VisibilityNotify:
7030 goto OTHER;
7032 case MappingNotify:
7033 /* Someone has changed the keyboard mapping - update the
7034 local cache. */
7035 switch (event.xmapping.request)
7037 case MappingModifier:
7038 x_find_modifier_meanings (dpyinfo);
7039 /* This is meant to fall through. */
7040 case MappingKeyboard:
7041 XRefreshKeyboardMapping (&event.xmapping);
7043 goto OTHER;
7045 case DestroyNotify:
7046 xft_settings_event (dpyinfo, &event);
7047 break;
7049 default:
7050 OTHER:
7051 #ifdef USE_X_TOOLKIT
7052 BLOCK_INPUT;
7053 if (*finish != X_EVENT_DROP)
7054 XtDispatchEvent (&event);
7055 UNBLOCK_INPUT;
7056 #endif /* USE_X_TOOLKIT */
7057 break;
7060 done:
7061 if (inev.ie.kind != NO_EVENT)
7063 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7064 count++;
7067 if (do_help
7068 && !(hold_quit && hold_quit->kind != NO_EVENT))
7070 Lisp_Object frame;
7072 if (f)
7073 XSETFRAME (frame, f);
7074 else
7075 frame = Qnil;
7077 if (do_help > 0)
7079 any_help_event_p = 1;
7080 gen_help_event (help_echo_string, frame, help_echo_window,
7081 help_echo_object, help_echo_pos);
7083 else
7085 help_echo_string = Qnil;
7086 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7088 count++;
7091 *eventp = event;
7092 return count;
7096 /* Handles the XEvent EVENT on display DISPLAY.
7097 This is used for event loops outside the normal event handling,
7098 i.e. looping while a popup menu or a dialog is posted.
7100 Returns the value handle_one_xevent sets in the finish argument. */
7102 x_dispatch_event (event, display)
7103 XEvent *event;
7104 Display *display;
7106 struct x_display_info *dpyinfo;
7107 int finish = X_EVENT_NORMAL;
7109 dpyinfo = x_display_info_for_display (display);
7111 if (dpyinfo)
7112 handle_one_xevent (dpyinfo, event, &finish, 0);
7114 return finish;
7118 /* Read events coming from the X server.
7119 This routine is called by the SIGIO handler.
7120 We return as soon as there are no more events to be read.
7122 We return the number of characters stored into the buffer,
7123 thus pretending to be `read' (except the characters we store
7124 in the keyboard buffer can be multibyte, so are not necessarily
7125 C chars).
7127 EXPECTED is nonzero if the caller knows input is available. */
7129 static int
7130 XTread_socket (terminal, expected, hold_quit)
7131 struct terminal *terminal;
7132 int expected;
7133 struct input_event *hold_quit;
7135 int count = 0;
7136 XEvent event;
7137 int event_found = 0;
7139 if (interrupt_input_blocked)
7141 interrupt_input_pending = 1;
7142 #ifdef SYNC_INPUT
7143 pending_signals = 1;
7144 #endif
7145 return -1;
7148 interrupt_input_pending = 0;
7149 #ifdef SYNC_INPUT
7150 pending_signals = pending_atimers;
7151 #endif
7152 BLOCK_INPUT;
7154 /* So people can tell when we have read the available input. */
7155 input_signal_count++;
7157 ++handling_signal;
7159 #ifdef HAVE_X_SM
7160 /* Only check session manager input for the primary display. */
7161 if (terminal->id == 1 && x_session_have_connection ())
7163 struct input_event inev;
7164 BLOCK_INPUT;
7165 /* We don't need to EVENT_INIT (inev) here, as
7166 x_session_check_input copies an entire input_event. */
7167 if (x_session_check_input (&inev))
7169 kbd_buffer_store_event_hold (&inev, hold_quit);
7170 count++;
7172 UNBLOCK_INPUT;
7174 #endif
7176 /* For debugging, this gives a way to fake an I/O error. */
7177 if (terminal->display_info.x == XTread_socket_fake_io_error)
7179 XTread_socket_fake_io_error = 0;
7180 x_io_error_quitter (terminal->display_info.x->display);
7183 #ifndef USE_GTK
7184 while (XPending (terminal->display_info.x->display))
7186 int finish;
7188 XNextEvent (terminal->display_info.x->display, &event);
7190 #ifdef HAVE_X_I18N
7191 /* Filter events for the current X input method. */
7192 if (x_filter_event (terminal->display_info.x, &event))
7193 continue;
7194 #endif
7195 event_found = 1;
7197 count += handle_one_xevent (terminal->display_info.x,
7198 &event, &finish, hold_quit);
7200 if (finish == X_EVENT_GOTO_OUT)
7201 goto out;
7204 #else /* USE_GTK */
7206 /* For GTK we must use the GTK event loop. But XEvents gets passed
7207 to our filter function above, and then to the big event switch.
7208 We use a bunch of globals to communicate with our filter function,
7209 that is kind of ugly, but it works.
7211 There is no way to do one display at the time, GTK just does events
7212 from all displays. */
7214 while (gtk_events_pending ())
7216 current_count = count;
7217 current_hold_quit = hold_quit;
7219 gtk_main_iteration ();
7221 count = current_count;
7222 current_count = -1;
7223 current_hold_quit = 0;
7225 if (current_finish == X_EVENT_GOTO_OUT)
7226 break;
7228 #endif /* USE_GTK */
7230 out:;
7232 /* On some systems, an X bug causes Emacs to get no more events
7233 when the window is destroyed. Detect that. (1994.) */
7234 if (! event_found)
7236 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7237 One XNOOP in 100 loops will make Emacs terminate.
7238 B. Bretthauer, 1994 */
7239 x_noop_count++;
7240 if (x_noop_count >= 100)
7242 x_noop_count=0;
7244 if (next_noop_dpyinfo == 0)
7245 next_noop_dpyinfo = x_display_list;
7247 XNoOp (next_noop_dpyinfo->display);
7249 /* Each time we get here, cycle through the displays now open. */
7250 next_noop_dpyinfo = next_noop_dpyinfo->next;
7254 /* If the focus was just given to an auto-raising frame,
7255 raise it now. */
7256 /* ??? This ought to be able to handle more than one such frame. */
7257 if (pending_autoraise_frame)
7259 x_raise_frame (pending_autoraise_frame);
7260 pending_autoraise_frame = 0;
7263 --handling_signal;
7264 UNBLOCK_INPUT;
7266 return count;
7272 /***********************************************************************
7273 Text Cursor
7274 ***********************************************************************/
7276 /* Set clipping for output in glyph row ROW. W is the window in which
7277 we operate. GC is the graphics context to set clipping in.
7279 ROW may be a text row or, e.g., a mode line. Text rows must be
7280 clipped to the interior of the window dedicated to text display,
7281 mode lines must be clipped to the whole window. */
7283 static void
7284 x_clip_to_row (w, row, area, gc)
7285 struct window *w;
7286 struct glyph_row *row;
7287 int area;
7288 GC gc;
7290 struct frame *f = XFRAME (WINDOW_FRAME (w));
7291 XRectangle clip_rect;
7292 int window_x, window_y, window_width;
7294 window_box (w, area, &window_x, &window_y, &window_width, 0);
7296 clip_rect.x = window_x;
7297 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7298 clip_rect.y = max (clip_rect.y, window_y);
7299 clip_rect.width = window_width;
7300 clip_rect.height = row->visible_height;
7302 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7306 /* Draw a hollow box cursor on window W in glyph row ROW. */
7308 static void
7309 x_draw_hollow_cursor (w, row)
7310 struct window *w;
7311 struct glyph_row *row;
7313 struct frame *f = XFRAME (WINDOW_FRAME (w));
7314 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7315 Display *dpy = FRAME_X_DISPLAY (f);
7316 int x, y, wd, h;
7317 XGCValues xgcv;
7318 struct glyph *cursor_glyph;
7319 GC gc;
7321 /* Get the glyph the cursor is on. If we can't tell because
7322 the current matrix is invalid or such, give up. */
7323 cursor_glyph = get_phys_cursor_glyph (w);
7324 if (cursor_glyph == NULL)
7325 return;
7327 /* Compute frame-relative coordinates for phys cursor. */
7328 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7329 wd = w->phys_cursor_width;
7331 /* The foreground of cursor_gc is typically the same as the normal
7332 background color, which can cause the cursor box to be invisible. */
7333 xgcv.foreground = f->output_data.x->cursor_pixel;
7334 if (dpyinfo->scratch_cursor_gc)
7335 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7336 else
7337 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7338 GCForeground, &xgcv);
7339 gc = dpyinfo->scratch_cursor_gc;
7341 /* Set clipping, draw the rectangle, and reset clipping again. */
7342 x_clip_to_row (w, row, TEXT_AREA, gc);
7343 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7344 XSetClipMask (dpy, gc, None);
7348 /* Draw a bar cursor on window W in glyph row ROW.
7350 Implementation note: One would like to draw a bar cursor with an
7351 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7352 Unfortunately, I didn't find a font yet that has this property set.
7353 --gerd. */
7355 static void
7356 x_draw_bar_cursor (w, row, width, kind)
7357 struct window *w;
7358 struct glyph_row *row;
7359 int width;
7360 enum text_cursor_kinds kind;
7362 struct frame *f = XFRAME (w->frame);
7363 struct glyph *cursor_glyph;
7365 /* If cursor is out of bounds, don't draw garbage. This can happen
7366 in mini-buffer windows when switching between echo area glyphs
7367 and mini-buffer. */
7368 cursor_glyph = get_phys_cursor_glyph (w);
7369 if (cursor_glyph == NULL)
7370 return;
7372 /* If on an image, draw like a normal cursor. That's usually better
7373 visible than drawing a bar, esp. if the image is large so that
7374 the bar might not be in the window. */
7375 if (cursor_glyph->type == IMAGE_GLYPH)
7377 struct glyph_row *row;
7378 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7379 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7381 else
7383 Display *dpy = FRAME_X_DISPLAY (f);
7384 Window window = FRAME_X_WINDOW (f);
7385 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7386 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7387 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7388 XGCValues xgcv;
7390 /* If the glyph's background equals the color we normally draw
7391 the bars cursor in, the bar cursor in its normal color is
7392 invisible. Use the glyph's foreground color instead in this
7393 case, on the assumption that the glyph's colors are chosen so
7394 that the glyph is legible. */
7395 if (face->background == f->output_data.x->cursor_pixel)
7396 xgcv.background = xgcv.foreground = face->foreground;
7397 else
7398 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7399 xgcv.graphics_exposures = 0;
7401 if (gc)
7402 XChangeGC (dpy, gc, mask, &xgcv);
7403 else
7405 gc = XCreateGC (dpy, window, mask, &xgcv);
7406 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7409 x_clip_to_row (w, row, TEXT_AREA, gc);
7411 if (kind == BAR_CURSOR)
7413 if (width < 0)
7414 width = FRAME_CURSOR_WIDTH (f);
7415 width = min (cursor_glyph->pixel_width, width);
7417 w->phys_cursor_width = width;
7419 XFillRectangle (dpy, window, gc,
7420 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7421 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7422 width, row->height);
7424 else
7426 int dummy_x, dummy_y, dummy_h;
7428 if (width < 0)
7429 width = row->height;
7431 width = min (row->height, width);
7433 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
7434 &dummy_y, &dummy_h);
7436 XFillRectangle (dpy, window, gc,
7437 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7438 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7439 row->height - width),
7440 w->phys_cursor_width, width);
7443 XSetClipMask (dpy, gc, None);
7448 /* RIF: Define cursor CURSOR on frame F. */
7450 static void
7451 x_define_frame_cursor (f, cursor)
7452 struct frame *f;
7453 Cursor cursor;
7455 if (!f->pointer_invisible
7456 && f->output_data.x->current_cursor != cursor)
7457 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7458 f->output_data.x->current_cursor = cursor;
7462 /* RIF: Clear area on frame F. */
7464 static void
7465 x_clear_frame_area (f, x, y, width, height)
7466 struct frame *f;
7467 int x, y, width, height;
7469 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7470 x, y, width, height, False);
7471 #ifdef USE_GTK
7472 /* Must queue a redraw, because scroll bars might have been cleared. */
7473 if (FRAME_GTK_WIDGET (f))
7474 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
7475 #endif
7479 /* RIF: Draw cursor on window W. */
7481 static void
7482 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7483 struct window *w;
7484 struct glyph_row *glyph_row;
7485 int x, y;
7486 int cursor_type, cursor_width;
7487 int on_p, active_p;
7489 struct frame *f = XFRAME (WINDOW_FRAME (w));
7491 if (on_p)
7493 w->phys_cursor_type = cursor_type;
7494 w->phys_cursor_on_p = 1;
7496 if (glyph_row->exact_window_width_line_p
7497 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7499 glyph_row->cursor_in_fringe_p = 1;
7500 draw_fringe_bitmap (w, glyph_row, 0);
7502 else
7503 switch (cursor_type)
7505 case HOLLOW_BOX_CURSOR:
7506 x_draw_hollow_cursor (w, glyph_row);
7507 break;
7509 case FILLED_BOX_CURSOR:
7510 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7511 break;
7513 case BAR_CURSOR:
7514 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7515 break;
7517 case HBAR_CURSOR:
7518 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7519 break;
7521 case NO_CURSOR:
7522 w->phys_cursor_width = 0;
7523 break;
7525 default:
7526 abort ();
7529 #ifdef HAVE_X_I18N
7530 if (w == XWINDOW (f->selected_window))
7531 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7532 xic_set_preeditarea (w, x, y);
7533 #endif
7536 #ifndef XFlush
7537 XFlush (FRAME_X_DISPLAY (f));
7538 #endif
7542 /* Icons. */
7544 /* Make the x-window of frame F use the gnu icon bitmap. */
7547 x_bitmap_icon (f, file)
7548 struct frame *f;
7549 Lisp_Object file;
7551 int bitmap_id;
7553 if (FRAME_X_WINDOW (f) == 0)
7554 return 1;
7556 /* Free up our existing icon bitmap and mask if any. */
7557 if (f->output_data.x->icon_bitmap > 0)
7558 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7559 f->output_data.x->icon_bitmap = 0;
7561 if (STRINGP (file))
7563 #ifdef USE_GTK
7564 /* Use gtk_window_set_icon_from_file () if available,
7565 It's not restricted to bitmaps */
7566 if (xg_set_icon (f, file))
7567 return 0;
7568 #endif /* USE_GTK */
7569 bitmap_id = x_create_bitmap_from_file (f, file);
7570 x_create_bitmap_mask (f, bitmap_id);
7572 else
7574 /* Create the GNU bitmap and mask if necessary. */
7575 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7577 int rc = -1;
7579 #ifdef USE_GTK
7581 if (xg_set_icon (f, xg_default_icon_file)
7582 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7583 return 0;
7585 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7587 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7588 if (rc != -1)
7589 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7591 #endif
7593 /* If all else fails, use the (black and white) xbm image. */
7594 if (rc == -1)
7596 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7597 gnu_xbm_width, gnu_xbm_height);
7598 if (rc == -1)
7599 return 1;
7601 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7602 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7606 /* The first time we create the GNU bitmap and mask,
7607 this increments the ref-count one extra time.
7608 As a result, the GNU bitmap and mask are never freed.
7609 That way, we don't have to worry about allocating it again. */
7610 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7612 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7615 x_wm_set_icon_pixmap (f, bitmap_id);
7616 f->output_data.x->icon_bitmap = bitmap_id;
7618 return 0;
7622 /* Make the x-window of frame F use a rectangle with text.
7623 Use ICON_NAME as the text. */
7626 x_text_icon (f, icon_name)
7627 struct frame *f;
7628 char *icon_name;
7630 if (FRAME_X_WINDOW (f) == 0)
7631 return 1;
7634 XTextProperty text;
7635 text.value = (unsigned char *) icon_name;
7636 text.encoding = XA_STRING;
7637 text.format = 8;
7638 text.nitems = strlen (icon_name);
7639 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7642 if (f->output_data.x->icon_bitmap > 0)
7643 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7644 f->output_data.x->icon_bitmap = 0;
7645 x_wm_set_icon_pixmap (f, 0);
7647 return 0;
7650 #define X_ERROR_MESSAGE_SIZE 200
7652 /* If non-nil, this should be a string.
7653 It means catch X errors and store the error message in this string.
7655 The reason we use a stack is that x_catch_error/x_uncatch_error can
7656 be called from a signal handler.
7659 struct x_error_message_stack {
7660 char string[X_ERROR_MESSAGE_SIZE];
7661 Display *dpy;
7662 struct x_error_message_stack *prev;
7664 static struct x_error_message_stack *x_error_message;
7666 /* An X error handler which stores the error message in
7667 *x_error_message. This is called from x_error_handler if
7668 x_catch_errors is in effect. */
7670 static void
7671 x_error_catcher (display, error)
7672 Display *display;
7673 XErrorEvent *error;
7675 XGetErrorText (display, error->error_code,
7676 x_error_message->string,
7677 X_ERROR_MESSAGE_SIZE);
7680 /* Begin trapping X errors for display DPY. Actually we trap X errors
7681 for all displays, but DPY should be the display you are actually
7682 operating on.
7684 After calling this function, X protocol errors no longer cause
7685 Emacs to exit; instead, they are recorded in the string
7686 stored in *x_error_message.
7688 Calling x_check_errors signals an Emacs error if an X error has
7689 occurred since the last call to x_catch_errors or x_check_errors.
7691 Calling x_uncatch_errors resumes the normal error handling. */
7693 void x_check_errors ();
7695 void
7696 x_catch_errors (dpy)
7697 Display *dpy;
7699 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7701 /* Make sure any errors from previous requests have been dealt with. */
7702 XSync (dpy, False);
7704 data->dpy = dpy;
7705 data->string[0] = 0;
7706 data->prev = x_error_message;
7707 x_error_message = data;
7710 /* Undo the last x_catch_errors call.
7711 DPY should be the display that was passed to x_catch_errors. */
7713 void
7714 x_uncatch_errors ()
7716 struct x_error_message_stack *tmp;
7718 BLOCK_INPUT;
7720 /* The display may have been closed before this function is called.
7721 Check if it is still open before calling XSync. */
7722 if (x_display_info_for_display (x_error_message->dpy) != 0)
7723 XSync (x_error_message->dpy, False);
7725 tmp = x_error_message;
7726 x_error_message = x_error_message->prev;
7727 xfree (tmp);
7728 UNBLOCK_INPUT;
7731 /* If any X protocol errors have arrived since the last call to
7732 x_catch_errors or x_check_errors, signal an Emacs error using
7733 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7735 void
7736 x_check_errors (dpy, format)
7737 Display *dpy;
7738 char *format;
7740 /* Make sure to catch any errors incurred so far. */
7741 XSync (dpy, False);
7743 if (x_error_message->string[0])
7745 char string[X_ERROR_MESSAGE_SIZE];
7746 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7747 x_uncatch_errors ();
7748 error (format, string);
7752 /* Nonzero if we had any X protocol errors
7753 since we did x_catch_errors on DPY. */
7756 x_had_errors_p (dpy)
7757 Display *dpy;
7759 /* Make sure to catch any errors incurred so far. */
7760 XSync (dpy, False);
7762 return x_error_message->string[0] != 0;
7765 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7767 void
7768 x_clear_errors (dpy)
7769 Display *dpy;
7771 x_error_message->string[0] = 0;
7774 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7775 * idea. --lorentey */
7776 /* Close off all unclosed x_catch_errors calls. */
7778 void
7779 x_fully_uncatch_errors ()
7781 while (x_error_message)
7782 x_uncatch_errors ();
7784 #endif
7786 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7789 x_catching_errors ()
7791 return x_error_message != 0;
7794 #if 0
7795 static unsigned int x_wire_count;
7796 x_trace_wire ()
7798 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7800 #endif /* ! 0 */
7803 /* Handle SIGPIPE, which can happen when the connection to a server
7804 simply goes away. SIGPIPE is handled by x_connection_signal.
7805 Don't need to do anything, because the write which caused the
7806 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7807 which will do the appropriate cleanup for us. */
7809 static SIGTYPE
7810 x_connection_signal (signalnum) /* If we don't have an argument, */
7811 int signalnum; /* some compilers complain in signal calls. */
7813 #ifdef USG
7814 /* USG systems forget handlers when they are used;
7815 must reestablish each time */
7816 signal (signalnum, x_connection_signal);
7817 #endif /* USG */
7821 /************************************************************************
7822 Handling X errors
7823 ************************************************************************/
7825 /* Error message passed to x_connection_closed. */
7827 static char *error_msg;
7829 /* Function installed as fatal_error_signal_hook in
7830 x_connection_closed. Print the X error message, and exit normally,
7831 instead of dumping core when XtCloseDisplay fails. */
7833 static void
7834 x_fatal_error_signal ()
7836 fprintf (stderr, "%s\n", error_msg);
7837 exit (70);
7840 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7841 the text of an error message that lead to the connection loss. */
7843 static SIGTYPE
7844 x_connection_closed (dpy, error_message)
7845 Display *dpy;
7846 char *error_message;
7848 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7849 Lisp_Object frame, tail;
7850 int index = SPECPDL_INDEX ();
7852 error_msg = (char *) alloca (strlen (error_message) + 1);
7853 strcpy (error_msg, error_message);
7854 handling_signal = 0;
7856 /* Prevent being called recursively because of an error condition
7857 below. Otherwise, we might end up with printing ``can't find per
7858 display information'' in the recursive call instead of printing
7859 the original message here. */
7860 x_catch_errors (dpy);
7862 /* Inhibit redisplay while frames are being deleted. */
7863 specbind (Qinhibit_redisplay, Qt);
7865 if (dpyinfo)
7867 /* Protect display from being closed when we delete the last
7868 frame on it. */
7869 dpyinfo->reference_count++;
7870 dpyinfo->terminal->reference_count++;
7873 /* First delete frames whose mini-buffers are on frames
7874 that are on the dead display. */
7875 FOR_EACH_FRAME (tail, frame)
7877 Lisp_Object minibuf_frame;
7878 minibuf_frame
7879 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7880 if (FRAME_X_P (XFRAME (frame))
7881 && FRAME_X_P (XFRAME (minibuf_frame))
7882 && ! EQ (frame, minibuf_frame)
7883 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7884 delete_frame (frame, Qnoelisp);
7887 /* Now delete all remaining frames on the dead display.
7888 We are now sure none of these is used as the mini-buffer
7889 for another frame that we need to delete. */
7890 FOR_EACH_FRAME (tail, frame)
7891 if (FRAME_X_P (XFRAME (frame))
7892 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7894 /* Set this to t so that delete_frame won't get confused
7895 trying to find a replacement. */
7896 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7897 delete_frame (frame, Qnoelisp);
7900 /* We have to close the display to inform Xt that it doesn't
7901 exist anymore. If we don't, Xt will continue to wait for
7902 events from the display. As a consequence, a sequence of
7904 M-x make-frame-on-display RET :1 RET
7905 ...kill the new frame, so that we get an IO error...
7906 M-x make-frame-on-display RET :1 RET
7908 will indefinitely wait in Xt for events for display `:1', opened
7909 in the first call to make-frame-on-display.
7911 Closing the display is reported to lead to a bus error on
7912 OpenWindows in certain situations. I suspect that is a bug
7913 in OpenWindows. I don't know how to circumvent it here. */
7915 if (dpyinfo)
7917 #ifdef USE_X_TOOLKIT
7918 /* If DPYINFO is null, this means we didn't open the display
7919 in the first place, so don't try to close it. */
7921 extern void (*fatal_error_signal_hook) P_ ((void));
7922 fatal_error_signal_hook = x_fatal_error_signal;
7923 XtCloseDisplay (dpy);
7924 fatal_error_signal_hook = NULL;
7926 #endif
7928 #ifdef USE_GTK
7929 /* Due to bugs in some Gtk+ versions, just exit here if this
7930 is the last display/terminal. */
7931 if (terminal_list->next_terminal == NULL)
7933 fprintf (stderr, "%s\n", error_msg);
7934 shut_down_emacs (0, 0, Qnil);
7935 exit (70);
7937 xg_display_close (dpyinfo->display);
7938 #endif
7940 /* Indicate that this display is dead. */
7941 dpyinfo->display = 0;
7943 dpyinfo->reference_count--;
7944 dpyinfo->terminal->reference_count--;
7945 if (dpyinfo->reference_count != 0)
7946 /* We have just closed all frames on this display. */
7947 abort ();
7950 Lisp_Object tmp;
7951 XSETTERMINAL (tmp, dpyinfo->terminal);
7952 Fdelete_terminal (tmp, Qnoelisp);
7956 x_uncatch_errors ();
7958 if (terminal_list == 0)
7960 fprintf (stderr, "%s\n", error_msg);
7961 shut_down_emacs (0, 0, Qnil);
7962 exit (70);
7965 /* Ordinary stack unwind doesn't deal with these. */
7966 #ifdef SIGIO
7967 sigunblock (sigmask (SIGIO));
7968 #endif
7969 sigunblock (sigmask (SIGALRM));
7970 TOTALLY_UNBLOCK_INPUT;
7972 unbind_to (index, Qnil);
7973 clear_waiting_for_input ();
7974 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7975 longjmp), because returning from this function would get us back into
7976 Xlib's code which will directly call `exit'. */
7977 error ("%s", error_msg);
7980 /* We specifically use it before defining it, so that gcc doesn't inline it,
7981 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7982 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7984 /* This is the first-level handler for X protocol errors.
7985 It calls x_error_quitter or x_error_catcher. */
7987 static int
7988 x_error_handler (display, error)
7989 Display *display;
7990 XErrorEvent *error;
7992 if (x_error_message)
7993 x_error_catcher (display, error);
7994 else
7995 x_error_quitter (display, error);
7996 return 0;
7999 /* This is the usual handler for X protocol errors.
8000 It kills all frames on the display that we got the error for.
8001 If that was the only one, it prints an error message and kills Emacs. */
8003 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
8005 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
8006 #define NO_INLINE __attribute__((noinline))
8007 #else
8008 #define NO_INLINE
8009 #endif
8011 /* Some versions of GNU/Linux define noinline in their headers. */
8013 #ifdef noinline
8014 #undef noinline
8015 #endif
8017 /* On older GCC versions, just putting x_error_quitter
8018 after x_error_handler prevents inlining into the former. */
8020 static void NO_INLINE
8021 x_error_quitter (display, error)
8022 Display *display;
8023 XErrorEvent *error;
8025 char buf[256], buf1[356];
8027 /* Ignore BadName errors. They can happen because of fonts
8028 or colors that are not defined. */
8030 if (error->error_code == BadName)
8031 return;
8033 /* Note that there is no real way portable across R3/R4 to get the
8034 original error handler. */
8036 XGetErrorText (display, error->error_code, buf, sizeof (buf));
8037 sprintf (buf1, "X protocol error: %s on protocol request %d",
8038 buf, error->request_code);
8039 x_connection_closed (display, buf1);
8043 /* This is the handler for X IO errors, always.
8044 It kills all frames on the display that we lost touch with.
8045 If that was the only one, it prints an error message and kills Emacs. */
8047 static int
8048 x_io_error_quitter (display)
8049 Display *display;
8051 char buf[256];
8053 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
8054 x_connection_closed (display, buf);
8055 return 0;
8058 /* Changing the font of the frame. */
8060 /* Give frame F the font FONT-OBJECT as its default font. The return
8061 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
8062 frame. If it is negative, generate a new fontset from
8063 FONT-OBJECT. */
8065 Lisp_Object
8066 x_new_font (f, font_object, fontset)
8067 struct frame *f;
8068 Lisp_Object font_object;
8069 int fontset;
8071 struct font *font = XFONT_OBJECT (font_object);
8073 if (fontset < 0)
8074 fontset = fontset_from_font (font_object);
8075 FRAME_FONTSET (f) = fontset;
8076 if (FRAME_FONT (f) == font)
8077 /* This font is already set in frame F. There's nothing more to
8078 do. */
8079 return font_object;
8081 FRAME_FONT (f) = font;
8082 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8083 FRAME_COLUMN_WIDTH (f) = font->average_width;
8084 FRAME_SPACE_WIDTH (f) = font->space_width;
8085 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8087 compute_fringe_widths (f, 1);
8089 /* Compute the scroll bar width in character columns. */
8090 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8092 int wid = FRAME_COLUMN_WIDTH (f);
8093 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8094 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8096 else
8098 int wid = FRAME_COLUMN_WIDTH (f);
8099 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8102 if (FRAME_X_WINDOW (f) != 0)
8104 /* Don't change the size of a tip frame; there's no point in
8105 doing it because it's done in Fx_show_tip, and it leads to
8106 problems because the tip frame has no widget. */
8107 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8108 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8111 #ifdef HAVE_X_I18N
8112 if (FRAME_XIC (f)
8113 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8115 BLOCK_INPUT;
8116 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8117 UNBLOCK_INPUT;
8119 #endif
8121 return font_object;
8125 /***********************************************************************
8126 X Input Methods
8127 ***********************************************************************/
8129 #ifdef HAVE_X_I18N
8131 #ifdef HAVE_X11R6
8133 /* XIM destroy callback function, which is called whenever the
8134 connection to input method XIM dies. CLIENT_DATA contains a
8135 pointer to the x_display_info structure corresponding to XIM. */
8137 static void
8138 xim_destroy_callback (xim, client_data, call_data)
8139 XIM xim;
8140 XPointer client_data;
8141 XPointer call_data;
8143 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8144 Lisp_Object frame, tail;
8146 BLOCK_INPUT;
8148 /* No need to call XDestroyIC.. */
8149 FOR_EACH_FRAME (tail, frame)
8151 struct frame *f = XFRAME (frame);
8152 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8154 FRAME_XIC (f) = NULL;
8155 xic_free_xfontset (f);
8159 /* No need to call XCloseIM. */
8160 dpyinfo->xim = NULL;
8161 XFree (dpyinfo->xim_styles);
8162 UNBLOCK_INPUT;
8165 #endif /* HAVE_X11R6 */
8167 #ifdef HAVE_X11R6
8168 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8169 extern char *XSetIMValues P_ ((XIM, ...));
8170 #endif
8172 /* Open the connection to the XIM server on display DPYINFO.
8173 RESOURCE_NAME is the resource name Emacs uses. */
8175 static void
8176 xim_open_dpy (dpyinfo, resource_name)
8177 struct x_display_info *dpyinfo;
8178 char *resource_name;
8180 XIM xim;
8182 #ifdef HAVE_XIM
8183 if (use_xim)
8185 if (dpyinfo->xim)
8186 XCloseIM (dpyinfo->xim);
8187 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8188 EMACS_CLASS);
8189 dpyinfo->xim = xim;
8191 if (xim)
8193 #ifdef HAVE_X11R6
8194 XIMCallback destroy;
8195 #endif
8197 /* Get supported styles and XIM values. */
8198 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8200 #ifdef HAVE_X11R6
8201 destroy.callback = xim_destroy_callback;
8202 destroy.client_data = (XPointer)dpyinfo;
8203 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8204 #endif
8208 else
8209 #endif /* HAVE_XIM */
8210 dpyinfo->xim = NULL;
8214 #ifdef HAVE_X11R6_XIM
8216 /* XIM instantiate callback function, which is called whenever an XIM
8217 server is available. DISPLAY is the display of the XIM.
8218 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8219 when the callback was registered. */
8221 static void
8222 xim_instantiate_callback (display, client_data, call_data)
8223 Display *display;
8224 XPointer client_data;
8225 XPointer call_data;
8227 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8228 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8230 /* We don't support multiple XIM connections. */
8231 if (dpyinfo->xim)
8232 return;
8234 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8236 /* Create XIC for the existing frames on the same display, as long
8237 as they have no XIC. */
8238 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8240 Lisp_Object tail, frame;
8242 BLOCK_INPUT;
8243 FOR_EACH_FRAME (tail, frame)
8245 struct frame *f = XFRAME (frame);
8247 if (FRAME_X_P (f)
8248 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8249 if (FRAME_XIC (f) == NULL)
8251 create_frame_xic (f);
8252 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8253 xic_set_statusarea (f);
8254 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8256 struct window *w = XWINDOW (f->selected_window);
8257 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8262 UNBLOCK_INPUT;
8266 #endif /* HAVE_X11R6_XIM */
8269 /* Open a connection to the XIM server on display DPYINFO.
8270 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8271 connection only at the first time. On X11R6, open the connection
8272 in the XIM instantiate callback function. */
8274 static void
8275 xim_initialize (dpyinfo, resource_name)
8276 struct x_display_info *dpyinfo;
8277 char *resource_name;
8279 dpyinfo->xim = NULL;
8280 #ifdef HAVE_XIM
8281 if (use_xim)
8283 #ifdef HAVE_X11R6_XIM
8284 struct xim_inst_t *xim_inst;
8285 int len;
8287 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8288 dpyinfo->xim_callback_data = xim_inst;
8289 xim_inst->dpyinfo = dpyinfo;
8290 len = strlen (resource_name);
8291 xim_inst->resource_name = (char *) xmalloc (len + 1);
8292 bcopy (resource_name, xim_inst->resource_name, len + 1);
8293 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8294 resource_name, EMACS_CLASS,
8295 xim_instantiate_callback,
8296 /* This is XPointer in XFree86
8297 but (XPointer *) on Tru64, at
8298 least, hence the configure test. */
8299 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8300 #else /* not HAVE_X11R6_XIM */
8301 xim_open_dpy (dpyinfo, resource_name);
8302 #endif /* not HAVE_X11R6_XIM */
8304 #endif /* HAVE_XIM */
8308 /* Close the connection to the XIM server on display DPYINFO. */
8310 static void
8311 xim_close_dpy (dpyinfo)
8312 struct x_display_info *dpyinfo;
8314 #ifdef HAVE_XIM
8315 if (use_xim)
8317 #ifdef HAVE_X11R6_XIM
8318 if (dpyinfo->display)
8319 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8320 NULL, EMACS_CLASS,
8321 xim_instantiate_callback, NULL);
8322 xfree (dpyinfo->xim_callback_data->resource_name);
8323 xfree (dpyinfo->xim_callback_data);
8324 #endif /* HAVE_X11R6_XIM */
8325 if (dpyinfo->display)
8326 XCloseIM (dpyinfo->xim);
8327 dpyinfo->xim = NULL;
8328 XFree (dpyinfo->xim_styles);
8330 #endif /* HAVE_XIM */
8333 #endif /* not HAVE_X11R6_XIM */
8337 /* Calculate the absolute position in frame F
8338 from its current recorded position values and gravity. */
8340 void
8341 x_calc_absolute_position (f)
8342 struct frame *f;
8344 int flags = f->size_hint_flags;
8346 /* We have nothing to do if the current position
8347 is already for the top-left corner. */
8348 if (! ((flags & XNegative) || (flags & YNegative)))
8349 return;
8351 /* Treat negative positions as relative to the leftmost bottommost
8352 position that fits on the screen. */
8353 if (flags & XNegative)
8354 f->left_pos = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
8355 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8358 int height = FRAME_PIXEL_HEIGHT (f);
8360 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8361 /* Something is fishy here. When using Motif, starting Emacs with
8362 `-g -0-0', the frame appears too low by a few pixels.
8364 This seems to be so because initially, while Emacs is starting,
8365 the column widget's height and the frame's pixel height are
8366 different. The column widget's height is the right one. In
8367 later invocations, when Emacs is up, the frame's pixel height
8368 is right, though.
8370 It's not obvious where the initial small difference comes from.
8371 2000-12-01, gerd. */
8373 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8374 #endif
8376 if (flags & YNegative)
8377 f->top_pos = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
8378 - height + f->top_pos;
8381 /* The left_pos and top_pos
8382 are now relative to the top and left screen edges,
8383 so the flags should correspond. */
8384 f->size_hint_flags &= ~ (XNegative | YNegative);
8387 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8388 to really change the position, and 0 when calling from
8389 x_make_frame_visible (in that case, XOFF and YOFF are the current
8390 position values). It is -1 when calling from x_set_frame_parameters,
8391 which means, do adjust for borders but don't change the gravity. */
8393 void
8394 x_set_offset (f, xoff, yoff, change_gravity)
8395 struct frame *f;
8396 register int xoff, yoff;
8397 int change_gravity;
8399 int modified_top, modified_left;
8401 if (change_gravity > 0)
8403 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8404 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8406 f->top_pos = yoff;
8407 f->left_pos = xoff;
8408 f->size_hint_flags &= ~ (XNegative | YNegative);
8409 if (xoff < 0)
8410 f->size_hint_flags |= XNegative;
8411 if (yoff < 0)
8412 f->size_hint_flags |= YNegative;
8413 f->win_gravity = NorthWestGravity;
8415 x_calc_absolute_position (f);
8417 BLOCK_INPUT;
8418 x_wm_set_size_hint (f, (long) 0, 0);
8420 modified_left = f->left_pos;
8421 modified_top = f->top_pos;
8423 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8425 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8426 than the WM decorations. So we use the calculated offset instead
8427 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8428 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8429 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8432 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8433 modified_left, modified_top);
8435 x_sync_with_move (f, f->left_pos, f->top_pos,
8436 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8437 ? 1 : 0);
8439 /* change_gravity is non-zero when this function is called from Lisp to
8440 programmatically move a frame. In that case, we call
8441 x_check_expected_move to discover if we have a "Type A" or "Type B"
8442 window manager, and, for a "Type A" window manager, adjust the position
8443 of the frame.
8445 We call x_check_expected_move if a programmatic move occurred, and
8446 either the window manager type (A/B) is unknown or it is Type A but we
8447 need to compute the top/left offset adjustment for this frame. */
8449 if (change_gravity != 0 &&
8450 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8451 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8452 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8453 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8454 x_check_expected_move (f, modified_left, modified_top);
8456 UNBLOCK_INPUT;
8459 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8460 on the root window for frame F contains ATOMNAME.
8461 This is how a WM check shall be done according to the Window Manager
8462 Specification/Extended Window Manager Hints at
8463 http://freedesktop.org/wiki/Specifications/wm-spec. */
8465 static int
8466 wm_supports (f, atomname)
8467 struct frame *f;
8468 const char *atomname;
8470 Atom actual_type;
8471 unsigned long actual_size, bytes_remaining;
8472 int i, rc, actual_format;
8473 Atom prop_atom;
8474 Window wmcheck_window;
8475 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8476 Window target_window = dpyinfo->root_window;
8477 long max_len = 65536;
8478 Display *dpy = FRAME_X_DISPLAY (f);
8479 unsigned char *tmp_data = NULL;
8480 Atom target_type = XA_WINDOW;
8481 Atom want_atom;
8483 BLOCK_INPUT;
8485 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8487 x_catch_errors (dpy);
8488 rc = XGetWindowProperty (dpy, target_window,
8489 prop_atom, 0, max_len, False, target_type,
8490 &actual_type, &actual_format, &actual_size,
8491 &bytes_remaining, &tmp_data);
8493 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8495 if (tmp_data) XFree (tmp_data);
8496 x_uncatch_errors ();
8497 UNBLOCK_INPUT;
8498 return 0;
8501 wmcheck_window = *(Window *) tmp_data;
8502 XFree (tmp_data);
8504 /* Check if window exists. */
8505 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8506 x_sync (f);
8507 if (x_had_errors_p (dpy))
8509 x_uncatch_errors ();
8510 UNBLOCK_INPUT;
8511 return 0;
8514 if (dpyinfo->net_supported_window != wmcheck_window)
8516 /* Window changed, reload atoms */
8517 if (dpyinfo->net_supported_atoms != NULL)
8518 XFree (dpyinfo->net_supported_atoms);
8519 dpyinfo->net_supported_atoms = NULL;
8520 dpyinfo->nr_net_supported_atoms = 0;
8521 dpyinfo->net_supported_window = 0;
8523 target_type = XA_ATOM;
8524 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8525 tmp_data = NULL;
8526 rc = XGetWindowProperty (dpy, target_window,
8527 prop_atom, 0, max_len, False, target_type,
8528 &actual_type, &actual_format, &actual_size,
8529 &bytes_remaining, &tmp_data);
8531 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8533 if (tmp_data) XFree (tmp_data);
8534 x_uncatch_errors ();
8535 UNBLOCK_INPUT;
8536 return 0;
8539 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8540 dpyinfo->nr_net_supported_atoms = actual_size;
8541 dpyinfo->net_supported_window = wmcheck_window;
8544 rc = 0;
8545 want_atom = XInternAtom (dpy, atomname, False);
8547 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8548 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8550 x_uncatch_errors ();
8551 UNBLOCK_INPUT;
8553 return rc;
8556 static void
8557 set_wm_state (frame, add, what, what2)
8558 Lisp_Object frame;
8559 int add;
8560 const char *what;
8561 const char *what2;
8563 const char *atom = "_NET_WM_STATE";
8564 Fx_send_client_event (frame, make_number (0), frame,
8565 make_unibyte_string (atom, strlen (atom)),
8566 make_number (32),
8567 /* 1 = add, 0 = remove */
8568 Fcons
8569 (make_number (add ? 1 : 0),
8570 Fcons
8571 (make_unibyte_string (what, strlen (what)),
8572 what2 != 0
8573 ? Fcons (make_unibyte_string (what2, strlen (what2)),
8574 Qnil)
8575 : Qnil)));
8578 void
8579 x_set_sticky (f, new_value, old_value)
8580 struct frame *f;
8581 Lisp_Object new_value, old_value;
8583 Lisp_Object frame;
8585 XSETFRAME (frame, f);
8586 set_wm_state (frame, NILP (new_value) ? 0 : 1,
8587 "_NET_WM_STATE_STICKY", NULL);
8590 /* Do fullscreen as specified in extended window manager hints */
8592 static int
8593 do_ewmh_fullscreen (f)
8594 struct frame *f;
8596 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8598 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8599 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8600 if (!have_net_atom)
8601 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8603 if (have_net_atom)
8605 Lisp_Object frame;
8606 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8607 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8608 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8610 XSETFRAME (frame, f);
8612 set_wm_state (frame, 0, fs, NULL);
8613 set_wm_state (frame, 0, fh, NULL);
8614 set_wm_state (frame, 0, fw, NULL);
8616 /* If there are _NET_ atoms we assume we have extended window manager
8617 hints. */
8618 switch (f->want_fullscreen)
8620 case FULLSCREEN_BOTH:
8621 set_wm_state (frame, 1, fs, NULL);
8622 break;
8623 case FULLSCREEN_WIDTH:
8624 set_wm_state (frame, 1, fw, NULL);
8625 break;
8626 case FULLSCREEN_HEIGHT:
8627 set_wm_state (frame, 1, fh, NULL);
8628 break;
8629 case FULLSCREEN_MAXIMIZED:
8630 set_wm_state (frame, 1, fw, fh);
8631 break;
8634 f->want_fullscreen = FULLSCREEN_NONE;
8638 return have_net_atom;
8641 static void
8642 XTfullscreen_hook (f)
8643 FRAME_PTR f;
8645 if (f->async_visible)
8647 BLOCK_INPUT;
8648 x_check_fullscreen (f);
8649 x_sync (f);
8650 UNBLOCK_INPUT;
8655 static void
8656 x_handle_net_wm_state (f, event)
8657 struct frame *f;
8658 XPropertyEvent *event;
8660 Atom actual_type;
8661 unsigned long actual_size, bytes_remaining;
8662 int i, rc, actual_format, value = FULLSCREEN_NONE;
8663 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8664 long max_len = 65536;
8665 Display *dpy = FRAME_X_DISPLAY (f);
8666 unsigned char *tmp_data = NULL;
8667 Atom target_type = XA_ATOM;
8668 Lisp_Object lval;
8669 int sticky = 0;
8671 BLOCK_INPUT;
8672 x_catch_errors (dpy);
8673 rc = XGetWindowProperty (dpy, event->window,
8674 event->atom, 0, max_len, False, target_type,
8675 &actual_type, &actual_format, &actual_size,
8676 &bytes_remaining, &tmp_data);
8678 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
8680 if (tmp_data) XFree (tmp_data);
8681 x_uncatch_errors ();
8682 UNBLOCK_INPUT;
8683 return;
8686 x_uncatch_errors ();
8688 for (i = 0; i < actual_size; ++i)
8690 Atom a = ((Atom*)tmp_data)[i];
8691 if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
8693 if (value == FULLSCREEN_HEIGHT)
8694 value = FULLSCREEN_MAXIMIZED;
8695 else
8696 value = FULLSCREEN_WIDTH;
8698 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
8700 if (value == FULLSCREEN_WIDTH)
8701 value = FULLSCREEN_MAXIMIZED;
8702 else
8703 value = FULLSCREEN_HEIGHT;
8705 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen_atom)
8706 value = FULLSCREEN_BOTH;
8707 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
8708 sticky = 1;
8711 lval = Qnil;
8712 switch (value)
8714 case FULLSCREEN_WIDTH:
8715 lval = Qfullwidth;
8716 break;
8717 case FULLSCREEN_HEIGHT:
8718 lval = Qfullheight;
8719 break;
8720 case FULLSCREEN_BOTH:
8721 lval = Qfullboth;
8722 break;
8723 case FULLSCREEN_MAXIMIZED:
8724 lval = Qmaximized;
8725 break;
8728 store_frame_param (f, Qfullscreen, lval);
8729 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
8731 if (tmp_data) XFree (tmp_data);
8732 UNBLOCK_INPUT;
8735 /* Check if we need to resize the frame due to a fullscreen request.
8736 If so needed, resize the frame. */
8737 static void
8738 x_check_fullscreen (f)
8739 struct frame *f;
8741 if (do_ewmh_fullscreen (f))
8742 return;
8744 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8745 return; /* Only fullscreen without WM or with EWM hints (above). */
8747 if (f->want_fullscreen != FULLSCREEN_NONE)
8749 int width = FRAME_COLS (f), height = FRAME_LINES (f);
8750 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8752 switch (f->want_fullscreen)
8754 /* No difference between these two when there is no WM */
8755 case FULLSCREEN_BOTH:
8756 case FULLSCREEN_MAXIMIZED:
8757 width = x_display_pixel_width (dpyinfo);
8758 height = x_display_pixel_height (dpyinfo);
8759 break;
8760 case FULLSCREEN_WIDTH:
8761 width = x_display_pixel_width (dpyinfo);
8762 break;
8763 case FULLSCREEN_HEIGHT:
8764 height = x_display_pixel_height (dpyinfo);
8767 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8769 change_frame_size (f, height, width, 0, 1, 0);
8770 SET_FRAME_GARBAGED (f);
8771 cancel_mouse_face (f);
8776 /* This function is called by x_set_offset to determine whether the window
8777 manager interfered with the positioning of the frame. Type A window
8778 managers position the surrounding window manager decorations a small
8779 amount above and left of the user-supplied position. Type B window
8780 managers position the surrounding window manager decorations at the
8781 user-specified position. If we detect a Type A window manager, we
8782 compensate by moving the window right and down by the proper amount. */
8784 static void
8785 x_check_expected_move (f, expected_left, expected_top)
8786 struct frame *f;
8787 int expected_left;
8788 int expected_top;
8790 int current_left = 0, current_top = 0;
8792 /* x_real_positions returns the left and top offsets of the outermost
8793 window manager window around the frame. */
8795 x_real_positions (f, &current_left, &current_top);
8797 if (current_left != expected_left || current_top != expected_top)
8799 /* It's a "Type A" window manager. */
8801 int adjusted_left;
8802 int adjusted_top;
8804 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8805 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8806 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8808 /* Now fix the mispositioned frame's location. */
8810 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8811 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8813 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8814 adjusted_left, adjusted_top);
8816 x_sync_with_move (f, expected_left, expected_top, 0);
8818 else
8819 /* It's a "Type B" window manager. We don't have to adjust the
8820 frame's position. */
8822 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8826 /* Wait for XGetGeometry to return up-to-date position information for a
8827 recently-moved frame. Call this immediately after calling XMoveWindow.
8828 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8829 frame has been moved to, so we use a fuzzy position comparison instead
8830 of an exact comparison. */
8832 static void
8833 x_sync_with_move (f, left, top, fuzzy)
8834 struct frame *f;
8835 int left, top, fuzzy;
8837 int count = 0;
8839 while (count++ < 50)
8841 int current_left = 0, current_top = 0;
8843 /* In theory, this call to XSync only needs to happen once, but in
8844 practice, it doesn't seem to work, hence the need for the surrounding
8845 loop. */
8847 XSync (FRAME_X_DISPLAY (f), False);
8848 x_real_positions (f, &current_left, &current_top);
8850 if (fuzzy)
8852 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8853 pixels. */
8855 if (eabs (current_left - left) <= 10
8856 && eabs (current_top - top) <= 40)
8857 return;
8859 else if (current_left == left && current_top == top)
8860 return;
8863 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8864 will then return up-to-date position info. */
8866 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8870 /* Wait for an event on frame F matching EVENTTYPE. */
8871 void
8872 x_wait_for_event (f, eventtype)
8873 struct frame *f;
8874 int eventtype;
8876 int level = interrupt_input_blocked;
8878 SELECT_TYPE fds;
8879 EMACS_TIME tmo, tmo_at, time_now;
8880 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
8882 pending_event_wait.f = f;
8883 pending_event_wait.eventtype = eventtype;
8885 /* Set timeout to 0.1 second. Hopefully not noticable.
8886 Maybe it should be configurable. */
8887 EMACS_SET_SECS_USECS (tmo, 0, 100000);
8888 EMACS_GET_TIME (tmo_at);
8889 EMACS_ADD_TIME (tmo_at, tmo_at, tmo);
8891 while (pending_event_wait.eventtype)
8893 interrupt_input_pending = 1;
8894 TOTALLY_UNBLOCK_INPUT;
8895 /* XTread_socket is called after unblock. */
8896 BLOCK_INPUT;
8897 interrupt_input_blocked = level;
8899 FD_ZERO (&fds);
8900 FD_SET (fd, &fds);
8902 EMACS_GET_TIME (time_now);
8903 EMACS_SUB_TIME (tmo, tmo_at, time_now);
8905 if (EMACS_TIME_NEG_P (tmo) || select (fd+1, &fds, NULL, NULL, &tmo) == 0)
8906 break; /* Timeout */
8908 pending_event_wait.f = 0;
8909 pending_event_wait.eventtype = 0;
8913 /* Change the size of frame F's X window to COLS/ROWS in the case F
8914 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8915 top-left-corner window gravity for this size change and subsequent
8916 size changes. Otherwise we leave the window gravity unchanged. */
8918 static void
8919 x_set_window_size_1 (f, change_gravity, cols, rows)
8920 struct frame *f;
8921 int change_gravity;
8922 int cols, rows;
8924 int pixelwidth, pixelheight;
8926 check_frame_size (f, &rows, &cols);
8927 f->scroll_bar_actual_width
8928 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8930 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8931 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8932 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8934 compute_fringe_widths (f, 0);
8936 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
8937 + 2*f->border_width;
8938 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
8939 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f)
8940 + 2*f->border_width;
8942 if (change_gravity) f->win_gravity = NorthWestGravity;
8943 x_wm_set_size_hint (f, (long) 0, 0);
8944 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8945 pixelwidth, pixelheight);
8948 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8949 receive in the ConfigureNotify event; if we get what we asked
8950 for, then the event won't cause the screen to become garbaged, so
8951 we have to make sure to do it here. */
8952 SET_FRAME_GARBAGED (f);
8954 /* Now, strictly speaking, we can't be sure that this is accurate,
8955 but the window manager will get around to dealing with the size
8956 change request eventually, and we'll hear how it went when the
8957 ConfigureNotify event gets here.
8959 We could just not bother storing any of this information here,
8960 and let the ConfigureNotify event set everything up, but that
8961 might be kind of confusing to the Lisp code, since size changes
8962 wouldn't be reported in the frame parameters until some random
8963 point in the future when the ConfigureNotify event arrives.
8965 We pass 1 for DELAY since we can't run Lisp code inside of
8966 a BLOCK_INPUT. */
8968 /* But the ConfigureNotify may in fact never arrive, and then this is
8969 not right if the frame is visible. Instead wait (with timeout)
8970 for the ConfigureNotify. */
8971 if (f->async_visible)
8972 x_wait_for_event (f, ConfigureNotify);
8973 else
8975 change_frame_size (f, rows, cols, 0, 1, 0);
8976 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8977 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8978 x_sync (f);
8983 /* Call this to change the size of frame F's x-window.
8984 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8985 for this size change and subsequent size changes.
8986 Otherwise we leave the window gravity unchanged. */
8988 void
8989 x_set_window_size (f, change_gravity, cols, rows)
8990 struct frame *f;
8991 int change_gravity;
8992 int cols, rows;
8994 BLOCK_INPUT;
8996 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8998 int r, c;
9000 /* When the frame is maximized/fullscreen or running under for
9001 example Xmonad, x_set_window_size_1 will be a no-op.
9002 In that case, the right thing to do is extend rows/cols to
9003 the current frame size. We do that first if x_set_window_size_1
9004 turns out to not be a no-op (there is no way to know).
9005 The size will be adjusted again if the frame gets a
9006 ConfigureNotify event as a result of x_set_window_size. */
9007 int pixelh = FRAME_PIXEL_HEIGHT (f);
9008 #ifdef USE_X_TOOLKIT
9009 /* The menu bar is not part of text lines. The tool bar
9010 is however. */
9011 pixelh -= FRAME_MENUBAR_HEIGHT (f);
9012 #endif
9013 r = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelh);
9014 /* Update f->scroll_bar_actual_width because it is used in
9015 FRAME_PIXEL_WIDTH_TO_TEXT_COLS. */
9016 f->scroll_bar_actual_width
9017 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
9018 c = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, FRAME_PIXEL_WIDTH (f));
9019 change_frame_size (f, r, c, 0, 1, 0);
9022 #ifdef USE_GTK
9023 if (FRAME_GTK_WIDGET (f))
9024 xg_frame_set_char_size (f, cols, rows);
9025 else
9026 x_set_window_size_1 (f, change_gravity, cols, rows);
9027 #else /* not USE_GTK */
9029 x_set_window_size_1 (f, change_gravity, cols, rows);
9031 #endif /* not USE_GTK */
9033 /* If cursor was outside the new size, mark it as off. */
9034 mark_window_cursors_off (XWINDOW (f->root_window));
9036 /* Clear out any recollection of where the mouse highlighting was,
9037 since it might be in a place that's outside the new frame size.
9038 Actually checking whether it is outside is a pain in the neck,
9039 so don't try--just let the highlighting be done afresh with new size. */
9040 cancel_mouse_face (f);
9042 UNBLOCK_INPUT;
9045 /* Mouse warping. */
9047 void
9048 x_set_mouse_position (f, x, y)
9049 struct frame *f;
9050 int x, y;
9052 int pix_x, pix_y;
9054 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
9055 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
9057 if (pix_x < 0) pix_x = 0;
9058 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
9060 if (pix_y < 0) pix_y = 0;
9061 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
9063 BLOCK_INPUT;
9065 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9066 0, 0, 0, 0, pix_x, pix_y);
9067 UNBLOCK_INPUT;
9070 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9072 void
9073 x_set_mouse_pixel_position (f, pix_x, pix_y)
9074 struct frame *f;
9075 int pix_x, pix_y;
9077 BLOCK_INPUT;
9079 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9080 0, 0, 0, 0, pix_x, pix_y);
9081 UNBLOCK_INPUT;
9084 /* focus shifting, raising and lowering. */
9086 void
9087 x_focus_on_frame (f)
9088 struct frame *f;
9090 #if 0
9091 /* I don't think that the ICCCM allows programs to do things like this
9092 without the interaction of the window manager. Whatever you end up
9093 doing with this code, do it to x_unfocus_frame too. */
9094 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9095 RevertToPointerRoot, CurrentTime);
9096 #endif /* ! 0 */
9099 void
9100 x_unfocus_frame (f)
9101 struct frame *f;
9103 #if 0
9104 /* Look at the remarks in x_focus_on_frame. */
9105 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
9106 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
9107 RevertToPointerRoot, CurrentTime);
9108 #endif /* ! 0 */
9111 /* Raise frame F. */
9113 void
9114 x_raise_frame (f)
9115 struct frame *f;
9117 BLOCK_INPUT;
9118 if (f->async_visible)
9119 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9121 XFlush (FRAME_X_DISPLAY (f));
9122 UNBLOCK_INPUT;
9125 /* Lower frame F. */
9127 void
9128 x_lower_frame (f)
9129 struct frame *f;
9131 if (f->async_visible)
9133 BLOCK_INPUT;
9134 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9135 XFlush (FRAME_X_DISPLAY (f));
9136 UNBLOCK_INPUT;
9140 /* Activate frame with Extended Window Manager Hints */
9142 void
9143 x_ewmh_activate_frame (f)
9144 FRAME_PTR f;
9146 /* See Window Manager Specification/Extended Window Manager Hints at
9147 http://freedesktop.org/wiki/Specifications/wm-spec */
9149 const char *atom = "_NET_ACTIVE_WINDOW";
9150 if (f->async_visible && wm_supports (f, atom))
9152 Lisp_Object frame;
9153 XSETFRAME (frame, f);
9154 Fx_send_client_event (frame, make_number (0), frame,
9155 make_unibyte_string (atom, strlen (atom)),
9156 make_number (32),
9157 Fcons (make_number (1),
9158 Fcons (make_number (last_user_time),
9159 Qnil)));
9163 static void
9164 XTframe_raise_lower (f, raise_flag)
9165 FRAME_PTR f;
9166 int raise_flag;
9168 if (raise_flag)
9169 x_raise_frame (f);
9170 else
9171 x_lower_frame (f);
9174 /* XEmbed implementation. */
9176 void
9177 xembed_set_info (f, flags)
9178 struct frame *f;
9179 enum xembed_info flags;
9181 Atom atom;
9182 unsigned long data[2];
9184 atom = XInternAtom (FRAME_X_DISPLAY (f), "_XEMBED_INFO", False);
9186 data[0] = XEMBED_VERSION;
9187 data[1] = flags;
9189 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), atom, atom,
9190 32, PropModeReplace, (unsigned char *) data, 2);
9193 void
9194 xembed_send_message (f, time, message, detail, data1, data2)
9195 struct frame *f;
9196 Time time;
9197 enum xembed_message message;
9198 long detail;
9199 long data1;
9200 long data2;
9202 XEvent event;
9204 event.xclient.type = ClientMessage;
9205 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9206 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
9207 event.xclient.format = 32;
9208 event.xclient.data.l[0] = time;
9209 event.xclient.data.l[1] = message;
9210 event.xclient.data.l[2] = detail;
9211 event.xclient.data.l[3] = data1;
9212 event.xclient.data.l[4] = data2;
9214 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9215 False, NoEventMask, &event);
9216 XSync (FRAME_X_DISPLAY (f), False);
9219 /* Change of visibility. */
9221 /* This tries to wait until the frame is really visible.
9222 However, if the window manager asks the user where to position
9223 the frame, this will return before the user finishes doing that.
9224 The frame will not actually be visible at that time,
9225 but it will become visible later when the window manager
9226 finishes with it. */
9228 void
9229 x_make_frame_visible (f)
9230 struct frame *f;
9232 Lisp_Object type;
9233 int original_top, original_left;
9234 int retry_count = 2;
9236 retry:
9238 BLOCK_INPUT;
9240 type = x_icon_type (f);
9241 if (!NILP (type))
9242 x_bitmap_icon (f, type);
9244 if (! FRAME_VISIBLE_P (f))
9246 /* We test FRAME_GARBAGED_P here to make sure we don't
9247 call x_set_offset a second time
9248 if we get to x_make_frame_visible a second time
9249 before the window gets really visible. */
9250 if (! FRAME_ICONIFIED_P (f)
9251 && ! FRAME_X_EMBEDDED_P (f)
9252 && ! f->output_data.x->asked_for_visible)
9253 x_set_offset (f, f->left_pos, f->top_pos, 0);
9255 f->output_data.x->asked_for_visible = 1;
9257 if (! EQ (Vx_no_window_manager, Qt))
9258 x_wm_set_window_state (f, NormalState);
9259 #ifdef USE_X_TOOLKIT
9260 if (FRAME_X_EMBEDDED_P (f))
9261 xembed_set_info (f, XEMBED_MAPPED);
9262 else
9264 /* This was XtPopup, but that did nothing for an iconified frame. */
9265 XtMapWidget (f->output_data.x->widget);
9267 #else /* not USE_X_TOOLKIT */
9268 #ifdef USE_GTK
9269 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9270 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9271 #else
9272 if (FRAME_X_EMBEDDED_P (f))
9273 xembed_set_info (f, XEMBED_MAPPED);
9274 else
9275 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9276 #endif /* not USE_GTK */
9277 #endif /* not USE_X_TOOLKIT */
9280 XFlush (FRAME_X_DISPLAY (f));
9282 /* Synchronize to ensure Emacs knows the frame is visible
9283 before we do anything else. We do this loop with input not blocked
9284 so that incoming events are handled. */
9286 Lisp_Object frame;
9287 int count;
9288 /* This must be before UNBLOCK_INPUT
9289 since events that arrive in response to the actions above
9290 will set it when they are handled. */
9291 int previously_visible = f->output_data.x->has_been_visible;
9293 original_left = f->left_pos;
9294 original_top = f->top_pos;
9296 /* This must come after we set COUNT. */
9297 UNBLOCK_INPUT;
9299 /* We unblock here so that arriving X events are processed. */
9301 /* Now move the window back to where it was "supposed to be".
9302 But don't do it if the gravity is negative.
9303 When the gravity is negative, this uses a position
9304 that is 3 pixels too low. Perhaps that's really the border width.
9306 Don't do this if the window has never been visible before,
9307 because the window manager may choose the position
9308 and we don't want to override it. */
9310 if (! FRAME_VISIBLE_P (f)
9311 && ! FRAME_ICONIFIED_P (f)
9312 && ! FRAME_X_EMBEDDED_P (f)
9313 && f->win_gravity == NorthWestGravity
9314 && previously_visible)
9316 Drawable rootw;
9317 int x, y;
9318 unsigned int width, height, border, depth;
9320 BLOCK_INPUT;
9322 /* On some window managers (such as FVWM) moving an existing
9323 window, even to the same place, causes the window manager
9324 to introduce an offset. This can cause the window to move
9325 to an unexpected location. Check the geometry (a little
9326 slow here) and then verify that the window is in the right
9327 place. If the window is not in the right place, move it
9328 there, and take the potential window manager hit. */
9329 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9330 &rootw, &x, &y, &width, &height, &border, &depth);
9332 if (original_left != x || original_top != y)
9333 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9334 original_left, original_top);
9336 UNBLOCK_INPUT;
9339 XSETFRAME (frame, f);
9341 /* Wait until the frame is visible. Process X events until a
9342 MapNotify event has been seen, or until we think we won't get a
9343 MapNotify at all.. */
9344 for (count = input_signal_count + 10;
9345 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9347 /* Force processing of queued events. */
9348 x_sync (f);
9350 /* Machines that do polling rather than SIGIO have been
9351 observed to go into a busy-wait here. So we'll fake an
9352 alarm signal to let the handler know that there's something
9353 to be read. We used to raise a real alarm, but it seems
9354 that the handler isn't always enabled here. This is
9355 probably a bug. */
9356 if (input_polling_used ())
9358 /* It could be confusing if a real alarm arrives while
9359 processing the fake one. Turn it off and let the
9360 handler reset it. */
9361 extern void poll_for_input_1 P_ ((void));
9362 int old_poll_suppress_count = poll_suppress_count;
9363 poll_suppress_count = 1;
9364 poll_for_input_1 ();
9365 poll_suppress_count = old_poll_suppress_count;
9368 /* See if a MapNotify event has been processed. */
9369 FRAME_SAMPLE_VISIBILITY (f);
9372 /* 2000-09-28: In
9374 (let ((f (selected-frame)))
9375 (iconify-frame f)
9376 (raise-frame f))
9378 the frame is not raised with various window managers on
9379 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9380 unknown reason, the call to XtMapWidget is completely ignored.
9381 Mapping the widget a second time works. */
9383 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9384 goto retry;
9388 /* Change from mapped state to withdrawn state. */
9390 /* Make the frame visible (mapped and not iconified). */
9392 void
9393 x_make_frame_invisible (f)
9394 struct frame *f;
9396 Window window;
9398 /* Use the frame's outermost window, not the one we normally draw on. */
9399 window = FRAME_OUTER_WINDOW (f);
9401 /* Don't keep the highlight on an invisible frame. */
9402 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9403 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9405 BLOCK_INPUT;
9407 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9408 that the current position of the window is user-specified, rather than
9409 program-specified, so that when the window is mapped again, it will be
9410 placed at the same location, without forcing the user to position it
9411 by hand again (they have already done that once for this window.) */
9412 x_wm_set_size_hint (f, (long) 0, 1);
9414 #ifdef USE_GTK
9415 if (FRAME_GTK_OUTER_WIDGET (f))
9416 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9417 else
9418 #else
9419 if (FRAME_X_EMBEDDED_P (f))
9420 xembed_set_info (f, 0);
9421 else
9422 #endif
9425 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9426 DefaultScreen (FRAME_X_DISPLAY (f))))
9428 UNBLOCK_INPUT_RESIGNAL;
9429 error ("Can't notify window manager of window withdrawal");
9433 /* We can't distinguish this from iconification
9434 just by the event that we get from the server.
9435 So we can't win using the usual strategy of letting
9436 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9437 and synchronize with the server to make sure we agree. */
9438 f->visible = 0;
9439 FRAME_ICONIFIED_P (f) = 0;
9440 f->async_visible = 0;
9441 f->async_iconified = 0;
9443 x_sync (f);
9445 UNBLOCK_INPUT;
9448 /* Change window state from mapped to iconified. */
9450 void
9451 x_iconify_frame (f)
9452 struct frame *f;
9454 int result;
9455 Lisp_Object type;
9457 /* Don't keep the highlight on an invisible frame. */
9458 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9459 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9461 if (f->async_iconified)
9462 return;
9464 BLOCK_INPUT;
9466 FRAME_SAMPLE_VISIBILITY (f);
9468 type = x_icon_type (f);
9469 if (!NILP (type))
9470 x_bitmap_icon (f, type);
9472 #ifdef USE_GTK
9473 if (FRAME_GTK_OUTER_WIDGET (f))
9475 if (! FRAME_VISIBLE_P (f))
9476 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9478 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9479 f->iconified = 1;
9480 f->visible = 1;
9481 f->async_iconified = 1;
9482 f->async_visible = 0;
9483 UNBLOCK_INPUT;
9484 return;
9486 #endif
9488 #ifdef USE_X_TOOLKIT
9490 if (! FRAME_VISIBLE_P (f))
9492 if (! EQ (Vx_no_window_manager, Qt))
9493 x_wm_set_window_state (f, IconicState);
9494 /* This was XtPopup, but that did nothing for an iconified frame. */
9495 XtMapWidget (f->output_data.x->widget);
9496 /* The server won't give us any event to indicate
9497 that an invisible frame was changed to an icon,
9498 so we have to record it here. */
9499 f->iconified = 1;
9500 f->visible = 1;
9501 f->async_iconified = 1;
9502 f->async_visible = 0;
9503 UNBLOCK_INPUT;
9504 return;
9507 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9508 XtWindow (f->output_data.x->widget),
9509 DefaultScreen (FRAME_X_DISPLAY (f)));
9510 UNBLOCK_INPUT;
9512 if (!result)
9513 error ("Can't notify window manager of iconification");
9515 f->async_iconified = 1;
9516 f->async_visible = 0;
9519 BLOCK_INPUT;
9520 XFlush (FRAME_X_DISPLAY (f));
9521 UNBLOCK_INPUT;
9522 #else /* not USE_X_TOOLKIT */
9524 /* Make sure the X server knows where the window should be positioned,
9525 in case the user deiconifies with the window manager. */
9526 if (! FRAME_VISIBLE_P (f)
9527 && ! FRAME_ICONIFIED_P (f)
9528 && ! FRAME_X_EMBEDDED_P (f))
9529 x_set_offset (f, f->left_pos, f->top_pos, 0);
9531 /* Since we don't know which revision of X we're running, we'll use both
9532 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9534 /* X11R4: send a ClientMessage to the window manager using the
9535 WM_CHANGE_STATE type. */
9537 XEvent message;
9539 message.xclient.window = FRAME_X_WINDOW (f);
9540 message.xclient.type = ClientMessage;
9541 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9542 message.xclient.format = 32;
9543 message.xclient.data.l[0] = IconicState;
9545 if (! XSendEvent (FRAME_X_DISPLAY (f),
9546 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9547 False,
9548 SubstructureRedirectMask | SubstructureNotifyMask,
9549 &message))
9551 UNBLOCK_INPUT_RESIGNAL;
9552 error ("Can't notify window manager of iconification");
9556 /* X11R3: set the initial_state field of the window manager hints to
9557 IconicState. */
9558 x_wm_set_window_state (f, IconicState);
9560 if (!FRAME_VISIBLE_P (f))
9562 /* If the frame was withdrawn, before, we must map it. */
9563 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9566 f->async_iconified = 1;
9567 f->async_visible = 0;
9569 XFlush (FRAME_X_DISPLAY (f));
9570 UNBLOCK_INPUT;
9571 #endif /* not USE_X_TOOLKIT */
9575 /* Free X resources of frame F. */
9577 void
9578 x_free_frame_resources (f)
9579 struct frame *f;
9581 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9582 Lisp_Object bar;
9583 struct scroll_bar *b;
9585 BLOCK_INPUT;
9587 /* If a display connection is dead, don't try sending more
9588 commands to the X server. */
9589 if (dpyinfo->display)
9591 /* We must free faces before destroying windows because some
9592 font-driver (e.g. xft) access a window while finishing a
9593 face. */
9594 if (FRAME_FACE_CACHE (f))
9595 free_frame_faces (f);
9597 if (f->output_data.x->icon_desc)
9598 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9600 #ifdef USE_X_TOOLKIT
9601 /* Explicitly destroy the scroll bars of the frame. Without
9602 this, we get "BadDrawable" errors from the toolkit later on,
9603 presumably from expose events generated for the disappearing
9604 toolkit scroll bars. */
9605 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9607 b = XSCROLL_BAR (bar);
9608 x_scroll_bar_remove (b);
9610 #endif
9612 #ifdef HAVE_X_I18N
9613 if (FRAME_XIC (f))
9614 free_frame_xic (f);
9615 #endif
9617 #ifdef USE_X_TOOLKIT
9618 if (f->output_data.x->widget)
9620 XtDestroyWidget (f->output_data.x->widget);
9621 f->output_data.x->widget = NULL;
9623 /* Tooltips don't have widgets, only a simple X window, even if
9624 we are using a toolkit. */
9625 else if (FRAME_X_WINDOW (f))
9626 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9628 free_frame_menubar (f);
9629 #else /* !USE_X_TOOLKIT */
9631 #ifdef USE_GTK
9632 /* In the GTK version, tooltips are normal X
9633 frames. We must check and free both types. */
9634 if (FRAME_GTK_OUTER_WIDGET (f))
9636 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9637 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9638 FRAME_GTK_OUTER_WIDGET (f) = 0;
9640 #endif /* USE_GTK */
9642 if (FRAME_X_WINDOW (f))
9643 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9644 #endif /* !USE_X_TOOLKIT */
9646 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9647 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9648 unload_color (f, f->output_data.x->cursor_pixel);
9649 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9650 unload_color (f, f->output_data.x->border_pixel);
9651 unload_color (f, f->output_data.x->mouse_pixel);
9653 if (f->output_data.x->scroll_bar_background_pixel != -1)
9654 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9655 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9656 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9657 #ifdef USE_TOOLKIT_SCROLL_BARS
9658 /* Scrollbar shadow colors. */
9659 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9660 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9661 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9662 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9663 #endif /* USE_TOOLKIT_SCROLL_BARS */
9664 if (f->output_data.x->white_relief.allocated_p)
9665 unload_color (f, f->output_data.x->white_relief.pixel);
9666 if (f->output_data.x->black_relief.allocated_p)
9667 unload_color (f, f->output_data.x->black_relief.pixel);
9669 x_free_gcs (f);
9670 XFlush (FRAME_X_DISPLAY (f));
9673 xfree (f->output_data.x->saved_menu_event);
9674 xfree (f->output_data.x);
9675 f->output_data.x = NULL;
9677 if (f == dpyinfo->x_focus_frame)
9678 dpyinfo->x_focus_frame = 0;
9679 if (f == dpyinfo->x_focus_event_frame)
9680 dpyinfo->x_focus_event_frame = 0;
9681 if (f == dpyinfo->x_highlight_frame)
9682 dpyinfo->x_highlight_frame = 0;
9684 if (f == dpyinfo->mouse_face_mouse_frame)
9686 dpyinfo->mouse_face_beg_row
9687 = dpyinfo->mouse_face_beg_col = -1;
9688 dpyinfo->mouse_face_end_row
9689 = dpyinfo->mouse_face_end_col = -1;
9690 dpyinfo->mouse_face_window = Qnil;
9691 dpyinfo->mouse_face_deferred_gc = 0;
9692 dpyinfo->mouse_face_mouse_frame = 0;
9695 UNBLOCK_INPUT;
9699 /* Destroy the X window of frame F. */
9701 void
9702 x_destroy_window (f)
9703 struct frame *f;
9705 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9707 /* If a display connection is dead, don't try sending more
9708 commands to the X server. */
9709 if (dpyinfo->display != 0)
9710 x_free_frame_resources (f);
9712 dpyinfo->reference_count--;
9716 /* Setting window manager hints. */
9718 /* Set the normal size hints for the window manager, for frame F.
9719 FLAGS is the flags word to use--or 0 meaning preserve the flags
9720 that the window now has.
9721 If USER_POSITION is nonzero, we set the USPosition
9722 flag (this is useful when FLAGS is 0).
9723 The GTK version is in gtkutils.c */
9725 #ifndef USE_GTK
9726 void
9727 x_wm_set_size_hint (f, flags, user_position)
9728 struct frame *f;
9729 long flags;
9730 int user_position;
9732 XSizeHints size_hints;
9733 Window window = FRAME_OUTER_WINDOW (f);
9735 /* Setting PMaxSize caused various problems. */
9736 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9738 size_hints.x = f->left_pos;
9739 size_hints.y = f->top_pos;
9741 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9742 size_hints.width = FRAME_PIXEL_WIDTH (f);
9744 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9745 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9746 size_hints.max_width = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
9747 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9748 size_hints.max_height = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
9749 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9751 /* Calculate the base and minimum sizes. */
9753 int base_width, base_height;
9754 int min_rows = 0, min_cols = 0;
9756 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9757 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9759 check_frame_size (f, &min_rows, &min_cols);
9761 /* The window manager uses the base width hints to calculate the
9762 current number of rows and columns in the frame while
9763 resizing; min_width and min_height aren't useful for this
9764 purpose, since they might not give the dimensions for a
9765 zero-row, zero-column frame.
9767 We use the base_width and base_height members if we have
9768 them; otherwise, we set the min_width and min_height members
9769 to the size for a zero x zero frame. */
9771 size_hints.flags |= PBaseSize;
9772 size_hints.base_width = base_width;
9773 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
9774 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9775 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9778 /* If we don't need the old flags, we don't need the old hint at all. */
9779 if (flags)
9781 size_hints.flags |= flags;
9782 goto no_read;
9786 XSizeHints hints; /* Sometimes I hate X Windows... */
9787 long supplied_return;
9788 int value;
9790 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9791 &supplied_return);
9793 if (flags)
9794 size_hints.flags |= flags;
9795 else
9797 if (value == 0)
9798 hints.flags = 0;
9799 if (hints.flags & PSize)
9800 size_hints.flags |= PSize;
9801 if (hints.flags & PPosition)
9802 size_hints.flags |= PPosition;
9803 if (hints.flags & USPosition)
9804 size_hints.flags |= USPosition;
9805 if (hints.flags & USSize)
9806 size_hints.flags |= USSize;
9810 no_read:
9812 #ifdef PWinGravity
9813 size_hints.win_gravity = f->win_gravity;
9814 size_hints.flags |= PWinGravity;
9816 if (user_position)
9818 size_hints.flags &= ~ PPosition;
9819 size_hints.flags |= USPosition;
9821 #endif /* PWinGravity */
9823 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9825 #endif /* not USE_GTK */
9827 /* Used for IconicState or NormalState */
9829 void
9830 x_wm_set_window_state (f, state)
9831 struct frame *f;
9832 int state;
9834 #ifdef USE_X_TOOLKIT
9835 Arg al[1];
9837 XtSetArg (al[0], XtNinitialState, state);
9838 XtSetValues (f->output_data.x->widget, al, 1);
9839 #else /* not USE_X_TOOLKIT */
9840 Window window = FRAME_X_WINDOW (f);
9842 f->output_data.x->wm_hints.flags |= StateHint;
9843 f->output_data.x->wm_hints.initial_state = state;
9845 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9846 #endif /* not USE_X_TOOLKIT */
9849 void
9850 x_wm_set_icon_pixmap (f, pixmap_id)
9851 struct frame *f;
9852 int pixmap_id;
9854 Pixmap icon_pixmap, icon_mask;
9856 #if !defined USE_X_TOOLKIT && !defined USE_GTK
9857 Window window = FRAME_OUTER_WINDOW (f);
9858 #endif
9860 if (pixmap_id > 0)
9862 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9863 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9864 icon_mask = x_bitmap_mask (f, pixmap_id);
9865 f->output_data.x->wm_hints.icon_mask = icon_mask;
9867 else
9869 /* It seems there is no way to turn off use of an icon
9870 pixmap. */
9871 return;
9875 #ifdef USE_GTK
9877 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9878 return;
9881 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9884 Arg al[1];
9885 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9886 XtSetValues (f->output_data.x->widget, al, 1);
9887 XtSetArg (al[0], XtNiconMask, icon_mask);
9888 XtSetValues (f->output_data.x->widget, al, 1);
9891 #else /* not USE_X_TOOLKIT && not USE_GTK */
9893 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9894 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9896 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9899 void
9900 x_wm_set_icon_position (f, icon_x, icon_y)
9901 struct frame *f;
9902 int icon_x, icon_y;
9904 Window window = FRAME_OUTER_WINDOW (f);
9906 f->output_data.x->wm_hints.flags |= IconPositionHint;
9907 f->output_data.x->wm_hints.icon_x = icon_x;
9908 f->output_data.x->wm_hints.icon_y = icon_y;
9910 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9914 /***********************************************************************
9915 Fonts
9916 ***********************************************************************/
9918 #if GLYPH_DEBUG
9920 /* Check that FONT is valid on frame F. It is if it can be found in F's
9921 font table. */
9923 static void
9924 x_check_font (f, font)
9925 struct frame *f;
9926 struct font *font;
9928 Lisp_Object frame;
9930 xassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9931 if (font->driver->check)
9932 xassert (font->driver->check (f, font) == 0);
9935 #endif /* GLYPH_DEBUG != 0 */
9938 /***********************************************************************
9939 Initialization
9940 ***********************************************************************/
9942 #ifdef USE_X_TOOLKIT
9943 static XrmOptionDescRec emacs_options[] = {
9944 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9945 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9947 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9948 XrmoptionSepArg, NULL},
9949 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9951 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9952 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9953 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9954 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9955 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9956 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9957 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9960 /* Whether atimer for Xt timeouts is activated or not. */
9962 static int x_timeout_atimer_activated_flag;
9964 #endif /* USE_X_TOOLKIT */
9966 static int x_initialized;
9968 #ifdef HAVE_X_SM
9969 static int x_session_initialized;
9970 #endif
9972 /* Test whether two display-name strings agree up to the dot that separates
9973 the screen number from the server number. */
9974 static int
9975 same_x_server (name1, name2)
9976 const char *name1, *name2;
9978 int seen_colon = 0;
9979 const unsigned char *system_name = SDATA (Vsystem_name);
9980 int system_name_length = strlen (system_name);
9981 int length_until_period = 0;
9983 while (system_name[length_until_period] != 0
9984 && system_name[length_until_period] != '.')
9985 length_until_period++;
9987 /* Treat `unix' like an empty host name. */
9988 if (! strncmp (name1, "unix:", 5))
9989 name1 += 4;
9990 if (! strncmp (name2, "unix:", 5))
9991 name2 += 4;
9992 /* Treat this host's name like an empty host name. */
9993 if (! strncmp (name1, system_name, system_name_length)
9994 && name1[system_name_length] == ':')
9995 name1 += system_name_length;
9996 if (! strncmp (name2, system_name, system_name_length)
9997 && name2[system_name_length] == ':')
9998 name2 += system_name_length;
9999 /* Treat this host's domainless name like an empty host name. */
10000 if (! strncmp (name1, system_name, length_until_period)
10001 && name1[length_until_period] == ':')
10002 name1 += length_until_period;
10003 if (! strncmp (name2, system_name, length_until_period)
10004 && name2[length_until_period] == ':')
10005 name2 += length_until_period;
10007 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10009 if (*name1 == ':')
10010 seen_colon++;
10011 if (seen_colon && *name1 == '.')
10012 return 1;
10014 return (seen_colon
10015 && (*name1 == '.' || *name1 == '\0')
10016 && (*name2 == '.' || *name2 == '\0'));
10019 /* Count number of set bits in mask and number of bits to shift to
10020 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10021 to 5. */
10022 static void
10023 get_bits_and_offset (mask, bits, offset)
10024 unsigned long mask;
10025 int *bits;
10026 int *offset;
10028 int nr = 0;
10029 int off = 0;
10031 while (!(mask & 1))
10033 off++;
10034 mask >>= 1;
10037 while (mask & 1)
10039 nr++;
10040 mask >>= 1;
10043 *offset = off;
10044 *bits = nr;
10047 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
10048 But don't permanently open it, just test its availability. */
10051 x_display_ok (display)
10052 const char *display;
10054 int dpy_ok = 1;
10055 Display *dpy;
10057 dpy = XOpenDisplay (display);
10058 if (dpy)
10059 XCloseDisplay (dpy);
10060 else
10061 dpy_ok = 0;
10062 return dpy_ok;
10065 #ifdef USE_GTK
10066 static void
10067 my_log_handler (log_domain, log_level, message, user_data)
10068 const gchar *log_domain;
10069 GLogLevelFlags log_level;
10070 const gchar *message;
10071 gpointer user_data;
10073 if (!strstr (message, "g_set_prgname"))
10074 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, message);
10076 #endif
10078 /* Open a connection to X display DISPLAY_NAME, and return
10079 the structure that describes the open display.
10080 If we cannot contact the display, return null. */
10082 struct x_display_info *
10083 x_term_init (display_name, xrm_option, resource_name)
10084 Lisp_Object display_name;
10085 char *xrm_option;
10086 char *resource_name;
10088 int connection;
10089 Display *dpy;
10090 struct terminal *terminal;
10091 struct x_display_info *dpyinfo;
10092 XrmDatabase xrdb;
10094 BLOCK_INPUT;
10096 if (!x_initialized)
10098 x_initialize ();
10099 ++x_initialized;
10102 if (! x_display_ok (SDATA (display_name)))
10103 error ("Display %s can't be opened", SDATA (display_name));
10105 #ifdef USE_GTK
10107 #define NUM_ARGV 10
10108 int argc;
10109 char *argv[NUM_ARGV];
10110 char **argv2 = argv;
10111 guint id;
10112 #ifndef HAVE_GTK_MULTIDISPLAY
10113 if (!EQ (Vinitial_window_system, Qx))
10114 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10115 #endif
10117 if (x_initialized++ > 1)
10119 #ifdef HAVE_GTK_MULTIDISPLAY
10120 /* Opening another display. If xg_display_open returns less
10121 than zero, we are probably on GTK 2.0, which can only handle
10122 one display. GTK 2.2 or later can handle more than one. */
10123 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10124 #endif
10125 error ("Sorry, this version of GTK can only handle one display");
10127 else
10129 for (argc = 0; argc < NUM_ARGV; ++argc)
10130 argv[argc] = 0;
10132 argc = 0;
10133 argv[argc++] = initial_argv[0];
10135 if (! NILP (display_name))
10137 argv[argc++] = "--display";
10138 argv[argc++] = SDATA (display_name);
10141 argv[argc++] = "--name";
10142 argv[argc++] = resource_name;
10144 XSetLocaleModifiers ("");
10146 /* Work around GLib bug that outputs a faulty warning. See
10147 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
10148 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
10149 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
10150 gtk_init (&argc, &argv2);
10151 g_log_remove_handler ("GLib", id);
10153 /* gtk_init does set_locale. We must fix locale after calling it. */
10154 fixup_locale ();
10155 xg_initialize ();
10157 dpy = GDK_DISPLAY ();
10159 /* NULL window -> events for all windows go to our function */
10160 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10162 /* Load our own gtkrc if it exists. */
10164 char *file = "~/.emacs.d/gtkrc";
10165 Lisp_Object s, abs_file;
10167 s = make_string (file, strlen (file));
10168 abs_file = Fexpand_file_name (s, Qnil);
10170 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10171 gtk_rc_parse (SDATA (abs_file));
10174 XSetErrorHandler (x_error_handler);
10175 XSetIOErrorHandler (x_io_error_quitter);
10178 #else /* not USE_GTK */
10179 #ifdef USE_X_TOOLKIT
10180 /* weiner@footloose.sps.mot.com reports that this causes
10181 errors with X11R5:
10182 X protocol error: BadAtom (invalid Atom parameter)
10183 on protocol request 18skiloaf.
10184 So let's not use it until R6. */
10185 #ifdef HAVE_X11XTR6
10186 XtSetLanguageProc (NULL, NULL, NULL);
10187 #endif
10190 int argc = 0;
10191 char *argv[3];
10193 argv[0] = "";
10194 argc = 1;
10195 if (xrm_option)
10197 argv[argc++] = "-xrm";
10198 argv[argc++] = xrm_option;
10200 turn_on_atimers (0);
10201 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10202 resource_name, EMACS_CLASS,
10203 emacs_options, XtNumber (emacs_options),
10204 &argc, argv);
10205 turn_on_atimers (1);
10207 #ifdef HAVE_X11XTR6
10208 /* I think this is to compensate for XtSetLanguageProc. */
10209 fixup_locale ();
10210 #endif
10213 #else /* not USE_X_TOOLKIT */
10214 XSetLocaleModifiers ("");
10215 dpy = XOpenDisplay (SDATA (display_name));
10216 #endif /* not USE_X_TOOLKIT */
10217 #endif /* not USE_GTK*/
10219 /* Detect failure. */
10220 if (dpy == 0)
10222 UNBLOCK_INPUT;
10223 return 0;
10226 /* We have definitely succeeded. Record the new connection. */
10228 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10229 bzero (dpyinfo, sizeof *dpyinfo);
10231 terminal = x_create_terminal (dpyinfo);
10234 struct x_display_info *share;
10235 Lisp_Object tail;
10237 for (share = x_display_list, tail = x_display_name_list; share;
10238 share = share->next, tail = XCDR (tail))
10239 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10240 SDATA (display_name)))
10241 break;
10242 if (share)
10243 terminal->kboard = share->terminal->kboard;
10244 else
10246 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10247 init_kboard (terminal->kboard);
10248 terminal->kboard->Vwindow_system = Qx;
10250 /* Add the keyboard to the list before running Lisp code (via
10251 Qvendor_specific_keysyms below), since these are not traced
10252 via terminals but only through all_kboards. */
10253 terminal->kboard->next_kboard = all_kboards;
10254 all_kboards = terminal->kboard;
10256 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10258 char *vendor = ServerVendor (dpy);
10260 /* Protect terminal from GC before removing it from the
10261 list of terminals. */
10262 struct gcpro gcpro1;
10263 Lisp_Object gcpro_term;
10264 XSETTERMINAL (gcpro_term, terminal);
10265 GCPRO1 (gcpro_term);
10267 /* Temporarily hide the partially initialized terminal. */
10268 terminal_list = terminal->next_terminal;
10269 UNBLOCK_INPUT;
10270 terminal->kboard->Vsystem_key_alist
10271 = call1 (Qvendor_specific_keysyms,
10272 vendor ? build_string (vendor) : empty_unibyte_string);
10273 BLOCK_INPUT;
10274 terminal->next_terminal = terminal_list;
10275 terminal_list = terminal;
10276 UNGCPRO;
10279 /* Don't let the initial kboard remain current longer than necessary.
10280 That would cause problems if a file loaded on startup tries to
10281 prompt in the mini-buffer. */
10282 if (current_kboard == initial_kboard)
10283 current_kboard = terminal->kboard;
10285 terminal->kboard->reference_count++;
10288 /* Put this display on the chain. */
10289 dpyinfo->next = x_display_list;
10290 x_display_list = dpyinfo;
10292 /* Put it on x_display_name_list as well, to keep them parallel. */
10293 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10294 x_display_name_list);
10295 dpyinfo->name_list_element = XCAR (x_display_name_list);
10297 dpyinfo->display = dpy;
10299 /* Set the name of the terminal. */
10300 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10301 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10302 terminal->name[SBYTES (display_name)] = 0;
10304 #if 0
10305 XSetAfterFunction (x_current_display, x_trace_wire);
10306 #endif /* ! 0 */
10308 dpyinfo->x_id_name
10309 = (char *) xmalloc (SBYTES (Vinvocation_name)
10310 + SBYTES (Vsystem_name)
10311 + 2);
10312 sprintf (dpyinfo->x_id_name, "%s@%s",
10313 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10315 /* Figure out which modifier bits mean what. */
10316 x_find_modifier_meanings (dpyinfo);
10318 /* Get the scroll bar cursor. */
10319 #ifdef USE_GTK
10320 /* We must create a GTK cursor, it is required for GTK widgets. */
10321 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10322 #endif /* USE_GTK */
10324 dpyinfo->vertical_scroll_bar_cursor
10325 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10327 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10328 resource_name, EMACS_CLASS);
10329 #ifdef HAVE_XRMSETDATABASE
10330 XrmSetDatabase (dpyinfo->display, xrdb);
10331 #else
10332 dpyinfo->display->db = xrdb;
10333 #endif
10334 /* Put the rdb where we can find it in a way that works on
10335 all versions. */
10336 dpyinfo->xrdb = xrdb;
10338 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10339 DefaultScreen (dpyinfo->display));
10340 select_visual (dpyinfo);
10341 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10342 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10343 dpyinfo->client_leader_window = 0;
10344 dpyinfo->grabbed = 0;
10345 dpyinfo->reference_count = 0;
10346 dpyinfo->icon_bitmap_id = -1;
10347 dpyinfo->n_fonts = 0;
10348 dpyinfo->bitmaps = 0;
10349 dpyinfo->bitmaps_size = 0;
10350 dpyinfo->bitmaps_last = 0;
10351 dpyinfo->scratch_cursor_gc = 0;
10352 dpyinfo->mouse_face_mouse_frame = 0;
10353 dpyinfo->mouse_face_deferred_gc = 0;
10354 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10355 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10356 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10357 dpyinfo->mouse_face_window = Qnil;
10358 dpyinfo->mouse_face_overlay = Qnil;
10359 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10360 dpyinfo->mouse_face_defer = 0;
10361 dpyinfo->mouse_face_hidden = 0;
10362 dpyinfo->x_focus_frame = 0;
10363 dpyinfo->x_focus_event_frame = 0;
10364 dpyinfo->x_highlight_frame = 0;
10365 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10367 /* See if we can construct pixel values from RGB values. */
10368 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10369 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10371 if (dpyinfo->visual->class == TrueColor)
10373 get_bits_and_offset (dpyinfo->visual->red_mask,
10374 &dpyinfo->red_bits, &dpyinfo->red_offset);
10375 get_bits_and_offset (dpyinfo->visual->blue_mask,
10376 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10377 get_bits_and_offset (dpyinfo->visual->green_mask,
10378 &dpyinfo->green_bits, &dpyinfo->green_offset);
10381 /* See if a private colormap is requested. */
10382 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10384 if (dpyinfo->visual->class == PseudoColor)
10386 Lisp_Object value;
10387 value = display_x_get_resource (dpyinfo,
10388 build_string ("privateColormap"),
10389 build_string ("PrivateColormap"),
10390 Qnil, Qnil);
10391 if (STRINGP (value)
10392 && (!strcmp (SDATA (value), "true")
10393 || !strcmp (SDATA (value), "on")))
10394 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10397 else
10398 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10399 dpyinfo->visual, AllocNone);
10401 #ifdef HAVE_XFT
10403 /* If we are using Xft, check dpi value in X resources.
10404 It is better we use it as well, since Xft will use it, as will all
10405 Gnome applications. If our real DPI is smaller or larger than the
10406 one Xft uses, our font will look smaller or larger than other
10407 for other applications, even if it is the same font name (monospace-10
10408 for example). */
10409 char *v = XGetDefault (dpyinfo->display, "Xft", "dpi");
10410 double d;
10411 if (v != NULL && sscanf (v, "%lf", &d) == 1)
10412 dpyinfo->resy = dpyinfo->resx = d;
10414 #endif
10416 if (dpyinfo->resy < 1)
10418 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10419 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10420 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10421 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10422 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10423 pixels = DisplayWidth (dpyinfo->display, screen_number);
10424 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10425 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10426 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10429 dpyinfo->Xatom_wm_protocols
10430 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10431 dpyinfo->Xatom_wm_take_focus
10432 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10433 dpyinfo->Xatom_wm_save_yourself
10434 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10435 dpyinfo->Xatom_wm_delete_window
10436 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10437 dpyinfo->Xatom_wm_change_state
10438 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10439 dpyinfo->Xatom_wm_configure_denied
10440 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10441 dpyinfo->Xatom_wm_window_moved
10442 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10443 dpyinfo->Xatom_wm_client_leader
10444 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10445 dpyinfo->Xatom_editres
10446 = XInternAtom (dpyinfo->display, "Editres", False);
10447 dpyinfo->Xatom_CLIPBOARD
10448 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10449 dpyinfo->Xatom_TIMESTAMP
10450 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10451 dpyinfo->Xatom_TEXT
10452 = XInternAtom (dpyinfo->display, "TEXT", False);
10453 dpyinfo->Xatom_COMPOUND_TEXT
10454 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10455 dpyinfo->Xatom_UTF8_STRING
10456 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10457 dpyinfo->Xatom_DELETE
10458 = XInternAtom (dpyinfo->display, "DELETE", False);
10459 dpyinfo->Xatom_MULTIPLE
10460 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10461 dpyinfo->Xatom_INCR
10462 = XInternAtom (dpyinfo->display, "INCR", False);
10463 dpyinfo->Xatom_EMACS_TMP
10464 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10465 dpyinfo->Xatom_TARGETS
10466 = XInternAtom (dpyinfo->display, "TARGETS", False);
10467 dpyinfo->Xatom_NULL
10468 = XInternAtom (dpyinfo->display, "NULL", False);
10469 dpyinfo->Xatom_ATOM_PAIR
10470 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10471 /* For properties of font. */
10472 dpyinfo->Xatom_PIXEL_SIZE
10473 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10474 dpyinfo->Xatom_AVERAGE_WIDTH
10475 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10476 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10477 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10478 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10479 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10480 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10481 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10483 /* Ghostscript support. */
10484 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10485 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10487 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10488 False);
10490 dpyinfo->Xatom_XEMBED = XInternAtom (dpyinfo->display, "_XEMBED",
10491 False);
10493 dpyinfo->Xatom_net_wm_state
10494 = XInternAtom (dpyinfo->display, "_NET_WM_STATE", False);
10495 dpyinfo->Xatom_net_wm_state_fullscreen_atom
10496 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_FULLSCREEN", False);
10497 dpyinfo->Xatom_net_wm_state_maximized_horz
10498 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
10499 dpyinfo->Xatom_net_wm_state_maximized_vert
10500 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
10501 dpyinfo->Xatom_net_wm_state_sticky
10502 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_STICKY", False);
10503 dpyinfo->Xatom_net_window_type
10504 = XInternAtom (dpyinfo->display, "_NET_WM_WINDOW_TYPE", False);
10505 dpyinfo->Xatom_net_window_type_tooltip
10506 = XInternAtom (dpyinfo->display, "_NET_WM_WINDOW_TYPE_TOOLTIP", False);
10508 dpyinfo->cut_buffers_initialized = 0;
10510 dpyinfo->x_dnd_atoms_size = 8;
10511 dpyinfo->x_dnd_atoms_length = 0;
10512 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10513 * dpyinfo->x_dnd_atoms_size);
10515 dpyinfo->net_supported_atoms = NULL;
10516 dpyinfo->nr_net_supported_atoms = 0;
10517 dpyinfo->net_supported_window = 0;
10519 connection = ConnectionNumber (dpyinfo->display);
10520 dpyinfo->connection = connection;
10523 extern int gray_bitmap_width, gray_bitmap_height;
10524 extern char *gray_bitmap_bits;
10525 dpyinfo->gray
10526 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10527 gray_bitmap_bits,
10528 gray_bitmap_width, gray_bitmap_height,
10529 (unsigned long) 1, (unsigned long) 0, 1);
10532 #ifdef HAVE_X_I18N
10533 xim_initialize (dpyinfo, resource_name);
10534 #endif
10536 xsettings_initialize (dpyinfo);
10538 #ifdef subprocesses
10539 /* This is only needed for distinguishing keyboard and process input. */
10540 if (connection != 0)
10541 add_keyboard_wait_descriptor (connection);
10542 #endif
10544 #ifdef F_SETOWN
10545 fcntl (connection, F_SETOWN, getpid ());
10546 #endif /* ! defined (F_SETOWN) */
10548 #ifdef SIGIO
10549 if (interrupt_input)
10550 init_sigio (connection);
10551 #endif /* ! defined (SIGIO) */
10553 #ifdef USE_LUCID
10555 Display *dpy = dpyinfo->display;
10556 XrmValue d, fr, to;
10557 Font font;
10559 d.addr = (XPointer)&dpy;
10560 d.size = sizeof (Display *);
10561 fr.addr = XtDefaultFont;
10562 fr.size = sizeof (XtDefaultFont);
10563 to.size = sizeof (Font *);
10564 to.addr = (XPointer)&font;
10565 x_catch_errors (dpy);
10566 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10567 abort ();
10568 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10569 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10570 x_uncatch_errors ();
10572 #endif
10574 /* See if we should run in synchronous mode. This is useful
10575 for debugging X code. */
10577 Lisp_Object value;
10578 value = display_x_get_resource (dpyinfo,
10579 build_string ("synchronous"),
10580 build_string ("Synchronous"),
10581 Qnil, Qnil);
10582 if (STRINGP (value)
10583 && (!strcmp (SDATA (value), "true")
10584 || !strcmp (SDATA (value), "on")))
10585 XSynchronize (dpyinfo->display, True);
10589 Lisp_Object value;
10590 value = display_x_get_resource (dpyinfo,
10591 build_string ("useXIM"),
10592 build_string ("UseXIM"),
10593 Qnil, Qnil);
10594 #ifdef USE_XIM
10595 if (STRINGP (value)
10596 && (!strcmp (SDATA (value), "false")
10597 || !strcmp (SDATA (value), "off")))
10598 use_xim = 0;
10599 #else
10600 if (STRINGP (value)
10601 && (!strcmp (SDATA (value), "true")
10602 || !strcmp (SDATA (value), "on")))
10603 use_xim = 1;
10604 #endif
10607 #ifdef HAVE_X_SM
10608 /* Only do this for the very first display in the Emacs session.
10609 Ignore X session management when Emacs was first started on a
10610 tty. */
10611 if (terminal->id == 1)
10612 x_session_initialize (dpyinfo);
10613 #endif
10615 UNBLOCK_INPUT;
10617 return dpyinfo;
10620 /* Get rid of display DPYINFO, deleting all frames on it,
10621 and without sending any more commands to the X server. */
10623 void
10624 x_delete_display (dpyinfo)
10625 struct x_display_info *dpyinfo;
10627 struct terminal *t;
10629 /* Close all frames and delete the generic struct terminal for this
10630 X display. */
10631 for (t = terminal_list; t; t = t->next_terminal)
10632 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10634 #ifdef HAVE_X_SM
10635 /* Close X session management when we close its display. */
10636 if (t->id == 1 && x_session_have_connection ())
10637 x_session_close();
10638 #endif
10639 delete_terminal (t);
10640 break;
10643 delete_keyboard_wait_descriptor (dpyinfo->connection);
10645 /* Discard this display from x_display_name_list and x_display_list.
10646 We can't use Fdelq because that can quit. */
10647 if (! NILP (x_display_name_list)
10648 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10649 x_display_name_list = XCDR (x_display_name_list);
10650 else
10652 Lisp_Object tail;
10654 tail = x_display_name_list;
10655 while (CONSP (tail) && CONSP (XCDR (tail)))
10657 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10659 XSETCDR (tail, XCDR (XCDR (tail)));
10660 break;
10662 tail = XCDR (tail);
10666 if (next_noop_dpyinfo == dpyinfo)
10667 next_noop_dpyinfo = dpyinfo->next;
10669 if (x_display_list == dpyinfo)
10670 x_display_list = dpyinfo->next;
10671 else
10673 struct x_display_info *tail;
10675 for (tail = x_display_list; tail; tail = tail->next)
10676 if (tail->next == dpyinfo)
10677 tail->next = tail->next->next;
10680 xfree (dpyinfo->x_id_name);
10681 xfree (dpyinfo->x_dnd_atoms);
10682 xfree (dpyinfo->color_cells);
10683 xfree (dpyinfo);
10686 #ifdef USE_X_TOOLKIT
10688 /* Atimer callback function for TIMER. Called every 0.1s to process
10689 Xt timeouts, if needed. We must avoid calling XtAppPending as
10690 much as possible because that function does an implicit XFlush
10691 that slows us down. */
10693 static void
10694 x_process_timeouts (timer)
10695 struct atimer *timer;
10697 BLOCK_INPUT;
10698 x_timeout_atimer_activated_flag = 0;
10699 if (toolkit_scroll_bar_interaction || popup_activated ())
10701 while (XtAppPending (Xt_app_con) & XtIMTimer)
10702 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10703 /* Reactivate the atimer for next time. */
10704 x_activate_timeout_atimer ();
10706 UNBLOCK_INPUT;
10709 /* Install an asynchronous timer that processes Xt timeout events
10710 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10711 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10712 function whenever these variables are set. This is necessary
10713 because some widget sets use timeouts internally, for example the
10714 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10715 processed, these widgets don't behave normally. */
10717 void
10718 x_activate_timeout_atimer ()
10720 BLOCK_INPUT;
10721 if (!x_timeout_atimer_activated_flag)
10723 EMACS_TIME interval;
10725 EMACS_SET_SECS_USECS (interval, 0, 100000);
10726 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10727 x_timeout_atimer_activated_flag = 1;
10729 UNBLOCK_INPUT;
10732 #endif /* USE_X_TOOLKIT */
10735 /* Set up use of X before we make the first connection. */
10737 extern frame_parm_handler x_frame_parm_handlers[];
10739 static struct redisplay_interface x_redisplay_interface =
10741 x_frame_parm_handlers,
10742 x_produce_glyphs,
10743 x_write_glyphs,
10744 x_insert_glyphs,
10745 x_clear_end_of_line,
10746 x_scroll_run,
10747 x_after_update_window_line,
10748 x_update_window_begin,
10749 x_update_window_end,
10750 x_cursor_to,
10751 x_flush,
10752 #ifdef XFlush
10753 x_flush,
10754 #else
10755 0, /* flush_display_optional */
10756 #endif
10757 x_clear_window_mouse_face,
10758 x_get_glyph_overhangs,
10759 x_fix_overlapping_area,
10760 x_draw_fringe_bitmap,
10761 0, /* define_fringe_bitmap */
10762 0, /* destroy_fringe_bitmap */
10763 x_compute_glyph_string_overhangs,
10764 x_draw_glyph_string,
10765 x_define_frame_cursor,
10766 x_clear_frame_area,
10767 x_draw_window_cursor,
10768 x_draw_vertical_window_border,
10769 x_shift_glyphs_for_insert
10773 /* This function is called when the last frame on a display is deleted. */
10774 void
10775 x_delete_terminal (struct terminal *terminal)
10777 struct x_display_info *dpyinfo = terminal->display_info.x;
10779 /* Protect against recursive calls. delete_frame in
10780 delete_terminal calls us back when it deletes our last frame. */
10781 if (!terminal->name)
10782 return;
10784 BLOCK_INPUT;
10785 #ifdef HAVE_X_I18N
10786 /* We must close our connection to the XIM server before closing the
10787 X display. */
10788 if (dpyinfo->xim)
10789 xim_close_dpy (dpyinfo);
10790 #endif
10792 /* If called from x_connection_closed, the display may already be closed
10793 and dpyinfo->display was set to 0 to indicate that. */
10794 if (dpyinfo->display)
10796 x_destroy_all_bitmaps (dpyinfo);
10797 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10799 /* Whether or not XCloseDisplay destroys the associated resource
10800 database depends on the version of libX11. To avoid both
10801 crash and memory leak, we dissociate the database from the
10802 display and then destroy dpyinfo->xrdb ourselves.
10804 Unfortunately, the above strategy does not work in some
10805 situations due to a bug in newer versions of libX11: because
10806 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
10807 dpy->db is NULL, XCloseDisplay destroys the associated
10808 database whereas it has not been created by XGetDefault
10809 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
10810 don't destroy the database here in order to avoid the crash
10811 in the above situations for now, though that may cause memory
10812 leaks in other situations. */
10813 #if 0
10814 #ifdef HAVE_XRMSETDATABASE
10815 XrmSetDatabase (dpyinfo->display, NULL);
10816 #else
10817 dpyinfo->display->db = NULL;
10818 #endif
10819 /* We used to call XrmDestroyDatabase from x_delete_display, but
10820 some older versions of libX11 crash if we call it after
10821 closing all the displays. */
10822 XrmDestroyDatabase (dpyinfo->xrdb);
10823 #endif
10825 #ifdef USE_GTK
10826 xg_display_close (dpyinfo->display);
10827 #else
10828 #ifdef USE_X_TOOLKIT
10829 XtCloseDisplay (dpyinfo->display);
10830 #else
10831 XCloseDisplay (dpyinfo->display);
10832 #endif
10833 #endif /* ! USE_GTK */
10836 /* Mark as dead. */
10837 dpyinfo->display = NULL;
10838 x_delete_display (dpyinfo);
10839 UNBLOCK_INPUT;
10842 /* Create a struct terminal, initialize it with the X11 specific
10843 functions and make DISPLAY->TERMINAL point to it. */
10845 static struct terminal *
10846 x_create_terminal (struct x_display_info *dpyinfo)
10848 struct terminal *terminal;
10850 terminal = create_terminal ();
10852 terminal->type = output_x_window;
10853 terminal->display_info.x = dpyinfo;
10854 dpyinfo->terminal = terminal;
10856 /* kboard is initialized in x_term_init. */
10858 terminal->clear_frame_hook = x_clear_frame;
10859 terminal->ins_del_lines_hook = x_ins_del_lines;
10860 terminal->delete_glyphs_hook = x_delete_glyphs;
10861 terminal->ring_bell_hook = XTring_bell;
10862 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
10863 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
10864 terminal->set_terminal_modes_hook = XTset_terminal_modes;
10865 terminal->update_begin_hook = x_update_begin;
10866 terminal->update_end_hook = x_update_end;
10867 terminal->set_terminal_window_hook = XTset_terminal_window;
10868 terminal->read_socket_hook = XTread_socket;
10869 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10870 terminal->mouse_position_hook = XTmouse_position;
10871 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10872 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10873 terminal->fullscreen_hook = XTfullscreen_hook;
10874 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10875 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10876 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10877 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10879 terminal->delete_frame_hook = x_destroy_window;
10880 terminal->delete_terminal_hook = x_delete_terminal;
10882 terminal->rif = &x_redisplay_interface;
10883 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10884 terminal->char_ins_del_ok = 1;
10885 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10886 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10887 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10888 off the bottom. */
10890 return terminal;
10893 void
10894 x_initialize ()
10896 baud_rate = 19200;
10898 x_noop_count = 0;
10899 last_tool_bar_item = -1;
10900 any_help_event_p = 0;
10901 ignore_next_mouse_click_timeout = 0;
10902 #ifdef HAVE_X_SM
10903 x_session_initialized = 0;
10904 #endif
10906 #ifdef USE_GTK
10907 current_count = -1;
10908 #endif
10910 /* Try to use interrupt input; if we can't, then start polling. */
10911 Fset_input_interrupt_mode (Qt);
10913 #ifdef USE_X_TOOLKIT
10914 XtToolkitInitialize ();
10916 Xt_app_con = XtCreateApplicationContext ();
10918 /* Register a converter from strings to pixels, which uses
10919 Emacs' color allocation infrastructure. */
10920 XtAppSetTypeConverter (Xt_app_con,
10921 XtRString, XtRPixel, cvt_string_to_pixel,
10922 cvt_string_to_pixel_args,
10923 XtNumber (cvt_string_to_pixel_args),
10924 XtCacheByDisplay, cvt_pixel_dtor);
10926 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10927 #endif
10929 #ifdef USE_TOOLKIT_SCROLL_BARS
10930 #ifndef USE_GTK
10931 xaw3d_arrow_scroll = False;
10932 xaw3d_pick_top = True;
10933 #endif
10934 #endif
10936 pending_autoraise_frame = 0;
10937 pending_event_wait.f = 0;
10938 pending_event_wait.eventtype = 0;
10940 /* Note that there is no real way portable across R3/R4 to get the
10941 original error handler. */
10942 XSetErrorHandler (x_error_handler);
10943 XSetIOErrorHandler (x_io_error_quitter);
10945 signal (SIGPIPE, x_connection_signal);
10947 xgselect_initialize ();
10951 void
10952 syms_of_xterm ()
10954 x_error_message = NULL;
10956 staticpro (&x_display_name_list);
10957 x_display_name_list = Qnil;
10959 staticpro (&last_mouse_scroll_bar);
10960 last_mouse_scroll_bar = Qnil;
10962 staticpro (&Qvendor_specific_keysyms);
10963 Qvendor_specific_keysyms = intern_c_string ("vendor-specific-keysyms");
10965 staticpro (&Qlatin_1);
10966 Qlatin_1 = intern_c_string ("latin-1");
10968 staticpro (&last_mouse_press_frame);
10969 last_mouse_press_frame = Qnil;
10971 #ifdef USE_GTK
10972 xg_default_icon_file = make_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
10973 staticpro (&xg_default_icon_file);
10975 Qx_gtk_map_stock = intern_c_string ("x-gtk-map-stock");
10976 staticpro (&Qx_gtk_map_stock);
10977 #endif
10979 DEFVAR_BOOL ("x-use-underline-position-properties",
10980 &x_use_underline_position_properties,
10981 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10982 A value of nil means ignore them. If you encounter fonts with bogus
10983 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10984 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10985 to override the font's UNDERLINE_POSITION for small font display
10986 sizes. */);
10987 x_use_underline_position_properties = 1;
10989 DEFVAR_BOOL ("x-underline-at-descent-line",
10990 &x_underline_at_descent_line,
10991 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
10992 A value of nil means to draw the underline according to the value of the
10993 variable `x-use-underline-position-properties', which is usually at the
10994 baseline level. The default value is nil. */);
10995 x_underline_at_descent_line = 0;
10997 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10998 &x_mouse_click_focus_ignore_position,
10999 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
11000 This variable is only used when the window manager requires that you
11001 click on a frame to select it (give it focus). In that case, a value
11002 of nil, means that the selected window and cursor position changes to
11003 reflect the mouse click position, while a non-nil value means that the
11004 selected window or cursor position is preserved. */);
11005 x_mouse_click_focus_ignore_position = 0;
11007 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
11008 doc: /* What X toolkit scroll bars Emacs uses.
11009 A value of nil means Emacs doesn't use X toolkit scroll bars.
11010 Otherwise, value is a symbol describing the X toolkit. */);
11011 #ifdef USE_TOOLKIT_SCROLL_BARS
11012 #ifdef USE_MOTIF
11013 Vx_toolkit_scroll_bars = intern_c_string ("motif");
11014 #elif defined HAVE_XAW3D
11015 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
11016 #elif USE_GTK
11017 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
11018 #else
11019 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
11020 #endif
11021 #else
11022 Vx_toolkit_scroll_bars = Qnil;
11023 #endif
11025 staticpro (&last_mouse_motion_frame);
11026 last_mouse_motion_frame = Qnil;
11028 Qmodifier_value = intern_c_string ("modifier-value");
11029 Qalt = intern_c_string ("alt");
11030 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
11031 Qhyper = intern_c_string ("hyper");
11032 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11033 Qmeta = intern_c_string ("meta");
11034 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11035 Qsuper = intern_c_string ("super");
11036 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11038 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
11039 doc: /* Which keys Emacs uses for the alt modifier.
11040 This should be one of the symbols `alt', `hyper', `meta', `super'.
11041 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11042 is nil, which is the same as `alt'. */);
11043 Vx_alt_keysym = Qnil;
11045 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
11046 doc: /* Which keys Emacs uses for the hyper modifier.
11047 This should be one of the symbols `alt', `hyper', `meta', `super'.
11048 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11049 default is nil, which is the same as `hyper'. */);
11050 Vx_hyper_keysym = Qnil;
11052 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
11053 doc: /* Which keys Emacs uses for the meta modifier.
11054 This should be one of the symbols `alt', `hyper', `meta', `super'.
11055 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11056 default is nil, which is the same as `meta'. */);
11057 Vx_meta_keysym = Qnil;
11059 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
11060 doc: /* Which keys Emacs uses for the super modifier.
11061 This should be one of the symbols `alt', `hyper', `meta', `super'.
11062 For example, `super' means use the Super_L and Super_R keysyms. The
11063 default is nil, which is the same as `super'. */);
11064 Vx_super_keysym = Qnil;
11066 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
11067 doc: /* Hash table of character codes indexed by X keysym codes. */);
11068 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
11069 make_float (DEFAULT_REHASH_SIZE),
11070 make_float (DEFAULT_REHASH_THRESHOLD),
11071 Qnil, Qnil, Qnil);
11074 #endif /* HAVE_X_WINDOWS */
11076 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
11077 (do not change this comment) */