merge from trunk
[emacs.git] / src / xterm.c
blob52a2809ffbbc6aadc2790e876272267642c0fb77
1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1989, 1993-2013 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
21 /* Xt features made by Fred Pierresteguy. */
23 #include <config.h>
24 #include <stdio.h>
26 #ifdef HAVE_X_WINDOWS
28 #include "lisp.h"
29 #include "blockinput.h"
30 #include "syssignal.h"
32 /* This may include sys/types.h, and that somehow loses
33 if this is not done before the other system files. */
34 #include "xterm.h"
35 #include <X11/cursorfont.h>
37 /* Load sys/types.h if not already loaded.
38 In some systems loading it twice is suicidal. */
39 #ifndef makedev
40 #include <sys/types.h>
41 #endif /* makedev */
43 #include <sys/ioctl.h>
45 #include "systime.h"
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <sys/stat.h>
50 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
51 /* #include <sys/param.h> */
53 #include "charset.h"
54 #include "character.h"
55 #include "coding.h"
56 #include "frame.h"
57 #include "dispextern.h"
58 #ifdef HAVE_XWIDGETS
59 #include "xwidget.h"
60 #endif
61 #include "fontset.h"
62 #include "termhooks.h"
63 #include "termopts.h"
64 #include "termchar.h"
65 #include "emacs-icon.h"
66 #include "disptab.h"
67 #include "buffer.h"
68 #include "window.h"
69 #include "keyboard.h"
70 #include "intervals.h"
71 #include "process.h"
72 #include "atimer.h"
73 #include "keymap.h"
74 #include "font.h"
75 #include "xsettings.h"
76 #include "xgselect.h"
77 #include "sysselect.h"
79 #ifdef USE_X_TOOLKIT
80 #include <X11/Shell.h>
81 #endif
83 #include <unistd.h>
85 #ifdef USE_GTK
86 #include "gtkutil.h"
87 #ifdef HAVE_GTK3
88 #include <X11/Xproto.h>
89 #endif
90 #endif
92 #ifdef USE_LUCID
93 #include "../lwlib/xlwmenu.h"
94 #endif
96 #ifdef USE_X_TOOLKIT
97 #if !defined (NO_EDITRES)
98 #define HACK_EDITRES
99 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
100 #endif /* not NO_EDITRES */
102 /* Include toolkit specific headers for the scroll bar widget. */
104 #ifdef USE_TOOLKIT_SCROLL_BARS
105 #if defined USE_MOTIF
106 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
107 #include <Xm/ScrollBar.h>
108 #else /* !USE_MOTIF i.e. use Xaw */
110 #ifdef HAVE_XAW3D
111 #include <X11/Xaw3d/Simple.h>
112 #include <X11/Xaw3d/Scrollbar.h>
113 #include <X11/Xaw3d/ThreeD.h>
114 #else /* !HAVE_XAW3D */
115 #include <X11/Xaw/Simple.h>
116 #include <X11/Xaw/Scrollbar.h>
117 #endif /* !HAVE_XAW3D */
118 #ifndef XtNpickTop
119 #define XtNpickTop "pickTop"
120 #endif /* !XtNpickTop */
121 #endif /* !USE_MOTIF */
122 #endif /* USE_TOOLKIT_SCROLL_BARS */
124 #endif /* USE_X_TOOLKIT */
126 #ifdef USE_X_TOOLKIT
127 #include "widget.h"
128 #ifndef XtNinitialState
129 #define XtNinitialState "initialState"
130 #endif
131 #endif
133 #include "bitmaps/gray.xbm"
135 #ifdef HAVE_XKB
136 #include <X11/XKBlib.h>
137 #endif
139 /* Default to using XIM if available. */
140 #ifdef USE_XIM
141 int use_xim = 1;
142 #else
143 int use_xim = 0; /* configure --without-xim */
144 #endif
146 /* Non-zero means that a HELP_EVENT has been generated since Emacs
147 start. */
149 static bool any_help_event_p;
151 /* This is a chain of structures for all the X displays currently in
152 use. */
154 struct x_display_info *x_display_list;
156 /* This is a list of cons cells, each of the form (NAME
157 . FONT-LIST-CACHE), one for each element of x_display_list and in
158 the same order. NAME is the name of the frame. FONT-LIST-CACHE
159 records previous values returned by x-list-fonts. */
161 Lisp_Object x_display_name_list;
163 /* This is a frame waiting to be auto-raised, within XTread_socket. */
165 static struct frame *pending_autoraise_frame;
167 /* This is a frame waiting for an event matching mask, within XTread_socket. */
169 static struct {
170 struct frame *f;
171 int eventtype;
172 } pending_event_wait;
174 #ifdef USE_X_TOOLKIT
175 /* The application context for Xt use. */
176 XtAppContext Xt_app_con;
177 static String Xt_default_resources[] = {0};
179 /* Non-zero means user is interacting with a toolkit scroll bar. */
181 static int toolkit_scroll_bar_interaction;
182 #endif /* USE_X_TOOLKIT */
184 /* Non-zero timeout value means ignore next mouse click if it arrives
185 before that timeout elapses (i.e. as part of the same sequence of
186 events resulting from clicking on a frame to select it). */
188 static unsigned long ignore_next_mouse_click_timeout;
190 /* Mouse movement.
192 Formerly, we used PointerMotionHintMask (in standard_event_mask)
193 so that we would have to call XQueryPointer after each MotionNotify
194 event to ask for another such event. However, this made mouse tracking
195 slow, and there was a bug that made it eventually stop.
197 Simply asking for MotionNotify all the time seems to work better.
199 In order to avoid asking for motion events and then throwing most
200 of them away or busy-polling the server for mouse positions, we ask
201 the server for pointer motion hints. This means that we get only
202 one event per group of mouse movements. "Groups" are delimited by
203 other kinds of events (focus changes and button clicks, for
204 example), or by XQueryPointer calls; when one of these happens, we
205 get another MotionNotify event the next time the mouse moves. This
206 is at least as efficient as getting motion events when mouse
207 tracking is on, and I suspect only negligibly worse when tracking
208 is off. */
210 /* Where the mouse was last time we reported a mouse event. */
212 static XRectangle last_mouse_glyph;
213 static struct frame *last_mouse_glyph_frame;
215 /* The scroll bar in which the last X motion event occurred.
217 If the last X motion event occurred in a scroll bar, we set this so
218 XTmouse_position can know whether to report a scroll bar motion or
219 an ordinary motion.
221 If the last X motion event didn't occur in a scroll bar, we set
222 this to Qnil, to tell XTmouse_position to return an ordinary motion
223 event. */
225 static Lisp_Object last_mouse_scroll_bar;
227 /* This is a hack. We would really prefer that XTmouse_position would
228 return the time associated with the position it returns, but there
229 doesn't seem to be any way to wrest the time-stamp from the server
230 along with the position query. So, we just keep track of the time
231 of the last movement we received, and return that in hopes that
232 it's somewhat accurate. */
234 static Time last_mouse_movement_time;
236 /* Incremented by XTread_socket whenever it really tries to read
237 events. */
239 static int volatile input_signal_count;
241 /* Used locally within XTread_socket. */
243 static int x_noop_count;
245 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
247 static Lisp_Object Qvendor_specific_keysyms;
248 static Lisp_Object Qlatin_1;
250 #ifdef USE_GTK
251 /* The name of the Emacs icon file. */
252 static Lisp_Object xg_default_icon_file;
254 /* Used in gtkutil.c. */
255 Lisp_Object Qx_gtk_map_stock;
256 #endif
258 /* Some functions take this as char *, not const char *. */
259 static char emacs_class[] = EMACS_CLASS;
261 enum xembed_info
263 XEMBED_MAPPED = 1 << 0
266 enum xembed_message
268 XEMBED_EMBEDDED_NOTIFY = 0,
269 XEMBED_WINDOW_ACTIVATE = 1,
270 XEMBED_WINDOW_DEACTIVATE = 2,
271 XEMBED_REQUEST_FOCUS = 3,
272 XEMBED_FOCUS_IN = 4,
273 XEMBED_FOCUS_OUT = 5,
274 XEMBED_FOCUS_NEXT = 6,
275 XEMBED_FOCUS_PREV = 7,
277 XEMBED_MODALITY_ON = 10,
278 XEMBED_MODALITY_OFF = 11,
279 XEMBED_REGISTER_ACCELERATOR = 12,
280 XEMBED_UNREGISTER_ACCELERATOR = 13,
281 XEMBED_ACTIVATE_ACCELERATOR = 14
284 static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
285 static void x_set_window_size_1 (struct frame *, int, int, int);
286 static void x_raise_frame (struct frame *);
287 static void x_lower_frame (struct frame *);
288 static const XColor *x_color_cells (Display *, int *);
289 static int x_io_error_quitter (Display *);
290 static struct terminal *x_create_terminal (struct x_display_info *);
291 void x_delete_terminal (struct terminal *);
292 static void x_update_end (struct frame *);
293 static void XTframe_up_to_date (struct frame *);
294 static void x_clear_frame (struct frame *);
295 static _Noreturn void x_ins_del_lines (struct frame *, int, int);
296 static void frame_highlight (struct frame *);
297 static void frame_unhighlight (struct frame *);
298 static void x_new_focus_frame (struct x_display_info *, struct frame *);
299 static void x_focus_changed (int, int, struct x_display_info *,
300 struct frame *, struct input_event *);
301 static void x_detect_focus_change (struct x_display_info *,
302 XEvent *, struct input_event *);
303 static void XTframe_rehighlight (struct frame *);
304 static void x_frame_rehighlight (struct x_display_info *);
305 static void x_draw_hollow_cursor (struct window *, struct glyph_row *);
306 static void x_draw_bar_cursor (struct window *, struct glyph_row *, int,
307 enum text_cursor_kinds);
309 static void x_clip_to_row (struct window *, struct glyph_row *,
310 enum glyph_row_area, GC);
311 static void x_flush (struct frame *f);
312 static void x_update_begin (struct frame *);
313 static void x_update_window_begin (struct window *);
314 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window);
315 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
316 enum scroll_bar_part *,
317 Lisp_Object *, Lisp_Object *,
318 Time *);
319 static int x_handle_net_wm_state (struct frame *, XPropertyEvent *);
320 static void x_check_fullscreen (struct frame *);
321 static void x_check_expected_move (struct frame *, int, int);
322 static void x_sync_with_move (struct frame *, int, int, int);
323 static int handle_one_xevent (struct x_display_info *, XEvent *,
324 int *, struct input_event *);
325 #ifdef USE_GTK
326 static int x_dispatch_event (XEvent *, Display *);
327 #endif
328 /* Don't declare this _Noreturn because we want no
329 interference with debugging failing X calls. */
330 static void x_connection_closed (Display *, const char *);
331 static void x_wm_set_window_state (struct frame *, int);
332 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
333 static void x_initialize (void);
336 /* Flush display of frame F. */
338 static void
339 x_flush (struct frame *f)
341 eassert (f && FRAME_X_P (f));
342 /* Don't call XFlush when it is not safe to redisplay; the X
343 connection may be broken. */
344 if (!NILP (Vinhibit_redisplay))
345 return;
347 block_input ();
348 XFlush (FRAME_X_DISPLAY (f));
349 unblock_input ();
353 /* Remove calls to XFlush by defining XFlush to an empty replacement.
354 Calls to XFlush should be unnecessary because the X output buffer
355 is flushed automatically as needed by calls to XPending,
356 XNextEvent, or XWindowEvent according to the XFlush man page.
357 XTread_socket calls XPending. Removing XFlush improves
358 performance. */
360 #define XFlush(DISPLAY) (void) 0
363 /***********************************************************************
364 Debugging
365 ***********************************************************************/
367 #if 0
369 /* This is a function useful for recording debugging information about
370 the sequence of occurrences in this file. */
372 struct record
374 char *locus;
375 int type;
378 struct record event_record[100];
380 int event_record_index;
382 void
383 record_event (char *locus, int type)
385 if (event_record_index == sizeof (event_record) / sizeof (struct record))
386 event_record_index = 0;
388 event_record[event_record_index].locus = locus;
389 event_record[event_record_index].type = type;
390 event_record_index++;
393 #endif /* 0 */
397 /* Return the struct x_display_info corresponding to DPY. */
399 struct x_display_info *
400 x_display_info_for_display (Display *dpy)
402 struct x_display_info *dpyinfo;
404 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
405 if (dpyinfo->display == dpy)
406 return dpyinfo;
408 return 0;
411 static Window
412 x_find_topmost_parent (struct frame *f)
414 struct x_output *x = f->output_data.x;
415 Window win = None, wi = x->parent_desc;
416 Display *dpy = FRAME_X_DISPLAY (f);
418 while (wi != FRAME_X_DISPLAY_INFO (f)->root_window)
420 Window root;
421 Window *children;
422 unsigned int nchildren;
424 win = wi;
425 XQueryTree (dpy, win, &root, &wi, &children, &nchildren);
426 XFree (children);
429 return win;
432 #define OPAQUE 0xffffffff
434 void
435 x_set_frame_alpha (struct frame *f)
437 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
438 Display *dpy = FRAME_X_DISPLAY (f);
439 Window win = FRAME_OUTER_WINDOW (f);
440 double alpha = 1.0;
441 double alpha_min = 1.0;
442 unsigned long opac;
443 Window parent;
445 if (dpyinfo->x_highlight_frame == f)
446 alpha = f->alpha[0];
447 else
448 alpha = f->alpha[1];
450 if (FLOATP (Vframe_alpha_lower_limit))
451 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
452 else if (INTEGERP (Vframe_alpha_lower_limit))
453 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
455 if (alpha < 0.0)
456 return;
457 else if (alpha > 1.0)
458 alpha = 1.0;
459 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
460 alpha = alpha_min;
462 opac = alpha * OPAQUE;
464 x_catch_errors (dpy);
466 /* If there is a parent from the window manager, put the property there
467 also, to work around broken window managers that fail to do that.
468 Do this unconditionally as this function is called on reparent when
469 alpha has not changed on the frame. */
471 parent = x_find_topmost_parent (f);
472 if (parent != None)
473 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
474 XA_CARDINAL, 32, PropModeReplace,
475 (unsigned char *) &opac, 1L);
477 /* return unless necessary */
479 unsigned char *data;
480 Atom actual;
481 int rc, format;
482 unsigned long n, left;
484 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
485 0L, 1L, False, XA_CARDINAL,
486 &actual, &format, &n, &left,
487 &data);
489 if (rc == Success && actual != None)
491 unsigned long value = *(unsigned long *)data;
492 XFree (data);
493 if (value == opac)
495 x_uncatch_errors ();
496 return;
501 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
502 XA_CARDINAL, 32, PropModeReplace,
503 (unsigned char *) &opac, 1L);
504 x_uncatch_errors ();
508 x_display_pixel_height (struct x_display_info *dpyinfo)
510 return HeightOfScreen (dpyinfo->screen);
514 x_display_pixel_width (struct x_display_info *dpyinfo)
516 return WidthOfScreen (dpyinfo->screen);
520 /***********************************************************************
521 Starting and ending an update
522 ***********************************************************************/
524 /* Start an update of frame F. This function is installed as a hook
525 for update_begin, i.e. it is called when update_begin is called.
526 This function is called prior to calls to x_update_window_begin for
527 each window being updated. Currently, there is nothing to do here
528 because all interesting stuff is done on a window basis. */
530 static void
531 x_update_begin (struct frame *f)
533 /* Nothing to do. */
537 /* Start update of window W. */
539 static void
540 x_update_window_begin (struct window *w)
542 struct frame *f = XFRAME (WINDOW_FRAME (w));
543 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
545 w->output_cursor = w->cursor;
547 block_input ();
549 if (f == hlinfo->mouse_face_mouse_frame)
551 /* Don't do highlighting for mouse motion during the update. */
552 hlinfo->mouse_face_defer = 1;
554 /* If F needs to be redrawn, simply forget about any prior mouse
555 highlighting. */
556 if (FRAME_GARBAGED_P (f))
557 hlinfo->mouse_face_window = Qnil;
560 unblock_input ();
564 /* Draw a vertical window border from (x,y0) to (x,y1) */
566 static void
567 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
569 struct frame *f = XFRAME (WINDOW_FRAME (w));
570 struct face *face;
572 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
573 if (face)
574 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
575 face->foreground);
577 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
578 f->output_data.x->normal_gc, x, y0, x, y1);
581 /* End update of window W.
583 Draw vertical borders between horizontally adjacent windows, and
584 display W's cursor if CURSOR_ON_P is non-zero.
586 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
587 glyphs in mouse-face were overwritten. In that case we have to
588 make sure that the mouse-highlight is properly redrawn.
590 W may be a menu bar pseudo-window in case we don't have X toolkit
591 support. Such windows don't have a cursor, so don't display it
592 here. */
594 static void
595 x_update_window_end (struct window *w, bool cursor_on_p,
596 bool mouse_face_overwritten_p)
598 if (!w->pseudo_window_p)
600 block_input ();
602 if (cursor_on_p)
603 display_and_set_cursor (w, 1,
604 w->output_cursor.hpos, w->output_cursor.vpos,
605 w->output_cursor.x, w->output_cursor.y);
607 if (draw_window_fringes (w, 1))
608 x_draw_vertical_border (w);
610 unblock_input ();
613 /* If a row with mouse-face was overwritten, arrange for
614 XTframe_up_to_date to redisplay the mouse highlight. */
615 if (mouse_face_overwritten_p)
616 reset_mouse_highlight (MOUSE_HL_INFO (XFRAME (w->frame)));
620 /* End update of frame F. This function is installed as a hook in
621 update_end. */
623 static void
624 x_update_end (struct frame *f)
626 /* Mouse highlight may be displayed again. */
627 MOUSE_HL_INFO (f)->mouse_face_defer = 0;
629 #ifndef XFlush
630 block_input ();
631 XFlush (FRAME_X_DISPLAY (f));
632 unblock_input ();
633 #endif
637 /* This function is called from various places in xdisp.c
638 whenever a complete update has been performed. */
640 static void
641 XTframe_up_to_date (struct frame *f)
643 if (FRAME_X_P (f))
644 FRAME_MOUSE_UPDATE (f);
648 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
649 arrow bitmaps, or clear the fringes if no bitmaps are required
650 before DESIRED_ROW is made current. This function is called from
651 update_window_line only if it is known that there are differences
652 between bitmaps to be drawn between current row and DESIRED_ROW. */
654 static void
655 x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
657 struct frame *f;
658 int width, height;
660 eassert (w);
662 if (!desired_row->mode_line_p && !w->pseudo_window_p)
663 desired_row->redraw_fringe_bitmaps_p = 1;
665 /* When a window has disappeared, make sure that no rest of
666 full-width rows stays visible in the internal border. Could
667 check here if updated window is the leftmost/rightmost window,
668 but I guess it's not worth doing since vertically split windows
669 are almost never used, internal border is rarely set, and the
670 overhead is very small. */
671 if (windows_or_buffers_changed
672 && desired_row->full_width_p
673 && (f = XFRAME (w->frame),
674 width = FRAME_INTERNAL_BORDER_WIDTH (f),
675 width != 0)
676 && (height = desired_row->visible_height,
677 height > 0))
679 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
681 block_input ();
682 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
683 0, y, width, height);
684 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
685 FRAME_PIXEL_WIDTH (f) - width,
686 y, width, height);
687 unblock_input ();
691 static void
692 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
694 struct frame *f = XFRAME (WINDOW_FRAME (w));
695 Display *display = FRAME_X_DISPLAY (f);
696 Window window = FRAME_X_WINDOW (f);
697 GC gc = f->output_data.x->normal_gc;
698 struct face *face = p->face;
700 /* Must clip because of partially visible lines. */
701 x_clip_to_row (w, row, ANY_AREA, gc);
703 if (!p->overlay_p)
705 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
707 /* In case the same realized face is used for fringes and
708 for something displayed in the text (e.g. face `region' on
709 mono-displays, the fill style may have been changed to
710 FillSolid in x_draw_glyph_string_background. */
711 if (face->stipple)
712 XSetFillStyle (display, face->gc, FillOpaqueStippled);
713 else
714 XSetForeground (display, face->gc, face->background);
716 #ifdef USE_TOOLKIT_SCROLL_BARS
717 /* If the fringe is adjacent to the left (right) scroll bar of a
718 leftmost (rightmost, respectively) window, then extend its
719 background to the gap between the fringe and the bar. */
720 if ((WINDOW_LEFTMOST_P (w)
721 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
722 || (WINDOW_RIGHTMOST_P (w)
723 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
725 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
727 if (sb_width > 0)
729 int bar_area_x = WINDOW_SCROLL_BAR_AREA_X (w);
730 int bar_area_width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
731 * FRAME_COLUMN_WIDTH (f));
733 if (bx < 0)
735 /* Bitmap fills the fringe. */
736 if (bar_area_x + bar_area_width == p->x)
737 bx = bar_area_x + sb_width;
738 else if (p->x + p->wd == bar_area_x)
739 bx = bar_area_x;
740 if (bx >= 0)
742 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
744 nx = bar_area_width - sb_width;
745 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
746 row->y));
747 ny = row->visible_height;
750 else
752 if (bar_area_x + bar_area_width == bx)
754 bx = bar_area_x + sb_width;
755 nx += bar_area_width - sb_width;
757 else if (bx + nx == bar_area_x)
758 nx += bar_area_width - sb_width;
762 #endif
763 if (bx >= 0 && nx > 0)
764 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
766 if (!face->stipple)
767 XSetForeground (display, face->gc, face->foreground);
770 if (p->which)
772 char *bits;
773 Pixmap pixmap, clipmask = (Pixmap) 0;
774 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
775 XGCValues gcv;
777 if (p->wd > 8)
778 bits = (char *) (p->bits + p->dh);
779 else
780 bits = (char *) p->bits + p->dh;
782 /* Draw the bitmap. I believe these small pixmaps can be cached
783 by the server. */
784 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
785 (p->cursor_p
786 ? (p->overlay_p ? face->background
787 : f->output_data.x->cursor_pixel)
788 : face->foreground),
789 face->background, depth);
791 if (p->overlay_p)
793 clipmask = XCreatePixmapFromBitmapData (display,
794 FRAME_X_DISPLAY_INFO (f)->root_window,
795 bits, p->wd, p->h,
796 1, 0, 1);
797 gcv.clip_mask = clipmask;
798 gcv.clip_x_origin = p->x;
799 gcv.clip_y_origin = p->y;
800 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
803 XCopyArea (display, pixmap, window, gc, 0, 0,
804 p->wd, p->h, p->x, p->y);
805 XFreePixmap (display, pixmap);
807 if (p->overlay_p)
809 gcv.clip_mask = (Pixmap) 0;
810 XChangeGC (display, gc, GCClipMask, &gcv);
811 XFreePixmap (display, clipmask);
815 XSetClipMask (display, gc, None);
818 /***********************************************************************
819 Glyph display
820 ***********************************************************************/
824 static void x_set_glyph_string_clipping (struct glyph_string *);
825 static void x_set_glyph_string_gc (struct glyph_string *);
826 static void x_draw_glyph_string_foreground (struct glyph_string *);
827 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
828 static void x_draw_glyph_string_box (struct glyph_string *);
829 static void x_draw_glyph_string (struct glyph_string *);
830 static _Noreturn void x_delete_glyphs (struct frame *, int);
831 static void x_compute_glyph_string_overhangs (struct glyph_string *);
832 static void x_set_cursor_gc (struct glyph_string *);
833 static void x_set_mode_line_face_gc (struct glyph_string *);
834 static void x_set_mouse_face_gc (struct glyph_string *);
835 static bool x_alloc_lighter_color (struct frame *, Display *, Colormap,
836 unsigned long *, double, int);
837 static void x_setup_relief_color (struct frame *, struct relief *,
838 double, int, unsigned long);
839 static void x_setup_relief_colors (struct glyph_string *);
840 static void x_draw_image_glyph_string (struct glyph_string *);
841 static void x_draw_image_relief (struct glyph_string *);
842 static void x_draw_image_foreground (struct glyph_string *);
843 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
844 static void x_clear_glyph_string_rect (struct glyph_string *, int,
845 int, int, int);
846 static void x_draw_relief_rect (struct frame *, int, int, int, int,
847 int, int, int, int, int, int,
848 XRectangle *);
849 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
850 int, int, int, XRectangle *);
851 static void x_scroll_bar_clear (struct frame *);
853 #ifdef GLYPH_DEBUG
854 static void x_check_font (struct frame *, struct font *);
855 #endif
858 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
859 face. */
861 static void
862 x_set_cursor_gc (struct glyph_string *s)
864 if (s->font == FRAME_FONT (s->f)
865 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
866 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
867 && !s->cmp)
868 s->gc = s->f->output_data.x->cursor_gc;
869 else
871 /* Cursor on non-default face: must merge. */
872 XGCValues xgcv;
873 unsigned long mask;
875 xgcv.background = s->f->output_data.x->cursor_pixel;
876 xgcv.foreground = s->face->background;
878 /* If the glyph would be invisible, try a different foreground. */
879 if (xgcv.foreground == xgcv.background)
880 xgcv.foreground = s->face->foreground;
881 if (xgcv.foreground == xgcv.background)
882 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
883 if (xgcv.foreground == xgcv.background)
884 xgcv.foreground = s->face->foreground;
886 /* Make sure the cursor is distinct from text in this face. */
887 if (xgcv.background == s->face->background
888 && xgcv.foreground == s->face->foreground)
890 xgcv.background = s->face->foreground;
891 xgcv.foreground = s->face->background;
894 IF_DEBUG (x_check_font (s->f, s->font));
895 xgcv.graphics_exposures = False;
896 mask = GCForeground | GCBackground | GCGraphicsExposures;
898 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
899 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
900 mask, &xgcv);
901 else
902 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
903 = XCreateGC (s->display, s->window, mask, &xgcv);
905 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
910 /* Set up S->gc of glyph string S for drawing text in mouse face. */
912 static void
913 x_set_mouse_face_gc (struct glyph_string *s)
915 int face_id;
916 struct face *face;
918 /* What face has to be used last for the mouse face? */
919 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
920 face = FACE_FROM_ID (s->f, face_id);
921 if (face == NULL)
922 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
924 if (s->first_glyph->type == CHAR_GLYPH)
925 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
926 else
927 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
928 s->face = FACE_FROM_ID (s->f, face_id);
929 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
931 if (s->font == s->face->font)
932 s->gc = s->face->gc;
933 else
935 /* Otherwise construct scratch_cursor_gc with values from FACE
936 except for FONT. */
937 XGCValues xgcv;
938 unsigned long mask;
940 xgcv.background = s->face->background;
941 xgcv.foreground = s->face->foreground;
942 xgcv.graphics_exposures = False;
943 mask = GCForeground | GCBackground | GCGraphicsExposures;
945 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
946 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
947 mask, &xgcv);
948 else
949 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
950 = XCreateGC (s->display, s->window, mask, &xgcv);
952 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
955 eassert (s->gc != 0);
959 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
960 Faces to use in the mode line have already been computed when the
961 matrix was built, so there isn't much to do, here. */
963 static void
964 x_set_mode_line_face_gc (struct glyph_string *s)
966 s->gc = s->face->gc;
970 /* Set S->gc of glyph string S for drawing that glyph string. Set
971 S->stippled_p to a non-zero value if the face of S has a stipple
972 pattern. */
974 static void
975 x_set_glyph_string_gc (struct glyph_string *s)
977 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
979 if (s->hl == DRAW_NORMAL_TEXT)
981 s->gc = s->face->gc;
982 s->stippled_p = s->face->stipple != 0;
984 else if (s->hl == DRAW_INVERSE_VIDEO)
986 x_set_mode_line_face_gc (s);
987 s->stippled_p = s->face->stipple != 0;
989 else if (s->hl == DRAW_CURSOR)
991 x_set_cursor_gc (s);
992 s->stippled_p = 0;
994 else if (s->hl == DRAW_MOUSE_FACE)
996 x_set_mouse_face_gc (s);
997 s->stippled_p = s->face->stipple != 0;
999 else if (s->hl == DRAW_IMAGE_RAISED
1000 || s->hl == DRAW_IMAGE_SUNKEN)
1002 s->gc = s->face->gc;
1003 s->stippled_p = s->face->stipple != 0;
1005 else
1007 s->gc = s->face->gc;
1008 s->stippled_p = s->face->stipple != 0;
1011 /* GC must have been set. */
1012 eassert (s->gc != 0);
1016 /* Set clipping for output of glyph string S. S may be part of a mode
1017 line or menu if we don't have X toolkit support. */
1019 static void
1020 x_set_glyph_string_clipping (struct glyph_string *s)
1022 XRectangle *r = s->clip;
1023 int n = get_glyph_string_clip_rects (s, r, 2);
1025 if (n > 0)
1026 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
1027 s->num_clips = n;
1031 /* Set SRC's clipping for output of glyph string DST. This is called
1032 when we are drawing DST's left_overhang or right_overhang only in
1033 the area of SRC. */
1035 static void
1036 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
1038 XRectangle r;
1040 r.x = src->x;
1041 r.width = src->width;
1042 r.y = src->y;
1043 r.height = src->height;
1044 dst->clip[0] = r;
1045 dst->num_clips = 1;
1046 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1050 /* RIF:
1051 Compute left and right overhang of glyph string S. */
1053 static void
1054 x_compute_glyph_string_overhangs (struct glyph_string *s)
1056 if (s->cmp == NULL
1057 && (s->first_glyph->type == CHAR_GLYPH
1058 || s->first_glyph->type == COMPOSITE_GLYPH))
1060 struct font_metrics metrics;
1062 if (s->first_glyph->type == CHAR_GLYPH)
1064 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1065 struct font *font = s->font;
1066 int i;
1068 for (i = 0; i < s->nchars; i++)
1069 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1070 font->driver->text_extents (font, code, s->nchars, &metrics);
1072 else
1074 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1076 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1078 s->right_overhang = (metrics.rbearing > metrics.width
1079 ? metrics.rbearing - metrics.width : 0);
1080 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1082 else if (s->cmp)
1084 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1085 s->left_overhang = - s->cmp->lbearing;
1090 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1092 static void
1093 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1095 XGCValues xgcv;
1096 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1097 XSetForeground (s->display, s->gc, xgcv.background);
1098 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1099 XSetForeground (s->display, s->gc, xgcv.foreground);
1103 /* Draw the background of glyph_string S. If S->background_filled_p
1104 is non-zero don't draw it. FORCE_P non-zero means draw the
1105 background even if it wouldn't be drawn normally. This is used
1106 when a string preceding S draws into the background of S, or S
1107 contains the first component of a composition. */
1109 static void
1110 x_draw_glyph_string_background (struct glyph_string *s, bool force_p)
1112 /* Nothing to do if background has already been drawn or if it
1113 shouldn't be drawn in the first place. */
1114 if (!s->background_filled_p)
1116 int box_line_width = max (s->face->box_line_width, 0);
1118 if (s->stippled_p)
1120 /* Fill background with a stipple pattern. */
1121 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1122 XFillRectangle (s->display, s->window, s->gc, s->x,
1123 s->y + box_line_width,
1124 s->background_width,
1125 s->height - 2 * box_line_width);
1126 XSetFillStyle (s->display, s->gc, FillSolid);
1127 s->background_filled_p = 1;
1129 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1130 || s->font_not_found_p
1131 || s->extends_to_end_of_line_p
1132 || force_p)
1134 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1135 s->background_width,
1136 s->height - 2 * box_line_width);
1137 s->background_filled_p = 1;
1143 /* Draw the foreground of glyph string S. */
1145 static void
1146 x_draw_glyph_string_foreground (struct glyph_string *s)
1148 int i, x;
1150 /* If first glyph of S has a left box line, start drawing the text
1151 of S to the right of that box line. */
1152 if (s->face->box != FACE_NO_BOX
1153 && s->first_glyph->left_box_line_p)
1154 x = s->x + eabs (s->face->box_line_width);
1155 else
1156 x = s->x;
1158 /* Draw characters of S as rectangles if S's font could not be
1159 loaded. */
1160 if (s->font_not_found_p)
1162 for (i = 0; i < s->nchars; ++i)
1164 struct glyph *g = s->first_glyph + i;
1165 XDrawRectangle (s->display, s->window,
1166 s->gc, x, s->y, g->pixel_width - 1,
1167 s->height - 1);
1168 x += g->pixel_width;
1171 else
1173 struct font *font = s->font;
1174 int boff = font->baseline_offset;
1175 int y;
1177 if (font->vertical_centering)
1178 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1180 y = s->ybase - boff;
1181 if (s->for_overlaps
1182 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1183 font->driver->draw (s, 0, s->nchars, x, y, 0);
1184 else
1185 font->driver->draw (s, 0, s->nchars, x, y, 1);
1186 if (s->face->overstrike)
1187 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1191 /* Draw the foreground of composite glyph string S. */
1193 static void
1194 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1196 int i, j, x;
1197 struct font *font = s->font;
1199 /* If first glyph of S has a left box line, start drawing the text
1200 of S to the right of that box line. */
1201 if (s->face && s->face->box != FACE_NO_BOX
1202 && s->first_glyph->left_box_line_p)
1203 x = s->x + eabs (s->face->box_line_width);
1204 else
1205 x = s->x;
1207 /* S is a glyph string for a composition. S->cmp_from is the index
1208 of the first character drawn for glyphs of this composition.
1209 S->cmp_from == 0 means we are drawing the very first character of
1210 this composition. */
1212 /* Draw a rectangle for the composition if the font for the very
1213 first character of the composition could not be loaded. */
1214 if (s->font_not_found_p)
1216 if (s->cmp_from == 0)
1217 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1218 s->width - 1, s->height - 1);
1220 else if (! s->first_glyph->u.cmp.automatic)
1222 int y = s->ybase;
1224 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1225 /* TAB in a composition means display glyphs with padding
1226 space on the left or right. */
1227 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1229 int xx = x + s->cmp->offsets[j * 2];
1230 int yy = y - s->cmp->offsets[j * 2 + 1];
1232 font->driver->draw (s, j, j + 1, xx, yy, 0);
1233 if (s->face->overstrike)
1234 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1237 else
1239 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1240 Lisp_Object glyph;
1241 int y = s->ybase;
1242 int width = 0;
1244 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1246 glyph = LGSTRING_GLYPH (gstring, i);
1247 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1248 width += LGLYPH_WIDTH (glyph);
1249 else
1251 int xoff, yoff, wadjust;
1253 if (j < i)
1255 font->driver->draw (s, j, i, x, y, 0);
1256 if (s->face->overstrike)
1257 font->driver->draw (s, j, i, x + 1, y, 0);
1258 x += width;
1260 xoff = LGLYPH_XOFF (glyph);
1261 yoff = LGLYPH_YOFF (glyph);
1262 wadjust = LGLYPH_WADJUST (glyph);
1263 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1264 if (s->face->overstrike)
1265 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff, 0);
1266 x += wadjust;
1267 j = i + 1;
1268 width = 0;
1271 if (j < i)
1273 font->driver->draw (s, j, i, x, y, 0);
1274 if (s->face->overstrike)
1275 font->driver->draw (s, j, i, x + 1, y, 0);
1281 /* Draw the foreground of glyph string S for glyphless characters. */
1283 static void
1284 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1286 struct glyph *glyph = s->first_glyph;
1287 XChar2b char2b[8];
1288 int x, i, j;
1290 /* If first glyph of S has a left box line, start drawing the text
1291 of S to the right of that box line. */
1292 if (s->face && s->face->box != FACE_NO_BOX
1293 && s->first_glyph->left_box_line_p)
1294 x = s->x + eabs (s->face->box_line_width);
1295 else
1296 x = s->x;
1298 s->char2b = char2b;
1300 for (i = 0; i < s->nchars; i++, glyph++)
1302 char buf[7], *str = NULL;
1303 int len = glyph->u.glyphless.len;
1305 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1307 if (len > 0
1308 && CHAR_TABLE_P (Vglyphless_char_display)
1309 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1310 >= 1))
1312 Lisp_Object acronym
1313 = (! glyph->u.glyphless.for_no_font
1314 ? CHAR_TABLE_REF (Vglyphless_char_display,
1315 glyph->u.glyphless.ch)
1316 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1317 if (STRINGP (acronym))
1318 str = SSDATA (acronym);
1321 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1323 sprintf (buf, "%0*X",
1324 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1325 glyph->u.glyphless.ch);
1326 str = buf;
1329 if (str)
1331 int upper_len = (len + 1) / 2;
1332 unsigned code;
1334 /* It is assured that all LEN characters in STR is ASCII. */
1335 for (j = 0; j < len; j++)
1337 code = s->font->driver->encode_char (s->font, str[j]);
1338 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1340 s->font->driver->draw (s, 0, upper_len,
1341 x + glyph->slice.glyphless.upper_xoff,
1342 s->ybase + glyph->slice.glyphless.upper_yoff,
1344 s->font->driver->draw (s, upper_len, len,
1345 x + glyph->slice.glyphless.lower_xoff,
1346 s->ybase + glyph->slice.glyphless.lower_yoff,
1349 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1350 XDrawRectangle (s->display, s->window, s->gc,
1351 x, s->ybase - glyph->ascent,
1352 glyph->pixel_width - 1,
1353 glyph->ascent + glyph->descent - 1);
1354 x += glyph->pixel_width;
1358 #ifdef USE_X_TOOLKIT
1360 #ifdef USE_LUCID
1362 /* Return the frame on which widget WIDGET is used.. Abort if frame
1363 cannot be determined. */
1365 static struct frame *
1366 x_frame_of_widget (Widget widget)
1368 struct x_display_info *dpyinfo;
1369 Lisp_Object tail, frame;
1370 struct frame *f;
1372 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1374 /* Find the top-level shell of the widget. Note that this function
1375 can be called when the widget is not yet realized, so XtWindow
1376 (widget) == 0. That's the reason we can't simply use
1377 x_any_window_to_frame. */
1378 while (!XtIsTopLevelShell (widget))
1379 widget = XtParent (widget);
1381 /* Look for a frame with that top-level widget. Allocate the color
1382 on that frame to get the right gamma correction value. */
1383 FOR_EACH_FRAME (tail, frame)
1385 f = XFRAME (frame);
1386 if (FRAME_X_P (f)
1387 && f->output_data.nothing != 1
1388 && FRAME_X_DISPLAY_INFO (f) == dpyinfo
1389 && f->output_data.x->widget == widget)
1390 return f;
1392 emacs_abort ();
1395 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1396 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1397 If this produces the same color as PIXEL, try a color where all RGB
1398 values have DELTA added. Return the allocated color in *PIXEL.
1399 DISPLAY is the X display, CMAP is the colormap to operate on.
1400 Value is true if successful. */
1402 bool
1403 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
1404 unsigned long *pixel, double factor, int delta)
1406 struct frame *f = x_frame_of_widget (widget);
1407 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1410 #endif /* USE_LUCID */
1413 /* Structure specifying which arguments should be passed by Xt to
1414 cvt_string_to_pixel. We want the widget's screen and colormap. */
1416 static XtConvertArgRec cvt_string_to_pixel_args[] =
1418 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1419 sizeof (Screen *)},
1420 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1421 sizeof (Colormap)}
1425 /* The address of this variable is returned by
1426 cvt_string_to_pixel. */
1428 static Pixel cvt_string_to_pixel_value;
1431 /* Convert a color name to a pixel color.
1433 DPY is the display we are working on.
1435 ARGS is an array of *NARGS XrmValue structures holding additional
1436 information about the widget for which the conversion takes place.
1437 The contents of this array are determined by the specification
1438 in cvt_string_to_pixel_args.
1440 FROM is a pointer to an XrmValue which points to the color name to
1441 convert. TO is an XrmValue in which to return the pixel color.
1443 CLOSURE_RET is a pointer to user-data, in which we record if
1444 we allocated the color or not.
1446 Value is True if successful, False otherwise. */
1448 static Boolean
1449 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
1450 XrmValue *from, XrmValue *to,
1451 XtPointer *closure_ret)
1453 Screen *screen;
1454 Colormap cmap;
1455 Pixel pixel;
1456 String color_name;
1457 XColor color;
1459 if (*nargs != 2)
1461 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1462 "wrongParameters", "cvt_string_to_pixel",
1463 "XtToolkitError",
1464 "Screen and colormap args required", NULL, NULL);
1465 return False;
1468 screen = *(Screen **) args[0].addr;
1469 cmap = *(Colormap *) args[1].addr;
1470 color_name = (String) from->addr;
1472 if (strcmp (color_name, XtDefaultBackground) == 0)
1474 *closure_ret = (XtPointer) False;
1475 pixel = WhitePixelOfScreen (screen);
1477 else if (strcmp (color_name, XtDefaultForeground) == 0)
1479 *closure_ret = (XtPointer) False;
1480 pixel = BlackPixelOfScreen (screen);
1482 else if (XParseColor (dpy, cmap, color_name, &color)
1483 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1485 pixel = color.pixel;
1486 *closure_ret = (XtPointer) True;
1488 else
1490 String params[1];
1491 Cardinal nparams = 1;
1493 params[0] = color_name;
1494 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1495 "badValue", "cvt_string_to_pixel",
1496 "XtToolkitError", "Invalid color `%s'",
1497 params, &nparams);
1498 return False;
1501 if (to->addr != NULL)
1503 if (to->size < sizeof (Pixel))
1505 to->size = sizeof (Pixel);
1506 return False;
1509 *(Pixel *) to->addr = pixel;
1511 else
1513 cvt_string_to_pixel_value = pixel;
1514 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1517 to->size = sizeof (Pixel);
1518 return True;
1522 /* Free a pixel color which was previously allocated via
1523 cvt_string_to_pixel. This is registered as the destructor
1524 for this type of resource via XtSetTypeConverter.
1526 APP is the application context in which we work.
1528 TO is a pointer to an XrmValue holding the color to free.
1529 CLOSURE is the value we stored in CLOSURE_RET for this color
1530 in cvt_string_to_pixel.
1532 ARGS and NARGS are like for cvt_string_to_pixel. */
1534 static void
1535 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
1536 Cardinal *nargs)
1538 if (*nargs != 2)
1540 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1541 "XtToolkitError",
1542 "Screen and colormap arguments required",
1543 NULL, NULL);
1545 else if (closure != NULL)
1547 /* We did allocate the pixel, so free it. */
1548 Screen *screen = *(Screen **) args[0].addr;
1549 Colormap cmap = *(Colormap *) args[1].addr;
1550 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1551 (Pixel *) to->addr, 1);
1556 #endif /* USE_X_TOOLKIT */
1559 /* Value is an array of XColor structures for the contents of the
1560 color map of display DPY. Set *NCELLS to the size of the array.
1561 Note that this probably shouldn't be called for large color maps,
1562 say a 24-bit TrueColor map. */
1564 static const XColor *
1565 x_color_cells (Display *dpy, int *ncells)
1567 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1569 if (dpyinfo->color_cells == NULL)
1571 Screen *screen = dpyinfo->screen;
1572 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1573 int i;
1575 dpyinfo->color_cells = xnmalloc (ncolor_cells,
1576 sizeof *dpyinfo->color_cells);
1577 dpyinfo->ncolor_cells = ncolor_cells;
1579 for (i = 0; i < ncolor_cells; ++i)
1580 dpyinfo->color_cells[i].pixel = i;
1582 XQueryColors (dpy, dpyinfo->cmap,
1583 dpyinfo->color_cells, ncolor_cells);
1586 *ncells = dpyinfo->ncolor_cells;
1587 return dpyinfo->color_cells;
1591 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1592 colors in COLORS. Use cached information, if available. */
1594 void
1595 x_query_colors (struct frame *f, XColor *colors, int ncolors)
1597 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1599 if (dpyinfo->color_cells)
1601 int i;
1602 for (i = 0; i < ncolors; ++i)
1604 unsigned long pixel = colors[i].pixel;
1605 eassert (pixel < dpyinfo->ncolor_cells);
1606 eassert (dpyinfo->color_cells[pixel].pixel == pixel);
1607 colors[i] = dpyinfo->color_cells[pixel];
1610 else
1611 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1615 /* On frame F, translate pixel color to RGB values for the color in
1616 COLOR. Use cached information, if available. */
1618 void
1619 x_query_color (struct frame *f, XColor *color)
1621 x_query_colors (f, color, 1);
1625 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1626 exact match can't be allocated, try the nearest color available.
1627 Value is true if successful. Set *COLOR to the color
1628 allocated. */
1630 static bool
1631 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
1633 bool rc;
1635 rc = XAllocColor (dpy, cmap, color) != 0;
1636 if (rc == 0)
1638 /* If we got to this point, the colormap is full, so we're going
1639 to try to get the next closest color. The algorithm used is
1640 a least-squares matching, which is what X uses for closest
1641 color matching with StaticColor visuals. */
1642 int nearest, i;
1643 int max_color_delta = 255;
1644 int max_delta = 3 * max_color_delta;
1645 int nearest_delta = max_delta + 1;
1646 int ncells;
1647 const XColor *cells = x_color_cells (dpy, &ncells);
1649 for (nearest = i = 0; i < ncells; ++i)
1651 int dred = (color->red >> 8) - (cells[i].red >> 8);
1652 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
1653 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1654 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
1656 if (delta < nearest_delta)
1658 nearest = i;
1659 nearest_delta = delta;
1663 color->red = cells[nearest].red;
1664 color->green = cells[nearest].green;
1665 color->blue = cells[nearest].blue;
1666 rc = XAllocColor (dpy, cmap, color) != 0;
1668 else
1670 /* If allocation succeeded, and the allocated pixel color is not
1671 equal to a cached pixel color recorded earlier, there was a
1672 change in the colormap, so clear the color cache. */
1673 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1674 XColor *cached_color;
1676 if (dpyinfo->color_cells
1677 && (cached_color = &dpyinfo->color_cells[color->pixel],
1678 (cached_color->red != color->red
1679 || cached_color->blue != color->blue
1680 || cached_color->green != color->green)))
1682 xfree (dpyinfo->color_cells);
1683 dpyinfo->color_cells = NULL;
1684 dpyinfo->ncolor_cells = 0;
1688 #ifdef DEBUG_X_COLORS
1689 if (rc)
1690 register_color (color->pixel);
1691 #endif /* DEBUG_X_COLORS */
1693 return rc;
1697 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1698 exact match can't be allocated, try the nearest color available.
1699 Value is true if successful. Set *COLOR to the color
1700 allocated. */
1702 bool
1703 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
1705 gamma_correct (f, color);
1706 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1710 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1711 It's necessary to do this instead of just using PIXEL directly to
1712 get color reference counts right. */
1714 unsigned long
1715 x_copy_color (struct frame *f, long unsigned int pixel)
1717 XColor color;
1719 color.pixel = pixel;
1720 block_input ();
1721 x_query_color (f, &color);
1722 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1723 unblock_input ();
1724 #ifdef DEBUG_X_COLORS
1725 register_color (pixel);
1726 #endif
1727 return color.pixel;
1731 /* Brightness beyond which a color won't have its highlight brightness
1732 boosted.
1734 Nominally, highlight colors for `3d' faces are calculated by
1735 brightening an object's color by a constant scale factor, but this
1736 doesn't yield good results for dark colors, so for colors who's
1737 brightness is less than this value (on a scale of 0-65535) have an
1738 use an additional additive factor.
1740 The value here is set so that the default menu-bar/mode-line color
1741 (grey75) will not have its highlights changed at all. */
1742 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1745 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1746 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1747 If this produces the same color as PIXEL, try a color where all RGB
1748 values have DELTA added. Return the allocated color in *PIXEL.
1749 DISPLAY is the X display, CMAP is the colormap to operate on.
1750 Value is non-zero if successful. */
1752 static bool
1753 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap, long unsigned int *pixel, double factor, int delta)
1755 XColor color, new;
1756 long bright;
1757 bool success_p;
1759 /* Get RGB color values. */
1760 color.pixel = *pixel;
1761 x_query_color (f, &color);
1763 /* Change RGB values by specified FACTOR. Avoid overflow! */
1764 eassert (factor >= 0);
1765 new.red = min (0xffff, factor * color.red);
1766 new.green = min (0xffff, factor * color.green);
1767 new.blue = min (0xffff, factor * color.blue);
1769 /* Calculate brightness of COLOR. */
1770 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1772 /* We only boost colors that are darker than
1773 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1774 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1775 /* Make an additive adjustment to NEW, because it's dark enough so
1776 that scaling by FACTOR alone isn't enough. */
1778 /* How far below the limit this color is (0 - 1, 1 being darker). */
1779 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1780 /* The additive adjustment. */
1781 int min_delta = delta * dimness * factor / 2;
1783 if (factor < 1)
1785 new.red = max (0, new.red - min_delta);
1786 new.green = max (0, new.green - min_delta);
1787 new.blue = max (0, new.blue - min_delta);
1789 else
1791 new.red = min (0xffff, min_delta + new.red);
1792 new.green = min (0xffff, min_delta + new.green);
1793 new.blue = min (0xffff, min_delta + new.blue);
1797 /* Try to allocate the color. */
1798 success_p = x_alloc_nearest_color (f, cmap, &new);
1799 if (success_p)
1801 if (new.pixel == *pixel)
1803 /* If we end up with the same color as before, try adding
1804 delta to the RGB values. */
1805 x_free_colors (f, &new.pixel, 1);
1807 new.red = min (0xffff, delta + color.red);
1808 new.green = min (0xffff, delta + color.green);
1809 new.blue = min (0xffff, delta + color.blue);
1810 success_p = x_alloc_nearest_color (f, cmap, &new);
1812 else
1813 success_p = 1;
1814 *pixel = new.pixel;
1817 return success_p;
1821 /* Set up the foreground color for drawing relief lines of glyph
1822 string S. RELIEF is a pointer to a struct relief containing the GC
1823 with which lines will be drawn. Use a color that is FACTOR or
1824 DELTA lighter or darker than the relief's background which is found
1825 in S->f->output_data.x->relief_background. If such a color cannot
1826 be allocated, use DEFAULT_PIXEL, instead. */
1828 static void
1829 x_setup_relief_color (struct frame *f, struct relief *relief, double factor, int delta, long unsigned int default_pixel)
1831 XGCValues xgcv;
1832 struct x_output *di = f->output_data.x;
1833 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1834 unsigned long pixel;
1835 unsigned long background = di->relief_background;
1836 Colormap cmap = FRAME_X_COLORMAP (f);
1837 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1838 Display *dpy = FRAME_X_DISPLAY (f);
1840 xgcv.graphics_exposures = False;
1841 xgcv.line_width = 1;
1843 /* Free previously allocated color. The color cell will be reused
1844 when it has been freed as many times as it was allocated, so this
1845 doesn't affect faces using the same colors. */
1846 if (relief->gc
1847 && relief->allocated_p)
1849 x_free_colors (f, &relief->pixel, 1);
1850 relief->allocated_p = 0;
1853 /* Allocate new color. */
1854 xgcv.foreground = default_pixel;
1855 pixel = background;
1856 if (dpyinfo->n_planes != 1
1857 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1859 relief->allocated_p = 1;
1860 xgcv.foreground = relief->pixel = pixel;
1863 if (relief->gc == 0)
1865 xgcv.stipple = dpyinfo->gray;
1866 mask |= GCStipple;
1867 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1869 else
1870 XChangeGC (dpy, relief->gc, mask, &xgcv);
1874 /* Set up colors for the relief lines around glyph string S. */
1876 static void
1877 x_setup_relief_colors (struct glyph_string *s)
1879 struct x_output *di = s->f->output_data.x;
1880 unsigned long color;
1882 if (s->face->use_box_color_for_shadows_p)
1883 color = s->face->box_color;
1884 else if (s->first_glyph->type == IMAGE_GLYPH
1885 && s->img->pixmap
1886 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1887 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1888 else
1890 XGCValues xgcv;
1892 /* Get the background color of the face. */
1893 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
1894 color = xgcv.background;
1897 if (di->white_relief.gc == 0
1898 || color != di->relief_background)
1900 di->relief_background = color;
1901 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1902 WHITE_PIX_DEFAULT (s->f));
1903 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1904 BLACK_PIX_DEFAULT (s->f));
1909 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
1910 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
1911 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
1912 relief. LEFT_P non-zero means draw a relief on the left side of
1913 the rectangle. RIGHT_P non-zero means draw a relief on the right
1914 side of the rectangle. CLIP_RECT is the clipping rectangle to use
1915 when drawing. */
1917 static void
1918 x_draw_relief_rect (struct frame *f,
1919 int left_x, int top_y, int right_x, int bottom_y, int width,
1920 int raised_p, int top_p, int bot_p, int left_p, int right_p,
1921 XRectangle *clip_rect)
1923 Display *dpy = FRAME_X_DISPLAY (f);
1924 Window window = FRAME_X_WINDOW (f);
1925 int i;
1926 GC gc;
1928 if (raised_p)
1929 gc = f->output_data.x->white_relief.gc;
1930 else
1931 gc = f->output_data.x->black_relief.gc;
1932 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1934 /* This code is more complicated than it has to be, because of two
1935 minor hacks to make the boxes look nicer: (i) if width > 1, draw
1936 the outermost line using the black relief. (ii) Omit the four
1937 corner pixels. */
1939 /* Top. */
1940 if (top_p)
1942 if (width == 1)
1943 XDrawLine (dpy, window, gc,
1944 left_x + (left_p ? 1 : 0), top_y,
1945 right_x + (right_p ? 0 : 1), top_y);
1947 for (i = 1; i < width; ++i)
1948 XDrawLine (dpy, window, gc,
1949 left_x + i * left_p, top_y + i,
1950 right_x + 1 - i * right_p, top_y + i);
1953 /* Left. */
1954 if (left_p)
1956 if (width == 1)
1957 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
1959 XClearArea (dpy, window, left_x, top_y, 1, 1, False);
1960 XClearArea (dpy, window, left_x, bottom_y, 1, 1, False);
1962 for (i = (width > 1 ? 1 : 0); i < width; ++i)
1963 XDrawLine (dpy, window, gc,
1964 left_x + i, top_y + (i + 1) * top_p,
1965 left_x + i, bottom_y + 1 - (i + 1) * bot_p);
1968 XSetClipMask (dpy, gc, None);
1969 if (raised_p)
1970 gc = f->output_data.x->black_relief.gc;
1971 else
1972 gc = f->output_data.x->white_relief.gc;
1973 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
1975 if (width > 1)
1977 /* Outermost top line. */
1978 if (top_p)
1979 XDrawLine (dpy, window, gc,
1980 left_x + (left_p ? 1 : 0), top_y,
1981 right_x + (right_p ? 0 : 1), top_y);
1983 /* Outermost left line. */
1984 if (left_p)
1985 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
1988 /* Bottom. */
1989 if (bot_p)
1991 XDrawLine (dpy, window, gc,
1992 left_x + (left_p ? 1 : 0), bottom_y,
1993 right_x + (right_p ? 0 : 1), bottom_y);
1994 for (i = 1; i < width; ++i)
1995 XDrawLine (dpy, window, gc,
1996 left_x + i * left_p, bottom_y - i,
1997 right_x + 1 - i * right_p, bottom_y - i);
2000 /* Right. */
2001 if (right_p)
2003 XClearArea (dpy, window, right_x, top_y, 1, 1, False);
2004 XClearArea (dpy, window, right_x, bottom_y, 1, 1, False);
2005 for (i = 0; i < width; ++i)
2006 XDrawLine (dpy, window, gc,
2007 right_x - i, top_y + (i + 1) * top_p,
2008 right_x - i, bottom_y + 1 - (i + 1) * bot_p);
2011 XSetClipMask (dpy, gc, None);
2015 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2016 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2017 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2018 left side of the rectangle. RIGHT_P non-zero means draw a line
2019 on the right side of the rectangle. CLIP_RECT is the clipping
2020 rectangle to use when drawing. */
2022 static void
2023 x_draw_box_rect (struct glyph_string *s,
2024 int left_x, int top_y, int right_x, int bottom_y, int width,
2025 int left_p, int right_p, XRectangle *clip_rect)
2027 XGCValues xgcv;
2029 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2030 XSetForeground (s->display, s->gc, s->face->box_color);
2031 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2033 /* Top. */
2034 XFillRectangle (s->display, s->window, s->gc,
2035 left_x, top_y, right_x - left_x + 1, width);
2037 /* Left. */
2038 if (left_p)
2039 XFillRectangle (s->display, s->window, s->gc,
2040 left_x, top_y, width, bottom_y - top_y + 1);
2042 /* Bottom. */
2043 XFillRectangle (s->display, s->window, s->gc,
2044 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2046 /* Right. */
2047 if (right_p)
2048 XFillRectangle (s->display, s->window, s->gc,
2049 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2051 XSetForeground (s->display, s->gc, xgcv.foreground);
2052 XSetClipMask (s->display, s->gc, None);
2056 /* Draw a box around glyph string S. */
2058 static void
2059 x_draw_glyph_string_box (struct glyph_string *s)
2061 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2062 int left_p, right_p;
2063 struct glyph *last_glyph;
2064 XRectangle clip_rect;
2066 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2067 ? WINDOW_RIGHT_EDGE_X (s->w)
2068 : window_box_right (s->w, s->area));
2070 /* The glyph that may have a right box line. */
2071 last_glyph = (s->cmp || s->img
2072 ? s->first_glyph
2073 : s->first_glyph + s->nchars - 1);
2075 width = eabs (s->face->box_line_width);
2076 raised_p = s->face->box == FACE_RAISED_BOX;
2077 left_x = s->x;
2078 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2079 ? last_x - 1
2080 : min (last_x, s->x + s->background_width) - 1);
2081 top_y = s->y;
2082 bottom_y = top_y + s->height - 1;
2084 left_p = (s->first_glyph->left_box_line_p
2085 || (s->hl == DRAW_MOUSE_FACE
2086 && (s->prev == NULL
2087 || s->prev->hl != s->hl)));
2088 right_p = (last_glyph->right_box_line_p
2089 || (s->hl == DRAW_MOUSE_FACE
2090 && (s->next == NULL
2091 || s->next->hl != s->hl)));
2093 get_glyph_string_clip_rect (s, &clip_rect);
2095 if (s->face->box == FACE_SIMPLE_BOX)
2096 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2097 left_p, right_p, &clip_rect);
2098 else
2100 x_setup_relief_colors (s);
2101 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2102 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2107 /* Draw foreground of image glyph string S. */
2109 static void
2110 x_draw_image_foreground (struct glyph_string *s)
2112 int x = s->x;
2113 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2115 /* If first glyph of S has a left box line, start drawing it to the
2116 right of that line. */
2117 if (s->face->box != FACE_NO_BOX
2118 && s->first_glyph->left_box_line_p
2119 && s->slice.x == 0)
2120 x += eabs (s->face->box_line_width);
2122 /* If there is a margin around the image, adjust x- and y-position
2123 by that margin. */
2124 if (s->slice.x == 0)
2125 x += s->img->hmargin;
2126 if (s->slice.y == 0)
2127 y += s->img->vmargin;
2129 if (s->img->pixmap)
2131 if (s->img->mask)
2133 /* We can't set both a clip mask and use XSetClipRectangles
2134 because the latter also sets a clip mask. We also can't
2135 trust on the shape extension to be available
2136 (XShapeCombineRegion). So, compute the rectangle to draw
2137 manually. */
2138 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2139 | GCFunction);
2140 XGCValues xgcv;
2141 XRectangle clip_rect, image_rect, r;
2143 xgcv.clip_mask = s->img->mask;
2144 xgcv.clip_x_origin = x;
2145 xgcv.clip_y_origin = y;
2146 xgcv.function = GXcopy;
2147 XChangeGC (s->display, s->gc, mask, &xgcv);
2149 get_glyph_string_clip_rect (s, &clip_rect);
2150 image_rect.x = x;
2151 image_rect.y = y;
2152 image_rect.width = s->slice.width;
2153 image_rect.height = s->slice.height;
2154 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2155 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2156 s->slice.x + r.x - x, s->slice.y + r.y - y,
2157 r.width, r.height, r.x, r.y);
2159 else
2161 XRectangle clip_rect, image_rect, r;
2163 get_glyph_string_clip_rect (s, &clip_rect);
2164 image_rect.x = x;
2165 image_rect.y = y;
2166 image_rect.width = s->slice.width;
2167 image_rect.height = s->slice.height;
2168 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2169 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2170 s->slice.x + r.x - x, s->slice.y + r.y - y,
2171 r.width, r.height, r.x, r.y);
2173 /* When the image has a mask, we can expect that at
2174 least part of a mouse highlight or a block cursor will
2175 be visible. If the image doesn't have a mask, make
2176 a block cursor visible by drawing a rectangle around
2177 the image. I believe it's looking better if we do
2178 nothing here for mouse-face. */
2179 if (s->hl == DRAW_CURSOR)
2181 int relief = eabs (s->img->relief);
2182 XDrawRectangle (s->display, s->window, s->gc,
2183 x - relief, y - relief,
2184 s->slice.width + relief*2 - 1,
2185 s->slice.height + relief*2 - 1);
2189 else
2190 /* Draw a rectangle if image could not be loaded. */
2191 XDrawRectangle (s->display, s->window, s->gc, x, y,
2192 s->slice.width - 1, s->slice.height - 1);
2196 /* Draw a relief around the image glyph string S. */
2198 static void
2199 x_draw_image_relief (struct glyph_string *s)
2201 int x1, y1, thick, raised_p, top_p, bot_p, left_p, right_p;
2202 XRectangle r;
2203 int x = s->x;
2204 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2206 /* If first glyph of S has a left box line, start drawing it to the
2207 right of that line. */
2208 if (s->face->box != FACE_NO_BOX
2209 && s->first_glyph->left_box_line_p
2210 && s->slice.x == 0)
2211 x += eabs (s->face->box_line_width);
2213 /* If there is a margin around the image, adjust x- and y-position
2214 by that margin. */
2215 if (s->slice.x == 0)
2216 x += s->img->hmargin;
2217 if (s->slice.y == 0)
2218 y += s->img->vmargin;
2220 if (s->hl == DRAW_IMAGE_SUNKEN
2221 || s->hl == DRAW_IMAGE_RAISED)
2223 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2224 raised_p = s->hl == DRAW_IMAGE_RAISED;
2226 else
2228 thick = eabs (s->img->relief);
2229 raised_p = s->img->relief > 0;
2232 x1 = x + s->slice.width - 1;
2233 y1 = y + s->slice.height - 1;
2234 top_p = bot_p = left_p = right_p = 0;
2236 if (s->slice.x == 0)
2237 x -= thick, left_p = 1;
2238 if (s->slice.y == 0)
2239 y -= thick, top_p = 1;
2240 if (s->slice.x + s->slice.width == s->img->width)
2241 x1 += thick, right_p = 1;
2242 if (s->slice.y + s->slice.height == s->img->height)
2243 y1 += thick, bot_p = 1;
2245 x_setup_relief_colors (s);
2246 get_glyph_string_clip_rect (s, &r);
2247 x_draw_relief_rect (s->f, x, y, x1, y1, thick, raised_p,
2248 top_p, bot_p, left_p, right_p, &r);
2252 /* Draw the foreground of image glyph string S to PIXMAP. */
2254 static void
2255 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
2257 int x = 0;
2258 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2260 /* If first glyph of S has a left box line, start drawing it to the
2261 right of that line. */
2262 if (s->face->box != FACE_NO_BOX
2263 && s->first_glyph->left_box_line_p
2264 && s->slice.x == 0)
2265 x += eabs (s->face->box_line_width);
2267 /* If there is a margin around the image, adjust x- and y-position
2268 by that margin. */
2269 if (s->slice.x == 0)
2270 x += s->img->hmargin;
2271 if (s->slice.y == 0)
2272 y += s->img->vmargin;
2274 if (s->img->pixmap)
2276 if (s->img->mask)
2278 /* We can't set both a clip mask and use XSetClipRectangles
2279 because the latter also sets a clip mask. We also can't
2280 trust on the shape extension to be available
2281 (XShapeCombineRegion). So, compute the rectangle to draw
2282 manually. */
2283 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2284 | GCFunction);
2285 XGCValues xgcv;
2287 xgcv.clip_mask = s->img->mask;
2288 xgcv.clip_x_origin = x - s->slice.x;
2289 xgcv.clip_y_origin = y - s->slice.y;
2290 xgcv.function = GXcopy;
2291 XChangeGC (s->display, s->gc, mask, &xgcv);
2293 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2294 s->slice.x, s->slice.y,
2295 s->slice.width, s->slice.height, x, y);
2296 XSetClipMask (s->display, s->gc, None);
2298 else
2300 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2301 s->slice.x, s->slice.y,
2302 s->slice.width, s->slice.height, x, y);
2304 /* When the image has a mask, we can expect that at
2305 least part of a mouse highlight or a block cursor will
2306 be visible. If the image doesn't have a mask, make
2307 a block cursor visible by drawing a rectangle around
2308 the image. I believe it's looking better if we do
2309 nothing here for mouse-face. */
2310 if (s->hl == DRAW_CURSOR)
2312 int r = eabs (s->img->relief);
2313 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2314 s->slice.width + r*2 - 1,
2315 s->slice.height + r*2 - 1);
2319 else
2320 /* Draw a rectangle if image could not be loaded. */
2321 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2322 s->slice.width - 1, s->slice.height - 1);
2326 /* Draw part of the background of glyph string S. X, Y, W, and H
2327 give the rectangle to draw. */
2329 static void
2330 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
2332 if (s->stippled_p)
2334 /* Fill background with a stipple pattern. */
2335 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2336 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2337 XSetFillStyle (s->display, s->gc, FillSolid);
2339 else
2340 x_clear_glyph_string_rect (s, x, y, w, h);
2344 /* Draw image glyph string S.
2346 s->y
2347 s->x +-------------------------
2348 | s->face->box
2350 | +-------------------------
2351 | | s->img->margin
2353 | | +-------------------
2354 | | | the image
2358 static void
2359 x_draw_image_glyph_string (struct glyph_string *s)
2361 int box_line_hwidth = eabs (s->face->box_line_width);
2362 int box_line_vwidth = max (s->face->box_line_width, 0);
2363 int height;
2364 Pixmap pixmap = None;
2366 height = s->height;
2367 if (s->slice.y == 0)
2368 height -= box_line_vwidth;
2369 if (s->slice.y + s->slice.height >= s->img->height)
2370 height -= box_line_vwidth;
2372 /* Fill background with face under the image. Do it only if row is
2373 taller than image or if image has a clip mask to reduce
2374 flickering. */
2375 s->stippled_p = s->face->stipple != 0;
2376 if (height > s->slice.height
2377 || s->img->hmargin
2378 || s->img->vmargin
2379 || s->img->mask
2380 || s->img->pixmap == 0
2381 || s->width != s->background_width)
2383 if (s->img->mask)
2385 /* Create a pixmap as large as the glyph string. Fill it
2386 with the background color. Copy the image to it, using
2387 its mask. Copy the temporary pixmap to the display. */
2388 Screen *screen = FRAME_X_SCREEN (s->f);
2389 int depth = DefaultDepthOfScreen (screen);
2391 /* Create a pixmap as large as the glyph string. */
2392 pixmap = XCreatePixmap (s->display, s->window,
2393 s->background_width,
2394 s->height, depth);
2396 /* Don't clip in the following because we're working on the
2397 pixmap. */
2398 XSetClipMask (s->display, s->gc, None);
2400 /* Fill the pixmap with the background color/stipple. */
2401 if (s->stippled_p)
2403 /* Fill background with a stipple pattern. */
2404 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2405 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2406 XFillRectangle (s->display, pixmap, s->gc,
2407 0, 0, s->background_width, s->height);
2408 XSetFillStyle (s->display, s->gc, FillSolid);
2409 XSetTSOrigin (s->display, s->gc, 0, 0);
2411 else
2413 XGCValues xgcv;
2414 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2415 &xgcv);
2416 XSetForeground (s->display, s->gc, xgcv.background);
2417 XFillRectangle (s->display, pixmap, s->gc,
2418 0, 0, s->background_width, s->height);
2419 XSetForeground (s->display, s->gc, xgcv.foreground);
2422 else
2424 int x = s->x;
2425 int y = s->y;
2427 if (s->first_glyph->left_box_line_p
2428 && s->slice.x == 0)
2429 x += box_line_hwidth;
2431 if (s->slice.y == 0)
2432 y += box_line_vwidth;
2434 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2437 s->background_filled_p = 1;
2440 /* Draw the foreground. */
2441 if (pixmap != None)
2443 x_draw_image_foreground_1 (s, pixmap);
2444 x_set_glyph_string_clipping (s);
2445 XCopyArea (s->display, pixmap, s->window, s->gc,
2446 0, 0, s->background_width, s->height, s->x, s->y);
2447 XFreePixmap (s->display, pixmap);
2449 else
2450 x_draw_image_foreground (s);
2452 /* If we must draw a relief around the image, do it. */
2453 if (s->img->relief
2454 || s->hl == DRAW_IMAGE_RAISED
2455 || s->hl == DRAW_IMAGE_SUNKEN)
2456 x_draw_image_relief (s);
2460 /* Draw stretch glyph string S. */
2462 static void
2463 x_draw_stretch_glyph_string (struct glyph_string *s)
2465 eassert (s->first_glyph->type == STRETCH_GLYPH);
2467 if (s->hl == DRAW_CURSOR
2468 && !x_stretch_cursor_p)
2470 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
2471 wide as the stretch glyph. */
2472 int width, background_width = s->background_width;
2473 int x = s->x;
2475 if (!s->row->reversed_p)
2477 int left_x = window_box_left_offset (s->w, TEXT_AREA);
2479 if (x < left_x)
2481 background_width -= left_x - x;
2482 x = left_x;
2485 else
2487 /* In R2L rows, draw the cursor on the right edge of the
2488 stretch glyph. */
2489 int right_x = window_box_right_offset (s->w, TEXT_AREA);
2491 if (x + background_width > right_x)
2492 background_width -= x - right_x;
2493 x += background_width;
2495 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2496 if (s->row->reversed_p)
2497 x -= width;
2499 /* Draw cursor. */
2500 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2502 /* Clear rest using the GC of the original non-cursor face. */
2503 if (width < background_width)
2505 int y = s->y;
2506 int w = background_width - width, h = s->height;
2507 XRectangle r;
2508 GC gc;
2510 if (!s->row->reversed_p)
2511 x += width;
2512 else
2513 x = s->x;
2514 if (s->row->mouse_face_p
2515 && cursor_in_mouse_face_p (s->w))
2517 x_set_mouse_face_gc (s);
2518 gc = s->gc;
2520 else
2521 gc = s->face->gc;
2523 get_glyph_string_clip_rect (s, &r);
2524 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2526 if (s->face->stipple)
2528 /* Fill background with a stipple pattern. */
2529 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2530 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2531 XSetFillStyle (s->display, gc, FillSolid);
2533 else
2535 XGCValues xgcv;
2536 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2537 XSetForeground (s->display, gc, xgcv.background);
2538 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2539 XSetForeground (s->display, gc, xgcv.foreground);
2543 else if (!s->background_filled_p)
2545 int background_width = s->background_width;
2546 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2548 /* Don't draw into left margin, fringe or scrollbar area
2549 except for header line and mode line. */
2550 if (x < left_x && !s->row->mode_line_p)
2552 background_width -= left_x - x;
2553 x = left_x;
2555 if (background_width > 0)
2556 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2559 s->background_filled_p = 1;
2563 Draw a wavy line under S. The wave fills wave_height pixels from y0.
2565 x0 wave_length = 2
2567 y0 * * * * *
2568 |* * * * * * * * *
2569 wave_height = 3 | * * * *
2573 static void
2574 x_draw_underwave (struct glyph_string *s)
2576 int wave_height = 3, wave_length = 2;
2577 int dx, dy, x0, y0, width, x1, y1, x2, y2, odd, xmax;
2578 XRectangle wave_clip, string_clip, final_clip;
2580 dx = wave_length;
2581 dy = wave_height - 1;
2582 x0 = s->x;
2583 y0 = s->ybase - wave_height + 3;
2584 width = s->width;
2585 xmax = x0 + width;
2587 /* Find and set clipping rectangle */
2589 wave_clip.x = x0;
2590 wave_clip.y = y0;
2591 wave_clip.width = width;
2592 wave_clip.height = wave_height;
2593 get_glyph_string_clip_rect (s, &string_clip);
2595 if (!x_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
2596 return;
2598 XSetClipRectangles (s->display, s->gc, 0, 0, &final_clip, 1, Unsorted);
2600 /* Draw the waves */
2602 x1 = x0 - (x0 % dx);
2603 x2 = x1 + dx;
2604 odd = (x1/dx) % 2;
2605 y1 = y2 = y0;
2607 if (odd)
2608 y1 += dy;
2609 else
2610 y2 += dy;
2612 if (INT_MAX - dx < xmax)
2613 emacs_abort ();
2615 while (x1 <= xmax)
2617 XDrawLine (s->display, s->window, s->gc, x1, y1, x2, y2);
2618 x1 = x2, y1 = y2;
2619 x2 += dx, y2 = y0 + odd*dy;
2620 odd = !odd;
2623 /* Restore previous clipping rectangle(s) */
2624 XSetClipRectangles (s->display, s->gc, 0, 0, s->clip, s->num_clips, Unsorted);
2628 /* Draw glyph string S. */
2630 static void
2631 x_draw_glyph_string (struct glyph_string *s)
2633 bool relief_drawn_p = 0;
2635 /* If S draws into the background of its successors, draw the
2636 background of the successors first so that S can draw into it.
2637 This makes S->next use XDrawString instead of XDrawImageString. */
2638 if (s->next && s->right_overhang && !s->for_overlaps)
2640 int width;
2641 struct glyph_string *next;
2643 for (width = 0, next = s->next;
2644 next && width < s->right_overhang;
2645 width += next->width, next = next->next)
2646 if (next->first_glyph->type != IMAGE_GLYPH)
2648 x_set_glyph_string_gc (next);
2649 x_set_glyph_string_clipping (next);
2650 if (next->first_glyph->type == STRETCH_GLYPH)
2651 x_draw_stretch_glyph_string (next);
2652 else
2653 x_draw_glyph_string_background (next, 1);
2654 next->num_clips = 0;
2658 /* Set up S->gc, set clipping and draw S. */
2659 x_set_glyph_string_gc (s);
2661 /* Draw relief (if any) in advance for char/composition so that the
2662 glyph string can be drawn over it. */
2663 if (!s->for_overlaps
2664 && s->face->box != FACE_NO_BOX
2665 && (s->first_glyph->type == CHAR_GLYPH
2666 || s->first_glyph->type == COMPOSITE_GLYPH))
2669 x_set_glyph_string_clipping (s);
2670 x_draw_glyph_string_background (s, 1);
2671 x_draw_glyph_string_box (s);
2672 x_set_glyph_string_clipping (s);
2673 relief_drawn_p = 1;
2675 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2676 && !s->clip_tail
2677 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2678 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2679 /* We must clip just this glyph. left_overhang part has already
2680 drawn when s->prev was drawn, and right_overhang part will be
2681 drawn later when s->next is drawn. */
2682 x_set_glyph_string_clipping_exactly (s, s);
2683 else
2684 x_set_glyph_string_clipping (s);
2686 switch (s->first_glyph->type)
2688 case IMAGE_GLYPH:
2689 x_draw_image_glyph_string (s);
2690 break;
2691 #ifdef HAVE_XWIDGETS
2692 case XWIDGET_GLYPH:
2693 //erase xwidget background
2694 //x_draw_glyph_string_background (s, 0);
2695 x_draw_xwidget_glyph_string (s);
2696 break;
2697 #endif
2698 case STRETCH_GLYPH:
2699 x_draw_stretch_glyph_string (s);
2700 break;
2702 case CHAR_GLYPH:
2703 if (s->for_overlaps)
2704 s->background_filled_p = 1;
2705 else
2706 x_draw_glyph_string_background (s, 0);
2707 x_draw_glyph_string_foreground (s);
2708 break;
2710 case COMPOSITE_GLYPH:
2711 if (s->for_overlaps || (s->cmp_from > 0
2712 && ! s->first_glyph->u.cmp.automatic))
2713 s->background_filled_p = 1;
2714 else
2715 x_draw_glyph_string_background (s, 1);
2716 x_draw_composite_glyph_string_foreground (s);
2717 break;
2719 case GLYPHLESS_GLYPH:
2720 if (s->for_overlaps)
2721 s->background_filled_p = 1;
2722 else
2723 x_draw_glyph_string_background (s, 1);
2724 x_draw_glyphless_glyph_string_foreground (s);
2725 break;
2727 default:
2728 emacs_abort ();
2731 if (!s->for_overlaps)
2733 /* Draw underline. */
2734 if (s->face->underline_p)
2736 if (s->face->underline_type == FACE_UNDER_WAVE)
2738 if (s->face->underline_defaulted_p)
2739 x_draw_underwave (s);
2740 else
2742 XGCValues xgcv;
2743 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2744 XSetForeground (s->display, s->gc, s->face->underline_color);
2745 x_draw_underwave (s);
2746 XSetForeground (s->display, s->gc, xgcv.foreground);
2749 else if (s->face->underline_type == FACE_UNDER_LINE)
2751 unsigned long thickness, position;
2752 int y;
2754 if (s->prev && s->prev->face->underline_p
2755 && s->prev->face->underline_type == FACE_UNDER_LINE)
2757 /* We use the same underline style as the previous one. */
2758 thickness = s->prev->underline_thickness;
2759 position = s->prev->underline_position;
2761 else
2763 /* Get the underline thickness. Default is 1 pixel. */
2764 if (s->font && s->font->underline_thickness > 0)
2765 thickness = s->font->underline_thickness;
2766 else
2767 thickness = 1;
2768 if (x_underline_at_descent_line)
2769 position = (s->height - thickness) - (s->ybase - s->y);
2770 else
2772 /* Get the underline position. This is the recommended
2773 vertical offset in pixels from the baseline to the top of
2774 the underline. This is a signed value according to the
2775 specs, and its default is
2777 ROUND ((maximum descent) / 2), with
2778 ROUND(x) = floor (x + 0.5) */
2780 if (x_use_underline_position_properties
2781 && s->font && s->font->underline_position >= 0)
2782 position = s->font->underline_position;
2783 else if (s->font)
2784 position = (s->font->descent + 1) / 2;
2785 else
2786 position = underline_minimum_offset;
2788 position = max (position, underline_minimum_offset);
2790 /* Check the sanity of thickness and position. We should
2791 avoid drawing underline out of the current line area. */
2792 if (s->y + s->height <= s->ybase + position)
2793 position = (s->height - 1) - (s->ybase - s->y);
2794 if (s->y + s->height < s->ybase + position + thickness)
2795 thickness = (s->y + s->height) - (s->ybase + position);
2796 s->underline_thickness = thickness;
2797 s->underline_position = position;
2798 y = s->ybase + position;
2799 if (s->face->underline_defaulted_p)
2800 XFillRectangle (s->display, s->window, s->gc,
2801 s->x, y, s->width, thickness);
2802 else
2804 XGCValues xgcv;
2805 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2806 XSetForeground (s->display, s->gc, s->face->underline_color);
2807 XFillRectangle (s->display, s->window, s->gc,
2808 s->x, y, s->width, thickness);
2809 XSetForeground (s->display, s->gc, xgcv.foreground);
2813 /* Draw overline. */
2814 if (s->face->overline_p)
2816 unsigned long dy = 0, h = 1;
2818 if (s->face->overline_color_defaulted_p)
2819 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2820 s->width, h);
2821 else
2823 XGCValues xgcv;
2824 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2825 XSetForeground (s->display, s->gc, s->face->overline_color);
2826 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2827 s->width, h);
2828 XSetForeground (s->display, s->gc, xgcv.foreground);
2832 /* Draw strike-through. */
2833 if (s->face->strike_through_p)
2835 unsigned long h = 1;
2836 unsigned long dy = (s->height - h) / 2;
2838 if (s->face->strike_through_color_defaulted_p)
2839 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2840 s->width, h);
2841 else
2843 XGCValues xgcv;
2844 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2845 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2846 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2847 s->width, h);
2848 XSetForeground (s->display, s->gc, xgcv.foreground);
2852 /* Draw relief if not yet drawn. */
2853 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2854 x_draw_glyph_string_box (s);
2856 if (s->prev)
2858 struct glyph_string *prev;
2860 for (prev = s->prev; prev; prev = prev->prev)
2861 if (prev->hl != s->hl
2862 && prev->x + prev->width + prev->right_overhang > s->x)
2864 /* As prev was drawn while clipped to its own area, we
2865 must draw the right_overhang part using s->hl now. */
2866 enum draw_glyphs_face save = prev->hl;
2868 prev->hl = s->hl;
2869 x_set_glyph_string_gc (prev);
2870 x_set_glyph_string_clipping_exactly (s, prev);
2871 if (prev->first_glyph->type == CHAR_GLYPH)
2872 x_draw_glyph_string_foreground (prev);
2873 else
2874 x_draw_composite_glyph_string_foreground (prev);
2875 XSetClipMask (prev->display, prev->gc, None);
2876 prev->hl = save;
2877 prev->num_clips = 0;
2881 if (s->next)
2883 struct glyph_string *next;
2885 for (next = s->next; next; next = next->next)
2886 if (next->hl != s->hl
2887 && next->x - next->left_overhang < s->x + s->width)
2889 /* As next will be drawn while clipped to its own area,
2890 we must draw the left_overhang part using s->hl now. */
2891 enum draw_glyphs_face save = next->hl;
2893 next->hl = s->hl;
2894 x_set_glyph_string_gc (next);
2895 x_set_glyph_string_clipping_exactly (s, next);
2896 if (next->first_glyph->type == CHAR_GLYPH)
2897 x_draw_glyph_string_foreground (next);
2898 else
2899 x_draw_composite_glyph_string_foreground (next);
2900 XSetClipMask (next->display, next->gc, None);
2901 next->hl = save;
2902 next->num_clips = 0;
2903 next->clip_head = s->next;
2908 /* Reset clipping. */
2909 XSetClipMask (s->display, s->gc, None);
2910 s->num_clips = 0;
2913 /* Shift display to make room for inserted glyphs. */
2915 static void
2916 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
2918 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2919 f->output_data.x->normal_gc,
2920 x, y, width, height,
2921 x + shift_by, y);
2924 /* Delete N glyphs at the nominal cursor position. Not implemented
2925 for X frames. */
2927 static void
2928 x_delete_glyphs (struct frame *f, register int n)
2930 emacs_abort ();
2934 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2935 If they are <= 0, this is probably an error. */
2937 void
2938 x_clear_area (Display *dpy, Window window, int x, int y, int width, int height)
2940 eassert (width > 0 && height > 0);
2941 XClearArea (dpy, window, x, y, width, height, False);
2945 /* Clear an entire frame. */
2947 static void
2948 x_clear_frame (struct frame *f)
2950 /* Clearing the frame will erase any cursor, so mark them all as no
2951 longer visible. */
2952 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2954 block_input ();
2956 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
2958 /* We have to clear the scroll bars. If we have changed colors or
2959 something like that, then they should be notified. */
2960 x_scroll_bar_clear (f);
2962 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2963 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2964 redisplay, do it here. */
2965 if (FRAME_GTK_WIDGET (f))
2966 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2967 #endif
2969 XFlush (FRAME_X_DISPLAY (f));
2971 unblock_input ();
2976 /* Invert the middle quarter of the frame for .15 sec. */
2978 static void
2979 XTflash (struct frame *f)
2981 block_input ();
2984 #ifdef USE_GTK
2985 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
2986 when the scroll bars and the edit widget share the same X window. */
2987 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
2988 #ifdef HAVE_GTK3
2989 cairo_t *cr = gdk_cairo_create (window);
2990 cairo_set_source_rgb (cr, 1, 1, 1);
2991 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
2992 #define XFillRectangle(d, win, gc, x, y, w, h) \
2993 do { \
2994 cairo_rectangle (cr, x, y, w, h); \
2995 cairo_fill (cr); \
2997 while (0)
2998 #else /* ! HAVE_GTK3 */
2999 GdkGCValues vals;
3000 GdkGC *gc;
3001 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3002 ^ FRAME_BACKGROUND_PIXEL (f));
3003 vals.function = GDK_XOR;
3004 gc = gdk_gc_new_with_values (window,
3005 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
3006 #define XFillRectangle(d, win, gc, x, y, w, h) \
3007 gdk_draw_rectangle (window, gc, TRUE, x, y, w, h)
3008 #endif /* ! HAVE_GTK3 */
3009 #else /* ! USE_GTK */
3010 GC gc;
3012 /* Create a GC that will use the GXxor function to flip foreground
3013 pixels into background pixels. */
3015 XGCValues values;
3017 values.function = GXxor;
3018 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3019 ^ FRAME_BACKGROUND_PIXEL (f));
3021 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3022 GCFunction | GCForeground, &values);
3024 #endif
3026 /* Get the height not including a menu bar widget. */
3027 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
3028 /* Height of each line to flash. */
3029 int flash_height = FRAME_LINE_HEIGHT (f);
3030 /* These will be the left and right margins of the rectangles. */
3031 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3032 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3034 int width;
3036 /* Don't flash the area between a scroll bar and the frame
3037 edge it is next to. */
3038 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3040 case vertical_scroll_bar_left:
3041 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3042 break;
3044 case vertical_scroll_bar_right:
3045 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3046 break;
3048 default:
3049 break;
3052 width = flash_right - flash_left;
3054 /* If window is tall, flash top and bottom line. */
3055 if (height > 3 * FRAME_LINE_HEIGHT (f))
3057 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3058 flash_left,
3059 (FRAME_INTERNAL_BORDER_WIDTH (f)
3060 + FRAME_TOP_MARGIN_HEIGHT (f)),
3061 width, flash_height);
3062 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3063 flash_left,
3064 (height - flash_height
3065 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3066 width, flash_height);
3069 else
3070 /* If it is short, flash it all. */
3071 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3072 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3073 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3075 x_flush (f);
3078 struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
3079 struct timespec wakeup = timespec_add (current_timespec (), delay);
3081 /* Keep waiting until past the time wakeup or any input gets
3082 available. */
3083 while (! detect_input_pending ())
3085 struct timespec current = current_timespec ();
3086 struct timespec timeout;
3088 /* Break if result would not be positive. */
3089 if (timespec_cmp (wakeup, current) <= 0)
3090 break;
3092 /* How long `select' should wait. */
3093 timeout = make_timespec (0, 10 * 1000 * 1000);
3095 /* Try to wait that long--but we might wake up sooner. */
3096 pselect (0, NULL, NULL, NULL, &timeout, NULL);
3100 /* If window is tall, flash top and bottom line. */
3101 if (height > 3 * FRAME_LINE_HEIGHT (f))
3103 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3104 flash_left,
3105 (FRAME_INTERNAL_BORDER_WIDTH (f)
3106 + FRAME_TOP_MARGIN_HEIGHT (f)),
3107 width, flash_height);
3108 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3109 flash_left,
3110 (height - flash_height
3111 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3112 width, flash_height);
3114 else
3115 /* If it is short, flash it all. */
3116 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3117 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3118 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3120 #ifdef USE_GTK
3121 #ifdef HAVE_GTK3
3122 cairo_destroy (cr);
3123 #else
3124 g_object_unref (G_OBJECT (gc));
3125 #endif
3126 #undef XFillRectangle
3127 #else
3128 XFreeGC (FRAME_X_DISPLAY (f), gc);
3129 #endif
3130 x_flush (f);
3134 unblock_input ();
3138 static void
3139 XTtoggle_invisible_pointer (struct frame *f, int invisible)
3141 block_input ();
3142 if (invisible)
3144 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor != 0)
3145 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3146 FRAME_X_DISPLAY_INFO (f)->invisible_cursor);
3148 else
3149 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3150 f->output_data.x->current_cursor);
3151 f->pointer_invisible = invisible;
3152 unblock_input ();
3156 /* Make audible bell. */
3158 static void
3159 XTring_bell (struct frame *f)
3161 if (FRAME_X_DISPLAY (f))
3163 if (visible_bell)
3164 XTflash (f);
3165 else
3167 block_input ();
3168 #ifdef HAVE_XKB
3169 XkbBell (FRAME_X_DISPLAY (f), None, 0, None);
3170 #else
3171 XBell (FRAME_X_DISPLAY (f), 0);
3172 #endif
3173 XFlush (FRAME_X_DISPLAY (f));
3174 unblock_input ();
3179 /***********************************************************************
3180 Line Dance
3181 ***********************************************************************/
3183 /* Perform an insert-lines or delete-lines operation, inserting N
3184 lines or deleting -N lines at vertical position VPOS. */
3186 static void
3187 x_ins_del_lines (struct frame *f, int vpos, int n)
3189 emacs_abort ();
3193 /* Scroll part of the display as described by RUN. */
3195 static void
3196 x_scroll_run (struct window *w, struct run *run)
3198 struct frame *f = XFRAME (w->frame);
3199 int x, y, width, height, from_y, to_y, bottom_y;
3201 /* Get frame-relative bounding box of the text display area of W,
3202 without mode lines. Include in this box the left and right
3203 fringe of W. */
3204 window_box (w, ANY_AREA, &x, &y, &width, &height);
3206 #ifdef USE_TOOLKIT_SCROLL_BARS
3207 /* If the fringe is adjacent to the left (right) scroll bar of a
3208 leftmost (rightmost, respectively) window, then extend its
3209 background to the gap between the fringe and the bar. */
3210 if ((WINDOW_LEFTMOST_P (w)
3211 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
3212 || (WINDOW_RIGHTMOST_P (w)
3213 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
3215 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
3217 if (sb_width > 0)
3219 int bar_area_x = WINDOW_SCROLL_BAR_AREA_X (w);
3220 int bar_area_width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
3221 * FRAME_COLUMN_WIDTH (f));
3223 if (bar_area_x + bar_area_width == x)
3225 x = bar_area_x + sb_width;
3226 width += bar_area_width - sb_width;
3228 else if (x + width == bar_area_x)
3229 width += bar_area_width - sb_width;
3232 #endif
3234 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3235 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3236 bottom_y = y + height;
3238 if (to_y < from_y)
3240 /* Scrolling up. Make sure we don't copy part of the mode
3241 line at the bottom. */
3242 if (from_y + run->height > bottom_y)
3243 height = bottom_y - from_y;
3244 else
3245 height = run->height;
3247 else
3249 /* Scrolling down. Make sure we don't copy over the mode line.
3250 at the bottom. */
3251 if (to_y + run->height > bottom_y)
3252 height = bottom_y - to_y;
3253 else
3254 height = run->height;
3257 block_input ();
3259 /* Cursor off. Will be switched on again in x_update_window_end. */
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 (struct frame *f)
3282 /* We used to only do this if Vx_no_window_manager was non-nil, but
3283 the ICCCM (section 4.1.6) says that the window's border pixmap
3284 and border pixel are window attributes which are "private to the
3285 client", so we can always change it to whatever we want. */
3286 block_input ();
3287 /* I recently started to get errors in this XSetWindowBorder, depending on
3288 the window-manager in use, tho something more is at play since I've been
3289 using that same window-manager binary for ever. Let's not crash just
3290 because of this (bug#9310). */
3291 x_catch_errors (FRAME_X_DISPLAY (f));
3292 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3293 f->output_data.x->border_pixel);
3294 x_uncatch_errors ();
3295 unblock_input ();
3296 x_update_cursor (f, 1);
3297 x_set_frame_alpha (f);
3300 static void
3301 frame_unhighlight (struct frame *f)
3303 /* We used to only do this if Vx_no_window_manager was non-nil, but
3304 the ICCCM (section 4.1.6) says that the window's border pixmap
3305 and border pixel are window attributes which are "private to the
3306 client", so we can always change it to whatever we want. */
3307 block_input ();
3308 /* Same as above for XSetWindowBorder (bug#9310). */
3309 x_catch_errors (FRAME_X_DISPLAY (f));
3310 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3311 f->output_data.x->border_tile);
3312 x_uncatch_errors ();
3313 unblock_input ();
3314 x_update_cursor (f, 1);
3315 x_set_frame_alpha (f);
3318 /* The focus has changed. Update the frames as necessary to reflect
3319 the new situation. Note that we can't change the selected frame
3320 here, because the Lisp code we are interrupting might become confused.
3321 Each event gets marked with the frame in which it occurred, so the
3322 Lisp code can tell when the switch took place by examining the events. */
3324 static void
3325 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
3327 struct frame *old_focus = dpyinfo->x_focus_frame;
3329 if (frame != dpyinfo->x_focus_frame)
3331 /* Set this before calling other routines, so that they see
3332 the correct value of x_focus_frame. */
3333 dpyinfo->x_focus_frame = frame;
3335 if (old_focus && old_focus->auto_lower)
3336 x_lower_frame (old_focus);
3338 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3339 pending_autoraise_frame = dpyinfo->x_focus_frame;
3340 else
3341 pending_autoraise_frame = 0;
3344 x_frame_rehighlight (dpyinfo);
3347 /* Handle FocusIn and FocusOut state changes for FRAME.
3348 If FRAME has focus and there exists more than one frame, puts
3349 a FOCUS_IN_EVENT into *BUFP. */
3351 static void
3352 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, 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 /* When run as a daemon, Vterminal_frame is always NIL. */
3364 bufp->arg = (((NILP (Vterminal_frame) || EQ (Fdaemonp (), Qt))
3365 && CONSP (Vframe_list)
3366 && !NILP (XCDR (Vframe_list)))
3367 ? Qt : Qnil);
3368 bufp->kind = FOCUS_IN_EVENT;
3369 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);
3388 bufp->kind = FOCUS_OUT_EVENT;
3389 XSETFRAME (bufp->frame_or_window, frame);
3392 #ifdef HAVE_X_I18N
3393 if (FRAME_XIC (frame))
3394 XUnsetICFocus (FRAME_XIC (frame));
3395 #endif
3396 if (frame->pointer_invisible)
3397 XTtoggle_invisible_pointer (frame, 0);
3401 /* Return the Emacs frame-object corresponding to an X window.
3402 It could be the frame's main window or an icon window. */
3404 static struct frame *
3405 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3407 Lisp_Object tail, frame;
3408 struct frame *f;
3410 if (wdesc == None)
3411 return NULL;
3413 FOR_EACH_FRAME (tail, frame)
3415 f = XFRAME (frame);
3416 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
3417 continue;
3418 if (f->output_data.x->hourglass_window == wdesc)
3419 return f;
3420 #ifdef USE_X_TOOLKIT
3421 if ((f->output_data.x->edit_widget
3422 && XtWindow (f->output_data.x->edit_widget) == wdesc)
3423 /* A tooltip frame? */
3424 || (!f->output_data.x->edit_widget
3425 && FRAME_X_WINDOW (f) == wdesc)
3426 || f->output_data.x->icon_desc == wdesc)
3427 return f;
3428 #else /* not USE_X_TOOLKIT */
3429 #ifdef USE_GTK
3430 if (f->output_data.x->edit_widget)
3432 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3433 struct x_output *x = f->output_data.x;
3434 if (gwdesc != 0 && gwdesc == x->edit_widget)
3435 return f;
3437 #endif /* USE_GTK */
3438 if (FRAME_X_WINDOW (f) == wdesc
3439 || f->output_data.x->icon_desc == wdesc)
3440 return f;
3441 #endif /* not USE_X_TOOLKIT */
3443 return 0;
3446 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3448 /* Like x_window_to_frame but also compares the window with the widget's
3449 windows. */
3451 static struct frame *
3452 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3454 Lisp_Object tail, frame;
3455 struct frame *f, *found = NULL;
3456 struct x_output *x;
3458 if (wdesc == None)
3459 return NULL;
3461 FOR_EACH_FRAME (tail, frame)
3463 if (found)
3464 break;
3465 f = XFRAME (frame);
3466 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
3468 /* This frame matches if the window is any of its widgets. */
3469 x = f->output_data.x;
3470 if (x->hourglass_window == wdesc)
3471 found = f;
3472 else if (x->widget)
3474 #ifdef USE_GTK
3475 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3476 if (gwdesc != 0
3477 && gtk_widget_get_toplevel (gwdesc) == x->widget)
3478 found = f;
3479 #else
3480 if (wdesc == XtWindow (x->widget)
3481 || wdesc == XtWindow (x->column_widget)
3482 || wdesc == XtWindow (x->edit_widget))
3483 found = f;
3484 /* Match if the window is this frame's menubar. */
3485 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
3486 found = f;
3487 #endif
3489 else if (FRAME_X_WINDOW (f) == wdesc)
3490 /* A tooltip frame. */
3491 found = f;
3495 return found;
3498 /* Likewise, but consider only the menu bar widget. */
3500 static struct frame *
3501 x_menubar_window_to_frame (struct x_display_info *dpyinfo, XEvent *event)
3503 Window wdesc = event->xany.window;
3504 Lisp_Object tail, frame;
3505 struct frame *f;
3506 struct x_output *x;
3508 if (wdesc == None)
3509 return NULL;
3511 FOR_EACH_FRAME (tail, frame)
3513 f = XFRAME (frame);
3514 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
3515 continue;
3516 x = f->output_data.x;
3517 #ifdef USE_GTK
3518 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
3519 return f;
3520 #else
3521 /* Match if the window is this frame's menubar. */
3522 if (x->menubar_widget
3523 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
3524 return f;
3525 #endif
3527 return 0;
3530 /* Return the frame whose principal (outermost) window is WDESC.
3531 If WDESC is some other (smaller) window, we return 0. */
3533 struct frame *
3534 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
3536 Lisp_Object tail, frame;
3537 struct frame *f;
3538 struct x_output *x;
3540 if (wdesc == None)
3541 return NULL;
3543 FOR_EACH_FRAME (tail, frame)
3545 f = XFRAME (frame);
3546 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
3547 continue;
3548 x = f->output_data.x;
3550 if (x->widget)
3552 /* This frame matches if the window is its topmost widget. */
3553 #ifdef USE_GTK
3554 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
3555 if (gwdesc == x->widget)
3556 return f;
3557 #else
3558 if (wdesc == XtWindow (x->widget))
3559 return f;
3560 #endif
3562 else if (FRAME_X_WINDOW (f) == wdesc)
3563 /* Tooltip frame. */
3564 return f;
3566 return 0;
3569 #else /* !USE_X_TOOLKIT && !USE_GTK */
3571 #define x_any_window_to_frame(d, i) x_window_to_frame (d, i)
3572 #define x_top_window_to_frame(d, i) x_window_to_frame (d, i)
3574 #endif /* USE_X_TOOLKIT || USE_GTK */
3576 /* The focus may have changed. Figure out if it is a real focus change,
3577 by checking both FocusIn/Out and Enter/LeaveNotify events.
3579 Returns FOCUS_IN_EVENT event in *BUFP. */
3581 static void
3582 x_detect_focus_change (struct x_display_info *dpyinfo, XEvent *event, struct input_event *bufp)
3584 struct frame *frame;
3586 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3587 if (! frame)
3588 return;
3590 switch (event->type)
3592 case EnterNotify:
3593 case LeaveNotify:
3595 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3596 int focus_state
3597 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3599 if (event->xcrossing.detail != NotifyInferior
3600 && event->xcrossing.focus
3601 && ! (focus_state & FOCUS_EXPLICIT))
3602 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3603 FOCUS_IMPLICIT,
3604 dpyinfo, frame, bufp);
3606 break;
3608 case FocusIn:
3609 case FocusOut:
3610 x_focus_changed (event->type,
3611 (event->xfocus.detail == NotifyPointer ?
3612 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3613 dpyinfo, frame, bufp);
3614 break;
3616 case ClientMessage:
3617 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3619 enum xembed_message msg = event->xclient.data.l[1];
3620 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3621 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3623 break;
3628 #if defined HAVE_MENUS && !defined USE_X_TOOLKIT && !defined USE_GTK
3629 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3631 void
3632 x_mouse_leave (struct x_display_info *dpyinfo)
3634 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3636 #endif
3638 /* The focus has changed, or we have redirected a frame's focus to
3639 another frame (this happens when a frame uses a surrogate
3640 mini-buffer frame). Shift the highlight as appropriate.
3642 The FRAME argument doesn't necessarily have anything to do with which
3643 frame is being highlighted or un-highlighted; we only use it to find
3644 the appropriate X display info. */
3646 static void
3647 XTframe_rehighlight (struct frame *frame)
3649 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3652 static void
3653 x_frame_rehighlight (struct x_display_info *dpyinfo)
3655 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3657 if (dpyinfo->x_focus_frame)
3659 dpyinfo->x_highlight_frame
3660 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3661 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3662 : dpyinfo->x_focus_frame);
3663 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3665 fset_focus_frame (dpyinfo->x_focus_frame, Qnil);
3666 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3669 else
3670 dpyinfo->x_highlight_frame = 0;
3672 if (dpyinfo->x_highlight_frame != old_highlight)
3674 if (old_highlight)
3675 frame_unhighlight (old_highlight);
3676 if (dpyinfo->x_highlight_frame)
3677 frame_highlight (dpyinfo->x_highlight_frame);
3683 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3685 /* Initialize mode_switch_bit and modifier_meaning. */
3686 static void
3687 x_find_modifier_meanings (struct x_display_info *dpyinfo)
3689 int min_code, max_code;
3690 KeySym *syms;
3691 int syms_per_code;
3692 XModifierKeymap *mods;
3694 dpyinfo->meta_mod_mask = 0;
3695 dpyinfo->shift_lock_mask = 0;
3696 dpyinfo->alt_mod_mask = 0;
3697 dpyinfo->super_mod_mask = 0;
3698 dpyinfo->hyper_mod_mask = 0;
3700 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3702 syms = XGetKeyboardMapping (dpyinfo->display,
3703 min_code, max_code - min_code + 1,
3704 &syms_per_code);
3705 mods = XGetModifierMapping (dpyinfo->display);
3707 /* Scan the modifier table to see which modifier bits the Meta and
3708 Alt keysyms are on. */
3710 int row, col; /* The row and column in the modifier table. */
3711 int found_alt_or_meta;
3713 for (row = 3; row < 8; row++)
3715 found_alt_or_meta = 0;
3716 for (col = 0; col < mods->max_keypermod; col++)
3718 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3720 /* Zeroes are used for filler. Skip them. */
3721 if (code == 0)
3722 continue;
3724 /* Are any of this keycode's keysyms a meta key? */
3726 int code_col;
3728 for (code_col = 0; code_col < syms_per_code; code_col++)
3730 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3732 switch (sym)
3734 case XK_Meta_L:
3735 case XK_Meta_R:
3736 found_alt_or_meta = 1;
3737 dpyinfo->meta_mod_mask |= (1 << row);
3738 break;
3740 case XK_Alt_L:
3741 case XK_Alt_R:
3742 found_alt_or_meta = 1;
3743 dpyinfo->alt_mod_mask |= (1 << row);
3744 break;
3746 case XK_Hyper_L:
3747 case XK_Hyper_R:
3748 if (!found_alt_or_meta)
3749 dpyinfo->hyper_mod_mask |= (1 << row);
3750 code_col = syms_per_code;
3751 col = mods->max_keypermod;
3752 break;
3754 case XK_Super_L:
3755 case XK_Super_R:
3756 if (!found_alt_or_meta)
3757 dpyinfo->super_mod_mask |= (1 << row);
3758 code_col = syms_per_code;
3759 col = mods->max_keypermod;
3760 break;
3762 case XK_Shift_Lock:
3763 /* Ignore this if it's not on the lock modifier. */
3764 if (!found_alt_or_meta && ((1 << row) == LockMask))
3765 dpyinfo->shift_lock_mask = LockMask;
3766 code_col = syms_per_code;
3767 col = mods->max_keypermod;
3768 break;
3776 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3777 if (! dpyinfo->meta_mod_mask)
3779 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3780 dpyinfo->alt_mod_mask = 0;
3783 /* If some keys are both alt and meta,
3784 make them just meta, not alt. */
3785 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3787 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3790 XFree (syms);
3791 XFreeModifiermap (mods);
3794 /* Convert between the modifier bits X uses and the modifier bits
3795 Emacs uses. */
3798 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
3800 int mod_meta = meta_modifier;
3801 int mod_alt = alt_modifier;
3802 int mod_hyper = hyper_modifier;
3803 int mod_super = super_modifier;
3804 Lisp_Object tem;
3806 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3807 if (INTEGERP (tem)) mod_alt = XINT (tem) & INT_MAX;
3808 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3809 if (INTEGERP (tem)) mod_meta = XINT (tem) & INT_MAX;
3810 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3811 if (INTEGERP (tem)) mod_hyper = XINT (tem) & INT_MAX;
3812 tem = Fget (Vx_super_keysym, Qmodifier_value);
3813 if (INTEGERP (tem)) mod_super = XINT (tem) & INT_MAX;
3815 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3816 | ((state & ControlMask) ? ctrl_modifier : 0)
3817 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3818 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3819 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3820 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3823 static int
3824 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
3826 EMACS_INT mod_meta = meta_modifier;
3827 EMACS_INT mod_alt = alt_modifier;
3828 EMACS_INT mod_hyper = hyper_modifier;
3829 EMACS_INT mod_super = super_modifier;
3831 Lisp_Object tem;
3833 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3834 if (INTEGERP (tem)) mod_alt = XINT (tem);
3835 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3836 if (INTEGERP (tem)) mod_meta = XINT (tem);
3837 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3838 if (INTEGERP (tem)) mod_hyper = XINT (tem);
3839 tem = Fget (Vx_super_keysym, Qmodifier_value);
3840 if (INTEGERP (tem)) mod_super = XINT (tem);
3843 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3844 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3845 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3846 | ((state & shift_modifier) ? ShiftMask : 0)
3847 | ((state & ctrl_modifier) ? ControlMask : 0)
3848 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3851 /* Convert a keysym to its name. */
3853 char *
3854 x_get_keysym_name (int keysym)
3856 char *value;
3858 block_input ();
3859 value = XKeysymToString (keysym);
3860 unblock_input ();
3862 return value;
3867 /* Mouse clicks and mouse movement. Rah. */
3869 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3871 If the event is a button press, then note that we have grabbed
3872 the mouse. */
3874 static Lisp_Object
3875 construct_mouse_click (struct input_event *result, XButtonEvent *event, struct frame *f)
3877 /* Make the event type NO_EVENT; we'll change that when we decide
3878 otherwise. */
3879 result->kind = MOUSE_CLICK_EVENT;
3880 result->code = event->button - Button1;
3881 result->timestamp = event->time;
3882 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3883 event->state)
3884 | (event->type == ButtonRelease
3885 ? up_modifier
3886 : down_modifier));
3888 XSETINT (result->x, event->x);
3889 XSETINT (result->y, event->y);
3890 XSETFRAME (result->frame_or_window, f);
3891 result->arg = Qnil;
3892 return Qnil;
3895 /* Function to report a mouse movement to the mainstream Emacs code.
3896 The input handler calls this.
3898 We have received a mouse movement event, which is given in *event.
3899 If the mouse is over a different glyph than it was last time, tell
3900 the mainstream emacs code by setting mouse_moved. If not, ask for
3901 another motion event, so we can check again the next time it moves. */
3903 static XMotionEvent last_mouse_motion_event;
3904 static Lisp_Object last_mouse_motion_frame;
3906 static int
3907 note_mouse_movement (struct frame *frame, XMotionEvent *event)
3909 last_mouse_movement_time = event->time;
3910 last_mouse_motion_event = *event;
3911 XSETFRAME (last_mouse_motion_frame, frame);
3913 if (!FRAME_X_OUTPUT (frame))
3914 return 0;
3916 if (event->window != FRAME_X_WINDOW (frame))
3918 frame->mouse_moved = 1;
3919 last_mouse_scroll_bar = Qnil;
3920 note_mouse_highlight (frame, -1, -1);
3921 last_mouse_glyph_frame = 0;
3922 return 1;
3926 /* Has the mouse moved off the glyph it was on at the last sighting? */
3927 if (frame != last_mouse_glyph_frame
3928 || event->x < last_mouse_glyph.x
3929 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3930 || event->y < last_mouse_glyph.y
3931 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3933 frame->mouse_moved = 1;
3934 last_mouse_scroll_bar = Qnil;
3935 note_mouse_highlight (frame, event->x, event->y);
3936 /* Remember which glyph we're now on. */
3937 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3938 last_mouse_glyph_frame = frame;
3939 return 1;
3942 return 0;
3946 /************************************************************************
3947 Mouse Face
3948 ************************************************************************/
3950 static void
3951 redo_mouse_highlight (void)
3953 if (!NILP (last_mouse_motion_frame)
3954 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3955 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3956 last_mouse_motion_event.x,
3957 last_mouse_motion_event.y);
3962 /* Return the current position of the mouse.
3963 *FP should be a frame which indicates which display to ask about.
3965 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3966 and *PART to the frame, window, and scroll bar part that the mouse
3967 is over. Set *X and *Y to the portion and whole of the mouse's
3968 position on the scroll bar.
3970 If the mouse movement started elsewhere, set *FP to the frame the
3971 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3972 the mouse is over.
3974 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
3975 was at this position.
3977 Don't store anything if we don't have a valid set of values to report.
3979 This clears the mouse_moved flag, so we can wait for the next mouse
3980 movement. */
3982 static void
3983 XTmouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
3984 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3985 Time *timestamp)
3987 struct frame *f1;
3989 block_input ();
3991 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3992 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
3993 else
3995 Window root;
3996 int root_x, root_y;
3998 Window dummy_window;
3999 int dummy;
4001 Lisp_Object frame, tail;
4003 /* Clear the mouse-moved flag for every frame on this display. */
4004 FOR_EACH_FRAME (tail, frame)
4005 if (FRAME_X_P (XFRAME (frame))
4006 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
4007 XFRAME (frame)->mouse_moved = 0;
4009 last_mouse_scroll_bar = Qnil;
4011 /* Figure out which root window we're on. */
4012 XQueryPointer (FRAME_X_DISPLAY (*fp),
4013 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
4015 /* The root window which contains the pointer. */
4016 &root,
4018 /* Trash which we can't trust if the pointer is on
4019 a different screen. */
4020 &dummy_window,
4022 /* The position on that root window. */
4023 &root_x, &root_y,
4025 /* More trash we can't trust. */
4026 &dummy, &dummy,
4028 /* Modifier keys and pointer buttons, about which
4029 we don't care. */
4030 (unsigned int *) &dummy);
4032 /* Now we have a position on the root; find the innermost window
4033 containing the pointer. */
4035 Window win, child;
4036 int win_x, win_y;
4037 int parent_x = 0, parent_y = 0;
4039 win = root;
4041 /* XTranslateCoordinates can get errors if the window
4042 structure is changing at the same time this function
4043 is running. So at least we must not crash from them. */
4045 x_catch_errors (FRAME_X_DISPLAY (*fp));
4047 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
4048 && FRAME_LIVE_P (last_mouse_frame))
4050 /* If mouse was grabbed on a frame, give coords for that frame
4051 even if the mouse is now outside it. */
4052 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4054 /* From-window, to-window. */
4055 root, FRAME_X_WINDOW (last_mouse_frame),
4057 /* From-position, to-position. */
4058 root_x, root_y, &win_x, &win_y,
4060 /* Child of win. */
4061 &child);
4062 f1 = last_mouse_frame;
4064 else
4066 while (1)
4068 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4070 /* From-window, to-window. */
4071 root, win,
4073 /* From-position, to-position. */
4074 root_x, root_y, &win_x, &win_y,
4076 /* Child of win. */
4077 &child);
4079 if (child == None || child == win)
4080 break;
4081 #ifdef USE_GTK
4082 /* We don't wan't to know the innermost window. We
4083 want the edit window. For non-Gtk+ the innermost
4084 window is the edit window. For Gtk+ it might not
4085 be. It might be the tool bar for example. */
4086 if (x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win))
4087 break;
4088 #endif
4089 win = child;
4090 parent_x = win_x;
4091 parent_y = win_y;
4094 /* Now we know that:
4095 win is the innermost window containing the pointer
4096 (XTC says it has no child containing the pointer),
4097 win_x and win_y are the pointer's position in it
4098 (XTC did this the last time through), and
4099 parent_x and parent_y are the pointer's position in win's parent.
4100 (They are what win_x and win_y were when win was child.
4101 If win is the root window, it has no parent, and
4102 parent_{x,y} are invalid, but that's okay, because we'll
4103 never use them in that case.) */
4105 #ifdef USE_GTK
4106 /* We don't wan't to know the innermost window. We
4107 want the edit window. */
4108 f1 = x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
4109 #else
4110 /* Is win one of our frames? */
4111 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
4112 #endif
4114 #ifdef USE_X_TOOLKIT
4115 /* If we end up with the menu bar window, say it's not
4116 on the frame. */
4117 if (f1 != NULL
4118 && f1->output_data.x->menubar_widget
4119 && win == XtWindow (f1->output_data.x->menubar_widget))
4120 f1 = NULL;
4121 #endif /* USE_X_TOOLKIT */
4124 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
4125 f1 = 0;
4127 x_uncatch_errors ();
4129 /* If not, is it one of our scroll bars? */
4130 if (! f1)
4132 struct scroll_bar *bar;
4134 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
4136 if (bar)
4138 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4139 win_x = parent_x;
4140 win_y = parent_y;
4144 if (f1 == 0 && insist > 0)
4145 f1 = SELECTED_FRAME ();
4147 if (f1)
4149 /* Ok, we found a frame. Store all the values.
4150 last_mouse_glyph is a rectangle used to reduce the
4151 generation of mouse events. To not miss any motion
4152 events, we must divide the frame into rectangles of the
4153 size of the smallest character that could be displayed
4154 on it, i.e. into the same rectangles that matrices on
4155 the frame are divided into. */
4157 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
4158 last_mouse_glyph_frame = f1;
4160 *bar_window = Qnil;
4161 *part = 0;
4162 *fp = f1;
4163 XSETINT (*x, win_x);
4164 XSETINT (*y, win_y);
4165 *timestamp = last_mouse_movement_time;
4170 unblock_input ();
4175 /***********************************************************************
4176 Scroll bars
4177 ***********************************************************************/
4179 /* Scroll bar support. */
4181 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4182 manages it.
4183 This can be called in GC, so we have to make sure to strip off mark
4184 bits. */
4186 static struct scroll_bar *
4187 x_window_to_scroll_bar (Display *display, Window window_id)
4189 Lisp_Object tail, frame;
4191 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4192 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4193 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4195 FOR_EACH_FRAME (tail, frame)
4197 Lisp_Object bar, condemned;
4199 if (! FRAME_X_P (XFRAME (frame)))
4200 continue;
4202 /* Scan this frame's scroll bar list for a scroll bar with the
4203 right window ID. */
4204 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4205 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4206 /* This trick allows us to search both the ordinary and
4207 condemned scroll bar lists with one loop. */
4208 ! NILP (bar) || (bar = condemned,
4209 condemned = Qnil,
4210 ! NILP (bar));
4211 bar = XSCROLL_BAR (bar)->next)
4212 if (XSCROLL_BAR (bar)->x_window == window_id &&
4213 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4214 return XSCROLL_BAR (bar);
4217 return NULL;
4221 #if defined USE_LUCID
4223 /* Return the Lucid menu bar WINDOW is part of. Return null
4224 if WINDOW is not part of a menu bar. */
4226 static Widget
4227 x_window_to_menu_bar (Window window)
4229 Lisp_Object tail, frame;
4231 FOR_EACH_FRAME (tail, frame)
4232 if (FRAME_X_P (XFRAME (frame)))
4234 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4236 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4237 return menu_bar;
4239 return NULL;
4242 #endif /* USE_LUCID */
4245 /************************************************************************
4246 Toolkit scroll bars
4247 ************************************************************************/
4249 #ifdef USE_TOOLKIT_SCROLL_BARS
4251 static void x_scroll_bar_to_input_event (XEvent *, struct input_event *);
4252 static void x_send_scroll_bar_event (Lisp_Object, int, int, int);
4253 static void x_create_toolkit_scroll_bar (struct frame *,
4254 struct scroll_bar *);
4255 static void x_set_toolkit_scroll_bar_thumb (struct scroll_bar *,
4256 int, int, int);
4259 /* Lisp window being scrolled. Set when starting to interact with
4260 a toolkit scroll bar, reset to nil when ending the interaction. */
4262 static Lisp_Object window_being_scrolled;
4264 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4265 that movements of 1/20 of the screen size are mapped to up/down. */
4267 #ifndef USE_GTK
4268 /* Id of action hook installed for scroll bars. */
4270 static XtActionHookId action_hook_id;
4272 static Boolean xaw3d_arrow_scroll;
4274 /* Whether the drag scrolling maintains the mouse at the top of the
4275 thumb. If not, resizing the thumb needs to be done more carefully
4276 to avoid jerkiness. */
4278 static Boolean xaw3d_pick_top;
4280 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4281 bars are used.. The hook is responsible for detecting when
4282 the user ends an interaction with the scroll bar, and generates
4283 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4285 static void
4286 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
4287 XEvent *event, String *params, Cardinal *num_params)
4289 int scroll_bar_p;
4290 const char *end_action;
4292 #ifdef USE_MOTIF
4293 scroll_bar_p = XmIsScrollBar (widget);
4294 end_action = "Release";
4295 #else /* !USE_MOTIF i.e. use Xaw */
4296 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4297 end_action = "EndScroll";
4298 #endif /* USE_MOTIF */
4300 if (scroll_bar_p
4301 && strcmp (action_name, end_action) == 0
4302 && WINDOWP (window_being_scrolled))
4304 struct window *w;
4305 struct scroll_bar *bar;
4307 x_send_scroll_bar_event (window_being_scrolled,
4308 scroll_bar_end_scroll, 0, 0);
4309 w = XWINDOW (window_being_scrolled);
4310 bar = XSCROLL_BAR (w->vertical_scroll_bar);
4312 if (bar->dragging != -1)
4314 bar->dragging = -1;
4315 /* The thumb size is incorrect while dragging: fix it. */
4316 set_vertical_scroll_bar (w);
4318 window_being_scrolled = Qnil;
4319 #if defined (USE_LUCID)
4320 bar->last_seen_part = scroll_bar_nowhere;
4321 #endif
4322 /* Xt timeouts no longer needed. */
4323 toolkit_scroll_bar_interaction = 0;
4326 #endif /* not USE_GTK */
4328 /* A vector of windows used for communication between
4329 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4331 static struct window **scroll_bar_windows;
4332 static ptrdiff_t scroll_bar_windows_size;
4335 /* Send a client message with message type Xatom_Scrollbar for a
4336 scroll action to the frame of WINDOW. PART is a value identifying
4337 the part of the scroll bar that was clicked on. PORTION is the
4338 amount to scroll of a whole of WHOLE. */
4340 static void
4341 x_send_scroll_bar_event (Lisp_Object window, int part, int portion, int whole)
4343 XEvent event;
4344 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4345 struct window *w = XWINDOW (window);
4346 struct frame *f = XFRAME (w->frame);
4347 ptrdiff_t i;
4349 block_input ();
4351 /* Construct a ClientMessage event to send to the frame. */
4352 ev->type = ClientMessage;
4353 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4354 ev->display = FRAME_X_DISPLAY (f);
4355 ev->window = FRAME_X_WINDOW (f);
4356 ev->format = 32;
4358 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4359 not enough to store a pointer or Lisp_Object on a 64 bit system.
4360 So, store the window in scroll_bar_windows and pass the index
4361 into that array in the event. */
4362 for (i = 0; i < scroll_bar_windows_size; ++i)
4363 if (scroll_bar_windows[i] == NULL)
4364 break;
4366 if (i == scroll_bar_windows_size)
4368 ptrdiff_t old_nbytes =
4369 scroll_bar_windows_size * sizeof *scroll_bar_windows;
4370 ptrdiff_t nbytes;
4371 enum { XClientMessageEvent_MAX = 0x7fffffff };
4372 scroll_bar_windows =
4373 xpalloc (scroll_bar_windows, &scroll_bar_windows_size, 1,
4374 XClientMessageEvent_MAX, sizeof *scroll_bar_windows);
4375 nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4376 memset (&scroll_bar_windows[i], 0, nbytes - old_nbytes);
4379 scroll_bar_windows[i] = w;
4380 ev->data.l[0] = (long) i;
4381 ev->data.l[1] = (long) part;
4382 ev->data.l[2] = (long) 0;
4383 ev->data.l[3] = (long) portion;
4384 ev->data.l[4] = (long) whole;
4386 /* Make Xt timeouts work while the scroll bar is active. */
4387 #ifdef USE_X_TOOLKIT
4388 toolkit_scroll_bar_interaction = 1;
4389 x_activate_timeout_atimer ();
4390 #endif
4392 /* Setting the event mask to zero means that the message will
4393 be sent to the client that created the window, and if that
4394 window no longer exists, no event will be sent. */
4395 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4396 unblock_input ();
4400 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4401 in *IEVENT. */
4403 static void
4404 x_scroll_bar_to_input_event (XEvent *event, struct input_event *ievent)
4406 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4407 Lisp_Object window;
4408 struct window *w;
4410 w = scroll_bar_windows[ev->data.l[0]];
4411 scroll_bar_windows[ev->data.l[0]] = NULL;
4413 XSETWINDOW (window, w);
4415 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4416 ievent->frame_or_window = window;
4417 ievent->arg = Qnil;
4418 #ifdef USE_GTK
4419 ievent->timestamp = CurrentTime;
4420 #else
4421 ievent->timestamp =
4422 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4423 #endif
4424 ievent->part = ev->data.l[1];
4425 ievent->code = ev->data.l[2];
4426 ievent->x = make_number ((int) ev->data.l[3]);
4427 ievent->y = make_number ((int) ev->data.l[4]);
4428 ievent->modifiers = 0;
4432 #ifdef USE_MOTIF
4434 /* Minimum and maximum values used for Motif scroll bars. */
4436 #define XM_SB_MAX 10000000
4439 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4440 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4441 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4443 static void
4444 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4446 struct scroll_bar *bar = client_data;
4447 XmScrollBarCallbackStruct *cs = call_data;
4448 int part = -1, whole = 0, portion = 0;
4450 switch (cs->reason)
4452 case XmCR_DECREMENT:
4453 bar->dragging = -1;
4454 part = scroll_bar_up_arrow;
4455 break;
4457 case XmCR_INCREMENT:
4458 bar->dragging = -1;
4459 part = scroll_bar_down_arrow;
4460 break;
4462 case XmCR_PAGE_DECREMENT:
4463 bar->dragging = -1;
4464 part = scroll_bar_above_handle;
4465 break;
4467 case XmCR_PAGE_INCREMENT:
4468 bar->dragging = -1;
4469 part = scroll_bar_below_handle;
4470 break;
4472 case XmCR_TO_TOP:
4473 bar->dragging = -1;
4474 part = scroll_bar_to_top;
4475 break;
4477 case XmCR_TO_BOTTOM:
4478 bar->dragging = -1;
4479 part = scroll_bar_to_bottom;
4480 break;
4482 case XmCR_DRAG:
4484 int slider_size;
4486 /* Get the slider size. */
4487 block_input ();
4488 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4489 unblock_input ();
4491 whole = XM_SB_MAX - slider_size;
4492 portion = min (cs->value, whole);
4493 part = scroll_bar_handle;
4494 bar->dragging = cs->value;
4496 break;
4498 case XmCR_VALUE_CHANGED:
4499 break;
4502 if (part >= 0)
4504 window_being_scrolled = bar->window;
4505 x_send_scroll_bar_event (bar->window, part, portion, whole);
4509 #elif defined USE_GTK
4511 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4512 bar widget. DATA is a pointer to the scroll_bar structure. */
4514 static gboolean
4515 xg_scroll_callback (GtkRange *range,
4516 GtkScrollType scroll,
4517 gdouble value,
4518 gpointer user_data)
4520 struct scroll_bar *bar = user_data;
4521 gdouble position;
4522 int part = -1, whole = 0, portion = 0;
4523 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4524 struct frame *f = g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4526 if (xg_ignore_gtk_scrollbar) return FALSE;
4527 position = gtk_adjustment_get_value (adj);
4530 switch (scroll)
4532 case GTK_SCROLL_JUMP:
4533 /* Buttons 1 2 or 3 must be grabbed. */
4534 if (FRAME_X_DISPLAY_INFO (f)->grabbed != 0
4535 && FRAME_X_DISPLAY_INFO (f)->grabbed < (1 << 4))
4537 part = scroll_bar_handle;
4538 whole = gtk_adjustment_get_upper (adj) -
4539 gtk_adjustment_get_page_size (adj);
4540 portion = min ((int)position, whole);
4541 bar->dragging = portion;
4543 break;
4544 case GTK_SCROLL_STEP_BACKWARD:
4545 part = scroll_bar_up_arrow;
4546 bar->dragging = -1;
4547 break;
4548 case GTK_SCROLL_STEP_FORWARD:
4549 part = scroll_bar_down_arrow;
4550 bar->dragging = -1;
4551 break;
4552 case GTK_SCROLL_PAGE_BACKWARD:
4553 part = scroll_bar_above_handle;
4554 bar->dragging = -1;
4555 break;
4556 case GTK_SCROLL_PAGE_FORWARD:
4557 part = scroll_bar_below_handle;
4558 bar->dragging = -1;
4559 break;
4562 if (part >= 0)
4564 window_being_scrolled = bar->window;
4565 x_send_scroll_bar_event (bar->window, part, portion, whole);
4568 return FALSE;
4571 /* Callback for button release. Sets dragging to -1 when dragging is done. */
4573 static gboolean
4574 xg_end_scroll_callback (GtkWidget *widget,
4575 GdkEventButton *event,
4576 gpointer user_data)
4578 struct scroll_bar *bar = user_data;
4579 bar->dragging = -1;
4580 if (WINDOWP (window_being_scrolled))
4582 x_send_scroll_bar_event (window_being_scrolled,
4583 scroll_bar_end_scroll, 0, 0);
4584 window_being_scrolled = Qnil;
4587 return FALSE;
4591 #else /* not USE_GTK and not USE_MOTIF */
4593 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4594 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4595 scroll bar struct. CALL_DATA is a pointer to a float saying where
4596 the thumb is. */
4598 static void
4599 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4601 struct scroll_bar *bar = client_data;
4602 float *top_addr = call_data;
4603 float top = *top_addr;
4604 float shown;
4605 int whole, portion, height;
4606 enum scroll_bar_part part;
4608 /* Get the size of the thumb, a value between 0 and 1. */
4609 block_input ();
4610 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4611 unblock_input ();
4613 whole = 10000000;
4614 portion = shown < 1 ? top * whole : 0;
4616 if (shown < 1 && (eabs (top + shown - 1) < 1.0f / height))
4617 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4618 the bottom, so we force the scrolling whenever we see that we're
4619 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4620 we try to ensure that we always stay two pixels away from the
4621 bottom). */
4622 part = scroll_bar_down_arrow;
4623 else
4624 part = scroll_bar_handle;
4626 window_being_scrolled = bar->window;
4627 bar->dragging = portion;
4628 bar->last_seen_part = part;
4629 x_send_scroll_bar_event (bar->window, part, portion, whole);
4633 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4634 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4635 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4636 the scroll bar. CALL_DATA is an integer specifying the action that
4637 has taken place. Its magnitude is in the range 0..height of the
4638 scroll bar. Negative values mean scroll towards buffer start.
4639 Values < height of scroll bar mean line-wise movement. */
4641 static void
4642 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4644 struct scroll_bar *bar = client_data;
4645 /* The position really is stored cast to a pointer. */
4646 int position = (intptr_t) call_data;
4647 Dimension height;
4648 enum scroll_bar_part part;
4650 /* Get the height of the scroll bar. */
4651 block_input ();
4652 XtVaGetValues (widget, XtNheight, &height, NULL);
4653 unblock_input ();
4655 if (eabs (position) >= height)
4656 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4658 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4659 it maps line-movement to call_data = max(5, height/20). */
4660 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4661 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4662 else
4663 part = scroll_bar_move_ratio;
4665 window_being_scrolled = bar->window;
4666 bar->dragging = -1;
4667 bar->last_seen_part = part;
4668 x_send_scroll_bar_event (bar->window, part, position, height);
4671 #endif /* not USE_GTK and not USE_MOTIF */
4673 #define SCROLL_BAR_NAME "verticalScrollBar"
4675 /* Create the widget for scroll bar BAR on frame F. Record the widget
4676 and X window of the scroll bar in BAR. */
4678 #ifdef USE_GTK
4679 static void
4680 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4682 const char *scroll_bar_name = SCROLL_BAR_NAME;
4684 block_input ();
4685 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4686 G_CALLBACK (xg_end_scroll_callback),
4687 scroll_bar_name);
4688 unblock_input ();
4691 #else /* not USE_GTK */
4693 static void
4694 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4696 Window xwindow;
4697 Widget widget;
4698 Arg av[20];
4699 int ac = 0;
4700 const char *scroll_bar_name = SCROLL_BAR_NAME;
4701 unsigned long pixel;
4703 block_input ();
4705 #ifdef USE_MOTIF
4706 /* Set resources. Create the widget. */
4707 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4708 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4709 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4710 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4711 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4712 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4713 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4715 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4716 if (pixel != -1)
4718 XtSetArg (av[ac], XmNforeground, pixel);
4719 ++ac;
4722 pixel = f->output_data.x->scroll_bar_background_pixel;
4723 if (pixel != -1)
4725 XtSetArg (av[ac], XmNbackground, pixel);
4726 ++ac;
4729 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4730 (char *) scroll_bar_name, av, ac);
4732 /* Add one callback for everything that can happen. */
4733 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4734 (XtPointer) bar);
4735 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4736 (XtPointer) bar);
4737 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4738 (XtPointer) bar);
4739 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4740 (XtPointer) bar);
4741 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4742 (XtPointer) bar);
4743 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4744 (XtPointer) bar);
4745 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4746 (XtPointer) bar);
4748 /* Realize the widget. Only after that is the X window created. */
4749 XtRealizeWidget (widget);
4751 /* Set the cursor to an arrow. I didn't find a resource to do that.
4752 And I'm wondering why it hasn't an arrow cursor by default. */
4753 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4754 f->output_data.x->nontext_cursor);
4756 #else /* !USE_MOTIF i.e. use Xaw */
4758 /* Set resources. Create the widget. The background of the
4759 Xaw3d scroll bar widget is a little bit light for my taste.
4760 We don't alter it here to let users change it according
4761 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4762 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4763 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4764 /* For smoother scrolling with Xaw3d -sm */
4765 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4767 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4768 if (pixel != -1)
4770 XtSetArg (av[ac], XtNforeground, pixel);
4771 ++ac;
4774 pixel = f->output_data.x->scroll_bar_background_pixel;
4775 if (pixel != -1)
4777 XtSetArg (av[ac], XtNbackground, pixel);
4778 ++ac;
4781 /* Top/bottom shadow colors. */
4783 /* Allocate them, if necessary. */
4784 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4786 pixel = f->output_data.x->scroll_bar_background_pixel;
4787 if (pixel != -1)
4789 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4790 FRAME_X_COLORMAP (f),
4791 &pixel, 1.2, 0x8000))
4792 pixel = -1;
4793 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4796 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4798 pixel = f->output_data.x->scroll_bar_background_pixel;
4799 if (pixel != -1)
4801 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4802 FRAME_X_COLORMAP (f),
4803 &pixel, 0.6, 0x4000))
4804 pixel = -1;
4805 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4809 #ifdef XtNbeNiceToColormap
4810 /* Tell the toolkit about them. */
4811 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4812 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4813 /* We tried to allocate a color for the top/bottom shadow, and
4814 failed, so tell Xaw3d to use dithering instead. */
4815 /* But only if we have a small colormap. Xaw3d can allocate nice
4816 colors itself. */
4818 XtSetArg (av[ac], XtNbeNiceToColormap,
4819 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
4820 ++ac;
4822 else
4823 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4824 be more consistent with other emacs 3d colors, and since Xaw3d is
4825 not good at dealing with allocation failure. */
4827 /* This tells Xaw3d to use real colors instead of dithering for
4828 the shadows. */
4829 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4830 ++ac;
4832 /* Specify the colors. */
4833 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4834 if (pixel != -1)
4836 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4837 ++ac;
4839 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4840 if (pixel != -1)
4842 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4843 ++ac;
4846 #endif
4848 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4849 f->output_data.x->edit_widget, av, ac);
4852 char const *initial = "";
4853 char const *val = initial;
4854 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4855 #ifdef XtNarrowScrollbars
4856 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4857 #endif
4858 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4859 if (xaw3d_arrow_scroll || val == initial)
4860 { /* ARROW_SCROLL */
4861 xaw3d_arrow_scroll = True;
4862 /* Isn't that just a personal preference ? --Stef */
4863 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4867 /* Define callbacks. */
4868 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4869 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4870 (XtPointer) bar);
4872 /* Realize the widget. Only after that is the X window created. */
4873 XtRealizeWidget (widget);
4875 #endif /* !USE_MOTIF */
4877 /* Install an action hook that lets us detect when the user
4878 finishes interacting with a scroll bar. */
4879 if (action_hook_id == 0)
4880 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4882 /* Remember X window and widget in the scroll bar vector. */
4883 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4884 xwindow = XtWindow (widget);
4885 bar->x_window = xwindow;
4887 unblock_input ();
4889 #endif /* not USE_GTK */
4892 /* Set the thumb size and position of scroll bar BAR. We are currently
4893 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4895 #ifdef USE_GTK
4896 static void
4897 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
4899 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4902 #else /* not USE_GTK */
4903 static void
4904 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
4905 int whole)
4907 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4908 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4909 float top, shown;
4911 block_input ();
4913 #ifdef USE_MOTIF
4915 if (scroll_bar_adjust_thumb_portion_p)
4917 /* We use an estimate of 30 chars per line rather than the real
4918 `portion' value. This has the disadvantage that the thumb size
4919 is not very representative, but it makes our life a lot easier.
4920 Otherwise, we have to constantly adjust the thumb size, which
4921 we can't always do quickly enough: while dragging, the size of
4922 the thumb might prevent the user from dragging the thumb all the
4923 way to the end. but Motif and some versions of Xaw3d don't allow
4924 updating the thumb size while dragging. Also, even if we can update
4925 its size, the update will often happen too late.
4926 If you don't believe it, check out revision 1.650 of xterm.c to see
4927 what hoops we were going through and the still poor behavior we got. */
4928 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4929 /* When the thumb is at the bottom, position == whole.
4930 So we need to increase `whole' to make space for the thumb. */
4931 whole += portion;
4934 if (whole <= 0)
4935 top = 0, shown = 1;
4936 else
4938 top = (float) position / whole;
4939 shown = (float) portion / whole;
4942 if (bar->dragging == -1)
4944 int size, value;
4946 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4947 is the scroll bar's maximum and MIN is the scroll bar's minimum
4948 value. */
4949 size = clip_to_bounds (1, shown * XM_SB_MAX, XM_SB_MAX);
4951 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4952 value = top * XM_SB_MAX;
4953 value = min (value, XM_SB_MAX - size);
4955 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4957 #else /* !USE_MOTIF i.e. use Xaw */
4959 if (whole == 0)
4960 top = 0, shown = 1;
4961 else
4963 top = (float) position / whole;
4964 shown = (float) portion / whole;
4968 float old_top, old_shown;
4969 Dimension height;
4970 XtVaGetValues (widget,
4971 XtNtopOfThumb, &old_top,
4972 XtNshown, &old_shown,
4973 XtNheight, &height,
4974 NULL);
4976 /* Massage the top+shown values. */
4977 if (bar->dragging == -1 || bar->last_seen_part == scroll_bar_down_arrow)
4978 top = max (0, min (1, top));
4979 else
4980 top = old_top;
4981 #if ! defined (HAVE_XAW3D)
4982 /* With Xaw, 'top' values too closer to 1.0 may
4983 cause the thumb to disappear. Fix that. */
4984 top = min (top, 0.99f);
4985 #endif
4986 /* Keep two pixels available for moving the thumb down. */
4987 shown = max (0, min (1 - top - (2.0f / height), shown));
4988 #if ! defined (HAVE_XAW3D)
4989 /* Likewise with too small 'shown'. */
4990 shown = max (shown, 0.01f);
4991 #endif
4993 /* If the call to XawScrollbarSetThumb below doesn't seem to
4994 work, check that 'NARROWPROTO' is defined in src/config.h.
4995 If this is not so, most likely you need to fix configure. */
4996 if (top != old_top || shown != old_shown)
4998 if (bar->dragging == -1)
4999 XawScrollbarSetThumb (widget, top, shown);
5000 else
5002 /* Try to make the scrolling a tad smoother. */
5003 if (!xaw3d_pick_top)
5004 shown = min (shown, old_shown);
5006 XawScrollbarSetThumb (widget, top, shown);
5010 #endif /* !USE_MOTIF */
5012 unblock_input ();
5014 #endif /* not USE_GTK */
5016 #endif /* USE_TOOLKIT_SCROLL_BARS */
5020 /************************************************************************
5021 Scroll bars, general
5022 ************************************************************************/
5024 /* Create a scroll bar and return the scroll bar vector for it. W is
5025 the Emacs window on which to create the scroll bar. TOP, LEFT,
5026 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
5027 scroll bar. */
5029 static struct scroll_bar *
5030 x_scroll_bar_create (struct window *w, int top, int left, int width, int height)
5032 struct frame *f = XFRAME (w->frame);
5033 struct scroll_bar *bar
5034 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
5035 Lisp_Object barobj;
5037 block_input ();
5039 #ifdef USE_TOOLKIT_SCROLL_BARS
5040 x_create_toolkit_scroll_bar (f, bar);
5041 #else /* not USE_TOOLKIT_SCROLL_BARS */
5043 XSetWindowAttributes a;
5044 unsigned long mask;
5045 Window window;
5047 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
5048 if (a.background_pixel == -1)
5049 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5051 a.event_mask = (ButtonPressMask | ButtonReleaseMask
5052 | ButtonMotionMask | PointerMotionHintMask
5053 | ExposureMask);
5054 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
5056 mask = (CWBackPixel | CWEventMask | CWCursor);
5058 /* Clear the area of W that will serve as a scroll bar. This is
5059 for the case that a window has been split horizontally. In
5060 this case, no clear_frame is generated to reduce flickering. */
5061 if (width > 0 && height > 0)
5062 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5063 left, top, width, window_box_height (w));
5065 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5066 /* Position and size of scroll bar. */
5067 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5068 top,
5069 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5070 height,
5071 /* Border width, depth, class, and visual. */
5073 CopyFromParent,
5074 CopyFromParent,
5075 CopyFromParent,
5076 /* Attributes. */
5077 mask, &a);
5078 bar->x_window = window;
5080 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5082 XSETWINDOW (bar->window, w);
5083 bar->top = top;
5084 bar->left = left;
5085 bar->width = width;
5086 bar->height = height;
5087 bar->start = 0;
5088 bar->end = 0;
5089 bar->dragging = -1;
5090 bar->fringe_extended_p = 0;
5091 #if defined (USE_TOOLKIT_SCROLL_BARS) && defined (USE_LUCID)
5092 bar->last_seen_part = scroll_bar_nowhere;
5093 #endif
5095 /* Add bar to its frame's list of scroll bars. */
5096 bar->next = FRAME_SCROLL_BARS (f);
5097 bar->prev = Qnil;
5098 XSETVECTOR (barobj, bar);
5099 fset_scroll_bars (f, barobj);
5100 if (!NILP (bar->next))
5101 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5103 /* Map the window/widget. */
5104 #ifdef USE_TOOLKIT_SCROLL_BARS
5106 #ifdef USE_GTK
5107 xg_update_scrollbar_pos (f,
5108 bar->x_window,
5109 top,
5110 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5111 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5112 max (height, 1));
5113 #else /* not USE_GTK */
5114 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5115 XtConfigureWidget (scroll_bar,
5116 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5117 top,
5118 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5119 max (height, 1), 0);
5120 XtMapWidget (scroll_bar);
5121 #endif /* not USE_GTK */
5123 #else /* not USE_TOOLKIT_SCROLL_BARS */
5124 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
5125 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5127 unblock_input ();
5128 return bar;
5132 #ifndef USE_TOOLKIT_SCROLL_BARS
5134 /* Draw BAR's handle in the proper position.
5136 If the handle is already drawn from START to END, don't bother
5137 redrawing it, unless REBUILD is non-zero; in that case, always
5138 redraw it. (REBUILD is handy for drawing the handle after expose
5139 events.)
5141 Normally, we want to constrain the start and end of the handle to
5142 fit inside its rectangle, but if the user is dragging the scroll
5143 bar handle, we want to let them drag it down all the way, so that
5144 the bar's top is as far down as it goes; otherwise, there's no way
5145 to move to the very end of the buffer. */
5147 static void
5148 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end, int rebuild)
5150 bool dragging = bar->dragging != -1;
5151 Window w = bar->x_window;
5152 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5153 GC gc = f->output_data.x->normal_gc;
5155 /* If the display is already accurate, do nothing. */
5156 if (! rebuild
5157 && start == bar->start
5158 && end == bar->end)
5159 return;
5161 block_input ();
5164 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5165 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5166 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5168 /* Make sure the values are reasonable, and try to preserve
5169 the distance between start and end. */
5171 int length = end - start;
5173 if (start < 0)
5174 start = 0;
5175 else if (start > top_range)
5176 start = top_range;
5177 end = start + length;
5179 if (end < start)
5180 end = start;
5181 else if (end > top_range && ! dragging)
5182 end = top_range;
5185 /* Store the adjusted setting in the scroll bar. */
5186 bar->start = start;
5187 bar->end = end;
5189 /* Clip the end position, just for display. */
5190 if (end > top_range)
5191 end = top_range;
5193 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5194 below top positions, to make sure the handle is always at least
5195 that many pixels tall. */
5196 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5198 /* Draw the empty space above the handle. Note that we can't clear
5199 zero-height areas; that means "clear to end of window." */
5200 if (start > 0)
5201 x_clear_area (FRAME_X_DISPLAY (f), w,
5202 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5203 VERTICAL_SCROLL_BAR_TOP_BORDER,
5204 inside_width, start);
5206 /* Change to proper foreground color if one is specified. */
5207 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5208 XSetForeground (FRAME_X_DISPLAY (f), gc,
5209 f->output_data.x->scroll_bar_foreground_pixel);
5211 /* Draw the handle itself. */
5212 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5213 /* x, y, width, height */
5214 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5215 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5216 inside_width, end - start);
5218 /* Restore the foreground color of the GC if we changed it above. */
5219 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5220 XSetForeground (FRAME_X_DISPLAY (f), gc,
5221 FRAME_FOREGROUND_PIXEL (f));
5223 /* Draw the empty space below the handle. Note that we can't
5224 clear zero-height areas; that means "clear to end of window." */
5225 if (end < inside_height)
5226 x_clear_area (FRAME_X_DISPLAY (f), w,
5227 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5228 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5229 inside_width, inside_height - end);
5232 unblock_input ();
5235 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5237 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5238 nil. */
5240 static void
5241 x_scroll_bar_remove (struct scroll_bar *bar)
5243 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5244 block_input ();
5246 #ifdef USE_TOOLKIT_SCROLL_BARS
5247 #ifdef USE_GTK
5248 xg_remove_scroll_bar (f, bar->x_window);
5249 #else /* not USE_GTK */
5250 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5251 #endif /* not USE_GTK */
5252 #else
5253 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5254 #endif
5256 /* Dissociate this scroll bar from its window. */
5257 wset_vertical_scroll_bar (XWINDOW (bar->window), Qnil);
5259 unblock_input ();
5263 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5264 that we are displaying PORTION characters out of a total of WHOLE
5265 characters, starting at POSITION. If WINDOW has no scroll bar,
5266 create one. */
5268 static void
5269 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
5271 struct frame *f = XFRAME (w->frame);
5272 Lisp_Object barobj;
5273 struct scroll_bar *bar;
5274 int top, height, left, sb_left, width, sb_width;
5275 int window_y, window_height;
5276 #ifdef USE_TOOLKIT_SCROLL_BARS
5277 bool fringe_extended_p;
5278 #endif
5280 /* Get window dimensions. */
5281 window_box (w, ANY_AREA, 0, &window_y, 0, &window_height);
5282 top = window_y;
5283 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5284 height = window_height;
5286 /* Compute the left edge of the scroll bar area. */
5287 left = WINDOW_SCROLL_BAR_AREA_X (w);
5289 /* Compute the width of the scroll bar which might be less than
5290 the width of the area reserved for the scroll bar. */
5291 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5292 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5293 else
5294 sb_width = width;
5296 /* Compute the left edge of the scroll bar. */
5297 #ifdef USE_TOOLKIT_SCROLL_BARS
5298 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5299 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5300 else
5301 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5302 #else
5303 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5304 sb_left = left + width - sb_width;
5305 else
5306 sb_left = left;
5307 #endif
5309 #ifdef USE_TOOLKIT_SCROLL_BARS
5310 fringe_extended_p = WINDOW_FRINGE_EXTENDED_P (w);
5311 #endif
5313 /* Does the scroll bar exist yet? */
5314 if (NILP (w->vertical_scroll_bar))
5316 if (width > 0 && height > 0)
5318 block_input ();
5319 #ifdef USE_TOOLKIT_SCROLL_BARS
5320 if (fringe_extended_p)
5321 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5322 sb_left, top, sb_width, height);
5323 else
5324 #endif
5325 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5326 left, top, width, height);
5327 unblock_input ();
5330 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5332 else
5334 /* It may just need to be moved and resized. */
5335 unsigned int mask = 0;
5337 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5339 block_input ();
5341 if (sb_left != bar->left)
5342 mask |= CWX;
5343 if (top != bar->top)
5344 mask |= CWY;
5345 if (sb_width != bar->width)
5346 mask |= CWWidth;
5347 if (height != bar->height)
5348 mask |= CWHeight;
5350 #ifdef USE_TOOLKIT_SCROLL_BARS
5352 /* Move/size the scroll bar widget. */
5353 if (mask || bar->fringe_extended_p != fringe_extended_p)
5355 /* Since toolkit scroll bars are smaller than the space reserved
5356 for them on the frame, we have to clear "under" them. */
5357 if (width > 0 && height > 0)
5359 if (fringe_extended_p)
5360 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5361 sb_left, top, sb_width, height);
5362 else
5363 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5364 left, top, width, height);
5366 #ifdef USE_GTK
5367 xg_update_scrollbar_pos (f,
5368 bar->x_window,
5369 top,
5370 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5371 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5372 max (height, 1));
5373 #else /* not USE_GTK */
5374 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5375 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5376 top,
5377 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5378 max (height, 1), 0);
5379 #endif /* not USE_GTK */
5381 #else /* not USE_TOOLKIT_SCROLL_BARS */
5383 /* Clear areas not covered by the scroll bar because of
5384 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5385 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5387 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5388 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM, height);
5389 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5390 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5391 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM, height);
5394 /* Clear areas not covered by the scroll bar because it's not as
5395 wide as the area reserved for it. This makes sure a
5396 previous mode line display is cleared after C-x 2 C-x 1, for
5397 example. */
5399 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5400 int rest = area_width - sb_width;
5401 if (rest > 0 && height > 0)
5403 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5404 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5405 left + area_width - rest, top, rest, height);
5406 else
5407 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5408 left, top, rest, height);
5412 /* Move/size the scroll bar window. */
5413 if (mask)
5415 XWindowChanges wc;
5417 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5418 wc.y = top;
5419 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5420 wc.height = height;
5421 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5422 mask, &wc);
5425 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5427 /* Remember new settings. */
5428 bar->left = sb_left;
5429 bar->top = top;
5430 bar->width = sb_width;
5431 bar->height = height;
5433 unblock_input ();
5436 #ifdef USE_TOOLKIT_SCROLL_BARS
5437 bar->fringe_extended_p = fringe_extended_p;
5439 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5440 #else /* not USE_TOOLKIT_SCROLL_BARS */
5441 /* Set the scroll bar's current state, unless we're currently being
5442 dragged. */
5443 if (bar->dragging == -1)
5445 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5447 if (whole == 0)
5448 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5449 else
5451 int start = ((double) position * top_range) / whole;
5452 int end = ((double) (position + portion) * top_range) / whole;
5453 x_scroll_bar_set_handle (bar, start, end, 0);
5456 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5458 XSETVECTOR (barobj, bar);
5459 wset_vertical_scroll_bar (w, barobj);
5463 /* The following three hooks are used when we're doing a thorough
5464 redisplay of the frame. We don't explicitly know which scroll bars
5465 are going to be deleted, because keeping track of when windows go
5466 away is a real pain - "Can you say set-window-configuration, boys
5467 and girls?" Instead, we just assert at the beginning of redisplay
5468 that *all* scroll bars are to be removed, and then save a scroll bar
5469 from the fiery pit when we actually redisplay its window. */
5471 /* Arrange for all scroll bars on FRAME to be removed at the next call
5472 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5473 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5475 static void
5476 XTcondemn_scroll_bars (struct frame *frame)
5478 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5479 while (! NILP (FRAME_SCROLL_BARS (frame)))
5481 Lisp_Object bar;
5482 bar = FRAME_SCROLL_BARS (frame);
5483 fset_scroll_bars (frame, XSCROLL_BAR (bar)->next);
5484 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5485 XSCROLL_BAR (bar)->prev = Qnil;
5486 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5487 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5488 fset_condemned_scroll_bars (frame, bar);
5493 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5494 Note that WINDOW isn't necessarily condemned at all. */
5496 static void
5497 XTredeem_scroll_bar (struct window *window)
5499 struct scroll_bar *bar;
5500 struct frame *f;
5501 Lisp_Object barobj;
5503 /* We can't redeem this window's scroll bar if it doesn't have one. */
5504 if (NILP (window->vertical_scroll_bar))
5505 emacs_abort ();
5507 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5509 /* Unlink it from the condemned list. */
5510 f = XFRAME (WINDOW_FRAME (window));
5511 if (NILP (bar->prev))
5513 /* If the prev pointer is nil, it must be the first in one of
5514 the lists. */
5515 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5516 /* It's not condemned. Everything's fine. */
5517 return;
5518 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5519 window->vertical_scroll_bar))
5520 fset_condemned_scroll_bars (f, bar->next);
5521 else
5522 /* If its prev pointer is nil, it must be at the front of
5523 one or the other! */
5524 emacs_abort ();
5526 else
5527 XSCROLL_BAR (bar->prev)->next = bar->next;
5529 if (! NILP (bar->next))
5530 XSCROLL_BAR (bar->next)->prev = bar->prev;
5532 bar->next = FRAME_SCROLL_BARS (f);
5533 bar->prev = Qnil;
5534 XSETVECTOR (barobj, bar);
5535 fset_scroll_bars (f, barobj);
5536 if (! NILP (bar->next))
5537 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5540 /* Remove all scroll bars on FRAME that haven't been saved since the
5541 last call to `*condemn_scroll_bars_hook'. */
5543 static void
5544 XTjudge_scroll_bars (struct frame *f)
5546 Lisp_Object bar, next;
5548 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5550 /* Clear out the condemned list now so we won't try to process any
5551 more events on the hapless scroll bars. */
5552 fset_condemned_scroll_bars (f, Qnil);
5554 for (; ! NILP (bar); bar = next)
5556 struct scroll_bar *b = XSCROLL_BAR (bar);
5558 x_scroll_bar_remove (b);
5560 next = b->next;
5561 b->next = b->prev = Qnil;
5564 /* Now there should be no references to the condemned scroll bars,
5565 and they should get garbage-collected. */
5569 #ifndef USE_TOOLKIT_SCROLL_BARS
5570 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5571 is a no-op when using toolkit scroll bars.
5573 This may be called from a signal handler, so we have to ignore GC
5574 mark bits. */
5576 static void
5577 x_scroll_bar_expose (struct scroll_bar *bar, XEvent *event)
5579 Window w = bar->x_window;
5580 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5581 GC gc = f->output_data.x->normal_gc;
5582 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5584 block_input ();
5586 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5588 /* Switch to scroll bar foreground color. */
5589 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5590 XSetForeground (FRAME_X_DISPLAY (f), gc,
5591 f->output_data.x->scroll_bar_foreground_pixel);
5593 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5594 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5596 /* x, y, width, height */
5597 0, 0,
5598 bar->width - 1 - width_trim - width_trim,
5599 bar->height - 1);
5601 /* Restore the foreground color of the GC if we changed it above. */
5602 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5603 XSetForeground (FRAME_X_DISPLAY (f), gc,
5604 FRAME_FOREGROUND_PIXEL (f));
5606 unblock_input ();
5609 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5611 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5612 is set to something other than NO_EVENT, it is enqueued.
5614 This may be called from a signal handler, so we have to ignore GC
5615 mark bits. */
5618 static void
5619 x_scroll_bar_handle_click (struct scroll_bar *bar, XEvent *event, struct input_event *emacs_event)
5621 if (! WINDOWP (bar->window))
5622 emacs_abort ();
5624 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5625 emacs_event->code = event->xbutton.button - Button1;
5626 emacs_event->modifiers
5627 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5628 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5629 event->xbutton.state)
5630 | (event->type == ButtonRelease
5631 ? up_modifier
5632 : down_modifier));
5633 emacs_event->frame_or_window = bar->window;
5634 emacs_event->arg = Qnil;
5635 emacs_event->timestamp = event->xbutton.time;
5637 int top_range
5638 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5639 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5641 if (y < 0) y = 0;
5642 if (y > top_range) y = top_range;
5644 if (y < bar->start)
5645 emacs_event->part = scroll_bar_above_handle;
5646 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5647 emacs_event->part = scroll_bar_handle;
5648 else
5649 emacs_event->part = scroll_bar_below_handle;
5651 #ifndef USE_TOOLKIT_SCROLL_BARS
5652 /* If the user has released the handle, set it to its final position. */
5653 if (event->type == ButtonRelease && bar->dragging != -1)
5655 int new_start = y - bar->dragging;
5656 int new_end = new_start + bar->end - bar->start;
5658 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5659 bar->dragging = -1;
5661 #endif
5663 XSETINT (emacs_event->x, y);
5664 XSETINT (emacs_event->y, top_range);
5668 #ifndef USE_TOOLKIT_SCROLL_BARS
5670 /* Handle some mouse motion while someone is dragging the scroll bar.
5672 This may be called from a signal handler, so we have to ignore GC
5673 mark bits. */
5675 static void
5676 x_scroll_bar_note_movement (struct scroll_bar *bar, XMotionEvent *event)
5678 struct frame *f = XFRAME (XWINDOW (bar->window)->frame);
5680 last_mouse_movement_time = event->time;
5682 f->mouse_moved = 1;
5683 XSETVECTOR (last_mouse_scroll_bar, bar);
5685 /* If we're dragging the bar, display it. */
5686 if (bar->dragging != -1)
5688 /* Where should the handle be now? */
5689 int new_start = event->y - bar->dragging;
5691 if (new_start != bar->start)
5693 int new_end = new_start + bar->end - bar->start;
5695 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5700 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5702 /* Return information to the user about the current position of the mouse
5703 on the scroll bar. */
5705 static void
5706 x_scroll_bar_report_motion (struct frame **fp, Lisp_Object *bar_window,
5707 enum scroll_bar_part *part, Lisp_Object *x,
5708 Lisp_Object *y, Time *timestamp)
5710 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5711 Window w = bar->x_window;
5712 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5713 int win_x, win_y;
5714 Window dummy_window;
5715 int dummy_coord;
5716 unsigned int dummy_mask;
5718 block_input ();
5720 /* Get the mouse's position relative to the scroll bar window, and
5721 report that. */
5722 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5724 /* Root, child, root x and root y. */
5725 &dummy_window, &dummy_window,
5726 &dummy_coord, &dummy_coord,
5728 /* Position relative to scroll bar. */
5729 &win_x, &win_y,
5731 /* Mouse buttons and modifier keys. */
5732 &dummy_mask))
5734 else
5736 int top_range
5737 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5739 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5741 if (bar->dragging != -1)
5742 win_y -= bar->dragging;
5744 if (win_y < 0)
5745 win_y = 0;
5746 if (win_y > top_range)
5747 win_y = top_range;
5749 *fp = f;
5750 *bar_window = bar->window;
5752 if (bar->dragging != -1)
5753 *part = scroll_bar_handle;
5754 else if (win_y < bar->start)
5755 *part = scroll_bar_above_handle;
5756 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5757 *part = scroll_bar_handle;
5758 else
5759 *part = scroll_bar_below_handle;
5761 XSETINT (*x, win_y);
5762 XSETINT (*y, top_range);
5764 f->mouse_moved = 0;
5765 last_mouse_scroll_bar = Qnil;
5768 *timestamp = last_mouse_movement_time;
5770 unblock_input ();
5774 /* The screen has been cleared so we may have changed foreground or
5775 background colors, and the scroll bars may need to be redrawn.
5776 Clear out the scroll bars, and ask for expose events, so we can
5777 redraw them. */
5779 static void
5780 x_scroll_bar_clear (struct frame *f)
5782 #ifndef USE_TOOLKIT_SCROLL_BARS
5783 Lisp_Object bar;
5785 /* We can have scroll bars even if this is 0,
5786 if we just turned off scroll bar mode.
5787 But in that case we should not clear them. */
5788 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5789 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5790 bar = XSCROLL_BAR (bar)->next)
5791 XClearArea (FRAME_X_DISPLAY (f),
5792 XSCROLL_BAR (bar)->x_window,
5793 0, 0, 0, 0, True);
5794 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5797 #ifdef ENABLE_CHECKING
5799 /* Record the last 100 characters stored
5800 to help debug the loss-of-chars-during-GC problem. */
5802 static int temp_index;
5803 static short temp_buffer[100];
5805 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5806 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5807 temp_index = 0; \
5808 temp_buffer[temp_index++] = (keysym)
5810 #else /* not ENABLE_CHECKING */
5812 #define STORE_KEYSYM_FOR_DEBUG(keysym) ((void)0)
5814 #endif /* ENABLE_CHECKING */
5816 /* Set this to nonzero to fake an "X I/O error"
5817 on a particular display. */
5819 static struct x_display_info *XTread_socket_fake_io_error;
5821 /* When we find no input here, we occasionally do a no-op command
5822 to verify that the X server is still running and we can still talk with it.
5823 We try all the open displays, one by one.
5824 This variable is used for cycling thru the displays. */
5826 static struct x_display_info *next_noop_dpyinfo;
5828 #if defined USE_X_TOOLKIT || defined USE_GTK
5829 #define SET_SAVED_BUTTON_EVENT \
5830 do \
5832 if (f->output_data.x->saved_menu_event == 0) \
5833 f->output_data.x->saved_menu_event = \
5834 xmalloc (sizeof (XEvent)); \
5835 *f->output_data.x->saved_menu_event = event; \
5836 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5837 XSETFRAME (inev.ie.frame_or_window, f); \
5839 while (0)
5840 #endif
5842 enum
5844 X_EVENT_NORMAL,
5845 X_EVENT_GOTO_OUT,
5846 X_EVENT_DROP
5849 /* Filter events for the current X input method.
5850 DPYINFO is the display this event is for.
5851 EVENT is the X event to filter.
5853 Returns non-zero if the event was filtered, caller shall not process
5854 this event further.
5855 Returns zero if event is wasn't filtered. */
5857 #ifdef HAVE_X_I18N
5858 static int
5859 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
5861 /* XFilterEvent returns non-zero if the input method has
5862 consumed the event. We pass the frame's X window to
5863 XFilterEvent because that's the one for which the IC
5864 was created. */
5866 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5867 event->xclient.window);
5869 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5871 #endif
5873 #ifdef USE_GTK
5874 static int current_count;
5875 static int current_finish;
5876 static struct input_event *current_hold_quit;
5878 /* This is the filter function invoked by the GTK event loop.
5879 It is invoked before the XEvent is translated to a GdkEvent,
5880 so we have a chance to act on the event before GTK. */
5881 static GdkFilterReturn
5882 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
5884 XEvent *xev = (XEvent *) gxev;
5886 block_input ();
5887 if (current_count >= 0)
5889 struct x_display_info *dpyinfo;
5891 dpyinfo = x_display_info_for_display (xev->xany.display);
5893 #ifdef HAVE_X_I18N
5894 /* Filter events for the current X input method.
5895 GTK calls XFilterEvent but not for key press and release,
5896 so we do it here. */
5897 if ((xev->type == KeyPress || xev->type == KeyRelease)
5898 && dpyinfo
5899 && x_filter_event (dpyinfo, xev))
5901 unblock_input ();
5902 return GDK_FILTER_REMOVE;
5904 #endif
5906 if (! dpyinfo)
5907 current_finish = X_EVENT_NORMAL;
5908 else
5909 current_count +=
5910 handle_one_xevent (dpyinfo, xev, &current_finish,
5911 current_hold_quit);
5913 else
5914 current_finish = x_dispatch_event (xev, xev->xany.display);
5916 unblock_input ();
5918 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5919 return GDK_FILTER_REMOVE;
5921 return GDK_FILTER_CONTINUE;
5923 #endif /* USE_GTK */
5926 static void xembed_send_message (struct frame *f, Time,
5927 enum xembed_message,
5928 long detail, long data1, long data2);
5930 /* Handles the XEvent EVENT on display DPYINFO.
5932 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5933 *FINISH is zero if caller should continue reading events.
5934 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5936 We return the number of characters stored into the buffer. */
5938 static int
5939 handle_one_xevent (struct x_display_info *dpyinfo, XEvent *eventptr,
5940 int *finish, struct input_event *hold_quit)
5942 union {
5943 struct input_event ie;
5944 struct selection_input_event sie;
5945 } inev;
5946 int count = 0;
5947 int do_help = 0;
5948 ptrdiff_t nbytes = 0;
5949 struct frame *f = NULL;
5950 struct coding_system coding;
5951 XEvent event = *eventptr;
5952 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
5953 /* This holds the state XLookupString needs to implement dead keys
5954 and other tricks known as "compose processing". _X Window System_
5955 says that a portable program can't use this, but Stephen Gildea assures
5956 me that letting the compiler initialize it to zeros will work okay. */
5957 static XComposeStatus compose_status;
5959 USE_SAFE_ALLOCA;
5961 *finish = X_EVENT_NORMAL;
5963 EVENT_INIT (inev.ie);
5964 inev.ie.kind = NO_EVENT;
5965 inev.ie.arg = Qnil;
5967 if (pending_event_wait.eventtype == event.type)
5968 pending_event_wait.eventtype = 0; /* Indicates we got it. */
5970 switch (event.type)
5972 case ClientMessage:
5974 if (event.xclient.message_type
5975 == dpyinfo->Xatom_wm_protocols
5976 && event.xclient.format == 32)
5978 if (event.xclient.data.l[0]
5979 == dpyinfo->Xatom_wm_take_focus)
5981 /* Use x_any_window_to_frame because this
5982 could be the shell widget window
5983 if the frame has no title bar. */
5984 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5985 #ifdef HAVE_X_I18N
5986 /* Not quite sure this is needed -pd */
5987 if (f && FRAME_XIC (f))
5988 XSetICFocus (FRAME_XIC (f));
5989 #endif
5990 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5991 instructs the WM to set the input focus automatically for
5992 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5993 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5994 it has set the focus. So, XSetInputFocus below is not
5995 needed.
5997 The call to XSetInputFocus below has also caused trouble. In
5998 cases where the XSetInputFocus done by the WM and the one
5999 below are temporally close (on a fast machine), the call
6000 below can generate additional FocusIn events which confuse
6001 Emacs. */
6003 /* Since we set WM_TAKE_FOCUS, we must call
6004 XSetInputFocus explicitly. But not if f is null,
6005 since that might be an event for a deleted frame. */
6006 if (f)
6008 Display *d = event.xclient.display;
6009 /* Catch and ignore errors, in case window has been
6010 iconified by a window manager such as GWM. */
6011 x_catch_errors (d);
6012 XSetInputFocus (d, event.xclient.window,
6013 /* The ICCCM says this is
6014 the only valid choice. */
6015 RevertToParent,
6016 event.xclient.data.l[1]);
6017 /* This is needed to detect the error
6018 if there is an error. */
6019 XSync (d, False);
6020 x_uncatch_errors ();
6022 /* Not certain about handling scroll bars here */
6023 #endif /* 0 */
6024 goto done;
6027 if (event.xclient.data.l[0]
6028 == dpyinfo->Xatom_wm_save_yourself)
6030 /* Save state modify the WM_COMMAND property to
6031 something which can reinstate us. This notifies
6032 the session manager, who's looking for such a
6033 PropertyNotify. Can restart processing when
6034 a keyboard or mouse event arrives. */
6035 /* If we have a session manager, don't set this.
6036 KDE will then start two Emacsen, one for the
6037 session manager and one for this. */
6038 #ifdef HAVE_X_SM
6039 if (! x_session_have_connection ())
6040 #endif
6042 f = x_top_window_to_frame (dpyinfo,
6043 event.xclient.window);
6044 /* This is just so we only give real data once
6045 for a single Emacs process. */
6046 if (f == SELECTED_FRAME ())
6047 XSetCommand (FRAME_X_DISPLAY (f),
6048 event.xclient.window,
6049 initial_argv, initial_argc);
6050 else if (f)
6051 XSetCommand (FRAME_X_DISPLAY (f),
6052 event.xclient.window,
6053 0, 0);
6055 goto done;
6058 if (event.xclient.data.l[0]
6059 == dpyinfo->Xatom_wm_delete_window)
6061 f = x_any_window_to_frame (dpyinfo,
6062 event.xclient.window);
6063 if (!f)
6064 goto OTHER; /* May be a dialog that is to be removed */
6066 inev.ie.kind = DELETE_WINDOW_EVENT;
6067 XSETFRAME (inev.ie.frame_or_window, f);
6068 goto done;
6071 goto done;
6074 if (event.xclient.message_type
6075 == dpyinfo->Xatom_wm_configure_denied)
6077 goto done;
6080 if (event.xclient.message_type
6081 == dpyinfo->Xatom_wm_window_moved)
6083 int new_x, new_y;
6084 f = x_window_to_frame (dpyinfo, event.xclient.window);
6086 new_x = event.xclient.data.s[0];
6087 new_y = event.xclient.data.s[1];
6089 if (f)
6091 f->left_pos = new_x;
6092 f->top_pos = new_y;
6094 goto done;
6097 #ifdef HACK_EDITRES
6098 if (event.xclient.message_type
6099 == dpyinfo->Xatom_editres)
6101 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6102 if (f)
6103 _XEditResCheckMessages (f->output_data.x->widget, NULL,
6104 &event, NULL);
6105 goto done;
6107 #endif /* HACK_EDITRES */
6109 if ((event.xclient.message_type
6110 == dpyinfo->Xatom_DONE)
6111 || (event.xclient.message_type
6112 == dpyinfo->Xatom_PAGE))
6114 /* Ghostview job completed. Kill it. We could
6115 reply with "Next" if we received "Page", but we
6116 currently never do because we are interested in
6117 images, only, which should have 1 page. */
6118 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
6119 f = x_window_to_frame (dpyinfo, event.xclient.window);
6120 if (!f)
6121 goto OTHER;
6122 x_kill_gs_process (pixmap, f);
6123 expose_frame (f, 0, 0, 0, 0);
6124 goto done;
6127 #ifdef USE_TOOLKIT_SCROLL_BARS
6128 /* Scroll bar callbacks send a ClientMessage from which
6129 we construct an input_event. */
6130 if (event.xclient.message_type
6131 == dpyinfo->Xatom_Scrollbar)
6133 x_scroll_bar_to_input_event (&event, &inev.ie);
6134 *finish = X_EVENT_GOTO_OUT;
6135 goto done;
6137 #endif /* USE_TOOLKIT_SCROLL_BARS */
6139 /* XEmbed messages from the embedder (if any). */
6140 if (event.xclient.message_type
6141 == dpyinfo->Xatom_XEMBED)
6143 enum xembed_message msg = event.xclient.data.l[1];
6144 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6145 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6147 *finish = X_EVENT_GOTO_OUT;
6148 goto done;
6151 xft_settings_event (dpyinfo, &event);
6153 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6154 if (!f)
6155 goto OTHER;
6156 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
6157 *finish = X_EVENT_DROP;
6159 break;
6161 case SelectionNotify:
6162 dpyinfo->last_user_time = event.xselection.time;
6163 #ifdef USE_X_TOOLKIT
6164 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6165 goto OTHER;
6166 #endif /* not USE_X_TOOLKIT */
6167 x_handle_selection_notify (&event.xselection);
6168 break;
6170 case SelectionClear: /* Someone has grabbed ownership. */
6171 dpyinfo->last_user_time = event.xselectionclear.time;
6172 #ifdef USE_X_TOOLKIT
6173 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6174 goto OTHER;
6175 #endif /* USE_X_TOOLKIT */
6177 XSelectionClearEvent *eventp = &(event.xselectionclear);
6179 inev.ie.kind = SELECTION_CLEAR_EVENT;
6180 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6181 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6182 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6184 break;
6186 case SelectionRequest: /* Someone wants our selection. */
6187 dpyinfo->last_user_time = event.xselectionrequest.time;
6188 #ifdef USE_X_TOOLKIT
6189 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6190 goto OTHER;
6191 #endif /* USE_X_TOOLKIT */
6193 XSelectionRequestEvent *eventp = &(event.xselectionrequest);
6195 inev.ie.kind = SELECTION_REQUEST_EVENT;
6196 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6197 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6198 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6199 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6200 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6201 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6203 break;
6205 case PropertyNotify:
6206 dpyinfo->last_user_time = event.xproperty.time;
6207 f = x_top_window_to_frame (dpyinfo, event.xproperty.window);
6208 if (f && event.xproperty.atom == dpyinfo->Xatom_net_wm_state)
6209 if (x_handle_net_wm_state (f, &event.xproperty)
6210 && FRAME_ICONIFIED_P (f)
6211 && f->output_data.x->net_wm_state_hidden_seen)
6213 /* Gnome shell does not iconify us when C-z is pressed.
6214 It hides the frame. So if our state says we aren't
6215 hidden anymore, treat it as deiconified. */
6216 SET_FRAME_VISIBLE (f, 1);
6217 SET_FRAME_ICONIFIED (f, 0);
6218 f->output_data.x->has_been_visible = 1;
6219 f->output_data.x->net_wm_state_hidden_seen = 0;
6220 inev.ie.kind = DEICONIFY_EVENT;
6221 XSETFRAME (inev.ie.frame_or_window, f);
6224 x_handle_property_notify (&event.xproperty);
6225 xft_settings_event (dpyinfo, &event);
6226 goto OTHER;
6228 case ReparentNotify:
6229 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6230 if (f)
6232 int x, y;
6233 f->output_data.x->parent_desc = event.xreparent.parent;
6234 x_real_positions (f, &x, &y);
6235 f->left_pos = x;
6236 f->top_pos = y;
6238 /* Perhaps reparented due to a WM restart. Reset this. */
6239 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6240 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6242 x_set_frame_alpha (f);
6244 goto OTHER;
6246 case Expose:
6247 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6248 if (f)
6250 #ifdef USE_GTK
6251 /* This seems to be needed for GTK 2.6. */
6252 x_clear_area (event.xexpose.display,
6253 event.xexpose.window,
6254 event.xexpose.x, event.xexpose.y,
6255 event.xexpose.width, event.xexpose.height);
6256 #endif
6257 if (!FRAME_VISIBLE_P (f))
6259 SET_FRAME_VISIBLE (f, 1);
6260 SET_FRAME_ICONIFIED (f, 0);
6261 f->output_data.x->has_been_visible = 1;
6262 SET_FRAME_GARBAGED (f);
6264 else
6265 expose_frame (f,
6266 event.xexpose.x, event.xexpose.y,
6267 event.xexpose.width, event.xexpose.height);
6269 else
6271 #ifndef USE_TOOLKIT_SCROLL_BARS
6272 struct scroll_bar *bar;
6273 #endif
6274 #if defined USE_LUCID
6275 /* Submenus of the Lucid menu bar aren't widgets
6276 themselves, so there's no way to dispatch events
6277 to them. Recognize this case separately. */
6279 Widget widget
6280 = x_window_to_menu_bar (event.xexpose.window);
6281 if (widget)
6282 xlwmenu_redisplay (widget);
6284 #endif /* USE_LUCID */
6286 #ifdef USE_TOOLKIT_SCROLL_BARS
6287 /* Dispatch event to the widget. */
6288 goto OTHER;
6289 #else /* not USE_TOOLKIT_SCROLL_BARS */
6290 bar = x_window_to_scroll_bar (event.xexpose.display,
6291 event.xexpose.window);
6293 if (bar)
6294 x_scroll_bar_expose (bar, &event);
6295 #ifdef USE_X_TOOLKIT
6296 else
6297 goto OTHER;
6298 #endif /* USE_X_TOOLKIT */
6299 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6301 break;
6303 case GraphicsExpose: /* This occurs when an XCopyArea's
6304 source area was obscured or not
6305 available. */
6306 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6307 if (f)
6309 expose_frame (f,
6310 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6311 event.xgraphicsexpose.width,
6312 event.xgraphicsexpose.height);
6314 #ifdef USE_X_TOOLKIT
6315 else
6316 goto OTHER;
6317 #endif /* USE_X_TOOLKIT */
6318 break;
6320 case NoExpose: /* This occurs when an XCopyArea's
6321 source area was completely
6322 available. */
6323 break;
6325 case UnmapNotify:
6326 /* Redo the mouse-highlight after the tooltip has gone. */
6327 if (event.xunmap.window == tip_window)
6329 tip_window = 0;
6330 redo_mouse_highlight ();
6333 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6334 if (f) /* F may no longer exist if
6335 the frame was deleted. */
6337 bool visible = FRAME_VISIBLE_P (f);
6338 /* While a frame is unmapped, display generation is
6339 disabled; you don't want to spend time updating a
6340 display that won't ever be seen. */
6341 SET_FRAME_VISIBLE (f, 0);
6342 /* We can't distinguish, from the event, whether the window
6343 has become iconified or invisible. So assume, if it
6344 was previously visible, than now it is iconified.
6345 But x_make_frame_invisible clears both
6346 the visible flag and the iconified flag;
6347 and that way, we know the window is not iconified now. */
6348 if (visible || FRAME_ICONIFIED_P (f))
6350 SET_FRAME_ICONIFIED (f, 1);
6351 inev.ie.kind = ICONIFY_EVENT;
6352 XSETFRAME (inev.ie.frame_or_window, f);
6355 goto OTHER;
6357 case MapNotify:
6358 if (event.xmap.window == tip_window)
6359 /* The tooltip has been drawn already. Avoid
6360 the SET_FRAME_GARBAGED below. */
6361 goto OTHER;
6363 /* We use x_top_window_to_frame because map events can
6364 come for sub-windows and they don't mean that the
6365 frame is visible. */
6366 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6367 if (f)
6369 bool iconified = FRAME_ICONIFIED_P (f);
6370 /* wait_reading_process_output will notice this and update
6371 the frame's display structures.
6372 If we where iconified, we should not set garbaged,
6373 because that stops redrawing on Expose events. This looks
6374 bad if we are called from a recursive event loop
6375 (x_dispatch_event), for example when a dialog is up. */
6376 if (!iconified)
6377 SET_FRAME_GARBAGED (f);
6379 /* Check if fullscreen was specified before we where mapped the
6380 first time, i.e. from the command line. */
6381 if (!f->output_data.x->has_been_visible)
6382 x_check_fullscreen (f);
6384 SET_FRAME_VISIBLE (f, 1);
6385 SET_FRAME_ICONIFIED (f, 0);
6386 f->output_data.x->has_been_visible = 1;
6388 if (iconified)
6390 inev.ie.kind = DEICONIFY_EVENT;
6391 XSETFRAME (inev.ie.frame_or_window, f);
6393 else if (! NILP (Vframe_list) && ! NILP (XCDR (Vframe_list)))
6394 /* Force a redisplay sooner or later to update the
6395 frame titles in case this is the second frame. */
6396 record_asynch_buffer_change ();
6398 #ifdef USE_GTK
6399 xg_frame_resized (f, -1, -1);
6400 #endif
6402 goto OTHER;
6404 case KeyPress:
6406 dpyinfo->last_user_time = event.xkey.time;
6407 ignore_next_mouse_click_timeout = 0;
6409 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6410 /* Dispatch KeyPress events when in menu. */
6411 if (popup_activated ())
6412 goto OTHER;
6413 #endif
6415 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6417 #if ! defined (USE_GTK)
6418 /* If mouse-highlight is an integer, input clears out
6419 mouse highlighting. */
6420 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6421 && (f == 0
6422 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window)))
6424 clear_mouse_face (hlinfo);
6425 hlinfo->mouse_face_hidden = 1;
6427 #endif
6429 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6430 if (f == 0)
6432 /* Scroll bars consume key events, but we want
6433 the keys to go to the scroll bar's frame. */
6434 Widget widget = XtWindowToWidget (dpyinfo->display,
6435 event.xkey.window);
6436 if (widget && XmIsScrollBar (widget))
6438 widget = XtParent (widget);
6439 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6442 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6444 if (f != 0)
6446 KeySym keysym, orig_keysym;
6447 /* al%imercury@uunet.uu.net says that making this 81
6448 instead of 80 fixed a bug whereby meta chars made
6449 his Emacs hang.
6451 It seems that some version of XmbLookupString has
6452 a bug of not returning XBufferOverflow in
6453 status_return even if the input is too long to
6454 fit in 81 bytes. So, we must prepare sufficient
6455 bytes for copy_buffer. 513 bytes (256 chars for
6456 two-byte character set) seems to be a fairly good
6457 approximation. -- 2000.8.10 handa@etl.go.jp */
6458 unsigned char copy_buffer[513];
6459 unsigned char *copy_bufptr = copy_buffer;
6460 int copy_bufsiz = sizeof (copy_buffer);
6461 int modifiers;
6462 Lisp_Object coding_system = Qlatin_1;
6463 Lisp_Object c;
6465 #ifdef USE_GTK
6466 /* Don't pass keys to GTK. A Tab will shift focus to the
6467 tool bar in GTK 2.4. Keys will still go to menus and
6468 dialogs because in that case popup_activated is TRUE
6469 (see above). */
6470 *finish = X_EVENT_DROP;
6471 #endif
6473 event.xkey.state
6474 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6475 extra_keyboard_modifiers);
6476 modifiers = event.xkey.state;
6478 /* This will have to go some day... */
6480 /* make_lispy_event turns chars into control chars.
6481 Don't do it here because XLookupString is too eager. */
6482 event.xkey.state &= ~ControlMask;
6483 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6484 | dpyinfo->super_mod_mask
6485 | dpyinfo->hyper_mod_mask
6486 | dpyinfo->alt_mod_mask);
6488 /* In case Meta is ComposeCharacter,
6489 clear its status. According to Markus Ehrnsperger
6490 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6491 this enables ComposeCharacter to work whether or
6492 not it is combined with Meta. */
6493 if (modifiers & dpyinfo->meta_mod_mask)
6494 memset (&compose_status, 0, sizeof (compose_status));
6496 #ifdef HAVE_X_I18N
6497 if (FRAME_XIC (f))
6499 Status status_return;
6501 coding_system = Vlocale_coding_system;
6502 nbytes = XmbLookupString (FRAME_XIC (f),
6503 &event.xkey, (char *) copy_bufptr,
6504 copy_bufsiz, &keysym,
6505 &status_return);
6506 if (status_return == XBufferOverflow)
6508 copy_bufsiz = nbytes + 1;
6509 copy_bufptr = alloca (copy_bufsiz);
6510 nbytes = XmbLookupString (FRAME_XIC (f),
6511 &event.xkey, (char *) copy_bufptr,
6512 copy_bufsiz, &keysym,
6513 &status_return);
6515 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6516 if (status_return == XLookupNone)
6517 break;
6518 else if (status_return == XLookupChars)
6520 keysym = NoSymbol;
6521 modifiers = 0;
6523 else if (status_return != XLookupKeySym
6524 && status_return != XLookupBoth)
6525 emacs_abort ();
6527 else
6528 nbytes = XLookupString (&event.xkey, (char *) copy_bufptr,
6529 copy_bufsiz, &keysym,
6530 &compose_status);
6531 #else
6532 nbytes = XLookupString (&event.xkey, (char *) copy_bufptr,
6533 copy_bufsiz, &keysym,
6534 &compose_status);
6535 #endif
6537 /* If not using XIM/XIC, and a compose sequence is in progress,
6538 we break here. Otherwise, chars_matched is always 0. */
6539 if (compose_status.chars_matched > 0 && nbytes == 0)
6540 break;
6542 memset (&compose_status, 0, sizeof (compose_status));
6543 orig_keysym = keysym;
6545 /* Common for all keysym input events. */
6546 XSETFRAME (inev.ie.frame_or_window, f);
6547 inev.ie.modifiers
6548 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6549 inev.ie.timestamp = event.xkey.time;
6551 /* First deal with keysyms which have defined
6552 translations to characters. */
6553 if (keysym >= 32 && keysym < 128)
6554 /* Avoid explicitly decoding each ASCII character. */
6556 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6557 inev.ie.code = keysym;
6558 goto done_keysym;
6561 /* Keysyms directly mapped to Unicode characters. */
6562 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6564 if (keysym < 0x01000080)
6565 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6566 else
6567 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6568 inev.ie.code = keysym & 0xFFFFFF;
6569 goto done_keysym;
6572 /* Now non-ASCII. */
6573 if (HASH_TABLE_P (Vx_keysym_table)
6574 && (c = Fgethash (make_number (keysym),
6575 Vx_keysym_table,
6576 Qnil),
6577 NATNUMP (c)))
6579 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6580 ? ASCII_KEYSTROKE_EVENT
6581 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6582 inev.ie.code = XFASTINT (c);
6583 goto done_keysym;
6586 /* Random non-modifier sorts of keysyms. */
6587 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6588 || keysym == XK_Delete
6589 #ifdef XK_ISO_Left_Tab
6590 || (keysym >= XK_ISO_Left_Tab
6591 && keysym <= XK_ISO_Enter)
6592 #endif
6593 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6594 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6595 #ifdef HPUX
6596 /* This recognizes the "extended function
6597 keys". It seems there's no cleaner way.
6598 Test IsModifierKey to avoid handling
6599 mode_switch incorrectly. */
6600 || (XK_Select <= keysym && keysym < XK_KP_Space)
6601 #endif
6602 #ifdef XK_dead_circumflex
6603 || orig_keysym == XK_dead_circumflex
6604 #endif
6605 #ifdef XK_dead_grave
6606 || orig_keysym == XK_dead_grave
6607 #endif
6608 #ifdef XK_dead_tilde
6609 || orig_keysym == XK_dead_tilde
6610 #endif
6611 #ifdef XK_dead_diaeresis
6612 || orig_keysym == XK_dead_diaeresis
6613 #endif
6614 #ifdef XK_dead_macron
6615 || orig_keysym == XK_dead_macron
6616 #endif
6617 #ifdef XK_dead_degree
6618 || orig_keysym == XK_dead_degree
6619 #endif
6620 #ifdef XK_dead_acute
6621 || orig_keysym == XK_dead_acute
6622 #endif
6623 #ifdef XK_dead_cedilla
6624 || orig_keysym == XK_dead_cedilla
6625 #endif
6626 #ifdef XK_dead_breve
6627 || orig_keysym == XK_dead_breve
6628 #endif
6629 #ifdef XK_dead_ogonek
6630 || orig_keysym == XK_dead_ogonek
6631 #endif
6632 #ifdef XK_dead_caron
6633 || orig_keysym == XK_dead_caron
6634 #endif
6635 #ifdef XK_dead_doubleacute
6636 || orig_keysym == XK_dead_doubleacute
6637 #endif
6638 #ifdef XK_dead_abovedot
6639 || orig_keysym == XK_dead_abovedot
6640 #endif
6641 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6642 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6643 /* Any "vendor-specific" key is ok. */
6644 || (orig_keysym & (1 << 28))
6645 || (keysym != NoSymbol && nbytes == 0))
6646 && ! (IsModifierKey (orig_keysym)
6647 /* The symbols from XK_ISO_Lock
6648 to XK_ISO_Last_Group_Lock
6649 don't have real modifiers but
6650 should be treated similarly to
6651 Mode_switch by Emacs. */
6652 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6653 || (XK_ISO_Lock <= orig_keysym
6654 && orig_keysym <= XK_ISO_Last_Group_Lock)
6655 #endif
6658 STORE_KEYSYM_FOR_DEBUG (keysym);
6659 /* make_lispy_event will convert this to a symbolic
6660 key. */
6661 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6662 inev.ie.code = keysym;
6663 goto done_keysym;
6666 { /* Raw bytes, not keysym. */
6667 ptrdiff_t i;
6668 int nchars, len;
6670 for (i = 0, nchars = 0; i < nbytes; i++)
6672 if (ASCII_BYTE_P (copy_bufptr[i]))
6673 nchars++;
6674 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6677 if (nchars < nbytes)
6679 /* Decode the input data. */
6681 /* The input should be decoded with `coding_system'
6682 which depends on which X*LookupString function
6683 we used just above and the locale. */
6684 setup_coding_system (coding_system, &coding);
6685 coding.src_multibyte = 0;
6686 coding.dst_multibyte = 1;
6687 /* The input is converted to events, thus we can't
6688 handle composition. Anyway, there's no XIM that
6689 gives us composition information. */
6690 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6692 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
6693 nbytes);
6694 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
6695 coding.mode |= CODING_MODE_LAST_BLOCK;
6696 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6697 nbytes = coding.produced;
6698 nchars = coding.produced_char;
6699 copy_bufptr = coding.destination;
6702 /* Convert the input data to a sequence of
6703 character events. */
6704 for (i = 0; i < nbytes; i += len)
6706 int ch;
6707 if (nchars == nbytes)
6708 ch = copy_bufptr[i], len = 1;
6709 else
6710 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
6711 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
6712 ? ASCII_KEYSTROKE_EVENT
6713 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6714 inev.ie.code = ch;
6715 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6718 count += nchars;
6720 inev.ie.kind = NO_EVENT; /* Already stored above. */
6722 if (keysym == NoSymbol)
6723 break;
6726 done_keysym:
6727 #ifdef HAVE_X_I18N
6728 /* Don't dispatch this event since XtDispatchEvent calls
6729 XFilterEvent, and two calls in a row may freeze the
6730 client. */
6731 break;
6732 #else
6733 goto OTHER;
6734 #endif
6736 case KeyRelease:
6737 dpyinfo->last_user_time = event.xkey.time;
6738 #ifdef HAVE_X_I18N
6739 /* Don't dispatch this event since XtDispatchEvent calls
6740 XFilterEvent, and two calls in a row may freeze the
6741 client. */
6742 break;
6743 #else
6744 goto OTHER;
6745 #endif
6747 case EnterNotify:
6748 dpyinfo->last_user_time = event.xcrossing.time;
6749 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6751 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6753 if (f && x_mouse_click_focus_ignore_position)
6754 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6756 /* EnterNotify counts as mouse movement,
6757 so update things that depend on mouse position. */
6758 if (f && !f->output_data.x->hourglass_p)
6759 note_mouse_movement (f, &event.xmotion);
6760 #ifdef USE_GTK
6761 /* We may get an EnterNotify on the buttons in the toolbar. In that
6762 case we moved out of any highlighted area and need to note this. */
6763 if (!f && last_mouse_glyph_frame)
6764 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6765 #endif
6766 goto OTHER;
6768 case FocusIn:
6769 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6770 goto OTHER;
6772 case LeaveNotify:
6773 dpyinfo->last_user_time = event.xcrossing.time;
6774 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6776 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6777 if (f)
6779 if (f == hlinfo->mouse_face_mouse_frame)
6781 /* If we move outside the frame, then we're
6782 certainly no longer on any text in the frame. */
6783 clear_mouse_face (hlinfo);
6784 hlinfo->mouse_face_mouse_frame = 0;
6787 /* Generate a nil HELP_EVENT to cancel a help-echo.
6788 Do it only if there's something to cancel.
6789 Otherwise, the startup message is cleared when
6790 the mouse leaves the frame. */
6791 if (any_help_event_p)
6792 do_help = -1;
6794 #ifdef USE_GTK
6795 /* See comment in EnterNotify above */
6796 else if (last_mouse_glyph_frame)
6797 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6798 #endif
6799 goto OTHER;
6801 case FocusOut:
6802 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6803 goto OTHER;
6805 case MotionNotify:
6807 dpyinfo->last_user_time = event.xmotion.time;
6808 previous_help_echo_string = help_echo_string;
6809 help_echo_string = Qnil;
6811 if (dpyinfo->grabbed && last_mouse_frame
6812 && FRAME_LIVE_P (last_mouse_frame))
6813 f = last_mouse_frame;
6814 else
6815 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6817 if (hlinfo->mouse_face_hidden)
6819 hlinfo->mouse_face_hidden = 0;
6820 clear_mouse_face (hlinfo);
6823 #ifdef USE_GTK
6824 if (f && xg_event_is_for_scrollbar (f, &event))
6825 f = 0;
6826 #endif
6827 if (f)
6830 /* Generate SELECT_WINDOW_EVENTs when needed.
6831 Don't let popup menus influence things (bug#1261). */
6832 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6834 static Lisp_Object last_mouse_window;
6835 Lisp_Object window = window_from_coordinates
6836 (f, event.xmotion.x, event.xmotion.y, 0, 0);
6838 /* Window will be selected only when it is not selected now and
6839 last mouse movement event was not in it. Minibuffer window
6840 will be selected only when it is active. */
6841 if (WINDOWP (window)
6842 && !EQ (window, last_mouse_window)
6843 && !EQ (window, selected_window)
6844 /* For click-to-focus window managers
6845 create event iff we don't leave the
6846 selected frame. */
6847 && (focus_follows_mouse
6848 || (EQ (XWINDOW (window)->frame,
6849 XWINDOW (selected_window)->frame))))
6851 inev.ie.kind = SELECT_WINDOW_EVENT;
6852 inev.ie.frame_or_window = window;
6854 /* Remember the last window where we saw the mouse. */
6855 last_mouse_window = window;
6857 if (!note_mouse_movement (f, &event.xmotion))
6858 help_echo_string = previous_help_echo_string;
6860 else
6862 #ifndef USE_TOOLKIT_SCROLL_BARS
6863 struct scroll_bar *bar
6864 = x_window_to_scroll_bar (event.xmotion.display,
6865 event.xmotion.window);
6867 if (bar)
6868 x_scroll_bar_note_movement (bar, &event.xmotion);
6869 #endif /* USE_TOOLKIT_SCROLL_BARS */
6871 /* If we move outside the frame, then we're
6872 certainly no longer on any text in the frame. */
6873 clear_mouse_face (hlinfo);
6876 /* If the contents of the global variable help_echo_string
6877 has changed, generate a HELP_EVENT. */
6878 if (!NILP (help_echo_string)
6879 || !NILP (previous_help_echo_string))
6880 do_help = 1;
6881 goto OTHER;
6884 case ConfigureNotify:
6885 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6886 #ifdef USE_GTK
6887 if (!f
6888 && (f = x_any_window_to_frame (dpyinfo, event.xconfigure.window))
6889 && event.xconfigure.window == FRAME_X_WINDOW (f))
6891 xg_frame_resized (f, event.xconfigure.width,
6892 event.xconfigure.height);
6893 f = 0;
6895 #endif
6896 if (f)
6898 #ifndef USE_X_TOOLKIT
6899 #ifndef USE_GTK
6900 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6901 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6903 /* In the toolkit version, change_frame_size
6904 is called by the code that handles resizing
6905 of the EmacsFrame widget. */
6907 /* Even if the number of character rows and columns has
6908 not changed, the font size may have changed, so we need
6909 to check the pixel dimensions as well. */
6910 if (columns != FRAME_COLS (f)
6911 || rows != FRAME_LINES (f)
6912 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6913 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6915 change_frame_size (f, rows, columns, 0, 1, 0);
6916 SET_FRAME_GARBAGED (f);
6917 cancel_mouse_face (f);
6920 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6921 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6922 #endif /* not USE_GTK */
6923 #endif
6925 #ifdef USE_GTK
6926 /* GTK creates windows but doesn't map them.
6927 Only get real positions when mapped. */
6928 if (FRAME_GTK_OUTER_WIDGET (f)
6929 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
6930 #endif
6932 x_real_positions (f, &f->left_pos, &f->top_pos);
6935 #ifdef HAVE_X_I18N
6936 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6937 xic_set_statusarea (f);
6938 #endif
6941 goto OTHER;
6943 case ButtonRelease:
6944 case ButtonPress:
6946 /* If we decide we want to generate an event to be seen
6947 by the rest of Emacs, we put it here. */
6948 bool tool_bar_p = 0;
6950 memset (&compose_status, 0, sizeof (compose_status));
6951 last_mouse_glyph_frame = 0;
6952 dpyinfo->last_user_time = event.xbutton.time;
6954 if (dpyinfo->grabbed
6955 && last_mouse_frame
6956 && FRAME_LIVE_P (last_mouse_frame))
6957 f = last_mouse_frame;
6958 else
6959 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6961 #ifdef USE_GTK
6962 if (f && xg_event_is_for_scrollbar (f, &event))
6963 f = 0;
6964 #endif
6965 if (f)
6967 #if ! defined (USE_GTK)
6968 /* Is this in the tool-bar? */
6969 if (WINDOWP (f->tool_bar_window)
6970 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6972 Lisp_Object window;
6973 int x = event.xbutton.x;
6974 int y = event.xbutton.y;
6976 window = window_from_coordinates (f, x, y, 0, 1);
6977 tool_bar_p = EQ (window, f->tool_bar_window);
6979 if (tool_bar_p && event.xbutton.button < 4)
6980 handle_tool_bar_click
6981 (f, x, y, event.xbutton.type == ButtonPress,
6982 x_x_to_emacs_modifiers (dpyinfo, event.xbutton.state));
6984 #endif /* !USE_GTK */
6986 if (!tool_bar_p)
6987 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6988 if (! popup_activated ())
6989 #endif
6991 if (ignore_next_mouse_click_timeout)
6993 if (event.type == ButtonPress
6994 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6996 ignore_next_mouse_click_timeout = 0;
6997 construct_mouse_click (&inev.ie, &event.xbutton, f);
6999 if (event.type == ButtonRelease)
7000 ignore_next_mouse_click_timeout = 0;
7002 else
7003 construct_mouse_click (&inev.ie, &event.xbutton, f);
7005 if (FRAME_X_EMBEDDED_P (f))
7006 xembed_send_message (f, event.xbutton.time,
7007 XEMBED_REQUEST_FOCUS, 0, 0, 0);
7009 else
7011 struct scroll_bar *bar
7012 = x_window_to_scroll_bar (event.xbutton.display,
7013 event.xbutton.window);
7015 #ifdef USE_TOOLKIT_SCROLL_BARS
7016 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
7017 scroll bars. */
7018 if (bar && event.xbutton.state & ControlMask)
7020 x_scroll_bar_handle_click (bar, &event, &inev.ie);
7021 *finish = X_EVENT_DROP;
7023 #else /* not USE_TOOLKIT_SCROLL_BARS */
7024 if (bar)
7025 x_scroll_bar_handle_click (bar, &event, &inev.ie);
7026 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7029 if (event.type == ButtonPress)
7031 dpyinfo->grabbed |= (1 << event.xbutton.button);
7032 last_mouse_frame = f;
7034 if (!tool_bar_p)
7035 last_tool_bar_item = -1;
7037 else
7038 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
7040 /* Ignore any mouse motion that happened before this event;
7041 any subsequent mouse-movement Emacs events should reflect
7042 only motion after the ButtonPress/Release. */
7043 if (f != 0)
7044 f->mouse_moved = 0;
7046 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7047 f = x_menubar_window_to_frame (dpyinfo, &event);
7048 /* For a down-event in the menu bar,
7049 don't pass it to Xt right now.
7050 Instead, save it away
7051 and we will pass it to Xt from kbd_buffer_get_event.
7052 That way, we can run some Lisp code first. */
7053 if (! popup_activated ()
7054 #ifdef USE_GTK
7055 /* Gtk+ menus only react to the first three buttons. */
7056 && event.xbutton.button < 3
7057 #endif
7058 && f && event.type == ButtonPress
7059 /* Verify the event is really within the menu bar
7060 and not just sent to it due to grabbing. */
7061 && event.xbutton.x >= 0
7062 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
7063 && event.xbutton.y >= 0
7064 && event.xbutton.y < f->output_data.x->menubar_height
7065 && event.xbutton.same_screen)
7067 SET_SAVED_BUTTON_EVENT;
7068 *finish = X_EVENT_DROP;
7070 else
7071 goto OTHER;
7072 #endif /* USE_X_TOOLKIT || USE_GTK */
7074 break;
7076 case CirculateNotify:
7077 goto OTHER;
7079 case CirculateRequest:
7080 goto OTHER;
7082 case VisibilityNotify:
7083 goto OTHER;
7085 case MappingNotify:
7086 /* Someone has changed the keyboard mapping - update the
7087 local cache. */
7088 switch (event.xmapping.request)
7090 case MappingModifier:
7091 x_find_modifier_meanings (dpyinfo);
7092 /* This is meant to fall through. */
7093 case MappingKeyboard:
7094 XRefreshKeyboardMapping (&event.xmapping);
7096 goto OTHER;
7098 case DestroyNotify:
7099 xft_settings_event (dpyinfo, &event);
7100 break;
7102 default:
7103 OTHER:
7104 #ifdef USE_X_TOOLKIT
7105 block_input ();
7106 if (*finish != X_EVENT_DROP)
7107 XtDispatchEvent (&event);
7108 unblock_input ();
7109 #endif /* USE_X_TOOLKIT */
7110 break;
7113 done:
7114 if (inev.ie.kind != NO_EVENT)
7116 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7117 count++;
7120 if (do_help
7121 && !(hold_quit && hold_quit->kind != NO_EVENT))
7123 Lisp_Object frame;
7125 if (f)
7126 XSETFRAME (frame, f);
7127 else
7128 frame = Qnil;
7130 if (do_help > 0)
7132 any_help_event_p = 1;
7133 gen_help_event (help_echo_string, frame, help_echo_window,
7134 help_echo_object, help_echo_pos);
7136 else
7138 help_echo_string = Qnil;
7139 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7141 count++;
7144 SAFE_FREE ();
7145 *eventptr = event;
7146 return count;
7149 #if defined USE_GTK || defined USE_X_TOOLKIT
7151 /* Handles the XEvent EVENT on display DISPLAY.
7152 This is used for event loops outside the normal event handling,
7153 i.e. looping while a popup menu or a dialog is posted.
7155 Returns the value handle_one_xevent sets in the finish argument. */
7157 x_dispatch_event (XEvent *event, Display *display)
7159 struct x_display_info *dpyinfo;
7160 int finish = X_EVENT_NORMAL;
7162 dpyinfo = x_display_info_for_display (display);
7164 if (dpyinfo)
7165 handle_one_xevent (dpyinfo, event, &finish, 0);
7167 return finish;
7169 #endif
7172 /* Read events coming from the X server.
7173 Return as soon as there are no more events to be read.
7175 Return the number of characters stored into the buffer,
7176 thus pretending to be `read' (except the characters we store
7177 in the keyboard buffer can be multibyte, so are not necessarily
7178 C chars). */
7180 static int
7181 XTread_socket (struct terminal *terminal, struct input_event *hold_quit)
7183 int count = 0;
7184 int event_found = 0;
7186 block_input ();
7188 /* So people can tell when we have read the available input. */
7189 input_signal_count++;
7191 /* For debugging, this gives a way to fake an I/O error. */
7192 if (terminal->display_info.x == XTread_socket_fake_io_error)
7194 XTread_socket_fake_io_error = 0;
7195 x_io_error_quitter (terminal->display_info.x->display);
7198 #ifndef USE_GTK
7199 while (XPending (terminal->display_info.x->display))
7201 int finish;
7202 XEvent event;
7204 XNextEvent (terminal->display_info.x->display, &event);
7206 #ifdef HAVE_X_I18N
7207 /* Filter events for the current X input method. */
7208 if (x_filter_event (terminal->display_info.x, &event))
7209 continue;
7210 #endif
7211 event_found = 1;
7213 count += handle_one_xevent (terminal->display_info.x,
7214 &event, &finish, hold_quit);
7216 if (finish == X_EVENT_GOTO_OUT)
7217 goto out;
7220 out:;
7222 #else /* USE_GTK */
7224 /* For GTK we must use the GTK event loop. But XEvents gets passed
7225 to our filter function above, and then to the big event switch.
7226 We use a bunch of globals to communicate with our filter function,
7227 that is kind of ugly, but it works.
7229 There is no way to do one display at the time, GTK just does events
7230 from all displays. */
7232 while (gtk_events_pending ())
7234 current_count = count;
7235 current_hold_quit = hold_quit;
7237 gtk_main_iteration ();
7239 count = current_count;
7240 current_count = -1;
7241 current_hold_quit = 0;
7243 if (current_finish == X_EVENT_GOTO_OUT)
7244 break;
7246 #endif /* USE_GTK */
7248 /* On some systems, an X bug causes Emacs to get no more events
7249 when the window is destroyed. Detect that. (1994.) */
7250 if (! event_found)
7252 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7253 One XNOOP in 100 loops will make Emacs terminate.
7254 B. Bretthauer, 1994 */
7255 x_noop_count++;
7256 if (x_noop_count >= 100)
7258 x_noop_count=0;
7260 if (next_noop_dpyinfo == 0)
7261 next_noop_dpyinfo = x_display_list;
7263 XNoOp (next_noop_dpyinfo->display);
7265 /* Each time we get here, cycle through the displays now open. */
7266 next_noop_dpyinfo = next_noop_dpyinfo->next;
7270 /* If the focus was just given to an auto-raising frame,
7271 raise it now. */
7272 /* ??? This ought to be able to handle more than one such frame. */
7273 if (pending_autoraise_frame)
7275 x_raise_frame (pending_autoraise_frame);
7276 pending_autoraise_frame = 0;
7279 unblock_input ();
7281 return count;
7287 /***********************************************************************
7288 Text Cursor
7289 ***********************************************************************/
7291 /* Set clipping for output in glyph row ROW. W is the window in which
7292 we operate. GC is the graphics context to set clipping in.
7294 ROW may be a text row or, e.g., a mode line. Text rows must be
7295 clipped to the interior of the window dedicated to text display,
7296 mode lines must be clipped to the whole window. */
7298 static void
7299 x_clip_to_row (struct window *w, struct glyph_row *row,
7300 enum glyph_row_area area, GC gc)
7302 struct frame *f = XFRAME (WINDOW_FRAME (w));
7303 XRectangle clip_rect;
7304 int window_x, window_y, window_width;
7306 window_box (w, area, &window_x, &window_y, &window_width, 0);
7308 clip_rect.x = window_x;
7309 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7310 clip_rect.y = max (clip_rect.y, window_y);
7311 clip_rect.width = window_width;
7312 clip_rect.height = row->visible_height;
7314 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7318 /* Draw a hollow box cursor on window W in glyph row ROW. */
7320 static void
7321 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
7323 struct frame *f = XFRAME (WINDOW_FRAME (w));
7324 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7325 Display *dpy = FRAME_X_DISPLAY (f);
7326 int x, y, wd, h;
7327 XGCValues xgcv;
7328 struct glyph *cursor_glyph;
7329 GC gc;
7331 /* Get the glyph the cursor is on. If we can't tell because
7332 the current matrix is invalid or such, give up. */
7333 cursor_glyph = get_phys_cursor_glyph (w);
7334 if (cursor_glyph == NULL)
7335 return;
7337 /* Compute frame-relative coordinates for phys cursor. */
7338 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7339 wd = w->phys_cursor_width;
7341 /* The foreground of cursor_gc is typically the same as the normal
7342 background color, which can cause the cursor box to be invisible. */
7343 xgcv.foreground = f->output_data.x->cursor_pixel;
7344 if (dpyinfo->scratch_cursor_gc)
7345 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7346 else
7347 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7348 GCForeground, &xgcv);
7349 gc = dpyinfo->scratch_cursor_gc;
7351 /* Set clipping, draw the rectangle, and reset clipping again. */
7352 x_clip_to_row (w, row, TEXT_AREA, gc);
7353 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7354 XSetClipMask (dpy, gc, None);
7358 /* Draw a bar cursor on window W in glyph row ROW.
7360 Implementation note: One would like to draw a bar cursor with an
7361 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7362 Unfortunately, I didn't find a font yet that has this property set.
7363 --gerd. */
7365 static void
7366 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
7368 struct frame *f = XFRAME (w->frame);
7369 struct glyph *cursor_glyph;
7371 /* If cursor is out of bounds, don't draw garbage. This can happen
7372 in mini-buffer windows when switching between echo area glyphs
7373 and mini-buffer. */
7374 cursor_glyph = get_phys_cursor_glyph (w);
7375 if (cursor_glyph == NULL)
7376 return;
7377 #ifdef HAVE_XWIDGETS
7378 if (cursor_glyph->type == XWIDGET_GLYPH){
7379 printf("tried avoiding xwidget cursor\n");
7380 return; //experimental avoidance of cursor on xwidget
7382 #endif
7383 /* If on an image, draw like a normal cursor. That's usually better
7384 visible than drawing a bar, esp. if the image is large so that
7385 the bar might not be in the window. */
7386 if (cursor_glyph->type == IMAGE_GLYPH)
7388 struct glyph_row *r;
7389 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7390 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
7392 else
7394 Display *dpy = FRAME_X_DISPLAY (f);
7395 Window window = FRAME_X_WINDOW (f);
7396 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7397 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7398 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7399 XGCValues xgcv;
7401 /* If the glyph's background equals the color we normally draw
7402 the bars cursor in, the bar cursor in its normal color is
7403 invisible. Use the glyph's foreground color instead in this
7404 case, on the assumption that the glyph's colors are chosen so
7405 that the glyph is legible. */
7406 if (face->background == f->output_data.x->cursor_pixel)
7407 xgcv.background = xgcv.foreground = face->foreground;
7408 else
7409 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7410 xgcv.graphics_exposures = 0;
7412 if (gc)
7413 XChangeGC (dpy, gc, mask, &xgcv);
7414 else
7416 gc = XCreateGC (dpy, window, mask, &xgcv);
7417 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7420 x_clip_to_row (w, row, TEXT_AREA, gc);
7422 if (kind == BAR_CURSOR)
7424 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7426 if (width < 0)
7427 width = FRAME_CURSOR_WIDTH (f);
7428 width = min (cursor_glyph->pixel_width, width);
7430 w->phys_cursor_width = width;
7432 /* If the character under cursor is R2L, draw the bar cursor
7433 on the right of its glyph, rather than on the left. */
7434 if ((cursor_glyph->resolved_level & 1) != 0)
7435 x += cursor_glyph->pixel_width - width;
7437 XFillRectangle (dpy, window, gc, x,
7438 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7439 width, row->height);
7441 else
7443 int dummy_x, dummy_y, dummy_h;
7445 if (width < 0)
7446 width = row->height;
7448 width = min (row->height, width);
7450 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
7451 &dummy_y, &dummy_h);
7453 XFillRectangle (dpy, window, gc,
7454 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7455 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7456 row->height - width),
7457 w->phys_cursor_width, width);
7460 XSetClipMask (dpy, gc, None);
7465 /* RIF: Define cursor CURSOR on frame F. */
7467 static void
7468 x_define_frame_cursor (struct frame *f, Cursor cursor)
7470 if (!f->pointer_invisible
7471 && f->output_data.x->current_cursor != cursor)
7472 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7473 f->output_data.x->current_cursor = cursor;
7477 /* RIF: Clear area on frame F. */
7479 static void
7480 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
7482 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), x, y, width, height);
7483 #ifdef USE_GTK
7484 /* Must queue a redraw, because scroll bars might have been cleared. */
7485 if (FRAME_GTK_WIDGET (f))
7486 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
7487 #endif
7491 /* RIF: Draw cursor on window W. */
7493 static void
7494 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x,
7495 int y, enum text_cursor_kinds cursor_type,
7496 int cursor_width, bool on_p, bool active_p)
7498 struct frame *f = XFRAME (WINDOW_FRAME (w));
7500 if (on_p)
7502 w->phys_cursor_type = cursor_type;
7503 w->phys_cursor_on_p = 1;
7505 if (glyph_row->exact_window_width_line_p
7506 && (glyph_row->reversed_p
7507 ? (w->phys_cursor.hpos < 0)
7508 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
7510 glyph_row->cursor_in_fringe_p = 1;
7511 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
7513 else
7515 switch (cursor_type)
7517 case HOLLOW_BOX_CURSOR:
7518 x_draw_hollow_cursor (w, glyph_row);
7519 break;
7521 case FILLED_BOX_CURSOR:
7522 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7523 break;
7525 case BAR_CURSOR:
7526 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7527 break;
7529 case HBAR_CURSOR:
7530 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7531 break;
7533 case NO_CURSOR:
7534 w->phys_cursor_width = 0;
7535 break;
7537 default:
7538 emacs_abort ();
7542 #ifdef HAVE_X_I18N
7543 if (w == XWINDOW (f->selected_window))
7544 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7545 xic_set_preeditarea (w, x, y);
7546 #endif
7549 XFlush (FRAME_X_DISPLAY (f));
7553 /* Icons. */
7555 /* Make the x-window of frame F use the gnu icon bitmap. */
7558 x_bitmap_icon (struct frame *f, Lisp_Object file)
7560 ptrdiff_t bitmap_id;
7562 if (FRAME_X_WINDOW (f) == 0)
7563 return 1;
7565 /* Free up our existing icon bitmap and mask if any. */
7566 if (f->output_data.x->icon_bitmap > 0)
7567 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7568 f->output_data.x->icon_bitmap = 0;
7570 if (STRINGP (file))
7572 #ifdef USE_GTK
7573 /* Use gtk_window_set_icon_from_file () if available,
7574 It's not restricted to bitmaps */
7575 if (xg_set_icon (f, file))
7576 return 0;
7577 #endif /* USE_GTK */
7578 bitmap_id = x_create_bitmap_from_file (f, file);
7579 x_create_bitmap_mask (f, bitmap_id);
7581 else
7583 /* Create the GNU bitmap and mask if necessary. */
7584 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7586 ptrdiff_t rc = -1;
7588 #ifdef USE_GTK
7590 if (xg_set_icon (f, xg_default_icon_file)
7591 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7592 return 0;
7594 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7596 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7597 if (rc != -1)
7598 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7600 #endif
7602 /* If all else fails, use the (black and white) xbm image. */
7603 if (rc == -1)
7605 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
7606 gnu_xbm_width, gnu_xbm_height);
7607 if (rc == -1)
7608 return 1;
7610 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7611 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7615 /* The first time we create the GNU bitmap and mask,
7616 this increments the ref-count one extra time.
7617 As a result, the GNU bitmap and mask are never freed.
7618 That way, we don't have to worry about allocating it again. */
7619 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7621 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7624 x_wm_set_icon_pixmap (f, bitmap_id);
7625 f->output_data.x->icon_bitmap = bitmap_id;
7627 return 0;
7631 /* Make the x-window of frame F use a rectangle with text.
7632 Use ICON_NAME as the text. */
7635 x_text_icon (struct frame *f, const char *icon_name)
7637 if (FRAME_X_WINDOW (f) == 0)
7638 return 1;
7641 XTextProperty text;
7642 text.value = (unsigned char *) icon_name;
7643 text.encoding = XA_STRING;
7644 text.format = 8;
7645 text.nitems = strlen (icon_name);
7646 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7649 if (f->output_data.x->icon_bitmap > 0)
7650 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7651 f->output_data.x->icon_bitmap = 0;
7652 x_wm_set_icon_pixmap (f, 0);
7654 return 0;
7657 #define X_ERROR_MESSAGE_SIZE 200
7659 /* If non-nil, this should be a string.
7660 It means catch X errors and store the error message in this string.
7662 The reason we use a stack is that x_catch_error/x_uncatch_error can
7663 be called from a signal handler.
7666 struct x_error_message_stack {
7667 char string[X_ERROR_MESSAGE_SIZE];
7668 Display *dpy;
7669 struct x_error_message_stack *prev;
7671 static struct x_error_message_stack *x_error_message;
7673 /* An X error handler which stores the error message in
7674 *x_error_message. This is called from x_error_handler if
7675 x_catch_errors is in effect. */
7677 static void
7678 x_error_catcher (Display *display, XErrorEvent *event)
7680 XGetErrorText (display, event->error_code,
7681 x_error_message->string,
7682 X_ERROR_MESSAGE_SIZE);
7685 /* Begin trapping X errors for display DPY. Actually we trap X errors
7686 for all displays, but DPY should be the display you are actually
7687 operating on.
7689 After calling this function, X protocol errors no longer cause
7690 Emacs to exit; instead, they are recorded in the string
7691 stored in *x_error_message.
7693 Calling x_check_errors signals an Emacs error if an X error has
7694 occurred since the last call to x_catch_errors or x_check_errors.
7696 Calling x_uncatch_errors resumes the normal error handling. */
7698 void
7699 x_catch_errors (Display *dpy)
7701 struct x_error_message_stack *data = xmalloc (sizeof *data);
7703 /* Make sure any errors from previous requests have been dealt with. */
7704 XSync (dpy, False);
7706 data->dpy = dpy;
7707 data->string[0] = 0;
7708 data->prev = x_error_message;
7709 x_error_message = data;
7712 /* Undo the last x_catch_errors call.
7713 DPY should be the display that was passed to x_catch_errors. */
7715 void
7716 x_uncatch_errors (void)
7718 struct x_error_message_stack *tmp;
7720 block_input ();
7722 /* The display may have been closed before this function is called.
7723 Check if it is still open before calling XSync. */
7724 if (x_display_info_for_display (x_error_message->dpy) != 0)
7725 XSync (x_error_message->dpy, False);
7727 tmp = x_error_message;
7728 x_error_message = x_error_message->prev;
7729 xfree (tmp);
7730 unblock_input ();
7733 /* If any X protocol errors have arrived since the last call to
7734 x_catch_errors or x_check_errors, signal an Emacs error using
7735 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7737 void
7738 x_check_errors (Display *dpy, const char *format)
7740 /* Make sure to catch any errors incurred so far. */
7741 XSync (dpy, False);
7743 if (x_error_message->string[0])
7745 char string[X_ERROR_MESSAGE_SIZE];
7746 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
7747 x_uncatch_errors ();
7748 error (format, string);
7752 /* Nonzero if we had any X protocol errors
7753 since we did x_catch_errors on DPY. */
7755 bool
7756 x_had_errors_p (Display *dpy)
7758 /* Make sure to catch any errors incurred so far. */
7759 XSync (dpy, False);
7761 return x_error_message->string[0] != 0;
7764 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7766 void
7767 x_clear_errors (Display *dpy)
7769 x_error_message->string[0] = 0;
7772 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7773 * idea. --lorentey */
7774 /* Close off all unclosed x_catch_errors calls. */
7776 void
7777 x_fully_uncatch_errors (void)
7779 while (x_error_message)
7780 x_uncatch_errors ();
7782 #endif
7784 #if 0
7785 static unsigned int x_wire_count;
7786 x_trace_wire (void)
7788 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7790 #endif /* ! 0 */
7793 /************************************************************************
7794 Handling X errors
7795 ************************************************************************/
7797 /* Error message passed to x_connection_closed. */
7799 static char *error_msg;
7801 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7802 the text of an error message that lead to the connection loss. */
7804 static void
7805 x_connection_closed (Display *dpy, const char *error_message)
7807 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7808 Lisp_Object frame, tail;
7809 ptrdiff_t idx = SPECPDL_INDEX ();
7811 error_msg = alloca (strlen (error_message) + 1);
7812 strcpy (error_msg, error_message);
7814 /* Inhibit redisplay while frames are being deleted. */
7815 specbind (Qinhibit_redisplay, Qt);
7817 if (dpyinfo)
7819 /* Protect display from being closed when we delete the last
7820 frame on it. */
7821 dpyinfo->reference_count++;
7822 dpyinfo->terminal->reference_count++;
7825 /* First delete frames whose mini-buffers are on frames
7826 that are on the dead display. */
7827 FOR_EACH_FRAME (tail, frame)
7829 Lisp_Object minibuf_frame;
7830 minibuf_frame
7831 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7832 if (FRAME_X_P (XFRAME (frame))
7833 && FRAME_X_P (XFRAME (minibuf_frame))
7834 && ! EQ (frame, minibuf_frame)
7835 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7836 delete_frame (frame, Qnoelisp);
7839 /* Now delete all remaining frames on the dead display.
7840 We are now sure none of these is used as the mini-buffer
7841 for another frame that we need to delete. */
7842 FOR_EACH_FRAME (tail, frame)
7843 if (FRAME_X_P (XFRAME (frame))
7844 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7846 /* Set this to t so that delete_frame won't get confused
7847 trying to find a replacement. */
7848 kset_default_minibuffer_frame (FRAME_KBOARD (XFRAME (frame)), Qt);
7849 delete_frame (frame, Qnoelisp);
7852 /* If DPYINFO is null, this means we didn't open the display in the
7853 first place, so don't try to close it. */
7854 if (dpyinfo)
7856 /* We can not call XtCloseDisplay here because it calls XSync.
7857 XSync inside the error handler apparently hangs Emacs. On
7858 current Xt versions, this isn't needed either. */
7859 #ifdef USE_GTK
7860 /* A long-standing GTK bug prevents proper disconnect handling
7861 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
7862 the resulting Glib error message loop filled a user's disk.
7863 To avoid this, kill Emacs unconditionally on disconnect. */
7864 shut_down_emacs (0, Qnil);
7865 fprintf (stderr, "%s\n\
7866 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
7867 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
7868 For details, see etc/PROBLEMS.\n",
7869 error_msg);
7870 emacs_abort ();
7871 #endif /* USE_GTK */
7873 /* Indicate that this display is dead. */
7874 dpyinfo->display = 0;
7876 dpyinfo->reference_count--;
7877 dpyinfo->terminal->reference_count--;
7878 if (dpyinfo->reference_count != 0)
7879 /* We have just closed all frames on this display. */
7880 emacs_abort ();
7883 Lisp_Object tmp;
7884 XSETTERMINAL (tmp, dpyinfo->terminal);
7885 Fdelete_terminal (tmp, Qnoelisp);
7889 if (terminal_list == 0)
7891 fprintf (stderr, "%s\n", error_msg);
7892 Fkill_emacs (make_number (70));
7893 /* NOTREACHED */
7896 totally_unblock_input ();
7898 unbind_to (idx, Qnil);
7899 clear_waiting_for_input ();
7901 /* Tell GCC not to suggest attribute 'noreturn' for this function. */
7902 IF_LINT (if (! terminal_list) return; )
7904 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7905 longjmp), because returning from this function would get us back into
7906 Xlib's code which will directly call `exit'. */
7907 error ("%s", error_msg);
7910 /* We specifically use it before defining it, so that gcc doesn't inline it,
7911 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7912 static void x_error_quitter (Display *, XErrorEvent *);
7914 /* This is the first-level handler for X protocol errors.
7915 It calls x_error_quitter or x_error_catcher. */
7917 static int
7918 x_error_handler (Display *display, XErrorEvent *event)
7920 #if defined USE_GTK && defined HAVE_GTK3
7921 if ((event->error_code == BadMatch || event->error_code == BadWindow)
7922 && event->request_code == X_SetInputFocus)
7924 return 0;
7926 #endif
7928 if (x_error_message)
7929 x_error_catcher (display, event);
7930 else
7931 x_error_quitter (display, event);
7932 return 0;
7935 /* This is the usual handler for X protocol errors.
7936 It kills all frames on the display that we got the error for.
7937 If that was the only one, it prints an error message and kills Emacs. */
7939 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7941 /* On older GCC versions, just putting x_error_quitter
7942 after x_error_handler prevents inlining into the former. */
7944 static void NO_INLINE
7945 x_error_quitter (Display *display, XErrorEvent *event)
7947 char buf[256], buf1[356];
7949 /* Ignore BadName errors. They can happen because of fonts
7950 or colors that are not defined. */
7952 if (event->error_code == BadName)
7953 return;
7955 /* Note that there is no real way portable across R3/R4 to get the
7956 original error handler. */
7958 XGetErrorText (display, event->error_code, buf, sizeof (buf));
7959 sprintf (buf1, "X protocol error: %s on protocol request %d",
7960 buf, event->request_code);
7961 x_connection_closed (display, buf1);
7965 /* This is the handler for X IO errors, always.
7966 It kills all frames on the display that we lost touch with.
7967 If that was the only one, it prints an error message and kills Emacs. */
7969 static int
7970 x_io_error_quitter (Display *display)
7972 char buf[256];
7974 snprintf (buf, sizeof buf, "Connection lost to X server `%s'",
7975 DisplayString (display));
7976 x_connection_closed (display, buf);
7977 return 0;
7980 /* Changing the font of the frame. */
7982 /* Give frame F the font FONT-OBJECT as its default font. The return
7983 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
7984 frame. If it is negative, generate a new fontset from
7985 FONT-OBJECT. */
7987 Lisp_Object
7988 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
7990 struct font *font = XFONT_OBJECT (font_object);
7992 if (fontset < 0)
7993 fontset = fontset_from_font (font_object);
7994 FRAME_FONTSET (f) = fontset;
7995 if (FRAME_FONT (f) == font)
7996 /* This font is already set in frame F. There's nothing more to
7997 do. */
7998 return font_object;
8000 FRAME_FONT (f) = font;
8001 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
8002 FRAME_COLUMN_WIDTH (f) = font->average_width;
8003 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
8005 compute_fringe_widths (f, 1);
8007 /* Compute the scroll bar width in character columns. */
8008 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8010 int wid = FRAME_COLUMN_WIDTH (f);
8011 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8012 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8014 else
8016 int wid = FRAME_COLUMN_WIDTH (f);
8017 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8020 if (FRAME_X_WINDOW (f) != 0)
8022 /* Don't change the size of a tip frame; there's no point in
8023 doing it because it's done in Fx_show_tip, and it leads to
8024 problems because the tip frame has no widget. */
8025 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8026 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8029 #ifdef HAVE_X_I18N
8030 if (FRAME_XIC (f)
8031 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8033 block_input ();
8034 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
8035 unblock_input ();
8037 #endif
8039 return font_object;
8043 /***********************************************************************
8044 X Input Methods
8045 ***********************************************************************/
8047 #ifdef HAVE_X_I18N
8049 #ifdef HAVE_X11R6
8051 /* XIM destroy callback function, which is called whenever the
8052 connection to input method XIM dies. CLIENT_DATA contains a
8053 pointer to the x_display_info structure corresponding to XIM. */
8055 static void
8056 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
8058 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8059 Lisp_Object frame, tail;
8061 block_input ();
8063 /* No need to call XDestroyIC.. */
8064 FOR_EACH_FRAME (tail, frame)
8066 struct frame *f = XFRAME (frame);
8067 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8069 FRAME_XIC (f) = NULL;
8070 xic_free_xfontset (f);
8074 /* No need to call XCloseIM. */
8075 dpyinfo->xim = NULL;
8076 XFree (dpyinfo->xim_styles);
8077 unblock_input ();
8080 #endif /* HAVE_X11R6 */
8082 #ifdef HAVE_X11R6
8083 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8084 extern char *XSetIMValues (XIM, ...);
8085 #endif
8087 /* Open the connection to the XIM server on display DPYINFO.
8088 RESOURCE_NAME is the resource name Emacs uses. */
8090 static void
8091 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
8093 XIM xim;
8095 #ifdef HAVE_XIM
8096 if (use_xim)
8098 if (dpyinfo->xim)
8099 XCloseIM (dpyinfo->xim);
8100 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8101 emacs_class);
8102 dpyinfo->xim = xim;
8104 if (xim)
8106 #ifdef HAVE_X11R6
8107 XIMCallback destroy;
8108 #endif
8110 /* Get supported styles and XIM values. */
8111 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8113 #ifdef HAVE_X11R6
8114 destroy.callback = xim_destroy_callback;
8115 destroy.client_data = (XPointer)dpyinfo;
8116 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8117 #endif
8121 else
8122 #endif /* HAVE_XIM */
8123 dpyinfo->xim = NULL;
8127 #ifdef HAVE_X11R6_XIM
8129 /* XIM instantiate callback function, which is called whenever an XIM
8130 server is available. DISPLAY is the display of the XIM.
8131 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8132 when the callback was registered. */
8134 static void
8135 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
8137 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8138 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8140 /* We don't support multiple XIM connections. */
8141 if (dpyinfo->xim)
8142 return;
8144 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8146 /* Create XIC for the existing frames on the same display, as long
8147 as they have no XIC. */
8148 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8150 Lisp_Object tail, frame;
8152 block_input ();
8153 FOR_EACH_FRAME (tail, frame)
8155 struct frame *f = XFRAME (frame);
8157 if (FRAME_X_P (f)
8158 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8159 if (FRAME_XIC (f) == NULL)
8161 create_frame_xic (f);
8162 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8163 xic_set_statusarea (f);
8164 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8166 struct window *w = XWINDOW (f->selected_window);
8167 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8172 unblock_input ();
8176 #endif /* HAVE_X11R6_XIM */
8179 /* Open a connection to the XIM server on display DPYINFO.
8180 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8181 connection only at the first time. On X11R6, open the connection
8182 in the XIM instantiate callback function. */
8184 static void
8185 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
8187 dpyinfo->xim = NULL;
8188 #ifdef HAVE_XIM
8189 if (use_xim)
8191 #ifdef HAVE_X11R6_XIM
8192 struct xim_inst_t *xim_inst = xmalloc (sizeof *xim_inst);
8194 dpyinfo->xim_callback_data = xim_inst;
8195 xim_inst->dpyinfo = dpyinfo;
8196 xim_inst->resource_name = xstrdup (resource_name);
8197 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8198 resource_name, emacs_class,
8199 xim_instantiate_callback,
8200 /* This is XPointer in XFree86
8201 but (XPointer *) on Tru64, at
8202 least, hence the configure test. */
8203 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8204 #else /* not HAVE_X11R6_XIM */
8205 xim_open_dpy (dpyinfo, resource_name);
8206 #endif /* not HAVE_X11R6_XIM */
8208 #endif /* HAVE_XIM */
8212 /* Close the connection to the XIM server on display DPYINFO. */
8214 static void
8215 xim_close_dpy (struct x_display_info *dpyinfo)
8217 #ifdef HAVE_XIM
8218 if (use_xim)
8220 #ifdef HAVE_X11R6_XIM
8221 if (dpyinfo->display)
8222 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8223 NULL, emacs_class,
8224 xim_instantiate_callback, NULL);
8225 xfree (dpyinfo->xim_callback_data->resource_name);
8226 xfree (dpyinfo->xim_callback_data);
8227 #endif /* HAVE_X11R6_XIM */
8228 if (dpyinfo->display)
8229 XCloseIM (dpyinfo->xim);
8230 dpyinfo->xim = NULL;
8231 XFree (dpyinfo->xim_styles);
8233 #endif /* HAVE_XIM */
8236 #endif /* not HAVE_X11R6_XIM */
8240 /* Calculate the absolute position in frame F
8241 from its current recorded position values and gravity. */
8243 static void
8244 x_calc_absolute_position (struct frame *f)
8246 int flags = f->size_hint_flags;
8248 /* We have nothing to do if the current position
8249 is already for the top-left corner. */
8250 if (! ((flags & XNegative) || (flags & YNegative)))
8251 return;
8253 /* Treat negative positions as relative to the leftmost bottommost
8254 position that fits on the screen. */
8255 if (flags & XNegative)
8256 f->left_pos = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
8257 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8260 int height = FRAME_PIXEL_HEIGHT (f);
8262 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8263 /* Something is fishy here. When using Motif, starting Emacs with
8264 `-g -0-0', the frame appears too low by a few pixels.
8266 This seems to be so because initially, while Emacs is starting,
8267 the column widget's height and the frame's pixel height are
8268 different. The column widget's height is the right one. In
8269 later invocations, when Emacs is up, the frame's pixel height
8270 is right, though.
8272 It's not obvious where the initial small difference comes from.
8273 2000-12-01, gerd. */
8275 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8276 #endif
8278 if (flags & YNegative)
8279 f->top_pos = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
8280 - height + f->top_pos;
8283 /* The left_pos and top_pos
8284 are now relative to the top and left screen edges,
8285 so the flags should correspond. */
8286 f->size_hint_flags &= ~ (XNegative | YNegative);
8289 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8290 to really change the position, and 0 when calling from
8291 x_make_frame_visible (in that case, XOFF and YOFF are the current
8292 position values). It is -1 when calling from x_set_frame_parameters,
8293 which means, do adjust for borders but don't change the gravity. */
8295 void
8296 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
8298 int modified_top, modified_left;
8300 if (change_gravity > 0)
8302 f->top_pos = yoff;
8303 f->left_pos = xoff;
8304 f->size_hint_flags &= ~ (XNegative | YNegative);
8305 if (xoff < 0)
8306 f->size_hint_flags |= XNegative;
8307 if (yoff < 0)
8308 f->size_hint_flags |= YNegative;
8309 f->win_gravity = NorthWestGravity;
8311 x_calc_absolute_position (f);
8313 block_input ();
8314 x_wm_set_size_hint (f, (long) 0, 0);
8316 modified_left = f->left_pos;
8317 modified_top = f->top_pos;
8319 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8321 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8322 than the WM decorations. So we use the calculated offset instead
8323 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8324 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8325 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8328 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8329 modified_left, modified_top);
8331 x_sync_with_move (f, f->left_pos, f->top_pos,
8332 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8333 ? 1 : 0);
8335 /* change_gravity is non-zero when this function is called from Lisp to
8336 programmatically move a frame. In that case, we call
8337 x_check_expected_move to discover if we have a "Type A" or "Type B"
8338 window manager, and, for a "Type A" window manager, adjust the position
8339 of the frame.
8341 We call x_check_expected_move if a programmatic move occurred, and
8342 either the window manager type (A/B) is unknown or it is Type A but we
8343 need to compute the top/left offset adjustment for this frame. */
8345 if (change_gravity != 0 &&
8346 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8347 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8348 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8349 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8350 x_check_expected_move (f, modified_left, modified_top);
8352 unblock_input ();
8355 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8356 on the root window for frame F contains ATOMNAME.
8357 This is how a WM check shall be done according to the Window Manager
8358 Specification/Extended Window Manager Hints at
8359 http://freedesktop.org/wiki/Specifications/wm-spec. */
8361 static int
8362 wm_supports (struct frame *f, Atom want_atom)
8364 Atom actual_type;
8365 unsigned long actual_size, bytes_remaining;
8366 int i, rc, actual_format;
8367 Window wmcheck_window;
8368 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8369 Window target_window = dpyinfo->root_window;
8370 long max_len = 65536;
8371 Display *dpy = FRAME_X_DISPLAY (f);
8372 unsigned char *tmp_data = NULL;
8373 Atom target_type = XA_WINDOW;
8375 block_input ();
8377 x_catch_errors (dpy);
8378 rc = XGetWindowProperty (dpy, target_window,
8379 dpyinfo->Xatom_net_supporting_wm_check,
8380 0, max_len, False, target_type,
8381 &actual_type, &actual_format, &actual_size,
8382 &bytes_remaining, &tmp_data);
8384 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8386 if (tmp_data) XFree (tmp_data);
8387 x_uncatch_errors ();
8388 unblock_input ();
8389 return 0;
8392 wmcheck_window = *(Window *) tmp_data;
8393 XFree (tmp_data);
8395 /* Check if window exists. */
8396 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8397 x_sync (f);
8398 if (x_had_errors_p (dpy))
8400 x_uncatch_errors ();
8401 unblock_input ();
8402 return 0;
8405 if (dpyinfo->net_supported_window != wmcheck_window)
8407 /* Window changed, reload atoms */
8408 if (dpyinfo->net_supported_atoms != NULL)
8409 XFree (dpyinfo->net_supported_atoms);
8410 dpyinfo->net_supported_atoms = NULL;
8411 dpyinfo->nr_net_supported_atoms = 0;
8412 dpyinfo->net_supported_window = 0;
8414 target_type = XA_ATOM;
8415 tmp_data = NULL;
8416 rc = XGetWindowProperty (dpy, target_window,
8417 dpyinfo->Xatom_net_supported,
8418 0, max_len, False, target_type,
8419 &actual_type, &actual_format, &actual_size,
8420 &bytes_remaining, &tmp_data);
8422 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8424 if (tmp_data) XFree (tmp_data);
8425 x_uncatch_errors ();
8426 unblock_input ();
8427 return 0;
8430 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8431 dpyinfo->nr_net_supported_atoms = actual_size;
8432 dpyinfo->net_supported_window = wmcheck_window;
8435 rc = 0;
8437 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8438 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8440 x_uncatch_errors ();
8441 unblock_input ();
8443 return rc;
8446 static void
8447 set_wm_state (Lisp_Object frame, int add, Atom atom, Atom value)
8449 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (frame));
8451 x_send_client_event (frame, make_number (0), frame,
8452 dpyinfo->Xatom_net_wm_state,
8453 make_number (32),
8454 /* 1 = add, 0 = remove */
8455 Fcons
8456 (make_number (add ? 1 : 0),
8457 Fcons
8458 (make_fixnum_or_float (atom),
8459 (value != 0
8460 ? list1 (make_fixnum_or_float (value))
8461 : Qnil))));
8464 void
8465 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
8467 Lisp_Object frame;
8468 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8470 XSETFRAME (frame, f);
8472 set_wm_state (frame, NILP (new_value) ? 0 : 1,
8473 dpyinfo->Xatom_net_wm_state_sticky, None);
8476 /* Return the current _NET_WM_STATE.
8477 SIZE_STATE is set to one of the FULLSCREEN_* values.
8478 STICKY is set to 1 if the sticky state is set, 0 if not.
8480 Return non-zero if we are not hidden, zero if we are. */
8482 static int
8483 get_current_wm_state (struct frame *f,
8484 Window window,
8485 int *size_state,
8486 int *sticky)
8488 Atom actual_type;
8489 unsigned long actual_size, bytes_remaining;
8490 int i, rc, actual_format, is_hidden = 0;
8491 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8492 long max_len = 65536;
8493 Display *dpy = FRAME_X_DISPLAY (f);
8494 unsigned char *tmp_data = NULL;
8495 Atom target_type = XA_ATOM;
8497 *sticky = 0;
8498 *size_state = FULLSCREEN_NONE;
8500 block_input ();
8501 x_catch_errors (dpy);
8502 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
8503 0, max_len, False, target_type,
8504 &actual_type, &actual_format, &actual_size,
8505 &bytes_remaining, &tmp_data);
8507 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
8509 if (tmp_data) XFree (tmp_data);
8510 x_uncatch_errors ();
8511 unblock_input ();
8512 return !FRAME_ICONIFIED_P (f);
8515 x_uncatch_errors ();
8517 for (i = 0; i < actual_size; ++i)
8519 Atom a = ((Atom*)tmp_data)[i];
8520 if (a == dpyinfo->Xatom_net_wm_state_hidden)
8522 is_hidden = 1;
8523 f->output_data.x->net_wm_state_hidden_seen = 1;
8525 else if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
8527 if (*size_state == FULLSCREEN_HEIGHT)
8528 *size_state = FULLSCREEN_MAXIMIZED;
8529 else
8530 *size_state = FULLSCREEN_WIDTH;
8532 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
8534 if (*size_state == FULLSCREEN_WIDTH)
8535 *size_state = FULLSCREEN_MAXIMIZED;
8536 else
8537 *size_state = FULLSCREEN_HEIGHT;
8539 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
8540 *size_state = FULLSCREEN_BOTH;
8541 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
8542 *sticky = 1;
8545 if (tmp_data) XFree (tmp_data);
8546 unblock_input ();
8547 return ! is_hidden;
8550 /* Do fullscreen as specified in extended window manager hints */
8552 static int
8553 do_ewmh_fullscreen (struct frame *f)
8555 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8556 int have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state);
8557 int cur, dummy;
8559 (void)get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
8561 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8562 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8563 if (!have_net_atom)
8564 have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
8566 if (have_net_atom && cur != f->want_fullscreen)
8568 Lisp_Object frame;
8570 XSETFRAME (frame, f);
8572 /* Keep number of calls to set_wm_state as low as possible.
8573 Some window managers, or possible Gtk+, hangs when too many
8574 are sent at once. */
8575 switch (f->want_fullscreen)
8577 case FULLSCREEN_BOTH:
8578 if (cur == FULLSCREEN_WIDTH || cur == FULLSCREEN_MAXIMIZED
8579 || cur == FULLSCREEN_HEIGHT)
8580 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_maximized_horz,
8581 dpyinfo->Xatom_net_wm_state_maximized_vert);
8582 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8583 break;
8584 case FULLSCREEN_WIDTH:
8585 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
8586 || cur == FULLSCREEN_MAXIMIZED)
8587 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen,
8588 dpyinfo->Xatom_net_wm_state_maximized_vert);
8589 if (cur != FULLSCREEN_MAXIMIZED)
8590 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_horz, None);
8591 break;
8592 case FULLSCREEN_HEIGHT:
8593 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
8594 || cur == FULLSCREEN_MAXIMIZED)
8595 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen,
8596 dpyinfo->Xatom_net_wm_state_maximized_horz);
8597 if (cur != FULLSCREEN_MAXIMIZED)
8598 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_vert, None);
8599 break;
8600 case FULLSCREEN_MAXIMIZED:
8601 if (cur == FULLSCREEN_BOTH)
8602 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8603 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_horz,
8604 dpyinfo->Xatom_net_wm_state_maximized_vert);
8605 break;
8606 case FULLSCREEN_NONE:
8607 if (cur == FULLSCREEN_BOTH)
8608 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8609 else
8610 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_maximized_horz,
8611 dpyinfo->Xatom_net_wm_state_maximized_vert);
8614 f->want_fullscreen = FULLSCREEN_NONE;
8618 return have_net_atom;
8621 static void
8622 XTfullscreen_hook (struct frame *f)
8624 if (FRAME_VISIBLE_P (f))
8626 block_input ();
8627 x_check_fullscreen (f);
8628 x_sync (f);
8629 unblock_input ();
8634 static int
8635 x_handle_net_wm_state (struct frame *f, XPropertyEvent *event)
8637 int value = FULLSCREEN_NONE;
8638 Lisp_Object lval;
8639 int sticky = 0;
8640 int not_hidden = get_current_wm_state (f, event->window, &value, &sticky);
8642 lval = Qnil;
8643 switch (value)
8645 case FULLSCREEN_WIDTH:
8646 lval = Qfullwidth;
8647 break;
8648 case FULLSCREEN_HEIGHT:
8649 lval = Qfullheight;
8650 break;
8651 case FULLSCREEN_BOTH:
8652 lval = Qfullboth;
8653 break;
8654 case FULLSCREEN_MAXIMIZED:
8655 lval = Qmaximized;
8656 break;
8659 store_frame_param (f, Qfullscreen, lval);
8660 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
8662 return not_hidden;
8665 /* Check if we need to resize the frame due to a fullscreen request.
8666 If so needed, resize the frame. */
8667 static void
8668 x_check_fullscreen (struct frame *f)
8670 if (do_ewmh_fullscreen (f))
8671 return;
8673 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8674 return; /* Only fullscreen without WM or with EWM hints (above). */
8676 /* Setting fullscreen to nil doesn't do anything. We could save the
8677 last non-fullscreen size and restore it, but it seems like a
8678 lot of work for this unusual case (no window manager running). */
8680 if (f->want_fullscreen != FULLSCREEN_NONE)
8682 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
8683 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8685 switch (f->want_fullscreen)
8687 /* No difference between these two when there is no WM */
8688 case FULLSCREEN_BOTH:
8689 case FULLSCREEN_MAXIMIZED:
8690 width = x_display_pixel_width (dpyinfo);
8691 height = x_display_pixel_height (dpyinfo);
8692 break;
8693 case FULLSCREEN_WIDTH:
8694 width = x_display_pixel_width (dpyinfo);
8695 break;
8696 case FULLSCREEN_HEIGHT:
8697 height = x_display_pixel_height (dpyinfo);
8700 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8701 width, height);
8705 /* This function is called by x_set_offset to determine whether the window
8706 manager interfered with the positioning of the frame. Type A window
8707 managers position the surrounding window manager decorations a small
8708 amount above and left of the user-supplied position. Type B window
8709 managers position the surrounding window manager decorations at the
8710 user-specified position. If we detect a Type A window manager, we
8711 compensate by moving the window right and down by the proper amount. */
8713 static void
8714 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
8716 int current_left = 0, current_top = 0;
8718 /* x_real_positions returns the left and top offsets of the outermost
8719 window manager window around the frame. */
8721 x_real_positions (f, &current_left, &current_top);
8723 if (current_left != expected_left || current_top != expected_top)
8725 /* It's a "Type A" window manager. */
8727 int adjusted_left;
8728 int adjusted_top;
8730 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8731 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8732 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8734 /* Now fix the mispositioned frame's location. */
8736 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8737 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8739 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8740 adjusted_left, adjusted_top);
8742 x_sync_with_move (f, expected_left, expected_top, 0);
8744 else
8745 /* It's a "Type B" window manager. We don't have to adjust the
8746 frame's position. */
8748 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8752 /* Wait for XGetGeometry to return up-to-date position information for a
8753 recently-moved frame. Call this immediately after calling XMoveWindow.
8754 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8755 frame has been moved to, so we use a fuzzy position comparison instead
8756 of an exact comparison. */
8758 static void
8759 x_sync_with_move (struct frame *f, int left, int top, int fuzzy)
8761 int count = 0;
8763 while (count++ < 50)
8765 int current_left = 0, current_top = 0;
8767 /* In theory, this call to XSync only needs to happen once, but in
8768 practice, it doesn't seem to work, hence the need for the surrounding
8769 loop. */
8771 XSync (FRAME_X_DISPLAY (f), False);
8772 x_real_positions (f, &current_left, &current_top);
8774 if (fuzzy)
8776 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8777 pixels. */
8779 if (eabs (current_left - left) <= 10
8780 && eabs (current_top - top) <= 40)
8781 return;
8783 else if (current_left == left && current_top == top)
8784 return;
8787 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8788 will then return up-to-date position info. */
8790 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8794 /* Wait for an event on frame F matching EVENTTYPE. */
8795 void
8796 x_wait_for_event (struct frame *f, int eventtype)
8798 int level = interrupt_input_blocked;
8800 fd_set fds;
8801 struct timespec tmo, tmo_at, time_now;
8802 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
8804 pending_event_wait.f = f;
8805 pending_event_wait.eventtype = eventtype;
8807 /* Set timeout to 0.1 second. Hopefully not noticeable.
8808 Maybe it should be configurable. */
8809 tmo = make_timespec (0, 100 * 1000 * 1000);
8810 tmo_at = timespec_add (current_timespec (), tmo);
8812 while (pending_event_wait.eventtype)
8814 pending_signals = 1;
8815 totally_unblock_input ();
8816 /* XTread_socket is called after unblock. */
8817 block_input ();
8818 interrupt_input_blocked = level;
8820 FD_ZERO (&fds);
8821 FD_SET (fd, &fds);
8823 time_now = current_timespec ();
8824 if (timespec_cmp (tmo_at, time_now) < 0)
8825 break;
8827 tmo = timespec_sub (tmo_at, time_now);
8828 if (pselect (fd + 1, &fds, NULL, NULL, &tmo, NULL) == 0)
8829 break; /* Timeout */
8831 pending_event_wait.f = 0;
8832 pending_event_wait.eventtype = 0;
8836 /* Change the size of frame F's X window to COLS/ROWS in the case F
8837 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8838 top-left-corner window gravity for this size change and subsequent
8839 size changes. Otherwise we leave the window gravity unchanged. */
8841 static void
8842 x_set_window_size_1 (struct frame *f, int change_gravity, int cols, int rows)
8844 int pixelwidth, pixelheight;
8846 check_frame_size (f, &rows, &cols);
8847 f->scroll_bar_actual_width
8848 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8850 : FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f));
8852 compute_fringe_widths (f, 0);
8854 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
8855 + FRAME_TOOLBAR_WIDTH (f);
8856 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
8857 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
8859 if (change_gravity) f->win_gravity = NorthWestGravity;
8860 x_wm_set_size_hint (f, (long) 0, 0);
8861 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8862 pixelwidth, pixelheight);
8865 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8866 receive in the ConfigureNotify event; if we get what we asked
8867 for, then the event won't cause the screen to become garbaged, so
8868 we have to make sure to do it here. */
8869 SET_FRAME_GARBAGED (f);
8871 /* Now, strictly speaking, we can't be sure that this is accurate,
8872 but the window manager will get around to dealing with the size
8873 change request eventually, and we'll hear how it went when the
8874 ConfigureNotify event gets here.
8876 We could just not bother storing any of this information here,
8877 and let the ConfigureNotify event set everything up, but that
8878 might be kind of confusing to the Lisp code, since size changes
8879 wouldn't be reported in the frame parameters until some random
8880 point in the future when the ConfigureNotify event arrives.
8882 We pass 1 for DELAY since we can't run Lisp code inside of
8883 a BLOCK_INPUT. */
8885 /* But the ConfigureNotify may in fact never arrive, and then this is
8886 not right if the frame is visible. Instead wait (with timeout)
8887 for the ConfigureNotify. */
8888 if (FRAME_VISIBLE_P (f))
8889 x_wait_for_event (f, ConfigureNotify);
8890 else
8892 change_frame_size (f, rows, cols, 0, 1, 0);
8893 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8894 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8895 x_sync (f);
8900 /* Call this to change the size of frame F's x-window.
8901 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8902 for this size change and subsequent size changes.
8903 Otherwise we leave the window gravity unchanged. */
8905 void
8906 x_set_window_size (struct frame *f, int change_gravity, int cols, int rows)
8908 block_input ();
8910 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8912 int r, c;
8914 /* When the frame is maximized/fullscreen or running under for
8915 example Xmonad, x_set_window_size_1 will be a no-op.
8916 In that case, the right thing to do is extend rows/cols to
8917 the current frame size. We do that first if x_set_window_size_1
8918 turns out to not be a no-op (there is no way to know).
8919 The size will be adjusted again if the frame gets a
8920 ConfigureNotify event as a result of x_set_window_size. */
8921 int pixelh = FRAME_PIXEL_HEIGHT (f);
8922 #ifdef USE_X_TOOLKIT
8923 /* The menu bar is not part of text lines. The tool bar
8924 is however. */
8925 pixelh -= FRAME_MENUBAR_HEIGHT (f);
8926 #endif
8927 r = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelh);
8928 /* Update f->scroll_bar_actual_width because it is used in
8929 FRAME_PIXEL_WIDTH_TO_TEXT_COLS. */
8930 f->scroll_bar_actual_width
8931 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
8932 c = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, FRAME_PIXEL_WIDTH (f));
8933 change_frame_size (f, r, c, 0, 1, 0);
8936 #ifdef USE_GTK
8937 if (FRAME_GTK_WIDGET (f))
8938 xg_frame_set_char_size (f, cols, rows);
8939 else
8940 x_set_window_size_1 (f, change_gravity, cols, rows);
8941 #else /* not USE_GTK */
8943 x_set_window_size_1 (f, change_gravity, cols, rows);
8945 #endif /* not USE_GTK */
8947 /* If cursor was outside the new size, mark it as off. */
8948 mark_window_cursors_off (XWINDOW (f->root_window));
8950 /* Clear out any recollection of where the mouse highlighting was,
8951 since it might be in a place that's outside the new frame size.
8952 Actually checking whether it is outside is a pain in the neck,
8953 so don't try--just let the highlighting be done afresh with new size. */
8954 cancel_mouse_face (f);
8956 unblock_input ();
8959 /* Mouse warping. */
8961 void
8962 x_set_mouse_position (struct frame *f, int x, int y)
8964 int pix_x, pix_y;
8966 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8967 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8969 if (pix_x < 0) pix_x = 0;
8970 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8972 if (pix_y < 0) pix_y = 0;
8973 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8975 block_input ();
8977 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8978 0, 0, 0, 0, pix_x, pix_y);
8979 unblock_input ();
8982 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8984 void
8985 x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
8987 block_input ();
8989 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8990 0, 0, 0, 0, pix_x, pix_y);
8991 unblock_input ();
8994 /* Raise frame F. */
8996 void
8997 x_raise_frame (struct frame *f)
8999 block_input ();
9000 if (FRAME_VISIBLE_P (f))
9001 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9002 XFlush (FRAME_X_DISPLAY (f));
9003 unblock_input ();
9006 /* Lower frame F. */
9008 static void
9009 x_lower_frame (struct frame *f)
9011 if (FRAME_VISIBLE_P (f))
9013 block_input ();
9014 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9015 XFlush (FRAME_X_DISPLAY (f));
9016 unblock_input ();
9020 /* Request focus with XEmbed */
9022 void
9023 xembed_request_focus (struct frame *f)
9025 /* See XEmbed Protocol Specification at
9026 http://freedesktop.org/wiki/Specifications/xembed-spec */
9027 if (FRAME_VISIBLE_P (f))
9028 xembed_send_message (f, CurrentTime,
9029 XEMBED_REQUEST_FOCUS, 0, 0, 0);
9032 /* Activate frame with Extended Window Manager Hints */
9034 void
9035 x_ewmh_activate_frame (struct frame *f)
9037 /* See Window Manager Specification/Extended Window Manager Hints at
9038 http://freedesktop.org/wiki/Specifications/wm-spec */
9040 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9042 if (FRAME_VISIBLE_P (f) && wm_supports (f, dpyinfo->Xatom_net_active_window))
9044 Lisp_Object frame;
9045 XSETFRAME (frame, f);
9046 x_send_client_event (frame, make_number (0), frame,
9047 dpyinfo->Xatom_net_active_window,
9048 make_number (32),
9049 list2i (1, dpyinfo->last_user_time));
9053 static void
9054 XTframe_raise_lower (struct frame *f, int raise_flag)
9056 if (raise_flag)
9057 x_raise_frame (f);
9058 else
9059 x_lower_frame (f);
9062 /* XEmbed implementation. */
9064 #if defined USE_X_TOOLKIT || ! defined USE_GTK
9066 /* XEmbed implementation. */
9068 #define XEMBED_VERSION 0
9070 static void
9071 xembed_set_info (struct frame *f, enum xembed_info flags)
9073 unsigned long data[2];
9074 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9076 data[0] = XEMBED_VERSION;
9077 data[1] = flags;
9079 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9080 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
9081 32, PropModeReplace, (unsigned char *) data, 2);
9083 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
9085 static void
9086 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
9087 long int detail, long int data1, long int data2)
9089 XEvent event;
9091 event.xclient.type = ClientMessage;
9092 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9093 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
9094 event.xclient.format = 32;
9095 event.xclient.data.l[0] = t;
9096 event.xclient.data.l[1] = msg;
9097 event.xclient.data.l[2] = detail;
9098 event.xclient.data.l[3] = data1;
9099 event.xclient.data.l[4] = data2;
9101 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9102 False, NoEventMask, &event);
9103 XSync (FRAME_X_DISPLAY (f), False);
9106 /* Change of visibility. */
9108 /* This tries to wait until the frame is really visible.
9109 However, if the window manager asks the user where to position
9110 the frame, this will return before the user finishes doing that.
9111 The frame will not actually be visible at that time,
9112 but it will become visible later when the window manager
9113 finishes with it. */
9115 void
9116 x_make_frame_visible (struct frame *f)
9118 Lisp_Object type;
9119 int original_top, original_left;
9120 int retry_count = 2;
9122 retry:
9124 block_input ();
9126 type = x_icon_type (f);
9127 if (!NILP (type))
9128 x_bitmap_icon (f, type);
9130 if (! FRAME_VISIBLE_P (f))
9132 /* We test FRAME_GARBAGED_P here to make sure we don't
9133 call x_set_offset a second time
9134 if we get to x_make_frame_visible a second time
9135 before the window gets really visible. */
9136 if (! FRAME_ICONIFIED_P (f)
9137 && ! FRAME_X_EMBEDDED_P (f)
9138 && ! f->output_data.x->asked_for_visible)
9139 x_set_offset (f, f->left_pos, f->top_pos, 0);
9141 f->output_data.x->asked_for_visible = 1;
9143 if (! EQ (Vx_no_window_manager, Qt))
9144 x_wm_set_window_state (f, NormalState);
9145 #ifdef USE_X_TOOLKIT
9146 if (FRAME_X_EMBEDDED_P (f))
9147 xembed_set_info (f, XEMBED_MAPPED);
9148 else
9150 /* This was XtPopup, but that did nothing for an iconified frame. */
9151 XtMapWidget (f->output_data.x->widget);
9153 #else /* not USE_X_TOOLKIT */
9154 #ifdef USE_GTK
9155 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9156 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9157 #else
9158 if (FRAME_X_EMBEDDED_P (f))
9159 xembed_set_info (f, XEMBED_MAPPED);
9160 else
9161 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9162 #endif /* not USE_GTK */
9163 #endif /* not USE_X_TOOLKIT */
9166 XFlush (FRAME_X_DISPLAY (f));
9168 /* Synchronize to ensure Emacs knows the frame is visible
9169 before we do anything else. We do this loop with input not blocked
9170 so that incoming events are handled. */
9172 Lisp_Object frame;
9173 int count;
9174 /* This must be before UNBLOCK_INPUT
9175 since events that arrive in response to the actions above
9176 will set it when they are handled. */
9177 int previously_visible = f->output_data.x->has_been_visible;
9179 original_left = f->left_pos;
9180 original_top = f->top_pos;
9182 /* This must come after we set COUNT. */
9183 unblock_input ();
9185 /* We unblock here so that arriving X events are processed. */
9187 /* Now move the window back to where it was "supposed to be".
9188 But don't do it if the gravity is negative.
9189 When the gravity is negative, this uses a position
9190 that is 3 pixels too low. Perhaps that's really the border width.
9192 Don't do this if the window has never been visible before,
9193 because the window manager may choose the position
9194 and we don't want to override it. */
9196 if (! FRAME_VISIBLE_P (f)
9197 && ! FRAME_ICONIFIED_P (f)
9198 && ! FRAME_X_EMBEDDED_P (f)
9199 && f->win_gravity == NorthWestGravity
9200 && previously_visible)
9202 Drawable rootw;
9203 int x, y;
9204 unsigned int width, height, border, depth;
9206 block_input ();
9208 /* On some window managers (such as FVWM) moving an existing
9209 window, even to the same place, causes the window manager
9210 to introduce an offset. This can cause the window to move
9211 to an unexpected location. Check the geometry (a little
9212 slow here) and then verify that the window is in the right
9213 place. If the window is not in the right place, move it
9214 there, and take the potential window manager hit. */
9215 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9216 &rootw, &x, &y, &width, &height, &border, &depth);
9218 if (original_left != x || original_top != y)
9219 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9220 original_left, original_top);
9222 unblock_input ();
9225 XSETFRAME (frame, f);
9227 /* Wait until the frame is visible. Process X events until a
9228 MapNotify event has been seen, or until we think we won't get a
9229 MapNotify at all.. */
9230 for (count = input_signal_count + 10;
9231 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9233 /* Force processing of queued events. */
9234 x_sync (f);
9236 /* Machines that do polling rather than SIGIO have been
9237 observed to go into a busy-wait here. So we'll fake an
9238 alarm signal to let the handler know that there's something
9239 to be read. We used to raise a real alarm, but it seems
9240 that the handler isn't always enabled here. This is
9241 probably a bug. */
9242 if (input_polling_used ())
9244 /* It could be confusing if a real alarm arrives while
9245 processing the fake one. Turn it off and let the
9246 handler reset it. */
9247 int old_poll_suppress_count = poll_suppress_count;
9248 poll_suppress_count = 1;
9249 poll_for_input_1 ();
9250 poll_suppress_count = old_poll_suppress_count;
9254 /* 2000-09-28: In
9256 (let ((f (selected-frame)))
9257 (iconify-frame f)
9258 (raise-frame f))
9260 the frame is not raised with various window managers on
9261 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9262 unknown reason, the call to XtMapWidget is completely ignored.
9263 Mapping the widget a second time works. */
9265 if (!FRAME_VISIBLE_P (f) && --retry_count != 0)
9266 goto retry;
9270 /* Change from mapped state to withdrawn state. */
9272 /* Make the frame visible (mapped and not iconified). */
9274 void
9275 x_make_frame_invisible (struct frame *f)
9277 Window window;
9279 /* Use the frame's outermost window, not the one we normally draw on. */
9280 window = FRAME_OUTER_WINDOW (f);
9282 /* Don't keep the highlight on an invisible frame. */
9283 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9284 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9286 block_input ();
9288 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9289 that the current position of the window is user-specified, rather than
9290 program-specified, so that when the window is mapped again, it will be
9291 placed at the same location, without forcing the user to position it
9292 by hand again (they have already done that once for this window.) */
9293 x_wm_set_size_hint (f, (long) 0, 1);
9295 #ifdef USE_GTK
9296 if (FRAME_GTK_OUTER_WIDGET (f))
9297 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9298 else
9299 #else
9300 if (FRAME_X_EMBEDDED_P (f))
9301 xembed_set_info (f, 0);
9302 else
9303 #endif
9306 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9307 DefaultScreen (FRAME_X_DISPLAY (f))))
9309 unblock_input ();
9310 error ("Can't notify window manager of window withdrawal");
9314 /* We can't distinguish this from iconification
9315 just by the event that we get from the server.
9316 So we can't win using the usual strategy of letting
9317 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9318 and synchronize with the server to make sure we agree. */
9319 SET_FRAME_VISIBLE (f, 0);
9320 SET_FRAME_ICONIFIED (f, 0);
9322 x_sync (f);
9324 unblock_input ();
9327 /* Change window state from mapped to iconified. */
9329 void
9330 x_iconify_frame (struct frame *f)
9332 #ifdef USE_X_TOOLKIT
9333 int result;
9334 #endif
9335 Lisp_Object type;
9337 /* Don't keep the highlight on an invisible frame. */
9338 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9339 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9341 if (FRAME_ICONIFIED_P (f))
9342 return;
9344 block_input ();
9346 type = x_icon_type (f);
9347 if (!NILP (type))
9348 x_bitmap_icon (f, type);
9350 #if defined (USE_GTK)
9351 if (FRAME_GTK_OUTER_WIDGET (f))
9353 if (! FRAME_VISIBLE_P (f))
9354 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9356 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9357 SET_FRAME_VISIBLE (f, 0);
9358 SET_FRAME_ICONIFIED (f, 1);
9359 unblock_input ();
9360 return;
9362 #endif
9364 #ifdef USE_X_TOOLKIT
9366 if (! FRAME_VISIBLE_P (f))
9368 if (! EQ (Vx_no_window_manager, Qt))
9369 x_wm_set_window_state (f, IconicState);
9370 /* This was XtPopup, but that did nothing for an iconified frame. */
9371 XtMapWidget (f->output_data.x->widget);
9372 /* The server won't give us any event to indicate
9373 that an invisible frame was changed to an icon,
9374 so we have to record it here. */
9375 SET_FRAME_VISIBLE (f, 0);
9376 SET_FRAME_ICONIFIED (f, 1);
9377 unblock_input ();
9378 return;
9381 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9382 XtWindow (f->output_data.x->widget),
9383 DefaultScreen (FRAME_X_DISPLAY (f)));
9384 unblock_input ();
9386 if (!result)
9387 error ("Can't notify window manager of iconification");
9389 SET_FRAME_ICONIFIED (f, 1);
9390 SET_FRAME_VISIBLE (f, 0);
9392 block_input ();
9393 XFlush (FRAME_X_DISPLAY (f));
9394 unblock_input ();
9395 #else /* not USE_X_TOOLKIT */
9397 /* Make sure the X server knows where the window should be positioned,
9398 in case the user deiconifies with the window manager. */
9399 if (! FRAME_VISIBLE_P (f)
9400 && ! FRAME_ICONIFIED_P (f)
9401 && ! FRAME_X_EMBEDDED_P (f))
9402 x_set_offset (f, f->left_pos, f->top_pos, 0);
9404 /* Since we don't know which revision of X we're running, we'll use both
9405 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9407 /* X11R4: send a ClientMessage to the window manager using the
9408 WM_CHANGE_STATE type. */
9410 XEvent msg;
9412 msg.xclient.window = FRAME_X_WINDOW (f);
9413 msg.xclient.type = ClientMessage;
9414 msg.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9415 msg.xclient.format = 32;
9416 msg.xclient.data.l[0] = IconicState;
9418 if (! XSendEvent (FRAME_X_DISPLAY (f),
9419 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9420 False,
9421 SubstructureRedirectMask | SubstructureNotifyMask,
9422 &msg))
9424 unblock_input ();
9425 error ("Can't notify window manager of iconification");
9429 /* X11R3: set the initial_state field of the window manager hints to
9430 IconicState. */
9431 x_wm_set_window_state (f, IconicState);
9433 if (!FRAME_VISIBLE_P (f))
9435 /* If the frame was withdrawn, before, we must map it. */
9436 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9439 SET_FRAME_ICONIFIED (f, 1);
9440 SET_FRAME_VISIBLE (f, 0);
9442 XFlush (FRAME_X_DISPLAY (f));
9443 unblock_input ();
9444 #endif /* not USE_X_TOOLKIT */
9448 /* Free X resources of frame F. */
9450 void
9451 x_free_frame_resources (struct frame *f)
9453 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9454 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
9455 #ifdef USE_X_TOOLKIT
9456 Lisp_Object bar;
9457 struct scroll_bar *b;
9458 #endif
9460 block_input ();
9462 /* If a display connection is dead, don't try sending more
9463 commands to the X server. */
9464 if (dpyinfo->display)
9466 /* We must free faces before destroying windows because some
9467 font-driver (e.g. xft) access a window while finishing a
9468 face. */
9469 if (FRAME_FACE_CACHE (f))
9470 free_frame_faces (f);
9472 if (f->output_data.x->icon_desc)
9473 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9475 #ifdef USE_X_TOOLKIT
9476 /* Explicitly destroy the scroll bars of the frame. Without
9477 this, we get "BadDrawable" errors from the toolkit later on,
9478 presumably from expose events generated for the disappearing
9479 toolkit scroll bars. */
9480 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9482 b = XSCROLL_BAR (bar);
9483 x_scroll_bar_remove (b);
9485 #endif
9487 #ifdef HAVE_X_I18N
9488 if (FRAME_XIC (f))
9489 free_frame_xic (f);
9490 #endif
9492 #ifdef USE_X_TOOLKIT
9493 if (f->output_data.x->widget)
9495 XtDestroyWidget (f->output_data.x->widget);
9496 f->output_data.x->widget = NULL;
9498 /* Tooltips don't have widgets, only a simple X window, even if
9499 we are using a toolkit. */
9500 else if (FRAME_X_WINDOW (f))
9501 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9503 free_frame_menubar (f);
9504 #else /* !USE_X_TOOLKIT */
9506 #ifdef USE_GTK
9507 xg_free_frame_widgets (f);
9508 #endif /* USE_GTK */
9510 if (FRAME_X_WINDOW (f))
9511 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9512 #endif /* !USE_X_TOOLKIT */
9514 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9515 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9516 unload_color (f, f->output_data.x->cursor_pixel);
9517 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9518 unload_color (f, f->output_data.x->border_pixel);
9519 unload_color (f, f->output_data.x->mouse_pixel);
9521 if (f->output_data.x->scroll_bar_background_pixel != -1)
9522 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9523 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9524 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9525 #ifdef USE_TOOLKIT_SCROLL_BARS
9526 /* Scrollbar shadow colors. */
9527 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9528 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9529 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9530 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9531 #endif /* USE_TOOLKIT_SCROLL_BARS */
9532 if (f->output_data.x->white_relief.allocated_p)
9533 unload_color (f, f->output_data.x->white_relief.pixel);
9534 if (f->output_data.x->black_relief.allocated_p)
9535 unload_color (f, f->output_data.x->black_relief.pixel);
9537 x_free_gcs (f);
9539 /* Free extra GCs allocated by x_setup_relief_colors. */
9540 if (f->output_data.x->white_relief.gc)
9542 XFreeGC (dpyinfo->display, f->output_data.x->white_relief.gc);
9543 f->output_data.x->white_relief.gc = 0;
9545 if (f->output_data.x->black_relief.gc)
9547 XFreeGC (dpyinfo->display, f->output_data.x->black_relief.gc);
9548 f->output_data.x->black_relief.gc = 0;
9551 XFlush (FRAME_X_DISPLAY (f));
9554 xfree (f->output_data.x->saved_menu_event);
9555 xfree (f->output_data.x);
9556 f->output_data.x = NULL;
9558 if (f == dpyinfo->x_focus_frame)
9559 dpyinfo->x_focus_frame = 0;
9560 if (f == dpyinfo->x_focus_event_frame)
9561 dpyinfo->x_focus_event_frame = 0;
9562 if (f == dpyinfo->x_highlight_frame)
9563 dpyinfo->x_highlight_frame = 0;
9564 if (f == hlinfo->mouse_face_mouse_frame)
9565 reset_mouse_highlight (hlinfo);
9567 unblock_input ();
9571 /* Destroy the X window of frame F. */
9573 static void
9574 x_destroy_window (struct frame *f)
9576 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9578 /* If a display connection is dead, don't try sending more
9579 commands to the X server. */
9580 if (dpyinfo->display != 0)
9581 x_free_frame_resources (f);
9583 dpyinfo->reference_count--;
9587 /* Setting window manager hints. */
9589 /* Set the normal size hints for the window manager, for frame F.
9590 FLAGS is the flags word to use--or 0 meaning preserve the flags
9591 that the window now has.
9592 If USER_POSITION, set the USPosition
9593 flag (this is useful when FLAGS is 0).
9594 The GTK version is in gtkutils.c. */
9596 #ifndef USE_GTK
9597 void
9598 x_wm_set_size_hint (struct frame *f, long flags, bool user_position)
9600 XSizeHints size_hints;
9601 Window window = FRAME_OUTER_WINDOW (f);
9603 #ifdef USE_X_TOOLKIT
9604 if (f->output_data.x->widget)
9606 widget_update_wm_size_hints (f->output_data.x->widget);
9607 return;
9609 #endif
9611 /* Setting PMaxSize caused various problems. */
9612 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9614 size_hints.x = f->left_pos;
9615 size_hints.y = f->top_pos;
9617 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9618 size_hints.width = FRAME_PIXEL_WIDTH (f);
9620 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9621 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9622 size_hints.max_width = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
9623 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9624 size_hints.max_height = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
9625 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9627 /* Calculate the base and minimum sizes. */
9629 int base_width, base_height;
9630 int min_rows = 0, min_cols = 0;
9632 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9633 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9635 check_frame_size (f, &min_rows, &min_cols);
9637 /* The window manager uses the base width hints to calculate the
9638 current number of rows and columns in the frame while
9639 resizing; min_width and min_height aren't useful for this
9640 purpose, since they might not give the dimensions for a
9641 zero-row, zero-column frame.
9643 We use the base_width and base_height members if we have
9644 them; otherwise, we set the min_width and min_height members
9645 to the size for a zero x zero frame. */
9647 size_hints.flags |= PBaseSize;
9648 size_hints.base_width = base_width;
9649 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
9650 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9651 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9654 /* If we don't need the old flags, we don't need the old hint at all. */
9655 if (flags)
9657 size_hints.flags |= flags;
9658 goto no_read;
9662 XSizeHints hints; /* Sometimes I hate X Windows... */
9663 long supplied_return;
9664 int value;
9666 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9667 &supplied_return);
9669 if (flags)
9670 size_hints.flags |= flags;
9671 else
9673 if (value == 0)
9674 hints.flags = 0;
9675 if (hints.flags & PSize)
9676 size_hints.flags |= PSize;
9677 if (hints.flags & PPosition)
9678 size_hints.flags |= PPosition;
9679 if (hints.flags & USPosition)
9680 size_hints.flags |= USPosition;
9681 if (hints.flags & USSize)
9682 size_hints.flags |= USSize;
9686 no_read:
9688 #ifdef PWinGravity
9689 size_hints.win_gravity = f->win_gravity;
9690 size_hints.flags |= PWinGravity;
9692 if (user_position)
9694 size_hints.flags &= ~ PPosition;
9695 size_hints.flags |= USPosition;
9697 #endif /* PWinGravity */
9699 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9701 #endif /* not USE_GTK */
9703 /* Used for IconicState or NormalState */
9705 static void
9706 x_wm_set_window_state (struct frame *f, int state)
9708 #ifdef USE_X_TOOLKIT
9709 Arg al[1];
9711 XtSetArg (al[0], XtNinitialState, state);
9712 XtSetValues (f->output_data.x->widget, al, 1);
9713 #else /* not USE_X_TOOLKIT */
9714 Window window = FRAME_X_WINDOW (f);
9716 f->output_data.x->wm_hints.flags |= StateHint;
9717 f->output_data.x->wm_hints.initial_state = state;
9719 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9720 #endif /* not USE_X_TOOLKIT */
9723 static void
9724 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
9726 Pixmap icon_pixmap, icon_mask;
9728 #if !defined USE_X_TOOLKIT && !defined USE_GTK
9729 Window window = FRAME_OUTER_WINDOW (f);
9730 #endif
9732 if (pixmap_id > 0)
9734 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9735 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9736 icon_mask = x_bitmap_mask (f, pixmap_id);
9737 f->output_data.x->wm_hints.icon_mask = icon_mask;
9739 else
9741 /* It seems there is no way to turn off use of an icon
9742 pixmap. */
9743 return;
9747 #ifdef USE_GTK
9749 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9750 return;
9753 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9756 Arg al[1];
9757 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9758 XtSetValues (f->output_data.x->widget, al, 1);
9759 XtSetArg (al[0], XtNiconMask, icon_mask);
9760 XtSetValues (f->output_data.x->widget, al, 1);
9763 #else /* not USE_X_TOOLKIT && not USE_GTK */
9765 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9766 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9768 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9771 void
9772 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
9774 Window window = FRAME_OUTER_WINDOW (f);
9776 f->output_data.x->wm_hints.flags |= IconPositionHint;
9777 f->output_data.x->wm_hints.icon_x = icon_x;
9778 f->output_data.x->wm_hints.icon_y = icon_y;
9780 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9784 /***********************************************************************
9785 Fonts
9786 ***********************************************************************/
9788 #ifdef GLYPH_DEBUG
9790 /* Check that FONT is valid on frame F. It is if it can be found in F's
9791 font table. */
9793 static void
9794 x_check_font (struct frame *f, struct font *font)
9796 eassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9797 if (font->driver->check)
9798 eassert (font->driver->check (f, font) == 0);
9801 #endif /* GLYPH_DEBUG */
9804 /***********************************************************************
9805 Initialization
9806 ***********************************************************************/
9808 #ifdef USE_X_TOOLKIT
9809 static XrmOptionDescRec emacs_options[] = {
9810 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9811 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9813 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9814 XrmoptionSepArg, NULL},
9815 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9817 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9818 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9819 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9820 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9821 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9822 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9823 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9826 /* Whether atimer for Xt timeouts is activated or not. */
9828 static int x_timeout_atimer_activated_flag;
9830 #endif /* USE_X_TOOLKIT */
9832 static int x_initialized;
9834 /* Test whether two display-name strings agree up to the dot that separates
9835 the screen number from the server number. */
9836 static int
9837 same_x_server (const char *name1, const char *name2)
9839 int seen_colon = 0;
9840 const char *system_name = SSDATA (Vsystem_name);
9841 ptrdiff_t system_name_length = SBYTES (Vsystem_name);
9842 ptrdiff_t length_until_period = 0;
9844 while (system_name[length_until_period] != 0
9845 && system_name[length_until_period] != '.')
9846 length_until_period++;
9848 /* Treat `unix' like an empty host name. */
9849 if (! strncmp (name1, "unix:", 5))
9850 name1 += 4;
9851 if (! strncmp (name2, "unix:", 5))
9852 name2 += 4;
9853 /* Treat this host's name like an empty host name. */
9854 if (! strncmp (name1, system_name, system_name_length)
9855 && name1[system_name_length] == ':')
9856 name1 += system_name_length;
9857 if (! strncmp (name2, system_name, system_name_length)
9858 && name2[system_name_length] == ':')
9859 name2 += system_name_length;
9860 /* Treat this host's domainless name like an empty host name. */
9861 if (! strncmp (name1, system_name, length_until_period)
9862 && name1[length_until_period] == ':')
9863 name1 += length_until_period;
9864 if (! strncmp (name2, system_name, length_until_period)
9865 && name2[length_until_period] == ':')
9866 name2 += length_until_period;
9868 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
9870 if (*name1 == ':')
9871 seen_colon = 1;
9872 if (seen_colon && *name1 == '.')
9873 return 1;
9875 return (seen_colon
9876 && (*name1 == '.' || *name1 == '\0')
9877 && (*name2 == '.' || *name2 == '\0'));
9880 /* Count number of set bits in mask and number of bits to shift to
9881 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
9882 to 5. */
9883 static void
9884 get_bits_and_offset (long unsigned int mask, int *bits, int *offset)
9886 int nr = 0;
9887 int off = 0;
9889 while (!(mask & 1))
9891 off++;
9892 mask >>= 1;
9895 while (mask & 1)
9897 nr++;
9898 mask >>= 1;
9901 *offset = off;
9902 *bits = nr;
9905 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
9906 But don't permanently open it, just test its availability. */
9908 bool
9909 x_display_ok (const char *display)
9911 Display *dpy = XOpenDisplay (display);
9912 return dpy ? (XCloseDisplay (dpy), 1) : 0;
9915 #ifdef USE_GTK
9916 static void
9917 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
9918 const gchar *msg, gpointer user_data)
9920 if (!strstr (msg, "g_set_prgname"))
9921 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
9923 #endif
9925 /* Open a connection to X display DISPLAY_NAME, and return
9926 the structure that describes the open display.
9927 If we cannot contact the display, return null. */
9929 struct x_display_info *
9930 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
9932 int connection;
9933 Display *dpy;
9934 struct terminal *terminal;
9935 struct x_display_info *dpyinfo;
9936 XrmDatabase xrdb;
9937 ptrdiff_t lim;
9939 block_input ();
9941 if (!x_initialized)
9943 x_initialize ();
9944 ++x_initialized;
9947 if (! x_display_ok (SSDATA (display_name)))
9948 error ("Display %s can't be opened", SSDATA (display_name));
9950 #ifdef USE_GTK
9952 #define NUM_ARGV 10
9953 int argc;
9954 char *argv[NUM_ARGV];
9955 char **argv2 = argv;
9956 guint id;
9958 if (x_initialized++ > 1)
9960 xg_display_open (SSDATA (display_name), &dpy);
9962 else
9964 static char display_opt[] = "--display";
9965 static char name_opt[] = "--name";
9967 for (argc = 0; argc < NUM_ARGV; ++argc)
9968 argv[argc] = 0;
9970 argc = 0;
9971 argv[argc++] = initial_argv[0];
9973 if (! NILP (display_name))
9975 argv[argc++] = display_opt;
9976 argv[argc++] = SSDATA (display_name);
9979 argv[argc++] = name_opt;
9980 argv[argc++] = resource_name;
9982 XSetLocaleModifiers ("");
9984 /* Emacs can only handle core input events, so make sure
9985 Gtk doesn't use Xinput or Xinput2 extensions. */
9986 xputenv ("GDK_CORE_DEVICE_EVENTS=1");
9988 /* Work around GLib bug that outputs a faulty warning. See
9989 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
9990 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
9991 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
9992 #ifdef HAVE_CLUTTER
9993 gtk_clutter_init (&argc, &argv2);
9994 #else
9996 /* NULL window -> events for all windows go to our function.
9997 Call before gtk_init so Gtk+ event filters comes after our. */
9998 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
10000 /* gtk_init does set_locale. Fix locale before and after. */
10001 fixup_locale ();
10002 gtk_init (&argc, &argv2);
10003 #endif
10004 fixup_locale ();
10006 g_log_remove_handler ("GLib", id);
10008 xg_initialize ();
10010 dpy = DEFAULT_GDK_DISPLAY ();
10012 #if ! GTK_CHECK_VERSION (2, 90, 0)
10013 /* Load our own gtkrc if it exists. */
10015 const char *file = "~/.emacs.d/gtkrc";
10016 Lisp_Object s, abs_file;
10018 s = build_string (file);
10019 abs_file = Fexpand_file_name (s, Qnil);
10021 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
10022 gtk_rc_parse (SSDATA (abs_file));
10024 #endif
10026 XSetErrorHandler (x_error_handler);
10027 XSetIOErrorHandler (x_io_error_quitter);
10030 #else /* not USE_GTK */
10031 #ifdef USE_X_TOOLKIT
10032 /* weiner@footloose.sps.mot.com reports that this causes
10033 errors with X11R5:
10034 X protocol error: BadAtom (invalid Atom parameter)
10035 on protocol request 18skiloaf.
10036 So let's not use it until R6. */
10037 #ifdef HAVE_X11XTR6
10038 XtSetLanguageProc (NULL, NULL, NULL);
10039 #endif
10042 int argc = 0;
10043 char *argv[3];
10045 argv[0] = "";
10046 argc = 1;
10047 if (xrm_option)
10049 argv[argc++] = "-xrm";
10050 argv[argc++] = xrm_option;
10052 turn_on_atimers (0);
10053 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
10054 resource_name, EMACS_CLASS,
10055 emacs_options, XtNumber (emacs_options),
10056 &argc, argv);
10057 turn_on_atimers (1);
10059 #ifdef HAVE_X11XTR6
10060 /* I think this is to compensate for XtSetLanguageProc. */
10061 fixup_locale ();
10062 #endif
10065 #else /* not USE_X_TOOLKIT */
10066 XSetLocaleModifiers ("");
10067 dpy = XOpenDisplay (SSDATA (display_name));
10068 #endif /* not USE_X_TOOLKIT */
10069 #endif /* not USE_GTK*/
10071 /* Detect failure. */
10072 if (dpy == 0)
10074 unblock_input ();
10075 return 0;
10078 /* We have definitely succeeded. Record the new connection. */
10080 dpyinfo = xzalloc (sizeof *dpyinfo);
10081 terminal = x_create_terminal (dpyinfo);
10084 struct x_display_info *share;
10085 Lisp_Object tail;
10087 for (share = x_display_list, tail = x_display_name_list; share;
10088 share = share->next, tail = XCDR (tail))
10089 if (same_x_server (SSDATA (XCAR (XCAR (tail))),
10090 SSDATA (display_name)))
10091 break;
10092 if (share)
10093 terminal->kboard = share->terminal->kboard;
10094 else
10096 terminal->kboard = xmalloc (sizeof *terminal->kboard);
10097 init_kboard (terminal->kboard);
10098 kset_window_system (terminal->kboard, Qx);
10100 /* Add the keyboard to the list before running Lisp code (via
10101 Qvendor_specific_keysyms below), since these are not traced
10102 via terminals but only through all_kboards. */
10103 terminal->kboard->next_kboard = all_kboards;
10104 all_kboards = terminal->kboard;
10106 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10108 char *vendor = ServerVendor (dpy);
10110 /* Protect terminal from GC before removing it from the
10111 list of terminals. */
10112 struct gcpro gcpro1;
10113 Lisp_Object gcpro_term;
10114 XSETTERMINAL (gcpro_term, terminal);
10115 GCPRO1 (gcpro_term);
10117 /* Temporarily hide the partially initialized terminal. */
10118 terminal_list = terminal->next_terminal;
10119 unblock_input ();
10120 kset_system_key_alist
10121 (terminal->kboard,
10122 call1 (Qvendor_specific_keysyms,
10123 vendor ? build_string (vendor) : empty_unibyte_string));
10124 block_input ();
10125 terminal->next_terminal = terminal_list;
10126 terminal_list = terminal;
10127 UNGCPRO;
10130 /* Don't let the initial kboard remain current longer than necessary.
10131 That would cause problems if a file loaded on startup tries to
10132 prompt in the mini-buffer. */
10133 if (current_kboard == initial_kboard)
10134 current_kboard = terminal->kboard;
10136 terminal->kboard->reference_count++;
10139 /* Put this display on the chain. */
10140 dpyinfo->next = x_display_list;
10141 x_display_list = dpyinfo;
10143 /* Put it on x_display_name_list as well, to keep them parallel. */
10144 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10145 x_display_name_list);
10146 dpyinfo->name_list_element = XCAR (x_display_name_list);
10148 dpyinfo->display = dpy;
10150 /* Set the name of the terminal. */
10151 terminal->name = xlispstrdup (display_name);
10153 #if 0
10154 XSetAfterFunction (x_current_display, x_trace_wire);
10155 #endif /* ! 0 */
10157 lim = min (PTRDIFF_MAX, SIZE_MAX) - sizeof "@";
10158 if (lim - SBYTES (Vinvocation_name) < SBYTES (Vsystem_name))
10159 memory_full (SIZE_MAX);
10160 dpyinfo->x_id_name = xmalloc (SBYTES (Vinvocation_name)
10161 + SBYTES (Vsystem_name) + 2);
10162 strcat (strcat (strcpy (dpyinfo->x_id_name, SSDATA (Vinvocation_name)), "@"),
10163 SSDATA (Vsystem_name));
10165 /* Figure out which modifier bits mean what. */
10166 x_find_modifier_meanings (dpyinfo);
10168 /* Get the scroll bar cursor. */
10169 #ifdef USE_GTK
10170 /* We must create a GTK cursor, it is required for GTK widgets. */
10171 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10172 #endif /* USE_GTK */
10174 dpyinfo->vertical_scroll_bar_cursor
10175 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10177 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10178 resource_name, EMACS_CLASS);
10179 #ifdef HAVE_XRMSETDATABASE
10180 XrmSetDatabase (dpyinfo->display, xrdb);
10181 #else
10182 dpyinfo->display->db = xrdb;
10183 #endif
10184 /* Put the rdb where we can find it in a way that works on
10185 all versions. */
10186 dpyinfo->xrdb = xrdb;
10188 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10189 DefaultScreen (dpyinfo->display));
10190 select_visual (dpyinfo);
10191 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10192 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10193 dpyinfo->icon_bitmap_id = -1;
10194 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10196 reset_mouse_highlight (&dpyinfo->mouse_highlight);
10198 /* See if we can construct pixel values from RGB values. */
10199 if (dpyinfo->visual->class == TrueColor)
10201 get_bits_and_offset (dpyinfo->visual->red_mask,
10202 &dpyinfo->red_bits, &dpyinfo->red_offset);
10203 get_bits_and_offset (dpyinfo->visual->blue_mask,
10204 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10205 get_bits_and_offset (dpyinfo->visual->green_mask,
10206 &dpyinfo->green_bits, &dpyinfo->green_offset);
10209 /* See if a private colormap is requested. */
10210 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10212 if (dpyinfo->visual->class == PseudoColor)
10214 Lisp_Object value;
10215 value = display_x_get_resource (dpyinfo,
10216 build_string ("privateColormap"),
10217 build_string ("PrivateColormap"),
10218 Qnil, Qnil);
10219 if (STRINGP (value)
10220 && (!strcmp (SSDATA (value), "true")
10221 || !strcmp (SSDATA (value), "on")))
10222 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10225 else
10226 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10227 dpyinfo->visual, AllocNone);
10229 #ifdef HAVE_XFT
10231 /* If we are using Xft, check dpi value in X resources.
10232 It is better we use it as well, since Xft will use it, as will all
10233 Gnome applications. If our real DPI is smaller or larger than the
10234 one Xft uses, our font will look smaller or larger than other
10235 for other applications, even if it is the same font name (monospace-10
10236 for example). */
10237 char *v = XGetDefault (dpyinfo->display, "Xft", "dpi");
10238 double d;
10239 if (v != NULL && sscanf (v, "%lf", &d) == 1)
10240 dpyinfo->resy = dpyinfo->resx = d;
10242 #endif
10244 if (dpyinfo->resy < 1)
10246 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10247 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10248 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10249 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10250 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10251 pixels = DisplayWidth (dpyinfo->display, screen_number);
10252 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10253 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10254 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10258 static const struct
10260 const char *name;
10261 int offset;
10262 } atom_refs[] = {
10263 #define ATOM_REFS_INIT(string, member) \
10264 { string, offsetof (struct x_display_info, member) },
10265 ATOM_REFS_INIT ("WM_PROTOCOLS", Xatom_wm_protocols)
10266 ATOM_REFS_INIT ("WM_TAKE_FOCUS", Xatom_wm_take_focus)
10267 ATOM_REFS_INIT ("WM_SAVE_YOURSELF", Xatom_wm_save_yourself)
10268 ATOM_REFS_INIT ("WM_DELETE_WINDOW", Xatom_wm_delete_window)
10269 ATOM_REFS_INIT ("WM_CHANGE_STATE", Xatom_wm_change_state)
10270 ATOM_REFS_INIT ("WM_CONFIGURE_DENIED", Xatom_wm_configure_denied)
10271 ATOM_REFS_INIT ("WM_MOVED", Xatom_wm_window_moved)
10272 ATOM_REFS_INIT ("WM_CLIENT_LEADER", Xatom_wm_client_leader)
10273 ATOM_REFS_INIT ("Editres", Xatom_editres)
10274 ATOM_REFS_INIT ("CLIPBOARD", Xatom_CLIPBOARD)
10275 ATOM_REFS_INIT ("TIMESTAMP", Xatom_TIMESTAMP)
10276 ATOM_REFS_INIT ("TEXT", Xatom_TEXT)
10277 ATOM_REFS_INIT ("COMPOUND_TEXT", Xatom_COMPOUND_TEXT)
10278 ATOM_REFS_INIT ("UTF8_STRING", Xatom_UTF8_STRING)
10279 ATOM_REFS_INIT ("DELETE", Xatom_DELETE)
10280 ATOM_REFS_INIT ("MULTIPLE", Xatom_MULTIPLE)
10281 ATOM_REFS_INIT ("INCR", Xatom_INCR)
10282 ATOM_REFS_INIT ("_EMACS_TMP_", Xatom_EMACS_TMP)
10283 ATOM_REFS_INIT ("TARGETS", Xatom_TARGETS)
10284 ATOM_REFS_INIT ("NULL", Xatom_NULL)
10285 ATOM_REFS_INIT ("ATOM", Xatom_ATOM)
10286 ATOM_REFS_INIT ("ATOM_PAIR", Xatom_ATOM_PAIR)
10287 ATOM_REFS_INIT ("CLIPBOARD_MANAGER", Xatom_CLIPBOARD_MANAGER)
10288 ATOM_REFS_INIT ("_XEMBED_INFO", Xatom_XEMBED_INFO)
10289 /* For properties of font. */
10290 ATOM_REFS_INIT ("PIXEL_SIZE", Xatom_PIXEL_SIZE)
10291 ATOM_REFS_INIT ("AVERAGE_WIDTH", Xatom_AVERAGE_WIDTH)
10292 ATOM_REFS_INIT ("_MULE_BASELINE_OFFSET", Xatom_MULE_BASELINE_OFFSET)
10293 ATOM_REFS_INIT ("_MULE_RELATIVE_COMPOSE", Xatom_MULE_RELATIVE_COMPOSE)
10294 ATOM_REFS_INIT ("_MULE_DEFAULT_ASCENT", Xatom_MULE_DEFAULT_ASCENT)
10295 /* Ghostscript support. */
10296 ATOM_REFS_INIT ("DONE", Xatom_DONE)
10297 ATOM_REFS_INIT ("PAGE", Xatom_PAGE)
10298 ATOM_REFS_INIT ("SCROLLBAR", Xatom_Scrollbar)
10299 ATOM_REFS_INIT ("_XEMBED", Xatom_XEMBED)
10300 /* EWMH */
10301 ATOM_REFS_INIT ("_NET_WM_STATE", Xatom_net_wm_state)
10302 ATOM_REFS_INIT ("_NET_WM_STATE_FULLSCREEN", Xatom_net_wm_state_fullscreen)
10303 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_HORZ",
10304 Xatom_net_wm_state_maximized_horz)
10305 ATOM_REFS_INIT ("_NET_WM_STATE_MAXIMIZED_VERT",
10306 Xatom_net_wm_state_maximized_vert)
10307 ATOM_REFS_INIT ("_NET_WM_STATE_STICKY", Xatom_net_wm_state_sticky)
10308 ATOM_REFS_INIT ("_NET_WM_STATE_HIDDEN", Xatom_net_wm_state_hidden)
10309 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE", Xatom_net_window_type)
10310 ATOM_REFS_INIT ("_NET_WM_WINDOW_TYPE_TOOLTIP",
10311 Xatom_net_window_type_tooltip)
10312 ATOM_REFS_INIT ("_NET_WM_ICON_NAME", Xatom_net_wm_icon_name)
10313 ATOM_REFS_INIT ("_NET_WM_NAME", Xatom_net_wm_name)
10314 ATOM_REFS_INIT ("_NET_SUPPORTED", Xatom_net_supported)
10315 ATOM_REFS_INIT ("_NET_SUPPORTING_WM_CHECK", Xatom_net_supporting_wm_check)
10316 ATOM_REFS_INIT ("_NET_WM_WINDOW_OPACITY", Xatom_net_wm_window_opacity)
10317 ATOM_REFS_INIT ("_NET_ACTIVE_WINDOW", Xatom_net_active_window)
10318 ATOM_REFS_INIT ("_NET_FRAME_EXTENTS", Xatom_net_frame_extents)
10319 ATOM_REFS_INIT ("_NET_CURRENT_DESKTOP", Xatom_net_current_desktop)
10320 ATOM_REFS_INIT ("_NET_WORKAREA", Xatom_net_workarea)
10321 /* Session management */
10322 ATOM_REFS_INIT ("SM_CLIENT_ID", Xatom_SM_CLIENT_ID)
10323 ATOM_REFS_INIT ("_XSETTINGS_SETTINGS", Xatom_xsettings_prop)
10324 ATOM_REFS_INIT ("MANAGER", Xatom_xsettings_mgr)
10327 int i;
10328 const int atom_count = sizeof (atom_refs) / sizeof (atom_refs[0]);
10329 /* 1 for _XSETTINGS_SN */
10330 const int total_atom_count = 1 + atom_count;
10331 Atom *atoms_return = xmalloc (total_atom_count * sizeof *atoms_return);
10332 char **atom_names = xmalloc (total_atom_count * sizeof *atom_names);
10333 static char const xsettings_fmt[] = "_XSETTINGS_S%d";
10334 char xsettings_atom_name[sizeof xsettings_fmt - 2
10335 + INT_STRLEN_BOUND (int)];
10337 for (i = 0; i < atom_count; i++)
10338 atom_names[i] = (char *) atom_refs[i].name;
10340 /* Build _XSETTINGS_SN atom name */
10341 sprintf (xsettings_atom_name, xsettings_fmt,
10342 XScreenNumberOfScreen (dpyinfo->screen));
10343 atom_names[i] = xsettings_atom_name;
10345 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
10346 False, atoms_return);
10348 for (i = 0; i < atom_count; i++)
10349 *(Atom *) ((char *) dpyinfo + atom_refs[i].offset) = atoms_return[i];
10351 /* Manual copy of last atom */
10352 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
10354 xfree (atom_names);
10355 xfree (atoms_return);
10358 dpyinfo->x_dnd_atoms_size = 8;
10359 dpyinfo->x_dnd_atoms = xmalloc (sizeof *dpyinfo->x_dnd_atoms
10360 * dpyinfo->x_dnd_atoms_size);
10361 dpyinfo->gray
10362 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10363 gray_bits, gray_width, gray_height,
10364 1, 0, 1);
10366 #ifdef HAVE_X_I18N
10367 xim_initialize (dpyinfo, resource_name);
10368 #endif
10370 xsettings_initialize (dpyinfo);
10372 connection = ConnectionNumber (dpyinfo->display);
10374 /* This is only needed for distinguishing keyboard and process input. */
10375 if (connection != 0)
10376 add_keyboard_wait_descriptor (connection);
10378 #ifdef F_SETOWN
10379 fcntl (connection, F_SETOWN, getpid ());
10380 #endif /* ! defined (F_SETOWN) */
10382 if (interrupt_input)
10383 init_sigio (connection);
10385 #ifdef USE_LUCID
10387 XrmValue d, fr, to;
10388 Font font;
10390 dpy = dpyinfo->display;
10391 d.addr = (XPointer)&dpy;
10392 d.size = sizeof (Display *);
10393 fr.addr = XtDefaultFont;
10394 fr.size = sizeof (XtDefaultFont);
10395 to.size = sizeof (Font *);
10396 to.addr = (XPointer)&font;
10397 x_catch_errors (dpy);
10398 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10399 emacs_abort ();
10400 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10401 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10402 x_uncatch_errors ();
10404 #endif
10406 /* See if we should run in synchronous mode. This is useful
10407 for debugging X code. */
10409 Lisp_Object value;
10410 value = display_x_get_resource (dpyinfo,
10411 build_string ("synchronous"),
10412 build_string ("Synchronous"),
10413 Qnil, Qnil);
10414 if (STRINGP (value)
10415 && (!strcmp (SSDATA (value), "true")
10416 || !strcmp (SSDATA (value), "on")))
10417 XSynchronize (dpyinfo->display, True);
10421 Lisp_Object value;
10422 value = display_x_get_resource (dpyinfo,
10423 build_string ("useXIM"),
10424 build_string ("UseXIM"),
10425 Qnil, Qnil);
10426 #ifdef USE_XIM
10427 if (STRINGP (value)
10428 && (!strcmp (SSDATA (value), "false")
10429 || !strcmp (SSDATA (value), "off")))
10430 use_xim = 0;
10431 #else
10432 if (STRINGP (value)
10433 && (!strcmp (SSDATA (value), "true")
10434 || !strcmp (SSDATA (value), "on")))
10435 use_xim = 1;
10436 #endif
10439 #ifdef HAVE_X_SM
10440 /* Only do this for the very first display in the Emacs session.
10441 Ignore X session management when Emacs was first started on a
10442 tty. */
10443 if (terminal->id == 1)
10444 x_session_initialize (dpyinfo);
10445 #endif
10447 unblock_input ();
10449 return dpyinfo;
10452 /* Get rid of display DPYINFO, deleting all frames on it,
10453 and without sending any more commands to the X server. */
10455 static void
10456 x_delete_display (struct x_display_info *dpyinfo)
10458 struct terminal *t;
10460 /* Close all frames and delete the generic struct terminal for this
10461 X display. */
10462 for (t = terminal_list; t; t = t->next_terminal)
10463 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10465 #ifdef HAVE_X_SM
10466 /* Close X session management when we close its display. */
10467 if (t->id == 1 && x_session_have_connection ())
10468 x_session_close ();
10469 #endif
10470 delete_terminal (t);
10471 break;
10474 /* Discard this display from x_display_name_list and x_display_list.
10475 We can't use Fdelq because that can quit. */
10476 if (! NILP (x_display_name_list)
10477 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10478 x_display_name_list = XCDR (x_display_name_list);
10479 else
10481 Lisp_Object tail;
10483 tail = x_display_name_list;
10484 while (CONSP (tail) && CONSP (XCDR (tail)))
10486 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10488 XSETCDR (tail, XCDR (XCDR (tail)));
10489 break;
10491 tail = XCDR (tail);
10495 if (next_noop_dpyinfo == dpyinfo)
10496 next_noop_dpyinfo = dpyinfo->next;
10498 if (x_display_list == dpyinfo)
10499 x_display_list = dpyinfo->next;
10500 else
10502 struct x_display_info *tail;
10504 for (tail = x_display_list; tail; tail = tail->next)
10505 if (tail->next == dpyinfo)
10506 tail->next = tail->next->next;
10509 xfree (dpyinfo->x_id_name);
10510 xfree (dpyinfo->x_dnd_atoms);
10511 xfree (dpyinfo->color_cells);
10512 xfree (dpyinfo);
10515 #ifdef USE_X_TOOLKIT
10517 /* Atimer callback function for TIMER. Called every 0.1s to process
10518 Xt timeouts, if needed. We must avoid calling XtAppPending as
10519 much as possible because that function does an implicit XFlush
10520 that slows us down. */
10522 static void
10523 x_process_timeouts (struct atimer *timer)
10525 block_input ();
10526 x_timeout_atimer_activated_flag = 0;
10527 if (toolkit_scroll_bar_interaction || popup_activated ())
10529 while (XtAppPending (Xt_app_con) & XtIMTimer)
10530 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10531 /* Reactivate the atimer for next time. */
10532 x_activate_timeout_atimer ();
10534 unblock_input ();
10537 /* Install an asynchronous timer that processes Xt timeout events
10538 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10539 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10540 function whenever these variables are set. This is necessary
10541 because some widget sets use timeouts internally, for example the
10542 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10543 processed, these widgets don't behave normally. */
10545 void
10546 x_activate_timeout_atimer (void)
10548 block_input ();
10549 if (!x_timeout_atimer_activated_flag)
10551 struct timespec interval = make_timespec (0, 100 * 1000 * 1000);
10552 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10553 x_timeout_atimer_activated_flag = 1;
10555 unblock_input ();
10558 #endif /* USE_X_TOOLKIT */
10561 /* Set up use of X before we make the first connection. */
10563 static struct redisplay_interface x_redisplay_interface =
10565 x_frame_parm_handlers,
10566 x_produce_glyphs,
10567 x_write_glyphs,
10568 x_insert_glyphs,
10569 x_clear_end_of_line,
10570 x_scroll_run,
10571 x_after_update_window_line,
10572 x_update_window_begin,
10573 x_update_window_end,
10574 x_flush,
10575 x_clear_window_mouse_face,
10576 x_get_glyph_overhangs,
10577 x_fix_overlapping_area,
10578 x_draw_fringe_bitmap,
10579 0, /* define_fringe_bitmap */
10580 0, /* destroy_fringe_bitmap */
10581 x_compute_glyph_string_overhangs,
10582 x_draw_glyph_string,
10583 x_define_frame_cursor,
10584 x_clear_frame_area,
10585 x_draw_window_cursor,
10586 x_draw_vertical_window_border,
10587 x_shift_glyphs_for_insert
10591 /* This function is called when the last frame on a display is deleted. */
10592 void
10593 x_delete_terminal (struct terminal *terminal)
10595 struct x_display_info *dpyinfo = terminal->display_info.x;
10596 int connection = -1;
10598 /* Protect against recursive calls. delete_frame in
10599 delete_terminal calls us back when it deletes our last frame. */
10600 if (!terminal->name)
10601 return;
10603 block_input ();
10604 #ifdef HAVE_X_I18N
10605 /* We must close our connection to the XIM server before closing the
10606 X display. */
10607 if (dpyinfo->xim)
10608 xim_close_dpy (dpyinfo);
10609 #endif
10611 /* If called from x_connection_closed, the display may already be closed
10612 and dpyinfo->display was set to 0 to indicate that. */
10613 if (dpyinfo->display)
10615 connection = ConnectionNumber (dpyinfo->display);
10617 x_destroy_all_bitmaps (dpyinfo);
10618 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10620 /* Whether or not XCloseDisplay destroys the associated resource
10621 database depends on the version of libX11. To avoid both
10622 crash and memory leak, we dissociate the database from the
10623 display and then destroy dpyinfo->xrdb ourselves.
10625 Unfortunately, the above strategy does not work in some
10626 situations due to a bug in newer versions of libX11: because
10627 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
10628 dpy->db is NULL, XCloseDisplay destroys the associated
10629 database whereas it has not been created by XGetDefault
10630 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
10631 don't destroy the database here in order to avoid the crash
10632 in the above situations for now, though that may cause memory
10633 leaks in other situations. */
10634 #if 0
10635 #ifdef HAVE_XRMSETDATABASE
10636 XrmSetDatabase (dpyinfo->display, NULL);
10637 #else
10638 dpyinfo->display->db = NULL;
10639 #endif
10640 /* We used to call XrmDestroyDatabase from x_delete_display, but
10641 some older versions of libX11 crash if we call it after
10642 closing all the displays. */
10643 XrmDestroyDatabase (dpyinfo->xrdb);
10644 #endif
10646 #ifdef USE_GTK
10647 xg_display_close (dpyinfo->display);
10648 #else
10649 #ifdef USE_X_TOOLKIT
10650 XtCloseDisplay (dpyinfo->display);
10651 #else
10652 XCloseDisplay (dpyinfo->display);
10653 #endif
10654 #endif /* ! USE_GTK */
10657 /* No more input on this descriptor. */
10658 if (connection != -1)
10659 delete_keyboard_wait_descriptor (connection);
10661 /* Mark as dead. */
10662 dpyinfo->display = NULL;
10663 x_delete_display (dpyinfo);
10664 unblock_input ();
10667 /* Create a struct terminal, initialize it with the X11 specific
10668 functions and make DISPLAY->TERMINAL point to it. */
10670 static struct terminal *
10671 x_create_terminal (struct x_display_info *dpyinfo)
10673 struct terminal *terminal;
10675 terminal = create_terminal ();
10677 terminal->type = output_x_window;
10678 terminal->display_info.x = dpyinfo;
10679 dpyinfo->terminal = terminal;
10681 /* kboard is initialized in x_term_init. */
10683 terminal->clear_frame_hook = x_clear_frame;
10684 terminal->ins_del_lines_hook = x_ins_del_lines;
10685 terminal->delete_glyphs_hook = x_delete_glyphs;
10686 terminal->ring_bell_hook = XTring_bell;
10687 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
10688 terminal->reset_terminal_modes_hook = NULL;
10689 terminal->set_terminal_modes_hook = NULL;
10690 terminal->update_begin_hook = x_update_begin;
10691 terminal->update_end_hook = x_update_end;
10692 terminal->set_terminal_window_hook = NULL;
10693 terminal->read_socket_hook = XTread_socket;
10694 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10695 terminal->mouse_position_hook = XTmouse_position;
10696 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10697 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10698 terminal->fullscreen_hook = XTfullscreen_hook;
10699 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10700 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10701 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10702 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10704 terminal->delete_frame_hook = x_destroy_window;
10705 terminal->delete_terminal_hook = x_delete_terminal;
10707 terminal->rif = &x_redisplay_interface;
10708 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10709 terminal->char_ins_del_ok = 1;
10710 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10711 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10712 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10713 off the bottom. */
10715 return terminal;
10718 void
10719 x_initialize (void)
10721 baud_rate = 19200;
10723 x_noop_count = 0;
10724 last_tool_bar_item = -1;
10725 any_help_event_p = 0;
10726 ignore_next_mouse_click_timeout = 0;
10728 #ifdef USE_GTK
10729 current_count = -1;
10730 #endif
10732 /* Try to use interrupt input; if we can't, then start polling. */
10733 Fset_input_interrupt_mode (Qt);
10735 #ifdef USE_X_TOOLKIT
10736 XtToolkitInitialize ();
10738 Xt_app_con = XtCreateApplicationContext ();
10740 /* Register a converter from strings to pixels, which uses
10741 Emacs' color allocation infrastructure. */
10742 XtAppSetTypeConverter (Xt_app_con,
10743 XtRString, XtRPixel, cvt_string_to_pixel,
10744 cvt_string_to_pixel_args,
10745 XtNumber (cvt_string_to_pixel_args),
10746 XtCacheByDisplay, cvt_pixel_dtor);
10748 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10749 #endif
10751 #ifdef USE_TOOLKIT_SCROLL_BARS
10752 #ifndef USE_GTK
10753 xaw3d_arrow_scroll = False;
10754 xaw3d_pick_top = True;
10755 #endif
10756 #endif
10758 pending_autoraise_frame = 0;
10759 pending_event_wait.f = 0;
10760 pending_event_wait.eventtype = 0;
10762 /* Note that there is no real way portable across R3/R4 to get the
10763 original error handler. */
10764 XSetErrorHandler (x_error_handler);
10765 XSetIOErrorHandler (x_io_error_quitter);
10769 void
10770 syms_of_xterm (void)
10772 x_error_message = NULL;
10774 staticpro (&x_display_name_list);
10775 x_display_name_list = Qnil;
10777 staticpro (&last_mouse_scroll_bar);
10778 last_mouse_scroll_bar = Qnil;
10780 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
10781 DEFSYM (Qlatin_1, "latin-1");
10783 #ifdef USE_GTK
10784 xg_default_icon_file = build_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
10785 staticpro (&xg_default_icon_file);
10787 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
10788 #endif
10790 DEFVAR_BOOL ("x-use-underline-position-properties",
10791 x_use_underline_position_properties,
10792 doc: /* Non-nil means make use of UNDERLINE_POSITION font properties.
10793 A value of nil means ignore them. If you encounter fonts with bogus
10794 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10795 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10796 to override the font's UNDERLINE_POSITION for small font display
10797 sizes. */);
10798 x_use_underline_position_properties = 1;
10800 DEFVAR_BOOL ("x-underline-at-descent-line",
10801 x_underline_at_descent_line,
10802 doc: /* Non-nil means to draw the underline at the same place as the descent line.
10803 A value of nil means to draw the underline according to the value of the
10804 variable `x-use-underline-position-properties', which is usually at the
10805 baseline level. The default value is nil. */);
10806 x_underline_at_descent_line = 0;
10808 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10809 x_mouse_click_focus_ignore_position,
10810 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10811 This variable is only used when the window manager requires that you
10812 click on a frame to select it (give it focus). In that case, a value
10813 of nil, means that the selected window and cursor position changes to
10814 reflect the mouse click position, while a non-nil value means that the
10815 selected window or cursor position is preserved. */);
10816 x_mouse_click_focus_ignore_position = 0;
10818 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
10819 doc: /* Which toolkit scroll bars Emacs uses, if any.
10820 A value of nil means Emacs doesn't use toolkit scroll bars.
10821 With the X Window system, the value is a symbol describing the
10822 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
10823 With MS Windows or Nextstep, the value is t. */);
10824 #ifdef USE_TOOLKIT_SCROLL_BARS
10825 #ifdef USE_MOTIF
10826 Vx_toolkit_scroll_bars = intern_c_string ("motif");
10827 #elif defined HAVE_XAW3D
10828 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
10829 #elif USE_GTK
10830 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
10831 #else
10832 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
10833 #endif
10834 #else
10835 Vx_toolkit_scroll_bars = Qnil;
10836 #endif
10838 staticpro (&last_mouse_motion_frame);
10839 last_mouse_motion_frame = Qnil;
10841 Qmodifier_value = intern_c_string ("modifier-value");
10842 Qalt = intern_c_string ("alt");
10843 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10844 Qhyper = intern_c_string ("hyper");
10845 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10846 Qmeta = intern_c_string ("meta");
10847 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10848 Qsuper = intern_c_string ("super");
10849 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10851 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
10852 doc: /* Which keys Emacs uses for the alt modifier.
10853 This should be one of the symbols `alt', `hyper', `meta', `super'.
10854 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10855 is nil, which is the same as `alt'. */);
10856 Vx_alt_keysym = Qnil;
10858 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
10859 doc: /* Which keys Emacs uses for the hyper modifier.
10860 This should be one of the symbols `alt', `hyper', `meta', `super'.
10861 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10862 default is nil, which is the same as `hyper'. */);
10863 Vx_hyper_keysym = Qnil;
10865 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
10866 doc: /* Which keys Emacs uses for the meta modifier.
10867 This should be one of the symbols `alt', `hyper', `meta', `super'.
10868 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10869 default is nil, which is the same as `meta'. */);
10870 Vx_meta_keysym = Qnil;
10872 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
10873 doc: /* Which keys Emacs uses for the super modifier.
10874 This should be one of the symbols `alt', `hyper', `meta', `super'.
10875 For example, `super' means use the Super_L and Super_R keysyms. The
10876 default is nil, which is the same as `super'. */);
10877 Vx_super_keysym = Qnil;
10879 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
10880 doc: /* Hash table of character codes indexed by X keysym codes. */);
10881 Vx_keysym_table = make_hash_table (hashtest_eql, make_number (900),
10882 make_float (DEFAULT_REHASH_SIZE),
10883 make_float (DEFAULT_REHASH_THRESHOLD),
10884 Qnil);
10887 #endif /* HAVE_X_WINDOWS */