Fixing user-spec for fonts instead of using name.
[emacs.git] / src / xterm.c
blobefd30f4a601aff2a818a5b691e2e92a5af81d710
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
22 /* Xt features made by Fred Pierresteguy. */
24 #include <config.h>
26 /* On 4.3 these lose if they come after xterm.h. */
27 /* Putting these at the beginning seems to be standard for other .c files. */
28 #include <signal.h>
30 #include <stdio.h>
31 #include <setjmp.h>
33 #ifdef HAVE_X_WINDOWS
35 #include "lisp.h"
36 #include "blockinput.h"
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
57 #include "systime.h"
59 #ifndef INCLUDED_FCNTL
60 #include <fcntl.h>
61 #endif
62 #include <ctype.h>
63 #include <errno.h>
64 #include <setjmp.h>
65 #include <sys/stat.h>
66 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
67 /* #include <sys/param.h> */
69 #include "charset.h"
70 #include "character.h"
71 #include "coding.h"
72 #include "frame.h"
73 #include "dispextern.h"
74 #include "fontset.h"
75 #include "termhooks.h"
76 #include "termopts.h"
77 #include "termchar.h"
78 #include "emacs-icon.h"
79 #include "disptab.h"
80 #include "buffer.h"
81 #include "window.h"
82 #include "keyboard.h"
83 #include "intervals.h"
84 #include "process.h"
85 #include "atimer.h"
86 #include "keymap.h"
87 #include "font.h"
88 #include "fontset.h"
89 #include "xsettings.h"
90 #include "xgselect.h"
91 #include "sysselect.h"
93 #ifdef USE_X_TOOLKIT
94 #include <X11/Shell.h>
95 #endif
97 #ifdef HAVE_SYS_TIME_H
98 #include <sys/time.h>
99 #endif
100 #ifdef HAVE_UNISTD_H
101 #include <unistd.h>
102 #endif
104 #ifdef USE_GTK
105 #include "gtkutil.h"
106 #endif
108 #ifdef USE_LUCID
109 extern int xlwmenu_window_p P_ ((Widget w, Window window));
110 extern void xlwmenu_redisplay P_ ((Widget));
111 #endif
113 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
115 extern void free_frame_menubar P_ ((struct frame *));
116 #endif
118 #ifdef USE_X_TOOLKIT
119 #if !defined(NO_EDITRES)
120 #define HACK_EDITRES
121 extern void _XEditResCheckMessages ();
122 #endif /* not NO_EDITRES */
124 /* Include toolkit specific headers for the scroll bar widget. */
126 #ifdef USE_TOOLKIT_SCROLL_BARS
127 #if defined USE_MOTIF
128 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
129 #include <Xm/ScrollBar.h>
130 #else /* !USE_MOTIF i.e. use Xaw */
132 #ifdef HAVE_XAW3D
133 #include <X11/Xaw3d/Simple.h>
134 #include <X11/Xaw3d/Scrollbar.h>
135 #include <X11/Xaw3d/ThreeD.h>
136 #else /* !HAVE_XAW3D */
137 #include <X11/Xaw/Simple.h>
138 #include <X11/Xaw/Scrollbar.h>
139 #endif /* !HAVE_XAW3D */
140 #ifndef XtNpickTop
141 #define XtNpickTop "pickTop"
142 #endif /* !XtNpickTop */
143 #endif /* !USE_MOTIF */
144 #endif /* USE_TOOLKIT_SCROLL_BARS */
146 #endif /* USE_X_TOOLKIT */
148 #ifdef USE_X_TOOLKIT
149 #include "widget.h"
150 #ifndef XtNinitialState
151 #define XtNinitialState "initialState"
152 #endif
153 #endif
155 /* Default to using XIM if available. */
156 #ifdef USE_XIM
157 int use_xim = 1;
158 #else
159 int use_xim = 0; /* configure --without-xim */
160 #endif
164 /* Non-nil means Emacs uses toolkit scroll bars. */
166 Lisp_Object Vx_toolkit_scroll_bars;
168 /* Non-zero means that a HELP_EVENT has been generated since Emacs
169 start. */
171 static int any_help_event_p;
173 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
174 static Lisp_Object last_window;
176 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
178 int x_use_underline_position_properties;
180 /* Non-zero means to draw the underline at the same place as the descent line. */
182 int x_underline_at_descent_line;
184 /* This is a chain of structures for all the X displays currently in
185 use. */
187 struct x_display_info *x_display_list;
189 /* This is a list of cons cells, each of the form (NAME
190 . FONT-LIST-CACHE), one for each element of x_display_list and in
191 the same order. NAME is the name of the frame. FONT-LIST-CACHE
192 records previous values returned by x-list-fonts. */
194 Lisp_Object x_display_name_list;
196 /* Frame being updated by update_frame. This is declared in term.c.
197 This is set by update_begin and looked at by all the XT functions.
198 It is zero while not inside an update. In that case, the XT
199 functions assume that `selected_frame' is the frame to apply to. */
201 extern struct frame *updating_frame;
203 /* This is a frame waiting to be auto-raised, within XTread_socket. */
205 static struct frame *pending_autoraise_frame;
207 /* This is a frame waiting for an event matching mask, within XTread_socket. */
209 static struct {
210 struct frame *f;
211 int eventtype;
212 } pending_event_wait;
214 #ifdef USE_X_TOOLKIT
215 /* The application context for Xt use. */
216 XtAppContext Xt_app_con;
217 static String Xt_default_resources[] = {0};
218 #endif /* USE_X_TOOLKIT */
220 /* Non-zero means user is interacting with a toolkit scroll bar. */
222 static int toolkit_scroll_bar_interaction;
224 /* Non-zero means to not move point as a result of clicking on a
225 frame to focus it (when focus-follows-mouse is nil). */
227 int x_mouse_click_focus_ignore_position;
229 /* Non-zero timeout value means ignore next mouse click if it arrives
230 before that timeout elapses (i.e. as part of the same sequence of
231 events resulting from clicking on a frame to select it). */
233 static unsigned long ignore_next_mouse_click_timeout;
235 /* Mouse movement.
237 Formerly, we used PointerMotionHintMask (in standard_event_mask)
238 so that we would have to call XQueryPointer after each MotionNotify
239 event to ask for another such event. However, this made mouse tracking
240 slow, and there was a bug that made it eventually stop.
242 Simply asking for MotionNotify all the time seems to work better.
244 In order to avoid asking for motion events and then throwing most
245 of them away or busy-polling the server for mouse positions, we ask
246 the server for pointer motion hints. This means that we get only
247 one event per group of mouse movements. "Groups" are delimited by
248 other kinds of events (focus changes and button clicks, for
249 example), or by XQueryPointer calls; when one of these happens, we
250 get another MotionNotify event the next time the mouse moves. This
251 is at least as efficient as getting motion events when mouse
252 tracking is on, and I suspect only negligibly worse when tracking
253 is off. */
255 /* Where the mouse was last time we reported a mouse event. */
257 static XRectangle last_mouse_glyph;
258 static FRAME_PTR last_mouse_glyph_frame;
259 static Lisp_Object last_mouse_press_frame;
261 /* The scroll bar in which the last X motion event occurred.
263 If the last X motion event occurred in a scroll bar, we set this so
264 XTmouse_position can know whether to report a scroll bar motion or
265 an ordinary motion.
267 If the last X motion event didn't occur in a scroll bar, we set
268 this to Qnil, to tell XTmouse_position to return an ordinary motion
269 event. */
271 static Lisp_Object last_mouse_scroll_bar;
273 /* This is a hack. We would really prefer that XTmouse_position would
274 return the time associated with the position it returns, but there
275 doesn't seem to be any way to wrest the time-stamp from the server
276 along with the position query. So, we just keep track of the time
277 of the last movement we received, and return that in hopes that
278 it's somewhat accurate. */
280 static Time last_mouse_movement_time;
282 /* Time for last user interaction as returned in X events. */
284 static Time last_user_time;
286 /* Incremented by XTread_socket whenever it really tries to read
287 events. */
289 #ifdef __STDC__
290 static int volatile input_signal_count;
291 #else
292 static int input_signal_count;
293 #endif
295 /* Used locally within XTread_socket. */
297 static int x_noop_count;
299 /* Initial values of argv and argc. */
301 extern char **initial_argv;
302 extern int initial_argc;
304 extern Lisp_Object Vcommand_line_args, Vsystem_name;
306 /* Tells if a window manager is present or not. */
308 extern Lisp_Object Vx_no_window_manager;
310 extern Lisp_Object Qeql;
312 extern int errno;
314 /* A mask of extra modifier bits to put into every keyboard char. */
316 extern EMACS_INT extra_keyboard_modifiers;
318 /* The keysyms to use for the various modifiers. */
320 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
321 Lisp_Object Vx_keysym_table;
322 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
324 static Lisp_Object Qvendor_specific_keysyms;
325 static Lisp_Object Qlatin_1;
327 #ifdef USE_GTK
328 /* The name of the Emacs icon file. */
329 static Lisp_Object xg_default_icon_file;
331 /* Used in gtkutil.c. */
332 Lisp_Object Qx_gtk_map_stock;
333 #endif
335 /* Used in x_flush. */
337 extern Lisp_Object Vinhibit_redisplay;
339 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
340 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
342 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
343 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
344 static const XColor *x_color_cells P_ ((Display *, int *));
345 static void x_update_window_end P_ ((struct window *, int, int));
347 static int x_io_error_quitter P_ ((Display *));
348 static struct terminal *x_create_terminal P_ ((struct x_display_info *));
349 void x_delete_terminal P_ ((struct terminal *));
350 static void x_update_end P_ ((struct frame *));
351 static void XTframe_up_to_date P_ ((struct frame *));
352 static void XTset_terminal_modes P_ ((struct terminal *));
353 static void XTreset_terminal_modes P_ ((struct terminal *));
354 static void x_clear_frame P_ ((struct frame *));
355 static void frame_highlight P_ ((struct frame *));
356 static void frame_unhighlight P_ ((struct frame *));
357 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
358 static void x_focus_changed P_ ((int, int, struct x_display_info *,
359 struct frame *, struct input_event *));
360 static void x_detect_focus_change P_ ((struct x_display_info *,
361 XEvent *, struct input_event *));
362 static void XTframe_rehighlight P_ ((struct frame *));
363 static void x_frame_rehighlight P_ ((struct x_display_info *));
364 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
365 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
366 enum text_cursor_kinds));
368 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
369 static void x_flush P_ ((struct frame *f));
370 static void x_update_begin P_ ((struct frame *));
371 static void x_update_window_begin P_ ((struct window *));
372 static void x_after_update_window_line P_ ((struct glyph_row *));
373 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
374 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
375 enum scroll_bar_part *,
376 Lisp_Object *, Lisp_Object *,
377 unsigned long *));
378 static void x_handle_net_wm_state P_ ((struct frame *, XPropertyEvent *));
379 static void x_check_fullscreen P_ ((struct frame *));
380 static void x_check_expected_move P_ ((struct frame *, int, int));
381 static void x_sync_with_move P_ ((struct frame *, int, int, int));
382 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
383 int *, struct input_event *));
384 /* Don't declare this NO_RETURN because we want no
385 interference with debugging failing X calls. */
386 static SIGTYPE x_connection_closed P_ ((Display *, char *));
389 /* Flush display of frame F, or of all frames if F is null. */
391 static void
392 x_flush (f)
393 struct frame *f;
395 /* Don't call XFlush when it is not safe to redisplay; the X
396 connection may be broken. */
397 if (!NILP (Vinhibit_redisplay))
398 return;
400 BLOCK_INPUT;
401 if (f == NULL)
403 Lisp_Object rest, frame;
404 FOR_EACH_FRAME (rest, frame)
405 if (FRAME_X_P (XFRAME (frame)))
406 x_flush (XFRAME (frame));
408 else if (FRAME_X_P (f))
409 XFlush (FRAME_X_DISPLAY (f));
410 UNBLOCK_INPUT;
414 /* Remove calls to XFlush by defining XFlush to an empty replacement.
415 Calls to XFlush should be unnecessary because the X output buffer
416 is flushed automatically as needed by calls to XPending,
417 XNextEvent, or XWindowEvent according to the XFlush man page.
418 XTread_socket calls XPending. Removing XFlush improves
419 performance. */
421 #define XFlush(DISPLAY) (void) 0
424 /***********************************************************************
425 Debugging
426 ***********************************************************************/
428 #if 0
430 /* This is a function useful for recording debugging information about
431 the sequence of occurrences in this file. */
433 struct record
435 char *locus;
436 int type;
439 struct record event_record[100];
441 int event_record_index;
443 record_event (locus, type)
444 char *locus;
445 int type;
447 if (event_record_index == sizeof (event_record) / sizeof (struct record))
448 event_record_index = 0;
450 event_record[event_record_index].locus = locus;
451 event_record[event_record_index].type = type;
452 event_record_index++;
455 #endif /* 0 */
459 /* Return the struct x_display_info corresponding to DPY. */
461 struct x_display_info *
462 x_display_info_for_display (dpy)
463 Display *dpy;
465 struct x_display_info *dpyinfo;
467 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
468 if (dpyinfo->display == dpy)
469 return dpyinfo;
471 return 0;
474 #define OPAQUE 0xffffffff
475 #define OPACITY "_NET_WM_WINDOW_OPACITY"
477 void
478 x_set_frame_alpha (f)
479 struct frame *f;
481 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
482 Display *dpy = FRAME_X_DISPLAY (f);
483 Window win = FRAME_OUTER_WINDOW (f);
484 double alpha = 1.0;
485 double alpha_min = 1.0;
486 unsigned long opac;
488 if (FRAME_X_DISPLAY_INFO (f)->root_window != FRAME_X_OUTPUT (f)->parent_desc)
489 /* Since the WM decoration lies under the FRAME_OUTER_WINDOW,
490 we must treat the former instead of the latter. */
491 win = FRAME_X_OUTPUT(f)->parent_desc;
493 if (dpyinfo->x_highlight_frame == f)
494 alpha = f->alpha[0];
495 else
496 alpha = f->alpha[1];
498 if (FLOATP (Vframe_alpha_lower_limit))
499 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
500 else if (INTEGERP (Vframe_alpha_lower_limit))
501 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
503 if (alpha < 0.0)
504 return;
505 else if (alpha > 1.0)
506 alpha = 1.0;
507 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
508 alpha = alpha_min;
510 opac = alpha * OPAQUE;
512 /* return unless necessary */
514 unsigned char *data;
515 Atom actual;
516 int rc, format;
517 unsigned long n, left;
519 x_catch_errors (dpy);
520 rc = XGetWindowProperty(dpy, win, XInternAtom(dpy, OPACITY, False),
521 0L, 1L, False, XA_CARDINAL,
522 &actual, &format, &n, &left,
523 &data);
525 if (rc == Success && actual != None)
526 if (*(unsigned long *)data == opac)
528 XFree ((void *) data);
529 x_uncatch_errors ();
530 return;
532 else
533 XFree ((void *) data);
534 x_uncatch_errors ();
537 x_catch_errors (dpy);
538 XChangeProperty (dpy, win, XInternAtom (dpy, OPACITY, False),
539 XA_CARDINAL, 32, PropModeReplace,
540 (unsigned char *) &opac, 1L);
541 x_uncatch_errors ();
545 x_display_pixel_height (dpyinfo)
546 struct x_display_info *dpyinfo;
548 return HeightOfScreen (dpyinfo->screen);
552 x_display_pixel_width (dpyinfo)
553 struct x_display_info *dpyinfo;
555 return WidthOfScreen (dpyinfo->screen);
559 /***********************************************************************
560 Starting and ending an update
561 ***********************************************************************/
563 /* Start an update of frame F. This function is installed as a hook
564 for update_begin, i.e. it is called when update_begin is called.
565 This function is called prior to calls to x_update_window_begin for
566 each window being updated. Currently, there is nothing to do here
567 because all interesting stuff is done on a window basis. */
569 static void
570 x_update_begin (f)
571 struct frame *f;
573 /* Nothing to do. */
577 /* Start update of window W. Set the global variable updated_window
578 to the window being updated and set output_cursor to the cursor
579 position of W. */
581 static void
582 x_update_window_begin (w)
583 struct window *w;
585 struct frame *f = XFRAME (WINDOW_FRAME (w));
586 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
588 updated_window = w;
589 set_output_cursor (&w->cursor);
591 BLOCK_INPUT;
593 if (f == display_info->mouse_face_mouse_frame)
595 /* Don't do highlighting for mouse motion during the update. */
596 display_info->mouse_face_defer = 1;
598 /* If F needs to be redrawn, simply forget about any prior mouse
599 highlighting. */
600 if (FRAME_GARBAGED_P (f))
601 display_info->mouse_face_window = Qnil;
604 UNBLOCK_INPUT;
608 /* Draw a vertical window border from (x,y0) to (x,y1) */
610 static void
611 x_draw_vertical_window_border (w, x, y0, y1)
612 struct window *w;
613 int x, y0, y1;
615 struct frame *f = XFRAME (WINDOW_FRAME (w));
616 struct face *face;
618 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
619 if (face)
620 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
621 face->foreground);
623 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
624 f->output_data.x->normal_gc, x, y0, x, y1);
627 /* End update of window W (which is equal to updated_window).
629 Draw vertical borders between horizontally adjacent windows, and
630 display W's cursor if CURSOR_ON_P is non-zero.
632 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
633 glyphs in mouse-face were overwritten. In that case we have to
634 make sure that the mouse-highlight is properly redrawn.
636 W may be a menu bar pseudo-window in case we don't have X toolkit
637 support. Such windows don't have a cursor, so don't display it
638 here. */
640 static void
641 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
642 struct window *w;
643 int cursor_on_p, mouse_face_overwritten_p;
645 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
647 if (!w->pseudo_window_p)
649 BLOCK_INPUT;
651 if (cursor_on_p)
652 display_and_set_cursor (w, 1, output_cursor.hpos,
653 output_cursor.vpos,
654 output_cursor.x, output_cursor.y);
656 if (draw_window_fringes (w, 1))
657 x_draw_vertical_border (w);
659 UNBLOCK_INPUT;
662 /* If a row with mouse-face was overwritten, arrange for
663 XTframe_up_to_date to redisplay the mouse highlight. */
664 if (mouse_face_overwritten_p)
666 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
667 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
668 dpyinfo->mouse_face_window = Qnil;
671 updated_window = NULL;
675 /* End update of frame F. This function is installed as a hook in
676 update_end. */
678 static void
679 x_update_end (f)
680 struct frame *f;
682 /* Mouse highlight may be displayed again. */
683 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
685 #ifndef XFlush
686 BLOCK_INPUT;
687 XFlush (FRAME_X_DISPLAY (f));
688 UNBLOCK_INPUT;
689 #endif
693 /* This function is called from various places in xdisp.c whenever a
694 complete update has been performed. The global variable
695 updated_window is not available here. */
697 static void
698 XTframe_up_to_date (f)
699 struct frame *f;
701 if (FRAME_X_P (f))
703 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
705 if (dpyinfo->mouse_face_deferred_gc
706 || f == dpyinfo->mouse_face_mouse_frame)
708 BLOCK_INPUT;
709 if (dpyinfo->mouse_face_mouse_frame)
710 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
711 dpyinfo->mouse_face_mouse_x,
712 dpyinfo->mouse_face_mouse_y);
713 dpyinfo->mouse_face_deferred_gc = 0;
714 UNBLOCK_INPUT;
720 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
721 arrow bitmaps, or clear the fringes if no bitmaps are required
722 before DESIRED_ROW is made current. The window being updated is
723 found in updated_window. This function It is called from
724 update_window_line only if it is known that there are differences
725 between bitmaps to be drawn between current row and DESIRED_ROW. */
727 static void
728 x_after_update_window_line (desired_row)
729 struct glyph_row *desired_row;
731 struct window *w = updated_window;
732 struct frame *f;
733 int width, height;
735 xassert (w);
737 if (!desired_row->mode_line_p && !w->pseudo_window_p)
738 desired_row->redraw_fringe_bitmaps_p = 1;
740 /* When a window has disappeared, make sure that no rest of
741 full-width rows stays visible in the internal border. Could
742 check here if updated_window is the leftmost/rightmost window,
743 but I guess it's not worth doing since vertically split windows
744 are almost never used, internal border is rarely set, and the
745 overhead is very small. */
746 if (windows_or_buffers_changed
747 && desired_row->full_width_p
748 && (f = XFRAME (w->frame),
749 width = FRAME_INTERNAL_BORDER_WIDTH (f),
750 width != 0)
751 && (height = desired_row->visible_height,
752 height > 0))
754 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
756 /* Internal border is drawn below the tool bar. */
757 if (WINDOWP (f->tool_bar_window)
758 && w == XWINDOW (f->tool_bar_window))
759 y -= width;
761 BLOCK_INPUT;
762 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
763 0, y, width, height, False);
764 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
765 FRAME_PIXEL_WIDTH (f) - width,
766 y, width, height, False);
767 UNBLOCK_INPUT;
771 static void
772 x_draw_fringe_bitmap (w, row, p)
773 struct window *w;
774 struct glyph_row *row;
775 struct draw_fringe_bitmap_params *p;
777 struct frame *f = XFRAME (WINDOW_FRAME (w));
778 Display *display = FRAME_X_DISPLAY (f);
779 Window window = FRAME_X_WINDOW (f);
780 GC gc = f->output_data.x->normal_gc;
781 struct face *face = p->face;
782 int rowY;
784 /* Must clip because of partially visible lines. */
785 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
786 if (p->y < rowY)
788 /* Adjust position of "bottom aligned" bitmap on partially
789 visible last row. */
790 int oldY = row->y;
791 int oldVH = row->visible_height;
792 row->visible_height = p->h;
793 row->y -= rowY - p->y;
794 x_clip_to_row (w, row, -1, gc);
795 row->y = oldY;
796 row->visible_height = oldVH;
798 else
799 x_clip_to_row (w, row, -1, gc);
801 if (!p->overlay_p)
803 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
805 /* In case the same realized face is used for fringes and
806 for something displayed in the text (e.g. face `region' on
807 mono-displays, the fill style may have been changed to
808 FillSolid in x_draw_glyph_string_background. */
809 if (face->stipple)
810 XSetFillStyle (display, face->gc, FillOpaqueStippled);
811 else
812 XSetForeground (display, face->gc, face->background);
814 #ifdef USE_TOOLKIT_SCROLL_BARS
815 /* If the fringe is adjacent to the left (right) scroll bar of a
816 leftmost (rightmost, respectively) window, then extend its
817 background to the gap between the fringe and the bar. */
818 if ((WINDOW_LEFTMOST_P (w)
819 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
820 || (WINDOW_RIGHTMOST_P (w)
821 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
823 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
825 if (sb_width > 0)
827 int left = WINDOW_SCROLL_BAR_AREA_X (w);
828 int width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
829 * FRAME_COLUMN_WIDTH (f));
831 if (bx < 0)
833 /* Bitmap fills the fringe. */
834 if (left + width == p->x)
835 bx = left + sb_width;
836 else if (p->x + p->wd == left)
837 bx = left;
838 if (bx >= 0)
840 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
842 nx = width - sb_width;
843 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
844 row->y));
845 ny = row->visible_height;
848 else
850 if (left + width == bx)
852 bx = left + sb_width;
853 nx += width - sb_width;
855 else if (bx + nx == left)
856 nx += width - sb_width;
860 #endif
861 if (bx >= 0 && nx > 0)
862 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
864 if (!face->stipple)
865 XSetForeground (display, face->gc, face->foreground);
868 if (p->which)
870 unsigned char *bits;
871 Pixmap pixmap, clipmask = (Pixmap) 0;
872 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
873 XGCValues gcv;
875 if (p->wd > 8)
876 bits = (unsigned char *)(p->bits + p->dh);
877 else
878 bits = (unsigned char *)p->bits + p->dh;
880 /* Draw the bitmap. I believe these small pixmaps can be cached
881 by the server. */
882 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
883 (p->cursor_p
884 ? (p->overlay_p ? face->background
885 : f->output_data.x->cursor_pixel)
886 : face->foreground),
887 face->background, depth);
889 if (p->overlay_p)
891 clipmask = XCreatePixmapFromBitmapData (display,
892 FRAME_X_DISPLAY_INFO (f)->root_window,
893 bits, p->wd, p->h,
894 1, 0, 1);
895 gcv.clip_mask = clipmask;
896 gcv.clip_x_origin = p->x;
897 gcv.clip_y_origin = p->y;
898 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
901 XCopyArea (display, pixmap, window, gc, 0, 0,
902 p->wd, p->h, p->x, p->y);
903 XFreePixmap (display, pixmap);
905 if (p->overlay_p)
907 gcv.clip_mask = (Pixmap) 0;
908 XChangeGC (display, gc, GCClipMask, &gcv);
909 XFreePixmap (display, clipmask);
913 XSetClipMask (display, gc, None);
918 /* This is called when starting Emacs and when restarting after
919 suspend. When starting Emacs, no X window is mapped. And nothing
920 must be done to Emacs's own window if it is suspended (though that
921 rarely happens). */
923 static void
924 XTset_terminal_modes (struct terminal *terminal)
928 /* This is called when exiting or suspending Emacs. Exiting will make
929 the X-windows go away, and suspending requires no action. */
931 static void
932 XTreset_terminal_modes (struct terminal *terminal)
937 /***********************************************************************
938 Glyph display
939 ***********************************************************************/
943 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
944 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
945 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
946 int));
947 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
948 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
949 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
950 static void x_draw_glyph_string P_ ((struct glyph_string *));
951 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
952 static void x_set_cursor_gc P_ ((struct glyph_string *));
953 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
954 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
955 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
956 unsigned long *, double, int));
957 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
958 double, int, unsigned long));
959 static void x_setup_relief_colors P_ ((struct glyph_string *));
960 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
961 static void x_draw_image_relief P_ ((struct glyph_string *));
962 static void x_draw_image_foreground P_ ((struct glyph_string *));
963 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
964 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
965 int, int, int));
966 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
967 int, int, int, int, int, int,
968 XRectangle *));
969 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
970 int, int, int, XRectangle *));
972 #if GLYPH_DEBUG
973 static void x_check_font P_ ((struct frame *, struct font *));
974 #endif
977 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
978 face. */
980 static void
981 x_set_cursor_gc (s)
982 struct glyph_string *s;
984 if (s->font == FRAME_FONT (s->f)
985 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
986 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
987 && !s->cmp)
988 s->gc = s->f->output_data.x->cursor_gc;
989 else
991 /* Cursor on non-default face: must merge. */
992 XGCValues xgcv;
993 unsigned long mask;
995 xgcv.background = s->f->output_data.x->cursor_pixel;
996 xgcv.foreground = s->face->background;
998 /* If the glyph would be invisible, try a different foreground. */
999 if (xgcv.foreground == xgcv.background)
1000 xgcv.foreground = s->face->foreground;
1001 if (xgcv.foreground == xgcv.background)
1002 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1003 if (xgcv.foreground == xgcv.background)
1004 xgcv.foreground = s->face->foreground;
1006 /* Make sure the cursor is distinct from text in this face. */
1007 if (xgcv.background == s->face->background
1008 && xgcv.foreground == s->face->foreground)
1010 xgcv.background = s->face->foreground;
1011 xgcv.foreground = s->face->background;
1014 IF_DEBUG (x_check_font (s->f, s->font));
1015 xgcv.graphics_exposures = False;
1016 mask = GCForeground | GCBackground | GCGraphicsExposures;
1018 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1019 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1020 mask, &xgcv);
1021 else
1022 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1023 = XCreateGC (s->display, s->window, mask, &xgcv);
1025 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1030 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1032 static void
1033 x_set_mouse_face_gc (s)
1034 struct glyph_string *s;
1036 int face_id;
1037 struct face *face;
1039 /* What face has to be used last for the mouse face? */
1040 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1041 face = FACE_FROM_ID (s->f, face_id);
1042 if (face == NULL)
1043 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1045 if (s->first_glyph->type == CHAR_GLYPH)
1046 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1047 else
1048 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1049 s->face = FACE_FROM_ID (s->f, face_id);
1050 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1052 if (s->font == s->face->font)
1053 s->gc = s->face->gc;
1054 else
1056 /* Otherwise construct scratch_cursor_gc with values from FACE
1057 except for FONT. */
1058 XGCValues xgcv;
1059 unsigned long mask;
1061 xgcv.background = s->face->background;
1062 xgcv.foreground = s->face->foreground;
1063 xgcv.graphics_exposures = False;
1064 mask = GCForeground | GCBackground | GCGraphicsExposures;
1066 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1067 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1068 mask, &xgcv);
1069 else
1070 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1071 = XCreateGC (s->display, s->window, mask, &xgcv);
1073 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1076 xassert (s->gc != 0);
1080 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1081 Faces to use in the mode line have already been computed when the
1082 matrix was built, so there isn't much to do, here. */
1084 static INLINE void
1085 x_set_mode_line_face_gc (s)
1086 struct glyph_string *s;
1088 s->gc = s->face->gc;
1092 /* Set S->gc of glyph string S for drawing that glyph string. Set
1093 S->stippled_p to a non-zero value if the face of S has a stipple
1094 pattern. */
1096 static INLINE void
1097 x_set_glyph_string_gc (s)
1098 struct glyph_string *s;
1100 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1102 if (s->hl == DRAW_NORMAL_TEXT)
1104 s->gc = s->face->gc;
1105 s->stippled_p = s->face->stipple != 0;
1107 else if (s->hl == DRAW_INVERSE_VIDEO)
1109 x_set_mode_line_face_gc (s);
1110 s->stippled_p = s->face->stipple != 0;
1112 else if (s->hl == DRAW_CURSOR)
1114 x_set_cursor_gc (s);
1115 s->stippled_p = 0;
1117 else if (s->hl == DRAW_MOUSE_FACE)
1119 x_set_mouse_face_gc (s);
1120 s->stippled_p = s->face->stipple != 0;
1122 else if (s->hl == DRAW_IMAGE_RAISED
1123 || s->hl == DRAW_IMAGE_SUNKEN)
1125 s->gc = s->face->gc;
1126 s->stippled_p = s->face->stipple != 0;
1128 else
1130 s->gc = s->face->gc;
1131 s->stippled_p = s->face->stipple != 0;
1134 /* GC must have been set. */
1135 xassert (s->gc != 0);
1139 /* Set clipping for output of glyph string S. S may be part of a mode
1140 line or menu if we don't have X toolkit support. */
1142 static INLINE void
1143 x_set_glyph_string_clipping (s)
1144 struct glyph_string *s;
1146 XRectangle *r = s->clip;
1147 int n = get_glyph_string_clip_rects (s, r, 2);
1149 if (n > 0)
1150 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
1151 s->num_clips = n;
1155 /* Set SRC's clipping for output of glyph string DST. This is called
1156 when we are drawing DST's left_overhang or right_overhang only in
1157 the area of SRC. */
1159 static void
1160 x_set_glyph_string_clipping_exactly (src, dst)
1161 struct glyph_string *src, *dst;
1163 XRectangle r;
1165 r.x = src->x;
1166 r.width = src->width;
1167 r.y = src->y;
1168 r.height = src->height;
1169 dst->clip[0] = r;
1170 dst->num_clips = 1;
1171 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1175 /* RIF:
1176 Compute left and right overhang of glyph string S. */
1178 static void
1179 x_compute_glyph_string_overhangs (s)
1180 struct glyph_string *s;
1182 if (s->cmp == NULL
1183 && (s->first_glyph->type == CHAR_GLYPH
1184 || s->first_glyph->type == COMPOSITE_GLYPH))
1186 struct font_metrics metrics;
1188 if (s->first_glyph->type == CHAR_GLYPH)
1190 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1191 struct font *font = s->font;
1192 int i;
1194 for (i = 0; i < s->nchars; i++)
1195 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1196 font->driver->text_extents (font, code, s->nchars, &metrics);
1198 else
1200 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1202 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1204 s->right_overhang = (metrics.rbearing > metrics.width
1205 ? metrics.rbearing - metrics.width : 0);
1206 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1208 else if (s->cmp)
1210 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1211 s->left_overhang = - s->cmp->lbearing;
1216 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1218 static INLINE void
1219 x_clear_glyph_string_rect (s, x, y, w, h)
1220 struct glyph_string *s;
1221 int x, y, w, h;
1223 XGCValues xgcv;
1224 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1225 XSetForeground (s->display, s->gc, xgcv.background);
1226 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1227 XSetForeground (s->display, s->gc, xgcv.foreground);
1231 /* Draw the background of glyph_string S. If S->background_filled_p
1232 is non-zero don't draw it. FORCE_P non-zero means draw the
1233 background even if it wouldn't be drawn normally. This is used
1234 when a string preceding S draws into the background of S, or S
1235 contains the first component of a composition. */
1237 static void
1238 x_draw_glyph_string_background (s, force_p)
1239 struct glyph_string *s;
1240 int force_p;
1242 /* Nothing to do if background has already been drawn or if it
1243 shouldn't be drawn in the first place. */
1244 if (!s->background_filled_p)
1246 int box_line_width = max (s->face->box_line_width, 0);
1248 if (s->stippled_p)
1250 /* Fill background with a stipple pattern. */
1251 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1252 XFillRectangle (s->display, s->window, s->gc, s->x,
1253 s->y + box_line_width,
1254 s->background_width,
1255 s->height - 2 * box_line_width);
1256 XSetFillStyle (s->display, s->gc, FillSolid);
1257 s->background_filled_p = 1;
1259 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1260 || s->font_not_found_p
1261 || s->extends_to_end_of_line_p
1262 || force_p)
1264 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1265 s->background_width,
1266 s->height - 2 * box_line_width);
1267 s->background_filled_p = 1;
1273 /* Draw the foreground of glyph string S. */
1275 static void
1276 x_draw_glyph_string_foreground (s)
1277 struct glyph_string *s;
1279 int i, x;
1281 /* If first glyph of S has a left box line, start drawing the text
1282 of S to the right of that box line. */
1283 if (s->face->box != FACE_NO_BOX
1284 && s->first_glyph->left_box_line_p)
1285 x = s->x + eabs (s->face->box_line_width);
1286 else
1287 x = s->x;
1289 /* Draw characters of S as rectangles if S's font could not be
1290 loaded. */
1291 if (s->font_not_found_p)
1293 for (i = 0; i < s->nchars; ++i)
1295 struct glyph *g = s->first_glyph + i;
1296 XDrawRectangle (s->display, s->window,
1297 s->gc, x, s->y, g->pixel_width - 1,
1298 s->height - 1);
1299 x += g->pixel_width;
1302 else
1304 struct font *font = s->font;
1305 int boff = font->baseline_offset;
1306 int y;
1308 if (font->vertical_centering)
1309 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1311 y = s->ybase - boff;
1312 if (s->for_overlaps
1313 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1314 font->driver->draw (s, 0, s->nchars, x, y, 0);
1315 else
1316 font->driver->draw (s, 0, s->nchars, x, y, 1);
1317 if (s->face->overstrike)
1318 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1322 /* Draw the foreground of composite glyph string S. */
1324 static void
1325 x_draw_composite_glyph_string_foreground (s)
1326 struct glyph_string *s;
1328 int i, j, x;
1329 struct font *font = s->font;
1331 /* If first glyph of S has a left box line, start drawing the text
1332 of S to the right of that box line. */
1333 if (s->face && s->face->box != FACE_NO_BOX
1334 && s->first_glyph->left_box_line_p)
1335 x = s->x + eabs (s->face->box_line_width);
1336 else
1337 x = s->x;
1339 /* S is a glyph string for a composition. S->cmp_from is the index
1340 of the first character drawn for glyphs of this composition.
1341 S->cmp_from == 0 means we are drawing the very first character of
1342 this composition. */
1344 /* Draw a rectangle for the composition if the font for the very
1345 first character of the composition could not be loaded. */
1346 if (s->font_not_found_p)
1348 if (s->cmp_from == 0)
1349 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1350 s->width - 1, s->height - 1);
1352 else if (! s->first_glyph->u.cmp.automatic)
1354 int y = s->ybase;
1356 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1357 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1359 int xx = x + s->cmp->offsets[j * 2];
1360 int yy = y - s->cmp->offsets[j * 2 + 1];
1362 font->driver->draw (s, j, j + 1, xx, yy, 0);
1363 if (s->face->overstrike)
1364 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1367 else
1369 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1370 Lisp_Object glyph;
1371 int y = s->ybase;
1372 int width = 0;
1374 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1376 glyph = LGSTRING_GLYPH (gstring, i);
1377 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1378 width += LGLYPH_WIDTH (glyph);
1379 else
1381 int xoff, yoff, wadjust;
1383 if (j < i)
1385 font->driver->draw (s, j, i, x, y, 0);
1386 x += width;
1388 xoff = LGLYPH_XOFF (glyph);
1389 yoff = LGLYPH_YOFF (glyph);
1390 wadjust = LGLYPH_WADJUST (glyph);
1391 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1392 x += wadjust;
1393 j = i + 1;
1394 width = 0;
1397 if (j < i)
1398 font->driver->draw (s, j, i, x, y, 0);
1403 #ifdef USE_X_TOOLKIT
1405 static struct frame *x_frame_of_widget P_ ((Widget));
1406 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1407 XrmValue *, XrmValue *, XtPointer *));
1408 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1409 XrmValue *, Cardinal *));
1412 /* Return the frame on which widget WIDGET is used.. Abort if frame
1413 cannot be determined. */
1415 static struct frame *
1416 x_frame_of_widget (widget)
1417 Widget widget;
1419 struct x_display_info *dpyinfo;
1420 Lisp_Object tail;
1421 struct frame *f;
1423 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1425 /* Find the top-level shell of the widget. Note that this function
1426 can be called when the widget is not yet realized, so XtWindow
1427 (widget) == 0. That's the reason we can't simply use
1428 x_any_window_to_frame. */
1429 while (!XtIsTopLevelShell (widget))
1430 widget = XtParent (widget);
1432 /* Look for a frame with that top-level widget. Allocate the color
1433 on that frame to get the right gamma correction value. */
1434 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1435 if (FRAMEP (XCAR (tail))
1436 && (f = XFRAME (XCAR (tail)),
1437 (FRAME_X_P (f)
1438 && f->output_data.nothing != 1
1439 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1440 && f->output_data.x->widget == widget)
1441 return f;
1443 abort ();
1447 /* Allocate the color COLOR->pixel on the screen and display of
1448 widget WIDGET in colormap CMAP. If an exact match cannot be
1449 allocated, try the nearest color available. Value is non-zero
1450 if successful. This is called from lwlib. */
1453 x_alloc_nearest_color_for_widget (widget, cmap, color)
1454 Widget widget;
1455 Colormap cmap;
1456 XColor *color;
1458 struct frame *f = x_frame_of_widget (widget);
1459 return x_alloc_nearest_color (f, cmap, color);
1463 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1464 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1465 If this produces the same color as PIXEL, try a color where all RGB
1466 values have DELTA added. Return the allocated color in *PIXEL.
1467 DISPLAY is the X display, CMAP is the colormap to operate on.
1468 Value is non-zero if successful. */
1471 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1472 Widget widget;
1473 Display *display;
1474 Colormap cmap;
1475 unsigned long *pixel;
1476 double factor;
1477 int delta;
1479 struct frame *f = x_frame_of_widget (widget);
1480 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1484 /* Structure specifying which arguments should be passed by Xt to
1485 cvt_string_to_pixel. We want the widget's screen and colormap. */
1487 static XtConvertArgRec cvt_string_to_pixel_args[] =
1489 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1490 sizeof (Screen *)},
1491 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1492 sizeof (Colormap)}
1496 /* The address of this variable is returned by
1497 cvt_string_to_pixel. */
1499 static Pixel cvt_string_to_pixel_value;
1502 /* Convert a color name to a pixel color.
1504 DPY is the display we are working on.
1506 ARGS is an array of *NARGS XrmValue structures holding additional
1507 information about the widget for which the conversion takes place.
1508 The contents of this array are determined by the specification
1509 in cvt_string_to_pixel_args.
1511 FROM is a pointer to an XrmValue which points to the color name to
1512 convert. TO is an XrmValue in which to return the pixel color.
1514 CLOSURE_RET is a pointer to user-data, in which we record if
1515 we allocated the color or not.
1517 Value is True if successful, False otherwise. */
1519 static Boolean
1520 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1521 Display *dpy;
1522 XrmValue *args;
1523 Cardinal *nargs;
1524 XrmValue *from, *to;
1525 XtPointer *closure_ret;
1527 Screen *screen;
1528 Colormap cmap;
1529 Pixel pixel;
1530 String color_name;
1531 XColor color;
1533 if (*nargs != 2)
1535 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1536 "wrongParameters", "cvt_string_to_pixel",
1537 "XtToolkitError",
1538 "Screen and colormap args required", NULL, NULL);
1539 return False;
1542 screen = *(Screen **) args[0].addr;
1543 cmap = *(Colormap *) args[1].addr;
1544 color_name = (String) from->addr;
1546 if (strcmp (color_name, XtDefaultBackground) == 0)
1548 *closure_ret = (XtPointer) False;
1549 pixel = WhitePixelOfScreen (screen);
1551 else if (strcmp (color_name, XtDefaultForeground) == 0)
1553 *closure_ret = (XtPointer) False;
1554 pixel = BlackPixelOfScreen (screen);
1556 else if (XParseColor (dpy, cmap, color_name, &color)
1557 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1559 pixel = color.pixel;
1560 *closure_ret = (XtPointer) True;
1562 else
1564 String params[1];
1565 Cardinal nparams = 1;
1567 params[0] = color_name;
1568 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1569 "badValue", "cvt_string_to_pixel",
1570 "XtToolkitError", "Invalid color `%s'",
1571 params, &nparams);
1572 return False;
1575 if (to->addr != NULL)
1577 if (to->size < sizeof (Pixel))
1579 to->size = sizeof (Pixel);
1580 return False;
1583 *(Pixel *) to->addr = pixel;
1585 else
1587 cvt_string_to_pixel_value = pixel;
1588 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1591 to->size = sizeof (Pixel);
1592 return True;
1596 /* Free a pixel color which was previously allocated via
1597 cvt_string_to_pixel. This is registered as the destructor
1598 for this type of resource via XtSetTypeConverter.
1600 APP is the application context in which we work.
1602 TO is a pointer to an XrmValue holding the color to free.
1603 CLOSURE is the value we stored in CLOSURE_RET for this color
1604 in cvt_string_to_pixel.
1606 ARGS and NARGS are like for cvt_string_to_pixel. */
1608 static void
1609 cvt_pixel_dtor (app, to, closure, args, nargs)
1610 XtAppContext app;
1611 XrmValuePtr to;
1612 XtPointer closure;
1613 XrmValuePtr args;
1614 Cardinal *nargs;
1616 if (*nargs != 2)
1618 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1619 "XtToolkitError",
1620 "Screen and colormap arguments required",
1621 NULL, NULL);
1623 else if (closure != NULL)
1625 /* We did allocate the pixel, so free it. */
1626 Screen *screen = *(Screen **) args[0].addr;
1627 Colormap cmap = *(Colormap *) args[1].addr;
1628 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1629 (Pixel *) to->addr, 1);
1634 #endif /* USE_X_TOOLKIT */
1637 /* Value is an array of XColor structures for the contents of the
1638 color map of display DPY. Set *NCELLS to the size of the array.
1639 Note that this probably shouldn't be called for large color maps,
1640 say a 24-bit TrueColor map. */
1642 static const XColor *
1643 x_color_cells (dpy, ncells)
1644 Display *dpy;
1645 int *ncells;
1647 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1649 if (dpyinfo->color_cells == NULL)
1651 Screen *screen = dpyinfo->screen;
1652 int i;
1654 dpyinfo->ncolor_cells
1655 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1656 dpyinfo->color_cells
1657 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1658 * sizeof *dpyinfo->color_cells);
1660 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1661 dpyinfo->color_cells[i].pixel = i;
1663 XQueryColors (dpy, dpyinfo->cmap,
1664 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1667 *ncells = dpyinfo->ncolor_cells;
1668 return dpyinfo->color_cells;
1672 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1673 colors in COLORS. Use cached information, if available. */
1675 void
1676 x_query_colors (f, colors, ncolors)
1677 struct frame *f;
1678 XColor *colors;
1679 int ncolors;
1681 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1683 if (dpyinfo->color_cells)
1685 int i;
1686 for (i = 0; i < ncolors; ++i)
1688 unsigned long pixel = colors[i].pixel;
1689 xassert (pixel < dpyinfo->ncolor_cells);
1690 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1691 colors[i] = dpyinfo->color_cells[pixel];
1694 else
1695 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1699 /* On frame F, translate pixel color to RGB values for the color in
1700 COLOR. Use cached information, if available. */
1702 void
1703 x_query_color (f, color)
1704 struct frame *f;
1705 XColor *color;
1707 x_query_colors (f, color, 1);
1711 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1712 exact match can't be allocated, try the nearest color available.
1713 Value is non-zero if successful. Set *COLOR to the color
1714 allocated. */
1716 static int
1717 x_alloc_nearest_color_1 (dpy, cmap, color)
1718 Display *dpy;
1719 Colormap cmap;
1720 XColor *color;
1722 int rc;
1724 rc = XAllocColor (dpy, cmap, color);
1725 if (rc == 0)
1727 /* If we got to this point, the colormap is full, so we're going
1728 to try to get the next closest color. The algorithm used is
1729 a least-squares matching, which is what X uses for closest
1730 color matching with StaticColor visuals. */
1731 int nearest, i;
1732 unsigned long nearest_delta = ~0;
1733 int ncells;
1734 const XColor *cells = x_color_cells (dpy, &ncells);
1736 for (nearest = i = 0; i < ncells; ++i)
1738 long dred = (color->red >> 8) - (cells[i].red >> 8);
1739 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1740 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1741 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1743 if (delta < nearest_delta)
1745 nearest = i;
1746 nearest_delta = delta;
1750 color->red = cells[nearest].red;
1751 color->green = cells[nearest].green;
1752 color->blue = cells[nearest].blue;
1753 rc = XAllocColor (dpy, cmap, color);
1755 else
1757 /* If allocation succeeded, and the allocated pixel color is not
1758 equal to a cached pixel color recorded earlier, there was a
1759 change in the colormap, so clear the color cache. */
1760 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1761 XColor *cached_color;
1763 if (dpyinfo->color_cells
1764 && (cached_color = &dpyinfo->color_cells[color->pixel],
1765 (cached_color->red != color->red
1766 || cached_color->blue != color->blue
1767 || cached_color->green != color->green)))
1769 xfree (dpyinfo->color_cells);
1770 dpyinfo->color_cells = NULL;
1771 dpyinfo->ncolor_cells = 0;
1775 #ifdef DEBUG_X_COLORS
1776 if (rc)
1777 register_color (color->pixel);
1778 #endif /* DEBUG_X_COLORS */
1780 return rc;
1784 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1785 exact match can't be allocated, try the nearest color available.
1786 Value is non-zero if successful. Set *COLOR to the color
1787 allocated. */
1790 x_alloc_nearest_color (f, cmap, color)
1791 struct frame *f;
1792 Colormap cmap;
1793 XColor *color;
1795 gamma_correct (f, color);
1796 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1800 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1801 It's necessary to do this instead of just using PIXEL directly to
1802 get color reference counts right. */
1804 unsigned long
1805 x_copy_color (f, pixel)
1806 struct frame *f;
1807 unsigned long pixel;
1809 XColor color;
1811 color.pixel = pixel;
1812 BLOCK_INPUT;
1813 x_query_color (f, &color);
1814 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1815 UNBLOCK_INPUT;
1816 #ifdef DEBUG_X_COLORS
1817 register_color (pixel);
1818 #endif
1819 return color.pixel;
1823 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1824 It's necessary to do this instead of just using PIXEL directly to
1825 get color reference counts right. */
1827 unsigned long
1828 x_copy_dpy_color (dpy, cmap, pixel)
1829 Display *dpy;
1830 Colormap cmap;
1831 unsigned long pixel;
1833 XColor color;
1835 color.pixel = pixel;
1836 BLOCK_INPUT;
1837 XQueryColor (dpy, cmap, &color);
1838 XAllocColor (dpy, cmap, &color);
1839 UNBLOCK_INPUT;
1840 #ifdef DEBUG_X_COLORS
1841 register_color (pixel);
1842 #endif
1843 return color.pixel;
1847 /* Brightness beyond which a color won't have its highlight brightness
1848 boosted.
1850 Nominally, highlight colors for `3d' faces are calculated by
1851 brightening an object's color by a constant scale factor, but this
1852 doesn't yield good results for dark colors, so for colors who's
1853 brightness is less than this value (on a scale of 0-65535) have an
1854 use an additional additive factor.
1856 The value here is set so that the default menu-bar/mode-line color
1857 (grey75) will not have its highlights changed at all. */
1858 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1861 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1862 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1863 If this produces the same color as PIXEL, try a color where all RGB
1864 values have DELTA added. Return the allocated color in *PIXEL.
1865 DISPLAY is the X display, CMAP is the colormap to operate on.
1866 Value is non-zero if successful. */
1868 static int
1869 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
1870 struct frame *f;
1871 Display *display;
1872 Colormap cmap;
1873 unsigned long *pixel;
1874 double factor;
1875 int delta;
1877 XColor color, new;
1878 long bright;
1879 int success_p;
1881 /* Get RGB color values. */
1882 color.pixel = *pixel;
1883 x_query_color (f, &color);
1885 /* Change RGB values by specified FACTOR. Avoid overflow! */
1886 xassert (factor >= 0);
1887 new.red = min (0xffff, factor * color.red);
1888 new.green = min (0xffff, factor * color.green);
1889 new.blue = min (0xffff, factor * color.blue);
1891 /* Calculate brightness of COLOR. */
1892 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1894 /* We only boost colors that are darker than
1895 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1896 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1897 /* Make an additive adjustment to NEW, because it's dark enough so
1898 that scaling by FACTOR alone isn't enough. */
1900 /* How far below the limit this color is (0 - 1, 1 being darker). */
1901 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1902 /* The additive adjustment. */
1903 int min_delta = delta * dimness * factor / 2;
1905 if (factor < 1)
1907 new.red = max (0, new.red - min_delta);
1908 new.green = max (0, new.green - min_delta);
1909 new.blue = max (0, new.blue - min_delta);
1911 else
1913 new.red = min (0xffff, min_delta + new.red);
1914 new.green = min (0xffff, min_delta + new.green);
1915 new.blue = min (0xffff, min_delta + new.blue);
1919 /* Try to allocate the color. */
1920 success_p = x_alloc_nearest_color (f, cmap, &new);
1921 if (success_p)
1923 if (new.pixel == *pixel)
1925 /* If we end up with the same color as before, try adding
1926 delta to the RGB values. */
1927 x_free_colors (f, &new.pixel, 1);
1929 new.red = min (0xffff, delta + color.red);
1930 new.green = min (0xffff, delta + color.green);
1931 new.blue = min (0xffff, delta + color.blue);
1932 success_p = x_alloc_nearest_color (f, cmap, &new);
1934 else
1935 success_p = 1;
1936 *pixel = new.pixel;
1939 return success_p;
1943 /* Set up the foreground color for drawing relief lines of glyph
1944 string S. RELIEF is a pointer to a struct relief containing the GC
1945 with which lines will be drawn. Use a color that is FACTOR or
1946 DELTA lighter or darker than the relief's background which is found
1947 in S->f->output_data.x->relief_background. If such a color cannot
1948 be allocated, use DEFAULT_PIXEL, instead. */
1950 static void
1951 x_setup_relief_color (f, relief, factor, delta, default_pixel)
1952 struct frame *f;
1953 struct relief *relief;
1954 double factor;
1955 int delta;
1956 unsigned long default_pixel;
1958 XGCValues xgcv;
1959 struct x_output *di = f->output_data.x;
1960 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1961 unsigned long pixel;
1962 unsigned long background = di->relief_background;
1963 Colormap cmap = FRAME_X_COLORMAP (f);
1964 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1965 Display *dpy = FRAME_X_DISPLAY (f);
1967 xgcv.graphics_exposures = False;
1968 xgcv.line_width = 1;
1970 /* Free previously allocated color. The color cell will be reused
1971 when it has been freed as many times as it was allocated, so this
1972 doesn't affect faces using the same colors. */
1973 if (relief->gc
1974 && relief->allocated_p)
1976 x_free_colors (f, &relief->pixel, 1);
1977 relief->allocated_p = 0;
1980 /* Allocate new color. */
1981 xgcv.foreground = default_pixel;
1982 pixel = background;
1983 if (dpyinfo->n_planes != 1
1984 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1986 relief->allocated_p = 1;
1987 xgcv.foreground = relief->pixel = pixel;
1990 if (relief->gc == 0)
1992 xgcv.stipple = dpyinfo->gray;
1993 mask |= GCStipple;
1994 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1996 else
1997 XChangeGC (dpy, relief->gc, mask, &xgcv);
2001 /* Set up colors for the relief lines around glyph string S. */
2003 static void
2004 x_setup_relief_colors (s)
2005 struct glyph_string *s;
2007 struct x_output *di = s->f->output_data.x;
2008 unsigned long color;
2010 if (s->face->use_box_color_for_shadows_p)
2011 color = s->face->box_color;
2012 else if (s->first_glyph->type == IMAGE_GLYPH
2013 && s->img->pixmap
2014 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2015 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2016 else
2018 XGCValues xgcv;
2020 /* Get the background color of the face. */
2021 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2022 color = xgcv.background;
2025 if (di->white_relief.gc == 0
2026 || color != di->relief_background)
2028 di->relief_background = color;
2029 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2030 WHITE_PIX_DEFAULT (s->f));
2031 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2032 BLACK_PIX_DEFAULT (s->f));
2037 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2038 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2039 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2040 relief. LEFT_P non-zero means draw a relief on the left side of
2041 the rectangle. RIGHT_P non-zero means draw a relief on the right
2042 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2043 when drawing. */
2045 static void
2046 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2047 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2048 struct frame *f;
2049 int left_x, top_y, right_x, bottom_y, width;
2050 int top_p, bot_p, left_p, right_p, raised_p;
2051 XRectangle *clip_rect;
2053 Display *dpy = FRAME_X_DISPLAY (f);
2054 Window window = FRAME_X_WINDOW (f);
2055 int i;
2056 GC gc;
2058 if (raised_p)
2059 gc = f->output_data.x->white_relief.gc;
2060 else
2061 gc = f->output_data.x->black_relief.gc;
2062 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2064 /* Top. */
2065 if (top_p)
2066 for (i = 0; i < width; ++i)
2067 XDrawLine (dpy, window, gc,
2068 left_x + i * left_p, top_y + i,
2069 right_x + 1 - i * right_p, top_y + i);
2071 /* Left. */
2072 if (left_p)
2073 for (i = 0; i < width; ++i)
2074 XDrawLine (dpy, window, gc,
2075 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2077 XSetClipMask (dpy, gc, None);
2078 if (raised_p)
2079 gc = f->output_data.x->black_relief.gc;
2080 else
2081 gc = f->output_data.x->white_relief.gc;
2082 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2084 /* Bottom. */
2085 if (bot_p)
2086 for (i = 0; i < width; ++i)
2087 XDrawLine (dpy, window, gc,
2088 left_x + i * left_p, bottom_y - i,
2089 right_x + 1 - i * right_p, bottom_y - i);
2091 /* Right. */
2092 if (right_p)
2093 for (i = 0; i < width; ++i)
2094 XDrawLine (dpy, window, gc,
2095 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2097 XSetClipMask (dpy, gc, None);
2101 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2102 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2103 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2104 left side of the rectangle. RIGHT_P non-zero means draw a line
2105 on the right side of the rectangle. CLIP_RECT is the clipping
2106 rectangle to use when drawing. */
2108 static void
2109 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2110 left_p, right_p, clip_rect)
2111 struct glyph_string *s;
2112 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2113 XRectangle *clip_rect;
2115 XGCValues xgcv;
2117 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2118 XSetForeground (s->display, s->gc, s->face->box_color);
2119 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2121 /* Top. */
2122 XFillRectangle (s->display, s->window, s->gc,
2123 left_x, top_y, right_x - left_x + 1, width);
2125 /* Left. */
2126 if (left_p)
2127 XFillRectangle (s->display, s->window, s->gc,
2128 left_x, top_y, width, bottom_y - top_y + 1);
2130 /* Bottom. */
2131 XFillRectangle (s->display, s->window, s->gc,
2132 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2134 /* Right. */
2135 if (right_p)
2136 XFillRectangle (s->display, s->window, s->gc,
2137 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2139 XSetForeground (s->display, s->gc, xgcv.foreground);
2140 XSetClipMask (s->display, s->gc, None);
2144 /* Draw a box around glyph string S. */
2146 static void
2147 x_draw_glyph_string_box (s)
2148 struct glyph_string *s;
2150 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2151 int left_p, right_p;
2152 struct glyph *last_glyph;
2153 XRectangle clip_rect;
2155 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2156 ? WINDOW_RIGHT_EDGE_X (s->w)
2157 : window_box_right (s->w, s->area));
2159 /* The glyph that may have a right box line. */
2160 last_glyph = (s->cmp || s->img
2161 ? s->first_glyph
2162 : s->first_glyph + s->nchars - 1);
2164 width = eabs (s->face->box_line_width);
2165 raised_p = s->face->box == FACE_RAISED_BOX;
2166 left_x = s->x;
2167 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2168 ? last_x - 1
2169 : min (last_x, s->x + s->background_width) - 1);
2170 top_y = s->y;
2171 bottom_y = top_y + s->height - 1;
2173 left_p = (s->first_glyph->left_box_line_p
2174 || (s->hl == DRAW_MOUSE_FACE
2175 && (s->prev == NULL
2176 || s->prev->hl != s->hl)));
2177 right_p = (last_glyph->right_box_line_p
2178 || (s->hl == DRAW_MOUSE_FACE
2179 && (s->next == NULL
2180 || s->next->hl != s->hl)));
2182 get_glyph_string_clip_rect (s, &clip_rect);
2184 if (s->face->box == FACE_SIMPLE_BOX)
2185 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2186 left_p, right_p, &clip_rect);
2187 else
2189 x_setup_relief_colors (s);
2190 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2191 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2196 /* Draw foreground of image glyph string S. */
2198 static void
2199 x_draw_image_foreground (s)
2200 struct glyph_string *s;
2202 int x = s->x;
2203 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2205 /* If first glyph of S has a left box line, start drawing it to the
2206 right of that line. */
2207 if (s->face->box != FACE_NO_BOX
2208 && s->first_glyph->left_box_line_p
2209 && s->slice.x == 0)
2210 x += eabs (s->face->box_line_width);
2212 /* If there is a margin around the image, adjust x- and y-position
2213 by that margin. */
2214 if (s->slice.x == 0)
2215 x += s->img->hmargin;
2216 if (s->slice.y == 0)
2217 y += s->img->vmargin;
2219 if (s->img->pixmap)
2221 if (s->img->mask)
2223 /* We can't set both a clip mask and use XSetClipRectangles
2224 because the latter also sets a clip mask. We also can't
2225 trust on the shape extension to be available
2226 (XShapeCombineRegion). So, compute the rectangle to draw
2227 manually. */
2228 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2229 | GCFunction);
2230 XGCValues xgcv;
2231 XRectangle clip_rect, image_rect, r;
2233 xgcv.clip_mask = s->img->mask;
2234 xgcv.clip_x_origin = x;
2235 xgcv.clip_y_origin = y;
2236 xgcv.function = GXcopy;
2237 XChangeGC (s->display, s->gc, mask, &xgcv);
2239 get_glyph_string_clip_rect (s, &clip_rect);
2240 image_rect.x = x;
2241 image_rect.y = y;
2242 image_rect.width = s->slice.width;
2243 image_rect.height = s->slice.height;
2244 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2245 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2246 s->slice.x + r.x - x, s->slice.y + r.y - y,
2247 r.width, r.height, r.x, r.y);
2249 else
2251 XRectangle clip_rect, image_rect, r;
2253 get_glyph_string_clip_rect (s, &clip_rect);
2254 image_rect.x = x;
2255 image_rect.y = y;
2256 image_rect.width = s->slice.width;
2257 image_rect.height = s->slice.height;
2258 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2259 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2260 s->slice.x + r.x - x, s->slice.y + r.y - y,
2261 r.width, r.height, r.x, r.y);
2263 /* When the image has a mask, we can expect that at
2264 least part of a mouse highlight or a block cursor will
2265 be visible. If the image doesn't have a mask, make
2266 a block cursor visible by drawing a rectangle around
2267 the image. I believe it's looking better if we do
2268 nothing here for mouse-face. */
2269 if (s->hl == DRAW_CURSOR)
2271 int r = s->img->relief;
2272 if (r < 0) r = -r;
2273 XDrawRectangle (s->display, s->window, s->gc,
2274 x - r, y - r,
2275 s->slice.width + r*2 - 1,
2276 s->slice.height + r*2 - 1);
2280 else
2281 /* Draw a rectangle if image could not be loaded. */
2282 XDrawRectangle (s->display, s->window, s->gc, x, y,
2283 s->slice.width - 1, s->slice.height - 1);
2287 /* Draw a relief around the image glyph string S. */
2289 static void
2290 x_draw_image_relief (s)
2291 struct glyph_string *s;
2293 int x0, y0, x1, y1, thick, raised_p;
2294 XRectangle r;
2295 int x = s->x;
2296 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2298 /* If first glyph of S has a left box line, start drawing it to the
2299 right of that line. */
2300 if (s->face->box != FACE_NO_BOX
2301 && s->first_glyph->left_box_line_p
2302 && s->slice.x == 0)
2303 x += eabs (s->face->box_line_width);
2305 /* If there is a margin around the image, adjust x- and y-position
2306 by that margin. */
2307 if (s->slice.x == 0)
2308 x += s->img->hmargin;
2309 if (s->slice.y == 0)
2310 y += s->img->vmargin;
2312 if (s->hl == DRAW_IMAGE_SUNKEN
2313 || s->hl == DRAW_IMAGE_RAISED)
2315 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2316 raised_p = s->hl == DRAW_IMAGE_RAISED;
2318 else
2320 thick = eabs (s->img->relief);
2321 raised_p = s->img->relief > 0;
2324 x0 = x - thick;
2325 y0 = y - thick;
2326 x1 = x + s->slice.width + thick - 1;
2327 y1 = y + s->slice.height + thick - 1;
2329 x_setup_relief_colors (s);
2330 get_glyph_string_clip_rect (s, &r);
2331 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2332 s->slice.y == 0,
2333 s->slice.y + s->slice.height == s->img->height,
2334 s->slice.x == 0,
2335 s->slice.x + s->slice.width == s->img->width,
2336 &r);
2340 /* Draw the foreground of image glyph string S to PIXMAP. */
2342 static void
2343 x_draw_image_foreground_1 (s, pixmap)
2344 struct glyph_string *s;
2345 Pixmap pixmap;
2347 int x = 0;
2348 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2350 /* If first glyph of S has a left box line, start drawing it to the
2351 right of that line. */
2352 if (s->face->box != FACE_NO_BOX
2353 && s->first_glyph->left_box_line_p
2354 && s->slice.x == 0)
2355 x += eabs (s->face->box_line_width);
2357 /* If there is a margin around the image, adjust x- and y-position
2358 by that margin. */
2359 if (s->slice.x == 0)
2360 x += s->img->hmargin;
2361 if (s->slice.y == 0)
2362 y += s->img->vmargin;
2364 if (s->img->pixmap)
2366 if (s->img->mask)
2368 /* We can't set both a clip mask and use XSetClipRectangles
2369 because the latter also sets a clip mask. We also can't
2370 trust on the shape extension to be available
2371 (XShapeCombineRegion). So, compute the rectangle to draw
2372 manually. */
2373 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2374 | GCFunction);
2375 XGCValues xgcv;
2377 xgcv.clip_mask = s->img->mask;
2378 xgcv.clip_x_origin = x - s->slice.x;
2379 xgcv.clip_y_origin = y - s->slice.y;
2380 xgcv.function = GXcopy;
2381 XChangeGC (s->display, s->gc, mask, &xgcv);
2383 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2384 s->slice.x, s->slice.y,
2385 s->slice.width, s->slice.height, x, y);
2386 XSetClipMask (s->display, s->gc, None);
2388 else
2390 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2391 s->slice.x, s->slice.y,
2392 s->slice.width, s->slice.height, x, y);
2394 /* When the image has a mask, we can expect that at
2395 least part of a mouse highlight or a block cursor will
2396 be visible. If the image doesn't have a mask, make
2397 a block cursor visible by drawing a rectangle around
2398 the image. I believe it's looking better if we do
2399 nothing here for mouse-face. */
2400 if (s->hl == DRAW_CURSOR)
2402 int r = s->img->relief;
2403 if (r < 0) r = -r;
2404 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2405 s->slice.width + r*2 - 1,
2406 s->slice.height + r*2 - 1);
2410 else
2411 /* Draw a rectangle if image could not be loaded. */
2412 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2413 s->slice.width - 1, s->slice.height - 1);
2417 /* Draw part of the background of glyph string S. X, Y, W, and H
2418 give the rectangle to draw. */
2420 static void
2421 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2422 struct glyph_string *s;
2423 int x, y, w, h;
2425 if (s->stippled_p)
2427 /* Fill background with a stipple pattern. */
2428 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2429 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2430 XSetFillStyle (s->display, s->gc, FillSolid);
2432 else
2433 x_clear_glyph_string_rect (s, x, y, w, h);
2437 /* Draw image glyph string S.
2439 s->y
2440 s->x +-------------------------
2441 | s->face->box
2443 | +-------------------------
2444 | | s->img->margin
2446 | | +-------------------
2447 | | | the image
2451 static void
2452 x_draw_image_glyph_string (s)
2453 struct glyph_string *s;
2455 int box_line_hwidth = eabs (s->face->box_line_width);
2456 int box_line_vwidth = max (s->face->box_line_width, 0);
2457 int height;
2458 Pixmap pixmap = None;
2460 height = s->height;
2461 if (s->slice.y == 0)
2462 height -= box_line_vwidth;
2463 if (s->slice.y + s->slice.height >= s->img->height)
2464 height -= box_line_vwidth;
2466 /* Fill background with face under the image. Do it only if row is
2467 taller than image or if image has a clip mask to reduce
2468 flickering. */
2469 s->stippled_p = s->face->stipple != 0;
2470 if (height > s->slice.height
2471 || s->img->hmargin
2472 || s->img->vmargin
2473 || s->img->mask
2474 || s->img->pixmap == 0
2475 || s->width != s->background_width)
2477 if (s->img->mask)
2479 /* Create a pixmap as large as the glyph string. Fill it
2480 with the background color. Copy the image to it, using
2481 its mask. Copy the temporary pixmap to the display. */
2482 Screen *screen = FRAME_X_SCREEN (s->f);
2483 int depth = DefaultDepthOfScreen (screen);
2485 /* Create a pixmap as large as the glyph string. */
2486 pixmap = XCreatePixmap (s->display, s->window,
2487 s->background_width,
2488 s->height, depth);
2490 /* Don't clip in the following because we're working on the
2491 pixmap. */
2492 XSetClipMask (s->display, s->gc, None);
2494 /* Fill the pixmap with the background color/stipple. */
2495 if (s->stippled_p)
2497 /* Fill background with a stipple pattern. */
2498 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2499 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2500 XFillRectangle (s->display, pixmap, s->gc,
2501 0, 0, s->background_width, s->height);
2502 XSetFillStyle (s->display, s->gc, FillSolid);
2503 XSetTSOrigin (s->display, s->gc, 0, 0);
2505 else
2507 XGCValues xgcv;
2508 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2509 &xgcv);
2510 XSetForeground (s->display, s->gc, xgcv.background);
2511 XFillRectangle (s->display, pixmap, s->gc,
2512 0, 0, s->background_width, s->height);
2513 XSetForeground (s->display, s->gc, xgcv.foreground);
2516 else
2518 int x = s->x;
2519 int y = s->y;
2521 if (s->first_glyph->left_box_line_p
2522 && s->slice.x == 0)
2523 x += box_line_hwidth;
2525 if (s->slice.y == 0)
2526 y += box_line_vwidth;
2528 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2531 s->background_filled_p = 1;
2534 /* Draw the foreground. */
2535 if (pixmap != None)
2537 x_draw_image_foreground_1 (s, pixmap);
2538 x_set_glyph_string_clipping (s);
2539 XCopyArea (s->display, pixmap, s->window, s->gc,
2540 0, 0, s->background_width, s->height, s->x, s->y);
2541 XFreePixmap (s->display, pixmap);
2543 else
2544 x_draw_image_foreground (s);
2546 /* If we must draw a relief around the image, do it. */
2547 if (s->img->relief
2548 || s->hl == DRAW_IMAGE_RAISED
2549 || s->hl == DRAW_IMAGE_SUNKEN)
2550 x_draw_image_relief (s);
2554 /* Draw stretch glyph string S. */
2556 static void
2557 x_draw_stretch_glyph_string (s)
2558 struct glyph_string *s;
2560 xassert (s->first_glyph->type == STRETCH_GLYPH);
2562 if (s->hl == DRAW_CURSOR
2563 && !x_stretch_cursor_p)
2565 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2566 as wide as the stretch glyph. */
2567 int width, background_width = s->background_width;
2568 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2570 if (x < left_x)
2572 background_width -= left_x - x;
2573 x = left_x;
2575 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2577 /* Draw cursor. */
2578 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2580 /* Clear rest using the GC of the original non-cursor face. */
2581 if (width < background_width)
2583 int y = s->y;
2584 int w = background_width - width, h = s->height;
2585 XRectangle r;
2586 GC gc;
2588 x += width;
2589 if (s->row->mouse_face_p
2590 && cursor_in_mouse_face_p (s->w))
2592 x_set_mouse_face_gc (s);
2593 gc = s->gc;
2595 else
2596 gc = s->face->gc;
2598 get_glyph_string_clip_rect (s, &r);
2599 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2601 if (s->face->stipple)
2603 /* Fill background with a stipple pattern. */
2604 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2605 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2606 XSetFillStyle (s->display, gc, FillSolid);
2608 else
2610 XGCValues xgcv;
2611 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2612 XSetForeground (s->display, gc, xgcv.background);
2613 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2614 XSetForeground (s->display, gc, xgcv.foreground);
2618 else if (!s->background_filled_p)
2620 int background_width = s->background_width;
2621 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2623 /* Don't draw into left margin, fringe or scrollbar area
2624 except for header line and mode line. */
2625 if (x < left_x && !s->row->mode_line_p)
2627 background_width -= left_x - x;
2628 x = left_x;
2630 if (background_width > 0)
2631 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2634 s->background_filled_p = 1;
2638 /* Draw glyph string S. */
2640 static void
2641 x_draw_glyph_string (s)
2642 struct glyph_string *s;
2644 int relief_drawn_p = 0;
2646 /* If S draws into the background of its successors, draw the
2647 background of the successors first so that S can draw into it.
2648 This makes S->next use XDrawString instead of XDrawImageString. */
2649 if (s->next && s->right_overhang && !s->for_overlaps)
2651 int width;
2652 struct glyph_string *next;
2654 for (width = 0, next = s->next;
2655 next && width < s->right_overhang;
2656 width += next->width, next = next->next)
2657 if (next->first_glyph->type != IMAGE_GLYPH)
2659 x_set_glyph_string_gc (next);
2660 x_set_glyph_string_clipping (next);
2661 if (next->first_glyph->type == STRETCH_GLYPH)
2662 x_draw_stretch_glyph_string (next);
2663 else
2664 x_draw_glyph_string_background (next, 1);
2665 next->num_clips = 0;
2669 /* Set up S->gc, set clipping and draw S. */
2670 x_set_glyph_string_gc (s);
2672 /* Draw relief (if any) in advance for char/composition so that the
2673 glyph string can be drawn over it. */
2674 if (!s->for_overlaps
2675 && s->face->box != FACE_NO_BOX
2676 && (s->first_glyph->type == CHAR_GLYPH
2677 || s->first_glyph->type == COMPOSITE_GLYPH))
2680 x_set_glyph_string_clipping (s);
2681 x_draw_glyph_string_background (s, 1);
2682 x_draw_glyph_string_box (s);
2683 x_set_glyph_string_clipping (s);
2684 relief_drawn_p = 1;
2686 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2687 && !s->clip_tail
2688 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2689 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2690 /* We must clip just this glyph. left_overhang part has already
2691 drawn when s->prev was drawn, and right_overhang part will be
2692 drawn later when s->next is drawn. */
2693 x_set_glyph_string_clipping_exactly (s, s);
2694 else
2695 x_set_glyph_string_clipping (s);
2697 switch (s->first_glyph->type)
2699 case IMAGE_GLYPH:
2700 x_draw_image_glyph_string (s);
2701 break;
2703 case STRETCH_GLYPH:
2704 x_draw_stretch_glyph_string (s);
2705 break;
2707 case CHAR_GLYPH:
2708 if (s->for_overlaps)
2709 s->background_filled_p = 1;
2710 else
2711 x_draw_glyph_string_background (s, 0);
2712 x_draw_glyph_string_foreground (s);
2713 break;
2715 case COMPOSITE_GLYPH:
2716 if (s->for_overlaps || (s->cmp_from > 0
2717 && ! s->first_glyph->u.cmp.automatic))
2718 s->background_filled_p = 1;
2719 else
2720 x_draw_glyph_string_background (s, 1);
2721 x_draw_composite_glyph_string_foreground (s);
2722 break;
2724 default:
2725 abort ();
2728 if (!s->for_overlaps)
2730 /* Draw underline. */
2731 if (s->face->underline_p)
2733 unsigned long thickness, position;
2734 int y;
2736 if (s->prev && s->prev->face->underline_p)
2738 /* We use the same underline style as the previous one. */
2739 thickness = s->prev->underline_thickness;
2740 position = s->prev->underline_position;
2742 else
2744 /* Get the underline thickness. Default is 1 pixel. */
2745 if (s->font && s->font->underline_thickness > 0)
2746 thickness = s->font->underline_thickness;
2747 else
2748 thickness = 1;
2749 if (x_underline_at_descent_line)
2750 position = (s->height - thickness) - (s->ybase - s->y);
2751 else
2753 /* Get the underline position. This is the recommended
2754 vertical offset in pixels from the baseline to the top of
2755 the underline. This is a signed value according to the
2756 specs, and its default is
2758 ROUND ((maximum descent) / 2), with
2759 ROUND(x) = floor (x + 0.5) */
2761 if (x_use_underline_position_properties
2762 && s->font && s->font->underline_position >= 0)
2763 position = s->font->underline_position;
2764 else if (s->font)
2765 position = (s->font->descent + 1) / 2;
2766 else
2767 position = underline_minimum_offset;
2769 position = max (position, underline_minimum_offset);
2771 /* Check the sanity of thickness and position. We should
2772 avoid drawing underline out of the current line area. */
2773 if (s->y + s->height <= s->ybase + position)
2774 position = (s->height - 1) - (s->ybase - s->y);
2775 if (s->y + s->height < s->ybase + position + thickness)
2776 thickness = (s->y + s->height) - (s->ybase + position);
2777 s->underline_thickness = thickness;
2778 s->underline_position = position;
2779 y = s->ybase + position;
2780 if (s->face->underline_defaulted_p)
2781 XFillRectangle (s->display, s->window, s->gc,
2782 s->x, y, s->width, thickness);
2783 else
2785 XGCValues xgcv;
2786 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2787 XSetForeground (s->display, s->gc, s->face->underline_color);
2788 XFillRectangle (s->display, s->window, s->gc,
2789 s->x, y, s->width, thickness);
2790 XSetForeground (s->display, s->gc, xgcv.foreground);
2794 /* Draw overline. */
2795 if (s->face->overline_p)
2797 unsigned long dy = 0, h = 1;
2799 if (s->face->overline_color_defaulted_p)
2800 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2801 s->width, h);
2802 else
2804 XGCValues xgcv;
2805 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2806 XSetForeground (s->display, s->gc, s->face->overline_color);
2807 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2808 s->width, h);
2809 XSetForeground (s->display, s->gc, xgcv.foreground);
2813 /* Draw strike-through. */
2814 if (s->face->strike_through_p)
2816 unsigned long h = 1;
2817 unsigned long dy = (s->height - h) / 2;
2819 if (s->face->strike_through_color_defaulted_p)
2820 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2821 s->width, h);
2822 else
2824 XGCValues xgcv;
2825 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2826 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2827 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2828 s->width, h);
2829 XSetForeground (s->display, s->gc, xgcv.foreground);
2833 /* Draw relief if not yet drawn. */
2834 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2835 x_draw_glyph_string_box (s);
2837 if (s->prev)
2839 struct glyph_string *prev;
2841 for (prev = s->prev; prev; prev = prev->prev)
2842 if (prev->hl != s->hl
2843 && prev->x + prev->width + prev->right_overhang > s->x)
2845 /* As prev was drawn while clipped to its own area, we
2846 must draw the right_overhang part using s->hl now. */
2847 enum draw_glyphs_face save = prev->hl;
2849 prev->hl = s->hl;
2850 x_set_glyph_string_gc (prev);
2851 x_set_glyph_string_clipping_exactly (s, prev);
2852 if (prev->first_glyph->type == CHAR_GLYPH)
2853 x_draw_glyph_string_foreground (prev);
2854 else
2855 x_draw_composite_glyph_string_foreground (prev);
2856 XSetClipMask (prev->display, prev->gc, None);
2857 prev->hl = save;
2858 prev->num_clips = 0;
2862 if (s->next)
2864 struct glyph_string *next;
2866 for (next = s->next; next; next = next->next)
2867 if (next->hl != s->hl
2868 && next->x - next->left_overhang < s->x + s->width)
2870 /* As next will be drawn while clipped to its own area,
2871 we must draw the left_overhang part using s->hl now. */
2872 enum draw_glyphs_face save = next->hl;
2874 next->hl = s->hl;
2875 x_set_glyph_string_gc (next);
2876 x_set_glyph_string_clipping_exactly (s, next);
2877 if (next->first_glyph->type == CHAR_GLYPH)
2878 x_draw_glyph_string_foreground (next);
2879 else
2880 x_draw_composite_glyph_string_foreground (next);
2881 XSetClipMask (next->display, next->gc, None);
2882 next->hl = save;
2883 next->num_clips = 0;
2888 /* Reset clipping. */
2889 XSetClipMask (s->display, s->gc, None);
2890 s->num_clips = 0;
2893 /* Shift display to make room for inserted glyphs. */
2895 void
2896 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
2897 struct frame *f;
2898 int x, y, width, height, shift_by;
2900 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2901 f->output_data.x->normal_gc,
2902 x, y, width, height,
2903 x + shift_by, y);
2906 /* Delete N glyphs at the nominal cursor position. Not implemented
2907 for X frames. */
2909 static void
2910 x_delete_glyphs (f, n)
2911 struct frame *f;
2912 register int n;
2914 abort ();
2918 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2919 If they are <= 0, this is probably an error. */
2921 void
2922 x_clear_area (dpy, window, x, y, width, height, exposures)
2923 Display *dpy;
2924 Window window;
2925 int x, y;
2926 int width, height;
2927 int exposures;
2929 xassert (width > 0 && height > 0);
2930 XClearArea (dpy, window, x, y, width, height, exposures);
2934 /* Clear an entire frame. */
2936 static void
2937 x_clear_frame (struct frame *f)
2939 /* Clearing the frame will erase any cursor, so mark them all as no
2940 longer visible. */
2941 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2942 output_cursor.hpos = output_cursor.vpos = 0;
2943 output_cursor.x = -1;
2945 /* We don't set the output cursor here because there will always
2946 follow an explicit cursor_to. */
2947 BLOCK_INPUT;
2948 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2950 /* We have to clear the scroll bars, too. If we have changed
2951 colors or something like that, then they should be notified. */
2952 x_scroll_bar_clear (f);
2954 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2955 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2956 redisplay, do it here. */
2957 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2958 #endif
2960 XFlush (FRAME_X_DISPLAY (f));
2962 UNBLOCK_INPUT;
2967 /* Invert the middle quarter of the frame for .15 sec. */
2969 /* We use the select system call to do the waiting, so we have to make
2970 sure it's available. If it isn't, we just won't do visual bells. */
2972 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
2975 /* Subtract the `struct timeval' values X and Y, storing the result in
2976 *RESULT. Return 1 if the difference is negative, otherwise 0. */
2978 static int
2979 timeval_subtract (result, x, y)
2980 struct timeval *result, x, y;
2982 /* Perform the carry for the later subtraction by updating y. This
2983 is safer because on some systems the tv_sec member is unsigned. */
2984 if (x.tv_usec < y.tv_usec)
2986 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
2987 y.tv_usec -= 1000000 * nsec;
2988 y.tv_sec += nsec;
2991 if (x.tv_usec - y.tv_usec > 1000000)
2993 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
2994 y.tv_usec += 1000000 * nsec;
2995 y.tv_sec -= nsec;
2998 /* Compute the time remaining to wait. tv_usec is certainly
2999 positive. */
3000 result->tv_sec = x.tv_sec - y.tv_sec;
3001 result->tv_usec = x.tv_usec - y.tv_usec;
3003 /* Return indication of whether the result should be considered
3004 negative. */
3005 return x.tv_sec < y.tv_sec;
3008 void
3009 XTflash (f)
3010 struct frame *f;
3012 BLOCK_INPUT;
3015 GC gc;
3017 /* Create a GC that will use the GXxor function to flip foreground
3018 pixels into background pixels. */
3020 XGCValues values;
3022 values.function = GXxor;
3023 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3024 ^ FRAME_BACKGROUND_PIXEL (f));
3026 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3027 GCFunction | GCForeground, &values);
3031 /* Get the height not including a menu bar widget. */
3032 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
3033 /* Height of each line to flash. */
3034 int flash_height = FRAME_LINE_HEIGHT (f);
3035 /* These will be the left and right margins of the rectangles. */
3036 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3037 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3039 int width;
3041 /* Don't flash the area between a scroll bar and the frame
3042 edge it is next to. */
3043 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3045 case vertical_scroll_bar_left:
3046 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3047 break;
3049 case vertical_scroll_bar_right:
3050 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3051 break;
3053 default:
3054 break;
3057 width = flash_right - flash_left;
3059 /* If window is tall, flash top and bottom line. */
3060 if (height > 3 * FRAME_LINE_HEIGHT (f))
3062 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3063 flash_left,
3064 (FRAME_INTERNAL_BORDER_WIDTH (f)
3065 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3066 width, flash_height);
3067 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3068 flash_left,
3069 (height - flash_height
3070 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3071 width, flash_height);
3073 else
3074 /* If it is short, flash it all. */
3075 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3076 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3077 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3079 x_flush (f);
3082 struct timeval wakeup;
3084 EMACS_GET_TIME (wakeup);
3086 /* Compute time to wait until, propagating carry from usecs. */
3087 wakeup.tv_usec += 150000;
3088 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3089 wakeup.tv_usec %= 1000000;
3091 /* Keep waiting until past the time wakeup or any input gets
3092 available. */
3093 while (! detect_input_pending ())
3095 struct timeval current;
3096 struct timeval timeout;
3098 EMACS_GET_TIME (current);
3100 /* Break if result would be negative. */
3101 if (timeval_subtract (&current, wakeup, current))
3102 break;
3104 /* How long `select' should wait. */
3105 timeout.tv_sec = 0;
3106 timeout.tv_usec = 10000;
3108 /* Try to wait that long--but we might wake up sooner. */
3109 select (0, NULL, NULL, NULL, &timeout);
3113 /* If window is tall, flash top and bottom line. */
3114 if (height > 3 * FRAME_LINE_HEIGHT (f))
3116 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3117 flash_left,
3118 (FRAME_INTERNAL_BORDER_WIDTH (f)
3119 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3120 width, flash_height);
3121 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3122 flash_left,
3123 (height - flash_height
3124 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3125 width, flash_height);
3127 else
3128 /* If it is short, flash it all. */
3129 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3130 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3131 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3133 XFreeGC (FRAME_X_DISPLAY (f), gc);
3134 x_flush (f);
3138 UNBLOCK_INPUT;
3141 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3144 static void
3145 XTtoggle_invisible_pointer (f, invisible)
3146 FRAME_PTR f;
3147 int invisible;
3149 BLOCK_INPUT;
3150 if (invisible)
3152 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor != 0)
3153 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3154 FRAME_X_DISPLAY_INFO (f)->invisible_cursor);
3156 else
3157 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3158 f->output_data.x->current_cursor);
3159 f->pointer_invisible = invisible;
3160 UNBLOCK_INPUT;
3164 /* Make audible bell. */
3166 void
3167 XTring_bell ()
3169 struct frame *f = SELECTED_FRAME ();
3171 if (FRAME_X_DISPLAY (f))
3173 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3174 if (visible_bell)
3175 XTflash (f);
3176 else
3177 #endif
3179 BLOCK_INPUT;
3180 XBell (FRAME_X_DISPLAY (f), 0);
3181 XFlush (FRAME_X_DISPLAY (f));
3182 UNBLOCK_INPUT;
3188 /* Specify how many text lines, from the top of the window,
3189 should be affected by insert-lines and delete-lines operations.
3190 This, and those operations, are used only within an update
3191 that is bounded by calls to x_update_begin and x_update_end. */
3193 static void
3194 XTset_terminal_window (n)
3195 register int n;
3197 /* This function intentionally left blank. */
3202 /***********************************************************************
3203 Line Dance
3204 ***********************************************************************/
3206 /* Perform an insert-lines or delete-lines operation, inserting N
3207 lines or deleting -N lines at vertical position VPOS. */
3209 static void
3210 x_ins_del_lines (f, vpos, n)
3211 struct frame *f;
3212 int vpos, n;
3214 abort ();
3218 /* Scroll part of the display as described by RUN. */
3220 static void
3221 x_scroll_run (w, run)
3222 struct window *w;
3223 struct run *run;
3225 struct frame *f = XFRAME (w->frame);
3226 int x, y, width, height, from_y, to_y, bottom_y;
3228 /* Get frame-relative bounding box of the text display area of W,
3229 without mode lines. Include in this box the left and right
3230 fringe of W. */
3231 window_box (w, -1, &x, &y, &width, &height);
3233 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3234 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3235 bottom_y = y + height;
3237 if (to_y < from_y)
3239 /* Scrolling up. Make sure we don't copy part of the mode
3240 line at the bottom. */
3241 if (from_y + run->height > bottom_y)
3242 height = bottom_y - from_y;
3243 else
3244 height = run->height;
3246 else
3248 /* Scolling down. Make sure we don't copy over the mode line.
3249 at the bottom. */
3250 if (to_y + run->height > bottom_y)
3251 height = bottom_y - to_y;
3252 else
3253 height = run->height;
3256 BLOCK_INPUT;
3258 /* Cursor off. Will be switched on again in x_update_window_end. */
3259 updated_window = w;
3260 x_clear_cursor (w);
3262 XCopyArea (FRAME_X_DISPLAY (f),
3263 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3264 f->output_data.x->normal_gc,
3265 x, from_y,
3266 width, height,
3267 x, to_y);
3269 UNBLOCK_INPUT;
3274 /***********************************************************************
3275 Exposure Events
3276 ***********************************************************************/
3279 static void
3280 frame_highlight (f)
3281 struct frame *f;
3283 /* We used to only do this if Vx_no_window_manager was non-nil, but
3284 the ICCCM (section 4.1.6) says that the window's border pixmap
3285 and border pixel are window attributes which are "private to the
3286 client", so we can always change it to whatever we want. */
3287 BLOCK_INPUT;
3288 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3289 f->output_data.x->border_pixel);
3290 UNBLOCK_INPUT;
3291 x_update_cursor (f, 1);
3292 x_set_frame_alpha (f);
3295 static void
3296 frame_unhighlight (f)
3297 struct frame *f;
3299 /* We used to only do this if Vx_no_window_manager was non-nil, but
3300 the ICCCM (section 4.1.6) says that the window's border pixmap
3301 and border pixel are window attributes which are "private to the
3302 client", so we can always change it to whatever we want. */
3303 BLOCK_INPUT;
3304 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3305 f->output_data.x->border_tile);
3306 UNBLOCK_INPUT;
3307 x_update_cursor (f, 1);
3308 x_set_frame_alpha (f);
3311 /* The focus has changed. Update the frames as necessary to reflect
3312 the new situation. Note that we can't change the selected frame
3313 here, because the Lisp code we are interrupting might become confused.
3314 Each event gets marked with the frame in which it occurred, so the
3315 Lisp code can tell when the switch took place by examining the events. */
3317 static void
3318 x_new_focus_frame (dpyinfo, frame)
3319 struct x_display_info *dpyinfo;
3320 struct frame *frame;
3322 struct frame *old_focus = dpyinfo->x_focus_frame;
3324 if (frame != dpyinfo->x_focus_frame)
3326 /* Set this before calling other routines, so that they see
3327 the correct value of x_focus_frame. */
3328 dpyinfo->x_focus_frame = frame;
3330 if (old_focus && old_focus->auto_lower)
3331 x_lower_frame (old_focus);
3333 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3334 pending_autoraise_frame = dpyinfo->x_focus_frame;
3335 else
3336 pending_autoraise_frame = 0;
3339 x_frame_rehighlight (dpyinfo);
3342 /* Handle FocusIn and FocusOut state changes for FRAME.
3343 If FRAME has focus and there exists more than one frame, puts
3344 a FOCUS_IN_EVENT into *BUFP. */
3346 static void
3347 x_focus_changed (type, state, dpyinfo, frame, bufp)
3348 int type;
3349 int state;
3350 struct x_display_info *dpyinfo;
3351 struct frame *frame;
3352 struct input_event *bufp;
3354 if (type == FocusIn)
3356 if (dpyinfo->x_focus_event_frame != frame)
3358 x_new_focus_frame (dpyinfo, frame);
3359 dpyinfo->x_focus_event_frame = frame;
3361 /* Don't stop displaying the initial startup message
3362 for a switch-frame event we don't need. */
3363 if (NILP (Vterminal_frame)
3364 && CONSP (Vframe_list)
3365 && !NILP (XCDR (Vframe_list)))
3367 bufp->kind = FOCUS_IN_EVENT;
3368 XSETFRAME (bufp->frame_or_window, frame);
3372 frame->output_data.x->focus_state |= state;
3374 #ifdef HAVE_X_I18N
3375 if (FRAME_XIC (frame))
3376 XSetICFocus (FRAME_XIC (frame));
3377 #endif
3379 else if (type == FocusOut)
3381 frame->output_data.x->focus_state &= ~state;
3383 if (dpyinfo->x_focus_event_frame == frame)
3385 dpyinfo->x_focus_event_frame = 0;
3386 x_new_focus_frame (dpyinfo, 0);
3389 #ifdef HAVE_X_I18N
3390 if (FRAME_XIC (frame))
3391 XUnsetICFocus (FRAME_XIC (frame));
3392 #endif
3393 if (frame->pointer_invisible)
3394 XTtoggle_invisible_pointer (frame, 0);
3398 /* The focus may have changed. Figure out if it is a real focus change,
3399 by checking both FocusIn/Out and Enter/LeaveNotify events.
3401 Returns FOCUS_IN_EVENT event in *BUFP. */
3403 static void
3404 x_detect_focus_change (dpyinfo, event, bufp)
3405 struct x_display_info *dpyinfo;
3406 XEvent *event;
3407 struct input_event *bufp;
3409 struct frame *frame;
3411 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3412 if (! frame)
3413 return;
3415 switch (event->type)
3417 case EnterNotify:
3418 case LeaveNotify:
3420 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3421 int focus_state
3422 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3424 if (event->xcrossing.detail != NotifyInferior
3425 && event->xcrossing.focus
3426 && ! (focus_state & FOCUS_EXPLICIT))
3427 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3428 FOCUS_IMPLICIT,
3429 dpyinfo, frame, bufp);
3431 break;
3433 case FocusIn:
3434 case FocusOut:
3435 x_focus_changed (event->type,
3436 (event->xfocus.detail == NotifyPointer ?
3437 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3438 dpyinfo, frame, bufp);
3439 break;
3441 case ClientMessage:
3442 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3444 enum xembed_message msg = event->xclient.data.l[1];
3445 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3446 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3448 break;
3453 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3455 void
3456 x_mouse_leave (dpyinfo)
3457 struct x_display_info *dpyinfo;
3459 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3462 /* The focus has changed, or we have redirected a frame's focus to
3463 another frame (this happens when a frame uses a surrogate
3464 mini-buffer frame). Shift the highlight as appropriate.
3466 The FRAME argument doesn't necessarily have anything to do with which
3467 frame is being highlighted or un-highlighted; we only use it to find
3468 the appropriate X display info. */
3470 static void
3471 XTframe_rehighlight (frame)
3472 struct frame *frame;
3474 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3477 static void
3478 x_frame_rehighlight (dpyinfo)
3479 struct x_display_info *dpyinfo;
3481 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3483 if (dpyinfo->x_focus_frame)
3485 dpyinfo->x_highlight_frame
3486 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3487 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3488 : dpyinfo->x_focus_frame);
3489 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3491 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3492 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3495 else
3496 dpyinfo->x_highlight_frame = 0;
3498 if (dpyinfo->x_highlight_frame != old_highlight)
3500 if (old_highlight)
3501 frame_unhighlight (old_highlight);
3502 if (dpyinfo->x_highlight_frame)
3503 frame_highlight (dpyinfo->x_highlight_frame);
3509 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3511 /* Initialize mode_switch_bit and modifier_meaning. */
3512 static void
3513 x_find_modifier_meanings (dpyinfo)
3514 struct x_display_info *dpyinfo;
3516 int min_code, max_code;
3517 KeySym *syms;
3518 int syms_per_code;
3519 XModifierKeymap *mods;
3521 dpyinfo->meta_mod_mask = 0;
3522 dpyinfo->shift_lock_mask = 0;
3523 dpyinfo->alt_mod_mask = 0;
3524 dpyinfo->super_mod_mask = 0;
3525 dpyinfo->hyper_mod_mask = 0;
3527 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3529 syms = XGetKeyboardMapping (dpyinfo->display,
3530 min_code, max_code - min_code + 1,
3531 &syms_per_code);
3532 mods = XGetModifierMapping (dpyinfo->display);
3534 /* Scan the modifier table to see which modifier bits the Meta and
3535 Alt keysyms are on. */
3537 int row, col; /* The row and column in the modifier table. */
3538 int found_alt_or_meta;
3540 for (row = 3; row < 8; row++)
3542 found_alt_or_meta = 0;
3543 for (col = 0; col < mods->max_keypermod; col++)
3545 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3547 /* Zeroes are used for filler. Skip them. */
3548 if (code == 0)
3549 continue;
3551 /* Are any of this keycode's keysyms a meta key? */
3553 int code_col;
3555 for (code_col = 0; code_col < syms_per_code; code_col++)
3557 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3559 switch (sym)
3561 case XK_Meta_L:
3562 case XK_Meta_R:
3563 found_alt_or_meta = 1;
3564 dpyinfo->meta_mod_mask |= (1 << row);
3565 break;
3567 case XK_Alt_L:
3568 case XK_Alt_R:
3569 found_alt_or_meta = 1;
3570 dpyinfo->alt_mod_mask |= (1 << row);
3571 break;
3573 case XK_Hyper_L:
3574 case XK_Hyper_R:
3575 if (!found_alt_or_meta)
3576 dpyinfo->hyper_mod_mask |= (1 << row);
3577 code_col = syms_per_code;
3578 col = mods->max_keypermod;
3579 break;
3581 case XK_Super_L:
3582 case XK_Super_R:
3583 if (!found_alt_or_meta)
3584 dpyinfo->super_mod_mask |= (1 << row);
3585 code_col = syms_per_code;
3586 col = mods->max_keypermod;
3587 break;
3589 case XK_Shift_Lock:
3590 /* Ignore this if it's not on the lock modifier. */
3591 if (!found_alt_or_meta && ((1 << row) == LockMask))
3592 dpyinfo->shift_lock_mask = LockMask;
3593 code_col = syms_per_code;
3594 col = mods->max_keypermod;
3595 break;
3603 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3604 if (! dpyinfo->meta_mod_mask)
3606 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3607 dpyinfo->alt_mod_mask = 0;
3610 /* If some keys are both alt and meta,
3611 make them just meta, not alt. */
3612 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3614 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3617 XFree ((char *) syms);
3618 XFreeModifiermap (mods);
3621 /* Convert between the modifier bits X uses and the modifier bits
3622 Emacs uses. */
3624 unsigned int
3625 x_x_to_emacs_modifiers (dpyinfo, state)
3626 struct x_display_info *dpyinfo;
3627 unsigned int state;
3629 EMACS_UINT mod_meta = meta_modifier;
3630 EMACS_UINT mod_alt = alt_modifier;
3631 EMACS_UINT mod_hyper = hyper_modifier;
3632 EMACS_UINT mod_super = super_modifier;
3633 Lisp_Object tem;
3635 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3636 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3637 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3638 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3639 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3640 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3641 tem = Fget (Vx_super_keysym, Qmodifier_value);
3642 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3645 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3646 | ((state & ControlMask) ? ctrl_modifier : 0)
3647 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3648 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3649 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3650 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3653 static unsigned int
3654 x_emacs_to_x_modifiers (dpyinfo, state)
3655 struct x_display_info *dpyinfo;
3656 unsigned int state;
3658 EMACS_UINT mod_meta = meta_modifier;
3659 EMACS_UINT mod_alt = alt_modifier;
3660 EMACS_UINT mod_hyper = hyper_modifier;
3661 EMACS_UINT mod_super = super_modifier;
3663 Lisp_Object tem;
3665 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3666 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3667 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3668 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3669 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3670 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3671 tem = Fget (Vx_super_keysym, Qmodifier_value);
3672 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3675 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3676 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3677 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3678 | ((state & shift_modifier) ? ShiftMask : 0)
3679 | ((state & ctrl_modifier) ? ControlMask : 0)
3680 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3683 /* Convert a keysym to its name. */
3685 char *
3686 x_get_keysym_name (keysym)
3687 KeySym keysym;
3689 char *value;
3691 BLOCK_INPUT;
3692 value = XKeysymToString (keysym);
3693 UNBLOCK_INPUT;
3695 return value;
3700 /* Mouse clicks and mouse movement. Rah. */
3702 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3704 If the event is a button press, then note that we have grabbed
3705 the mouse. */
3707 static Lisp_Object
3708 construct_mouse_click (result, event, f)
3709 struct input_event *result;
3710 XButtonEvent *event;
3711 struct frame *f;
3713 /* Make the event type NO_EVENT; we'll change that when we decide
3714 otherwise. */
3715 result->kind = MOUSE_CLICK_EVENT;
3716 result->code = event->button - Button1;
3717 result->timestamp = event->time;
3718 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3719 event->state)
3720 | (event->type == ButtonRelease
3721 ? up_modifier
3722 : down_modifier));
3724 XSETINT (result->x, event->x);
3725 XSETINT (result->y, event->y);
3726 XSETFRAME (result->frame_or_window, f);
3727 result->arg = Qnil;
3728 return Qnil;
3732 /* Function to report a mouse movement to the mainstream Emacs code.
3733 The input handler calls this.
3735 We have received a mouse movement event, which is given in *event.
3736 If the mouse is over a different glyph than it was last time, tell
3737 the mainstream emacs code by setting mouse_moved. If not, ask for
3738 another motion event, so we can check again the next time it moves. */
3740 static XMotionEvent last_mouse_motion_event;
3741 static Lisp_Object last_mouse_motion_frame;
3743 static int
3744 note_mouse_movement (frame, event)
3745 FRAME_PTR frame;
3746 XMotionEvent *event;
3748 last_mouse_movement_time = event->time;
3749 last_mouse_motion_event = *event;
3750 XSETFRAME (last_mouse_motion_frame, frame);
3752 if (!FRAME_X_OUTPUT (frame))
3753 return 0;
3755 if (event->window != FRAME_X_WINDOW (frame))
3757 frame->mouse_moved = 1;
3758 last_mouse_scroll_bar = Qnil;
3759 note_mouse_highlight (frame, -1, -1);
3760 last_mouse_glyph_frame = 0;
3761 return 1;
3765 /* Has the mouse moved off the glyph it was on at the last sighting? */
3766 if (frame != last_mouse_glyph_frame
3767 || event->x < last_mouse_glyph.x
3768 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3769 || event->y < last_mouse_glyph.y
3770 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3772 frame->mouse_moved = 1;
3773 last_mouse_scroll_bar = Qnil;
3774 note_mouse_highlight (frame, event->x, event->y);
3775 /* Remember which glyph we're now on. */
3776 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3777 last_mouse_glyph_frame = frame;
3778 return 1;
3781 return 0;
3785 /************************************************************************
3786 Mouse Face
3787 ************************************************************************/
3789 static void
3790 redo_mouse_highlight ()
3792 if (!NILP (last_mouse_motion_frame)
3793 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3794 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3795 last_mouse_motion_event.x,
3796 last_mouse_motion_event.y);
3801 /* Return the current position of the mouse.
3802 *FP should be a frame which indicates which display to ask about.
3804 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3805 and *PART to the frame, window, and scroll bar part that the mouse
3806 is over. Set *X and *Y to the portion and whole of the mouse's
3807 position on the scroll bar.
3809 If the mouse movement started elsewhere, set *FP to the frame the
3810 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3811 the mouse is over.
3813 Set *TIME to the server time-stamp for the time at which the mouse
3814 was at this position.
3816 Don't store anything if we don't have a valid set of values to report.
3818 This clears the mouse_moved flag, so we can wait for the next mouse
3819 movement. */
3821 static void
3822 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3823 FRAME_PTR *fp;
3824 int insist;
3825 Lisp_Object *bar_window;
3826 enum scroll_bar_part *part;
3827 Lisp_Object *x, *y;
3828 unsigned long *time;
3830 FRAME_PTR f1;
3832 BLOCK_INPUT;
3834 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3835 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3836 else
3838 Window root;
3839 int root_x, root_y;
3841 Window dummy_window;
3842 int dummy;
3844 Lisp_Object frame, tail;
3846 /* Clear the mouse-moved flag for every frame on this display. */
3847 FOR_EACH_FRAME (tail, frame)
3848 if (FRAME_X_P (XFRAME (frame))
3849 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3850 XFRAME (frame)->mouse_moved = 0;
3852 last_mouse_scroll_bar = Qnil;
3854 /* Figure out which root window we're on. */
3855 XQueryPointer (FRAME_X_DISPLAY (*fp),
3856 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3858 /* The root window which contains the pointer. */
3859 &root,
3861 /* Trash which we can't trust if the pointer is on
3862 a different screen. */
3863 &dummy_window,
3865 /* The position on that root window. */
3866 &root_x, &root_y,
3868 /* More trash we can't trust. */
3869 &dummy, &dummy,
3871 /* Modifier keys and pointer buttons, about which
3872 we don't care. */
3873 (unsigned int *) &dummy);
3875 /* Now we have a position on the root; find the innermost window
3876 containing the pointer. */
3878 Window win, child;
3879 int win_x, win_y;
3880 int parent_x = 0, parent_y = 0;
3882 win = root;
3884 /* XTranslateCoordinates can get errors if the window
3885 structure is changing at the same time this function
3886 is running. So at least we must not crash from them. */
3888 x_catch_errors (FRAME_X_DISPLAY (*fp));
3890 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3891 && FRAME_LIVE_P (last_mouse_frame))
3893 /* If mouse was grabbed on a frame, give coords for that frame
3894 even if the mouse is now outside it. */
3895 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3897 /* From-window, to-window. */
3898 root, FRAME_X_WINDOW (last_mouse_frame),
3900 /* From-position, to-position. */
3901 root_x, root_y, &win_x, &win_y,
3903 /* Child of win. */
3904 &child);
3905 f1 = last_mouse_frame;
3907 else
3909 while (1)
3911 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3913 /* From-window, to-window. */
3914 root, win,
3916 /* From-position, to-position. */
3917 root_x, root_y, &win_x, &win_y,
3919 /* Child of win. */
3920 &child);
3922 if (child == None || child == win)
3923 break;
3924 #ifdef USE_GTK
3925 /* We don't wan't to know the innermost window. We
3926 want the edit window. For non-Gtk+ the innermost
3927 window is the edit window. For Gtk+ it might not
3928 be. It might be the tool bar for example. */
3929 if (x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win))
3930 break;
3931 #endif
3932 win = child;
3933 parent_x = win_x;
3934 parent_y = win_y;
3937 /* Now we know that:
3938 win is the innermost window containing the pointer
3939 (XTC says it has no child containing the pointer),
3940 win_x and win_y are the pointer's position in it
3941 (XTC did this the last time through), and
3942 parent_x and parent_y are the pointer's position in win's parent.
3943 (They are what win_x and win_y were when win was child.
3944 If win is the root window, it has no parent, and
3945 parent_{x,y} are invalid, but that's okay, because we'll
3946 never use them in that case.) */
3948 #ifdef USE_GTK
3949 /* We don't wan't to know the innermost window. We
3950 want the edit window. */
3951 f1 = x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3952 #else
3953 /* Is win one of our frames? */
3954 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
3955 #endif
3957 #ifdef USE_X_TOOLKIT
3958 /* If we end up with the menu bar window, say it's not
3959 on the frame. */
3960 if (f1 != NULL
3961 && f1->output_data.x->menubar_widget
3962 && win == XtWindow (f1->output_data.x->menubar_widget))
3963 f1 = NULL;
3964 #endif /* USE_X_TOOLKIT */
3967 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
3968 f1 = 0;
3970 x_uncatch_errors ();
3972 /* If not, is it one of our scroll bars? */
3973 if (! f1)
3975 struct scroll_bar *bar;
3977 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
3979 if (bar)
3981 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
3982 win_x = parent_x;
3983 win_y = parent_y;
3987 if (f1 == 0 && insist > 0)
3988 f1 = SELECTED_FRAME ();
3990 if (f1)
3992 /* Ok, we found a frame. Store all the values.
3993 last_mouse_glyph is a rectangle used to reduce the
3994 generation of mouse events. To not miss any motion
3995 events, we must divide the frame into rectangles of the
3996 size of the smallest character that could be displayed
3997 on it, i.e. into the same rectangles that matrices on
3998 the frame are divided into. */
4000 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
4001 last_mouse_glyph_frame = f1;
4003 *bar_window = Qnil;
4004 *part = 0;
4005 *fp = f1;
4006 XSETINT (*x, win_x);
4007 XSETINT (*y, win_y);
4008 *time = last_mouse_movement_time;
4013 UNBLOCK_INPUT;
4018 /***********************************************************************
4019 Scroll bars
4020 ***********************************************************************/
4022 /* Scroll bar support. */
4024 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4025 manages it.
4026 This can be called in GC, so we have to make sure to strip off mark
4027 bits. */
4029 static struct scroll_bar *
4030 x_window_to_scroll_bar (display, window_id)
4031 Display *display;
4032 Window window_id;
4034 Lisp_Object tail;
4036 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4037 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4038 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4040 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4042 Lisp_Object frame, bar, condemned;
4044 frame = XCAR (tail);
4045 /* All elements of Vframe_list should be frames. */
4046 if (! FRAMEP (frame))
4047 abort ();
4049 if (! FRAME_X_P (XFRAME (frame)))
4050 continue;
4052 /* Scan this frame's scroll bar list for a scroll bar with the
4053 right window ID. */
4054 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4055 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4056 /* This trick allows us to search both the ordinary and
4057 condemned scroll bar lists with one loop. */
4058 ! NILP (bar) || (bar = condemned,
4059 condemned = Qnil,
4060 ! NILP (bar));
4061 bar = XSCROLL_BAR (bar)->next)
4062 if (XSCROLL_BAR (bar)->x_window == window_id &&
4063 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4064 return XSCROLL_BAR (bar);
4067 return 0;
4071 #if defined USE_LUCID
4073 /* Return the Lucid menu bar WINDOW is part of. Return null
4074 if WINDOW is not part of a menu bar. */
4076 static Widget
4077 x_window_to_menu_bar (window)
4078 Window window;
4080 Lisp_Object tail;
4082 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4084 if (FRAME_X_P (XFRAME (XCAR (tail))))
4086 Lisp_Object frame = XCAR (tail);
4087 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4089 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4090 return menu_bar;
4094 return NULL;
4097 #endif /* USE_LUCID */
4100 /************************************************************************
4101 Toolkit scroll bars
4102 ************************************************************************/
4104 #ifdef USE_TOOLKIT_SCROLL_BARS
4106 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4107 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4108 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4109 struct scroll_bar *));
4110 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4111 int, int, int));
4114 /* Lisp window being scrolled. Set when starting to interact with
4115 a toolkit scroll bar, reset to nil when ending the interaction. */
4117 static Lisp_Object window_being_scrolled;
4119 /* Last scroll bar part sent in xm_scroll_callback. */
4121 static int last_scroll_bar_part;
4123 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4124 that movements of 1/20 of the screen size are mapped to up/down. */
4126 #ifndef USE_GTK
4127 /* Id of action hook installed for scroll bars. */
4129 static XtActionHookId action_hook_id;
4131 static Boolean xaw3d_arrow_scroll;
4133 /* Whether the drag scrolling maintains the mouse at the top of the
4134 thumb. If not, resizing the thumb needs to be done more carefully
4135 to avoid jerkyness. */
4137 static Boolean xaw3d_pick_top;
4139 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4140 bars are used.. The hook is responsible for detecting when
4141 the user ends an interaction with the scroll bar, and generates
4142 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4144 static void
4145 xt_action_hook (widget, client_data, action_name, event, params,
4146 num_params)
4147 Widget widget;
4148 XtPointer client_data;
4149 String action_name;
4150 XEvent *event;
4151 String *params;
4152 Cardinal *num_params;
4154 int scroll_bar_p;
4155 char *end_action;
4157 #ifdef USE_MOTIF
4158 scroll_bar_p = XmIsScrollBar (widget);
4159 end_action = "Release";
4160 #else /* !USE_MOTIF i.e. use Xaw */
4161 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4162 end_action = "EndScroll";
4163 #endif /* USE_MOTIF */
4165 if (scroll_bar_p
4166 && strcmp (action_name, end_action) == 0
4167 && WINDOWP (window_being_scrolled))
4169 struct window *w;
4171 x_send_scroll_bar_event (window_being_scrolled,
4172 scroll_bar_end_scroll, 0, 0);
4173 w = XWINDOW (window_being_scrolled);
4175 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4177 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4178 /* The thumb size is incorrect while dragging: fix it. */
4179 set_vertical_scroll_bar (w);
4181 window_being_scrolled = Qnil;
4182 last_scroll_bar_part = -1;
4184 /* Xt timeouts no longer needed. */
4185 toolkit_scroll_bar_interaction = 0;
4188 #endif /* not USE_GTK */
4190 /* A vector of windows used for communication between
4191 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4193 static struct window **scroll_bar_windows;
4194 static int scroll_bar_windows_size;
4197 /* Send a client message with message type Xatom_Scrollbar for a
4198 scroll action to the frame of WINDOW. PART is a value identifying
4199 the part of the scroll bar that was clicked on. PORTION is the
4200 amount to scroll of a whole of WHOLE. */
4202 static void
4203 x_send_scroll_bar_event (window, part, portion, whole)
4204 Lisp_Object window;
4205 int part, portion, whole;
4207 XEvent event;
4208 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4209 struct window *w = XWINDOW (window);
4210 struct frame *f = XFRAME (w->frame);
4211 int i;
4213 BLOCK_INPUT;
4215 /* Construct a ClientMessage event to send to the frame. */
4216 ev->type = ClientMessage;
4217 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4218 ev->display = FRAME_X_DISPLAY (f);
4219 ev->window = FRAME_X_WINDOW (f);
4220 ev->format = 32;
4222 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4223 not enough to store a pointer or Lisp_Object on a 64 bit system.
4224 So, store the window in scroll_bar_windows and pass the index
4225 into that array in the event. */
4226 for (i = 0; i < scroll_bar_windows_size; ++i)
4227 if (scroll_bar_windows[i] == NULL)
4228 break;
4230 if (i == scroll_bar_windows_size)
4232 int new_size = max (10, 2 * scroll_bar_windows_size);
4233 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4234 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4236 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4237 nbytes);
4238 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4239 scroll_bar_windows_size = new_size;
4242 scroll_bar_windows[i] = w;
4243 ev->data.l[0] = (long) i;
4244 ev->data.l[1] = (long) part;
4245 ev->data.l[2] = (long) 0;
4246 ev->data.l[3] = (long) portion;
4247 ev->data.l[4] = (long) whole;
4249 /* Make Xt timeouts work while the scroll bar is active. */
4250 toolkit_scroll_bar_interaction = 1;
4251 #ifdef USE_X_TOOLKIT
4252 x_activate_timeout_atimer ();
4253 #endif
4255 /* Setting the event mask to zero means that the message will
4256 be sent to the client that created the window, and if that
4257 window no longer exists, no event will be sent. */
4258 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4259 UNBLOCK_INPUT;
4263 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4264 in *IEVENT. */
4266 static void
4267 x_scroll_bar_to_input_event (event, ievent)
4268 XEvent *event;
4269 struct input_event *ievent;
4271 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4272 Lisp_Object window;
4273 struct frame *f;
4274 struct window *w;
4276 w = scroll_bar_windows[ev->data.l[0]];
4277 scroll_bar_windows[ev->data.l[0]] = NULL;
4279 XSETWINDOW (window, w);
4280 f = XFRAME (w->frame);
4282 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4283 ievent->frame_or_window = window;
4284 ievent->arg = Qnil;
4285 #ifdef USE_GTK
4286 ievent->timestamp = CurrentTime;
4287 #else
4288 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4289 #endif
4290 ievent->part = ev->data.l[1];
4291 ievent->code = ev->data.l[2];
4292 ievent->x = make_number ((int) ev->data.l[3]);
4293 ievent->y = make_number ((int) ev->data.l[4]);
4294 ievent->modifiers = 0;
4298 #ifdef USE_MOTIF
4300 /* Minimum and maximum values used for Motif scroll bars. */
4302 #define XM_SB_MAX 10000000
4305 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4306 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4307 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4309 static void
4310 xm_scroll_callback (widget, client_data, call_data)
4311 Widget widget;
4312 XtPointer client_data, call_data;
4314 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4315 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4316 int part = -1, whole = 0, portion = 0;
4318 switch (cs->reason)
4320 case XmCR_DECREMENT:
4321 bar->dragging = Qnil;
4322 part = scroll_bar_up_arrow;
4323 break;
4325 case XmCR_INCREMENT:
4326 bar->dragging = Qnil;
4327 part = scroll_bar_down_arrow;
4328 break;
4330 case XmCR_PAGE_DECREMENT:
4331 bar->dragging = Qnil;
4332 part = scroll_bar_above_handle;
4333 break;
4335 case XmCR_PAGE_INCREMENT:
4336 bar->dragging = Qnil;
4337 part = scroll_bar_below_handle;
4338 break;
4340 case XmCR_TO_TOP:
4341 bar->dragging = Qnil;
4342 part = scroll_bar_to_top;
4343 break;
4345 case XmCR_TO_BOTTOM:
4346 bar->dragging = Qnil;
4347 part = scroll_bar_to_bottom;
4348 break;
4350 case XmCR_DRAG:
4352 int slider_size;
4354 /* Get the slider size. */
4355 BLOCK_INPUT;
4356 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4357 UNBLOCK_INPUT;
4359 whole = XM_SB_MAX - slider_size;
4360 portion = min (cs->value, whole);
4361 part = scroll_bar_handle;
4362 bar->dragging = make_number (cs->value);
4364 break;
4366 case XmCR_VALUE_CHANGED:
4367 break;
4370 if (part >= 0)
4372 window_being_scrolled = bar->window;
4373 last_scroll_bar_part = part;
4374 x_send_scroll_bar_event (bar->window, part, portion, whole);
4378 #elif defined USE_GTK
4380 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4381 bar widget. DATA is a pointer to the scroll_bar structure. */
4383 static gboolean
4384 xg_scroll_callback (GtkRange *range,
4385 GtkScrollType scroll,
4386 gdouble value,
4387 gpointer user_data)
4389 struct scroll_bar *bar = (struct scroll_bar *) user_data;
4390 gdouble position;
4391 int part = -1, whole = 0, portion = 0;
4392 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4393 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4395 if (xg_ignore_gtk_scrollbar) return FALSE;
4396 position = gtk_adjustment_get_value (adj);
4399 switch (scroll)
4401 case GTK_SCROLL_JUMP:
4402 /* Buttons 1 2 or 3 must be grabbed. */
4403 if (FRAME_X_DISPLAY_INFO (f)->grabbed != 0
4404 && FRAME_X_DISPLAY_INFO (f)->grabbed < (1 << 4))
4406 part = scroll_bar_handle;
4407 whole = adj->upper - adj->page_size;
4408 portion = min ((int)position, whole);
4409 bar->dragging = make_number ((int)portion);
4411 break;
4412 case GTK_SCROLL_STEP_BACKWARD:
4413 part = scroll_bar_up_arrow;
4414 bar->dragging = Qnil;
4415 break;
4416 case GTK_SCROLL_STEP_FORWARD:
4417 part = scroll_bar_down_arrow;
4418 bar->dragging = Qnil;
4419 break;
4420 case GTK_SCROLL_PAGE_BACKWARD:
4421 part = scroll_bar_above_handle;
4422 bar->dragging = Qnil;
4423 break;
4424 case GTK_SCROLL_PAGE_FORWARD:
4425 part = scroll_bar_below_handle;
4426 bar->dragging = Qnil;
4427 break;
4430 if (part >= 0)
4432 window_being_scrolled = bar->window;
4433 last_scroll_bar_part = part;
4434 x_send_scroll_bar_event (bar->window, part, portion, whole);
4437 return FALSE;
4440 /* Callback for button release. Sets dragging to Qnil when dragging is done. */
4442 static gboolean
4443 xg_end_scroll_callback (GtkWidget *widget,
4444 GdkEventButton *event,
4445 gpointer user_data)
4447 struct scroll_bar *bar = (struct scroll_bar *) user_data;
4448 bar->dragging = Qnil;
4449 if (WINDOWP (window_being_scrolled))
4451 x_send_scroll_bar_event (window_being_scrolled,
4452 scroll_bar_end_scroll, 0, 0);
4453 window_being_scrolled = Qnil;
4456 return FALSE;
4460 #else /* not USE_GTK and not USE_MOTIF */
4462 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4463 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4464 scroll bar struct. CALL_DATA is a pointer to a float saying where
4465 the thumb is. */
4467 static void
4468 xaw_jump_callback (widget, client_data, call_data)
4469 Widget widget;
4470 XtPointer client_data, call_data;
4472 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4473 float top = *(float *) call_data;
4474 float shown;
4475 int whole, portion, height;
4476 int part;
4478 /* Get the size of the thumb, a value between 0 and 1. */
4479 BLOCK_INPUT;
4480 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4481 UNBLOCK_INPUT;
4483 whole = 10000000;
4484 portion = shown < 1 ? top * whole : 0;
4486 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4487 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4488 the bottom, so we force the scrolling whenever we see that we're
4489 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4490 we try to ensure that we always stay two pixels away from the
4491 bottom). */
4492 part = scroll_bar_down_arrow;
4493 else
4494 part = scroll_bar_handle;
4496 window_being_scrolled = bar->window;
4497 bar->dragging = make_number (portion);
4498 last_scroll_bar_part = part;
4499 x_send_scroll_bar_event (bar->window, part, portion, whole);
4503 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4504 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4505 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4506 the scroll bar. CALL_DATA is an integer specifying the action that
4507 has taken place. Its magnitude is in the range 0..height of the
4508 scroll bar. Negative values mean scroll towards buffer start.
4509 Values < height of scroll bar mean line-wise movement. */
4511 static void
4512 xaw_scroll_callback (widget, client_data, call_data)
4513 Widget widget;
4514 XtPointer client_data, call_data;
4516 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4517 /* The position really is stored cast to a pointer. */
4518 int position = (long) call_data;
4519 Dimension height;
4520 int part;
4522 /* Get the height of the scroll bar. */
4523 BLOCK_INPUT;
4524 XtVaGetValues (widget, XtNheight, &height, NULL);
4525 UNBLOCK_INPUT;
4527 if (eabs (position) >= height)
4528 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4530 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4531 it maps line-movement to call_data = max(5, height/20). */
4532 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4533 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4534 else
4535 part = scroll_bar_move_ratio;
4537 window_being_scrolled = bar->window;
4538 bar->dragging = Qnil;
4539 last_scroll_bar_part = part;
4540 x_send_scroll_bar_event (bar->window, part, position, height);
4543 #endif /* not USE_GTK and not USE_MOTIF */
4545 #define SCROLL_BAR_NAME "verticalScrollBar"
4547 /* Create the widget for scroll bar BAR on frame F. Record the widget
4548 and X window of the scroll bar in BAR. */
4550 #ifdef USE_GTK
4551 static void
4552 x_create_toolkit_scroll_bar (f, bar)
4553 struct frame *f;
4554 struct scroll_bar *bar;
4556 char *scroll_bar_name = SCROLL_BAR_NAME;
4558 BLOCK_INPUT;
4559 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4560 G_CALLBACK (xg_end_scroll_callback),
4561 scroll_bar_name);
4562 UNBLOCK_INPUT;
4565 #else /* not USE_GTK */
4567 static void
4568 x_create_toolkit_scroll_bar (f, bar)
4569 struct frame *f;
4570 struct scroll_bar *bar;
4572 Window xwindow;
4573 Widget widget;
4574 Arg av[20];
4575 int ac = 0;
4576 char *scroll_bar_name = SCROLL_BAR_NAME;
4577 unsigned long pixel;
4579 BLOCK_INPUT;
4581 #ifdef USE_MOTIF
4582 /* Set resources. Create the widget. */
4583 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4584 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4585 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4586 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4587 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4588 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4589 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4591 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4592 if (pixel != -1)
4594 XtSetArg (av[ac], XmNforeground, pixel);
4595 ++ac;
4598 pixel = f->output_data.x->scroll_bar_background_pixel;
4599 if (pixel != -1)
4601 XtSetArg (av[ac], XmNbackground, pixel);
4602 ++ac;
4605 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4606 scroll_bar_name, av, ac);
4608 /* Add one callback for everything that can happen. */
4609 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4610 (XtPointer) bar);
4611 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4612 (XtPointer) bar);
4613 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4614 (XtPointer) bar);
4615 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4616 (XtPointer) bar);
4617 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4618 (XtPointer) bar);
4619 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4620 (XtPointer) bar);
4621 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4622 (XtPointer) bar);
4624 /* Realize the widget. Only after that is the X window created. */
4625 XtRealizeWidget (widget);
4627 /* Set the cursor to an arrow. I didn't find a resource to do that.
4628 And I'm wondering why it hasn't an arrow cursor by default. */
4629 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4630 f->output_data.x->nontext_cursor);
4632 #else /* !USE_MOTIF i.e. use Xaw */
4634 /* Set resources. Create the widget. The background of the
4635 Xaw3d scroll bar widget is a little bit light for my taste.
4636 We don't alter it here to let users change it according
4637 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4638 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4639 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4640 /* For smoother scrolling with Xaw3d -sm */
4641 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4643 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4644 if (pixel != -1)
4646 XtSetArg (av[ac], XtNforeground, pixel);
4647 ++ac;
4650 pixel = f->output_data.x->scroll_bar_background_pixel;
4651 if (pixel != -1)
4653 XtSetArg (av[ac], XtNbackground, pixel);
4654 ++ac;
4657 /* Top/bottom shadow colors. */
4659 /* Allocate them, if necessary. */
4660 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4662 pixel = f->output_data.x->scroll_bar_background_pixel;
4663 if (pixel != -1)
4665 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4666 FRAME_X_COLORMAP (f),
4667 &pixel, 1.2, 0x8000))
4668 pixel = -1;
4669 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4672 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4674 pixel = f->output_data.x->scroll_bar_background_pixel;
4675 if (pixel != -1)
4677 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4678 FRAME_X_COLORMAP (f),
4679 &pixel, 0.6, 0x4000))
4680 pixel = -1;
4681 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4685 #ifdef XtNbeNiceToColormap
4686 /* Tell the toolkit about them. */
4687 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4688 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4689 /* We tried to allocate a color for the top/bottom shadow, and
4690 failed, so tell Xaw3d to use dithering instead. */
4692 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4693 ++ac;
4695 else
4696 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4697 be more consistent with other emacs 3d colors, and since Xaw3d is
4698 not good at dealing with allocation failure. */
4700 /* This tells Xaw3d to use real colors instead of dithering for
4701 the shadows. */
4702 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4703 ++ac;
4705 /* Specify the colors. */
4706 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4707 if (pixel != -1)
4709 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4710 ++ac;
4712 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4713 if (pixel != -1)
4715 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4716 ++ac;
4719 #endif
4721 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4722 f->output_data.x->edit_widget, av, ac);
4725 char *initial = "";
4726 char *val = initial;
4727 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4728 #ifdef XtNarrowScrollbars
4729 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4730 #endif
4731 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4732 if (xaw3d_arrow_scroll || val == initial)
4733 { /* ARROW_SCROLL */
4734 xaw3d_arrow_scroll = True;
4735 /* Isn't that just a personal preference ? --Stef */
4736 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4740 /* Define callbacks. */
4741 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4742 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4743 (XtPointer) bar);
4745 /* Realize the widget. Only after that is the X window created. */
4746 XtRealizeWidget (widget);
4748 #endif /* !USE_MOTIF */
4750 /* Install an action hook that lets us detect when the user
4751 finishes interacting with a scroll bar. */
4752 if (action_hook_id == 0)
4753 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4755 /* Remember X window and widget in the scroll bar vector. */
4756 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4757 xwindow = XtWindow (widget);
4758 bar->x_window = xwindow;
4760 UNBLOCK_INPUT;
4762 #endif /* not USE_GTK */
4765 /* Set the thumb size and position of scroll bar BAR. We are currently
4766 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4768 #ifdef USE_GTK
4769 static void
4770 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4771 struct scroll_bar *bar;
4772 int portion, position, whole;
4774 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4777 #else /* not USE_GTK */
4778 static void
4779 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4780 struct scroll_bar *bar;
4781 int portion, position, whole;
4783 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4784 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4785 float top, shown;
4787 BLOCK_INPUT;
4789 #ifdef USE_MOTIF
4791 /* We use an estimate of 30 chars per line rather than the real
4792 `portion' value. This has the disadvantage that the thumb size
4793 is not very representative, but it makes our life a lot easier.
4794 Otherwise, we have to constantly adjust the thumb size, which
4795 we can't always do quickly enough: while dragging, the size of
4796 the thumb might prevent the user from dragging the thumb all the
4797 way to the end. but Motif and some versions of Xaw3d don't allow
4798 updating the thumb size while dragging. Also, even if we can update
4799 its size, the update will often happen too late.
4800 If you don't believe it, check out revision 1.650 of xterm.c to see
4801 what hoops we were going through and the still poor behavior we got. */
4802 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4803 /* When the thumb is at the bottom, position == whole.
4804 So we need to increase `whole' to make space for the thumb. */
4805 whole += portion;
4807 if (whole <= 0)
4808 top = 0, shown = 1;
4809 else
4811 top = (float) position / whole;
4812 shown = (float) portion / whole;
4815 if (NILP (bar->dragging))
4817 int size, value;
4819 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4820 is the scroll bar's maximum and MIN is the scroll bar's minimum
4821 value. */
4822 size = shown * XM_SB_MAX;
4823 size = min (size, XM_SB_MAX);
4824 size = max (size, 1);
4826 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4827 value = top * XM_SB_MAX;
4828 value = min (value, XM_SB_MAX - size);
4830 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4832 #else /* !USE_MOTIF i.e. use Xaw */
4834 if (whole == 0)
4835 top = 0, shown = 1;
4836 else
4838 top = (float) position / whole;
4839 shown = (float) portion / whole;
4843 float old_top, old_shown;
4844 Dimension height;
4845 XtVaGetValues (widget,
4846 XtNtopOfThumb, &old_top,
4847 XtNshown, &old_shown,
4848 XtNheight, &height,
4849 NULL);
4851 /* Massage the top+shown values. */
4852 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4853 top = max (0, min (1, top));
4854 else
4855 top = old_top;
4856 /* Keep two pixels available for moving the thumb down. */
4857 shown = max (0, min (1 - top - (2.0 / height), shown));
4859 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4860 check that your system's configuration file contains a define
4861 for `NARROWPROTO'. See s/freebsd.h for an example. */
4862 if (top != old_top || shown != old_shown)
4864 if (NILP (bar->dragging))
4865 XawScrollbarSetThumb (widget, top, shown);
4866 else
4868 /* Try to make the scrolling a tad smoother. */
4869 if (!xaw3d_pick_top)
4870 shown = min (shown, old_shown);
4872 XawScrollbarSetThumb (widget, top, shown);
4876 #endif /* !USE_MOTIF */
4878 UNBLOCK_INPUT;
4880 #endif /* not USE_GTK */
4882 #endif /* USE_TOOLKIT_SCROLL_BARS */
4886 /************************************************************************
4887 Scroll bars, general
4888 ************************************************************************/
4890 /* Create a scroll bar and return the scroll bar vector for it. W is
4891 the Emacs window on which to create the scroll bar. TOP, LEFT,
4892 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4893 scroll bar. */
4895 static struct scroll_bar *
4896 x_scroll_bar_create (w, top, left, width, height)
4897 struct window *w;
4898 int top, left, width, height;
4900 struct frame *f = XFRAME (w->frame);
4901 struct scroll_bar *bar
4902 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4904 BLOCK_INPUT;
4906 #ifdef USE_TOOLKIT_SCROLL_BARS
4907 x_create_toolkit_scroll_bar (f, bar);
4908 #else /* not USE_TOOLKIT_SCROLL_BARS */
4910 XSetWindowAttributes a;
4911 unsigned long mask;
4912 Window window;
4914 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4915 if (a.background_pixel == -1)
4916 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4918 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4919 | ButtonMotionMask | PointerMotionHintMask
4920 | ExposureMask);
4921 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4923 mask = (CWBackPixel | CWEventMask | CWCursor);
4925 /* Clear the area of W that will serve as a scroll bar. This is
4926 for the case that a window has been split horizontally. In
4927 this case, no clear_frame is generated to reduce flickering. */
4928 if (width > 0 && height > 0)
4929 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4930 left, top, width,
4931 window_box_height (w), False);
4933 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4934 /* Position and size of scroll bar. */
4935 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4936 top,
4937 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4938 height,
4939 /* Border width, depth, class, and visual. */
4941 CopyFromParent,
4942 CopyFromParent,
4943 CopyFromParent,
4944 /* Attributes. */
4945 mask, &a);
4946 bar->x_window = window;
4948 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4950 XSETWINDOW (bar->window, w);
4951 bar->top = top;
4952 bar->left = left;
4953 bar->width = width;
4954 bar->height = height;
4955 bar->start = 0;
4956 bar->end = 0;
4957 bar->dragging = Qnil;
4958 bar->fringe_extended_p = 0;
4960 /* Add bar to its frame's list of scroll bars. */
4961 bar->next = FRAME_SCROLL_BARS (f);
4962 bar->prev = Qnil;
4963 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4964 if (!NILP (bar->next))
4965 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4967 /* Map the window/widget. */
4968 #ifdef USE_TOOLKIT_SCROLL_BARS
4970 #ifdef USE_GTK
4971 xg_update_scrollbar_pos (f,
4972 bar->x_window,
4973 top,
4974 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4975 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4976 max (height, 1));
4977 xg_show_scroll_bar (bar->x_window);
4978 #else /* not USE_GTK */
4979 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4980 XtConfigureWidget (scroll_bar,
4981 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4982 top,
4983 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4984 max (height, 1), 0);
4985 XtMapWidget (scroll_bar);
4986 #endif /* not USE_GTK */
4988 #else /* not USE_TOOLKIT_SCROLL_BARS */
4989 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
4990 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4992 UNBLOCK_INPUT;
4993 return bar;
4997 #ifndef USE_TOOLKIT_SCROLL_BARS
4999 /* Draw BAR's handle in the proper position.
5001 If the handle is already drawn from START to END, don't bother
5002 redrawing it, unless REBUILD is non-zero; in that case, always
5003 redraw it. (REBUILD is handy for drawing the handle after expose
5004 events.)
5006 Normally, we want to constrain the start and end of the handle to
5007 fit inside its rectangle, but if the user is dragging the scroll
5008 bar handle, we want to let them drag it down all the way, so that
5009 the bar's top is as far down as it goes; otherwise, there's no way
5010 to move to the very end of the buffer. */
5012 static void
5013 x_scroll_bar_set_handle (bar, start, end, rebuild)
5014 struct scroll_bar *bar;
5015 int start, end;
5016 int rebuild;
5018 int dragging = ! NILP (bar->dragging);
5019 Window w = bar->x_window;
5020 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5021 GC gc = f->output_data.x->normal_gc;
5023 /* If the display is already accurate, do nothing. */
5024 if (! rebuild
5025 && start == bar->start
5026 && end == bar->end)
5027 return;
5029 BLOCK_INPUT;
5032 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5033 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5034 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5036 /* Make sure the values are reasonable, and try to preserve
5037 the distance between start and end. */
5039 int length = end - start;
5041 if (start < 0)
5042 start = 0;
5043 else if (start > top_range)
5044 start = top_range;
5045 end = start + length;
5047 if (end < start)
5048 end = start;
5049 else if (end > top_range && ! dragging)
5050 end = top_range;
5053 /* Store the adjusted setting in the scroll bar. */
5054 bar->start = start;
5055 bar->end = end;
5057 /* Clip the end position, just for display. */
5058 if (end > top_range)
5059 end = top_range;
5061 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5062 below top positions, to make sure the handle is always at least
5063 that many pixels tall. */
5064 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5066 /* Draw the empty space above the handle. Note that we can't clear
5067 zero-height areas; that means "clear to end of window." */
5068 if (0 < start)
5069 x_clear_area (FRAME_X_DISPLAY (f), w,
5070 /* x, y, width, height, and exposures. */
5071 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5072 VERTICAL_SCROLL_BAR_TOP_BORDER,
5073 inside_width, start,
5074 False);
5076 /* Change to proper foreground color if one is specified. */
5077 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5078 XSetForeground (FRAME_X_DISPLAY (f), gc,
5079 f->output_data.x->scroll_bar_foreground_pixel);
5081 /* Draw the handle itself. */
5082 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5083 /* x, y, width, height */
5084 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5085 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5086 inside_width, end - start);
5088 /* Restore the foreground color of the GC if we changed it above. */
5089 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5090 XSetForeground (FRAME_X_DISPLAY (f), gc,
5091 FRAME_FOREGROUND_PIXEL (f));
5093 /* Draw the empty space below the handle. Note that we can't
5094 clear zero-height areas; that means "clear to end of window." */
5095 if (end < inside_height)
5096 x_clear_area (FRAME_X_DISPLAY (f), w,
5097 /* x, y, width, height, and exposures. */
5098 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5099 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5100 inside_width, inside_height - end,
5101 False);
5105 UNBLOCK_INPUT;
5108 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5110 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5111 nil. */
5113 static void
5114 x_scroll_bar_remove (bar)
5115 struct scroll_bar *bar;
5117 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5118 BLOCK_INPUT;
5120 #ifdef USE_TOOLKIT_SCROLL_BARS
5121 #ifdef USE_GTK
5122 xg_remove_scroll_bar (f, bar->x_window);
5123 #else /* not USE_GTK */
5124 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5125 #endif /* not USE_GTK */
5126 #else
5127 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5128 #endif
5130 /* Disassociate this scroll bar from its window. */
5131 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5133 UNBLOCK_INPUT;
5137 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5138 that we are displaying PORTION characters out of a total of WHOLE
5139 characters, starting at POSITION. If WINDOW has no scroll bar,
5140 create one. */
5142 static void
5143 XTset_vertical_scroll_bar (w, portion, whole, position)
5144 struct window *w;
5145 int portion, whole, position;
5147 struct frame *f = XFRAME (w->frame);
5148 struct scroll_bar *bar;
5149 int top, height, left, sb_left, width, sb_width;
5150 int window_y, window_height;
5151 #ifdef USE_TOOLKIT_SCROLL_BARS
5152 int fringe_extended_p;
5153 #endif
5155 /* Get window dimensions. */
5156 window_box (w, -1, 0, &window_y, 0, &window_height);
5157 top = window_y;
5158 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5159 height = window_height;
5161 /* Compute the left edge of the scroll bar area. */
5162 left = WINDOW_SCROLL_BAR_AREA_X (w);
5164 /* Compute the width of the scroll bar which might be less than
5165 the width of the area reserved for the scroll bar. */
5166 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5167 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5168 else
5169 sb_width = width;
5171 /* Compute the left edge of the scroll bar. */
5172 #ifdef USE_TOOLKIT_SCROLL_BARS
5173 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5174 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5175 else
5176 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5177 #else
5178 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5179 sb_left = left + width - sb_width;
5180 else
5181 sb_left = left;
5182 #endif
5184 #ifdef USE_TOOLKIT_SCROLL_BARS
5185 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5186 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5187 && WINDOW_LEFT_FRINGE_WIDTH (w)
5188 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5189 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5190 else
5191 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5192 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5193 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5194 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5195 #endif
5197 /* Does the scroll bar exist yet? */
5198 if (NILP (w->vertical_scroll_bar))
5200 if (width > 0 && height > 0)
5202 BLOCK_INPUT;
5203 #ifdef USE_TOOLKIT_SCROLL_BARS
5204 if (fringe_extended_p)
5205 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5206 sb_left, top, sb_width, height, False);
5207 else
5208 #endif
5209 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5210 left, top, width, height, False);
5211 UNBLOCK_INPUT;
5214 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5216 else
5218 /* It may just need to be moved and resized. */
5219 unsigned int mask = 0;
5221 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5223 BLOCK_INPUT;
5225 if (sb_left != bar->left)
5226 mask |= CWX;
5227 if (top != bar->top)
5228 mask |= CWY;
5229 if (sb_width != bar->width)
5230 mask |= CWWidth;
5231 if (height != bar->height)
5232 mask |= CWHeight;
5234 #ifdef USE_TOOLKIT_SCROLL_BARS
5236 /* Move/size the scroll bar widget. */
5237 if (mask || bar->fringe_extended_p != fringe_extended_p)
5239 /* Since toolkit scroll bars are smaller than the space reserved
5240 for them on the frame, we have to clear "under" them. */
5241 if (width > 0 && height > 0)
5243 if (fringe_extended_p)
5244 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5245 sb_left, top, sb_width, height, False);
5246 else
5247 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5248 left, top, width, height, False);
5250 #ifdef USE_GTK
5251 xg_update_scrollbar_pos (f,
5252 bar->x_window,
5253 top,
5254 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5255 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5256 max (height, 1));
5257 #else /* not USE_GTK */
5258 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5259 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5260 top,
5261 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5262 max (height, 1), 0);
5263 #endif /* not USE_GTK */
5265 #else /* not USE_TOOLKIT_SCROLL_BARS */
5267 /* Clear areas not covered by the scroll bar because of
5268 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5269 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5271 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5272 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5273 height, False);
5274 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5275 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5276 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5277 height, False);
5280 /* Clear areas not covered by the scroll bar because it's not as
5281 wide as the area reserved for it. This makes sure a
5282 previous mode line display is cleared after C-x 2 C-x 1, for
5283 example. */
5285 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5286 int rest = area_width - sb_width;
5287 if (rest > 0 && height > 0)
5289 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5290 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5291 left + area_width - rest, top,
5292 rest, height, False);
5293 else
5294 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5295 left, top, rest, height, False);
5299 /* Move/size the scroll bar window. */
5300 if (mask)
5302 XWindowChanges wc;
5304 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5305 wc.y = top;
5306 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5307 wc.height = height;
5308 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5309 mask, &wc);
5312 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5314 /* Remember new settings. */
5315 bar->left = sb_left;
5316 bar->top = top;
5317 bar->width = sb_width;
5318 bar->height = height;
5320 UNBLOCK_INPUT;
5323 #ifdef USE_TOOLKIT_SCROLL_BARS
5324 bar->fringe_extended_p = fringe_extended_p;
5326 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5327 #else /* not USE_TOOLKIT_SCROLL_BARS */
5328 /* Set the scroll bar's current state, unless we're currently being
5329 dragged. */
5330 if (NILP (bar->dragging))
5332 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5334 if (whole == 0)
5335 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5336 else
5338 int start = ((double) position * top_range) / whole;
5339 int end = ((double) (position + portion) * top_range) / whole;
5340 x_scroll_bar_set_handle (bar, start, end, 0);
5343 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5345 XSETVECTOR (w->vertical_scroll_bar, bar);
5349 /* The following three hooks are used when we're doing a thorough
5350 redisplay of the frame. We don't explicitly know which scroll bars
5351 are going to be deleted, because keeping track of when windows go
5352 away is a real pain - "Can you say set-window-configuration, boys
5353 and girls?" Instead, we just assert at the beginning of redisplay
5354 that *all* scroll bars are to be removed, and then save a scroll bar
5355 from the fiery pit when we actually redisplay its window. */
5357 /* Arrange for all scroll bars on FRAME to be removed at the next call
5358 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5359 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5361 static void
5362 XTcondemn_scroll_bars (frame)
5363 FRAME_PTR frame;
5365 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5366 while (! NILP (FRAME_SCROLL_BARS (frame)))
5368 Lisp_Object bar;
5369 bar = FRAME_SCROLL_BARS (frame);
5370 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5371 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5372 XSCROLL_BAR (bar)->prev = Qnil;
5373 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5374 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5375 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5380 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5381 Note that WINDOW isn't necessarily condemned at all. */
5383 static void
5384 XTredeem_scroll_bar (window)
5385 struct window *window;
5387 struct scroll_bar *bar;
5388 struct frame *f;
5390 /* We can't redeem this window's scroll bar if it doesn't have one. */
5391 if (NILP (window->vertical_scroll_bar))
5392 abort ();
5394 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5396 /* Unlink it from the condemned list. */
5397 f = XFRAME (WINDOW_FRAME (window));
5398 if (NILP (bar->prev))
5400 /* If the prev pointer is nil, it must be the first in one of
5401 the lists. */
5402 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5403 /* It's not condemned. Everything's fine. */
5404 return;
5405 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5406 window->vertical_scroll_bar))
5407 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5408 else
5409 /* If its prev pointer is nil, it must be at the front of
5410 one or the other! */
5411 abort ();
5413 else
5414 XSCROLL_BAR (bar->prev)->next = bar->next;
5416 if (! NILP (bar->next))
5417 XSCROLL_BAR (bar->next)->prev = bar->prev;
5419 bar->next = FRAME_SCROLL_BARS (f);
5420 bar->prev = Qnil;
5421 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5422 if (! NILP (bar->next))
5423 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5426 /* Remove all scroll bars on FRAME that haven't been saved since the
5427 last call to `*condemn_scroll_bars_hook'. */
5429 static void
5430 XTjudge_scroll_bars (f)
5431 FRAME_PTR f;
5433 Lisp_Object bar, next;
5435 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5437 /* Clear out the condemned list now so we won't try to process any
5438 more events on the hapless scroll bars. */
5439 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5441 for (; ! NILP (bar); bar = next)
5443 struct scroll_bar *b = XSCROLL_BAR (bar);
5445 x_scroll_bar_remove (b);
5447 next = b->next;
5448 b->next = b->prev = Qnil;
5451 /* Now there should be no references to the condemned scroll bars,
5452 and they should get garbage-collected. */
5456 #ifndef USE_TOOLKIT_SCROLL_BARS
5457 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5458 is a no-op when using toolkit scroll bars.
5460 This may be called from a signal handler, so we have to ignore GC
5461 mark bits. */
5463 static void
5464 x_scroll_bar_expose (bar, event)
5465 struct scroll_bar *bar;
5466 XEvent *event;
5468 Window w = bar->x_window;
5469 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5470 GC gc = f->output_data.x->normal_gc;
5471 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5473 BLOCK_INPUT;
5475 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5477 /* Switch to scroll bar foreground color. */
5478 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5479 XSetForeground (FRAME_X_DISPLAY (f), gc,
5480 f->output_data.x->scroll_bar_foreground_pixel);
5482 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5483 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5485 /* x, y, width, height */
5486 0, 0,
5487 bar->width - 1 - width_trim - width_trim,
5488 bar->height - 1);
5490 /* Restore the foreground color of the GC if we changed it above. */
5491 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5492 XSetForeground (FRAME_X_DISPLAY (f), gc,
5493 FRAME_FOREGROUND_PIXEL (f));
5495 UNBLOCK_INPUT;
5498 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5500 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5501 is set to something other than NO_EVENT, it is enqueued.
5503 This may be called from a signal handler, so we have to ignore GC
5504 mark bits. */
5507 static void
5508 x_scroll_bar_handle_click (bar, event, emacs_event)
5509 struct scroll_bar *bar;
5510 XEvent *event;
5511 struct input_event *emacs_event;
5513 if (! WINDOWP (bar->window))
5514 abort ();
5516 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5517 emacs_event->code = event->xbutton.button - Button1;
5518 emacs_event->modifiers
5519 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5520 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5521 event->xbutton.state)
5522 | (event->type == ButtonRelease
5523 ? up_modifier
5524 : down_modifier));
5525 emacs_event->frame_or_window = bar->window;
5526 emacs_event->arg = Qnil;
5527 emacs_event->timestamp = event->xbutton.time;
5529 int top_range
5530 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5531 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5533 if (y < 0) y = 0;
5534 if (y > top_range) y = top_range;
5536 if (y < bar->start)
5537 emacs_event->part = scroll_bar_above_handle;
5538 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5539 emacs_event->part = scroll_bar_handle;
5540 else
5541 emacs_event->part = scroll_bar_below_handle;
5543 #ifndef USE_TOOLKIT_SCROLL_BARS
5544 /* If the user has released the handle, set it to its final position. */
5545 if (event->type == ButtonRelease
5546 && ! NILP (bar->dragging))
5548 int new_start = y - XINT (bar->dragging);
5549 int new_end = new_start + bar->end - bar->start;
5551 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5552 bar->dragging = Qnil;
5554 #endif
5556 XSETINT (emacs_event->x, y);
5557 XSETINT (emacs_event->y, top_range);
5561 #ifndef USE_TOOLKIT_SCROLL_BARS
5563 /* Handle some mouse motion while someone is dragging the scroll bar.
5565 This may be called from a signal handler, so we have to ignore GC
5566 mark bits. */
5568 static void
5569 x_scroll_bar_note_movement (bar, event)
5570 struct scroll_bar *bar;
5571 XEvent *event;
5573 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5575 last_mouse_movement_time = event->xmotion.time;
5577 f->mouse_moved = 1;
5578 XSETVECTOR (last_mouse_scroll_bar, bar);
5580 /* If we're dragging the bar, display it. */
5581 if (! NILP (bar->dragging))
5583 /* Where should the handle be now? */
5584 int new_start = event->xmotion.y - XINT (bar->dragging);
5586 if (new_start != bar->start)
5588 int new_end = new_start + bar->end - bar->start;
5590 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5595 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5597 /* Return information to the user about the current position of the mouse
5598 on the scroll bar. */
5600 static void
5601 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5602 FRAME_PTR *fp;
5603 Lisp_Object *bar_window;
5604 enum scroll_bar_part *part;
5605 Lisp_Object *x, *y;
5606 unsigned long *time;
5608 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5609 Window w = bar->x_window;
5610 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5611 int win_x, win_y;
5612 Window dummy_window;
5613 int dummy_coord;
5614 unsigned int dummy_mask;
5616 BLOCK_INPUT;
5618 /* Get the mouse's position relative to the scroll bar window, and
5619 report that. */
5620 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5622 /* Root, child, root x and root y. */
5623 &dummy_window, &dummy_window,
5624 &dummy_coord, &dummy_coord,
5626 /* Position relative to scroll bar. */
5627 &win_x, &win_y,
5629 /* Mouse buttons and modifier keys. */
5630 &dummy_mask))
5632 else
5634 int top_range
5635 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5637 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5639 if (! NILP (bar->dragging))
5640 win_y -= XINT (bar->dragging);
5642 if (win_y < 0)
5643 win_y = 0;
5644 if (win_y > top_range)
5645 win_y = top_range;
5647 *fp = f;
5648 *bar_window = bar->window;
5650 if (! NILP (bar->dragging))
5651 *part = scroll_bar_handle;
5652 else if (win_y < bar->start)
5653 *part = scroll_bar_above_handle;
5654 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5655 *part = scroll_bar_handle;
5656 else
5657 *part = scroll_bar_below_handle;
5659 XSETINT (*x, win_y);
5660 XSETINT (*y, top_range);
5662 f->mouse_moved = 0;
5663 last_mouse_scroll_bar = Qnil;
5666 *time = last_mouse_movement_time;
5668 UNBLOCK_INPUT;
5672 /* The screen has been cleared so we may have changed foreground or
5673 background colors, and the scroll bars may need to be redrawn.
5674 Clear out the scroll bars, and ask for expose events, so we can
5675 redraw them. */
5677 void
5678 x_scroll_bar_clear (f)
5679 FRAME_PTR f;
5681 #ifndef USE_TOOLKIT_SCROLL_BARS
5682 Lisp_Object bar;
5684 /* We can have scroll bars even if this is 0,
5685 if we just turned off scroll bar mode.
5686 But in that case we should not clear them. */
5687 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5688 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5689 bar = XSCROLL_BAR (bar)->next)
5690 XClearArea (FRAME_X_DISPLAY (f),
5691 XSCROLL_BAR (bar)->x_window,
5692 0, 0, 0, 0, True);
5693 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5697 /* The main X event-reading loop - XTread_socket. */
5699 /* This holds the state XLookupString needs to implement dead keys
5700 and other tricks known as "compose processing". _X Window System_
5701 says that a portable program can't use this, but Stephen Gildea assures
5702 me that letting the compiler initialize it to zeros will work okay.
5704 This must be defined outside of XTread_socket, for the same reasons
5705 given for enter_timestamp, above. */
5707 static XComposeStatus compose_status;
5709 /* Record the last 100 characters stored
5710 to help debug the loss-of-chars-during-GC problem. */
5712 static int temp_index;
5713 static short temp_buffer[100];
5715 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5716 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5717 temp_index = 0; \
5718 temp_buffer[temp_index++] = (keysym)
5720 /* Set this to nonzero to fake an "X I/O error"
5721 on a particular display. */
5723 struct x_display_info *XTread_socket_fake_io_error;
5725 /* When we find no input here, we occasionally do a no-op command
5726 to verify that the X server is still running and we can still talk with it.
5727 We try all the open displays, one by one.
5728 This variable is used for cycling thru the displays. */
5730 static struct x_display_info *next_noop_dpyinfo;
5732 #define SET_SAVED_MENU_EVENT(size) \
5733 do \
5735 if (f->output_data.x->saved_menu_event == 0) \
5736 f->output_data.x->saved_menu_event \
5737 = (XEvent *) xmalloc (sizeof (XEvent)); \
5738 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5739 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5740 XSETFRAME (inev.ie.frame_or_window, f); \
5742 while (0)
5744 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5745 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5748 enum
5750 X_EVENT_NORMAL,
5751 X_EVENT_GOTO_OUT,
5752 X_EVENT_DROP
5755 /* Filter events for the current X input method.
5756 DPYINFO is the display this event is for.
5757 EVENT is the X event to filter.
5759 Returns non-zero if the event was filtered, caller shall not process
5760 this event further.
5761 Returns zero if event is wasn't filtered. */
5763 #ifdef HAVE_X_I18N
5764 static int
5765 x_filter_event (dpyinfo, event)
5766 struct x_display_info *dpyinfo;
5767 XEvent *event;
5769 /* XFilterEvent returns non-zero if the input method has
5770 consumed the event. We pass the frame's X window to
5771 XFilterEvent because that's the one for which the IC
5772 was created. */
5774 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5775 event->xclient.window);
5777 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5779 #endif
5781 #ifdef USE_GTK
5782 static int current_count;
5783 static int current_finish;
5784 static struct input_event *current_hold_quit;
5786 /* This is the filter function invoked by the GTK event loop.
5787 It is invoked before the XEvent is translated to a GdkEvent,
5788 so we have a chance to act on the event before GTK. */
5789 static GdkFilterReturn
5790 event_handler_gdk (gxev, ev, data)
5791 GdkXEvent *gxev;
5792 GdkEvent *ev;
5793 gpointer data;
5795 XEvent *xev = (XEvent *) gxev;
5797 if (current_count >= 0)
5799 struct x_display_info *dpyinfo;
5801 dpyinfo = x_display_info_for_display (xev->xany.display);
5803 #ifdef HAVE_X_I18N
5804 /* Filter events for the current X input method.
5805 GTK calls XFilterEvent but not for key press and release,
5806 so we do it here. */
5807 if (xev->type == KeyPress || xev->type == KeyRelease)
5808 if (dpyinfo && x_filter_event (dpyinfo, xev))
5809 return GDK_FILTER_REMOVE;
5810 #endif
5812 if (! dpyinfo)
5813 current_finish = X_EVENT_NORMAL;
5814 else
5816 current_count +=
5817 handle_one_xevent (dpyinfo, xev, &current_finish,
5818 current_hold_quit);
5821 else
5822 current_finish = x_dispatch_event (xev, xev->xany.display);
5824 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5825 return GDK_FILTER_REMOVE;
5827 return GDK_FILTER_CONTINUE;
5829 #endif /* USE_GTK */
5832 /* Handles the XEvent EVENT on display DPYINFO.
5834 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5835 *FINISH is zero if caller should continue reading events.
5836 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5838 We return the number of characters stored into the buffer. */
5840 static int
5841 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5842 struct x_display_info *dpyinfo;
5843 XEvent *eventp;
5844 int *finish;
5845 struct input_event *hold_quit;
5847 union {
5848 struct input_event ie;
5849 struct selection_input_event sie;
5850 } inev;
5851 int count = 0;
5852 int do_help = 0;
5853 int nbytes = 0;
5854 struct frame *f = NULL;
5855 struct coding_system coding;
5856 XEvent event = *eventp;
5858 *finish = X_EVENT_NORMAL;
5860 EVENT_INIT (inev.ie);
5861 inev.ie.kind = NO_EVENT;
5862 inev.ie.arg = Qnil;
5864 if (pending_event_wait.eventtype == event.type)
5865 pending_event_wait.eventtype = 0; /* Indicates we got it. */
5867 switch (event.type)
5869 case ClientMessage:
5871 if (event.xclient.message_type
5872 == dpyinfo->Xatom_wm_protocols
5873 && event.xclient.format == 32)
5875 if (event.xclient.data.l[0]
5876 == dpyinfo->Xatom_wm_take_focus)
5878 /* Use x_any_window_to_frame because this
5879 could be the shell widget window
5880 if the frame has no title bar. */
5881 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5882 #ifdef HAVE_X_I18N
5883 /* Not quite sure this is needed -pd */
5884 if (f && FRAME_XIC (f))
5885 XSetICFocus (FRAME_XIC (f));
5886 #endif
5887 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5888 instructs the WM to set the input focus automatically for
5889 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5890 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5891 it has set the focus. So, XSetInputFocus below is not
5892 needed.
5894 The call to XSetInputFocus below has also caused trouble. In
5895 cases where the XSetInputFocus done by the WM and the one
5896 below are temporally close (on a fast machine), the call
5897 below can generate additional FocusIn events which confuse
5898 Emacs. */
5900 /* Since we set WM_TAKE_FOCUS, we must call
5901 XSetInputFocus explicitly. But not if f is null,
5902 since that might be an event for a deleted frame. */
5903 if (f)
5905 Display *d = event.xclient.display;
5906 /* Catch and ignore errors, in case window has been
5907 iconified by a window manager such as GWM. */
5908 x_catch_errors (d);
5909 XSetInputFocus (d, event.xclient.window,
5910 /* The ICCCM says this is
5911 the only valid choice. */
5912 RevertToParent,
5913 event.xclient.data.l[1]);
5914 /* This is needed to detect the error
5915 if there is an error. */
5916 XSync (d, False);
5917 x_uncatch_errors ();
5919 /* Not certain about handling scroll bars here */
5920 #endif /* 0 */
5921 goto done;
5924 if (event.xclient.data.l[0]
5925 == dpyinfo->Xatom_wm_save_yourself)
5927 /* Save state modify the WM_COMMAND property to
5928 something which can reinstate us. This notifies
5929 the session manager, who's looking for such a
5930 PropertyNotify. Can restart processing when
5931 a keyboard or mouse event arrives. */
5932 /* If we have a session manager, don't set this.
5933 KDE will then start two Emacsen, one for the
5934 session manager and one for this. */
5935 #ifdef HAVE_X_SM
5936 if (! x_session_have_connection ())
5937 #endif
5939 f = x_top_window_to_frame (dpyinfo,
5940 event.xclient.window);
5941 /* This is just so we only give real data once
5942 for a single Emacs process. */
5943 if (f == SELECTED_FRAME ())
5944 XSetCommand (FRAME_X_DISPLAY (f),
5945 event.xclient.window,
5946 initial_argv, initial_argc);
5947 else if (f)
5948 XSetCommand (FRAME_X_DISPLAY (f),
5949 event.xclient.window,
5950 0, 0);
5952 goto done;
5955 if (event.xclient.data.l[0]
5956 == dpyinfo->Xatom_wm_delete_window)
5958 f = x_any_window_to_frame (dpyinfo,
5959 event.xclient.window);
5960 if (!f)
5961 goto OTHER; /* May be a dialog that is to be removed */
5963 inev.ie.kind = DELETE_WINDOW_EVENT;
5964 XSETFRAME (inev.ie.frame_or_window, f);
5965 goto done;
5968 goto done;
5971 if (event.xclient.message_type
5972 == dpyinfo->Xatom_wm_configure_denied)
5974 goto done;
5977 if (event.xclient.message_type
5978 == dpyinfo->Xatom_wm_window_moved)
5980 int new_x, new_y;
5981 f = x_window_to_frame (dpyinfo, event.xclient.window);
5983 new_x = event.xclient.data.s[0];
5984 new_y = event.xclient.data.s[1];
5986 if (f)
5988 f->left_pos = new_x;
5989 f->top_pos = new_y;
5991 goto done;
5994 #ifdef HACK_EDITRES
5995 if (event.xclient.message_type
5996 == dpyinfo->Xatom_editres)
5998 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5999 if (f)
6000 _XEditResCheckMessages (f->output_data.x->widget, NULL,
6001 &event, NULL);
6002 goto done;
6004 #endif /* HACK_EDITRES */
6006 if ((event.xclient.message_type
6007 == dpyinfo->Xatom_DONE)
6008 || (event.xclient.message_type
6009 == dpyinfo->Xatom_PAGE))
6011 /* Ghostview job completed. Kill it. We could
6012 reply with "Next" if we received "Page", but we
6013 currently never do because we are interested in
6014 images, only, which should have 1 page. */
6015 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
6016 f = x_window_to_frame (dpyinfo, event.xclient.window);
6017 if (!f)
6018 goto OTHER;
6019 x_kill_gs_process (pixmap, f);
6020 expose_frame (f, 0, 0, 0, 0);
6021 goto done;
6024 #ifdef USE_TOOLKIT_SCROLL_BARS
6025 /* Scroll bar callbacks send a ClientMessage from which
6026 we construct an input_event. */
6027 if (event.xclient.message_type
6028 == dpyinfo->Xatom_Scrollbar)
6030 x_scroll_bar_to_input_event (&event, &inev.ie);
6031 *finish = X_EVENT_GOTO_OUT;
6032 goto done;
6034 #endif /* USE_TOOLKIT_SCROLL_BARS */
6036 /* XEmbed messages from the embedder (if any). */
6037 if (event.xclient.message_type
6038 == dpyinfo->Xatom_XEMBED)
6040 enum xembed_message msg = event.xclient.data.l[1];
6041 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6042 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6044 *finish = X_EVENT_GOTO_OUT;
6045 goto done;
6048 xft_settings_event (dpyinfo, &event);
6050 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6051 if (!f)
6052 goto OTHER;
6053 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
6054 *finish = X_EVENT_DROP;
6056 break;
6058 case SelectionNotify:
6059 last_user_time = event.xselection.time;
6060 #ifdef USE_X_TOOLKIT
6061 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6062 goto OTHER;
6063 #endif /* not USE_X_TOOLKIT */
6064 x_handle_selection_notify (&event.xselection);
6065 break;
6067 case SelectionClear: /* Someone has grabbed ownership. */
6068 last_user_time = event.xselectionclear.time;
6069 #ifdef USE_X_TOOLKIT
6070 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6071 goto OTHER;
6072 #endif /* USE_X_TOOLKIT */
6074 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
6076 inev.ie.kind = SELECTION_CLEAR_EVENT;
6077 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6078 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6079 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6080 inev.ie.frame_or_window = Qnil;
6082 break;
6084 case SelectionRequest: /* Someone wants our selection. */
6085 last_user_time = event.xselectionrequest.time;
6086 #ifdef USE_X_TOOLKIT
6087 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6088 goto OTHER;
6089 #endif /* USE_X_TOOLKIT */
6091 XSelectionRequestEvent *eventp
6092 = (XSelectionRequestEvent *) &event;
6094 inev.ie.kind = SELECTION_REQUEST_EVENT;
6095 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6096 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6097 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6098 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6099 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6100 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6101 inev.ie.frame_or_window = Qnil;
6103 break;
6105 case PropertyNotify:
6106 last_user_time = event.xproperty.time;
6107 f = x_top_window_to_frame (dpyinfo, event.xproperty.window);
6108 if (f && event.xproperty.atom == dpyinfo->Xatom_net_wm_state)
6109 x_handle_net_wm_state (f, &event.xproperty);
6111 x_handle_property_notify (&event.xproperty);
6112 xft_settings_event (dpyinfo, &event);
6113 goto OTHER;
6115 case ReparentNotify:
6116 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6117 if (f)
6119 int x, y;
6120 f->output_data.x->parent_desc = event.xreparent.parent;
6121 x_real_positions (f, &x, &y);
6122 f->left_pos = x;
6123 f->top_pos = y;
6125 /* Perhaps reparented due to a WM restart. Reset this. */
6126 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6127 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6129 goto OTHER;
6131 case Expose:
6132 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6133 if (f)
6135 #ifdef USE_GTK
6136 /* This seems to be needed for GTK 2.6. */
6137 x_clear_area (event.xexpose.display,
6138 event.xexpose.window,
6139 event.xexpose.x, event.xexpose.y,
6140 event.xexpose.width, event.xexpose.height,
6141 FALSE);
6142 #endif
6143 if (f->async_visible == 0)
6145 f->async_visible = 1;
6146 f->async_iconified = 0;
6147 f->output_data.x->has_been_visible = 1;
6148 SET_FRAME_GARBAGED (f);
6150 else
6151 expose_frame (f,
6152 event.xexpose.x, event.xexpose.y,
6153 event.xexpose.width, event.xexpose.height);
6155 else
6157 #ifndef USE_TOOLKIT_SCROLL_BARS
6158 struct scroll_bar *bar;
6159 #endif
6160 #if defined USE_LUCID
6161 /* Submenus of the Lucid menu bar aren't widgets
6162 themselves, so there's no way to dispatch events
6163 to them. Recognize this case separately. */
6165 Widget widget
6166 = x_window_to_menu_bar (event.xexpose.window);
6167 if (widget)
6168 xlwmenu_redisplay (widget);
6170 #endif /* USE_LUCID */
6172 #ifdef USE_TOOLKIT_SCROLL_BARS
6173 /* Dispatch event to the widget. */
6174 goto OTHER;
6175 #else /* not USE_TOOLKIT_SCROLL_BARS */
6176 bar = x_window_to_scroll_bar (event.xexpose.display,
6177 event.xexpose.window);
6179 if (bar)
6180 x_scroll_bar_expose (bar, &event);
6181 #ifdef USE_X_TOOLKIT
6182 else
6183 goto OTHER;
6184 #endif /* USE_X_TOOLKIT */
6185 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6187 break;
6189 case GraphicsExpose: /* This occurs when an XCopyArea's
6190 source area was obscured or not
6191 available. */
6192 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6193 if (f)
6195 expose_frame (f,
6196 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6197 event.xgraphicsexpose.width,
6198 event.xgraphicsexpose.height);
6200 #ifdef USE_X_TOOLKIT
6201 else
6202 goto OTHER;
6203 #endif /* USE_X_TOOLKIT */
6204 break;
6206 case NoExpose: /* This occurs when an XCopyArea's
6207 source area was completely
6208 available. */
6209 break;
6211 case UnmapNotify:
6212 /* Redo the mouse-highlight after the tooltip has gone. */
6213 if (event.xmap.window == tip_window)
6215 tip_window = 0;
6216 redo_mouse_highlight ();
6219 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6220 if (f) /* F may no longer exist if
6221 the frame was deleted. */
6223 /* While a frame is unmapped, display generation is
6224 disabled; you don't want to spend time updating a
6225 display that won't ever be seen. */
6226 f->async_visible = 0;
6227 /* We can't distinguish, from the event, whether the window
6228 has become iconified or invisible. So assume, if it
6229 was previously visible, than now it is iconified.
6230 But x_make_frame_invisible clears both
6231 the visible flag and the iconified flag;
6232 and that way, we know the window is not iconified now. */
6233 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6235 f->async_iconified = 1;
6237 inev.ie.kind = ICONIFY_EVENT;
6238 XSETFRAME (inev.ie.frame_or_window, f);
6241 goto OTHER;
6243 case MapNotify:
6244 if (event.xmap.window == tip_window)
6245 /* The tooltip has been drawn already. Avoid
6246 the SET_FRAME_GARBAGED below. */
6247 goto OTHER;
6249 /* We use x_top_window_to_frame because map events can
6250 come for sub-windows and they don't mean that the
6251 frame is visible. */
6252 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6253 if (f)
6255 /* wait_reading_process_output will notice this and update
6256 the frame's display structures.
6257 If we where iconified, we should not set garbaged,
6258 because that stops redrawing on Expose events. This looks
6259 bad if we are called from a recursive event loop
6260 (x_dispatch_event), for example when a dialog is up. */
6261 if (! f->async_iconified)
6262 SET_FRAME_GARBAGED (f);
6264 /* Check if fullscreen was specified before we where mapped the
6265 first time, i.e. from the command line. */
6266 if (!f->output_data.x->has_been_visible)
6267 x_check_fullscreen (f);
6269 f->async_visible = 1;
6270 f->async_iconified = 0;
6271 f->output_data.x->has_been_visible = 1;
6273 if (f->iconified)
6275 inev.ie.kind = DEICONIFY_EVENT;
6276 XSETFRAME (inev.ie.frame_or_window, f);
6278 else if (! NILP (Vframe_list)
6279 && ! NILP (XCDR (Vframe_list)))
6280 /* Force a redisplay sooner or later
6281 to update the frame titles
6282 in case this is the second frame. */
6283 record_asynch_buffer_change ();
6285 #ifdef USE_GTK
6286 xg_frame_resized (f, -1, -1);
6287 #endif
6289 goto OTHER;
6291 case KeyPress:
6293 last_user_time = event.xkey.time;
6294 ignore_next_mouse_click_timeout = 0;
6296 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6297 /* Dispatch KeyPress events when in menu. */
6298 if (popup_activated ())
6299 goto OTHER;
6300 #endif
6302 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6304 /* If mouse-highlight is an integer, input clears out
6305 mouse highlighting. */
6306 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6307 && (f == 0
6308 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6310 clear_mouse_face (dpyinfo);
6311 dpyinfo->mouse_face_hidden = 1;
6314 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6315 if (f == 0)
6317 /* Scroll bars consume key events, but we want
6318 the keys to go to the scroll bar's frame. */
6319 Widget widget = XtWindowToWidget (dpyinfo->display,
6320 event.xkey.window);
6321 if (widget && XmIsScrollBar (widget))
6323 widget = XtParent (widget);
6324 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6327 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6329 if (f != 0)
6331 KeySym keysym, orig_keysym;
6332 /* al%imercury@uunet.uu.net says that making this 81
6333 instead of 80 fixed a bug whereby meta chars made
6334 his Emacs hang.
6336 It seems that some version of XmbLookupString has
6337 a bug of not returning XBufferOverflow in
6338 status_return even if the input is too long to
6339 fit in 81 bytes. So, we must prepare sufficient
6340 bytes for copy_buffer. 513 bytes (256 chars for
6341 two-byte character set) seems to be a fairly good
6342 approximation. -- 2000.8.10 handa@etl.go.jp */
6343 unsigned char copy_buffer[513];
6344 unsigned char *copy_bufptr = copy_buffer;
6345 int copy_bufsiz = sizeof (copy_buffer);
6346 int modifiers;
6347 Lisp_Object coding_system = Qlatin_1;
6348 Lisp_Object c;
6350 #ifdef USE_GTK
6351 /* Don't pass keys to GTK. A Tab will shift focus to the
6352 tool bar in GTK 2.4. Keys will still go to menus and
6353 dialogs because in that case popup_activated is TRUE
6354 (see above). */
6355 *finish = X_EVENT_DROP;
6356 #endif
6358 event.xkey.state
6359 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6360 extra_keyboard_modifiers);
6361 modifiers = event.xkey.state;
6363 /* This will have to go some day... */
6365 /* make_lispy_event turns chars into control chars.
6366 Don't do it here because XLookupString is too eager. */
6367 event.xkey.state &= ~ControlMask;
6368 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6369 | dpyinfo->super_mod_mask
6370 | dpyinfo->hyper_mod_mask
6371 | dpyinfo->alt_mod_mask);
6373 /* In case Meta is ComposeCharacter,
6374 clear its status. According to Markus Ehrnsperger
6375 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6376 this enables ComposeCharacter to work whether or
6377 not it is combined with Meta. */
6378 if (modifiers & dpyinfo->meta_mod_mask)
6379 bzero (&compose_status, sizeof (compose_status));
6381 #ifdef HAVE_X_I18N
6382 if (FRAME_XIC (f))
6384 Status status_return;
6386 coding_system = Vlocale_coding_system;
6387 nbytes = XmbLookupString (FRAME_XIC (f),
6388 &event.xkey, copy_bufptr,
6389 copy_bufsiz, &keysym,
6390 &status_return);
6391 if (status_return == XBufferOverflow)
6393 copy_bufsiz = nbytes + 1;
6394 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6395 nbytes = XmbLookupString (FRAME_XIC (f),
6396 &event.xkey, copy_bufptr,
6397 copy_bufsiz, &keysym,
6398 &status_return);
6400 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6401 if (status_return == XLookupNone)
6402 break;
6403 else if (status_return == XLookupChars)
6405 keysym = NoSymbol;
6406 modifiers = 0;
6408 else if (status_return != XLookupKeySym
6409 && status_return != XLookupBoth)
6410 abort ();
6412 else
6413 nbytes = XLookupString (&event.xkey, copy_bufptr,
6414 copy_bufsiz, &keysym,
6415 &compose_status);
6416 #else
6417 nbytes = XLookupString (&event.xkey, copy_bufptr,
6418 copy_bufsiz, &keysym,
6419 &compose_status);
6420 #endif
6422 /* If not using XIM/XIC, and a compose sequence is in progress,
6423 we break here. Otherwise, chars_matched is always 0. */
6424 if (compose_status.chars_matched > 0 && nbytes == 0)
6425 break;
6427 bzero (&compose_status, sizeof (compose_status));
6428 orig_keysym = keysym;
6430 /* Common for all keysym input events. */
6431 XSETFRAME (inev.ie.frame_or_window, f);
6432 inev.ie.modifiers
6433 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6434 inev.ie.timestamp = event.xkey.time;
6436 /* First deal with keysyms which have defined
6437 translations to characters. */
6438 if (keysym >= 32 && keysym < 128)
6439 /* Avoid explicitly decoding each ASCII character. */
6441 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6442 inev.ie.code = keysym;
6443 goto done_keysym;
6446 /* Keysyms directly mapped to Unicode characters. */
6447 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6449 if (keysym < 0x01000080)
6450 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6451 else
6452 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6453 inev.ie.code = keysym & 0xFFFFFF;
6454 goto done_keysym;
6457 /* Now non-ASCII. */
6458 if (HASH_TABLE_P (Vx_keysym_table)
6459 && (NATNUMP (c = Fgethash (make_number (keysym),
6460 Vx_keysym_table,
6461 Qnil))))
6463 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6464 ? ASCII_KEYSTROKE_EVENT
6465 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6466 inev.ie.code = XFASTINT (c);
6467 goto done_keysym;
6470 /* Random non-modifier sorts of keysyms. */
6471 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6472 || keysym == XK_Delete
6473 #ifdef XK_ISO_Left_Tab
6474 || (keysym >= XK_ISO_Left_Tab
6475 && keysym <= XK_ISO_Enter)
6476 #endif
6477 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6478 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6479 #ifdef HPUX
6480 /* This recognizes the "extended function
6481 keys". It seems there's no cleaner way.
6482 Test IsModifierKey to avoid handling
6483 mode_switch incorrectly. */
6484 || ((unsigned) (keysym) >= XK_Select
6485 && (unsigned)(keysym) < XK_KP_Space)
6486 #endif
6487 #ifdef XK_dead_circumflex
6488 || orig_keysym == XK_dead_circumflex
6489 #endif
6490 #ifdef XK_dead_grave
6491 || orig_keysym == XK_dead_grave
6492 #endif
6493 #ifdef XK_dead_tilde
6494 || orig_keysym == XK_dead_tilde
6495 #endif
6496 #ifdef XK_dead_diaeresis
6497 || orig_keysym == XK_dead_diaeresis
6498 #endif
6499 #ifdef XK_dead_macron
6500 || orig_keysym == XK_dead_macron
6501 #endif
6502 #ifdef XK_dead_degree
6503 || orig_keysym == XK_dead_degree
6504 #endif
6505 #ifdef XK_dead_acute
6506 || orig_keysym == XK_dead_acute
6507 #endif
6508 #ifdef XK_dead_cedilla
6509 || orig_keysym == XK_dead_cedilla
6510 #endif
6511 #ifdef XK_dead_breve
6512 || orig_keysym == XK_dead_breve
6513 #endif
6514 #ifdef XK_dead_ogonek
6515 || orig_keysym == XK_dead_ogonek
6516 #endif
6517 #ifdef XK_dead_caron
6518 || orig_keysym == XK_dead_caron
6519 #endif
6520 #ifdef XK_dead_doubleacute
6521 || orig_keysym == XK_dead_doubleacute
6522 #endif
6523 #ifdef XK_dead_abovedot
6524 || orig_keysym == XK_dead_abovedot
6525 #endif
6526 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6527 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6528 /* Any "vendor-specific" key is ok. */
6529 || (orig_keysym & (1 << 28))
6530 || (keysym != NoSymbol && nbytes == 0))
6531 && ! (IsModifierKey (orig_keysym)
6532 /* The symbols from XK_ISO_Lock
6533 to XK_ISO_Last_Group_Lock
6534 don't have real modifiers but
6535 should be treated similarly to
6536 Mode_switch by Emacs. */
6537 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6538 || ((unsigned)(orig_keysym)
6539 >= XK_ISO_Lock
6540 && (unsigned)(orig_keysym)
6541 <= XK_ISO_Last_Group_Lock)
6542 #endif
6545 STORE_KEYSYM_FOR_DEBUG (keysym);
6546 /* make_lispy_event will convert this to a symbolic
6547 key. */
6548 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6549 inev.ie.code = keysym;
6550 goto done_keysym;
6553 { /* Raw bytes, not keysym. */
6554 register int i;
6555 register int c;
6556 int nchars, len;
6558 for (i = 0, nchars = 0; i < nbytes; i++)
6560 if (ASCII_BYTE_P (copy_bufptr[i]))
6561 nchars++;
6562 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6565 if (nchars < nbytes)
6567 /* Decode the input data. */
6568 int require;
6569 unsigned char *p;
6571 /* The input should be decoded with `coding_system'
6572 which depends on which X*LookupString function
6573 we used just above and the locale. */
6574 setup_coding_system (coding_system, &coding);
6575 coding.src_multibyte = 0;
6576 coding.dst_multibyte = 1;
6577 /* The input is converted to events, thus we can't
6578 handle composition. Anyway, there's no XIM that
6579 gives us composition information. */
6580 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6582 require = MAX_MULTIBYTE_LENGTH * nbytes;
6583 coding.destination = alloca (require);
6584 coding.dst_bytes = require;
6585 coding.mode |= CODING_MODE_LAST_BLOCK;
6586 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6587 nbytes = coding.produced;
6588 nchars = coding.produced_char;
6589 copy_bufptr = coding.destination;
6592 /* Convert the input data to a sequence of
6593 character events. */
6594 for (i = 0; i < nbytes; i += len)
6596 if (nchars == nbytes)
6597 c = copy_bufptr[i], len = 1;
6598 else
6599 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
6600 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6601 ? ASCII_KEYSTROKE_EVENT
6602 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6603 inev.ie.code = c;
6604 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6607 count += nchars;
6609 inev.ie.kind = NO_EVENT; /* Already stored above. */
6611 if (keysym == NoSymbol)
6612 break;
6615 done_keysym:
6616 #ifdef HAVE_X_I18N
6617 /* Don't dispatch this event since XtDispatchEvent calls
6618 XFilterEvent, and two calls in a row may freeze the
6619 client. */
6620 break;
6621 #else
6622 goto OTHER;
6623 #endif
6625 case KeyRelease:
6626 last_user_time = event.xkey.time;
6627 #ifdef HAVE_X_I18N
6628 /* Don't dispatch this event since XtDispatchEvent calls
6629 XFilterEvent, and two calls in a row may freeze the
6630 client. */
6631 break;
6632 #else
6633 goto OTHER;
6634 #endif
6636 case EnterNotify:
6637 last_user_time = event.xcrossing.time;
6638 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6640 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6642 if (f && x_mouse_click_focus_ignore_position)
6643 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6645 /* EnterNotify counts as mouse movement,
6646 so update things that depend on mouse position. */
6647 if (f && !f->output_data.x->hourglass_p)
6648 note_mouse_movement (f, &event.xmotion);
6649 #ifdef USE_GTK
6650 /* We may get an EnterNotify on the buttons in the toolbar. In that
6651 case we moved out of any highlighted area and need to note this. */
6652 if (!f && last_mouse_glyph_frame)
6653 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6654 #endif
6655 goto OTHER;
6657 case FocusIn:
6658 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6659 goto OTHER;
6661 case LeaveNotify:
6662 last_user_time = event.xcrossing.time;
6663 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6665 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6666 if (f)
6668 if (f == dpyinfo->mouse_face_mouse_frame)
6670 /* If we move outside the frame, then we're
6671 certainly no longer on any text in the frame. */
6672 clear_mouse_face (dpyinfo);
6673 dpyinfo->mouse_face_mouse_frame = 0;
6676 /* Generate a nil HELP_EVENT to cancel a help-echo.
6677 Do it only if there's something to cancel.
6678 Otherwise, the startup message is cleared when
6679 the mouse leaves the frame. */
6680 if (any_help_event_p)
6681 do_help = -1;
6683 #ifdef USE_GTK
6684 /* See comment in EnterNotify above */
6685 else if (last_mouse_glyph_frame)
6686 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6687 #endif
6688 goto OTHER;
6690 case FocusOut:
6691 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6692 goto OTHER;
6694 case MotionNotify:
6696 last_user_time = event.xmotion.time;
6697 previous_help_echo_string = help_echo_string;
6698 help_echo_string = Qnil;
6700 if (dpyinfo->grabbed && last_mouse_frame
6701 && FRAME_LIVE_P (last_mouse_frame))
6702 f = last_mouse_frame;
6703 else
6704 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6706 if (dpyinfo->mouse_face_hidden)
6708 dpyinfo->mouse_face_hidden = 0;
6709 clear_mouse_face (dpyinfo);
6712 #ifdef USE_GTK
6713 if (f && xg_event_is_for_scrollbar (f, &event))
6714 f = 0;
6715 #endif
6716 if (f)
6719 /* Generate SELECT_WINDOW_EVENTs when needed.
6720 Don't let popup menus influence things (bug#1261). */
6721 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6723 Lisp_Object window;
6725 window = window_from_coordinates (f,
6726 event.xmotion.x, event.xmotion.y,
6727 0, 0, 0, 0);
6729 /* Window will be selected only when it is not selected now and
6730 last mouse movement event was not in it. Minibuffer window
6731 will be selected only when it is active. */
6732 if (WINDOWP (window)
6733 && !EQ (window, last_window)
6734 && !EQ (window, selected_window)
6735 /* For click-to-focus window managers
6736 create event iff we don't leave the
6737 selected frame. */
6738 && (focus_follows_mouse
6739 || (EQ (XWINDOW (window)->frame,
6740 XWINDOW (selected_window)->frame))))
6742 inev.ie.kind = SELECT_WINDOW_EVENT;
6743 inev.ie.frame_or_window = window;
6746 last_window=window;
6748 if (!note_mouse_movement (f, &event.xmotion))
6749 help_echo_string = previous_help_echo_string;
6751 else
6753 #ifndef USE_TOOLKIT_SCROLL_BARS
6754 struct scroll_bar *bar
6755 = x_window_to_scroll_bar (event.xmotion.display,
6756 event.xmotion.window);
6758 if (bar)
6759 x_scroll_bar_note_movement (bar, &event);
6760 #endif /* USE_TOOLKIT_SCROLL_BARS */
6762 /* If we move outside the frame, then we're
6763 certainly no longer on any text in the frame. */
6764 clear_mouse_face (dpyinfo);
6767 /* If the contents of the global variable help_echo_string
6768 has changed, generate a HELP_EVENT. */
6769 if (!NILP (help_echo_string)
6770 || !NILP (previous_help_echo_string))
6771 do_help = 1;
6772 goto OTHER;
6775 case ConfigureNotify:
6776 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6777 #ifdef USE_GTK
6778 if (!f
6779 && (f = x_any_window_to_frame (dpyinfo, event.xconfigure.window))
6780 && event.xconfigure.window == FRAME_X_WINDOW (f))
6782 xg_frame_resized (f, event.xconfigure.width,
6783 event.xconfigure.height);
6784 f = 0;
6786 #endif
6787 if (f)
6789 #ifndef USE_X_TOOLKIT
6790 #ifndef USE_GTK
6791 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6792 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6794 /* In the toolkit version, change_frame_size
6795 is called by the code that handles resizing
6796 of the EmacsFrame widget. */
6798 /* Even if the number of character rows and columns has
6799 not changed, the font size may have changed, so we need
6800 to check the pixel dimensions as well. */
6801 if (columns != FRAME_COLS (f)
6802 || rows != FRAME_LINES (f)
6803 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6804 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6806 change_frame_size (f, rows, columns, 0, 1, 0);
6807 SET_FRAME_GARBAGED (f);
6808 cancel_mouse_face (f);
6811 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6812 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6813 #endif /* not USE_GTK */
6814 #endif
6816 #ifdef USE_GTK
6817 /* GTK creates windows but doesn't map them.
6818 Only get real positions when mapped. */
6819 if (FRAME_GTK_OUTER_WIDGET (f)
6820 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6821 #endif
6823 x_real_positions (f, &f->left_pos, &f->top_pos);
6826 #ifdef HAVE_X_I18N
6827 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6828 xic_set_statusarea (f);
6829 #endif
6832 goto OTHER;
6834 case ButtonRelease:
6835 case ButtonPress:
6837 /* If we decide we want to generate an event to be seen
6838 by the rest of Emacs, we put it here. */
6839 int tool_bar_p = 0;
6841 bzero (&compose_status, sizeof (compose_status));
6842 last_mouse_glyph_frame = 0;
6843 last_user_time = event.xbutton.time;
6845 if (dpyinfo->grabbed
6846 && last_mouse_frame
6847 && FRAME_LIVE_P (last_mouse_frame))
6848 f = last_mouse_frame;
6849 else
6850 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6852 #ifdef USE_GTK
6853 if (f && xg_event_is_for_scrollbar (f, &event))
6854 f = 0;
6855 #endif
6856 if (f)
6858 /* Is this in the tool-bar? */
6859 if (WINDOWP (f->tool_bar_window)
6860 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6862 Lisp_Object window;
6863 int x = event.xbutton.x;
6864 int y = event.xbutton.y;
6866 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6867 tool_bar_p = EQ (window, f->tool_bar_window);
6869 if (tool_bar_p && event.xbutton.button < 4)
6871 handle_tool_bar_click (f, x, y,
6872 event.xbutton.type == ButtonPress,
6873 x_x_to_emacs_modifiers (dpyinfo,
6874 event.xbutton.state));
6878 if (!tool_bar_p)
6879 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6880 if (! popup_activated ())
6881 #endif
6883 if (ignore_next_mouse_click_timeout)
6885 if (event.type == ButtonPress
6886 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6888 ignore_next_mouse_click_timeout = 0;
6889 construct_mouse_click (&inev.ie, &event.xbutton, f);
6891 if (event.type == ButtonRelease)
6892 ignore_next_mouse_click_timeout = 0;
6894 else
6895 construct_mouse_click (&inev.ie, &event.xbutton, f);
6897 if (FRAME_X_EMBEDDED_P (f))
6898 xembed_send_message (f, event.xbutton.time,
6899 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6901 else
6903 struct scroll_bar *bar
6904 = x_window_to_scroll_bar (event.xbutton.display,
6905 event.xbutton.window);
6907 #ifdef USE_TOOLKIT_SCROLL_BARS
6908 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6909 scroll bars. */
6910 if (bar && event.xbutton.state & ControlMask)
6912 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6913 *finish = X_EVENT_DROP;
6915 #else /* not USE_TOOLKIT_SCROLL_BARS */
6916 if (bar)
6917 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6918 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6921 if (event.type == ButtonPress)
6923 dpyinfo->grabbed |= (1 << event.xbutton.button);
6924 last_mouse_frame = f;
6926 if (!tool_bar_p)
6927 last_tool_bar_item = -1;
6929 else
6930 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6932 /* Ignore any mouse motion that happened before this event;
6933 any subsequent mouse-movement Emacs events should reflect
6934 only motion after the ButtonPress/Release. */
6935 if (f != 0)
6936 f->mouse_moved = 0;
6938 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6939 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
6940 /* For a down-event in the menu bar,
6941 don't pass it to Xt right now.
6942 Instead, save it away
6943 and we will pass it to Xt from kbd_buffer_get_event.
6944 That way, we can run some Lisp code first. */
6945 if (
6946 #ifdef USE_GTK
6947 ! popup_activated ()
6948 /* Gtk+ menus only react to the first three buttons. */
6949 && event.xbutton.button < 3
6951 #endif
6952 f && event.type == ButtonPress
6953 /* Verify the event is really within the menu bar
6954 and not just sent to it due to grabbing. */
6955 && event.xbutton.x >= 0
6956 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6957 && event.xbutton.y >= 0
6958 && event.xbutton.y < f->output_data.x->menubar_height
6959 && event.xbutton.same_screen)
6961 SET_SAVED_BUTTON_EVENT;
6962 XSETFRAME (last_mouse_press_frame, f);
6963 #ifdef USE_GTK
6964 *finish = X_EVENT_DROP;
6965 #endif
6967 else if (event.type == ButtonPress)
6969 last_mouse_press_frame = Qnil;
6970 goto OTHER;
6973 #ifdef USE_MOTIF /* This should do not harm for Lucid,
6974 but I am trying to be cautious. */
6975 else if (event.type == ButtonRelease)
6977 if (!NILP (last_mouse_press_frame))
6979 f = XFRAME (last_mouse_press_frame);
6980 if (f->output_data.x)
6981 SET_SAVED_BUTTON_EVENT;
6983 else
6984 goto OTHER;
6986 #endif /* USE_MOTIF */
6987 else
6988 goto OTHER;
6989 #endif /* USE_X_TOOLKIT || USE_GTK */
6991 break;
6993 case CirculateNotify:
6994 goto OTHER;
6996 case CirculateRequest:
6997 goto OTHER;
6999 case VisibilityNotify:
7000 goto OTHER;
7002 case MappingNotify:
7003 /* Someone has changed the keyboard mapping - update the
7004 local cache. */
7005 switch (event.xmapping.request)
7007 case MappingModifier:
7008 x_find_modifier_meanings (dpyinfo);
7009 /* This is meant to fall through. */
7010 case MappingKeyboard:
7011 XRefreshKeyboardMapping (&event.xmapping);
7013 goto OTHER;
7015 case DestroyNotify:
7016 xft_settings_event (dpyinfo, &event);
7017 break;
7019 default:
7020 OTHER:
7021 #ifdef USE_X_TOOLKIT
7022 BLOCK_INPUT;
7023 if (*finish != X_EVENT_DROP)
7024 XtDispatchEvent (&event);
7025 UNBLOCK_INPUT;
7026 #endif /* USE_X_TOOLKIT */
7027 break;
7030 done:
7031 if (inev.ie.kind != NO_EVENT)
7033 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7034 count++;
7037 if (do_help
7038 && !(hold_quit && hold_quit->kind != NO_EVENT))
7040 Lisp_Object frame;
7042 if (f)
7043 XSETFRAME (frame, f);
7044 else
7045 frame = Qnil;
7047 if (do_help > 0)
7049 any_help_event_p = 1;
7050 gen_help_event (help_echo_string, frame, help_echo_window,
7051 help_echo_object, help_echo_pos);
7053 else
7055 help_echo_string = Qnil;
7056 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7058 count++;
7061 *eventp = event;
7062 return count;
7066 /* Handles the XEvent EVENT on display DISPLAY.
7067 This is used for event loops outside the normal event handling,
7068 i.e. looping while a popup menu or a dialog is posted.
7070 Returns the value handle_one_xevent sets in the finish argument. */
7072 x_dispatch_event (event, display)
7073 XEvent *event;
7074 Display *display;
7076 struct x_display_info *dpyinfo;
7077 int finish = X_EVENT_NORMAL;
7079 dpyinfo = x_display_info_for_display (display);
7081 if (dpyinfo)
7082 handle_one_xevent (dpyinfo, event, &finish, 0);
7084 return finish;
7088 /* Read events coming from the X server.
7089 This routine is called by the SIGIO handler.
7090 We return as soon as there are no more events to be read.
7092 We return the number of characters stored into the buffer,
7093 thus pretending to be `read' (except the characters we store
7094 in the keyboard buffer can be multibyte, so are not necessarily
7095 C chars).
7097 EXPECTED is nonzero if the caller knows input is available. */
7099 static int
7100 XTread_socket (terminal, expected, hold_quit)
7101 struct terminal *terminal;
7102 int expected;
7103 struct input_event *hold_quit;
7105 int count = 0;
7106 XEvent event;
7107 int event_found = 0;
7109 if (interrupt_input_blocked)
7111 interrupt_input_pending = 1;
7112 #ifdef SYNC_INPUT
7113 pending_signals = 1;
7114 #endif
7115 return -1;
7118 interrupt_input_pending = 0;
7119 #ifdef SYNC_INPUT
7120 pending_signals = pending_atimers;
7121 #endif
7122 BLOCK_INPUT;
7124 /* So people can tell when we have read the available input. */
7125 input_signal_count++;
7127 ++handling_signal;
7129 #ifdef HAVE_X_SM
7130 /* Only check session manager input for the primary display. */
7131 if (terminal->id == 1 && x_session_have_connection ())
7133 struct input_event inev;
7134 BLOCK_INPUT;
7135 /* We don't need to EVENT_INIT (inev) here, as
7136 x_session_check_input copies an entire input_event. */
7137 if (x_session_check_input (&inev))
7139 kbd_buffer_store_event_hold (&inev, hold_quit);
7140 count++;
7142 UNBLOCK_INPUT;
7144 #endif
7146 /* For debugging, this gives a way to fake an I/O error. */
7147 if (terminal->display_info.x == XTread_socket_fake_io_error)
7149 XTread_socket_fake_io_error = 0;
7150 x_io_error_quitter (terminal->display_info.x->display);
7153 #ifndef USE_GTK
7154 while (XPending (terminal->display_info.x->display))
7156 int finish;
7158 XNextEvent (terminal->display_info.x->display, &event);
7160 #ifdef HAVE_X_I18N
7161 /* Filter events for the current X input method. */
7162 if (x_filter_event (terminal->display_info.x, &event))
7163 continue;
7164 #endif
7165 event_found = 1;
7167 count += handle_one_xevent (terminal->display_info.x,
7168 &event, &finish, hold_quit);
7170 if (finish == X_EVENT_GOTO_OUT)
7171 goto out;
7174 #else /* USE_GTK */
7176 /* For GTK we must use the GTK event loop. But XEvents gets passed
7177 to our filter function above, and then to the big event switch.
7178 We use a bunch of globals to communicate with our filter function,
7179 that is kind of ugly, but it works.
7181 There is no way to do one display at the time, GTK just does events
7182 from all displays. */
7184 while (gtk_events_pending ())
7186 current_count = count;
7187 current_hold_quit = hold_quit;
7189 gtk_main_iteration ();
7191 count = current_count;
7192 current_count = -1;
7193 current_hold_quit = 0;
7195 if (current_finish == X_EVENT_GOTO_OUT)
7196 break;
7198 #endif /* USE_GTK */
7200 out:;
7202 /* On some systems, an X bug causes Emacs to get no more events
7203 when the window is destroyed. Detect that. (1994.) */
7204 if (! event_found)
7206 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7207 One XNOOP in 100 loops will make Emacs terminate.
7208 B. Bretthauer, 1994 */
7209 x_noop_count++;
7210 if (x_noop_count >= 100)
7212 x_noop_count=0;
7214 if (next_noop_dpyinfo == 0)
7215 next_noop_dpyinfo = x_display_list;
7217 XNoOp (next_noop_dpyinfo->display);
7219 /* Each time we get here, cycle through the displays now open. */
7220 next_noop_dpyinfo = next_noop_dpyinfo->next;
7224 /* If the focus was just given to an auto-raising frame,
7225 raise it now. */
7226 /* ??? This ought to be able to handle more than one such frame. */
7227 if (pending_autoraise_frame)
7229 x_raise_frame (pending_autoraise_frame);
7230 pending_autoraise_frame = 0;
7233 --handling_signal;
7234 UNBLOCK_INPUT;
7236 return count;
7242 /***********************************************************************
7243 Text Cursor
7244 ***********************************************************************/
7246 /* Set clipping for output in glyph row ROW. W is the window in which
7247 we operate. GC is the graphics context to set clipping in.
7249 ROW may be a text row or, e.g., a mode line. Text rows must be
7250 clipped to the interior of the window dedicated to text display,
7251 mode lines must be clipped to the whole window. */
7253 static void
7254 x_clip_to_row (w, row, area, gc)
7255 struct window *w;
7256 struct glyph_row *row;
7257 int area;
7258 GC gc;
7260 struct frame *f = XFRAME (WINDOW_FRAME (w));
7261 XRectangle clip_rect;
7262 int window_x, window_y, window_width;
7264 window_box (w, area, &window_x, &window_y, &window_width, 0);
7266 clip_rect.x = window_x;
7267 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7268 clip_rect.y = max (clip_rect.y, window_y);
7269 clip_rect.width = window_width;
7270 clip_rect.height = row->visible_height;
7272 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7276 /* Draw a hollow box cursor on window W in glyph row ROW. */
7278 static void
7279 x_draw_hollow_cursor (w, row)
7280 struct window *w;
7281 struct glyph_row *row;
7283 struct frame *f = XFRAME (WINDOW_FRAME (w));
7284 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7285 Display *dpy = FRAME_X_DISPLAY (f);
7286 int x, y, wd, h;
7287 XGCValues xgcv;
7288 struct glyph *cursor_glyph;
7289 GC gc;
7291 /* Get the glyph the cursor is on. If we can't tell because
7292 the current matrix is invalid or such, give up. */
7293 cursor_glyph = get_phys_cursor_glyph (w);
7294 if (cursor_glyph == NULL)
7295 return;
7297 /* Compute frame-relative coordinates for phys cursor. */
7298 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7299 wd = w->phys_cursor_width;
7301 /* The foreground of cursor_gc is typically the same as the normal
7302 background color, which can cause the cursor box to be invisible. */
7303 xgcv.foreground = f->output_data.x->cursor_pixel;
7304 if (dpyinfo->scratch_cursor_gc)
7305 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7306 else
7307 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7308 GCForeground, &xgcv);
7309 gc = dpyinfo->scratch_cursor_gc;
7311 /* Set clipping, draw the rectangle, and reset clipping again. */
7312 x_clip_to_row (w, row, TEXT_AREA, gc);
7313 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7314 XSetClipMask (dpy, gc, None);
7318 /* Draw a bar cursor on window W in glyph row ROW.
7320 Implementation note: One would like to draw a bar cursor with an
7321 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7322 Unfortunately, I didn't find a font yet that has this property set.
7323 --gerd. */
7325 static void
7326 x_draw_bar_cursor (w, row, width, kind)
7327 struct window *w;
7328 struct glyph_row *row;
7329 int width;
7330 enum text_cursor_kinds kind;
7332 struct frame *f = XFRAME (w->frame);
7333 struct glyph *cursor_glyph;
7335 /* If cursor is out of bounds, don't draw garbage. This can happen
7336 in mini-buffer windows when switching between echo area glyphs
7337 and mini-buffer. */
7338 cursor_glyph = get_phys_cursor_glyph (w);
7339 if (cursor_glyph == NULL)
7340 return;
7342 /* If on an image, draw like a normal cursor. That's usually better
7343 visible than drawing a bar, esp. if the image is large so that
7344 the bar might not be in the window. */
7345 if (cursor_glyph->type == IMAGE_GLYPH)
7347 struct glyph_row *row;
7348 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7349 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7351 else
7353 Display *dpy = FRAME_X_DISPLAY (f);
7354 Window window = FRAME_X_WINDOW (f);
7355 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7356 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7357 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7358 XGCValues xgcv;
7360 /* If the glyph's background equals the color we normally draw
7361 the bars cursor in, the bar cursor in its normal color is
7362 invisible. Use the glyph's foreground color instead in this
7363 case, on the assumption that the glyph's colors are chosen so
7364 that the glyph is legible. */
7365 if (face->background == f->output_data.x->cursor_pixel)
7366 xgcv.background = xgcv.foreground = face->foreground;
7367 else
7368 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7369 xgcv.graphics_exposures = 0;
7371 if (gc)
7372 XChangeGC (dpy, gc, mask, &xgcv);
7373 else
7375 gc = XCreateGC (dpy, window, mask, &xgcv);
7376 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7379 x_clip_to_row (w, row, TEXT_AREA, gc);
7381 if (kind == BAR_CURSOR)
7383 if (width < 0)
7384 width = FRAME_CURSOR_WIDTH (f);
7385 width = min (cursor_glyph->pixel_width, width);
7387 w->phys_cursor_width = width;
7389 XFillRectangle (dpy, window, gc,
7390 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7391 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7392 width, row->height);
7394 else
7396 int dummy_x, dummy_y, dummy_h;
7398 if (width < 0)
7399 width = row->height;
7401 width = min (row->height, width);
7403 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
7404 &dummy_y, &dummy_h);
7406 XFillRectangle (dpy, window, gc,
7407 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7408 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7409 row->height - width),
7410 w->phys_cursor_width, width);
7413 XSetClipMask (dpy, gc, None);
7418 /* RIF: Define cursor CURSOR on frame F. */
7420 static void
7421 x_define_frame_cursor (f, cursor)
7422 struct frame *f;
7423 Cursor cursor;
7425 if (!f->pointer_invisible
7426 && f->output_data.x->current_cursor != cursor)
7427 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7428 f->output_data.x->current_cursor = cursor;
7432 /* RIF: Clear area on frame F. */
7434 static void
7435 x_clear_frame_area (f, x, y, width, height)
7436 struct frame *f;
7437 int x, y, width, height;
7439 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7440 x, y, width, height, False);
7444 /* RIF: Draw cursor on window W. */
7446 static void
7447 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7448 struct window *w;
7449 struct glyph_row *glyph_row;
7450 int x, y;
7451 int cursor_type, cursor_width;
7452 int on_p, active_p;
7454 struct frame *f = XFRAME (WINDOW_FRAME (w));
7456 if (on_p)
7458 w->phys_cursor_type = cursor_type;
7459 w->phys_cursor_on_p = 1;
7461 if (glyph_row->exact_window_width_line_p
7462 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7464 glyph_row->cursor_in_fringe_p = 1;
7465 draw_fringe_bitmap (w, glyph_row, 0);
7467 else
7468 switch (cursor_type)
7470 case HOLLOW_BOX_CURSOR:
7471 x_draw_hollow_cursor (w, glyph_row);
7472 break;
7474 case FILLED_BOX_CURSOR:
7475 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7476 break;
7478 case BAR_CURSOR:
7479 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7480 break;
7482 case HBAR_CURSOR:
7483 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7484 break;
7486 case NO_CURSOR:
7487 w->phys_cursor_width = 0;
7488 break;
7490 default:
7491 abort ();
7494 #ifdef HAVE_X_I18N
7495 if (w == XWINDOW (f->selected_window))
7496 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7497 xic_set_preeditarea (w, x, y);
7498 #endif
7501 #ifndef XFlush
7502 XFlush (FRAME_X_DISPLAY (f));
7503 #endif
7507 /* Icons. */
7509 /* Make the x-window of frame F use the gnu icon bitmap. */
7512 x_bitmap_icon (f, file)
7513 struct frame *f;
7514 Lisp_Object file;
7516 int bitmap_id;
7518 if (FRAME_X_WINDOW (f) == 0)
7519 return 1;
7521 /* Free up our existing icon bitmap and mask if any. */
7522 if (f->output_data.x->icon_bitmap > 0)
7523 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7524 f->output_data.x->icon_bitmap = 0;
7526 if (STRINGP (file))
7528 #ifdef USE_GTK
7529 /* Use gtk_window_set_icon_from_file () if available,
7530 It's not restricted to bitmaps */
7531 if (xg_set_icon (f, file))
7532 return 0;
7533 #endif /* USE_GTK */
7534 bitmap_id = x_create_bitmap_from_file (f, file);
7535 x_create_bitmap_mask (f, bitmap_id);
7537 else
7539 /* Create the GNU bitmap and mask if necessary. */
7540 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7542 int rc = -1;
7544 #ifdef USE_GTK
7546 if (xg_set_icon (f, xg_default_icon_file)
7547 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7548 return 0;
7550 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7552 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7553 if (rc != -1)
7554 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7556 #endif
7558 /* If all else fails, use the (black and white) xbm image. */
7559 if (rc == -1)
7561 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7562 gnu_xbm_width, gnu_xbm_height);
7563 if (rc == -1)
7564 return 1;
7566 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7567 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7571 /* The first time we create the GNU bitmap and mask,
7572 this increments the ref-count one extra time.
7573 As a result, the GNU bitmap and mask are never freed.
7574 That way, we don't have to worry about allocating it again. */
7575 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7577 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7580 x_wm_set_icon_pixmap (f, bitmap_id);
7581 f->output_data.x->icon_bitmap = bitmap_id;
7583 return 0;
7587 /* Make the x-window of frame F use a rectangle with text.
7588 Use ICON_NAME as the text. */
7591 x_text_icon (f, icon_name)
7592 struct frame *f;
7593 char *icon_name;
7595 if (FRAME_X_WINDOW (f) == 0)
7596 return 1;
7599 XTextProperty text;
7600 text.value = (unsigned char *) icon_name;
7601 text.encoding = XA_STRING;
7602 text.format = 8;
7603 text.nitems = strlen (icon_name);
7604 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7607 if (f->output_data.x->icon_bitmap > 0)
7608 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7609 f->output_data.x->icon_bitmap = 0;
7610 x_wm_set_icon_pixmap (f, 0);
7612 return 0;
7615 #define X_ERROR_MESSAGE_SIZE 200
7617 /* If non-nil, this should be a string.
7618 It means catch X errors and store the error message in this string.
7620 The reason we use a stack is that x_catch_error/x_uncatch_error can
7621 be called from a signal handler.
7624 struct x_error_message_stack {
7625 char string[X_ERROR_MESSAGE_SIZE];
7626 Display *dpy;
7627 struct x_error_message_stack *prev;
7629 static struct x_error_message_stack *x_error_message;
7631 /* An X error handler which stores the error message in
7632 *x_error_message. This is called from x_error_handler if
7633 x_catch_errors is in effect. */
7635 static void
7636 x_error_catcher (display, error)
7637 Display *display;
7638 XErrorEvent *error;
7640 XGetErrorText (display, error->error_code,
7641 x_error_message->string,
7642 X_ERROR_MESSAGE_SIZE);
7645 /* Begin trapping X errors for display DPY. Actually we trap X errors
7646 for all displays, but DPY should be the display you are actually
7647 operating on.
7649 After calling this function, X protocol errors no longer cause
7650 Emacs to exit; instead, they are recorded in the string
7651 stored in *x_error_message.
7653 Calling x_check_errors signals an Emacs error if an X error has
7654 occurred since the last call to x_catch_errors or x_check_errors.
7656 Calling x_uncatch_errors resumes the normal error handling. */
7658 void x_check_errors ();
7660 void
7661 x_catch_errors (dpy)
7662 Display *dpy;
7664 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7666 /* Make sure any errors from previous requests have been dealt with. */
7667 XSync (dpy, False);
7669 data->dpy = dpy;
7670 data->string[0] = 0;
7671 data->prev = x_error_message;
7672 x_error_message = data;
7675 /* Undo the last x_catch_errors call.
7676 DPY should be the display that was passed to x_catch_errors. */
7678 void
7679 x_uncatch_errors ()
7681 struct x_error_message_stack *tmp;
7683 BLOCK_INPUT;
7685 /* The display may have been closed before this function is called.
7686 Check if it is still open before calling XSync. */
7687 if (x_display_info_for_display (x_error_message->dpy) != 0)
7688 XSync (x_error_message->dpy, False);
7690 tmp = x_error_message;
7691 x_error_message = x_error_message->prev;
7692 xfree (tmp);
7693 UNBLOCK_INPUT;
7696 /* If any X protocol errors have arrived since the last call to
7697 x_catch_errors or x_check_errors, signal an Emacs error using
7698 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7700 void
7701 x_check_errors (dpy, format)
7702 Display *dpy;
7703 char *format;
7705 /* Make sure to catch any errors incurred so far. */
7706 XSync (dpy, False);
7708 if (x_error_message->string[0])
7710 char string[X_ERROR_MESSAGE_SIZE];
7711 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7712 x_uncatch_errors ();
7713 error (format, string);
7717 /* Nonzero if we had any X protocol errors
7718 since we did x_catch_errors on DPY. */
7721 x_had_errors_p (dpy)
7722 Display *dpy;
7724 /* Make sure to catch any errors incurred so far. */
7725 XSync (dpy, False);
7727 return x_error_message->string[0] != 0;
7730 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7732 void
7733 x_clear_errors (dpy)
7734 Display *dpy;
7736 x_error_message->string[0] = 0;
7739 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7740 * idea. --lorentey */
7741 /* Close off all unclosed x_catch_errors calls. */
7743 void
7744 x_fully_uncatch_errors ()
7746 while (x_error_message)
7747 x_uncatch_errors ();
7749 #endif
7751 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7754 x_catching_errors ()
7756 return x_error_message != 0;
7759 #if 0
7760 static unsigned int x_wire_count;
7761 x_trace_wire ()
7763 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7765 #endif /* ! 0 */
7768 /* Handle SIGPIPE, which can happen when the connection to a server
7769 simply goes away. SIGPIPE is handled by x_connection_signal.
7770 Don't need to do anything, because the write which caused the
7771 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7772 which will do the appropriate cleanup for us. */
7774 static SIGTYPE
7775 x_connection_signal (signalnum) /* If we don't have an argument, */
7776 int signalnum; /* some compilers complain in signal calls. */
7778 #ifdef USG
7779 /* USG systems forget handlers when they are used;
7780 must reestablish each time */
7781 signal (signalnum, x_connection_signal);
7782 #endif /* USG */
7786 /************************************************************************
7787 Handling X errors
7788 ************************************************************************/
7790 /* Error message passed to x_connection_closed. */
7792 static char *error_msg;
7794 /* Function installed as fatal_error_signal_hook in
7795 x_connection_closed. Print the X error message, and exit normally,
7796 instead of dumping core when XtCloseDisplay fails. */
7798 static void
7799 x_fatal_error_signal ()
7801 fprintf (stderr, "%s\n", error_msg);
7802 exit (70);
7805 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7806 the text of an error message that lead to the connection loss. */
7808 static SIGTYPE
7809 x_connection_closed (dpy, error_message)
7810 Display *dpy;
7811 char *error_message;
7813 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7814 Lisp_Object frame, tail;
7815 int index = SPECPDL_INDEX ();
7817 error_msg = (char *) alloca (strlen (error_message) + 1);
7818 strcpy (error_msg, error_message);
7819 handling_signal = 0;
7821 /* Prevent being called recursively because of an error condition
7822 below. Otherwise, we might end up with printing ``can't find per
7823 display information'' in the recursive call instead of printing
7824 the original message here. */
7825 x_catch_errors (dpy);
7827 /* Inhibit redisplay while frames are being deleted. */
7828 specbind (Qinhibit_redisplay, Qt);
7830 if (dpyinfo)
7832 /* Protect display from being closed when we delete the last
7833 frame on it. */
7834 dpyinfo->reference_count++;
7835 dpyinfo->terminal->reference_count++;
7838 /* First delete frames whose mini-buffers are on frames
7839 that are on the dead display. */
7840 FOR_EACH_FRAME (tail, frame)
7842 Lisp_Object minibuf_frame;
7843 minibuf_frame
7844 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7845 if (FRAME_X_P (XFRAME (frame))
7846 && FRAME_X_P (XFRAME (minibuf_frame))
7847 && ! EQ (frame, minibuf_frame)
7848 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7849 delete_frame (frame, Qnoelisp);
7852 /* Now delete all remaining frames on the dead display.
7853 We are now sure none of these is used as the mini-buffer
7854 for another frame that we need to delete. */
7855 FOR_EACH_FRAME (tail, frame)
7856 if (FRAME_X_P (XFRAME (frame))
7857 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7859 /* Set this to t so that delete_frame won't get confused
7860 trying to find a replacement. */
7861 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7862 delete_frame (frame, Qnoelisp);
7865 /* We have to close the display to inform Xt that it doesn't
7866 exist anymore. If we don't, Xt will continue to wait for
7867 events from the display. As a consequence, a sequence of
7869 M-x make-frame-on-display RET :1 RET
7870 ...kill the new frame, so that we get an IO error...
7871 M-x make-frame-on-display RET :1 RET
7873 will indefinitely wait in Xt for events for display `:1', opened
7874 in the first call to make-frame-on-display.
7876 Closing the display is reported to lead to a bus error on
7877 OpenWindows in certain situations. I suspect that is a bug
7878 in OpenWindows. I don't know how to circumvent it here. */
7880 if (dpyinfo)
7882 #ifdef USE_X_TOOLKIT
7883 /* If DPYINFO is null, this means we didn't open the display
7884 in the first place, so don't try to close it. */
7886 extern void (*fatal_error_signal_hook) P_ ((void));
7887 fatal_error_signal_hook = x_fatal_error_signal;
7888 XtCloseDisplay (dpy);
7889 fatal_error_signal_hook = NULL;
7891 #endif
7893 #ifdef USE_GTK
7894 /* Due to bugs in some Gtk+ versions, just exit here if this
7895 is the last display/terminal. */
7896 if (terminal_list->next_terminal == NULL)
7898 fprintf (stderr, "%s\n", error_msg);
7899 shut_down_emacs (0, 0, Qnil);
7900 exit (70);
7902 xg_display_close (dpyinfo->display);
7903 #endif
7905 /* Indicate that this display is dead. */
7906 dpyinfo->display = 0;
7908 dpyinfo->reference_count--;
7909 dpyinfo->terminal->reference_count--;
7910 if (dpyinfo->reference_count != 0)
7911 /* We have just closed all frames on this display. */
7912 abort ();
7915 Lisp_Object tmp;
7916 XSETTERMINAL (tmp, dpyinfo->terminal);
7917 Fdelete_terminal (tmp, Qnoelisp);
7921 x_uncatch_errors ();
7923 if (terminal_list == 0)
7925 fprintf (stderr, "%s\n", error_msg);
7926 shut_down_emacs (0, 0, Qnil);
7927 exit (70);
7930 /* Ordinary stack unwind doesn't deal with these. */
7931 #ifdef SIGIO
7932 sigunblock (sigmask (SIGIO));
7933 #endif
7934 sigunblock (sigmask (SIGALRM));
7935 TOTALLY_UNBLOCK_INPUT;
7937 unbind_to (index, Qnil);
7938 clear_waiting_for_input ();
7939 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7940 longjmp), because returning from this function would get us back into
7941 Xlib's code which will directly call `exit'. */
7942 error ("%s", error_msg);
7945 /* We specifically use it before defining it, so that gcc doesn't inline it,
7946 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7947 static void x_error_quitter P_ ((Display *, XErrorEvent *));
7949 /* This is the first-level handler for X protocol errors.
7950 It calls x_error_quitter or x_error_catcher. */
7952 static int
7953 x_error_handler (display, error)
7954 Display *display;
7955 XErrorEvent *error;
7957 if (x_error_message)
7958 x_error_catcher (display, error);
7959 else
7960 x_error_quitter (display, error);
7961 return 0;
7964 /* This is the usual handler for X protocol errors.
7965 It kills all frames on the display that we got the error for.
7966 If that was the only one, it prints an error message and kills Emacs. */
7968 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7970 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
7971 #define NO_INLINE __attribute__((noinline))
7972 #else
7973 #define NO_INLINE
7974 #endif
7976 /* Some versions of GNU/Linux define noinline in their headers. */
7978 #ifdef noinline
7979 #undef noinline
7980 #endif
7982 /* On older GCC versions, just putting x_error_quitter
7983 after x_error_handler prevents inlining into the former. */
7985 static void NO_INLINE
7986 x_error_quitter (display, error)
7987 Display *display;
7988 XErrorEvent *error;
7990 char buf[256], buf1[356];
7992 /* Ignore BadName errors. They can happen because of fonts
7993 or colors that are not defined. */
7995 if (error->error_code == BadName)
7996 return;
7998 /* Note that there is no real way portable across R3/R4 to get the
7999 original error handler. */
8001 XGetErrorText (display, error->error_code, buf, sizeof (buf));
8002 sprintf (buf1, "X protocol error: %s on protocol request %d",
8003 buf, error->request_code);
8004 x_connection_closed (display, buf1);
8008 /* This is the handler for X IO errors, always.
8009 It kills all frames on the display that we lost touch with.
8010 If that was the only one, it prints an error message and kills Emacs. */
8012 static int
8013 x_io_error_quitter (display)
8014 Display *display;
8016 char buf[256];
8018 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
8019 x_connection_closed (display, buf);
8020 return 0;
8023 /* Changing the font of the frame. */
8025 /* Give frame F the font FONT-OBJECT as its default font. The return
8026 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
8027 frame. If it is negative, generate a new fontset from
8028 FONT-OBJECT. */
8030 Lisp_Object
8031 x_new_font (f, font_object, fontset)
8032 struct frame *f;
8033 Lisp_Object font_object;
8034 int fontset;
8036 struct font *font = XFONT_OBJECT (font_object);
8038 if (fontset < 0)
8039 fontset = fontset_from_font (font_object);
8040 FRAME_FONTSET (f) = fontset;
8041 if (FRAME_FONT (f) == font)
8042 /* This font is already set in frame F. There's nothing more to
8043 do. */
8044 return font_object;
8046 FRAME_FONT (f) = font;
8047 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8048 FRAME_COLUMN_WIDTH (f) = font->average_width;
8049 FRAME_SPACE_WIDTH (f) = font->space_width;
8050 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8052 compute_fringe_widths (f, 1);
8054 /* Compute the scroll bar width in character columns. */
8055 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8057 int wid = FRAME_COLUMN_WIDTH (f);
8058 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8059 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8061 else
8063 int wid = FRAME_COLUMN_WIDTH (f);
8064 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8067 if (FRAME_X_WINDOW (f) != 0)
8069 /* Don't change the size of a tip frame; there's no point in
8070 doing it because it's done in Fx_show_tip, and it leads to
8071 problems because the tip frame has no widget. */
8072 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8073 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8076 #ifdef HAVE_X_I18N
8077 if (FRAME_XIC (f)
8078 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8080 BLOCK_INPUT;
8081 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8082 UNBLOCK_INPUT;
8084 #endif
8086 return font_object;
8090 /***********************************************************************
8091 X Input Methods
8092 ***********************************************************************/
8094 #ifdef HAVE_X_I18N
8096 #ifdef HAVE_X11R6
8098 /* XIM destroy callback function, which is called whenever the
8099 connection to input method XIM dies. CLIENT_DATA contains a
8100 pointer to the x_display_info structure corresponding to XIM. */
8102 static void
8103 xim_destroy_callback (xim, client_data, call_data)
8104 XIM xim;
8105 XPointer client_data;
8106 XPointer call_data;
8108 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8109 Lisp_Object frame, tail;
8111 BLOCK_INPUT;
8113 /* No need to call XDestroyIC.. */
8114 FOR_EACH_FRAME (tail, frame)
8116 struct frame *f = XFRAME (frame);
8117 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8119 FRAME_XIC (f) = NULL;
8120 xic_free_xfontset (f);
8124 /* No need to call XCloseIM. */
8125 dpyinfo->xim = NULL;
8126 XFree (dpyinfo->xim_styles);
8127 UNBLOCK_INPUT;
8130 #endif /* HAVE_X11R6 */
8132 #ifdef HAVE_X11R6
8133 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8134 extern char *XSetIMValues P_ ((XIM, ...));
8135 #endif
8137 /* Open the connection to the XIM server on display DPYINFO.
8138 RESOURCE_NAME is the resource name Emacs uses. */
8140 static void
8141 xim_open_dpy (dpyinfo, resource_name)
8142 struct x_display_info *dpyinfo;
8143 char *resource_name;
8145 XIM xim;
8147 #ifdef HAVE_XIM
8148 if (use_xim)
8150 if (dpyinfo->xim)
8151 XCloseIM (dpyinfo->xim);
8152 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8153 EMACS_CLASS);
8154 dpyinfo->xim = xim;
8156 if (xim)
8158 #ifdef HAVE_X11R6
8159 XIMCallback destroy;
8160 #endif
8162 /* Get supported styles and XIM values. */
8163 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8165 #ifdef HAVE_X11R6
8166 destroy.callback = xim_destroy_callback;
8167 destroy.client_data = (XPointer)dpyinfo;
8168 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8169 #endif
8173 else
8174 #endif /* HAVE_XIM */
8175 dpyinfo->xim = NULL;
8179 #ifdef HAVE_X11R6_XIM
8181 /* XIM instantiate callback function, which is called whenever an XIM
8182 server is available. DISPLAY is the display of the XIM.
8183 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8184 when the callback was registered. */
8186 static void
8187 xim_instantiate_callback (display, client_data, call_data)
8188 Display *display;
8189 XPointer client_data;
8190 XPointer call_data;
8192 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8193 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8195 /* We don't support multiple XIM connections. */
8196 if (dpyinfo->xim)
8197 return;
8199 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8201 /* Create XIC for the existing frames on the same display, as long
8202 as they have no XIC. */
8203 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8205 Lisp_Object tail, frame;
8207 BLOCK_INPUT;
8208 FOR_EACH_FRAME (tail, frame)
8210 struct frame *f = XFRAME (frame);
8212 if (FRAME_X_P (f)
8213 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8214 if (FRAME_XIC (f) == NULL)
8216 create_frame_xic (f);
8217 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8218 xic_set_statusarea (f);
8219 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8221 struct window *w = XWINDOW (f->selected_window);
8222 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8227 UNBLOCK_INPUT;
8231 #endif /* HAVE_X11R6_XIM */
8234 /* Open a connection to the XIM server on display DPYINFO.
8235 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8236 connection only at the first time. On X11R6, open the connection
8237 in the XIM instantiate callback function. */
8239 static void
8240 xim_initialize (dpyinfo, resource_name)
8241 struct x_display_info *dpyinfo;
8242 char *resource_name;
8244 dpyinfo->xim = NULL;
8245 #ifdef HAVE_XIM
8246 if (use_xim)
8248 #ifdef HAVE_X11R6_XIM
8249 struct xim_inst_t *xim_inst;
8250 int len;
8252 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8253 dpyinfo->xim_callback_data = xim_inst;
8254 xim_inst->dpyinfo = dpyinfo;
8255 len = strlen (resource_name);
8256 xim_inst->resource_name = (char *) xmalloc (len + 1);
8257 bcopy (resource_name, xim_inst->resource_name, len + 1);
8258 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8259 resource_name, EMACS_CLASS,
8260 xim_instantiate_callback,
8261 /* This is XPointer in XFree86
8262 but (XPointer *) on Tru64, at
8263 least, hence the configure test. */
8264 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8265 #else /* not HAVE_X11R6_XIM */
8266 xim_open_dpy (dpyinfo, resource_name);
8267 #endif /* not HAVE_X11R6_XIM */
8269 #endif /* HAVE_XIM */
8273 /* Close the connection to the XIM server on display DPYINFO. */
8275 static void
8276 xim_close_dpy (dpyinfo)
8277 struct x_display_info *dpyinfo;
8279 #ifdef HAVE_XIM
8280 if (use_xim)
8282 #ifdef HAVE_X11R6_XIM
8283 if (dpyinfo->display)
8284 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8285 NULL, EMACS_CLASS,
8286 xim_instantiate_callback, NULL);
8287 xfree (dpyinfo->xim_callback_data->resource_name);
8288 xfree (dpyinfo->xim_callback_data);
8289 #endif /* HAVE_X11R6_XIM */
8290 if (dpyinfo->display)
8291 XCloseIM (dpyinfo->xim);
8292 dpyinfo->xim = NULL;
8293 XFree (dpyinfo->xim_styles);
8295 #endif /* HAVE_XIM */
8298 #endif /* not HAVE_X11R6_XIM */
8302 /* Calculate the absolute position in frame F
8303 from its current recorded position values and gravity. */
8305 void
8306 x_calc_absolute_position (f)
8307 struct frame *f;
8309 int flags = f->size_hint_flags;
8311 /* We have nothing to do if the current position
8312 is already for the top-left corner. */
8313 if (! ((flags & XNegative) || (flags & YNegative)))
8314 return;
8316 /* Treat negative positions as relative to the leftmost bottommost
8317 position that fits on the screen. */
8318 if (flags & XNegative)
8319 f->left_pos = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
8320 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8323 int height = FRAME_PIXEL_HEIGHT (f);
8325 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8326 /* Something is fishy here. When using Motif, starting Emacs with
8327 `-g -0-0', the frame appears too low by a few pixels.
8329 This seems to be so because initially, while Emacs is starting,
8330 the column widget's height and the frame's pixel height are
8331 different. The column widget's height is the right one. In
8332 later invocations, when Emacs is up, the frame's pixel height
8333 is right, though.
8335 It's not obvious where the initial small difference comes from.
8336 2000-12-01, gerd. */
8338 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8339 #endif
8341 if (flags & YNegative)
8342 f->top_pos = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
8343 - height + f->top_pos;
8346 /* The left_pos and top_pos
8347 are now relative to the top and left screen edges,
8348 so the flags should correspond. */
8349 f->size_hint_flags &= ~ (XNegative | YNegative);
8352 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8353 to really change the position, and 0 when calling from
8354 x_make_frame_visible (in that case, XOFF and YOFF are the current
8355 position values). It is -1 when calling from x_set_frame_parameters,
8356 which means, do adjust for borders but don't change the gravity. */
8358 void
8359 x_set_offset (f, xoff, yoff, change_gravity)
8360 struct frame *f;
8361 register int xoff, yoff;
8362 int change_gravity;
8364 int modified_top, modified_left;
8366 if (change_gravity > 0)
8368 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8369 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8371 f->top_pos = yoff;
8372 f->left_pos = xoff;
8373 f->size_hint_flags &= ~ (XNegative | YNegative);
8374 if (xoff < 0)
8375 f->size_hint_flags |= XNegative;
8376 if (yoff < 0)
8377 f->size_hint_flags |= YNegative;
8378 f->win_gravity = NorthWestGravity;
8380 x_calc_absolute_position (f);
8382 BLOCK_INPUT;
8383 x_wm_set_size_hint (f, (long) 0, 0);
8385 modified_left = f->left_pos;
8386 modified_top = f->top_pos;
8388 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8390 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8391 than the WM decorations. So we use the calculated offset instead
8392 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8393 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8394 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8397 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8398 modified_left, modified_top);
8400 x_sync_with_move (f, f->left_pos, f->top_pos,
8401 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8402 ? 1 : 0);
8404 /* change_gravity is non-zero when this function is called from Lisp to
8405 programmatically move a frame. In that case, we call
8406 x_check_expected_move to discover if we have a "Type A" or "Type B"
8407 window manager, and, for a "Type A" window manager, adjust the position
8408 of the frame.
8410 We call x_check_expected_move if a programmatic move occurred, and
8411 either the window manager type (A/B) is unknown or it is Type A but we
8412 need to compute the top/left offset adjustment for this frame. */
8414 if (change_gravity != 0 &&
8415 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8416 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8417 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8418 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8419 x_check_expected_move (f, modified_left, modified_top);
8421 UNBLOCK_INPUT;
8424 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8425 on the root window for frame F contains ATOMNAME.
8426 This is how a WM check shall be done according to the Window Manager
8427 Specification/Extended Window Manager Hints at
8428 http://freedesktop.org/wiki/Specifications/wm-spec. */
8430 static int
8431 wm_supports (f, atomname)
8432 struct frame *f;
8433 const char *atomname;
8435 Atom actual_type;
8436 unsigned long actual_size, bytes_remaining;
8437 int i, rc, actual_format;
8438 Atom prop_atom;
8439 Window wmcheck_window;
8440 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8441 Window target_window = dpyinfo->root_window;
8442 long max_len = 65536;
8443 Display *dpy = FRAME_X_DISPLAY (f);
8444 unsigned char *tmp_data = NULL;
8445 Atom target_type = XA_WINDOW;
8446 Atom want_atom;
8448 BLOCK_INPUT;
8450 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8452 x_catch_errors (dpy);
8453 rc = XGetWindowProperty (dpy, target_window,
8454 prop_atom, 0, max_len, False, target_type,
8455 &actual_type, &actual_format, &actual_size,
8456 &bytes_remaining, &tmp_data);
8458 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8460 if (tmp_data) XFree (tmp_data);
8461 x_uncatch_errors ();
8462 UNBLOCK_INPUT;
8463 return 0;
8466 wmcheck_window = *(Window *) tmp_data;
8467 XFree (tmp_data);
8469 /* Check if window exists. */
8470 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8471 x_sync (f);
8472 if (x_had_errors_p (dpy))
8474 x_uncatch_errors ();
8475 UNBLOCK_INPUT;
8476 return 0;
8479 if (dpyinfo->net_supported_window != wmcheck_window)
8481 /* Window changed, reload atoms */
8482 if (dpyinfo->net_supported_atoms != NULL)
8483 XFree (dpyinfo->net_supported_atoms);
8484 dpyinfo->net_supported_atoms = NULL;
8485 dpyinfo->nr_net_supported_atoms = 0;
8486 dpyinfo->net_supported_window = 0;
8488 target_type = XA_ATOM;
8489 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8490 tmp_data = NULL;
8491 rc = XGetWindowProperty (dpy, target_window,
8492 prop_atom, 0, max_len, False, target_type,
8493 &actual_type, &actual_format, &actual_size,
8494 &bytes_remaining, &tmp_data);
8496 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8498 if (tmp_data) XFree (tmp_data);
8499 x_uncatch_errors ();
8500 UNBLOCK_INPUT;
8501 return 0;
8504 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8505 dpyinfo->nr_net_supported_atoms = actual_size;
8506 dpyinfo->net_supported_window = wmcheck_window;
8509 rc = 0;
8510 want_atom = XInternAtom (dpy, atomname, False);
8512 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8513 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8515 x_uncatch_errors ();
8516 UNBLOCK_INPUT;
8518 return rc;
8521 static void
8522 set_wm_state (frame, add, what, what2)
8523 Lisp_Object frame;
8524 int add;
8525 const char *what;
8526 const char *what2;
8528 const char *atom = "_NET_WM_STATE";
8529 Fx_send_client_event (frame, make_number (0), frame,
8530 make_unibyte_string (atom, strlen (atom)),
8531 make_number (32),
8532 /* 1 = add, 0 = remove */
8533 Fcons
8534 (make_number (add ? 1 : 0),
8535 Fcons
8536 (make_unibyte_string (what, strlen (what)),
8537 what2 != 0
8538 ? Fcons (make_unibyte_string (what2, strlen (what2)),
8539 Qnil)
8540 : Qnil)));
8543 void
8544 x_set_sticky (f, new_value, old_value)
8545 struct frame *f;
8546 Lisp_Object new_value, old_value;
8548 Lisp_Object frame;
8550 XSETFRAME (frame, f);
8551 set_wm_state (frame, NILP (new_value) ? 0 : 1,
8552 "_NET_WM_STATE_STICKY", NULL);
8555 /* Do fullscreen as specified in extended window manager hints */
8557 static int
8558 do_ewmh_fullscreen (f)
8559 struct frame *f;
8561 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8563 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8564 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8565 if (!have_net_atom)
8566 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8568 if (have_net_atom)
8570 Lisp_Object frame;
8571 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8572 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8573 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8575 XSETFRAME (frame, f);
8577 set_wm_state (frame, 0, fs, NULL);
8578 set_wm_state (frame, 0, fh, NULL);
8579 set_wm_state (frame, 0, fw, NULL);
8581 /* If there are _NET_ atoms we assume we have extended window manager
8582 hints. */
8583 switch (f->want_fullscreen)
8585 case FULLSCREEN_BOTH:
8586 set_wm_state (frame, 1, fs, NULL);
8587 break;
8588 case FULLSCREEN_WIDTH:
8589 set_wm_state (frame, 1, fw, NULL);
8590 break;
8591 case FULLSCREEN_HEIGHT:
8592 set_wm_state (frame, 1, fh, NULL);
8593 break;
8594 case FULLSCREEN_MAXIMIZED:
8595 set_wm_state (frame, 1, fw, fh);
8596 break;
8599 f->want_fullscreen = FULLSCREEN_NONE;
8603 return have_net_atom;
8606 static void
8607 XTfullscreen_hook (f)
8608 FRAME_PTR f;
8610 if (f->async_visible)
8612 BLOCK_INPUT;
8613 x_check_fullscreen (f);
8614 x_sync (f);
8615 UNBLOCK_INPUT;
8620 static void
8621 x_handle_net_wm_state (f, event)
8622 struct frame *f;
8623 XPropertyEvent *event;
8625 Atom actual_type;
8626 unsigned long actual_size, bytes_remaining;
8627 int i, rc, actual_format, value = FULLSCREEN_NONE;
8628 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8629 long max_len = 65536;
8630 Display *dpy = FRAME_X_DISPLAY (f);
8631 unsigned char *tmp_data = NULL;
8632 Atom target_type = XA_ATOM;
8633 Lisp_Object lval;
8634 int sticky = 0;
8636 BLOCK_INPUT;
8637 x_catch_errors (dpy);
8638 rc = XGetWindowProperty (dpy, event->window,
8639 event->atom, 0, max_len, False, target_type,
8640 &actual_type, &actual_format, &actual_size,
8641 &bytes_remaining, &tmp_data);
8643 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
8645 if (tmp_data) XFree (tmp_data);
8646 x_uncatch_errors ();
8647 UNBLOCK_INPUT;
8648 return;
8651 x_uncatch_errors ();
8653 for (i = 0; i < actual_size; ++i)
8655 Atom a = ((Atom*)tmp_data)[i];
8656 if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
8658 if (value == FULLSCREEN_HEIGHT)
8659 value = FULLSCREEN_MAXIMIZED;
8660 else
8661 value = FULLSCREEN_WIDTH;
8663 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
8665 if (value == FULLSCREEN_WIDTH)
8666 value = FULLSCREEN_MAXIMIZED;
8667 else
8668 value = FULLSCREEN_HEIGHT;
8670 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen_atom)
8671 value = FULLSCREEN_BOTH;
8672 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
8673 sticky = 1;
8676 lval = Qnil;
8677 switch (value)
8679 case FULLSCREEN_WIDTH:
8680 lval = Qfullwidth;
8681 break;
8682 case FULLSCREEN_HEIGHT:
8683 lval = Qfullheight;
8684 break;
8685 case FULLSCREEN_BOTH:
8686 lval = Qfullboth;
8687 break;
8688 case FULLSCREEN_MAXIMIZED:
8689 lval = Qmaximized;
8690 break;
8693 store_frame_param (f, Qfullscreen, lval);
8694 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
8696 if (tmp_data) XFree (tmp_data);
8697 UNBLOCK_INPUT;
8700 /* Check if we need to resize the frame due to a fullscreen request.
8701 If so needed, resize the frame. */
8702 static void
8703 x_check_fullscreen (f)
8704 struct frame *f;
8706 if (do_ewmh_fullscreen (f))
8707 return;
8709 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8710 return; /* Only fullscreen without WM or with EWM hints (above). */
8712 if (f->want_fullscreen != FULLSCREEN_NONE)
8714 int width = FRAME_COLS (f), height = FRAME_LINES (f);
8715 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8717 switch (f->want_fullscreen)
8719 /* No difference between these two when there is no WM */
8720 case FULLSCREEN_BOTH:
8721 case FULLSCREEN_MAXIMIZED:
8722 width = x_display_pixel_width (dpyinfo);
8723 height = x_display_pixel_height (dpyinfo);
8724 break;
8725 case FULLSCREEN_WIDTH:
8726 width = x_display_pixel_width (dpyinfo);
8727 break;
8728 case FULLSCREEN_HEIGHT:
8729 height = x_display_pixel_height (dpyinfo);
8732 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8734 change_frame_size (f, height, width, 0, 1, 0);
8735 SET_FRAME_GARBAGED (f);
8736 cancel_mouse_face (f);
8741 /* This function is called by x_set_offset to determine whether the window
8742 manager interfered with the positioning of the frame. Type A window
8743 managers position the surrounding window manager decorations a small
8744 amount above and left of the user-supplied position. Type B window
8745 managers position the surrounding window manager decorations at the
8746 user-specified position. If we detect a Type A window manager, we
8747 compensate by moving the window right and down by the proper amount. */
8749 static void
8750 x_check_expected_move (f, expected_left, expected_top)
8751 struct frame *f;
8752 int expected_left;
8753 int expected_top;
8755 int current_left = 0, current_top = 0;
8757 /* x_real_positions returns the left and top offsets of the outermost
8758 window manager window around the frame. */
8760 x_real_positions (f, &current_left, &current_top);
8762 if (current_left != expected_left || current_top != expected_top)
8764 /* It's a "Type A" window manager. */
8766 int adjusted_left;
8767 int adjusted_top;
8769 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8770 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8771 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8773 /* Now fix the mispositioned frame's location. */
8775 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8776 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8778 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8779 adjusted_left, adjusted_top);
8781 x_sync_with_move (f, expected_left, expected_top, 0);
8783 else
8784 /* It's a "Type B" window manager. We don't have to adjust the
8785 frame's position. */
8787 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8791 /* Wait for XGetGeometry to return up-to-date position information for a
8792 recently-moved frame. Call this immediately after calling XMoveWindow.
8793 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8794 frame has been moved to, so we use a fuzzy position comparison instead
8795 of an exact comparison. */
8797 static void
8798 x_sync_with_move (f, left, top, fuzzy)
8799 struct frame *f;
8800 int left, top, fuzzy;
8802 int count = 0;
8804 while (count++ < 50)
8806 int current_left = 0, current_top = 0;
8808 /* In theory, this call to XSync only needs to happen once, but in
8809 practice, it doesn't seem to work, hence the need for the surrounding
8810 loop. */
8812 XSync (FRAME_X_DISPLAY (f), False);
8813 x_real_positions (f, &current_left, &current_top);
8815 if (fuzzy)
8817 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8818 pixels. */
8820 if (eabs (current_left - left) <= 10
8821 && eabs (current_top - top) <= 40)
8822 return;
8824 else if (current_left == left && current_top == top)
8825 return;
8828 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8829 will then return up-to-date position info. */
8831 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8835 /* Wait for an event on frame F matching EVENTTYPE. */
8836 void
8837 x_wait_for_event (f, eventtype)
8838 struct frame *f;
8839 int eventtype;
8841 int level = interrupt_input_blocked;
8843 SELECT_TYPE fds;
8844 EMACS_TIME tmo, tmo_at, time_now;
8845 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
8847 pending_event_wait.f = f;
8848 pending_event_wait.eventtype = eventtype;
8850 /* Set timeout to 0.1 second. Hopefully not noticable.
8851 Maybe it should be configurable. */
8852 EMACS_SET_SECS_USECS (tmo, 0, 100000);
8853 EMACS_GET_TIME (tmo_at);
8854 EMACS_ADD_TIME (tmo_at, tmo_at, tmo);
8856 while (pending_event_wait.eventtype)
8858 interrupt_input_pending = 1;
8859 TOTALLY_UNBLOCK_INPUT;
8860 /* XTread_socket is called after unblock. */
8861 BLOCK_INPUT;
8862 interrupt_input_blocked = level;
8864 FD_ZERO (&fds);
8865 FD_SET (fd, &fds);
8867 EMACS_GET_TIME (time_now);
8868 EMACS_SUB_TIME (tmo, tmo_at, time_now);
8870 if (EMACS_TIME_NEG_P (tmo) || select (fd+1, &fds, NULL, NULL, &tmo) == 0)
8871 break; /* Timeout */
8873 pending_event_wait.f = 0;
8874 pending_event_wait.eventtype = 0;
8878 /* Change the size of frame F's X window to COLS/ROWS in the case F
8879 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8880 top-left-corner window gravity for this size change and subsequent
8881 size changes. Otherwise we leave the window gravity unchanged. */
8883 static void
8884 x_set_window_size_1 (f, change_gravity, cols, rows)
8885 struct frame *f;
8886 int change_gravity;
8887 int cols, rows;
8889 int pixelwidth, pixelheight;
8891 check_frame_size (f, &rows, &cols);
8892 f->scroll_bar_actual_width
8893 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8895 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8896 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8897 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8899 compute_fringe_widths (f, 0);
8901 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
8902 + 2*f->border_width;
8903 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
8904 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f)
8905 + 2*f->border_width;
8907 if (change_gravity) f->win_gravity = NorthWestGravity;
8908 x_wm_set_size_hint (f, (long) 0, 0);
8909 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8910 pixelwidth, pixelheight);
8913 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8914 receive in the ConfigureNotify event; if we get what we asked
8915 for, then the event won't cause the screen to become garbaged, so
8916 we have to make sure to do it here. */
8917 SET_FRAME_GARBAGED (f);
8919 /* Now, strictly speaking, we can't be sure that this is accurate,
8920 but the window manager will get around to dealing with the size
8921 change request eventually, and we'll hear how it went when the
8922 ConfigureNotify event gets here.
8924 We could just not bother storing any of this information here,
8925 and let the ConfigureNotify event set everything up, but that
8926 might be kind of confusing to the Lisp code, since size changes
8927 wouldn't be reported in the frame parameters until some random
8928 point in the future when the ConfigureNotify event arrives.
8930 We pass 1 for DELAY since we can't run Lisp code inside of
8931 a BLOCK_INPUT. */
8933 /* But the ConfigureNotify may in fact never arrive, and then this is
8934 not right if the frame is visible. Instead wait (with timeout)
8935 for the ConfigureNotify. */
8936 if (f->async_visible)
8937 x_wait_for_event (f, ConfigureNotify);
8938 else
8940 change_frame_size (f, rows, cols, 0, 1, 0);
8941 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8942 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8943 x_sync (f);
8948 /* Call this to change the size of frame F's x-window.
8949 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8950 for this size change and subsequent size changes.
8951 Otherwise we leave the window gravity unchanged. */
8953 void
8954 x_set_window_size (f, change_gravity, cols, rows)
8955 struct frame *f;
8956 int change_gravity;
8957 int cols, rows;
8959 BLOCK_INPUT;
8961 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8963 int r, c;
8965 /* When the frame is maximized/fullscreen or running under for
8966 example Xmonad, x_set_window_size_1 will be a no-op.
8967 In that case, the right thing to do is extend rows/cols to
8968 the current frame size. We do that first if x_set_window_size_1
8969 turns out to not be a no-op (there is no way to know).
8970 The size will be adjusted again if the frame gets a
8971 ConfigureNotify event as a result of x_set_window_size. */
8972 int pixelh = FRAME_PIXEL_HEIGHT (f);
8973 #ifdef USE_X_TOOLKIT
8974 /* The menu bar is not part of text lines. The tool bar
8975 is however. */
8976 pixelh -= FRAME_MENUBAR_HEIGHT (f);
8977 #endif
8978 r = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelh);
8979 /* Update f->scroll_bar_actual_width because it is used in
8980 FRAME_PIXEL_WIDTH_TO_TEXT_COLS. */
8981 f->scroll_bar_actual_width
8982 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
8983 c = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, FRAME_PIXEL_WIDTH (f));
8984 change_frame_size (f, r, c, 0, 1, 0);
8987 #ifdef USE_GTK
8988 if (FRAME_GTK_WIDGET (f))
8989 xg_frame_set_char_size (f, cols, rows);
8990 else
8991 x_set_window_size_1 (f, change_gravity, cols, rows);
8992 #else /* not USE_GTK */
8994 x_set_window_size_1 (f, change_gravity, cols, rows);
8996 #endif /* not USE_GTK */
8998 /* If cursor was outside the new size, mark it as off. */
8999 mark_window_cursors_off (XWINDOW (f->root_window));
9001 /* Clear out any recollection of where the mouse highlighting was,
9002 since it might be in a place that's outside the new frame size.
9003 Actually checking whether it is outside is a pain in the neck,
9004 so don't try--just let the highlighting be done afresh with new size. */
9005 cancel_mouse_face (f);
9007 UNBLOCK_INPUT;
9010 /* Mouse warping. */
9012 void
9013 x_set_mouse_position (f, x, y)
9014 struct frame *f;
9015 int x, y;
9017 int pix_x, pix_y;
9019 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
9020 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
9022 if (pix_x < 0) pix_x = 0;
9023 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
9025 if (pix_y < 0) pix_y = 0;
9026 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
9028 BLOCK_INPUT;
9030 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9031 0, 0, 0, 0, pix_x, pix_y);
9032 UNBLOCK_INPUT;
9035 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9037 void
9038 x_set_mouse_pixel_position (f, pix_x, pix_y)
9039 struct frame *f;
9040 int pix_x, pix_y;
9042 BLOCK_INPUT;
9044 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9045 0, 0, 0, 0, pix_x, pix_y);
9046 UNBLOCK_INPUT;
9049 /* focus shifting, raising and lowering. */
9051 void
9052 x_focus_on_frame (f)
9053 struct frame *f;
9055 #if 0
9056 /* I don't think that the ICCCM allows programs to do things like this
9057 without the interaction of the window manager. Whatever you end up
9058 doing with this code, do it to x_unfocus_frame too. */
9059 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9060 RevertToPointerRoot, CurrentTime);
9061 #endif /* ! 0 */
9064 void
9065 x_unfocus_frame (f)
9066 struct frame *f;
9068 #if 0
9069 /* Look at the remarks in x_focus_on_frame. */
9070 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
9071 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
9072 RevertToPointerRoot, CurrentTime);
9073 #endif /* ! 0 */
9076 /* Raise frame F. */
9078 void
9079 x_raise_frame (f)
9080 struct frame *f;
9082 BLOCK_INPUT;
9083 if (f->async_visible)
9084 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9086 XFlush (FRAME_X_DISPLAY (f));
9087 UNBLOCK_INPUT;
9090 /* Lower frame F. */
9092 void
9093 x_lower_frame (f)
9094 struct frame *f;
9096 if (f->async_visible)
9098 BLOCK_INPUT;
9099 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9100 XFlush (FRAME_X_DISPLAY (f));
9101 UNBLOCK_INPUT;
9105 /* Activate frame with Extended Window Manager Hints */
9107 void
9108 x_ewmh_activate_frame (f)
9109 FRAME_PTR f;
9111 /* See Window Manager Specification/Extended Window Manager Hints at
9112 http://freedesktop.org/wiki/Specifications/wm-spec */
9114 const char *atom = "_NET_ACTIVE_WINDOW";
9115 if (f->async_visible && wm_supports (f, atom))
9117 Lisp_Object frame;
9118 XSETFRAME (frame, f);
9119 Fx_send_client_event (frame, make_number (0), frame,
9120 make_unibyte_string (atom, strlen (atom)),
9121 make_number (32),
9122 Fcons (make_number (1),
9123 Fcons (make_number (last_user_time),
9124 Qnil)));
9128 static void
9129 XTframe_raise_lower (f, raise_flag)
9130 FRAME_PTR f;
9131 int raise_flag;
9133 if (raise_flag)
9134 x_raise_frame (f);
9135 else
9136 x_lower_frame (f);
9139 /* XEmbed implementation. */
9141 void
9142 xembed_set_info (f, flags)
9143 struct frame *f;
9144 enum xembed_info flags;
9146 Atom atom;
9147 unsigned long data[2];
9149 atom = XInternAtom (FRAME_X_DISPLAY (f), "_XEMBED_INFO", False);
9151 data[0] = XEMBED_VERSION;
9152 data[1] = flags;
9154 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), atom, atom,
9155 32, PropModeReplace, (unsigned char *) data, 2);
9158 void
9159 xembed_send_message (f, time, message, detail, data1, data2)
9160 struct frame *f;
9161 Time time;
9162 enum xembed_message message;
9163 long detail;
9164 long data1;
9165 long data2;
9167 XEvent event;
9169 event.xclient.type = ClientMessage;
9170 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9171 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
9172 event.xclient.format = 32;
9173 event.xclient.data.l[0] = time;
9174 event.xclient.data.l[1] = message;
9175 event.xclient.data.l[2] = detail;
9176 event.xclient.data.l[3] = data1;
9177 event.xclient.data.l[4] = data2;
9179 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9180 False, NoEventMask, &event);
9181 XSync (FRAME_X_DISPLAY (f), False);
9184 /* Change of visibility. */
9186 /* This tries to wait until the frame is really visible.
9187 However, if the window manager asks the user where to position
9188 the frame, this will return before the user finishes doing that.
9189 The frame will not actually be visible at that time,
9190 but it will become visible later when the window manager
9191 finishes with it. */
9193 void
9194 x_make_frame_visible (f)
9195 struct frame *f;
9197 Lisp_Object type;
9198 int original_top, original_left;
9199 int retry_count = 2;
9201 retry:
9203 BLOCK_INPUT;
9205 type = x_icon_type (f);
9206 if (!NILP (type))
9207 x_bitmap_icon (f, type);
9209 if (! FRAME_VISIBLE_P (f))
9211 /* We test FRAME_GARBAGED_P here to make sure we don't
9212 call x_set_offset a second time
9213 if we get to x_make_frame_visible a second time
9214 before the window gets really visible. */
9215 if (! FRAME_ICONIFIED_P (f)
9216 && ! FRAME_X_EMBEDDED_P (f)
9217 && ! f->output_data.x->asked_for_visible)
9218 x_set_offset (f, f->left_pos, f->top_pos, 0);
9220 f->output_data.x->asked_for_visible = 1;
9222 if (! EQ (Vx_no_window_manager, Qt))
9223 x_wm_set_window_state (f, NormalState);
9224 #ifdef USE_X_TOOLKIT
9225 if (FRAME_X_EMBEDDED_P (f))
9226 xembed_set_info (f, XEMBED_MAPPED);
9227 else
9229 /* This was XtPopup, but that did nothing for an iconified frame. */
9230 XtMapWidget (f->output_data.x->widget);
9232 #else /* not USE_X_TOOLKIT */
9233 #ifdef USE_GTK
9234 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9235 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9236 #else
9237 if (FRAME_X_EMBEDDED_P (f))
9238 xembed_set_info (f, XEMBED_MAPPED);
9239 else
9240 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9241 #endif /* not USE_GTK */
9242 #endif /* not USE_X_TOOLKIT */
9245 XFlush (FRAME_X_DISPLAY (f));
9247 /* Synchronize to ensure Emacs knows the frame is visible
9248 before we do anything else. We do this loop with input not blocked
9249 so that incoming events are handled. */
9251 Lisp_Object frame;
9252 int count;
9253 /* This must be before UNBLOCK_INPUT
9254 since events that arrive in response to the actions above
9255 will set it when they are handled. */
9256 int previously_visible = f->output_data.x->has_been_visible;
9258 original_left = f->left_pos;
9259 original_top = f->top_pos;
9261 /* This must come after we set COUNT. */
9262 UNBLOCK_INPUT;
9264 /* We unblock here so that arriving X events are processed. */
9266 /* Now move the window back to where it was "supposed to be".
9267 But don't do it if the gravity is negative.
9268 When the gravity is negative, this uses a position
9269 that is 3 pixels too low. Perhaps that's really the border width.
9271 Don't do this if the window has never been visible before,
9272 because the window manager may choose the position
9273 and we don't want to override it. */
9275 if (! FRAME_VISIBLE_P (f)
9276 && ! FRAME_ICONIFIED_P (f)
9277 && ! FRAME_X_EMBEDDED_P (f)
9278 && f->win_gravity == NorthWestGravity
9279 && previously_visible)
9281 Drawable rootw;
9282 int x, y;
9283 unsigned int width, height, border, depth;
9285 BLOCK_INPUT;
9287 /* On some window managers (such as FVWM) moving an existing
9288 window, even to the same place, causes the window manager
9289 to introduce an offset. This can cause the window to move
9290 to an unexpected location. Check the geometry (a little
9291 slow here) and then verify that the window is in the right
9292 place. If the window is not in the right place, move it
9293 there, and take the potential window manager hit. */
9294 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9295 &rootw, &x, &y, &width, &height, &border, &depth);
9297 if (original_left != x || original_top != y)
9298 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9299 original_left, original_top);
9301 UNBLOCK_INPUT;
9304 XSETFRAME (frame, f);
9306 /* Wait until the frame is visible. Process X events until a
9307 MapNotify event has been seen, or until we think we won't get a
9308 MapNotify at all.. */
9309 for (count = input_signal_count + 10;
9310 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9312 /* Force processing of queued events. */
9313 x_sync (f);
9315 /* Machines that do polling rather than SIGIO have been
9316 observed to go into a busy-wait here. So we'll fake an
9317 alarm signal to let the handler know that there's something
9318 to be read. We used to raise a real alarm, but it seems
9319 that the handler isn't always enabled here. This is
9320 probably a bug. */
9321 if (input_polling_used ())
9323 /* It could be confusing if a real alarm arrives while
9324 processing the fake one. Turn it off and let the
9325 handler reset it. */
9326 extern void poll_for_input_1 P_ ((void));
9327 int old_poll_suppress_count = poll_suppress_count;
9328 poll_suppress_count = 1;
9329 poll_for_input_1 ();
9330 poll_suppress_count = old_poll_suppress_count;
9333 /* See if a MapNotify event has been processed. */
9334 FRAME_SAMPLE_VISIBILITY (f);
9337 /* 2000-09-28: In
9339 (let ((f (selected-frame)))
9340 (iconify-frame f)
9341 (raise-frame f))
9343 the frame is not raised with various window managers on
9344 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9345 unknown reason, the call to XtMapWidget is completely ignored.
9346 Mapping the widget a second time works. */
9348 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9349 goto retry;
9353 /* Change from mapped state to withdrawn state. */
9355 /* Make the frame visible (mapped and not iconified). */
9357 void
9358 x_make_frame_invisible (f)
9359 struct frame *f;
9361 Window window;
9363 /* Use the frame's outermost window, not the one we normally draw on. */
9364 window = FRAME_OUTER_WINDOW (f);
9366 /* Don't keep the highlight on an invisible frame. */
9367 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9368 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9370 BLOCK_INPUT;
9372 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9373 that the current position of the window is user-specified, rather than
9374 program-specified, so that when the window is mapped again, it will be
9375 placed at the same location, without forcing the user to position it
9376 by hand again (they have already done that once for this window.) */
9377 x_wm_set_size_hint (f, (long) 0, 1);
9379 #ifdef USE_GTK
9380 if (FRAME_GTK_OUTER_WIDGET (f))
9381 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9382 else
9383 #else
9384 if (FRAME_X_EMBEDDED_P (f))
9385 xembed_set_info (f, 0);
9386 else
9387 #endif
9390 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9391 DefaultScreen (FRAME_X_DISPLAY (f))))
9393 UNBLOCK_INPUT_RESIGNAL;
9394 error ("Can't notify window manager of window withdrawal");
9398 /* We can't distinguish this from iconification
9399 just by the event that we get from the server.
9400 So we can't win using the usual strategy of letting
9401 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9402 and synchronize with the server to make sure we agree. */
9403 f->visible = 0;
9404 FRAME_ICONIFIED_P (f) = 0;
9405 f->async_visible = 0;
9406 f->async_iconified = 0;
9408 x_sync (f);
9410 UNBLOCK_INPUT;
9413 /* Change window state from mapped to iconified. */
9415 void
9416 x_iconify_frame (f)
9417 struct frame *f;
9419 int result;
9420 Lisp_Object type;
9422 /* Don't keep the highlight on an invisible frame. */
9423 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9424 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9426 if (f->async_iconified)
9427 return;
9429 BLOCK_INPUT;
9431 FRAME_SAMPLE_VISIBILITY (f);
9433 type = x_icon_type (f);
9434 if (!NILP (type))
9435 x_bitmap_icon (f, type);
9437 #ifdef USE_GTK
9438 if (FRAME_GTK_OUTER_WIDGET (f))
9440 if (! FRAME_VISIBLE_P (f))
9441 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9443 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9444 f->iconified = 1;
9445 f->visible = 1;
9446 f->async_iconified = 1;
9447 f->async_visible = 0;
9448 UNBLOCK_INPUT;
9449 return;
9451 #endif
9453 #ifdef USE_X_TOOLKIT
9455 if (! FRAME_VISIBLE_P (f))
9457 if (! EQ (Vx_no_window_manager, Qt))
9458 x_wm_set_window_state (f, IconicState);
9459 /* This was XtPopup, but that did nothing for an iconified frame. */
9460 XtMapWidget (f->output_data.x->widget);
9461 /* The server won't give us any event to indicate
9462 that an invisible frame was changed to an icon,
9463 so we have to record it here. */
9464 f->iconified = 1;
9465 f->visible = 1;
9466 f->async_iconified = 1;
9467 f->async_visible = 0;
9468 UNBLOCK_INPUT;
9469 return;
9472 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9473 XtWindow (f->output_data.x->widget),
9474 DefaultScreen (FRAME_X_DISPLAY (f)));
9475 UNBLOCK_INPUT;
9477 if (!result)
9478 error ("Can't notify window manager of iconification");
9480 f->async_iconified = 1;
9481 f->async_visible = 0;
9484 BLOCK_INPUT;
9485 XFlush (FRAME_X_DISPLAY (f));
9486 UNBLOCK_INPUT;
9487 #else /* not USE_X_TOOLKIT */
9489 /* Make sure the X server knows where the window should be positioned,
9490 in case the user deiconifies with the window manager. */
9491 if (! FRAME_VISIBLE_P (f)
9492 && ! FRAME_ICONIFIED_P (f)
9493 && ! FRAME_X_EMBEDDED_P (f))
9494 x_set_offset (f, f->left_pos, f->top_pos, 0);
9496 /* Since we don't know which revision of X we're running, we'll use both
9497 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9499 /* X11R4: send a ClientMessage to the window manager using the
9500 WM_CHANGE_STATE type. */
9502 XEvent message;
9504 message.xclient.window = FRAME_X_WINDOW (f);
9505 message.xclient.type = ClientMessage;
9506 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9507 message.xclient.format = 32;
9508 message.xclient.data.l[0] = IconicState;
9510 if (! XSendEvent (FRAME_X_DISPLAY (f),
9511 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9512 False,
9513 SubstructureRedirectMask | SubstructureNotifyMask,
9514 &message))
9516 UNBLOCK_INPUT_RESIGNAL;
9517 error ("Can't notify window manager of iconification");
9521 /* X11R3: set the initial_state field of the window manager hints to
9522 IconicState. */
9523 x_wm_set_window_state (f, IconicState);
9525 if (!FRAME_VISIBLE_P (f))
9527 /* If the frame was withdrawn, before, we must map it. */
9528 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9531 f->async_iconified = 1;
9532 f->async_visible = 0;
9534 XFlush (FRAME_X_DISPLAY (f));
9535 UNBLOCK_INPUT;
9536 #endif /* not USE_X_TOOLKIT */
9540 /* Free X resources of frame F. */
9542 void
9543 x_free_frame_resources (f)
9544 struct frame *f;
9546 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9547 Lisp_Object bar;
9548 struct scroll_bar *b;
9550 BLOCK_INPUT;
9552 /* If a display connection is dead, don't try sending more
9553 commands to the X server. */
9554 if (dpyinfo->display)
9556 /* We must free faces before destroying windows because some
9557 font-driver (e.g. xft) access a window while finishing a
9558 face. */
9559 if (FRAME_FACE_CACHE (f))
9560 free_frame_faces (f);
9562 if (f->output_data.x->icon_desc)
9563 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9565 #ifdef USE_X_TOOLKIT
9566 /* Explicitly destroy the scroll bars of the frame. Without
9567 this, we get "BadDrawable" errors from the toolkit later on,
9568 presumably from expose events generated for the disappearing
9569 toolkit scroll bars. */
9570 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9572 b = XSCROLL_BAR (bar);
9573 x_scroll_bar_remove (b);
9575 #endif
9577 #ifdef HAVE_X_I18N
9578 if (FRAME_XIC (f))
9579 free_frame_xic (f);
9580 #endif
9582 #ifdef USE_X_TOOLKIT
9583 if (f->output_data.x->widget)
9585 XtDestroyWidget (f->output_data.x->widget);
9586 f->output_data.x->widget = NULL;
9588 /* Tooltips don't have widgets, only a simple X window, even if
9589 we are using a toolkit. */
9590 else if (FRAME_X_WINDOW (f))
9591 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9593 free_frame_menubar (f);
9594 #else /* !USE_X_TOOLKIT */
9596 #ifdef USE_GTK
9597 /* In the GTK version, tooltips are normal X
9598 frames. We must check and free both types. */
9599 if (FRAME_GTK_OUTER_WIDGET (f))
9601 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9602 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9603 FRAME_GTK_OUTER_WIDGET (f) = 0;
9605 #endif /* USE_GTK */
9607 if (FRAME_X_WINDOW (f))
9608 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9609 #endif /* !USE_X_TOOLKIT */
9611 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9612 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9613 unload_color (f, f->output_data.x->cursor_pixel);
9614 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9615 unload_color (f, f->output_data.x->border_pixel);
9616 unload_color (f, f->output_data.x->mouse_pixel);
9618 if (f->output_data.x->scroll_bar_background_pixel != -1)
9619 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9620 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9621 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9622 #ifdef USE_TOOLKIT_SCROLL_BARS
9623 /* Scrollbar shadow colors. */
9624 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9625 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9626 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9627 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9628 #endif /* USE_TOOLKIT_SCROLL_BARS */
9629 if (f->output_data.x->white_relief.allocated_p)
9630 unload_color (f, f->output_data.x->white_relief.pixel);
9631 if (f->output_data.x->black_relief.allocated_p)
9632 unload_color (f, f->output_data.x->black_relief.pixel);
9634 x_free_gcs (f);
9635 XFlush (FRAME_X_DISPLAY (f));
9638 xfree (f->output_data.x->saved_menu_event);
9639 xfree (f->output_data.x);
9640 f->output_data.x = NULL;
9642 if (f == dpyinfo->x_focus_frame)
9643 dpyinfo->x_focus_frame = 0;
9644 if (f == dpyinfo->x_focus_event_frame)
9645 dpyinfo->x_focus_event_frame = 0;
9646 if (f == dpyinfo->x_highlight_frame)
9647 dpyinfo->x_highlight_frame = 0;
9649 if (f == dpyinfo->mouse_face_mouse_frame)
9651 dpyinfo->mouse_face_beg_row
9652 = dpyinfo->mouse_face_beg_col = -1;
9653 dpyinfo->mouse_face_end_row
9654 = dpyinfo->mouse_face_end_col = -1;
9655 dpyinfo->mouse_face_window = Qnil;
9656 dpyinfo->mouse_face_deferred_gc = 0;
9657 dpyinfo->mouse_face_mouse_frame = 0;
9660 UNBLOCK_INPUT;
9664 /* Destroy the X window of frame F. */
9666 void
9667 x_destroy_window (f)
9668 struct frame *f;
9670 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9672 /* If a display connection is dead, don't try sending more
9673 commands to the X server. */
9674 if (dpyinfo->display != 0)
9675 x_free_frame_resources (f);
9677 dpyinfo->reference_count--;
9681 /* Setting window manager hints. */
9683 /* Set the normal size hints for the window manager, for frame F.
9684 FLAGS is the flags word to use--or 0 meaning preserve the flags
9685 that the window now has.
9686 If USER_POSITION is nonzero, we set the USPosition
9687 flag (this is useful when FLAGS is 0).
9688 The GTK version is in gtkutils.c */
9690 #ifndef USE_GTK
9691 void
9692 x_wm_set_size_hint (f, flags, user_position)
9693 struct frame *f;
9694 long flags;
9695 int user_position;
9697 XSizeHints size_hints;
9698 Window window = FRAME_OUTER_WINDOW (f);
9700 /* Setting PMaxSize caused various problems. */
9701 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9703 size_hints.x = f->left_pos;
9704 size_hints.y = f->top_pos;
9706 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9707 size_hints.width = FRAME_PIXEL_WIDTH (f);
9709 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9710 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9711 size_hints.max_width = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
9712 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9713 size_hints.max_height = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
9714 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9716 /* Calculate the base and minimum sizes. */
9718 int base_width, base_height;
9719 int min_rows = 0, min_cols = 0;
9721 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9722 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9724 check_frame_size (f, &min_rows, &min_cols);
9726 /* The window manager uses the base width hints to calculate the
9727 current number of rows and columns in the frame while
9728 resizing; min_width and min_height aren't useful for this
9729 purpose, since they might not give the dimensions for a
9730 zero-row, zero-column frame.
9732 We use the base_width and base_height members if we have
9733 them; otherwise, we set the min_width and min_height members
9734 to the size for a zero x zero frame. */
9736 size_hints.flags |= PBaseSize;
9737 size_hints.base_width = base_width;
9738 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
9739 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9740 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9743 /* If we don't need the old flags, we don't need the old hint at all. */
9744 if (flags)
9746 size_hints.flags |= flags;
9747 goto no_read;
9751 XSizeHints hints; /* Sometimes I hate X Windows... */
9752 long supplied_return;
9753 int value;
9755 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9756 &supplied_return);
9758 if (flags)
9759 size_hints.flags |= flags;
9760 else
9762 if (value == 0)
9763 hints.flags = 0;
9764 if (hints.flags & PSize)
9765 size_hints.flags |= PSize;
9766 if (hints.flags & PPosition)
9767 size_hints.flags |= PPosition;
9768 if (hints.flags & USPosition)
9769 size_hints.flags |= USPosition;
9770 if (hints.flags & USSize)
9771 size_hints.flags |= USSize;
9775 no_read:
9777 #ifdef PWinGravity
9778 size_hints.win_gravity = f->win_gravity;
9779 size_hints.flags |= PWinGravity;
9781 if (user_position)
9783 size_hints.flags &= ~ PPosition;
9784 size_hints.flags |= USPosition;
9786 #endif /* PWinGravity */
9788 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9790 #endif /* not USE_GTK */
9792 /* Used for IconicState or NormalState */
9794 void
9795 x_wm_set_window_state (f, state)
9796 struct frame *f;
9797 int state;
9799 #ifdef USE_X_TOOLKIT
9800 Arg al[1];
9802 XtSetArg (al[0], XtNinitialState, state);
9803 XtSetValues (f->output_data.x->widget, al, 1);
9804 #else /* not USE_X_TOOLKIT */
9805 Window window = FRAME_X_WINDOW (f);
9807 f->output_data.x->wm_hints.flags |= StateHint;
9808 f->output_data.x->wm_hints.initial_state = state;
9810 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9811 #endif /* not USE_X_TOOLKIT */
9814 void
9815 x_wm_set_icon_pixmap (f, pixmap_id)
9816 struct frame *f;
9817 int pixmap_id;
9819 Pixmap icon_pixmap, icon_mask;
9821 #ifndef USE_X_TOOLKIT
9822 Window window = FRAME_OUTER_WINDOW (f);
9823 #endif
9825 if (pixmap_id > 0)
9827 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9828 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9829 icon_mask = x_bitmap_mask (f, pixmap_id);
9830 f->output_data.x->wm_hints.icon_mask = icon_mask;
9832 else
9834 /* It seems there is no way to turn off use of an icon
9835 pixmap. */
9836 return;
9840 #ifdef USE_GTK
9842 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9843 return;
9846 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9849 Arg al[1];
9850 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9851 XtSetValues (f->output_data.x->widget, al, 1);
9852 XtSetArg (al[0], XtNiconMask, icon_mask);
9853 XtSetValues (f->output_data.x->widget, al, 1);
9856 #else /* not USE_X_TOOLKIT && not USE_GTK */
9858 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9859 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9861 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9864 void
9865 x_wm_set_icon_position (f, icon_x, icon_y)
9866 struct frame *f;
9867 int icon_x, icon_y;
9869 Window window = FRAME_OUTER_WINDOW (f);
9871 f->output_data.x->wm_hints.flags |= IconPositionHint;
9872 f->output_data.x->wm_hints.icon_x = icon_x;
9873 f->output_data.x->wm_hints.icon_y = icon_y;
9875 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9879 /***********************************************************************
9880 Fonts
9881 ***********************************************************************/
9883 #if GLYPH_DEBUG
9885 /* Check that FONT is valid on frame F. It is if it can be found in F's
9886 font table. */
9888 static void
9889 x_check_font (f, font)
9890 struct frame *f;
9891 struct font *font;
9893 Lisp_Object frame;
9895 xassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9896 if (font->driver->check)
9897 xassert (font->driver->check (f, font) == 0);
9900 #endif /* GLYPH_DEBUG != 0 */
9903 /***********************************************************************
9904 Initialization
9905 ***********************************************************************/
9907 #ifdef USE_X_TOOLKIT
9908 static XrmOptionDescRec emacs_options[] = {
9909 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9910 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9912 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9913 XrmoptionSepArg, NULL},
9914 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9916 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9917 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9918 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9919 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9920 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9921 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9922 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9925 /* Whether atimer for Xt timeouts is activated or not. */
9927 static int x_timeout_atimer_activated_flag;
9929 #endif /* USE_X_TOOLKIT */
9931 static int x_initialized;
9933 #ifdef HAVE_X_SM
9934 static int x_session_initialized;
9935 #endif
9937 /* Test whether two display-name strings agree up to the dot that separates
9938 the screen number from the server number. */
9939 static int
9940 same_x_server (name1, name2)
9941 const char *name1, *name2;
9943 int seen_colon = 0;
9944 const unsigned char *system_name = SDATA (Vsystem_name);
9945 int system_name_length = strlen (system_name);
9946 int length_until_period = 0;
9948 while (system_name[length_until_period] != 0
9949 && system_name[length_until_period] != '.')
9950 length_until_period++;
9952 /* Treat `unix' like an empty host name. */
9953 if (! strncmp (name1, "unix:", 5))
9954 name1 += 4;
9955 if (! strncmp (name2, "unix:", 5))
9956 name2 += 4;
9957 /* Treat this host's name like an empty host name. */
9958 if (! strncmp (name1, system_name, system_name_length)
9959 && name1[system_name_length] == ':')
9960 name1 += system_name_length;
9961 if (! strncmp (name2, system_name, system_name_length)
9962 && name2[system_name_length] == ':')
9963 name2 += system_name_length;
9964 /* Treat this host's domainless name like an empty host name. */
9965 if (! strncmp (name1, system_name, length_until_period)
9966 && name1[length_until_period] == ':')
9967 name1 += length_until_period;
9968 if (! strncmp (name2, system_name, length_until_period)
9969 && name2[length_until_period] == ':')
9970 name2 += length_until_period;
9972 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
9974 if (*name1 == ':')
9975 seen_colon++;
9976 if (seen_colon && *name1 == '.')
9977 return 1;
9979 return (seen_colon
9980 && (*name1 == '.' || *name1 == '\0')
9981 && (*name2 == '.' || *name2 == '\0'));
9984 /* Count number of set bits in mask and number of bits to shift to
9985 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
9986 to 5. */
9987 static void
9988 get_bits_and_offset (mask, bits, offset)
9989 unsigned long mask;
9990 int *bits;
9991 int *offset;
9993 int nr = 0;
9994 int off = 0;
9996 while (!(mask & 1))
9998 off++;
9999 mask >>= 1;
10002 while (mask & 1)
10004 nr++;
10005 mask >>= 1;
10008 *offset = off;
10009 *bits = nr;
10012 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
10013 But don't permanently open it, just test its availability. */
10016 x_display_ok (display)
10017 const char *display;
10019 int dpy_ok = 1;
10020 Display *dpy;
10022 dpy = XOpenDisplay (display);
10023 if (dpy)
10024 XCloseDisplay (dpy);
10025 else
10026 dpy_ok = 0;
10027 return dpy_ok;
10030 #ifdef USE_GTK
10031 static void
10032 my_log_handler (log_domain, log_level, message, user_data)
10033 const gchar *log_domain;
10034 GLogLevelFlags log_level;
10035 const gchar *message;
10036 gpointer user_data;
10038 if (!strstr (message, "g_set_prgname"))
10039 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, message);
10041 #endif
10043 /* Open a connection to X display DISPLAY_NAME, and return
10044 the structure that describes the open display.
10045 If we cannot contact the display, return null. */
10047 struct x_display_info *
10048 x_term_init (display_name, xrm_option, resource_name)
10049 Lisp_Object display_name;
10050 char *xrm_option;
10051 char *resource_name;
10053 int connection;
10054 Display *dpy;
10055 struct terminal *terminal;
10056 struct x_display_info *dpyinfo;
10057 XrmDatabase xrdb;
10059 BLOCK_INPUT;
10061 if (!x_initialized)
10063 x_initialize ();
10064 ++x_initialized;
10067 if (! x_display_ok (SDATA (display_name)))
10068 error ("Display %s can't be opened", SDATA (display_name));
10070 #ifdef USE_GTK
10072 #define NUM_ARGV 10
10073 int argc;
10074 char *argv[NUM_ARGV];
10075 char **argv2 = argv;
10076 GdkAtom atom;
10077 guint id;
10078 #ifndef HAVE_GTK_MULTIDISPLAY
10079 if (!EQ (Vinitial_window_system, Qx))
10080 error ("Sorry, you cannot connect to X servers with the GTK toolkit");
10081 #endif
10083 if (x_initialized++ > 1)
10085 #ifdef HAVE_GTK_MULTIDISPLAY
10086 /* Opening another display. If xg_display_open returns less
10087 than zero, we are probably on GTK 2.0, which can only handle
10088 one display. GTK 2.2 or later can handle more than one. */
10089 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10090 #endif
10091 error ("Sorry, this version of GTK can only handle one display");
10093 else
10095 for (argc = 0; argc < NUM_ARGV; ++argc)
10096 argv[argc] = 0;
10098 argc = 0;
10099 argv[argc++] = initial_argv[0];
10101 if (! NILP (display_name))
10103 argv[argc++] = "--display";
10104 argv[argc++] = SDATA (display_name);
10107 argv[argc++] = "--name";
10108 argv[argc++] = resource_name;
10110 XSetLocaleModifiers ("");
10112 /* Work around GLib bug that outputs a faulty warning. See
10113 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
10114 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
10115 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
10116 gtk_init (&argc, &argv2);
10117 g_log_remove_handler ("GLib", id);
10119 /* gtk_init does set_locale. We must fix locale after calling it. */
10120 fixup_locale ();
10121 xg_initialize ();
10123 dpy = GDK_DISPLAY ();
10125 /* NULL window -> events for all windows go to our function */
10126 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10128 /* Load our own gtkrc if it exists. */
10130 char *file = "~/.emacs.d/gtkrc";
10131 Lisp_Object s, abs_file;
10133 s = make_string (file, strlen (file));
10134 abs_file = Fexpand_file_name (s, Qnil);
10136 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10137 gtk_rc_parse (SDATA (abs_file));
10140 XSetErrorHandler (x_error_handler);
10141 XSetIOErrorHandler (x_io_error_quitter);
10144 #else /* not USE_GTK */
10145 #ifdef USE_X_TOOLKIT
10146 /* weiner@footloose.sps.mot.com reports that this causes
10147 errors with X11R5:
10148 X protocol error: BadAtom (invalid Atom parameter)
10149 on protocol request 18skiloaf.
10150 So let's not use it until R6. */
10151 #ifdef HAVE_X11XTR6
10152 XtSetLanguageProc (NULL, NULL, NULL);
10153 #endif
10156 int argc = 0;
10157 char *argv[3];
10159 argv[0] = "";
10160 argc = 1;
10161 if (xrm_option)
10163 argv[argc++] = "-xrm";
10164 argv[argc++] = xrm_option;
10166 turn_on_atimers (0);
10167 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
10168 resource_name, EMACS_CLASS,
10169 emacs_options, XtNumber (emacs_options),
10170 &argc, argv);
10171 turn_on_atimers (1);
10173 #ifdef HAVE_X11XTR6
10174 /* I think this is to compensate for XtSetLanguageProc. */
10175 fixup_locale ();
10176 #endif
10179 #else /* not USE_X_TOOLKIT */
10180 XSetLocaleModifiers ("");
10181 dpy = XOpenDisplay (SDATA (display_name));
10182 #endif /* not USE_X_TOOLKIT */
10183 #endif /* not USE_GTK*/
10185 /* Detect failure. */
10186 if (dpy == 0)
10188 UNBLOCK_INPUT;
10189 return 0;
10192 /* We have definitely succeeded. Record the new connection. */
10194 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10195 bzero (dpyinfo, sizeof *dpyinfo);
10197 terminal = x_create_terminal (dpyinfo);
10200 struct x_display_info *share;
10201 Lisp_Object tail;
10203 for (share = x_display_list, tail = x_display_name_list; share;
10204 share = share->next, tail = XCDR (tail))
10205 if (same_x_server (SDATA (XCAR (XCAR (tail))),
10206 SDATA (display_name)))
10207 break;
10208 if (share)
10209 terminal->kboard = share->terminal->kboard;
10210 else
10212 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10213 init_kboard (terminal->kboard);
10214 terminal->kboard->Vwindow_system = Qx;
10215 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10217 char *vendor = ServerVendor (dpy);
10218 /* Temporarily hide the partially initialized terminal */
10219 terminal_list = terminal->next_terminal;
10220 UNBLOCK_INPUT;
10221 terminal->kboard->Vsystem_key_alist
10222 = call1 (Qvendor_specific_keysyms,
10223 vendor ? build_string (vendor) : empty_unibyte_string);
10224 BLOCK_INPUT;
10225 terminal->next_terminal = terminal_list;
10226 terminal_list = terminal;
10229 terminal->kboard->next_kboard = all_kboards;
10230 all_kboards = terminal->kboard;
10231 /* Don't let the initial kboard remain current longer than necessary.
10232 That would cause problems if a file loaded on startup tries to
10233 prompt in the mini-buffer. */
10234 if (current_kboard == initial_kboard)
10235 current_kboard = terminal->kboard;
10237 terminal->kboard->reference_count++;
10240 /* Put this display on the chain. */
10241 dpyinfo->next = x_display_list;
10242 x_display_list = dpyinfo;
10244 /* Put it on x_display_name_list as well, to keep them parallel. */
10245 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10246 x_display_name_list);
10247 dpyinfo->name_list_element = XCAR (x_display_name_list);
10249 dpyinfo->display = dpy;
10251 /* Set the name of the terminal. */
10252 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10253 strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
10254 terminal->name[SBYTES (display_name)] = 0;
10256 #if 0
10257 XSetAfterFunction (x_current_display, x_trace_wire);
10258 #endif /* ! 0 */
10260 dpyinfo->x_id_name
10261 = (char *) xmalloc (SBYTES (Vinvocation_name)
10262 + SBYTES (Vsystem_name)
10263 + 2);
10264 sprintf (dpyinfo->x_id_name, "%s@%s",
10265 SDATA (Vinvocation_name), SDATA (Vsystem_name));
10267 /* Figure out which modifier bits mean what. */
10268 x_find_modifier_meanings (dpyinfo);
10270 /* Get the scroll bar cursor. */
10271 #ifdef USE_GTK
10272 /* We must create a GTK cursor, it is required for GTK widgets. */
10273 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10274 #endif /* USE_GTK */
10276 dpyinfo->vertical_scroll_bar_cursor
10277 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10279 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10280 resource_name, EMACS_CLASS);
10281 #ifdef HAVE_XRMSETDATABASE
10282 XrmSetDatabase (dpyinfo->display, xrdb);
10283 #else
10284 dpyinfo->display->db = xrdb;
10285 #endif
10286 /* Put the rdb where we can find it in a way that works on
10287 all versions. */
10288 dpyinfo->xrdb = xrdb;
10290 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10291 DefaultScreen (dpyinfo->display));
10292 select_visual (dpyinfo);
10293 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10294 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10295 dpyinfo->client_leader_window = 0;
10296 dpyinfo->grabbed = 0;
10297 dpyinfo->reference_count = 0;
10298 dpyinfo->icon_bitmap_id = -1;
10299 dpyinfo->n_fonts = 0;
10300 dpyinfo->bitmaps = 0;
10301 dpyinfo->bitmaps_size = 0;
10302 dpyinfo->bitmaps_last = 0;
10303 dpyinfo->scratch_cursor_gc = 0;
10304 dpyinfo->mouse_face_mouse_frame = 0;
10305 dpyinfo->mouse_face_deferred_gc = 0;
10306 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
10307 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
10308 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10309 dpyinfo->mouse_face_window = Qnil;
10310 dpyinfo->mouse_face_overlay = Qnil;
10311 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
10312 dpyinfo->mouse_face_defer = 0;
10313 dpyinfo->mouse_face_hidden = 0;
10314 dpyinfo->x_focus_frame = 0;
10315 dpyinfo->x_focus_event_frame = 0;
10316 dpyinfo->x_highlight_frame = 0;
10317 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10319 /* See if we can construct pixel values from RGB values. */
10320 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10321 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10323 if (dpyinfo->visual->class == TrueColor)
10325 get_bits_and_offset (dpyinfo->visual->red_mask,
10326 &dpyinfo->red_bits, &dpyinfo->red_offset);
10327 get_bits_and_offset (dpyinfo->visual->blue_mask,
10328 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10329 get_bits_and_offset (dpyinfo->visual->green_mask,
10330 &dpyinfo->green_bits, &dpyinfo->green_offset);
10333 /* See if a private colormap is requested. */
10334 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10336 if (dpyinfo->visual->class == PseudoColor)
10338 Lisp_Object value;
10339 value = display_x_get_resource (dpyinfo,
10340 build_string ("privateColormap"),
10341 build_string ("PrivateColormap"),
10342 Qnil, Qnil);
10343 if (STRINGP (value)
10344 && (!strcmp (SDATA (value), "true")
10345 || !strcmp (SDATA (value), "on")))
10346 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10349 else
10350 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10351 dpyinfo->visual, AllocNone);
10353 #ifdef HAVE_XFT
10355 /* If we are using Xft, check dpi value in X resources.
10356 It is better we use it as well, since Xft will use it, as will all
10357 Gnome applications. If our real DPI is smaller or larger than the
10358 one Xft uses, our font will look smaller or larger than other
10359 for other applications, even if it is the same font name (monospace-10
10360 for example). */
10361 char *v = XGetDefault (dpyinfo->display, "Xft", "dpi");
10362 double d;
10363 if (v != NULL && sscanf (v, "%lf", &d) == 1)
10364 dpyinfo->resy = dpyinfo->resx = d;
10366 #endif
10368 if (dpyinfo->resy < 1)
10370 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10371 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10372 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10373 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10374 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10375 pixels = DisplayWidth (dpyinfo->display, screen_number);
10376 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10377 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10378 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10381 dpyinfo->Xatom_wm_protocols
10382 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
10383 dpyinfo->Xatom_wm_take_focus
10384 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
10385 dpyinfo->Xatom_wm_save_yourself
10386 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
10387 dpyinfo->Xatom_wm_delete_window
10388 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
10389 dpyinfo->Xatom_wm_change_state
10390 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
10391 dpyinfo->Xatom_wm_configure_denied
10392 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
10393 dpyinfo->Xatom_wm_window_moved
10394 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
10395 dpyinfo->Xatom_wm_client_leader
10396 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
10397 dpyinfo->Xatom_editres
10398 = XInternAtom (dpyinfo->display, "Editres", False);
10399 dpyinfo->Xatom_CLIPBOARD
10400 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
10401 dpyinfo->Xatom_TIMESTAMP
10402 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
10403 dpyinfo->Xatom_TEXT
10404 = XInternAtom (dpyinfo->display, "TEXT", False);
10405 dpyinfo->Xatom_COMPOUND_TEXT
10406 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
10407 dpyinfo->Xatom_UTF8_STRING
10408 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
10409 dpyinfo->Xatom_DELETE
10410 = XInternAtom (dpyinfo->display, "DELETE", False);
10411 dpyinfo->Xatom_MULTIPLE
10412 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
10413 dpyinfo->Xatom_INCR
10414 = XInternAtom (dpyinfo->display, "INCR", False);
10415 dpyinfo->Xatom_EMACS_TMP
10416 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
10417 dpyinfo->Xatom_TARGETS
10418 = XInternAtom (dpyinfo->display, "TARGETS", False);
10419 dpyinfo->Xatom_NULL
10420 = XInternAtom (dpyinfo->display, "NULL", False);
10421 dpyinfo->Xatom_ATOM_PAIR
10422 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
10423 /* For properties of font. */
10424 dpyinfo->Xatom_PIXEL_SIZE
10425 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
10426 dpyinfo->Xatom_AVERAGE_WIDTH
10427 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
10428 dpyinfo->Xatom_MULE_BASELINE_OFFSET
10429 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
10430 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
10431 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
10432 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
10433 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
10435 /* Ghostscript support. */
10436 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
10437 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
10439 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
10440 False);
10442 dpyinfo->Xatom_XEMBED = XInternAtom (dpyinfo->display, "_XEMBED",
10443 False);
10445 dpyinfo->Xatom_net_wm_state
10446 = XInternAtom (dpyinfo->display, "_NET_WM_STATE", False);
10447 dpyinfo->Xatom_net_wm_state_fullscreen_atom
10448 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_FULLSCREEN", False);
10449 dpyinfo->Xatom_net_wm_state_maximized_horz
10450 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
10451 dpyinfo->Xatom_net_wm_state_maximized_vert
10452 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
10453 dpyinfo->Xatom_net_wm_state_sticky
10454 = XInternAtom (dpyinfo->display, "_NET_WM_STATE_STICKY", False);
10455 dpyinfo->Xatom_net_window_type
10456 = XInternAtom (dpyinfo->display, "_NET_WM_WINDOW_TYPE", False);
10457 dpyinfo->Xatom_net_window_type_tooltip
10458 = XInternAtom (dpyinfo->display, "_NET_WM_WINDOW_TYPE_TOOLTIP", False);
10460 dpyinfo->cut_buffers_initialized = 0;
10462 dpyinfo->x_dnd_atoms_size = 8;
10463 dpyinfo->x_dnd_atoms_length = 0;
10464 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10465 * dpyinfo->x_dnd_atoms_size);
10467 dpyinfo->net_supported_atoms = NULL;
10468 dpyinfo->nr_net_supported_atoms = 0;
10469 dpyinfo->net_supported_window = 0;
10471 connection = ConnectionNumber (dpyinfo->display);
10472 dpyinfo->connection = connection;
10475 extern int gray_bitmap_width, gray_bitmap_height;
10476 extern char *gray_bitmap_bits;
10477 dpyinfo->gray
10478 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10479 gray_bitmap_bits,
10480 gray_bitmap_width, gray_bitmap_height,
10481 (unsigned long) 1, (unsigned long) 0, 1);
10484 #ifdef HAVE_X_I18N
10485 xim_initialize (dpyinfo, resource_name);
10486 #endif
10488 xsettings_initialize (dpyinfo);
10490 #ifdef subprocesses
10491 /* This is only needed for distinguishing keyboard and process input. */
10492 if (connection != 0)
10493 add_keyboard_wait_descriptor (connection);
10494 #endif
10496 #ifdef F_SETOWN
10497 fcntl (connection, F_SETOWN, getpid ());
10498 #endif /* ! defined (F_SETOWN) */
10500 #ifdef SIGIO
10501 if (interrupt_input)
10502 init_sigio (connection);
10503 #endif /* ! defined (SIGIO) */
10505 #ifdef USE_LUCID
10507 Display *dpy = dpyinfo->display;
10508 XrmValue d, fr, to;
10509 Font font;
10511 d.addr = (XPointer)&dpy;
10512 d.size = sizeof (Display *);
10513 fr.addr = XtDefaultFont;
10514 fr.size = sizeof (XtDefaultFont);
10515 to.size = sizeof (Font *);
10516 to.addr = (XPointer)&font;
10517 x_catch_errors (dpy);
10518 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10519 abort ();
10520 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10521 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10522 x_uncatch_errors ();
10524 #endif
10526 /* See if we should run in synchronous mode. This is useful
10527 for debugging X code. */
10529 Lisp_Object value;
10530 value = display_x_get_resource (dpyinfo,
10531 build_string ("synchronous"),
10532 build_string ("Synchronous"),
10533 Qnil, Qnil);
10534 if (STRINGP (value)
10535 && (!strcmp (SDATA (value), "true")
10536 || !strcmp (SDATA (value), "on")))
10537 XSynchronize (dpyinfo->display, True);
10541 Lisp_Object value;
10542 value = display_x_get_resource (dpyinfo,
10543 build_string ("useXIM"),
10544 build_string ("UseXIM"),
10545 Qnil, Qnil);
10546 #ifdef USE_XIM
10547 if (STRINGP (value)
10548 && (!strcmp (SDATA (value), "false")
10549 || !strcmp (SDATA (value), "off")))
10550 use_xim = 0;
10551 #else
10552 if (STRINGP (value)
10553 && (!strcmp (SDATA (value), "true")
10554 || !strcmp (SDATA (value), "on")))
10555 use_xim = 1;
10556 #endif
10559 #ifdef HAVE_X_SM
10560 /* Only do this for the very first display in the Emacs session.
10561 Ignore X session management when Emacs was first started on a
10562 tty. */
10563 if (terminal->id == 1)
10564 x_session_initialize (dpyinfo);
10565 #endif
10567 UNBLOCK_INPUT;
10569 return dpyinfo;
10572 /* Get rid of display DPYINFO, deleting all frames on it,
10573 and without sending any more commands to the X server. */
10575 void
10576 x_delete_display (dpyinfo)
10577 struct x_display_info *dpyinfo;
10579 int i;
10580 struct terminal *t;
10582 /* Close all frames and delete the generic struct terminal for this
10583 X display. */
10584 for (t = terminal_list; t; t = t->next_terminal)
10585 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10587 #ifdef HAVE_X_SM
10588 /* Close X session management when we close its display. */
10589 if (t->id == 1 && x_session_have_connection ())
10590 x_session_close();
10591 #endif
10592 delete_terminal (t);
10593 break;
10596 delete_keyboard_wait_descriptor (dpyinfo->connection);
10598 /* Discard this display from x_display_name_list and x_display_list.
10599 We can't use Fdelq because that can quit. */
10600 if (! NILP (x_display_name_list)
10601 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10602 x_display_name_list = XCDR (x_display_name_list);
10603 else
10605 Lisp_Object tail;
10607 tail = x_display_name_list;
10608 while (CONSP (tail) && CONSP (XCDR (tail)))
10610 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10612 XSETCDR (tail, XCDR (XCDR (tail)));
10613 break;
10615 tail = XCDR (tail);
10619 if (next_noop_dpyinfo == dpyinfo)
10620 next_noop_dpyinfo = dpyinfo->next;
10622 if (x_display_list == dpyinfo)
10623 x_display_list = dpyinfo->next;
10624 else
10626 struct x_display_info *tail;
10628 for (tail = x_display_list; tail; tail = tail->next)
10629 if (tail->next == dpyinfo)
10630 tail->next = tail->next->next;
10633 xfree (dpyinfo->x_id_name);
10634 xfree (dpyinfo->x_dnd_atoms);
10635 xfree (dpyinfo->color_cells);
10636 xfree (dpyinfo);
10639 #ifdef USE_X_TOOLKIT
10641 /* Atimer callback function for TIMER. Called every 0.1s to process
10642 Xt timeouts, if needed. We must avoid calling XtAppPending as
10643 much as possible because that function does an implicit XFlush
10644 that slows us down. */
10646 static void
10647 x_process_timeouts (timer)
10648 struct atimer *timer;
10650 BLOCK_INPUT;
10651 x_timeout_atimer_activated_flag = 0;
10652 if (toolkit_scroll_bar_interaction || popup_activated ())
10654 while (XtAppPending (Xt_app_con) & XtIMTimer)
10655 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10656 /* Reactivate the atimer for next time. */
10657 x_activate_timeout_atimer ();
10659 UNBLOCK_INPUT;
10662 /* Install an asynchronous timer that processes Xt timeout events
10663 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10664 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10665 function whenever these variables are set. This is necessary
10666 because some widget sets use timeouts internally, for example the
10667 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10668 processed, these widgets don't behave normally. */
10670 void
10671 x_activate_timeout_atimer ()
10673 BLOCK_INPUT;
10674 if (!x_timeout_atimer_activated_flag)
10676 EMACS_TIME interval;
10678 EMACS_SET_SECS_USECS (interval, 0, 100000);
10679 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10680 x_timeout_atimer_activated_flag = 1;
10682 UNBLOCK_INPUT;
10685 #endif /* USE_X_TOOLKIT */
10688 /* Set up use of X before we make the first connection. */
10690 extern frame_parm_handler x_frame_parm_handlers[];
10692 static struct redisplay_interface x_redisplay_interface =
10694 x_frame_parm_handlers,
10695 x_produce_glyphs,
10696 x_write_glyphs,
10697 x_insert_glyphs,
10698 x_clear_end_of_line,
10699 x_scroll_run,
10700 x_after_update_window_line,
10701 x_update_window_begin,
10702 x_update_window_end,
10703 x_cursor_to,
10704 x_flush,
10705 #ifdef XFlush
10706 x_flush,
10707 #else
10708 0, /* flush_display_optional */
10709 #endif
10710 x_clear_window_mouse_face,
10711 x_get_glyph_overhangs,
10712 x_fix_overlapping_area,
10713 x_draw_fringe_bitmap,
10714 0, /* define_fringe_bitmap */
10715 0, /* destroy_fringe_bitmap */
10716 x_compute_glyph_string_overhangs,
10717 x_draw_glyph_string,
10718 x_define_frame_cursor,
10719 x_clear_frame_area,
10720 x_draw_window_cursor,
10721 x_draw_vertical_window_border,
10722 x_shift_glyphs_for_insert
10726 /* This function is called when the last frame on a display is deleted. */
10727 void
10728 x_delete_terminal (struct terminal *terminal)
10730 struct x_display_info *dpyinfo = terminal->display_info.x;
10731 int i;
10733 /* Protect against recursive calls. delete_frame in
10734 delete_terminal calls us back when it deletes our last frame. */
10735 if (!terminal->name)
10736 return;
10738 BLOCK_INPUT;
10739 #ifdef HAVE_X_I18N
10740 /* We must close our connection to the XIM server before closing the
10741 X display. */
10742 if (dpyinfo->xim)
10743 xim_close_dpy (dpyinfo);
10744 #endif
10746 /* If called from x_connection_closed, the display may already be closed
10747 and dpyinfo->display was set to 0 to indicate that. */
10748 if (dpyinfo->display)
10750 x_destroy_all_bitmaps (dpyinfo);
10751 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10753 /* Whether or not XCloseDisplay destroys the associated resource
10754 database depends on the version of libX11. To avoid both
10755 crash and memory leak, we dissociate the database from the
10756 display and then destroy dpyinfo->xrdb ourselves.
10758 Unfortunately, the above strategy does not work in some
10759 situations due to a bug in newer versions of libX11: because
10760 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
10761 dpy->db is NULL, XCloseDisplay destroys the associated
10762 database whereas it has not been created by XGetDefault
10763 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
10764 don't destroy the database here in order to avoid the crash
10765 in the above situations for now, though that may cause memory
10766 leaks in other situations. */
10767 #if 0
10768 #ifdef HAVE_XRMSETDATABASE
10769 XrmSetDatabase (dpyinfo->display, NULL);
10770 #else
10771 dpyinfo->display->db = NULL;
10772 #endif
10773 /* We used to call XrmDestroyDatabase from x_delete_display, but
10774 some older versions of libX11 crash if we call it after
10775 closing all the displays. */
10776 XrmDestroyDatabase (dpyinfo->xrdb);
10777 #endif
10779 #ifdef USE_GTK
10780 xg_display_close (dpyinfo->display);
10781 #else
10782 #ifdef USE_X_TOOLKIT
10783 XtCloseDisplay (dpyinfo->display);
10784 #else
10785 XCloseDisplay (dpyinfo->display);
10786 #endif
10787 #endif /* ! USE_GTK */
10790 /* Mark as dead. */
10791 dpyinfo->display = NULL;
10792 x_delete_display (dpyinfo);
10793 UNBLOCK_INPUT;
10796 /* Create a struct terminal, initialize it with the X11 specific
10797 functions and make DISPLAY->TERMINAL point to it. */
10799 static struct terminal *
10800 x_create_terminal (struct x_display_info *dpyinfo)
10802 struct terminal *terminal;
10804 terminal = create_terminal ();
10806 terminal->type = output_x_window;
10807 terminal->display_info.x = dpyinfo;
10808 dpyinfo->terminal = terminal;
10810 /* kboard is initialized in x_term_init. */
10812 terminal->clear_frame_hook = x_clear_frame;
10813 terminal->ins_del_lines_hook = x_ins_del_lines;
10814 terminal->delete_glyphs_hook = x_delete_glyphs;
10815 terminal->ring_bell_hook = XTring_bell;
10816 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
10817 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
10818 terminal->set_terminal_modes_hook = XTset_terminal_modes;
10819 terminal->update_begin_hook = x_update_begin;
10820 terminal->update_end_hook = x_update_end;
10821 terminal->set_terminal_window_hook = XTset_terminal_window;
10822 terminal->read_socket_hook = XTread_socket;
10823 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10824 terminal->mouse_position_hook = XTmouse_position;
10825 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10826 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10827 terminal->fullscreen_hook = XTfullscreen_hook;
10828 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10829 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10830 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10831 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10833 terminal->delete_frame_hook = x_destroy_window;
10834 terminal->delete_terminal_hook = x_delete_terminal;
10836 terminal->rif = &x_redisplay_interface;
10837 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10838 terminal->char_ins_del_ok = 1;
10839 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10840 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10841 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10842 off the bottom. */
10844 return terminal;
10847 void
10848 x_initialize ()
10850 baud_rate = 19200;
10852 x_noop_count = 0;
10853 last_tool_bar_item = -1;
10854 any_help_event_p = 0;
10855 ignore_next_mouse_click_timeout = 0;
10856 #ifdef HAVE_X_SM
10857 x_session_initialized = 0;
10858 #endif
10860 #ifdef USE_GTK
10861 current_count = -1;
10862 #endif
10864 /* Try to use interrupt input; if we can't, then start polling. */
10865 Fset_input_interrupt_mode (Qt);
10867 #ifdef USE_X_TOOLKIT
10868 XtToolkitInitialize ();
10870 Xt_app_con = XtCreateApplicationContext ();
10872 /* Register a converter from strings to pixels, which uses
10873 Emacs' color allocation infrastructure. */
10874 XtAppSetTypeConverter (Xt_app_con,
10875 XtRString, XtRPixel, cvt_string_to_pixel,
10876 cvt_string_to_pixel_args,
10877 XtNumber (cvt_string_to_pixel_args),
10878 XtCacheByDisplay, cvt_pixel_dtor);
10880 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10881 #endif
10883 #ifdef USE_TOOLKIT_SCROLL_BARS
10884 #ifndef USE_GTK
10885 xaw3d_arrow_scroll = False;
10886 xaw3d_pick_top = True;
10887 #endif
10888 #endif
10890 pending_autoraise_frame = 0;
10891 pending_event_wait.f = 0;
10892 pending_event_wait.eventtype = 0;
10894 /* Note that there is no real way portable across R3/R4 to get the
10895 original error handler. */
10896 XSetErrorHandler (x_error_handler);
10897 XSetIOErrorHandler (x_io_error_quitter);
10899 signal (SIGPIPE, x_connection_signal);
10901 xgselect_initialize ();
10905 void
10906 syms_of_xterm ()
10908 x_error_message = NULL;
10910 staticpro (&x_display_name_list);
10911 x_display_name_list = Qnil;
10913 staticpro (&last_mouse_scroll_bar);
10914 last_mouse_scroll_bar = Qnil;
10916 staticpro (&Qvendor_specific_keysyms);
10917 Qvendor_specific_keysyms = intern_c_string ("vendor-specific-keysyms");
10919 staticpro (&Qlatin_1);
10920 Qlatin_1 = intern_c_string ("latin-1");
10922 staticpro (&last_mouse_press_frame);
10923 last_mouse_press_frame = Qnil;
10925 #ifdef USE_GTK
10926 xg_default_icon_file = make_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
10927 staticpro (&xg_default_icon_file);
10929 Qx_gtk_map_stock = intern_c_string ("x-gtk-map-stock");
10930 staticpro (&Qx_gtk_map_stock);
10931 #endif
10933 DEFVAR_BOOL ("x-use-underline-position-properties",
10934 &x_use_underline_position_properties,
10935 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10936 A value of nil means ignore them. If you encounter fonts with bogus
10937 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10938 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10939 to override the font's UNDERLINE_POSITION for small font display
10940 sizes. */);
10941 x_use_underline_position_properties = 1;
10943 DEFVAR_BOOL ("x-underline-at-descent-line",
10944 &x_underline_at_descent_line,
10945 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
10946 A value of nil means to draw the underline according to the value of the
10947 variable `x-use-underline-position-properties', which is usually at the
10948 baseline level. The default value is nil. */);
10949 x_underline_at_descent_line = 0;
10951 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10952 &x_mouse_click_focus_ignore_position,
10953 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10954 This variable is only used when the window manager requires that you
10955 click on a frame to select it (give it focus). In that case, a value
10956 of nil, means that the selected window and cursor position changes to
10957 reflect the mouse click position, while a non-nil value means that the
10958 selected window or cursor position is preserved. */);
10959 x_mouse_click_focus_ignore_position = 0;
10961 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
10962 doc: /* What X toolkit scroll bars Emacs uses.
10963 A value of nil means Emacs doesn't use X toolkit scroll bars.
10964 Otherwise, value is a symbol describing the X toolkit. */);
10965 #ifdef USE_TOOLKIT_SCROLL_BARS
10966 #ifdef USE_MOTIF
10967 Vx_toolkit_scroll_bars = intern_c_string ("motif");
10968 #elif defined HAVE_XAW3D
10969 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
10970 #elif USE_GTK
10971 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
10972 #else
10973 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
10974 #endif
10975 #else
10976 Vx_toolkit_scroll_bars = Qnil;
10977 #endif
10979 staticpro (&last_mouse_motion_frame);
10980 last_mouse_motion_frame = Qnil;
10982 Qmodifier_value = intern_c_string ("modifier-value");
10983 Qalt = intern_c_string ("alt");
10984 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10985 Qhyper = intern_c_string ("hyper");
10986 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10987 Qmeta = intern_c_string ("meta");
10988 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10989 Qsuper = intern_c_string ("super");
10990 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10992 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
10993 doc: /* Which keys Emacs uses for the alt modifier.
10994 This should be one of the symbols `alt', `hyper', `meta', `super'.
10995 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10996 is nil, which is the same as `alt'. */);
10997 Vx_alt_keysym = Qnil;
10999 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
11000 doc: /* Which keys Emacs uses for the hyper modifier.
11001 This should be one of the symbols `alt', `hyper', `meta', `super'.
11002 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11003 default is nil, which is the same as `hyper'. */);
11004 Vx_hyper_keysym = Qnil;
11006 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
11007 doc: /* Which keys Emacs uses for the meta modifier.
11008 This should be one of the symbols `alt', `hyper', `meta', `super'.
11009 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11010 default is nil, which is the same as `meta'. */);
11011 Vx_meta_keysym = Qnil;
11013 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
11014 doc: /* Which keys Emacs uses for the super modifier.
11015 This should be one of the symbols `alt', `hyper', `meta', `super'.
11016 For example, `super' means use the Super_L and Super_R keysyms. The
11017 default is nil, which is the same as `super'. */);
11018 Vx_super_keysym = Qnil;
11020 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
11021 doc: /* Hash table of character codes indexed by X keysym codes. */);
11022 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
11023 make_float (DEFAULT_REHASH_SIZE),
11024 make_float (DEFAULT_REHASH_THRESHOLD),
11025 Qnil, Qnil, Qnil);
11028 #endif /* HAVE_X_WINDOWS */
11030 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
11031 (do not change this comment) */