(Foverlay_buffer): Add EXFUN.
[emacs.git] / src / xterm.c
blob89cf06c05e036e49d8e0e4763ca74f1f2079c52b
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
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, or (at your option)
11 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; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
24 /* Xt features made by Fred Pierresteguy. */
26 #include <config.h>
28 /* On 4.3 these lose if they come after xterm.h. */
29 /* Putting these at the beginning seems to be standard for other .c files. */
30 #include <signal.h>
32 #include <stdio.h>
34 #ifdef HAVE_X_WINDOWS
36 #include "lisp.h"
37 #include "blockinput.h"
39 /* Need syssignal.h for various externs and definitions that may be required
40 by some configurations for calls to signal later in this source file. */
41 #include "syssignal.h"
43 /* This may include sys/types.h, and that somehow loses
44 if this is not done before the other system files. */
45 #include "xterm.h"
46 #include <X11/cursorfont.h>
48 /* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
50 #ifndef makedev
51 #include <sys/types.h>
52 #endif /* makedev */
54 #ifdef BSD_SYSTEM
55 #include <sys/ioctl.h>
56 #endif /* ! defined (BSD_SYSTEM) */
58 #include "systty.h"
59 #include "systime.h"
61 #ifndef INCLUDED_FCNTL
62 #include <fcntl.h>
63 #endif
64 #include <ctype.h>
65 #include <errno.h>
66 #include <setjmp.h>
67 #include <sys/stat.h>
68 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
69 /* #include <sys/param.h> */
71 #include "charset.h"
72 #include "coding.h"
73 #include "ccl.h"
74 #include "frame.h"
75 #include "dispextern.h"
76 #include "fontset.h"
77 #include "termhooks.h"
78 #include "termopts.h"
79 #include "termchar.h"
80 #include "gnu.h"
81 #include "disptab.h"
82 #include "buffer.h"
83 #include "window.h"
84 #include "keyboard.h"
85 #include "intervals.h"
86 #include "process.h"
87 #include "atimer.h"
88 #include "keymap.h"
90 #ifdef USE_X_TOOLKIT
91 #include <X11/Shell.h>
92 #endif
94 #ifdef HAVE_SYS_TIME_H
95 #include <sys/time.h>
96 #endif
97 #ifdef HAVE_UNISTD_H
98 #include <unistd.h>
99 #endif
101 #ifdef USE_GTK
102 #include "gtkutil.h"
103 #endif
105 #ifdef USE_LUCID
106 extern int xlwmenu_window_p P_ ((Widget w, Window window));
107 extern void xlwmenu_redisplay P_ ((Widget));
108 #endif
110 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
112 extern void free_frame_menubar P_ ((struct frame *));
113 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
114 int));
115 #endif
117 #ifdef USE_X_TOOLKIT
118 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
119 #define HACK_EDITRES
120 extern void _XEditResCheckMessages ();
121 #endif /* not NO_EDITRES */
123 /* Include toolkit specific headers for the scroll bar widget. */
125 #ifdef USE_TOOLKIT_SCROLL_BARS
126 #if defined USE_MOTIF
127 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
128 #include <Xm/ScrollBar.h>
129 #else /* !USE_MOTIF i.e. use Xaw */
131 #ifdef HAVE_XAW3D
132 #include <X11/Xaw3d/Simple.h>
133 #include <X11/Xaw3d/Scrollbar.h>
134 #include <X11/Xaw3d/ThreeD.h>
135 #else /* !HAVE_XAW3D */
136 #include <X11/Xaw/Simple.h>
137 #include <X11/Xaw/Scrollbar.h>
138 #endif /* !HAVE_XAW3D */
139 #ifndef XtNpickTop
140 #define XtNpickTop "pickTop"
141 #endif /* !XtNpickTop */
142 #endif /* !USE_MOTIF */
143 #endif /* USE_TOOLKIT_SCROLL_BARS */
145 #endif /* USE_X_TOOLKIT */
147 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
148 #define x_any_window_to_frame x_window_to_frame
149 #define x_top_window_to_frame x_window_to_frame
150 #endif
152 #ifdef USE_X_TOOLKIT
153 #include "widget.h"
154 #ifndef XtNinitialState
155 #define XtNinitialState "initialState"
156 #endif
157 #endif
159 #undef abs
160 #define abs(x) ((x) < 0 ? -(x) : (x))
162 /* Default to using XIM if available. */
163 #ifdef USE_XIM
164 int use_xim = 1;
165 #else
166 int use_xim = 0; /* configure --without-xim */
167 #endif
171 /* Non-nil means Emacs uses toolkit scroll bars. */
173 Lisp_Object Vx_toolkit_scroll_bars;
175 /* Non-zero means that a HELP_EVENT has been generated since Emacs
176 start. */
178 static int any_help_event_p;
180 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
181 static Lisp_Object last_window;
183 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
185 int x_use_underline_position_properties;
187 /* Non-zero means to draw the underline at the same place as the descent line. */
189 int x_underline_at_descent_line;
191 /* This is a chain of structures for all the X displays currently in
192 use. */
194 struct x_display_info *x_display_list;
196 /* This is a list of cons cells, each of the form (NAME
197 . FONT-LIST-CACHE), one for each element of x_display_list and in
198 the same order. NAME is the name of the frame. FONT-LIST-CACHE
199 records previous values returned by x-list-fonts. */
201 Lisp_Object x_display_name_list;
203 /* Frame being updated by update_frame. This is declared in term.c.
204 This is set by update_begin and looked at by all the XT functions.
205 It is zero while not inside an update. In that case, the XT
206 functions assume that `selected_frame' is the frame to apply to. */
208 extern struct frame *updating_frame;
210 /* This is a frame waiting to be auto-raised, within XTread_socket. */
212 struct frame *pending_autoraise_frame;
214 #ifdef USE_X_TOOLKIT
215 /* The application context for Xt use. */
216 XtAppContext Xt_app_con;
217 static String Xt_default_resources[] = {0};
218 #endif /* USE_X_TOOLKIT */
220 /* Non-zero means user is interacting with a toolkit scroll bar. */
222 static int toolkit_scroll_bar_interaction;
224 /* Non-zero means to not move point as a result of clicking on a
225 frame to focus it (when focus-follows-mouse is nil). */
227 int x_mouse_click_focus_ignore_position;
229 /* Non-zero timeout value means ignore next mouse click if it arrives
230 before that timeout elapses (i.e. as part of the same sequence of
231 events resulting from clicking on a frame to select it). */
233 static unsigned long ignore_next_mouse_click_timeout;
235 /* Mouse movement.
237 Formerly, we used PointerMotionHintMask (in standard_event_mask)
238 so that we would have to call XQueryPointer after each MotionNotify
239 event to ask for another such event. However, this made mouse tracking
240 slow, and there was a bug that made it eventually stop.
242 Simply asking for MotionNotify all the time seems to work better.
244 In order to avoid asking for motion events and then throwing most
245 of them away or busy-polling the server for mouse positions, we ask
246 the server for pointer motion hints. This means that we get only
247 one event per group of mouse movements. "Groups" are delimited by
248 other kinds of events (focus changes and button clicks, for
249 example), or by XQueryPointer calls; when one of these happens, we
250 get another MotionNotify event the next time the mouse moves. This
251 is at least as efficient as getting motion events when mouse
252 tracking is on, and I suspect only negligibly worse when tracking
253 is off. */
255 /* Where the mouse was last time we reported a mouse event. */
257 static XRectangle last_mouse_glyph;
258 static FRAME_PTR last_mouse_glyph_frame;
259 static Lisp_Object last_mouse_press_frame;
261 /* The scroll bar in which the last X motion event occurred.
263 If the last X motion event occurred in a scroll bar, we set this so
264 XTmouse_position can know whether to report a scroll bar motion or
265 an ordinary motion.
267 If the last X motion event didn't occur in a scroll bar, we set
268 this to Qnil, to tell XTmouse_position to return an ordinary motion
269 event. */
271 static Lisp_Object last_mouse_scroll_bar;
273 /* This is a hack. We would really prefer that XTmouse_position would
274 return the time associated with the position it returns, but there
275 doesn't seem to be any way to wrest the time-stamp from the server
276 along with the position query. So, we just keep track of the time
277 of the last movement we received, and return that in hopes that
278 it's somewhat accurate. */
280 static Time last_mouse_movement_time;
282 /* Time for last user interaction as returned in X events. */
284 static Time last_user_time;
286 /* Incremented by XTread_socket whenever it really tries to read
287 events. */
289 #ifdef __STDC__
290 static int volatile input_signal_count;
291 #else
292 static int input_signal_count;
293 #endif
295 /* Used locally within XTread_socket. */
297 static int x_noop_count;
299 /* Initial values of argv and argc. */
301 extern char **initial_argv;
302 extern int initial_argc;
304 extern Lisp_Object Vcommand_line_args, Vsystem_name;
306 /* Tells if a window manager is present or not. */
308 extern Lisp_Object Vx_no_window_manager;
310 extern Lisp_Object Qeql;
312 extern int errno;
314 /* A mask of extra modifier bits to put into every keyboard char. */
316 extern EMACS_INT extra_keyboard_modifiers;
318 /* The keysyms to use for the various modifiers. */
320 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
321 Lisp_Object Vx_keysym_table;
322 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
324 static Lisp_Object Qvendor_specific_keysyms;
325 static Lisp_Object Qlatin_1;
327 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
328 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
330 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
331 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
332 static const XColor *x_color_cells P_ ((Display *, int *));
333 static void x_update_window_end P_ ((struct window *, int, int));
335 static int x_io_error_quitter P_ ((Display *));
336 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
337 static int x_compute_min_glyph_bounds P_ ((struct frame *));
338 static void x_update_end P_ ((struct frame *));
339 static void XTframe_up_to_date P_ ((struct frame *));
340 static void XTset_terminal_modes P_ ((void));
341 static void XTreset_terminal_modes P_ ((void));
342 static void x_clear_frame P_ ((void));
343 static void frame_highlight P_ ((struct frame *));
344 static void frame_unhighlight P_ ((struct frame *));
345 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
346 static void x_focus_changed P_ ((int, int, struct x_display_info *,
347 struct frame *, struct input_event *));
348 static void x_detect_focus_change P_ ((struct x_display_info *,
349 XEvent *, struct input_event *));
350 static void XTframe_rehighlight P_ ((struct frame *));
351 static void x_frame_rehighlight P_ ((struct x_display_info *));
352 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
353 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
354 enum text_cursor_kinds));
356 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
357 static void x_flush P_ ((struct frame *f));
358 static void x_update_begin P_ ((struct frame *));
359 static void x_update_window_begin P_ ((struct window *));
360 static void x_after_update_window_line P_ ((struct glyph_row *));
361 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
362 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
363 enum scroll_bar_part *,
364 Lisp_Object *, Lisp_Object *,
365 unsigned long *));
366 static void x_check_fullscreen P_ ((struct frame *));
367 static void x_check_expected_move P_ ((struct frame *, int, int));
368 static void x_sync_with_move P_ ((struct frame *, int, int, int));
369 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
370 int *, struct input_event *));
371 /* Don't declare this NO_RETURN because we want no
372 interference with debugging failing X calls. */
373 static SIGTYPE x_connection_closed P_ ((Display *, char *));
376 /* Flush display of frame F, or of all frames if F is null. */
378 static void
379 x_flush (f)
380 struct frame *f;
382 BLOCK_INPUT;
383 if (f == NULL)
385 Lisp_Object rest, frame;
386 FOR_EACH_FRAME (rest, frame)
387 x_flush (XFRAME (frame));
389 else if (FRAME_X_P (f))
390 XFlush (FRAME_X_DISPLAY (f));
391 UNBLOCK_INPUT;
395 /* Remove calls to XFlush by defining XFlush to an empty replacement.
396 Calls to XFlush should be unnecessary because the X output buffer
397 is flushed automatically as needed by calls to XPending,
398 XNextEvent, or XWindowEvent according to the XFlush man page.
399 XTread_socket calls XPending. Removing XFlush improves
400 performance. */
402 #define XFlush(DISPLAY) (void) 0
405 /***********************************************************************
406 Debugging
407 ***********************************************************************/
409 #if 0
411 /* This is a function useful for recording debugging information about
412 the sequence of occurrences in this file. */
414 struct record
416 char *locus;
417 int type;
420 struct record event_record[100];
422 int event_record_index;
424 record_event (locus, type)
425 char *locus;
426 int type;
428 if (event_record_index == sizeof (event_record) / sizeof (struct record))
429 event_record_index = 0;
431 event_record[event_record_index].locus = locus;
432 event_record[event_record_index].type = type;
433 event_record_index++;
436 #endif /* 0 */
440 /* Return the struct x_display_info corresponding to DPY. */
442 struct x_display_info *
443 x_display_info_for_display (dpy)
444 Display *dpy;
446 struct x_display_info *dpyinfo;
448 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
449 if (dpyinfo->display == dpy)
450 return dpyinfo;
452 return 0;
457 /***********************************************************************
458 Starting and ending an update
459 ***********************************************************************/
461 /* Start an update of frame F. This function is installed as a hook
462 for update_begin, i.e. it is called when update_begin is called.
463 This function is called prior to calls to x_update_window_begin for
464 each window being updated. Currently, there is nothing to do here
465 because all interesting stuff is done on a window basis. */
467 static void
468 x_update_begin (f)
469 struct frame *f;
471 /* Nothing to do. */
475 /* Start update of window W. Set the global variable updated_window
476 to the window being updated and set output_cursor to the cursor
477 position of W. */
479 static void
480 x_update_window_begin (w)
481 struct window *w;
483 struct frame *f = XFRAME (WINDOW_FRAME (w));
484 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
486 updated_window = w;
487 set_output_cursor (&w->cursor);
489 BLOCK_INPUT;
491 if (f == display_info->mouse_face_mouse_frame)
493 /* Don't do highlighting for mouse motion during the update. */
494 display_info->mouse_face_defer = 1;
496 /* If F needs to be redrawn, simply forget about any prior mouse
497 highlighting. */
498 if (FRAME_GARBAGED_P (f))
499 display_info->mouse_face_window = Qnil;
501 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
502 their mouse_face_p flag set, which means that they are always
503 unequal to rows in a desired matrix which never have that
504 flag set. So, rows containing mouse-face glyphs are never
505 scrolled, and we don't have to switch the mouse highlight off
506 here to prevent it from being scrolled. */
508 /* Can we tell that this update does not affect the window
509 where the mouse highlight is? If so, no need to turn off.
510 Likewise, don't do anything if the frame is garbaged;
511 in that case, the frame's current matrix that we would use
512 is all wrong, and we will redisplay that line anyway. */
513 if (!NILP (display_info->mouse_face_window)
514 && w == XWINDOW (display_info->mouse_face_window))
516 int i;
518 for (i = 0; i < w->desired_matrix->nrows; ++i)
519 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
520 break;
522 if (i < w->desired_matrix->nrows)
523 clear_mouse_face (display_info);
525 #endif /* 0 */
528 UNBLOCK_INPUT;
532 /* Draw a vertical window border from (x,y0) to (x,y1) */
534 static void
535 x_draw_vertical_window_border (w, x, y0, y1)
536 struct window *w;
537 int x, y0, y1;
539 struct frame *f = XFRAME (WINDOW_FRAME (w));
540 struct face *face;
542 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
543 if (face)
544 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
545 face->foreground);
547 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
548 f->output_data.x->normal_gc, x, y0, x, y1);
551 /* End update of window W (which is equal to updated_window).
553 Draw vertical borders between horizontally adjacent windows, and
554 display W's cursor if CURSOR_ON_P is non-zero.
556 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
557 glyphs in mouse-face were overwritten. In that case we have to
558 make sure that the mouse-highlight is properly redrawn.
560 W may be a menu bar pseudo-window in case we don't have X toolkit
561 support. Such windows don't have a cursor, so don't display it
562 here. */
564 static void
565 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
566 struct window *w;
567 int cursor_on_p, mouse_face_overwritten_p;
569 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
571 if (!w->pseudo_window_p)
573 BLOCK_INPUT;
575 if (cursor_on_p)
576 display_and_set_cursor (w, 1, output_cursor.hpos,
577 output_cursor.vpos,
578 output_cursor.x, output_cursor.y);
580 if (draw_window_fringes (w, 1))
581 x_draw_vertical_border (w);
583 UNBLOCK_INPUT;
586 /* If a row with mouse-face was overwritten, arrange for
587 XTframe_up_to_date to redisplay the mouse highlight. */
588 if (mouse_face_overwritten_p)
590 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
591 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
592 dpyinfo->mouse_face_window = Qnil;
595 updated_window = NULL;
599 /* End update of frame F. This function is installed as a hook in
600 update_end. */
602 static void
603 x_update_end (f)
604 struct frame *f;
606 /* Mouse highlight may be displayed again. */
607 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
609 #ifndef XFlush
610 BLOCK_INPUT;
611 XFlush (FRAME_X_DISPLAY (f));
612 UNBLOCK_INPUT;
613 #endif
617 /* This function is called from various places in xdisp.c whenever a
618 complete update has been performed. The global variable
619 updated_window is not available here. */
621 static void
622 XTframe_up_to_date (f)
623 struct frame *f;
625 if (FRAME_X_P (f))
627 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
629 if (dpyinfo->mouse_face_deferred_gc
630 || f == dpyinfo->mouse_face_mouse_frame)
632 BLOCK_INPUT;
633 if (dpyinfo->mouse_face_mouse_frame)
634 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
635 dpyinfo->mouse_face_mouse_x,
636 dpyinfo->mouse_face_mouse_y);
637 dpyinfo->mouse_face_deferred_gc = 0;
638 UNBLOCK_INPUT;
644 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
645 arrow bitmaps, or clear the fringes if no bitmaps are required
646 before DESIRED_ROW is made current. The window being updated is
647 found in updated_window. This function It is called from
648 update_window_line only if it is known that there are differences
649 between bitmaps to be drawn between current row and DESIRED_ROW. */
651 static void
652 x_after_update_window_line (desired_row)
653 struct glyph_row *desired_row;
655 struct window *w = updated_window;
656 struct frame *f;
657 int width, height;
659 xassert (w);
661 if (!desired_row->mode_line_p && !w->pseudo_window_p)
662 desired_row->redraw_fringe_bitmaps_p = 1;
664 /* When a window has disappeared, make sure that no rest of
665 full-width rows stays visible in the internal border. Could
666 check here if updated_window is the leftmost/rightmost window,
667 but I guess it's not worth doing since vertically split windows
668 are almost never used, internal border is rarely set, and the
669 overhead is very small. */
670 if (windows_or_buffers_changed
671 && desired_row->full_width_p
672 && (f = XFRAME (w->frame),
673 width = FRAME_INTERNAL_BORDER_WIDTH (f),
674 width != 0)
675 && (height = desired_row->visible_height,
676 height > 0))
678 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
680 /* Internal border is drawn below the tool bar. */
681 if (WINDOWP (f->tool_bar_window)
682 && w == XWINDOW (f->tool_bar_window))
683 y -= width;
685 BLOCK_INPUT;
686 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
687 0, y, width, height, False);
688 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
689 FRAME_PIXEL_WIDTH (f) - width,
690 y, width, height, False);
691 UNBLOCK_INPUT;
695 static void
696 x_draw_fringe_bitmap (w, row, p)
697 struct window *w;
698 struct glyph_row *row;
699 struct draw_fringe_bitmap_params *p;
701 struct frame *f = XFRAME (WINDOW_FRAME (w));
702 Display *display = FRAME_X_DISPLAY (f);
703 Window window = FRAME_X_WINDOW (f);
704 GC gc = f->output_data.x->normal_gc;
705 struct face *face = p->face;
706 int rowY;
708 /* Must clip because of partially visible lines. */
709 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
710 if (p->y < rowY)
712 /* Adjust position of "bottom aligned" bitmap on partially
713 visible last row. */
714 int oldY = row->y;
715 int oldVH = row->visible_height;
716 row->visible_height = p->h;
717 row->y -= rowY - p->y;
718 x_clip_to_row (w, row, -1, gc);
719 row->y = oldY;
720 row->visible_height = oldVH;
722 else
723 x_clip_to_row (w, row, -1, gc);
725 if (p->bx >= 0 && !p->overlay_p)
727 /* In case the same realized face is used for fringes and
728 for something displayed in the text (e.g. face `region' on
729 mono-displays, the fill style may have been changed to
730 FillSolid in x_draw_glyph_string_background. */
731 if (face->stipple)
732 XSetFillStyle (display, face->gc, FillOpaqueStippled);
733 else
734 XSetForeground (display, face->gc, face->background);
736 XFillRectangle (display, window, face->gc,
737 p->bx, p->by, p->nx, p->ny);
739 if (!face->stipple)
740 XSetForeground (display, face->gc, face->foreground);
743 if (p->which)
745 unsigned char *bits;
746 Pixmap pixmap, clipmask = (Pixmap) 0;
747 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
748 XGCValues gcv;
750 if (p->wd > 8)
751 bits = (unsigned char *)(p->bits + p->dh);
752 else
753 bits = (unsigned char *)p->bits + p->dh;
755 /* Draw the bitmap. I believe these small pixmaps can be cached
756 by the server. */
757 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
758 (p->cursor_p
759 ? (p->overlay_p ? face->background
760 : f->output_data.x->cursor_pixel)
761 : face->foreground),
762 face->background, depth);
764 if (p->overlay_p)
766 clipmask = XCreatePixmapFromBitmapData (display,
767 FRAME_X_DISPLAY_INFO (f)->root_window,
768 bits, p->wd, p->h,
769 1, 0, 1);
770 gcv.clip_mask = clipmask;
771 gcv.clip_x_origin = p->x;
772 gcv.clip_y_origin = p->y;
773 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
776 XCopyArea (display, pixmap, window, gc, 0, 0,
777 p->wd, p->h, p->x, p->y);
778 XFreePixmap (display, pixmap);
780 if (p->overlay_p)
782 gcv.clip_mask = (Pixmap) 0;
783 XChangeGC (display, gc, GCClipMask, &gcv);
784 XFreePixmap (display, clipmask);
788 XSetClipMask (display, gc, None);
793 /* This is called when starting Emacs and when restarting after
794 suspend. When starting Emacs, no X window is mapped. And nothing
795 must be done to Emacs's own window if it is suspended (though that
796 rarely happens). */
798 static void
799 XTset_terminal_modes ()
803 /* This is called when exiting or suspending Emacs. Exiting will make
804 the X-windows go away, and suspending requires no action. */
806 static void
807 XTreset_terminal_modes ()
813 /***********************************************************************
814 Display Iterator
815 ***********************************************************************/
817 /* Function prototypes of this page. */
819 static int x_encode_char P_ ((int, XChar2b *, struct font_info *, int *));
822 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
823 is not contained in the font. */
825 static XCharStruct *
826 x_per_char_metric (font, char2b, font_type)
827 XFontStruct *font;
828 XChar2b *char2b;
829 int font_type; /* unused on X */
831 /* The result metric information. */
832 XCharStruct *pcm = NULL;
834 xassert (font && char2b);
836 if (font->per_char != NULL)
838 if (font->min_byte1 == 0 && font->max_byte1 == 0)
840 /* min_char_or_byte2 specifies the linear character index
841 corresponding to the first element of the per_char array,
842 max_char_or_byte2 is the index of the last character. A
843 character with non-zero CHAR2B->byte1 is not in the font.
844 A character with byte2 less than min_char_or_byte2 or
845 greater max_char_or_byte2 is not in the font. */
846 if (char2b->byte1 == 0
847 && char2b->byte2 >= font->min_char_or_byte2
848 && char2b->byte2 <= font->max_char_or_byte2)
849 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
851 else
853 /* If either min_byte1 or max_byte1 are nonzero, both
854 min_char_or_byte2 and max_char_or_byte2 are less than
855 256, and the 2-byte character index values corresponding
856 to the per_char array element N (counting from 0) are:
858 byte1 = N/D + min_byte1
859 byte2 = N\D + min_char_or_byte2
861 where:
863 D = max_char_or_byte2 - min_char_or_byte2 + 1
864 / = integer division
865 \ = integer modulus */
866 if (char2b->byte1 >= font->min_byte1
867 && char2b->byte1 <= font->max_byte1
868 && char2b->byte2 >= font->min_char_or_byte2
869 && char2b->byte2 <= font->max_char_or_byte2)
871 pcm = (font->per_char
872 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
873 * (char2b->byte1 - font->min_byte1))
874 + (char2b->byte2 - font->min_char_or_byte2));
878 else
880 /* If the per_char pointer is null, all glyphs between the first
881 and last character indexes inclusive have the same
882 information, as given by both min_bounds and max_bounds. */
883 if (char2b->byte2 >= font->min_char_or_byte2
884 && char2b->byte2 <= font->max_char_or_byte2)
885 pcm = &font->max_bounds;
888 return ((pcm == NULL
889 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
890 ? NULL : pcm);
894 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
895 the two-byte form of C. Encoding is returned in *CHAR2B. */
897 static int
898 x_encode_char (c, char2b, font_info, two_byte_p)
899 int c;
900 XChar2b *char2b;
901 struct font_info *font_info;
902 int *two_byte_p;
904 int charset = CHAR_CHARSET (c);
905 XFontStruct *font = font_info->font;
907 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
908 This may be either a program in a special encoder language or a
909 fixed encoding. */
910 if (font_info->font_encoder)
912 /* It's a program. */
913 struct ccl_program *ccl = font_info->font_encoder;
915 check_ccl_update (ccl);
916 if (CHARSET_DIMENSION (charset) == 1)
918 ccl->reg[0] = charset;
919 ccl->reg[1] = char2b->byte2;
920 ccl->reg[2] = -1;
922 else
924 ccl->reg[0] = charset;
925 ccl->reg[1] = char2b->byte1;
926 ccl->reg[2] = char2b->byte2;
929 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
931 /* We assume that MSBs are appropriately set/reset by CCL
932 program. */
933 if (font->max_byte1 == 0) /* 1-byte font */
934 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
935 else
936 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
938 else if (font_info->encoding[charset])
940 /* Fixed encoding scheme. See fontset.h for the meaning of the
941 encoding numbers. */
942 int enc = font_info->encoding[charset];
944 if ((enc == 1 || enc == 2)
945 && CHARSET_DIMENSION (charset) == 2)
946 char2b->byte1 |= 0x80;
948 if (enc == 1 || enc == 3)
949 char2b->byte2 |= 0x80;
952 if (two_byte_p)
953 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
955 return FONT_TYPE_UNKNOWN;
960 /***********************************************************************
961 Glyph display
962 ***********************************************************************/
966 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
967 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
968 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
969 int));
970 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
971 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
972 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
973 static void x_draw_glyph_string P_ ((struct glyph_string *));
974 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
975 static void x_set_cursor_gc P_ ((struct glyph_string *));
976 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
977 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
978 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
979 unsigned long *, double, int));
980 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
981 double, int, unsigned long));
982 static void x_setup_relief_colors P_ ((struct glyph_string *));
983 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
984 static void x_draw_image_relief P_ ((struct glyph_string *));
985 static void x_draw_image_foreground P_ ((struct glyph_string *));
986 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
987 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
988 int, int, int));
989 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
990 int, int, int, int, int, int,
991 XRectangle *));
992 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
993 int, int, int, XRectangle *));
995 #if GLYPH_DEBUG
996 static void x_check_font P_ ((struct frame *, XFontStruct *));
997 #endif
1000 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1001 face. */
1003 static void
1004 x_set_cursor_gc (s)
1005 struct glyph_string *s;
1007 if (s->font == FRAME_FONT (s->f)
1008 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1009 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1010 && !s->cmp)
1011 s->gc = s->f->output_data.x->cursor_gc;
1012 else
1014 /* Cursor on non-default face: must merge. */
1015 XGCValues xgcv;
1016 unsigned long mask;
1018 xgcv.background = s->f->output_data.x->cursor_pixel;
1019 xgcv.foreground = s->face->background;
1021 /* If the glyph would be invisible, try a different foreground. */
1022 if (xgcv.foreground == xgcv.background)
1023 xgcv.foreground = s->face->foreground;
1024 if (xgcv.foreground == xgcv.background)
1025 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1026 if (xgcv.foreground == xgcv.background)
1027 xgcv.foreground = s->face->foreground;
1029 /* Make sure the cursor is distinct from text in this face. */
1030 if (xgcv.background == s->face->background
1031 && xgcv.foreground == s->face->foreground)
1033 xgcv.background = s->face->foreground;
1034 xgcv.foreground = s->face->background;
1037 IF_DEBUG (x_check_font (s->f, s->font));
1038 xgcv.font = s->font->fid;
1039 xgcv.graphics_exposures = False;
1040 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1042 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1043 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1044 mask, &xgcv);
1045 else
1046 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1047 = XCreateGC (s->display, s->window, mask, &xgcv);
1049 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1054 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1056 static void
1057 x_set_mouse_face_gc (s)
1058 struct glyph_string *s;
1060 int face_id;
1061 struct face *face;
1063 /* What face has to be used last for the mouse face? */
1064 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1065 face = FACE_FROM_ID (s->f, face_id);
1066 if (face == NULL)
1067 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1069 if (s->first_glyph->type == CHAR_GLYPH)
1070 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
1071 else
1072 face_id = FACE_FOR_CHAR (s->f, face, 0);
1073 s->face = FACE_FROM_ID (s->f, face_id);
1074 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1076 /* If font in this face is same as S->font, use it. */
1077 if (s->font == s->face->font)
1078 s->gc = s->face->gc;
1079 else
1081 /* Otherwise construct scratch_cursor_gc with values from FACE
1082 but font FONT. */
1083 XGCValues xgcv;
1084 unsigned long mask;
1086 xgcv.background = s->face->background;
1087 xgcv.foreground = s->face->foreground;
1088 IF_DEBUG (x_check_font (s->f, s->font));
1089 xgcv.font = s->font->fid;
1090 xgcv.graphics_exposures = False;
1091 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1093 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1094 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1095 mask, &xgcv);
1096 else
1097 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1098 = XCreateGC (s->display, s->window, mask, &xgcv);
1100 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1103 xassert (s->gc != 0);
1107 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1108 Faces to use in the mode line have already been computed when the
1109 matrix was built, so there isn't much to do, here. */
1111 static INLINE void
1112 x_set_mode_line_face_gc (s)
1113 struct glyph_string *s;
1115 s->gc = s->face->gc;
1119 /* Set S->gc of glyph string S for drawing that glyph string. Set
1120 S->stippled_p to a non-zero value if the face of S has a stipple
1121 pattern. */
1123 static INLINE void
1124 x_set_glyph_string_gc (s)
1125 struct glyph_string *s;
1127 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1129 if (s->hl == DRAW_NORMAL_TEXT)
1131 s->gc = s->face->gc;
1132 s->stippled_p = s->face->stipple != 0;
1134 else if (s->hl == DRAW_INVERSE_VIDEO)
1136 x_set_mode_line_face_gc (s);
1137 s->stippled_p = s->face->stipple != 0;
1139 else if (s->hl == DRAW_CURSOR)
1141 x_set_cursor_gc (s);
1142 s->stippled_p = 0;
1144 else if (s->hl == DRAW_MOUSE_FACE)
1146 x_set_mouse_face_gc (s);
1147 s->stippled_p = s->face->stipple != 0;
1149 else if (s->hl == DRAW_IMAGE_RAISED
1150 || s->hl == DRAW_IMAGE_SUNKEN)
1152 s->gc = s->face->gc;
1153 s->stippled_p = s->face->stipple != 0;
1155 else
1157 s->gc = s->face->gc;
1158 s->stippled_p = s->face->stipple != 0;
1161 /* GC must have been set. */
1162 xassert (s->gc != 0);
1166 /* Set clipping for output of glyph string S. S may be part of a mode
1167 line or menu if we don't have X toolkit support. */
1169 static INLINE void
1170 x_set_glyph_string_clipping (s)
1171 struct glyph_string *s;
1173 XRectangle r;
1174 get_glyph_string_clip_rect (s, &r);
1175 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
1179 /* RIF:
1180 Compute left and right overhang of glyph string S. If S is a glyph
1181 string for a composition, assume overhangs don't exist. */
1183 static void
1184 x_compute_glyph_string_overhangs (s)
1185 struct glyph_string *s;
1187 if (s->cmp == NULL
1188 && s->first_glyph->type == CHAR_GLYPH)
1190 XCharStruct cs;
1191 int direction, font_ascent, font_descent;
1192 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1193 &font_ascent, &font_descent, &cs);
1194 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1195 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1200 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1202 static INLINE void
1203 x_clear_glyph_string_rect (s, x, y, w, h)
1204 struct glyph_string *s;
1205 int x, y, w, h;
1207 XGCValues xgcv;
1208 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1209 XSetForeground (s->display, s->gc, xgcv.background);
1210 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1211 XSetForeground (s->display, s->gc, xgcv.foreground);
1215 /* Draw the background of glyph_string S. If S->background_filled_p
1216 is non-zero don't draw it. FORCE_P non-zero means draw the
1217 background even if it wouldn't be drawn normally. This is used
1218 when a string preceding S draws into the background of S, or S
1219 contains the first component of a composition. */
1221 static void
1222 x_draw_glyph_string_background (s, force_p)
1223 struct glyph_string *s;
1224 int force_p;
1226 /* Nothing to do if background has already been drawn or if it
1227 shouldn't be drawn in the first place. */
1228 if (!s->background_filled_p)
1230 int box_line_width = max (s->face->box_line_width, 0);
1232 if (s->stippled_p)
1234 /* Fill background with a stipple pattern. */
1235 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1236 XFillRectangle (s->display, s->window, s->gc, s->x,
1237 s->y + box_line_width,
1238 s->background_width,
1239 s->height - 2 * box_line_width);
1240 XSetFillStyle (s->display, s->gc, FillSolid);
1241 s->background_filled_p = 1;
1243 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1244 || s->font_not_found_p
1245 || s->extends_to_end_of_line_p
1246 || force_p)
1248 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1249 s->background_width,
1250 s->height - 2 * box_line_width);
1251 s->background_filled_p = 1;
1257 /* Draw the foreground of glyph string S. */
1259 static void
1260 x_draw_glyph_string_foreground (s)
1261 struct glyph_string *s;
1263 int i, x;
1265 /* If first glyph of S has a left box line, start drawing the text
1266 of S to the right of that box line. */
1267 if (s->face->box != FACE_NO_BOX
1268 && s->first_glyph->left_box_line_p)
1269 x = s->x + abs (s->face->box_line_width);
1270 else
1271 x = s->x;
1273 /* Draw characters of S as rectangles if S's font could not be
1274 loaded. */
1275 if (s->font_not_found_p)
1277 for (i = 0; i < s->nchars; ++i)
1279 struct glyph *g = s->first_glyph + i;
1280 XDrawRectangle (s->display, s->window,
1281 s->gc, x, s->y, g->pixel_width - 1,
1282 s->height - 1);
1283 x += g->pixel_width;
1286 else
1288 char *char1b = (char *) s->char2b;
1289 int boff = s->font_info->baseline_offset;
1291 if (s->font_info->vertical_centering)
1292 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1294 /* If we can use 8-bit functions, condense S->char2b. */
1295 if (!s->two_byte_p)
1296 for (i = 0; i < s->nchars; ++i)
1297 char1b[i] = s->char2b[i].byte2;
1299 /* Draw text with XDrawString if background has already been
1300 filled. Otherwise, use XDrawImageString. (Note that
1301 XDrawImageString is usually faster than XDrawString.) Always
1302 use XDrawImageString when drawing the cursor so that there is
1303 no chance that characters under a box cursor are invisible. */
1304 if (s->for_overlaps
1305 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1307 /* Draw characters with 16-bit or 8-bit functions. */
1308 if (s->two_byte_p)
1309 XDrawString16 (s->display, s->window, s->gc, x,
1310 s->ybase - boff, s->char2b, s->nchars);
1311 else
1312 XDrawString (s->display, s->window, s->gc, x,
1313 s->ybase - boff, char1b, s->nchars);
1315 else
1317 if (s->two_byte_p)
1318 XDrawImageString16 (s->display, s->window, s->gc, x,
1319 s->ybase - boff, s->char2b, s->nchars);
1320 else
1321 XDrawImageString (s->display, s->window, s->gc, x,
1322 s->ybase - boff, char1b, s->nchars);
1325 if (s->face->overstrike)
1327 /* For overstriking (to simulate bold-face), draw the
1328 characters again shifted to the right by one pixel. */
1329 if (s->two_byte_p)
1330 XDrawString16 (s->display, s->window, s->gc, x + 1,
1331 s->ybase - boff, s->char2b, s->nchars);
1332 else
1333 XDrawString (s->display, s->window, s->gc, x + 1,
1334 s->ybase - boff, char1b, s->nchars);
1339 /* Draw the foreground of composite glyph string S. */
1341 static void
1342 x_draw_composite_glyph_string_foreground (s)
1343 struct glyph_string *s;
1345 int i, x;
1347 /* If first glyph of S has a left box line, start drawing the text
1348 of S to the right of that box line. */
1349 if (s->face->box != FACE_NO_BOX
1350 && s->first_glyph->left_box_line_p)
1351 x = s->x + abs (s->face->box_line_width);
1352 else
1353 x = s->x;
1355 /* S is a glyph string for a composition. S->gidx is the index of
1356 the first character drawn for glyphs of this composition.
1357 S->gidx == 0 means we are drawing the very first character of
1358 this composition. */
1360 /* Draw a rectangle for the composition if the font for the very
1361 first character of the composition could not be loaded. */
1362 if (s->font_not_found_p)
1364 if (s->gidx == 0)
1365 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1366 s->width - 1, s->height - 1);
1368 else
1370 for (i = 0; i < s->nchars; i++, ++s->gidx)
1372 XDrawString16 (s->display, s->window, s->gc,
1373 x + s->cmp->offsets[s->gidx * 2],
1374 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1375 s->char2b + i, 1);
1376 if (s->face->overstrike)
1377 XDrawString16 (s->display, s->window, s->gc,
1378 x + s->cmp->offsets[s->gidx * 2] + 1,
1379 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
1380 s->char2b + i, 1);
1386 #ifdef USE_X_TOOLKIT
1388 static struct frame *x_frame_of_widget P_ ((Widget));
1389 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1390 XrmValue *, XrmValue *, XtPointer *));
1391 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1392 XrmValue *, Cardinal *));
1395 /* Return the frame on which widget WIDGET is used.. Abort if frame
1396 cannot be determined. */
1398 static struct frame *
1399 x_frame_of_widget (widget)
1400 Widget widget;
1402 struct x_display_info *dpyinfo;
1403 Lisp_Object tail;
1404 struct frame *f;
1406 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1408 /* Find the top-level shell of the widget. Note that this function
1409 can be called when the widget is not yet realized, so XtWindow
1410 (widget) == 0. That's the reason we can't simply use
1411 x_any_window_to_frame. */
1412 while (!XtIsTopLevelShell (widget))
1413 widget = XtParent (widget);
1415 /* Look for a frame with that top-level widget. Allocate the color
1416 on that frame to get the right gamma correction value. */
1417 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
1418 if (GC_FRAMEP (XCAR (tail))
1419 && (f = XFRAME (XCAR (tail)),
1420 (f->output_data.nothing != 1
1421 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1422 && f->output_data.x->widget == widget)
1423 return f;
1425 abort ();
1429 /* Allocate the color COLOR->pixel on the screen and display of
1430 widget WIDGET in colormap CMAP. If an exact match cannot be
1431 allocated, try the nearest color available. Value is non-zero
1432 if successful. This is called from lwlib. */
1435 x_alloc_nearest_color_for_widget (widget, cmap, color)
1436 Widget widget;
1437 Colormap cmap;
1438 XColor *color;
1440 struct frame *f = x_frame_of_widget (widget);
1441 return x_alloc_nearest_color (f, cmap, color);
1445 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1446 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1447 If this produces the same color as PIXEL, try a color where all RGB
1448 values have DELTA added. Return the allocated color in *PIXEL.
1449 DISPLAY is the X display, CMAP is the colormap to operate on.
1450 Value is non-zero if successful. */
1453 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1454 Widget widget;
1455 Display *display;
1456 Colormap cmap;
1457 unsigned long *pixel;
1458 double factor;
1459 int delta;
1461 struct frame *f = x_frame_of_widget (widget);
1462 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1466 /* Structure specifying which arguments should be passed by Xt to
1467 cvt_string_to_pixel. We want the widget's screen and colormap. */
1469 static XtConvertArgRec cvt_string_to_pixel_args[] =
1471 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1472 sizeof (Screen *)},
1473 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1474 sizeof (Colormap)}
1478 /* The address of this variable is returned by
1479 cvt_string_to_pixel. */
1481 static Pixel cvt_string_to_pixel_value;
1484 /* Convert a color name to a pixel color.
1486 DPY is the display we are working on.
1488 ARGS is an array of *NARGS XrmValue structures holding additional
1489 information about the widget for which the conversion takes place.
1490 The contents of this array are determined by the specification
1491 in cvt_string_to_pixel_args.
1493 FROM is a pointer to an XrmValue which points to the color name to
1494 convert. TO is an XrmValue in which to return the pixel color.
1496 CLOSURE_RET is a pointer to user-data, in which we record if
1497 we allocated the color or not.
1499 Value is True if successful, False otherwise. */
1501 static Boolean
1502 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1503 Display *dpy;
1504 XrmValue *args;
1505 Cardinal *nargs;
1506 XrmValue *from, *to;
1507 XtPointer *closure_ret;
1509 Screen *screen;
1510 Colormap cmap;
1511 Pixel pixel;
1512 String color_name;
1513 XColor color;
1515 if (*nargs != 2)
1517 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1518 "wrongParameters", "cvt_string_to_pixel",
1519 "XtToolkitError",
1520 "Screen and colormap args required", NULL, NULL);
1521 return False;
1524 screen = *(Screen **) args[0].addr;
1525 cmap = *(Colormap *) args[1].addr;
1526 color_name = (String) from->addr;
1528 if (strcmp (color_name, XtDefaultBackground) == 0)
1530 *closure_ret = (XtPointer) False;
1531 pixel = WhitePixelOfScreen (screen);
1533 else if (strcmp (color_name, XtDefaultForeground) == 0)
1535 *closure_ret = (XtPointer) False;
1536 pixel = BlackPixelOfScreen (screen);
1538 else if (XParseColor (dpy, cmap, color_name, &color)
1539 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1541 pixel = color.pixel;
1542 *closure_ret = (XtPointer) True;
1544 else
1546 String params[1];
1547 Cardinal nparams = 1;
1549 params[0] = color_name;
1550 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1551 "badValue", "cvt_string_to_pixel",
1552 "XtToolkitError", "Invalid color `%s'",
1553 params, &nparams);
1554 return False;
1557 if (to->addr != NULL)
1559 if (to->size < sizeof (Pixel))
1561 to->size = sizeof (Pixel);
1562 return False;
1565 *(Pixel *) to->addr = pixel;
1567 else
1569 cvt_string_to_pixel_value = pixel;
1570 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1573 to->size = sizeof (Pixel);
1574 return True;
1578 /* Free a pixel color which was previously allocated via
1579 cvt_string_to_pixel. This is registered as the destructor
1580 for this type of resource via XtSetTypeConverter.
1582 APP is the application context in which we work.
1584 TO is a pointer to an XrmValue holding the color to free.
1585 CLOSURE is the value we stored in CLOSURE_RET for this color
1586 in cvt_string_to_pixel.
1588 ARGS and NARGS are like for cvt_string_to_pixel. */
1590 static void
1591 cvt_pixel_dtor (app, to, closure, args, nargs)
1592 XtAppContext app;
1593 XrmValuePtr to;
1594 XtPointer closure;
1595 XrmValuePtr args;
1596 Cardinal *nargs;
1598 if (*nargs != 2)
1600 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1601 "XtToolkitError",
1602 "Screen and colormap arguments required",
1603 NULL, NULL);
1605 else if (closure != NULL)
1607 /* We did allocate the pixel, so free it. */
1608 Screen *screen = *(Screen **) args[0].addr;
1609 Colormap cmap = *(Colormap *) args[1].addr;
1610 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1611 (Pixel *) to->addr, 1);
1616 #endif /* USE_X_TOOLKIT */
1619 /* Value is an array of XColor structures for the contents of the
1620 color map of display DPY. Set *NCELLS to the size of the array.
1621 Note that this probably shouldn't be called for large color maps,
1622 say a 24-bit TrueColor map. */
1624 static const XColor *
1625 x_color_cells (dpy, ncells)
1626 Display *dpy;
1627 int *ncells;
1629 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1631 if (dpyinfo->color_cells == NULL)
1633 Screen *screen = dpyinfo->screen;
1634 int i;
1636 dpyinfo->ncolor_cells
1637 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1638 dpyinfo->color_cells
1639 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1640 * sizeof *dpyinfo->color_cells);
1642 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1643 dpyinfo->color_cells[i].pixel = i;
1645 XQueryColors (dpy, dpyinfo->cmap,
1646 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1649 *ncells = dpyinfo->ncolor_cells;
1650 return dpyinfo->color_cells;
1654 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1655 colors in COLORS. Use cached information, if available. */
1657 void
1658 x_query_colors (f, colors, ncolors)
1659 struct frame *f;
1660 XColor *colors;
1661 int ncolors;
1663 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1665 if (dpyinfo->color_cells)
1667 int i;
1668 for (i = 0; i < ncolors; ++i)
1670 unsigned long pixel = colors[i].pixel;
1671 xassert (pixel < dpyinfo->ncolor_cells);
1672 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1673 colors[i] = dpyinfo->color_cells[pixel];
1676 else
1677 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1681 /* On frame F, translate pixel color to RGB values for the color in
1682 COLOR. Use cached information, if available. */
1684 void
1685 x_query_color (f, color)
1686 struct frame *f;
1687 XColor *color;
1689 x_query_colors (f, color, 1);
1693 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1694 exact match can't be allocated, try the nearest color available.
1695 Value is non-zero if successful. Set *COLOR to the color
1696 allocated. */
1698 static int
1699 x_alloc_nearest_color_1 (dpy, cmap, color)
1700 Display *dpy;
1701 Colormap cmap;
1702 XColor *color;
1704 int rc;
1706 rc = XAllocColor (dpy, cmap, color);
1707 if (rc == 0)
1709 /* If we got to this point, the colormap is full, so we're going
1710 to try to get the next closest color. The algorithm used is
1711 a least-squares matching, which is what X uses for closest
1712 color matching with StaticColor visuals. */
1713 int nearest, i;
1714 unsigned long nearest_delta = ~0;
1715 int ncells;
1716 const XColor *cells = x_color_cells (dpy, &ncells);
1718 for (nearest = i = 0; i < ncells; ++i)
1720 long dred = (color->red >> 8) - (cells[i].red >> 8);
1721 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1722 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1723 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1725 if (delta < nearest_delta)
1727 nearest = i;
1728 nearest_delta = delta;
1732 color->red = cells[nearest].red;
1733 color->green = cells[nearest].green;
1734 color->blue = cells[nearest].blue;
1735 rc = XAllocColor (dpy, cmap, color);
1737 else
1739 /* If allocation succeeded, and the allocated pixel color is not
1740 equal to a cached pixel color recorded earlier, there was a
1741 change in the colormap, so clear the color cache. */
1742 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1743 XColor *cached_color;
1745 if (dpyinfo->color_cells
1746 && (cached_color = &dpyinfo->color_cells[color->pixel],
1747 (cached_color->red != color->red
1748 || cached_color->blue != color->blue
1749 || cached_color->green != color->green)))
1751 xfree (dpyinfo->color_cells);
1752 dpyinfo->color_cells = NULL;
1753 dpyinfo->ncolor_cells = 0;
1757 #ifdef DEBUG_X_COLORS
1758 if (rc)
1759 register_color (color->pixel);
1760 #endif /* DEBUG_X_COLORS */
1762 return rc;
1766 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1767 exact match can't be allocated, try the nearest color available.
1768 Value is non-zero if successful. Set *COLOR to the color
1769 allocated. */
1772 x_alloc_nearest_color (f, cmap, color)
1773 struct frame *f;
1774 Colormap cmap;
1775 XColor *color;
1777 gamma_correct (f, color);
1778 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1782 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1783 It's necessary to do this instead of just using PIXEL directly to
1784 get color reference counts right. */
1786 unsigned long
1787 x_copy_color (f, pixel)
1788 struct frame *f;
1789 unsigned long pixel;
1791 XColor color;
1793 color.pixel = pixel;
1794 BLOCK_INPUT;
1795 x_query_color (f, &color);
1796 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1797 UNBLOCK_INPUT;
1798 #ifdef DEBUG_X_COLORS
1799 register_color (pixel);
1800 #endif
1801 return color.pixel;
1805 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1806 It's necessary to do this instead of just using PIXEL directly to
1807 get color reference counts right. */
1809 unsigned long
1810 x_copy_dpy_color (dpy, cmap, pixel)
1811 Display *dpy;
1812 Colormap cmap;
1813 unsigned long pixel;
1815 XColor color;
1817 color.pixel = pixel;
1818 BLOCK_INPUT;
1819 XQueryColor (dpy, cmap, &color);
1820 XAllocColor (dpy, cmap, &color);
1821 UNBLOCK_INPUT;
1822 #ifdef DEBUG_X_COLORS
1823 register_color (pixel);
1824 #endif
1825 return color.pixel;
1829 /* Brightness beyond which a color won't have its highlight brightness
1830 boosted.
1832 Nominally, highlight colors for `3d' faces are calculated by
1833 brightening an object's color by a constant scale factor, but this
1834 doesn't yield good results for dark colors, so for colors who's
1835 brightness is less than this value (on a scale of 0-65535) have an
1836 use an additional additive factor.
1838 The value here is set so that the default menu-bar/mode-line color
1839 (grey75) will not have its highlights changed at all. */
1840 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1843 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1844 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1845 If this produces the same color as PIXEL, try a color where all RGB
1846 values have DELTA added. Return the allocated color in *PIXEL.
1847 DISPLAY is the X display, CMAP is the colormap to operate on.
1848 Value is non-zero if successful. */
1850 static int
1851 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1852 struct frame *f;
1853 Display *display;
1854 Colormap cmap;
1855 unsigned long *pixel;
1856 double factor;
1857 int delta;
1859 XColor color, new;
1860 long bright;
1861 int success_p;
1863 /* Get RGB color values. */
1864 color.pixel = *pixel;
1865 x_query_color (f, &color);
1867 /* Change RGB values by specified FACTOR. Avoid overflow! */
1868 xassert (factor >= 0);
1869 new.red = min (0xffff, factor * color.red);
1870 new.green = min (0xffff, factor * color.green);
1871 new.blue = min (0xffff, factor * color.blue);
1873 /* Calculate brightness of COLOR. */
1874 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1876 /* We only boost colors that are darker than
1877 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1878 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1879 /* Make an additive adjustment to NEW, because it's dark enough so
1880 that scaling by FACTOR alone isn't enough. */
1882 /* How far below the limit this color is (0 - 1, 1 being darker). */
1883 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1884 /* The additive adjustment. */
1885 int min_delta = delta * dimness * factor / 2;
1887 if (factor < 1)
1889 new.red = max (0, new.red - min_delta);
1890 new.green = max (0, new.green - min_delta);
1891 new.blue = max (0, new.blue - min_delta);
1893 else
1895 new.red = min (0xffff, min_delta + new.red);
1896 new.green = min (0xffff, min_delta + new.green);
1897 new.blue = min (0xffff, min_delta + new.blue);
1901 /* Try to allocate the color. */
1902 success_p = x_alloc_nearest_color (f, cmap, &new);
1903 if (success_p)
1905 if (new.pixel == *pixel)
1907 /* If we end up with the same color as before, try adding
1908 delta to the RGB values. */
1909 x_free_colors (f, &new.pixel, 1);
1911 new.red = min (0xffff, delta + color.red);
1912 new.green = min (0xffff, delta + color.green);
1913 new.blue = min (0xffff, delta + color.blue);
1914 success_p = x_alloc_nearest_color (f, cmap, &new);
1916 else
1917 success_p = 1;
1918 *pixel = new.pixel;
1921 return success_p;
1925 /* Set up the foreground color for drawing relief lines of glyph
1926 string S. RELIEF is a pointer to a struct relief containing the GC
1927 with which lines will be drawn. Use a color that is FACTOR or
1928 DELTA lighter or darker than the relief's background which is found
1929 in S->f->output_data.x->relief_background. If such a color cannot
1930 be allocated, use DEFAULT_PIXEL, instead. */
1932 static void
1933 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1934 struct frame *f;
1935 struct relief *relief;
1936 double factor;
1937 int delta;
1938 unsigned long default_pixel;
1940 XGCValues xgcv;
1941 struct x_output *di = f->output_data.x;
1942 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1943 unsigned long pixel;
1944 unsigned long background = di->relief_background;
1945 Colormap cmap = FRAME_X_COLORMAP (f);
1946 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1947 Display *dpy = FRAME_X_DISPLAY (f);
1949 xgcv.graphics_exposures = False;
1950 xgcv.line_width = 1;
1952 /* Free previously allocated color. The color cell will be reused
1953 when it has been freed as many times as it was allocated, so this
1954 doesn't affect faces using the same colors. */
1955 if (relief->gc
1956 && relief->allocated_p)
1958 x_free_colors (f, &relief->pixel, 1);
1959 relief->allocated_p = 0;
1962 /* Allocate new color. */
1963 xgcv.foreground = default_pixel;
1964 pixel = background;
1965 if (dpyinfo->n_planes != 1
1966 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1968 relief->allocated_p = 1;
1969 xgcv.foreground = relief->pixel = pixel;
1972 if (relief->gc == 0)
1974 xgcv.stipple = dpyinfo->gray;
1975 mask |= GCStipple;
1976 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1978 else
1979 XChangeGC (dpy, relief->gc, mask, &xgcv);
1983 /* Set up colors for the relief lines around glyph string S. */
1985 static void
1986 x_setup_relief_colors (s)
1987 struct glyph_string *s;
1989 struct x_output *di = s->f->output_data.x;
1990 unsigned long color;
1992 if (s->face->use_box_color_for_shadows_p)
1993 color = s->face->box_color;
1994 else if (s->first_glyph->type == IMAGE_GLYPH
1995 && s->img->pixmap
1996 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1997 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1998 else
2000 XGCValues xgcv;
2002 /* Get the background color of the face. */
2003 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2004 color = xgcv.background;
2007 if (di->white_relief.gc == 0
2008 || color != di->relief_background)
2010 di->relief_background = color;
2011 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2012 WHITE_PIX_DEFAULT (s->f));
2013 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2014 BLACK_PIX_DEFAULT (s->f));
2019 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2020 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2021 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2022 relief. LEFT_P non-zero means draw a relief on the left side of
2023 the rectangle. RIGHT_P non-zero means draw a relief on the right
2024 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2025 when drawing. */
2027 static void
2028 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2029 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2030 struct frame *f;
2031 int left_x, top_y, right_x, bottom_y, width;
2032 int top_p, bot_p, left_p, right_p, raised_p;
2033 XRectangle *clip_rect;
2035 Display *dpy = FRAME_X_DISPLAY (f);
2036 Window window = FRAME_X_WINDOW (f);
2037 int i;
2038 GC gc;
2040 if (raised_p)
2041 gc = f->output_data.x->white_relief.gc;
2042 else
2043 gc = f->output_data.x->black_relief.gc;
2044 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2046 /* Top. */
2047 if (top_p)
2048 for (i = 0; i < width; ++i)
2049 XDrawLine (dpy, window, gc,
2050 left_x + i * left_p, top_y + i,
2051 right_x + 1 - i * right_p, top_y + i);
2053 /* Left. */
2054 if (left_p)
2055 for (i = 0; i < width; ++i)
2056 XDrawLine (dpy, window, gc,
2057 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2059 XSetClipMask (dpy, gc, None);
2060 if (raised_p)
2061 gc = f->output_data.x->black_relief.gc;
2062 else
2063 gc = f->output_data.x->white_relief.gc;
2064 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2066 /* Bottom. */
2067 if (bot_p)
2068 for (i = 0; i < width; ++i)
2069 XDrawLine (dpy, window, gc,
2070 left_x + i * left_p, bottom_y - i,
2071 right_x + 1 - i * right_p, bottom_y - i);
2073 /* Right. */
2074 if (right_p)
2075 for (i = 0; i < width; ++i)
2076 XDrawLine (dpy, window, gc,
2077 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2079 XSetClipMask (dpy, gc, None);
2083 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2084 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2085 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2086 left side of the rectangle. RIGHT_P non-zero means draw a line
2087 on the right side of the rectangle. CLIP_RECT is the clipping
2088 rectangle to use when drawing. */
2090 static void
2091 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2092 left_p, right_p, clip_rect)
2093 struct glyph_string *s;
2094 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2095 XRectangle *clip_rect;
2097 XGCValues xgcv;
2099 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2100 XSetForeground (s->display, s->gc, s->face->box_color);
2101 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2103 /* Top. */
2104 XFillRectangle (s->display, s->window, s->gc,
2105 left_x, top_y, right_x - left_x + 1, width);
2107 /* Left. */
2108 if (left_p)
2109 XFillRectangle (s->display, s->window, s->gc,
2110 left_x, top_y, width, bottom_y - top_y + 1);
2112 /* Bottom. */
2113 XFillRectangle (s->display, s->window, s->gc,
2114 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2116 /* Right. */
2117 if (right_p)
2118 XFillRectangle (s->display, s->window, s->gc,
2119 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2121 XSetForeground (s->display, s->gc, xgcv.foreground);
2122 XSetClipMask (s->display, s->gc, None);
2126 /* Draw a box around glyph string S. */
2128 static void
2129 x_draw_glyph_string_box (s)
2130 struct glyph_string *s;
2132 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2133 int left_p, right_p;
2134 struct glyph *last_glyph;
2135 XRectangle clip_rect;
2137 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2138 ? WINDOW_RIGHT_EDGE_X (s->w)
2139 : window_box_right (s->w, s->area));
2141 /* The glyph that may have a right box line. */
2142 last_glyph = (s->cmp || s->img
2143 ? s->first_glyph
2144 : s->first_glyph + s->nchars - 1);
2146 width = abs (s->face->box_line_width);
2147 raised_p = s->face->box == FACE_RAISED_BOX;
2148 left_x = s->x;
2149 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2150 ? last_x - 1
2151 : min (last_x, s->x + s->background_width) - 1);
2152 top_y = s->y;
2153 bottom_y = top_y + s->height - 1;
2155 left_p = (s->first_glyph->left_box_line_p
2156 || (s->hl == DRAW_MOUSE_FACE
2157 && (s->prev == NULL
2158 || s->prev->hl != s->hl)));
2159 right_p = (last_glyph->right_box_line_p
2160 || (s->hl == DRAW_MOUSE_FACE
2161 && (s->next == NULL
2162 || s->next->hl != s->hl)));
2164 get_glyph_string_clip_rect (s, &clip_rect);
2166 if (s->face->box == FACE_SIMPLE_BOX)
2167 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2168 left_p, right_p, &clip_rect);
2169 else
2171 x_setup_relief_colors (s);
2172 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2173 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2178 /* Draw foreground of image glyph string S. */
2180 static void
2181 x_draw_image_foreground (s)
2182 struct glyph_string *s;
2184 int x = s->x;
2185 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2187 /* If first glyph of S has a left box line, start drawing it to the
2188 right of that line. */
2189 if (s->face->box != FACE_NO_BOX
2190 && s->first_glyph->left_box_line_p
2191 && s->slice.x == 0)
2192 x += abs (s->face->box_line_width);
2194 /* If there is a margin around the image, adjust x- and y-position
2195 by that margin. */
2196 if (s->slice.x == 0)
2197 x += s->img->hmargin;
2198 if (s->slice.y == 0)
2199 y += s->img->vmargin;
2201 if (s->img->pixmap)
2203 if (s->img->mask)
2205 /* We can't set both a clip mask and use XSetClipRectangles
2206 because the latter also sets a clip mask. We also can't
2207 trust on the shape extension to be available
2208 (XShapeCombineRegion). So, compute the rectangle to draw
2209 manually. */
2210 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2211 | GCFunction);
2212 XGCValues xgcv;
2213 XRectangle clip_rect, image_rect, r;
2215 xgcv.clip_mask = s->img->mask;
2216 xgcv.clip_x_origin = x;
2217 xgcv.clip_y_origin = y;
2218 xgcv.function = GXcopy;
2219 XChangeGC (s->display, s->gc, mask, &xgcv);
2221 get_glyph_string_clip_rect (s, &clip_rect);
2222 image_rect.x = x;
2223 image_rect.y = y;
2224 image_rect.width = s->slice.width;
2225 image_rect.height = s->slice.height;
2226 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2227 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2228 s->slice.x + r.x - x, s->slice.y + r.y - y,
2229 r.width, r.height, r.x, r.y);
2231 else
2233 XRectangle clip_rect, image_rect, r;
2235 get_glyph_string_clip_rect (s, &clip_rect);
2236 image_rect.x = x;
2237 image_rect.y = y;
2238 image_rect.width = s->slice.width;
2239 image_rect.height = s->slice.height;
2240 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2241 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2242 s->slice.x + r.x - x, s->slice.y + r.y - y,
2243 r.width, r.height, r.x, r.y);
2245 /* When the image has a mask, we can expect that at
2246 least part of a mouse highlight or a block cursor will
2247 be visible. If the image doesn't have a mask, make
2248 a block cursor visible by drawing a rectangle around
2249 the image. I believe it's looking better if we do
2250 nothing here for mouse-face. */
2251 if (s->hl == DRAW_CURSOR)
2253 int r = s->img->relief;
2254 if (r < 0) r = -r;
2255 XDrawRectangle (s->display, s->window, s->gc,
2256 x - r, y - r,
2257 s->slice.width + r*2 - 1,
2258 s->slice.height + r*2 - 1);
2262 else
2263 /* Draw a rectangle if image could not be loaded. */
2264 XDrawRectangle (s->display, s->window, s->gc, x, y,
2265 s->slice.width - 1, s->slice.height - 1);
2269 /* Draw a relief around the image glyph string S. */
2271 static void
2272 x_draw_image_relief (s)
2273 struct glyph_string *s;
2275 int x0, y0, x1, y1, thick, raised_p;
2276 XRectangle r;
2277 int x = s->x;
2278 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2280 /* If first glyph of S has a left box line, start drawing it to the
2281 right of that line. */
2282 if (s->face->box != FACE_NO_BOX
2283 && s->first_glyph->left_box_line_p
2284 && s->slice.x == 0)
2285 x += abs (s->face->box_line_width);
2287 /* If there is a margin around the image, adjust x- and y-position
2288 by that margin. */
2289 if (s->slice.x == 0)
2290 x += s->img->hmargin;
2291 if (s->slice.y == 0)
2292 y += s->img->vmargin;
2294 if (s->hl == DRAW_IMAGE_SUNKEN
2295 || s->hl == DRAW_IMAGE_RAISED)
2297 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2298 raised_p = s->hl == DRAW_IMAGE_RAISED;
2300 else
2302 thick = abs (s->img->relief);
2303 raised_p = s->img->relief > 0;
2306 x0 = x - thick;
2307 y0 = y - thick;
2308 x1 = x + s->slice.width + thick - 1;
2309 y1 = y + s->slice.height + thick - 1;
2311 x_setup_relief_colors (s);
2312 get_glyph_string_clip_rect (s, &r);
2313 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2314 s->slice.y == 0,
2315 s->slice.y + s->slice.height == s->img->height,
2316 s->slice.x == 0,
2317 s->slice.x + s->slice.width == s->img->width,
2318 &r);
2322 /* Draw the foreground of image glyph string S to PIXMAP. */
2324 static void
2325 x_draw_image_foreground_1 (s, pixmap)
2326 struct glyph_string *s;
2327 Pixmap pixmap;
2329 int x = 0;
2330 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2332 /* If first glyph of S has a left box line, start drawing it to the
2333 right of that line. */
2334 if (s->face->box != FACE_NO_BOX
2335 && s->first_glyph->left_box_line_p
2336 && s->slice.x == 0)
2337 x += abs (s->face->box_line_width);
2339 /* If there is a margin around the image, adjust x- and y-position
2340 by that margin. */
2341 if (s->slice.x == 0)
2342 x += s->img->hmargin;
2343 if (s->slice.y == 0)
2344 y += s->img->vmargin;
2346 if (s->img->pixmap)
2348 if (s->img->mask)
2350 /* We can't set both a clip mask and use XSetClipRectangles
2351 because the latter also sets a clip mask. We also can't
2352 trust on the shape extension to be available
2353 (XShapeCombineRegion). So, compute the rectangle to draw
2354 manually. */
2355 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2356 | GCFunction);
2357 XGCValues xgcv;
2359 xgcv.clip_mask = s->img->mask;
2360 xgcv.clip_x_origin = x - s->slice.x;
2361 xgcv.clip_y_origin = y - s->slice.y;
2362 xgcv.function = GXcopy;
2363 XChangeGC (s->display, s->gc, mask, &xgcv);
2365 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2366 s->slice.x, s->slice.y,
2367 s->slice.width, s->slice.height, x, y);
2368 XSetClipMask (s->display, s->gc, None);
2370 else
2372 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2373 s->slice.x, s->slice.y,
2374 s->slice.width, s->slice.height, x, y);
2376 /* When the image has a mask, we can expect that at
2377 least part of a mouse highlight or a block cursor will
2378 be visible. If the image doesn't have a mask, make
2379 a block cursor visible by drawing a rectangle around
2380 the image. I believe it's looking better if we do
2381 nothing here for mouse-face. */
2382 if (s->hl == DRAW_CURSOR)
2384 int r = s->img->relief;
2385 if (r < 0) r = -r;
2386 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2387 s->slice.width + r*2 - 1,
2388 s->slice.height + r*2 - 1);
2392 else
2393 /* Draw a rectangle if image could not be loaded. */
2394 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2395 s->slice.width - 1, s->slice.height - 1);
2399 /* Draw part of the background of glyph string S. X, Y, W, and H
2400 give the rectangle to draw. */
2402 static void
2403 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2404 struct glyph_string *s;
2405 int x, y, w, h;
2407 if (s->stippled_p)
2409 /* Fill background with a stipple pattern. */
2410 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2411 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2412 XSetFillStyle (s->display, s->gc, FillSolid);
2414 else
2415 x_clear_glyph_string_rect (s, x, y, w, h);
2419 /* Draw image glyph string S.
2421 s->y
2422 s->x +-------------------------
2423 | s->face->box
2425 | +-------------------------
2426 | | s->img->margin
2428 | | +-------------------
2429 | | | the image
2433 static void
2434 x_draw_image_glyph_string (s)
2435 struct glyph_string *s;
2437 int box_line_hwidth = abs (s->face->box_line_width);
2438 int box_line_vwidth = max (s->face->box_line_width, 0);
2439 int height;
2440 Pixmap pixmap = None;
2442 height = s->height;
2443 if (s->slice.y == 0)
2444 height -= box_line_vwidth;
2445 if (s->slice.y + s->slice.height >= s->img->height)
2446 height -= box_line_vwidth;
2448 /* Fill background with face under the image. Do it only if row is
2449 taller than image or if image has a clip mask to reduce
2450 flickering. */
2451 s->stippled_p = s->face->stipple != 0;
2452 if (height > s->slice.height
2453 || s->img->hmargin
2454 || s->img->vmargin
2455 || s->img->mask
2456 || s->img->pixmap == 0
2457 || s->width != s->background_width)
2459 if (s->img->mask)
2461 /* Create a pixmap as large as the glyph string. Fill it
2462 with the background color. Copy the image to it, using
2463 its mask. Copy the temporary pixmap to the display. */
2464 Screen *screen = FRAME_X_SCREEN (s->f);
2465 int depth = DefaultDepthOfScreen (screen);
2467 /* Create a pixmap as large as the glyph string. */
2468 pixmap = XCreatePixmap (s->display, s->window,
2469 s->background_width,
2470 s->height, depth);
2472 /* Don't clip in the following because we're working on the
2473 pixmap. */
2474 XSetClipMask (s->display, s->gc, None);
2476 /* Fill the pixmap with the background color/stipple. */
2477 if (s->stippled_p)
2479 /* Fill background with a stipple pattern. */
2480 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2481 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2482 XFillRectangle (s->display, pixmap, s->gc,
2483 0, 0, s->background_width, s->height);
2484 XSetFillStyle (s->display, s->gc, FillSolid);
2485 XSetTSOrigin (s->display, s->gc, 0, 0);
2487 else
2489 XGCValues xgcv;
2490 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2491 &xgcv);
2492 XSetForeground (s->display, s->gc, xgcv.background);
2493 XFillRectangle (s->display, pixmap, s->gc,
2494 0, 0, s->background_width, s->height);
2495 XSetForeground (s->display, s->gc, xgcv.foreground);
2498 else
2500 int x = s->x;
2501 int y = s->y;
2503 if (s->first_glyph->left_box_line_p
2504 && s->slice.x == 0)
2505 x += box_line_hwidth;
2507 if (s->slice.y == 0)
2508 y += box_line_vwidth;
2510 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2513 s->background_filled_p = 1;
2516 /* Draw the foreground. */
2517 if (pixmap != None)
2519 x_draw_image_foreground_1 (s, pixmap);
2520 x_set_glyph_string_clipping (s);
2521 XCopyArea (s->display, pixmap, s->window, s->gc,
2522 0, 0, s->background_width, s->height, s->x, s->y);
2523 XFreePixmap (s->display, pixmap);
2525 else
2526 x_draw_image_foreground (s);
2528 /* If we must draw a relief around the image, do it. */
2529 if (s->img->relief
2530 || s->hl == DRAW_IMAGE_RAISED
2531 || s->hl == DRAW_IMAGE_SUNKEN)
2532 x_draw_image_relief (s);
2536 /* Draw stretch glyph string S. */
2538 static void
2539 x_draw_stretch_glyph_string (s)
2540 struct glyph_string *s;
2542 xassert (s->first_glyph->type == STRETCH_GLYPH);
2544 if (s->hl == DRAW_CURSOR
2545 && !x_stretch_cursor_p)
2547 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2548 as wide as the stretch glyph. */
2549 int width, background_width = s->background_width;
2550 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2552 if (x < left_x)
2554 background_width -= left_x - x;
2555 x = left_x;
2557 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2559 /* Draw cursor. */
2560 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2562 /* Clear rest using the GC of the original non-cursor face. */
2563 if (width < background_width)
2565 int y = s->y;
2566 int w = background_width - width, h = s->height;
2567 XRectangle r;
2568 GC gc;
2570 x += width;
2571 if (s->row->mouse_face_p
2572 && cursor_in_mouse_face_p (s->w))
2574 x_set_mouse_face_gc (s);
2575 gc = s->gc;
2577 else
2578 gc = s->face->gc;
2580 get_glyph_string_clip_rect (s, &r);
2581 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2583 if (s->face->stipple)
2585 /* Fill background with a stipple pattern. */
2586 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2587 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2588 XSetFillStyle (s->display, gc, FillSolid);
2590 else
2592 XGCValues xgcv;
2593 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2594 XSetForeground (s->display, gc, xgcv.background);
2595 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2596 XSetForeground (s->display, gc, xgcv.foreground);
2600 else if (!s->background_filled_p)
2602 int background_width = s->background_width;
2603 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2605 /* Don't draw into left margin, fringe or scrollbar area
2606 except for header line and mode line. */
2607 if (x < left_x && !s->row->mode_line_p)
2609 background_width -= left_x - x;
2610 x = left_x;
2612 if (background_width > 0)
2613 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2616 s->background_filled_p = 1;
2620 /* Draw glyph string S. */
2622 static void
2623 x_draw_glyph_string (s)
2624 struct glyph_string *s;
2626 int relief_drawn_p = 0;
2628 /* If S draws into the background of its successor, draw the
2629 background of the successor first so that S can draw into it.
2630 This makes S->next use XDrawString instead of XDrawImageString. */
2631 if (s->next && s->right_overhang && !s->for_overlaps)
2633 xassert (s->next->img == NULL);
2634 x_set_glyph_string_gc (s->next);
2635 x_set_glyph_string_clipping (s->next);
2636 x_draw_glyph_string_background (s->next, 1);
2639 /* Set up S->gc, set clipping and draw S. */
2640 x_set_glyph_string_gc (s);
2642 /* Draw relief (if any) in advance for char/composition so that the
2643 glyph string can be drawn over it. */
2644 if (!s->for_overlaps
2645 && s->face->box != FACE_NO_BOX
2646 && (s->first_glyph->type == CHAR_GLYPH
2647 || s->first_glyph->type == COMPOSITE_GLYPH))
2650 x_set_glyph_string_clipping (s);
2651 x_draw_glyph_string_background (s, 1);
2652 x_draw_glyph_string_box (s);
2653 x_set_glyph_string_clipping (s);
2654 relief_drawn_p = 1;
2656 else
2657 x_set_glyph_string_clipping (s);
2659 switch (s->first_glyph->type)
2661 case IMAGE_GLYPH:
2662 x_draw_image_glyph_string (s);
2663 break;
2665 case STRETCH_GLYPH:
2666 x_draw_stretch_glyph_string (s);
2667 break;
2669 case CHAR_GLYPH:
2670 if (s->for_overlaps)
2671 s->background_filled_p = 1;
2672 else
2673 x_draw_glyph_string_background (s, 0);
2674 x_draw_glyph_string_foreground (s);
2675 break;
2677 case COMPOSITE_GLYPH:
2678 if (s->for_overlaps || s->gidx > 0)
2679 s->background_filled_p = 1;
2680 else
2681 x_draw_glyph_string_background (s, 1);
2682 x_draw_composite_glyph_string_foreground (s);
2683 break;
2685 default:
2686 abort ();
2689 if (!s->for_overlaps)
2691 /* Draw underline. */
2692 if (s->face->underline_p)
2694 unsigned long tem, h;
2695 int y;
2697 /* Get the underline thickness. Default is 1 pixel. */
2698 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
2699 h = 1;
2701 y = s->y + s->height - h;
2702 if (!x_underline_at_descent_line)
2704 /* Get the underline position. This is the recommended
2705 vertical offset in pixels from the baseline to the top of
2706 the underline. This is a signed value according to the
2707 specs, and its default is
2709 ROUND ((maximum descent) / 2), with
2710 ROUND(x) = floor (x + 0.5) */
2712 if (x_use_underline_position_properties
2713 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
2714 y = s->ybase + (long) tem;
2715 else if (s->face->font)
2716 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2719 if (s->face->underline_defaulted_p)
2720 XFillRectangle (s->display, s->window, s->gc,
2721 s->x, y, s->background_width, h);
2722 else
2724 XGCValues xgcv;
2725 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2726 XSetForeground (s->display, s->gc, s->face->underline_color);
2727 XFillRectangle (s->display, s->window, s->gc,
2728 s->x, y, s->background_width, h);
2729 XSetForeground (s->display, s->gc, xgcv.foreground);
2733 /* Draw overline. */
2734 if (s->face->overline_p)
2736 unsigned long dy = 0, h = 1;
2738 if (s->face->overline_color_defaulted_p)
2739 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2740 s->background_width, h);
2741 else
2743 XGCValues xgcv;
2744 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2745 XSetForeground (s->display, s->gc, s->face->overline_color);
2746 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2747 s->background_width, h);
2748 XSetForeground (s->display, s->gc, xgcv.foreground);
2752 /* Draw strike-through. */
2753 if (s->face->strike_through_p)
2755 unsigned long h = 1;
2756 unsigned long dy = (s->height - h) / 2;
2758 if (s->face->strike_through_color_defaulted_p)
2759 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2760 s->width, h);
2761 else
2763 XGCValues xgcv;
2764 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2765 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2766 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2767 s->width, h);
2768 XSetForeground (s->display, s->gc, xgcv.foreground);
2772 /* Draw relief if not yet drawn. */
2773 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2774 x_draw_glyph_string_box (s);
2777 /* Reset clipping. */
2778 XSetClipMask (s->display, s->gc, None);
2781 /* Shift display to make room for inserted glyphs. */
2783 void
2784 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2785 struct frame *f;
2786 int x, y, width, height, shift_by;
2788 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2789 f->output_data.x->normal_gc,
2790 x, y, width, height,
2791 x + shift_by, y);
2794 /* Delete N glyphs at the nominal cursor position. Not implemented
2795 for X frames. */
2797 static void
2798 x_delete_glyphs (n)
2799 register int n;
2801 abort ();
2805 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2806 If they are <= 0, this is probably an error. */
2808 void
2809 x_clear_area (dpy, window, x, y, width, height, exposures)
2810 Display *dpy;
2811 Window window;
2812 int x, y;
2813 int width, height;
2814 int exposures;
2816 xassert (width > 0 && height > 0);
2817 XClearArea (dpy, window, x, y, width, height, exposures);
2821 /* Clear entire frame. If updating_frame is non-null, clear that
2822 frame. Otherwise clear the selected frame. */
2824 static void
2825 x_clear_frame ()
2827 struct frame *f;
2829 if (updating_frame)
2830 f = updating_frame;
2831 else
2832 f = SELECTED_FRAME ();
2834 /* Clearing the frame will erase any cursor, so mark them all as no
2835 longer visible. */
2836 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2837 output_cursor.hpos = output_cursor.vpos = 0;
2838 output_cursor.x = -1;
2840 /* We don't set the output cursor here because there will always
2841 follow an explicit cursor_to. */
2842 BLOCK_INPUT;
2843 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2845 /* We have to clear the scroll bars, too. If we have changed
2846 colors or something like that, then they should be notified. */
2847 x_scroll_bar_clear (f);
2849 XFlush (FRAME_X_DISPLAY (f));
2851 UNBLOCK_INPUT;
2856 /* Invert the middle quarter of the frame for .15 sec. */
2858 /* We use the select system call to do the waiting, so we have to make
2859 sure it's available. If it isn't, we just won't do visual bells. */
2861 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2864 /* Subtract the `struct timeval' values X and Y, storing the result in
2865 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2867 static int
2868 timeval_subtract (result, x, y)
2869 struct timeval *result, x, y;
2871 /* Perform the carry for the later subtraction by updating y. This
2872 is safer because on some systems the tv_sec member is unsigned. */
2873 if (x.tv_usec < y.tv_usec)
2875 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2876 y.tv_usec -= 1000000 * nsec;
2877 y.tv_sec += nsec;
2880 if (x.tv_usec - y.tv_usec > 1000000)
2882 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2883 y.tv_usec += 1000000 * nsec;
2884 y.tv_sec -= nsec;
2887 /* Compute the time remaining to wait. tv_usec is certainly
2888 positive. */
2889 result->tv_sec = x.tv_sec - y.tv_sec;
2890 result->tv_usec = x.tv_usec - y.tv_usec;
2892 /* Return indication of whether the result should be considered
2893 negative. */
2894 return x.tv_sec < y.tv_sec;
2897 void
2898 XTflash (f)
2899 struct frame *f;
2901 BLOCK_INPUT;
2904 GC gc;
2906 /* Create a GC that will use the GXxor function to flip foreground
2907 pixels into background pixels. */
2909 XGCValues values;
2911 values.function = GXxor;
2912 values.foreground = (f->output_data.x->foreground_pixel
2913 ^ f->output_data.x->background_pixel);
2915 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2916 GCFunction | GCForeground, &values);
2920 /* Get the height not including a menu bar widget. */
2921 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
2922 /* Height of each line to flash. */
2923 int flash_height = FRAME_LINE_HEIGHT (f);
2924 /* These will be the left and right margins of the rectangles. */
2925 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
2926 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
2928 int width;
2930 /* Don't flash the area between a scroll bar and the frame
2931 edge it is next to. */
2932 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
2934 case vertical_scroll_bar_left:
2935 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2936 break;
2938 case vertical_scroll_bar_right:
2939 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2940 break;
2942 default:
2943 break;
2946 width = flash_right - flash_left;
2948 /* If window is tall, flash top and bottom line. */
2949 if (height > 3 * FRAME_LINE_HEIGHT (f))
2951 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2952 flash_left,
2953 (FRAME_INTERNAL_BORDER_WIDTH (f)
2954 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
2955 width, flash_height);
2956 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2957 flash_left,
2958 (height - flash_height
2959 - FRAME_INTERNAL_BORDER_WIDTH (f)),
2960 width, flash_height);
2962 else
2963 /* If it is short, flash it all. */
2964 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
2965 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
2966 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
2968 x_flush (f);
2971 struct timeval wakeup;
2973 EMACS_GET_TIME (wakeup);
2975 /* Compute time to wait until, propagating carry from usecs. */
2976 wakeup.tv_usec += 150000;
2977 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
2978 wakeup.tv_usec %= 1000000;
2980 /* Keep waiting until past the time wakeup or any input gets
2981 available. */
2982 while (! detect_input_pending ())
2984 struct timeval current;
2985 struct timeval timeout;
2987 EMACS_GET_TIME (current);
2989 /* Break if result would be negative. */
2990 if (timeval_subtract (&current, wakeup, current))
2991 break;
2993 /* How long `select' should wait. */
2994 timeout.tv_sec = 0;
2995 timeout.tv_usec = 10000;
2997 /* Try to wait that long--but we might wake up sooner. */
2998 select (0, NULL, NULL, NULL, &timeout);
3002 /* If window is tall, flash top and bottom line. */
3003 if (height > 3 * FRAME_LINE_HEIGHT (f))
3005 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3006 flash_left,
3007 (FRAME_INTERNAL_BORDER_WIDTH (f)
3008 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3009 width, flash_height);
3010 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3011 flash_left,
3012 (height - flash_height
3013 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3014 width, flash_height);
3016 else
3017 /* If it is short, flash it all. */
3018 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3019 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3020 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3022 XFreeGC (FRAME_X_DISPLAY (f), gc);
3023 x_flush (f);
3027 UNBLOCK_INPUT;
3030 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3033 /* Make audible bell. */
3035 void
3036 XTring_bell ()
3038 struct frame *f = SELECTED_FRAME ();
3040 if (FRAME_X_DISPLAY (f))
3042 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3043 if (visible_bell)
3044 XTflash (f);
3045 else
3046 #endif
3048 BLOCK_INPUT;
3049 XBell (FRAME_X_DISPLAY (f), 0);
3050 XFlush (FRAME_X_DISPLAY (f));
3051 UNBLOCK_INPUT;
3057 /* Specify how many text lines, from the top of the window,
3058 should be affected by insert-lines and delete-lines operations.
3059 This, and those operations, are used only within an update
3060 that is bounded by calls to x_update_begin and x_update_end. */
3062 static void
3063 XTset_terminal_window (n)
3064 register int n;
3066 /* This function intentionally left blank. */
3071 /***********************************************************************
3072 Line Dance
3073 ***********************************************************************/
3075 /* Perform an insert-lines or delete-lines operation, inserting N
3076 lines or deleting -N lines at vertical position VPOS. */
3078 static void
3079 x_ins_del_lines (vpos, n)
3080 int vpos, n;
3082 abort ();
3086 /* Scroll part of the display as described by RUN. */
3088 static void
3089 x_scroll_run (w, run)
3090 struct window *w;
3091 struct run *run;
3093 struct frame *f = XFRAME (w->frame);
3094 int x, y, width, height, from_y, to_y, bottom_y;
3096 /* Get frame-relative bounding box of the text display area of W,
3097 without mode lines. Include in this box the left and right
3098 fringe of W. */
3099 window_box (w, -1, &x, &y, &width, &height);
3101 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3102 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3103 bottom_y = y + height;
3105 if (to_y < from_y)
3107 /* Scrolling up. Make sure we don't copy part of the mode
3108 line at the bottom. */
3109 if (from_y + run->height > bottom_y)
3110 height = bottom_y - from_y;
3111 else
3112 height = run->height;
3114 else
3116 /* Scolling down. Make sure we don't copy over the mode line.
3117 at the bottom. */
3118 if (to_y + run->height > bottom_y)
3119 height = bottom_y - to_y;
3120 else
3121 height = run->height;
3124 BLOCK_INPUT;
3126 /* Cursor off. Will be switched on again in x_update_window_end. */
3127 updated_window = w;
3128 x_clear_cursor (w);
3130 XCopyArea (FRAME_X_DISPLAY (f),
3131 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3132 f->output_data.x->normal_gc,
3133 x, from_y,
3134 width, height,
3135 x, to_y);
3137 UNBLOCK_INPUT;
3142 /***********************************************************************
3143 Exposure Events
3144 ***********************************************************************/
3147 static void
3148 frame_highlight (f)
3149 struct frame *f;
3151 /* We used to only do this if Vx_no_window_manager was non-nil, but
3152 the ICCCM (section 4.1.6) says that the window's border pixmap
3153 and border pixel are window attributes which are "private to the
3154 client", so we can always change it to whatever we want. */
3155 BLOCK_INPUT;
3156 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3157 f->output_data.x->border_pixel);
3158 UNBLOCK_INPUT;
3159 x_update_cursor (f, 1);
3162 static void
3163 frame_unhighlight (f)
3164 struct frame *f;
3166 /* We used to only do this if Vx_no_window_manager was non-nil, but
3167 the ICCCM (section 4.1.6) says that the window's border pixmap
3168 and border pixel are window attributes which are "private to the
3169 client", so we can always change it to whatever we want. */
3170 BLOCK_INPUT;
3171 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3172 f->output_data.x->border_tile);
3173 UNBLOCK_INPUT;
3174 x_update_cursor (f, 1);
3177 /* The focus has changed. Update the frames as necessary to reflect
3178 the new situation. Note that we can't change the selected frame
3179 here, because the Lisp code we are interrupting might become confused.
3180 Each event gets marked with the frame in which it occurred, so the
3181 Lisp code can tell when the switch took place by examining the events. */
3183 static void
3184 x_new_focus_frame (dpyinfo, frame)
3185 struct x_display_info *dpyinfo;
3186 struct frame *frame;
3188 struct frame *old_focus = dpyinfo->x_focus_frame;
3190 if (frame != dpyinfo->x_focus_frame)
3192 /* Set this before calling other routines, so that they see
3193 the correct value of x_focus_frame. */
3194 dpyinfo->x_focus_frame = frame;
3196 if (old_focus && old_focus->auto_lower)
3197 x_lower_frame (old_focus);
3199 #if 0
3200 selected_frame = frame;
3201 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3202 selected_frame);
3203 Fselect_window (selected_frame->selected_window, Qnil);
3204 choose_minibuf_frame ();
3205 #endif /* ! 0 */
3207 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3208 pending_autoraise_frame = dpyinfo->x_focus_frame;
3209 else
3210 pending_autoraise_frame = 0;
3213 x_frame_rehighlight (dpyinfo);
3216 /* Handle FocusIn and FocusOut state changes for FRAME.
3217 If FRAME has focus and there exists more than one frame, puts
3218 a FOCUS_IN_EVENT into *BUFP. */
3220 static void
3221 x_focus_changed (type, state, dpyinfo, frame, bufp)
3222 int type;
3223 int state;
3224 struct x_display_info *dpyinfo;
3225 struct frame *frame;
3226 struct input_event *bufp;
3228 if (type == FocusIn)
3230 if (dpyinfo->x_focus_event_frame != frame)
3232 x_new_focus_frame (dpyinfo, frame);
3233 dpyinfo->x_focus_event_frame = frame;
3235 /* Don't stop displaying the initial startup message
3236 for a switch-frame event we don't need. */
3237 if (GC_NILP (Vterminal_frame)
3238 && GC_CONSP (Vframe_list)
3239 && !GC_NILP (XCDR (Vframe_list)))
3241 bufp->kind = FOCUS_IN_EVENT;
3242 XSETFRAME (bufp->frame_or_window, frame);
3246 frame->output_data.x->focus_state |= state;
3248 #ifdef HAVE_X_I18N
3249 if (FRAME_XIC (frame))
3250 XSetICFocus (FRAME_XIC (frame));
3251 #endif
3253 else if (type == FocusOut)
3255 frame->output_data.x->focus_state &= ~state;
3257 if (dpyinfo->x_focus_event_frame == frame)
3259 dpyinfo->x_focus_event_frame = 0;
3260 x_new_focus_frame (dpyinfo, 0);
3263 #ifdef HAVE_X_I18N
3264 if (FRAME_XIC (frame))
3265 XUnsetICFocus (FRAME_XIC (frame));
3266 #endif
3270 /* The focus may have changed. Figure out if it is a real focus change,
3271 by checking both FocusIn/Out and Enter/LeaveNotify events.
3273 Returns FOCUS_IN_EVENT event in *BUFP. */
3275 static void
3276 x_detect_focus_change (dpyinfo, event, bufp)
3277 struct x_display_info *dpyinfo;
3278 XEvent *event;
3279 struct input_event *bufp;
3281 struct frame *frame;
3283 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3284 if (! frame)
3285 return;
3287 switch (event->type)
3289 case EnterNotify:
3290 case LeaveNotify:
3292 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3293 int focus_state
3294 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3296 if (event->xcrossing.detail != NotifyInferior
3297 && event->xcrossing.focus
3298 && ! (focus_state & FOCUS_EXPLICIT))
3299 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3300 FOCUS_IMPLICIT,
3301 dpyinfo, frame, bufp);
3303 break;
3305 case FocusIn:
3306 case FocusOut:
3307 x_focus_changed (event->type,
3308 (event->xfocus.detail == NotifyPointer ?
3309 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3310 dpyinfo, frame, bufp);
3311 break;
3316 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3318 void
3319 x_mouse_leave (dpyinfo)
3320 struct x_display_info *dpyinfo;
3322 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3325 /* The focus has changed, or we have redirected a frame's focus to
3326 another frame (this happens when a frame uses a surrogate
3327 mini-buffer frame). Shift the highlight as appropriate.
3329 The FRAME argument doesn't necessarily have anything to do with which
3330 frame is being highlighted or un-highlighted; we only use it to find
3331 the appropriate X display info. */
3333 static void
3334 XTframe_rehighlight (frame)
3335 struct frame *frame;
3337 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3340 static void
3341 x_frame_rehighlight (dpyinfo)
3342 struct x_display_info *dpyinfo;
3344 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3346 if (dpyinfo->x_focus_frame)
3348 dpyinfo->x_highlight_frame
3349 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3350 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3351 : dpyinfo->x_focus_frame);
3352 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3354 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3355 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3358 else
3359 dpyinfo->x_highlight_frame = 0;
3361 if (dpyinfo->x_highlight_frame != old_highlight)
3363 if (old_highlight)
3364 frame_unhighlight (old_highlight);
3365 if (dpyinfo->x_highlight_frame)
3366 frame_highlight (dpyinfo->x_highlight_frame);
3372 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3374 /* Initialize mode_switch_bit and modifier_meaning. */
3375 static void
3376 x_find_modifier_meanings (dpyinfo)
3377 struct x_display_info *dpyinfo;
3379 int min_code, max_code;
3380 KeySym *syms;
3381 int syms_per_code;
3382 XModifierKeymap *mods;
3384 dpyinfo->meta_mod_mask = 0;
3385 dpyinfo->shift_lock_mask = 0;
3386 dpyinfo->alt_mod_mask = 0;
3387 dpyinfo->super_mod_mask = 0;
3388 dpyinfo->hyper_mod_mask = 0;
3390 #ifdef HAVE_X11R4
3391 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3392 #else
3393 min_code = dpyinfo->display->min_keycode;
3394 max_code = dpyinfo->display->max_keycode;
3395 #endif
3397 syms = XGetKeyboardMapping (dpyinfo->display,
3398 min_code, max_code - min_code + 1,
3399 &syms_per_code);
3400 mods = XGetModifierMapping (dpyinfo->display);
3402 /* Scan the modifier table to see which modifier bits the Meta and
3403 Alt keysyms are on. */
3405 int row, col; /* The row and column in the modifier table. */
3406 int found_alt_or_meta;
3408 for (row = 3; row < 8; row++)
3410 found_alt_or_meta = 0;
3411 for (col = 0; col < mods->max_keypermod; col++)
3413 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3415 /* Zeroes are used for filler. Skip them. */
3416 if (code == 0)
3417 continue;
3419 /* Are any of this keycode's keysyms a meta key? */
3421 int code_col;
3423 for (code_col = 0; code_col < syms_per_code; code_col++)
3425 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3427 switch (sym)
3429 case XK_Meta_L:
3430 case XK_Meta_R:
3431 found_alt_or_meta = 1;
3432 dpyinfo->meta_mod_mask |= (1 << row);
3433 break;
3435 case XK_Alt_L:
3436 case XK_Alt_R:
3437 found_alt_or_meta = 1;
3438 dpyinfo->alt_mod_mask |= (1 << row);
3439 break;
3441 case XK_Hyper_L:
3442 case XK_Hyper_R:
3443 if (!found_alt_or_meta)
3444 dpyinfo->hyper_mod_mask |= (1 << row);
3445 code_col = syms_per_code;
3446 col = mods->max_keypermod;
3447 break;
3449 case XK_Super_L:
3450 case XK_Super_R:
3451 if (!found_alt_or_meta)
3452 dpyinfo->super_mod_mask |= (1 << row);
3453 code_col = syms_per_code;
3454 col = mods->max_keypermod;
3455 break;
3457 case XK_Shift_Lock:
3458 /* Ignore this if it's not on the lock modifier. */
3459 if (!found_alt_or_meta && ((1 << row) == LockMask))
3460 dpyinfo->shift_lock_mask = LockMask;
3461 code_col = syms_per_code;
3462 col = mods->max_keypermod;
3463 break;
3471 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3472 if (! dpyinfo->meta_mod_mask)
3474 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3475 dpyinfo->alt_mod_mask = 0;
3478 /* If some keys are both alt and meta,
3479 make them just meta, not alt. */
3480 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3482 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3485 XFree ((char *) syms);
3486 XFreeModifiermap (mods);
3489 /* Convert between the modifier bits X uses and the modifier bits
3490 Emacs uses. */
3492 unsigned int
3493 x_x_to_emacs_modifiers (dpyinfo, state)
3494 struct x_display_info *dpyinfo;
3495 unsigned int state;
3497 EMACS_UINT mod_meta = meta_modifier;
3498 EMACS_UINT mod_alt = alt_modifier;
3499 EMACS_UINT mod_hyper = hyper_modifier;
3500 EMACS_UINT mod_super = super_modifier;
3501 Lisp_Object tem;
3503 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3504 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3505 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3506 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3507 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3508 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3509 tem = Fget (Vx_super_keysym, Qmodifier_value);
3510 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3513 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3514 | ((state & ControlMask) ? ctrl_modifier : 0)
3515 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3516 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3517 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3518 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3521 static unsigned int
3522 x_emacs_to_x_modifiers (dpyinfo, state)
3523 struct x_display_info *dpyinfo;
3524 unsigned int state;
3526 EMACS_UINT mod_meta = meta_modifier;
3527 EMACS_UINT mod_alt = alt_modifier;
3528 EMACS_UINT mod_hyper = hyper_modifier;
3529 EMACS_UINT mod_super = super_modifier;
3531 Lisp_Object tem;
3533 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3534 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3535 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3536 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3537 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3538 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3539 tem = Fget (Vx_super_keysym, Qmodifier_value);
3540 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3543 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3544 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3545 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3546 | ((state & shift_modifier) ? ShiftMask : 0)
3547 | ((state & ctrl_modifier) ? ControlMask : 0)
3548 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3551 /* Convert a keysym to its name. */
3553 char *
3554 x_get_keysym_name (keysym)
3555 KeySym keysym;
3557 char *value;
3559 BLOCK_INPUT;
3560 value = XKeysymToString (keysym);
3561 UNBLOCK_INPUT;
3563 return value;
3568 /* Mouse clicks and mouse movement. Rah. */
3570 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3572 If the event is a button press, then note that we have grabbed
3573 the mouse. */
3575 static Lisp_Object
3576 construct_mouse_click (result, event, f)
3577 struct input_event *result;
3578 XButtonEvent *event;
3579 struct frame *f;
3581 /* Make the event type NO_EVENT; we'll change that when we decide
3582 otherwise. */
3583 result->kind = MOUSE_CLICK_EVENT;
3584 result->code = event->button - Button1;
3585 result->timestamp = event->time;
3586 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3587 event->state)
3588 | (event->type == ButtonRelease
3589 ? up_modifier
3590 : down_modifier));
3592 XSETINT (result->x, event->x);
3593 XSETINT (result->y, event->y);
3594 XSETFRAME (result->frame_or_window, f);
3595 result->arg = Qnil;
3596 return Qnil;
3600 /* Function to report a mouse movement to the mainstream Emacs code.
3601 The input handler calls this.
3603 We have received a mouse movement event, which is given in *event.
3604 If the mouse is over a different glyph than it was last time, tell
3605 the mainstream emacs code by setting mouse_moved. If not, ask for
3606 another motion event, so we can check again the next time it moves. */
3608 static XMotionEvent last_mouse_motion_event;
3609 static Lisp_Object last_mouse_motion_frame;
3611 static int
3612 note_mouse_movement (frame, event)
3613 FRAME_PTR frame;
3614 XMotionEvent *event;
3616 last_mouse_movement_time = event->time;
3617 last_mouse_motion_event = *event;
3618 XSETFRAME (last_mouse_motion_frame, frame);
3620 if (!FRAME_X_OUTPUT (frame))
3621 return 0;
3623 if (event->window != FRAME_X_WINDOW (frame))
3625 frame->mouse_moved = 1;
3626 last_mouse_scroll_bar = Qnil;
3627 note_mouse_highlight (frame, -1, -1);
3628 last_mouse_glyph_frame = 0;
3629 return 1;
3633 /* Has the mouse moved off the glyph it was on at the last sighting? */
3634 if (frame != last_mouse_glyph_frame
3635 || event->x < last_mouse_glyph.x
3636 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3637 || event->y < last_mouse_glyph.y
3638 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3640 frame->mouse_moved = 1;
3641 last_mouse_scroll_bar = Qnil;
3642 note_mouse_highlight (frame, event->x, event->y);
3643 /* Remember which glyph we're now on. */
3644 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3645 last_mouse_glyph_frame = frame;
3646 return 1;
3649 return 0;
3653 /************************************************************************
3654 Mouse Face
3655 ************************************************************************/
3657 static void
3658 redo_mouse_highlight ()
3660 if (!NILP (last_mouse_motion_frame)
3661 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3662 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3663 last_mouse_motion_event.x,
3664 last_mouse_motion_event.y);
3669 /* Return the current position of the mouse.
3670 *FP should be a frame which indicates which display to ask about.
3672 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3673 and *PART to the frame, window, and scroll bar part that the mouse
3674 is over. Set *X and *Y to the portion and whole of the mouse's
3675 position on the scroll bar.
3677 If the mouse movement started elsewhere, set *FP to the frame the
3678 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3679 the mouse is over.
3681 Set *TIME to the server time-stamp for the time at which the mouse
3682 was at this position.
3684 Don't store anything if we don't have a valid set of values to report.
3686 This clears the mouse_moved flag, so we can wait for the next mouse
3687 movement. */
3689 static void
3690 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3691 FRAME_PTR *fp;
3692 int insist;
3693 Lisp_Object *bar_window;
3694 enum scroll_bar_part *part;
3695 Lisp_Object *x, *y;
3696 unsigned long *time;
3698 FRAME_PTR f1;
3700 BLOCK_INPUT;
3702 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3703 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3704 else
3706 Window root;
3707 int root_x, root_y;
3709 Window dummy_window;
3710 int dummy;
3712 Lisp_Object frame, tail;
3714 /* Clear the mouse-moved flag for every frame on this display. */
3715 FOR_EACH_FRAME (tail, frame)
3716 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3717 XFRAME (frame)->mouse_moved = 0;
3719 last_mouse_scroll_bar = Qnil;
3721 /* Figure out which root window we're on. */
3722 XQueryPointer (FRAME_X_DISPLAY (*fp),
3723 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3725 /* The root window which contains the pointer. */
3726 &root,
3728 /* Trash which we can't trust if the pointer is on
3729 a different screen. */
3730 &dummy_window,
3732 /* The position on that root window. */
3733 &root_x, &root_y,
3735 /* More trash we can't trust. */
3736 &dummy, &dummy,
3738 /* Modifier keys and pointer buttons, about which
3739 we don't care. */
3740 (unsigned int *) &dummy);
3742 /* Now we have a position on the root; find the innermost window
3743 containing the pointer. */
3745 Window win, child;
3746 int win_x, win_y;
3747 int parent_x = 0, parent_y = 0;
3749 win = root;
3751 /* XTranslateCoordinates can get errors if the window
3752 structure is changing at the same time this function
3753 is running. So at least we must not crash from them. */
3755 x_catch_errors (FRAME_X_DISPLAY (*fp));
3757 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3758 && FRAME_LIVE_P (last_mouse_frame))
3760 /* If mouse was grabbed on a frame, give coords for that frame
3761 even if the mouse is now outside it. */
3762 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3764 /* From-window, to-window. */
3765 root, FRAME_X_WINDOW (last_mouse_frame),
3767 /* From-position, to-position. */
3768 root_x, root_y, &win_x, &win_y,
3770 /* Child of win. */
3771 &child);
3772 f1 = last_mouse_frame;
3774 else
3776 while (1)
3778 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3780 /* From-window, to-window. */
3781 root, win,
3783 /* From-position, to-position. */
3784 root_x, root_y, &win_x, &win_y,
3786 /* Child of win. */
3787 &child);
3789 if (child == None || child == win)
3790 break;
3792 win = child;
3793 parent_x = win_x;
3794 parent_y = win_y;
3797 /* Now we know that:
3798 win is the innermost window containing the pointer
3799 (XTC says it has no child containing the pointer),
3800 win_x and win_y are the pointer's position in it
3801 (XTC did this the last time through), and
3802 parent_x and parent_y are the pointer's position in win's parent.
3803 (They are what win_x and win_y were when win was child.
3804 If win is the root window, it has no parent, and
3805 parent_{x,y} are invalid, but that's okay, because we'll
3806 never use them in that case.) */
3808 /* Is win one of our frames? */
3809 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3811 #ifdef USE_X_TOOLKIT
3812 /* If we end up with the menu bar window, say it's not
3813 on the frame. */
3814 if (f1 != NULL
3815 && f1->output_data.x->menubar_widget
3816 && win == XtWindow (f1->output_data.x->menubar_widget))
3817 f1 = NULL;
3818 #endif /* USE_X_TOOLKIT */
3821 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3822 f1 = 0;
3824 x_uncatch_errors ();
3826 /* If not, is it one of our scroll bars? */
3827 if (! f1)
3829 struct scroll_bar *bar;
3831 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3833 if (bar)
3835 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3836 win_x = parent_x;
3837 win_y = parent_y;
3841 if (f1 == 0 && insist > 0)
3842 f1 = SELECTED_FRAME ();
3844 if (f1)
3846 /* Ok, we found a frame. Store all the values.
3847 last_mouse_glyph is a rectangle used to reduce the
3848 generation of mouse events. To not miss any motion
3849 events, we must divide the frame into rectangles of the
3850 size of the smallest character that could be displayed
3851 on it, i.e. into the same rectangles that matrices on
3852 the frame are divided into. */
3854 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
3855 last_mouse_glyph_frame = f1;
3857 *bar_window = Qnil;
3858 *part = 0;
3859 *fp = f1;
3860 XSETINT (*x, win_x);
3861 XSETINT (*y, win_y);
3862 *time = last_mouse_movement_time;
3867 UNBLOCK_INPUT;
3872 /***********************************************************************
3873 Scroll bars
3874 ***********************************************************************/
3876 /* Scroll bar support. */
3878 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
3879 manages it.
3880 This can be called in GC, so we have to make sure to strip off mark
3881 bits. */
3883 static struct scroll_bar *
3884 x_window_to_scroll_bar (display, window_id)
3885 Display *display;
3886 Window window_id;
3888 Lisp_Object tail;
3890 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3891 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
3892 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
3894 for (tail = Vframe_list;
3895 XGCTYPE (tail) == Lisp_Cons;
3896 tail = XCDR (tail))
3898 Lisp_Object frame, bar, condemned;
3900 frame = XCAR (tail);
3901 /* All elements of Vframe_list should be frames. */
3902 if (! GC_FRAMEP (frame))
3903 abort ();
3905 /* Scan this frame's scroll bar list for a scroll bar with the
3906 right window ID. */
3907 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
3908 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
3909 /* This trick allows us to search both the ordinary and
3910 condemned scroll bar lists with one loop. */
3911 ! GC_NILP (bar) || (bar = condemned,
3912 condemned = Qnil,
3913 ! GC_NILP (bar));
3914 bar = XSCROLL_BAR (bar)->next)
3915 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id &&
3916 FRAME_X_DISPLAY (XFRAME (frame)) == display)
3917 return XSCROLL_BAR (bar);
3920 return 0;
3924 #if defined USE_LUCID
3926 /* Return the Lucid menu bar WINDOW is part of. Return null
3927 if WINDOW is not part of a menu bar. */
3929 static Widget
3930 x_window_to_menu_bar (window)
3931 Window window;
3933 Lisp_Object tail;
3935 for (tail = Vframe_list;
3936 XGCTYPE (tail) == Lisp_Cons;
3937 tail = XCDR (tail))
3939 Lisp_Object frame = XCAR (tail);
3940 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
3942 if (menu_bar && xlwmenu_window_p (menu_bar, window))
3943 return menu_bar;
3946 return NULL;
3949 #endif /* USE_LUCID */
3952 /************************************************************************
3953 Toolkit scroll bars
3954 ************************************************************************/
3956 #ifdef USE_TOOLKIT_SCROLL_BARS
3958 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
3959 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
3960 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
3961 struct scroll_bar *));
3962 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
3963 int, int, int));
3966 /* Lisp window being scrolled. Set when starting to interact with
3967 a toolkit scroll bar, reset to nil when ending the interaction. */
3969 static Lisp_Object window_being_scrolled;
3971 /* Last scroll bar part sent in xm_scroll_callback. */
3973 static int last_scroll_bar_part;
3975 /* Whether this is an Xaw with arrow-scrollbars. This should imply
3976 that movements of 1/20 of the screen size are mapped to up/down. */
3978 #ifndef USE_GTK
3979 /* Id of action hook installed for scroll bars. */
3981 static XtActionHookId action_hook_id;
3983 static Boolean xaw3d_arrow_scroll;
3985 /* Whether the drag scrolling maintains the mouse at the top of the
3986 thumb. If not, resizing the thumb needs to be done more carefully
3987 to avoid jerkyness. */
3989 static Boolean xaw3d_pick_top;
3991 /* Action hook installed via XtAppAddActionHook when toolkit scroll
3992 bars are used.. The hook is responsible for detecting when
3993 the user ends an interaction with the scroll bar, and generates
3994 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
3996 static void
3997 xt_action_hook (widget, client_data, action_name, event, params,
3998 num_params)
3999 Widget widget;
4000 XtPointer client_data;
4001 String action_name;
4002 XEvent *event;
4003 String *params;
4004 Cardinal *num_params;
4006 int scroll_bar_p;
4007 char *end_action;
4009 #ifdef USE_MOTIF
4010 scroll_bar_p = XmIsScrollBar (widget);
4011 end_action = "Release";
4012 #else /* !USE_MOTIF i.e. use Xaw */
4013 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4014 end_action = "EndScroll";
4015 #endif /* USE_MOTIF */
4017 if (scroll_bar_p
4018 && strcmp (action_name, end_action) == 0
4019 && WINDOWP (window_being_scrolled))
4021 struct window *w;
4023 x_send_scroll_bar_event (window_being_scrolled,
4024 scroll_bar_end_scroll, 0, 0);
4025 w = XWINDOW (window_being_scrolled);
4027 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4029 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4030 /* The thumb size is incorrect while dragging: fix it. */
4031 set_vertical_scroll_bar (w);
4033 window_being_scrolled = Qnil;
4034 last_scroll_bar_part = -1;
4036 /* Xt timeouts no longer needed. */
4037 toolkit_scroll_bar_interaction = 0;
4040 #endif /* not USE_GTK */
4042 /* A vector of windows used for communication between
4043 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4045 static struct window **scroll_bar_windows;
4046 static int scroll_bar_windows_size;
4049 /* Send a client message with message type Xatom_Scrollbar for a
4050 scroll action to the frame of WINDOW. PART is a value identifying
4051 the part of the scroll bar that was clicked on. PORTION is the
4052 amount to scroll of a whole of WHOLE. */
4054 static void
4055 x_send_scroll_bar_event (window, part, portion, whole)
4056 Lisp_Object window;
4057 int part, portion, whole;
4059 XEvent event;
4060 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4061 struct window *w = XWINDOW (window);
4062 struct frame *f = XFRAME (w->frame);
4063 int i;
4065 BLOCK_INPUT;
4067 /* Construct a ClientMessage event to send to the frame. */
4068 ev->type = ClientMessage;
4069 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4070 ev->display = FRAME_X_DISPLAY (f);
4071 ev->window = FRAME_X_WINDOW (f);
4072 ev->format = 32;
4074 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4075 not enough to store a pointer or Lisp_Object on a 64 bit system.
4076 So, store the window in scroll_bar_windows and pass the index
4077 into that array in the event. */
4078 for (i = 0; i < scroll_bar_windows_size; ++i)
4079 if (scroll_bar_windows[i] == NULL)
4080 break;
4082 if (i == scroll_bar_windows_size)
4084 int new_size = max (10, 2 * scroll_bar_windows_size);
4085 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4086 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4088 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4089 nbytes);
4090 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4091 scroll_bar_windows_size = new_size;
4094 scroll_bar_windows[i] = w;
4095 ev->data.l[0] = (long) i;
4096 ev->data.l[1] = (long) part;
4097 ev->data.l[2] = (long) 0;
4098 ev->data.l[3] = (long) portion;
4099 ev->data.l[4] = (long) whole;
4101 /* Make Xt timeouts work while the scroll bar is active. */
4102 toolkit_scroll_bar_interaction = 1;
4103 #ifdef USE_X_TOOLKIT
4104 x_activate_timeout_atimer ();
4105 #endif
4107 /* Setting the event mask to zero means that the message will
4108 be sent to the client that created the window, and if that
4109 window no longer exists, no event will be sent. */
4110 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4111 UNBLOCK_INPUT;
4115 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4116 in *IEVENT. */
4118 static void
4119 x_scroll_bar_to_input_event (event, ievent)
4120 XEvent *event;
4121 struct input_event *ievent;
4123 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4124 Lisp_Object window;
4125 struct frame *f;
4126 struct window *w;
4128 w = scroll_bar_windows[ev->data.l[0]];
4129 scroll_bar_windows[ev->data.l[0]] = NULL;
4131 XSETWINDOW (window, w);
4132 f = XFRAME (w->frame);
4134 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4135 ievent->frame_or_window = window;
4136 ievent->arg = Qnil;
4137 #ifdef USE_GTK
4138 ievent->timestamp = CurrentTime;
4139 #else
4140 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4141 #endif
4142 ievent->part = ev->data.l[1];
4143 ievent->code = ev->data.l[2];
4144 ievent->x = make_number ((int) ev->data.l[3]);
4145 ievent->y = make_number ((int) ev->data.l[4]);
4146 ievent->modifiers = 0;
4150 #ifdef USE_MOTIF
4152 /* Minimum and maximum values used for Motif scroll bars. */
4154 #define XM_SB_MAX 10000000
4157 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4158 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4159 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4161 static void
4162 xm_scroll_callback (widget, client_data, call_data)
4163 Widget widget;
4164 XtPointer client_data, call_data;
4166 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4167 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4168 int part = -1, whole = 0, portion = 0;
4170 switch (cs->reason)
4172 case XmCR_DECREMENT:
4173 bar->dragging = Qnil;
4174 part = scroll_bar_up_arrow;
4175 break;
4177 case XmCR_INCREMENT:
4178 bar->dragging = Qnil;
4179 part = scroll_bar_down_arrow;
4180 break;
4182 case XmCR_PAGE_DECREMENT:
4183 bar->dragging = Qnil;
4184 part = scroll_bar_above_handle;
4185 break;
4187 case XmCR_PAGE_INCREMENT:
4188 bar->dragging = Qnil;
4189 part = scroll_bar_below_handle;
4190 break;
4192 case XmCR_TO_TOP:
4193 bar->dragging = Qnil;
4194 part = scroll_bar_to_top;
4195 break;
4197 case XmCR_TO_BOTTOM:
4198 bar->dragging = Qnil;
4199 part = scroll_bar_to_bottom;
4200 break;
4202 case XmCR_DRAG:
4204 int slider_size;
4206 /* Get the slider size. */
4207 BLOCK_INPUT;
4208 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4209 UNBLOCK_INPUT;
4211 whole = XM_SB_MAX - slider_size;
4212 portion = min (cs->value, whole);
4213 part = scroll_bar_handle;
4214 bar->dragging = make_number (cs->value);
4216 break;
4218 case XmCR_VALUE_CHANGED:
4219 break;
4222 if (part >= 0)
4224 window_being_scrolled = bar->window;
4225 last_scroll_bar_part = part;
4226 x_send_scroll_bar_event (bar->window, part, portion, whole);
4231 #else /* !USE_MOTIF, i.e. Xaw or GTK */
4232 #ifdef USE_GTK
4233 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4234 bar widget. DATA is a pointer to the scroll_bar structure. */
4236 static void
4237 xg_scroll_callback (widget, data)
4238 GtkRange *widget;
4239 gpointer data;
4241 struct scroll_bar *bar = (struct scroll_bar *) data;
4242 gdouble previous;
4243 gdouble position;
4244 gdouble *p;
4245 int diff;
4247 int part = -1, whole = 0, portion = 0;
4248 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4250 position = gtk_adjustment_get_value (adj);
4252 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4253 if (! p)
4255 p = (gdouble*) xmalloc (sizeof (gdouble));
4256 *p = XG_SB_MIN;
4257 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4260 previous = *p;
4261 *p = position;
4263 if (xg_ignore_gtk_scrollbar) return;
4265 diff = (int) (position - previous);
4267 if (diff == (int) adj->step_increment)
4269 part = scroll_bar_down_arrow;
4270 bar->dragging = Qnil;
4272 else if (-diff == (int) adj->step_increment)
4274 part = scroll_bar_up_arrow;
4275 bar->dragging = Qnil;
4277 else if (diff == (int) adj->page_increment)
4279 part = scroll_bar_below_handle;
4280 bar->dragging = Qnil;
4282 else if (-diff == (int) adj->page_increment)
4284 part = scroll_bar_above_handle;
4285 bar->dragging = Qnil;
4287 else
4289 part = scroll_bar_handle;
4290 whole = adj->upper - adj->page_size;
4291 portion = min ((int)position, whole);
4292 bar->dragging = make_number ((int)portion);
4295 if (part >= 0)
4297 window_being_scrolled = bar->window;
4298 last_scroll_bar_part = part;
4299 x_send_scroll_bar_event (bar->window, part, portion, whole);
4303 #else /* not USE_GTK */
4305 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4306 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4307 scroll bar struct. CALL_DATA is a pointer to a float saying where
4308 the thumb is. */
4310 static void
4311 xaw_jump_callback (widget, client_data, call_data)
4312 Widget widget;
4313 XtPointer client_data, call_data;
4315 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4316 float top = *(float *) call_data;
4317 float shown;
4318 int whole, portion, height;
4319 int part;
4321 /* Get the size of the thumb, a value between 0 and 1. */
4322 BLOCK_INPUT;
4323 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4324 UNBLOCK_INPUT;
4326 whole = 10000000;
4327 portion = shown < 1 ? top * whole : 0;
4329 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
4330 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4331 the bottom, so we force the scrolling whenever we see that we're
4332 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4333 we try to ensure that we always stay two pixels away from the
4334 bottom). */
4335 part = scroll_bar_down_arrow;
4336 else
4337 part = scroll_bar_handle;
4339 window_being_scrolled = bar->window;
4340 bar->dragging = make_number (portion);
4341 last_scroll_bar_part = part;
4342 x_send_scroll_bar_event (bar->window, part, portion, whole);
4346 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4347 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4348 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4349 the scroll bar. CALL_DATA is an integer specifying the action that
4350 has taken place. Its magnitude is in the range 0..height of the
4351 scroll bar. Negative values mean scroll towards buffer start.
4352 Values < height of scroll bar mean line-wise movement. */
4354 static void
4355 xaw_scroll_callback (widget, client_data, call_data)
4356 Widget widget;
4357 XtPointer client_data, call_data;
4359 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4360 /* The position really is stored cast to a pointer. */
4361 int position = (long) call_data;
4362 Dimension height;
4363 int part;
4365 /* Get the height of the scroll bar. */
4366 BLOCK_INPUT;
4367 XtVaGetValues (widget, XtNheight, &height, NULL);
4368 UNBLOCK_INPUT;
4370 if (abs (position) >= height)
4371 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4373 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4374 it maps line-movement to call_data = max(5, height/20). */
4375 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
4376 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4377 else
4378 part = scroll_bar_move_ratio;
4380 window_being_scrolled = bar->window;
4381 bar->dragging = Qnil;
4382 last_scroll_bar_part = part;
4383 x_send_scroll_bar_event (bar->window, part, position, height);
4386 #endif /* not USE_GTK */
4387 #endif /* not USE_MOTIF */
4389 #define SCROLL_BAR_NAME "verticalScrollBar"
4391 /* Create the widget for scroll bar BAR on frame F. Record the widget
4392 and X window of the scroll bar in BAR. */
4394 #ifdef USE_GTK
4395 static void
4396 x_create_toolkit_scroll_bar (f, bar)
4397 struct frame *f;
4398 struct scroll_bar *bar;
4400 char *scroll_bar_name = SCROLL_BAR_NAME;
4402 BLOCK_INPUT;
4403 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4404 scroll_bar_name);
4405 UNBLOCK_INPUT;
4408 #else /* not USE_GTK */
4410 static void
4411 x_create_toolkit_scroll_bar (f, bar)
4412 struct frame *f;
4413 struct scroll_bar *bar;
4415 Window xwindow;
4416 Widget widget;
4417 Arg av[20];
4418 int ac = 0;
4419 char *scroll_bar_name = SCROLL_BAR_NAME;
4420 unsigned long pixel;
4422 BLOCK_INPUT;
4424 #ifdef USE_MOTIF
4425 /* Set resources. Create the widget. */
4426 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4427 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4428 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4429 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4430 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4431 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4432 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4434 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4435 if (pixel != -1)
4437 XtSetArg (av[ac], XmNforeground, pixel);
4438 ++ac;
4441 pixel = f->output_data.x->scroll_bar_background_pixel;
4442 if (pixel != -1)
4444 XtSetArg (av[ac], XmNbackground, pixel);
4445 ++ac;
4448 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4449 scroll_bar_name, av, ac);
4451 /* Add one callback for everything that can happen. */
4452 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4453 (XtPointer) bar);
4454 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4455 (XtPointer) bar);
4456 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4457 (XtPointer) bar);
4458 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4459 (XtPointer) bar);
4460 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4461 (XtPointer) bar);
4462 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4463 (XtPointer) bar);
4464 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4465 (XtPointer) bar);
4467 /* Realize the widget. Only after that is the X window created. */
4468 XtRealizeWidget (widget);
4470 /* Set the cursor to an arrow. I didn't find a resource to do that.
4471 And I'm wondering why it hasn't an arrow cursor by default. */
4472 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4473 f->output_data.x->nontext_cursor);
4475 #else /* !USE_MOTIF i.e. use Xaw */
4477 /* Set resources. Create the widget. The background of the
4478 Xaw3d scroll bar widget is a little bit light for my taste.
4479 We don't alter it here to let users change it according
4480 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4481 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4482 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4483 /* For smoother scrolling with Xaw3d -sm */
4484 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4486 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4487 if (pixel != -1)
4489 XtSetArg (av[ac], XtNforeground, pixel);
4490 ++ac;
4493 pixel = f->output_data.x->scroll_bar_background_pixel;
4494 if (pixel != -1)
4496 XtSetArg (av[ac], XtNbackground, pixel);
4497 ++ac;
4500 /* Top/bottom shadow colors. */
4502 /* Allocate them, if necessary. */
4503 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4505 pixel = f->output_data.x->scroll_bar_background_pixel;
4506 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4507 &pixel, 1.2, 0x8000))
4508 pixel = -1;
4509 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4511 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4513 pixel = f->output_data.x->scroll_bar_background_pixel;
4514 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4515 &pixel, 0.6, 0x4000))
4516 pixel = -1;
4517 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4520 #ifdef XtNbeNiceToColormap
4521 /* Tell the toolkit about them. */
4522 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4523 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4524 /* We tried to allocate a color for the top/bottom shadow, and
4525 failed, so tell Xaw3d to use dithering instead. */
4527 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4528 ++ac;
4530 else
4531 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4532 be more consistent with other emacs 3d colors, and since Xaw3d is
4533 not good at dealing with allocation failure. */
4535 /* This tells Xaw3d to use real colors instead of dithering for
4536 the shadows. */
4537 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4538 ++ac;
4540 /* Specify the colors. */
4541 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4542 if (pixel != -1)
4544 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4545 ++ac;
4547 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4548 if (pixel != -1)
4550 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4551 ++ac;
4554 #endif
4556 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4557 f->output_data.x->edit_widget, av, ac);
4560 char *initial = "";
4561 char *val = initial;
4562 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4563 #ifdef XtNarrowScrollbars
4564 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4565 #endif
4566 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4567 if (xaw3d_arrow_scroll || val == initial)
4568 { /* ARROW_SCROLL */
4569 xaw3d_arrow_scroll = True;
4570 /* Isn't that just a personal preference ? --Stef */
4571 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4575 /* Define callbacks. */
4576 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4577 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4578 (XtPointer) bar);
4580 /* Realize the widget. Only after that is the X window created. */
4581 XtRealizeWidget (widget);
4583 #endif /* !USE_MOTIF */
4585 /* Install an action hook that lets us detect when the user
4586 finishes interacting with a scroll bar. */
4587 if (action_hook_id == 0)
4588 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4590 /* Remember X window and widget in the scroll bar vector. */
4591 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4592 xwindow = XtWindow (widget);
4593 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
4595 UNBLOCK_INPUT;
4597 #endif /* not USE_GTK */
4600 /* Set the thumb size and position of scroll bar BAR. We are currently
4601 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4603 #ifdef USE_GTK
4604 static void
4605 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4606 struct scroll_bar *bar;
4607 int portion, position, whole;
4609 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4612 #else /* not USE_GTK */
4613 static void
4614 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4615 struct scroll_bar *bar;
4616 int portion, position, whole;
4618 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4619 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4620 float top, shown;
4622 BLOCK_INPUT;
4624 #ifdef USE_MOTIF
4626 /* We use an estimate of 30 chars per line rather than the real
4627 `portion' value. This has the disadvantage that the thumb size
4628 is not very representative, but it makes our life a lot easier.
4629 Otherwise, we have to constantly adjust the thumb size, which
4630 we can't always do quickly enough: while dragging, the size of
4631 the thumb might prevent the user from dragging the thumb all the
4632 way to the end. but Motif and some versions of Xaw3d don't allow
4633 updating the thumb size while dragging. Also, even if we can update
4634 its size, the update will often happen too late.
4635 If you don't believe it, check out revision 1.650 of xterm.c to see
4636 what hoops we were going through and the still poor behavior we got. */
4637 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4638 /* When the thumb is at the bottom, position == whole.
4639 So we need to increase `whole' to make space for the thumb. */
4640 whole += portion;
4642 if (whole <= 0)
4643 top = 0, shown = 1;
4644 else
4646 top = (float) position / whole;
4647 shown = (float) portion / whole;
4650 if (NILP (bar->dragging))
4652 int size, value;
4654 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4655 is the scroll bar's maximum and MIN is the scroll bar's minimum
4656 value. */
4657 size = shown * XM_SB_MAX;
4658 size = min (size, XM_SB_MAX);
4659 size = max (size, 1);
4661 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4662 value = top * XM_SB_MAX;
4663 value = min (value, XM_SB_MAX - size);
4665 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4667 #else /* !USE_MOTIF i.e. use Xaw */
4669 if (whole == 0)
4670 top = 0, shown = 1;
4671 else
4673 top = (float) position / whole;
4674 shown = (float) portion / whole;
4678 float old_top, old_shown;
4679 Dimension height;
4680 XtVaGetValues (widget,
4681 XtNtopOfThumb, &old_top,
4682 XtNshown, &old_shown,
4683 XtNheight, &height,
4684 NULL);
4686 /* Massage the top+shown values. */
4687 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4688 top = max (0, min (1, top));
4689 else
4690 top = old_top;
4691 /* Keep two pixels available for moving the thumb down. */
4692 shown = max (0, min (1 - top - (2.0 / height), shown));
4694 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4695 check that your system's configuration file contains a define
4696 for `NARROWPROTO'. See s/freebsd.h for an example. */
4697 if (top != old_top || shown != old_shown)
4699 if (NILP (bar->dragging))
4700 XawScrollbarSetThumb (widget, top, shown);
4701 else
4703 /* Try to make the scrolling a tad smoother. */
4704 if (!xaw3d_pick_top)
4705 shown = min (shown, old_shown);
4707 XawScrollbarSetThumb (widget, top, shown);
4711 #endif /* !USE_MOTIF */
4713 UNBLOCK_INPUT;
4715 #endif /* not USE_GTK */
4717 #endif /* USE_TOOLKIT_SCROLL_BARS */
4721 /************************************************************************
4722 Scroll bars, general
4723 ************************************************************************/
4725 /* Create a scroll bar and return the scroll bar vector for it. W is
4726 the Emacs window on which to create the scroll bar. TOP, LEFT,
4727 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4728 scroll bar. */
4730 static struct scroll_bar *
4731 x_scroll_bar_create (w, top, left, width, height)
4732 struct window *w;
4733 int top, left, width, height;
4735 struct frame *f = XFRAME (w->frame);
4736 struct scroll_bar *bar
4737 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
4739 BLOCK_INPUT;
4741 #ifdef USE_TOOLKIT_SCROLL_BARS
4742 x_create_toolkit_scroll_bar (f, bar);
4743 #else /* not USE_TOOLKIT_SCROLL_BARS */
4745 XSetWindowAttributes a;
4746 unsigned long mask;
4747 Window window;
4749 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4750 if (a.background_pixel == -1)
4751 a.background_pixel = f->output_data.x->background_pixel;
4753 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4754 | ButtonMotionMask | PointerMotionHintMask
4755 | ExposureMask);
4756 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4758 mask = (CWBackPixel | CWEventMask | CWCursor);
4760 /* Clear the area of W that will serve as a scroll bar. This is
4761 for the case that a window has been split horizontally. In
4762 this case, no clear_frame is generated to reduce flickering. */
4763 if (width > 0 && height > 0)
4764 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4765 left, top, width,
4766 window_box_height (w), False);
4768 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4769 /* Position and size of scroll bar. */
4770 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4771 top,
4772 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4773 height,
4774 /* Border width, depth, class, and visual. */
4776 CopyFromParent,
4777 CopyFromParent,
4778 CopyFromParent,
4779 /* Attributes. */
4780 mask, &a);
4781 SET_SCROLL_BAR_X_WINDOW (bar, window);
4783 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4785 XSETWINDOW (bar->window, w);
4786 XSETINT (bar->top, top);
4787 XSETINT (bar->left, left);
4788 XSETINT (bar->width, width);
4789 XSETINT (bar->height, height);
4790 XSETINT (bar->start, 0);
4791 XSETINT (bar->end, 0);
4792 bar->dragging = Qnil;
4794 /* Add bar to its frame's list of scroll bars. */
4795 bar->next = FRAME_SCROLL_BARS (f);
4796 bar->prev = Qnil;
4797 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4798 if (!NILP (bar->next))
4799 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4801 /* Map the window/widget. */
4802 #ifdef USE_TOOLKIT_SCROLL_BARS
4804 #ifdef USE_GTK
4805 xg_update_scrollbar_pos (f,
4806 SCROLL_BAR_X_WINDOW (bar),
4807 top,
4808 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4809 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4810 max (height, 1));
4811 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
4812 #else /* not USE_GTK */
4813 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4814 XtConfigureWidget (scroll_bar,
4815 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4816 top,
4817 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4818 max (height, 1), 0);
4819 XtMapWidget (scroll_bar);
4820 #endif /* not USE_GTK */
4822 #else /* not USE_TOOLKIT_SCROLL_BARS */
4823 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4824 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4826 UNBLOCK_INPUT;
4827 return bar;
4831 /* Draw BAR's handle in the proper position.
4833 If the handle is already drawn from START to END, don't bother
4834 redrawing it, unless REBUILD is non-zero; in that case, always
4835 redraw it. (REBUILD is handy for drawing the handle after expose
4836 events.)
4838 Normally, we want to constrain the start and end of the handle to
4839 fit inside its rectangle, but if the user is dragging the scroll
4840 bar handle, we want to let them drag it down all the way, so that
4841 the bar's top is as far down as it goes; otherwise, there's no way
4842 to move to the very end of the buffer. */
4844 #ifndef USE_TOOLKIT_SCROLL_BARS
4846 static void
4847 x_scroll_bar_set_handle (bar, start, end, rebuild)
4848 struct scroll_bar *bar;
4849 int start, end;
4850 int rebuild;
4852 int dragging = ! NILP (bar->dragging);
4853 Window w = SCROLL_BAR_X_WINDOW (bar);
4854 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4855 GC gc = f->output_data.x->normal_gc;
4857 /* If the display is already accurate, do nothing. */
4858 if (! rebuild
4859 && start == XINT (bar->start)
4860 && end == XINT (bar->end))
4861 return;
4863 BLOCK_INPUT;
4866 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
4867 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
4868 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
4870 /* Make sure the values are reasonable, and try to preserve
4871 the distance between start and end. */
4873 int length = end - start;
4875 if (start < 0)
4876 start = 0;
4877 else if (start > top_range)
4878 start = top_range;
4879 end = start + length;
4881 if (end < start)
4882 end = start;
4883 else if (end > top_range && ! dragging)
4884 end = top_range;
4887 /* Store the adjusted setting in the scroll bar. */
4888 XSETINT (bar->start, start);
4889 XSETINT (bar->end, end);
4891 /* Clip the end position, just for display. */
4892 if (end > top_range)
4893 end = top_range;
4895 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
4896 below top positions, to make sure the handle is always at least
4897 that many pixels tall. */
4898 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
4900 /* Draw the empty space above the handle. Note that we can't clear
4901 zero-height areas; that means "clear to end of window." */
4902 if (0 < start)
4903 x_clear_area (FRAME_X_DISPLAY (f), w,
4904 /* x, y, width, height, and exposures. */
4905 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4906 VERTICAL_SCROLL_BAR_TOP_BORDER,
4907 inside_width, start,
4908 False);
4910 /* Change to proper foreground color if one is specified. */
4911 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4912 XSetForeground (FRAME_X_DISPLAY (f), gc,
4913 f->output_data.x->scroll_bar_foreground_pixel);
4915 /* Draw the handle itself. */
4916 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
4917 /* x, y, width, height */
4918 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4919 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
4920 inside_width, end - start);
4922 /* Restore the foreground color of the GC if we changed it above. */
4923 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
4924 XSetForeground (FRAME_X_DISPLAY (f), gc,
4925 f->output_data.x->foreground_pixel);
4927 /* Draw the empty space below the handle. Note that we can't
4928 clear zero-height areas; that means "clear to end of window." */
4929 if (end < inside_height)
4930 x_clear_area (FRAME_X_DISPLAY (f), w,
4931 /* x, y, width, height, and exposures. */
4932 VERTICAL_SCROLL_BAR_LEFT_BORDER,
4933 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
4934 inside_width, inside_height - end,
4935 False);
4939 UNBLOCK_INPUT;
4942 #endif /* !USE_TOOLKIT_SCROLL_BARS */
4944 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
4945 nil. */
4947 static void
4948 x_scroll_bar_remove (bar)
4949 struct scroll_bar *bar;
4951 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4952 BLOCK_INPUT;
4954 #ifdef USE_TOOLKIT_SCROLL_BARS
4955 #ifdef USE_GTK
4956 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
4957 #else /* not USE_GTK */
4958 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
4959 #endif /* not USE_GTK */
4960 #else
4961 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
4962 #endif
4964 /* Disassociate this scroll bar from its window. */
4965 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
4967 UNBLOCK_INPUT;
4971 /* Set the handle of the vertical scroll bar for WINDOW to indicate
4972 that we are displaying PORTION characters out of a total of WHOLE
4973 characters, starting at POSITION. If WINDOW has no scroll bar,
4974 create one. */
4976 static void
4977 XTset_vertical_scroll_bar (w, portion, whole, position)
4978 struct window *w;
4979 int portion, whole, position;
4981 struct frame *f = XFRAME (w->frame);
4982 struct scroll_bar *bar;
4983 int top, height, left, sb_left, width, sb_width;
4984 int window_y, window_height;
4986 /* Get window dimensions. */
4987 window_box (w, -1, 0, &window_y, 0, &window_height);
4988 top = window_y;
4989 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
4990 height = window_height;
4992 /* Compute the left edge of the scroll bar area. */
4993 left = WINDOW_SCROLL_BAR_AREA_X (w);
4995 /* Compute the width of the scroll bar which might be less than
4996 the width of the area reserved for the scroll bar. */
4997 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
4998 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
4999 else
5000 sb_width = width;
5002 /* Compute the left edge of the scroll bar. */
5003 #ifdef USE_TOOLKIT_SCROLL_BARS
5004 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5005 sb_left = (left +
5006 (WINDOW_RIGHTMOST_P (w)
5007 ? width - sb_width - (width - sb_width) / 2
5008 : 0));
5009 else
5010 sb_left = (left +
5011 (WINDOW_LEFTMOST_P (w)
5012 ? (width - sb_width) / 2
5013 : width - sb_width));
5014 #else
5015 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5016 sb_left = left + width - sb_width;
5017 else
5018 sb_left = left;
5019 #endif
5021 /* Does the scroll bar exist yet? */
5022 if (NILP (w->vertical_scroll_bar))
5024 if (width > 0 && height > 0)
5026 BLOCK_INPUT;
5027 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5028 left, top, width, height, False);
5029 UNBLOCK_INPUT;
5032 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5034 else
5036 /* It may just need to be moved and resized. */
5037 unsigned int mask = 0;
5039 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5041 BLOCK_INPUT;
5043 if (sb_left != XINT (bar->left))
5044 mask |= CWX;
5045 if (top != XINT (bar->top))
5046 mask |= CWY;
5047 if (sb_width != XINT (bar->width))
5048 mask |= CWWidth;
5049 if (height != XINT (bar->height))
5050 mask |= CWHeight;
5052 #ifdef USE_TOOLKIT_SCROLL_BARS
5054 /* Move/size the scroll bar widget. */
5055 if (mask)
5057 /* Since toolkit scroll bars are smaller than the space reserved
5058 for them on the frame, we have to clear "under" them. */
5059 if (width > 0 && height > 0)
5060 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5061 left, top, width, height, False);
5062 #ifdef USE_GTK
5063 xg_update_scrollbar_pos (f,
5064 SCROLL_BAR_X_WINDOW (bar),
5065 top,
5066 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5067 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5068 max (height, 1));
5069 #else /* not USE_GTK */
5070 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5071 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5072 top,
5073 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5074 max (height, 1), 0);
5075 #endif /* not USE_GTK */
5077 #else /* not USE_TOOLKIT_SCROLL_BARS */
5079 /* Clear areas not covered by the scroll bar because of
5080 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5081 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5083 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5084 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5085 height, False);
5086 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5087 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5088 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5089 height, False);
5092 /* Clear areas not covered by the scroll bar because it's not as
5093 wide as the area reserved for it. This makes sure a
5094 previous mode line display is cleared after C-x 2 C-x 1, for
5095 example. */
5097 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5098 int rest = area_width - sb_width;
5099 if (rest > 0 && height > 0)
5101 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5102 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5103 left + area_width - rest, top,
5104 rest, height, False);
5105 else
5106 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5107 left, top, rest, height, False);
5111 /* Move/size the scroll bar window. */
5112 if (mask)
5114 XWindowChanges wc;
5116 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5117 wc.y = top;
5118 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5119 wc.height = height;
5120 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
5121 mask, &wc);
5124 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5126 /* Remember new settings. */
5127 XSETINT (bar->left, sb_left);
5128 XSETINT (bar->top, top);
5129 XSETINT (bar->width, sb_width);
5130 XSETINT (bar->height, height);
5132 UNBLOCK_INPUT;
5135 #ifdef USE_TOOLKIT_SCROLL_BARS
5136 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5137 #else /* not USE_TOOLKIT_SCROLL_BARS */
5138 /* Set the scroll bar's current state, unless we're currently being
5139 dragged. */
5140 if (NILP (bar->dragging))
5142 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5144 if (whole == 0)
5145 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5146 else
5148 int start = ((double) position * top_range) / whole;
5149 int end = ((double) (position + portion) * top_range) / whole;
5150 x_scroll_bar_set_handle (bar, start, end, 0);
5153 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5155 XSETVECTOR (w->vertical_scroll_bar, bar);
5159 /* The following three hooks are used when we're doing a thorough
5160 redisplay of the frame. We don't explicitly know which scroll bars
5161 are going to be deleted, because keeping track of when windows go
5162 away is a real pain - "Can you say set-window-configuration, boys
5163 and girls?" Instead, we just assert at the beginning of redisplay
5164 that *all* scroll bars are to be removed, and then save a scroll bar
5165 from the fiery pit when we actually redisplay its window. */
5167 /* Arrange for all scroll bars on FRAME to be removed at the next call
5168 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5169 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5171 static void
5172 XTcondemn_scroll_bars (frame)
5173 FRAME_PTR frame;
5175 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5176 while (! NILP (FRAME_SCROLL_BARS (frame)))
5178 Lisp_Object bar;
5179 bar = FRAME_SCROLL_BARS (frame);
5180 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5181 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5182 XSCROLL_BAR (bar)->prev = Qnil;
5183 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5184 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5185 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5190 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5191 Note that WINDOW isn't necessarily condemned at all. */
5193 static void
5194 XTredeem_scroll_bar (window)
5195 struct window *window;
5197 struct scroll_bar *bar;
5198 struct frame *f;
5200 /* We can't redeem this window's scroll bar if it doesn't have one. */
5201 if (NILP (window->vertical_scroll_bar))
5202 abort ();
5204 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5206 /* Unlink it from the condemned list. */
5207 f = XFRAME (WINDOW_FRAME (window));
5208 if (NILP (bar->prev))
5210 /* If the prev pointer is nil, it must be the first in one of
5211 the lists. */
5212 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5213 /* It's not condemned. Everything's fine. */
5214 return;
5215 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5216 window->vertical_scroll_bar))
5217 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5218 else
5219 /* If its prev pointer is nil, it must be at the front of
5220 one or the other! */
5221 abort ();
5223 else
5224 XSCROLL_BAR (bar->prev)->next = bar->next;
5226 if (! NILP (bar->next))
5227 XSCROLL_BAR (bar->next)->prev = bar->prev;
5229 bar->next = FRAME_SCROLL_BARS (f);
5230 bar->prev = Qnil;
5231 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5232 if (! NILP (bar->next))
5233 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5236 /* Remove all scroll bars on FRAME that haven't been saved since the
5237 last call to `*condemn_scroll_bars_hook'. */
5239 static void
5240 XTjudge_scroll_bars (f)
5241 FRAME_PTR f;
5243 Lisp_Object bar, next;
5245 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5247 /* Clear out the condemned list now so we won't try to process any
5248 more events on the hapless scroll bars. */
5249 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5251 for (; ! NILP (bar); bar = next)
5253 struct scroll_bar *b = XSCROLL_BAR (bar);
5255 x_scroll_bar_remove (b);
5257 next = b->next;
5258 b->next = b->prev = Qnil;
5261 /* Now there should be no references to the condemned scroll bars,
5262 and they should get garbage-collected. */
5266 #ifndef USE_TOOLKIT_SCROLL_BARS
5267 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5268 is a no-op when using toolkit scroll bars.
5270 This may be called from a signal handler, so we have to ignore GC
5271 mark bits. */
5273 static void
5274 x_scroll_bar_expose (bar, event)
5275 struct scroll_bar *bar;
5276 XEvent *event;
5278 Window w = SCROLL_BAR_X_WINDOW (bar);
5279 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5280 GC gc = f->output_data.x->normal_gc;
5281 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5283 BLOCK_INPUT;
5285 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
5287 /* Switch to scroll bar foreground color. */
5288 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5289 XSetForeground (FRAME_X_DISPLAY (f), gc,
5290 f->output_data.x->scroll_bar_foreground_pixel);
5292 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5293 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5295 /* x, y, width, height */
5296 0, 0,
5297 XINT (bar->width) - 1 - width_trim - width_trim,
5298 XINT (bar->height) - 1);
5300 /* Restore the foreground color of the GC if we changed it above. */
5301 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5302 XSetForeground (FRAME_X_DISPLAY (f), gc,
5303 f->output_data.x->foreground_pixel);
5305 UNBLOCK_INPUT;
5308 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5310 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5311 is set to something other than NO_EVENT, it is enqueued.
5313 This may be called from a signal handler, so we have to ignore GC
5314 mark bits. */
5317 static void
5318 x_scroll_bar_handle_click (bar, event, emacs_event)
5319 struct scroll_bar *bar;
5320 XEvent *event;
5321 struct input_event *emacs_event;
5323 if (! GC_WINDOWP (bar->window))
5324 abort ();
5326 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5327 emacs_event->code = event->xbutton.button - Button1;
5328 emacs_event->modifiers
5329 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5330 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5331 event->xbutton.state)
5332 | (event->type == ButtonRelease
5333 ? up_modifier
5334 : down_modifier));
5335 emacs_event->frame_or_window = bar->window;
5336 emacs_event->arg = Qnil;
5337 emacs_event->timestamp = event->xbutton.time;
5339 #if 0
5340 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5341 int internal_height
5342 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5343 #endif
5344 int top_range
5345 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5346 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5348 if (y < 0) y = 0;
5349 if (y > top_range) y = top_range;
5351 if (y < XINT (bar->start))
5352 emacs_event->part = scroll_bar_above_handle;
5353 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5354 emacs_event->part = scroll_bar_handle;
5355 else
5356 emacs_event->part = scroll_bar_below_handle;
5358 /* Just because the user has clicked on the handle doesn't mean
5359 they want to drag it. Lisp code needs to be able to decide
5360 whether or not we're dragging. */
5361 #if 0
5362 /* If the user has just clicked on the handle, record where they're
5363 holding it. */
5364 if (event->type == ButtonPress
5365 && emacs_event->part == scroll_bar_handle)
5366 XSETINT (bar->dragging, y - XINT (bar->start));
5367 #endif
5369 #ifndef USE_TOOLKIT_SCROLL_BARS
5370 /* If the user has released the handle, set it to its final position. */
5371 if (event->type == ButtonRelease
5372 && ! NILP (bar->dragging))
5374 int new_start = y - XINT (bar->dragging);
5375 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5377 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5378 bar->dragging = Qnil;
5380 #endif
5382 /* Same deal here as the other #if 0. */
5383 #if 0
5384 /* Clicks on the handle are always reported as occurring at the top of
5385 the handle. */
5386 if (emacs_event->part == scroll_bar_handle)
5387 emacs_event->x = bar->start;
5388 else
5389 XSETINT (emacs_event->x, y);
5390 #else
5391 XSETINT (emacs_event->x, y);
5392 #endif
5394 XSETINT (emacs_event->y, top_range);
5398 #ifndef USE_TOOLKIT_SCROLL_BARS
5400 /* Handle some mouse motion while someone is dragging the scroll bar.
5402 This may be called from a signal handler, so we have to ignore GC
5403 mark bits. */
5405 static void
5406 x_scroll_bar_note_movement (bar, event)
5407 struct scroll_bar *bar;
5408 XEvent *event;
5410 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5412 last_mouse_movement_time = event->xmotion.time;
5414 f->mouse_moved = 1;
5415 XSETVECTOR (last_mouse_scroll_bar, bar);
5417 /* If we're dragging the bar, display it. */
5418 if (! GC_NILP (bar->dragging))
5420 /* Where should the handle be now? */
5421 int new_start = event->xmotion.y - XINT (bar->dragging);
5423 if (new_start != XINT (bar->start))
5425 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5427 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5432 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5434 /* Return information to the user about the current position of the mouse
5435 on the scroll bar. */
5437 static void
5438 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5439 FRAME_PTR *fp;
5440 Lisp_Object *bar_window;
5441 enum scroll_bar_part *part;
5442 Lisp_Object *x, *y;
5443 unsigned long *time;
5445 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5446 Window w = SCROLL_BAR_X_WINDOW (bar);
5447 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5448 int win_x, win_y;
5449 Window dummy_window;
5450 int dummy_coord;
5451 unsigned int dummy_mask;
5453 BLOCK_INPUT;
5455 /* Get the mouse's position relative to the scroll bar window, and
5456 report that. */
5457 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5459 /* Root, child, root x and root y. */
5460 &dummy_window, &dummy_window,
5461 &dummy_coord, &dummy_coord,
5463 /* Position relative to scroll bar. */
5464 &win_x, &win_y,
5466 /* Mouse buttons and modifier keys. */
5467 &dummy_mask))
5469 else
5471 #if 0
5472 int inside_height
5473 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5474 #endif
5475 int top_range
5476 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5478 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5480 if (! NILP (bar->dragging))
5481 win_y -= XINT (bar->dragging);
5483 if (win_y < 0)
5484 win_y = 0;
5485 if (win_y > top_range)
5486 win_y = top_range;
5488 *fp = f;
5489 *bar_window = bar->window;
5491 if (! NILP (bar->dragging))
5492 *part = scroll_bar_handle;
5493 else if (win_y < XINT (bar->start))
5494 *part = scroll_bar_above_handle;
5495 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5496 *part = scroll_bar_handle;
5497 else
5498 *part = scroll_bar_below_handle;
5500 XSETINT (*x, win_y);
5501 XSETINT (*y, top_range);
5503 f->mouse_moved = 0;
5504 last_mouse_scroll_bar = Qnil;
5507 *time = last_mouse_movement_time;
5509 UNBLOCK_INPUT;
5513 /* The screen has been cleared so we may have changed foreground or
5514 background colors, and the scroll bars may need to be redrawn.
5515 Clear out the scroll bars, and ask for expose events, so we can
5516 redraw them. */
5518 void
5519 x_scroll_bar_clear (f)
5520 FRAME_PTR f;
5522 #ifndef USE_TOOLKIT_SCROLL_BARS
5523 Lisp_Object bar;
5525 /* We can have scroll bars even if this is 0,
5526 if we just turned off scroll bar mode.
5527 But in that case we should not clear them. */
5528 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5529 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5530 bar = XSCROLL_BAR (bar)->next)
5531 XClearArea (FRAME_X_DISPLAY (f),
5532 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
5533 0, 0, 0, 0, True);
5534 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5538 /* The main X event-reading loop - XTread_socket. */
5540 #if 0
5541 /* Time stamp of enter window event. This is only used by XTread_socket,
5542 but we have to put it out here, since static variables within functions
5543 sometimes don't work. */
5545 static Time enter_timestamp;
5546 #endif
5548 /* This holds the state XLookupString needs to implement dead keys
5549 and other tricks known as "compose processing". _X Window System_
5550 says that a portable program can't use this, but Stephen Gildea assures
5551 me that letting the compiler initialize it to zeros will work okay.
5553 This must be defined outside of XTread_socket, for the same reasons
5554 given for enter_timestamp, above. */
5556 static XComposeStatus compose_status;
5558 /* Record the last 100 characters stored
5559 to help debug the loss-of-chars-during-GC problem. */
5561 static int temp_index;
5562 static short temp_buffer[100];
5564 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5565 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5566 temp_index = 0; \
5567 temp_buffer[temp_index++] = (keysym)
5569 /* Set this to nonzero to fake an "X I/O error"
5570 on a particular display. */
5572 struct x_display_info *XTread_socket_fake_io_error;
5574 /* When we find no input here, we occasionally do a no-op command
5575 to verify that the X server is still running and we can still talk with it.
5576 We try all the open displays, one by one.
5577 This variable is used for cycling thru the displays. */
5579 static struct x_display_info *next_noop_dpyinfo;
5581 #define SET_SAVED_MENU_EVENT(size) \
5582 do \
5584 if (f->output_data.x->saved_menu_event == 0) \
5585 f->output_data.x->saved_menu_event \
5586 = (XEvent *) xmalloc (sizeof (XEvent)); \
5587 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5588 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5589 XSETFRAME (inev.ie.frame_or_window, f); \
5591 while (0)
5593 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5594 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5597 enum
5599 X_EVENT_NORMAL,
5600 X_EVENT_GOTO_OUT,
5601 X_EVENT_DROP
5604 /* Filter events for the current X input method.
5605 DPYINFO is the display this event is for.
5606 EVENT is the X event to filter.
5608 Returns non-zero if the event was filtered, caller shall not process
5609 this event further.
5610 Returns zero if event is wasn't filtered. */
5612 #ifdef HAVE_X_I18N
5613 static int
5614 x_filter_event (dpyinfo, event)
5615 struct x_display_info *dpyinfo;
5616 XEvent *event;
5618 /* XFilterEvent returns non-zero if the input method has
5619 consumed the event. We pass the frame's X window to
5620 XFilterEvent because that's the one for which the IC
5621 was created. */
5623 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5624 event->xclient.window);
5626 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5628 #endif
5630 #ifdef USE_GTK
5631 static int current_count;
5632 static int current_finish;
5633 static struct input_event *current_hold_quit;
5635 /* This is the filter function invoked by the GTK event loop.
5636 It is invoked before the XEvent is translated to a GdkEvent,
5637 so we have a chance to act on the event before GTK. */
5638 static GdkFilterReturn
5639 event_handler_gdk (gxev, ev, data)
5640 GdkXEvent *gxev;
5641 GdkEvent *ev;
5642 gpointer data;
5644 XEvent *xev = (XEvent *) gxev;
5646 if (current_count >= 0)
5648 struct x_display_info *dpyinfo;
5650 dpyinfo = x_display_info_for_display (xev->xany.display);
5652 #ifdef HAVE_X_I18N
5653 /* Filter events for the current X input method.
5654 GTK calls XFilterEvent but not for key press and release,
5655 so we do it here. */
5656 if (xev->type == KeyPress || xev->type == KeyRelease)
5657 if (dpyinfo && x_filter_event (dpyinfo, xev))
5658 return GDK_FILTER_REMOVE;
5659 #endif
5661 if (! dpyinfo)
5662 current_finish = X_EVENT_NORMAL;
5663 else
5665 current_count +=
5666 handle_one_xevent (dpyinfo, xev, &current_finish,
5667 current_hold_quit);
5670 else
5671 current_finish = x_dispatch_event (xev, xev->xany.display);
5673 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5674 return GDK_FILTER_REMOVE;
5676 return GDK_FILTER_CONTINUE;
5678 #endif /* USE_GTK */
5681 /* Handles the XEvent EVENT on display DPYINFO.
5683 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5684 *FINISH is zero if caller should continue reading events.
5685 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5687 We return the number of characters stored into the buffer. */
5689 static int
5690 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5691 struct x_display_info *dpyinfo;
5692 XEvent *eventp;
5693 int *finish;
5694 struct input_event *hold_quit;
5696 union {
5697 struct input_event ie;
5698 struct selection_input_event sie;
5699 } inev;
5700 int count = 0;
5701 int do_help = 0;
5702 int nbytes = 0;
5703 struct frame *f;
5704 struct coding_system coding;
5705 XEvent event = *eventp;
5707 *finish = X_EVENT_NORMAL;
5709 EVENT_INIT (inev.ie);
5710 inev.ie.kind = NO_EVENT;
5711 inev.ie.arg = Qnil;
5713 switch (event.type)
5715 case ClientMessage:
5717 if (event.xclient.message_type
5718 == dpyinfo->Xatom_wm_protocols
5719 && event.xclient.format == 32)
5721 if (event.xclient.data.l[0]
5722 == dpyinfo->Xatom_wm_take_focus)
5724 /* Use x_any_window_to_frame because this
5725 could be the shell widget window
5726 if the frame has no title bar. */
5727 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5728 #ifdef HAVE_X_I18N
5729 /* Not quite sure this is needed -pd */
5730 if (f && FRAME_XIC (f))
5731 XSetICFocus (FRAME_XIC (f));
5732 #endif
5733 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5734 instructs the WM to set the input focus automatically for
5735 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5736 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5737 it has set the focus. So, XSetInputFocus below is not
5738 needed.
5740 The call to XSetInputFocus below has also caused trouble. In
5741 cases where the XSetInputFocus done by the WM and the one
5742 below are temporally close (on a fast machine), the call
5743 below can generate additional FocusIn events which confuse
5744 Emacs. */
5746 /* Since we set WM_TAKE_FOCUS, we must call
5747 XSetInputFocus explicitly. But not if f is null,
5748 since that might be an event for a deleted frame. */
5749 if (f)
5751 Display *d = event.xclient.display;
5752 /* Catch and ignore errors, in case window has been
5753 iconified by a window manager such as GWM. */
5754 x_catch_errors (d);
5755 XSetInputFocus (d, event.xclient.window,
5756 /* The ICCCM says this is
5757 the only valid choice. */
5758 RevertToParent,
5759 event.xclient.data.l[1]);
5760 /* This is needed to detect the error
5761 if there is an error. */
5762 XSync (d, False);
5763 x_uncatch_errors ();
5765 /* Not certain about handling scroll bars here */
5766 #endif /* 0 */
5767 goto done;
5770 if (event.xclient.data.l[0]
5771 == dpyinfo->Xatom_wm_save_yourself)
5773 /* Save state modify the WM_COMMAND property to
5774 something which can reinstate us. This notifies
5775 the session manager, who's looking for such a
5776 PropertyNotify. Can restart processing when
5777 a keyboard or mouse event arrives. */
5778 /* If we have a session manager, don't set this.
5779 KDE will then start two Emacsen, one for the
5780 session manager and one for this. */
5781 #ifdef HAVE_X_SM
5782 if (! x_session_have_connection ())
5783 #endif
5785 f = x_top_window_to_frame (dpyinfo,
5786 event.xclient.window);
5787 /* This is just so we only give real data once
5788 for a single Emacs process. */
5789 if (f == SELECTED_FRAME ())
5790 XSetCommand (FRAME_X_DISPLAY (f),
5791 event.xclient.window,
5792 initial_argv, initial_argc);
5793 else if (f)
5794 XSetCommand (FRAME_X_DISPLAY (f),
5795 event.xclient.window,
5796 0, 0);
5798 goto done;
5801 if (event.xclient.data.l[0]
5802 == dpyinfo->Xatom_wm_delete_window)
5804 f = x_any_window_to_frame (dpyinfo,
5805 event.xclient.window);
5806 if (!f)
5807 goto OTHER; /* May be a dialog that is to be removed */
5809 inev.ie.kind = DELETE_WINDOW_EVENT;
5810 XSETFRAME (inev.ie.frame_or_window, f);
5811 goto done;
5814 goto done;
5817 if (event.xclient.message_type
5818 == dpyinfo->Xatom_wm_configure_denied)
5820 goto done;
5823 if (event.xclient.message_type
5824 == dpyinfo->Xatom_wm_window_moved)
5826 int new_x, new_y;
5827 f = x_window_to_frame (dpyinfo, event.xclient.window);
5829 new_x = event.xclient.data.s[0];
5830 new_y = event.xclient.data.s[1];
5832 if (f)
5834 f->left_pos = new_x;
5835 f->top_pos = new_y;
5837 goto done;
5840 #ifdef HACK_EDITRES
5841 if (event.xclient.message_type
5842 == dpyinfo->Xatom_editres)
5844 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5845 if (f)
5846 _XEditResCheckMessages (f->output_data.x->widget, NULL,
5847 &event, NULL);
5848 goto done;
5850 #endif /* HACK_EDITRES */
5852 if ((event.xclient.message_type
5853 == dpyinfo->Xatom_DONE)
5854 || (event.xclient.message_type
5855 == dpyinfo->Xatom_PAGE))
5857 /* Ghostview job completed. Kill it. We could
5858 reply with "Next" if we received "Page", but we
5859 currently never do because we are interested in
5860 images, only, which should have 1 page. */
5861 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
5862 f = x_window_to_frame (dpyinfo, event.xclient.window);
5863 if (!f)
5864 goto OTHER;
5865 x_kill_gs_process (pixmap, f);
5866 expose_frame (f, 0, 0, 0, 0);
5867 goto done;
5870 #ifdef USE_TOOLKIT_SCROLL_BARS
5871 /* Scroll bar callbacks send a ClientMessage from which
5872 we construct an input_event. */
5873 if (event.xclient.message_type
5874 == dpyinfo->Xatom_Scrollbar)
5876 x_scroll_bar_to_input_event (&event, &inev.ie);
5877 *finish = X_EVENT_GOTO_OUT;
5878 goto done;
5880 #endif /* USE_TOOLKIT_SCROLL_BARS */
5882 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5883 if (!f)
5884 goto OTHER;
5885 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
5886 *finish = X_EVENT_DROP;
5888 break;
5890 case SelectionNotify:
5891 last_user_time = event.xselection.time;
5892 #ifdef USE_X_TOOLKIT
5893 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
5894 goto OTHER;
5895 #endif /* not USE_X_TOOLKIT */
5896 x_handle_selection_notify (&event.xselection);
5897 break;
5899 case SelectionClear: /* Someone has grabbed ownership. */
5900 last_user_time = event.xselectionclear.time;
5901 #ifdef USE_X_TOOLKIT
5902 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
5903 goto OTHER;
5904 #endif /* USE_X_TOOLKIT */
5906 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
5908 inev.ie.kind = SELECTION_CLEAR_EVENT;
5909 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5910 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5911 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5912 inev.ie.frame_or_window = Qnil;
5914 break;
5916 case SelectionRequest: /* Someone wants our selection. */
5917 last_user_time = event.xselectionrequest.time;
5918 #ifdef USE_X_TOOLKIT
5919 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
5920 goto OTHER;
5921 #endif /* USE_X_TOOLKIT */
5923 XSelectionRequestEvent *eventp
5924 = (XSelectionRequestEvent *) &event;
5926 inev.ie.kind = SELECTION_REQUEST_EVENT;
5927 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
5928 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
5929 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
5930 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
5931 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
5932 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
5933 inev.ie.frame_or_window = Qnil;
5935 break;
5937 case PropertyNotify:
5938 last_user_time = event.xproperty.time;
5939 #if 0 /* This is plain wrong. In the case that we are waiting for a
5940 PropertyNotify used as an ACK in incremental selection
5941 transfer, the property will be on the receiver's window. */
5942 #if defined USE_X_TOOLKIT
5943 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
5944 goto OTHER;
5945 #endif
5946 #endif
5947 x_handle_property_notify (&event.xproperty);
5948 goto OTHER;
5950 case ReparentNotify:
5951 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
5952 if (f)
5954 int x, y;
5955 f->output_data.x->parent_desc = event.xreparent.parent;
5956 x_real_positions (f, &x, &y);
5957 f->left_pos = x;
5958 f->top_pos = y;
5960 /* Perhaps reparented due to a WM restart. Reset this. */
5961 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
5962 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
5964 goto OTHER;
5966 case Expose:
5967 f = x_window_to_frame (dpyinfo, event.xexpose.window);
5968 if (f)
5970 x_check_fullscreen (f);
5972 #ifdef USE_GTK
5973 /* This seems to be needed for GTK 2.6. */
5974 x_clear_area (event.xexpose.display,
5975 event.xexpose.window,
5976 event.xexpose.x, event.xexpose.y,
5977 event.xexpose.width, event.xexpose.height,
5978 FALSE);
5979 #endif
5980 if (f->async_visible == 0)
5982 f->async_visible = 1;
5983 f->async_iconified = 0;
5984 f->output_data.x->has_been_visible = 1;
5985 SET_FRAME_GARBAGED (f);
5987 else
5988 expose_frame (f,
5989 event.xexpose.x, event.xexpose.y,
5990 event.xexpose.width, event.xexpose.height);
5992 else
5994 #ifndef USE_TOOLKIT_SCROLL_BARS
5995 struct scroll_bar *bar;
5996 #endif
5997 #if defined USE_LUCID
5998 /* Submenus of the Lucid menu bar aren't widgets
5999 themselves, so there's no way to dispatch events
6000 to them. Recognize this case separately. */
6002 Widget widget
6003 = x_window_to_menu_bar (event.xexpose.window);
6004 if (widget)
6005 xlwmenu_redisplay (widget);
6007 #endif /* USE_LUCID */
6009 #ifdef USE_TOOLKIT_SCROLL_BARS
6010 /* Dispatch event to the widget. */
6011 goto OTHER;
6012 #else /* not USE_TOOLKIT_SCROLL_BARS */
6013 bar = x_window_to_scroll_bar (event.xexpose.display,
6014 event.xexpose.window);
6016 if (bar)
6017 x_scroll_bar_expose (bar, &event);
6018 #ifdef USE_X_TOOLKIT
6019 else
6020 goto OTHER;
6021 #endif /* USE_X_TOOLKIT */
6022 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6024 break;
6026 case GraphicsExpose: /* This occurs when an XCopyArea's
6027 source area was obscured or not
6028 available. */
6029 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6030 if (f)
6032 expose_frame (f,
6033 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6034 event.xgraphicsexpose.width,
6035 event.xgraphicsexpose.height);
6037 #ifdef USE_X_TOOLKIT
6038 else
6039 goto OTHER;
6040 #endif /* USE_X_TOOLKIT */
6041 break;
6043 case NoExpose: /* This occurs when an XCopyArea's
6044 source area was completely
6045 available. */
6046 break;
6048 case UnmapNotify:
6049 /* Redo the mouse-highlight after the tooltip has gone. */
6050 if (event.xmap.window == tip_window)
6052 tip_window = 0;
6053 redo_mouse_highlight ();
6056 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6057 if (f) /* F may no longer exist if
6058 the frame was deleted. */
6060 /* While a frame is unmapped, display generation is
6061 disabled; you don't want to spend time updating a
6062 display that won't ever be seen. */
6063 f->async_visible = 0;
6064 /* We can't distinguish, from the event, whether the window
6065 has become iconified or invisible. So assume, if it
6066 was previously visible, than now it is iconified.
6067 But x_make_frame_invisible clears both
6068 the visible flag and the iconified flag;
6069 and that way, we know the window is not iconified now. */
6070 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6072 f->async_iconified = 1;
6074 inev.ie.kind = ICONIFY_EVENT;
6075 XSETFRAME (inev.ie.frame_or_window, f);
6078 goto OTHER;
6080 case MapNotify:
6081 if (event.xmap.window == tip_window)
6082 /* The tooltip has been drawn already. Avoid
6083 the SET_FRAME_GARBAGED below. */
6084 goto OTHER;
6086 /* We use x_top_window_to_frame because map events can
6087 come for sub-windows and they don't mean that the
6088 frame is visible. */
6089 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6090 if (f)
6092 /* wait_reading_process_output will notice this and update
6093 the frame's display structures.
6094 If we where iconified, we should not set garbaged,
6095 because that stops redrawing on Expose events. This looks
6096 bad if we are called from a recursive event loop
6097 (x_dispatch_event), for example when a dialog is up. */
6098 if (! f->async_iconified)
6099 SET_FRAME_GARBAGED (f);
6101 f->async_visible = 1;
6102 f->async_iconified = 0;
6103 f->output_data.x->has_been_visible = 1;
6105 if (f->iconified)
6107 inev.ie.kind = DEICONIFY_EVENT;
6108 XSETFRAME (inev.ie.frame_or_window, f);
6110 else if (! NILP (Vframe_list)
6111 && ! NILP (XCDR (Vframe_list)))
6112 /* Force a redisplay sooner or later
6113 to update the frame titles
6114 in case this is the second frame. */
6115 record_asynch_buffer_change ();
6117 goto OTHER;
6119 case KeyPress:
6121 last_user_time = event.xkey.time;
6122 ignore_next_mouse_click_timeout = 0;
6124 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6125 /* Dispatch KeyPress events when in menu. */
6126 if (popup_activated ())
6127 goto OTHER;
6128 #endif
6130 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6132 /* If mouse-highlight is an integer, input clears out
6133 mouse highlighting. */
6134 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6135 && (f == 0
6136 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6138 clear_mouse_face (dpyinfo);
6139 dpyinfo->mouse_face_hidden = 1;
6142 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6143 if (f == 0)
6145 /* Scroll bars consume key events, but we want
6146 the keys to go to the scroll bar's frame. */
6147 Widget widget = XtWindowToWidget (dpyinfo->display,
6148 event.xkey.window);
6149 if (widget && XmIsScrollBar (widget))
6151 widget = XtParent (widget);
6152 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6155 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6157 if (f != 0)
6159 KeySym keysym, orig_keysym;
6160 /* al%imercury@uunet.uu.net says that making this 81
6161 instead of 80 fixed a bug whereby meta chars made
6162 his Emacs hang.
6164 It seems that some version of XmbLookupString has
6165 a bug of not returning XBufferOverflow in
6166 status_return even if the input is too long to
6167 fit in 81 bytes. So, we must prepare sufficient
6168 bytes for copy_buffer. 513 bytes (256 chars for
6169 two-byte character set) seems to be a fairly good
6170 approximation. -- 2000.8.10 handa@etl.go.jp */
6171 unsigned char copy_buffer[513];
6172 unsigned char *copy_bufptr = copy_buffer;
6173 int copy_bufsiz = sizeof (copy_buffer);
6174 int modifiers;
6175 Lisp_Object coding_system = Qlatin_1;
6176 Lisp_Object c;
6178 #ifdef USE_GTK
6179 /* Don't pass keys to GTK. A Tab will shift focus to the
6180 tool bar in GTK 2.4. Keys will still go to menus and
6181 dialogs because in that case popup_activated is TRUE
6182 (see above). */
6183 *finish = X_EVENT_DROP;
6184 #endif
6186 event.xkey.state
6187 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6188 extra_keyboard_modifiers);
6189 modifiers = event.xkey.state;
6191 /* This will have to go some day... */
6193 /* make_lispy_event turns chars into control chars.
6194 Don't do it here because XLookupString is too eager. */
6195 event.xkey.state &= ~ControlMask;
6196 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6197 | dpyinfo->super_mod_mask
6198 | dpyinfo->hyper_mod_mask
6199 | dpyinfo->alt_mod_mask);
6201 /* In case Meta is ComposeCharacter,
6202 clear its status. According to Markus Ehrnsperger
6203 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6204 this enables ComposeCharacter to work whether or
6205 not it is combined with Meta. */
6206 if (modifiers & dpyinfo->meta_mod_mask)
6207 bzero (&compose_status, sizeof (compose_status));
6209 #ifdef HAVE_X_I18N
6210 if (FRAME_XIC (f))
6212 Status status_return;
6214 coding_system = Vlocale_coding_system;
6215 nbytes = XmbLookupString (FRAME_XIC (f),
6216 &event.xkey, copy_bufptr,
6217 copy_bufsiz, &keysym,
6218 &status_return);
6219 if (status_return == XBufferOverflow)
6221 copy_bufsiz = nbytes + 1;
6222 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6223 nbytes = XmbLookupString (FRAME_XIC (f),
6224 &event.xkey, copy_bufptr,
6225 copy_bufsiz, &keysym,
6226 &status_return);
6228 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6229 #if 0 && defined X_HAVE_UTF8_STRING
6230 else if (status_return == XLookupKeySym)
6231 { /* Try again but with utf-8. */
6232 coding_system = Qutf_8;
6233 nbytes = Xutf8LookupString (FRAME_XIC (f),
6234 &event.xkey, copy_bufptr,
6235 copy_bufsiz, &keysym,
6236 &status_return);
6237 if (status_return == XBufferOverflow)
6239 copy_bufsiz = nbytes + 1;
6240 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6241 nbytes = Xutf8LookupString (FRAME_XIC (f),
6242 &event.xkey,
6243 copy_bufptr,
6244 copy_bufsiz, &keysym,
6245 &status_return);
6248 #endif
6250 if (status_return == XLookupNone)
6251 break;
6252 else if (status_return == XLookupChars)
6254 keysym = NoSymbol;
6255 modifiers = 0;
6257 else if (status_return != XLookupKeySym
6258 && status_return != XLookupBoth)
6259 abort ();
6261 else
6262 nbytes = XLookupString (&event.xkey, copy_bufptr,
6263 copy_bufsiz, &keysym,
6264 &compose_status);
6265 #else
6266 nbytes = XLookupString (&event.xkey, copy_bufptr,
6267 copy_bufsiz, &keysym,
6268 &compose_status);
6269 #endif
6271 /* If not using XIM/XIC, and a compose sequence is in progress,
6272 we break here. Otherwise, chars_matched is always 0. */
6273 if (compose_status.chars_matched > 0 && nbytes == 0)
6274 break;
6276 bzero (&compose_status, sizeof (compose_status));
6277 orig_keysym = keysym;
6279 /* Common for all keysym input events. */
6280 XSETFRAME (inev.ie.frame_or_window, f);
6281 inev.ie.modifiers
6282 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6283 inev.ie.timestamp = event.xkey.time;
6285 /* First deal with keysyms which have defined
6286 translations to characters. */
6287 if (keysym >= 32 && keysym < 128)
6288 /* Avoid explicitly decoding each ASCII character. */
6290 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6291 inev.ie.code = keysym;
6292 goto done_keysym;
6295 /* Keysyms directly mapped to supported Unicode characters. */
6296 if ((keysym >= 0x01000000 && keysym <= 0x010033ff)
6297 || (keysym >= 0x0100e000 && keysym <= 0x0100ffff))
6299 int code = keysym & 0xFFFF, charset_id, c1, c2;
6301 if (code < 0x80)
6303 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6304 inev.ie.code = code;
6306 else if (code < 0x100)
6308 if (code < 0xA0)
6309 charset_id = CHARSET_8_BIT_CONTROL;
6310 else
6311 charset_id = charset_latin_iso8859_1;
6312 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6313 inev.ie.code = MAKE_CHAR (charset_id, code, 0);
6315 else
6317 if (code < 0x2500)
6318 charset_id = charset_mule_unicode_0100_24ff,
6319 code -= 0x100;
6320 else if (code < 0xE000)
6321 charset_id = charset_mule_unicode_2500_33ff,
6322 code -= 0x2500;
6323 else
6324 charset_id = charset_mule_unicode_e000_ffff,
6325 code -= 0xE000;
6326 c1 = (code / 96) + 32, c2 = (code % 96) + 32;
6327 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6328 inev.ie.code = MAKE_CHAR (charset_id, c1, c2);
6330 goto done_keysym;
6333 /* Now non-ASCII. */
6334 if (HASH_TABLE_P (Vx_keysym_table)
6335 && (NATNUMP (c = Fgethash (make_number (keysym),
6336 Vx_keysym_table,
6337 Qnil))))
6339 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6340 ? ASCII_KEYSTROKE_EVENT
6341 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6342 inev.ie.code = XFASTINT (c);
6343 goto done_keysym;
6346 /* Random non-modifier sorts of keysyms. */
6347 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6348 || keysym == XK_Delete
6349 #ifdef XK_ISO_Left_Tab
6350 || (keysym >= XK_ISO_Left_Tab
6351 && keysym <= XK_ISO_Enter)
6352 #endif
6353 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6354 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6355 #ifdef HPUX
6356 /* This recognizes the "extended function
6357 keys". It seems there's no cleaner way.
6358 Test IsModifierKey to avoid handling
6359 mode_switch incorrectly. */
6360 || ((unsigned) (keysym) >= XK_Select
6361 && (unsigned)(keysym) < XK_KP_Space)
6362 #endif
6363 #ifdef XK_dead_circumflex
6364 || orig_keysym == XK_dead_circumflex
6365 #endif
6366 #ifdef XK_dead_grave
6367 || orig_keysym == XK_dead_grave
6368 #endif
6369 #ifdef XK_dead_tilde
6370 || orig_keysym == XK_dead_tilde
6371 #endif
6372 #ifdef XK_dead_diaeresis
6373 || orig_keysym == XK_dead_diaeresis
6374 #endif
6375 #ifdef XK_dead_macron
6376 || orig_keysym == XK_dead_macron
6377 #endif
6378 #ifdef XK_dead_degree
6379 || orig_keysym == XK_dead_degree
6380 #endif
6381 #ifdef XK_dead_acute
6382 || orig_keysym == XK_dead_acute
6383 #endif
6384 #ifdef XK_dead_cedilla
6385 || orig_keysym == XK_dead_cedilla
6386 #endif
6387 #ifdef XK_dead_breve
6388 || orig_keysym == XK_dead_breve
6389 #endif
6390 #ifdef XK_dead_ogonek
6391 || orig_keysym == XK_dead_ogonek
6392 #endif
6393 #ifdef XK_dead_caron
6394 || orig_keysym == XK_dead_caron
6395 #endif
6396 #ifdef XK_dead_doubleacute
6397 || orig_keysym == XK_dead_doubleacute
6398 #endif
6399 #ifdef XK_dead_abovedot
6400 || orig_keysym == XK_dead_abovedot
6401 #endif
6402 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6403 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6404 /* Any "vendor-specific" key is ok. */
6405 || (orig_keysym & (1 << 28))
6406 || (keysym != NoSymbol && nbytes == 0))
6407 && ! (IsModifierKey (orig_keysym)
6408 #ifndef HAVE_X11R5
6409 #ifdef XK_Mode_switch
6410 || ((unsigned)(orig_keysym) == XK_Mode_switch)
6411 #endif
6412 #ifdef XK_Num_Lock
6413 || ((unsigned)(orig_keysym) == XK_Num_Lock)
6414 #endif
6415 #endif /* not HAVE_X11R5 */
6416 /* The symbols from XK_ISO_Lock
6417 to XK_ISO_Last_Group_Lock
6418 don't have real modifiers but
6419 should be treated similarly to
6420 Mode_switch by Emacs. */
6421 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6422 || ((unsigned)(orig_keysym)
6423 >= XK_ISO_Lock
6424 && (unsigned)(orig_keysym)
6425 <= XK_ISO_Last_Group_Lock)
6426 #endif
6429 STORE_KEYSYM_FOR_DEBUG (keysym);
6430 /* make_lispy_event will convert this to a symbolic
6431 key. */
6432 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6433 inev.ie.code = keysym;
6434 goto done_keysym;
6437 { /* Raw bytes, not keysym. */
6438 register int i;
6439 register int c;
6440 int nchars, len;
6442 /* The input should be decoded with `coding_system'
6443 which depends on which X*LookupString function
6444 we used just above and the locale. */
6445 setup_coding_system (coding_system, &coding);
6446 coding.src_multibyte = 0;
6447 coding.dst_multibyte = 1;
6448 /* The input is converted to events, thus we can't
6449 handle composition. Anyway, there's no XIM that
6450 gives us composition information. */
6451 coding.composing = COMPOSITION_DISABLED;
6453 for (i = 0; i < nbytes; i++)
6455 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6459 /* Decode the input data. */
6460 int require;
6461 unsigned char *p;
6463 require = decoding_buffer_size (&coding, nbytes);
6464 p = (unsigned char *) alloca (require);
6465 coding.mode |= CODING_MODE_LAST_BLOCK;
6466 /* We explicitly disable composition handling because
6467 key data should not contain any composition sequence. */
6468 coding.composing = COMPOSITION_DISABLED;
6469 decode_coding (&coding, copy_bufptr, p, nbytes, require);
6470 nbytes = coding.produced;
6471 nchars = coding.produced_char;
6472 copy_bufptr = p;
6475 /* Convert the input data to a sequence of
6476 character events. */
6477 for (i = 0; i < nbytes; i += len)
6479 if (nchars == nbytes)
6480 c = copy_bufptr[i], len = 1;
6481 else
6482 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6483 nbytes - i, len);
6484 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6485 ? ASCII_KEYSTROKE_EVENT
6486 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6487 inev.ie.code = c;
6488 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6491 count += nchars;
6493 inev.ie.kind = NO_EVENT; /* Already stored above. */
6495 if (keysym == NoSymbol)
6496 break;
6499 done_keysym:
6500 #ifdef HAVE_X_I18N
6501 /* Don't dispatch this event since XtDispatchEvent calls
6502 XFilterEvent, and two calls in a row may freeze the
6503 client. */
6504 break;
6505 #else
6506 goto OTHER;
6507 #endif
6509 case KeyRelease:
6510 last_user_time = event.xkey.time;
6511 #ifdef HAVE_X_I18N
6512 /* Don't dispatch this event since XtDispatchEvent calls
6513 XFilterEvent, and two calls in a row may freeze the
6514 client. */
6515 break;
6516 #else
6517 goto OTHER;
6518 #endif
6520 case EnterNotify:
6521 last_user_time = event.xcrossing.time;
6522 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6524 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6526 if (f && x_mouse_click_focus_ignore_position)
6527 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6529 #if 0
6530 if (event.xcrossing.focus)
6532 /* Avoid nasty pop/raise loops. */
6533 if (f && (!(f->auto_raise)
6534 || !(f->auto_lower)
6535 || (event.xcrossing.time - enter_timestamp) > 500))
6537 x_new_focus_frame (dpyinfo, f);
6538 enter_timestamp = event.xcrossing.time;
6541 else if (f == dpyinfo->x_focus_frame)
6542 x_new_focus_frame (dpyinfo, 0);
6543 #endif
6545 /* EnterNotify counts as mouse movement,
6546 so update things that depend on mouse position. */
6547 if (f && !f->output_data.x->hourglass_p)
6548 note_mouse_movement (f, &event.xmotion);
6549 #ifdef USE_GTK
6550 /* We may get an EnterNotify on the buttons in the toolbar. In that
6551 case we moved out of any highlighted area and need to note this. */
6552 if (!f && last_mouse_glyph_frame)
6553 note_mouse_movement (last_mouse_glyph_frame, &event);
6554 #endif
6555 goto OTHER;
6557 case FocusIn:
6558 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6559 goto OTHER;
6561 case LeaveNotify:
6562 last_user_time = event.xcrossing.time;
6563 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6565 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6566 if (f)
6568 if (f == dpyinfo->mouse_face_mouse_frame)
6570 /* If we move outside the frame, then we're
6571 certainly no longer on any text in the frame. */
6572 clear_mouse_face (dpyinfo);
6573 dpyinfo->mouse_face_mouse_frame = 0;
6576 /* Generate a nil HELP_EVENT to cancel a help-echo.
6577 Do it only if there's something to cancel.
6578 Otherwise, the startup message is cleared when
6579 the mouse leaves the frame. */
6580 if (any_help_event_p)
6581 do_help = -1;
6583 #ifdef USE_GTK
6584 /* See comment in EnterNotify above */
6585 else if (last_mouse_glyph_frame)
6586 note_mouse_movement (last_mouse_glyph_frame, &event);
6587 #endif
6588 goto OTHER;
6590 case FocusOut:
6591 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6592 goto OTHER;
6594 case MotionNotify:
6596 last_user_time = event.xmotion.time;
6597 previous_help_echo_string = help_echo_string;
6598 help_echo_string = Qnil;
6600 if (dpyinfo->grabbed && last_mouse_frame
6601 && FRAME_LIVE_P (last_mouse_frame))
6602 f = last_mouse_frame;
6603 else
6604 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6606 if (dpyinfo->mouse_face_hidden)
6608 dpyinfo->mouse_face_hidden = 0;
6609 clear_mouse_face (dpyinfo);
6612 if (f)
6615 /* Generate SELECT_WINDOW_EVENTs when needed. */
6616 if (!NILP (Vmouse_autoselect_window))
6618 Lisp_Object window;
6620 window = window_from_coordinates (f,
6621 event.xmotion.x, event.xmotion.y,
6622 0, 0, 0, 0);
6624 /* Window will be selected only when it is not selected now and
6625 last mouse movement event was not in it. Minibuffer window
6626 will be selected only when it is active. */
6627 if (WINDOWP (window)
6628 && !EQ (window, last_window)
6629 && !EQ (window, selected_window)
6630 /* For click-to-focus window managers
6631 create event iff we don't leave the
6632 selected frame. */
6633 && (focus_follows_mouse
6634 || (EQ (XWINDOW (window)->frame,
6635 XWINDOW (selected_window)->frame))))
6637 inev.ie.kind = SELECT_WINDOW_EVENT;
6638 inev.ie.frame_or_window = window;
6641 last_window=window;
6643 if (!note_mouse_movement (f, &event.xmotion))
6644 help_echo_string = previous_help_echo_string;
6646 else
6648 #ifndef USE_TOOLKIT_SCROLL_BARS
6649 struct scroll_bar *bar
6650 = x_window_to_scroll_bar (event.xmotion.display,
6651 event.xmotion.window);
6653 if (bar)
6654 x_scroll_bar_note_movement (bar, &event);
6655 #endif /* USE_TOOLKIT_SCROLL_BARS */
6657 /* If we move outside the frame, then we're
6658 certainly no longer on any text in the frame. */
6659 clear_mouse_face (dpyinfo);
6662 /* If the contents of the global variable help_echo_string
6663 has changed, generate a HELP_EVENT. */
6664 if (!NILP (help_echo_string)
6665 || !NILP (previous_help_echo_string))
6666 do_help = 1;
6667 goto OTHER;
6670 case ConfigureNotify:
6671 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6672 if (f)
6674 #ifndef USE_X_TOOLKIT
6675 #ifdef USE_GTK
6676 xg_resize_widgets (f, event.xconfigure.width,
6677 event.xconfigure.height);
6678 #else /* not USE_GTK */
6679 /* If there is a pending resize for fullscreen, don't
6680 do this one, the right one will come later.
6681 The toolkit version doesn't seem to need this, but we
6682 need to reset it below. */
6683 int dont_resize
6684 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6685 && f->new_text_cols != 0);
6686 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6687 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6689 if (dont_resize)
6690 goto OTHER;
6692 /* In the toolkit version, change_frame_size
6693 is called by the code that handles resizing
6694 of the EmacsFrame widget. */
6696 /* Even if the number of character rows and columns has
6697 not changed, the font size may have changed, so we need
6698 to check the pixel dimensions as well. */
6699 if (columns != FRAME_COLS (f)
6700 || rows != FRAME_LINES (f)
6701 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6702 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6704 change_frame_size (f, rows, columns, 0, 1, 0);
6705 SET_FRAME_GARBAGED (f);
6706 cancel_mouse_face (f);
6708 #endif /* not USE_GTK */
6709 #endif
6711 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6712 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6714 #ifdef USE_GTK
6715 /* GTK creates windows but doesn't map them.
6716 Only get real positions and check fullscreen when mapped. */
6717 if (FRAME_GTK_OUTER_WIDGET (f)
6718 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6719 #endif
6721 x_real_positions (f, &f->left_pos, &f->top_pos);
6723 if (f->want_fullscreen & FULLSCREEN_WAIT)
6724 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6727 #ifdef HAVE_X_I18N
6728 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6729 xic_set_statusarea (f);
6730 #endif
6732 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6734 /* Since the WM decorations come below top_pos now,
6735 we must put them below top_pos in the future. */
6736 f->win_gravity = NorthWestGravity;
6737 x_wm_set_size_hint (f, (long) 0, 0);
6740 goto OTHER;
6742 case ButtonRelease:
6743 case ButtonPress:
6745 /* If we decide we want to generate an event to be seen
6746 by the rest of Emacs, we put it here. */
6747 int tool_bar_p = 0;
6749 bzero (&compose_status, sizeof (compose_status));
6750 last_mouse_glyph_frame = 0;
6751 last_user_time = event.xbutton.time;
6753 if (dpyinfo->grabbed
6754 && last_mouse_frame
6755 && FRAME_LIVE_P (last_mouse_frame))
6756 f = last_mouse_frame;
6757 else
6758 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6760 if (f)
6762 /* Is this in the tool-bar? */
6763 if (WINDOWP (f->tool_bar_window)
6764 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6766 Lisp_Object window;
6767 int x = event.xbutton.x;
6768 int y = event.xbutton.y;
6770 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6771 tool_bar_p = EQ (window, f->tool_bar_window);
6773 if (tool_bar_p && event.xbutton.button < 4)
6775 if (event.xbutton.type == ButtonPress)
6776 handle_tool_bar_click (f, x, y, 1, 0);
6777 else
6778 handle_tool_bar_click (f, x, y, 0,
6779 x_x_to_emacs_modifiers (dpyinfo,
6780 event.xbutton.state));
6784 if (!tool_bar_p)
6785 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6786 if (! popup_activated ())
6787 #endif
6789 if (ignore_next_mouse_click_timeout)
6791 if (event.type == ButtonPress
6792 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6794 ignore_next_mouse_click_timeout = 0;
6795 construct_mouse_click (&inev.ie, &event.xbutton, f);
6797 if (event.type == ButtonRelease)
6798 ignore_next_mouse_click_timeout = 0;
6800 else
6801 construct_mouse_click (&inev.ie, &event.xbutton, f);
6804 else
6806 struct scroll_bar *bar
6807 = x_window_to_scroll_bar (event.xbutton.display,
6808 event.xbutton.window);
6810 #ifdef USE_TOOLKIT_SCROLL_BARS
6811 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6812 scroll bars. */
6813 if (bar && event.xbutton.state & ControlMask)
6815 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6816 *finish = X_EVENT_DROP;
6818 #else /* not USE_TOOLKIT_SCROLL_BARS */
6819 if (bar)
6820 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6821 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6824 if (event.type == ButtonPress)
6826 dpyinfo->grabbed |= (1 << event.xbutton.button);
6827 last_mouse_frame = f;
6829 if (!tool_bar_p)
6830 last_tool_bar_item = -1;
6832 else
6833 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6835 /* Ignore any mouse motion that happened before this event;
6836 any subsequent mouse-movement Emacs events should reflect
6837 only motion after the ButtonPress/Release. */
6838 if (f != 0)
6839 f->mouse_moved = 0;
6841 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6842 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6843 /* For a down-event in the menu bar,
6844 don't pass it to Xt right now.
6845 Instead, save it away
6846 and we will pass it to Xt from kbd_buffer_get_event.
6847 That way, we can run some Lisp code first. */
6848 if (
6849 #ifdef USE_GTK
6850 ! popup_activated ()
6851 /* Gtk+ menus only react to the first three buttons. */
6852 && event.xbutton.button < 3
6854 #endif
6855 f && event.type == ButtonPress
6856 /* Verify the event is really within the menu bar
6857 and not just sent to it due to grabbing. */
6858 && event.xbutton.x >= 0
6859 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6860 && event.xbutton.y >= 0
6861 && event.xbutton.y < f->output_data.x->menubar_height
6862 && event.xbutton.same_screen)
6864 SET_SAVED_BUTTON_EVENT;
6865 XSETFRAME (last_mouse_press_frame, f);
6866 #ifdef USE_GTK
6867 *finish = X_EVENT_DROP;
6868 #endif
6870 else if (event.type == ButtonPress)
6872 last_mouse_press_frame = Qnil;
6873 goto OTHER;
6876 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6877 but I am trying to be cautious. */
6878 else if (event.type == ButtonRelease)
6880 if (!NILP (last_mouse_press_frame))
6882 f = XFRAME (last_mouse_press_frame);
6883 if (f->output_data.x)
6884 SET_SAVED_BUTTON_EVENT;
6886 else
6887 goto OTHER;
6889 #endif /* USE_MOTIF */
6890 else
6891 goto OTHER;
6892 #endif /* USE_X_TOOLKIT || USE_GTK */
6894 break;
6896 case CirculateNotify:
6897 goto OTHER;
6899 case CirculateRequest:
6900 goto OTHER;
6902 case VisibilityNotify:
6903 goto OTHER;
6905 case MappingNotify:
6906 /* Someone has changed the keyboard mapping - update the
6907 local cache. */
6908 switch (event.xmapping.request)
6910 case MappingModifier:
6911 x_find_modifier_meanings (dpyinfo);
6912 /* This is meant to fall through. */
6913 case MappingKeyboard:
6914 XRefreshKeyboardMapping (&event.xmapping);
6916 goto OTHER;
6918 default:
6919 OTHER:
6920 #ifdef USE_X_TOOLKIT
6921 BLOCK_INPUT;
6922 if (*finish != X_EVENT_DROP)
6923 XtDispatchEvent (&event);
6924 UNBLOCK_INPUT;
6925 #endif /* USE_X_TOOLKIT */
6926 break;
6929 done:
6930 if (inev.ie.kind != NO_EVENT)
6932 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6933 count++;
6936 if (do_help
6937 && !(hold_quit && hold_quit->kind != NO_EVENT))
6939 Lisp_Object frame;
6941 if (f)
6942 XSETFRAME (frame, f);
6943 else
6944 frame = Qnil;
6946 if (do_help > 0)
6948 any_help_event_p = 1;
6949 gen_help_event (help_echo_string, frame, help_echo_window,
6950 help_echo_object, help_echo_pos);
6952 else
6954 help_echo_string = Qnil;
6955 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
6957 count++;
6960 *eventp = event;
6961 return count;
6965 /* Handles the XEvent EVENT on display DISPLAY.
6966 This is used for event loops outside the normal event handling,
6967 i.e. looping while a popup menu or a dialog is posted.
6969 Returns the value handle_one_xevent sets in the finish argument. */
6971 x_dispatch_event (event, display)
6972 XEvent *event;
6973 Display *display;
6975 struct x_display_info *dpyinfo;
6976 int finish = X_EVENT_NORMAL;
6978 dpyinfo = x_display_info_for_display (display);
6980 if (dpyinfo)
6981 handle_one_xevent (dpyinfo, event, &finish, 0);
6983 return finish;
6987 /* Read events coming from the X server.
6988 This routine is called by the SIGIO handler.
6989 We return as soon as there are no more events to be read.
6991 We return the number of characters stored into the buffer,
6992 thus pretending to be `read' (except the characters we store
6993 in the keyboard buffer can be multibyte, so are not necessarily
6994 C chars).
6996 EXPECTED is nonzero if the caller knows input is available. */
6998 static int
6999 XTread_socket (sd, expected, hold_quit)
7000 register int sd;
7001 int expected;
7002 struct input_event *hold_quit;
7004 int count = 0;
7005 XEvent event;
7006 int event_found = 0;
7007 struct x_display_info *dpyinfo;
7009 if (interrupt_input_blocked)
7011 interrupt_input_pending = 1;
7012 return -1;
7015 interrupt_input_pending = 0;
7016 BLOCK_INPUT;
7018 /* So people can tell when we have read the available input. */
7019 input_signal_count++;
7021 ++handling_signal;
7023 /* Find the display we are supposed to read input for.
7024 It's the one communicating on descriptor SD. */
7025 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7027 #if 0 /* This ought to be unnecessary; let's verify it. */
7028 #ifdef FIOSNBIO
7029 /* If available, Xlib uses FIOSNBIO to make the socket
7030 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7031 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7032 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7033 fcntl (dpyinfo->connection, F_SETFL, 0);
7034 #endif /* ! defined (FIOSNBIO) */
7035 #endif
7037 #if 0 /* This code can't be made to work, with multiple displays,
7038 and appears not to be used on any system any more.
7039 Also keyboard.c doesn't turn O_NDELAY on and off
7040 for X connections. */
7041 #ifndef SIGIO
7042 #ifndef HAVE_SELECT
7043 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7045 extern int read_alarm_should_throw;
7046 read_alarm_should_throw = 1;
7047 XPeekEvent (dpyinfo->display, &event);
7048 read_alarm_should_throw = 0;
7050 #endif /* HAVE_SELECT */
7051 #endif /* SIGIO */
7052 #endif
7054 /* For debugging, this gives a way to fake an I/O error. */
7055 if (dpyinfo == XTread_socket_fake_io_error)
7057 XTread_socket_fake_io_error = 0;
7058 x_io_error_quitter (dpyinfo->display);
7061 #ifdef HAVE_X_SM
7063 struct input_event inev;
7064 BLOCK_INPUT;
7065 /* We don't need to EVENT_INIT (inev) here, as
7066 x_session_check_input copies an entire input_event. */
7067 if (x_session_check_input (&inev))
7069 kbd_buffer_store_event_hold (&inev, hold_quit);
7070 count++;
7072 UNBLOCK_INPUT;
7074 #endif
7076 #ifndef USE_GTK
7077 while (XPending (dpyinfo->display))
7079 int finish;
7081 XNextEvent (dpyinfo->display, &event);
7083 #ifdef HAVE_X_I18N
7084 /* Filter events for the current X input method. */
7085 if (x_filter_event (dpyinfo, &event))
7086 break;
7087 #endif
7088 event_found = 1;
7090 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
7092 if (finish == X_EVENT_GOTO_OUT)
7093 goto out;
7095 #endif /* not USE_GTK */
7098 #ifdef USE_GTK
7100 /* For GTK we must use the GTK event loop. But XEvents gets passed
7101 to our filter function above, and then to the big event switch.
7102 We use a bunch of globals to communicate with our filter function,
7103 that is kind of ugly, but it works.
7105 There is no way to do one display at the time, GTK just does events
7106 from all displays. */
7108 while (gtk_events_pending ())
7110 current_count = count;
7111 current_hold_quit = hold_quit;
7113 gtk_main_iteration ();
7115 count = current_count;
7116 current_count = -1;
7117 current_hold_quit = 0;
7119 if (current_finish == X_EVENT_GOTO_OUT)
7120 break;
7122 #endif /* USE_GTK */
7124 out:;
7126 /* On some systems, an X bug causes Emacs to get no more events
7127 when the window is destroyed. Detect that. (1994.) */
7128 if (! event_found)
7130 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7131 One XNOOP in 100 loops will make Emacs terminate.
7132 B. Bretthauer, 1994 */
7133 x_noop_count++;
7134 if (x_noop_count >= 100)
7136 x_noop_count=0;
7138 if (next_noop_dpyinfo == 0)
7139 next_noop_dpyinfo = x_display_list;
7141 XNoOp (next_noop_dpyinfo->display);
7143 /* Each time we get here, cycle through the displays now open. */
7144 next_noop_dpyinfo = next_noop_dpyinfo->next;
7148 /* If the focus was just given to an auto-raising frame,
7149 raise it now. */
7150 /* ??? This ought to be able to handle more than one such frame. */
7151 if (pending_autoraise_frame)
7153 x_raise_frame (pending_autoraise_frame);
7154 pending_autoraise_frame = 0;
7157 --handling_signal;
7158 UNBLOCK_INPUT;
7160 return count;
7166 /***********************************************************************
7167 Text Cursor
7168 ***********************************************************************/
7170 /* Set clipping for output in glyph row ROW. W is the window in which
7171 we operate. GC is the graphics context to set clipping in.
7173 ROW may be a text row or, e.g., a mode line. Text rows must be
7174 clipped to the interior of the window dedicated to text display,
7175 mode lines must be clipped to the whole window. */
7177 static void
7178 x_clip_to_row (w, row, area, gc)
7179 struct window *w;
7180 struct glyph_row *row;
7181 int area;
7182 GC gc;
7184 struct frame *f = XFRAME (WINDOW_FRAME (w));
7185 XRectangle clip_rect;
7186 int window_x, window_y, window_width;
7188 window_box (w, area, &window_x, &window_y, &window_width, 0);
7190 clip_rect.x = window_x;
7191 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7192 clip_rect.y = max (clip_rect.y, window_y);
7193 clip_rect.width = window_width;
7194 clip_rect.height = row->visible_height;
7196 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7200 /* Draw a hollow box cursor on window W in glyph row ROW. */
7202 static void
7203 x_draw_hollow_cursor (w, row)
7204 struct window *w;
7205 struct glyph_row *row;
7207 struct frame *f = XFRAME (WINDOW_FRAME (w));
7208 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7209 Display *dpy = FRAME_X_DISPLAY (f);
7210 int x, y, wd, h;
7211 XGCValues xgcv;
7212 struct glyph *cursor_glyph;
7213 GC gc;
7215 /* Get the glyph the cursor is on. If we can't tell because
7216 the current matrix is invalid or such, give up. */
7217 cursor_glyph = get_phys_cursor_glyph (w);
7218 if (cursor_glyph == NULL)
7219 return;
7221 /* Compute frame-relative coordinates for phys cursor. */
7222 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7223 wd = w->phys_cursor_width;
7225 /* The foreground of cursor_gc is typically the same as the normal
7226 background color, which can cause the cursor box to be invisible. */
7227 xgcv.foreground = f->output_data.x->cursor_pixel;
7228 if (dpyinfo->scratch_cursor_gc)
7229 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7230 else
7231 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7232 GCForeground, &xgcv);
7233 gc = dpyinfo->scratch_cursor_gc;
7235 /* Set clipping, draw the rectangle, and reset clipping again. */
7236 x_clip_to_row (w, row, TEXT_AREA, gc);
7237 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7238 XSetClipMask (dpy, gc, None);
7242 /* Draw a bar cursor on window W in glyph row ROW.
7244 Implementation note: One would like to draw a bar cursor with an
7245 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7246 Unfortunately, I didn't find a font yet that has this property set.
7247 --gerd. */
7249 static void
7250 x_draw_bar_cursor (w, row, width, kind)
7251 struct window *w;
7252 struct glyph_row *row;
7253 int width;
7254 enum text_cursor_kinds kind;
7256 struct frame *f = XFRAME (w->frame);
7257 struct glyph *cursor_glyph;
7259 /* If cursor is out of bounds, don't draw garbage. This can happen
7260 in mini-buffer windows when switching between echo area glyphs
7261 and mini-buffer. */
7262 cursor_glyph = get_phys_cursor_glyph (w);
7263 if (cursor_glyph == NULL)
7264 return;
7266 /* If on an image, draw like a normal cursor. That's usually better
7267 visible than drawing a bar, esp. if the image is large so that
7268 the bar might not be in the window. */
7269 if (cursor_glyph->type == IMAGE_GLYPH)
7271 struct glyph_row *row;
7272 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7273 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7275 else
7277 Display *dpy = FRAME_X_DISPLAY (f);
7278 Window window = FRAME_X_WINDOW (f);
7279 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7280 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7281 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7282 XGCValues xgcv;
7284 /* If the glyph's background equals the color we normally draw
7285 the bar cursor in, the bar cursor in its normal color is
7286 invisible. Use the glyph's foreground color instead in this
7287 case, on the assumption that the glyph's colors are chosen so
7288 that the glyph is legible. */
7289 if (face->background == f->output_data.x->cursor_pixel)
7290 xgcv.background = xgcv.foreground = face->foreground;
7291 else
7292 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7293 xgcv.graphics_exposures = 0;
7295 if (gc)
7296 XChangeGC (dpy, gc, mask, &xgcv);
7297 else
7299 gc = XCreateGC (dpy, window, mask, &xgcv);
7300 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7303 if (width < 0)
7304 width = FRAME_CURSOR_WIDTH (f);
7305 width = min (cursor_glyph->pixel_width, width);
7307 w->phys_cursor_width = width;
7308 x_clip_to_row (w, row, TEXT_AREA, gc);
7310 if (kind == BAR_CURSOR)
7311 XFillRectangle (dpy, window, gc,
7312 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7313 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7314 width, row->height);
7315 else
7316 XFillRectangle (dpy, window, gc,
7317 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7318 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7319 row->height - width),
7320 cursor_glyph->pixel_width,
7321 width);
7323 XSetClipMask (dpy, gc, None);
7328 /* RIF: Define cursor CURSOR on frame F. */
7330 static void
7331 x_define_frame_cursor (f, cursor)
7332 struct frame *f;
7333 Cursor cursor;
7335 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7339 /* RIF: Clear area on frame F. */
7341 static void
7342 x_clear_frame_area (f, x, y, width, height)
7343 struct frame *f;
7344 int x, y, width, height;
7346 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7347 x, y, width, height, False);
7351 /* RIF: Draw cursor on window W. */
7353 static void
7354 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7355 struct window *w;
7356 struct glyph_row *glyph_row;
7357 int x, y;
7358 int cursor_type, cursor_width;
7359 int on_p, active_p;
7361 struct frame *f = XFRAME (WINDOW_FRAME (w));
7363 if (on_p)
7365 w->phys_cursor_type = cursor_type;
7366 w->phys_cursor_on_p = 1;
7368 if (glyph_row->exact_window_width_line_p
7369 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7371 glyph_row->cursor_in_fringe_p = 1;
7372 draw_fringe_bitmap (w, glyph_row, 0);
7374 else
7375 switch (cursor_type)
7377 case HOLLOW_BOX_CURSOR:
7378 x_draw_hollow_cursor (w, glyph_row);
7379 break;
7381 case FILLED_BOX_CURSOR:
7382 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7383 break;
7385 case BAR_CURSOR:
7386 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7387 break;
7389 case HBAR_CURSOR:
7390 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7391 break;
7393 case NO_CURSOR:
7394 w->phys_cursor_width = 0;
7395 break;
7397 default:
7398 abort ();
7401 #ifdef HAVE_X_I18N
7402 if (w == XWINDOW (f->selected_window))
7403 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7404 xic_set_preeditarea (w, x, y);
7405 #endif
7408 #ifndef XFlush
7409 if (updating_frame != f)
7410 XFlush (FRAME_X_DISPLAY (f));
7411 #endif
7415 /* Icons. */
7417 /* Make the x-window of frame F use the gnu icon bitmap. */
7420 x_bitmap_icon (f, file)
7421 struct frame *f;
7422 Lisp_Object file;
7424 int bitmap_id;
7426 if (FRAME_X_WINDOW (f) == 0)
7427 return 1;
7429 /* Free up our existing icon bitmap and mask if any. */
7430 if (f->output_data.x->icon_bitmap > 0)
7431 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7432 f->output_data.x->icon_bitmap = 0;
7434 if (STRINGP (file))
7436 #ifdef USE_GTK
7437 /* Use gtk_window_set_icon_from_file () if available,
7438 It's not restricted to bitmaps */
7439 if (xg_set_icon (f, file))
7440 return 0;
7441 #endif /* USE_GTK */
7442 bitmap_id = x_create_bitmap_from_file (f, file);
7443 x_create_bitmap_mask (f, bitmap_id);
7445 else
7447 /* Create the GNU bitmap and mask if necessary. */
7448 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7450 int rc = -1;
7452 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7453 #ifdef USE_GTK
7454 if (xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7455 return 0;
7456 #else
7457 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7458 if (rc != -1)
7459 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7460 #endif /* USE_GTK */
7461 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
7463 /* If all else fails, use the (black and white) xbm image. */
7464 if (rc == -1)
7466 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7467 gnu_xbm_width, gnu_xbm_height);
7468 if (rc == -1)
7469 return 1;
7471 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7472 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7476 /* The first time we create the GNU bitmap and mask,
7477 this increments the ref-count one extra time.
7478 As a result, the GNU bitmap and mask are never freed.
7479 That way, we don't have to worry about allocating it again. */
7480 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7482 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7485 x_wm_set_icon_pixmap (f, bitmap_id);
7486 f->output_data.x->icon_bitmap = bitmap_id;
7488 return 0;
7492 /* Make the x-window of frame F use a rectangle with text.
7493 Use ICON_NAME as the text. */
7496 x_text_icon (f, icon_name)
7497 struct frame *f;
7498 char *icon_name;
7500 if (FRAME_X_WINDOW (f) == 0)
7501 return 1;
7503 #ifdef HAVE_X11R4
7505 XTextProperty text;
7506 text.value = (unsigned char *) icon_name;
7507 text.encoding = XA_STRING;
7508 text.format = 8;
7509 text.nitems = strlen (icon_name);
7510 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7512 #else /* not HAVE_X11R4 */
7513 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
7514 #endif /* not HAVE_X11R4 */
7516 if (f->output_data.x->icon_bitmap > 0)
7517 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7518 f->output_data.x->icon_bitmap = 0;
7519 x_wm_set_icon_pixmap (f, 0);
7521 return 0;
7524 #define X_ERROR_MESSAGE_SIZE 200
7526 /* If non-nil, this should be a string.
7527 It means catch X errors and store the error message in this string.
7529 The reason we use a stack is that x_catch_error/x_uncatch_error can
7530 be called from a signal handler.
7533 struct x_error_message_stack {
7534 char string[X_ERROR_MESSAGE_SIZE];
7535 Display *dpy;
7536 struct x_error_message_stack *prev;
7538 static struct x_error_message_stack *x_error_message;
7540 /* An X error handler which stores the error message in
7541 *x_error_message. This is called from x_error_handler if
7542 x_catch_errors is in effect. */
7544 static void
7545 x_error_catcher (display, error)
7546 Display *display;
7547 XErrorEvent *error;
7549 XGetErrorText (display, error->error_code,
7550 x_error_message->string,
7551 X_ERROR_MESSAGE_SIZE);
7554 /* Begin trapping X errors for display DPY. Actually we trap X errors
7555 for all displays, but DPY should be the display you are actually
7556 operating on.
7558 After calling this function, X protocol errors no longer cause
7559 Emacs to exit; instead, they are recorded in the string
7560 stored in *x_error_message.
7562 Calling x_check_errors signals an Emacs error if an X error has
7563 occurred since the last call to x_catch_errors or x_check_errors.
7565 Calling x_uncatch_errors resumes the normal error handling. */
7567 void x_check_errors ();
7569 void
7570 x_catch_errors (dpy)
7571 Display *dpy;
7573 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7575 /* Make sure any errors from previous requests have been dealt with. */
7576 XSync (dpy, False);
7578 data->dpy = dpy;
7579 data->string[0] = 0;
7580 data->prev = x_error_message;
7581 x_error_message = data;
7584 /* Undo the last x_catch_errors call.
7585 DPY should be the display that was passed to x_catch_errors. */
7587 void
7588 x_uncatch_errors ()
7590 struct x_error_message_stack *tmp;
7592 BLOCK_INPUT;
7594 /* The display may have been closed before this function is called.
7595 Check if it is still open before calling XSync. */
7596 if (x_display_info_for_display (x_error_message->dpy) != 0)
7597 XSync (x_error_message->dpy, False);
7599 tmp = x_error_message;
7600 x_error_message = x_error_message->prev;
7601 xfree (tmp);
7602 UNBLOCK_INPUT;
7605 /* If any X protocol errors have arrived since the last call to
7606 x_catch_errors or x_check_errors, signal an Emacs error using
7607 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7609 void
7610 x_check_errors (dpy, format)
7611 Display *dpy;
7612 char *format;
7614 /* Make sure to catch any errors incurred so far. */
7615 XSync (dpy, False);
7617 if (x_error_message->string[0])
7619 char string[X_ERROR_MESSAGE_SIZE];
7620 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7621 x_uncatch_errors ();
7622 error (format, string);
7626 /* Nonzero if we had any X protocol errors
7627 since we did x_catch_errors on DPY. */
7630 x_had_errors_p (dpy)
7631 Display *dpy;
7633 /* Make sure to catch any errors incurred so far. */
7634 XSync (dpy, False);
7636 return x_error_message->string[0] != 0;
7639 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7641 void
7642 x_clear_errors (dpy)
7643 Display *dpy;
7645 x_error_message->string[0] = 0;
7648 /* Close off all unclosed x_catch_errors calls. */
7650 void
7651 x_fully_uncatch_errors ()
7653 while (x_error_message)
7654 x_uncatch_errors ();
7657 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7660 x_catching_errors ()
7662 return x_error_message != 0;
7665 #if 0
7666 static unsigned int x_wire_count;
7667 x_trace_wire ()
7669 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7671 #endif /* ! 0 */
7674 /* Handle SIGPIPE, which can happen when the connection to a server
7675 simply goes away. SIGPIPE is handled by x_connection_signal.
7676 Don't need to do anything, because the write which caused the
7677 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7678 which will do the appropriate cleanup for us. */
7680 static SIGTYPE
7681 x_connection_signal (signalnum) /* If we don't have an argument, */
7682 int signalnum; /* some compilers complain in signal calls. */
7684 #ifdef USG
7685 /* USG systems forget handlers when they are used;
7686 must reestablish each time */
7687 signal (signalnum, x_connection_signal);
7688 #endif /* USG */
7692 /************************************************************************
7693 Handling X errors
7694 ************************************************************************/
7696 /* Error message passed to x_connection_closed. */
7698 static char *error_msg;
7700 /* Function installed as fatal_error_signal_hook in
7701 x_connection_closed. Print the X error message, and exit normally,
7702 instead of dumping core when XtCloseDisplay fails. */
7704 static void
7705 x_fatal_error_signal ()
7707 fprintf (stderr, "%s\n", error_msg);
7708 exit (70);
7711 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7712 the text of an error message that lead to the connection loss. */
7714 static SIGTYPE
7715 x_connection_closed (dpy, error_message)
7716 Display *dpy;
7717 char *error_message;
7719 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7720 Lisp_Object frame, tail;
7722 error_msg = (char *) alloca (strlen (error_message) + 1);
7723 strcpy (error_msg, error_message);
7724 handling_signal = 0;
7726 /* Prevent being called recursively because of an error condition
7727 below. Otherwise, we might end up with printing ``can't find per
7728 display information'' in the recursive call instead of printing
7729 the original message here. */
7730 x_catch_errors (dpy);
7732 /* We have to close the display to inform Xt that it doesn't
7733 exist anymore. If we don't, Xt will continue to wait for
7734 events from the display. As a consequence, a sequence of
7736 M-x make-frame-on-display RET :1 RET
7737 ...kill the new frame, so that we get an IO error...
7738 M-x make-frame-on-display RET :1 RET
7740 will indefinitely wait in Xt for events for display `:1', opened
7741 in the first class to make-frame-on-display.
7743 Closing the display is reported to lead to a bus error on
7744 OpenWindows in certain situations. I suspect that is a bug
7745 in OpenWindows. I don't know how to cicumvent it here. */
7747 #ifdef USE_X_TOOLKIT
7748 /* If DPYINFO is null, this means we didn't open the display
7749 in the first place, so don't try to close it. */
7750 if (dpyinfo)
7752 extern void (*fatal_error_signal_hook) P_ ((void));
7753 fatal_error_signal_hook = x_fatal_error_signal;
7754 XtCloseDisplay (dpy);
7755 fatal_error_signal_hook = NULL;
7757 #endif
7759 #ifdef USE_GTK
7760 if (dpyinfo)
7761 xg_display_close (dpyinfo->display);
7762 #endif
7764 /* Indicate that this display is dead. */
7765 if (dpyinfo)
7766 dpyinfo->display = 0;
7768 /* First delete frames whose mini-buffers are on frames
7769 that are on the dead display. */
7770 FOR_EACH_FRAME (tail, frame)
7772 Lisp_Object minibuf_frame;
7773 minibuf_frame
7774 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7775 if (FRAME_X_P (XFRAME (frame))
7776 && FRAME_X_P (XFRAME (minibuf_frame))
7777 && ! EQ (frame, minibuf_frame)
7778 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7779 Fdelete_frame (frame, Qt);
7782 /* Now delete all remaining frames on the dead display.
7783 We are now sure none of these is used as the mini-buffer
7784 for another frame that we need to delete. */
7785 FOR_EACH_FRAME (tail, frame)
7786 if (FRAME_X_P (XFRAME (frame))
7787 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7789 /* Set this to t so that Fdelete_frame won't get confused
7790 trying to find a replacement. */
7791 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7792 Fdelete_frame (frame, Qt);
7795 if (dpyinfo)
7796 x_delete_display (dpyinfo);
7798 x_uncatch_errors ();
7800 if (x_display_list == 0)
7802 fprintf (stderr, "%s\n", error_msg);
7803 shut_down_emacs (0, 0, Qnil);
7804 exit (70);
7807 /* Ordinary stack unwind doesn't deal with these. */
7808 #ifdef SIGIO
7809 sigunblock (sigmask (SIGIO));
7810 #endif
7811 sigunblock (sigmask (SIGALRM));
7812 TOTALLY_UNBLOCK_INPUT;
7814 clear_waiting_for_input ();
7815 error ("%s", error_msg);
7818 /* We specifically use it before defining it, so that gcc doesn't inline it,
7819 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7820 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7822 /* This is the first-level handler for X protocol errors.
7823 It calls x_error_quitter or x_error_catcher. */
7825 static int
7826 x_error_handler (display, error)
7827 Display *display;
7828 XErrorEvent *error;
7830 if (x_error_message)
7831 x_error_catcher (display, error);
7832 else
7833 x_error_quitter (display, error);
7834 return 0;
7837 /* This is the usual handler for X protocol errors.
7838 It kills all frames on the display that we got the error for.
7839 If that was the only one, it prints an error message and kills Emacs. */
7841 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7843 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
7844 #define NO_INLINE __attribute__((noinline))
7845 #else
7846 #define NO_INLINE
7847 #endif
7849 /* Some versions of GNU/Linux define noinline in their headers. */
7851 #ifdef noinline
7852 #undef noinline
7853 #endif
7855 /* On older GCC versions, just putting x_error_quitter
7856 after x_error_handler prevents inlining into the former. */
7858 static void NO_INLINE
7859 x_error_quitter (display, error)
7860 Display *display;
7861 XErrorEvent *error;
7863 char buf[256], buf1[356];
7865 /* Ignore BadName errors. They can happen because of fonts
7866 or colors that are not defined. */
7868 if (error->error_code == BadName)
7869 return;
7871 /* Note that there is no real way portable across R3/R4 to get the
7872 original error handler. */
7874 XGetErrorText (display, error->error_code, buf, sizeof (buf));
7875 sprintf (buf1, "X protocol error: %s on protocol request %d",
7876 buf, error->request_code);
7877 x_connection_closed (display, buf1);
7881 /* This is the handler for X IO errors, always.
7882 It kills all frames on the display that we lost touch with.
7883 If that was the only one, it prints an error message and kills Emacs. */
7885 static int
7886 x_io_error_quitter (display)
7887 Display *display;
7889 char buf[256];
7891 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
7892 x_connection_closed (display, buf);
7893 return 0;
7896 /* Changing the font of the frame. */
7898 /* Give frame F the font named FONTNAME as its default font, and
7899 return the full name of that font. FONTNAME may be a wildcard
7900 pattern; in that case, we choose some font that fits the pattern.
7901 The return value shows which font we chose. */
7903 Lisp_Object
7904 x_new_font (f, fontname)
7905 struct frame *f;
7906 register char *fontname;
7908 struct font_info *fontp
7909 = FS_LOAD_FONT (f, 0, fontname, -1);
7911 if (!fontp)
7912 return Qnil;
7914 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
7915 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
7916 FRAME_FONTSET (f) = -1;
7918 FRAME_COLUMN_WIDTH (f) = fontp->average_width;
7919 FRAME_SPACE_WIDTH (f) = fontp->space_width;
7920 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
7922 compute_fringe_widths (f, 1);
7924 /* Compute the scroll bar width in character columns. */
7925 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
7927 int wid = FRAME_COLUMN_WIDTH (f);
7928 FRAME_CONFIG_SCROLL_BAR_COLS (f)
7929 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
7931 else
7933 int wid = FRAME_COLUMN_WIDTH (f);
7934 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
7937 /* Now make the frame display the given font. */
7938 if (FRAME_X_WINDOW (f) != 0)
7940 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
7941 FRAME_FONT (f)->fid);
7942 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
7943 FRAME_FONT (f)->fid);
7944 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
7945 FRAME_FONT (f)->fid);
7947 /* Don't change the size of a tip frame; there's no point in
7948 doing it because it's done in Fx_show_tip, and it leads to
7949 problems because the tip frame has no widget. */
7950 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
7951 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
7954 return build_string (fontp->full_name);
7957 /* Give frame F the fontset named FONTSETNAME as its default font, and
7958 return the full name of that fontset. FONTSETNAME may be a wildcard
7959 pattern; in that case, we choose some fontset that fits the pattern.
7960 The return value shows which fontset we chose. */
7962 Lisp_Object
7963 x_new_fontset (f, fontsetname)
7964 struct frame *f;
7965 char *fontsetname;
7967 int fontset = fs_query_fontset (build_string (fontsetname), 0);
7968 Lisp_Object result;
7970 if (fontset < 0)
7971 return Qnil;
7973 if (FRAME_FONTSET (f) == fontset)
7974 /* This fontset is already set in frame F. There's nothing more
7975 to do. */
7976 return fontset_name (fontset);
7978 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
7980 if (!STRINGP (result))
7981 /* Can't load ASCII font. */
7982 return Qnil;
7984 /* Since x_new_font doesn't update any fontset information, do it now. */
7985 FRAME_FONTSET (f) = fontset;
7987 #ifdef HAVE_X_I18N
7988 if (FRAME_XIC (f)
7989 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
7990 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
7991 #endif
7993 return build_string (fontsetname);
7997 /***********************************************************************
7998 X Input Methods
7999 ***********************************************************************/
8001 #ifdef HAVE_X_I18N
8003 #ifdef HAVE_X11R6
8005 /* XIM destroy callback function, which is called whenever the
8006 connection to input method XIM dies. CLIENT_DATA contains a
8007 pointer to the x_display_info structure corresponding to XIM. */
8009 static void
8010 xim_destroy_callback (xim, client_data, call_data)
8011 XIM xim;
8012 XPointer client_data;
8013 XPointer call_data;
8015 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8016 Lisp_Object frame, tail;
8018 BLOCK_INPUT;
8020 /* No need to call XDestroyIC.. */
8021 FOR_EACH_FRAME (tail, frame)
8023 struct frame *f = XFRAME (frame);
8024 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8026 FRAME_XIC (f) = NULL;
8027 xic_free_xfontset (f);
8031 /* No need to call XCloseIM. */
8032 dpyinfo->xim = NULL;
8033 XFree (dpyinfo->xim_styles);
8034 UNBLOCK_INPUT;
8037 #endif /* HAVE_X11R6 */
8039 #ifdef HAVE_X11R6
8040 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8041 extern char *XSetIMValues P_ ((XIM, ...));
8042 #endif
8044 /* Open the connection to the XIM server on display DPYINFO.
8045 RESOURCE_NAME is the resource name Emacs uses. */
8047 static void
8048 xim_open_dpy (dpyinfo, resource_name)
8049 struct x_display_info *dpyinfo;
8050 char *resource_name;
8052 XIM xim;
8054 #ifdef HAVE_XIM
8055 if (use_xim)
8057 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8058 EMACS_CLASS);
8059 dpyinfo->xim = xim;
8061 if (xim)
8063 #ifdef HAVE_X11R6
8064 XIMCallback destroy;
8065 #endif
8067 /* Get supported styles and XIM values. */
8068 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8070 #ifdef HAVE_X11R6
8071 destroy.callback = xim_destroy_callback;
8072 destroy.client_data = (XPointer)dpyinfo;
8073 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8074 #endif
8078 else
8079 #endif /* HAVE_XIM */
8080 dpyinfo->xim = NULL;
8084 #ifdef HAVE_X11R6_XIM
8086 struct xim_inst_t
8088 struct x_display_info *dpyinfo;
8089 char *resource_name;
8092 /* XIM instantiate callback function, which is called whenever an XIM
8093 server is available. DISPLAY is the display of the XIM.
8094 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8095 when the callback was registered. */
8097 static void
8098 xim_instantiate_callback (display, client_data, call_data)
8099 Display *display;
8100 XPointer client_data;
8101 XPointer call_data;
8103 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8104 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8106 /* We don't support multiple XIM connections. */
8107 if (dpyinfo->xim)
8108 return;
8110 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8112 /* Create XIC for the existing frames on the same display, as long
8113 as they have no XIC. */
8114 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8116 Lisp_Object tail, frame;
8118 BLOCK_INPUT;
8119 FOR_EACH_FRAME (tail, frame)
8121 struct frame *f = XFRAME (frame);
8123 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8124 if (FRAME_XIC (f) == NULL)
8126 create_frame_xic (f);
8127 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8128 xic_set_statusarea (f);
8129 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8131 struct window *w = XWINDOW (f->selected_window);
8132 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8137 UNBLOCK_INPUT;
8141 #endif /* HAVE_X11R6_XIM */
8144 /* Open a connection to the XIM server on display DPYINFO.
8145 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8146 connection only at the first time. On X11R6, open the connection
8147 in the XIM instantiate callback function. */
8149 static void
8150 xim_initialize (dpyinfo, resource_name)
8151 struct x_display_info *dpyinfo;
8152 char *resource_name;
8154 #ifdef HAVE_XIM
8155 if (use_xim)
8157 #ifdef HAVE_X11R6_XIM
8158 struct xim_inst_t *xim_inst;
8159 int len;
8161 dpyinfo->xim = NULL;
8162 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8163 xim_inst->dpyinfo = dpyinfo;
8164 len = strlen (resource_name);
8165 xim_inst->resource_name = (char *) xmalloc (len + 1);
8166 bcopy (resource_name, xim_inst->resource_name, len + 1);
8167 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8168 resource_name, EMACS_CLASS,
8169 xim_instantiate_callback,
8170 /* This is XPointer in XFree86
8171 but (XPointer *) on Tru64, at
8172 least, hence the configure test. */
8173 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8174 #else /* not HAVE_X11R6_XIM */
8175 dpyinfo->xim = NULL;
8176 xim_open_dpy (dpyinfo, resource_name);
8177 #endif /* not HAVE_X11R6_XIM */
8180 else
8181 #endif /* HAVE_XIM */
8182 dpyinfo->xim = NULL;
8186 /* Close the connection to the XIM server on display DPYINFO. */
8188 static void
8189 xim_close_dpy (dpyinfo)
8190 struct x_display_info *dpyinfo;
8192 #ifdef HAVE_XIM
8193 if (use_xim)
8195 #ifdef HAVE_X11R6_XIM
8196 if (dpyinfo->display)
8197 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8198 NULL, EMACS_CLASS,
8199 xim_instantiate_callback, NULL);
8200 #endif /* not HAVE_X11R6_XIM */
8201 if (dpyinfo->display)
8202 XCloseIM (dpyinfo->xim);
8203 dpyinfo->xim = NULL;
8204 XFree (dpyinfo->xim_styles);
8206 #endif /* HAVE_XIM */
8209 #endif /* not HAVE_X11R6_XIM */
8213 /* Calculate the absolute position in frame F
8214 from its current recorded position values and gravity. */
8216 void
8217 x_calc_absolute_position (f)
8218 struct frame *f;
8220 int flags = f->size_hint_flags;
8222 /* We have nothing to do if the current position
8223 is already for the top-left corner. */
8224 if (! ((flags & XNegative) || (flags & YNegative)))
8225 return;
8227 /* Treat negative positions as relative to the leftmost bottommost
8228 position that fits on the screen. */
8229 if (flags & XNegative)
8230 f->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8231 - FRAME_PIXEL_WIDTH (f) + f->left_pos);
8234 int height = FRAME_PIXEL_HEIGHT (f);
8236 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8237 /* Something is fishy here. When using Motif, starting Emacs with
8238 `-g -0-0', the frame appears too low by a few pixels.
8240 This seems to be so because initially, while Emacs is starting,
8241 the column widget's height and the frame's pixel height are
8242 different. The column widget's height is the right one. In
8243 later invocations, when Emacs is up, the frame's pixel height
8244 is right, though.
8246 It's not obvious where the initial small difference comes from.
8247 2000-12-01, gerd. */
8249 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8250 #endif
8252 if (flags & YNegative)
8253 f->top_pos = (FRAME_X_DISPLAY_INFO (f)->height - height + f->top_pos);
8256 /* The left_pos and top_pos
8257 are now relative to the top and left screen edges,
8258 so the flags should correspond. */
8259 f->size_hint_flags &= ~ (XNegative | YNegative);
8262 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8263 to really change the position, and 0 when calling from
8264 x_make_frame_visible (in that case, XOFF and YOFF are the current
8265 position values). It is -1 when calling from x_set_frame_parameters,
8266 which means, do adjust for borders but don't change the gravity. */
8268 void
8269 x_set_offset (f, xoff, yoff, change_gravity)
8270 struct frame *f;
8271 register int xoff, yoff;
8272 int change_gravity;
8274 int modified_top, modified_left;
8276 if (change_gravity > 0)
8278 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8279 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8281 f->top_pos = yoff;
8282 f->left_pos = xoff;
8283 f->size_hint_flags &= ~ (XNegative | YNegative);
8284 if (xoff < 0)
8285 f->size_hint_flags |= XNegative;
8286 if (yoff < 0)
8287 f->size_hint_flags |= YNegative;
8288 f->win_gravity = NorthWestGravity;
8290 x_calc_absolute_position (f);
8292 BLOCK_INPUT;
8293 x_wm_set_size_hint (f, (long) 0, 0);
8295 modified_left = f->left_pos;
8296 modified_top = f->top_pos;
8298 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8300 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8301 than the WM decorations. So we use the calculated offset instead
8302 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8303 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8304 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8307 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8308 modified_left, modified_top);
8310 x_sync_with_move (f, f->left_pos, f->top_pos,
8311 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8312 ? 1 : 0);
8314 /* change_gravity is non-zero when this function is called from Lisp to
8315 programmatically move a frame. In that case, we call
8316 x_check_expected_move to discover if we have a "Type A" or "Type B"
8317 window manager, and, for a "Type A" window manager, adjust the position
8318 of the frame.
8320 We call x_check_expected_move if a programmatic move occurred, and
8321 either the window manager type (A/B) is unknown or it is Type A but we
8322 need to compute the top/left offset adjustment for this frame. */
8324 if (change_gravity != 0 &&
8325 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8326 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8327 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8328 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8329 x_check_expected_move (f, modified_left, modified_top);
8331 UNBLOCK_INPUT;
8334 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8335 on the root window for frame F contains ATOMNAME.
8336 This is how a WM check shall be done according to the Window Manager
8337 Specification/Extended Window Manager Hints at
8338 http://freedesktop.org/wiki/Standards_2fwm_2dspec. */
8340 static int
8341 wm_supports (f, atomname)
8342 struct frame *f;
8343 const char *atomname;
8345 Atom actual_type;
8346 unsigned long actual_size, bytes_remaining;
8347 int i, rc, actual_format;
8348 Atom prop_atom;
8349 Window wmcheck_window;
8350 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8351 Window target_window = dpyinfo->root_window;
8352 long max_len = 65536;
8353 Display *dpy = FRAME_X_DISPLAY (f);
8354 unsigned char *tmp_data = NULL;
8355 Atom target_type = XA_WINDOW;
8356 Atom want_atom;
8358 BLOCK_INPUT;
8360 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8362 x_catch_errors (dpy);
8363 rc = XGetWindowProperty (dpy, target_window,
8364 prop_atom, 0, max_len, False, target_type,
8365 &actual_type, &actual_format, &actual_size,
8366 &bytes_remaining, &tmp_data);
8368 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8370 if (tmp_data) XFree (tmp_data);
8371 x_uncatch_errors ();
8372 UNBLOCK_INPUT;
8373 return 0;
8376 wmcheck_window = *(Window *) tmp_data;
8377 XFree (tmp_data);
8379 /* Check if window exists. */
8380 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8381 x_sync (f);
8382 if (x_had_errors_p (dpy))
8384 x_uncatch_errors ();
8385 UNBLOCK_INPUT;
8386 return 0;
8389 if (dpyinfo->net_supported_window != wmcheck_window)
8391 /* Window changed, reload atoms */
8392 if (dpyinfo->net_supported_atoms != NULL)
8393 XFree (dpyinfo->net_supported_atoms);
8394 dpyinfo->net_supported_atoms = NULL;
8395 dpyinfo->nr_net_supported_atoms = 0;
8396 dpyinfo->net_supported_window = 0;
8398 target_type = XA_ATOM;
8399 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8400 tmp_data = NULL;
8401 rc = XGetWindowProperty (dpy, target_window,
8402 prop_atom, 0, max_len, False, target_type,
8403 &actual_type, &actual_format, &actual_size,
8404 &bytes_remaining, &tmp_data);
8406 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8408 if (tmp_data) XFree (tmp_data);
8409 x_uncatch_errors ();
8410 UNBLOCK_INPUT;
8411 return 0;
8414 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8415 dpyinfo->nr_net_supported_atoms = actual_size;
8416 dpyinfo->net_supported_window = wmcheck_window;
8419 rc = 0;
8420 want_atom = XInternAtom (dpy, atomname, False);
8422 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8423 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8425 x_uncatch_errors ();
8426 UNBLOCK_INPUT;
8428 return rc;
8431 /* Do fullscreen as specified in extended window manager hints */
8433 static int
8434 do_ewmh_fullscreen (f)
8435 struct frame *f;
8437 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8439 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8440 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8441 if (!have_net_atom)
8442 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8444 if (have_net_atom)
8446 Lisp_Object frame;
8447 const char *atom = "_NET_WM_STATE";
8448 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8449 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8450 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8451 const char *what = NULL;
8453 XSETFRAME (frame, f);
8455 /* If there are _NET_ atoms we assume we have extended window manager
8456 hints. */
8457 switch (f->want_fullscreen)
8459 case FULLSCREEN_BOTH:
8460 what = fs;
8461 break;
8462 case FULLSCREEN_WIDTH:
8463 what = fw;
8464 break;
8465 case FULLSCREEN_HEIGHT:
8466 what = fh;
8467 break;
8470 if (what != NULL && !wm_supports (f, what)) return 0;
8473 Fx_send_client_event (frame, make_number (0), frame,
8474 make_unibyte_string (atom, strlen (atom)),
8475 make_number (32),
8476 Fcons (make_number (0), /* Remove */
8477 Fcons
8478 (make_unibyte_string (fs,
8479 strlen (fs)),
8480 Qnil)));
8481 Fx_send_client_event (frame, make_number (0), frame,
8482 make_unibyte_string (atom, strlen (atom)),
8483 make_number (32),
8484 Fcons (make_number (0), /* Remove */
8485 Fcons
8486 (make_unibyte_string (fh,
8487 strlen (fh)),
8488 Qnil)));
8489 Fx_send_client_event (frame, make_number (0), frame,
8490 make_unibyte_string (atom, strlen (atom)),
8491 make_number (32),
8492 Fcons (make_number (0), /* Remove */
8493 Fcons
8494 (make_unibyte_string (fw,
8495 strlen (fw)),
8496 Qnil)));
8497 f->want_fullscreen = FULLSCREEN_NONE;
8498 if (what != NULL)
8499 Fx_send_client_event (frame, make_number (0), frame,
8500 make_unibyte_string (atom, strlen (atom)),
8501 make_number (32),
8502 Fcons (make_number (1), /* Add */
8503 Fcons
8504 (make_unibyte_string (what,
8505 strlen (what)),
8506 Qnil)));
8509 return have_net_atom;
8512 static void
8513 XTfullscreen_hook (f)
8514 FRAME_PTR f;
8516 if (f->async_visible)
8518 BLOCK_INPUT;
8519 do_ewmh_fullscreen (f);
8520 x_sync (f);
8521 UNBLOCK_INPUT;
8526 /* Check if we need to resize the frame due to a fullscreen request.
8527 If so needed, resize the frame. */
8528 static void
8529 x_check_fullscreen (f)
8530 struct frame *f;
8532 if (f->want_fullscreen & FULLSCREEN_BOTH)
8534 int width, height, ign;
8536 if (do_ewmh_fullscreen (f))
8537 return;
8539 x_real_positions (f, &f->left_pos, &f->top_pos);
8541 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8543 /* We do not need to move the window, it shall be taken care of
8544 when setting WM manager hints.
8545 If the frame is visible already, the position is checked by
8546 x_check_expected_move. */
8547 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8549 change_frame_size (f, height, width, 0, 1, 0);
8550 SET_FRAME_GARBAGED (f);
8551 cancel_mouse_face (f);
8553 /* Wait for the change of frame size to occur */
8554 f->want_fullscreen |= FULLSCREEN_WAIT;
8559 /* This function is called by x_set_offset to determine whether the window
8560 manager interfered with the positioning of the frame. Type A window
8561 managers position the surrounding window manager decorations a small
8562 amount above and left of the user-supplied position. Type B window
8563 managers position the surrounding window manager decorations at the
8564 user-specified position. If we detect a Type A window manager, we
8565 compensate by moving the window right and down by the proper amount. */
8567 static void
8568 x_check_expected_move (f, expected_left, expected_top)
8569 struct frame *f;
8570 int expected_left;
8571 int expected_top;
8573 int current_left = 0, current_top = 0;
8575 /* x_real_positions returns the left and top offsets of the outermost
8576 window manager window around the frame. */
8578 x_real_positions (f, &current_left, &current_top);
8580 if (current_left != expected_left || current_top != expected_top)
8582 /* It's a "Type A" window manager. */
8584 int adjusted_left;
8585 int adjusted_top;
8587 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8588 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8589 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8591 /* Now fix the mispositioned frame's location. */
8593 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8594 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8596 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8597 adjusted_left, adjusted_top);
8599 x_sync_with_move (f, expected_left, expected_top, 0);
8601 else
8602 /* It's a "Type B" window manager. We don't have to adjust the
8603 frame's position. */
8605 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8609 /* Wait for XGetGeometry to return up-to-date position information for a
8610 recently-moved frame. Call this immediately after calling XMoveWindow.
8611 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8612 frame has been moved to, so we use a fuzzy position comparison instead
8613 of an exact comparison. */
8615 static void
8616 x_sync_with_move (f, left, top, fuzzy)
8617 struct frame *f;
8618 int left, top, fuzzy;
8620 int count = 0;
8622 while (count++ < 50)
8624 int current_left = 0, current_top = 0;
8626 /* In theory, this call to XSync only needs to happen once, but in
8627 practice, it doesn't seem to work, hence the need for the surrounding
8628 loop. */
8630 XSync (FRAME_X_DISPLAY (f), False);
8631 x_real_positions (f, &current_left, &current_top);
8633 if (fuzzy)
8635 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8636 pixels. */
8638 if (abs (current_left - left) <= 10 && abs (current_top - top) <= 40)
8639 return;
8641 else if (current_left == left && current_top == top)
8642 return;
8645 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8646 will then return up-to-date position info. */
8648 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8652 /* Change the size of frame F's X window to COLS/ROWS in the case F
8653 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8654 top-left-corner window gravity for this size change and subsequent
8655 size changes. Otherwise we leave the window gravity unchanged. */
8657 static void
8658 x_set_window_size_1 (f, change_gravity, cols, rows)
8659 struct frame *f;
8660 int change_gravity;
8661 int cols, rows;
8663 int pixelwidth, pixelheight;
8665 check_frame_size (f, &rows, &cols);
8666 f->scroll_bar_actual_width
8667 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8669 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8670 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8671 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8673 compute_fringe_widths (f, 0);
8675 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8676 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8678 f->win_gravity = NorthWestGravity;
8679 x_wm_set_size_hint (f, (long) 0, 0);
8681 XSync (FRAME_X_DISPLAY (f), False);
8682 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8683 pixelwidth, pixelheight);
8685 /* Now, strictly speaking, we can't be sure that this is accurate,
8686 but the window manager will get around to dealing with the size
8687 change request eventually, and we'll hear how it went when the
8688 ConfigureNotify event gets here.
8690 We could just not bother storing any of this information here,
8691 and let the ConfigureNotify event set everything up, but that
8692 might be kind of confusing to the Lisp code, since size changes
8693 wouldn't be reported in the frame parameters until some random
8694 point in the future when the ConfigureNotify event arrives.
8696 We pass 1 for DELAY since we can't run Lisp code inside of
8697 a BLOCK_INPUT. */
8698 change_frame_size (f, rows, cols, 0, 1, 0);
8699 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8700 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8702 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8703 receive in the ConfigureNotify event; if we get what we asked
8704 for, then the event won't cause the screen to become garbaged, so
8705 we have to make sure to do it here. */
8706 SET_FRAME_GARBAGED (f);
8708 XFlush (FRAME_X_DISPLAY (f));
8712 /* Call this to change the size of frame F's x-window.
8713 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8714 for this size change and subsequent size changes.
8715 Otherwise we leave the window gravity unchanged. */
8717 void
8718 x_set_window_size (f, change_gravity, cols, rows)
8719 struct frame *f;
8720 int change_gravity;
8721 int cols, rows;
8723 BLOCK_INPUT;
8725 #ifdef USE_GTK
8726 if (FRAME_GTK_WIDGET (f))
8727 xg_frame_set_char_size (f, cols, rows);
8728 else
8729 x_set_window_size_1 (f, change_gravity, cols, rows);
8730 #elif USE_X_TOOLKIT
8732 if (f->output_data.x->widget != NULL)
8734 /* The x and y position of the widget is clobbered by the
8735 call to XtSetValues within EmacsFrameSetCharSize.
8736 This is a real kludge, but I don't understand Xt so I can't
8737 figure out a correct fix. Can anyone else tell me? -- rms. */
8738 int xpos = f->output_data.x->widget->core.x;
8739 int ypos = f->output_data.x->widget->core.y;
8740 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
8741 f->output_data.x->widget->core.x = xpos;
8742 f->output_data.x->widget->core.y = ypos;
8744 else
8745 x_set_window_size_1 (f, change_gravity, cols, rows);
8747 #else /* not USE_X_TOOLKIT */
8749 x_set_window_size_1 (f, change_gravity, cols, rows);
8751 #endif /* not USE_X_TOOLKIT */
8753 /* If cursor was outside the new size, mark it as off. */
8754 mark_window_cursors_off (XWINDOW (f->root_window));
8756 /* Clear out any recollection of where the mouse highlighting was,
8757 since it might be in a place that's outside the new frame size.
8758 Actually checking whether it is outside is a pain in the neck,
8759 so don't try--just let the highlighting be done afresh with new size. */
8760 cancel_mouse_face (f);
8762 UNBLOCK_INPUT;
8765 /* Mouse warping. */
8767 void
8768 x_set_mouse_position (f, x, y)
8769 struct frame *f;
8770 int x, y;
8772 int pix_x, pix_y;
8774 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8775 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8777 if (pix_x < 0) pix_x = 0;
8778 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8780 if (pix_y < 0) pix_y = 0;
8781 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8783 BLOCK_INPUT;
8785 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8786 0, 0, 0, 0, pix_x, pix_y);
8787 UNBLOCK_INPUT;
8790 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8792 void
8793 x_set_mouse_pixel_position (f, pix_x, pix_y)
8794 struct frame *f;
8795 int pix_x, pix_y;
8797 BLOCK_INPUT;
8799 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8800 0, 0, 0, 0, pix_x, pix_y);
8801 UNBLOCK_INPUT;
8804 /* focus shifting, raising and lowering. */
8806 void
8807 x_focus_on_frame (f)
8808 struct frame *f;
8810 #if 0 /* This proves to be unpleasant. */
8811 x_raise_frame (f);
8812 #endif
8813 #if 0
8814 /* I don't think that the ICCCM allows programs to do things like this
8815 without the interaction of the window manager. Whatever you end up
8816 doing with this code, do it to x_unfocus_frame too. */
8817 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8818 RevertToPointerRoot, CurrentTime);
8819 #endif /* ! 0 */
8822 void
8823 x_unfocus_frame (f)
8824 struct frame *f;
8826 #if 0
8827 /* Look at the remarks in x_focus_on_frame. */
8828 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
8829 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
8830 RevertToPointerRoot, CurrentTime);
8831 #endif /* ! 0 */
8834 /* Raise frame F. */
8836 void
8837 x_raise_frame (f)
8838 struct frame *f;
8840 BLOCK_INPUT;
8841 if (f->async_visible)
8842 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8844 XFlush (FRAME_X_DISPLAY (f));
8845 UNBLOCK_INPUT;
8848 /* Lower frame F. */
8850 void
8851 x_lower_frame (f)
8852 struct frame *f;
8854 if (f->async_visible)
8856 BLOCK_INPUT;
8857 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8858 XFlush (FRAME_X_DISPLAY (f));
8859 UNBLOCK_INPUT;
8863 static void
8864 XTframe_raise_lower (f, raise_flag)
8865 FRAME_PTR f;
8866 int raise_flag;
8868 if (raise_flag)
8870 /* The following code is needed for `raise-frame' to work on
8871 some versions of metacity; see Window Manager
8872 Specification/Extended Window Manager Hints at
8873 http://freedesktop.org/wiki/Standards_2fwm_2dspec */
8875 #if 0
8876 /* However, on other versions (metacity 2.17.2-1.fc7), it
8877 reportedly causes hangs when resizing frames. */
8879 const char *atom = "_NET_ACTIVE_WINDOW";
8880 if (f->async_visible && wm_supports (f, atom))
8882 Lisp_Object frame;
8883 XSETFRAME (frame, f);
8884 Fx_send_client_event (frame, make_number (0), frame,
8885 make_unibyte_string (atom, strlen (atom)),
8886 make_number (32),
8887 Fcons (make_number (1),
8888 Fcons (make_number (last_user_time),
8889 Qnil)));
8891 else
8892 #endif
8893 x_raise_frame (f);
8895 else
8896 x_lower_frame (f);
8899 /* Change of visibility. */
8901 /* This tries to wait until the frame is really visible.
8902 However, if the window manager asks the user where to position
8903 the frame, this will return before the user finishes doing that.
8904 The frame will not actually be visible at that time,
8905 but it will become visible later when the window manager
8906 finishes with it. */
8908 void
8909 x_make_frame_visible (f)
8910 struct frame *f;
8912 Lisp_Object type;
8913 int original_top, original_left;
8914 int retry_count = 2;
8916 retry:
8918 BLOCK_INPUT;
8920 type = x_icon_type (f);
8921 if (!NILP (type))
8922 x_bitmap_icon (f, type);
8924 if (! FRAME_VISIBLE_P (f))
8926 /* We test FRAME_GARBAGED_P here to make sure we don't
8927 call x_set_offset a second time
8928 if we get to x_make_frame_visible a second time
8929 before the window gets really visible. */
8930 if (! FRAME_ICONIFIED_P (f)
8931 && ! f->output_data.x->asked_for_visible)
8932 x_set_offset (f, f->left_pos, f->top_pos, 0);
8934 f->output_data.x->asked_for_visible = 1;
8936 if (! EQ (Vx_no_window_manager, Qt))
8937 x_wm_set_window_state (f, NormalState);
8938 #ifdef USE_X_TOOLKIT
8939 /* This was XtPopup, but that did nothing for an iconified frame. */
8940 XtMapWidget (f->output_data.x->widget);
8941 #else /* not USE_X_TOOLKIT */
8942 #ifdef USE_GTK
8943 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
8944 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
8945 #else
8946 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8947 #endif /* not USE_GTK */
8948 #endif /* not USE_X_TOOLKIT */
8949 #if 0 /* This seems to bring back scroll bars in the wrong places
8950 if the window configuration has changed. They seem
8951 to come back ok without this. */
8952 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8953 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
8954 #endif
8957 XFlush (FRAME_X_DISPLAY (f));
8959 /* Synchronize to ensure Emacs knows the frame is visible
8960 before we do anything else. We do this loop with input not blocked
8961 so that incoming events are handled. */
8963 Lisp_Object frame;
8964 int count;
8965 /* This must be before UNBLOCK_INPUT
8966 since events that arrive in response to the actions above
8967 will set it when they are handled. */
8968 int previously_visible = f->output_data.x->has_been_visible;
8970 original_left = f->left_pos;
8971 original_top = f->top_pos;
8973 /* This must come after we set COUNT. */
8974 UNBLOCK_INPUT;
8976 /* We unblock here so that arriving X events are processed. */
8978 /* Now move the window back to where it was "supposed to be".
8979 But don't do it if the gravity is negative.
8980 When the gravity is negative, this uses a position
8981 that is 3 pixels too low. Perhaps that's really the border width.
8983 Don't do this if the window has never been visible before,
8984 because the window manager may choose the position
8985 and we don't want to override it. */
8987 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
8988 && f->win_gravity == NorthWestGravity
8989 && previously_visible)
8991 Drawable rootw;
8992 int x, y;
8993 unsigned int width, height, border, depth;
8995 BLOCK_INPUT;
8997 /* On some window managers (such as FVWM) moving an existing
8998 window, even to the same place, causes the window manager
8999 to introduce an offset. This can cause the window to move
9000 to an unexpected location. Check the geometry (a little
9001 slow here) and then verify that the window is in the right
9002 place. If the window is not in the right place, move it
9003 there, and take the potential window manager hit. */
9004 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9005 &rootw, &x, &y, &width, &height, &border, &depth);
9007 if (original_left != x || original_top != y)
9008 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9009 original_left, original_top);
9011 UNBLOCK_INPUT;
9014 XSETFRAME (frame, f);
9016 /* Wait until the frame is visible. Process X events until a
9017 MapNotify event has been seen, or until we think we won't get a
9018 MapNotify at all.. */
9019 for (count = input_signal_count + 10;
9020 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9022 /* Force processing of queued events. */
9023 x_sync (f);
9025 /* Machines that do polling rather than SIGIO have been
9026 observed to go into a busy-wait here. So we'll fake an
9027 alarm signal to let the handler know that there's something
9028 to be read. We used to raise a real alarm, but it seems
9029 that the handler isn't always enabled here. This is
9030 probably a bug. */
9031 if (input_polling_used ())
9033 /* It could be confusing if a real alarm arrives while
9034 processing the fake one. Turn it off and let the
9035 handler reset it. */
9036 extern void poll_for_input_1 P_ ((void));
9037 int old_poll_suppress_count = poll_suppress_count;
9038 poll_suppress_count = 1;
9039 poll_for_input_1 ();
9040 poll_suppress_count = old_poll_suppress_count;
9043 /* See if a MapNotify event has been processed. */
9044 FRAME_SAMPLE_VISIBILITY (f);
9047 /* 2000-09-28: In
9049 (let ((f (selected-frame)))
9050 (iconify-frame f)
9051 (raise-frame f))
9053 the frame is not raised with various window managers on
9054 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9055 unknown reason, the call to XtMapWidget is completely ignored.
9056 Mapping the widget a second time works. */
9058 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9059 goto retry;
9063 /* Change from mapped state to withdrawn state. */
9065 /* Make the frame visible (mapped and not iconified). */
9067 void
9068 x_make_frame_invisible (f)
9069 struct frame *f;
9071 Window window;
9073 /* Use the frame's outermost window, not the one we normally draw on. */
9074 window = FRAME_OUTER_WINDOW (f);
9076 /* Don't keep the highlight on an invisible frame. */
9077 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9078 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9080 #if 0/* This might add unreliability; I don't trust it -- rms. */
9081 if (! f->async_visible && ! f->async_iconified)
9082 return;
9083 #endif
9085 BLOCK_INPUT;
9087 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9088 that the current position of the window is user-specified, rather than
9089 program-specified, so that when the window is mapped again, it will be
9090 placed at the same location, without forcing the user to position it
9091 by hand again (they have already done that once for this window.) */
9092 x_wm_set_size_hint (f, (long) 0, 1);
9094 #ifdef USE_GTK
9095 if (FRAME_GTK_OUTER_WIDGET (f))
9096 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9097 else
9098 #endif
9100 #ifdef HAVE_X11R4
9102 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9103 DefaultScreen (FRAME_X_DISPLAY (f))))
9105 UNBLOCK_INPUT_RESIGNAL;
9106 error ("Can't notify window manager of window withdrawal");
9108 #else /* ! defined (HAVE_X11R4) */
9110 /* Tell the window manager what we're going to do. */
9111 if (! EQ (Vx_no_window_manager, Qt))
9113 XEvent unmap;
9115 unmap.xunmap.type = UnmapNotify;
9116 unmap.xunmap.window = window;
9117 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
9118 unmap.xunmap.from_configure = False;
9119 if (! XSendEvent (FRAME_X_DISPLAY (f),
9120 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9121 False,
9122 SubstructureRedirectMaskSubstructureNotifyMask,
9123 &unmap))
9125 UNBLOCK_INPUT_RESIGNAL;
9126 error ("Can't notify window manager of withdrawal");
9130 /* Unmap the window ourselves. Cheeky! */
9131 XUnmapWindow (FRAME_X_DISPLAY (f), window);
9132 #endif /* ! defined (HAVE_X11R4) */
9135 /* We can't distinguish this from iconification
9136 just by the event that we get from the server.
9137 So we can't win using the usual strategy of letting
9138 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9139 and synchronize with the server to make sure we agree. */
9140 f->visible = 0;
9141 FRAME_ICONIFIED_P (f) = 0;
9142 f->async_visible = 0;
9143 f->async_iconified = 0;
9145 x_sync (f);
9147 UNBLOCK_INPUT;
9150 /* Change window state from mapped to iconified. */
9152 void
9153 x_iconify_frame (f)
9154 struct frame *f;
9156 int result;
9157 Lisp_Object type;
9159 /* Don't keep the highlight on an invisible frame. */
9160 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9161 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9163 if (f->async_iconified)
9164 return;
9166 BLOCK_INPUT;
9168 FRAME_SAMPLE_VISIBILITY (f);
9170 type = x_icon_type (f);
9171 if (!NILP (type))
9172 x_bitmap_icon (f, type);
9174 #ifdef USE_GTK
9175 if (FRAME_GTK_OUTER_WIDGET (f))
9177 if (! FRAME_VISIBLE_P (f))
9178 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9180 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9181 f->iconified = 1;
9182 f->visible = 1;
9183 f->async_iconified = 1;
9184 f->async_visible = 0;
9185 UNBLOCK_INPUT;
9186 return;
9188 #endif
9190 #ifdef USE_X_TOOLKIT
9192 if (! FRAME_VISIBLE_P (f))
9194 if (! EQ (Vx_no_window_manager, Qt))
9195 x_wm_set_window_state (f, IconicState);
9196 /* This was XtPopup, but that did nothing for an iconified frame. */
9197 XtMapWidget (f->output_data.x->widget);
9198 /* The server won't give us any event to indicate
9199 that an invisible frame was changed to an icon,
9200 so we have to record it here. */
9201 f->iconified = 1;
9202 f->visible = 1;
9203 f->async_iconified = 1;
9204 f->async_visible = 0;
9205 UNBLOCK_INPUT;
9206 return;
9209 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9210 XtWindow (f->output_data.x->widget),
9211 DefaultScreen (FRAME_X_DISPLAY (f)));
9212 UNBLOCK_INPUT;
9214 if (!result)
9215 error ("Can't notify window manager of iconification");
9217 f->async_iconified = 1;
9218 f->async_visible = 0;
9221 BLOCK_INPUT;
9222 XFlush (FRAME_X_DISPLAY (f));
9223 UNBLOCK_INPUT;
9224 #else /* not USE_X_TOOLKIT */
9226 /* Make sure the X server knows where the window should be positioned,
9227 in case the user deiconifies with the window manager. */
9228 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
9229 x_set_offset (f, f->left_pos, f->top_pos, 0);
9231 /* Since we don't know which revision of X we're running, we'll use both
9232 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9234 /* X11R4: send a ClientMessage to the window manager using the
9235 WM_CHANGE_STATE type. */
9237 XEvent message;
9239 message.xclient.window = FRAME_X_WINDOW (f);
9240 message.xclient.type = ClientMessage;
9241 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9242 message.xclient.format = 32;
9243 message.xclient.data.l[0] = IconicState;
9245 if (! XSendEvent (FRAME_X_DISPLAY (f),
9246 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9247 False,
9248 SubstructureRedirectMask | SubstructureNotifyMask,
9249 &message))
9251 UNBLOCK_INPUT_RESIGNAL;
9252 error ("Can't notify window manager of iconification");
9256 /* X11R3: set the initial_state field of the window manager hints to
9257 IconicState. */
9258 x_wm_set_window_state (f, IconicState);
9260 if (!FRAME_VISIBLE_P (f))
9262 /* If the frame was withdrawn, before, we must map it. */
9263 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9266 f->async_iconified = 1;
9267 f->async_visible = 0;
9269 XFlush (FRAME_X_DISPLAY (f));
9270 UNBLOCK_INPUT;
9271 #endif /* not USE_X_TOOLKIT */
9275 /* Free X resources of frame F. */
9277 void
9278 x_free_frame_resources (f)
9279 struct frame *f;
9281 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9282 Lisp_Object bar;
9283 struct scroll_bar *b;
9285 BLOCK_INPUT;
9287 /* If a display connection is dead, don't try sending more
9288 commands to the X server. */
9289 if (dpyinfo->display)
9291 if (f->output_data.x->icon_desc)
9292 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9294 #ifdef USE_X_TOOLKIT
9295 /* Explicitly destroy the scroll bars of the frame. Without
9296 this, we get "BadDrawable" errors from the toolkit later on,
9297 presumably from expose events generated for the disappearing
9298 toolkit scroll bars. */
9299 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9301 b = XSCROLL_BAR (bar);
9302 x_scroll_bar_remove (b);
9304 #endif
9306 #ifdef HAVE_X_I18N
9307 if (FRAME_XIC (f))
9308 free_frame_xic (f);
9309 #endif
9311 #ifdef USE_X_TOOLKIT
9312 if (f->output_data.x->widget)
9314 XtDestroyWidget (f->output_data.x->widget);
9315 f->output_data.x->widget = NULL;
9317 /* Tooltips don't have widgets, only a simple X window, even if
9318 we are using a toolkit. */
9319 else if (FRAME_X_WINDOW (f))
9320 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9322 free_frame_menubar (f);
9323 #else /* !USE_X_TOOLKIT */
9325 #ifdef USE_GTK
9326 /* In the GTK version, tooltips are normal X
9327 frames. We must check and free both types. */
9328 if (FRAME_GTK_OUTER_WIDGET (f))
9330 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9331 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9332 FRAME_GTK_OUTER_WIDGET (f) = 0;
9334 #endif /* USE_GTK */
9336 if (FRAME_X_WINDOW (f))
9337 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9338 #endif /* !USE_X_TOOLKIT */
9340 unload_color (f, f->output_data.x->foreground_pixel);
9341 unload_color (f, f->output_data.x->background_pixel);
9342 unload_color (f, f->output_data.x->cursor_pixel);
9343 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9344 unload_color (f, f->output_data.x->border_pixel);
9345 unload_color (f, f->output_data.x->mouse_pixel);
9347 if (f->output_data.x->scroll_bar_background_pixel != -1)
9348 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9349 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9350 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9351 #ifdef USE_TOOLKIT_SCROLL_BARS
9352 /* Scrollbar shadow colors. */
9353 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9354 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9355 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9356 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9357 #endif /* USE_TOOLKIT_SCROLL_BARS */
9358 if (f->output_data.x->white_relief.allocated_p)
9359 unload_color (f, f->output_data.x->white_relief.pixel);
9360 if (f->output_data.x->black_relief.allocated_p)
9361 unload_color (f, f->output_data.x->black_relief.pixel);
9363 if (FRAME_FACE_CACHE (f))
9364 free_frame_faces (f);
9366 x_free_gcs (f);
9367 XFlush (FRAME_X_DISPLAY (f));
9370 if (f->output_data.x->saved_menu_event)
9371 xfree (f->output_data.x->saved_menu_event);
9373 xfree (f->output_data.x);
9374 f->output_data.x = NULL;
9376 if (f == dpyinfo->x_focus_frame)
9377 dpyinfo->x_focus_frame = 0;
9378 if (f == dpyinfo->x_focus_event_frame)
9379 dpyinfo->x_focus_event_frame = 0;
9380 if (f == dpyinfo->x_highlight_frame)
9381 dpyinfo->x_highlight_frame = 0;
9383 if (f == dpyinfo->mouse_face_mouse_frame)
9385 dpyinfo->mouse_face_beg_row
9386 = dpyinfo->mouse_face_beg_col = -1;
9387 dpyinfo->mouse_face_end_row
9388 = dpyinfo->mouse_face_end_col = -1;
9389 dpyinfo->mouse_face_window = Qnil;
9390 dpyinfo->mouse_face_deferred_gc = 0;
9391 dpyinfo->mouse_face_mouse_frame = 0;
9394 UNBLOCK_INPUT;
9398 /* Destroy the X window of frame F. */
9400 void
9401 x_destroy_window (f)
9402 struct frame *f;
9404 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9406 /* If a display connection is dead, don't try sending more
9407 commands to the X server. */
9408 if (dpyinfo->display != 0)
9409 x_free_frame_resources (f);
9411 dpyinfo->reference_count--;
9415 /* Setting window manager hints. */
9417 /* Set the normal size hints for the window manager, for frame F.
9418 FLAGS is the flags word to use--or 0 meaning preserve the flags
9419 that the window now has.
9420 If USER_POSITION is nonzero, we set the USPosition
9421 flag (this is useful when FLAGS is 0).
9422 The GTK version is in gtkutils.c */
9424 #ifndef USE_GTK
9425 void
9426 x_wm_set_size_hint (f, flags, user_position)
9427 struct frame *f;
9428 long flags;
9429 int user_position;
9431 XSizeHints size_hints;
9433 #ifdef USE_X_TOOLKIT
9434 Arg al[2];
9435 int ac = 0;
9436 Dimension widget_width, widget_height;
9437 #endif
9439 Window window = FRAME_OUTER_WINDOW (f);
9441 /* Setting PMaxSize caused various problems. */
9442 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9444 size_hints.x = f->left_pos;
9445 size_hints.y = f->top_pos;
9447 #ifdef USE_X_TOOLKIT
9448 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9449 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9450 XtGetValues (f->output_data.x->widget, al, ac);
9451 size_hints.height = widget_height;
9452 size_hints.width = widget_width;
9453 #else /* not USE_X_TOOLKIT */
9454 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9455 size_hints.width = FRAME_PIXEL_WIDTH (f);
9456 #endif /* not USE_X_TOOLKIT */
9458 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9459 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9460 size_hints.max_width
9461 = FRAME_X_DISPLAY_INFO (f)->width - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9462 size_hints.max_height
9463 = FRAME_X_DISPLAY_INFO (f)->height - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9465 /* Calculate the base and minimum sizes.
9467 (When we use the X toolkit, we don't do it here.
9468 Instead we copy the values that the widgets are using, below.) */
9469 #ifndef USE_X_TOOLKIT
9471 int base_width, base_height;
9472 int min_rows = 0, min_cols = 0;
9474 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9475 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9477 check_frame_size (f, &min_rows, &min_cols);
9479 /* The window manager uses the base width hints to calculate the
9480 current number of rows and columns in the frame while
9481 resizing; min_width and min_height aren't useful for this
9482 purpose, since they might not give the dimensions for a
9483 zero-row, zero-column frame.
9485 We use the base_width and base_height members if we have
9486 them; otherwise, we set the min_width and min_height members
9487 to the size for a zero x zero frame. */
9489 #ifdef HAVE_X11R4
9490 size_hints.flags |= PBaseSize;
9491 size_hints.base_width = base_width;
9492 size_hints.base_height = base_height;
9493 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9494 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9495 #else
9496 size_hints.min_width = base_width;
9497 size_hints.min_height = base_height;
9498 #endif
9501 /* If we don't need the old flags, we don't need the old hint at all. */
9502 if (flags)
9504 size_hints.flags |= flags;
9505 goto no_read;
9507 #endif /* not USE_X_TOOLKIT */
9510 XSizeHints hints; /* Sometimes I hate X Windows... */
9511 long supplied_return;
9512 int value;
9514 #ifdef HAVE_X11R4
9515 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9516 &supplied_return);
9517 #else
9518 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
9519 #endif
9521 #ifdef USE_X_TOOLKIT
9522 size_hints.base_height = hints.base_height;
9523 size_hints.base_width = hints.base_width;
9524 size_hints.min_height = hints.min_height;
9525 size_hints.min_width = hints.min_width;
9526 #endif
9528 if (flags)
9529 size_hints.flags |= flags;
9530 else
9532 if (value == 0)
9533 hints.flags = 0;
9534 if (hints.flags & PSize)
9535 size_hints.flags |= PSize;
9536 if (hints.flags & PPosition)
9537 size_hints.flags |= PPosition;
9538 if (hints.flags & USPosition)
9539 size_hints.flags |= USPosition;
9540 if (hints.flags & USSize)
9541 size_hints.flags |= USSize;
9545 #ifndef USE_X_TOOLKIT
9546 no_read:
9547 #endif
9549 #ifdef PWinGravity
9550 size_hints.win_gravity = f->win_gravity;
9551 size_hints.flags |= PWinGravity;
9553 if (user_position)
9555 size_hints.flags &= ~ PPosition;
9556 size_hints.flags |= USPosition;
9558 #endif /* PWinGravity */
9560 #ifdef HAVE_X11R4
9561 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9562 #else
9563 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9564 #endif
9566 #endif /* not USE_GTK */
9568 /* Used for IconicState or NormalState */
9570 void
9571 x_wm_set_window_state (f, state)
9572 struct frame *f;
9573 int state;
9575 #ifdef USE_X_TOOLKIT
9576 Arg al[1];
9578 XtSetArg (al[0], XtNinitialState, state);
9579 XtSetValues (f->output_data.x->widget, al, 1);
9580 #else /* not USE_X_TOOLKIT */
9581 Window window = FRAME_X_WINDOW (f);
9583 f->output_data.x->wm_hints.flags |= StateHint;
9584 f->output_data.x->wm_hints.initial_state = state;
9586 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9587 #endif /* not USE_X_TOOLKIT */
9590 void
9591 x_wm_set_icon_pixmap (f, pixmap_id)
9592 struct frame *f;
9593 int pixmap_id;
9595 Pixmap icon_pixmap, icon_mask;
9597 #ifndef USE_X_TOOLKIT
9598 Window window = FRAME_OUTER_WINDOW (f);
9599 #endif
9601 if (pixmap_id > 0)
9603 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9604 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9605 icon_mask = x_bitmap_mask (f, pixmap_id);
9606 f->output_data.x->wm_hints.icon_mask = icon_mask;
9608 else
9610 /* It seems there is no way to turn off use of an icon pixmap.
9611 The following line does it, only if no icon has yet been created,
9612 for some window managers. But with mwm it crashes.
9613 Some people say it should clear the IconPixmapHint bit in this case,
9614 but that doesn't work, and the X consortium said it isn't the
9615 right thing at all. Since there is no way to win,
9616 best to explicitly give up. */
9617 #if 0
9618 f->output_data.x->wm_hints.icon_pixmap = None;
9619 f->output_data.x->wm_hints.icon_mask = None;
9620 #else
9621 return;
9622 #endif
9626 #ifdef USE_GTK
9628 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9629 return;
9632 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9635 Arg al[1];
9636 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9637 XtSetValues (f->output_data.x->widget, al, 1);
9638 XtSetArg (al[0], XtNiconMask, icon_mask);
9639 XtSetValues (f->output_data.x->widget, al, 1);
9642 #else /* not USE_X_TOOLKIT && not USE_GTK */
9644 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9645 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9647 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9650 void
9651 x_wm_set_icon_position (f, icon_x, icon_y)
9652 struct frame *f;
9653 int icon_x, icon_y;
9655 Window window = FRAME_OUTER_WINDOW (f);
9657 f->output_data.x->wm_hints.flags |= IconPositionHint;
9658 f->output_data.x->wm_hints.icon_x = icon_x;
9659 f->output_data.x->wm_hints.icon_y = icon_y;
9661 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9665 /***********************************************************************
9666 Fonts
9667 ***********************************************************************/
9669 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
9671 struct font_info *
9672 x_get_font_info (f, font_idx)
9673 FRAME_PTR f;
9674 int font_idx;
9676 return (FRAME_X_FONT_TABLE (f) + font_idx);
9680 /* Return a list of names of available fonts matching PATTERN on frame F.
9682 If SIZE is > 0, it is the size (maximum bounds width) of fonts
9683 to be listed.
9685 SIZE < 0 means include scalable fonts.
9687 Frame F null means we have not yet created any frame on X, and
9688 consult the first display in x_display_list. MAXNAMES sets a limit
9689 on how many fonts to match. */
9691 Lisp_Object
9692 x_list_fonts (f, pattern, size, maxnames)
9693 struct frame *f;
9694 Lisp_Object pattern;
9695 int size;
9696 int maxnames;
9698 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
9699 Lisp_Object tem, second_best;
9700 struct x_display_info *dpyinfo
9701 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
9702 Display *dpy = dpyinfo->display;
9703 int try_XLoadQueryFont = 0;
9704 int allow_auto_scaled_font = 0;
9706 if (size < 0)
9708 allow_auto_scaled_font = 1;
9709 size = 0;
9712 patterns = Fassoc (pattern, Valternate_fontname_alist);
9713 if (NILP (patterns))
9714 patterns = Fcons (pattern, Qnil);
9716 if (maxnames == 1 && !size)
9717 /* We can return any single font matching PATTERN. */
9718 try_XLoadQueryFont = 1;
9720 for (; CONSP (patterns); patterns = XCDR (patterns))
9722 int num_fonts;
9723 char **names = NULL;
9725 pattern = XCAR (patterns);
9726 /* See if we cached the result for this particular query.
9727 The cache is an alist of the form:
9728 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
9729 tem = XCDR (dpyinfo->name_list_element);
9730 key = Fcons (Fcons (pattern, make_number (maxnames)),
9731 allow_auto_scaled_font ? Qt : Qnil);
9732 list = Fassoc (key, tem);
9733 if (!NILP (list))
9735 list = Fcdr_safe (list);
9736 /* We have a cashed list. Don't have to get the list again. */
9737 goto label_cached;
9740 /* At first, put PATTERN in the cache. */
9742 BLOCK_INPUT;
9743 x_catch_errors (dpy);
9745 if (try_XLoadQueryFont)
9747 XFontStruct *font;
9748 unsigned long value;
9750 font = XLoadQueryFont (dpy, SDATA (pattern));
9751 if (x_had_errors_p (dpy))
9753 /* This error is perhaps due to insufficient memory on X
9754 server. Let's just ignore it. */
9755 font = NULL;
9756 x_clear_errors (dpy);
9759 if (font
9760 && XGetFontProperty (font, XA_FONT, &value))
9762 char *name = (char *) XGetAtomName (dpy, (Atom) value);
9763 int len = strlen (name);
9764 char *tmp;
9766 /* If DXPC (a Differential X Protocol Compressor)
9767 Ver.3.7 is running, XGetAtomName will return null
9768 string. We must avoid such a name. */
9769 if (len == 0)
9770 try_XLoadQueryFont = 0;
9771 else
9773 num_fonts = 1;
9774 names = (char **) alloca (sizeof (char *));
9775 /* Some systems only allow alloca assigned to a
9776 simple var. */
9777 tmp = (char *) alloca (len + 1); names[0] = tmp;
9778 bcopy (name, names[0], len + 1);
9779 XFree (name);
9782 else
9783 try_XLoadQueryFont = 0;
9785 if (font)
9786 XFreeFont (dpy, font);
9789 if (!try_XLoadQueryFont)
9791 /* We try at least 10 fonts because XListFonts will return
9792 auto-scaled fonts at the head. */
9793 if (maxnames < 0)
9795 int limit;
9797 for (limit = 500;;)
9799 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
9800 if (num_fonts == limit)
9802 BLOCK_INPUT;
9803 XFreeFontNames (names);
9804 UNBLOCK_INPUT;
9805 limit *= 2;
9807 else
9808 break;
9811 else
9812 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
9813 &num_fonts);
9815 if (x_had_errors_p (dpy))
9817 /* This error is perhaps due to insufficient memory on X
9818 server. Let's just ignore it. */
9819 names = NULL;
9820 x_clear_errors (dpy);
9824 x_uncatch_errors ();
9825 UNBLOCK_INPUT;
9827 if (names)
9829 int i;
9831 /* Make a list of all the fonts we got back.
9832 Store that in the font cache for the display. */
9833 for (i = 0; i < num_fonts; i++)
9835 int width = 0;
9836 char *p = names[i];
9837 int average_width = -1, resx = 0, dashes = 0;
9839 /* Count the number of dashes in NAMES[I]. If there are
9840 14 dashes, the field value following 9th dash
9841 (RESOLUTION_X) is nonzero, and the field value
9842 following 12th dash (AVERAGE_WIDTH) is 0, this is a
9843 auto-scaled font which is usually too ugly to be used
9844 for editing. Let's ignore it. */
9845 while (*p)
9846 if (*p++ == '-')
9848 dashes++;
9849 if (dashes == 7) /* PIXEL_SIZE field */
9850 width = atoi (p);
9851 else if (dashes == 9)
9852 resx = atoi (p);
9853 else if (dashes == 12) /* AVERAGE_WIDTH field */
9854 average_width = atoi (p);
9857 if (allow_auto_scaled_font
9858 || dashes < 14 || average_width != 0 || resx == 0)
9860 tem = build_string (names[i]);
9861 if (NILP (Fassoc (tem, list)))
9863 if (STRINGP (Vx_pixel_size_width_font_regexp)
9864 && ((fast_c_string_match_ignore_case
9865 (Vx_pixel_size_width_font_regexp, names[i]))
9866 >= 0))
9867 /* We can set the value of PIXEL_SIZE to the
9868 width of this font. */
9869 list = Fcons (Fcons (tem, make_number (width)), list);
9870 else
9871 /* For the moment, width is not known. */
9872 list = Fcons (Fcons (tem, Qnil), list);
9877 if (!try_XLoadQueryFont)
9879 BLOCK_INPUT;
9880 XFreeFontNames (names);
9881 UNBLOCK_INPUT;
9885 /* Now store the result in the cache. */
9886 XSETCDR (dpyinfo->name_list_element,
9887 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
9889 label_cached:
9890 if (NILP (list)) continue; /* Try the remaining alternatives. */
9892 newlist = second_best = Qnil;
9893 /* Make a list of the fonts that have the right width. */
9894 for (; CONSP (list); list = XCDR (list))
9896 int found_size;
9898 tem = XCAR (list);
9900 if (!CONSP (tem) || NILP (XCAR (tem)))
9901 continue;
9902 if (!size)
9904 newlist = Fcons (XCAR (tem), newlist);
9905 continue;
9908 if (!INTEGERP (XCDR (tem)))
9910 /* Since we have not yet known the size of this font, we
9911 must try slow function call XLoadQueryFont. */
9912 XFontStruct *thisinfo;
9914 BLOCK_INPUT;
9915 x_catch_errors (dpy);
9916 thisinfo = XLoadQueryFont (dpy,
9917 SDATA (XCAR (tem)));
9918 if (x_had_errors_p (dpy))
9920 /* This error is perhaps due to insufficient memory on X
9921 server. Let's just ignore it. */
9922 thisinfo = NULL;
9923 x_clear_errors (dpy);
9925 x_uncatch_errors ();
9926 UNBLOCK_INPUT;
9928 if (thisinfo)
9930 XSETCDR (tem,
9931 (thisinfo->min_bounds.width == 0
9932 ? make_number (0)
9933 : make_number (thisinfo->max_bounds.width)));
9934 BLOCK_INPUT;
9935 XFreeFont (dpy, thisinfo);
9936 UNBLOCK_INPUT;
9938 else
9939 /* For unknown reason, the previous call of XListFont had
9940 returned a font which can't be opened. Record the size
9941 as 0 not to try to open it again. */
9942 XSETCDR (tem, make_number (0));
9945 found_size = XINT (XCDR (tem));
9946 if (found_size == size)
9947 newlist = Fcons (XCAR (tem), newlist);
9948 else if (found_size > 0)
9950 if (NILP (second_best))
9951 second_best = tem;
9952 else if (found_size < size)
9954 if (XINT (XCDR (second_best)) > size
9955 || XINT (XCDR (second_best)) < found_size)
9956 second_best = tem;
9958 else
9960 if (XINT (XCDR (second_best)) > size
9961 && XINT (XCDR (second_best)) > found_size)
9962 second_best = tem;
9966 if (!NILP (newlist))
9967 break;
9968 else if (!NILP (second_best))
9970 newlist = Fcons (XCAR (second_best), Qnil);
9971 break;
9975 return newlist;
9979 #if GLYPH_DEBUG
9981 /* Check that FONT is valid on frame F. It is if it can be found in F's
9982 font table. */
9984 static void
9985 x_check_font (f, font)
9986 struct frame *f;
9987 XFontStruct *font;
9989 int i;
9990 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9992 xassert (font != NULL);
9994 for (i = 0; i < dpyinfo->n_fonts; i++)
9995 if (dpyinfo->font_table[i].name
9996 && font == dpyinfo->font_table[i].font)
9997 break;
9999 xassert (i < dpyinfo->n_fonts);
10002 #endif /* GLYPH_DEBUG != 0 */
10004 /* Set *W to the minimum width, *H to the minimum font height of FONT.
10005 Note: There are (broken) X fonts out there with invalid XFontStruct
10006 min_bounds contents. For example, handa@etl.go.jp reports that
10007 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
10008 have font->min_bounds.width == 0. */
10010 static INLINE void
10011 x_font_min_bounds (font, w, h)
10012 XFontStruct *font;
10013 int *w, *h;
10015 *h = FONT_HEIGHT (font);
10016 *w = font->min_bounds.width;
10018 /* Try to handle the case where FONT->min_bounds has invalid
10019 contents. Since the only font known to have invalid min_bounds
10020 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
10021 if (*w <= 0)
10022 *w = font->max_bounds.width;
10026 /* Compute the smallest character width and smallest font height over
10027 all fonts available on frame F. Set the members smallest_char_width
10028 and smallest_font_height in F's x_display_info structure to
10029 the values computed. Value is non-zero if smallest_font_height or
10030 smallest_char_width become smaller than they were before. */
10032 static int
10033 x_compute_min_glyph_bounds (f)
10034 struct frame *f;
10036 int i;
10037 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10038 XFontStruct *font;
10039 int old_width = dpyinfo->smallest_char_width;
10040 int old_height = dpyinfo->smallest_font_height;
10042 dpyinfo->smallest_font_height = 100000;
10043 dpyinfo->smallest_char_width = 100000;
10045 for (i = 0; i < dpyinfo->n_fonts; ++i)
10046 if (dpyinfo->font_table[i].name)
10048 struct font_info *fontp = dpyinfo->font_table + i;
10049 int w, h;
10051 font = (XFontStruct *) fontp->font;
10052 xassert (font != (XFontStruct *) ~0);
10053 x_font_min_bounds (font, &w, &h);
10055 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
10056 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
10059 xassert (dpyinfo->smallest_char_width > 0
10060 && dpyinfo->smallest_font_height > 0);
10062 return (dpyinfo->n_fonts == 1
10063 || dpyinfo->smallest_char_width < old_width
10064 || dpyinfo->smallest_font_height < old_height);
10068 /* Load font named FONTNAME of the size SIZE for frame F, and return a
10069 pointer to the structure font_info while allocating it dynamically.
10070 If SIZE is 0, load any size of font.
10071 If loading is failed, return NULL. */
10073 struct font_info *
10074 x_load_font (f, fontname, size)
10075 struct frame *f;
10076 register char *fontname;
10077 int size;
10079 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10080 Lisp_Object font_names;
10082 /* Get a list of all the fonts that match this name. Once we
10083 have a list of matching fonts, we compare them against the fonts
10084 we already have by comparing names. */
10085 font_names = x_list_fonts (f, build_string (fontname), size, 1);
10087 if (!NILP (font_names))
10089 Lisp_Object tail;
10090 int i;
10092 for (i = 0; i < dpyinfo->n_fonts; i++)
10093 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
10094 if (dpyinfo->font_table[i].name
10095 && (!strcmp (dpyinfo->font_table[i].name,
10096 SDATA (XCAR (tail)))
10097 || !strcmp (dpyinfo->font_table[i].full_name,
10098 SDATA (XCAR (tail)))))
10099 return (dpyinfo->font_table + i);
10102 /* Load the font and add it to the table. */
10104 char *full_name;
10105 XFontStruct *font;
10106 struct font_info *fontp;
10107 unsigned long value;
10108 int i;
10110 /* If we have found fonts by x_list_font, load one of them. If
10111 not, we still try to load a font by the name given as FONTNAME
10112 because XListFonts (called in x_list_font) of some X server has
10113 a bug of not finding a font even if the font surely exists and
10114 is loadable by XLoadQueryFont. */
10115 if (size > 0 && !NILP (font_names))
10116 fontname = (char *) SDATA (XCAR (font_names));
10118 BLOCK_INPUT;
10119 x_catch_errors (FRAME_X_DISPLAY (f));
10120 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
10121 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10123 /* This error is perhaps due to insufficient memory on X
10124 server. Let's just ignore it. */
10125 font = NULL;
10126 x_clear_errors (FRAME_X_DISPLAY (f));
10128 x_uncatch_errors ();
10129 UNBLOCK_INPUT;
10130 if (!font)
10131 return NULL;
10133 /* Find a free slot in the font table. */
10134 for (i = 0; i < dpyinfo->n_fonts; ++i)
10135 if (dpyinfo->font_table[i].name == NULL)
10136 break;
10138 /* If no free slot found, maybe enlarge the font table. */
10139 if (i == dpyinfo->n_fonts
10140 && dpyinfo->n_fonts == dpyinfo->font_table_size)
10142 int sz;
10143 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
10144 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
10145 dpyinfo->font_table
10146 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
10149 fontp = dpyinfo->font_table + i;
10150 if (i == dpyinfo->n_fonts)
10151 ++dpyinfo->n_fonts;
10153 /* Now fill in the slots of *FONTP. */
10154 BLOCK_INPUT;
10155 bzero (fontp, sizeof (*fontp));
10156 fontp->font = font;
10157 fontp->font_idx = i;
10158 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
10159 bcopy (fontname, fontp->name, strlen (fontname) + 1);
10161 if (font->min_bounds.width == font->max_bounds.width)
10163 /* Fixed width font. */
10164 fontp->average_width = fontp->space_width = font->min_bounds.width;
10166 else
10168 XChar2b char2b;
10169 XCharStruct *pcm;
10171 char2b.byte1 = 0x00, char2b.byte2 = 0x20;
10172 pcm = x_per_char_metric (font, &char2b, 0);
10173 if (pcm)
10174 fontp->space_width = pcm->width;
10175 else
10176 fontp->space_width = FONT_WIDTH (font);
10178 fontp->average_width
10179 = (XGetFontProperty (font, dpyinfo->Xatom_AVERAGE_WIDTH, &value)
10180 ? (long) value / 10 : 0);
10181 if (fontp->average_width < 0)
10182 fontp->average_width = - fontp->average_width;
10183 if (fontp->average_width == 0)
10185 if (pcm)
10187 int width = pcm->width;
10188 for (char2b.byte2 = 33; char2b.byte2 <= 126; char2b.byte2++)
10189 if ((pcm = x_per_char_metric (font, &char2b, 0)) != NULL)
10190 width += pcm->width;
10191 fontp->average_width = width / 95;
10193 else
10194 fontp->average_width = FONT_WIDTH (font);
10198 /* Try to get the full name of FONT. Put it in FULL_NAME. */
10199 full_name = 0;
10200 if (XGetFontProperty (font, XA_FONT, &value))
10202 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
10203 char *p = name;
10204 int dashes = 0;
10206 /* Count the number of dashes in the "full name".
10207 If it is too few, this isn't really the font's full name,
10208 so don't use it.
10209 In X11R4, the fonts did not come with their canonical names
10210 stored in them. */
10211 while (*p)
10213 if (*p == '-')
10214 dashes++;
10215 p++;
10218 if (dashes >= 13)
10220 full_name = (char *) xmalloc (p - name + 1);
10221 bcopy (name, full_name, p - name + 1);
10224 XFree (name);
10227 if (full_name != 0)
10228 fontp->full_name = full_name;
10229 else
10230 fontp->full_name = fontp->name;
10232 fontp->size = font->max_bounds.width;
10233 fontp->height = FONT_HEIGHT (font);
10235 if (NILP (font_names))
10237 /* We come here because of a bug of XListFonts mentioned at
10238 the head of this block. Let's store this information in
10239 the cache for x_list_fonts. */
10240 Lisp_Object lispy_name = build_string (fontname);
10241 Lisp_Object lispy_full_name = build_string (fontp->full_name);
10242 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
10243 Qnil);
10245 XSETCDR (dpyinfo->name_list_element,
10246 Fcons (Fcons (key,
10247 Fcons (Fcons (lispy_full_name,
10248 make_number (fontp->size)),
10249 Qnil)),
10250 XCDR (dpyinfo->name_list_element)));
10251 if (full_name)
10253 key = Fcons (Fcons (lispy_full_name, make_number (256)),
10254 Qnil);
10255 XSETCDR (dpyinfo->name_list_element,
10256 Fcons (Fcons (key,
10257 Fcons (Fcons (lispy_full_name,
10258 make_number (fontp->size)),
10259 Qnil)),
10260 XCDR (dpyinfo->name_list_element)));
10264 /* The slot `encoding' specifies how to map a character
10265 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
10266 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
10267 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
10268 2:0xA020..0xFF7F). For the moment, we don't know which charset
10269 uses this font. So, we set information in fontp->encoding[1]
10270 which is never used by any charset. If mapping can't be
10271 decided, set FONT_ENCODING_NOT_DECIDED. */
10272 fontp->encoding[1]
10273 = (font->max_byte1 == 0
10274 /* 1-byte font */
10275 ? (font->min_char_or_byte2 < 0x80
10276 ? (font->max_char_or_byte2 < 0x80
10277 ? 0 /* 0x20..0x7F */
10278 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
10279 : 1) /* 0xA0..0xFF */
10280 /* 2-byte font */
10281 : (font->min_byte1 < 0x80
10282 ? (font->max_byte1 < 0x80
10283 ? (font->min_char_or_byte2 < 0x80
10284 ? (font->max_char_or_byte2 < 0x80
10285 ? 0 /* 0x2020..0x7F7F */
10286 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
10287 : 3) /* 0x20A0..0x7FFF */
10288 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
10289 : (font->min_char_or_byte2 < 0x80
10290 ? (font->max_char_or_byte2 < 0x80
10291 ? 2 /* 0xA020..0xFF7F */
10292 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
10293 : 1))); /* 0xA0A0..0xFFFF */
10295 fontp->baseline_offset
10296 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
10297 ? (long) value : 0);
10298 fontp->relative_compose
10299 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
10300 ? (long) value : 0);
10301 fontp->default_ascent
10302 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
10303 ? (long) value : 0);
10305 /* Set global flag fonts_changed_p to non-zero if the font loaded
10306 has a character with a smaller width than any other character
10307 before, or if the font loaded has a smaller height than any
10308 other font loaded before. If this happens, it will make a
10309 glyph matrix reallocation necessary. */
10310 fonts_changed_p |= x_compute_min_glyph_bounds (f);
10311 UNBLOCK_INPUT;
10312 return fontp;
10317 /* Return a pointer to struct font_info of a font named FONTNAME for
10318 frame F. If no such font is loaded, return NULL. */
10320 struct font_info *
10321 x_query_font (f, fontname)
10322 struct frame *f;
10323 register char *fontname;
10325 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10326 int i;
10328 for (i = 0; i < dpyinfo->n_fonts; i++)
10329 if (dpyinfo->font_table[i].name
10330 && (!xstricmp (dpyinfo->font_table[i].name, fontname)
10331 || !xstricmp (dpyinfo->font_table[i].full_name, fontname)))
10332 return (dpyinfo->font_table + i);
10333 return NULL;
10337 /* Find a CCL program for a font specified by FONTP, and set the member
10338 `encoder' of the structure. */
10340 void
10341 x_find_ccl_program (fontp)
10342 struct font_info *fontp;
10344 Lisp_Object list, elt;
10346 elt = Qnil;
10347 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
10349 elt = XCAR (list);
10350 if (CONSP (elt)
10351 && STRINGP (XCAR (elt))
10352 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10353 >= 0)
10354 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10355 >= 0)))
10356 break;
10359 if (! NILP (list))
10361 struct ccl_program *ccl
10362 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
10364 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
10365 xfree (ccl);
10366 else
10367 fontp->font_encoder = ccl;
10373 /***********************************************************************
10374 Initialization
10375 ***********************************************************************/
10377 #ifdef USE_X_TOOLKIT
10378 static XrmOptionDescRec emacs_options[] = {
10379 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10380 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10382 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10383 XrmoptionSepArg, NULL},
10384 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10386 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10387 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10388 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10389 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10390 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10391 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10392 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10395 /* Whether atimer for Xt timeouts is activated or not. */
10397 static int x_timeout_atimer_activated_flag;
10399 #endif /* USE_X_TOOLKIT */
10401 static int x_initialized;
10403 #ifdef HAVE_X_SM
10404 static int x_session_initialized;
10405 #endif
10407 #ifdef MULTI_KBOARD
10408 /* Test whether two display-name strings agree up to the dot that separates
10409 the screen number from the server number. */
10410 static int
10411 same_x_server (name1, name2)
10412 const char *name1, *name2;
10414 int seen_colon = 0;
10415 const unsigned char *system_name = SDATA (Vsystem_name);
10416 int system_name_length = strlen (system_name);
10417 int length_until_period = 0;
10419 while (system_name[length_until_period] != 0
10420 && system_name[length_until_period] != '.')
10421 length_until_period++;
10423 /* Treat `unix' like an empty host name. */
10424 if (! strncmp (name1, "unix:", 5))
10425 name1 += 4;
10426 if (! strncmp (name2, "unix:", 5))
10427 name2 += 4;
10428 /* Treat this host's name like an empty host name. */
10429 if (! strncmp (name1, system_name, system_name_length)
10430 && name1[system_name_length] == ':')
10431 name1 += system_name_length;
10432 if (! strncmp (name2, system_name, system_name_length)
10433 && name2[system_name_length] == ':')
10434 name2 += system_name_length;
10435 /* Treat this host's domainless name like an empty host name. */
10436 if (! strncmp (name1, system_name, length_until_period)
10437 && name1[length_until_period] == ':')
10438 name1 += length_until_period;
10439 if (! strncmp (name2, system_name, length_until_period)
10440 && name2[length_until_period] == ':')
10441 name2 += length_until_period;
10443 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10445 if (*name1 == ':')
10446 seen_colon++;
10447 if (seen_colon && *name1 == '.')
10448 return 1;
10450 return (seen_colon
10451 && (*name1 == '.' || *name1 == '\0')
10452 && (*name2 == '.' || *name2 == '\0'));
10454 #endif
10456 /* Count number of set bits in mask and number of bits to shift to
10457 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10458 to 5. */
10459 static void
10460 get_bits_and_offset (mask, bits, offset)
10461 unsigned long mask;
10462 int *bits;
10463 int *offset;
10465 int nr = 0;
10466 int off = 0;
10468 while (!(mask & 1))
10470 off++;
10471 mask >>= 1;
10474 while (mask & 1)
10476 nr++;
10477 mask >>= 1;
10480 *offset = off;
10481 *bits = nr;
10485 x_display_ok (display)
10486 const char * display;
10488 int dpy_ok = 1;
10489 Display *dpy;
10491 dpy = XOpenDisplay (display);
10492 if (dpy)
10493 XCloseDisplay (dpy);
10494 else
10495 dpy_ok = 0;
10496 return dpy_ok;
10499 struct x_display_info *
10500 x_term_init (display_name, xrm_option, resource_name)
10501 Lisp_Object display_name;
10502 char *xrm_option;
10503 char *resource_name;
10505 int connection;
10506 Display *dpy;
10507 struct x_display_info *dpyinfo;
10508 XrmDatabase xrdb;
10510 BLOCK_INPUT;
10512 if (!x_initialized)
10514 x_initialize ();
10515 ++x_initialized;
10518 #ifdef USE_GTK
10520 #define NUM_ARGV 10
10521 int argc;
10522 char *argv[NUM_ARGV];
10523 char **argv2 = argv;
10524 GdkAtom atom;
10526 if (x_initialized++ > 1)
10528 /* Opening another display. If xg_display_open returns less
10529 than zero, we are probably on GTK 2.0, which can only handle
10530 one display. GTK 2.2 or later can handle more than one. */
10531 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10532 error ("Sorry, this version of GTK can only handle one display");
10534 else
10536 for (argc = 0; argc < NUM_ARGV; ++argc)
10537 argv[argc] = 0;
10539 argc = 0;
10540 argv[argc++] = initial_argv[0];
10542 if (! NILP (display_name))
10544 argv[argc++] = "--display";
10545 argv[argc++] = SDATA (display_name);
10548 argv[argc++] = "--name";
10549 argv[argc++] = resource_name;
10551 #ifdef HAVE_X11R5
10552 XSetLocaleModifiers ("");
10553 #endif
10555 gtk_init (&argc, &argv2);
10557 /* gtk_init does set_locale. We must fix locale after calling it. */
10558 fixup_locale ();
10559 xg_initialize ();
10561 dpy = GDK_DISPLAY ();
10563 /* NULL window -> events for all windows go to our function */
10564 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10566 /* Load our own gtkrc if it exists. */
10568 char *file = "~/.emacs.d/gtkrc";
10569 Lisp_Object s, abs_file;
10571 s = make_string (file, strlen (file));
10572 abs_file = Fexpand_file_name (s, Qnil);
10574 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10575 gtk_rc_parse (SDATA (abs_file));
10578 XSetErrorHandler (x_error_handler);
10579 XSetIOErrorHandler (x_io_error_quitter);
10582 #else /* not USE_GTK */
10583 #ifdef USE_X_TOOLKIT
10584 /* weiner@footloose.sps.mot.com reports that this causes
10585 errors with X11R5:
10586 X protocol error: BadAtom (invalid Atom parameter)
10587 on protocol request 18skiloaf.
10588 So let's not use it until R6. */
10589 #ifdef HAVE_X11XTR6
10590 XtSetLanguageProc (NULL, NULL, NULL);
10591 #endif
10594 int argc = 0;
10595 char *argv[3];
10597 argv[0] = "";
10598 argc = 1;
10599 if (xrm_option)
10601 argv[argc++] = "-xrm";
10602 argv[argc++] = xrm_option;
10604 turn_on_atimers (0);
10605 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10606 resource_name, EMACS_CLASS,
10607 emacs_options, XtNumber (emacs_options),
10608 &argc, argv);
10609 turn_on_atimers (1);
10611 #ifdef HAVE_X11XTR6
10612 /* I think this is to compensate for XtSetLanguageProc. */
10613 fixup_locale ();
10614 #endif
10617 #else /* not USE_X_TOOLKIT */
10618 #ifdef HAVE_X11R5
10619 XSetLocaleModifiers ("");
10620 #endif
10621 dpy = XOpenDisplay (SDATA (display_name));
10622 #endif /* not USE_X_TOOLKIT */
10623 #endif /* not USE_GTK*/
10625 /* Detect failure. */
10626 if (dpy == 0)
10628 UNBLOCK_INPUT;
10629 return 0;
10632 /* We have definitely succeeded. Record the new connection. */
10634 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10635 bzero (dpyinfo, sizeof *dpyinfo);
10637 #ifdef MULTI_KBOARD
10639 struct x_display_info *share;
10640 Lisp_Object tail;
10642 for (share = x_display_list, tail = x_display_name_list; share;
10643 share = share->next, tail = XCDR (tail))
10644 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10645 SDATA (display_name)))
10646 break;
10647 if (share)
10648 dpyinfo->kboard = share->kboard;
10649 else
10651 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10652 init_kboard (dpyinfo->kboard);
10653 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10655 char *vendor = ServerVendor (dpy);
10656 UNBLOCK_INPUT;
10657 dpyinfo->kboard->Vsystem_key_alist
10658 = call1 (Qvendor_specific_keysyms,
10659 build_string (vendor ? vendor : ""));
10660 BLOCK_INPUT;
10663 dpyinfo->kboard->next_kboard = all_kboards;
10664 all_kboards = dpyinfo->kboard;
10665 /* Don't let the initial kboard remain current longer than necessary.
10666 That would cause problems if a file loaded on startup tries to
10667 prompt in the mini-buffer. */
10668 if (current_kboard == initial_kboard)
10669 current_kboard = dpyinfo->kboard;
10671 dpyinfo->kboard->reference_count++;
10673 #endif
10675 /* Put this display on the chain. */
10676 dpyinfo->next = x_display_list;
10677 x_display_list = dpyinfo;
10679 /* Put it on x_display_name_list as well, to keep them parallel. */
10680 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10681 x_display_name_list);
10682 dpyinfo->name_list_element = XCAR (x_display_name_list);
10684 dpyinfo->display = dpy;
10686 #if 0
10687 XSetAfterFunction (x_current_display, x_trace_wire);
10688 #endif /* ! 0 */
10690 dpyinfo->x_id_name
10691 = (char *) xmalloc (SBYTES (Vinvocation_name)
10692 + SBYTES (Vsystem_name)
10693 + 2);
10694 sprintf (dpyinfo->x_id_name, "%s@%s",
10695 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10697 /* Figure out which modifier bits mean what. */
10698 x_find_modifier_meanings (dpyinfo);
10700 /* Get the scroll bar cursor. */
10701 #ifdef USE_GTK
10702 /* We must create a GTK cursor, it is required for GTK widgets. */
10703 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10704 #endif /* USE_GTK */
10706 dpyinfo->vertical_scroll_bar_cursor
10707 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10709 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10710 resource_name, EMACS_CLASS);
10711 #ifdef HAVE_XRMSETDATABASE
10712 XrmSetDatabase (dpyinfo->display, xrdb);
10713 #else
10714 dpyinfo->display->db = xrdb;
10715 #endif
10716 /* Put the rdb where we can find it in a way that works on
10717 all versions. */
10718 dpyinfo->xrdb = xrdb;
10720 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10721 DefaultScreen (dpyinfo->display));
10722 select_visual (dpyinfo);
10723 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10724 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
10725 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
10726 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10727 dpyinfo->client_leader_window = 0;
10728 dpyinfo->grabbed = 0;
10729 dpyinfo->reference_count = 0;
10730 dpyinfo->icon_bitmap_id = -1;
10731 dpyinfo->font_table = NULL;
10732 dpyinfo->n_fonts = 0;
10733 dpyinfo->font_table_size = 0;
10734 dpyinfo->bitmaps = 0;
10735 dpyinfo->bitmaps_size = 0;
10736 dpyinfo->bitmaps_last = 0;
10737 dpyinfo->scratch_cursor_gc = 0;
10738 dpyinfo->mouse_face_mouse_frame = 0;
10739 dpyinfo->mouse_face_deferred_gc = 0;
10740 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10741 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10742 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10743 dpyinfo->mouse_face_window = Qnil;
10744 dpyinfo->mouse_face_overlay = Qnil;
10745 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10746 dpyinfo->mouse_face_defer = 0;
10747 dpyinfo->mouse_face_hidden = 0;
10748 dpyinfo->x_focus_frame = 0;
10749 dpyinfo->x_focus_event_frame = 0;
10750 dpyinfo->x_highlight_frame = 0;
10751 dpyinfo->image_cache = make_image_cache ();
10752 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10754 /* See if we can construct pixel values from RGB values. */
10755 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10756 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10758 if (dpyinfo->visual->class == TrueColor)
10760 get_bits_and_offset (dpyinfo->visual->red_mask,
10761 &dpyinfo->red_bits, &dpyinfo->red_offset);
10762 get_bits_and_offset (dpyinfo->visual->blue_mask,
10763 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10764 get_bits_and_offset (dpyinfo->visual->green_mask,
10765 &dpyinfo->green_bits, &dpyinfo->green_offset);
10768 /* See if a private colormap is requested. */
10769 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10771 if (dpyinfo->visual->class == PseudoColor)
10773 Lisp_Object value;
10774 value = display_x_get_resource (dpyinfo,
10775 build_string ("privateColormap"),
10776 build_string ("PrivateColormap"),
10777 Qnil, Qnil);
10778 if (STRINGP (value)
10779 && (!strcmp (SDATA (value), "true")
10780 || !strcmp (SDATA (value), "on")))
10781 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10784 else
10785 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10786 dpyinfo->visual, AllocNone);
10789 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10790 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10791 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10792 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10793 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10794 pixels = DisplayWidth (dpyinfo->display, screen_number);
10795 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10796 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10797 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10800 dpyinfo->Xatom_wm_protocols
10801 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10802 dpyinfo->Xatom_wm_take_focus
10803 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10804 dpyinfo->Xatom_wm_save_yourself
10805 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10806 dpyinfo->Xatom_wm_delete_window
10807 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10808 dpyinfo->Xatom_wm_change_state
10809 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10810 dpyinfo->Xatom_wm_configure_denied
10811 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10812 dpyinfo->Xatom_wm_window_moved
10813 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10814 dpyinfo->Xatom_wm_client_leader
10815 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10816 dpyinfo->Xatom_editres
10817 = XInternAtom (dpyinfo->display, "Editres", False);
10818 dpyinfo->Xatom_CLIPBOARD
10819 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10820 dpyinfo->Xatom_TIMESTAMP
10821 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10822 dpyinfo->Xatom_TEXT
10823 = XInternAtom (dpyinfo->display, "TEXT", False);
10824 dpyinfo->Xatom_COMPOUND_TEXT
10825 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10826 dpyinfo->Xatom_UTF8_STRING
10827 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10828 dpyinfo->Xatom_DELETE
10829 = XInternAtom (dpyinfo->display, "DELETE", False);
10830 dpyinfo->Xatom_MULTIPLE
10831 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10832 dpyinfo->Xatom_INCR
10833 = XInternAtom (dpyinfo->display, "INCR", False);
10834 dpyinfo->Xatom_EMACS_TMP
10835 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10836 dpyinfo->Xatom_TARGETS
10837 = XInternAtom (dpyinfo->display, "TARGETS", False);
10838 dpyinfo->Xatom_NULL
10839 = XInternAtom (dpyinfo->display, "NULL", False);
10840 dpyinfo->Xatom_ATOM_PAIR
10841 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10842 /* For properties of font. */
10843 dpyinfo->Xatom_PIXEL_SIZE
10844 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10845 dpyinfo->Xatom_AVERAGE_WIDTH
10846 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10847 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10848 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10849 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10850 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10851 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10852 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10854 /* Ghostscript support. */
10855 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10856 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10858 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10859 False);
10861 dpyinfo->cut_buffers_initialized = 0;
10863 dpyinfo->x_dnd_atoms_size = 8;
10864 dpyinfo->x_dnd_atoms_length = 0;
10865 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10866 * dpyinfo->x_dnd_atoms_size);
10868 dpyinfo->net_supported_atoms = NULL;
10869 dpyinfo->nr_net_supported_atoms = 0;
10870 dpyinfo->net_supported_window = 0;
10872 connection = ConnectionNumber (dpyinfo->display);
10873 dpyinfo->connection = connection;
10876 char null_bits[1];
10878 null_bits[0] = 0x00;
10880 dpyinfo->null_pixel
10881 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10882 null_bits, 1, 1, (long) 0, (long) 0,
10887 extern int gray_bitmap_width, gray_bitmap_height;
10888 extern char *gray_bitmap_bits;
10889 dpyinfo->gray
10890 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10891 gray_bitmap_bits,
10892 gray_bitmap_width, gray_bitmap_height,
10893 (unsigned long) 1, (unsigned long) 0, 1);
10896 #ifdef HAVE_X_I18N
10897 xim_initialize (dpyinfo, resource_name);
10898 #endif
10900 #ifdef subprocesses
10901 /* This is only needed for distinguishing keyboard and process input. */
10902 if (connection != 0)
10903 add_keyboard_wait_descriptor (connection);
10904 #endif
10906 #ifndef F_SETOWN_BUG
10907 #ifdef F_SETOWN
10908 #ifdef F_SETOWN_SOCK_NEG
10909 /* stdin is a socket here */
10910 fcntl (connection, F_SETOWN, -getpid ());
10911 #else /* ! defined (F_SETOWN_SOCK_NEG) */
10912 fcntl (connection, F_SETOWN, getpid ());
10913 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
10914 #endif /* ! defined (F_SETOWN) */
10915 #endif /* F_SETOWN_BUG */
10917 #ifdef SIGIO
10918 if (interrupt_input)
10919 init_sigio (connection);
10920 #endif /* ! defined (SIGIO) */
10922 #ifdef USE_LUCID
10923 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
10924 /* Make sure that we have a valid font for dialog boxes
10925 so that Xt does not crash. */
10927 Display *dpy = dpyinfo->display;
10928 XrmValue d, fr, to;
10929 Font font;
10931 d.addr = (XPointer)&dpy;
10932 d.size = sizeof (Display *);
10933 fr.addr = XtDefaultFont;
10934 fr.size = sizeof (XtDefaultFont);
10935 to.size = sizeof (Font *);
10936 to.addr = (XPointer)&font;
10937 x_catch_errors (dpy);
10938 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10939 abort ();
10940 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10941 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10942 x_uncatch_errors ();
10944 #endif
10945 #endif
10947 /* See if we should run in synchronous mode. This is useful
10948 for debugging X code. */
10950 Lisp_Object value;
10951 value = display_x_get_resource (dpyinfo,
10952 build_string ("synchronous"),
10953 build_string ("Synchronous"),
10954 Qnil, Qnil);
10955 if (STRINGP (value)
10956 && (!strcmp (SDATA (value), "true")
10957 || !strcmp (SDATA (value), "on")))
10958 XSynchronize (dpyinfo->display, True);
10962 Lisp_Object value;
10963 value = display_x_get_resource (dpyinfo,
10964 build_string ("useXIM"),
10965 build_string ("UseXIM"),
10966 Qnil, Qnil);
10967 #ifdef USE_XIM
10968 if (STRINGP (value)
10969 && (!strcmp (XSTRING (value)->data, "false")
10970 || !strcmp (XSTRING (value)->data, "off")))
10971 use_xim = 0;
10972 #else
10973 if (STRINGP (value)
10974 && (!strcmp (XSTRING (value)->data, "true")
10975 || !strcmp (XSTRING (value)->data, "on")))
10976 use_xim = 1;
10977 #endif
10980 #ifdef HAVE_X_SM
10981 /* Only do this for the first display. */
10982 if (!x_session_initialized++)
10983 x_session_initialize (dpyinfo);
10984 #endif
10986 UNBLOCK_INPUT;
10988 return dpyinfo;
10991 /* Get rid of display DPYINFO, assuming all frames are already gone,
10992 and without sending any more commands to the X server. */
10994 void
10995 x_delete_display (dpyinfo)
10996 struct x_display_info *dpyinfo;
10998 int i;
11000 delete_keyboard_wait_descriptor (dpyinfo->connection);
11002 /* Discard this display from x_display_name_list and x_display_list.
11003 We can't use Fdelq because that can quit. */
11004 if (! NILP (x_display_name_list)
11005 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
11006 x_display_name_list = XCDR (x_display_name_list);
11007 else
11009 Lisp_Object tail;
11011 tail = x_display_name_list;
11012 while (CONSP (tail) && CONSP (XCDR (tail)))
11014 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
11016 XSETCDR (tail, XCDR (XCDR (tail)));
11017 break;
11019 tail = XCDR (tail);
11023 if (next_noop_dpyinfo == dpyinfo)
11024 next_noop_dpyinfo = dpyinfo->next;
11026 if (x_display_list == dpyinfo)
11027 x_display_list = dpyinfo->next;
11028 else
11030 struct x_display_info *tail;
11032 for (tail = x_display_list; tail; tail = tail->next)
11033 if (tail->next == dpyinfo)
11034 tail->next = tail->next->next;
11037 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
11038 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
11039 XrmDestroyDatabase (dpyinfo->xrdb);
11040 #endif
11041 #endif
11042 #ifdef MULTI_KBOARD
11043 if (--dpyinfo->kboard->reference_count == 0)
11044 delete_kboard (dpyinfo->kboard);
11045 #endif
11046 #ifdef HAVE_X_I18N
11047 if (dpyinfo->xim)
11048 xim_close_dpy (dpyinfo);
11049 #endif
11051 /* Free the font names in the font table. */
11052 for (i = 0; i < dpyinfo->n_fonts; i++)
11053 if (dpyinfo->font_table[i].name)
11055 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
11056 xfree (dpyinfo->font_table[i].full_name);
11057 xfree (dpyinfo->font_table[i].name);
11060 if (dpyinfo->font_table)
11062 if (dpyinfo->font_table->font_encoder)
11063 xfree (dpyinfo->font_table->font_encoder);
11064 xfree (dpyinfo->font_table);
11066 if (dpyinfo->x_id_name)
11067 xfree (dpyinfo->x_id_name);
11068 if (dpyinfo->color_cells)
11069 xfree (dpyinfo->color_cells);
11070 xfree (dpyinfo);
11073 #ifdef USE_X_TOOLKIT
11075 /* Atimer callback function for TIMER. Called every 0.1s to process
11076 Xt timeouts, if needed. We must avoid calling XtAppPending as
11077 much as possible because that function does an implicit XFlush
11078 that slows us down. */
11080 static void
11081 x_process_timeouts (timer)
11082 struct atimer *timer;
11084 BLOCK_INPUT;
11085 x_timeout_atimer_activated_flag = 0;
11086 if (toolkit_scroll_bar_interaction || popup_activated ())
11088 while (XtAppPending (Xt_app_con) & XtIMTimer)
11089 XtAppProcessEvent (Xt_app_con, XtIMTimer);
11090 /* Reactivate the atimer for next time. */
11091 x_activate_timeout_atimer ();
11093 UNBLOCK_INPUT;
11096 /* Install an asynchronous timer that processes Xt timeout events
11097 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
11098 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
11099 function whenever these variables are set. This is necessary
11100 because some widget sets use timeouts internally, for example the
11101 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
11102 processed, these widgets don't behave normally. */
11104 void
11105 x_activate_timeout_atimer ()
11107 BLOCK_INPUT;
11108 if (!x_timeout_atimer_activated_flag)
11110 EMACS_TIME interval;
11112 EMACS_SET_SECS_USECS (interval, 0, 100000);
11113 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
11114 x_timeout_atimer_activated_flag = 1;
11116 UNBLOCK_INPUT;
11119 #endif /* USE_X_TOOLKIT */
11122 /* Set up use of X before we make the first connection. */
11124 extern frame_parm_handler x_frame_parm_handlers[];
11126 static struct redisplay_interface x_redisplay_interface =
11128 x_frame_parm_handlers,
11129 x_produce_glyphs,
11130 x_write_glyphs,
11131 x_insert_glyphs,
11132 x_clear_end_of_line,
11133 x_scroll_run,
11134 x_after_update_window_line,
11135 x_update_window_begin,
11136 x_update_window_end,
11137 x_cursor_to,
11138 x_flush,
11139 #ifdef XFlush
11140 x_flush,
11141 #else
11142 0, /* flush_display_optional */
11143 #endif
11144 x_clear_window_mouse_face,
11145 x_get_glyph_overhangs,
11146 x_fix_overlapping_area,
11147 x_draw_fringe_bitmap,
11148 0, /* define_fringe_bitmap */
11149 0, /* destroy_fringe_bitmap */
11150 x_per_char_metric,
11151 x_encode_char,
11152 x_compute_glyph_string_overhangs,
11153 x_draw_glyph_string,
11154 x_define_frame_cursor,
11155 x_clear_frame_area,
11156 x_draw_window_cursor,
11157 x_draw_vertical_window_border,
11158 x_shift_glyphs_for_insert
11161 void
11162 x_initialize ()
11164 rif = &x_redisplay_interface;
11166 clear_frame_hook = x_clear_frame;
11167 ins_del_lines_hook = x_ins_del_lines;
11168 delete_glyphs_hook = x_delete_glyphs;
11169 ring_bell_hook = XTring_bell;
11170 reset_terminal_modes_hook = XTreset_terminal_modes;
11171 set_terminal_modes_hook = XTset_terminal_modes;
11172 update_begin_hook = x_update_begin;
11173 update_end_hook = x_update_end;
11174 set_terminal_window_hook = XTset_terminal_window;
11175 read_socket_hook = XTread_socket;
11176 frame_up_to_date_hook = XTframe_up_to_date;
11177 mouse_position_hook = XTmouse_position;
11178 frame_rehighlight_hook = XTframe_rehighlight;
11179 frame_raise_lower_hook = XTframe_raise_lower;
11180 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
11181 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
11182 redeem_scroll_bar_hook = XTredeem_scroll_bar;
11183 judge_scroll_bars_hook = XTjudge_scroll_bars;
11184 fullscreen_hook = XTfullscreen_hook;
11186 scroll_region_ok = 1; /* we'll scroll partial frames */
11187 char_ins_del_ok = 1;
11188 line_ins_del_ok = 1; /* we'll just blt 'em */
11189 fast_clear_end_of_line = 1; /* X does this well */
11190 memory_below_frame = 0; /* we don't remember what scrolls
11191 off the bottom */
11192 baud_rate = 19200;
11194 x_noop_count = 0;
11195 last_tool_bar_item = -1;
11196 any_help_event_p = 0;
11197 ignore_next_mouse_click_timeout = 0;
11198 #ifdef HAVE_X_SM
11199 x_session_initialized = 0;
11200 #endif
11202 #ifdef USE_GTK
11203 current_count = -1;
11204 #endif
11206 /* Try to use interrupt input; if we can't, then start polling. */
11207 Fset_input_mode (Qt, Qnil, Qt, Qnil);
11209 #ifdef USE_X_TOOLKIT
11210 XtToolkitInitialize ();
11212 Xt_app_con = XtCreateApplicationContext ();
11214 /* Register a converter from strings to pixels, which uses
11215 Emacs' color allocation infrastructure. */
11216 XtAppSetTypeConverter (Xt_app_con,
11217 XtRString, XtRPixel, cvt_string_to_pixel,
11218 cvt_string_to_pixel_args,
11219 XtNumber (cvt_string_to_pixel_args),
11220 XtCacheByDisplay, cvt_pixel_dtor);
11222 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
11223 #endif
11225 #ifdef USE_TOOLKIT_SCROLL_BARS
11226 #ifndef USE_GTK
11227 xaw3d_arrow_scroll = False;
11228 xaw3d_pick_top = True;
11229 #endif
11230 #endif
11232 /* Note that there is no real way portable across R3/R4 to get the
11233 original error handler. */
11234 XSetErrorHandler (x_error_handler);
11235 XSetIOErrorHandler (x_io_error_quitter);
11237 /* Disable Window Change signals; they are handled by X events. */
11238 #ifdef SIGWINCH
11239 signal (SIGWINCH, SIG_DFL);
11240 #endif /* SIGWINCH */
11242 signal (SIGPIPE, x_connection_signal);
11246 void
11247 syms_of_xterm ()
11249 x_error_message = NULL;
11251 staticpro (&x_display_name_list);
11252 x_display_name_list = Qnil;
11254 staticpro (&last_mouse_scroll_bar);
11255 last_mouse_scroll_bar = Qnil;
11257 staticpro (&Qvendor_specific_keysyms);
11258 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
11260 staticpro (&Qutf_8);
11261 Qutf_8 = intern ("utf-8");
11262 staticpro (&Qlatin_1);
11263 Qlatin_1 = intern ("latin-1");
11265 staticpro (&last_mouse_press_frame);
11266 last_mouse_press_frame = Qnil;
11268 DEFVAR_BOOL ("x-use-underline-position-properties",
11269 &x_use_underline_position_properties,
11270 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
11271 A value of nil means ignore them. If you encounter fonts with bogus
11272 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
11273 to 4.1, set this to nil. */);
11274 x_use_underline_position_properties = 1;
11276 DEFVAR_BOOL ("x-underline-at-descent-line",
11277 &x_underline_at_descent_line,
11278 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
11279 A value of nil means to draw the underline according to the value of the
11280 variable `x-use-underline-position-properties', which is usually at the
11281 baseline level. The default value is nil. */);
11282 x_underline_at_descent_line = 0;
11284 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
11285 &x_mouse_click_focus_ignore_position,
11286 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
11287 This variable is only used when the window manager requires that you
11288 click on a frame to select it (give it focus). In that case, a value
11289 of nil, means that the selected window and cursor position changes to
11290 reflect the mouse click position, while a non-nil value means that the
11291 selected window or cursor position is preserved. */);
11292 x_mouse_click_focus_ignore_position = 0;
11294 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
11295 doc: /* What X toolkit scroll bars Emacs uses.
11296 A value of nil means Emacs doesn't use X toolkit scroll bars.
11297 Otherwise, value is a symbol describing the X toolkit. */);
11298 #ifdef USE_TOOLKIT_SCROLL_BARS
11299 #ifdef USE_MOTIF
11300 Vx_toolkit_scroll_bars = intern ("motif");
11301 #elif defined HAVE_XAW3D
11302 Vx_toolkit_scroll_bars = intern ("xaw3d");
11303 #elif USE_GTK
11304 Vx_toolkit_scroll_bars = intern ("gtk");
11305 #else
11306 Vx_toolkit_scroll_bars = intern ("xaw");
11307 #endif
11308 #else
11309 Vx_toolkit_scroll_bars = Qnil;
11310 #endif
11312 staticpro (&last_mouse_motion_frame);
11313 last_mouse_motion_frame = Qnil;
11315 Qmodifier_value = intern ("modifier-value");
11316 Qalt = intern ("alt");
11317 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
11318 Qhyper = intern ("hyper");
11319 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11320 Qmeta = intern ("meta");
11321 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11322 Qsuper = intern ("super");
11323 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11325 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
11326 doc: /* Which keys Emacs uses for the alt modifier.
11327 This should be one of the symbols `alt', `hyper', `meta', `super'.
11328 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11329 is nil, which is the same as `alt'. */);
11330 Vx_alt_keysym = Qnil;
11332 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
11333 doc: /* Which keys Emacs uses for the hyper modifier.
11334 This should be one of the symbols `alt', `hyper', `meta', `super'.
11335 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11336 default is nil, which is the same as `hyper'. */);
11337 Vx_hyper_keysym = Qnil;
11339 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
11340 doc: /* Which keys Emacs uses for the meta modifier.
11341 This should be one of the symbols `alt', `hyper', `meta', `super'.
11342 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11343 default is nil, which is the same as `meta'. */);
11344 Vx_meta_keysym = Qnil;
11346 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
11347 doc: /* Which keys Emacs uses for the super modifier.
11348 This should be one of the symbols `alt', `hyper', `meta', `super'.
11349 For example, `super' means use the Super_L and Super_R keysyms. The
11350 default is nil, which is the same as `super'. */);
11351 Vx_super_keysym = Qnil;
11353 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
11354 doc: /* Hash table of character codes indexed by X keysym codes. */);
11355 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
11356 make_float (DEFAULT_REHASH_SIZE),
11357 make_float (DEFAULT_REHASH_THRESHOLD),
11358 Qnil, Qnil, Qnil);
11361 #endif /* HAVE_X_WINDOWS */
11363 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
11364 (do not change this comment) */