Merge from mainline.
[emacs.git] / src / xterm.c
blobb541683e5ad088ad6270157747a3a957fe293708
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
22 /* Xt features made by Fred Pierresteguy. */
24 #include <config.h>
26 /* On 4.3 these lose if they come after xterm.h. */
27 /* Putting these at the beginning seems to be standard for other .c files. */
28 #include <signal.h>
30 #include <stdio.h>
31 #include <setjmp.h>
33 #ifdef HAVE_X_WINDOWS
35 #include "lisp.h"
36 #include "blockinput.h"
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
57 #include "systime.h"
59 #ifndef INCLUDED_FCNTL
60 #include <fcntl.h>
61 #endif
62 #include <ctype.h>
63 #include <errno.h>
64 #include <setjmp.h>
65 #include <sys/stat.h>
66 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
67 /* #include <sys/param.h> */
69 #include "charset.h"
70 #include "character.h"
71 #include "coding.h"
72 #include "frame.h"
73 #include "dispextern.h"
74 #include "fontset.h"
75 #include "termhooks.h"
76 #include "termopts.h"
77 #include "termchar.h"
78 #include "emacs-icon.h"
79 #include "disptab.h"
80 #include "buffer.h"
81 #include "window.h"
82 #include "keyboard.h"
83 #include "intervals.h"
84 #include "process.h"
85 #include "atimer.h"
86 #include "keymap.h"
87 #include "font.h"
88 #include "fontset.h"
89 #include "xsettings.h"
90 #include "xgselect.h"
91 #include "sysselect.h"
93 #ifdef USE_X_TOOLKIT
94 #include <X11/Shell.h>
95 #endif
97 #ifdef HAVE_SYS_TIME_H
98 #include <sys/time.h>
99 #endif
100 #ifdef HAVE_UNISTD_H
101 #include <unistd.h>
102 #endif
104 #ifdef USE_GTK
105 #include "gtkutil.h"
106 #endif
108 #ifdef USE_LUCID
109 extern int xlwmenu_window_p P_ ((Widget w, Window window));
110 extern void xlwmenu_redisplay P_ ((Widget));
111 #endif
113 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
115 extern void free_frame_menubar P_ ((struct frame *));
116 #endif
118 #ifdef USE_X_TOOLKIT
119 #if !defined(NO_EDITRES)
120 #define HACK_EDITRES
121 extern void _XEditResCheckMessages ();
122 #endif /* not NO_EDITRES */
124 /* Include toolkit specific headers for the scroll bar widget. */
126 #ifdef USE_TOOLKIT_SCROLL_BARS
127 #if defined USE_MOTIF
128 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
129 #include <Xm/ScrollBar.h>
130 #else /* !USE_MOTIF i.e. use Xaw */
132 #ifdef HAVE_XAW3D
133 #include <X11/Xaw3d/Simple.h>
134 #include <X11/Xaw3d/Scrollbar.h>
135 #include <X11/Xaw3d/ThreeD.h>
136 #else /* !HAVE_XAW3D */
137 #include <X11/Xaw/Simple.h>
138 #include <X11/Xaw/Scrollbar.h>
139 #endif /* !HAVE_XAW3D */
140 #ifndef XtNpickTop
141 #define XtNpickTop "pickTop"
142 #endif /* !XtNpickTop */
143 #endif /* !USE_MOTIF */
144 #endif /* USE_TOOLKIT_SCROLL_BARS */
146 #endif /* USE_X_TOOLKIT */
148 #ifdef USE_X_TOOLKIT
149 #include "widget.h"
150 #ifndef XtNinitialState
151 #define XtNinitialState "initialState"
152 #endif
153 #endif
155 /* Default to using XIM if available. */
156 #ifdef USE_XIM
157 int use_xim = 1;
158 #else
159 int use_xim = 0; /* configure --without-xim */
160 #endif
164 /* Non-nil means Emacs uses toolkit scroll bars. */
166 Lisp_Object Vx_toolkit_scroll_bars;
168 /* Non-zero means that a HELP_EVENT has been generated since Emacs
169 start. */
171 static int any_help_event_p;
173 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
174 static Lisp_Object last_window;
176 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
178 int x_use_underline_position_properties;
180 /* Non-zero means to draw the underline at the same place as the descent line. */
182 int x_underline_at_descent_line;
184 /* This is a chain of structures for all the X displays currently in
185 use. */
187 struct x_display_info *x_display_list;
189 /* This is a list of cons cells, each of the form (NAME
190 . FONT-LIST-CACHE), one for each element of x_display_list and in
191 the same order. NAME is the name of the frame. FONT-LIST-CACHE
192 records previous values returned by x-list-fonts. */
194 Lisp_Object x_display_name_list;
196 /* Frame being updated by update_frame. This is declared in term.c.
197 This is set by update_begin and looked at by all the XT functions.
198 It is zero while not inside an update. In that case, the XT
199 functions assume that `selected_frame' is the frame to apply to. */
201 extern struct frame *updating_frame;
203 /* This is a frame waiting to be auto-raised, within XTread_socket. */
205 static struct frame *pending_autoraise_frame;
207 /* This is a frame waiting for an event matching mask, within XTread_socket. */
209 static struct {
210 struct frame *f;
211 int eventtype;
212 } pending_event_wait;
214 #ifdef USE_X_TOOLKIT
215 /* The application context for Xt use. */
216 XtAppContext Xt_app_con;
217 static String Xt_default_resources[] = {0};
218 #endif /* USE_X_TOOLKIT */
220 /* Non-zero means user is interacting with a toolkit scroll bar. */
222 static int toolkit_scroll_bar_interaction;
224 /* Non-zero means to not move point as a result of clicking on a
225 frame to focus it (when focus-follows-mouse is nil). */
227 int x_mouse_click_focus_ignore_position;
229 /* Non-zero timeout value means ignore next mouse click if it arrives
230 before that timeout elapses (i.e. as part of the same sequence of
231 events resulting from clicking on a frame to select it). */
233 static unsigned long ignore_next_mouse_click_timeout;
235 /* Mouse movement.
237 Formerly, we used PointerMotionHintMask (in standard_event_mask)
238 so that we would have to call XQueryPointer after each MotionNotify
239 event to ask for another such event. However, this made mouse tracking
240 slow, and there was a bug that made it eventually stop.
242 Simply asking for MotionNotify all the time seems to work better.
244 In order to avoid asking for motion events and then throwing most
245 of them away or busy-polling the server for mouse positions, we ask
246 the server for pointer motion hints. This means that we get only
247 one event per group of mouse movements. "Groups" are delimited by
248 other kinds of events (focus changes and button clicks, for
249 example), or by XQueryPointer calls; when one of these happens, we
250 get another MotionNotify event the next time the mouse moves. This
251 is at least as efficient as getting motion events when mouse
252 tracking is on, and I suspect only negligibly worse when tracking
253 is off. */
255 /* Where the mouse was last time we reported a mouse event. */
257 static XRectangle last_mouse_glyph;
258 static FRAME_PTR last_mouse_glyph_frame;
259 static Lisp_Object last_mouse_press_frame;
261 /* The scroll bar in which the last X motion event occurred.
263 If the last X motion event occurred in a scroll bar, we set this so
264 XTmouse_position can know whether to report a scroll bar motion or
265 an ordinary motion.
267 If the last X motion event didn't occur in a scroll bar, we set
268 this to Qnil, to tell XTmouse_position to return an ordinary motion
269 event. */
271 static Lisp_Object last_mouse_scroll_bar;
273 /* This is a hack. We would really prefer that XTmouse_position would
274 return the time associated with the position it returns, but there
275 doesn't seem to be any way to wrest the time-stamp from the server
276 along with the position query. So, we just keep track of the time
277 of the last movement we received, and return that in hopes that
278 it's somewhat accurate. */
280 static Time last_mouse_movement_time;
282 /* Time for last user interaction as returned in X events. */
284 static Time last_user_time;
286 /* Incremented by XTread_socket whenever it really tries to read
287 events. */
289 #ifdef __STDC__
290 static int volatile input_signal_count;
291 #else
292 static int input_signal_count;
293 #endif
295 /* Used locally within XTread_socket. */
297 static int x_noop_count;
299 /* Initial values of argv and argc. */
301 extern char **initial_argv;
302 extern int initial_argc;
304 extern Lisp_Object Vcommand_line_args, Vsystem_name;
306 /* Tells if a window manager is present or not. */
308 extern Lisp_Object Vx_no_window_manager;
310 extern Lisp_Object Qeql;
312 extern int errno;
314 /* A mask of extra modifier bits to put into every keyboard char. */
316 extern EMACS_INT extra_keyboard_modifiers;
318 /* The keysyms to use for the various modifiers. */
320 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
321 Lisp_Object Vx_keysym_table;
322 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
324 static Lisp_Object Qvendor_specific_keysyms;
325 static Lisp_Object Qlatin_1;
327 #ifdef USE_GTK
328 /* The name of the Emacs icon file. */
329 static Lisp_Object xg_default_icon_file;
331 /* Used in gtkutil.c. */
332 Lisp_Object Qx_gtk_map_stock;
333 #endif
335 /* Used in x_flush. */
337 extern Lisp_Object Vinhibit_redisplay;
339 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
340 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
342 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
343 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
344 static const XColor *x_color_cells P_ ((Display *, int *));
345 static void x_update_window_end P_ ((struct window *, int, int));
347 static int x_io_error_quitter P_ ((Display *));
348 static struct terminal *x_create_terminal P_ ((struct x_display_info *));
349 void x_delete_terminal P_ ((struct terminal *));
350 static void x_update_end P_ ((struct frame *));
351 static void XTframe_up_to_date P_ ((struct frame *));
352 static void XTset_terminal_modes P_ ((struct terminal *));
353 static void XTreset_terminal_modes P_ ((struct terminal *));
354 static void x_clear_frame P_ ((struct frame *));
355 static void frame_highlight P_ ((struct frame *));
356 static void frame_unhighlight P_ ((struct frame *));
357 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
358 static void x_focus_changed P_ ((int, int, struct x_display_info *,
359 struct frame *, struct input_event *));
360 static void x_detect_focus_change P_ ((struct x_display_info *,
361 XEvent *, struct input_event *));
362 static void XTframe_rehighlight P_ ((struct frame *));
363 static void x_frame_rehighlight P_ ((struct x_display_info *));
364 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
365 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
366 enum text_cursor_kinds));
368 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
369 static void x_flush P_ ((struct frame *f));
370 static void x_update_begin P_ ((struct frame *));
371 static void x_update_window_begin P_ ((struct window *));
372 static void x_after_update_window_line P_ ((struct glyph_row *));
373 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
374 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
375 enum scroll_bar_part *,
376 Lisp_Object *, Lisp_Object *,
377 unsigned long *));
378 static void x_handle_net_wm_state P_ ((struct frame *, XPropertyEvent *));
379 static void x_check_fullscreen P_ ((struct frame *));
380 static void x_check_expected_move P_ ((struct frame *, int, int));
381 static void x_sync_with_move P_ ((struct frame *, int, int, int));
382 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
383 int *, struct input_event *));
384 /* Don't declare this NO_RETURN because we want no
385 interference with debugging failing X calls. */
386 static SIGTYPE x_connection_closed P_ ((Display *, char *));
389 /* Flush display of frame F, or of all frames if F is null. */
391 static void
392 x_flush (f)
393 struct frame *f;
395 /* Don't call XFlush when it is not safe to redisplay; the X
396 connection may be broken. */
397 if (!NILP (Vinhibit_redisplay))
398 return;
400 BLOCK_INPUT;
401 if (f == NULL)
403 Lisp_Object rest, frame;
404 FOR_EACH_FRAME (rest, frame)
405 if (FRAME_X_P (XFRAME (frame)))
406 x_flush (XFRAME (frame));
408 else if (FRAME_X_P (f))
409 XFlush (FRAME_X_DISPLAY (f));
410 UNBLOCK_INPUT;
414 /* Remove calls to XFlush by defining XFlush to an empty replacement.
415 Calls to XFlush should be unnecessary because the X output buffer
416 is flushed automatically as needed by calls to XPending,
417 XNextEvent, or XWindowEvent according to the XFlush man page.
418 XTread_socket calls XPending. Removing XFlush improves
419 performance. */
421 #define XFlush(DISPLAY) (void) 0
424 /***********************************************************************
425 Debugging
426 ***********************************************************************/
428 #if 0
430 /* This is a function useful for recording debugging information about
431 the sequence of occurrences in this file. */
433 struct record
435 char *locus;
436 int type;
439 struct record event_record[100];
441 int event_record_index;
443 record_event (locus, type)
444 char *locus;
445 int type;
447 if (event_record_index == sizeof (event_record) / sizeof (struct record))
448 event_record_index = 0;
450 event_record[event_record_index].locus = locus;
451 event_record[event_record_index].type = type;
452 event_record_index++;
455 #endif /* 0 */
459 /* Return the struct x_display_info corresponding to DPY. */
461 struct x_display_info *
462 x_display_info_for_display (dpy)
463 Display *dpy;
465 struct x_display_info *dpyinfo;
467 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
468 if (dpyinfo->display == dpy)
469 return dpyinfo;
471 return 0;
474 #define OPAQUE 0xffffffff
475 #define OPACITY "_NET_WM_WINDOW_OPACITY"
477 void
478 x_set_frame_alpha (f)
479 struct frame *f;
481 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
482 Display *dpy = FRAME_X_DISPLAY (f);
483 Window win = FRAME_OUTER_WINDOW (f);
484 double alpha = 1.0;
485 double alpha_min = 1.0;
486 unsigned long opac;
488 if (FRAME_X_DISPLAY_INFO (f)->root_window != FRAME_X_OUTPUT (f)->parent_desc)
489 /* Since the WM decoration lies under the FRAME_OUTER_WINDOW,
490 we must treat the former instead of the latter. */
491 win = FRAME_X_OUTPUT(f)->parent_desc;
493 if (dpyinfo->x_highlight_frame == f)
494 alpha = f->alpha[0];
495 else
496 alpha = f->alpha[1];
498 if (FLOATP (Vframe_alpha_lower_limit))
499 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
500 else if (INTEGERP (Vframe_alpha_lower_limit))
501 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
503 if (alpha < 0.0)
504 return;
505 else if (alpha > 1.0)
506 alpha = 1.0;
507 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
508 alpha = alpha_min;
510 opac = alpha * OPAQUE;
512 /* return unless necessary */
514 unsigned char *data;
515 Atom actual;
516 int rc, format;
517 unsigned long n, left;
519 x_catch_errors (dpy);
520 rc = XGetWindowProperty(dpy, win, XInternAtom(dpy, OPACITY, False),
521 0L, 1L, False, XA_CARDINAL,
522 &actual, &format, &n, &left,
523 &data);
525 if (rc == Success && actual != None)
526 if (*(unsigned long *)data == opac)
528 XFree ((void *) data);
529 x_uncatch_errors ();
530 return;
532 else
533 XFree ((void *) data);
534 x_uncatch_errors ();
537 x_catch_errors (dpy);
538 XChangeProperty (dpy, win, XInternAtom (dpy, OPACITY, False),
539 XA_CARDINAL, 32, PropModeReplace,
540 (unsigned char *) &opac, 1L);
541 x_uncatch_errors ();
545 x_display_pixel_height (dpyinfo)
546 struct x_display_info *dpyinfo;
548 return HeightOfScreen (dpyinfo->screen);
552 x_display_pixel_width (dpyinfo)
553 struct x_display_info *dpyinfo;
555 return WidthOfScreen (dpyinfo->screen);
559 /***********************************************************************
560 Starting and ending an update
561 ***********************************************************************/
563 /* Start an update of frame F. This function is installed as a hook
564 for update_begin, i.e. it is called when update_begin is called.
565 This function is called prior to calls to x_update_window_begin for
566 each window being updated. Currently, there is nothing to do here
567 because all interesting stuff is done on a window basis. */
569 static void
570 x_update_begin (f)
571 struct frame *f;
573 /* Nothing to do. */
577 /* Start update of window W. Set the global variable updated_window
578 to the window being updated and set output_cursor to the cursor
579 position of W. */
581 static void
582 x_update_window_begin (w)
583 struct window *w;
585 struct frame *f = XFRAME (WINDOW_FRAME (w));
586 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
588 updated_window = w;
589 set_output_cursor (&w->cursor);
591 BLOCK_INPUT;
593 if (f == display_info->mouse_face_mouse_frame)
595 /* Don't do highlighting for mouse motion during the update. */
596 display_info->mouse_face_defer = 1;
598 /* If F needs to be redrawn, simply forget about any prior mouse
599 highlighting. */
600 if (FRAME_GARBAGED_P (f))
601 display_info->mouse_face_window = Qnil;
604 UNBLOCK_INPUT;
608 /* Draw a vertical window border from (x,y0) to (x,y1) */
610 static void
611 x_draw_vertical_window_border (w, x, y0, y1)
612 struct window *w;
613 int x, y0, y1;
615 struct frame *f = XFRAME (WINDOW_FRAME (w));
616 struct face *face;
618 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
619 if (face)
620 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
621 face->foreground);
623 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
624 f->output_data.x->normal_gc, x, y0, x, y1);
627 /* End update of window W (which is equal to updated_window).
629 Draw vertical borders between horizontally adjacent windows, and
630 display W's cursor if CURSOR_ON_P is non-zero.
632 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
633 glyphs in mouse-face were overwritten. In that case we have to
634 make sure that the mouse-highlight is properly redrawn.
636 W may be a menu bar pseudo-window in case we don't have X toolkit
637 support. Such windows don't have a cursor, so don't display it
638 here. */
640 static void
641 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
642 struct window *w;
643 int cursor_on_p, mouse_face_overwritten_p;
645 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
647 if (!w->pseudo_window_p)
649 BLOCK_INPUT;
651 if (cursor_on_p)
652 display_and_set_cursor (w, 1, output_cursor.hpos,
653 output_cursor.vpos,
654 output_cursor.x, output_cursor.y);
656 if (draw_window_fringes (w, 1))
657 x_draw_vertical_border (w);
659 UNBLOCK_INPUT;
662 /* If a row with mouse-face was overwritten, arrange for
663 XTframe_up_to_date to redisplay the mouse highlight. */
664 if (mouse_face_overwritten_p)
666 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
667 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
668 dpyinfo->mouse_face_window = Qnil;
671 updated_window = NULL;
675 /* End update of frame F. This function is installed as a hook in
676 update_end. */
678 static void
679 x_update_end (f)
680 struct frame *f;
682 /* Mouse highlight may be displayed again. */
683 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
685 #ifndef XFlush
686 BLOCK_INPUT;
687 XFlush (FRAME_X_DISPLAY (f));
688 UNBLOCK_INPUT;
689 #endif
693 /* This function is called from various places in xdisp.c whenever a
694 complete update has been performed. The global variable
695 updated_window is not available here. */
697 static void
698 XTframe_up_to_date (f)
699 struct frame *f;
701 if (FRAME_X_P (f))
703 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
705 if (dpyinfo->mouse_face_deferred_gc
706 || f == dpyinfo->mouse_face_mouse_frame)
708 BLOCK_INPUT;
709 if (dpyinfo->mouse_face_mouse_frame)
710 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
711 dpyinfo->mouse_face_mouse_x,
712 dpyinfo->mouse_face_mouse_y);
713 dpyinfo->mouse_face_deferred_gc = 0;
714 UNBLOCK_INPUT;
720 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
721 arrow bitmaps, or clear the fringes if no bitmaps are required
722 before DESIRED_ROW is made current. The window being updated is
723 found in updated_window. This function It is called from
724 update_window_line only if it is known that there are differences
725 between bitmaps to be drawn between current row and DESIRED_ROW. */
727 static void
728 x_after_update_window_line (desired_row)
729 struct glyph_row *desired_row;
731 struct window *w = updated_window;
732 struct frame *f;
733 int width, height;
735 xassert (w);
737 if (!desired_row->mode_line_p && !w->pseudo_window_p)
738 desired_row->redraw_fringe_bitmaps_p = 1;
740 /* When a window has disappeared, make sure that no rest of
741 full-width rows stays visible in the internal border. Could
742 check here if updated_window is the leftmost/rightmost window,
743 but I guess it's not worth doing since vertically split windows
744 are almost never used, internal border is rarely set, and the
745 overhead is very small. */
746 if (windows_or_buffers_changed
747 && desired_row->full_width_p
748 && (f = XFRAME (w->frame),
749 width = FRAME_INTERNAL_BORDER_WIDTH (f),
750 width != 0)
751 && (height = desired_row->visible_height,
752 height > 0))
754 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
756 BLOCK_INPUT;
757 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
758 0, y, width, height, False);
759 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
760 FRAME_PIXEL_WIDTH (f) - width,
761 y, width, height, False);
762 UNBLOCK_INPUT;
766 static void
767 x_draw_fringe_bitmap (w, row, p)
768 struct window *w;
769 struct glyph_row *row;
770 struct draw_fringe_bitmap_params *p;
772 struct frame *f = XFRAME (WINDOW_FRAME (w));
773 Display *display = FRAME_X_DISPLAY (f);
774 Window window = FRAME_X_WINDOW (f);
775 GC gc = f->output_data.x->normal_gc;
776 struct face *face = p->face;
777 int rowY;
779 /* Must clip because of partially visible lines. */
780 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
781 if (p->y < rowY)
783 /* Adjust position of "bottom aligned" bitmap on partially
784 visible last row. */
785 int oldY = row->y;
786 int oldVH = row->visible_height;
787 row->visible_height = p->h;
788 row->y -= rowY - p->y;
789 x_clip_to_row (w, row, -1, gc);
790 row->y = oldY;
791 row->visible_height = oldVH;
793 else
794 x_clip_to_row (w, row, -1, gc);
796 if (!p->overlay_p)
798 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
800 /* In case the same realized face is used for fringes and
801 for something displayed in the text (e.g. face `region' on
802 mono-displays, the fill style may have been changed to
803 FillSolid in x_draw_glyph_string_background. */
804 if (face->stipple)
805 XSetFillStyle (display, face->gc, FillOpaqueStippled);
806 else
807 XSetForeground (display, face->gc, face->background);
809 #ifdef USE_TOOLKIT_SCROLL_BARS
810 /* If the fringe is adjacent to the left (right) scroll bar of a
811 leftmost (rightmost, respectively) window, then extend its
812 background to the gap between the fringe and the bar. */
813 if ((WINDOW_LEFTMOST_P (w)
814 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
815 || (WINDOW_RIGHTMOST_P (w)
816 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
818 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
820 if (sb_width > 0)
822 int left = WINDOW_SCROLL_BAR_AREA_X (w);
823 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
824 * FRAME_COLUMN_WIDTH (f));
826 if (bx < 0)
828 /* Bitmap fills the fringe. */
829 if (left + width == p->x)
830 bx = left + sb_width;
831 else if (p->x + p->wd == left)
832 bx = left;
833 if (bx >= 0)
835 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
837 nx = width - sb_width;
838 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
839 row->y));
840 ny = row->visible_height;
843 else
845 if (left + width == bx)
847 bx = left + sb_width;
848 nx += width - sb_width;
850 else if (bx + nx == left)
851 nx += width - sb_width;
855 #endif
856 if (bx >= 0 && nx > 0)
857 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
859 if (!face->stipple)
860 XSetForeground (display, face->gc, face->foreground);
863 if (p->which)
865 unsigned char *bits;
866 Pixmap pixmap, clipmask = (Pixmap) 0;
867 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
868 XGCValues gcv;
870 if (p->wd > 8)
871 bits = (unsigned char *)(p->bits + p->dh);
872 else
873 bits = (unsigned char *)p->bits + p->dh;
875 /* Draw the bitmap. I believe these small pixmaps can be cached
876 by the server. */
877 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
878 (p->cursor_p
879 ? (p->overlay_p ? face->background
880 : f->output_data.x->cursor_pixel)
881 : face->foreground),
882 face->background, depth);
884 if (p->overlay_p)
886 clipmask = XCreatePixmapFromBitmapData (display,
887 FRAME_X_DISPLAY_INFO (f)->root_window,
888 bits, p->wd, p->h,
889 1, 0, 1);
890 gcv.clip_mask = clipmask;
891 gcv.clip_x_origin = p->x;
892 gcv.clip_y_origin = p->y;
893 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
896 XCopyArea (display, pixmap, window, gc, 0, 0,
897 p->wd, p->h, p->x, p->y);
898 XFreePixmap (display, pixmap);
900 if (p->overlay_p)
902 gcv.clip_mask = (Pixmap) 0;
903 XChangeGC (display, gc, GCClipMask, &gcv);
904 XFreePixmap (display, clipmask);
908 XSetClipMask (display, gc, None);
913 /* This is called when starting Emacs and when restarting after
914 suspend. When starting Emacs, no X window is mapped. And nothing
915 must be done to Emacs's own window if it is suspended (though that
916 rarely happens). */
918 static void
919 XTset_terminal_modes (struct terminal *terminal)
923 /* This is called when exiting or suspending Emacs. Exiting will make
924 the X-windows go away, and suspending requires no action. */
926 static void
927 XTreset_terminal_modes (struct terminal *terminal)
932 /***********************************************************************
933 Glyph display
934 ***********************************************************************/
938 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
939 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
940 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
941 int));
942 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
943 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
944 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
945 static void x_draw_glyph_string P_ ((struct glyph_string *));
946 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
947 static void x_set_cursor_gc P_ ((struct glyph_string *));
948 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
949 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
950 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
951 unsigned long *, double, int));
952 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
953 double, int, unsigned long));
954 static void x_setup_relief_colors P_ ((struct glyph_string *));
955 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
956 static void x_draw_image_relief P_ ((struct glyph_string *));
957 static void x_draw_image_foreground P_ ((struct glyph_string *));
958 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
959 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
960 int, int, int));
961 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
962 int, int, int, int, int, int,
963 XRectangle *));
964 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
965 int, int, int, XRectangle *));
967 #if GLYPH_DEBUG
968 static void x_check_font P_ ((struct frame *, struct font *));
969 #endif
972 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
973 face. */
975 static void
976 x_set_cursor_gc (s)
977 struct glyph_string *s;
979 if (s->font == FRAME_FONT (s->f)
980 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
981 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
982 && !s->cmp)
983 s->gc = s->f->output_data.x->cursor_gc;
984 else
986 /* Cursor on non-default face: must merge. */
987 XGCValues xgcv;
988 unsigned long mask;
990 xgcv.background = s->f->output_data.x->cursor_pixel;
991 xgcv.foreground = s->face->background;
993 /* If the glyph would be invisible, try a different foreground. */
994 if (xgcv.foreground == xgcv.background)
995 xgcv.foreground = s->face->foreground;
996 if (xgcv.foreground == xgcv.background)
997 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
998 if (xgcv.foreground == xgcv.background)
999 xgcv.foreground = s->face->foreground;
1001 /* Make sure the cursor is distinct from text in this face. */
1002 if (xgcv.background == s->face->background
1003 && xgcv.foreground == s->face->foreground)
1005 xgcv.background = s->face->foreground;
1006 xgcv.foreground = s->face->background;
1009 IF_DEBUG (x_check_font (s->f, s->font));
1010 xgcv.graphics_exposures = False;
1011 mask = GCForeground | GCBackground | GCGraphicsExposures;
1013 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1014 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1015 mask, &xgcv);
1016 else
1017 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1018 = XCreateGC (s->display, s->window, mask, &xgcv);
1020 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1025 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1027 static void
1028 x_set_mouse_face_gc (s)
1029 struct glyph_string *s;
1031 int face_id;
1032 struct face *face;
1034 /* What face has to be used last for the mouse face? */
1035 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1036 face = FACE_FROM_ID (s->f, face_id);
1037 if (face == NULL)
1038 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1040 if (s->first_glyph->type == CHAR_GLYPH)
1041 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1042 else
1043 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1044 s->face = FACE_FROM_ID (s->f, face_id);
1045 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1047 if (s->font == s->face->font)
1048 s->gc = s->face->gc;
1049 else
1051 /* Otherwise construct scratch_cursor_gc with values from FACE
1052 except for FONT. */
1053 XGCValues xgcv;
1054 unsigned long mask;
1056 xgcv.background = s->face->background;
1057 xgcv.foreground = s->face->foreground;
1058 xgcv.graphics_exposures = False;
1059 mask = GCForeground | GCBackground | GCGraphicsExposures;
1061 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1062 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1063 mask, &xgcv);
1064 else
1065 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1066 = XCreateGC (s->display, s->window, mask, &xgcv);
1068 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1071 xassert (s->gc != 0);
1075 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1076 Faces to use in the mode line have already been computed when the
1077 matrix was built, so there isn't much to do, here. */
1079 static INLINE void
1080 x_set_mode_line_face_gc (s)
1081 struct glyph_string *s;
1083 s->gc = s->face->gc;
1087 /* Set S->gc of glyph string S for drawing that glyph string. Set
1088 S->stippled_p to a non-zero value if the face of S has a stipple
1089 pattern. */
1091 static INLINE void
1092 x_set_glyph_string_gc (s)
1093 struct glyph_string *s;
1095 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1097 if (s->hl == DRAW_NORMAL_TEXT)
1099 s->gc = s->face->gc;
1100 s->stippled_p = s->face->stipple != 0;
1102 else if (s->hl == DRAW_INVERSE_VIDEO)
1104 x_set_mode_line_face_gc (s);
1105 s->stippled_p = s->face->stipple != 0;
1107 else if (s->hl == DRAW_CURSOR)
1109 x_set_cursor_gc (s);
1110 s->stippled_p = 0;
1112 else if (s->hl == DRAW_MOUSE_FACE)
1114 x_set_mouse_face_gc (s);
1115 s->stippled_p = s->face->stipple != 0;
1117 else if (s->hl == DRAW_IMAGE_RAISED
1118 || s->hl == DRAW_IMAGE_SUNKEN)
1120 s->gc = s->face->gc;
1121 s->stippled_p = s->face->stipple != 0;
1123 else
1125 s->gc = s->face->gc;
1126 s->stippled_p = s->face->stipple != 0;
1129 /* GC must have been set. */
1130 xassert (s->gc != 0);
1134 /* Set clipping for output of glyph string S. S may be part of a mode
1135 line or menu if we don't have X toolkit support. */
1137 static INLINE void
1138 x_set_glyph_string_clipping (s)
1139 struct glyph_string *s;
1141 XRectangle *r = s->clip;
1142 int n = get_glyph_string_clip_rects (s, r, 2);
1144 if (n > 0)
1145 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
1146 s->num_clips = n;
1150 /* Set SRC's clipping for output of glyph string DST. This is called
1151 when we are drawing DST's left_overhang or right_overhang only in
1152 the area of SRC. */
1154 static void
1155 x_set_glyph_string_clipping_exactly (src, dst)
1156 struct glyph_string *src, *dst;
1158 XRectangle r;
1160 r.x = src->x;
1161 r.width = src->width;
1162 r.y = src->y;
1163 r.height = src->height;
1164 dst->clip[0] = r;
1165 dst->num_clips = 1;
1166 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1170 /* RIF:
1171 Compute left and right overhang of glyph string S. */
1173 static void
1174 x_compute_glyph_string_overhangs (s)
1175 struct glyph_string *s;
1177 if (s->cmp == NULL
1178 && (s->first_glyph->type == CHAR_GLYPH
1179 || s->first_glyph->type == COMPOSITE_GLYPH))
1181 struct font_metrics metrics;
1183 if (s->first_glyph->type == CHAR_GLYPH)
1185 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1186 struct font *font = s->font;
1187 int i;
1189 for (i = 0; i < s->nchars; i++)
1190 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1191 font->driver->text_extents (font, code, s->nchars, &metrics);
1193 else
1195 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1197 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1199 s->right_overhang = (metrics.rbearing > metrics.width
1200 ? metrics.rbearing - metrics.width : 0);
1201 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1203 else if (s->cmp)
1205 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1206 s->left_overhang = - s->cmp->lbearing;
1211 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1213 static INLINE void
1214 x_clear_glyph_string_rect (s, x, y, w, h)
1215 struct glyph_string *s;
1216 int x, y, w, h;
1218 XGCValues xgcv;
1219 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1220 XSetForeground (s->display, s->gc, xgcv.background);
1221 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1222 XSetForeground (s->display, s->gc, xgcv.foreground);
1226 /* Draw the background of glyph_string S. If S->background_filled_p
1227 is non-zero don't draw it. FORCE_P non-zero means draw the
1228 background even if it wouldn't be drawn normally. This is used
1229 when a string preceding S draws into the background of S, or S
1230 contains the first component of a composition. */
1232 static void
1233 x_draw_glyph_string_background (s, force_p)
1234 struct glyph_string *s;
1235 int force_p;
1237 /* Nothing to do if background has already been drawn or if it
1238 shouldn't be drawn in the first place. */
1239 if (!s->background_filled_p)
1241 int box_line_width = max (s->face->box_line_width, 0);
1243 if (s->stippled_p)
1245 /* Fill background with a stipple pattern. */
1246 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1247 XFillRectangle (s->display, s->window, s->gc, s->x,
1248 s->y + box_line_width,
1249 s->background_width,
1250 s->height - 2 * box_line_width);
1251 XSetFillStyle (s->display, s->gc, FillSolid);
1252 s->background_filled_p = 1;
1254 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1255 || s->font_not_found_p
1256 || s->extends_to_end_of_line_p
1257 || force_p)
1259 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1260 s->background_width,
1261 s->height - 2 * box_line_width);
1262 s->background_filled_p = 1;
1268 /* Draw the foreground of glyph string S. */
1270 static void
1271 x_draw_glyph_string_foreground (s)
1272 struct glyph_string *s;
1274 int i, x;
1276 /* If first glyph of S has a left box line, start drawing the text
1277 of S to the right of that box line. */
1278 if (s->face->box != FACE_NO_BOX
1279 && s->first_glyph->left_box_line_p)
1280 x = s->x + eabs (s->face->box_line_width);
1281 else
1282 x = s->x;
1284 /* Draw characters of S as rectangles if S's font could not be
1285 loaded. */
1286 if (s->font_not_found_p)
1288 for (i = 0; i < s->nchars; ++i)
1290 struct glyph *g = s->first_glyph + i;
1291 XDrawRectangle (s->display, s->window,
1292 s->gc, x, s->y, g->pixel_width - 1,
1293 s->height - 1);
1294 x += g->pixel_width;
1297 else
1299 struct font *font = s->font;
1300 int boff = font->baseline_offset;
1301 int y;
1303 if (font->vertical_centering)
1304 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1306 y = s->ybase - boff;
1307 if (s->for_overlaps
1308 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1309 font->driver->draw (s, 0, s->nchars, x, y, 0);
1310 else
1311 font->driver->draw (s, 0, s->nchars, x, y, 1);
1312 if (s->face->overstrike)
1313 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1317 /* Draw the foreground of composite glyph string S. */
1319 static void
1320 x_draw_composite_glyph_string_foreground (s)
1321 struct glyph_string *s;
1323 int i, j, x;
1324 struct font *font = s->font;
1326 /* If first glyph of S has a left box line, start drawing the text
1327 of S to the right of that box line. */
1328 if (s->face && s->face->box != FACE_NO_BOX
1329 && s->first_glyph->left_box_line_p)
1330 x = s->x + eabs (s->face->box_line_width);
1331 else
1332 x = s->x;
1334 /* S is a glyph string for a composition. S->cmp_from is the index
1335 of the first character drawn for glyphs of this composition.
1336 S->cmp_from == 0 means we are drawing the very first character of
1337 this composition. */
1339 /* Draw a rectangle for the composition if the font for the very
1340 first character of the composition could not be loaded. */
1341 if (s->font_not_found_p)
1343 if (s->cmp_from == 0)
1344 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1345 s->width - 1, s->height - 1);
1347 else if (! s->first_glyph->u.cmp.automatic)
1349 int y = s->ybase;
1351 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1352 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1354 int xx = x + s->cmp->offsets[j * 2];
1355 int yy = y - s->cmp->offsets[j * 2 + 1];
1357 font->driver->draw (s, j, j + 1, xx, yy, 0);
1358 if (s->face->overstrike)
1359 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1362 else
1364 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1365 Lisp_Object glyph;
1366 int y = s->ybase;
1367 int width = 0;
1369 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1371 glyph = LGSTRING_GLYPH (gstring, i);
1372 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1373 width += LGLYPH_WIDTH (glyph);
1374 else
1376 int xoff, yoff, wadjust;
1378 if (j < i)
1380 font->driver->draw (s, j, i, x, y, 0);
1381 if (s->face->overstrike)
1382 font->driver->draw (s, j, i, x + 1, y, 0);
1383 x += width;
1385 xoff = LGLYPH_XOFF (glyph);
1386 yoff = LGLYPH_YOFF (glyph);
1387 wadjust = LGLYPH_WADJUST (glyph);
1388 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1389 if (s->face->overstrike)
1390 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff, 0);
1391 x += wadjust;
1392 j = i + 1;
1393 width = 0;
1396 if (j < i)
1398 font->driver->draw (s, j, i, x, y, 0);
1399 if (s->face->overstrike)
1400 font->driver->draw (s, j, i, x + 1, y, 0);
1406 #ifdef USE_X_TOOLKIT
1408 static struct frame *x_frame_of_widget P_ ((Widget));
1409 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1410 XrmValue *, XrmValue *, XtPointer *));
1411 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1412 XrmValue *, Cardinal *));
1415 /* Return the frame on which widget WIDGET is used.. Abort if frame
1416 cannot be determined. */
1418 static struct frame *
1419 x_frame_of_widget (widget)
1420 Widget widget;
1422 struct x_display_info *dpyinfo;
1423 Lisp_Object tail;
1424 struct frame *f;
1426 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1428 /* Find the top-level shell of the widget. Note that this function
1429 can be called when the widget is not yet realized, so XtWindow
1430 (widget) == 0. That's the reason we can't simply use
1431 x_any_window_to_frame. */
1432 while (!XtIsTopLevelShell (widget))
1433 widget = XtParent (widget);
1435 /* Look for a frame with that top-level widget. Allocate the color
1436 on that frame to get the right gamma correction value. */
1437 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1438 if (FRAMEP (XCAR (tail))
1439 && (f = XFRAME (XCAR (tail)),
1440 (FRAME_X_P (f)
1441 && f->output_data.nothing != 1
1442 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1443 && f->output_data.x->widget == widget)
1444 return f;
1446 abort ();
1450 /* Allocate the color COLOR->pixel on the screen and display of
1451 widget WIDGET in colormap CMAP. If an exact match cannot be
1452 allocated, try the nearest color available. Value is non-zero
1453 if successful. This is called from lwlib. */
1456 x_alloc_nearest_color_for_widget (widget, cmap, color)
1457 Widget widget;
1458 Colormap cmap;
1459 XColor *color;
1461 struct frame *f = x_frame_of_widget (widget);
1462 return x_alloc_nearest_color (f, cmap, color);
1466 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1467 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1468 If this produces the same color as PIXEL, try a color where all RGB
1469 values have DELTA added. Return the allocated color in *PIXEL.
1470 DISPLAY is the X display, CMAP is the colormap to operate on.
1471 Value is non-zero if successful. */
1474 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1475 Widget widget;
1476 Display *display;
1477 Colormap cmap;
1478 unsigned long *pixel;
1479 double factor;
1480 int delta;
1482 struct frame *f = x_frame_of_widget (widget);
1483 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1487 /* Structure specifying which arguments should be passed by Xt to
1488 cvt_string_to_pixel. We want the widget's screen and colormap. */
1490 static XtConvertArgRec cvt_string_to_pixel_args[] =
1492 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1493 sizeof (Screen *)},
1494 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1495 sizeof (Colormap)}
1499 /* The address of this variable is returned by
1500 cvt_string_to_pixel. */
1502 static Pixel cvt_string_to_pixel_value;
1505 /* Convert a color name to a pixel color.
1507 DPY is the display we are working on.
1509 ARGS is an array of *NARGS XrmValue structures holding additional
1510 information about the widget for which the conversion takes place.
1511 The contents of this array are determined by the specification
1512 in cvt_string_to_pixel_args.
1514 FROM is a pointer to an XrmValue which points to the color name to
1515 convert. TO is an XrmValue in which to return the pixel color.
1517 CLOSURE_RET is a pointer to user-data, in which we record if
1518 we allocated the color or not.
1520 Value is True if successful, False otherwise. */
1522 static Boolean
1523 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1524 Display *dpy;
1525 XrmValue *args;
1526 Cardinal *nargs;
1527 XrmValue *from, *to;
1528 XtPointer *closure_ret;
1530 Screen *screen;
1531 Colormap cmap;
1532 Pixel pixel;
1533 String color_name;
1534 XColor color;
1536 if (*nargs != 2)
1538 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1539 "wrongParameters", "cvt_string_to_pixel",
1540 "XtToolkitError",
1541 "Screen and colormap args required", NULL, NULL);
1542 return False;
1545 screen = *(Screen **) args[0].addr;
1546 cmap = *(Colormap *) args[1].addr;
1547 color_name = (String) from->addr;
1549 if (strcmp (color_name, XtDefaultBackground) == 0)
1551 *closure_ret = (XtPointer) False;
1552 pixel = WhitePixelOfScreen (screen);
1554 else if (strcmp (color_name, XtDefaultForeground) == 0)
1556 *closure_ret = (XtPointer) False;
1557 pixel = BlackPixelOfScreen (screen);
1559 else if (XParseColor (dpy, cmap, color_name, &color)
1560 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1562 pixel = color.pixel;
1563 *closure_ret = (XtPointer) True;
1565 else
1567 String params[1];
1568 Cardinal nparams = 1;
1570 params[0] = color_name;
1571 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1572 "badValue", "cvt_string_to_pixel",
1573 "XtToolkitError", "Invalid color `%s'",
1574 params, &nparams);
1575 return False;
1578 if (to->addr != NULL)
1580 if (to->size < sizeof (Pixel))
1582 to->size = sizeof (Pixel);
1583 return False;
1586 *(Pixel *) to->addr = pixel;
1588 else
1590 cvt_string_to_pixel_value = pixel;
1591 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1594 to->size = sizeof (Pixel);
1595 return True;
1599 /* Free a pixel color which was previously allocated via
1600 cvt_string_to_pixel. This is registered as the destructor
1601 for this type of resource via XtSetTypeConverter.
1603 APP is the application context in which we work.
1605 TO is a pointer to an XrmValue holding the color to free.
1606 CLOSURE is the value we stored in CLOSURE_RET for this color
1607 in cvt_string_to_pixel.
1609 ARGS and NARGS are like for cvt_string_to_pixel. */
1611 static void
1612 cvt_pixel_dtor (app, to, closure, args, nargs)
1613 XtAppContext app;
1614 XrmValuePtr to;
1615 XtPointer closure;
1616 XrmValuePtr args;
1617 Cardinal *nargs;
1619 if (*nargs != 2)
1621 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1622 "XtToolkitError",
1623 "Screen and colormap arguments required",
1624 NULL, NULL);
1626 else if (closure != NULL)
1628 /* We did allocate the pixel, so free it. */
1629 Screen *screen = *(Screen **) args[0].addr;
1630 Colormap cmap = *(Colormap *) args[1].addr;
1631 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1632 (Pixel *) to->addr, 1);
1637 #endif /* USE_X_TOOLKIT */
1640 /* Value is an array of XColor structures for the contents of the
1641 color map of display DPY. Set *NCELLS to the size of the array.
1642 Note that this probably shouldn't be called for large color maps,
1643 say a 24-bit TrueColor map. */
1645 static const XColor *
1646 x_color_cells (dpy, ncells)
1647 Display *dpy;
1648 int *ncells;
1650 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1652 if (dpyinfo->color_cells == NULL)
1654 Screen *screen = dpyinfo->screen;
1655 int i;
1657 dpyinfo->ncolor_cells
1658 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1659 dpyinfo->color_cells
1660 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1661 * sizeof *dpyinfo->color_cells);
1663 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1664 dpyinfo->color_cells[i].pixel = i;
1666 XQueryColors (dpy, dpyinfo->cmap,
1667 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1670 *ncells = dpyinfo->ncolor_cells;
1671 return dpyinfo->color_cells;
1675 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1676 colors in COLORS. Use cached information, if available. */
1678 void
1679 x_query_colors (f, colors, ncolors)
1680 struct frame *f;
1681 XColor *colors;
1682 int ncolors;
1684 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1686 if (dpyinfo->color_cells)
1688 int i;
1689 for (i = 0; i < ncolors; ++i)
1691 unsigned long pixel = colors[i].pixel;
1692 xassert (pixel < dpyinfo->ncolor_cells);
1693 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1694 colors[i] = dpyinfo->color_cells[pixel];
1697 else
1698 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1702 /* On frame F, translate pixel color to RGB values for the color in
1703 COLOR. Use cached information, if available. */
1705 void
1706 x_query_color (f, color)
1707 struct frame *f;
1708 XColor *color;
1710 x_query_colors (f, color, 1);
1714 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1715 exact match can't be allocated, try the nearest color available.
1716 Value is non-zero if successful. Set *COLOR to the color
1717 allocated. */
1719 static int
1720 x_alloc_nearest_color_1 (dpy, cmap, color)
1721 Display *dpy;
1722 Colormap cmap;
1723 XColor *color;
1725 int rc;
1727 rc = XAllocColor (dpy, cmap, color);
1728 if (rc == 0)
1730 /* If we got to this point, the colormap is full, so we're going
1731 to try to get the next closest color. The algorithm used is
1732 a least-squares matching, which is what X uses for closest
1733 color matching with StaticColor visuals. */
1734 int nearest, i;
1735 unsigned long nearest_delta = ~0;
1736 int ncells;
1737 const XColor *cells = x_color_cells (dpy, &ncells);
1739 for (nearest = i = 0; i < ncells; ++i)
1741 long dred = (color->red >> 8) - (cells[i].red >> 8);
1742 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1743 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1744 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1746 if (delta < nearest_delta)
1748 nearest = i;
1749 nearest_delta = delta;
1753 color->red = cells[nearest].red;
1754 color->green = cells[nearest].green;
1755 color->blue = cells[nearest].blue;
1756 rc = XAllocColor (dpy, cmap, color);
1758 else
1760 /* If allocation succeeded, and the allocated pixel color is not
1761 equal to a cached pixel color recorded earlier, there was a
1762 change in the colormap, so clear the color cache. */
1763 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1764 XColor *cached_color;
1766 if (dpyinfo->color_cells
1767 && (cached_color = &dpyinfo->color_cells[color->pixel],
1768 (cached_color->red != color->red
1769 || cached_color->blue != color->blue
1770 || cached_color->green != color->green)))
1772 xfree (dpyinfo->color_cells);
1773 dpyinfo->color_cells = NULL;
1774 dpyinfo->ncolor_cells = 0;
1778 #ifdef DEBUG_X_COLORS
1779 if (rc)
1780 register_color (color->pixel);
1781 #endif /* DEBUG_X_COLORS */
1783 return rc;
1787 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1788 exact match can't be allocated, try the nearest color available.
1789 Value is non-zero if successful. Set *COLOR to the color
1790 allocated. */
1793 x_alloc_nearest_color (f, cmap, color)
1794 struct frame *f;
1795 Colormap cmap;
1796 XColor *color;
1798 gamma_correct (f, color);
1799 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1803 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1804 It's necessary to do this instead of just using PIXEL directly to
1805 get color reference counts right. */
1807 unsigned long
1808 x_copy_color (f, pixel)
1809 struct frame *f;
1810 unsigned long pixel;
1812 XColor color;
1814 color.pixel = pixel;
1815 BLOCK_INPUT;
1816 x_query_color (f, &color);
1817 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1818 UNBLOCK_INPUT;
1819 #ifdef DEBUG_X_COLORS
1820 register_color (pixel);
1821 #endif
1822 return color.pixel;
1826 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1827 It's necessary to do this instead of just using PIXEL directly to
1828 get color reference counts right. */
1830 unsigned long
1831 x_copy_dpy_color (dpy, cmap, pixel)
1832 Display *dpy;
1833 Colormap cmap;
1834 unsigned long pixel;
1836 XColor color;
1838 color.pixel = pixel;
1839 BLOCK_INPUT;
1840 XQueryColor (dpy, cmap, &color);
1841 XAllocColor (dpy, cmap, &color);
1842 UNBLOCK_INPUT;
1843 #ifdef DEBUG_X_COLORS
1844 register_color (pixel);
1845 #endif
1846 return color.pixel;
1850 /* Brightness beyond which a color won't have its highlight brightness
1851 boosted.
1853 Nominally, highlight colors for `3d' faces are calculated by
1854 brightening an object's color by a constant scale factor, but this
1855 doesn't yield good results for dark colors, so for colors who's
1856 brightness is less than this value (on a scale of 0-65535) have an
1857 use an additional additive factor.
1859 The value here is set so that the default menu-bar/mode-line color
1860 (grey75) will not have its highlights changed at all. */
1861 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1864 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1865 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1866 If this produces the same color as PIXEL, try a color where all RGB
1867 values have DELTA added. Return the allocated color in *PIXEL.
1868 DISPLAY is the X display, CMAP is the colormap to operate on.
1869 Value is non-zero if successful. */
1871 static int
1872 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1873 struct frame *f;
1874 Display *display;
1875 Colormap cmap;
1876 unsigned long *pixel;
1877 double factor;
1878 int delta;
1880 XColor color, new;
1881 long bright;
1882 int success_p;
1884 /* Get RGB color values. */
1885 color.pixel = *pixel;
1886 x_query_color (f, &color);
1888 /* Change RGB values by specified FACTOR. Avoid overflow! */
1889 xassert (factor >= 0);
1890 new.red = min (0xffff, factor * color.red);
1891 new.green = min (0xffff, factor * color.green);
1892 new.blue = min (0xffff, factor * color.blue);
1894 /* Calculate brightness of COLOR. */
1895 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1897 /* We only boost colors that are darker than
1898 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1899 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1900 /* Make an additive adjustment to NEW, because it's dark enough so
1901 that scaling by FACTOR alone isn't enough. */
1903 /* How far below the limit this color is (0 - 1, 1 being darker). */
1904 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1905 /* The additive adjustment. */
1906 int min_delta = delta * dimness * factor / 2;
1908 if (factor < 1)
1910 new.red = max (0, new.red - min_delta);
1911 new.green = max (0, new.green - min_delta);
1912 new.blue = max (0, new.blue - min_delta);
1914 else
1916 new.red = min (0xffff, min_delta + new.red);
1917 new.green = min (0xffff, min_delta + new.green);
1918 new.blue = min (0xffff, min_delta + new.blue);
1922 /* Try to allocate the color. */
1923 success_p = x_alloc_nearest_color (f, cmap, &new);
1924 if (success_p)
1926 if (new.pixel == *pixel)
1928 /* If we end up with the same color as before, try adding
1929 delta to the RGB values. */
1930 x_free_colors (f, &new.pixel, 1);
1932 new.red = min (0xffff, delta + color.red);
1933 new.green = min (0xffff, delta + color.green);
1934 new.blue = min (0xffff, delta + color.blue);
1935 success_p = x_alloc_nearest_color (f, cmap, &new);
1937 else
1938 success_p = 1;
1939 *pixel = new.pixel;
1942 return success_p;
1946 /* Set up the foreground color for drawing relief lines of glyph
1947 string S. RELIEF is a pointer to a struct relief containing the GC
1948 with which lines will be drawn. Use a color that is FACTOR or
1949 DELTA lighter or darker than the relief's background which is found
1950 in S->f->output_data.x->relief_background. If such a color cannot
1951 be allocated, use DEFAULT_PIXEL, instead. */
1953 static void
1954 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1955 struct frame *f;
1956 struct relief *relief;
1957 double factor;
1958 int delta;
1959 unsigned long default_pixel;
1961 XGCValues xgcv;
1962 struct x_output *di = f->output_data.x;
1963 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1964 unsigned long pixel;
1965 unsigned long background = di->relief_background;
1966 Colormap cmap = FRAME_X_COLORMAP (f);
1967 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1968 Display *dpy = FRAME_X_DISPLAY (f);
1970 xgcv.graphics_exposures = False;
1971 xgcv.line_width = 1;
1973 /* Free previously allocated color. The color cell will be reused
1974 when it has been freed as many times as it was allocated, so this
1975 doesn't affect faces using the same colors. */
1976 if (relief->gc
1977 && relief->allocated_p)
1979 x_free_colors (f, &relief->pixel, 1);
1980 relief->allocated_p = 0;
1983 /* Allocate new color. */
1984 xgcv.foreground = default_pixel;
1985 pixel = background;
1986 if (dpyinfo->n_planes != 1
1987 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1989 relief->allocated_p = 1;
1990 xgcv.foreground = relief->pixel = pixel;
1993 if (relief->gc == 0)
1995 xgcv.stipple = dpyinfo->gray;
1996 mask |= GCStipple;
1997 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1999 else
2000 XChangeGC (dpy, relief->gc, mask, &xgcv);
2004 /* Set up colors for the relief lines around glyph string S. */
2006 static void
2007 x_setup_relief_colors (s)
2008 struct glyph_string *s;
2010 struct x_output *di = s->f->output_data.x;
2011 unsigned long color;
2013 if (s->face->use_box_color_for_shadows_p)
2014 color = s->face->box_color;
2015 else if (s->first_glyph->type == IMAGE_GLYPH
2016 && s->img->pixmap
2017 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2018 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2019 else
2021 XGCValues xgcv;
2023 /* Get the background color of the face. */
2024 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2025 color = xgcv.background;
2028 if (di->white_relief.gc == 0
2029 || color != di->relief_background)
2031 di->relief_background = color;
2032 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2033 WHITE_PIX_DEFAULT (s->f));
2034 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2035 BLACK_PIX_DEFAULT (s->f));
2040 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2041 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2042 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2043 relief. LEFT_P non-zero means draw a relief on the left side of
2044 the rectangle. RIGHT_P non-zero means draw a relief on the right
2045 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2046 when drawing. */
2048 static void
2049 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2050 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2051 struct frame *f;
2052 int left_x, top_y, right_x, bottom_y, width;
2053 int top_p, bot_p, left_p, right_p, raised_p;
2054 XRectangle *clip_rect;
2056 Display *dpy = FRAME_X_DISPLAY (f);
2057 Window window = FRAME_X_WINDOW (f);
2058 int i;
2059 GC gc;
2061 if (raised_p)
2062 gc = f->output_data.x->white_relief.gc;
2063 else
2064 gc = f->output_data.x->black_relief.gc;
2065 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2067 /* Top. */
2068 if (top_p)
2069 for (i = 0; i < width; ++i)
2070 XDrawLine (dpy, window, gc,
2071 left_x + i * left_p, top_y + i,
2072 right_x + 1 - i * right_p, top_y + i);
2074 /* Left. */
2075 if (left_p)
2076 for (i = 0; i < width; ++i)
2077 XDrawLine (dpy, window, gc,
2078 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2080 XSetClipMask (dpy, gc, None);
2081 if (raised_p)
2082 gc = f->output_data.x->black_relief.gc;
2083 else
2084 gc = f->output_data.x->white_relief.gc;
2085 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2087 /* Bottom. */
2088 if (bot_p)
2089 for (i = 0; i < width; ++i)
2090 XDrawLine (dpy, window, gc,
2091 left_x + i * left_p, bottom_y - i,
2092 right_x + 1 - i * right_p, bottom_y - i);
2094 /* Right. */
2095 if (right_p)
2096 for (i = 0; i < width; ++i)
2097 XDrawLine (dpy, window, gc,
2098 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2100 XSetClipMask (dpy, gc, None);
2104 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2105 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2106 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2107 left side of the rectangle. RIGHT_P non-zero means draw a line
2108 on the right side of the rectangle. CLIP_RECT is the clipping
2109 rectangle to use when drawing. */
2111 static void
2112 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2113 left_p, right_p, clip_rect)
2114 struct glyph_string *s;
2115 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2116 XRectangle *clip_rect;
2118 XGCValues xgcv;
2120 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2121 XSetForeground (s->display, s->gc, s->face->box_color);
2122 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2124 /* Top. */
2125 XFillRectangle (s->display, s->window, s->gc,
2126 left_x, top_y, right_x - left_x + 1, width);
2128 /* Left. */
2129 if (left_p)
2130 XFillRectangle (s->display, s->window, s->gc,
2131 left_x, top_y, width, bottom_y - top_y + 1);
2133 /* Bottom. */
2134 XFillRectangle (s->display, s->window, s->gc,
2135 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2137 /* Right. */
2138 if (right_p)
2139 XFillRectangle (s->display, s->window, s->gc,
2140 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2142 XSetForeground (s->display, s->gc, xgcv.foreground);
2143 XSetClipMask (s->display, s->gc, None);
2147 /* Draw a box around glyph string S. */
2149 static void
2150 x_draw_glyph_string_box (s)
2151 struct glyph_string *s;
2153 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2154 int left_p, right_p;
2155 struct glyph *last_glyph;
2156 XRectangle clip_rect;
2158 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2159 ? WINDOW_RIGHT_EDGE_X (s->w)
2160 : window_box_right (s->w, s->area));
2162 /* The glyph that may have a right box line. */
2163 last_glyph = (s->cmp || s->img
2164 ? s->first_glyph
2165 : s->first_glyph + s->nchars - 1);
2167 width = eabs (s->face->box_line_width);
2168 raised_p = s->face->box == FACE_RAISED_BOX;
2169 left_x = s->x;
2170 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2171 ? last_x - 1
2172 : min (last_x, s->x + s->background_width) - 1);
2173 top_y = s->y;
2174 bottom_y = top_y + s->height - 1;
2176 left_p = (s->first_glyph->left_box_line_p
2177 || (s->hl == DRAW_MOUSE_FACE
2178 && (s->prev == NULL
2179 || s->prev->hl != s->hl)));
2180 right_p = (last_glyph->right_box_line_p
2181 || (s->hl == DRAW_MOUSE_FACE
2182 && (s->next == NULL
2183 || s->next->hl != s->hl)));
2185 get_glyph_string_clip_rect (s, &clip_rect);
2187 if (s->face->box == FACE_SIMPLE_BOX)
2188 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2189 left_p, right_p, &clip_rect);
2190 else
2192 x_setup_relief_colors (s);
2193 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2194 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2199 /* Draw foreground of image glyph string S. */
2201 static void
2202 x_draw_image_foreground (s)
2203 struct glyph_string *s;
2205 int x = s->x;
2206 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2208 /* If first glyph of S has a left box line, start drawing it to the
2209 right of that line. */
2210 if (s->face->box != FACE_NO_BOX
2211 && s->first_glyph->left_box_line_p
2212 && s->slice.x == 0)
2213 x += eabs (s->face->box_line_width);
2215 /* If there is a margin around the image, adjust x- and y-position
2216 by that margin. */
2217 if (s->slice.x == 0)
2218 x += s->img->hmargin;
2219 if (s->slice.y == 0)
2220 y += s->img->vmargin;
2222 if (s->img->pixmap)
2224 if (s->img->mask)
2226 /* We can't set both a clip mask and use XSetClipRectangles
2227 because the latter also sets a clip mask. We also can't
2228 trust on the shape extension to be available
2229 (XShapeCombineRegion). So, compute the rectangle to draw
2230 manually. */
2231 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2232 | GCFunction);
2233 XGCValues xgcv;
2234 XRectangle clip_rect, image_rect, r;
2236 xgcv.clip_mask = s->img->mask;
2237 xgcv.clip_x_origin = x;
2238 xgcv.clip_y_origin = y;
2239 xgcv.function = GXcopy;
2240 XChangeGC (s->display, s->gc, mask, &xgcv);
2242 get_glyph_string_clip_rect (s, &clip_rect);
2243 image_rect.x = x;
2244 image_rect.y = y;
2245 image_rect.width = s->slice.width;
2246 image_rect.height = s->slice.height;
2247 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2248 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2249 s->slice.x + r.x - x, s->slice.y + r.y - y,
2250 r.width, r.height, r.x, r.y);
2252 else
2254 XRectangle clip_rect, image_rect, r;
2256 get_glyph_string_clip_rect (s, &clip_rect);
2257 image_rect.x = x;
2258 image_rect.y = y;
2259 image_rect.width = s->slice.width;
2260 image_rect.height = s->slice.height;
2261 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2262 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2263 s->slice.x + r.x - x, s->slice.y + r.y - y,
2264 r.width, r.height, r.x, r.y);
2266 /* When the image has a mask, we can expect that at
2267 least part of a mouse highlight or a block cursor will
2268 be visible. If the image doesn't have a mask, make
2269 a block cursor visible by drawing a rectangle around
2270 the image. I believe it's looking better if we do
2271 nothing here for mouse-face. */
2272 if (s->hl == DRAW_CURSOR)
2274 int r = s->img->relief;
2275 if (r < 0) r = -r;
2276 XDrawRectangle (s->display, s->window, s->gc,
2277 x - r, y - r,
2278 s->slice.width + r*2 - 1,
2279 s->slice.height + r*2 - 1);
2283 else
2284 /* Draw a rectangle if image could not be loaded. */
2285 XDrawRectangle (s->display, s->window, s->gc, x, y,
2286 s->slice.width - 1, s->slice.height - 1);
2290 /* Draw a relief around the image glyph string S. */
2292 static void
2293 x_draw_image_relief (s)
2294 struct glyph_string *s;
2296 int x0, y0, x1, y1, thick, raised_p;
2297 XRectangle r;
2298 int x = s->x;
2299 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2301 /* If first glyph of S has a left box line, start drawing it to the
2302 right of that line. */
2303 if (s->face->box != FACE_NO_BOX
2304 && s->first_glyph->left_box_line_p
2305 && s->slice.x == 0)
2306 x += eabs (s->face->box_line_width);
2308 /* If there is a margin around the image, adjust x- and y-position
2309 by that margin. */
2310 if (s->slice.x == 0)
2311 x += s->img->hmargin;
2312 if (s->slice.y == 0)
2313 y += s->img->vmargin;
2315 if (s->hl == DRAW_IMAGE_SUNKEN
2316 || s->hl == DRAW_IMAGE_RAISED)
2318 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2319 raised_p = s->hl == DRAW_IMAGE_RAISED;
2321 else
2323 thick = eabs (s->img->relief);
2324 raised_p = s->img->relief > 0;
2327 x0 = x - thick;
2328 y0 = y - thick;
2329 x1 = x + s->slice.width + thick - 1;
2330 y1 = y + s->slice.height + thick - 1;
2332 x_setup_relief_colors (s);
2333 get_glyph_string_clip_rect (s, &r);
2334 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2335 s->slice.y == 0,
2336 s->slice.y + s->slice.height == s->img->height,
2337 s->slice.x == 0,
2338 s->slice.x + s->slice.width == s->img->width,
2339 &r);
2343 /* Draw the foreground of image glyph string S to PIXMAP. */
2345 static void
2346 x_draw_image_foreground_1 (s, pixmap)
2347 struct glyph_string *s;
2348 Pixmap pixmap;
2350 int x = 0;
2351 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2353 /* If first glyph of S has a left box line, start drawing it to the
2354 right of that line. */
2355 if (s->face->box != FACE_NO_BOX
2356 && s->first_glyph->left_box_line_p
2357 && s->slice.x == 0)
2358 x += eabs (s->face->box_line_width);
2360 /* If there is a margin around the image, adjust x- and y-position
2361 by that margin. */
2362 if (s->slice.x == 0)
2363 x += s->img->hmargin;
2364 if (s->slice.y == 0)
2365 y += s->img->vmargin;
2367 if (s->img->pixmap)
2369 if (s->img->mask)
2371 /* We can't set both a clip mask and use XSetClipRectangles
2372 because the latter also sets a clip mask. We also can't
2373 trust on the shape extension to be available
2374 (XShapeCombineRegion). So, compute the rectangle to draw
2375 manually. */
2376 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2377 | GCFunction);
2378 XGCValues xgcv;
2380 xgcv.clip_mask = s->img->mask;
2381 xgcv.clip_x_origin = x - s->slice.x;
2382 xgcv.clip_y_origin = y - s->slice.y;
2383 xgcv.function = GXcopy;
2384 XChangeGC (s->display, s->gc, mask, &xgcv);
2386 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2387 s->slice.x, s->slice.y,
2388 s->slice.width, s->slice.height, x, y);
2389 XSetClipMask (s->display, s->gc, None);
2391 else
2393 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2394 s->slice.x, s->slice.y,
2395 s->slice.width, s->slice.height, x, y);
2397 /* When the image has a mask, we can expect that at
2398 least part of a mouse highlight or a block cursor will
2399 be visible. If the image doesn't have a mask, make
2400 a block cursor visible by drawing a rectangle around
2401 the image. I believe it's looking better if we do
2402 nothing here for mouse-face. */
2403 if (s->hl == DRAW_CURSOR)
2405 int r = s->img->relief;
2406 if (r < 0) r = -r;
2407 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2408 s->slice.width + r*2 - 1,
2409 s->slice.height + r*2 - 1);
2413 else
2414 /* Draw a rectangle if image could not be loaded. */
2415 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2416 s->slice.width - 1, s->slice.height - 1);
2420 /* Draw part of the background of glyph string S. X, Y, W, and H
2421 give the rectangle to draw. */
2423 static void
2424 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2425 struct glyph_string *s;
2426 int x, y, w, h;
2428 if (s->stippled_p)
2430 /* Fill background with a stipple pattern. */
2431 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2432 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2433 XSetFillStyle (s->display, s->gc, FillSolid);
2435 else
2436 x_clear_glyph_string_rect (s, x, y, w, h);
2440 /* Draw image glyph string S.
2442 s->y
2443 s->x +-------------------------
2444 | s->face->box
2446 | +-------------------------
2447 | | s->img->margin
2449 | | +-------------------
2450 | | | the image
2454 static void
2455 x_draw_image_glyph_string (s)
2456 struct glyph_string *s;
2458 int box_line_hwidth = eabs (s->face->box_line_width);
2459 int box_line_vwidth = max (s->face->box_line_width, 0);
2460 int height;
2461 Pixmap pixmap = None;
2463 height = s->height;
2464 if (s->slice.y == 0)
2465 height -= box_line_vwidth;
2466 if (s->slice.y + s->slice.height >= s->img->height)
2467 height -= box_line_vwidth;
2469 /* Fill background with face under the image. Do it only if row is
2470 taller than image or if image has a clip mask to reduce
2471 flickering. */
2472 s->stippled_p = s->face->stipple != 0;
2473 if (height > s->slice.height
2474 || s->img->hmargin
2475 || s->img->vmargin
2476 || s->img->mask
2477 || s->img->pixmap == 0
2478 || s->width != s->background_width)
2480 if (s->img->mask)
2482 /* Create a pixmap as large as the glyph string. Fill it
2483 with the background color. Copy the image to it, using
2484 its mask. Copy the temporary pixmap to the display. */
2485 Screen *screen = FRAME_X_SCREEN (s->f);
2486 int depth = DefaultDepthOfScreen (screen);
2488 /* Create a pixmap as large as the glyph string. */
2489 pixmap = XCreatePixmap (s->display, s->window,
2490 s->background_width,
2491 s->height, depth);
2493 /* Don't clip in the following because we're working on the
2494 pixmap. */
2495 XSetClipMask (s->display, s->gc, None);
2497 /* Fill the pixmap with the background color/stipple. */
2498 if (s->stippled_p)
2500 /* Fill background with a stipple pattern. */
2501 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2502 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2503 XFillRectangle (s->display, pixmap, s->gc,
2504 0, 0, s->background_width, s->height);
2505 XSetFillStyle (s->display, s->gc, FillSolid);
2506 XSetTSOrigin (s->display, s->gc, 0, 0);
2508 else
2510 XGCValues xgcv;
2511 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2512 &xgcv);
2513 XSetForeground (s->display, s->gc, xgcv.background);
2514 XFillRectangle (s->display, pixmap, s->gc,
2515 0, 0, s->background_width, s->height);
2516 XSetForeground (s->display, s->gc, xgcv.foreground);
2519 else
2521 int x = s->x;
2522 int y = s->y;
2524 if (s->first_glyph->left_box_line_p
2525 && s->slice.x == 0)
2526 x += box_line_hwidth;
2528 if (s->slice.y == 0)
2529 y += box_line_vwidth;
2531 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2534 s->background_filled_p = 1;
2537 /* Draw the foreground. */
2538 if (pixmap != None)
2540 x_draw_image_foreground_1 (s, pixmap);
2541 x_set_glyph_string_clipping (s);
2542 XCopyArea (s->display, pixmap, s->window, s->gc,
2543 0, 0, s->background_width, s->height, s->x, s->y);
2544 XFreePixmap (s->display, pixmap);
2546 else
2547 x_draw_image_foreground (s);
2549 /* If we must draw a relief around the image, do it. */
2550 if (s->img->relief
2551 || s->hl == DRAW_IMAGE_RAISED
2552 || s->hl == DRAW_IMAGE_SUNKEN)
2553 x_draw_image_relief (s);
2557 /* Draw stretch glyph string S. */
2559 static void
2560 x_draw_stretch_glyph_string (s)
2561 struct glyph_string *s;
2563 xassert (s->first_glyph->type == STRETCH_GLYPH);
2565 if (s->hl == DRAW_CURSOR
2566 && !x_stretch_cursor_p)
2568 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2569 as wide as the stretch glyph. */
2570 int width, background_width = s->background_width;
2571 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2573 if (x < left_x)
2575 background_width -= left_x - x;
2576 x = left_x;
2578 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2580 /* Draw cursor. */
2581 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2583 /* Clear rest using the GC of the original non-cursor face. */
2584 if (width < background_width)
2586 int y = s->y;
2587 int w = background_width - width, h = s->height;
2588 XRectangle r;
2589 GC gc;
2591 x += width;
2592 if (s->row->mouse_face_p
2593 && cursor_in_mouse_face_p (s->w))
2595 x_set_mouse_face_gc (s);
2596 gc = s->gc;
2598 else
2599 gc = s->face->gc;
2601 get_glyph_string_clip_rect (s, &r);
2602 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2604 if (s->face->stipple)
2606 /* Fill background with a stipple pattern. */
2607 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2608 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2609 XSetFillStyle (s->display, gc, FillSolid);
2611 else
2613 XGCValues xgcv;
2614 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2615 XSetForeground (s->display, gc, xgcv.background);
2616 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2617 XSetForeground (s->display, gc, xgcv.foreground);
2621 else if (!s->background_filled_p)
2623 int background_width = s->background_width;
2624 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2626 /* Don't draw into left margin, fringe or scrollbar area
2627 except for header line and mode line. */
2628 if (x < left_x && !s->row->mode_line_p)
2630 background_width -= left_x - x;
2631 x = left_x;
2633 if (background_width > 0)
2634 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2637 s->background_filled_p = 1;
2641 /* Draw glyph string S. */
2643 static void
2644 x_draw_glyph_string (s)
2645 struct glyph_string *s;
2647 int relief_drawn_p = 0;
2649 /* If S draws into the background of its successors, draw the
2650 background of the successors first so that S can draw into it.
2651 This makes S->next use XDrawString instead of XDrawImageString. */
2652 if (s->next && s->right_overhang && !s->for_overlaps)
2654 int width;
2655 struct glyph_string *next;
2657 for (width = 0, next = s->next;
2658 next && width < s->right_overhang;
2659 width += next->width, next = next->next)
2660 if (next->first_glyph->type != IMAGE_GLYPH)
2662 x_set_glyph_string_gc (next);
2663 x_set_glyph_string_clipping (next);
2664 if (next->first_glyph->type == STRETCH_GLYPH)
2665 x_draw_stretch_glyph_string (next);
2666 else
2667 x_draw_glyph_string_background (next, 1);
2668 next->num_clips = 0;
2672 /* Set up S->gc, set clipping and draw S. */
2673 x_set_glyph_string_gc (s);
2675 /* Draw relief (if any) in advance for char/composition so that the
2676 glyph string can be drawn over it. */
2677 if (!s->for_overlaps
2678 && s->face->box != FACE_NO_BOX
2679 && (s->first_glyph->type == CHAR_GLYPH
2680 || s->first_glyph->type == COMPOSITE_GLYPH))
2683 x_set_glyph_string_clipping (s);
2684 x_draw_glyph_string_background (s, 1);
2685 x_draw_glyph_string_box (s);
2686 x_set_glyph_string_clipping (s);
2687 relief_drawn_p = 1;
2689 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2690 && !s->clip_tail
2691 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2692 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2693 /* We must clip just this glyph. left_overhang part has already
2694 drawn when s->prev was drawn, and right_overhang part will be
2695 drawn later when s->next is drawn. */
2696 x_set_glyph_string_clipping_exactly (s, s);
2697 else
2698 x_set_glyph_string_clipping (s);
2700 switch (s->first_glyph->type)
2702 case IMAGE_GLYPH:
2703 x_draw_image_glyph_string (s);
2704 break;
2706 case STRETCH_GLYPH:
2707 x_draw_stretch_glyph_string (s);
2708 break;
2710 case CHAR_GLYPH:
2711 if (s->for_overlaps)
2712 s->background_filled_p = 1;
2713 else
2714 x_draw_glyph_string_background (s, 0);
2715 x_draw_glyph_string_foreground (s);
2716 break;
2718 case COMPOSITE_GLYPH:
2719 if (s->for_overlaps || (s->cmp_from > 0
2720 && ! s->first_glyph->u.cmp.automatic))
2721 s->background_filled_p = 1;
2722 else
2723 x_draw_glyph_string_background (s, 1);
2724 x_draw_composite_glyph_string_foreground (s);
2725 break;
2727 default:
2728 abort ();
2731 if (!s->for_overlaps)
2733 /* Draw underline. */
2734 if (s->face->underline_p)
2736 unsigned long thickness, position;
2737 int y;
2739 if (s->prev && s->prev->face->underline_p)
2741 /* We use the same underline style as the previous one. */
2742 thickness = s->prev->underline_thickness;
2743 position = s->prev->underline_position;
2745 else
2747 /* Get the underline thickness. Default is 1 pixel. */
2748 if (s->font && s->font->underline_thickness > 0)
2749 thickness = s->font->underline_thickness;
2750 else
2751 thickness = 1;
2752 if (x_underline_at_descent_line)
2753 position = (s->height - thickness) - (s->ybase - s->y);
2754 else
2756 /* Get the underline position. This is the recommended
2757 vertical offset in pixels from the baseline to the top of
2758 the underline. This is a signed value according to the
2759 specs, and its default is
2761 ROUND ((maximum descent) / 2), with
2762 ROUND(x) = floor (x + 0.5) */
2764 if (x_use_underline_position_properties
2765 && s->font && s->font->underline_position >= 0)
2766 position = s->font->underline_position;
2767 else if (s->font)
2768 position = (s->font->descent + 1) / 2;
2769 else
2770 position = underline_minimum_offset;
2772 position = max (position, underline_minimum_offset);
2774 /* Check the sanity of thickness and position. We should
2775 avoid drawing underline out of the current line area. */
2776 if (s->y + s->height <= s->ybase + position)
2777 position = (s->height - 1) - (s->ybase - s->y);
2778 if (s->y + s->height < s->ybase + position + thickness)
2779 thickness = (s->y + s->height) - (s->ybase + position);
2780 s->underline_thickness = thickness;
2781 s->underline_position = position;
2782 y = s->ybase + position;
2783 if (s->face->underline_defaulted_p)
2784 XFillRectangle (s->display, s->window, s->gc,
2785 s->x, y, s->width, thickness);
2786 else
2788 XGCValues xgcv;
2789 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2790 XSetForeground (s->display, s->gc, s->face->underline_color);
2791 XFillRectangle (s->display, s->window, s->gc,
2792 s->x, y, s->width, thickness);
2793 XSetForeground (s->display, s->gc, xgcv.foreground);
2797 /* Draw overline. */
2798 if (s->face->overline_p)
2800 unsigned long dy = 0, h = 1;
2802 if (s->face->overline_color_defaulted_p)
2803 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2804 s->width, h);
2805 else
2807 XGCValues xgcv;
2808 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2809 XSetForeground (s->display, s->gc, s->face->overline_color);
2810 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2811 s->width, h);
2812 XSetForeground (s->display, s->gc, xgcv.foreground);
2816 /* Draw strike-through. */
2817 if (s->face->strike_through_p)
2819 unsigned long h = 1;
2820 unsigned long dy = (s->height - h) / 2;
2822 if (s->face->strike_through_color_defaulted_p)
2823 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2824 s->width, h);
2825 else
2827 XGCValues xgcv;
2828 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2829 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2830 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2831 s->width, h);
2832 XSetForeground (s->display, s->gc, xgcv.foreground);
2836 /* Draw relief if not yet drawn. */
2837 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2838 x_draw_glyph_string_box (s);
2840 if (s->prev)
2842 struct glyph_string *prev;
2844 for (prev = s->prev; prev; prev = prev->prev)
2845 if (prev->hl != s->hl
2846 && prev->x + prev->width + prev->right_overhang > s->x)
2848 /* As prev was drawn while clipped to its own area, we
2849 must draw the right_overhang part using s->hl now. */
2850 enum draw_glyphs_face save = prev->hl;
2852 prev->hl = s->hl;
2853 x_set_glyph_string_gc (prev);
2854 x_set_glyph_string_clipping_exactly (s, prev);
2855 if (prev->first_glyph->type == CHAR_GLYPH)
2856 x_draw_glyph_string_foreground (prev);
2857 else
2858 x_draw_composite_glyph_string_foreground (prev);
2859 XSetClipMask (prev->display, prev->gc, None);
2860 prev->hl = save;
2861 prev->num_clips = 0;
2865 if (s->next)
2867 struct glyph_string *next;
2869 for (next = s->next; next; next = next->next)
2870 if (next->hl != s->hl
2871 && next->x - next->left_overhang < s->x + s->width)
2873 /* As next will be drawn while clipped to its own area,
2874 we must draw the left_overhang part using s->hl now. */
2875 enum draw_glyphs_face save = next->hl;
2877 next->hl = s->hl;
2878 x_set_glyph_string_gc (next);
2879 x_set_glyph_string_clipping_exactly (s, next);
2880 if (next->first_glyph->type == CHAR_GLYPH)
2881 x_draw_glyph_string_foreground (next);
2882 else
2883 x_draw_composite_glyph_string_foreground (next);
2884 XSetClipMask (next->display, next->gc, None);
2885 next->hl = save;
2886 next->num_clips = 0;
2891 /* Reset clipping. */
2892 XSetClipMask (s->display, s->gc, None);
2893 s->num_clips = 0;
2896 /* Shift display to make room for inserted glyphs. */
2898 void
2899 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2900 struct frame *f;
2901 int x, y, width, height, shift_by;
2903 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2904 f->output_data.x->normal_gc,
2905 x, y, width, height,
2906 x + shift_by, y);
2909 /* Delete N glyphs at the nominal cursor position. Not implemented
2910 for X frames. */
2912 static void
2913 x_delete_glyphs (f, n)
2914 struct frame *f;
2915 register int n;
2917 abort ();
2921 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2922 If they are <= 0, this is probably an error. */
2924 void
2925 x_clear_area (dpy, window, x, y, width, height, exposures)
2926 Display *dpy;
2927 Window window;
2928 int x, y;
2929 int width, height;
2930 int exposures;
2932 xassert (width > 0 && height > 0);
2933 XClearArea (dpy, window, x, y, width, height, exposures);
2937 /* Clear an entire frame. */
2939 static void
2940 x_clear_frame (struct frame *f)
2942 /* Clearing the frame will erase any cursor, so mark them all as no
2943 longer visible. */
2944 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2945 output_cursor.hpos = output_cursor.vpos = 0;
2946 output_cursor.x = -1;
2948 /* We don't set the output cursor here because there will always
2949 follow an explicit cursor_to. */
2950 BLOCK_INPUT;
2951 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2953 /* We have to clear the scroll bars, too. If we have changed
2954 colors or something like that, then they should be notified. */
2955 x_scroll_bar_clear (f);
2957 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2958 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2959 redisplay, do it here. */
2960 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2961 #endif
2963 XFlush (FRAME_X_DISPLAY (f));
2965 UNBLOCK_INPUT;
2970 /* Invert the middle quarter of the frame for .15 sec. */
2972 /* We use the select system call to do the waiting, so we have to make
2973 sure it's available. If it isn't, we just won't do visual bells. */
2975 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2978 /* Subtract the `struct timeval' values X and Y, storing the result in
2979 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2981 static int
2982 timeval_subtract (result, x, y)
2983 struct timeval *result, x, y;
2985 /* Perform the carry for the later subtraction by updating y. This
2986 is safer because on some systems the tv_sec member is unsigned. */
2987 if (x.tv_usec < y.tv_usec)
2989 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2990 y.tv_usec -= 1000000 * nsec;
2991 y.tv_sec += nsec;
2994 if (x.tv_usec - y.tv_usec > 1000000)
2996 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2997 y.tv_usec += 1000000 * nsec;
2998 y.tv_sec -= nsec;
3001 /* Compute the time remaining to wait. tv_usec is certainly
3002 positive. */
3003 result->tv_sec = x.tv_sec - y.tv_sec;
3004 result->tv_usec = x.tv_usec - y.tv_usec;
3006 /* Return indication of whether the result should be considered
3007 negative. */
3008 return x.tv_sec < y.tv_sec;
3011 void
3012 XTflash (f)
3013 struct frame *f;
3015 BLOCK_INPUT;
3018 GC gc;
3020 /* Create a GC that will use the GXxor function to flip foreground
3021 pixels into background pixels. */
3023 XGCValues values;
3025 values.function = GXxor;
3026 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3027 ^ FRAME_BACKGROUND_PIXEL (f));
3029 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3030 GCFunction | GCForeground, &values);
3034 /* Get the height not including a menu bar widget. */
3035 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
3036 /* Height of each line to flash. */
3037 int flash_height = FRAME_LINE_HEIGHT (f);
3038 /* These will be the left and right margins of the rectangles. */
3039 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3040 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3042 int width;
3044 /* Don't flash the area between a scroll bar and the frame
3045 edge it is next to. */
3046 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3048 case vertical_scroll_bar_left:
3049 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3050 break;
3052 case vertical_scroll_bar_right:
3053 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3054 break;
3056 default:
3057 break;
3060 width = flash_right - flash_left;
3062 /* If window is tall, flash top and bottom line. */
3063 if (height > 3 * FRAME_LINE_HEIGHT (f))
3065 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3066 flash_left,
3067 (FRAME_INTERNAL_BORDER_WIDTH (f)
3068 + FRAME_TOP_MARGIN_HEIGHT (f)),
3069 width, flash_height);
3070 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3071 flash_left,
3072 (height - flash_height
3073 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3074 width, flash_height);
3076 else
3077 /* If it is short, flash it all. */
3078 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3079 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3080 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3082 x_flush (f);
3085 struct timeval wakeup;
3087 EMACS_GET_TIME (wakeup);
3089 /* Compute time to wait until, propagating carry from usecs. */
3090 wakeup.tv_usec += 150000;
3091 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3092 wakeup.tv_usec %= 1000000;
3094 /* Keep waiting until past the time wakeup or any input gets
3095 available. */
3096 while (! detect_input_pending ())
3098 struct timeval current;
3099 struct timeval timeout;
3101 EMACS_GET_TIME (current);
3103 /* Break if result would be negative. */
3104 if (timeval_subtract (&current, wakeup, current))
3105 break;
3107 /* How long `select' should wait. */
3108 timeout.tv_sec = 0;
3109 timeout.tv_usec = 10000;
3111 /* Try to wait that long--but we might wake up sooner. */
3112 select (0, NULL, NULL, NULL, &timeout);
3116 /* If window is tall, flash top and bottom line. */
3117 if (height > 3 * FRAME_LINE_HEIGHT (f))
3119 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3120 flash_left,
3121 (FRAME_INTERNAL_BORDER_WIDTH (f)
3122 + FRAME_TOP_MARGIN_HEIGHT (f)),
3123 width, flash_height);
3124 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3125 flash_left,
3126 (height - flash_height
3127 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3128 width, flash_height);
3130 else
3131 /* If it is short, flash it all. */
3132 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3133 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3134 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3136 XFreeGC (FRAME_X_DISPLAY (f), gc);
3137 x_flush (f);
3141 UNBLOCK_INPUT;
3144 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3147 static void
3148 XTtoggle_invisible_pointer (f, invisible)
3149 FRAME_PTR f;
3150 int invisible;
3152 BLOCK_INPUT;
3153 if (invisible)
3155 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor != 0)
3156 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3157 FRAME_X_DISPLAY_INFO (f)->invisible_cursor);
3159 else
3160 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3161 f->output_data.x->current_cursor);
3162 f->pointer_invisible = invisible;
3163 UNBLOCK_INPUT;
3167 /* Make audible bell. */
3169 void
3170 XTring_bell ()
3172 struct frame *f = SELECTED_FRAME ();
3174 if (FRAME_X_DISPLAY (f))
3176 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3177 if (visible_bell)
3178 XTflash (f);
3179 else
3180 #endif
3182 BLOCK_INPUT;
3183 XBell (FRAME_X_DISPLAY (f), 0);
3184 XFlush (FRAME_X_DISPLAY (f));
3185 UNBLOCK_INPUT;
3191 /* Specify how many text lines, from the top of the window,
3192 should be affected by insert-lines and delete-lines operations.
3193 This, and those operations, are used only within an update
3194 that is bounded by calls to x_update_begin and x_update_end. */
3196 static void
3197 XTset_terminal_window (n)
3198 register int n;
3200 /* This function intentionally left blank. */
3205 /***********************************************************************
3206 Line Dance
3207 ***********************************************************************/
3209 /* Perform an insert-lines or delete-lines operation, inserting N
3210 lines or deleting -N lines at vertical position VPOS. */
3212 static void
3213 x_ins_del_lines (f, vpos, n)
3214 struct frame *f;
3215 int vpos, n;
3217 abort ();
3221 /* Scroll part of the display as described by RUN. */
3223 static void
3224 x_scroll_run (w, run)
3225 struct window *w;
3226 struct run *run;
3228 struct frame *f = XFRAME (w->frame);
3229 int x, y, width, height, from_y, to_y, bottom_y;
3231 /* Get frame-relative bounding box of the text display area of W,
3232 without mode lines. Include in this box the left and right
3233 fringe of W. */
3234 window_box (w, -1, &x, &y, &width, &height);
3236 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3237 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3238 bottom_y = y + height;
3240 if (to_y < from_y)
3242 /* Scrolling up. Make sure we don't copy part of the mode
3243 line at the bottom. */
3244 if (from_y + run->height > bottom_y)
3245 height = bottom_y - from_y;
3246 else
3247 height = run->height;
3249 else
3251 /* Scolling down. Make sure we don't copy over the mode line.
3252 at the bottom. */
3253 if (to_y + run->height > bottom_y)
3254 height = bottom_y - to_y;
3255 else
3256 height = run->height;
3259 BLOCK_INPUT;
3261 /* Cursor off. Will be switched on again in x_update_window_end. */
3262 updated_window = w;
3263 x_clear_cursor (w);
3265 XCopyArea (FRAME_X_DISPLAY (f),
3266 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3267 f->output_data.x->normal_gc,
3268 x, from_y,
3269 width, height,
3270 x, to_y);
3272 UNBLOCK_INPUT;
3277 /***********************************************************************
3278 Exposure Events
3279 ***********************************************************************/
3282 static void
3283 frame_highlight (f)
3284 struct frame *f;
3286 /* We used to only do this if Vx_no_window_manager was non-nil, but
3287 the ICCCM (section 4.1.6) says that the window's border pixmap
3288 and border pixel are window attributes which are "private to the
3289 client", so we can always change it to whatever we want. */
3290 BLOCK_INPUT;
3291 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3292 f->output_data.x->border_pixel);
3293 UNBLOCK_INPUT;
3294 x_update_cursor (f, 1);
3295 x_set_frame_alpha (f);
3298 static void
3299 frame_unhighlight (f)
3300 struct frame *f;
3302 /* We used to only do this if Vx_no_window_manager was non-nil, but
3303 the ICCCM (section 4.1.6) says that the window's border pixmap
3304 and border pixel are window attributes which are "private to the
3305 client", so we can always change it to whatever we want. */
3306 BLOCK_INPUT;
3307 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3308 f->output_data.x->border_tile);
3309 UNBLOCK_INPUT;
3310 x_update_cursor (f, 1);
3311 x_set_frame_alpha (f);
3314 /* The focus has changed. Update the frames as necessary to reflect
3315 the new situation. Note that we can't change the selected frame
3316 here, because the Lisp code we are interrupting might become confused.
3317 Each event gets marked with the frame in which it occurred, so the
3318 Lisp code can tell when the switch took place by examining the events. */
3320 static void
3321 x_new_focus_frame (dpyinfo, frame)
3322 struct x_display_info *dpyinfo;
3323 struct frame *frame;
3325 struct frame *old_focus = dpyinfo->x_focus_frame;
3327 if (frame != dpyinfo->x_focus_frame)
3329 /* Set this before calling other routines, so that they see
3330 the correct value of x_focus_frame. */
3331 dpyinfo->x_focus_frame = frame;
3333 if (old_focus && old_focus->auto_lower)
3334 x_lower_frame (old_focus);
3336 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3337 pending_autoraise_frame = dpyinfo->x_focus_frame;
3338 else
3339 pending_autoraise_frame = 0;
3342 x_frame_rehighlight (dpyinfo);
3345 /* Handle FocusIn and FocusOut state changes for FRAME.
3346 If FRAME has focus and there exists more than one frame, puts
3347 a FOCUS_IN_EVENT into *BUFP. */
3349 static void
3350 x_focus_changed (type, state, dpyinfo, frame, bufp)
3351 int type;
3352 int state;
3353 struct x_display_info *dpyinfo;
3354 struct frame *frame;
3355 struct input_event *bufp;
3357 if (type == FocusIn)
3359 if (dpyinfo->x_focus_event_frame != frame)
3361 x_new_focus_frame (dpyinfo, frame);
3362 dpyinfo->x_focus_event_frame = frame;
3364 /* Don't stop displaying the initial startup message
3365 for a switch-frame event we don't need. */
3366 if (NILP (Vterminal_frame)
3367 && CONSP (Vframe_list)
3368 && !NILP (XCDR (Vframe_list)))
3370 bufp->kind = FOCUS_IN_EVENT;
3371 XSETFRAME (bufp->frame_or_window, frame);
3375 frame->output_data.x->focus_state |= state;
3377 #ifdef HAVE_X_I18N
3378 if (FRAME_XIC (frame))
3379 XSetICFocus (FRAME_XIC (frame));
3380 #endif
3382 else if (type == FocusOut)
3384 frame->output_data.x->focus_state &= ~state;
3386 if (dpyinfo->x_focus_event_frame == frame)
3388 dpyinfo->x_focus_event_frame = 0;
3389 x_new_focus_frame (dpyinfo, 0);
3392 #ifdef HAVE_X_I18N
3393 if (FRAME_XIC (frame))
3394 XUnsetICFocus (FRAME_XIC (frame));
3395 #endif
3396 if (frame->pointer_invisible)
3397 XTtoggle_invisible_pointer (frame, 0);
3401 /* The focus may have changed. Figure out if it is a real focus change,
3402 by checking both FocusIn/Out and Enter/LeaveNotify events.
3404 Returns FOCUS_IN_EVENT event in *BUFP. */
3406 static void
3407 x_detect_focus_change (dpyinfo, event, bufp)
3408 struct x_display_info *dpyinfo;
3409 XEvent *event;
3410 struct input_event *bufp;
3412 struct frame *frame;
3414 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3415 if (! frame)
3416 return;
3418 switch (event->type)
3420 case EnterNotify:
3421 case LeaveNotify:
3423 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3424 int focus_state
3425 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3427 if (event->xcrossing.detail != NotifyInferior
3428 && event->xcrossing.focus
3429 && ! (focus_state & FOCUS_EXPLICIT))
3430 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3431 FOCUS_IMPLICIT,
3432 dpyinfo, frame, bufp);
3434 break;
3436 case FocusIn:
3437 case FocusOut:
3438 x_focus_changed (event->type,
3439 (event->xfocus.detail == NotifyPointer ?
3440 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3441 dpyinfo, frame, bufp);
3442 break;
3444 case ClientMessage:
3445 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3447 enum xembed_message msg = event->xclient.data.l[1];
3448 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3449 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3451 break;
3456 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3458 void
3459 x_mouse_leave (dpyinfo)
3460 struct x_display_info *dpyinfo;
3462 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3465 /* The focus has changed, or we have redirected a frame's focus to
3466 another frame (this happens when a frame uses a surrogate
3467 mini-buffer frame). Shift the highlight as appropriate.
3469 The FRAME argument doesn't necessarily have anything to do with which
3470 frame is being highlighted or un-highlighted; we only use it to find
3471 the appropriate X display info. */
3473 static void
3474 XTframe_rehighlight (frame)
3475 struct frame *frame;
3477 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3480 static void
3481 x_frame_rehighlight (dpyinfo)
3482 struct x_display_info *dpyinfo;
3484 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3486 if (dpyinfo->x_focus_frame)
3488 dpyinfo->x_highlight_frame
3489 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3490 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3491 : dpyinfo->x_focus_frame);
3492 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3494 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3495 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3498 else
3499 dpyinfo->x_highlight_frame = 0;
3501 if (dpyinfo->x_highlight_frame != old_highlight)
3503 if (old_highlight)
3504 frame_unhighlight (old_highlight);
3505 if (dpyinfo->x_highlight_frame)
3506 frame_highlight (dpyinfo->x_highlight_frame);
3512 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3514 /* Initialize mode_switch_bit and modifier_meaning. */
3515 static void
3516 x_find_modifier_meanings (dpyinfo)
3517 struct x_display_info *dpyinfo;
3519 int min_code, max_code;
3520 KeySym *syms;
3521 int syms_per_code;
3522 XModifierKeymap *mods;
3524 dpyinfo->meta_mod_mask = 0;
3525 dpyinfo->shift_lock_mask = 0;
3526 dpyinfo->alt_mod_mask = 0;
3527 dpyinfo->super_mod_mask = 0;
3528 dpyinfo->hyper_mod_mask = 0;
3530 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3532 syms = XGetKeyboardMapping (dpyinfo->display,
3533 min_code, max_code - min_code + 1,
3534 &syms_per_code);
3535 mods = XGetModifierMapping (dpyinfo->display);
3537 /* Scan the modifier table to see which modifier bits the Meta and
3538 Alt keysyms are on. */
3540 int row, col; /* The row and column in the modifier table. */
3541 int found_alt_or_meta;
3543 for (row = 3; row < 8; row++)
3545 found_alt_or_meta = 0;
3546 for (col = 0; col < mods->max_keypermod; col++)
3548 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3550 /* Zeroes are used for filler. Skip them. */
3551 if (code == 0)
3552 continue;
3554 /* Are any of this keycode's keysyms a meta key? */
3556 int code_col;
3558 for (code_col = 0; code_col < syms_per_code; code_col++)
3560 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3562 switch (sym)
3564 case XK_Meta_L:
3565 case XK_Meta_R:
3566 found_alt_or_meta = 1;
3567 dpyinfo->meta_mod_mask |= (1 << row);
3568 break;
3570 case XK_Alt_L:
3571 case XK_Alt_R:
3572 found_alt_or_meta = 1;
3573 dpyinfo->alt_mod_mask |= (1 << row);
3574 break;
3576 case XK_Hyper_L:
3577 case XK_Hyper_R:
3578 if (!found_alt_or_meta)
3579 dpyinfo->hyper_mod_mask |= (1 << row);
3580 code_col = syms_per_code;
3581 col = mods->max_keypermod;
3582 break;
3584 case XK_Super_L:
3585 case XK_Super_R:
3586 if (!found_alt_or_meta)
3587 dpyinfo->super_mod_mask |= (1 << row);
3588 code_col = syms_per_code;
3589 col = mods->max_keypermod;
3590 break;
3592 case XK_Shift_Lock:
3593 /* Ignore this if it's not on the lock modifier. */
3594 if (!found_alt_or_meta && ((1 << row) == LockMask))
3595 dpyinfo->shift_lock_mask = LockMask;
3596 code_col = syms_per_code;
3597 col = mods->max_keypermod;
3598 break;
3606 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3607 if (! dpyinfo->meta_mod_mask)
3609 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3610 dpyinfo->alt_mod_mask = 0;
3613 /* If some keys are both alt and meta,
3614 make them just meta, not alt. */
3615 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3617 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3620 XFree ((char *) syms);
3621 XFreeModifiermap (mods);
3624 /* Convert between the modifier bits X uses and the modifier bits
3625 Emacs uses. */
3627 unsigned int
3628 x_x_to_emacs_modifiers (dpyinfo, state)
3629 struct x_display_info *dpyinfo;
3630 unsigned int state;
3632 EMACS_UINT mod_meta = meta_modifier;
3633 EMACS_UINT mod_alt = alt_modifier;
3634 EMACS_UINT mod_hyper = hyper_modifier;
3635 EMACS_UINT mod_super = super_modifier;
3636 Lisp_Object tem;
3638 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3639 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3640 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3641 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3642 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3643 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3644 tem = Fget (Vx_super_keysym, Qmodifier_value);
3645 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3648 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3649 | ((state & ControlMask) ? ctrl_modifier : 0)
3650 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3651 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3652 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3653 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3656 static unsigned int
3657 x_emacs_to_x_modifiers (dpyinfo, state)
3658 struct x_display_info *dpyinfo;
3659 unsigned int state;
3661 EMACS_UINT mod_meta = meta_modifier;
3662 EMACS_UINT mod_alt = alt_modifier;
3663 EMACS_UINT mod_hyper = hyper_modifier;
3664 EMACS_UINT mod_super = super_modifier;
3666 Lisp_Object tem;
3668 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3669 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3670 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3671 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3672 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3673 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3674 tem = Fget (Vx_super_keysym, Qmodifier_value);
3675 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3678 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3679 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3680 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3681 | ((state & shift_modifier) ? ShiftMask : 0)
3682 | ((state & ctrl_modifier) ? ControlMask : 0)
3683 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3686 /* Convert a keysym to its name. */
3688 char *
3689 x_get_keysym_name (keysym)
3690 KeySym keysym;
3692 char *value;
3694 BLOCK_INPUT;
3695 value = XKeysymToString (keysym);
3696 UNBLOCK_INPUT;
3698 return value;
3703 /* Mouse clicks and mouse movement. Rah. */
3705 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3707 If the event is a button press, then note that we have grabbed
3708 the mouse. */
3710 static Lisp_Object
3711 construct_mouse_click (result, event, f)
3712 struct input_event *result;
3713 XButtonEvent *event;
3714 struct frame *f;
3716 /* Make the event type NO_EVENT; we'll change that when we decide
3717 otherwise. */
3718 result->kind = MOUSE_CLICK_EVENT;
3719 result->code = event->button - Button1;
3720 result->timestamp = event->time;
3721 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3722 event->state)
3723 | (event->type == ButtonRelease
3724 ? up_modifier
3725 : down_modifier));
3727 XSETINT (result->x, event->x);
3728 XSETINT (result->y, event->y);
3729 XSETFRAME (result->frame_or_window, f);
3730 result->arg = Qnil;
3731 return Qnil;
3735 /* Function to report a mouse movement to the mainstream Emacs code.
3736 The input handler calls this.
3738 We have received a mouse movement event, which is given in *event.
3739 If the mouse is over a different glyph than it was last time, tell
3740 the mainstream emacs code by setting mouse_moved. If not, ask for
3741 another motion event, so we can check again the next time it moves. */
3743 static XMotionEvent last_mouse_motion_event;
3744 static Lisp_Object last_mouse_motion_frame;
3746 static int
3747 note_mouse_movement (frame, event)
3748 FRAME_PTR frame;
3749 XMotionEvent *event;
3751 last_mouse_movement_time = event->time;
3752 last_mouse_motion_event = *event;
3753 XSETFRAME (last_mouse_motion_frame, frame);
3755 if (!FRAME_X_OUTPUT (frame))
3756 return 0;
3758 if (event->window != FRAME_X_WINDOW (frame))
3760 frame->mouse_moved = 1;
3761 last_mouse_scroll_bar = Qnil;
3762 note_mouse_highlight (frame, -1, -1);
3763 last_mouse_glyph_frame = 0;
3764 return 1;
3768 /* Has the mouse moved off the glyph it was on at the last sighting? */
3769 if (frame != last_mouse_glyph_frame
3770 || event->x < last_mouse_glyph.x
3771 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3772 || event->y < last_mouse_glyph.y
3773 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3775 frame->mouse_moved = 1;
3776 last_mouse_scroll_bar = Qnil;
3777 note_mouse_highlight (frame, event->x, event->y);
3778 /* Remember which glyph we're now on. */
3779 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3780 last_mouse_glyph_frame = frame;
3781 return 1;
3784 return 0;
3788 /************************************************************************
3789 Mouse Face
3790 ************************************************************************/
3792 static void
3793 redo_mouse_highlight ()
3795 if (!NILP (last_mouse_motion_frame)
3796 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3797 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3798 last_mouse_motion_event.x,
3799 last_mouse_motion_event.y);
3804 /* Return the current position of the mouse.
3805 *FP should be a frame which indicates which display to ask about.
3807 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3808 and *PART to the frame, window, and scroll bar part that the mouse
3809 is over. Set *X and *Y to the portion and whole of the mouse's
3810 position on the scroll bar.
3812 If the mouse movement started elsewhere, set *FP to the frame the
3813 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3814 the mouse is over.
3816 Set *TIME to the server time-stamp for the time at which the mouse
3817 was at this position.
3819 Don't store anything if we don't have a valid set of values to report.
3821 This clears the mouse_moved flag, so we can wait for the next mouse
3822 movement. */
3824 static void
3825 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3826 FRAME_PTR *fp;
3827 int insist;
3828 Lisp_Object *bar_window;
3829 enum scroll_bar_part *part;
3830 Lisp_Object *x, *y;
3831 unsigned long *time;
3833 FRAME_PTR f1;
3835 BLOCK_INPUT;
3837 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3838 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3839 else
3841 Window root;
3842 int root_x, root_y;
3844 Window dummy_window;
3845 int dummy;
3847 Lisp_Object frame, tail;
3849 /* Clear the mouse-moved flag for every frame on this display. */
3850 FOR_EACH_FRAME (tail, frame)
3851 if (FRAME_X_P (XFRAME (frame))
3852 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3853 XFRAME (frame)->mouse_moved = 0;
3855 last_mouse_scroll_bar = Qnil;
3857 /* Figure out which root window we're on. */
3858 XQueryPointer (FRAME_X_DISPLAY (*fp),
3859 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3861 /* The root window which contains the pointer. */
3862 &root,
3864 /* Trash which we can't trust if the pointer is on
3865 a different screen. */
3866 &dummy_window,
3868 /* The position on that root window. */
3869 &root_x, &root_y,
3871 /* More trash we can't trust. */
3872 &dummy, &dummy,
3874 /* Modifier keys and pointer buttons, about which
3875 we don't care. */
3876 (unsigned int *) &dummy);
3878 /* Now we have a position on the root; find the innermost window
3879 containing the pointer. */
3881 Window win, child;
3882 int win_x, win_y;
3883 int parent_x = 0, parent_y = 0;
3885 win = root;
3887 /* XTranslateCoordinates can get errors if the window
3888 structure is changing at the same time this function
3889 is running. So at least we must not crash from them. */
3891 x_catch_errors (FRAME_X_DISPLAY (*fp));
3893 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3894 && FRAME_LIVE_P (last_mouse_frame))
3896 /* If mouse was grabbed on a frame, give coords for that frame
3897 even if the mouse is now outside it. */
3898 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3900 /* From-window, to-window. */
3901 root, FRAME_X_WINDOW (last_mouse_frame),
3903 /* From-position, to-position. */
3904 root_x, root_y, &win_x, &win_y,
3906 /* Child of win. */
3907 &child);
3908 f1 = last_mouse_frame;
3910 else
3912 while (1)
3914 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3916 /* From-window, to-window. */
3917 root, win,
3919 /* From-position, to-position. */
3920 root_x, root_y, &win_x, &win_y,
3922 /* Child of win. */
3923 &child);
3925 if (child == None || child == win)
3926 break;
3927 #ifdef USE_GTK
3928 /* We don't wan't to know the innermost window. We
3929 want the edit window. For non-Gtk+ the innermost
3930 window is the edit window. For Gtk+ it might not
3931 be. It might be the tool bar for example. */
3932 if (x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win))
3933 break;
3934 #endif
3935 win = child;
3936 parent_x = win_x;
3937 parent_y = win_y;
3940 /* Now we know that:
3941 win is the innermost window containing the pointer
3942 (XTC says it has no child containing the pointer),
3943 win_x and win_y are the pointer's position in it
3944 (XTC did this the last time through), and
3945 parent_x and parent_y are the pointer's position in win's parent.
3946 (They are what win_x and win_y were when win was child.
3947 If win is the root window, it has no parent, and
3948 parent_{x,y} are invalid, but that's okay, because we'll
3949 never use them in that case.) */
3951 #ifdef USE_GTK
3952 /* We don't wan't to know the innermost window. We
3953 want the edit window. */
3954 f1 = x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3955 #else
3956 /* Is win one of our frames? */
3957 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3958 #endif
3960 #ifdef USE_X_TOOLKIT
3961 /* If we end up with the menu bar window, say it's not
3962 on the frame. */
3963 if (f1 != NULL
3964 && f1->output_data.x->menubar_widget
3965 && win == XtWindow (f1->output_data.x->menubar_widget))
3966 f1 = NULL;
3967 #endif /* USE_X_TOOLKIT */
3970 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3971 f1 = 0;
3973 x_uncatch_errors ();
3975 /* If not, is it one of our scroll bars? */
3976 if (! f1)
3978 struct scroll_bar *bar;
3980 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3982 if (bar)
3984 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3985 win_x = parent_x;
3986 win_y = parent_y;
3990 if (f1 == 0 && insist > 0)
3991 f1 = SELECTED_FRAME ();
3993 if (f1)
3995 /* Ok, we found a frame. Store all the values.
3996 last_mouse_glyph is a rectangle used to reduce the
3997 generation of mouse events. To not miss any motion
3998 events, we must divide the frame into rectangles of the
3999 size of the smallest character that could be displayed
4000 on it, i.e. into the same rectangles that matrices on
4001 the frame are divided into. */
4003 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
4004 last_mouse_glyph_frame = f1;
4006 *bar_window = Qnil;
4007 *part = 0;
4008 *fp = f1;
4009 XSETINT (*x, win_x);
4010 XSETINT (*y, win_y);
4011 *time = last_mouse_movement_time;
4016 UNBLOCK_INPUT;
4021 /***********************************************************************
4022 Scroll bars
4023 ***********************************************************************/
4025 /* Scroll bar support. */
4027 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4028 manages it.
4029 This can be called in GC, so we have to make sure to strip off mark
4030 bits. */
4032 static struct scroll_bar *
4033 x_window_to_scroll_bar (display, window_id)
4034 Display *display;
4035 Window window_id;
4037 Lisp_Object tail;
4039 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4040 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4041 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4043 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4045 Lisp_Object frame, bar, condemned;
4047 frame = XCAR (tail);
4048 /* All elements of Vframe_list should be frames. */
4049 if (! FRAMEP (frame))
4050 abort ();
4052 if (! FRAME_X_P (XFRAME (frame)))
4053 continue;
4055 /* Scan this frame's scroll bar list for a scroll bar with the
4056 right window ID. */
4057 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4058 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4059 /* This trick allows us to search both the ordinary and
4060 condemned scroll bar lists with one loop. */
4061 ! NILP (bar) || (bar = condemned,
4062 condemned = Qnil,
4063 ! NILP (bar));
4064 bar = XSCROLL_BAR (bar)->next)
4065 if (XSCROLL_BAR (bar)->x_window == window_id &&
4066 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4067 return XSCROLL_BAR (bar);
4070 return 0;
4074 #if defined USE_LUCID
4076 /* Return the Lucid menu bar WINDOW is part of. Return null
4077 if WINDOW is not part of a menu bar. */
4079 static Widget
4080 x_window_to_menu_bar (window)
4081 Window window;
4083 Lisp_Object tail;
4085 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4087 if (FRAME_X_P (XFRAME (XCAR (tail))))
4089 Lisp_Object frame = XCAR (tail);
4090 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4092 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4093 return menu_bar;
4097 return NULL;
4100 #endif /* USE_LUCID */
4103 /************************************************************************
4104 Toolkit scroll bars
4105 ************************************************************************/
4107 #ifdef USE_TOOLKIT_SCROLL_BARS
4109 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4110 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4111 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4112 struct scroll_bar *));
4113 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4114 int, int, int));
4117 /* Lisp window being scrolled. Set when starting to interact with
4118 a toolkit scroll bar, reset to nil when ending the interaction. */
4120 static Lisp_Object window_being_scrolled;
4122 /* Last scroll bar part sent in xm_scroll_callback. */
4124 static int last_scroll_bar_part;
4126 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4127 that movements of 1/20 of the screen size are mapped to up/down. */
4129 #ifndef USE_GTK
4130 /* Id of action hook installed for scroll bars. */
4132 static XtActionHookId action_hook_id;
4134 static Boolean xaw3d_arrow_scroll;
4136 /* Whether the drag scrolling maintains the mouse at the top of the
4137 thumb. If not, resizing the thumb needs to be done more carefully
4138 to avoid jerkyness. */
4140 static Boolean xaw3d_pick_top;
4142 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4143 bars are used.. The hook is responsible for detecting when
4144 the user ends an interaction with the scroll bar, and generates
4145 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4147 static void
4148 xt_action_hook (widget, client_data, action_name, event, params,
4149 num_params)
4150 Widget widget;
4151 XtPointer client_data;
4152 String action_name;
4153 XEvent *event;
4154 String *params;
4155 Cardinal *num_params;
4157 int scroll_bar_p;
4158 char *end_action;
4160 #ifdef USE_MOTIF
4161 scroll_bar_p = XmIsScrollBar (widget);
4162 end_action = "Release";
4163 #else /* !USE_MOTIF i.e. use Xaw */
4164 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4165 end_action = "EndScroll";
4166 #endif /* USE_MOTIF */
4168 if (scroll_bar_p
4169 && strcmp (action_name, end_action) == 0
4170 && WINDOWP (window_being_scrolled))
4172 struct window *w;
4174 x_send_scroll_bar_event (window_being_scrolled,
4175 scroll_bar_end_scroll, 0, 0);
4176 w = XWINDOW (window_being_scrolled);
4178 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4180 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4181 /* The thumb size is incorrect while dragging: fix it. */
4182 set_vertical_scroll_bar (w);
4184 window_being_scrolled = Qnil;
4185 last_scroll_bar_part = -1;
4187 /* Xt timeouts no longer needed. */
4188 toolkit_scroll_bar_interaction = 0;
4191 #endif /* not USE_GTK */
4193 /* A vector of windows used for communication between
4194 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4196 static struct window **scroll_bar_windows;
4197 static int scroll_bar_windows_size;
4200 /* Send a client message with message type Xatom_Scrollbar for a
4201 scroll action to the frame of WINDOW. PART is a value identifying
4202 the part of the scroll bar that was clicked on. PORTION is the
4203 amount to scroll of a whole of WHOLE. */
4205 static void
4206 x_send_scroll_bar_event (window, part, portion, whole)
4207 Lisp_Object window;
4208 int part, portion, whole;
4210 XEvent event;
4211 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4212 struct window *w = XWINDOW (window);
4213 struct frame *f = XFRAME (w->frame);
4214 int i;
4216 BLOCK_INPUT;
4218 /* Construct a ClientMessage event to send to the frame. */
4219 ev->type = ClientMessage;
4220 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4221 ev->display = FRAME_X_DISPLAY (f);
4222 ev->window = FRAME_X_WINDOW (f);
4223 ev->format = 32;
4225 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4226 not enough to store a pointer or Lisp_Object on a 64 bit system.
4227 So, store the window in scroll_bar_windows and pass the index
4228 into that array in the event. */
4229 for (i = 0; i < scroll_bar_windows_size; ++i)
4230 if (scroll_bar_windows[i] == NULL)
4231 break;
4233 if (i == scroll_bar_windows_size)
4235 int new_size = max (10, 2 * scroll_bar_windows_size);
4236 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4237 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4239 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4240 nbytes);
4241 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4242 scroll_bar_windows_size = new_size;
4245 scroll_bar_windows[i] = w;
4246 ev->data.l[0] = (long) i;
4247 ev->data.l[1] = (long) part;
4248 ev->data.l[2] = (long) 0;
4249 ev->data.l[3] = (long) portion;
4250 ev->data.l[4] = (long) whole;
4252 /* Make Xt timeouts work while the scroll bar is active. */
4253 toolkit_scroll_bar_interaction = 1;
4254 #ifdef USE_X_TOOLKIT
4255 x_activate_timeout_atimer ();
4256 #endif
4258 /* Setting the event mask to zero means that the message will
4259 be sent to the client that created the window, and if that
4260 window no longer exists, no event will be sent. */
4261 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4262 UNBLOCK_INPUT;
4266 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4267 in *IEVENT. */
4269 static void
4270 x_scroll_bar_to_input_event (event, ievent)
4271 XEvent *event;
4272 struct input_event *ievent;
4274 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4275 Lisp_Object window;
4276 struct frame *f;
4277 struct window *w;
4279 w = scroll_bar_windows[ev->data.l[0]];
4280 scroll_bar_windows[ev->data.l[0]] = NULL;
4282 XSETWINDOW (window, w);
4283 f = XFRAME (w->frame);
4285 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4286 ievent->frame_or_window = window;
4287 ievent->arg = Qnil;
4288 #ifdef USE_GTK
4289 ievent->timestamp = CurrentTime;
4290 #else
4291 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4292 #endif
4293 ievent->part = ev->data.l[1];
4294 ievent->code = ev->data.l[2];
4295 ievent->x = make_number ((int) ev->data.l[3]);
4296 ievent->y = make_number ((int) ev->data.l[4]);
4297 ievent->modifiers = 0;
4301 #ifdef USE_MOTIF
4303 /* Minimum and maximum values used for Motif scroll bars. */
4305 #define XM_SB_MAX 10000000
4308 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4309 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4310 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4312 static void
4313 xm_scroll_callback (widget, client_data, call_data)
4314 Widget widget;
4315 XtPointer client_data, call_data;
4317 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4318 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4319 int part = -1, whole = 0, portion = 0;
4321 switch (cs->reason)
4323 case XmCR_DECREMENT:
4324 bar->dragging = Qnil;
4325 part = scroll_bar_up_arrow;
4326 break;
4328 case XmCR_INCREMENT:
4329 bar->dragging = Qnil;
4330 part = scroll_bar_down_arrow;
4331 break;
4333 case XmCR_PAGE_DECREMENT:
4334 bar->dragging = Qnil;
4335 part = scroll_bar_above_handle;
4336 break;
4338 case XmCR_PAGE_INCREMENT:
4339 bar->dragging = Qnil;
4340 part = scroll_bar_below_handle;
4341 break;
4343 case XmCR_TO_TOP:
4344 bar->dragging = Qnil;
4345 part = scroll_bar_to_top;
4346 break;
4348 case XmCR_TO_BOTTOM:
4349 bar->dragging = Qnil;
4350 part = scroll_bar_to_bottom;
4351 break;
4353 case XmCR_DRAG:
4355 int slider_size;
4357 /* Get the slider size. */
4358 BLOCK_INPUT;
4359 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4360 UNBLOCK_INPUT;
4362 whole = XM_SB_MAX - slider_size;
4363 portion = min (cs->value, whole);
4364 part = scroll_bar_handle;
4365 bar->dragging = make_number (cs->value);
4367 break;
4369 case XmCR_VALUE_CHANGED:
4370 break;
4373 if (part >= 0)
4375 window_being_scrolled = bar->window;
4376 last_scroll_bar_part = part;
4377 x_send_scroll_bar_event (bar->window, part, portion, whole);
4381 #elif defined USE_GTK
4383 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4384 bar widget. DATA is a pointer to the scroll_bar structure. */
4386 static gboolean
4387 xg_scroll_callback (GtkRange *range,
4388 GtkScrollType scroll,
4389 gdouble value,
4390 gpointer user_data)
4392 struct scroll_bar *bar = (struct scroll_bar *) user_data;
4393 gdouble position;
4394 int part = -1, whole = 0, portion = 0;
4395 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4396 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4398 if (xg_ignore_gtk_scrollbar) return FALSE;
4399 position = gtk_adjustment_get_value (adj);
4402 switch (scroll)
4404 case GTK_SCROLL_JUMP:
4405 /* Buttons 1 2 or 3 must be grabbed. */
4406 if (FRAME_X_DISPLAY_INFO (f)->grabbed != 0
4407 && FRAME_X_DISPLAY_INFO (f)->grabbed < (1 << 4))
4409 part = scroll_bar_handle;
4410 whole = adj->upper - adj->page_size;
4411 portion = min ((int)position, whole);
4412 bar->dragging = make_number ((int)portion);
4414 break;
4415 case GTK_SCROLL_STEP_BACKWARD:
4416 part = scroll_bar_up_arrow;
4417 bar->dragging = Qnil;
4418 break;
4419 case GTK_SCROLL_STEP_FORWARD:
4420 part = scroll_bar_down_arrow;
4421 bar->dragging = Qnil;
4422 break;
4423 case GTK_SCROLL_PAGE_BACKWARD:
4424 part = scroll_bar_above_handle;
4425 bar->dragging = Qnil;
4426 break;
4427 case GTK_SCROLL_PAGE_FORWARD:
4428 part = scroll_bar_below_handle;
4429 bar->dragging = Qnil;
4430 break;
4433 if (part >= 0)
4435 window_being_scrolled = bar->window;
4436 last_scroll_bar_part = part;
4437 x_send_scroll_bar_event (bar->window, part, portion, whole);
4440 return FALSE;
4443 /* Callback for button release. Sets dragging to Qnil when dragging is done. */
4445 static gboolean
4446 xg_end_scroll_callback (GtkWidget *widget,
4447 GdkEventButton *event,
4448 gpointer user_data)
4450 struct scroll_bar *bar = (struct scroll_bar *) user_data;
4451 bar->dragging = Qnil;
4452 if (WINDOWP (window_being_scrolled))
4454 x_send_scroll_bar_event (window_being_scrolled,
4455 scroll_bar_end_scroll, 0, 0);
4456 window_being_scrolled = Qnil;
4459 return FALSE;
4463 #else /* not USE_GTK and not USE_MOTIF */
4465 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4466 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4467 scroll bar struct. CALL_DATA is a pointer to a float saying where
4468 the thumb is. */
4470 static void
4471 xaw_jump_callback (widget, client_data, call_data)
4472 Widget widget;
4473 XtPointer client_data, call_data;
4475 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4476 float top = *(float *) call_data;
4477 float shown;
4478 int whole, portion, height;
4479 int part;
4481 /* Get the size of the thumb, a value between 0 and 1. */
4482 BLOCK_INPUT;
4483 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4484 UNBLOCK_INPUT;
4486 whole = 10000000;
4487 portion = shown < 1 ? top * whole : 0;
4489 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4490 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4491 the bottom, so we force the scrolling whenever we see that we're
4492 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4493 we try to ensure that we always stay two pixels away from the
4494 bottom). */
4495 part = scroll_bar_down_arrow;
4496 else
4497 part = scroll_bar_handle;
4499 window_being_scrolled = bar->window;
4500 bar->dragging = make_number (portion);
4501 last_scroll_bar_part = part;
4502 x_send_scroll_bar_event (bar->window, part, portion, whole);
4506 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4507 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4508 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4509 the scroll bar. CALL_DATA is an integer specifying the action that
4510 has taken place. Its magnitude is in the range 0..height of the
4511 scroll bar. Negative values mean scroll towards buffer start.
4512 Values < height of scroll bar mean line-wise movement. */
4514 static void
4515 xaw_scroll_callback (widget, client_data, call_data)
4516 Widget widget;
4517 XtPointer client_data, call_data;
4519 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4520 /* The position really is stored cast to a pointer. */
4521 int position = (long) call_data;
4522 Dimension height;
4523 int part;
4525 /* Get the height of the scroll bar. */
4526 BLOCK_INPUT;
4527 XtVaGetValues (widget, XtNheight, &height, NULL);
4528 UNBLOCK_INPUT;
4530 if (eabs (position) >= height)
4531 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4533 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4534 it maps line-movement to call_data = max(5, height/20). */
4535 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4536 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4537 else
4538 part = scroll_bar_move_ratio;
4540 window_being_scrolled = bar->window;
4541 bar->dragging = Qnil;
4542 last_scroll_bar_part = part;
4543 x_send_scroll_bar_event (bar->window, part, position, height);
4546 #endif /* not USE_GTK and not USE_MOTIF */
4548 #define SCROLL_BAR_NAME "verticalScrollBar"
4550 /* Create the widget for scroll bar BAR on frame F. Record the widget
4551 and X window of the scroll bar in BAR. */
4553 #ifdef USE_GTK
4554 static void
4555 x_create_toolkit_scroll_bar (f, bar)
4556 struct frame *f;
4557 struct scroll_bar *bar;
4559 char *scroll_bar_name = SCROLL_BAR_NAME;
4561 BLOCK_INPUT;
4562 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4563 G_CALLBACK (xg_end_scroll_callback),
4564 scroll_bar_name);
4565 UNBLOCK_INPUT;
4568 #else /* not USE_GTK */
4570 static void
4571 x_create_toolkit_scroll_bar (f, bar)
4572 struct frame *f;
4573 struct scroll_bar *bar;
4575 Window xwindow;
4576 Widget widget;
4577 Arg av[20];
4578 int ac = 0;
4579 char *scroll_bar_name = SCROLL_BAR_NAME;
4580 unsigned long pixel;
4582 BLOCK_INPUT;
4584 #ifdef USE_MOTIF
4585 /* Set resources. Create the widget. */
4586 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4587 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4588 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4589 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4590 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4591 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4592 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4594 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4595 if (pixel != -1)
4597 XtSetArg (av[ac], XmNforeground, pixel);
4598 ++ac;
4601 pixel = f->output_data.x->scroll_bar_background_pixel;
4602 if (pixel != -1)
4604 XtSetArg (av[ac], XmNbackground, pixel);
4605 ++ac;
4608 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4609 scroll_bar_name, av, ac);
4611 /* Add one callback for everything that can happen. */
4612 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4613 (XtPointer) bar);
4614 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4615 (XtPointer) bar);
4616 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4617 (XtPointer) bar);
4618 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4619 (XtPointer) bar);
4620 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4621 (XtPointer) bar);
4622 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4623 (XtPointer) bar);
4624 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4625 (XtPointer) bar);
4627 /* Realize the widget. Only after that is the X window created. */
4628 XtRealizeWidget (widget);
4630 /* Set the cursor to an arrow. I didn't find a resource to do that.
4631 And I'm wondering why it hasn't an arrow cursor by default. */
4632 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4633 f->output_data.x->nontext_cursor);
4635 #else /* !USE_MOTIF i.e. use Xaw */
4637 /* Set resources. Create the widget. The background of the
4638 Xaw3d scroll bar widget is a little bit light for my taste.
4639 We don't alter it here to let users change it according
4640 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4641 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4642 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4643 /* For smoother scrolling with Xaw3d -sm */
4644 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4646 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4647 if (pixel != -1)
4649 XtSetArg (av[ac], XtNforeground, pixel);
4650 ++ac;
4653 pixel = f->output_data.x->scroll_bar_background_pixel;
4654 if (pixel != -1)
4656 XtSetArg (av[ac], XtNbackground, pixel);
4657 ++ac;
4660 /* Top/bottom shadow colors. */
4662 /* Allocate them, if necessary. */
4663 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4665 pixel = f->output_data.x->scroll_bar_background_pixel;
4666 if (pixel != -1)
4668 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4669 FRAME_X_COLORMAP (f),
4670 &pixel, 1.2, 0x8000))
4671 pixel = -1;
4672 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4675 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4677 pixel = f->output_data.x->scroll_bar_background_pixel;
4678 if (pixel != -1)
4680 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4681 FRAME_X_COLORMAP (f),
4682 &pixel, 0.6, 0x4000))
4683 pixel = -1;
4684 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4688 #ifdef XtNbeNiceToColormap
4689 /* Tell the toolkit about them. */
4690 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4691 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4692 /* We tried to allocate a color for the top/bottom shadow, and
4693 failed, so tell Xaw3d to use dithering instead. */
4695 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4696 ++ac;
4698 else
4699 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4700 be more consistent with other emacs 3d colors, and since Xaw3d is
4701 not good at dealing with allocation failure. */
4703 /* This tells Xaw3d to use real colors instead of dithering for
4704 the shadows. */
4705 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4706 ++ac;
4708 /* Specify the colors. */
4709 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4710 if (pixel != -1)
4712 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4713 ++ac;
4715 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4716 if (pixel != -1)
4718 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4719 ++ac;
4722 #endif
4724 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4725 f->output_data.x->edit_widget, av, ac);
4728 char *initial = "";
4729 char *val = initial;
4730 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4731 #ifdef XtNarrowScrollbars
4732 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4733 #endif
4734 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4735 if (xaw3d_arrow_scroll || val == initial)
4736 { /* ARROW_SCROLL */
4737 xaw3d_arrow_scroll = True;
4738 /* Isn't that just a personal preference ? --Stef */
4739 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4743 /* Define callbacks. */
4744 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4745 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4746 (XtPointer) bar);
4748 /* Realize the widget. Only after that is the X window created. */
4749 XtRealizeWidget (widget);
4751 #endif /* !USE_MOTIF */
4753 /* Install an action hook that lets us detect when the user
4754 finishes interacting with a scroll bar. */
4755 if (action_hook_id == 0)
4756 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4758 /* Remember X window and widget in the scroll bar vector. */
4759 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4760 xwindow = XtWindow (widget);
4761 bar->x_window = xwindow;
4763 UNBLOCK_INPUT;
4765 #endif /* not USE_GTK */
4768 /* Set the thumb size and position of scroll bar BAR. We are currently
4769 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4771 #ifdef USE_GTK
4772 static void
4773 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4774 struct scroll_bar *bar;
4775 int portion, position, whole;
4777 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4780 #else /* not USE_GTK */
4781 static void
4782 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4783 struct scroll_bar *bar;
4784 int portion, position, whole;
4786 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4787 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4788 float top, shown;
4790 BLOCK_INPUT;
4792 #ifdef USE_MOTIF
4794 /* We use an estimate of 30 chars per line rather than the real
4795 `portion' value. This has the disadvantage that the thumb size
4796 is not very representative, but it makes our life a lot easier.
4797 Otherwise, we have to constantly adjust the thumb size, which
4798 we can't always do quickly enough: while dragging, the size of
4799 the thumb might prevent the user from dragging the thumb all the
4800 way to the end. but Motif and some versions of Xaw3d don't allow
4801 updating the thumb size while dragging. Also, even if we can update
4802 its size, the update will often happen too late.
4803 If you don't believe it, check out revision 1.650 of xterm.c to see
4804 what hoops we were going through and the still poor behavior we got. */
4805 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4806 /* When the thumb is at the bottom, position == whole.
4807 So we need to increase `whole' to make space for the thumb. */
4808 whole += portion;
4810 if (whole <= 0)
4811 top = 0, shown = 1;
4812 else
4814 top = (float) position / whole;
4815 shown = (float) portion / whole;
4818 if (NILP (bar->dragging))
4820 int size, value;
4822 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4823 is the scroll bar's maximum and MIN is the scroll bar's minimum
4824 value. */
4825 size = shown * XM_SB_MAX;
4826 size = min (size, XM_SB_MAX);
4827 size = max (size, 1);
4829 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4830 value = top * XM_SB_MAX;
4831 value = min (value, XM_SB_MAX - size);
4833 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4835 #else /* !USE_MOTIF i.e. use Xaw */
4837 if (whole == 0)
4838 top = 0, shown = 1;
4839 else
4841 top = (float) position / whole;
4842 shown = (float) portion / whole;
4846 float old_top, old_shown;
4847 Dimension height;
4848 XtVaGetValues (widget,
4849 XtNtopOfThumb, &old_top,
4850 XtNshown, &old_shown,
4851 XtNheight, &height,
4852 NULL);
4854 /* Massage the top+shown values. */
4855 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4856 top = max (0, min (1, top));
4857 else
4858 top = old_top;
4859 /* Keep two pixels available for moving the thumb down. */
4860 shown = max (0, min (1 - top - (2.0 / height), shown));
4862 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4863 check that your system's configuration file contains a define
4864 for `NARROWPROTO'. See s/freebsd.h for an example. */
4865 if (top != old_top || shown != old_shown)
4867 if (NILP (bar->dragging))
4868 XawScrollbarSetThumb (widget, top, shown);
4869 else
4871 /* Try to make the scrolling a tad smoother. */
4872 if (!xaw3d_pick_top)
4873 shown = min (shown, old_shown);
4875 XawScrollbarSetThumb (widget, top, shown);
4879 #endif /* !USE_MOTIF */
4881 UNBLOCK_INPUT;
4883 #endif /* not USE_GTK */
4885 #endif /* USE_TOOLKIT_SCROLL_BARS */
4889 /************************************************************************
4890 Scroll bars, general
4891 ************************************************************************/
4893 /* Create a scroll bar and return the scroll bar vector for it. W is
4894 the Emacs window on which to create the scroll bar. TOP, LEFT,
4895 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4896 scroll bar. */
4898 static struct scroll_bar *
4899 x_scroll_bar_create (w, top, left, width, height)
4900 struct window *w;
4901 int top, left, width, height;
4903 struct frame *f = XFRAME (w->frame);
4904 struct scroll_bar *bar
4905 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4907 BLOCK_INPUT;
4909 #ifdef USE_TOOLKIT_SCROLL_BARS
4910 x_create_toolkit_scroll_bar (f, bar);
4911 #else /* not USE_TOOLKIT_SCROLL_BARS */
4913 XSetWindowAttributes a;
4914 unsigned long mask;
4915 Window window;
4917 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4918 if (a.background_pixel == -1)
4919 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4921 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4922 | ButtonMotionMask | PointerMotionHintMask
4923 | ExposureMask);
4924 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4926 mask = (CWBackPixel | CWEventMask | CWCursor);
4928 /* Clear the area of W that will serve as a scroll bar. This is
4929 for the case that a window has been split horizontally. In
4930 this case, no clear_frame is generated to reduce flickering. */
4931 if (width > 0 && height > 0)
4932 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4933 left, top, width,
4934 window_box_height (w), False);
4936 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4937 /* Position and size of scroll bar. */
4938 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4939 top,
4940 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4941 height,
4942 /* Border width, depth, class, and visual. */
4944 CopyFromParent,
4945 CopyFromParent,
4946 CopyFromParent,
4947 /* Attributes. */
4948 mask, &a);
4949 bar->x_window = window;
4951 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4953 XSETWINDOW (bar->window, w);
4954 bar->top = top;
4955 bar->left = left;
4956 bar->width = width;
4957 bar->height = height;
4958 bar->start = 0;
4959 bar->end = 0;
4960 bar->dragging = Qnil;
4961 bar->fringe_extended_p = 0;
4963 /* Add bar to its frame's list of scroll bars. */
4964 bar->next = FRAME_SCROLL_BARS (f);
4965 bar->prev = Qnil;
4966 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4967 if (!NILP (bar->next))
4968 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4970 /* Map the window/widget. */
4971 #ifdef USE_TOOLKIT_SCROLL_BARS
4973 #ifdef USE_GTK
4974 xg_update_scrollbar_pos (f,
4975 bar->x_window,
4976 top,
4977 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4978 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4979 max (height, 1));
4980 xg_show_scroll_bar (bar->x_window);
4981 #else /* not USE_GTK */
4982 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4983 XtConfigureWidget (scroll_bar,
4984 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4985 top,
4986 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4987 max (height, 1), 0);
4988 XtMapWidget (scroll_bar);
4989 #endif /* not USE_GTK */
4991 #else /* not USE_TOOLKIT_SCROLL_BARS */
4992 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
4993 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4995 UNBLOCK_INPUT;
4996 return bar;
5000 #ifndef USE_TOOLKIT_SCROLL_BARS
5002 /* Draw BAR's handle in the proper position.
5004 If the handle is already drawn from START to END, don't bother
5005 redrawing it, unless REBUILD is non-zero; in that case, always
5006 redraw it. (REBUILD is handy for drawing the handle after expose
5007 events.)
5009 Normally, we want to constrain the start and end of the handle to
5010 fit inside its rectangle, but if the user is dragging the scroll
5011 bar handle, we want to let them drag it down all the way, so that
5012 the bar's top is as far down as it goes; otherwise, there's no way
5013 to move to the very end of the buffer. */
5015 static void
5016 x_scroll_bar_set_handle (bar, start, end, rebuild)
5017 struct scroll_bar *bar;
5018 int start, end;
5019 int rebuild;
5021 int dragging = ! NILP (bar->dragging);
5022 Window w = bar->x_window;
5023 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5024 GC gc = f->output_data.x->normal_gc;
5026 /* If the display is already accurate, do nothing. */
5027 if (! rebuild
5028 && start == bar->start
5029 && end == bar->end)
5030 return;
5032 BLOCK_INPUT;
5035 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5036 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5037 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5039 /* Make sure the values are reasonable, and try to preserve
5040 the distance between start and end. */
5042 int length = end - start;
5044 if (start < 0)
5045 start = 0;
5046 else if (start > top_range)
5047 start = top_range;
5048 end = start + length;
5050 if (end < start)
5051 end = start;
5052 else if (end > top_range && ! dragging)
5053 end = top_range;
5056 /* Store the adjusted setting in the scroll bar. */
5057 bar->start = start;
5058 bar->end = end;
5060 /* Clip the end position, just for display. */
5061 if (end > top_range)
5062 end = top_range;
5064 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5065 below top positions, to make sure the handle is always at least
5066 that many pixels tall. */
5067 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5069 /* Draw the empty space above the handle. Note that we can't clear
5070 zero-height areas; that means "clear to end of window." */
5071 if (0 < start)
5072 x_clear_area (FRAME_X_DISPLAY (f), w,
5073 /* x, y, width, height, and exposures. */
5074 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5075 VERTICAL_SCROLL_BAR_TOP_BORDER,
5076 inside_width, start,
5077 False);
5079 /* Change to proper foreground color if one is specified. */
5080 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5081 XSetForeground (FRAME_X_DISPLAY (f), gc,
5082 f->output_data.x->scroll_bar_foreground_pixel);
5084 /* Draw the handle itself. */
5085 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5086 /* x, y, width, height */
5087 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5088 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5089 inside_width, end - start);
5091 /* Restore the foreground color of the GC if we changed it above. */
5092 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5093 XSetForeground (FRAME_X_DISPLAY (f), gc,
5094 FRAME_FOREGROUND_PIXEL (f));
5096 /* Draw the empty space below the handle. Note that we can't
5097 clear zero-height areas; that means "clear to end of window." */
5098 if (end < inside_height)
5099 x_clear_area (FRAME_X_DISPLAY (f), w,
5100 /* x, y, width, height, and exposures. */
5101 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5102 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5103 inside_width, inside_height - end,
5104 False);
5108 UNBLOCK_INPUT;
5111 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5113 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5114 nil. */
5116 static void
5117 x_scroll_bar_remove (bar)
5118 struct scroll_bar *bar;
5120 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5121 BLOCK_INPUT;
5123 #ifdef USE_TOOLKIT_SCROLL_BARS
5124 #ifdef USE_GTK
5125 xg_remove_scroll_bar (f, bar->x_window);
5126 #else /* not USE_GTK */
5127 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5128 #endif /* not USE_GTK */
5129 #else
5130 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5131 #endif
5133 /* Disassociate this scroll bar from its window. */
5134 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5136 UNBLOCK_INPUT;
5140 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5141 that we are displaying PORTION characters out of a total of WHOLE
5142 characters, starting at POSITION. If WINDOW has no scroll bar,
5143 create one. */
5145 static void
5146 XTset_vertical_scroll_bar (w, portion, whole, position)
5147 struct window *w;
5148 int portion, whole, position;
5150 struct frame *f = XFRAME (w->frame);
5151 struct scroll_bar *bar;
5152 int top, height, left, sb_left, width, sb_width;
5153 int window_y, window_height;
5154 #ifdef USE_TOOLKIT_SCROLL_BARS
5155 int fringe_extended_p;
5156 #endif
5158 /* Get window dimensions. */
5159 window_box (w, -1, 0, &window_y, 0, &window_height);
5160 top = window_y;
5161 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5162 height = window_height;
5164 /* Compute the left edge of the scroll bar area. */
5165 left = WINDOW_SCROLL_BAR_AREA_X (w);
5167 /* Compute the width of the scroll bar which might be less than
5168 the width of the area reserved for the scroll bar. */
5169 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5170 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5171 else
5172 sb_width = width;
5174 /* Compute the left edge of the scroll bar. */
5175 #ifdef USE_TOOLKIT_SCROLL_BARS
5176 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5177 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5178 else
5179 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5180 #else
5181 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5182 sb_left = left + width - sb_width;
5183 else
5184 sb_left = left;
5185 #endif
5187 #ifdef USE_TOOLKIT_SCROLL_BARS
5188 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5189 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5190 && WINDOW_LEFT_FRINGE_WIDTH (w)
5191 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5192 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5193 else
5194 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5195 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5196 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5197 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5198 #endif
5200 /* Does the scroll bar exist yet? */
5201 if (NILP (w->vertical_scroll_bar))
5203 if (width > 0 && height > 0)
5205 BLOCK_INPUT;
5206 #ifdef USE_TOOLKIT_SCROLL_BARS
5207 if (fringe_extended_p)
5208 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5209 sb_left, top, sb_width, height, False);
5210 else
5211 #endif
5212 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5213 left, top, width, height, False);
5214 UNBLOCK_INPUT;
5217 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5219 else
5221 /* It may just need to be moved and resized. */
5222 unsigned int mask = 0;
5224 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5226 BLOCK_INPUT;
5228 if (sb_left != bar->left)
5229 mask |= CWX;
5230 if (top != bar->top)
5231 mask |= CWY;
5232 if (sb_width != bar->width)
5233 mask |= CWWidth;
5234 if (height != bar->height)
5235 mask |= CWHeight;
5237 #ifdef USE_TOOLKIT_SCROLL_BARS
5239 /* Move/size the scroll bar widget. */
5240 if (mask || bar->fringe_extended_p != fringe_extended_p)
5242 /* Since toolkit scroll bars are smaller than the space reserved
5243 for them on the frame, we have to clear "under" them. */
5244 if (width > 0 && height > 0)
5246 if (fringe_extended_p)
5247 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5248 sb_left, top, sb_width, height, False);
5249 else
5250 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5251 left, top, width, height, False);
5253 #ifdef USE_GTK
5254 xg_update_scrollbar_pos (f,
5255 bar->x_window,
5256 top,
5257 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5258 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5259 max (height, 1));
5260 #else /* not USE_GTK */
5261 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5262 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5263 top,
5264 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5265 max (height, 1), 0);
5266 #endif /* not USE_GTK */
5268 #else /* not USE_TOOLKIT_SCROLL_BARS */
5270 /* Clear areas not covered by the scroll bar because of
5271 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5272 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5274 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5275 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5276 height, False);
5277 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5278 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5279 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5280 height, False);
5283 /* Clear areas not covered by the scroll bar because it's not as
5284 wide as the area reserved for it. This makes sure a
5285 previous mode line display is cleared after C-x 2 C-x 1, for
5286 example. */
5288 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5289 int rest = area_width - sb_width;
5290 if (rest > 0 && height > 0)
5292 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5293 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5294 left + area_width - rest, top,
5295 rest, height, False);
5296 else
5297 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5298 left, top, rest, height, False);
5302 /* Move/size the scroll bar window. */
5303 if (mask)
5305 XWindowChanges wc;
5307 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5308 wc.y = top;
5309 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5310 wc.height = height;
5311 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5312 mask, &wc);
5315 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5317 /* Remember new settings. */
5318 bar->left = sb_left;
5319 bar->top = top;
5320 bar->width = sb_width;
5321 bar->height = height;
5323 UNBLOCK_INPUT;
5326 #ifdef USE_TOOLKIT_SCROLL_BARS
5327 bar->fringe_extended_p = fringe_extended_p;
5329 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5330 #else /* not USE_TOOLKIT_SCROLL_BARS */
5331 /* Set the scroll bar's current state, unless we're currently being
5332 dragged. */
5333 if (NILP (bar->dragging))
5335 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5337 if (whole == 0)
5338 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5339 else
5341 int start = ((double) position * top_range) / whole;
5342 int end = ((double) (position + portion) * top_range) / whole;
5343 x_scroll_bar_set_handle (bar, start, end, 0);
5346 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5348 XSETVECTOR (w->vertical_scroll_bar, bar);
5352 /* The following three hooks are used when we're doing a thorough
5353 redisplay of the frame. We don't explicitly know which scroll bars
5354 are going to be deleted, because keeping track of when windows go
5355 away is a real pain - "Can you say set-window-configuration, boys
5356 and girls?" Instead, we just assert at the beginning of redisplay
5357 that *all* scroll bars are to be removed, and then save a scroll bar
5358 from the fiery pit when we actually redisplay its window. */
5360 /* Arrange for all scroll bars on FRAME to be removed at the next call
5361 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5362 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5364 static void
5365 XTcondemn_scroll_bars (frame)
5366 FRAME_PTR frame;
5368 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5369 while (! NILP (FRAME_SCROLL_BARS (frame)))
5371 Lisp_Object bar;
5372 bar = FRAME_SCROLL_BARS (frame);
5373 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5374 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5375 XSCROLL_BAR (bar)->prev = Qnil;
5376 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5377 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5378 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5383 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5384 Note that WINDOW isn't necessarily condemned at all. */
5386 static void
5387 XTredeem_scroll_bar (window)
5388 struct window *window;
5390 struct scroll_bar *bar;
5391 struct frame *f;
5393 /* We can't redeem this window's scroll bar if it doesn't have one. */
5394 if (NILP (window->vertical_scroll_bar))
5395 abort ();
5397 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5399 /* Unlink it from the condemned list. */
5400 f = XFRAME (WINDOW_FRAME (window));
5401 if (NILP (bar->prev))
5403 /* If the prev pointer is nil, it must be the first in one of
5404 the lists. */
5405 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5406 /* It's not condemned. Everything's fine. */
5407 return;
5408 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5409 window->vertical_scroll_bar))
5410 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5411 else
5412 /* If its prev pointer is nil, it must be at the front of
5413 one or the other! */
5414 abort ();
5416 else
5417 XSCROLL_BAR (bar->prev)->next = bar->next;
5419 if (! NILP (bar->next))
5420 XSCROLL_BAR (bar->next)->prev = bar->prev;
5422 bar->next = FRAME_SCROLL_BARS (f);
5423 bar->prev = Qnil;
5424 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5425 if (! NILP (bar->next))
5426 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5429 /* Remove all scroll bars on FRAME that haven't been saved since the
5430 last call to `*condemn_scroll_bars_hook'. */
5432 static void
5433 XTjudge_scroll_bars (f)
5434 FRAME_PTR f;
5436 Lisp_Object bar, next;
5438 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5440 /* Clear out the condemned list now so we won't try to process any
5441 more events on the hapless scroll bars. */
5442 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5444 for (; ! NILP (bar); bar = next)
5446 struct scroll_bar *b = XSCROLL_BAR (bar);
5448 x_scroll_bar_remove (b);
5450 next = b->next;
5451 b->next = b->prev = Qnil;
5454 /* Now there should be no references to the condemned scroll bars,
5455 and they should get garbage-collected. */
5459 #ifndef USE_TOOLKIT_SCROLL_BARS
5460 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5461 is a no-op when using toolkit scroll bars.
5463 This may be called from a signal handler, so we have to ignore GC
5464 mark bits. */
5466 static void
5467 x_scroll_bar_expose (bar, event)
5468 struct scroll_bar *bar;
5469 XEvent *event;
5471 Window w = bar->x_window;
5472 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5473 GC gc = f->output_data.x->normal_gc;
5474 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5476 BLOCK_INPUT;
5478 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5480 /* Switch to scroll bar foreground color. */
5481 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5482 XSetForeground (FRAME_X_DISPLAY (f), gc,
5483 f->output_data.x->scroll_bar_foreground_pixel);
5485 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5486 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5488 /* x, y, width, height */
5489 0, 0,
5490 bar->width - 1 - width_trim - width_trim,
5491 bar->height - 1);
5493 /* Restore the foreground color of the GC if we changed it above. */
5494 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5495 XSetForeground (FRAME_X_DISPLAY (f), gc,
5496 FRAME_FOREGROUND_PIXEL (f));
5498 UNBLOCK_INPUT;
5501 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5503 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5504 is set to something other than NO_EVENT, it is enqueued.
5506 This may be called from a signal handler, so we have to ignore GC
5507 mark bits. */
5510 static void
5511 x_scroll_bar_handle_click (bar, event, emacs_event)
5512 struct scroll_bar *bar;
5513 XEvent *event;
5514 struct input_event *emacs_event;
5516 if (! WINDOWP (bar->window))
5517 abort ();
5519 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5520 emacs_event->code = event->xbutton.button - Button1;
5521 emacs_event->modifiers
5522 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5523 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5524 event->xbutton.state)
5525 | (event->type == ButtonRelease
5526 ? up_modifier
5527 : down_modifier));
5528 emacs_event->frame_or_window = bar->window;
5529 emacs_event->arg = Qnil;
5530 emacs_event->timestamp = event->xbutton.time;
5532 int top_range
5533 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5534 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5536 if (y < 0) y = 0;
5537 if (y > top_range) y = top_range;
5539 if (y < bar->start)
5540 emacs_event->part = scroll_bar_above_handle;
5541 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5542 emacs_event->part = scroll_bar_handle;
5543 else
5544 emacs_event->part = scroll_bar_below_handle;
5546 #ifndef USE_TOOLKIT_SCROLL_BARS
5547 /* If the user has released the handle, set it to its final position. */
5548 if (event->type == ButtonRelease
5549 && ! NILP (bar->dragging))
5551 int new_start = y - XINT (bar->dragging);
5552 int new_end = new_start + bar->end - bar->start;
5554 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5555 bar->dragging = Qnil;
5557 #endif
5559 XSETINT (emacs_event->x, y);
5560 XSETINT (emacs_event->y, top_range);
5564 #ifndef USE_TOOLKIT_SCROLL_BARS
5566 /* Handle some mouse motion while someone is dragging the scroll bar.
5568 This may be called from a signal handler, so we have to ignore GC
5569 mark bits. */
5571 static void
5572 x_scroll_bar_note_movement (bar, event)
5573 struct scroll_bar *bar;
5574 XEvent *event;
5576 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5578 last_mouse_movement_time = event->xmotion.time;
5580 f->mouse_moved = 1;
5581 XSETVECTOR (last_mouse_scroll_bar, bar);
5583 /* If we're dragging the bar, display it. */
5584 if (! NILP (bar->dragging))
5586 /* Where should the handle be now? */
5587 int new_start = event->xmotion.y - XINT (bar->dragging);
5589 if (new_start != bar->start)
5591 int new_end = new_start + bar->end - bar->start;
5593 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5598 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5600 /* Return information to the user about the current position of the mouse
5601 on the scroll bar. */
5603 static void
5604 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5605 FRAME_PTR *fp;
5606 Lisp_Object *bar_window;
5607 enum scroll_bar_part *part;
5608 Lisp_Object *x, *y;
5609 unsigned long *time;
5611 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5612 Window w = bar->x_window;
5613 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5614 int win_x, win_y;
5615 Window dummy_window;
5616 int dummy_coord;
5617 unsigned int dummy_mask;
5619 BLOCK_INPUT;
5621 /* Get the mouse's position relative to the scroll bar window, and
5622 report that. */
5623 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5625 /* Root, child, root x and root y. */
5626 &dummy_window, &dummy_window,
5627 &dummy_coord, &dummy_coord,
5629 /* Position relative to scroll bar. */
5630 &win_x, &win_y,
5632 /* Mouse buttons and modifier keys. */
5633 &dummy_mask))
5635 else
5637 int top_range
5638 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5640 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5642 if (! NILP (bar->dragging))
5643 win_y -= XINT (bar->dragging);
5645 if (win_y < 0)
5646 win_y = 0;
5647 if (win_y > top_range)
5648 win_y = top_range;
5650 *fp = f;
5651 *bar_window = bar->window;
5653 if (! NILP (bar->dragging))
5654 *part = scroll_bar_handle;
5655 else if (win_y < bar->start)
5656 *part = scroll_bar_above_handle;
5657 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5658 *part = scroll_bar_handle;
5659 else
5660 *part = scroll_bar_below_handle;
5662 XSETINT (*x, win_y);
5663 XSETINT (*y, top_range);
5665 f->mouse_moved = 0;
5666 last_mouse_scroll_bar = Qnil;
5669 *time = last_mouse_movement_time;
5671 UNBLOCK_INPUT;
5675 /* The screen has been cleared so we may have changed foreground or
5676 background colors, and the scroll bars may need to be redrawn.
5677 Clear out the scroll bars, and ask for expose events, so we can
5678 redraw them. */
5680 void
5681 x_scroll_bar_clear (f)
5682 FRAME_PTR f;
5684 #ifndef USE_TOOLKIT_SCROLL_BARS
5685 Lisp_Object bar;
5687 /* We can have scroll bars even if this is 0,
5688 if we just turned off scroll bar mode.
5689 But in that case we should not clear them. */
5690 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5691 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5692 bar = XSCROLL_BAR (bar)->next)
5693 XClearArea (FRAME_X_DISPLAY (f),
5694 XSCROLL_BAR (bar)->x_window,
5695 0, 0, 0, 0, True);
5696 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5700 /* The main X event-reading loop - XTread_socket. */
5702 /* This holds the state XLookupString needs to implement dead keys
5703 and other tricks known as "compose processing". _X Window System_
5704 says that a portable program can't use this, but Stephen Gildea assures
5705 me that letting the compiler initialize it to zeros will work okay.
5707 This must be defined outside of XTread_socket, for the same reasons
5708 given for enter_timestamp, above. */
5710 static XComposeStatus compose_status;
5712 /* Record the last 100 characters stored
5713 to help debug the loss-of-chars-during-GC problem. */
5715 static int temp_index;
5716 static short temp_buffer[100];
5718 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5719 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5720 temp_index = 0; \
5721 temp_buffer[temp_index++] = (keysym)
5723 /* Set this to nonzero to fake an "X I/O error"
5724 on a particular display. */
5726 struct x_display_info *XTread_socket_fake_io_error;
5728 /* When we find no input here, we occasionally do a no-op command
5729 to verify that the X server is still running and we can still talk with it.
5730 We try all the open displays, one by one.
5731 This variable is used for cycling thru the displays. */
5733 static struct x_display_info *next_noop_dpyinfo;
5735 #define SET_SAVED_MENU_EVENT(size) \
5736 do \
5738 if (f->output_data.x->saved_menu_event == 0) \
5739 f->output_data.x->saved_menu_event \
5740 = (XEvent *) xmalloc (sizeof (XEvent)); \
5741 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5742 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5743 XSETFRAME (inev.ie.frame_or_window, f); \
5745 while (0)
5747 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5748 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5751 enum
5753 X_EVENT_NORMAL,
5754 X_EVENT_GOTO_OUT,
5755 X_EVENT_DROP
5758 /* Filter events for the current X input method.
5759 DPYINFO is the display this event is for.
5760 EVENT is the X event to filter.
5762 Returns non-zero if the event was filtered, caller shall not process
5763 this event further.
5764 Returns zero if event is wasn't filtered. */
5766 #ifdef HAVE_X_I18N
5767 static int
5768 x_filter_event (dpyinfo, event)
5769 struct x_display_info *dpyinfo;
5770 XEvent *event;
5772 /* XFilterEvent returns non-zero if the input method has
5773 consumed the event. We pass the frame's X window to
5774 XFilterEvent because that's the one for which the IC
5775 was created. */
5777 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5778 event->xclient.window);
5780 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5782 #endif
5784 #ifdef USE_GTK
5785 static int current_count;
5786 static int current_finish;
5787 static struct input_event *current_hold_quit;
5789 /* This is the filter function invoked by the GTK event loop.
5790 It is invoked before the XEvent is translated to a GdkEvent,
5791 so we have a chance to act on the event before GTK. */
5792 static GdkFilterReturn
5793 event_handler_gdk (gxev, ev, data)
5794 GdkXEvent *gxev;
5795 GdkEvent *ev;
5796 gpointer data;
5798 XEvent *xev = (XEvent *) gxev;
5800 if (current_count >= 0)
5802 struct x_display_info *dpyinfo;
5804 dpyinfo = x_display_info_for_display (xev->xany.display);
5806 #ifdef HAVE_X_I18N
5807 /* Filter events for the current X input method.
5808 GTK calls XFilterEvent but not for key press and release,
5809 so we do it here. */
5810 if (xev->type == KeyPress || xev->type == KeyRelease)
5811 if (dpyinfo && x_filter_event (dpyinfo, xev))
5812 return GDK_FILTER_REMOVE;
5813 #endif
5815 if (! dpyinfo)
5816 current_finish = X_EVENT_NORMAL;
5817 else
5819 current_count +=
5820 handle_one_xevent (dpyinfo, xev, &current_finish,
5821 current_hold_quit);
5824 else
5825 current_finish = x_dispatch_event (xev, xev->xany.display);
5827 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5828 return GDK_FILTER_REMOVE;
5830 return GDK_FILTER_CONTINUE;
5832 #endif /* USE_GTK */
5835 /* Handles the XEvent EVENT on display DPYINFO.
5837 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5838 *FINISH is zero if caller should continue reading events.
5839 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5841 We return the number of characters stored into the buffer. */
5843 static int
5844 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5845 struct x_display_info *dpyinfo;
5846 XEvent *eventp;
5847 int *finish;
5848 struct input_event *hold_quit;
5850 union {
5851 struct input_event ie;
5852 struct selection_input_event sie;
5853 } inev;
5854 int count = 0;
5855 int do_help = 0;
5856 int nbytes = 0;
5857 struct frame *f = NULL;
5858 struct coding_system coding;
5859 XEvent event = *eventp;
5861 *finish = X_EVENT_NORMAL;
5863 EVENT_INIT (inev.ie);
5864 inev.ie.kind = NO_EVENT;
5865 inev.ie.arg = Qnil;
5867 if (pending_event_wait.eventtype == event.type)
5868 pending_event_wait.eventtype = 0; /* Indicates we got it. */
5870 switch (event.type)
5872 case ClientMessage:
5874 if (event.xclient.message_type
5875 == dpyinfo->Xatom_wm_protocols
5876 && event.xclient.format == 32)
5878 if (event.xclient.data.l[0]
5879 == dpyinfo->Xatom_wm_take_focus)
5881 /* Use x_any_window_to_frame because this
5882 could be the shell widget window
5883 if the frame has no title bar. */
5884 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5885 #ifdef HAVE_X_I18N
5886 /* Not quite sure this is needed -pd */
5887 if (f && FRAME_XIC (f))
5888 XSetICFocus (FRAME_XIC (f));
5889 #endif
5890 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5891 instructs the WM to set the input focus automatically for
5892 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5893 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5894 it has set the focus. So, XSetInputFocus below is not
5895 needed.
5897 The call to XSetInputFocus below has also caused trouble. In
5898 cases where the XSetInputFocus done by the WM and the one
5899 below are temporally close (on a fast machine), the call
5900 below can generate additional FocusIn events which confuse
5901 Emacs. */
5903 /* Since we set WM_TAKE_FOCUS, we must call
5904 XSetInputFocus explicitly. But not if f is null,
5905 since that might be an event for a deleted frame. */
5906 if (f)
5908 Display *d = event.xclient.display;
5909 /* Catch and ignore errors, in case window has been
5910 iconified by a window manager such as GWM. */
5911 x_catch_errors (d);
5912 XSetInputFocus (d, event.xclient.window,
5913 /* The ICCCM says this is
5914 the only valid choice. */
5915 RevertToParent,
5916 event.xclient.data.l[1]);
5917 /* This is needed to detect the error
5918 if there is an error. */
5919 XSync (d, False);
5920 x_uncatch_errors ();
5922 /* Not certain about handling scroll bars here */
5923 #endif /* 0 */
5924 goto done;
5927 if (event.xclient.data.l[0]
5928 == dpyinfo->Xatom_wm_save_yourself)
5930 /* Save state modify the WM_COMMAND property to
5931 something which can reinstate us. This notifies
5932 the session manager, who's looking for such a
5933 PropertyNotify. Can restart processing when
5934 a keyboard or mouse event arrives. */
5935 /* If we have a session manager, don't set this.
5936 KDE will then start two Emacsen, one for the
5937 session manager and one for this. */
5938 #ifdef HAVE_X_SM
5939 if (! x_session_have_connection ())
5940 #endif
5942 f = x_top_window_to_frame (dpyinfo,
5943 event.xclient.window);
5944 /* This is just so we only give real data once
5945 for a single Emacs process. */
5946 if (f == SELECTED_FRAME ())
5947 XSetCommand (FRAME_X_DISPLAY (f),
5948 event.xclient.window,
5949 initial_argv, initial_argc);
5950 else if (f)
5951 XSetCommand (FRAME_X_DISPLAY (f),
5952 event.xclient.window,
5953 0, 0);
5955 goto done;
5958 if (event.xclient.data.l[0]
5959 == dpyinfo->Xatom_wm_delete_window)
5961 f = x_any_window_to_frame (dpyinfo,
5962 event.xclient.window);
5963 if (!f)
5964 goto OTHER; /* May be a dialog that is to be removed */
5966 inev.ie.kind = DELETE_WINDOW_EVENT;
5967 XSETFRAME (inev.ie.frame_or_window, f);
5968 goto done;
5971 goto done;
5974 if (event.xclient.message_type
5975 == dpyinfo->Xatom_wm_configure_denied)
5977 goto done;
5980 if (event.xclient.message_type
5981 == dpyinfo->Xatom_wm_window_moved)
5983 int new_x, new_y;
5984 f = x_window_to_frame (dpyinfo, event.xclient.window);
5986 new_x = event.xclient.data.s[0];
5987 new_y = event.xclient.data.s[1];
5989 if (f)
5991 f->left_pos = new_x;
5992 f->top_pos = new_y;
5994 goto done;
5997 #ifdef HACK_EDITRES
5998 if (event.xclient.message_type
5999 == dpyinfo->Xatom_editres)
6001 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6002 if (f)
6003 _XEditResCheckMessages (f->output_data.x->widget, NULL,
6004 &event, NULL);
6005 goto done;
6007 #endif /* HACK_EDITRES */
6009 if ((event.xclient.message_type
6010 == dpyinfo->Xatom_DONE)
6011 || (event.xclient.message_type
6012 == dpyinfo->Xatom_PAGE))
6014 /* Ghostview job completed. Kill it. We could
6015 reply with "Next" if we received "Page", but we
6016 currently never do because we are interested in
6017 images, only, which should have 1 page. */
6018 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
6019 f = x_window_to_frame (dpyinfo, event.xclient.window);
6020 if (!f)
6021 goto OTHER;
6022 x_kill_gs_process (pixmap, f);
6023 expose_frame (f, 0, 0, 0, 0);
6024 goto done;
6027 #ifdef USE_TOOLKIT_SCROLL_BARS
6028 /* Scroll bar callbacks send a ClientMessage from which
6029 we construct an input_event. */
6030 if (event.xclient.message_type
6031 == dpyinfo->Xatom_Scrollbar)
6033 x_scroll_bar_to_input_event (&event, &inev.ie);
6034 *finish = X_EVENT_GOTO_OUT;
6035 goto done;
6037 #endif /* USE_TOOLKIT_SCROLL_BARS */
6039 /* XEmbed messages from the embedder (if any). */
6040 if (event.xclient.message_type
6041 == dpyinfo->Xatom_XEMBED)
6043 enum xembed_message msg = event.xclient.data.l[1];
6044 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6045 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6047 *finish = X_EVENT_GOTO_OUT;
6048 goto done;
6051 xft_settings_event (dpyinfo, &event);
6053 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6054 if (!f)
6055 goto OTHER;
6056 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
6057 *finish = X_EVENT_DROP;
6059 break;
6061 case SelectionNotify:
6062 last_user_time = event.xselection.time;
6063 #ifdef USE_X_TOOLKIT
6064 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6065 goto OTHER;
6066 #endif /* not USE_X_TOOLKIT */
6067 x_handle_selection_notify (&event.xselection);
6068 break;
6070 case SelectionClear: /* Someone has grabbed ownership. */
6071 last_user_time = event.xselectionclear.time;
6072 #ifdef USE_X_TOOLKIT
6073 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6074 goto OTHER;
6075 #endif /* USE_X_TOOLKIT */
6077 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
6079 inev.ie.kind = SELECTION_CLEAR_EVENT;
6080 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6081 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6082 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6083 inev.ie.frame_or_window = Qnil;
6085 break;
6087 case SelectionRequest: /* Someone wants our selection. */
6088 last_user_time = event.xselectionrequest.time;
6089 #ifdef USE_X_TOOLKIT
6090 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6091 goto OTHER;
6092 #endif /* USE_X_TOOLKIT */
6094 XSelectionRequestEvent *eventp
6095 = (XSelectionRequestEvent *) &event;
6097 inev.ie.kind = SELECTION_REQUEST_EVENT;
6098 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6099 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6100 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6101 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6102 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6103 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6104 inev.ie.frame_or_window = Qnil;
6106 break;
6108 case PropertyNotify:
6109 last_user_time = event.xproperty.time;
6110 f = x_top_window_to_frame (dpyinfo, event.xproperty.window);
6111 if (f && event.xproperty.atom == dpyinfo->Xatom_net_wm_state)
6112 x_handle_net_wm_state (f, &event.xproperty);
6114 x_handle_property_notify (&event.xproperty);
6115 xft_settings_event (dpyinfo, &event);
6116 goto OTHER;
6118 case ReparentNotify:
6119 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6120 if (f)
6122 int x, y;
6123 f->output_data.x->parent_desc = event.xreparent.parent;
6124 x_real_positions (f, &x, &y);
6125 f->left_pos = x;
6126 f->top_pos = y;
6128 /* Perhaps reparented due to a WM restart. Reset this. */
6129 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6130 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6132 goto OTHER;
6134 case Expose:
6135 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6136 if (f)
6138 #ifdef USE_GTK
6139 /* This seems to be needed for GTK 2.6. */
6140 x_clear_area (event.xexpose.display,
6141 event.xexpose.window,
6142 event.xexpose.x, event.xexpose.y,
6143 event.xexpose.width, event.xexpose.height,
6144 FALSE);
6145 #endif
6146 if (f->async_visible == 0)
6148 f->async_visible = 1;
6149 f->async_iconified = 0;
6150 f->output_data.x->has_been_visible = 1;
6151 SET_FRAME_GARBAGED (f);
6153 else
6154 expose_frame (f,
6155 event.xexpose.x, event.xexpose.y,
6156 event.xexpose.width, event.xexpose.height);
6158 else
6160 #ifndef USE_TOOLKIT_SCROLL_BARS
6161 struct scroll_bar *bar;
6162 #endif
6163 #if defined USE_LUCID
6164 /* Submenus of the Lucid menu bar aren't widgets
6165 themselves, so there's no way to dispatch events
6166 to them. Recognize this case separately. */
6168 Widget widget
6169 = x_window_to_menu_bar (event.xexpose.window);
6170 if (widget)
6171 xlwmenu_redisplay (widget);
6173 #endif /* USE_LUCID */
6175 #ifdef USE_TOOLKIT_SCROLL_BARS
6176 /* Dispatch event to the widget. */
6177 goto OTHER;
6178 #else /* not USE_TOOLKIT_SCROLL_BARS */
6179 bar = x_window_to_scroll_bar (event.xexpose.display,
6180 event.xexpose.window);
6182 if (bar)
6183 x_scroll_bar_expose (bar, &event);
6184 #ifdef USE_X_TOOLKIT
6185 else
6186 goto OTHER;
6187 #endif /* USE_X_TOOLKIT */
6188 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6190 break;
6192 case GraphicsExpose: /* This occurs when an XCopyArea's
6193 source area was obscured or not
6194 available. */
6195 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6196 if (f)
6198 expose_frame (f,
6199 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6200 event.xgraphicsexpose.width,
6201 event.xgraphicsexpose.height);
6203 #ifdef USE_X_TOOLKIT
6204 else
6205 goto OTHER;
6206 #endif /* USE_X_TOOLKIT */
6207 break;
6209 case NoExpose: /* This occurs when an XCopyArea's
6210 source area was completely
6211 available. */
6212 break;
6214 case UnmapNotify:
6215 /* Redo the mouse-highlight after the tooltip has gone. */
6216 if (event.xmap.window == tip_window)
6218 tip_window = 0;
6219 redo_mouse_highlight ();
6222 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6223 if (f) /* F may no longer exist if
6224 the frame was deleted. */
6226 /* While a frame is unmapped, display generation is
6227 disabled; you don't want to spend time updating a
6228 display that won't ever be seen. */
6229 f->async_visible = 0;
6230 /* We can't distinguish, from the event, whether the window
6231 has become iconified or invisible. So assume, if it
6232 was previously visible, than now it is iconified.
6233 But x_make_frame_invisible clears both
6234 the visible flag and the iconified flag;
6235 and that way, we know the window is not iconified now. */
6236 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6238 f->async_iconified = 1;
6240 inev.ie.kind = ICONIFY_EVENT;
6241 XSETFRAME (inev.ie.frame_or_window, f);
6244 goto OTHER;
6246 case MapNotify:
6247 if (event.xmap.window == tip_window)
6248 /* The tooltip has been drawn already. Avoid
6249 the SET_FRAME_GARBAGED below. */
6250 goto OTHER;
6252 /* We use x_top_window_to_frame because map events can
6253 come for sub-windows and they don't mean that the
6254 frame is visible. */
6255 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6256 if (f)
6258 /* wait_reading_process_output will notice this and update
6259 the frame's display structures.
6260 If we where iconified, we should not set garbaged,
6261 because that stops redrawing on Expose events. This looks
6262 bad if we are called from a recursive event loop
6263 (x_dispatch_event), for example when a dialog is up. */
6264 if (! f->async_iconified)
6265 SET_FRAME_GARBAGED (f);
6267 /* Check if fullscreen was specified before we where mapped the
6268 first time, i.e. from the command line. */
6269 if (!f->output_data.x->has_been_visible)
6270 x_check_fullscreen (f);
6272 f->async_visible = 1;
6273 f->async_iconified = 0;
6274 f->output_data.x->has_been_visible = 1;
6276 if (f->iconified)
6278 inev.ie.kind = DEICONIFY_EVENT;
6279 XSETFRAME (inev.ie.frame_or_window, f);
6281 else if (! NILP (Vframe_list)
6282 && ! NILP (XCDR (Vframe_list)))
6283 /* Force a redisplay sooner or later
6284 to update the frame titles
6285 in case this is the second frame. */
6286 record_asynch_buffer_change ();
6288 #ifdef USE_GTK
6289 xg_frame_resized (f, -1, -1);
6290 #endif
6292 goto OTHER;
6294 case KeyPress:
6296 last_user_time = event.xkey.time;
6297 ignore_next_mouse_click_timeout = 0;
6299 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6300 /* Dispatch KeyPress events when in menu. */
6301 if (popup_activated ())
6302 goto OTHER;
6303 #endif
6305 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6307 /* If mouse-highlight is an integer, input clears out
6308 mouse highlighting. */
6309 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6310 && (f == 0
6311 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6313 clear_mouse_face (dpyinfo);
6314 dpyinfo->mouse_face_hidden = 1;
6317 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6318 if (f == 0)
6320 /* Scroll bars consume key events, but we want
6321 the keys to go to the scroll bar's frame. */
6322 Widget widget = XtWindowToWidget (dpyinfo->display,
6323 event.xkey.window);
6324 if (widget && XmIsScrollBar (widget))
6326 widget = XtParent (widget);
6327 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6330 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6332 if (f != 0)
6334 KeySym keysym, orig_keysym;
6335 /* al%imercury@uunet.uu.net says that making this 81
6336 instead of 80 fixed a bug whereby meta chars made
6337 his Emacs hang.
6339 It seems that some version of XmbLookupString has
6340 a bug of not returning XBufferOverflow in
6341 status_return even if the input is too long to
6342 fit in 81 bytes. So, we must prepare sufficient
6343 bytes for copy_buffer. 513 bytes (256 chars for
6344 two-byte character set) seems to be a fairly good
6345 approximation. -- 2000.8.10 handa@etl.go.jp */
6346 unsigned char copy_buffer[513];
6347 unsigned char *copy_bufptr = copy_buffer;
6348 int copy_bufsiz = sizeof (copy_buffer);
6349 int modifiers;
6350 Lisp_Object coding_system = Qlatin_1;
6351 Lisp_Object c;
6353 #ifdef USE_GTK
6354 /* Don't pass keys to GTK. A Tab will shift focus to the
6355 tool bar in GTK 2.4. Keys will still go to menus and
6356 dialogs because in that case popup_activated is TRUE
6357 (see above). */
6358 *finish = X_EVENT_DROP;
6359 #endif
6361 event.xkey.state
6362 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6363 extra_keyboard_modifiers);
6364 modifiers = event.xkey.state;
6366 /* This will have to go some day... */
6368 /* make_lispy_event turns chars into control chars.
6369 Don't do it here because XLookupString is too eager. */
6370 event.xkey.state &= ~ControlMask;
6371 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6372 | dpyinfo->super_mod_mask
6373 | dpyinfo->hyper_mod_mask
6374 | dpyinfo->alt_mod_mask);
6376 /* In case Meta is ComposeCharacter,
6377 clear its status. According to Markus Ehrnsperger
6378 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6379 this enables ComposeCharacter to work whether or
6380 not it is combined with Meta. */
6381 if (modifiers & dpyinfo->meta_mod_mask)
6382 bzero (&compose_status, sizeof (compose_status));
6384 #ifdef HAVE_X_I18N
6385 if (FRAME_XIC (f))
6387 Status status_return;
6389 coding_system = Vlocale_coding_system;
6390 nbytes = XmbLookupString (FRAME_XIC (f),
6391 &event.xkey, copy_bufptr,
6392 copy_bufsiz, &keysym,
6393 &status_return);
6394 if (status_return == XBufferOverflow)
6396 copy_bufsiz = nbytes + 1;
6397 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6398 nbytes = XmbLookupString (FRAME_XIC (f),
6399 &event.xkey, copy_bufptr,
6400 copy_bufsiz, &keysym,
6401 &status_return);
6403 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6404 if (status_return == XLookupNone)
6405 break;
6406 else if (status_return == XLookupChars)
6408 keysym = NoSymbol;
6409 modifiers = 0;
6411 else if (status_return != XLookupKeySym
6412 && status_return != XLookupBoth)
6413 abort ();
6415 else
6416 nbytes = XLookupString (&event.xkey, copy_bufptr,
6417 copy_bufsiz, &keysym,
6418 &compose_status);
6419 #else
6420 nbytes = XLookupString (&event.xkey, copy_bufptr,
6421 copy_bufsiz, &keysym,
6422 &compose_status);
6423 #endif
6425 /* If not using XIM/XIC, and a compose sequence is in progress,
6426 we break here. Otherwise, chars_matched is always 0. */
6427 if (compose_status.chars_matched > 0 && nbytes == 0)
6428 break;
6430 bzero (&compose_status, sizeof (compose_status));
6431 orig_keysym = keysym;
6433 /* Common for all keysym input events. */
6434 XSETFRAME (inev.ie.frame_or_window, f);
6435 inev.ie.modifiers
6436 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6437 inev.ie.timestamp = event.xkey.time;
6439 /* First deal with keysyms which have defined
6440 translations to characters. */
6441 if (keysym >= 32 && keysym < 128)
6442 /* Avoid explicitly decoding each ASCII character. */
6444 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6445 inev.ie.code = keysym;
6446 goto done_keysym;
6449 /* Keysyms directly mapped to Unicode characters. */
6450 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6452 if (keysym < 0x01000080)
6453 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6454 else
6455 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6456 inev.ie.code = keysym & 0xFFFFFF;
6457 goto done_keysym;
6460 /* Now non-ASCII. */
6461 if (HASH_TABLE_P (Vx_keysym_table)
6462 && (NATNUMP (c = Fgethash (make_number (keysym),
6463 Vx_keysym_table,
6464 Qnil))))
6466 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6467 ? ASCII_KEYSTROKE_EVENT
6468 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6469 inev.ie.code = XFASTINT (c);
6470 goto done_keysym;
6473 /* Random non-modifier sorts of keysyms. */
6474 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6475 || keysym == XK_Delete
6476 #ifdef XK_ISO_Left_Tab
6477 || (keysym >= XK_ISO_Left_Tab
6478 && keysym <= XK_ISO_Enter)
6479 #endif
6480 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6481 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6482 #ifdef HPUX
6483 /* This recognizes the "extended function
6484 keys". It seems there's no cleaner way.
6485 Test IsModifierKey to avoid handling
6486 mode_switch incorrectly. */
6487 || ((unsigned) (keysym) >= XK_Select
6488 && (unsigned)(keysym) < XK_KP_Space)
6489 #endif
6490 #ifdef XK_dead_circumflex
6491 || orig_keysym == XK_dead_circumflex
6492 #endif
6493 #ifdef XK_dead_grave
6494 || orig_keysym == XK_dead_grave
6495 #endif
6496 #ifdef XK_dead_tilde
6497 || orig_keysym == XK_dead_tilde
6498 #endif
6499 #ifdef XK_dead_diaeresis
6500 || orig_keysym == XK_dead_diaeresis
6501 #endif
6502 #ifdef XK_dead_macron
6503 || orig_keysym == XK_dead_macron
6504 #endif
6505 #ifdef XK_dead_degree
6506 || orig_keysym == XK_dead_degree
6507 #endif
6508 #ifdef XK_dead_acute
6509 || orig_keysym == XK_dead_acute
6510 #endif
6511 #ifdef XK_dead_cedilla
6512 || orig_keysym == XK_dead_cedilla
6513 #endif
6514 #ifdef XK_dead_breve
6515 || orig_keysym == XK_dead_breve
6516 #endif
6517 #ifdef XK_dead_ogonek
6518 || orig_keysym == XK_dead_ogonek
6519 #endif
6520 #ifdef XK_dead_caron
6521 || orig_keysym == XK_dead_caron
6522 #endif
6523 #ifdef XK_dead_doubleacute
6524 || orig_keysym == XK_dead_doubleacute
6525 #endif
6526 #ifdef XK_dead_abovedot
6527 || orig_keysym == XK_dead_abovedot
6528 #endif
6529 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6530 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6531 /* Any "vendor-specific" key is ok. */
6532 || (orig_keysym & (1 << 28))
6533 || (keysym != NoSymbol && nbytes == 0))
6534 && ! (IsModifierKey (orig_keysym)
6535 /* The symbols from XK_ISO_Lock
6536 to XK_ISO_Last_Group_Lock
6537 don't have real modifiers but
6538 should be treated similarly to
6539 Mode_switch by Emacs. */
6540 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6541 || ((unsigned)(orig_keysym)
6542 >= XK_ISO_Lock
6543 && (unsigned)(orig_keysym)
6544 <= XK_ISO_Last_Group_Lock)
6545 #endif
6548 STORE_KEYSYM_FOR_DEBUG (keysym);
6549 /* make_lispy_event will convert this to a symbolic
6550 key. */
6551 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6552 inev.ie.code = keysym;
6553 goto done_keysym;
6556 { /* Raw bytes, not keysym. */
6557 register int i;
6558 register int c;
6559 int nchars, len;
6561 for (i = 0, nchars = 0; i < nbytes; i++)
6563 if (ASCII_BYTE_P (copy_bufptr[i]))
6564 nchars++;
6565 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6568 if (nchars < nbytes)
6570 /* Decode the input data. */
6571 int require;
6572 unsigned char *p;
6574 /* The input should be decoded with `coding_system'
6575 which depends on which X*LookupString function
6576 we used just above and the locale. */
6577 setup_coding_system (coding_system, &coding);
6578 coding.src_multibyte = 0;
6579 coding.dst_multibyte = 1;
6580 /* The input is converted to events, thus we can't
6581 handle composition. Anyway, there's no XIM that
6582 gives us composition information. */
6583 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6585 require = MAX_MULTIBYTE_LENGTH * nbytes;
6586 coding.destination = alloca (require);
6587 coding.dst_bytes = require;
6588 coding.mode |= CODING_MODE_LAST_BLOCK;
6589 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6590 nbytes = coding.produced;
6591 nchars = coding.produced_char;
6592 copy_bufptr = coding.destination;
6595 /* Convert the input data to a sequence of
6596 character events. */
6597 for (i = 0; i < nbytes; i += len)
6599 if (nchars == nbytes)
6600 c = copy_bufptr[i], len = 1;
6601 else
6602 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
6603 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6604 ? ASCII_KEYSTROKE_EVENT
6605 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6606 inev.ie.code = c;
6607 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6610 count += nchars;
6612 inev.ie.kind = NO_EVENT; /* Already stored above. */
6614 if (keysym == NoSymbol)
6615 break;
6618 done_keysym:
6619 #ifdef HAVE_X_I18N
6620 /* Don't dispatch this event since XtDispatchEvent calls
6621 XFilterEvent, and two calls in a row may freeze the
6622 client. */
6623 break;
6624 #else
6625 goto OTHER;
6626 #endif
6628 case KeyRelease:
6629 last_user_time = event.xkey.time;
6630 #ifdef HAVE_X_I18N
6631 /* Don't dispatch this event since XtDispatchEvent calls
6632 XFilterEvent, and two calls in a row may freeze the
6633 client. */
6634 break;
6635 #else
6636 goto OTHER;
6637 #endif
6639 case EnterNotify:
6640 last_user_time = event.xcrossing.time;
6641 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6643 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6645 if (f && x_mouse_click_focus_ignore_position)
6646 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6648 /* EnterNotify counts as mouse movement,
6649 so update things that depend on mouse position. */
6650 if (f && !f->output_data.x->hourglass_p)
6651 note_mouse_movement (f, &event.xmotion);
6652 #ifdef USE_GTK
6653 /* We may get an EnterNotify on the buttons in the toolbar. In that
6654 case we moved out of any highlighted area and need to note this. */
6655 if (!f && last_mouse_glyph_frame)
6656 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6657 #endif
6658 goto OTHER;
6660 case FocusIn:
6661 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6662 goto OTHER;
6664 case LeaveNotify:
6665 last_user_time = event.xcrossing.time;
6666 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6668 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6669 if (f)
6671 if (f == dpyinfo->mouse_face_mouse_frame)
6673 /* If we move outside the frame, then we're
6674 certainly no longer on any text in the frame. */
6675 clear_mouse_face (dpyinfo);
6676 dpyinfo->mouse_face_mouse_frame = 0;
6679 /* Generate a nil HELP_EVENT to cancel a help-echo.
6680 Do it only if there's something to cancel.
6681 Otherwise, the startup message is cleared when
6682 the mouse leaves the frame. */
6683 if (any_help_event_p)
6684 do_help = -1;
6686 #ifdef USE_GTK
6687 /* See comment in EnterNotify above */
6688 else if (last_mouse_glyph_frame)
6689 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6690 #endif
6691 goto OTHER;
6693 case FocusOut:
6694 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6695 goto OTHER;
6697 case MotionNotify:
6699 last_user_time = event.xmotion.time;
6700 previous_help_echo_string = help_echo_string;
6701 help_echo_string = Qnil;
6703 if (dpyinfo->grabbed && last_mouse_frame
6704 && FRAME_LIVE_P (last_mouse_frame))
6705 f = last_mouse_frame;
6706 else
6707 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6709 if (dpyinfo->mouse_face_hidden)
6711 dpyinfo->mouse_face_hidden = 0;
6712 clear_mouse_face (dpyinfo);
6715 #ifdef USE_GTK
6716 if (f && xg_event_is_for_scrollbar (f, &event))
6717 f = 0;
6718 #endif
6719 if (f)
6722 /* Generate SELECT_WINDOW_EVENTs when needed.
6723 Don't let popup menus influence things (bug#1261). */
6724 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6726 Lisp_Object window;
6728 window = window_from_coordinates (f,
6729 event.xmotion.x, event.xmotion.y,
6730 0, 0, 0, 0);
6732 /* Window will be selected only when it is not selected now and
6733 last mouse movement event was not in it. Minibuffer window
6734 will be selected only when it is active. */
6735 if (WINDOWP (window)
6736 && !EQ (window, last_window)
6737 && !EQ (window, selected_window)
6738 /* For click-to-focus window managers
6739 create event iff we don't leave the
6740 selected frame. */
6741 && (focus_follows_mouse
6742 || (EQ (XWINDOW (window)->frame,
6743 XWINDOW (selected_window)->frame))))
6745 inev.ie.kind = SELECT_WINDOW_EVENT;
6746 inev.ie.frame_or_window = window;
6749 last_window=window;
6751 if (!note_mouse_movement (f, &event.xmotion))
6752 help_echo_string = previous_help_echo_string;
6754 else
6756 #ifndef USE_TOOLKIT_SCROLL_BARS
6757 struct scroll_bar *bar
6758 = x_window_to_scroll_bar (event.xmotion.display,
6759 event.xmotion.window);
6761 if (bar)
6762 x_scroll_bar_note_movement (bar, &event);
6763 #endif /* USE_TOOLKIT_SCROLL_BARS */
6765 /* If we move outside the frame, then we're
6766 certainly no longer on any text in the frame. */
6767 clear_mouse_face (dpyinfo);
6770 /* If the contents of the global variable help_echo_string
6771 has changed, generate a HELP_EVENT. */
6772 if (!NILP (help_echo_string)
6773 || !NILP (previous_help_echo_string))
6774 do_help = 1;
6775 goto OTHER;
6778 case ConfigureNotify:
6779 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6780 #ifdef USE_GTK
6781 if (!f
6782 && (f = x_any_window_to_frame (dpyinfo, event.xconfigure.window))
6783 && event.xconfigure.window == FRAME_X_WINDOW (f))
6785 xg_frame_resized (f, event.xconfigure.width,
6786 event.xconfigure.height);
6787 f = 0;
6789 #endif
6790 if (f)
6792 #ifndef USE_X_TOOLKIT
6793 #ifndef USE_GTK
6794 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6795 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6797 /* In the toolkit version, change_frame_size
6798 is called by the code that handles resizing
6799 of the EmacsFrame widget. */
6801 /* Even if the number of character rows and columns has
6802 not changed, the font size may have changed, so we need
6803 to check the pixel dimensions as well. */
6804 if (columns != FRAME_COLS (f)
6805 || rows != FRAME_LINES (f)
6806 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6807 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6809 change_frame_size (f, rows, columns, 0, 1, 0);
6810 SET_FRAME_GARBAGED (f);
6811 cancel_mouse_face (f);
6814 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6815 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6816 #endif /* not USE_GTK */
6817 #endif
6819 #ifdef USE_GTK
6820 /* GTK creates windows but doesn't map them.
6821 Only get real positions when mapped. */
6822 if (FRAME_GTK_OUTER_WIDGET (f)
6823 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6824 #endif
6826 x_real_positions (f, &f->left_pos, &f->top_pos);
6829 #ifdef HAVE_X_I18N
6830 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6831 xic_set_statusarea (f);
6832 #endif
6835 goto OTHER;
6837 case ButtonRelease:
6838 case ButtonPress:
6840 /* If we decide we want to generate an event to be seen
6841 by the rest of Emacs, we put it here. */
6842 int tool_bar_p = 0;
6844 bzero (&compose_status, sizeof (compose_status));
6845 last_mouse_glyph_frame = 0;
6846 last_user_time = event.xbutton.time;
6848 if (dpyinfo->grabbed
6849 && last_mouse_frame
6850 && FRAME_LIVE_P (last_mouse_frame))
6851 f = last_mouse_frame;
6852 else
6853 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6855 #ifdef USE_GTK
6856 if (f && xg_event_is_for_scrollbar (f, &event))
6857 f = 0;
6858 #endif
6859 if (f)
6861 /* Is this in the tool-bar? */
6862 if (WINDOWP (f->tool_bar_window)
6863 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6865 Lisp_Object window;
6866 int x = event.xbutton.x;
6867 int y = event.xbutton.y;
6869 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6870 tool_bar_p = EQ (window, f->tool_bar_window);
6872 if (tool_bar_p && event.xbutton.button < 4)
6874 handle_tool_bar_click (f, x, y,
6875 event.xbutton.type == ButtonPress,
6876 x_x_to_emacs_modifiers (dpyinfo,
6877 event.xbutton.state));
6881 if (!tool_bar_p)
6882 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6883 if (! popup_activated ())
6884 #endif
6886 if (ignore_next_mouse_click_timeout)
6888 if (event.type == ButtonPress
6889 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6891 ignore_next_mouse_click_timeout = 0;
6892 construct_mouse_click (&inev.ie, &event.xbutton, f);
6894 if (event.type == ButtonRelease)
6895 ignore_next_mouse_click_timeout = 0;
6897 else
6898 construct_mouse_click (&inev.ie, &event.xbutton, f);
6900 if (FRAME_X_EMBEDDED_P (f))
6901 xembed_send_message (f, event.xbutton.time,
6902 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6904 else
6906 struct scroll_bar *bar
6907 = x_window_to_scroll_bar (event.xbutton.display,
6908 event.xbutton.window);
6910 #ifdef USE_TOOLKIT_SCROLL_BARS
6911 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6912 scroll bars. */
6913 if (bar && event.xbutton.state & ControlMask)
6915 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6916 *finish = X_EVENT_DROP;
6918 #else /* not USE_TOOLKIT_SCROLL_BARS */
6919 if (bar)
6920 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6921 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6924 if (event.type == ButtonPress)
6926 dpyinfo->grabbed |= (1 << event.xbutton.button);
6927 last_mouse_frame = f;
6929 if (!tool_bar_p)
6930 last_tool_bar_item = -1;
6932 else
6933 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6935 /* Ignore any mouse motion that happened before this event;
6936 any subsequent mouse-movement Emacs events should reflect
6937 only motion after the ButtonPress/Release. */
6938 if (f != 0)
6939 f->mouse_moved = 0;
6941 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6942 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6943 /* For a down-event in the menu bar,
6944 don't pass it to Xt right now.
6945 Instead, save it away
6946 and we will pass it to Xt from kbd_buffer_get_event.
6947 That way, we can run some Lisp code first. */
6948 if (
6949 #ifdef USE_GTK
6950 ! popup_activated ()
6951 /* Gtk+ menus only react to the first three buttons. */
6952 && event.xbutton.button < 3
6954 #endif
6955 f && event.type == ButtonPress
6956 /* Verify the event is really within the menu bar
6957 and not just sent to it due to grabbing. */
6958 && event.xbutton.x >= 0
6959 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6960 && event.xbutton.y >= 0
6961 && event.xbutton.y < f->output_data.x->menubar_height
6962 && event.xbutton.same_screen)
6964 SET_SAVED_BUTTON_EVENT;
6965 XSETFRAME (last_mouse_press_frame, f);
6966 #ifdef USE_GTK
6967 *finish = X_EVENT_DROP;
6968 #endif
6970 else if (event.type == ButtonPress)
6972 last_mouse_press_frame = Qnil;
6973 goto OTHER;
6976 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6977 but I am trying to be cautious. */
6978 else if (event.type == ButtonRelease)
6980 if (!NILP (last_mouse_press_frame))
6982 f = XFRAME (last_mouse_press_frame);
6983 if (f->output_data.x)
6984 SET_SAVED_BUTTON_EVENT;
6986 else
6987 goto OTHER;
6989 #endif /* USE_MOTIF */
6990 else
6991 goto OTHER;
6992 #endif /* USE_X_TOOLKIT || USE_GTK */
6994 break;
6996 case CirculateNotify:
6997 goto OTHER;
6999 case CirculateRequest:
7000 goto OTHER;
7002 case VisibilityNotify:
7003 goto OTHER;
7005 case MappingNotify:
7006 /* Someone has changed the keyboard mapping - update the
7007 local cache. */
7008 switch (event.xmapping.request)
7010 case MappingModifier:
7011 x_find_modifier_meanings (dpyinfo);
7012 /* This is meant to fall through. */
7013 case MappingKeyboard:
7014 XRefreshKeyboardMapping (&event.xmapping);
7016 goto OTHER;
7018 case DestroyNotify:
7019 xft_settings_event (dpyinfo, &event);
7020 break;
7022 default:
7023 OTHER:
7024 #ifdef USE_X_TOOLKIT
7025 BLOCK_INPUT;
7026 if (*finish != X_EVENT_DROP)
7027 XtDispatchEvent (&event);
7028 UNBLOCK_INPUT;
7029 #endif /* USE_X_TOOLKIT */
7030 break;
7033 done:
7034 if (inev.ie.kind != NO_EVENT)
7036 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7037 count++;
7040 if (do_help
7041 && !(hold_quit && hold_quit->kind != NO_EVENT))
7043 Lisp_Object frame;
7045 if (f)
7046 XSETFRAME (frame, f);
7047 else
7048 frame = Qnil;
7050 if (do_help > 0)
7052 any_help_event_p = 1;
7053 gen_help_event (help_echo_string, frame, help_echo_window,
7054 help_echo_object, help_echo_pos);
7056 else
7058 help_echo_string = Qnil;
7059 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7061 count++;
7064 *eventp = event;
7065 return count;
7069 /* Handles the XEvent EVENT on display DISPLAY.
7070 This is used for event loops outside the normal event handling,
7071 i.e. looping while a popup menu or a dialog is posted.
7073 Returns the value handle_one_xevent sets in the finish argument. */
7075 x_dispatch_event (event, display)
7076 XEvent *event;
7077 Display *display;
7079 struct x_display_info *dpyinfo;
7080 int finish = X_EVENT_NORMAL;
7082 dpyinfo = x_display_info_for_display (display);
7084 if (dpyinfo)
7085 handle_one_xevent (dpyinfo, event, &finish, 0);
7087 return finish;
7091 /* Read events coming from the X server.
7092 This routine is called by the SIGIO handler.
7093 We return as soon as there are no more events to be read.
7095 We return the number of characters stored into the buffer,
7096 thus pretending to be `read' (except the characters we store
7097 in the keyboard buffer can be multibyte, so are not necessarily
7098 C chars).
7100 EXPECTED is nonzero if the caller knows input is available. */
7102 static int
7103 XTread_socket (terminal, expected, hold_quit)
7104 struct terminal *terminal;
7105 int expected;
7106 struct input_event *hold_quit;
7108 int count = 0;
7109 XEvent event;
7110 int event_found = 0;
7112 if (interrupt_input_blocked)
7114 interrupt_input_pending = 1;
7115 #ifdef SYNC_INPUT
7116 pending_signals = 1;
7117 #endif
7118 return -1;
7121 interrupt_input_pending = 0;
7122 #ifdef SYNC_INPUT
7123 pending_signals = pending_atimers;
7124 #endif
7125 BLOCK_INPUT;
7127 /* So people can tell when we have read the available input. */
7128 input_signal_count++;
7130 ++handling_signal;
7132 #ifdef HAVE_X_SM
7133 /* Only check session manager input for the primary display. */
7134 if (terminal->id == 1 && x_session_have_connection ())
7136 struct input_event inev;
7137 BLOCK_INPUT;
7138 /* We don't need to EVENT_INIT (inev) here, as
7139 x_session_check_input copies an entire input_event. */
7140 if (x_session_check_input (&inev))
7142 kbd_buffer_store_event_hold (&inev, hold_quit);
7143 count++;
7145 UNBLOCK_INPUT;
7147 #endif
7149 /* For debugging, this gives a way to fake an I/O error. */
7150 if (terminal->display_info.x == XTread_socket_fake_io_error)
7152 XTread_socket_fake_io_error = 0;
7153 x_io_error_quitter (terminal->display_info.x->display);
7156 #ifndef USE_GTK
7157 while (XPending (terminal->display_info.x->display))
7159 int finish;
7161 XNextEvent (terminal->display_info.x->display, &event);
7163 #ifdef HAVE_X_I18N
7164 /* Filter events for the current X input method. */
7165 if (x_filter_event (terminal->display_info.x, &event))
7166 continue;
7167 #endif
7168 event_found = 1;
7170 count += handle_one_xevent (terminal->display_info.x,
7171 &event, &finish, hold_quit);
7173 if (finish == X_EVENT_GOTO_OUT)
7174 goto out;
7177 #else /* USE_GTK */
7179 /* For GTK we must use the GTK event loop. But XEvents gets passed
7180 to our filter function above, and then to the big event switch.
7181 We use a bunch of globals to communicate with our filter function,
7182 that is kind of ugly, but it works.
7184 There is no way to do one display at the time, GTK just does events
7185 from all displays. */
7187 while (gtk_events_pending ())
7189 current_count = count;
7190 current_hold_quit = hold_quit;
7192 gtk_main_iteration ();
7194 count = current_count;
7195 current_count = -1;
7196 current_hold_quit = 0;
7198 if (current_finish == X_EVENT_GOTO_OUT)
7199 break;
7201 #endif /* USE_GTK */
7203 out:;
7205 /* On some systems, an X bug causes Emacs to get no more events
7206 when the window is destroyed. Detect that. (1994.) */
7207 if (! event_found)
7209 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7210 One XNOOP in 100 loops will make Emacs terminate.
7211 B. Bretthauer, 1994 */
7212 x_noop_count++;
7213 if (x_noop_count >= 100)
7215 x_noop_count=0;
7217 if (next_noop_dpyinfo == 0)
7218 next_noop_dpyinfo = x_display_list;
7220 XNoOp (next_noop_dpyinfo->display);
7222 /* Each time we get here, cycle through the displays now open. */
7223 next_noop_dpyinfo = next_noop_dpyinfo->next;
7227 /* If the focus was just given to an auto-raising frame,
7228 raise it now. */
7229 /* ??? This ought to be able to handle more than one such frame. */
7230 if (pending_autoraise_frame)
7232 x_raise_frame (pending_autoraise_frame);
7233 pending_autoraise_frame = 0;
7236 --handling_signal;
7237 UNBLOCK_INPUT;
7239 return count;
7245 /***********************************************************************
7246 Text Cursor
7247 ***********************************************************************/
7249 /* Set clipping for output in glyph row ROW. W is the window in which
7250 we operate. GC is the graphics context to set clipping in.
7252 ROW may be a text row or, e.g., a mode line. Text rows must be
7253 clipped to the interior of the window dedicated to text display,
7254 mode lines must be clipped to the whole window. */
7256 static void
7257 x_clip_to_row (w, row, area, gc)
7258 struct window *w;
7259 struct glyph_row *row;
7260 int area;
7261 GC gc;
7263 struct frame *f = XFRAME (WINDOW_FRAME (w));
7264 XRectangle clip_rect;
7265 int window_x, window_y, window_width;
7267 window_box (w, area, &window_x, &window_y, &window_width, 0);
7269 clip_rect.x = window_x;
7270 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7271 clip_rect.y = max (clip_rect.y, window_y);
7272 clip_rect.width = window_width;
7273 clip_rect.height = row->visible_height;
7275 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7279 /* Draw a hollow box cursor on window W in glyph row ROW. */
7281 static void
7282 x_draw_hollow_cursor (w, row)
7283 struct window *w;
7284 struct glyph_row *row;
7286 struct frame *f = XFRAME (WINDOW_FRAME (w));
7287 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7288 Display *dpy = FRAME_X_DISPLAY (f);
7289 int x, y, wd, h;
7290 XGCValues xgcv;
7291 struct glyph *cursor_glyph;
7292 GC gc;
7294 /* Get the glyph the cursor is on. If we can't tell because
7295 the current matrix is invalid or such, give up. */
7296 cursor_glyph = get_phys_cursor_glyph (w);
7297 if (cursor_glyph == NULL)
7298 return;
7300 /* Compute frame-relative coordinates for phys cursor. */
7301 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7302 wd = w->phys_cursor_width;
7304 /* The foreground of cursor_gc is typically the same as the normal
7305 background color, which can cause the cursor box to be invisible. */
7306 xgcv.foreground = f->output_data.x->cursor_pixel;
7307 if (dpyinfo->scratch_cursor_gc)
7308 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7309 else
7310 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7311 GCForeground, &xgcv);
7312 gc = dpyinfo->scratch_cursor_gc;
7314 /* Set clipping, draw the rectangle, and reset clipping again. */
7315 x_clip_to_row (w, row, TEXT_AREA, gc);
7316 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7317 XSetClipMask (dpy, gc, None);
7321 /* Draw a bar cursor on window W in glyph row ROW.
7323 Implementation note: One would like to draw a bar cursor with an
7324 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7325 Unfortunately, I didn't find a font yet that has this property set.
7326 --gerd. */
7328 static void
7329 x_draw_bar_cursor (w, row, width, kind)
7330 struct window *w;
7331 struct glyph_row *row;
7332 int width;
7333 enum text_cursor_kinds kind;
7335 struct frame *f = XFRAME (w->frame);
7336 struct glyph *cursor_glyph;
7338 /* If cursor is out of bounds, don't draw garbage. This can happen
7339 in mini-buffer windows when switching between echo area glyphs
7340 and mini-buffer. */
7341 cursor_glyph = get_phys_cursor_glyph (w);
7342 if (cursor_glyph == NULL)
7343 return;
7345 /* If on an image, draw like a normal cursor. That's usually better
7346 visible than drawing a bar, esp. if the image is large so that
7347 the bar might not be in the window. */
7348 if (cursor_glyph->type == IMAGE_GLYPH)
7350 struct glyph_row *row;
7351 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7352 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7354 else
7356 Display *dpy = FRAME_X_DISPLAY (f);
7357 Window window = FRAME_X_WINDOW (f);
7358 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7359 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7360 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7361 XGCValues xgcv;
7363 /* If the glyph's background equals the color we normally draw
7364 the bars cursor in, the bar cursor in its normal color is
7365 invisible. Use the glyph's foreground color instead in this
7366 case, on the assumption that the glyph's colors are chosen so
7367 that the glyph is legible. */
7368 if (face->background == f->output_data.x->cursor_pixel)
7369 xgcv.background = xgcv.foreground = face->foreground;
7370 else
7371 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7372 xgcv.graphics_exposures = 0;
7374 if (gc)
7375 XChangeGC (dpy, gc, mask, &xgcv);
7376 else
7378 gc = XCreateGC (dpy, window, mask, &xgcv);
7379 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7382 x_clip_to_row (w, row, TEXT_AREA, gc);
7384 if (kind == BAR_CURSOR)
7386 if (width < 0)
7387 width = FRAME_CURSOR_WIDTH (f);
7388 width = min (cursor_glyph->pixel_width, width);
7390 w->phys_cursor_width = width;
7392 XFillRectangle (dpy, window, gc,
7393 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7394 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7395 width, row->height);
7397 else
7399 int dummy_x, dummy_y, dummy_h;
7401 if (width < 0)
7402 width = row->height;
7404 width = min (row->height, width);
7406 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
7407 &dummy_y, &dummy_h);
7409 XFillRectangle (dpy, window, gc,
7410 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7411 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7412 row->height - width),
7413 w->phys_cursor_width, width);
7416 XSetClipMask (dpy, gc, None);
7421 /* RIF: Define cursor CURSOR on frame F. */
7423 static void
7424 x_define_frame_cursor (f, cursor)
7425 struct frame *f;
7426 Cursor cursor;
7428 if (!f->pointer_invisible
7429 && f->output_data.x->current_cursor != cursor)
7430 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7431 f->output_data.x->current_cursor = cursor;
7435 /* RIF: Clear area on frame F. */
7437 static void
7438 x_clear_frame_area (f, x, y, width, height)
7439 struct frame *f;
7440 int x, y, width, height;
7442 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7443 x, y, width, height, False);
7447 /* RIF: Draw cursor on window W. */
7449 static void
7450 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7451 struct window *w;
7452 struct glyph_row *glyph_row;
7453 int x, y;
7454 int cursor_type, cursor_width;
7455 int on_p, active_p;
7457 struct frame *f = XFRAME (WINDOW_FRAME (w));
7459 if (on_p)
7461 w->phys_cursor_type = cursor_type;
7462 w->phys_cursor_on_p = 1;
7464 if (glyph_row->exact_window_width_line_p
7465 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7467 glyph_row->cursor_in_fringe_p = 1;
7468 draw_fringe_bitmap (w, glyph_row, 0);
7470 else
7471 switch (cursor_type)
7473 case HOLLOW_BOX_CURSOR:
7474 x_draw_hollow_cursor (w, glyph_row);
7475 break;
7477 case FILLED_BOX_CURSOR:
7478 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7479 break;
7481 case BAR_CURSOR:
7482 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7483 break;
7485 case HBAR_CURSOR:
7486 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7487 break;
7489 case NO_CURSOR:
7490 w->phys_cursor_width = 0;
7491 break;
7493 default:
7494 abort ();
7497 #ifdef HAVE_X_I18N
7498 if (w == XWINDOW (f->selected_window))
7499 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7500 xic_set_preeditarea (w, x, y);
7501 #endif
7504 #ifndef XFlush
7505 XFlush (FRAME_X_DISPLAY (f));
7506 #endif
7510 /* Icons. */
7512 /* Make the x-window of frame F use the gnu icon bitmap. */
7515 x_bitmap_icon (f, file)
7516 struct frame *f;
7517 Lisp_Object file;
7519 int bitmap_id;
7521 if (FRAME_X_WINDOW (f) == 0)
7522 return 1;
7524 /* Free up our existing icon bitmap and mask if any. */
7525 if (f->output_data.x->icon_bitmap > 0)
7526 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7527 f->output_data.x->icon_bitmap = 0;
7529 if (STRINGP (file))
7531 #ifdef USE_GTK
7532 /* Use gtk_window_set_icon_from_file () if available,
7533 It's not restricted to bitmaps */
7534 if (xg_set_icon (f, file))
7535 return 0;
7536 #endif /* USE_GTK */
7537 bitmap_id = x_create_bitmap_from_file (f, file);
7538 x_create_bitmap_mask (f, bitmap_id);
7540 else
7542 /* Create the GNU bitmap and mask if necessary. */
7543 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7545 int rc = -1;
7547 #ifdef USE_GTK
7549 if (xg_set_icon (f, xg_default_icon_file)
7550 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7551 return 0;
7553 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7555 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7556 if (rc != -1)
7557 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7559 #endif
7561 /* If all else fails, use the (black and white) xbm image. */
7562 if (rc == -1)
7564 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7565 gnu_xbm_width, gnu_xbm_height);
7566 if (rc == -1)
7567 return 1;
7569 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7570 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7574 /* The first time we create the GNU bitmap and mask,
7575 this increments the ref-count one extra time.
7576 As a result, the GNU bitmap and mask are never freed.
7577 That way, we don't have to worry about allocating it again. */
7578 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7580 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7583 x_wm_set_icon_pixmap (f, bitmap_id);
7584 f->output_data.x->icon_bitmap = bitmap_id;
7586 return 0;
7590 /* Make the x-window of frame F use a rectangle with text.
7591 Use ICON_NAME as the text. */
7594 x_text_icon (f, icon_name)
7595 struct frame *f;
7596 char *icon_name;
7598 if (FRAME_X_WINDOW (f) == 0)
7599 return 1;
7602 XTextProperty text;
7603 text.value = (unsigned char *) icon_name;
7604 text.encoding = XA_STRING;
7605 text.format = 8;
7606 text.nitems = strlen (icon_name);
7607 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7610 if (f->output_data.x->icon_bitmap > 0)
7611 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7612 f->output_data.x->icon_bitmap = 0;
7613 x_wm_set_icon_pixmap (f, 0);
7615 return 0;
7618 #define X_ERROR_MESSAGE_SIZE 200
7620 /* If non-nil, this should be a string.
7621 It means catch X errors and store the error message in this string.
7623 The reason we use a stack is that x_catch_error/x_uncatch_error can
7624 be called from a signal handler.
7627 struct x_error_message_stack {
7628 char string[X_ERROR_MESSAGE_SIZE];
7629 Display *dpy;
7630 struct x_error_message_stack *prev;
7632 static struct x_error_message_stack *x_error_message;
7634 /* An X error handler which stores the error message in
7635 *x_error_message. This is called from x_error_handler if
7636 x_catch_errors is in effect. */
7638 static void
7639 x_error_catcher (display, error)
7640 Display *display;
7641 XErrorEvent *error;
7643 XGetErrorText (display, error->error_code,
7644 x_error_message->string,
7645 X_ERROR_MESSAGE_SIZE);
7648 /* Begin trapping X errors for display DPY. Actually we trap X errors
7649 for all displays, but DPY should be the display you are actually
7650 operating on.
7652 After calling this function, X protocol errors no longer cause
7653 Emacs to exit; instead, they are recorded in the string
7654 stored in *x_error_message.
7656 Calling x_check_errors signals an Emacs error if an X error has
7657 occurred since the last call to x_catch_errors or x_check_errors.
7659 Calling x_uncatch_errors resumes the normal error handling. */
7661 void x_check_errors ();
7663 void
7664 x_catch_errors (dpy)
7665 Display *dpy;
7667 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7669 /* Make sure any errors from previous requests have been dealt with. */
7670 XSync (dpy, False);
7672 data->dpy = dpy;
7673 data->string[0] = 0;
7674 data->prev = x_error_message;
7675 x_error_message = data;
7678 /* Undo the last x_catch_errors call.
7679 DPY should be the display that was passed to x_catch_errors. */
7681 void
7682 x_uncatch_errors ()
7684 struct x_error_message_stack *tmp;
7686 BLOCK_INPUT;
7688 /* The display may have been closed before this function is called.
7689 Check if it is still open before calling XSync. */
7690 if (x_display_info_for_display (x_error_message->dpy) != 0)
7691 XSync (x_error_message->dpy, False);
7693 tmp = x_error_message;
7694 x_error_message = x_error_message->prev;
7695 xfree (tmp);
7696 UNBLOCK_INPUT;
7699 /* If any X protocol errors have arrived since the last call to
7700 x_catch_errors or x_check_errors, signal an Emacs error using
7701 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7703 void
7704 x_check_errors (dpy, format)
7705 Display *dpy;
7706 char *format;
7708 /* Make sure to catch any errors incurred so far. */
7709 XSync (dpy, False);
7711 if (x_error_message->string[0])
7713 char string[X_ERROR_MESSAGE_SIZE];
7714 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7715 x_uncatch_errors ();
7716 error (format, string);
7720 /* Nonzero if we had any X protocol errors
7721 since we did x_catch_errors on DPY. */
7724 x_had_errors_p (dpy)
7725 Display *dpy;
7727 /* Make sure to catch any errors incurred so far. */
7728 XSync (dpy, False);
7730 return x_error_message->string[0] != 0;
7733 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7735 void
7736 x_clear_errors (dpy)
7737 Display *dpy;
7739 x_error_message->string[0] = 0;
7742 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7743 * idea. --lorentey */
7744 /* Close off all unclosed x_catch_errors calls. */
7746 void
7747 x_fully_uncatch_errors ()
7749 while (x_error_message)
7750 x_uncatch_errors ();
7752 #endif
7754 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7757 x_catching_errors ()
7759 return x_error_message != 0;
7762 #if 0
7763 static unsigned int x_wire_count;
7764 x_trace_wire ()
7766 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7768 #endif /* ! 0 */
7771 /* Handle SIGPIPE, which can happen when the connection to a server
7772 simply goes away. SIGPIPE is handled by x_connection_signal.
7773 Don't need to do anything, because the write which caused the
7774 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7775 which will do the appropriate cleanup for us. */
7777 static SIGTYPE
7778 x_connection_signal (signalnum) /* If we don't have an argument, */
7779 int signalnum; /* some compilers complain in signal calls. */
7781 #ifdef USG
7782 /* USG systems forget handlers when they are used;
7783 must reestablish each time */
7784 signal (signalnum, x_connection_signal);
7785 #endif /* USG */
7789 /************************************************************************
7790 Handling X errors
7791 ************************************************************************/
7793 /* Error message passed to x_connection_closed. */
7795 static char *error_msg;
7797 /* Function installed as fatal_error_signal_hook in
7798 x_connection_closed. Print the X error message, and exit normally,
7799 instead of dumping core when XtCloseDisplay fails. */
7801 static void
7802 x_fatal_error_signal ()
7804 fprintf (stderr, "%s\n", error_msg);
7805 exit (70);
7808 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7809 the text of an error message that lead to the connection loss. */
7811 static SIGTYPE
7812 x_connection_closed (dpy, error_message)
7813 Display *dpy;
7814 char *error_message;
7816 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7817 Lisp_Object frame, tail;
7818 int index = SPECPDL_INDEX ();
7820 error_msg = (char *) alloca (strlen (error_message) + 1);
7821 strcpy (error_msg, error_message);
7822 handling_signal = 0;
7824 /* Prevent being called recursively because of an error condition
7825 below. Otherwise, we might end up with printing ``can't find per
7826 display information'' in the recursive call instead of printing
7827 the original message here. */
7828 x_catch_errors (dpy);
7830 /* Inhibit redisplay while frames are being deleted. */
7831 specbind (Qinhibit_redisplay, Qt);
7833 if (dpyinfo)
7835 /* Protect display from being closed when we delete the last
7836 frame on it. */
7837 dpyinfo->reference_count++;
7838 dpyinfo->terminal->reference_count++;
7841 /* First delete frames whose mini-buffers are on frames
7842 that are on the dead display. */
7843 FOR_EACH_FRAME (tail, frame)
7845 Lisp_Object minibuf_frame;
7846 minibuf_frame
7847 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7848 if (FRAME_X_P (XFRAME (frame))
7849 && FRAME_X_P (XFRAME (minibuf_frame))
7850 && ! EQ (frame, minibuf_frame)
7851 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7852 delete_frame (frame, Qnoelisp);
7855 /* Now delete all remaining frames on the dead display.
7856 We are now sure none of these is used as the mini-buffer
7857 for another frame that we need to delete. */
7858 FOR_EACH_FRAME (tail, frame)
7859 if (FRAME_X_P (XFRAME (frame))
7860 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7862 /* Set this to t so that delete_frame won't get confused
7863 trying to find a replacement. */
7864 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7865 delete_frame (frame, Qnoelisp);
7868 /* We have to close the display to inform Xt that it doesn't
7869 exist anymore. If we don't, Xt will continue to wait for
7870 events from the display. As a consequence, a sequence of
7872 M-x make-frame-on-display RET :1 RET
7873 ...kill the new frame, so that we get an IO error...
7874 M-x make-frame-on-display RET :1 RET
7876 will indefinitely wait in Xt for events for display `:1', opened
7877 in the first call to make-frame-on-display.
7879 Closing the display is reported to lead to a bus error on
7880 OpenWindows in certain situations. I suspect that is a bug
7881 in OpenWindows. I don't know how to circumvent it here. */
7883 if (dpyinfo)
7885 #ifdef USE_X_TOOLKIT
7886 /* If DPYINFO is null, this means we didn't open the display
7887 in the first place, so don't try to close it. */
7889 extern void (*fatal_error_signal_hook) P_ ((void));
7890 fatal_error_signal_hook = x_fatal_error_signal;
7891 XtCloseDisplay (dpy);
7892 fatal_error_signal_hook = NULL;
7894 #endif
7896 #ifdef USE_GTK
7897 /* Due to bugs in some Gtk+ versions, just exit here if this
7898 is the last display/terminal. */
7899 if (terminal_list->next_terminal == NULL)
7901 fprintf (stderr, "%s\n", error_msg);
7902 shut_down_emacs (0, 0, Qnil);
7903 exit (70);
7905 xg_display_close (dpyinfo->display);
7906 #endif
7908 /* Indicate that this display is dead. */
7909 dpyinfo->display = 0;
7911 dpyinfo->reference_count--;
7912 dpyinfo->terminal->reference_count--;
7913 if (dpyinfo->reference_count != 0)
7914 /* We have just closed all frames on this display. */
7915 abort ();
7918 Lisp_Object tmp;
7919 XSETTERMINAL (tmp, dpyinfo->terminal);
7920 Fdelete_terminal (tmp, Qnoelisp);
7924 x_uncatch_errors ();
7926 if (terminal_list == 0)
7928 fprintf (stderr, "%s\n", error_msg);
7929 shut_down_emacs (0, 0, Qnil);
7930 exit (70);
7933 /* Ordinary stack unwind doesn't deal with these. */
7934 #ifdef SIGIO
7935 sigunblock (sigmask (SIGIO));
7936 #endif
7937 sigunblock (sigmask (SIGALRM));
7938 TOTALLY_UNBLOCK_INPUT;
7940 unbind_to (index, Qnil);
7941 clear_waiting_for_input ();
7942 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7943 longjmp), because returning from this function would get us back into
7944 Xlib's code which will directly call `exit'. */
7945 error ("%s", error_msg);
7948 /* We specifically use it before defining it, so that gcc doesn't inline it,
7949 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7950 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7952 /* This is the first-level handler for X protocol errors.
7953 It calls x_error_quitter or x_error_catcher. */
7955 static int
7956 x_error_handler (display, error)
7957 Display *display;
7958 XErrorEvent *error;
7960 if (x_error_message)
7961 x_error_catcher (display, error);
7962 else
7963 x_error_quitter (display, error);
7964 return 0;
7967 /* This is the usual handler for X protocol errors.
7968 It kills all frames on the display that we got the error for.
7969 If that was the only one, it prints an error message and kills Emacs. */
7971 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7973 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
7974 #define NO_INLINE __attribute__((noinline))
7975 #else
7976 #define NO_INLINE
7977 #endif
7979 /* Some versions of GNU/Linux define noinline in their headers. */
7981 #ifdef noinline
7982 #undef noinline
7983 #endif
7985 /* On older GCC versions, just putting x_error_quitter
7986 after x_error_handler prevents inlining into the former. */
7988 static void NO_INLINE
7989 x_error_quitter (display, error)
7990 Display *display;
7991 XErrorEvent *error;
7993 char buf[256], buf1[356];
7995 /* Ignore BadName errors. They can happen because of fonts
7996 or colors that are not defined. */
7998 if (error->error_code == BadName)
7999 return;
8001 /* Note that there is no real way portable across R3/R4 to get the
8002 original error handler. */
8004 XGetErrorText (display, error->error_code, buf, sizeof (buf));
8005 sprintf (buf1, "X protocol error: %s on protocol request %d",
8006 buf, error->request_code);
8007 x_connection_closed (display, buf1);
8011 /* This is the handler for X IO errors, always.
8012 It kills all frames on the display that we lost touch with.
8013 If that was the only one, it prints an error message and kills Emacs. */
8015 static int
8016 x_io_error_quitter (display)
8017 Display *display;
8019 char buf[256];
8021 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
8022 x_connection_closed (display, buf);
8023 return 0;
8026 /* Changing the font of the frame. */
8028 /* Give frame F the font FONT-OBJECT as its default font. The return
8029 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
8030 frame. If it is negative, generate a new fontset from
8031 FONT-OBJECT. */
8033 Lisp_Object
8034 x_new_font (f, font_object, fontset)
8035 struct frame *f;
8036 Lisp_Object font_object;
8037 int fontset;
8039 struct font *font = XFONT_OBJECT (font_object);
8041 if (fontset < 0)
8042 fontset = fontset_from_font (font_object);
8043 FRAME_FONTSET (f) = fontset;
8044 if (FRAME_FONT (f) == font)
8045 /* This font is already set in frame F. There's nothing more to
8046 do. */
8047 return font_object;
8049 FRAME_FONT (f) = font;
8050 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8051 FRAME_COLUMN_WIDTH (f) = font->average_width;
8052 FRAME_SPACE_WIDTH (f) = font->space_width;
8053 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8055 compute_fringe_widths (f, 1);
8057 /* Compute the scroll bar width in character columns. */
8058 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8060 int wid = FRAME_COLUMN_WIDTH (f);
8061 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8062 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8064 else
8066 int wid = FRAME_COLUMN_WIDTH (f);
8067 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8070 if (FRAME_X_WINDOW (f) != 0)
8072 /* Don't change the size of a tip frame; there's no point in
8073 doing it because it's done in Fx_show_tip, and it leads to
8074 problems because the tip frame has no widget. */
8075 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8076 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8079 #ifdef HAVE_X_I18N
8080 if (FRAME_XIC (f)
8081 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8083 BLOCK_INPUT;
8084 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8085 UNBLOCK_INPUT;
8087 #endif
8089 return font_object;
8093 /***********************************************************************
8094 X Input Methods
8095 ***********************************************************************/
8097 #ifdef HAVE_X_I18N
8099 #ifdef HAVE_X11R6
8101 /* XIM destroy callback function, which is called whenever the
8102 connection to input method XIM dies. CLIENT_DATA contains a
8103 pointer to the x_display_info structure corresponding to XIM. */
8105 static void
8106 xim_destroy_callback (xim, client_data, call_data)
8107 XIM xim;
8108 XPointer client_data;
8109 XPointer call_data;
8111 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8112 Lisp_Object frame, tail;
8114 BLOCK_INPUT;
8116 /* No need to call XDestroyIC.. */
8117 FOR_EACH_FRAME (tail, frame)
8119 struct frame *f = XFRAME (frame);
8120 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8122 FRAME_XIC (f) = NULL;
8123 xic_free_xfontset (f);
8127 /* No need to call XCloseIM. */
8128 dpyinfo->xim = NULL;
8129 XFree (dpyinfo->xim_styles);
8130 UNBLOCK_INPUT;
8133 #endif /* HAVE_X11R6 */
8135 #ifdef HAVE_X11R6
8136 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8137 extern char *XSetIMValues P_ ((XIM, ...));
8138 #endif
8140 /* Open the connection to the XIM server on display DPYINFO.
8141 RESOURCE_NAME is the resource name Emacs uses. */
8143 static void
8144 xim_open_dpy (dpyinfo, resource_name)
8145 struct x_display_info *dpyinfo;
8146 char *resource_name;
8148 XIM xim;
8150 #ifdef HAVE_XIM
8151 if (use_xim)
8153 if (dpyinfo->xim)
8154 XCloseIM (dpyinfo->xim);
8155 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8156 EMACS_CLASS);
8157 dpyinfo->xim = xim;
8159 if (xim)
8161 #ifdef HAVE_X11R6
8162 XIMCallback destroy;
8163 #endif
8165 /* Get supported styles and XIM values. */
8166 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8168 #ifdef HAVE_X11R6
8169 destroy.callback = xim_destroy_callback;
8170 destroy.client_data = (XPointer)dpyinfo;
8171 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8172 #endif
8176 else
8177 #endif /* HAVE_XIM */
8178 dpyinfo->xim = NULL;
8182 #ifdef HAVE_X11R6_XIM
8184 /* XIM instantiate callback function, which is called whenever an XIM
8185 server is available. DISPLAY is the display of the XIM.
8186 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8187 when the callback was registered. */
8189 static void
8190 xim_instantiate_callback (display, client_data, call_data)
8191 Display *display;
8192 XPointer client_data;
8193 XPointer call_data;
8195 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8196 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8198 /* We don't support multiple XIM connections. */
8199 if (dpyinfo->xim)
8200 return;
8202 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8204 /* Create XIC for the existing frames on the same display, as long
8205 as they have no XIC. */
8206 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8208 Lisp_Object tail, frame;
8210 BLOCK_INPUT;
8211 FOR_EACH_FRAME (tail, frame)
8213 struct frame *f = XFRAME (frame);
8215 if (FRAME_X_P (f)
8216 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8217 if (FRAME_XIC (f) == NULL)
8219 create_frame_xic (f);
8220 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8221 xic_set_statusarea (f);
8222 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8224 struct window *w = XWINDOW (f->selected_window);
8225 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8230 UNBLOCK_INPUT;
8234 #endif /* HAVE_X11R6_XIM */
8237 /* Open a connection to the XIM server on display DPYINFO.
8238 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8239 connection only at the first time. On X11R6, open the connection
8240 in the XIM instantiate callback function. */
8242 static void
8243 xim_initialize (dpyinfo, resource_name)
8244 struct x_display_info *dpyinfo;
8245 char *resource_name;
8247 dpyinfo->xim = NULL;
8248 #ifdef HAVE_XIM
8249 if (use_xim)
8251 #ifdef HAVE_X11R6_XIM
8252 struct xim_inst_t *xim_inst;
8253 int len;
8255 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8256 dpyinfo->xim_callback_data = xim_inst;
8257 xim_inst->dpyinfo = dpyinfo;
8258 len = strlen (resource_name);
8259 xim_inst->resource_name = (char *) xmalloc (len + 1);
8260 bcopy (resource_name, xim_inst->resource_name, len + 1);
8261 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8262 resource_name, EMACS_CLASS,
8263 xim_instantiate_callback,
8264 /* This is XPointer in XFree86
8265 but (XPointer *) on Tru64, at
8266 least, hence the configure test. */
8267 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8268 #else /* not HAVE_X11R6_XIM */
8269 xim_open_dpy (dpyinfo, resource_name);
8270 #endif /* not HAVE_X11R6_XIM */
8272 #endif /* HAVE_XIM */
8276 /* Close the connection to the XIM server on display DPYINFO. */
8278 static void
8279 xim_close_dpy (dpyinfo)
8280 struct x_display_info *dpyinfo;
8282 #ifdef HAVE_XIM
8283 if (use_xim)
8285 #ifdef HAVE_X11R6_XIM
8286 if (dpyinfo->display)
8287 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8288 NULL, EMACS_CLASS,
8289 xim_instantiate_callback, NULL);
8290 xfree (dpyinfo->xim_callback_data->resource_name);
8291 xfree (dpyinfo->xim_callback_data);
8292 #endif /* HAVE_X11R6_XIM */
8293 if (dpyinfo->display)
8294 XCloseIM (dpyinfo->xim);
8295 dpyinfo->xim = NULL;
8296 XFree (dpyinfo->xim_styles);
8298 #endif /* HAVE_XIM */
8301 #endif /* not HAVE_X11R6_XIM */
8305 /* Calculate the absolute position in frame F
8306 from its current recorded position values and gravity. */
8308 void
8309 x_calc_absolute_position (f)
8310 struct frame *f;
8312 int flags = f->size_hint_flags;
8314 /* We have nothing to do if the current position
8315 is already for the top-left corner. */
8316 if (! ((flags & XNegative) || (flags & YNegative)))
8317 return;
8319 /* Treat negative positions as relative to the leftmost bottommost
8320 position that fits on the screen. */
8321 if (flags & XNegative)
8322 f->left_pos = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
8323 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8326 int height = FRAME_PIXEL_HEIGHT (f);
8328 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8329 /* Something is fishy here. When using Motif, starting Emacs with
8330 `-g -0-0', the frame appears too low by a few pixels.
8332 This seems to be so because initially, while Emacs is starting,
8333 the column widget's height and the frame's pixel height are
8334 different. The column widget's height is the right one. In
8335 later invocations, when Emacs is up, the frame's pixel height
8336 is right, though.
8338 It's not obvious where the initial small difference comes from.
8339 2000-12-01, gerd. */
8341 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8342 #endif
8344 if (flags & YNegative)
8345 f->top_pos = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
8346 - height + f->top_pos;
8349 /* The left_pos and top_pos
8350 are now relative to the top and left screen edges,
8351 so the flags should correspond. */
8352 f->size_hint_flags &= ~ (XNegative | YNegative);
8355 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8356 to really change the position, and 0 when calling from
8357 x_make_frame_visible (in that case, XOFF and YOFF are the current
8358 position values). It is -1 when calling from x_set_frame_parameters,
8359 which means, do adjust for borders but don't change the gravity. */
8361 void
8362 x_set_offset (f, xoff, yoff, change_gravity)
8363 struct frame *f;
8364 register int xoff, yoff;
8365 int change_gravity;
8367 int modified_top, modified_left;
8369 if (change_gravity > 0)
8371 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8372 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8374 f->top_pos = yoff;
8375 f->left_pos = xoff;
8376 f->size_hint_flags &= ~ (XNegative | YNegative);
8377 if (xoff < 0)
8378 f->size_hint_flags |= XNegative;
8379 if (yoff < 0)
8380 f->size_hint_flags |= YNegative;
8381 f->win_gravity = NorthWestGravity;
8383 x_calc_absolute_position (f);
8385 BLOCK_INPUT;
8386 x_wm_set_size_hint (f, (long) 0, 0);
8388 modified_left = f->left_pos;
8389 modified_top = f->top_pos;
8391 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8393 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8394 than the WM decorations. So we use the calculated offset instead
8395 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8396 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8397 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8400 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8401 modified_left, modified_top);
8403 x_sync_with_move (f, f->left_pos, f->top_pos,
8404 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8405 ? 1 : 0);
8407 /* change_gravity is non-zero when this function is called from Lisp to
8408 programmatically move a frame. In that case, we call
8409 x_check_expected_move to discover if we have a "Type A" or "Type B"
8410 window manager, and, for a "Type A" window manager, adjust the position
8411 of the frame.
8413 We call x_check_expected_move if a programmatic move occurred, and
8414 either the window manager type (A/B) is unknown or it is Type A but we
8415 need to compute the top/left offset adjustment for this frame. */
8417 if (change_gravity != 0 &&
8418 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8419 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8420 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8421 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8422 x_check_expected_move (f, modified_left, modified_top);
8424 UNBLOCK_INPUT;
8427 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8428 on the root window for frame F contains ATOMNAME.
8429 This is how a WM check shall be done according to the Window Manager
8430 Specification/Extended Window Manager Hints at
8431 http://freedesktop.org/wiki/Specifications/wm-spec. */
8433 static int
8434 wm_supports (f, atomname)
8435 struct frame *f;
8436 const char *atomname;
8438 Atom actual_type;
8439 unsigned long actual_size, bytes_remaining;
8440 int i, rc, actual_format;
8441 Atom prop_atom;
8442 Window wmcheck_window;
8443 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8444 Window target_window = dpyinfo->root_window;
8445 long max_len = 65536;
8446 Display *dpy = FRAME_X_DISPLAY (f);
8447 unsigned char *tmp_data = NULL;
8448 Atom target_type = XA_WINDOW;
8449 Atom want_atom;
8451 BLOCK_INPUT;
8453 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8455 x_catch_errors (dpy);
8456 rc = XGetWindowProperty (dpy, target_window,
8457 prop_atom, 0, max_len, False, target_type,
8458 &actual_type, &actual_format, &actual_size,
8459 &bytes_remaining, &tmp_data);
8461 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8463 if (tmp_data) XFree (tmp_data);
8464 x_uncatch_errors ();
8465 UNBLOCK_INPUT;
8466 return 0;
8469 wmcheck_window = *(Window *) tmp_data;
8470 XFree (tmp_data);
8472 /* Check if window exists. */
8473 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8474 x_sync (f);
8475 if (x_had_errors_p (dpy))
8477 x_uncatch_errors ();
8478 UNBLOCK_INPUT;
8479 return 0;
8482 if (dpyinfo->net_supported_window != wmcheck_window)
8484 /* Window changed, reload atoms */
8485 if (dpyinfo->net_supported_atoms != NULL)
8486 XFree (dpyinfo->net_supported_atoms);
8487 dpyinfo->net_supported_atoms = NULL;
8488 dpyinfo->nr_net_supported_atoms = 0;
8489 dpyinfo->net_supported_window = 0;
8491 target_type = XA_ATOM;
8492 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8493 tmp_data = NULL;
8494 rc = XGetWindowProperty (dpy, target_window,
8495 prop_atom, 0, max_len, False, target_type,
8496 &actual_type, &actual_format, &actual_size,
8497 &bytes_remaining, &tmp_data);
8499 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8501 if (tmp_data) XFree (tmp_data);
8502 x_uncatch_errors ();
8503 UNBLOCK_INPUT;
8504 return 0;
8507 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8508 dpyinfo->nr_net_supported_atoms = actual_size;
8509 dpyinfo->net_supported_window = wmcheck_window;
8512 rc = 0;
8513 want_atom = XInternAtom (dpy, atomname, False);
8515 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8516 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8518 x_uncatch_errors ();
8519 UNBLOCK_INPUT;
8521 return rc;
8524 static void
8525 set_wm_state (frame, add, what, what2)
8526 Lisp_Object frame;
8527 int add;
8528 const char *what;
8529 const char *what2;
8531 const char *atom = "_NET_WM_STATE";
8532 Fx_send_client_event (frame, make_number (0), frame,
8533 make_unibyte_string (atom, strlen (atom)),
8534 make_number (32),
8535 /* 1 = add, 0 = remove */
8536 Fcons
8537 (make_number (add ? 1 : 0),
8538 Fcons
8539 (make_unibyte_string (what, strlen (what)),
8540 what2 != 0
8541 ? Fcons (make_unibyte_string (what2, strlen (what2)),
8542 Qnil)
8543 : Qnil)));
8546 void
8547 x_set_sticky (f, new_value, old_value)
8548 struct frame *f;
8549 Lisp_Object new_value, old_value;
8551 Lisp_Object frame;
8553 XSETFRAME (frame, f);
8554 set_wm_state (frame, NILP (new_value) ? 0 : 1,
8555 "_NET_WM_STATE_STICKY", NULL);
8558 /* Do fullscreen as specified in extended window manager hints */
8560 static int
8561 do_ewmh_fullscreen (f)
8562 struct frame *f;
8564 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8566 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8567 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8568 if (!have_net_atom)
8569 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8571 if (have_net_atom)
8573 Lisp_Object frame;
8574 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8575 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8576 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8578 XSETFRAME (frame, f);
8580 set_wm_state (frame, 0, fs, NULL);
8581 set_wm_state (frame, 0, fh, NULL);
8582 set_wm_state (frame, 0, fw, NULL);
8584 /* If there are _NET_ atoms we assume we have extended window manager
8585 hints. */
8586 switch (f->want_fullscreen)
8588 case FULLSCREEN_BOTH:
8589 set_wm_state (frame, 1, fs, NULL);
8590 break;
8591 case FULLSCREEN_WIDTH:
8592 set_wm_state (frame, 1, fw, NULL);
8593 break;
8594 case FULLSCREEN_HEIGHT:
8595 set_wm_state (frame, 1, fh, NULL);
8596 break;
8597 case FULLSCREEN_MAXIMIZED:
8598 set_wm_state (frame, 1, fw, fh);
8599 break;
8602 f->want_fullscreen = FULLSCREEN_NONE;
8606 return have_net_atom;
8609 static void
8610 XTfullscreen_hook (f)
8611 FRAME_PTR f;
8613 if (f->async_visible)
8615 BLOCK_INPUT;
8616 x_check_fullscreen (f);
8617 x_sync (f);
8618 UNBLOCK_INPUT;
8623 static void
8624 x_handle_net_wm_state (f, event)
8625 struct frame *f;
8626 XPropertyEvent *event;
8628 Atom actual_type;
8629 unsigned long actual_size, bytes_remaining;
8630 int i, rc, actual_format, value = FULLSCREEN_NONE;
8631 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8632 long max_len = 65536;
8633 Display *dpy = FRAME_X_DISPLAY (f);
8634 unsigned char *tmp_data = NULL;
8635 Atom target_type = XA_ATOM;
8636 Lisp_Object lval;
8637 int sticky = 0;
8639 BLOCK_INPUT;
8640 x_catch_errors (dpy);
8641 rc = XGetWindowProperty (dpy, event->window,
8642 event->atom, 0, max_len, False, target_type,
8643 &actual_type, &actual_format, &actual_size,
8644 &bytes_remaining, &tmp_data);
8646 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
8648 if (tmp_data) XFree (tmp_data);
8649 x_uncatch_errors ();
8650 UNBLOCK_INPUT;
8651 return;
8654 x_uncatch_errors ();
8656 for (i = 0; i < actual_size; ++i)
8658 Atom a = ((Atom*)tmp_data)[i];
8659 if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
8661 if (value == FULLSCREEN_HEIGHT)
8662 value = FULLSCREEN_MAXIMIZED;
8663 else
8664 value = FULLSCREEN_WIDTH;
8666 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
8668 if (value == FULLSCREEN_WIDTH)
8669 value = FULLSCREEN_MAXIMIZED;
8670 else
8671 value = FULLSCREEN_HEIGHT;
8673 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen_atom)
8674 value = FULLSCREEN_BOTH;
8675 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
8676 sticky = 1;
8679 lval = Qnil;
8680 switch (value)
8682 case FULLSCREEN_WIDTH:
8683 lval = Qfullwidth;
8684 break;
8685 case FULLSCREEN_HEIGHT:
8686 lval = Qfullheight;
8687 break;
8688 case FULLSCREEN_BOTH:
8689 lval = Qfullboth;
8690 break;
8691 case FULLSCREEN_MAXIMIZED:
8692 lval = Qmaximized;
8693 break;
8696 store_frame_param (f, Qfullscreen, lval);
8697 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
8699 if (tmp_data) XFree (tmp_data);
8700 UNBLOCK_INPUT;
8703 /* Check if we need to resize the frame due to a fullscreen request.
8704 If so needed, resize the frame. */
8705 static void
8706 x_check_fullscreen (f)
8707 struct frame *f;
8709 if (do_ewmh_fullscreen (f))
8710 return;
8712 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8713 return; /* Only fullscreen without WM or with EWM hints (above). */
8715 if (f->want_fullscreen != FULLSCREEN_NONE)
8717 int width = FRAME_COLS (f), height = FRAME_LINES (f);
8718 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8720 switch (f->want_fullscreen)
8722 /* No difference between these two when there is no WM */
8723 case FULLSCREEN_BOTH:
8724 case FULLSCREEN_MAXIMIZED:
8725 width = x_display_pixel_width (dpyinfo);
8726 height = x_display_pixel_height (dpyinfo);
8727 break;
8728 case FULLSCREEN_WIDTH:
8729 width = x_display_pixel_width (dpyinfo);
8730 break;
8731 case FULLSCREEN_HEIGHT:
8732 height = x_display_pixel_height (dpyinfo);
8735 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8737 change_frame_size (f, height, width, 0, 1, 0);
8738 SET_FRAME_GARBAGED (f);
8739 cancel_mouse_face (f);
8744 /* This function is called by x_set_offset to determine whether the window
8745 manager interfered with the positioning of the frame. Type A window
8746 managers position the surrounding window manager decorations a small
8747 amount above and left of the user-supplied position. Type B window
8748 managers position the surrounding window manager decorations at the
8749 user-specified position. If we detect a Type A window manager, we
8750 compensate by moving the window right and down by the proper amount. */
8752 static void
8753 x_check_expected_move (f, expected_left, expected_top)
8754 struct frame *f;
8755 int expected_left;
8756 int expected_top;
8758 int current_left = 0, current_top = 0;
8760 /* x_real_positions returns the left and top offsets of the outermost
8761 window manager window around the frame. */
8763 x_real_positions (f, &current_left, &current_top);
8765 if (current_left != expected_left || current_top != expected_top)
8767 /* It's a "Type A" window manager. */
8769 int adjusted_left;
8770 int adjusted_top;
8772 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8773 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8774 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8776 /* Now fix the mispositioned frame's location. */
8778 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8779 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8781 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8782 adjusted_left, adjusted_top);
8784 x_sync_with_move (f, expected_left, expected_top, 0);
8786 else
8787 /* It's a "Type B" window manager. We don't have to adjust the
8788 frame's position. */
8790 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8794 /* Wait for XGetGeometry to return up-to-date position information for a
8795 recently-moved frame. Call this immediately after calling XMoveWindow.
8796 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8797 frame has been moved to, so we use a fuzzy position comparison instead
8798 of an exact comparison. */
8800 static void
8801 x_sync_with_move (f, left, top, fuzzy)
8802 struct frame *f;
8803 int left, top, fuzzy;
8805 int count = 0;
8807 while (count++ < 50)
8809 int current_left = 0, current_top = 0;
8811 /* In theory, this call to XSync only needs to happen once, but in
8812 practice, it doesn't seem to work, hence the need for the surrounding
8813 loop. */
8815 XSync (FRAME_X_DISPLAY (f), False);
8816 x_real_positions (f, &current_left, &current_top);
8818 if (fuzzy)
8820 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8821 pixels. */
8823 if (eabs (current_left - left) <= 10
8824 && eabs (current_top - top) <= 40)
8825 return;
8827 else if (current_left == left && current_top == top)
8828 return;
8831 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8832 will then return up-to-date position info. */
8834 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8838 /* Wait for an event on frame F matching EVENTTYPE. */
8839 void
8840 x_wait_for_event (f, eventtype)
8841 struct frame *f;
8842 int eventtype;
8844 int level = interrupt_input_blocked;
8846 SELECT_TYPE fds;
8847 EMACS_TIME tmo, tmo_at, time_now;
8848 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
8850 pending_event_wait.f = f;
8851 pending_event_wait.eventtype = eventtype;
8853 /* Set timeout to 0.1 second. Hopefully not noticable.
8854 Maybe it should be configurable. */
8855 EMACS_SET_SECS_USECS (tmo, 0, 100000);
8856 EMACS_GET_TIME (tmo_at);
8857 EMACS_ADD_TIME (tmo_at, tmo_at, tmo);
8859 while (pending_event_wait.eventtype)
8861 interrupt_input_pending = 1;
8862 TOTALLY_UNBLOCK_INPUT;
8863 /* XTread_socket is called after unblock. */
8864 BLOCK_INPUT;
8865 interrupt_input_blocked = level;
8867 FD_ZERO (&fds);
8868 FD_SET (fd, &fds);
8870 EMACS_GET_TIME (time_now);
8871 EMACS_SUB_TIME (tmo, tmo_at, time_now);
8873 if (EMACS_TIME_NEG_P (tmo) || select (fd+1, &fds, NULL, NULL, &tmo) == 0)
8874 break; /* Timeout */
8876 pending_event_wait.f = 0;
8877 pending_event_wait.eventtype = 0;
8881 /* Change the size of frame F's X window to COLS/ROWS in the case F
8882 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8883 top-left-corner window gravity for this size change and subsequent
8884 size changes. Otherwise we leave the window gravity unchanged. */
8886 static void
8887 x_set_window_size_1 (f, change_gravity, cols, rows)
8888 struct frame *f;
8889 int change_gravity;
8890 int cols, rows;
8892 int pixelwidth, pixelheight;
8894 check_frame_size (f, &rows, &cols);
8895 f->scroll_bar_actual_width
8896 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8898 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8899 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8900 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8902 compute_fringe_widths (f, 0);
8904 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
8905 + 2*f->border_width;
8906 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
8907 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f)
8908 + 2*f->border_width;
8910 if (change_gravity) f->win_gravity = NorthWestGravity;
8911 x_wm_set_size_hint (f, (long) 0, 0);
8912 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8913 pixelwidth, pixelheight);
8916 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8917 receive in the ConfigureNotify event; if we get what we asked
8918 for, then the event won't cause the screen to become garbaged, so
8919 we have to make sure to do it here. */
8920 SET_FRAME_GARBAGED (f);
8922 /* Now, strictly speaking, we can't be sure that this is accurate,
8923 but the window manager will get around to dealing with the size
8924 change request eventually, and we'll hear how it went when the
8925 ConfigureNotify event gets here.
8927 We could just not bother storing any of this information here,
8928 and let the ConfigureNotify event set everything up, but that
8929 might be kind of confusing to the Lisp code, since size changes
8930 wouldn't be reported in the frame parameters until some random
8931 point in the future when the ConfigureNotify event arrives.
8933 We pass 1 for DELAY since we can't run Lisp code inside of
8934 a BLOCK_INPUT. */
8936 /* But the ConfigureNotify may in fact never arrive, and then this is
8937 not right if the frame is visible. Instead wait (with timeout)
8938 for the ConfigureNotify. */
8939 if (f->async_visible)
8940 x_wait_for_event (f, ConfigureNotify);
8941 else
8943 change_frame_size (f, rows, cols, 0, 1, 0);
8944 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8945 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8946 x_sync (f);
8951 /* Call this to change the size of frame F's x-window.
8952 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8953 for this size change and subsequent size changes.
8954 Otherwise we leave the window gravity unchanged. */
8956 void
8957 x_set_window_size (f, change_gravity, cols, rows)
8958 struct frame *f;
8959 int change_gravity;
8960 int cols, rows;
8962 BLOCK_INPUT;
8964 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8966 int r, c;
8968 /* When the frame is maximized/fullscreen or running under for
8969 example Xmonad, x_set_window_size_1 will be a no-op.
8970 In that case, the right thing to do is extend rows/cols to
8971 the current frame size. We do that first if x_set_window_size_1
8972 turns out to not be a no-op (there is no way to know).
8973 The size will be adjusted again if the frame gets a
8974 ConfigureNotify event as a result of x_set_window_size. */
8975 int pixelh = FRAME_PIXEL_HEIGHT (f);
8976 #ifdef USE_X_TOOLKIT
8977 /* The menu bar is not part of text lines. The tool bar
8978 is however. */
8979 pixelh -= FRAME_MENUBAR_HEIGHT (f);
8980 #endif
8981 r = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelh);
8982 /* Update f->scroll_bar_actual_width because it is used in
8983 FRAME_PIXEL_WIDTH_TO_TEXT_COLS. */
8984 f->scroll_bar_actual_width
8985 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
8986 c = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, FRAME_PIXEL_WIDTH (f));
8987 change_frame_size (f, r, c, 0, 1, 0);
8990 #ifdef USE_GTK
8991 if (FRAME_GTK_WIDGET (f))
8992 xg_frame_set_char_size (f, cols, rows);
8993 else
8994 x_set_window_size_1 (f, change_gravity, cols, rows);
8995 #else /* not USE_GTK */
8997 x_set_window_size_1 (f, change_gravity, cols, rows);
8999 #endif /* not USE_GTK */
9001 /* If cursor was outside the new size, mark it as off. */
9002 mark_window_cursors_off (XWINDOW (f->root_window));
9004 /* Clear out any recollection of where the mouse highlighting was,
9005 since it might be in a place that's outside the new frame size.
9006 Actually checking whether it is outside is a pain in the neck,
9007 so don't try--just let the highlighting be done afresh with new size. */
9008 cancel_mouse_face (f);
9010 UNBLOCK_INPUT;
9013 /* Mouse warping. */
9015 void
9016 x_set_mouse_position (f, x, y)
9017 struct frame *f;
9018 int x, y;
9020 int pix_x, pix_y;
9022 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
9023 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
9025 if (pix_x < 0) pix_x = 0;
9026 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
9028 if (pix_y < 0) pix_y = 0;
9029 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
9031 BLOCK_INPUT;
9033 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9034 0, 0, 0, 0, pix_x, pix_y);
9035 UNBLOCK_INPUT;
9038 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9040 void
9041 x_set_mouse_pixel_position (f, pix_x, pix_y)
9042 struct frame *f;
9043 int pix_x, pix_y;
9045 BLOCK_INPUT;
9047 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9048 0, 0, 0, 0, pix_x, pix_y);
9049 UNBLOCK_INPUT;
9052 /* focus shifting, raising and lowering. */
9054 void
9055 x_focus_on_frame (f)
9056 struct frame *f;
9058 #if 0
9059 /* I don't think that the ICCCM allows programs to do things like this
9060 without the interaction of the window manager. Whatever you end up
9061 doing with this code, do it to x_unfocus_frame too. */
9062 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9063 RevertToPointerRoot, CurrentTime);
9064 #endif /* ! 0 */
9067 void
9068 x_unfocus_frame (f)
9069 struct frame *f;
9071 #if 0
9072 /* Look at the remarks in x_focus_on_frame. */
9073 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
9074 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
9075 RevertToPointerRoot, CurrentTime);
9076 #endif /* ! 0 */
9079 /* Raise frame F. */
9081 void
9082 x_raise_frame (f)
9083 struct frame *f;
9085 BLOCK_INPUT;
9086 if (f->async_visible)
9087 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9089 XFlush (FRAME_X_DISPLAY (f));
9090 UNBLOCK_INPUT;
9093 /* Lower frame F. */
9095 void
9096 x_lower_frame (f)
9097 struct frame *f;
9099 if (f->async_visible)
9101 BLOCK_INPUT;
9102 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9103 XFlush (FRAME_X_DISPLAY (f));
9104 UNBLOCK_INPUT;
9108 /* Activate frame with Extended Window Manager Hints */
9110 void
9111 x_ewmh_activate_frame (f)
9112 FRAME_PTR f;
9114 /* See Window Manager Specification/Extended Window Manager Hints at
9115 http://freedesktop.org/wiki/Specifications/wm-spec */
9117 const char *atom = "_NET_ACTIVE_WINDOW";
9118 if (f->async_visible && wm_supports (f, atom))
9120 Lisp_Object frame;
9121 XSETFRAME (frame, f);
9122 Fx_send_client_event (frame, make_number (0), frame,
9123 make_unibyte_string (atom, strlen (atom)),
9124 make_number (32),
9125 Fcons (make_number (1),
9126 Fcons (make_number (last_user_time),
9127 Qnil)));
9131 static void
9132 XTframe_raise_lower (f, raise_flag)
9133 FRAME_PTR f;
9134 int raise_flag;
9136 if (raise_flag)
9137 x_raise_frame (f);
9138 else
9139 x_lower_frame (f);
9142 /* XEmbed implementation. */
9144 void
9145 xembed_set_info (f, flags)
9146 struct frame *f;
9147 enum xembed_info flags;
9149 Atom atom;
9150 unsigned long data[2];
9152 atom = XInternAtom (FRAME_X_DISPLAY (f), "_XEMBED_INFO", False);
9154 data[0] = XEMBED_VERSION;
9155 data[1] = flags;
9157 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), atom, atom,
9158 32, PropModeReplace, (unsigned char *) data, 2);
9161 void
9162 xembed_send_message (f, time, message, detail, data1, data2)
9163 struct frame *f;
9164 Time time;
9165 enum xembed_message message;
9166 long detail;
9167 long data1;
9168 long data2;
9170 XEvent event;
9172 event.xclient.type = ClientMessage;
9173 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9174 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
9175 event.xclient.format = 32;
9176 event.xclient.data.l[0] = time;
9177 event.xclient.data.l[1] = message;
9178 event.xclient.data.l[2] = detail;
9179 event.xclient.data.l[3] = data1;
9180 event.xclient.data.l[4] = data2;
9182 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9183 False, NoEventMask, &event);
9184 XSync (FRAME_X_DISPLAY (f), False);
9187 /* Change of visibility. */
9189 /* This tries to wait until the frame is really visible.
9190 However, if the window manager asks the user where to position
9191 the frame, this will return before the user finishes doing that.
9192 The frame will not actually be visible at that time,
9193 but it will become visible later when the window manager
9194 finishes with it. */
9196 void
9197 x_make_frame_visible (f)
9198 struct frame *f;
9200 Lisp_Object type;
9201 int original_top, original_left;
9202 int retry_count = 2;
9204 retry:
9206 BLOCK_INPUT;
9208 type = x_icon_type (f);
9209 if (!NILP (type))
9210 x_bitmap_icon (f, type);
9212 if (! FRAME_VISIBLE_P (f))
9214 /* We test FRAME_GARBAGED_P here to make sure we don't
9215 call x_set_offset a second time
9216 if we get to x_make_frame_visible a second time
9217 before the window gets really visible. */
9218 if (! FRAME_ICONIFIED_P (f)
9219 && ! FRAME_X_EMBEDDED_P (f)
9220 && ! f->output_data.x->asked_for_visible)
9221 x_set_offset (f, f->left_pos, f->top_pos, 0);
9223 f->output_data.x->asked_for_visible = 1;
9225 if (! EQ (Vx_no_window_manager, Qt))
9226 x_wm_set_window_state (f, NormalState);
9227 #ifdef USE_X_TOOLKIT
9228 if (FRAME_X_EMBEDDED_P (f))
9229 xembed_set_info (f, XEMBED_MAPPED);
9230 else
9232 /* This was XtPopup, but that did nothing for an iconified frame. */
9233 XtMapWidget (f->output_data.x->widget);
9235 #else /* not USE_X_TOOLKIT */
9236 #ifdef USE_GTK
9237 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9238 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9239 #else
9240 if (FRAME_X_EMBEDDED_P (f))
9241 xembed_set_info (f, XEMBED_MAPPED);
9242 else
9243 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9244 #endif /* not USE_GTK */
9245 #endif /* not USE_X_TOOLKIT */
9248 XFlush (FRAME_X_DISPLAY (f));
9250 /* Synchronize to ensure Emacs knows the frame is visible
9251 before we do anything else. We do this loop with input not blocked
9252 so that incoming events are handled. */
9254 Lisp_Object frame;
9255 int count;
9256 /* This must be before UNBLOCK_INPUT
9257 since events that arrive in response to the actions above
9258 will set it when they are handled. */
9259 int previously_visible = f->output_data.x->has_been_visible;
9261 original_left = f->left_pos;
9262 original_top = f->top_pos;
9264 /* This must come after we set COUNT. */
9265 UNBLOCK_INPUT;
9267 /* We unblock here so that arriving X events are processed. */
9269 /* Now move the window back to where it was "supposed to be".
9270 But don't do it if the gravity is negative.
9271 When the gravity is negative, this uses a position
9272 that is 3 pixels too low. Perhaps that's really the border width.
9274 Don't do this if the window has never been visible before,
9275 because the window manager may choose the position
9276 and we don't want to override it. */
9278 if (! FRAME_VISIBLE_P (f)
9279 && ! FRAME_ICONIFIED_P (f)
9280 && ! FRAME_X_EMBEDDED_P (f)
9281 && f->win_gravity == NorthWestGravity
9282 && previously_visible)
9284 Drawable rootw;
9285 int x, y;
9286 unsigned int width, height, border, depth;
9288 BLOCK_INPUT;
9290 /* On some window managers (such as FVWM) moving an existing
9291 window, even to the same place, causes the window manager
9292 to introduce an offset. This can cause the window to move
9293 to an unexpected location. Check the geometry (a little
9294 slow here) and then verify that the window is in the right
9295 place. If the window is not in the right place, move it
9296 there, and take the potential window manager hit. */
9297 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9298 &rootw, &x, &y, &width, &height, &border, &depth);
9300 if (original_left != x || original_top != y)
9301 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9302 original_left, original_top);
9304 UNBLOCK_INPUT;
9307 XSETFRAME (frame, f);
9309 /* Wait until the frame is visible. Process X events until a
9310 MapNotify event has been seen, or until we think we won't get a
9311 MapNotify at all.. */
9312 for (count = input_signal_count + 10;
9313 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9315 /* Force processing of queued events. */
9316 x_sync (f);
9318 /* Machines that do polling rather than SIGIO have been
9319 observed to go into a busy-wait here. So we'll fake an
9320 alarm signal to let the handler know that there's something
9321 to be read. We used to raise a real alarm, but it seems
9322 that the handler isn't always enabled here. This is
9323 probably a bug. */
9324 if (input_polling_used ())
9326 /* It could be confusing if a real alarm arrives while
9327 processing the fake one. Turn it off and let the
9328 handler reset it. */
9329 extern void poll_for_input_1 P_ ((void));
9330 int old_poll_suppress_count = poll_suppress_count;
9331 poll_suppress_count = 1;
9332 poll_for_input_1 ();
9333 poll_suppress_count = old_poll_suppress_count;
9336 /* See if a MapNotify event has been processed. */
9337 FRAME_SAMPLE_VISIBILITY (f);
9340 /* 2000-09-28: In
9342 (let ((f (selected-frame)))
9343 (iconify-frame f)
9344 (raise-frame f))
9346 the frame is not raised with various window managers on
9347 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9348 unknown reason, the call to XtMapWidget is completely ignored.
9349 Mapping the widget a second time works. */
9351 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9352 goto retry;
9356 /* Change from mapped state to withdrawn state. */
9358 /* Make the frame visible (mapped and not iconified). */
9360 void
9361 x_make_frame_invisible (f)
9362 struct frame *f;
9364 Window window;
9366 /* Use the frame's outermost window, not the one we normally draw on. */
9367 window = FRAME_OUTER_WINDOW (f);
9369 /* Don't keep the highlight on an invisible frame. */
9370 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9371 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9373 BLOCK_INPUT;
9375 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9376 that the current position of the window is user-specified, rather than
9377 program-specified, so that when the window is mapped again, it will be
9378 placed at the same location, without forcing the user to position it
9379 by hand again (they have already done that once for this window.) */
9380 x_wm_set_size_hint (f, (long) 0, 1);
9382 #ifdef USE_GTK
9383 if (FRAME_GTK_OUTER_WIDGET (f))
9384 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9385 else
9386 #else
9387 if (FRAME_X_EMBEDDED_P (f))
9388 xembed_set_info (f, 0);
9389 else
9390 #endif
9393 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9394 DefaultScreen (FRAME_X_DISPLAY (f))))
9396 UNBLOCK_INPUT_RESIGNAL;
9397 error ("Can't notify window manager of window withdrawal");
9401 /* We can't distinguish this from iconification
9402 just by the event that we get from the server.
9403 So we can't win using the usual strategy of letting
9404 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9405 and synchronize with the server to make sure we agree. */
9406 f->visible = 0;
9407 FRAME_ICONIFIED_P (f) = 0;
9408 f->async_visible = 0;
9409 f->async_iconified = 0;
9411 x_sync (f);
9413 UNBLOCK_INPUT;
9416 /* Change window state from mapped to iconified. */
9418 void
9419 x_iconify_frame (f)
9420 struct frame *f;
9422 int result;
9423 Lisp_Object type;
9425 /* Don't keep the highlight on an invisible frame. */
9426 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9427 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9429 if (f->async_iconified)
9430 return;
9432 BLOCK_INPUT;
9434 FRAME_SAMPLE_VISIBILITY (f);
9436 type = x_icon_type (f);
9437 if (!NILP (type))
9438 x_bitmap_icon (f, type);
9440 #ifdef USE_GTK
9441 if (FRAME_GTK_OUTER_WIDGET (f))
9443 if (! FRAME_VISIBLE_P (f))
9444 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9446 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9447 f->iconified = 1;
9448 f->visible = 1;
9449 f->async_iconified = 1;
9450 f->async_visible = 0;
9451 UNBLOCK_INPUT;
9452 return;
9454 #endif
9456 #ifdef USE_X_TOOLKIT
9458 if (! FRAME_VISIBLE_P (f))
9460 if (! EQ (Vx_no_window_manager, Qt))
9461 x_wm_set_window_state (f, IconicState);
9462 /* This was XtPopup, but that did nothing for an iconified frame. */
9463 XtMapWidget (f->output_data.x->widget);
9464 /* The server won't give us any event to indicate
9465 that an invisible frame was changed to an icon,
9466 so we have to record it here. */
9467 f->iconified = 1;
9468 f->visible = 1;
9469 f->async_iconified = 1;
9470 f->async_visible = 0;
9471 UNBLOCK_INPUT;
9472 return;
9475 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9476 XtWindow (f->output_data.x->widget),
9477 DefaultScreen (FRAME_X_DISPLAY (f)));
9478 UNBLOCK_INPUT;
9480 if (!result)
9481 error ("Can't notify window manager of iconification");
9483 f->async_iconified = 1;
9484 f->async_visible = 0;
9487 BLOCK_INPUT;
9488 XFlush (FRAME_X_DISPLAY (f));
9489 UNBLOCK_INPUT;
9490 #else /* not USE_X_TOOLKIT */
9492 /* Make sure the X server knows where the window should be positioned,
9493 in case the user deiconifies with the window manager. */
9494 if (! FRAME_VISIBLE_P (f)
9495 && ! FRAME_ICONIFIED_P (f)
9496 && ! FRAME_X_EMBEDDED_P (f))
9497 x_set_offset (f, f->left_pos, f->top_pos, 0);
9499 /* Since we don't know which revision of X we're running, we'll use both
9500 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9502 /* X11R4: send a ClientMessage to the window manager using the
9503 WM_CHANGE_STATE type. */
9505 XEvent message;
9507 message.xclient.window = FRAME_X_WINDOW (f);
9508 message.xclient.type = ClientMessage;
9509 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9510 message.xclient.format = 32;
9511 message.xclient.data.l[0] = IconicState;
9513 if (! XSendEvent (FRAME_X_DISPLAY (f),
9514 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9515 False,
9516 SubstructureRedirectMask | SubstructureNotifyMask,
9517 &message))
9519 UNBLOCK_INPUT_RESIGNAL;
9520 error ("Can't notify window manager of iconification");
9524 /* X11R3: set the initial_state field of the window manager hints to
9525 IconicState. */
9526 x_wm_set_window_state (f, IconicState);
9528 if (!FRAME_VISIBLE_P (f))
9530 /* If the frame was withdrawn, before, we must map it. */
9531 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9534 f->async_iconified = 1;
9535 f->async_visible = 0;
9537 XFlush (FRAME_X_DISPLAY (f));
9538 UNBLOCK_INPUT;
9539 #endif /* not USE_X_TOOLKIT */
9543 /* Free X resources of frame F. */
9545 void
9546 x_free_frame_resources (f)
9547 struct frame *f;
9549 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9550 Lisp_Object bar;
9551 struct scroll_bar *b;
9553 BLOCK_INPUT;
9555 /* If a display connection is dead, don't try sending more
9556 commands to the X server. */
9557 if (dpyinfo->display)
9559 /* We must free faces before destroying windows because some
9560 font-driver (e.g. xft) access a window while finishing a
9561 face. */
9562 if (FRAME_FACE_CACHE (f))
9563 free_frame_faces (f);
9565 if (f->output_data.x->icon_desc)
9566 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9568 #ifdef USE_X_TOOLKIT
9569 /* Explicitly destroy the scroll bars of the frame. Without
9570 this, we get "BadDrawable" errors from the toolkit later on,
9571 presumably from expose events generated for the disappearing
9572 toolkit scroll bars. */
9573 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9575 b = XSCROLL_BAR (bar);
9576 x_scroll_bar_remove (b);
9578 #endif
9580 #ifdef HAVE_X_I18N
9581 if (FRAME_XIC (f))
9582 free_frame_xic (f);
9583 #endif
9585 #ifdef USE_X_TOOLKIT
9586 if (f->output_data.x->widget)
9588 XtDestroyWidget (f->output_data.x->widget);
9589 f->output_data.x->widget = NULL;
9591 /* Tooltips don't have widgets, only a simple X window, even if
9592 we are using a toolkit. */
9593 else if (FRAME_X_WINDOW (f))
9594 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9596 free_frame_menubar (f);
9597 #else /* !USE_X_TOOLKIT */
9599 #ifdef USE_GTK
9600 /* In the GTK version, tooltips are normal X
9601 frames. We must check and free both types. */
9602 if (FRAME_GTK_OUTER_WIDGET (f))
9604 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9605 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9606 FRAME_GTK_OUTER_WIDGET (f) = 0;
9608 #endif /* USE_GTK */
9610 if (FRAME_X_WINDOW (f))
9611 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9612 #endif /* !USE_X_TOOLKIT */
9614 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9615 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9616 unload_color (f, f->output_data.x->cursor_pixel);
9617 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9618 unload_color (f, f->output_data.x->border_pixel);
9619 unload_color (f, f->output_data.x->mouse_pixel);
9621 if (f->output_data.x->scroll_bar_background_pixel != -1)
9622 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9623 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9624 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9625 #ifdef USE_TOOLKIT_SCROLL_BARS
9626 /* Scrollbar shadow colors. */
9627 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9628 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9629 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9630 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9631 #endif /* USE_TOOLKIT_SCROLL_BARS */
9632 if (f->output_data.x->white_relief.allocated_p)
9633 unload_color (f, f->output_data.x->white_relief.pixel);
9634 if (f->output_data.x->black_relief.allocated_p)
9635 unload_color (f, f->output_data.x->black_relief.pixel);
9637 x_free_gcs (f);
9638 XFlush (FRAME_X_DISPLAY (f));
9641 xfree (f->output_data.x->saved_menu_event);
9642 xfree (f->output_data.x);
9643 f->output_data.x = NULL;
9645 if (f == dpyinfo->x_focus_frame)
9646 dpyinfo->x_focus_frame = 0;
9647 if (f == dpyinfo->x_focus_event_frame)
9648 dpyinfo->x_focus_event_frame = 0;
9649 if (f == dpyinfo->x_highlight_frame)
9650 dpyinfo->x_highlight_frame = 0;
9652 if (f == dpyinfo->mouse_face_mouse_frame)
9654 dpyinfo->mouse_face_beg_row
9655 = dpyinfo->mouse_face_beg_col = -1;
9656 dpyinfo->mouse_face_end_row
9657 = dpyinfo->mouse_face_end_col = -1;
9658 dpyinfo->mouse_face_window = Qnil;
9659 dpyinfo->mouse_face_deferred_gc = 0;
9660 dpyinfo->mouse_face_mouse_frame = 0;
9663 UNBLOCK_INPUT;
9667 /* Destroy the X window of frame F. */
9669 void
9670 x_destroy_window (f)
9671 struct frame *f;
9673 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9675 /* If a display connection is dead, don't try sending more
9676 commands to the X server. */
9677 if (dpyinfo->display != 0)
9678 x_free_frame_resources (f);
9680 dpyinfo->reference_count--;
9684 /* Setting window manager hints. */
9686 /* Set the normal size hints for the window manager, for frame F.
9687 FLAGS is the flags word to use--or 0 meaning preserve the flags
9688 that the window now has.
9689 If USER_POSITION is nonzero, we set the USPosition
9690 flag (this is useful when FLAGS is 0).
9691 The GTK version is in gtkutils.c */
9693 #ifndef USE_GTK
9694 void
9695 x_wm_set_size_hint (f, flags, user_position)
9696 struct frame *f;
9697 long flags;
9698 int user_position;
9700 XSizeHints size_hints;
9701 Window window = FRAME_OUTER_WINDOW (f);
9703 /* Setting PMaxSize caused various problems. */
9704 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9706 size_hints.x = f->left_pos;
9707 size_hints.y = f->top_pos;
9709 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9710 size_hints.width = FRAME_PIXEL_WIDTH (f);
9712 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9713 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9714 size_hints.max_width = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
9715 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9716 size_hints.max_height = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
9717 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9719 /* Calculate the base and minimum sizes. */
9721 int base_width, base_height;
9722 int min_rows = 0, min_cols = 0;
9724 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9725 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9727 check_frame_size (f, &min_rows, &min_cols);
9729 /* The window manager uses the base width hints to calculate the
9730 current number of rows and columns in the frame while
9731 resizing; min_width and min_height aren't useful for this
9732 purpose, since they might not give the dimensions for a
9733 zero-row, zero-column frame.
9735 We use the base_width and base_height members if we have
9736 them; otherwise, we set the min_width and min_height members
9737 to the size for a zero x zero frame. */
9739 size_hints.flags |= PBaseSize;
9740 size_hints.base_width = base_width;
9741 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
9742 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9743 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9746 /* If we don't need the old flags, we don't need the old hint at all. */
9747 if (flags)
9749 size_hints.flags |= flags;
9750 goto no_read;
9754 XSizeHints hints; /* Sometimes I hate X Windows... */
9755 long supplied_return;
9756 int value;
9758 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9759 &supplied_return);
9761 if (flags)
9762 size_hints.flags |= flags;
9763 else
9765 if (value == 0)
9766 hints.flags = 0;
9767 if (hints.flags & PSize)
9768 size_hints.flags |= PSize;
9769 if (hints.flags & PPosition)
9770 size_hints.flags |= PPosition;
9771 if (hints.flags & USPosition)
9772 size_hints.flags |= USPosition;
9773 if (hints.flags & USSize)
9774 size_hints.flags |= USSize;
9778 no_read:
9780 #ifdef PWinGravity
9781 size_hints.win_gravity = f->win_gravity;
9782 size_hints.flags |= PWinGravity;
9784 if (user_position)
9786 size_hints.flags &= ~ PPosition;
9787 size_hints.flags |= USPosition;
9789 #endif /* PWinGravity */
9791 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9793 #endif /* not USE_GTK */
9795 /* Used for IconicState or NormalState */
9797 void
9798 x_wm_set_window_state (f, state)
9799 struct frame *f;
9800 int state;
9802 #ifdef USE_X_TOOLKIT
9803 Arg al[1];
9805 XtSetArg (al[0], XtNinitialState, state);
9806 XtSetValues (f->output_data.x->widget, al, 1);
9807 #else /* not USE_X_TOOLKIT */
9808 Window window = FRAME_X_WINDOW (f);
9810 f->output_data.x->wm_hints.flags |= StateHint;
9811 f->output_data.x->wm_hints.initial_state = state;
9813 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9814 #endif /* not USE_X_TOOLKIT */
9817 void
9818 x_wm_set_icon_pixmap (f, pixmap_id)
9819 struct frame *f;
9820 int pixmap_id;
9822 Pixmap icon_pixmap, icon_mask;
9824 #if !defined USE_X_TOOLKIT && !defined USE_GTK
9825 Window window = FRAME_OUTER_WINDOW (f);
9826 #endif
9828 if (pixmap_id > 0)
9830 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9831 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9832 icon_mask = x_bitmap_mask (f, pixmap_id);
9833 f->output_data.x->wm_hints.icon_mask = icon_mask;
9835 else
9837 /* It seems there is no way to turn off use of an icon
9838 pixmap. */
9839 return;
9843 #ifdef USE_GTK
9845 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9846 return;
9849 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9852 Arg al[1];
9853 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9854 XtSetValues (f->output_data.x->widget, al, 1);
9855 XtSetArg (al[0], XtNiconMask, icon_mask);
9856 XtSetValues (f->output_data.x->widget, al, 1);
9859 #else /* not USE_X_TOOLKIT && not USE_GTK */
9861 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9862 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9864 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9867 void
9868 x_wm_set_icon_position (f, icon_x, icon_y)
9869 struct frame *f;
9870 int icon_x, icon_y;
9872 Window window = FRAME_OUTER_WINDOW (f);
9874 f->output_data.x->wm_hints.flags |= IconPositionHint;
9875 f->output_data.x->wm_hints.icon_x = icon_x;
9876 f->output_data.x->wm_hints.icon_y = icon_y;
9878 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9882 /***********************************************************************
9883 Fonts
9884 ***********************************************************************/
9886 #if GLYPH_DEBUG
9888 /* Check that FONT is valid on frame F. It is if it can be found in F's
9889 font table. */
9891 static void
9892 x_check_font (f, font)
9893 struct frame *f;
9894 struct font *font;
9896 Lisp_Object frame;
9898 xassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9899 if (font->driver->check)
9900 xassert (font->driver->check (f, font) == 0);
9903 #endif /* GLYPH_DEBUG != 0 */
9906 /***********************************************************************
9907 Initialization
9908 ***********************************************************************/
9910 #ifdef USE_X_TOOLKIT
9911 static XrmOptionDescRec emacs_options[] = {
9912 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9913 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9915 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9916 XrmoptionSepArg, NULL},
9917 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9919 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9920 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9921 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9922 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9923 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9924 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9925 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9928 /* Whether atimer for Xt timeouts is activated or not. */
9930 static int x_timeout_atimer_activated_flag;
9932 #endif /* USE_X_TOOLKIT */
9934 static int x_initialized;
9936 #ifdef HAVE_X_SM
9937 static int x_session_initialized;
9938 #endif
9940 /* Test whether two display-name strings agree up to the dot that separates
9941 the screen number from the server number. */
9942 static int
9943 same_x_server (name1, name2)
9944 const char *name1, *name2;
9946 int seen_colon = 0;
9947 const unsigned char *system_name = SDATA (Vsystem_name);
9948 int system_name_length = strlen (system_name);
9949 int length_until_period = 0;
9951 while (system_name[length_until_period] != 0
9952 && system_name[length_until_period] != '.')
9953 length_until_period++;
9955 /* Treat `unix' like an empty host name. */
9956 if (! strncmp (name1, "unix:", 5))
9957 name1 += 4;
9958 if (! strncmp (name2, "unix:", 5))
9959 name2 += 4;
9960 /* Treat this host's name like an empty host name. */
9961 if (! strncmp (name1, system_name, system_name_length)
9962 && name1[system_name_length] == ':')
9963 name1 += system_name_length;
9964 if (! strncmp (name2, system_name, system_name_length)
9965 && name2[system_name_length] == ':')
9966 name2 += system_name_length;
9967 /* Treat this host's domainless name like an empty host name. */
9968 if (! strncmp (name1, system_name, length_until_period)
9969 && name1[length_until_period] == ':')
9970 name1 += length_until_period;
9971 if (! strncmp (name2, system_name, length_until_period)
9972 && name2[length_until_period] == ':')
9973 name2 += length_until_period;
9975 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
9977 if (*name1 == ':')
9978 seen_colon++;
9979 if (seen_colon && *name1 == '.')
9980 return 1;
9982 return (seen_colon
9983 && (*name1 == '.' || *name1 == '\0')
9984 && (*name2 == '.' || *name2 == '\0'));
9987 /* Count number of set bits in mask and number of bits to shift to
9988 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
9989 to 5. */
9990 static void
9991 get_bits_and_offset (mask, bits, offset)
9992 unsigned long mask;
9993 int *bits;
9994 int *offset;
9996 int nr = 0;
9997 int off = 0;
9999 while (!(mask & 1))
10001 off++;
10002 mask >>= 1;
10005 while (mask & 1)
10007 nr++;
10008 mask >>= 1;
10011 *offset = off;
10012 *bits = nr;
10015 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
10016 But don't permanently open it, just test its availability. */
10019 x_display_ok (display)
10020 const char *display;
10022 int dpy_ok = 1;
10023 Display *dpy;
10025 dpy = XOpenDisplay (display);
10026 if (dpy)
10027 XCloseDisplay (dpy);
10028 else
10029 dpy_ok = 0;
10030 return dpy_ok;
10033 #ifdef USE_GTK
10034 static void
10035 my_log_handler (log_domain, log_level, message, user_data)
10036 const gchar *log_domain;
10037 GLogLevelFlags log_level;
10038 const gchar *message;
10039 gpointer user_data;
10041 if (!strstr (message, "g_set_prgname"))
10042 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, message);
10044 #endif
10046 /* Open a connection to X display DISPLAY_NAME, and return
10047 the structure that describes the open display.
10048 If we cannot contact the display, return null. */
10050 struct x_display_info *
10051 x_term_init (display_name, xrm_option, resource_name)
10052 Lisp_Object display_name;
10053 char *xrm_option;
10054 char *resource_name;
10056 int connection;
10057 Display *dpy;
10058 struct terminal *terminal;
10059 struct x_display_info *dpyinfo;
10060 XrmDatabase xrdb;
10062 BLOCK_INPUT;
10064 if (!x_initialized)
10066 x_initialize ();
10067 ++x_initialized;
10070 if (! x_display_ok (SDATA (display_name)))
10071 error ("Display %s can't be opened", SDATA (display_name));
10073 #ifdef USE_GTK
10075 #define NUM_ARGV 10
10076 int argc;
10077 char *argv[NUM_ARGV];
10078 char **argv2 = argv;
10079 guint id;
10080 #ifndef HAVE_GTK_MULTIDISPLAY
10081 if (!EQ (Vinitial_window_system, Qx))
10082 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10083 #endif
10085 if (x_initialized++ > 1)
10087 #ifdef HAVE_GTK_MULTIDISPLAY
10088 /* Opening another display. If xg_display_open returns less
10089 than zero, we are probably on GTK 2.0, which can only handle
10090 one display. GTK 2.2 or later can handle more than one. */
10091 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10092 #endif
10093 error ("Sorry, this version of GTK can only handle one display");
10095 else
10097 for (argc = 0; argc < NUM_ARGV; ++argc)
10098 argv[argc] = 0;
10100 argc = 0;
10101 argv[argc++] = initial_argv[0];
10103 if (! NILP (display_name))
10105 argv[argc++] = "--display";
10106 argv[argc++] = SDATA (display_name);
10109 argv[argc++] = "--name";
10110 argv[argc++] = resource_name;
10112 XSetLocaleModifiers ("");
10114 /* Work around GLib bug that outputs a faulty warning. See
10115 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
10116 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
10117 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
10118 gtk_init (&argc, &argv2);
10119 g_log_remove_handler ("GLib", id);
10121 /* gtk_init does set_locale. We must fix locale after calling it. */
10122 fixup_locale ();
10123 xg_initialize ();
10125 dpy = GDK_DISPLAY ();
10127 /* NULL window -> events for all windows go to our function */
10128 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10130 /* Load our own gtkrc if it exists. */
10132 char *file = "~/.emacs.d/gtkrc";
10133 Lisp_Object s, abs_file;
10135 s = make_string (file, strlen (file));
10136 abs_file = Fexpand_file_name (s, Qnil);
10138 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10139 gtk_rc_parse (SDATA (abs_file));
10142 XSetErrorHandler (x_error_handler);
10143 XSetIOErrorHandler (x_io_error_quitter);
10146 #else /* not USE_GTK */
10147 #ifdef USE_X_TOOLKIT
10148 /* weiner@footloose.sps.mot.com reports that this causes
10149 errors with X11R5:
10150 X protocol error: BadAtom (invalid Atom parameter)
10151 on protocol request 18skiloaf.
10152 So let's not use it until R6. */
10153 #ifdef HAVE_X11XTR6
10154 XtSetLanguageProc (NULL, NULL, NULL);
10155 #endif
10158 int argc = 0;
10159 char *argv[3];
10161 argv[0] = "";
10162 argc = 1;
10163 if (xrm_option)
10165 argv[argc++] = "-xrm";
10166 argv[argc++] = xrm_option;
10168 turn_on_atimers (0);
10169 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10170 resource_name, EMACS_CLASS,
10171 emacs_options, XtNumber (emacs_options),
10172 &argc, argv);
10173 turn_on_atimers (1);
10175 #ifdef HAVE_X11XTR6
10176 /* I think this is to compensate for XtSetLanguageProc. */
10177 fixup_locale ();
10178 #endif
10181 #else /* not USE_X_TOOLKIT */
10182 XSetLocaleModifiers ("");
10183 dpy = XOpenDisplay (SDATA (display_name));
10184 #endif /* not USE_X_TOOLKIT */
10185 #endif /* not USE_GTK*/
10187 /* Detect failure. */
10188 if (dpy == 0)
10190 UNBLOCK_INPUT;
10191 return 0;
10194 /* We have definitely succeeded. Record the new connection. */
10196 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10197 bzero (dpyinfo, sizeof *dpyinfo);
10199 terminal = x_create_terminal (dpyinfo);
10202 struct x_display_info *share;
10203 Lisp_Object tail;
10205 for (share = x_display_list, tail = x_display_name_list; share;
10206 share = share->next, tail = XCDR (tail))
10207 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10208 SDATA (display_name)))
10209 break;
10210 if (share)
10211 terminal->kboard = share->terminal->kboard;
10212 else
10214 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10215 init_kboard (terminal->kboard);
10216 terminal->kboard->Vwindow_system = Qx;
10218 /* Add the keyboard to the list before running Lisp code (via
10219 Qvendor_specific_keysyms below), since these are not traced
10220 via terminals but only through all_kboards. */
10221 terminal->kboard->next_kboard = all_kboards;
10222 all_kboards = terminal->kboard;
10224 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10226 char *vendor = ServerVendor (dpy);
10228 /* Protect terminal from GC before removing it from the
10229 list of terminals. */
10230 struct gcpro gcpro1;
10231 Lisp_Object gcpro_term;
10232 XSETTERMINAL (gcpro_term, terminal);
10233 GCPRO1 (gcpro_term);
10235 /* Temporarily hide the partially initialized terminal. */
10236 terminal_list = terminal->next_terminal;
10237 UNBLOCK_INPUT;
10238 terminal->kboard->Vsystem_key_alist
10239 = call1 (Qvendor_specific_keysyms,
10240 vendor ? build_string (vendor) : empty_unibyte_string);
10241 BLOCK_INPUT;
10242 terminal->next_terminal = terminal_list;
10243 terminal_list = terminal;
10244 UNGCPRO;
10247 /* Don't let the initial kboard remain current longer than necessary.
10248 That would cause problems if a file loaded on startup tries to
10249 prompt in the mini-buffer. */
10250 if (current_kboard == initial_kboard)
10251 current_kboard = terminal->kboard;
10253 terminal->kboard->reference_count++;
10256 /* Put this display on the chain. */
10257 dpyinfo->next = x_display_list;
10258 x_display_list = dpyinfo;
10260 /* Put it on x_display_name_list as well, to keep them parallel. */
10261 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10262 x_display_name_list);
10263 dpyinfo->name_list_element = XCAR (x_display_name_list);
10265 dpyinfo->display = dpy;
10267 /* Set the name of the terminal. */
10268 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10269 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10270 terminal->name[SBYTES (display_name)] = 0;
10272 #if 0
10273 XSetAfterFunction (x_current_display, x_trace_wire);
10274 #endif /* ! 0 */
10276 dpyinfo->x_id_name
10277 = (char *) xmalloc (SBYTES (Vinvocation_name)
10278 + SBYTES (Vsystem_name)
10279 + 2);
10280 sprintf (dpyinfo->x_id_name, "%s@%s",
10281 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10283 /* Figure out which modifier bits mean what. */
10284 x_find_modifier_meanings (dpyinfo);
10286 /* Get the scroll bar cursor. */
10287 #ifdef USE_GTK
10288 /* We must create a GTK cursor, it is required for GTK widgets. */
10289 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10290 #endif /* USE_GTK */
10292 dpyinfo->vertical_scroll_bar_cursor
10293 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10295 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10296 resource_name, EMACS_CLASS);
10297 #ifdef HAVE_XRMSETDATABASE
10298 XrmSetDatabase (dpyinfo->display, xrdb);
10299 #else
10300 dpyinfo->display->db = xrdb;
10301 #endif
10302 /* Put the rdb where we can find it in a way that works on
10303 all versions. */
10304 dpyinfo->xrdb = xrdb;
10306 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10307 DefaultScreen (dpyinfo->display));
10308 select_visual (dpyinfo);
10309 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10310 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10311 dpyinfo->client_leader_window = 0;
10312 dpyinfo->grabbed = 0;
10313 dpyinfo->reference_count = 0;
10314 dpyinfo->icon_bitmap_id = -1;
10315 dpyinfo->n_fonts = 0;
10316 dpyinfo->bitmaps = 0;
10317 dpyinfo->bitmaps_size = 0;
10318 dpyinfo->bitmaps_last = 0;
10319 dpyinfo->scratch_cursor_gc = 0;
10320 dpyinfo->mouse_face_mouse_frame = 0;
10321 dpyinfo->mouse_face_deferred_gc = 0;
10322 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10323 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10324 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10325 dpyinfo->mouse_face_window = Qnil;
10326 dpyinfo->mouse_face_overlay = Qnil;
10327 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10328 dpyinfo->mouse_face_defer = 0;
10329 dpyinfo->mouse_face_hidden = 0;
10330 dpyinfo->x_focus_frame = 0;
10331 dpyinfo->x_focus_event_frame = 0;
10332 dpyinfo->x_highlight_frame = 0;
10333 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10335 /* See if we can construct pixel values from RGB values. */
10336 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10337 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10339 if (dpyinfo->visual->class == TrueColor)
10341 get_bits_and_offset (dpyinfo->visual->red_mask,
10342 &dpyinfo->red_bits, &dpyinfo->red_offset);
10343 get_bits_and_offset (dpyinfo->visual->blue_mask,
10344 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10345 get_bits_and_offset (dpyinfo->visual->green_mask,
10346 &dpyinfo->green_bits, &dpyinfo->green_offset);
10349 /* See if a private colormap is requested. */
10350 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10352 if (dpyinfo->visual->class == PseudoColor)
10354 Lisp_Object value;
10355 value = display_x_get_resource (dpyinfo,
10356 build_string ("privateColormap"),
10357 build_string ("PrivateColormap"),
10358 Qnil, Qnil);
10359 if (STRINGP (value)
10360 && (!strcmp (SDATA (value), "true")
10361 || !strcmp (SDATA (value), "on")))
10362 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10365 else
10366 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10367 dpyinfo->visual, AllocNone);
10369 #ifdef HAVE_XFT
10371 /* If we are using Xft, check dpi value in X resources.
10372 It is better we use it as well, since Xft will use it, as will all
10373 Gnome applications. If our real DPI is smaller or larger than the
10374 one Xft uses, our font will look smaller or larger than other
10375 for other applications, even if it is the same font name (monospace-10
10376 for example). */
10377 char *v = XGetDefault (dpyinfo->display, "Xft", "dpi");
10378 double d;
10379 if (v != NULL && sscanf (v, "%lf", &d) == 1)
10380 dpyinfo->resy = dpyinfo->resx = d;
10382 #endif
10384 if (dpyinfo->resy < 1)
10386 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10387 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10388 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10389 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10390 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10391 pixels = DisplayWidth (dpyinfo->display, screen_number);
10392 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10393 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10394 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10397 dpyinfo->Xatom_wm_protocols
10398 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10399 dpyinfo->Xatom_wm_take_focus
10400 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10401 dpyinfo->Xatom_wm_save_yourself
10402 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10403 dpyinfo->Xatom_wm_delete_window
10404 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10405 dpyinfo->Xatom_wm_change_state
10406 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10407 dpyinfo->Xatom_wm_configure_denied
10408 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10409 dpyinfo->Xatom_wm_window_moved
10410 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10411 dpyinfo->Xatom_wm_client_leader
10412 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10413 dpyinfo->Xatom_editres
10414 = XInternAtom (dpyinfo->display, "Editres", False);
10415 dpyinfo->Xatom_CLIPBOARD
10416 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10417 dpyinfo->Xatom_TIMESTAMP
10418 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10419 dpyinfo->Xatom_TEXT
10420 = XInternAtom (dpyinfo->display, "TEXT", False);
10421 dpyinfo->Xatom_COMPOUND_TEXT
10422 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10423 dpyinfo->Xatom_UTF8_STRING
10424 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10425 dpyinfo->Xatom_DELETE
10426 = XInternAtom (dpyinfo->display, "DELETE", False);
10427 dpyinfo->Xatom_MULTIPLE
10428 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10429 dpyinfo->Xatom_INCR
10430 = XInternAtom (dpyinfo->display, "INCR", False);
10431 dpyinfo->Xatom_EMACS_TMP
10432 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10433 dpyinfo->Xatom_TARGETS
10434 = XInternAtom (dpyinfo->display, "TARGETS", False);
10435 dpyinfo->Xatom_NULL
10436 = XInternAtom (dpyinfo->display, "NULL", False);
10437 dpyinfo->Xatom_ATOM_PAIR
10438 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10439 /* For properties of font. */
10440 dpyinfo->Xatom_PIXEL_SIZE
10441 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10442 dpyinfo->Xatom_AVERAGE_WIDTH
10443 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10444 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10445 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10446 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10447 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10448 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10449 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10451 /* Ghostscript support. */
10452 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10453 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10455 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10456 False);
10458 dpyinfo->Xatom_XEMBED = XInternAtom (dpyinfo->display, "_XEMBED",
10459 False);
10461 dpyinfo->Xatom_net_wm_state
10462 = XInternAtom (dpyinfo->display, "_NET_WM_STATE", False);
10463 dpyinfo->Xatom_net_wm_state_fullscreen_atom
10464 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_FULLSCREEN", False);
10465 dpyinfo->Xatom_net_wm_state_maximized_horz
10466 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
10467 dpyinfo->Xatom_net_wm_state_maximized_vert
10468 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
10469 dpyinfo->Xatom_net_wm_state_sticky
10470 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_STICKY", False);
10471 dpyinfo->Xatom_net_window_type
10472 = XInternAtom (dpyinfo->display, "_NET_WM_WINDOW_TYPE", False);
10473 dpyinfo->Xatom_net_window_type_tooltip
10474 = XInternAtom (dpyinfo->display, "_NET_WM_WINDOW_TYPE_TOOLTIP", False);
10476 dpyinfo->cut_buffers_initialized = 0;
10478 dpyinfo->x_dnd_atoms_size = 8;
10479 dpyinfo->x_dnd_atoms_length = 0;
10480 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10481 * dpyinfo->x_dnd_atoms_size);
10483 dpyinfo->net_supported_atoms = NULL;
10484 dpyinfo->nr_net_supported_atoms = 0;
10485 dpyinfo->net_supported_window = 0;
10487 connection = ConnectionNumber (dpyinfo->display);
10488 dpyinfo->connection = connection;
10491 extern int gray_bitmap_width, gray_bitmap_height;
10492 extern char *gray_bitmap_bits;
10493 dpyinfo->gray
10494 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10495 gray_bitmap_bits,
10496 gray_bitmap_width, gray_bitmap_height,
10497 (unsigned long) 1, (unsigned long) 0, 1);
10500 #ifdef HAVE_X_I18N
10501 xim_initialize (dpyinfo, resource_name);
10502 #endif
10504 xsettings_initialize (dpyinfo);
10506 #ifdef subprocesses
10507 /* This is only needed for distinguishing keyboard and process input. */
10508 if (connection != 0)
10509 add_keyboard_wait_descriptor (connection);
10510 #endif
10512 #ifdef F_SETOWN
10513 fcntl (connection, F_SETOWN, getpid ());
10514 #endif /* ! defined (F_SETOWN) */
10516 #ifdef SIGIO
10517 if (interrupt_input)
10518 init_sigio (connection);
10519 #endif /* ! defined (SIGIO) */
10521 #ifdef USE_LUCID
10523 Display *dpy = dpyinfo->display;
10524 XrmValue d, fr, to;
10525 Font font;
10527 d.addr = (XPointer)&dpy;
10528 d.size = sizeof (Display *);
10529 fr.addr = XtDefaultFont;
10530 fr.size = sizeof (XtDefaultFont);
10531 to.size = sizeof (Font *);
10532 to.addr = (XPointer)&font;
10533 x_catch_errors (dpy);
10534 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10535 abort ();
10536 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10537 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10538 x_uncatch_errors ();
10540 #endif
10542 /* See if we should run in synchronous mode. This is useful
10543 for debugging X code. */
10545 Lisp_Object value;
10546 value = display_x_get_resource (dpyinfo,
10547 build_string ("synchronous"),
10548 build_string ("Synchronous"),
10549 Qnil, Qnil);
10550 if (STRINGP (value)
10551 && (!strcmp (SDATA (value), "true")
10552 || !strcmp (SDATA (value), "on")))
10553 XSynchronize (dpyinfo->display, True);
10557 Lisp_Object value;
10558 value = display_x_get_resource (dpyinfo,
10559 build_string ("useXIM"),
10560 build_string ("UseXIM"),
10561 Qnil, Qnil);
10562 #ifdef USE_XIM
10563 if (STRINGP (value)
10564 && (!strcmp (SDATA (value), "false")
10565 || !strcmp (SDATA (value), "off")))
10566 use_xim = 0;
10567 #else
10568 if (STRINGP (value)
10569 && (!strcmp (SDATA (value), "true")
10570 || !strcmp (SDATA (value), "on")))
10571 use_xim = 1;
10572 #endif
10575 #ifdef HAVE_X_SM
10576 /* Only do this for the very first display in the Emacs session.
10577 Ignore X session management when Emacs was first started on a
10578 tty. */
10579 if (terminal->id == 1)
10580 x_session_initialize (dpyinfo);
10581 #endif
10583 UNBLOCK_INPUT;
10585 return dpyinfo;
10588 /* Get rid of display DPYINFO, deleting all frames on it,
10589 and without sending any more commands to the X server. */
10591 void
10592 x_delete_display (dpyinfo)
10593 struct x_display_info *dpyinfo;
10595 struct terminal *t;
10597 /* Close all frames and delete the generic struct terminal for this
10598 X display. */
10599 for (t = terminal_list; t; t = t->next_terminal)
10600 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10602 #ifdef HAVE_X_SM
10603 /* Close X session management when we close its display. */
10604 if (t->id == 1 && x_session_have_connection ())
10605 x_session_close();
10606 #endif
10607 delete_terminal (t);
10608 break;
10611 delete_keyboard_wait_descriptor (dpyinfo->connection);
10613 /* Discard this display from x_display_name_list and x_display_list.
10614 We can't use Fdelq because that can quit. */
10615 if (! NILP (x_display_name_list)
10616 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10617 x_display_name_list = XCDR (x_display_name_list);
10618 else
10620 Lisp_Object tail;
10622 tail = x_display_name_list;
10623 while (CONSP (tail) && CONSP (XCDR (tail)))
10625 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10627 XSETCDR (tail, XCDR (XCDR (tail)));
10628 break;
10630 tail = XCDR (tail);
10634 if (next_noop_dpyinfo == dpyinfo)
10635 next_noop_dpyinfo = dpyinfo->next;
10637 if (x_display_list == dpyinfo)
10638 x_display_list = dpyinfo->next;
10639 else
10641 struct x_display_info *tail;
10643 for (tail = x_display_list; tail; tail = tail->next)
10644 if (tail->next == dpyinfo)
10645 tail->next = tail->next->next;
10648 xfree (dpyinfo->x_id_name);
10649 xfree (dpyinfo->x_dnd_atoms);
10650 xfree (dpyinfo->color_cells);
10651 xfree (dpyinfo);
10654 #ifdef USE_X_TOOLKIT
10656 /* Atimer callback function for TIMER. Called every 0.1s to process
10657 Xt timeouts, if needed. We must avoid calling XtAppPending as
10658 much as possible because that function does an implicit XFlush
10659 that slows us down. */
10661 static void
10662 x_process_timeouts (timer)
10663 struct atimer *timer;
10665 BLOCK_INPUT;
10666 x_timeout_atimer_activated_flag = 0;
10667 if (toolkit_scroll_bar_interaction || popup_activated ())
10669 while (XtAppPending (Xt_app_con) & XtIMTimer)
10670 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10671 /* Reactivate the atimer for next time. */
10672 x_activate_timeout_atimer ();
10674 UNBLOCK_INPUT;
10677 /* Install an asynchronous timer that processes Xt timeout events
10678 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10679 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10680 function whenever these variables are set. This is necessary
10681 because some widget sets use timeouts internally, for example the
10682 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10683 processed, these widgets don't behave normally. */
10685 void
10686 x_activate_timeout_atimer ()
10688 BLOCK_INPUT;
10689 if (!x_timeout_atimer_activated_flag)
10691 EMACS_TIME interval;
10693 EMACS_SET_SECS_USECS (interval, 0, 100000);
10694 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10695 x_timeout_atimer_activated_flag = 1;
10697 UNBLOCK_INPUT;
10700 #endif /* USE_X_TOOLKIT */
10703 /* Set up use of X before we make the first connection. */
10705 extern frame_parm_handler x_frame_parm_handlers[];
10707 static struct redisplay_interface x_redisplay_interface =
10709 x_frame_parm_handlers,
10710 x_produce_glyphs,
10711 x_write_glyphs,
10712 x_insert_glyphs,
10713 x_clear_end_of_line,
10714 x_scroll_run,
10715 x_after_update_window_line,
10716 x_update_window_begin,
10717 x_update_window_end,
10718 x_cursor_to,
10719 x_flush,
10720 #ifdef XFlush
10721 x_flush,
10722 #else
10723 0, /* flush_display_optional */
10724 #endif
10725 x_clear_window_mouse_face,
10726 x_get_glyph_overhangs,
10727 x_fix_overlapping_area,
10728 x_draw_fringe_bitmap,
10729 0, /* define_fringe_bitmap */
10730 0, /* destroy_fringe_bitmap */
10731 x_compute_glyph_string_overhangs,
10732 x_draw_glyph_string,
10733 x_define_frame_cursor,
10734 x_clear_frame_area,
10735 x_draw_window_cursor,
10736 x_draw_vertical_window_border,
10737 x_shift_glyphs_for_insert
10741 /* This function is called when the last frame on a display is deleted. */
10742 void
10743 x_delete_terminal (struct terminal *terminal)
10745 struct x_display_info *dpyinfo = terminal->display_info.x;
10747 /* Protect against recursive calls. delete_frame in
10748 delete_terminal calls us back when it deletes our last frame. */
10749 if (!terminal->name)
10750 return;
10752 BLOCK_INPUT;
10753 #ifdef HAVE_X_I18N
10754 /* We must close our connection to the XIM server before closing the
10755 X display. */
10756 if (dpyinfo->xim)
10757 xim_close_dpy (dpyinfo);
10758 #endif
10760 /* If called from x_connection_closed, the display may already be closed
10761 and dpyinfo->display was set to 0 to indicate that. */
10762 if (dpyinfo->display)
10764 x_destroy_all_bitmaps (dpyinfo);
10765 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10767 /* Whether or not XCloseDisplay destroys the associated resource
10768 database depends on the version of libX11. To avoid both
10769 crash and memory leak, we dissociate the database from the
10770 display and then destroy dpyinfo->xrdb ourselves.
10772 Unfortunately, the above strategy does not work in some
10773 situations due to a bug in newer versions of libX11: because
10774 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
10775 dpy->db is NULL, XCloseDisplay destroys the associated
10776 database whereas it has not been created by XGetDefault
10777 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
10778 don't destroy the database here in order to avoid the crash
10779 in the above situations for now, though that may cause memory
10780 leaks in other situations. */
10781 #if 0
10782 #ifdef HAVE_XRMSETDATABASE
10783 XrmSetDatabase (dpyinfo->display, NULL);
10784 #else
10785 dpyinfo->display->db = NULL;
10786 #endif
10787 /* We used to call XrmDestroyDatabase from x_delete_display, but
10788 some older versions of libX11 crash if we call it after
10789 closing all the displays. */
10790 XrmDestroyDatabase (dpyinfo->xrdb);
10791 #endif
10793 #ifdef USE_GTK
10794 xg_display_close (dpyinfo->display);
10795 #else
10796 #ifdef USE_X_TOOLKIT
10797 XtCloseDisplay (dpyinfo->display);
10798 #else
10799 XCloseDisplay (dpyinfo->display);
10800 #endif
10801 #endif /* ! USE_GTK */
10804 /* Mark as dead. */
10805 dpyinfo->display = NULL;
10806 x_delete_display (dpyinfo);
10807 UNBLOCK_INPUT;
10810 /* Create a struct terminal, initialize it with the X11 specific
10811 functions and make DISPLAY->TERMINAL point to it. */
10813 static struct terminal *
10814 x_create_terminal (struct x_display_info *dpyinfo)
10816 struct terminal *terminal;
10818 terminal = create_terminal ();
10820 terminal->type = output_x_window;
10821 terminal->display_info.x = dpyinfo;
10822 dpyinfo->terminal = terminal;
10824 /* kboard is initialized in x_term_init. */
10826 terminal->clear_frame_hook = x_clear_frame;
10827 terminal->ins_del_lines_hook = x_ins_del_lines;
10828 terminal->delete_glyphs_hook = x_delete_glyphs;
10829 terminal->ring_bell_hook = XTring_bell;
10830 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
10831 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
10832 terminal->set_terminal_modes_hook = XTset_terminal_modes;
10833 terminal->update_begin_hook = x_update_begin;
10834 terminal->update_end_hook = x_update_end;
10835 terminal->set_terminal_window_hook = XTset_terminal_window;
10836 terminal->read_socket_hook = XTread_socket;
10837 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10838 terminal->mouse_position_hook = XTmouse_position;
10839 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10840 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10841 terminal->fullscreen_hook = XTfullscreen_hook;
10842 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10843 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10844 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10845 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10847 terminal->delete_frame_hook = x_destroy_window;
10848 terminal->delete_terminal_hook = x_delete_terminal;
10850 terminal->rif = &x_redisplay_interface;
10851 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10852 terminal->char_ins_del_ok = 1;
10853 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10854 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10855 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10856 off the bottom. */
10858 return terminal;
10861 void
10862 x_initialize ()
10864 baud_rate = 19200;
10866 x_noop_count = 0;
10867 last_tool_bar_item = -1;
10868 any_help_event_p = 0;
10869 ignore_next_mouse_click_timeout = 0;
10870 #ifdef HAVE_X_SM
10871 x_session_initialized = 0;
10872 #endif
10874 #ifdef USE_GTK
10875 current_count = -1;
10876 #endif
10878 /* Try to use interrupt input; if we can't, then start polling. */
10879 Fset_input_interrupt_mode (Qt);
10881 #ifdef USE_X_TOOLKIT
10882 XtToolkitInitialize ();
10884 Xt_app_con = XtCreateApplicationContext ();
10886 /* Register a converter from strings to pixels, which uses
10887 Emacs' color allocation infrastructure. */
10888 XtAppSetTypeConverter (Xt_app_con,
10889 XtRString, XtRPixel, cvt_string_to_pixel,
10890 cvt_string_to_pixel_args,
10891 XtNumber (cvt_string_to_pixel_args),
10892 XtCacheByDisplay, cvt_pixel_dtor);
10894 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10895 #endif
10897 #ifdef USE_TOOLKIT_SCROLL_BARS
10898 #ifndef USE_GTK
10899 xaw3d_arrow_scroll = False;
10900 xaw3d_pick_top = True;
10901 #endif
10902 #endif
10904 pending_autoraise_frame = 0;
10905 pending_event_wait.f = 0;
10906 pending_event_wait.eventtype = 0;
10908 /* Note that there is no real way portable across R3/R4 to get the
10909 original error handler. */
10910 XSetErrorHandler (x_error_handler);
10911 XSetIOErrorHandler (x_io_error_quitter);
10913 signal (SIGPIPE, x_connection_signal);
10915 xgselect_initialize ();
10919 void
10920 syms_of_xterm ()
10922 x_error_message = NULL;
10924 staticpro (&x_display_name_list);
10925 x_display_name_list = Qnil;
10927 staticpro (&last_mouse_scroll_bar);
10928 last_mouse_scroll_bar = Qnil;
10930 staticpro (&Qvendor_specific_keysyms);
10931 Qvendor_specific_keysyms = intern_c_string ("vendor-specific-keysyms");
10933 staticpro (&Qlatin_1);
10934 Qlatin_1 = intern_c_string ("latin-1");
10936 staticpro (&last_mouse_press_frame);
10937 last_mouse_press_frame = Qnil;
10939 #ifdef USE_GTK
10940 xg_default_icon_file = make_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
10941 staticpro (&xg_default_icon_file);
10943 Qx_gtk_map_stock = intern_c_string ("x-gtk-map-stock");
10944 staticpro (&Qx_gtk_map_stock);
10945 #endif
10947 DEFVAR_BOOL ("x-use-underline-position-properties",
10948 &x_use_underline_position_properties,
10949 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10950 A value of nil means ignore them. If you encounter fonts with bogus
10951 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10952 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10953 to override the font's UNDERLINE_POSITION for small font display
10954 sizes. */);
10955 x_use_underline_position_properties = 1;
10957 DEFVAR_BOOL ("x-underline-at-descent-line",
10958 &x_underline_at_descent_line,
10959 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
10960 A value of nil means to draw the underline according to the value of the
10961 variable `x-use-underline-position-properties', which is usually at the
10962 baseline level. The default value is nil. */);
10963 x_underline_at_descent_line = 0;
10965 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10966 &x_mouse_click_focus_ignore_position,
10967 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10968 This variable is only used when the window manager requires that you
10969 click on a frame to select it (give it focus). In that case, a value
10970 of nil, means that the selected window and cursor position changes to
10971 reflect the mouse click position, while a non-nil value means that the
10972 selected window or cursor position is preserved. */);
10973 x_mouse_click_focus_ignore_position = 0;
10975 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10976 doc: /* What X toolkit scroll bars Emacs uses.
10977 A value of nil means Emacs doesn't use X toolkit scroll bars.
10978 Otherwise, value is a symbol describing the X toolkit. */);
10979 #ifdef USE_TOOLKIT_SCROLL_BARS
10980 #ifdef USE_MOTIF
10981 Vx_toolkit_scroll_bars = intern_c_string ("motif");
10982 #elif defined HAVE_XAW3D
10983 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
10984 #elif USE_GTK
10985 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
10986 #else
10987 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
10988 #endif
10989 #else
10990 Vx_toolkit_scroll_bars = Qnil;
10991 #endif
10993 staticpro (&last_mouse_motion_frame);
10994 last_mouse_motion_frame = Qnil;
10996 Qmodifier_value = intern_c_string ("modifier-value");
10997 Qalt = intern_c_string ("alt");
10998 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10999 Qhyper = intern_c_string ("hyper");
11000 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11001 Qmeta = intern_c_string ("meta");
11002 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11003 Qsuper = intern_c_string ("super");
11004 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11006 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
11007 doc: /* Which keys Emacs uses for the alt modifier.
11008 This should be one of the symbols `alt', `hyper', `meta', `super'.
11009 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11010 is nil, which is the same as `alt'. */);
11011 Vx_alt_keysym = Qnil;
11013 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
11014 doc: /* Which keys Emacs uses for the hyper modifier.
11015 This should be one of the symbols `alt', `hyper', `meta', `super'.
11016 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11017 default is nil, which is the same as `hyper'. */);
11018 Vx_hyper_keysym = Qnil;
11020 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
11021 doc: /* Which keys Emacs uses for the meta modifier.
11022 This should be one of the symbols `alt', `hyper', `meta', `super'.
11023 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11024 default is nil, which is the same as `meta'. */);
11025 Vx_meta_keysym = Qnil;
11027 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
11028 doc: /* Which keys Emacs uses for the super modifier.
11029 This should be one of the symbols `alt', `hyper', `meta', `super'.
11030 For example, `super' means use the Super_L and Super_R keysyms. The
11031 default is nil, which is the same as `super'. */);
11032 Vx_super_keysym = Qnil;
11034 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
11035 doc: /* Hash table of character codes indexed by X keysym codes. */);
11036 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
11037 make_float (DEFAULT_REHASH_SIZE),
11038 make_float (DEFAULT_REHASH_THRESHOLD),
11039 Qnil, Qnil, Qnil);
11042 #endif /* HAVE_X_WINDOWS */
11044 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
11045 (do not change this comment) */