upstream
[emacs.git] / src / xterm.c
blob40dbe540903fedc187f03de14ff56c578f5e5b92
1 /* X Communication module for terminals which understand the X protocol.
3 Copyright (C) 1989, 1993-2011 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 <signal.h>
25 #include <stdio.h>
26 #include <setjmp.h>
28 #ifdef HAVE_X_WINDOWS
30 #include "lisp.h"
31 #include "blockinput.h"
33 /* Need syssignal.h for various externs and definitions that may be required
34 by some configurations for calls to signal later in this source file. */
35 #include "syssignal.h"
37 /* This may include sys/types.h, and that somehow loses
38 if this is not done before the other system files. */
39 #include "xterm.h"
40 #include <X11/cursorfont.h>
42 /* Load sys/types.h if not already loaded.
43 In some systems loading it twice is suicidal. */
44 #ifndef makedev
45 #include <sys/types.h>
46 #endif /* makedev */
48 #include <sys/ioctl.h>
50 #include "systime.h"
52 #include <fcntl.h>
53 #include <ctype.h>
54 #include <errno.h>
55 #include <setjmp.h>
56 #include <sys/stat.h>
57 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
58 /* #include <sys/param.h> */
60 #include "charset.h"
61 #include "character.h"
62 #include "coding.h"
63 #include "frame.h"
64 #include "dispextern.h"
65 #ifdef HAVE_XWIDGETS
66 #include "xwidget.h"
67 #endif
68 #include "fontset.h"
69 #include "termhooks.h"
70 #include "termopts.h"
71 #include "termchar.h"
72 #include "emacs-icon.h"
73 #include "disptab.h"
74 #include "buffer.h"
75 #include "window.h"
76 #include "keyboard.h"
77 #include "intervals.h"
78 #include "process.h"
79 #include "atimer.h"
80 #include "keymap.h"
81 #include "font.h"
82 #include "fontset.h"
83 #include "xsettings.h"
84 #include "xgselect.h"
85 #include "sysselect.h"
87 #ifdef USE_X_TOOLKIT
88 #include <X11/Shell.h>
89 #endif
91 #ifdef HAVE_SYS_TIME_H
92 #include <sys/time.h>
93 #endif
95 #include <unistd.h>
97 #ifdef USE_GTK
98 #include "gtkutil.h"
99 #endif
101 #ifdef USE_LUCID
102 #include "../lwlib/xlwmenu.h"
103 #endif
105 #ifdef USE_X_TOOLKIT
106 #if !defined(NO_EDITRES)
107 #define HACK_EDITRES
108 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
109 #endif /* not NO_EDITRES */
111 /* Include toolkit specific headers for the scroll bar widget. */
113 #ifdef USE_TOOLKIT_SCROLL_BARS
114 #if defined USE_MOTIF
115 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
116 #include <Xm/ScrollBar.h>
117 #else /* !USE_MOTIF i.e. use Xaw */
119 #ifdef HAVE_XAW3D
120 #include <X11/Xaw3d/Simple.h>
121 #include <X11/Xaw3d/Scrollbar.h>
122 #include <X11/Xaw3d/ThreeD.h>
123 #else /* !HAVE_XAW3D */
124 #include <X11/Xaw/Simple.h>
125 #include <X11/Xaw/Scrollbar.h>
126 #endif /* !HAVE_XAW3D */
127 #ifndef XtNpickTop
128 #define XtNpickTop "pickTop"
129 #endif /* !XtNpickTop */
130 #endif /* !USE_MOTIF */
131 #endif /* USE_TOOLKIT_SCROLL_BARS */
133 #endif /* USE_X_TOOLKIT */
135 #ifdef USE_X_TOOLKIT
136 #include "widget.h"
137 #ifndef XtNinitialState
138 #define XtNinitialState "initialState"
139 #endif
140 #endif
142 /* Default to using XIM if available. */
143 #ifdef USE_XIM
144 int use_xim = 1;
145 #else
146 int use_xim = 0; /* configure --without-xim */
147 #endif
151 /* Non-zero means that a HELP_EVENT has been generated since Emacs
152 start. */
154 static int any_help_event_p;
156 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
157 static Lisp_Object last_window;
159 /* This is a chain of structures for all the X displays currently in
160 use. */
162 struct x_display_info *x_display_list;
164 /* This is a list of cons cells, each of the form (NAME
165 . FONT-LIST-CACHE), one for each element of x_display_list and in
166 the same order. NAME is the name of the frame. FONT-LIST-CACHE
167 records previous values returned by x-list-fonts. */
169 Lisp_Object x_display_name_list;
171 /* Frame being updated by update_frame. This is declared in term.c.
172 This is set by update_begin and looked at by all the XT functions.
173 It is zero while not inside an update. In that case, the XT
174 functions assume that `selected_frame' is the frame to apply to. */
176 extern struct frame *updating_frame;
178 /* This is a frame waiting to be auto-raised, within XTread_socket. */
180 static struct frame *pending_autoraise_frame;
182 /* This is a frame waiting for an event matching mask, within XTread_socket. */
184 static struct {
185 struct frame *f;
186 int eventtype;
187 } pending_event_wait;
189 #ifdef USE_X_TOOLKIT
190 /* The application context for Xt use. */
191 XtAppContext Xt_app_con;
192 static String Xt_default_resources[] = {0};
194 /* Non-zero means user is interacting with a toolkit scroll bar. */
196 static int toolkit_scroll_bar_interaction;
197 #endif /* USE_X_TOOLKIT */
199 /* Non-zero timeout value means ignore next mouse click if it arrives
200 before that timeout elapses (i.e. as part of the same sequence of
201 events resulting from clicking on a frame to select it). */
203 static unsigned long ignore_next_mouse_click_timeout;
205 /* Mouse movement.
207 Formerly, we used PointerMotionHintMask (in standard_event_mask)
208 so that we would have to call XQueryPointer after each MotionNotify
209 event to ask for another such event. However, this made mouse tracking
210 slow, and there was a bug that made it eventually stop.
212 Simply asking for MotionNotify all the time seems to work better.
214 In order to avoid asking for motion events and then throwing most
215 of them away or busy-polling the server for mouse positions, we ask
216 the server for pointer motion hints. This means that we get only
217 one event per group of mouse movements. "Groups" are delimited by
218 other kinds of events (focus changes and button clicks, for
219 example), or by XQueryPointer calls; when one of these happens, we
220 get another MotionNotify event the next time the mouse moves. This
221 is at least as efficient as getting motion events when mouse
222 tracking is on, and I suspect only negligibly worse when tracking
223 is off. */
225 /* Where the mouse was last time we reported a mouse event. */
227 static XRectangle last_mouse_glyph;
228 static FRAME_PTR last_mouse_glyph_frame;
229 static Lisp_Object last_mouse_press_frame;
231 /* The scroll bar in which the last X motion event occurred.
233 If the last X motion event occurred in a scroll bar, we set this so
234 XTmouse_position can know whether to report a scroll bar motion or
235 an ordinary motion.
237 If the last X motion event didn't occur in a scroll bar, we set
238 this to Qnil, to tell XTmouse_position to return an ordinary motion
239 event. */
241 static Lisp_Object last_mouse_scroll_bar;
243 /* This is a hack. We would really prefer that XTmouse_position would
244 return the time associated with the position it returns, but there
245 doesn't seem to be any way to wrest the time-stamp from the server
246 along with the position query. So, we just keep track of the time
247 of the last movement we received, and return that in hopes that
248 it's somewhat accurate. */
250 static Time last_mouse_movement_time;
252 /* Time for last user interaction as returned in X events. */
254 static Time last_user_time;
256 /* Incremented by XTread_socket whenever it really tries to read
257 events. */
259 #ifdef __STDC__
260 static int volatile input_signal_count;
261 #else
262 static int input_signal_count;
263 #endif
265 /* Used locally within XTread_socket. */
267 static int x_noop_count;
269 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
271 static Lisp_Object Qvendor_specific_keysyms;
272 static Lisp_Object Qlatin_1;
274 #ifdef USE_GTK
275 /* The name of the Emacs icon file. */
276 static Lisp_Object xg_default_icon_file;
278 /* Used in gtkutil.c. */
279 Lisp_Object Qx_gtk_map_stock;
280 #endif
282 /* Some functions take this as char *, not const char *. */
283 static char emacs_class[] = EMACS_CLASS;
285 enum xembed_info
287 XEMBED_MAPPED = 1 << 0
290 enum xembed_message
292 XEMBED_EMBEDDED_NOTIFY = 0,
293 XEMBED_WINDOW_ACTIVATE = 1,
294 XEMBED_WINDOW_DEACTIVATE = 2,
295 XEMBED_REQUEST_FOCUS = 3,
296 XEMBED_FOCUS_IN = 4,
297 XEMBED_FOCUS_OUT = 5,
298 XEMBED_FOCUS_NEXT = 6,
299 XEMBED_FOCUS_PREV = 7,
301 XEMBED_MODALITY_ON = 10,
302 XEMBED_MODALITY_OFF = 11,
303 XEMBED_REGISTER_ACCELERATOR = 12,
304 XEMBED_UNREGISTER_ACCELERATOR = 13,
305 XEMBED_ACTIVATE_ACCELERATOR = 14
308 /* Used in x_flush. */
310 static int x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
311 static void x_set_window_size_1 (struct frame *, int, int, int);
312 static void x_raise_frame (struct frame *);
313 static void x_lower_frame (struct frame *);
314 static const XColor *x_color_cells (Display *, int *);
315 static void x_update_window_end (struct window *, int, int);
317 static int x_io_error_quitter (Display *);
318 static struct terminal *x_create_terminal (struct x_display_info *);
319 void x_delete_terminal (struct terminal *);
320 static void x_update_end (struct frame *);
321 static void XTframe_up_to_date (struct frame *);
322 static void XTset_terminal_modes (struct terminal *);
323 static void XTreset_terminal_modes (struct terminal *);
324 static void x_clear_frame (struct frame *);
325 static void x_ins_del_lines (struct frame *, int, int) NO_RETURN;
326 static void frame_highlight (struct frame *);
327 static void frame_unhighlight (struct frame *);
328 static void x_new_focus_frame (struct x_display_info *, struct frame *);
329 static void x_focus_changed (int, int, struct x_display_info *,
330 struct frame *, struct input_event *);
331 static void x_detect_focus_change (struct x_display_info *,
332 XEvent *, struct input_event *);
333 static void XTframe_rehighlight (struct frame *);
334 static void x_frame_rehighlight (struct x_display_info *);
335 static void x_draw_hollow_cursor (struct window *, struct glyph_row *);
336 static void x_draw_bar_cursor (struct window *, struct glyph_row *, int,
337 enum text_cursor_kinds);
339 static void x_clip_to_row (struct window *, struct glyph_row *, int, GC);
340 static void x_flush (struct frame *f);
341 static void x_update_begin (struct frame *);
342 static void x_update_window_begin (struct window *);
343 static void x_after_update_window_line (struct glyph_row *);
344 static struct scroll_bar *x_window_to_scroll_bar (Display *, Window);
345 static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
346 enum scroll_bar_part *,
347 Lisp_Object *, Lisp_Object *,
348 Time *);
349 static void x_handle_net_wm_state (struct frame *, XPropertyEvent *);
350 static void x_check_fullscreen (struct frame *);
351 static void x_check_expected_move (struct frame *, int, int);
352 static void x_sync_with_move (struct frame *, int, int, int);
353 static int handle_one_xevent (struct x_display_info *, XEvent *,
354 int *, struct input_event *);
355 #ifdef USE_GTK
356 static int x_dispatch_event (XEvent *, Display *);
357 #endif
358 /* Don't declare this NO_RETURN because we want no
359 interference with debugging failing X calls. */
360 static void x_connection_closed (Display *, const char *);
361 static void x_wm_set_window_state (struct frame *, int);
362 static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
363 static void x_initialize (void);
366 /* Flush display of frame F, or of all frames if F is null. */
368 static void
369 x_flush (struct frame *f)
371 /* Don't call XFlush when it is not safe to redisplay; the X
372 connection may be broken. */
373 if (!NILP (Vinhibit_redisplay))
374 return;
376 BLOCK_INPUT;
377 if (f == NULL)
379 Lisp_Object rest, frame;
380 FOR_EACH_FRAME (rest, frame)
381 if (FRAME_X_P (XFRAME (frame)))
382 x_flush (XFRAME (frame));
384 else if (FRAME_X_P (f))
385 XFlush (FRAME_X_DISPLAY (f));
386 UNBLOCK_INPUT;
390 /* Remove calls to XFlush by defining XFlush to an empty replacement.
391 Calls to XFlush should be unnecessary because the X output buffer
392 is flushed automatically as needed by calls to XPending,
393 XNextEvent, or XWindowEvent according to the XFlush man page.
394 XTread_socket calls XPending. Removing XFlush improves
395 performance. */
397 #define XFlush(DISPLAY) (void) 0
400 /***********************************************************************
401 Debugging
402 ***********************************************************************/
404 #if 0
406 /* This is a function useful for recording debugging information about
407 the sequence of occurrences in this file. */
409 struct record
411 char *locus;
412 int type;
415 struct record event_record[100];
417 int event_record_index;
419 void
420 record_event (char *locus, int type)
422 if (event_record_index == sizeof (event_record) / sizeof (struct record))
423 event_record_index = 0;
425 event_record[event_record_index].locus = locus;
426 event_record[event_record_index].type = type;
427 event_record_index++;
430 #endif /* 0 */
434 /* Return the struct x_display_info corresponding to DPY. */
436 struct x_display_info *
437 x_display_info_for_display (Display *dpy)
439 struct x_display_info *dpyinfo;
441 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
442 if (dpyinfo->display == dpy)
443 return dpyinfo;
445 return 0;
448 static Window
449 x_find_topmost_parent (struct frame *f)
451 struct x_output *x = f->output_data.x;
452 Window win = None, wi = x->parent_desc;
453 Display *dpy = FRAME_X_DISPLAY (f);
455 while (wi != FRAME_X_DISPLAY_INFO (f)->root_window)
457 Window root;
458 Window *children;
459 unsigned int nchildren;
461 win = wi;
462 XQueryTree (dpy, win, &root, &wi, &children, &nchildren);
463 XFree (children);
466 return win;
469 #define OPAQUE 0xffffffff
471 void
472 x_set_frame_alpha (struct frame *f)
474 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
475 Display *dpy = FRAME_X_DISPLAY (f);
476 Window win = FRAME_OUTER_WINDOW (f);
477 double alpha = 1.0;
478 double alpha_min = 1.0;
479 unsigned long opac;
480 Window parent;
482 if (dpyinfo->x_highlight_frame == f)
483 alpha = f->alpha[0];
484 else
485 alpha = f->alpha[1];
487 if (FLOATP (Vframe_alpha_lower_limit))
488 alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
489 else if (INTEGERP (Vframe_alpha_lower_limit))
490 alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;
492 if (alpha < 0.0)
493 return;
494 else if (alpha > 1.0)
495 alpha = 1.0;
496 else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
497 alpha = alpha_min;
499 opac = alpha * OPAQUE;
501 x_catch_errors (dpy);
503 /* If there is a parent from the window manager, put the property there
504 also, to work around broken window managers that fail to do that.
505 Do this unconditionally as this function is called on reparent when
506 alpha has not changed on the frame. */
508 parent = x_find_topmost_parent (f);
509 if (parent != None)
510 XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
511 XA_CARDINAL, 32, PropModeReplace,
512 (unsigned char *) &opac, 1L);
514 /* return unless necessary */
516 unsigned char *data;
517 Atom actual;
518 int rc, format;
519 unsigned long n, left;
521 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
522 0L, 1L, False, XA_CARDINAL,
523 &actual, &format, &n, &left,
524 &data);
526 if (rc == Success && actual != None)
528 unsigned long value = *(unsigned long *)data;
529 XFree ((void *) data);
530 if (value == opac)
532 x_uncatch_errors ();
533 return;
538 XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
539 XA_CARDINAL, 32, PropModeReplace,
540 (unsigned char *) &opac, 1L);
541 x_uncatch_errors ();
545 x_display_pixel_height (struct x_display_info *dpyinfo)
547 return HeightOfScreen (dpyinfo->screen);
551 x_display_pixel_width (struct x_display_info *dpyinfo)
553 return WidthOfScreen (dpyinfo->screen);
557 /***********************************************************************
558 Starting and ending an update
559 ***********************************************************************/
561 /* Start an update of frame F. This function is installed as a hook
562 for update_begin, i.e. it is called when update_begin is called.
563 This function is called prior to calls to x_update_window_begin for
564 each window being updated. Currently, there is nothing to do here
565 because all interesting stuff is done on a window basis. */
567 static void
568 x_update_begin (struct frame *f)
570 /* Nothing to do. */
574 /* Start update of window W. Set the global variable updated_window
575 to the window being updated and set output_cursor to the cursor
576 position of W. */
578 static void
579 x_update_window_begin (struct window *w)
581 struct frame *f = XFRAME (WINDOW_FRAME (w));
582 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
584 updated_window = w;
585 set_output_cursor (&w->cursor);
587 BLOCK_INPUT;
589 if (f == hlinfo->mouse_face_mouse_frame)
591 /* Don't do highlighting for mouse motion during the update. */
592 hlinfo->mouse_face_defer = 1;
594 /* If F needs to be redrawn, simply forget about any prior mouse
595 highlighting. */
596 if (FRAME_GARBAGED_P (f))
597 hlinfo->mouse_face_window = Qnil;
600 UNBLOCK_INPUT;
604 /* Draw a vertical window border from (x,y0) to (x,y1) */
606 static void
607 x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
609 struct frame *f = XFRAME (WINDOW_FRAME (w));
610 struct face *face;
612 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
613 if (face)
614 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
615 face->foreground);
617 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
618 f->output_data.x->normal_gc, x, y0, x, y1);
621 /* End update of window W (which is equal to updated_window).
623 Draw vertical borders between horizontally adjacent windows, and
624 display W's cursor if CURSOR_ON_P is non-zero.
626 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
627 glyphs in mouse-face were overwritten. In that case we have to
628 make sure that the mouse-highlight is properly redrawn.
630 W may be a menu bar pseudo-window in case we don't have X toolkit
631 support. Such windows don't have a cursor, so don't display it
632 here. */
634 static void
635 x_update_window_end (struct window *w, int cursor_on_p, int mouse_face_overwritten_p)
637 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
639 if (!w->pseudo_window_p)
641 BLOCK_INPUT;
643 if (cursor_on_p)
644 display_and_set_cursor (w, 1, output_cursor.hpos,
645 output_cursor.vpos,
646 output_cursor.x, output_cursor.y);
648 if (draw_window_fringes (w, 1))
649 x_draw_vertical_border (w);
651 UNBLOCK_INPUT;
654 /* If a row with mouse-face was overwritten, arrange for
655 XTframe_up_to_date to redisplay the mouse highlight. */
656 if (mouse_face_overwritten_p)
658 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
659 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
660 hlinfo->mouse_face_window = Qnil;
663 updated_window = NULL;
667 /* End update of frame F. This function is installed as a hook in
668 update_end. */
670 static void
671 x_update_end (struct frame *f)
673 /* Mouse highlight may be displayed again. */
674 MOUSE_HL_INFO (f)->mouse_face_defer = 0;
676 #ifndef XFlush
677 BLOCK_INPUT;
678 XFlush (FRAME_X_DISPLAY (f));
679 UNBLOCK_INPUT;
680 #endif
684 /* This function is called from various places in xdisp.c whenever a
685 complete update has been performed. The global variable
686 updated_window is not available here. */
688 static void
689 XTframe_up_to_date (struct frame *f)
691 if (FRAME_X_P (f))
693 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
695 if (hlinfo->mouse_face_deferred_gc
696 || f == hlinfo->mouse_face_mouse_frame)
698 BLOCK_INPUT;
699 if (hlinfo->mouse_face_mouse_frame)
700 note_mouse_highlight (hlinfo->mouse_face_mouse_frame,
701 hlinfo->mouse_face_mouse_x,
702 hlinfo->mouse_face_mouse_y);
703 hlinfo->mouse_face_deferred_gc = 0;
704 UNBLOCK_INPUT;
710 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
711 arrow bitmaps, or clear the fringes if no bitmaps are required
712 before DESIRED_ROW is made current. The window being updated is
713 found in updated_window. This function It is called from
714 update_window_line only if it is known that there are differences
715 between bitmaps to be drawn between current row and DESIRED_ROW. */
717 static void
718 x_after_update_window_line (struct glyph_row *desired_row)
720 struct window *w = updated_window;
721 struct frame *f;
722 int width, height;
724 xassert (w);
726 if (!desired_row->mode_line_p && !w->pseudo_window_p)
727 desired_row->redraw_fringe_bitmaps_p = 1;
729 /* When a window has disappeared, make sure that no rest of
730 full-width rows stays visible in the internal border. Could
731 check here if updated_window is the leftmost/rightmost window,
732 but I guess it's not worth doing since vertically split windows
733 are almost never used, internal border is rarely set, and the
734 overhead is very small. */
735 if (windows_or_buffers_changed
736 && desired_row->full_width_p
737 && (f = XFRAME (w->frame),
738 width = FRAME_INTERNAL_BORDER_WIDTH (f),
739 width != 0)
740 && (height = desired_row->visible_height,
741 height > 0))
743 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
745 BLOCK_INPUT;
746 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
747 0, y, width, height, False);
748 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
749 FRAME_PIXEL_WIDTH (f) - width,
750 y, width, height, False);
751 UNBLOCK_INPUT;
755 static void
756 x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
758 struct frame *f = XFRAME (WINDOW_FRAME (w));
759 Display *display = FRAME_X_DISPLAY (f);
760 Window window = FRAME_X_WINDOW (f);
761 GC gc = f->output_data.x->normal_gc;
762 struct face *face = p->face;
764 /* Must clip because of partially visible lines. */
765 x_clip_to_row (w, row, -1, gc);
767 if (!p->overlay_p)
769 int bx = p->bx, by = p->by, nx = p->nx, ny = p->ny;
771 /* In case the same realized face is used for fringes and
772 for something displayed in the text (e.g. face `region' on
773 mono-displays, the fill style may have been changed to
774 FillSolid in x_draw_glyph_string_background. */
775 if (face->stipple)
776 XSetFillStyle (display, face->gc, FillOpaqueStippled);
777 else
778 XSetForeground (display, face->gc, face->background);
780 #ifdef USE_TOOLKIT_SCROLL_BARS
781 /* If the fringe is adjacent to the left (right) scroll bar of a
782 leftmost (rightmost, respectively) window, then extend its
783 background to the gap between the fringe and the bar. */
784 if ((WINDOW_LEFTMOST_P (w)
785 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
786 || (WINDOW_RIGHTMOST_P (w)
787 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
789 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
791 if (sb_width > 0)
793 int bar_area_x = WINDOW_SCROLL_BAR_AREA_X (w);
794 int bar_area_width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
795 * FRAME_COLUMN_WIDTH (f));
797 if (bx < 0)
799 /* Bitmap fills the fringe. */
800 if (bar_area_x + bar_area_width == p->x)
801 bx = bar_area_x + sb_width;
802 else if (p->x + p->wd == bar_area_x)
803 bx = bar_area_x;
804 if (bx >= 0)
806 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
808 nx = bar_area_width - sb_width;
809 by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
810 row->y));
811 ny = row->visible_height;
814 else
816 if (bar_area_x + bar_area_width == bx)
818 bx = bar_area_x + sb_width;
819 nx += bar_area_width - sb_width;
821 else if (bx + nx == bar_area_x)
822 nx += bar_area_width - sb_width;
826 #endif
827 if (bx >= 0 && nx > 0)
828 XFillRectangle (display, window, face->gc, bx, by, nx, ny);
830 if (!face->stipple)
831 XSetForeground (display, face->gc, face->foreground);
834 if (p->which)
836 char *bits;
837 Pixmap pixmap, clipmask = (Pixmap) 0;
838 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
839 XGCValues gcv;
841 if (p->wd > 8)
842 bits = (char *) (p->bits + p->dh);
843 else
844 bits = (char *) p->bits + p->dh;
846 /* Draw the bitmap. I believe these small pixmaps can be cached
847 by the server. */
848 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
849 (p->cursor_p
850 ? (p->overlay_p ? face->background
851 : f->output_data.x->cursor_pixel)
852 : face->foreground),
853 face->background, depth);
855 if (p->overlay_p)
857 clipmask = XCreatePixmapFromBitmapData (display,
858 FRAME_X_DISPLAY_INFO (f)->root_window,
859 bits, p->wd, p->h,
860 1, 0, 1);
861 gcv.clip_mask = clipmask;
862 gcv.clip_x_origin = p->x;
863 gcv.clip_y_origin = p->y;
864 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
867 XCopyArea (display, pixmap, window, gc, 0, 0,
868 p->wd, p->h, p->x, p->y);
869 XFreePixmap (display, pixmap);
871 if (p->overlay_p)
873 gcv.clip_mask = (Pixmap) 0;
874 XChangeGC (display, gc, GCClipMask, &gcv);
875 XFreePixmap (display, clipmask);
879 XSetClipMask (display, gc, None);
884 /* This is called when starting Emacs and when restarting after
885 suspend. When starting Emacs, no X window is mapped. And nothing
886 must be done to Emacs's own window if it is suspended (though that
887 rarely happens). */
889 static void
890 XTset_terminal_modes (struct terminal *terminal)
894 /* This is called when exiting or suspending Emacs. Exiting will make
895 the X-windows go away, and suspending requires no action. */
897 static void
898 XTreset_terminal_modes (struct terminal *terminal)
903 /***********************************************************************
904 Glyph display
905 ***********************************************************************/
909 static void x_set_glyph_string_clipping (struct glyph_string *);
910 static void x_set_glyph_string_gc (struct glyph_string *);
911 static void x_draw_glyph_string_background (struct glyph_string *,
912 int);
913 static void x_draw_glyph_string_foreground (struct glyph_string *);
914 static void x_draw_composite_glyph_string_foreground (struct glyph_string *);
915 static void x_draw_glyph_string_box (struct glyph_string *);
916 static void x_draw_glyph_string (struct glyph_string *);
917 static void x_delete_glyphs (struct frame *, int) NO_RETURN;
918 static void x_compute_glyph_string_overhangs (struct glyph_string *);
919 static void x_set_cursor_gc (struct glyph_string *);
920 static void x_set_mode_line_face_gc (struct glyph_string *);
921 static void x_set_mouse_face_gc (struct glyph_string *);
922 static int x_alloc_lighter_color (struct frame *, Display *, Colormap,
923 unsigned long *, double, int);
924 static void x_setup_relief_color (struct frame *, struct relief *,
925 double, int, unsigned long);
926 static void x_setup_relief_colors (struct glyph_string *);
927 static void x_draw_image_glyph_string (struct glyph_string *);
928 static void x_draw_image_relief (struct glyph_string *);
929 static void x_draw_image_foreground (struct glyph_string *);
930 static void x_draw_image_foreground_1 (struct glyph_string *, Pixmap);
931 static void x_clear_glyph_string_rect (struct glyph_string *, int,
932 int, int, int);
933 static void x_draw_relief_rect (struct frame *, int, int, int, int,
934 int, int, int, int, int, int,
935 XRectangle *);
936 static void x_draw_box_rect (struct glyph_string *, int, int, int, int,
937 int, int, int, XRectangle *);
938 static void x_scroll_bar_clear (struct frame *);
940 #if GLYPH_DEBUG
941 static void x_check_font (struct frame *, struct font *);
942 #endif
945 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
946 face. */
948 static void
949 x_set_cursor_gc (struct glyph_string *s)
951 if (s->font == FRAME_FONT (s->f)
952 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
953 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
954 && !s->cmp)
955 s->gc = s->f->output_data.x->cursor_gc;
956 else
958 /* Cursor on non-default face: must merge. */
959 XGCValues xgcv;
960 unsigned long mask;
962 xgcv.background = s->f->output_data.x->cursor_pixel;
963 xgcv.foreground = s->face->background;
965 /* If the glyph would be invisible, try a different foreground. */
966 if (xgcv.foreground == xgcv.background)
967 xgcv.foreground = s->face->foreground;
968 if (xgcv.foreground == xgcv.background)
969 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
970 if (xgcv.foreground == xgcv.background)
971 xgcv.foreground = s->face->foreground;
973 /* Make sure the cursor is distinct from text in this face. */
974 if (xgcv.background == s->face->background
975 && xgcv.foreground == s->face->foreground)
977 xgcv.background = s->face->foreground;
978 xgcv.foreground = s->face->background;
981 IF_DEBUG (x_check_font (s->f, s->font));
982 xgcv.graphics_exposures = False;
983 mask = GCForeground | GCBackground | GCGraphicsExposures;
985 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
986 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
987 mask, &xgcv);
988 else
989 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
990 = XCreateGC (s->display, s->window, mask, &xgcv);
992 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
997 /* Set up S->gc of glyph string S for drawing text in mouse face. */
999 static void
1000 x_set_mouse_face_gc (struct glyph_string *s)
1002 int face_id;
1003 struct face *face;
1005 /* What face has to be used last for the mouse face? */
1006 face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
1007 face = FACE_FROM_ID (s->f, face_id);
1008 if (face == NULL)
1009 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1011 if (s->first_glyph->type == CHAR_GLYPH)
1012 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1013 else
1014 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1015 s->face = FACE_FROM_ID (s->f, face_id);
1016 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1018 if (s->font == s->face->font)
1019 s->gc = s->face->gc;
1020 else
1022 /* Otherwise construct scratch_cursor_gc with values from FACE
1023 except for FONT. */
1024 XGCValues xgcv;
1025 unsigned long mask;
1027 xgcv.background = s->face->background;
1028 xgcv.foreground = s->face->foreground;
1029 xgcv.graphics_exposures = False;
1030 mask = GCForeground | GCBackground | GCGraphicsExposures;
1032 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1033 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1034 mask, &xgcv);
1035 else
1036 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1037 = XCreateGC (s->display, s->window, mask, &xgcv);
1039 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1042 xassert (s->gc != 0);
1046 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1047 Faces to use in the mode line have already been computed when the
1048 matrix was built, so there isn't much to do, here. */
1050 static inline void
1051 x_set_mode_line_face_gc (struct glyph_string *s)
1053 s->gc = s->face->gc;
1057 /* Set S->gc of glyph string S for drawing that glyph string. Set
1058 S->stippled_p to a non-zero value if the face of S has a stipple
1059 pattern. */
1061 static inline void
1062 x_set_glyph_string_gc (struct glyph_string *s)
1064 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1066 if (s->hl == DRAW_NORMAL_TEXT)
1068 s->gc = s->face->gc;
1069 s->stippled_p = s->face->stipple != 0;
1071 else if (s->hl == DRAW_INVERSE_VIDEO)
1073 x_set_mode_line_face_gc (s);
1074 s->stippled_p = s->face->stipple != 0;
1076 else if (s->hl == DRAW_CURSOR)
1078 x_set_cursor_gc (s);
1079 s->stippled_p = 0;
1081 else if (s->hl == DRAW_MOUSE_FACE)
1083 x_set_mouse_face_gc (s);
1084 s->stippled_p = s->face->stipple != 0;
1086 else if (s->hl == DRAW_IMAGE_RAISED
1087 || s->hl == DRAW_IMAGE_SUNKEN)
1089 s->gc = s->face->gc;
1090 s->stippled_p = s->face->stipple != 0;
1092 else
1094 s->gc = s->face->gc;
1095 s->stippled_p = s->face->stipple != 0;
1098 /* GC must have been set. */
1099 xassert (s->gc != 0);
1103 /* Set clipping for output of glyph string S. S may be part of a mode
1104 line or menu if we don't have X toolkit support. */
1106 static inline void
1107 x_set_glyph_string_clipping (struct glyph_string *s)
1109 XRectangle *r = s->clip;
1110 int n = get_glyph_string_clip_rects (s, r, 2);
1112 if (n > 0)
1113 XSetClipRectangles (s->display, s->gc, 0, 0, r, n, Unsorted);
1114 s->num_clips = n;
1118 /* Set SRC's clipping for output of glyph string DST. This is called
1119 when we are drawing DST's left_overhang or right_overhang only in
1120 the area of SRC. */
1122 static void
1123 x_set_glyph_string_clipping_exactly (struct glyph_string *src, struct glyph_string *dst)
1125 XRectangle r;
1127 r.x = src->x;
1128 r.width = src->width;
1129 r.y = src->y;
1130 r.height = src->height;
1131 dst->clip[0] = r;
1132 dst->num_clips = 1;
1133 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1137 /* RIF:
1138 Compute left and right overhang of glyph string S. */
1140 static void
1141 x_compute_glyph_string_overhangs (struct glyph_string *s)
1143 if (s->cmp == NULL
1144 && (s->first_glyph->type == CHAR_GLYPH
1145 || s->first_glyph->type == COMPOSITE_GLYPH))
1147 struct font_metrics metrics;
1149 if (s->first_glyph->type == CHAR_GLYPH)
1151 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1152 struct font *font = s->font;
1153 int i;
1155 for (i = 0; i < s->nchars; i++)
1156 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1157 font->driver->text_extents (font, code, s->nchars, &metrics);
1159 else
1161 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1163 composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
1165 s->right_overhang = (metrics.rbearing > metrics.width
1166 ? metrics.rbearing - metrics.width : 0);
1167 s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
1169 else if (s->cmp)
1171 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1172 s->left_overhang = - s->cmp->lbearing;
1177 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1179 static inline void
1180 x_clear_glyph_string_rect (struct glyph_string *s, int x, int y, int w, int h)
1182 XGCValues xgcv;
1183 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1184 XSetForeground (s->display, s->gc, xgcv.background);
1185 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1186 XSetForeground (s->display, s->gc, xgcv.foreground);
1190 /* Draw the background of glyph_string S. If S->background_filled_p
1191 is non-zero don't draw it. FORCE_P non-zero means draw the
1192 background even if it wouldn't be drawn normally. This is used
1193 when a string preceding S draws into the background of S, or S
1194 contains the first component of a composition. */
1196 static void
1197 x_draw_glyph_string_background (struct glyph_string *s, int force_p)
1199 /* Nothing to do if background has already been drawn or if it
1200 shouldn't be drawn in the first place. */
1201 if (!s->background_filled_p)
1203 int box_line_width = max (s->face->box_line_width, 0);
1205 if (s->stippled_p)
1207 /* Fill background with a stipple pattern. */
1208 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1209 XFillRectangle (s->display, s->window, s->gc, s->x,
1210 s->y + box_line_width,
1211 s->background_width,
1212 s->height - 2 * box_line_width);
1213 XSetFillStyle (s->display, s->gc, FillSolid);
1214 s->background_filled_p = 1;
1216 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1217 || s->font_not_found_p
1218 || s->extends_to_end_of_line_p
1219 || force_p)
1221 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1222 s->background_width,
1223 s->height - 2 * box_line_width);
1224 s->background_filled_p = 1;
1230 /* Draw the foreground of glyph string S. */
1232 static void
1233 x_draw_glyph_string_foreground (struct glyph_string *s)
1235 int i, x;
1237 /* If first glyph of S has a left box line, start drawing the text
1238 of S to the right of that box line. */
1239 if (s->face->box != FACE_NO_BOX
1240 && s->first_glyph->left_box_line_p)
1241 x = s->x + eabs (s->face->box_line_width);
1242 else
1243 x = s->x;
1245 /* Draw characters of S as rectangles if S's font could not be
1246 loaded. */
1247 if (s->font_not_found_p)
1249 for (i = 0; i < s->nchars; ++i)
1251 struct glyph *g = s->first_glyph + i;
1252 XDrawRectangle (s->display, s->window,
1253 s->gc, x, s->y, g->pixel_width - 1,
1254 s->height - 1);
1255 x += g->pixel_width;
1258 else
1260 struct font *font = s->font;
1261 int boff = font->baseline_offset;
1262 int y;
1264 if (font->vertical_centering)
1265 boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;
1267 y = s->ybase - boff;
1268 if (s->for_overlaps
1269 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1270 font->driver->draw (s, 0, s->nchars, x, y, 0);
1271 else
1272 font->driver->draw (s, 0, s->nchars, x, y, 1);
1273 if (s->face->overstrike)
1274 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1278 /* Draw the foreground of composite glyph string S. */
1280 static void
1281 x_draw_composite_glyph_string_foreground (struct glyph_string *s)
1283 int i, j, x;
1284 struct font *font = s->font;
1286 /* If first glyph of S has a left box line, start drawing the text
1287 of S to the right of that box line. */
1288 if (s->face && s->face->box != FACE_NO_BOX
1289 && s->first_glyph->left_box_line_p)
1290 x = s->x + eabs (s->face->box_line_width);
1291 else
1292 x = s->x;
1294 /* S is a glyph string for a composition. S->cmp_from is the index
1295 of the first character drawn for glyphs of this composition.
1296 S->cmp_from == 0 means we are drawing the very first character of
1297 this composition. */
1299 /* Draw a rectangle for the composition if the font for the very
1300 first character of the composition could not be loaded. */
1301 if (s->font_not_found_p)
1303 if (s->cmp_from == 0)
1304 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1305 s->width - 1, s->height - 1);
1307 else if (! s->first_glyph->u.cmp.automatic)
1309 int y = s->ybase;
1311 for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
1312 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1314 int xx = x + s->cmp->offsets[j * 2];
1315 int yy = y - s->cmp->offsets[j * 2 + 1];
1317 font->driver->draw (s, j, j + 1, xx, yy, 0);
1318 if (s->face->overstrike)
1319 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1322 else
1324 Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
1325 Lisp_Object glyph;
1326 int y = s->ybase;
1327 int width = 0;
1329 for (i = j = s->cmp_from; i < s->cmp_to; i++)
1331 glyph = LGSTRING_GLYPH (gstring, i);
1332 if (NILP (LGLYPH_ADJUSTMENT (glyph)))
1333 width += LGLYPH_WIDTH (glyph);
1334 else
1336 int xoff, yoff, wadjust;
1338 if (j < i)
1340 font->driver->draw (s, j, i, x, y, 0);
1341 if (s->face->overstrike)
1342 font->driver->draw (s, j, i, x + 1, y, 0);
1343 x += width;
1345 xoff = LGLYPH_XOFF (glyph);
1346 yoff = LGLYPH_YOFF (glyph);
1347 wadjust = LGLYPH_WADJUST (glyph);
1348 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1349 if (s->face->overstrike)
1350 font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff, 0);
1351 x += wadjust;
1352 j = i + 1;
1353 width = 0;
1356 if (j < i)
1358 font->driver->draw (s, j, i, x, y, 0);
1359 if (s->face->overstrike)
1360 font->driver->draw (s, j, i, x + 1, y, 0);
1366 /* Draw the foreground of glyph string S for glyphless characters. */
1368 static void
1369 x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
1371 struct glyph *glyph = s->first_glyph;
1372 XChar2b char2b[8];
1373 int x, i, j;
1375 /* If first glyph of S has a left box line, start drawing the text
1376 of S to the right of that box line. */
1377 if (s->face && s->face->box != FACE_NO_BOX
1378 && s->first_glyph->left_box_line_p)
1379 x = s->x + eabs (s->face->box_line_width);
1380 else
1381 x = s->x;
1383 s->char2b = char2b;
1385 for (i = 0; i < s->nchars; i++, glyph++)
1387 char buf[7], *str = NULL;
1388 int len = glyph->u.glyphless.len;
1390 if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
1392 if (len > 0
1393 && CHAR_TABLE_P (Vglyphless_char_display)
1394 && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
1395 >= 1))
1397 Lisp_Object acronym
1398 = (! glyph->u.glyphless.for_no_font
1399 ? CHAR_TABLE_REF (Vglyphless_char_display,
1400 glyph->u.glyphless.ch)
1401 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
1402 if (STRINGP (acronym))
1403 str = SSDATA (acronym);
1406 else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
1408 sprintf ((char *) buf, "%0*X",
1409 glyph->u.glyphless.ch < 0x10000 ? 4 : 6,
1410 glyph->u.glyphless.ch);
1411 str = buf;
1414 if (str)
1416 int upper_len = (len + 1) / 2;
1417 unsigned code;
1419 /* It is assured that all LEN characters in STR is ASCII. */
1420 for (j = 0; j < len; j++)
1422 code = s->font->driver->encode_char (s->font, str[j]);
1423 STORE_XCHAR2B (char2b + j, code >> 8, code & 0xFF);
1425 s->font->driver->draw (s, 0, upper_len,
1426 x + glyph->slice.glyphless.upper_xoff,
1427 s->ybase + glyph->slice.glyphless.upper_yoff,
1429 s->font->driver->draw (s, upper_len, len,
1430 x + glyph->slice.glyphless.lower_xoff,
1431 s->ybase + glyph->slice.glyphless.lower_yoff,
1434 if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
1435 XDrawRectangle (s->display, s->window, s->gc,
1436 x, s->ybase - glyph->ascent,
1437 glyph->pixel_width - 1,
1438 glyph->ascent + glyph->descent - 1);
1439 x += glyph->pixel_width;
1443 #ifdef USE_X_TOOLKIT
1445 static struct frame *x_frame_of_widget (Widget);
1446 static Boolean cvt_string_to_pixel (Display *, XrmValue *, Cardinal *,
1447 XrmValue *, XrmValue *, XtPointer *);
1448 static void cvt_pixel_dtor (XtAppContext, XrmValue *, XtPointer,
1449 XrmValue *, Cardinal *);
1452 /* Return the frame on which widget WIDGET is used.. Abort if frame
1453 cannot be determined. */
1455 static struct frame *
1456 x_frame_of_widget (Widget widget)
1458 struct x_display_info *dpyinfo;
1459 Lisp_Object tail;
1460 struct frame *f;
1462 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1464 /* Find the top-level shell of the widget. Note that this function
1465 can be called when the widget is not yet realized, so XtWindow
1466 (widget) == 0. That's the reason we can't simply use
1467 x_any_window_to_frame. */
1468 while (!XtIsTopLevelShell (widget))
1469 widget = XtParent (widget);
1471 /* Look for a frame with that top-level widget. Allocate the color
1472 on that frame to get the right gamma correction value. */
1473 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1474 if (FRAMEP (XCAR (tail))
1475 && (f = XFRAME (XCAR (tail)),
1476 (FRAME_X_P (f)
1477 && f->output_data.nothing != 1
1478 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1479 && f->output_data.x->widget == widget)
1480 return f;
1482 abort ();
1486 #ifdef USE_LUCID
1488 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1489 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1490 If this produces the same color as PIXEL, try a color where all RGB
1491 values have DELTA added. Return the allocated color in *PIXEL.
1492 DISPLAY is the X display, CMAP is the colormap to operate on.
1493 Value is non-zero if successful. */
1496 x_alloc_lighter_color_for_widget (Widget widget, Display *display, Colormap cmap,
1497 unsigned long *pixel, double factor, int delta)
1499 struct frame *f = x_frame_of_widget (widget);
1500 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1503 #endif
1506 /* Structure specifying which arguments should be passed by Xt to
1507 cvt_string_to_pixel. We want the widget's screen and colormap. */
1509 static XtConvertArgRec cvt_string_to_pixel_args[] =
1511 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1512 sizeof (Screen *)},
1513 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1514 sizeof (Colormap)}
1518 /* The address of this variable is returned by
1519 cvt_string_to_pixel. */
1521 static Pixel cvt_string_to_pixel_value;
1524 /* Convert a color name to a pixel color.
1526 DPY is the display we are working on.
1528 ARGS is an array of *NARGS XrmValue structures holding additional
1529 information about the widget for which the conversion takes place.
1530 The contents of this array are determined by the specification
1531 in cvt_string_to_pixel_args.
1533 FROM is a pointer to an XrmValue which points to the color name to
1534 convert. TO is an XrmValue in which to return the pixel color.
1536 CLOSURE_RET is a pointer to user-data, in which we record if
1537 we allocated the color or not.
1539 Value is True if successful, False otherwise. */
1541 static Boolean
1542 cvt_string_to_pixel (Display *dpy, XrmValue *args, Cardinal *nargs,
1543 XrmValue *from, XrmValue *to,
1544 XtPointer *closure_ret)
1546 Screen *screen;
1547 Colormap cmap;
1548 Pixel pixel;
1549 String color_name;
1550 XColor color;
1552 if (*nargs != 2)
1554 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1555 "wrongParameters", "cvt_string_to_pixel",
1556 "XtToolkitError",
1557 "Screen and colormap args required", NULL, NULL);
1558 return False;
1561 screen = *(Screen **) args[0].addr;
1562 cmap = *(Colormap *) args[1].addr;
1563 color_name = (String) from->addr;
1565 if (strcmp (color_name, XtDefaultBackground) == 0)
1567 *closure_ret = (XtPointer) False;
1568 pixel = WhitePixelOfScreen (screen);
1570 else if (strcmp (color_name, XtDefaultForeground) == 0)
1572 *closure_ret = (XtPointer) False;
1573 pixel = BlackPixelOfScreen (screen);
1575 else if (XParseColor (dpy, cmap, color_name, &color)
1576 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1578 pixel = color.pixel;
1579 *closure_ret = (XtPointer) True;
1581 else
1583 String params[1];
1584 Cardinal nparams = 1;
1586 params[0] = color_name;
1587 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1588 "badValue", "cvt_string_to_pixel",
1589 "XtToolkitError", "Invalid color `%s'",
1590 params, &nparams);
1591 return False;
1594 if (to->addr != NULL)
1596 if (to->size < sizeof (Pixel))
1598 to->size = sizeof (Pixel);
1599 return False;
1602 *(Pixel *) to->addr = pixel;
1604 else
1606 cvt_string_to_pixel_value = pixel;
1607 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1610 to->size = sizeof (Pixel);
1611 return True;
1615 /* Free a pixel color which was previously allocated via
1616 cvt_string_to_pixel. This is registered as the destructor
1617 for this type of resource via XtSetTypeConverter.
1619 APP is the application context in which we work.
1621 TO is a pointer to an XrmValue holding the color to free.
1622 CLOSURE is the value we stored in CLOSURE_RET for this color
1623 in cvt_string_to_pixel.
1625 ARGS and NARGS are like for cvt_string_to_pixel. */
1627 static void
1628 cvt_pixel_dtor (XtAppContext app, XrmValuePtr to, XtPointer closure, XrmValuePtr args,
1629 Cardinal *nargs)
1631 if (*nargs != 2)
1633 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1634 "XtToolkitError",
1635 "Screen and colormap arguments required",
1636 NULL, NULL);
1638 else if (closure != NULL)
1640 /* We did allocate the pixel, so free it. */
1641 Screen *screen = *(Screen **) args[0].addr;
1642 Colormap cmap = *(Colormap *) args[1].addr;
1643 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1644 (Pixel *) to->addr, 1);
1649 #endif /* USE_X_TOOLKIT */
1652 /* Value is an array of XColor structures for the contents of the
1653 color map of display DPY. Set *NCELLS to the size of the array.
1654 Note that this probably shouldn't be called for large color maps,
1655 say a 24-bit TrueColor map. */
1657 static const XColor *
1658 x_color_cells (Display *dpy, int *ncells)
1660 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1662 if (dpyinfo->color_cells == NULL)
1664 Screen *screen = dpyinfo->screen;
1665 int ncolor_cells = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1666 int i;
1668 dpyinfo->color_cells = xnmalloc (ncolor_cells,
1669 sizeof *dpyinfo->color_cells);
1670 dpyinfo->ncolor_cells = ncolor_cells;
1672 for (i = 0; i < ncolor_cells; ++i)
1673 dpyinfo->color_cells[i].pixel = i;
1675 XQueryColors (dpy, dpyinfo->cmap,
1676 dpyinfo->color_cells, ncolor_cells);
1679 *ncells = dpyinfo->ncolor_cells;
1680 return dpyinfo->color_cells;
1684 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1685 colors in COLORS. Use cached information, if available. */
1687 void
1688 x_query_colors (struct frame *f, XColor *colors, int ncolors)
1690 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1692 if (dpyinfo->color_cells)
1694 int i;
1695 for (i = 0; i < ncolors; ++i)
1697 unsigned long pixel = colors[i].pixel;
1698 xassert (pixel < dpyinfo->ncolor_cells);
1699 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1700 colors[i] = dpyinfo->color_cells[pixel];
1703 else
1704 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1708 /* On frame F, translate pixel color to RGB values for the color in
1709 COLOR. Use cached information, if available. */
1711 void
1712 x_query_color (struct frame *f, XColor *color)
1714 x_query_colors (f, color, 1);
1718 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1719 exact match can't be allocated, try the nearest color available.
1720 Value is non-zero if successful. Set *COLOR to the color
1721 allocated. */
1723 static int
1724 x_alloc_nearest_color_1 (Display *dpy, Colormap cmap, XColor *color)
1726 int rc;
1728 rc = XAllocColor (dpy, cmap, color);
1729 if (rc == 0)
1731 /* If we got to this point, the colormap is full, so we're going
1732 to try to get the next closest color. The algorithm used is
1733 a least-squares matching, which is what X uses for closest
1734 color matching with StaticColor visuals. */
1735 int nearest, i;
1736 int max_color_delta = 255;
1737 int max_delta = 3 * max_color_delta;
1738 int nearest_delta = max_delta + 1;
1739 int ncells;
1740 const XColor *cells = x_color_cells (dpy, &ncells);
1742 for (nearest = i = 0; i < ncells; ++i)
1744 int dred = (color->red >> 8) - (cells[i].red >> 8);
1745 int dgreen = (color->green >> 8) - (cells[i].green >> 8);
1746 int dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1747 int delta = dred * dred + dgreen * dgreen + dblue * dblue;
1749 if (delta < nearest_delta)
1751 nearest = i;
1752 nearest_delta = delta;
1756 color->red = cells[nearest].red;
1757 color->green = cells[nearest].green;
1758 color->blue = cells[nearest].blue;
1759 rc = XAllocColor (dpy, cmap, color);
1761 else
1763 /* If allocation succeeded, and the allocated pixel color is not
1764 equal to a cached pixel color recorded earlier, there was a
1765 change in the colormap, so clear the color cache. */
1766 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1767 XColor *cached_color;
1769 if (dpyinfo->color_cells
1770 && (cached_color = &dpyinfo->color_cells[color->pixel],
1771 (cached_color->red != color->red
1772 || cached_color->blue != color->blue
1773 || cached_color->green != color->green)))
1775 xfree (dpyinfo->color_cells);
1776 dpyinfo->color_cells = NULL;
1777 dpyinfo->ncolor_cells = 0;
1781 #ifdef DEBUG_X_COLORS
1782 if (rc)
1783 register_color (color->pixel);
1784 #endif /* DEBUG_X_COLORS */
1786 return rc;
1790 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1791 exact match can't be allocated, try the nearest color available.
1792 Value is non-zero if successful. Set *COLOR to the color
1793 allocated. */
1796 x_alloc_nearest_color (struct frame *f, Colormap cmap, XColor *color)
1798 gamma_correct (f, color);
1799 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1803 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1804 It's necessary to do this instead of just using PIXEL directly to
1805 get color reference counts right. */
1807 unsigned long
1808 x_copy_color (struct frame *f, long unsigned int pixel)
1810 XColor color;
1812 color.pixel = pixel;
1813 BLOCK_INPUT;
1814 x_query_color (f, &color);
1815 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1816 UNBLOCK_INPUT;
1817 #ifdef DEBUG_X_COLORS
1818 register_color (pixel);
1819 #endif
1820 return color.pixel;
1824 /* Brightness beyond which a color won't have its highlight brightness
1825 boosted.
1827 Nominally, highlight colors for `3d' faces are calculated by
1828 brightening an object's color by a constant scale factor, but this
1829 doesn't yield good results for dark colors, so for colors who's
1830 brightness is less than this value (on a scale of 0-65535) have an
1831 use an additional additive factor.
1833 The value here is set so that the default menu-bar/mode-line color
1834 (grey75) will not have its highlights changed at all. */
1835 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1838 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1839 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1840 If this produces the same color as PIXEL, try a color where all RGB
1841 values have DELTA added. Return the allocated color in *PIXEL.
1842 DISPLAY is the X display, CMAP is the colormap to operate on.
1843 Value is non-zero if successful. */
1845 static int
1846 x_alloc_lighter_color (struct frame *f, Display *display, Colormap cmap, long unsigned int *pixel, double factor, int delta)
1848 XColor color, new;
1849 long bright;
1850 int success_p;
1852 /* Get RGB color values. */
1853 color.pixel = *pixel;
1854 x_query_color (f, &color);
1856 /* Change RGB values by specified FACTOR. Avoid overflow! */
1857 xassert (factor >= 0);
1858 new.red = min (0xffff, factor * color.red);
1859 new.green = min (0xffff, factor * color.green);
1860 new.blue = min (0xffff, factor * color.blue);
1862 /* Calculate brightness of COLOR. */
1863 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
1865 /* We only boost colors that are darker than
1866 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
1867 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
1868 /* Make an additive adjustment to NEW, because it's dark enough so
1869 that scaling by FACTOR alone isn't enough. */
1871 /* How far below the limit this color is (0 - 1, 1 being darker). */
1872 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
1873 /* The additive adjustment. */
1874 int min_delta = delta * dimness * factor / 2;
1876 if (factor < 1)
1878 new.red = max (0, new.red - min_delta);
1879 new.green = max (0, new.green - min_delta);
1880 new.blue = max (0, new.blue - min_delta);
1882 else
1884 new.red = min (0xffff, min_delta + new.red);
1885 new.green = min (0xffff, min_delta + new.green);
1886 new.blue = min (0xffff, min_delta + new.blue);
1890 /* Try to allocate the color. */
1891 success_p = x_alloc_nearest_color (f, cmap, &new);
1892 if (success_p)
1894 if (new.pixel == *pixel)
1896 /* If we end up with the same color as before, try adding
1897 delta to the RGB values. */
1898 x_free_colors (f, &new.pixel, 1);
1900 new.red = min (0xffff, delta + color.red);
1901 new.green = min (0xffff, delta + color.green);
1902 new.blue = min (0xffff, delta + color.blue);
1903 success_p = x_alloc_nearest_color (f, cmap, &new);
1905 else
1906 success_p = 1;
1907 *pixel = new.pixel;
1910 return success_p;
1914 /* Set up the foreground color for drawing relief lines of glyph
1915 string S. RELIEF is a pointer to a struct relief containing the GC
1916 with which lines will be drawn. Use a color that is FACTOR or
1917 DELTA lighter or darker than the relief's background which is found
1918 in S->f->output_data.x->relief_background. If such a color cannot
1919 be allocated, use DEFAULT_PIXEL, instead. */
1921 static void
1922 x_setup_relief_color (struct frame *f, struct relief *relief, double factor, int delta, long unsigned int default_pixel)
1924 XGCValues xgcv;
1925 struct x_output *di = f->output_data.x;
1926 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
1927 unsigned long pixel;
1928 unsigned long background = di->relief_background;
1929 Colormap cmap = FRAME_X_COLORMAP (f);
1930 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1931 Display *dpy = FRAME_X_DISPLAY (f);
1933 xgcv.graphics_exposures = False;
1934 xgcv.line_width = 1;
1936 /* Free previously allocated color. The color cell will be reused
1937 when it has been freed as many times as it was allocated, so this
1938 doesn't affect faces using the same colors. */
1939 if (relief->gc
1940 && relief->allocated_p)
1942 x_free_colors (f, &relief->pixel, 1);
1943 relief->allocated_p = 0;
1946 /* Allocate new color. */
1947 xgcv.foreground = default_pixel;
1948 pixel = background;
1949 if (dpyinfo->n_planes != 1
1950 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
1952 relief->allocated_p = 1;
1953 xgcv.foreground = relief->pixel = pixel;
1956 if (relief->gc == 0)
1958 xgcv.stipple = dpyinfo->gray;
1959 mask |= GCStipple;
1960 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
1962 else
1963 XChangeGC (dpy, relief->gc, mask, &xgcv);
1967 /* Set up colors for the relief lines around glyph string S. */
1969 static void
1970 x_setup_relief_colors (struct glyph_string *s)
1972 struct x_output *di = s->f->output_data.x;
1973 unsigned long color;
1975 if (s->face->use_box_color_for_shadows_p)
1976 color = s->face->box_color;
1977 else if (s->first_glyph->type == IMAGE_GLYPH
1978 && s->img->pixmap
1979 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
1980 color = IMAGE_BACKGROUND (s->img, s->f, 0);
1981 else
1983 XGCValues xgcv;
1985 /* Get the background color of the face. */
1986 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
1987 color = xgcv.background;
1990 if (di->white_relief.gc == 0
1991 || color != di->relief_background)
1993 di->relief_background = color;
1994 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
1995 WHITE_PIX_DEFAULT (s->f));
1996 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
1997 BLACK_PIX_DEFAULT (s->f));
2002 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2003 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2004 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2005 relief. LEFT_P non-zero means draw a relief on the left side of
2006 the rectangle. RIGHT_P non-zero means draw a relief on the right
2007 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2008 when drawing. */
2010 static void
2011 x_draw_relief_rect (struct frame *f,
2012 int left_x, int top_y, int right_x, int bottom_y, int width,
2013 int raised_p, int top_p, int bot_p, int left_p, int right_p,
2014 XRectangle *clip_rect)
2016 Display *dpy = FRAME_X_DISPLAY (f);
2017 Window window = FRAME_X_WINDOW (f);
2018 int i;
2019 GC gc;
2021 if (raised_p)
2022 gc = f->output_data.x->white_relief.gc;
2023 else
2024 gc = f->output_data.x->black_relief.gc;
2025 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2027 /* This code is more complicated than it has to be, because of two
2028 minor hacks to make the boxes look nicer: (i) if width > 1, draw
2029 the outermost line using the black relief. (ii) Omit the four
2030 corner pixels. */
2032 /* Top. */
2033 if (top_p)
2035 if (width == 1)
2036 XDrawLine (dpy, window, gc,
2037 left_x + (left_p ? 1 : 0), top_y,
2038 right_x + (right_p ? 0 : 1), top_y);
2040 for (i = 1; i < width; ++i)
2041 XDrawLine (dpy, window, gc,
2042 left_x + i * left_p, top_y + i,
2043 right_x + 1 - i * right_p, top_y + i);
2046 /* Left. */
2047 if (left_p)
2049 if (width == 1)
2050 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
2052 XClearArea (dpy, window, left_x, top_y, 1, 1, False);
2053 XClearArea (dpy, window, left_x, bottom_y, 1, 1, False);
2055 for (i = (width > 1 ? 1 : 0); i < width; ++i)
2056 XDrawLine (dpy, window, gc,
2057 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2060 XSetClipMask (dpy, gc, None);
2061 if (raised_p)
2062 gc = f->output_data.x->black_relief.gc;
2063 else
2064 gc = f->output_data.x->white_relief.gc;
2065 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2067 if (width > 1)
2069 /* Outermost top line. */
2070 if (top_p)
2071 XDrawLine (dpy, window, gc,
2072 left_x + (left_p ? 1 : 0), top_y,
2073 right_x + (right_p ? 0 : 1), top_y);
2075 /* Outermost left line. */
2076 if (left_p)
2077 XDrawLine (dpy, window, gc, left_x, top_y + 1, left_x, bottom_y);
2080 /* Bottom. */
2081 if (bot_p)
2083 XDrawLine (dpy, window, gc,
2084 left_x + (left_p ? 1 : 0), bottom_y,
2085 right_x + (right_p ? 0 : 1), bottom_y);
2086 for (i = 1; i < width; ++i)
2087 XDrawLine (dpy, window, gc,
2088 left_x + i * left_p, bottom_y - i,
2089 right_x + 1 - i * right_p, bottom_y - i);
2092 /* Right. */
2093 if (right_p)
2095 XClearArea (dpy, window, right_x, top_y, 1, 1, False);
2096 XClearArea (dpy, window, right_x, bottom_y, 1, 1, False);
2097 for (i = 0; i < width; ++i)
2098 XDrawLine (dpy, window, gc,
2099 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2102 XSetClipMask (dpy, gc, None);
2106 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2107 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2108 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2109 left side of the rectangle. RIGHT_P non-zero means draw a line
2110 on the right side of the rectangle. CLIP_RECT is the clipping
2111 rectangle to use when drawing. */
2113 static void
2114 x_draw_box_rect (struct glyph_string *s,
2115 int left_x, int top_y, int right_x, int bottom_y, int width,
2116 int left_p, int right_p, XRectangle *clip_rect)
2118 XGCValues xgcv;
2120 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2121 XSetForeground (s->display, s->gc, s->face->box_color);
2122 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2124 /* Top. */
2125 XFillRectangle (s->display, s->window, s->gc,
2126 left_x, top_y, right_x - left_x + 1, width);
2128 /* Left. */
2129 if (left_p)
2130 XFillRectangle (s->display, s->window, s->gc,
2131 left_x, top_y, width, bottom_y - top_y + 1);
2133 /* Bottom. */
2134 XFillRectangle (s->display, s->window, s->gc,
2135 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2137 /* Right. */
2138 if (right_p)
2139 XFillRectangle (s->display, s->window, s->gc,
2140 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2142 XSetForeground (s->display, s->gc, xgcv.foreground);
2143 XSetClipMask (s->display, s->gc, None);
2147 /* Draw a box around glyph string S. */
2149 static void
2150 x_draw_glyph_string_box (struct glyph_string *s)
2152 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2153 int left_p, right_p;
2154 struct glyph *last_glyph;
2155 XRectangle clip_rect;
2157 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2158 ? WINDOW_RIGHT_EDGE_X (s->w)
2159 : window_box_right (s->w, s->area));
2161 /* The glyph that may have a right box line. */
2162 last_glyph = (s->cmp || s->img
2163 ? s->first_glyph
2164 : s->first_glyph + s->nchars - 1);
2166 width = eabs (s->face->box_line_width);
2167 raised_p = s->face->box == FACE_RAISED_BOX;
2168 left_x = s->x;
2169 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2170 ? last_x - 1
2171 : min (last_x, s->x + s->background_width) - 1);
2172 top_y = s->y;
2173 bottom_y = top_y + s->height - 1;
2175 left_p = (s->first_glyph->left_box_line_p
2176 || (s->hl == DRAW_MOUSE_FACE
2177 && (s->prev == NULL
2178 || s->prev->hl != s->hl)));
2179 right_p = (last_glyph->right_box_line_p
2180 || (s->hl == DRAW_MOUSE_FACE
2181 && (s->next == NULL
2182 || s->next->hl != s->hl)));
2184 get_glyph_string_clip_rect (s, &clip_rect);
2186 if (s->face->box == FACE_SIMPLE_BOX)
2187 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2188 left_p, right_p, &clip_rect);
2189 else
2191 x_setup_relief_colors (s);
2192 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2193 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2198 /* Draw foreground of image glyph string S. */
2200 static void
2201 x_draw_image_foreground (struct glyph_string *s)
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->img->pixmap)
2222 if (s->img->mask)
2224 /* We can't set both a clip mask and use XSetClipRectangles
2225 because the latter also sets a clip mask. We also can't
2226 trust on the shape extension to be available
2227 (XShapeCombineRegion). So, compute the rectangle to draw
2228 manually. */
2229 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2230 | GCFunction);
2231 XGCValues xgcv;
2232 XRectangle clip_rect, image_rect, r;
2234 xgcv.clip_mask = s->img->mask;
2235 xgcv.clip_x_origin = x;
2236 xgcv.clip_y_origin = y;
2237 xgcv.function = GXcopy;
2238 XChangeGC (s->display, s->gc, mask, &xgcv);
2240 get_glyph_string_clip_rect (s, &clip_rect);
2241 image_rect.x = x;
2242 image_rect.y = y;
2243 image_rect.width = s->slice.width;
2244 image_rect.height = s->slice.height;
2245 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2246 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2247 s->slice.x + r.x - x, s->slice.y + r.y - y,
2248 r.width, r.height, r.x, r.y);
2250 else
2252 XRectangle clip_rect, image_rect, r;
2254 get_glyph_string_clip_rect (s, &clip_rect);
2255 image_rect.x = x;
2256 image_rect.y = y;
2257 image_rect.width = s->slice.width;
2258 image_rect.height = s->slice.height;
2259 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2260 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2261 s->slice.x + r.x - x, s->slice.y + r.y - y,
2262 r.width, r.height, r.x, r.y);
2264 /* When the image has a mask, we can expect that at
2265 least part of a mouse highlight or a block cursor will
2266 be visible. If the image doesn't have a mask, make
2267 a block cursor visible by drawing a rectangle around
2268 the image. I believe it's looking better if we do
2269 nothing here for mouse-face. */
2270 if (s->hl == DRAW_CURSOR)
2272 int relief = s->img->relief;
2273 if (relief < 0) relief = -relief;
2274 XDrawRectangle (s->display, s->window, s->gc,
2275 x - relief, y - relief,
2276 s->slice.width + relief*2 - 1,
2277 s->slice.height + relief*2 - 1);
2281 else
2282 /* Draw a rectangle if image could not be loaded. */
2283 XDrawRectangle (s->display, s->window, s->gc, x, y,
2284 s->slice.width - 1, s->slice.height - 1);
2288 /* Draw a relief around the image glyph string S. */
2290 static void
2291 x_draw_image_relief (struct glyph_string *s)
2293 int x0, y0, x1, y1, thick, raised_p, extra;
2294 XRectangle r;
2295 int x = s->x;
2296 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2298 /* If first glyph of S has a left box line, start drawing it to the
2299 right of that line. */
2300 if (s->face->box != FACE_NO_BOX
2301 && s->first_glyph->left_box_line_p
2302 && s->slice.x == 0)
2303 x += eabs (s->face->box_line_width);
2305 /* If there is a margin around the image, adjust x- and y-position
2306 by that margin. */
2307 if (s->slice.x == 0)
2308 x += s->img->hmargin;
2309 if (s->slice.y == 0)
2310 y += s->img->vmargin;
2312 if (s->hl == DRAW_IMAGE_SUNKEN
2313 || s->hl == DRAW_IMAGE_RAISED)
2315 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2316 raised_p = s->hl == DRAW_IMAGE_RAISED;
2318 else
2320 thick = eabs (s->img->relief);
2321 raised_p = s->img->relief > 0;
2324 extra = s->face->id == TOOL_BAR_FACE_ID
2325 ? XINT (Vtool_bar_button_margin) : 0;
2327 x0 = x - thick - extra;
2328 y0 = y - thick - extra;
2329 x1 = x + s->slice.width + thick - 1 + extra;
2330 y1 = y + s->slice.height + thick - 1 + extra;
2332 x_setup_relief_colors (s);
2333 get_glyph_string_clip_rect (s, &r);
2334 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2335 s->slice.y == 0,
2336 s->slice.y + s->slice.height == s->img->height,
2337 s->slice.x == 0,
2338 s->slice.x + s->slice.width == s->img->width,
2339 &r);
2343 /* Draw the foreground of image glyph string S to PIXMAP. */
2345 static void
2346 x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
2348 int x = 0;
2349 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2351 /* If first glyph of S has a left box line, start drawing it to the
2352 right of that line. */
2353 if (s->face->box != FACE_NO_BOX
2354 && s->first_glyph->left_box_line_p
2355 && s->slice.x == 0)
2356 x += eabs (s->face->box_line_width);
2358 /* If there is a margin around the image, adjust x- and y-position
2359 by that margin. */
2360 if (s->slice.x == 0)
2361 x += s->img->hmargin;
2362 if (s->slice.y == 0)
2363 y += s->img->vmargin;
2365 if (s->img->pixmap)
2367 if (s->img->mask)
2369 /* We can't set both a clip mask and use XSetClipRectangles
2370 because the latter also sets a clip mask. We also can't
2371 trust on the shape extension to be available
2372 (XShapeCombineRegion). So, compute the rectangle to draw
2373 manually. */
2374 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2375 | GCFunction);
2376 XGCValues xgcv;
2378 xgcv.clip_mask = s->img->mask;
2379 xgcv.clip_x_origin = x - s->slice.x;
2380 xgcv.clip_y_origin = y - s->slice.y;
2381 xgcv.function = GXcopy;
2382 XChangeGC (s->display, s->gc, mask, &xgcv);
2384 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2385 s->slice.x, s->slice.y,
2386 s->slice.width, s->slice.height, x, y);
2387 XSetClipMask (s->display, s->gc, None);
2389 else
2391 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2392 s->slice.x, s->slice.y,
2393 s->slice.width, s->slice.height, x, y);
2395 /* When the image has a mask, we can expect that at
2396 least part of a mouse highlight or a block cursor will
2397 be visible. If the image doesn't have a mask, make
2398 a block cursor visible by drawing a rectangle around
2399 the image. I believe it's looking better if we do
2400 nothing here for mouse-face. */
2401 if (s->hl == DRAW_CURSOR)
2403 int r = s->img->relief;
2404 if (r < 0) r = -r;
2405 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2406 s->slice.width + r*2 - 1,
2407 s->slice.height + r*2 - 1);
2411 else
2412 /* Draw a rectangle if image could not be loaded. */
2413 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2414 s->slice.width - 1, s->slice.height - 1);
2418 /* Draw part of the background of glyph string S. X, Y, W, and H
2419 give the rectangle to draw. */
2421 static void
2422 x_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y, int w, int h)
2424 if (s->stippled_p)
2426 /* Fill background with a stipple pattern. */
2427 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2428 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2429 XSetFillStyle (s->display, s->gc, FillSolid);
2431 else
2432 x_clear_glyph_string_rect (s, x, y, w, h);
2436 /* Draw image glyph string S.
2438 s->y
2439 s->x +-------------------------
2440 | s->face->box
2442 | +-------------------------
2443 | | s->img->margin
2445 | | +-------------------
2446 | | | the image
2450 static void
2451 x_draw_image_glyph_string (struct glyph_string *s)
2453 int box_line_hwidth = eabs (s->face->box_line_width);
2454 int box_line_vwidth = max (s->face->box_line_width, 0);
2455 int height;
2456 Pixmap pixmap = None;
2458 height = s->height;
2459 if (s->slice.y == 0)
2460 height -= box_line_vwidth;
2461 if (s->slice.y + s->slice.height >= s->img->height)
2462 height -= box_line_vwidth;
2464 /* Fill background with face under the image. Do it only if row is
2465 taller than image or if image has a clip mask to reduce
2466 flickering. */
2467 s->stippled_p = s->face->stipple != 0;
2468 if (height > s->slice.height
2469 || s->img->hmargin
2470 || s->img->vmargin
2471 || s->img->mask
2472 || s->img->pixmap == 0
2473 || s->width != s->background_width)
2475 if (s->img->mask)
2477 /* Create a pixmap as large as the glyph string. Fill it
2478 with the background color. Copy the image to it, using
2479 its mask. Copy the temporary pixmap to the display. */
2480 Screen *screen = FRAME_X_SCREEN (s->f);
2481 int depth = DefaultDepthOfScreen (screen);
2483 /* Create a pixmap as large as the glyph string. */
2484 pixmap = XCreatePixmap (s->display, s->window,
2485 s->background_width,
2486 s->height, depth);
2488 /* Don't clip in the following because we're working on the
2489 pixmap. */
2490 XSetClipMask (s->display, s->gc, None);
2492 /* Fill the pixmap with the background color/stipple. */
2493 if (s->stippled_p)
2495 /* Fill background with a stipple pattern. */
2496 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2497 XSetTSOrigin (s->display, s->gc, - s->x, - s->y);
2498 XFillRectangle (s->display, pixmap, s->gc,
2499 0, 0, s->background_width, s->height);
2500 XSetFillStyle (s->display, s->gc, FillSolid);
2501 XSetTSOrigin (s->display, s->gc, 0, 0);
2503 else
2505 XGCValues xgcv;
2506 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2507 &xgcv);
2508 XSetForeground (s->display, s->gc, xgcv.background);
2509 XFillRectangle (s->display, pixmap, s->gc,
2510 0, 0, s->background_width, s->height);
2511 XSetForeground (s->display, s->gc, xgcv.foreground);
2514 else
2516 int x = s->x;
2517 int y = s->y;
2519 if (s->first_glyph->left_box_line_p
2520 && s->slice.x == 0)
2521 x += box_line_hwidth;
2523 if (s->slice.y == 0)
2524 y += box_line_vwidth;
2526 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2529 s->background_filled_p = 1;
2532 /* Draw the foreground. */
2533 if (pixmap != None)
2535 x_draw_image_foreground_1 (s, pixmap);
2536 x_set_glyph_string_clipping (s);
2537 XCopyArea (s->display, pixmap, s->window, s->gc,
2538 0, 0, s->background_width, s->height, s->x, s->y);
2539 XFreePixmap (s->display, pixmap);
2541 else
2542 x_draw_image_foreground (s);
2544 /* If we must draw a relief around the image, do it. */
2545 if (s->img->relief
2546 || s->hl == DRAW_IMAGE_RAISED
2547 || s->hl == DRAW_IMAGE_SUNKEN)
2548 x_draw_image_relief (s);
2552 /* Draw stretch glyph string S. */
2554 static void
2555 x_draw_stretch_glyph_string (struct glyph_string *s)
2557 xassert (s->first_glyph->type == STRETCH_GLYPH);
2559 if (s->hl == DRAW_CURSOR
2560 && !x_stretch_cursor_p)
2562 /* If `x-stretch-cursor' is nil, don't draw a block cursor as
2563 wide as the stretch glyph. */
2564 int width, background_width = s->background_width;
2565 int x = s->x;
2567 if (!s->row->reversed_p)
2569 int left_x = window_box_left_offset (s->w, TEXT_AREA);
2571 if (x < left_x)
2573 background_width -= left_x - x;
2574 x = left_x;
2577 else
2579 /* In R2L rows, draw the cursor on the right edge of the
2580 stretch glyph. */
2581 int right_x = window_box_right_offset (s->w, TEXT_AREA);
2583 if (x + background_width > right_x)
2584 background_width -= x - right_x;
2585 x += background_width;
2587 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2588 if (s->row->reversed_p)
2589 x -= width;
2591 /* Draw cursor. */
2592 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2594 /* Clear rest using the GC of the original non-cursor face. */
2595 if (width < background_width)
2597 int y = s->y;
2598 int w = background_width - width, h = s->height;
2599 XRectangle r;
2600 GC gc;
2602 if (!s->row->reversed_p)
2603 x += width;
2604 else
2605 x = s->x;
2606 if (s->row->mouse_face_p
2607 && cursor_in_mouse_face_p (s->w))
2609 x_set_mouse_face_gc (s);
2610 gc = s->gc;
2612 else
2613 gc = s->face->gc;
2615 get_glyph_string_clip_rect (s, &r);
2616 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2618 if (s->face->stipple)
2620 /* Fill background with a stipple pattern. */
2621 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2622 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2623 XSetFillStyle (s->display, gc, FillSolid);
2625 else
2627 XGCValues xgcv;
2628 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2629 XSetForeground (s->display, gc, xgcv.background);
2630 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2631 XSetForeground (s->display, gc, xgcv.foreground);
2635 else if (!s->background_filled_p)
2637 int background_width = s->background_width;
2638 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2640 /* Don't draw into left margin, fringe or scrollbar area
2641 except for header line and mode line. */
2642 if (x < left_x && !s->row->mode_line_p)
2644 background_width -= left_x - x;
2645 x = left_x;
2647 if (background_width > 0)
2648 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2651 s->background_filled_p = 1;
2655 /* Draw glyph string S. */
2657 static void
2658 x_draw_glyph_string (struct glyph_string *s)
2660 int relief_drawn_p = 0;
2662 /* If S draws into the background of its successors, draw the
2663 background of the successors first so that S can draw into it.
2664 This makes S->next use XDrawString instead of XDrawImageString. */
2665 if (s->next && s->right_overhang && !s->for_overlaps)
2667 int width;
2668 struct glyph_string *next;
2670 for (width = 0, next = s->next;
2671 next && width < s->right_overhang;
2672 width += next->width, next = next->next)
2673 if (next->first_glyph->type != IMAGE_GLYPH)
2675 x_set_glyph_string_gc (next);
2676 x_set_glyph_string_clipping (next);
2677 if (next->first_glyph->type == STRETCH_GLYPH)
2678 x_draw_stretch_glyph_string (next);
2679 else
2680 x_draw_glyph_string_background (next, 1);
2681 next->num_clips = 0;
2685 /* Set up S->gc, set clipping and draw S. */
2686 x_set_glyph_string_gc (s);
2688 /* Draw relief (if any) in advance for char/composition so that the
2689 glyph string can be drawn over it. */
2690 if (!s->for_overlaps
2691 && s->face->box != FACE_NO_BOX
2692 && (s->first_glyph->type == CHAR_GLYPH
2693 || s->first_glyph->type == COMPOSITE_GLYPH))
2696 x_set_glyph_string_clipping (s);
2697 x_draw_glyph_string_background (s, 1);
2698 x_draw_glyph_string_box (s);
2699 x_set_glyph_string_clipping (s);
2700 relief_drawn_p = 1;
2702 else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
2703 && !s->clip_tail
2704 && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2705 || (s->next && s->next->hl != s->hl && s->right_overhang)))
2706 /* We must clip just this glyph. left_overhang part has already
2707 drawn when s->prev was drawn, and right_overhang part will be
2708 drawn later when s->next is drawn. */
2709 x_set_glyph_string_clipping_exactly (s, s);
2710 else
2711 x_set_glyph_string_clipping (s);
2713 switch (s->first_glyph->type)
2715 case IMAGE_GLYPH:
2716 x_draw_image_glyph_string (s);
2717 break;
2718 #ifdef HAVE_XWIDGETS
2719 case XWIDGET_GLYPH:
2720 //erase xwidget background
2721 //x_draw_glyph_string_background (s, 0);
2722 x_draw_xwidget_glyph_string (s);
2723 break;
2724 #endif
2725 case STRETCH_GLYPH:
2726 x_draw_stretch_glyph_string (s);
2727 break;
2729 case CHAR_GLYPH:
2730 if (s->for_overlaps)
2731 s->background_filled_p = 1;
2732 else
2733 x_draw_glyph_string_background (s, 0);
2734 x_draw_glyph_string_foreground (s);
2735 break;
2737 case COMPOSITE_GLYPH:
2738 if (s->for_overlaps || (s->cmp_from > 0
2739 && ! s->first_glyph->u.cmp.automatic))
2740 s->background_filled_p = 1;
2741 else
2742 x_draw_glyph_string_background (s, 1);
2743 x_draw_composite_glyph_string_foreground (s);
2744 break;
2746 case GLYPHLESS_GLYPH:
2747 if (s->for_overlaps)
2748 s->background_filled_p = 1;
2749 else
2750 x_draw_glyph_string_background (s, 1);
2751 x_draw_glyphless_glyph_string_foreground (s);
2752 break;
2754 default:
2755 abort ();
2758 if (!s->for_overlaps)
2760 /* Draw underline. */
2761 if (s->face->underline_p)
2763 unsigned long thickness, position;
2764 int y;
2766 if (s->prev && s->prev->face->underline_p)
2768 /* We use the same underline style as the previous one. */
2769 thickness = s->prev->underline_thickness;
2770 position = s->prev->underline_position;
2772 else
2774 /* Get the underline thickness. Default is 1 pixel. */
2775 if (s->font && s->font->underline_thickness > 0)
2776 thickness = s->font->underline_thickness;
2777 else
2778 thickness = 1;
2779 if (x_underline_at_descent_line)
2780 position = (s->height - thickness) - (s->ybase - s->y);
2781 else
2783 /* Get the underline position. This is the recommended
2784 vertical offset in pixels from the baseline to the top of
2785 the underline. This is a signed value according to the
2786 specs, and its default is
2788 ROUND ((maximum descent) / 2), with
2789 ROUND(x) = floor (x + 0.5) */
2791 if (x_use_underline_position_properties
2792 && s->font && s->font->underline_position >= 0)
2793 position = s->font->underline_position;
2794 else if (s->font)
2795 position = (s->font->descent + 1) / 2;
2796 else
2797 position = underline_minimum_offset;
2799 position = max (position, underline_minimum_offset);
2801 /* Check the sanity of thickness and position. We should
2802 avoid drawing underline out of the current line area. */
2803 if (s->y + s->height <= s->ybase + position)
2804 position = (s->height - 1) - (s->ybase - s->y);
2805 if (s->y + s->height < s->ybase + position + thickness)
2806 thickness = (s->y + s->height) - (s->ybase + position);
2807 s->underline_thickness = thickness;
2808 s->underline_position = position;
2809 y = s->ybase + position;
2810 if (s->face->underline_defaulted_p)
2811 XFillRectangle (s->display, s->window, s->gc,
2812 s->x, y, s->width, thickness);
2813 else
2815 XGCValues xgcv;
2816 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2817 XSetForeground (s->display, s->gc, s->face->underline_color);
2818 XFillRectangle (s->display, s->window, s->gc,
2819 s->x, y, s->width, thickness);
2820 XSetForeground (s->display, s->gc, xgcv.foreground);
2824 /* Draw overline. */
2825 if (s->face->overline_p)
2827 unsigned long dy = 0, h = 1;
2829 if (s->face->overline_color_defaulted_p)
2830 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2831 s->width, h);
2832 else
2834 XGCValues xgcv;
2835 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2836 XSetForeground (s->display, s->gc, s->face->overline_color);
2837 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2838 s->width, h);
2839 XSetForeground (s->display, s->gc, xgcv.foreground);
2843 /* Draw strike-through. */
2844 if (s->face->strike_through_p)
2846 unsigned long h = 1;
2847 unsigned long dy = (s->height - h) / 2;
2849 if (s->face->strike_through_color_defaulted_p)
2850 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2851 s->width, h);
2852 else
2854 XGCValues xgcv;
2855 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2856 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2857 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2858 s->width, h);
2859 XSetForeground (s->display, s->gc, xgcv.foreground);
2863 /* Draw relief if not yet drawn. */
2864 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2865 x_draw_glyph_string_box (s);
2867 if (s->prev)
2869 struct glyph_string *prev;
2871 for (prev = s->prev; prev; prev = prev->prev)
2872 if (prev->hl != s->hl
2873 && prev->x + prev->width + prev->right_overhang > s->x)
2875 /* As prev was drawn while clipped to its own area, we
2876 must draw the right_overhang part using s->hl now. */
2877 enum draw_glyphs_face save = prev->hl;
2879 prev->hl = s->hl;
2880 x_set_glyph_string_gc (prev);
2881 x_set_glyph_string_clipping_exactly (s, prev);
2882 if (prev->first_glyph->type == CHAR_GLYPH)
2883 x_draw_glyph_string_foreground (prev);
2884 else
2885 x_draw_composite_glyph_string_foreground (prev);
2886 XSetClipMask (prev->display, prev->gc, None);
2887 prev->hl = save;
2888 prev->num_clips = 0;
2892 if (s->next)
2894 struct glyph_string *next;
2896 for (next = s->next; next; next = next->next)
2897 if (next->hl != s->hl
2898 && next->x - next->left_overhang < s->x + s->width)
2900 /* As next will be drawn while clipped to its own area,
2901 we must draw the left_overhang part using s->hl now. */
2902 enum draw_glyphs_face save = next->hl;
2904 next->hl = s->hl;
2905 x_set_glyph_string_gc (next);
2906 x_set_glyph_string_clipping_exactly (s, next);
2907 if (next->first_glyph->type == CHAR_GLYPH)
2908 x_draw_glyph_string_foreground (next);
2909 else
2910 x_draw_composite_glyph_string_foreground (next);
2911 XSetClipMask (next->display, next->gc, None);
2912 next->hl = save;
2913 next->num_clips = 0;
2918 /* Reset clipping. */
2919 XSetClipMask (s->display, s->gc, None);
2920 s->num_clips = 0;
2923 /* Shift display to make room for inserted glyphs. */
2925 static void
2926 x_shift_glyphs_for_insert (struct frame *f, int x, int y, int width, int height, int shift_by)
2928 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
2929 f->output_data.x->normal_gc,
2930 x, y, width, height,
2931 x + shift_by, y);
2934 /* Delete N glyphs at the nominal cursor position. Not implemented
2935 for X frames. */
2937 static void
2938 x_delete_glyphs (struct frame *f, register int n)
2940 abort ();
2944 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
2945 If they are <= 0, this is probably an error. */
2947 void
2948 x_clear_area (Display *dpy, Window window, int x, int y, int width, int height, int exposures)
2950 xassert (width > 0 && height > 0);
2951 XClearArea (dpy, window, x, y, width, height, exposures);
2955 /* Clear an entire frame. */
2957 static void
2958 x_clear_frame (struct frame *f)
2960 /* Clearing the frame will erase any cursor, so mark them all as no
2961 longer visible. */
2962 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
2963 output_cursor.hpos = output_cursor.vpos = 0;
2964 output_cursor.x = -1;
2966 /* We don't set the output cursor here because there will always
2967 follow an explicit cursor_to. */
2968 BLOCK_INPUT;
2970 /* The following call is commented out because it does not seem to accomplish
2971 anything, apart from causing flickering during window resize. */
2972 /* XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f)); */
2974 /* We have to clear the scroll bars. If we have changed colors or
2975 something like that, then they should be notified. */
2976 x_scroll_bar_clear (f);
2978 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
2979 /* Make sure scroll bars are redrawn. As they aren't redrawn by
2980 redisplay, do it here. */
2981 if (FRAME_GTK_WIDGET (f))
2982 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
2983 #endif
2985 XFlush (FRAME_X_DISPLAY (f));
2987 UNBLOCK_INPUT;
2992 /* Invert the middle quarter of the frame for .15 sec. */
2994 /* We use the select system call to do the waiting, so we have to make
2995 sure it's available. If it isn't, we just won't do visual bells. */
2997 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3000 /* Subtract the `struct timeval' values X and Y, storing the result in
3001 *RESULT. Return 1 if the difference is negative, otherwise 0. */
3003 static int
3004 timeval_subtract (struct timeval *result, struct timeval x, struct timeval y)
3006 /* Perform the carry for the later subtraction by updating y. This
3007 is safer because on some systems the tv_sec member is unsigned. */
3008 if (x.tv_usec < y.tv_usec)
3010 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
3011 y.tv_usec -= 1000000 * nsec;
3012 y.tv_sec += nsec;
3015 if (x.tv_usec - y.tv_usec > 1000000)
3017 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
3018 y.tv_usec += 1000000 * nsec;
3019 y.tv_sec -= nsec;
3022 /* Compute the time remaining to wait. tv_usec is certainly
3023 positive. */
3024 result->tv_sec = x.tv_sec - y.tv_sec;
3025 result->tv_usec = x.tv_usec - y.tv_usec;
3027 /* Return indication of whether the result should be considered
3028 negative. */
3029 return x.tv_sec < y.tv_sec;
3032 static void
3033 XTflash (struct frame *f)
3035 BLOCK_INPUT;
3038 #ifdef USE_GTK
3039 /* Use Gdk routines to draw. This way, we won't draw over scroll bars
3040 when the scroll bars and the edit widget share the same X window. */
3041 GdkWindow *window = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
3042 #ifdef HAVE_GTK3
3043 cairo_t *cr = gdk_cairo_create (window);
3044 cairo_set_source_rgb (cr, 1, 1, 1);
3045 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
3046 #define XFillRectangle(d, win, gc, x, y, w, h) \
3047 do { \
3048 cairo_rectangle (cr, x, y, w, h); \
3049 cairo_fill (cr); \
3051 while (0)
3052 #else /* ! HAVE_GTK3 */
3053 GdkGCValues vals;
3054 GdkGC *gc;
3055 vals.foreground.pixel = (FRAME_FOREGROUND_PIXEL (f)
3056 ^ FRAME_BACKGROUND_PIXEL (f));
3057 vals.function = GDK_XOR;
3058 gc = gdk_gc_new_with_values (window,
3059 &vals, GDK_GC_FUNCTION | GDK_GC_FOREGROUND);
3060 #define XFillRectangle(d, win, gc, x, y, w, h) \
3061 gdk_draw_rectangle (window, gc, TRUE, x, y, w, h)
3062 #endif /* ! HAVE_GTK3 */
3063 #else /* ! USE_GTK */
3064 GC gc;
3066 /* Create a GC that will use the GXxor function to flip foreground
3067 pixels into background pixels. */
3069 XGCValues values;
3071 values.function = GXxor;
3072 values.foreground = (FRAME_FOREGROUND_PIXEL (f)
3073 ^ FRAME_BACKGROUND_PIXEL (f));
3075 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3076 GCFunction | GCForeground, &values);
3078 #endif
3080 /* Get the height not including a menu bar widget. */
3081 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
3082 /* Height of each line to flash. */
3083 int flash_height = FRAME_LINE_HEIGHT (f);
3084 /* These will be the left and right margins of the rectangles. */
3085 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3086 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3088 int width;
3090 /* Don't flash the area between a scroll bar and the frame
3091 edge it is next to. */
3092 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3094 case vertical_scroll_bar_left:
3095 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3096 break;
3098 case vertical_scroll_bar_right:
3099 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3100 break;
3102 default:
3103 break;
3106 width = flash_right - flash_left;
3108 /* If window is tall, flash top and bottom line. */
3109 if (height > 3 * FRAME_LINE_HEIGHT (f))
3111 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3112 flash_left,
3113 (FRAME_INTERNAL_BORDER_WIDTH (f)
3114 + FRAME_TOP_MARGIN_HEIGHT (f)),
3115 width, flash_height);
3116 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3117 flash_left,
3118 (height - flash_height
3119 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3120 width, flash_height);
3123 else
3124 /* If it is short, flash it all. */
3125 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3126 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3127 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3129 x_flush (f);
3132 struct timeval wakeup;
3134 EMACS_GET_TIME (wakeup);
3136 /* Compute time to wait until, propagating carry from usecs. */
3137 wakeup.tv_usec += 150000;
3138 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3139 wakeup.tv_usec %= 1000000;
3141 /* Keep waiting until past the time wakeup or any input gets
3142 available. */
3143 while (! detect_input_pending ())
3145 struct timeval current;
3146 struct timeval timeout;
3148 EMACS_GET_TIME (current);
3150 /* Break if result would be negative. */
3151 if (timeval_subtract (&current, wakeup, current))
3152 break;
3154 /* How long `select' should wait. */
3155 timeout.tv_sec = 0;
3156 timeout.tv_usec = 10000;
3158 /* Try to wait that long--but we might wake up sooner. */
3159 select (0, NULL, NULL, NULL, &timeout);
3163 /* If window is tall, flash top and bottom line. */
3164 if (height > 3 * FRAME_LINE_HEIGHT (f))
3166 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3167 flash_left,
3168 (FRAME_INTERNAL_BORDER_WIDTH (f)
3169 + FRAME_TOP_MARGIN_HEIGHT (f)),
3170 width, flash_height);
3171 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3172 flash_left,
3173 (height - flash_height
3174 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3175 width, flash_height);
3177 else
3178 /* If it is short, flash it all. */
3179 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3180 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3181 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3183 #ifdef USE_GTK
3184 #ifdef HAVE_GTK3
3185 cairo_destroy (cr);
3186 #else
3187 g_object_unref (G_OBJECT (gc));
3188 #endif
3189 #undef XFillRectangle
3190 #else
3191 XFreeGC (FRAME_X_DISPLAY (f), gc);
3192 #endif
3193 x_flush (f);
3197 UNBLOCK_INPUT;
3200 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3203 static void
3204 XTtoggle_invisible_pointer (FRAME_PTR f, int invisible)
3206 BLOCK_INPUT;
3207 if (invisible)
3209 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor != 0)
3210 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3211 FRAME_X_DISPLAY_INFO (f)->invisible_cursor);
3213 else
3214 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3215 f->output_data.x->current_cursor);
3216 f->pointer_invisible = invisible;
3217 UNBLOCK_INPUT;
3221 /* Make audible bell. */
3223 static void
3224 XTring_bell (struct frame *f)
3226 if (FRAME_X_DISPLAY (f))
3228 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3229 if (visible_bell)
3230 XTflash (f);
3231 else
3232 #endif
3234 BLOCK_INPUT;
3235 XBell (FRAME_X_DISPLAY (f), 0);
3236 XFlush (FRAME_X_DISPLAY (f));
3237 UNBLOCK_INPUT;
3243 /* Specify how many text lines, from the top of the window,
3244 should be affected by insert-lines and delete-lines operations.
3245 This, and those operations, are used only within an update
3246 that is bounded by calls to x_update_begin and x_update_end. */
3248 static void
3249 XTset_terminal_window (struct frame *f, int n)
3251 /* This function intentionally left blank. */
3256 /***********************************************************************
3257 Line Dance
3258 ***********************************************************************/
3260 /* Perform an insert-lines or delete-lines operation, inserting N
3261 lines or deleting -N lines at vertical position VPOS. */
3263 static void
3264 x_ins_del_lines (struct frame *f, int vpos, int n)
3266 abort ();
3270 /* Scroll part of the display as described by RUN. */
3272 static void
3273 x_scroll_run (struct window *w, struct run *run)
3275 struct frame *f = XFRAME (w->frame);
3276 int x, y, width, height, from_y, to_y, bottom_y;
3278 /* Get frame-relative bounding box of the text display area of W,
3279 without mode lines. Include in this box the left and right
3280 fringe of W. */
3281 window_box (w, -1, &x, &y, &width, &height);
3283 #ifdef USE_TOOLKIT_SCROLL_BARS
3284 /* If the fringe is adjacent to the left (right) scroll bar of a
3285 leftmost (rightmost, respectively) window, then extend its
3286 background to the gap between the fringe and the bar. */
3287 if ((WINDOW_LEFTMOST_P (w)
3288 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
3289 || (WINDOW_RIGHTMOST_P (w)
3290 && WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w)))
3292 int sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
3294 if (sb_width > 0)
3296 int bar_area_x = WINDOW_SCROLL_BAR_AREA_X (w);
3297 int bar_area_width = (WINDOW_CONFIG_SCROLL_BAR_COLS (w)
3298 * FRAME_COLUMN_WIDTH (f));
3300 if (bar_area_x + bar_area_width == x)
3302 x = bar_area_x + sb_width;
3303 width += bar_area_width - sb_width;
3305 else if (x + width == bar_area_x)
3306 width += bar_area_width - sb_width;
3309 #endif
3311 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3312 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3313 bottom_y = y + height;
3315 if (to_y < from_y)
3317 /* Scrolling up. Make sure we don't copy part of the mode
3318 line at the bottom. */
3319 if (from_y + run->height > bottom_y)
3320 height = bottom_y - from_y;
3321 else
3322 height = run->height;
3324 else
3326 /* Scolling down. Make sure we don't copy over the mode line.
3327 at the bottom. */
3328 if (to_y + run->height > bottom_y)
3329 height = bottom_y - to_y;
3330 else
3331 height = run->height;
3334 BLOCK_INPUT;
3336 /* Cursor off. Will be switched on again in x_update_window_end. */
3337 updated_window = w;
3338 x_clear_cursor (w);
3340 XCopyArea (FRAME_X_DISPLAY (f),
3341 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3342 f->output_data.x->normal_gc,
3343 x, from_y,
3344 width, height,
3345 x, to_y);
3347 UNBLOCK_INPUT;
3352 /***********************************************************************
3353 Exposure Events
3354 ***********************************************************************/
3357 static void
3358 frame_highlight (struct frame *f)
3360 /* We used to only do this if Vx_no_window_manager was non-nil, but
3361 the ICCCM (section 4.1.6) says that the window's border pixmap
3362 and border pixel are window attributes which are "private to the
3363 client", so we can always change it to whatever we want. */
3364 BLOCK_INPUT;
3365 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3366 f->output_data.x->border_pixel);
3367 UNBLOCK_INPUT;
3368 x_update_cursor (f, 1);
3369 x_set_frame_alpha (f);
3372 static void
3373 frame_unhighlight (struct frame *f)
3375 /* We used to only do this if Vx_no_window_manager was non-nil, but
3376 the ICCCM (section 4.1.6) says that the window's border pixmap
3377 and border pixel are window attributes which are "private to the
3378 client", so we can always change it to whatever we want. */
3379 BLOCK_INPUT;
3380 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3381 f->output_data.x->border_tile);
3382 UNBLOCK_INPUT;
3383 x_update_cursor (f, 1);
3384 x_set_frame_alpha (f);
3387 /* The focus has changed. Update the frames as necessary to reflect
3388 the new situation. Note that we can't change the selected frame
3389 here, because the Lisp code we are interrupting might become confused.
3390 Each event gets marked with the frame in which it occurred, so the
3391 Lisp code can tell when the switch took place by examining the events. */
3393 static void
3394 x_new_focus_frame (struct x_display_info *dpyinfo, struct frame *frame)
3396 struct frame *old_focus = dpyinfo->x_focus_frame;
3398 if (frame != dpyinfo->x_focus_frame)
3400 /* Set this before calling other routines, so that they see
3401 the correct value of x_focus_frame. */
3402 dpyinfo->x_focus_frame = frame;
3404 if (old_focus && old_focus->auto_lower)
3405 x_lower_frame (old_focus);
3407 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3408 pending_autoraise_frame = dpyinfo->x_focus_frame;
3409 else
3410 pending_autoraise_frame = 0;
3413 x_frame_rehighlight (dpyinfo);
3416 /* Handle FocusIn and FocusOut state changes for FRAME.
3417 If FRAME has focus and there exists more than one frame, puts
3418 a FOCUS_IN_EVENT into *BUFP. */
3420 static void
3421 x_focus_changed (int type, int state, struct x_display_info *dpyinfo, struct frame *frame, struct input_event *bufp)
3423 if (type == FocusIn)
3425 if (dpyinfo->x_focus_event_frame != frame)
3427 x_new_focus_frame (dpyinfo, frame);
3428 dpyinfo->x_focus_event_frame = frame;
3430 /* Don't stop displaying the initial startup message
3431 for a switch-frame event we don't need. */
3432 if (NILP (Vterminal_frame)
3433 && CONSP (Vframe_list)
3434 && !NILP (XCDR (Vframe_list)))
3436 bufp->kind = FOCUS_IN_EVENT;
3437 XSETFRAME (bufp->frame_or_window, frame);
3441 frame->output_data.x->focus_state |= state;
3443 #ifdef HAVE_X_I18N
3444 if (FRAME_XIC (frame))
3445 XSetICFocus (FRAME_XIC (frame));
3446 #endif
3448 else if (type == FocusOut)
3450 frame->output_data.x->focus_state &= ~state;
3452 if (dpyinfo->x_focus_event_frame == frame)
3454 dpyinfo->x_focus_event_frame = 0;
3455 x_new_focus_frame (dpyinfo, 0);
3458 #ifdef HAVE_X_I18N
3459 if (FRAME_XIC (frame))
3460 XUnsetICFocus (FRAME_XIC (frame));
3461 #endif
3462 if (frame->pointer_invisible)
3463 XTtoggle_invisible_pointer (frame, 0);
3467 /* The focus may have changed. Figure out if it is a real focus change,
3468 by checking both FocusIn/Out and Enter/LeaveNotify events.
3470 Returns FOCUS_IN_EVENT event in *BUFP. */
3472 static void
3473 x_detect_focus_change (struct x_display_info *dpyinfo, XEvent *event, struct input_event *bufp)
3475 struct frame *frame;
3477 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3478 if (! frame)
3479 return;
3481 switch (event->type)
3483 case EnterNotify:
3484 case LeaveNotify:
3486 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3487 int focus_state
3488 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3490 if (event->xcrossing.detail != NotifyInferior
3491 && event->xcrossing.focus
3492 && ! (focus_state & FOCUS_EXPLICIT))
3493 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3494 FOCUS_IMPLICIT,
3495 dpyinfo, frame, bufp);
3497 break;
3499 case FocusIn:
3500 case FocusOut:
3501 x_focus_changed (event->type,
3502 (event->xfocus.detail == NotifyPointer ?
3503 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3504 dpyinfo, frame, bufp);
3505 break;
3507 case ClientMessage:
3508 if (event->xclient.message_type == dpyinfo->Xatom_XEMBED)
3510 enum xembed_message msg = event->xclient.data.l[1];
3511 x_focus_changed ((msg == XEMBED_FOCUS_IN ? FocusIn : FocusOut),
3512 FOCUS_EXPLICIT, dpyinfo, frame, bufp);
3514 break;
3519 #if defined HAVE_MENUS && !defined USE_X_TOOLKIT && !defined USE_GTK
3520 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3522 void
3523 x_mouse_leave (struct x_display_info *dpyinfo)
3525 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3527 #endif
3529 /* The focus has changed, or we have redirected a frame's focus to
3530 another frame (this happens when a frame uses a surrogate
3531 mini-buffer frame). Shift the highlight as appropriate.
3533 The FRAME argument doesn't necessarily have anything to do with which
3534 frame is being highlighted or un-highlighted; we only use it to find
3535 the appropriate X display info. */
3537 static void
3538 XTframe_rehighlight (struct frame *frame)
3540 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3543 static void
3544 x_frame_rehighlight (struct x_display_info *dpyinfo)
3546 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3548 if (dpyinfo->x_focus_frame)
3550 dpyinfo->x_highlight_frame
3551 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3552 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3553 : dpyinfo->x_focus_frame);
3554 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3556 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3557 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3560 else
3561 dpyinfo->x_highlight_frame = 0;
3563 if (dpyinfo->x_highlight_frame != old_highlight)
3565 if (old_highlight)
3566 frame_unhighlight (old_highlight);
3567 if (dpyinfo->x_highlight_frame)
3568 frame_highlight (dpyinfo->x_highlight_frame);
3574 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3576 /* Initialize mode_switch_bit and modifier_meaning. */
3577 static void
3578 x_find_modifier_meanings (struct x_display_info *dpyinfo)
3580 int min_code, max_code;
3581 KeySym *syms;
3582 int syms_per_code;
3583 XModifierKeymap *mods;
3585 dpyinfo->meta_mod_mask = 0;
3586 dpyinfo->shift_lock_mask = 0;
3587 dpyinfo->alt_mod_mask = 0;
3588 dpyinfo->super_mod_mask = 0;
3589 dpyinfo->hyper_mod_mask = 0;
3591 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3593 syms = XGetKeyboardMapping (dpyinfo->display,
3594 min_code, max_code - min_code + 1,
3595 &syms_per_code);
3596 mods = XGetModifierMapping (dpyinfo->display);
3598 /* Scan the modifier table to see which modifier bits the Meta and
3599 Alt keysyms are on. */
3601 int row, col; /* The row and column in the modifier table. */
3602 int found_alt_or_meta;
3604 for (row = 3; row < 8; row++)
3606 found_alt_or_meta = 0;
3607 for (col = 0; col < mods->max_keypermod; col++)
3609 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3611 /* Zeroes are used for filler. Skip them. */
3612 if (code == 0)
3613 continue;
3615 /* Are any of this keycode's keysyms a meta key? */
3617 int code_col;
3619 for (code_col = 0; code_col < syms_per_code; code_col++)
3621 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3623 switch (sym)
3625 case XK_Meta_L:
3626 case XK_Meta_R:
3627 found_alt_or_meta = 1;
3628 dpyinfo->meta_mod_mask |= (1 << row);
3629 break;
3631 case XK_Alt_L:
3632 case XK_Alt_R:
3633 found_alt_or_meta = 1;
3634 dpyinfo->alt_mod_mask |= (1 << row);
3635 break;
3637 case XK_Hyper_L:
3638 case XK_Hyper_R:
3639 if (!found_alt_or_meta)
3640 dpyinfo->hyper_mod_mask |= (1 << row);
3641 code_col = syms_per_code;
3642 col = mods->max_keypermod;
3643 break;
3645 case XK_Super_L:
3646 case XK_Super_R:
3647 if (!found_alt_or_meta)
3648 dpyinfo->super_mod_mask |= (1 << row);
3649 code_col = syms_per_code;
3650 col = mods->max_keypermod;
3651 break;
3653 case XK_Shift_Lock:
3654 /* Ignore this if it's not on the lock modifier. */
3655 if (!found_alt_or_meta && ((1 << row) == LockMask))
3656 dpyinfo->shift_lock_mask = LockMask;
3657 code_col = syms_per_code;
3658 col = mods->max_keypermod;
3659 break;
3667 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3668 if (! dpyinfo->meta_mod_mask)
3670 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3671 dpyinfo->alt_mod_mask = 0;
3674 /* If some keys are both alt and meta,
3675 make them just meta, not alt. */
3676 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3678 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3681 XFree ((char *) syms);
3682 XFreeModifiermap (mods);
3685 /* Convert between the modifier bits X uses and the modifier bits
3686 Emacs uses. */
3688 EMACS_INT
3689 x_x_to_emacs_modifiers (struct x_display_info *dpyinfo, int state)
3691 EMACS_INT mod_meta = meta_modifier;
3692 EMACS_INT mod_alt = alt_modifier;
3693 EMACS_INT mod_hyper = hyper_modifier;
3694 EMACS_INT mod_super = super_modifier;
3695 Lisp_Object tem;
3697 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3698 if (INTEGERP (tem)) mod_alt = XINT (tem);
3699 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3700 if (INTEGERP (tem)) mod_meta = XINT (tem);
3701 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3702 if (INTEGERP (tem)) mod_hyper = XINT (tem);
3703 tem = Fget (Vx_super_keysym, Qmodifier_value);
3704 if (INTEGERP (tem)) mod_super = XINT (tem);
3707 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3708 | ((state & ControlMask) ? ctrl_modifier : 0)
3709 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3710 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3711 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3712 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3715 static int
3716 x_emacs_to_x_modifiers (struct x_display_info *dpyinfo, EMACS_INT state)
3718 int mod_meta = meta_modifier;
3719 int mod_alt = alt_modifier;
3720 int mod_hyper = hyper_modifier;
3721 int mod_super = super_modifier;
3723 Lisp_Object tem;
3725 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3726 if (INTEGERP (tem)) mod_alt = XINT (tem);
3727 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3728 if (INTEGERP (tem)) mod_meta = XINT (tem);
3729 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3730 if (INTEGERP (tem)) mod_hyper = XINT (tem);
3731 tem = Fget (Vx_super_keysym, Qmodifier_value);
3732 if (INTEGERP (tem)) mod_super = XINT (tem);
3735 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3736 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3737 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3738 | ((state & shift_modifier) ? ShiftMask : 0)
3739 | ((state & ctrl_modifier) ? ControlMask : 0)
3740 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3743 /* Convert a keysym to its name. */
3745 char *
3746 x_get_keysym_name (int keysym)
3748 char *value;
3750 BLOCK_INPUT;
3751 value = XKeysymToString (keysym);
3752 UNBLOCK_INPUT;
3754 return value;
3759 /* Mouse clicks and mouse movement. Rah. */
3761 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3763 If the event is a button press, then note that we have grabbed
3764 the mouse. */
3766 static Lisp_Object
3767 construct_mouse_click (struct input_event *result, XButtonEvent *event, struct frame *f)
3769 /* Make the event type NO_EVENT; we'll change that when we decide
3770 otherwise. */
3771 result->kind = MOUSE_CLICK_EVENT;
3772 result->code = event->button - Button1;
3773 result->timestamp = event->time;
3774 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3775 event->state)
3776 | (event->type == ButtonRelease
3777 ? up_modifier
3778 : down_modifier));
3780 XSETINT (result->x, event->x);
3781 XSETINT (result->y, event->y);
3782 XSETFRAME (result->frame_or_window, f);
3783 result->arg = Qnil;
3784 return Qnil;
3788 /* Function to report a mouse movement to the mainstream Emacs code.
3789 The input handler calls this.
3791 We have received a mouse movement event, which is given in *event.
3792 If the mouse is over a different glyph than it was last time, tell
3793 the mainstream emacs code by setting mouse_moved. If not, ask for
3794 another motion event, so we can check again the next time it moves. */
3796 static XMotionEvent last_mouse_motion_event;
3797 static Lisp_Object last_mouse_motion_frame;
3799 static int
3800 note_mouse_movement (FRAME_PTR frame, XMotionEvent *event)
3802 last_mouse_movement_time = event->time;
3803 last_mouse_motion_event = *event;
3804 XSETFRAME (last_mouse_motion_frame, frame);
3806 if (!FRAME_X_OUTPUT (frame))
3807 return 0;
3809 if (event->window != FRAME_X_WINDOW (frame))
3811 frame->mouse_moved = 1;
3812 last_mouse_scroll_bar = Qnil;
3813 note_mouse_highlight (frame, -1, -1);
3814 last_mouse_glyph_frame = 0;
3815 return 1;
3819 /* Has the mouse moved off the glyph it was on at the last sighting? */
3820 if (frame != last_mouse_glyph_frame
3821 || event->x < last_mouse_glyph.x
3822 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3823 || event->y < last_mouse_glyph.y
3824 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3826 frame->mouse_moved = 1;
3827 last_mouse_scroll_bar = Qnil;
3828 note_mouse_highlight (frame, event->x, event->y);
3829 /* Remember which glyph we're now on. */
3830 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3831 last_mouse_glyph_frame = frame;
3832 return 1;
3835 return 0;
3839 /************************************************************************
3840 Mouse Face
3841 ************************************************************************/
3843 static void
3844 redo_mouse_highlight (void)
3846 if (!NILP (last_mouse_motion_frame)
3847 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3848 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3849 last_mouse_motion_event.x,
3850 last_mouse_motion_event.y);
3855 /* Return the current position of the mouse.
3856 *FP should be a frame which indicates which display to ask about.
3858 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3859 and *PART to the frame, window, and scroll bar part that the mouse
3860 is over. Set *X and *Y to the portion and whole of the mouse's
3861 position on the scroll bar.
3863 If the mouse movement started elsewhere, set *FP to the frame the
3864 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3865 the mouse is over.
3867 Set *TIMESTAMP to the server time-stamp for the time at which the mouse
3868 was at this position.
3870 Don't store anything if we don't have a valid set of values to report.
3872 This clears the mouse_moved flag, so we can wait for the next mouse
3873 movement. */
3875 static void
3876 XTmouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
3877 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
3878 Time *timestamp)
3880 FRAME_PTR f1;
3882 BLOCK_INPUT;
3884 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3885 x_scroll_bar_report_motion (fp, bar_window, part, x, y, timestamp);
3886 else
3888 Window root;
3889 int root_x, root_y;
3891 Window dummy_window;
3892 int dummy;
3894 Lisp_Object frame, tail;
3896 /* Clear the mouse-moved flag for every frame on this display. */
3897 FOR_EACH_FRAME (tail, frame)
3898 if (FRAME_X_P (XFRAME (frame))
3899 && FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3900 XFRAME (frame)->mouse_moved = 0;
3902 last_mouse_scroll_bar = Qnil;
3904 /* Figure out which root window we're on. */
3905 XQueryPointer (FRAME_X_DISPLAY (*fp),
3906 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3908 /* The root window which contains the pointer. */
3909 &root,
3911 /* Trash which we can't trust if the pointer is on
3912 a different screen. */
3913 &dummy_window,
3915 /* The position on that root window. */
3916 &root_x, &root_y,
3918 /* More trash we can't trust. */
3919 &dummy, &dummy,
3921 /* Modifier keys and pointer buttons, about which
3922 we don't care. */
3923 (unsigned int *) &dummy);
3925 /* Now we have a position on the root; find the innermost window
3926 containing the pointer. */
3928 Window win, child;
3929 int win_x, win_y;
3930 int parent_x = 0, parent_y = 0;
3932 win = root;
3934 /* XTranslateCoordinates can get errors if the window
3935 structure is changing at the same time this function
3936 is running. So at least we must not crash from them. */
3938 x_catch_errors (FRAME_X_DISPLAY (*fp));
3940 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3941 && FRAME_LIVE_P (last_mouse_frame))
3943 /* If mouse was grabbed on a frame, give coords for that frame
3944 even if the mouse is now outside it. */
3945 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3947 /* From-window, to-window. */
3948 root, FRAME_X_WINDOW (last_mouse_frame),
3950 /* From-position, to-position. */
3951 root_x, root_y, &win_x, &win_y,
3953 /* Child of win. */
3954 &child);
3955 f1 = last_mouse_frame;
3957 else
3959 while (1)
3961 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
3963 /* From-window, to-window. */
3964 root, win,
3966 /* From-position, to-position. */
3967 root_x, root_y, &win_x, &win_y,
3969 /* Child of win. */
3970 &child);
3972 if (child == None || child == win)
3973 break;
3974 #ifdef USE_GTK
3975 /* We don't wan't to know the innermost window. We
3976 want the edit window. For non-Gtk+ the innermost
3977 window is the edit window. For Gtk+ it might not
3978 be. It might be the tool bar for example. */
3979 if (x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win))
3980 break;
3981 #endif
3982 win = child;
3983 parent_x = win_x;
3984 parent_y = win_y;
3987 /* Now we know that:
3988 win is the innermost window containing the pointer
3989 (XTC says it has no child containing the pointer),
3990 win_x and win_y are the pointer's position in it
3991 (XTC did this the last time through), and
3992 parent_x and parent_y are the pointer's position in win's parent.
3993 (They are what win_x and win_y were when win was child.
3994 If win is the root window, it has no parent, and
3995 parent_{x,y} are invalid, but that's okay, because we'll
3996 never use them in that case.) */
3998 #ifdef USE_GTK
3999 /* We don't wan't to know the innermost window. We
4000 want the edit window. */
4001 f1 = x_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
4002 #else
4003 /* Is win one of our frames? */
4004 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
4005 #endif
4007 #ifdef USE_X_TOOLKIT
4008 /* If we end up with the menu bar window, say it's not
4009 on the frame. */
4010 if (f1 != NULL
4011 && f1->output_data.x->menubar_widget
4012 && win == XtWindow (f1->output_data.x->menubar_widget))
4013 f1 = NULL;
4014 #endif /* USE_X_TOOLKIT */
4017 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
4018 f1 = 0;
4020 x_uncatch_errors ();
4022 /* If not, is it one of our scroll bars? */
4023 if (! f1)
4025 struct scroll_bar *bar;
4027 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
4029 if (bar)
4031 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4032 win_x = parent_x;
4033 win_y = parent_y;
4037 if (f1 == 0 && insist > 0)
4038 f1 = SELECTED_FRAME ();
4040 if (f1)
4042 /* Ok, we found a frame. Store all the values.
4043 last_mouse_glyph is a rectangle used to reduce the
4044 generation of mouse events. To not miss any motion
4045 events, we must divide the frame into rectangles of the
4046 size of the smallest character that could be displayed
4047 on it, i.e. into the same rectangles that matrices on
4048 the frame are divided into. */
4050 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
4051 last_mouse_glyph_frame = f1;
4053 *bar_window = Qnil;
4054 *part = 0;
4055 *fp = f1;
4056 XSETINT (*x, win_x);
4057 XSETINT (*y, win_y);
4058 *timestamp = last_mouse_movement_time;
4063 UNBLOCK_INPUT;
4068 /***********************************************************************
4069 Scroll bars
4070 ***********************************************************************/
4072 /* Scroll bar support. */
4074 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4075 manages it.
4076 This can be called in GC, so we have to make sure to strip off mark
4077 bits. */
4079 static struct scroll_bar *
4080 x_window_to_scroll_bar (Display *display, Window window_id)
4082 Lisp_Object tail;
4084 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4085 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4086 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4088 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4090 Lisp_Object frame, bar, condemned;
4092 frame = XCAR (tail);
4093 /* All elements of Vframe_list should be frames. */
4094 if (! FRAMEP (frame))
4095 abort ();
4097 if (! FRAME_X_P (XFRAME (frame)))
4098 continue;
4100 /* Scan this frame's scroll bar list for a scroll bar with the
4101 right window ID. */
4102 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4103 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4104 /* This trick allows us to search both the ordinary and
4105 condemned scroll bar lists with one loop. */
4106 ! NILP (bar) || (bar = condemned,
4107 condemned = Qnil,
4108 ! NILP (bar));
4109 bar = XSCROLL_BAR (bar)->next)
4110 if (XSCROLL_BAR (bar)->x_window == window_id &&
4111 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4112 return XSCROLL_BAR (bar);
4115 return NULL;
4119 #if defined USE_LUCID
4121 /* Return the Lucid menu bar WINDOW is part of. Return null
4122 if WINDOW is not part of a menu bar. */
4124 static Widget
4125 x_window_to_menu_bar (Window window)
4127 Lisp_Object tail;
4129 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4131 if (FRAME_X_P (XFRAME (XCAR (tail))))
4133 Lisp_Object frame = XCAR (tail);
4134 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4136 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4137 return menu_bar;
4141 return NULL;
4144 #endif /* USE_LUCID */
4147 /************************************************************************
4148 Toolkit scroll bars
4149 ************************************************************************/
4151 #ifdef USE_TOOLKIT_SCROLL_BARS
4153 static void x_scroll_bar_to_input_event (XEvent *, struct input_event *);
4154 static void x_send_scroll_bar_event (Lisp_Object, int, int, int);
4155 static void x_create_toolkit_scroll_bar (struct frame *,
4156 struct scroll_bar *);
4157 static void x_set_toolkit_scroll_bar_thumb (struct scroll_bar *,
4158 int, int, int);
4161 /* Lisp window being scrolled. Set when starting to interact with
4162 a toolkit scroll bar, reset to nil when ending the interaction. */
4164 static Lisp_Object window_being_scrolled;
4166 /* Last scroll bar part sent in xm_scroll_callback. */
4168 static int last_scroll_bar_part;
4170 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4171 that movements of 1/20 of the screen size are mapped to up/down. */
4173 #ifndef USE_GTK
4174 /* Id of action hook installed for scroll bars. */
4176 static XtActionHookId action_hook_id;
4178 static Boolean xaw3d_arrow_scroll;
4180 /* Whether the drag scrolling maintains the mouse at the top of the
4181 thumb. If not, resizing the thumb needs to be done more carefully
4182 to avoid jerkyness. */
4184 static Boolean xaw3d_pick_top;
4186 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4187 bars are used.. The hook is responsible for detecting when
4188 the user ends an interaction with the scroll bar, and generates
4189 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4191 static void
4192 xt_action_hook (Widget widget, XtPointer client_data, String action_name,
4193 XEvent *event, String *params, Cardinal *num_params)
4195 int scroll_bar_p;
4196 const char *end_action;
4198 #ifdef USE_MOTIF
4199 scroll_bar_p = XmIsScrollBar (widget);
4200 end_action = "Release";
4201 #else /* !USE_MOTIF i.e. use Xaw */
4202 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4203 end_action = "EndScroll";
4204 #endif /* USE_MOTIF */
4206 if (scroll_bar_p
4207 && strcmp (action_name, end_action) == 0
4208 && WINDOWP (window_being_scrolled))
4210 struct window *w;
4212 x_send_scroll_bar_event (window_being_scrolled,
4213 scroll_bar_end_scroll, 0, 0);
4214 w = XWINDOW (window_being_scrolled);
4216 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4218 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4219 /* The thumb size is incorrect while dragging: fix it. */
4220 set_vertical_scroll_bar (w);
4222 window_being_scrolled = Qnil;
4223 last_scroll_bar_part = -1;
4225 /* Xt timeouts no longer needed. */
4226 toolkit_scroll_bar_interaction = 0;
4229 #endif /* not USE_GTK */
4231 /* A vector of windows used for communication between
4232 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4234 static struct window **scroll_bar_windows;
4235 static ptrdiff_t scroll_bar_windows_size;
4238 /* Send a client message with message type Xatom_Scrollbar for a
4239 scroll action to the frame of WINDOW. PART is a value identifying
4240 the part of the scroll bar that was clicked on. PORTION is the
4241 amount to scroll of a whole of WHOLE. */
4243 static void
4244 x_send_scroll_bar_event (Lisp_Object window, int part, int portion, int whole)
4246 XEvent event;
4247 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4248 struct window *w = XWINDOW (window);
4249 struct frame *f = XFRAME (w->frame);
4250 ptrdiff_t i;
4252 BLOCK_INPUT;
4254 /* Construct a ClientMessage event to send to the frame. */
4255 ev->type = ClientMessage;
4256 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4257 ev->display = FRAME_X_DISPLAY (f);
4258 ev->window = FRAME_X_WINDOW (f);
4259 ev->format = 32;
4261 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4262 not enough to store a pointer or Lisp_Object on a 64 bit system.
4263 So, store the window in scroll_bar_windows and pass the index
4264 into that array in the event. */
4265 for (i = 0; i < scroll_bar_windows_size; ++i)
4266 if (scroll_bar_windows[i] == NULL)
4267 break;
4269 if (i == scroll_bar_windows_size)
4271 ptrdiff_t old_nbytes =
4272 scroll_bar_windows_size * sizeof *scroll_bar_windows;
4273 ptrdiff_t nbytes;
4274 enum { XClientMessageEvent_MAX = 0x7fffffff };
4275 scroll_bar_windows =
4276 xpalloc (scroll_bar_windows, &scroll_bar_windows_size, 1,
4277 XClientMessageEvent_MAX, sizeof *scroll_bar_windows);
4278 nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4279 memset (&scroll_bar_windows[i], 0, nbytes - old_nbytes);
4282 scroll_bar_windows[i] = w;
4283 ev->data.l[0] = (long) i;
4284 ev->data.l[1] = (long) part;
4285 ev->data.l[2] = (long) 0;
4286 ev->data.l[3] = (long) portion;
4287 ev->data.l[4] = (long) whole;
4289 /* Make Xt timeouts work while the scroll bar is active. */
4290 #ifdef USE_X_TOOLKIT
4291 toolkit_scroll_bar_interaction = 1;
4292 x_activate_timeout_atimer ();
4293 #endif
4295 /* Setting the event mask to zero means that the message will
4296 be sent to the client that created the window, and if that
4297 window no longer exists, no event will be sent. */
4298 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4299 UNBLOCK_INPUT;
4303 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4304 in *IEVENT. */
4306 static void
4307 x_scroll_bar_to_input_event (XEvent *event, struct input_event *ievent)
4309 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4310 Lisp_Object window;
4311 struct window *w;
4313 w = scroll_bar_windows[ev->data.l[0]];
4314 scroll_bar_windows[ev->data.l[0]] = NULL;
4316 XSETWINDOW (window, w);
4318 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4319 ievent->frame_or_window = window;
4320 ievent->arg = Qnil;
4321 #ifdef USE_GTK
4322 ievent->timestamp = CurrentTime;
4323 #else
4324 ievent->timestamp =
4325 XtLastTimestampProcessed (FRAME_X_DISPLAY (XFRAME (w->frame)));
4326 #endif
4327 ievent->part = ev->data.l[1];
4328 ievent->code = ev->data.l[2];
4329 ievent->x = make_number ((int) ev->data.l[3]);
4330 ievent->y = make_number ((int) ev->data.l[4]);
4331 ievent->modifiers = 0;
4335 #ifdef USE_MOTIF
4337 /* Minimum and maximum values used for Motif scroll bars. */
4339 #define XM_SB_MAX 10000000
4342 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4343 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4344 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4346 static void
4347 xm_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4349 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4350 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4351 int part = -1, whole = 0, portion = 0;
4353 switch (cs->reason)
4355 case XmCR_DECREMENT:
4356 bar->dragging = Qnil;
4357 part = scroll_bar_up_arrow;
4358 break;
4360 case XmCR_INCREMENT:
4361 bar->dragging = Qnil;
4362 part = scroll_bar_down_arrow;
4363 break;
4365 case XmCR_PAGE_DECREMENT:
4366 bar->dragging = Qnil;
4367 part = scroll_bar_above_handle;
4368 break;
4370 case XmCR_PAGE_INCREMENT:
4371 bar->dragging = Qnil;
4372 part = scroll_bar_below_handle;
4373 break;
4375 case XmCR_TO_TOP:
4376 bar->dragging = Qnil;
4377 part = scroll_bar_to_top;
4378 break;
4380 case XmCR_TO_BOTTOM:
4381 bar->dragging = Qnil;
4382 part = scroll_bar_to_bottom;
4383 break;
4385 case XmCR_DRAG:
4387 int slider_size;
4389 /* Get the slider size. */
4390 BLOCK_INPUT;
4391 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4392 UNBLOCK_INPUT;
4394 whole = XM_SB_MAX - slider_size;
4395 portion = min (cs->value, whole);
4396 part = scroll_bar_handle;
4397 bar->dragging = make_number (cs->value);
4399 break;
4401 case XmCR_VALUE_CHANGED:
4402 break;
4405 if (part >= 0)
4407 window_being_scrolled = bar->window;
4408 last_scroll_bar_part = part;
4409 x_send_scroll_bar_event (bar->window, part, portion, whole);
4413 #elif defined USE_GTK
4415 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4416 bar widget. DATA is a pointer to the scroll_bar structure. */
4418 static gboolean
4419 xg_scroll_callback (GtkRange *range,
4420 GtkScrollType scroll,
4421 gdouble value,
4422 gpointer user_data)
4424 struct scroll_bar *bar = (struct scroll_bar *) user_data;
4425 gdouble position;
4426 int part = -1, whole = 0, portion = 0;
4427 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (range));
4428 FRAME_PTR f = (FRAME_PTR) g_object_get_data (G_OBJECT (range), XG_FRAME_DATA);
4430 if (xg_ignore_gtk_scrollbar) return FALSE;
4431 position = gtk_adjustment_get_value (adj);
4434 switch (scroll)
4436 case GTK_SCROLL_JUMP:
4437 /* Buttons 1 2 or 3 must be grabbed. */
4438 if (FRAME_X_DISPLAY_INFO (f)->grabbed != 0
4439 && FRAME_X_DISPLAY_INFO (f)->grabbed < (1 << 4))
4441 part = scroll_bar_handle;
4442 whole = gtk_adjustment_get_upper (adj) -
4443 gtk_adjustment_get_page_size (adj);
4444 portion = min ((int)position, whole);
4445 bar->dragging = make_number ((int)portion);
4447 break;
4448 case GTK_SCROLL_STEP_BACKWARD:
4449 part = scroll_bar_up_arrow;
4450 bar->dragging = Qnil;
4451 break;
4452 case GTK_SCROLL_STEP_FORWARD:
4453 part = scroll_bar_down_arrow;
4454 bar->dragging = Qnil;
4455 break;
4456 case GTK_SCROLL_PAGE_BACKWARD:
4457 part = scroll_bar_above_handle;
4458 bar->dragging = Qnil;
4459 break;
4460 case GTK_SCROLL_PAGE_FORWARD:
4461 part = scroll_bar_below_handle;
4462 bar->dragging = Qnil;
4463 break;
4466 if (part >= 0)
4468 window_being_scrolled = bar->window;
4469 last_scroll_bar_part = part;
4470 x_send_scroll_bar_event (bar->window, part, portion, whole);
4473 return FALSE;
4476 /* Callback for button release. Sets dragging to Qnil when dragging is done. */
4478 static gboolean
4479 xg_end_scroll_callback (GtkWidget *widget,
4480 GdkEventButton *event,
4481 gpointer user_data)
4483 struct scroll_bar *bar = (struct scroll_bar *) user_data;
4484 bar->dragging = Qnil;
4485 if (WINDOWP (window_being_scrolled))
4487 x_send_scroll_bar_event (window_being_scrolled,
4488 scroll_bar_end_scroll, 0, 0);
4489 window_being_scrolled = Qnil;
4492 return FALSE;
4496 #else /* not USE_GTK and not USE_MOTIF */
4498 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4499 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4500 scroll bar struct. CALL_DATA is a pointer to a float saying where
4501 the thumb is. */
4503 static void
4504 xaw_jump_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4506 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4507 float top = *(float *) call_data;
4508 float shown;
4509 int whole, portion, height;
4510 int part;
4512 /* Get the size of the thumb, a value between 0 and 1. */
4513 BLOCK_INPUT;
4514 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4515 UNBLOCK_INPUT;
4517 whole = 10000000;
4518 portion = shown < 1 ? top * whole : 0;
4520 if (shown < 1 && (eabs (top + shown - 1) < 1.0/height))
4521 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4522 the bottom, so we force the scrolling whenever we see that we're
4523 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4524 we try to ensure that we always stay two pixels away from the
4525 bottom). */
4526 part = scroll_bar_down_arrow;
4527 else
4528 part = scroll_bar_handle;
4530 window_being_scrolled = bar->window;
4531 bar->dragging = make_number (portion);
4532 last_scroll_bar_part = part;
4533 x_send_scroll_bar_event (bar->window, part, portion, whole);
4537 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4538 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4539 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4540 the scroll bar. CALL_DATA is an integer specifying the action that
4541 has taken place. Its magnitude is in the range 0..height of the
4542 scroll bar. Negative values mean scroll towards buffer start.
4543 Values < height of scroll bar mean line-wise movement. */
4545 static void
4546 xaw_scroll_callback (Widget widget, XtPointer client_data, XtPointer call_data)
4548 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4549 /* The position really is stored cast to a pointer. */
4550 int position = (long) call_data;
4551 Dimension height;
4552 int part;
4554 /* Get the height of the scroll bar. */
4555 BLOCK_INPUT;
4556 XtVaGetValues (widget, XtNheight, &height, NULL);
4557 UNBLOCK_INPUT;
4559 if (eabs (position) >= height)
4560 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4562 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4563 it maps line-movement to call_data = max(5, height/20). */
4564 else if (xaw3d_arrow_scroll && eabs (position) <= max (5, height / 20))
4565 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4566 else
4567 part = scroll_bar_move_ratio;
4569 window_being_scrolled = bar->window;
4570 bar->dragging = Qnil;
4571 last_scroll_bar_part = part;
4572 x_send_scroll_bar_event (bar->window, part, position, height);
4575 #endif /* not USE_GTK and not USE_MOTIF */
4577 #define SCROLL_BAR_NAME "verticalScrollBar"
4579 /* Create the widget for scroll bar BAR on frame F. Record the widget
4580 and X window of the scroll bar in BAR. */
4582 #ifdef USE_GTK
4583 static void
4584 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4586 const char *scroll_bar_name = SCROLL_BAR_NAME;
4588 BLOCK_INPUT;
4589 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4590 G_CALLBACK (xg_end_scroll_callback),
4591 scroll_bar_name);
4592 UNBLOCK_INPUT;
4595 #else /* not USE_GTK */
4597 static void
4598 x_create_toolkit_scroll_bar (struct frame *f, struct scroll_bar *bar)
4600 Window xwindow;
4601 Widget widget;
4602 Arg av[20];
4603 int ac = 0;
4604 char const *scroll_bar_name = SCROLL_BAR_NAME;
4605 unsigned long pixel;
4607 BLOCK_INPUT;
4609 #ifdef USE_MOTIF
4610 /* Set resources. Create the widget. */
4611 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4612 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4613 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4614 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4615 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4616 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4617 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4619 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4620 if (pixel != -1)
4622 XtSetArg (av[ac], XmNforeground, pixel);
4623 ++ac;
4626 pixel = f->output_data.x->scroll_bar_background_pixel;
4627 if (pixel != -1)
4629 XtSetArg (av[ac], XmNbackground, pixel);
4630 ++ac;
4633 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4634 scroll_bar_name, av, ac);
4636 /* Add one callback for everything that can happen. */
4637 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4638 (XtPointer) bar);
4639 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4640 (XtPointer) bar);
4641 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4642 (XtPointer) bar);
4643 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4644 (XtPointer) bar);
4645 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4646 (XtPointer) bar);
4647 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4648 (XtPointer) bar);
4649 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4650 (XtPointer) bar);
4652 /* Realize the widget. Only after that is the X window created. */
4653 XtRealizeWidget (widget);
4655 /* Set the cursor to an arrow. I didn't find a resource to do that.
4656 And I'm wondering why it hasn't an arrow cursor by default. */
4657 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4658 f->output_data.x->nontext_cursor);
4660 #else /* !USE_MOTIF i.e. use Xaw */
4662 /* Set resources. Create the widget. The background of the
4663 Xaw3d scroll bar widget is a little bit light for my taste.
4664 We don't alter it here to let users change it according
4665 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4666 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4667 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4668 /* For smoother scrolling with Xaw3d -sm */
4669 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4671 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4672 if (pixel != -1)
4674 XtSetArg (av[ac], XtNforeground, pixel);
4675 ++ac;
4678 pixel = f->output_data.x->scroll_bar_background_pixel;
4679 if (pixel != -1)
4681 XtSetArg (av[ac], XtNbackground, pixel);
4682 ++ac;
4685 /* Top/bottom shadow colors. */
4687 /* Allocate them, if necessary. */
4688 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4690 pixel = f->output_data.x->scroll_bar_background_pixel;
4691 if (pixel != -1)
4693 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4694 FRAME_X_COLORMAP (f),
4695 &pixel, 1.2, 0x8000))
4696 pixel = -1;
4697 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4700 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4702 pixel = f->output_data.x->scroll_bar_background_pixel;
4703 if (pixel != -1)
4705 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f),
4706 FRAME_X_COLORMAP (f),
4707 &pixel, 0.6, 0x4000))
4708 pixel = -1;
4709 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4713 #ifdef XtNbeNiceToColormap
4714 /* Tell the toolkit about them. */
4715 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4716 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4717 /* We tried to allocate a color for the top/bottom shadow, and
4718 failed, so tell Xaw3d to use dithering instead. */
4719 /* But only if we have a small colormap. Xaw3d can allocate nice
4720 colors itself. */
4722 XtSetArg (av[ac], XtNbeNiceToColormap,
4723 DefaultDepthOfScreen (FRAME_X_SCREEN (f)) < 16);
4724 ++ac;
4726 else
4727 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4728 be more consistent with other emacs 3d colors, and since Xaw3d is
4729 not good at dealing with allocation failure. */
4731 /* This tells Xaw3d to use real colors instead of dithering for
4732 the shadows. */
4733 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4734 ++ac;
4736 /* Specify the colors. */
4737 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4738 if (pixel != -1)
4740 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4741 ++ac;
4743 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4744 if (pixel != -1)
4746 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4747 ++ac;
4750 #endif
4752 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4753 f->output_data.x->edit_widget, av, ac);
4756 char const *initial = "";
4757 char const *val = initial;
4758 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4759 #ifdef XtNarrowScrollbars
4760 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4761 #endif
4762 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4763 if (xaw3d_arrow_scroll || val == initial)
4764 { /* ARROW_SCROLL */
4765 xaw3d_arrow_scroll = True;
4766 /* Isn't that just a personal preference ? --Stef */
4767 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4771 /* Define callbacks. */
4772 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4773 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4774 (XtPointer) bar);
4776 /* Realize the widget. Only after that is the X window created. */
4777 XtRealizeWidget (widget);
4779 #endif /* !USE_MOTIF */
4781 /* Install an action hook that lets us detect when the user
4782 finishes interacting with a scroll bar. */
4783 if (action_hook_id == 0)
4784 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4786 /* Remember X window and widget in the scroll bar vector. */
4787 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4788 xwindow = XtWindow (widget);
4789 bar->x_window = xwindow;
4791 UNBLOCK_INPUT;
4793 #endif /* not USE_GTK */
4796 /* Set the thumb size and position of scroll bar BAR. We are currently
4797 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4799 #ifdef USE_GTK
4800 static void
4801 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position, int whole)
4803 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4806 #else /* not USE_GTK */
4807 static void
4808 x_set_toolkit_scroll_bar_thumb (struct scroll_bar *bar, int portion, int position,
4809 int whole)
4811 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4812 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4813 float top, shown;
4815 BLOCK_INPUT;
4817 #ifdef USE_MOTIF
4819 /* We use an estimate of 30 chars per line rather than the real
4820 `portion' value. This has the disadvantage that the thumb size
4821 is not very representative, but it makes our life a lot easier.
4822 Otherwise, we have to constantly adjust the thumb size, which
4823 we can't always do quickly enough: while dragging, the size of
4824 the thumb might prevent the user from dragging the thumb all the
4825 way to the end. but Motif and some versions of Xaw3d don't allow
4826 updating the thumb size while dragging. Also, even if we can update
4827 its size, the update will often happen too late.
4828 If you don't believe it, check out revision 1.650 of xterm.c to see
4829 what hoops we were going through and the still poor behavior we got. */
4830 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4831 /* When the thumb is at the bottom, position == whole.
4832 So we need to increase `whole' to make space for the thumb. */
4833 whole += portion;
4835 if (whole <= 0)
4836 top = 0, shown = 1;
4837 else
4839 top = (float) position / whole;
4840 shown = (float) portion / whole;
4843 if (NILP (bar->dragging))
4845 int size, value;
4847 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4848 is the scroll bar's maximum and MIN is the scroll bar's minimum
4849 value. */
4850 size = shown * XM_SB_MAX;
4851 size = min (size, XM_SB_MAX);
4852 size = max (size, 1);
4854 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4855 value = top * XM_SB_MAX;
4856 value = min (value, XM_SB_MAX - size);
4858 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4860 #else /* !USE_MOTIF i.e. use Xaw */
4862 if (whole == 0)
4863 top = 0, shown = 1;
4864 else
4866 top = (float) position / whole;
4867 shown = (float) portion / whole;
4871 float old_top, old_shown;
4872 Dimension height;
4873 XtVaGetValues (widget,
4874 XtNtopOfThumb, &old_top,
4875 XtNshown, &old_shown,
4876 XtNheight, &height,
4877 NULL);
4879 /* Massage the top+shown values. */
4880 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4881 top = max (0, min (1, top));
4882 else
4883 top = old_top;
4884 /* Keep two pixels available for moving the thumb down. */
4885 shown = max (0, min (1 - top - (2.0 / height), shown));
4887 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4888 check that your system's configuration file contains a define
4889 for `NARROWPROTO'. See s/freebsd.h for an example. */
4890 if (top != old_top || shown != old_shown)
4892 if (NILP (bar->dragging))
4893 XawScrollbarSetThumb (widget, top, shown);
4894 else
4896 /* Try to make the scrolling a tad smoother. */
4897 if (!xaw3d_pick_top)
4898 shown = min (shown, old_shown);
4900 XawScrollbarSetThumb (widget, top, shown);
4904 #endif /* !USE_MOTIF */
4906 UNBLOCK_INPUT;
4908 #endif /* not USE_GTK */
4910 #endif /* USE_TOOLKIT_SCROLL_BARS */
4914 /************************************************************************
4915 Scroll bars, general
4916 ************************************************************************/
4918 /* Create a scroll bar and return the scroll bar vector for it. W is
4919 the Emacs window on which to create the scroll bar. TOP, LEFT,
4920 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4921 scroll bar. */
4923 static struct scroll_bar *
4924 x_scroll_bar_create (struct window *w, int top, int left, int width, int height)
4926 struct frame *f = XFRAME (w->frame);
4927 struct scroll_bar *bar
4928 = ALLOCATE_PSEUDOVECTOR (struct scroll_bar, x_window, PVEC_OTHER);
4930 BLOCK_INPUT;
4932 #ifdef USE_TOOLKIT_SCROLL_BARS
4933 x_create_toolkit_scroll_bar (f, bar);
4934 #else /* not USE_TOOLKIT_SCROLL_BARS */
4936 XSetWindowAttributes a;
4937 unsigned long mask;
4938 Window window;
4940 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4941 if (a.background_pixel == -1)
4942 a.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4944 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4945 | ButtonMotionMask | PointerMotionHintMask
4946 | ExposureMask);
4947 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4949 mask = (CWBackPixel | CWEventMask | CWCursor);
4951 /* Clear the area of W that will serve as a scroll bar. This is
4952 for the case that a window has been split horizontally. In
4953 this case, no clear_frame is generated to reduce flickering. */
4954 if (width > 0 && height > 0)
4955 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4956 left, top, width,
4957 window_box_height (w), False);
4959 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4960 /* Position and size of scroll bar. */
4961 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
4962 top,
4963 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
4964 height,
4965 /* Border width, depth, class, and visual. */
4967 CopyFromParent,
4968 CopyFromParent,
4969 CopyFromParent,
4970 /* Attributes. */
4971 mask, &a);
4972 bar->x_window = window;
4974 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4976 XSETWINDOW (bar->window, w);
4977 bar->top = top;
4978 bar->left = left;
4979 bar->width = width;
4980 bar->height = height;
4981 bar->start = 0;
4982 bar->end = 0;
4983 bar->dragging = Qnil;
4984 bar->fringe_extended_p = 0;
4986 /* Add bar to its frame's list of scroll bars. */
4987 bar->next = FRAME_SCROLL_BARS (f);
4988 bar->prev = Qnil;
4989 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
4990 if (!NILP (bar->next))
4991 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
4993 /* Map the window/widget. */
4994 #ifdef USE_TOOLKIT_SCROLL_BARS
4996 #ifdef USE_GTK
4997 xg_update_scrollbar_pos (f,
4998 bar->x_window,
4999 top,
5000 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5001 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5002 max (height, 1));
5003 #else /* not USE_GTK */
5004 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5005 XtConfigureWidget (scroll_bar,
5006 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5007 top,
5008 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5009 max (height, 1), 0);
5010 XtMapWidget (scroll_bar);
5011 #endif /* not USE_GTK */
5013 #else /* not USE_TOOLKIT_SCROLL_BARS */
5014 XMapRaised (FRAME_X_DISPLAY (f), bar->x_window);
5015 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5017 UNBLOCK_INPUT;
5018 return bar;
5022 #ifndef USE_TOOLKIT_SCROLL_BARS
5024 /* Draw BAR's handle in the proper position.
5026 If the handle is already drawn from START to END, don't bother
5027 redrawing it, unless REBUILD is non-zero; in that case, always
5028 redraw it. (REBUILD is handy for drawing the handle after expose
5029 events.)
5031 Normally, we want to constrain the start and end of the handle to
5032 fit inside its rectangle, but if the user is dragging the scroll
5033 bar handle, we want to let them drag it down all the way, so that
5034 the bar's top is as far down as it goes; otherwise, there's no way
5035 to move to the very end of the buffer. */
5037 static void
5038 x_scroll_bar_set_handle (struct scroll_bar *bar, int start, int end, int rebuild)
5040 int dragging = ! NILP (bar->dragging);
5041 Window w = bar->x_window;
5042 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5043 GC gc = f->output_data.x->normal_gc;
5045 /* If the display is already accurate, do nothing. */
5046 if (! rebuild
5047 && start == bar->start
5048 && end == bar->end)
5049 return;
5051 BLOCK_INPUT;
5054 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, bar->width);
5055 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, bar->height);
5056 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5058 /* Make sure the values are reasonable, and try to preserve
5059 the distance between start and end. */
5061 int length = end - start;
5063 if (start < 0)
5064 start = 0;
5065 else if (start > top_range)
5066 start = top_range;
5067 end = start + length;
5069 if (end < start)
5070 end = start;
5071 else if (end > top_range && ! dragging)
5072 end = top_range;
5075 /* Store the adjusted setting in the scroll bar. */
5076 bar->start = start;
5077 bar->end = end;
5079 /* Clip the end position, just for display. */
5080 if (end > top_range)
5081 end = top_range;
5083 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5084 below top positions, to make sure the handle is always at least
5085 that many pixels tall. */
5086 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5088 /* Draw the empty space above the handle. Note that we can't clear
5089 zero-height areas; that means "clear to end of window." */
5090 if (0 < start)
5091 x_clear_area (FRAME_X_DISPLAY (f), w,
5092 /* x, y, width, height, and exposures. */
5093 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5094 VERTICAL_SCROLL_BAR_TOP_BORDER,
5095 inside_width, start,
5096 False);
5098 /* Change to proper foreground color if one is specified. */
5099 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5100 XSetForeground (FRAME_X_DISPLAY (f), gc,
5101 f->output_data.x->scroll_bar_foreground_pixel);
5103 /* Draw the handle itself. */
5104 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5105 /* x, y, width, height */
5106 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5107 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5108 inside_width, end - start);
5110 /* Restore the foreground color of the GC if we changed it above. */
5111 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5112 XSetForeground (FRAME_X_DISPLAY (f), gc,
5113 FRAME_FOREGROUND_PIXEL (f));
5115 /* Draw the empty space below the handle. Note that we can't
5116 clear zero-height areas; that means "clear to end of window." */
5117 if (end < inside_height)
5118 x_clear_area (FRAME_X_DISPLAY (f), w,
5119 /* x, y, width, height, and exposures. */
5120 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5121 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5122 inside_width, inside_height - end,
5123 False);
5127 UNBLOCK_INPUT;
5130 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5132 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5133 nil. */
5135 static void
5136 x_scroll_bar_remove (struct scroll_bar *bar)
5138 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5139 BLOCK_INPUT;
5141 #ifdef USE_TOOLKIT_SCROLL_BARS
5142 #ifdef USE_GTK
5143 xg_remove_scroll_bar (f, bar->x_window);
5144 #else /* not USE_GTK */
5145 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5146 #endif /* not USE_GTK */
5147 #else
5148 XDestroyWindow (FRAME_X_DISPLAY (f), bar->x_window);
5149 #endif
5151 /* Disassociate this scroll bar from its window. */
5152 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5154 UNBLOCK_INPUT;
5158 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5159 that we are displaying PORTION characters out of a total of WHOLE
5160 characters, starting at POSITION. If WINDOW has no scroll bar,
5161 create one. */
5163 static void
5164 XTset_vertical_scroll_bar (struct window *w, int portion, int whole, int position)
5166 struct frame *f = XFRAME (w->frame);
5167 struct scroll_bar *bar;
5168 int top, height, left, sb_left, width, sb_width;
5169 int window_y, window_height;
5170 #ifdef USE_TOOLKIT_SCROLL_BARS
5171 int fringe_extended_p;
5172 #endif
5174 /* Get window dimensions. */
5175 window_box (w, -1, 0, &window_y, 0, &window_height);
5176 top = window_y;
5177 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5178 height = window_height;
5180 /* Compute the left edge of the scroll bar area. */
5181 left = WINDOW_SCROLL_BAR_AREA_X (w);
5183 /* Compute the width of the scroll bar which might be less than
5184 the width of the area reserved for the scroll bar. */
5185 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5186 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5187 else
5188 sb_width = width;
5190 /* Compute the left edge of the scroll bar. */
5191 #ifdef USE_TOOLKIT_SCROLL_BARS
5192 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5193 sb_left = left + (WINDOW_RIGHTMOST_P (w) ? width - sb_width : 0);
5194 else
5195 sb_left = left + (WINDOW_LEFTMOST_P (w) ? 0 : width - sb_width);
5196 #else
5197 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5198 sb_left = left + width - sb_width;
5199 else
5200 sb_left = left;
5201 #endif
5203 #ifdef USE_TOOLKIT_SCROLL_BARS
5204 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5205 fringe_extended_p = (WINDOW_LEFTMOST_P (w)
5206 && WINDOW_LEFT_FRINGE_WIDTH (w)
5207 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5208 || WINDOW_LEFT_MARGIN_COLS (w) == 0));
5209 else
5210 fringe_extended_p = (WINDOW_RIGHTMOST_P (w)
5211 && WINDOW_RIGHT_FRINGE_WIDTH (w)
5212 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5213 || WINDOW_RIGHT_MARGIN_COLS (w) == 0));
5214 #endif
5216 /* Does the scroll bar exist yet? */
5217 if (NILP (w->vertical_scroll_bar))
5219 if (width > 0 && height > 0)
5221 BLOCK_INPUT;
5222 #ifdef USE_TOOLKIT_SCROLL_BARS
5223 if (fringe_extended_p)
5224 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5225 sb_left, top, sb_width, height, False);
5226 else
5227 #endif
5228 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5229 left, top, width, height, False);
5230 UNBLOCK_INPUT;
5233 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5235 else
5237 /* It may just need to be moved and resized. */
5238 unsigned int mask = 0;
5240 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5242 BLOCK_INPUT;
5244 if (sb_left != bar->left)
5245 mask |= CWX;
5246 if (top != bar->top)
5247 mask |= CWY;
5248 if (sb_width != bar->width)
5249 mask |= CWWidth;
5250 if (height != bar->height)
5251 mask |= CWHeight;
5253 #ifdef USE_TOOLKIT_SCROLL_BARS
5255 /* Move/size the scroll bar widget. */
5256 if (mask || bar->fringe_extended_p != fringe_extended_p)
5258 /* Since toolkit scroll bars are smaller than the space reserved
5259 for them on the frame, we have to clear "under" them. */
5260 if (width > 0 && height > 0)
5262 if (fringe_extended_p)
5263 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5264 sb_left, top, sb_width, height, False);
5265 else
5266 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5267 left, top, width, height, False);
5269 #ifdef USE_GTK
5270 xg_update_scrollbar_pos (f,
5271 bar->x_window,
5272 top,
5273 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5274 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5275 max (height, 1));
5276 #else /* not USE_GTK */
5277 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5278 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5279 top,
5280 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5281 max (height, 1), 0);
5282 #endif /* not USE_GTK */
5284 #else /* not USE_TOOLKIT_SCROLL_BARS */
5286 /* Clear areas not covered by the scroll bar because of
5287 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5288 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5290 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5291 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5292 height, False);
5293 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5294 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5295 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5296 height, False);
5299 /* Clear areas not covered by the scroll bar because it's not as
5300 wide as the area reserved for it. This makes sure a
5301 previous mode line display is cleared after C-x 2 C-x 1, for
5302 example. */
5304 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5305 int rest = area_width - sb_width;
5306 if (rest > 0 && height > 0)
5308 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5309 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5310 left + area_width - rest, top,
5311 rest, height, False);
5312 else
5313 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5314 left, top, rest, height, False);
5318 /* Move/size the scroll bar window. */
5319 if (mask)
5321 XWindowChanges wc;
5323 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5324 wc.y = top;
5325 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5326 wc.height = height;
5327 XConfigureWindow (FRAME_X_DISPLAY (f), bar->x_window,
5328 mask, &wc);
5331 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5333 /* Remember new settings. */
5334 bar->left = sb_left;
5335 bar->top = top;
5336 bar->width = sb_width;
5337 bar->height = height;
5339 UNBLOCK_INPUT;
5342 #ifdef USE_TOOLKIT_SCROLL_BARS
5343 bar->fringe_extended_p = fringe_extended_p;
5345 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5346 #else /* not USE_TOOLKIT_SCROLL_BARS */
5347 /* Set the scroll bar's current state, unless we're currently being
5348 dragged. */
5349 if (NILP (bar->dragging))
5351 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5353 if (whole == 0)
5354 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5355 else
5357 int start = ((double) position * top_range) / whole;
5358 int end = ((double) (position + portion) * top_range) / whole;
5359 x_scroll_bar_set_handle (bar, start, end, 0);
5362 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5364 XSETVECTOR (w->vertical_scroll_bar, bar);
5368 /* The following three hooks are used when we're doing a thorough
5369 redisplay of the frame. We don't explicitly know which scroll bars
5370 are going to be deleted, because keeping track of when windows go
5371 away is a real pain - "Can you say set-window-configuration, boys
5372 and girls?" Instead, we just assert at the beginning of redisplay
5373 that *all* scroll bars are to be removed, and then save a scroll bar
5374 from the fiery pit when we actually redisplay its window. */
5376 /* Arrange for all scroll bars on FRAME to be removed at the next call
5377 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5378 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5380 static void
5381 XTcondemn_scroll_bars (FRAME_PTR frame)
5383 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5384 while (! NILP (FRAME_SCROLL_BARS (frame)))
5386 Lisp_Object bar;
5387 bar = FRAME_SCROLL_BARS (frame);
5388 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5389 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5390 XSCROLL_BAR (bar)->prev = Qnil;
5391 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5392 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5393 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5398 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5399 Note that WINDOW isn't necessarily condemned at all. */
5401 static void
5402 XTredeem_scroll_bar (struct window *window)
5404 struct scroll_bar *bar;
5405 struct frame *f;
5407 /* We can't redeem this window's scroll bar if it doesn't have one. */
5408 if (NILP (window->vertical_scroll_bar))
5409 abort ();
5411 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5413 /* Unlink it from the condemned list. */
5414 f = XFRAME (WINDOW_FRAME (window));
5415 if (NILP (bar->prev))
5417 /* If the prev pointer is nil, it must be the first in one of
5418 the lists. */
5419 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5420 /* It's not condemned. Everything's fine. */
5421 return;
5422 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5423 window->vertical_scroll_bar))
5424 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5425 else
5426 /* If its prev pointer is nil, it must be at the front of
5427 one or the other! */
5428 abort ();
5430 else
5431 XSCROLL_BAR (bar->prev)->next = bar->next;
5433 if (! NILP (bar->next))
5434 XSCROLL_BAR (bar->next)->prev = bar->prev;
5436 bar->next = FRAME_SCROLL_BARS (f);
5437 bar->prev = Qnil;
5438 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5439 if (! NILP (bar->next))
5440 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5443 /* Remove all scroll bars on FRAME that haven't been saved since the
5444 last call to `*condemn_scroll_bars_hook'. */
5446 static void
5447 XTjudge_scroll_bars (FRAME_PTR f)
5449 Lisp_Object bar, next;
5451 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5453 /* Clear out the condemned list now so we won't try to process any
5454 more events on the hapless scroll bars. */
5455 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5457 for (; ! NILP (bar); bar = next)
5459 struct scroll_bar *b = XSCROLL_BAR (bar);
5461 x_scroll_bar_remove (b);
5463 next = b->next;
5464 b->next = b->prev = Qnil;
5467 /* Now there should be no references to the condemned scroll bars,
5468 and they should get garbage-collected. */
5472 #ifndef USE_TOOLKIT_SCROLL_BARS
5473 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5474 is a no-op when using toolkit scroll bars.
5476 This may be called from a signal handler, so we have to ignore GC
5477 mark bits. */
5479 static void
5480 x_scroll_bar_expose (struct scroll_bar *bar, XEvent *event)
5482 Window w = bar->x_window;
5483 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5484 GC gc = f->output_data.x->normal_gc;
5485 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5487 BLOCK_INPUT;
5489 x_scroll_bar_set_handle (bar, bar->start, bar->end, 1);
5491 /* Switch to scroll bar foreground color. */
5492 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5493 XSetForeground (FRAME_X_DISPLAY (f), gc,
5494 f->output_data.x->scroll_bar_foreground_pixel);
5496 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5497 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5499 /* x, y, width, height */
5500 0, 0,
5501 bar->width - 1 - width_trim - width_trim,
5502 bar->height - 1);
5504 /* Restore the foreground color of the GC if we changed it above. */
5505 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5506 XSetForeground (FRAME_X_DISPLAY (f), gc,
5507 FRAME_FOREGROUND_PIXEL (f));
5509 UNBLOCK_INPUT;
5512 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5514 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5515 is set to something other than NO_EVENT, it is enqueued.
5517 This may be called from a signal handler, so we have to ignore GC
5518 mark bits. */
5521 static void
5522 x_scroll_bar_handle_click (struct scroll_bar *bar, XEvent *event, struct input_event *emacs_event)
5524 if (! WINDOWP (bar->window))
5525 abort ();
5527 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5528 emacs_event->code = event->xbutton.button - Button1;
5529 emacs_event->modifiers
5530 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5531 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5532 event->xbutton.state)
5533 | (event->type == ButtonRelease
5534 ? up_modifier
5535 : down_modifier));
5536 emacs_event->frame_or_window = bar->window;
5537 emacs_event->arg = Qnil;
5538 emacs_event->timestamp = event->xbutton.time;
5540 int top_range
5541 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5542 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5544 if (y < 0) y = 0;
5545 if (y > top_range) y = top_range;
5547 if (y < bar->start)
5548 emacs_event->part = scroll_bar_above_handle;
5549 else if (y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5550 emacs_event->part = scroll_bar_handle;
5551 else
5552 emacs_event->part = scroll_bar_below_handle;
5554 #ifndef USE_TOOLKIT_SCROLL_BARS
5555 /* If the user has released the handle, set it to its final position. */
5556 if (event->type == ButtonRelease
5557 && ! NILP (bar->dragging))
5559 int new_start = y - XINT (bar->dragging);
5560 int new_end = new_start + bar->end - bar->start;
5562 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5563 bar->dragging = Qnil;
5565 #endif
5567 XSETINT (emacs_event->x, y);
5568 XSETINT (emacs_event->y, top_range);
5572 #ifndef USE_TOOLKIT_SCROLL_BARS
5574 /* Handle some mouse motion while someone is dragging the scroll bar.
5576 This may be called from a signal handler, so we have to ignore GC
5577 mark bits. */
5579 static void
5580 x_scroll_bar_note_movement (struct scroll_bar *bar, XEvent *event)
5582 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5584 last_mouse_movement_time = event->xmotion.time;
5586 f->mouse_moved = 1;
5587 XSETVECTOR (last_mouse_scroll_bar, bar);
5589 /* If we're dragging the bar, display it. */
5590 if (! NILP (bar->dragging))
5592 /* Where should the handle be now? */
5593 int new_start = event->xmotion.y - XINT (bar->dragging);
5595 if (new_start != bar->start)
5597 int new_end = new_start + bar->end - bar->start;
5599 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5604 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5606 /* Return information to the user about the current position of the mouse
5607 on the scroll bar. */
5609 static void
5610 x_scroll_bar_report_motion (FRAME_PTR *fp, Lisp_Object *bar_window,
5611 enum scroll_bar_part *part, Lisp_Object *x,
5612 Lisp_Object *y, Time *timestamp)
5614 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5615 Window w = bar->x_window;
5616 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5617 int win_x, win_y;
5618 Window dummy_window;
5619 int dummy_coord;
5620 unsigned int dummy_mask;
5622 BLOCK_INPUT;
5624 /* Get the mouse's position relative to the scroll bar window, and
5625 report that. */
5626 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5628 /* Root, child, root x and root y. */
5629 &dummy_window, &dummy_window,
5630 &dummy_coord, &dummy_coord,
5632 /* Position relative to scroll bar. */
5633 &win_x, &win_y,
5635 /* Mouse buttons and modifier keys. */
5636 &dummy_mask))
5638 else
5640 int top_range
5641 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, bar->height);
5643 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5645 if (! NILP (bar->dragging))
5646 win_y -= XINT (bar->dragging);
5648 if (win_y < 0)
5649 win_y = 0;
5650 if (win_y > top_range)
5651 win_y = top_range;
5653 *fp = f;
5654 *bar_window = bar->window;
5656 if (! NILP (bar->dragging))
5657 *part = scroll_bar_handle;
5658 else if (win_y < bar->start)
5659 *part = scroll_bar_above_handle;
5660 else if (win_y < bar->end + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5661 *part = scroll_bar_handle;
5662 else
5663 *part = scroll_bar_below_handle;
5665 XSETINT (*x, win_y);
5666 XSETINT (*y, top_range);
5668 f->mouse_moved = 0;
5669 last_mouse_scroll_bar = Qnil;
5672 *timestamp = last_mouse_movement_time;
5674 UNBLOCK_INPUT;
5678 /* The screen has been cleared so we may have changed foreground or
5679 background colors, and the scroll bars may need to be redrawn.
5680 Clear out the scroll bars, and ask for expose events, so we can
5681 redraw them. */
5683 static void
5684 x_scroll_bar_clear (FRAME_PTR f)
5686 #ifndef USE_TOOLKIT_SCROLL_BARS
5687 Lisp_Object bar;
5689 /* We can have scroll bars even if this is 0,
5690 if we just turned off scroll bar mode.
5691 But in that case we should not clear them. */
5692 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5693 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5694 bar = XSCROLL_BAR (bar)->next)
5695 XClearArea (FRAME_X_DISPLAY (f),
5696 XSCROLL_BAR (bar)->x_window,
5697 0, 0, 0, 0, True);
5698 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5702 /* The main X event-reading loop - XTread_socket. */
5704 /* This holds the state XLookupString needs to implement dead keys
5705 and other tricks known as "compose processing". _X Window System_
5706 says that a portable program can't use this, but Stephen Gildea assures
5707 me that letting the compiler initialize it to zeros will work okay.
5709 This must be defined outside of XTread_socket, for the same reasons
5710 given for enter_timestamp, above. */
5712 static XComposeStatus compose_status;
5714 /* Record the last 100 characters stored
5715 to help debug the loss-of-chars-during-GC problem. */
5717 static int temp_index;
5718 static short temp_buffer[100];
5720 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5721 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5722 temp_index = 0; \
5723 temp_buffer[temp_index++] = (keysym)
5725 /* Set this to nonzero to fake an "X I/O error"
5726 on a particular display. */
5728 static struct x_display_info *XTread_socket_fake_io_error;
5730 /* When we find no input here, we occasionally do a no-op command
5731 to verify that the X server is still running and we can still talk with it.
5732 We try all the open displays, one by one.
5733 This variable is used for cycling thru the displays. */
5735 static struct x_display_info *next_noop_dpyinfo;
5737 #if defined USE_X_TOOLKIT || defined USE_GTK
5738 #define SET_SAVED_BUTTON_EVENT \
5739 do \
5741 if (f->output_data.x->saved_menu_event == 0) \
5742 f->output_data.x->saved_menu_event \
5743 = (XEvent *) xmalloc (sizeof (XEvent)); \
5744 *f->output_data.x->saved_menu_event = event; \
5745 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5746 XSETFRAME (inev.ie.frame_or_window, f); \
5748 while (0)
5749 #endif
5751 enum
5753 X_EVENT_NORMAL,
5754 X_EVENT_GOTO_OUT,
5755 X_EVENT_DROP
5758 /* Filter events for the current X input method.
5759 DPYINFO is the display this event is for.
5760 EVENT is the X event to filter.
5762 Returns non-zero if the event was filtered, caller shall not process
5763 this event further.
5764 Returns zero if event is wasn't filtered. */
5766 #ifdef HAVE_X_I18N
5767 static int
5768 x_filter_event (struct x_display_info *dpyinfo, XEvent *event)
5770 /* XFilterEvent returns non-zero if the input method has
5771 consumed the event. We pass the frame's X window to
5772 XFilterEvent because that's the one for which the IC
5773 was created. */
5775 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5776 event->xclient.window);
5778 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5780 #endif
5782 #ifdef USE_GTK
5783 static int current_count;
5784 static int current_finish;
5785 static struct input_event *current_hold_quit;
5787 /* This is the filter function invoked by the GTK event loop.
5788 It is invoked before the XEvent is translated to a GdkEvent,
5789 so we have a chance to act on the event before GTK. */
5790 static GdkFilterReturn
5791 event_handler_gdk (GdkXEvent *gxev, GdkEvent *ev, gpointer data)
5793 XEvent *xev = (XEvent *) gxev;
5795 BLOCK_INPUT;
5796 if (current_count >= 0)
5798 struct x_display_info *dpyinfo;
5800 dpyinfo = x_display_info_for_display (xev->xany.display);
5802 #ifdef HAVE_X_I18N
5803 /* Filter events for the current X input method.
5804 GTK calls XFilterEvent but not for key press and release,
5805 so we do it here. */
5806 if ((xev->type == KeyPress || xev->type == KeyRelease)
5807 && dpyinfo
5808 && x_filter_event (dpyinfo, xev))
5810 UNBLOCK_INPUT;
5811 return GDK_FILTER_REMOVE;
5813 #endif
5815 if (! dpyinfo)
5816 current_finish = X_EVENT_NORMAL;
5817 else
5818 current_count +=
5819 handle_one_xevent (dpyinfo, xev, &current_finish,
5820 current_hold_quit);
5822 else
5823 current_finish = x_dispatch_event (xev, xev->xany.display);
5825 UNBLOCK_INPUT;
5827 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5828 return GDK_FILTER_REMOVE;
5830 return GDK_FILTER_CONTINUE;
5832 #endif /* USE_GTK */
5835 static void xembed_send_message (struct frame *f, Time,
5836 enum xembed_message,
5837 long detail, long data1, long data2);
5839 /* Handles the XEvent EVENT on display DPYINFO.
5841 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5842 *FINISH is zero if caller should continue reading events.
5843 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5845 We return the number of characters stored into the buffer. */
5847 static int
5848 handle_one_xevent (struct x_display_info *dpyinfo, XEvent *eventptr,
5849 int *finish, struct input_event *hold_quit)
5851 union {
5852 struct input_event ie;
5853 struct selection_input_event sie;
5854 } inev;
5855 int count = 0;
5856 int do_help = 0;
5857 ptrdiff_t nbytes = 0;
5858 struct frame *f = NULL;
5859 struct coding_system coding;
5860 XEvent event = *eventptr;
5861 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
5862 USE_SAFE_ALLOCA;
5864 *finish = X_EVENT_NORMAL;
5866 EVENT_INIT (inev.ie);
5867 inev.ie.kind = NO_EVENT;
5868 inev.ie.arg = Qnil;
5870 if (pending_event_wait.eventtype == event.type)
5871 pending_event_wait.eventtype = 0; /* Indicates we got it. */
5873 switch (event.type)
5875 case ClientMessage:
5877 if (event.xclient.message_type
5878 == dpyinfo->Xatom_wm_protocols
5879 && event.xclient.format == 32)
5881 if (event.xclient.data.l[0]
5882 == dpyinfo->Xatom_wm_take_focus)
5884 /* Use x_any_window_to_frame because this
5885 could be the shell widget window
5886 if the frame has no title bar. */
5887 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5888 #ifdef HAVE_X_I18N
5889 /* Not quite sure this is needed -pd */
5890 if (f && FRAME_XIC (f))
5891 XSetICFocus (FRAME_XIC (f));
5892 #endif
5893 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5894 instructs the WM to set the input focus automatically for
5895 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5896 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5897 it has set the focus. So, XSetInputFocus below is not
5898 needed.
5900 The call to XSetInputFocus below has also caused trouble. In
5901 cases where the XSetInputFocus done by the WM and the one
5902 below are temporally close (on a fast machine), the call
5903 below can generate additional FocusIn events which confuse
5904 Emacs. */
5906 /* Since we set WM_TAKE_FOCUS, we must call
5907 XSetInputFocus explicitly. But not if f is null,
5908 since that might be an event for a deleted frame. */
5909 if (f)
5911 Display *d = event.xclient.display;
5912 /* Catch and ignore errors, in case window has been
5913 iconified by a window manager such as GWM. */
5914 x_catch_errors (d);
5915 XSetInputFocus (d, event.xclient.window,
5916 /* The ICCCM says this is
5917 the only valid choice. */
5918 RevertToParent,
5919 event.xclient.data.l[1]);
5920 /* This is needed to detect the error
5921 if there is an error. */
5922 XSync (d, False);
5923 x_uncatch_errors ();
5925 /* Not certain about handling scroll bars here */
5926 #endif /* 0 */
5927 goto done;
5930 if (event.xclient.data.l[0]
5931 == dpyinfo->Xatom_wm_save_yourself)
5933 /* Save state modify the WM_COMMAND property to
5934 something which can reinstate us. This notifies
5935 the session manager, who's looking for such a
5936 PropertyNotify. Can restart processing when
5937 a keyboard or mouse event arrives. */
5938 /* If we have a session manager, don't set this.
5939 KDE will then start two Emacsen, one for the
5940 session manager and one for this. */
5941 #ifdef HAVE_X_SM
5942 if (! x_session_have_connection ())
5943 #endif
5945 f = x_top_window_to_frame (dpyinfo,
5946 event.xclient.window);
5947 /* This is just so we only give real data once
5948 for a single Emacs process. */
5949 if (f == SELECTED_FRAME ())
5950 XSetCommand (FRAME_X_DISPLAY (f),
5951 event.xclient.window,
5952 initial_argv, initial_argc);
5953 else if (f)
5954 XSetCommand (FRAME_X_DISPLAY (f),
5955 event.xclient.window,
5956 0, 0);
5958 goto done;
5961 if (event.xclient.data.l[0]
5962 == dpyinfo->Xatom_wm_delete_window)
5964 f = x_any_window_to_frame (dpyinfo,
5965 event.xclient.window);
5966 if (!f)
5967 goto OTHER; /* May be a dialog that is to be removed */
5969 inev.ie.kind = DELETE_WINDOW_EVENT;
5970 XSETFRAME (inev.ie.frame_or_window, f);
5971 goto done;
5974 goto done;
5977 if (event.xclient.message_type
5978 == dpyinfo->Xatom_wm_configure_denied)
5980 goto done;
5983 if (event.xclient.message_type
5984 == dpyinfo->Xatom_wm_window_moved)
5986 int new_x, new_y;
5987 f = x_window_to_frame (dpyinfo, event.xclient.window);
5989 new_x = event.xclient.data.s[0];
5990 new_y = event.xclient.data.s[1];
5992 if (f)
5994 f->left_pos = new_x;
5995 f->top_pos = new_y;
5997 goto done;
6000 #ifdef HACK_EDITRES
6001 if (event.xclient.message_type
6002 == dpyinfo->Xatom_editres)
6004 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6005 if (f)
6006 _XEditResCheckMessages (f->output_data.x->widget, NULL,
6007 &event, NULL);
6008 goto done;
6010 #endif /* HACK_EDITRES */
6012 if ((event.xclient.message_type
6013 == dpyinfo->Xatom_DONE)
6014 || (event.xclient.message_type
6015 == dpyinfo->Xatom_PAGE))
6017 /* Ghostview job completed. Kill it. We could
6018 reply with "Next" if we received "Page", but we
6019 currently never do because we are interested in
6020 images, only, which should have 1 page. */
6021 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
6022 f = x_window_to_frame (dpyinfo, event.xclient.window);
6023 if (!f)
6024 goto OTHER;
6025 x_kill_gs_process (pixmap, f);
6026 expose_frame (f, 0, 0, 0, 0);
6027 goto done;
6030 #ifdef USE_TOOLKIT_SCROLL_BARS
6031 /* Scroll bar callbacks send a ClientMessage from which
6032 we construct an input_event. */
6033 if (event.xclient.message_type
6034 == dpyinfo->Xatom_Scrollbar)
6036 x_scroll_bar_to_input_event (&event, &inev.ie);
6037 *finish = X_EVENT_GOTO_OUT;
6038 goto done;
6040 #endif /* USE_TOOLKIT_SCROLL_BARS */
6042 /* XEmbed messages from the embedder (if any). */
6043 if (event.xclient.message_type
6044 == dpyinfo->Xatom_XEMBED)
6046 enum xembed_message msg = event.xclient.data.l[1];
6047 if (msg == XEMBED_FOCUS_IN || msg == XEMBED_FOCUS_OUT)
6048 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6050 *finish = X_EVENT_GOTO_OUT;
6051 goto done;
6054 xft_settings_event (dpyinfo, &event);
6056 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6057 if (!f)
6058 goto OTHER;
6059 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
6060 *finish = X_EVENT_DROP;
6062 break;
6064 case SelectionNotify:
6065 last_user_time = event.xselection.time;
6066 #ifdef USE_X_TOOLKIT
6067 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6068 goto OTHER;
6069 #endif /* not USE_X_TOOLKIT */
6070 x_handle_selection_notify (&event.xselection);
6071 break;
6073 case SelectionClear: /* Someone has grabbed ownership. */
6074 last_user_time = event.xselectionclear.time;
6075 #ifdef USE_X_TOOLKIT
6076 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6077 goto OTHER;
6078 #endif /* USE_X_TOOLKIT */
6080 XSelectionClearEvent *eventp = &(event.xselectionclear);
6082 inev.ie.kind = SELECTION_CLEAR_EVENT;
6083 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6084 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6085 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6086 inev.ie.frame_or_window = Qnil;
6088 break;
6090 case SelectionRequest: /* Someone wants our selection. */
6091 last_user_time = event.xselectionrequest.time;
6092 #ifdef USE_X_TOOLKIT
6093 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6094 goto OTHER;
6095 #endif /* USE_X_TOOLKIT */
6097 XSelectionRequestEvent *eventp = &(event.xselectionrequest);
6099 inev.ie.kind = SELECTION_REQUEST_EVENT;
6100 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6101 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6102 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6103 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6104 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6105 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6106 inev.ie.frame_or_window = Qnil;
6108 break;
6110 case PropertyNotify:
6111 last_user_time = event.xproperty.time;
6112 f = x_top_window_to_frame (dpyinfo, event.xproperty.window);
6113 if (f && event.xproperty.atom == dpyinfo->Xatom_net_wm_state)
6114 x_handle_net_wm_state (f, &event.xproperty);
6116 x_handle_property_notify (&event.xproperty);
6117 xft_settings_event (dpyinfo, &event);
6118 goto OTHER;
6120 case ReparentNotify:
6121 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6122 if (f)
6124 int x, y;
6125 f->output_data.x->parent_desc = event.xreparent.parent;
6126 x_real_positions (f, &x, &y);
6127 f->left_pos = x;
6128 f->top_pos = y;
6130 /* Perhaps reparented due to a WM restart. Reset this. */
6131 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6132 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6134 x_set_frame_alpha (f);
6136 goto OTHER;
6138 case Expose:
6139 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6140 if (f)
6142 #ifdef USE_GTK
6143 /* This seems to be needed for GTK 2.6. */
6144 x_clear_area (event.xexpose.display,
6145 event.xexpose.window,
6146 event.xexpose.x, event.xexpose.y,
6147 event.xexpose.width, event.xexpose.height,
6148 FALSE);
6149 #endif
6150 if (f->async_visible == 0)
6152 f->async_visible = 1;
6153 f->async_iconified = 0;
6154 f->output_data.x->has_been_visible = 1;
6155 SET_FRAME_GARBAGED (f);
6157 else
6158 expose_frame (f,
6159 event.xexpose.x, event.xexpose.y,
6160 event.xexpose.width, event.xexpose.height);
6162 else
6164 #ifndef USE_TOOLKIT_SCROLL_BARS
6165 struct scroll_bar *bar;
6166 #endif
6167 #if defined USE_LUCID
6168 /* Submenus of the Lucid menu bar aren't widgets
6169 themselves, so there's no way to dispatch events
6170 to them. Recognize this case separately. */
6172 Widget widget
6173 = x_window_to_menu_bar (event.xexpose.window);
6174 if (widget)
6175 xlwmenu_redisplay (widget);
6177 #endif /* USE_LUCID */
6179 #ifdef USE_TOOLKIT_SCROLL_BARS
6180 /* Dispatch event to the widget. */
6181 goto OTHER;
6182 #else /* not USE_TOOLKIT_SCROLL_BARS */
6183 bar = x_window_to_scroll_bar (event.xexpose.display,
6184 event.xexpose.window);
6186 if (bar)
6187 x_scroll_bar_expose (bar, &event);
6188 #ifdef USE_X_TOOLKIT
6189 else
6190 goto OTHER;
6191 #endif /* USE_X_TOOLKIT */
6192 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6194 break;
6196 case GraphicsExpose: /* This occurs when an XCopyArea's
6197 source area was obscured or not
6198 available. */
6199 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6200 if (f)
6202 expose_frame (f,
6203 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6204 event.xgraphicsexpose.width,
6205 event.xgraphicsexpose.height);
6207 #ifdef USE_X_TOOLKIT
6208 else
6209 goto OTHER;
6210 #endif /* USE_X_TOOLKIT */
6211 break;
6213 case NoExpose: /* This occurs when an XCopyArea's
6214 source area was completely
6215 available. */
6216 break;
6218 case UnmapNotify:
6219 /* Redo the mouse-highlight after the tooltip has gone. */
6220 if (event.xmap.window == tip_window)
6222 tip_window = 0;
6223 redo_mouse_highlight ();
6226 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6227 if (f) /* F may no longer exist if
6228 the frame was deleted. */
6230 /* While a frame is unmapped, display generation is
6231 disabled; you don't want to spend time updating a
6232 display that won't ever be seen. */
6233 f->async_visible = 0;
6234 /* We can't distinguish, from the event, whether the window
6235 has become iconified or invisible. So assume, if it
6236 was previously visible, than now it is iconified.
6237 But x_make_frame_invisible clears both
6238 the visible flag and the iconified flag;
6239 and that way, we know the window is not iconified now. */
6240 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6242 f->async_iconified = 1;
6244 inev.ie.kind = ICONIFY_EVENT;
6245 XSETFRAME (inev.ie.frame_or_window, f);
6248 goto OTHER;
6250 case MapNotify:
6251 if (event.xmap.window == tip_window)
6252 /* The tooltip has been drawn already. Avoid
6253 the SET_FRAME_GARBAGED below. */
6254 goto OTHER;
6256 /* We use x_top_window_to_frame because map events can
6257 come for sub-windows and they don't mean that the
6258 frame is visible. */
6259 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6260 if (f)
6262 /* wait_reading_process_output will notice this and update
6263 the frame's display structures.
6264 If we where iconified, we should not set garbaged,
6265 because that stops redrawing on Expose events. This looks
6266 bad if we are called from a recursive event loop
6267 (x_dispatch_event), for example when a dialog is up. */
6268 if (! f->async_iconified)
6269 SET_FRAME_GARBAGED (f);
6271 /* Check if fullscreen was specified before we where mapped the
6272 first time, i.e. from the command line. */
6273 if (!f->output_data.x->has_been_visible)
6274 x_check_fullscreen (f);
6276 f->async_visible = 1;
6277 f->async_iconified = 0;
6278 f->output_data.x->has_been_visible = 1;
6280 if (f->iconified)
6282 inev.ie.kind = DEICONIFY_EVENT;
6283 XSETFRAME (inev.ie.frame_or_window, f);
6285 else if (! NILP (Vframe_list)
6286 && ! NILP (XCDR (Vframe_list)))
6287 /* Force a redisplay sooner or later
6288 to update the frame titles
6289 in case this is the second frame. */
6290 record_asynch_buffer_change ();
6292 #ifdef USE_GTK
6293 xg_frame_resized (f, -1, -1);
6294 #endif
6296 goto OTHER;
6298 case KeyPress:
6300 last_user_time = event.xkey.time;
6301 ignore_next_mouse_click_timeout = 0;
6303 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6304 /* Dispatch KeyPress events when in menu. */
6305 if (popup_activated ())
6306 goto OTHER;
6307 #endif
6309 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6311 /* If mouse-highlight is an integer, input clears out
6312 mouse highlighting. */
6313 if (!hlinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6314 && (f == 0
6315 || !EQ (f->tool_bar_window, hlinfo->mouse_face_window)))
6317 clear_mouse_face (hlinfo);
6318 hlinfo->mouse_face_hidden = 1;
6321 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6322 if (f == 0)
6324 /* Scroll bars consume key events, but we want
6325 the keys to go to the scroll bar's frame. */
6326 Widget widget = XtWindowToWidget (dpyinfo->display,
6327 event.xkey.window);
6328 if (widget && XmIsScrollBar (widget))
6330 widget = XtParent (widget);
6331 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6334 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6336 if (f != 0)
6338 KeySym keysym, orig_keysym;
6339 /* al%imercury@uunet.uu.net says that making this 81
6340 instead of 80 fixed a bug whereby meta chars made
6341 his Emacs hang.
6343 It seems that some version of XmbLookupString has
6344 a bug of not returning XBufferOverflow in
6345 status_return even if the input is too long to
6346 fit in 81 bytes. So, we must prepare sufficient
6347 bytes for copy_buffer. 513 bytes (256 chars for
6348 two-byte character set) seems to be a fairly good
6349 approximation. -- 2000.8.10 handa@etl.go.jp */
6350 unsigned char copy_buffer[513];
6351 unsigned char *copy_bufptr = copy_buffer;
6352 int copy_bufsiz = sizeof (copy_buffer);
6353 int modifiers;
6354 Lisp_Object coding_system = Qlatin_1;
6355 Lisp_Object c;
6357 #ifdef USE_GTK
6358 /* Don't pass keys to GTK. A Tab will shift focus to the
6359 tool bar in GTK 2.4. Keys will still go to menus and
6360 dialogs because in that case popup_activated is TRUE
6361 (see above). */
6362 *finish = X_EVENT_DROP;
6363 #endif
6365 event.xkey.state
6366 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6367 extra_keyboard_modifiers);
6368 modifiers = event.xkey.state;
6370 /* This will have to go some day... */
6372 /* make_lispy_event turns chars into control chars.
6373 Don't do it here because XLookupString is too eager. */
6374 event.xkey.state &= ~ControlMask;
6375 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6376 | dpyinfo->super_mod_mask
6377 | dpyinfo->hyper_mod_mask
6378 | dpyinfo->alt_mod_mask);
6380 /* In case Meta is ComposeCharacter,
6381 clear its status. According to Markus Ehrnsperger
6382 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6383 this enables ComposeCharacter to work whether or
6384 not it is combined with Meta. */
6385 if (modifiers & dpyinfo->meta_mod_mask)
6386 memset (&compose_status, 0, sizeof (compose_status));
6388 #ifdef HAVE_X_I18N
6389 if (FRAME_XIC (f))
6391 Status status_return;
6393 coding_system = Vlocale_coding_system;
6394 nbytes = XmbLookupString (FRAME_XIC (f),
6395 &event.xkey, (char *) copy_bufptr,
6396 copy_bufsiz, &keysym,
6397 &status_return);
6398 if (status_return == XBufferOverflow)
6400 copy_bufsiz = nbytes + 1;
6401 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6402 nbytes = XmbLookupString (FRAME_XIC (f),
6403 &event.xkey, (char *) copy_bufptr,
6404 copy_bufsiz, &keysym,
6405 &status_return);
6407 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6408 if (status_return == XLookupNone)
6409 break;
6410 else if (status_return == XLookupChars)
6412 keysym = NoSymbol;
6413 modifiers = 0;
6415 else if (status_return != XLookupKeySym
6416 && status_return != XLookupBoth)
6417 abort ();
6419 else
6420 nbytes = XLookupString (&event.xkey, (char *) copy_bufptr,
6421 copy_bufsiz, &keysym,
6422 &compose_status);
6423 #else
6424 nbytes = XLookupString (&event.xkey, (char *) copy_bufptr,
6425 copy_bufsiz, &keysym,
6426 &compose_status);
6427 #endif
6429 /* If not using XIM/XIC, and a compose sequence is in progress,
6430 we break here. Otherwise, chars_matched is always 0. */
6431 if (compose_status.chars_matched > 0 && nbytes == 0)
6432 break;
6434 memset (&compose_status, 0, sizeof (compose_status));
6435 orig_keysym = keysym;
6437 /* Common for all keysym input events. */
6438 XSETFRAME (inev.ie.frame_or_window, f);
6439 inev.ie.modifiers
6440 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6441 inev.ie.timestamp = event.xkey.time;
6443 /* First deal with keysyms which have defined
6444 translations to characters. */
6445 if (keysym >= 32 && keysym < 128)
6446 /* Avoid explicitly decoding each ASCII character. */
6448 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6449 inev.ie.code = keysym;
6450 goto done_keysym;
6453 /* Keysyms directly mapped to Unicode characters. */
6454 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6456 if (keysym < 0x01000080)
6457 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6458 else
6459 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6460 inev.ie.code = keysym & 0xFFFFFF;
6461 goto done_keysym;
6464 /* Now non-ASCII. */
6465 if (HASH_TABLE_P (Vx_keysym_table)
6466 && (NATNUMP (c = Fgethash (make_number (keysym),
6467 Vx_keysym_table,
6468 Qnil))))
6470 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6471 ? ASCII_KEYSTROKE_EVENT
6472 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6473 inev.ie.code = XFASTINT (c);
6474 goto done_keysym;
6477 /* Random non-modifier sorts of keysyms. */
6478 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6479 || keysym == XK_Delete
6480 #ifdef XK_ISO_Left_Tab
6481 || (keysym >= XK_ISO_Left_Tab
6482 && keysym <= XK_ISO_Enter)
6483 #endif
6484 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6485 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6486 #ifdef HPUX
6487 /* This recognizes the "extended function
6488 keys". It seems there's no cleaner way.
6489 Test IsModifierKey to avoid handling
6490 mode_switch incorrectly. */
6491 || (XK_Select <= keysym && keysym < XK_KP_Space)
6492 #endif
6493 #ifdef XK_dead_circumflex
6494 || orig_keysym == XK_dead_circumflex
6495 #endif
6496 #ifdef XK_dead_grave
6497 || orig_keysym == XK_dead_grave
6498 #endif
6499 #ifdef XK_dead_tilde
6500 || orig_keysym == XK_dead_tilde
6501 #endif
6502 #ifdef XK_dead_diaeresis
6503 || orig_keysym == XK_dead_diaeresis
6504 #endif
6505 #ifdef XK_dead_macron
6506 || orig_keysym == XK_dead_macron
6507 #endif
6508 #ifdef XK_dead_degree
6509 || orig_keysym == XK_dead_degree
6510 #endif
6511 #ifdef XK_dead_acute
6512 || orig_keysym == XK_dead_acute
6513 #endif
6514 #ifdef XK_dead_cedilla
6515 || orig_keysym == XK_dead_cedilla
6516 #endif
6517 #ifdef XK_dead_breve
6518 || orig_keysym == XK_dead_breve
6519 #endif
6520 #ifdef XK_dead_ogonek
6521 || orig_keysym == XK_dead_ogonek
6522 #endif
6523 #ifdef XK_dead_caron
6524 || orig_keysym == XK_dead_caron
6525 #endif
6526 #ifdef XK_dead_doubleacute
6527 || orig_keysym == XK_dead_doubleacute
6528 #endif
6529 #ifdef XK_dead_abovedot
6530 || orig_keysym == XK_dead_abovedot
6531 #endif
6532 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6533 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6534 /* Any "vendor-specific" key is ok. */
6535 || (orig_keysym & (1 << 28))
6536 || (keysym != NoSymbol && nbytes == 0))
6537 && ! (IsModifierKey (orig_keysym)
6538 /* The symbols from XK_ISO_Lock
6539 to XK_ISO_Last_Group_Lock
6540 don't have real modifiers but
6541 should be treated similarly to
6542 Mode_switch by Emacs. */
6543 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6544 || (XK_ISO_Lock <= orig_keysym
6545 && orig_keysym <= XK_ISO_Last_Group_Lock)
6546 #endif
6549 STORE_KEYSYM_FOR_DEBUG (keysym);
6550 /* make_lispy_event will convert this to a symbolic
6551 key. */
6552 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6553 inev.ie.code = keysym;
6554 goto done_keysym;
6557 { /* Raw bytes, not keysym. */
6558 ptrdiff_t i;
6559 int nchars, len;
6561 for (i = 0, nchars = 0; i < nbytes; i++)
6563 if (ASCII_BYTE_P (copy_bufptr[i]))
6564 nchars++;
6565 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6568 if (nchars < nbytes)
6570 /* Decode the input data. */
6572 /* The input should be decoded with `coding_system'
6573 which depends on which X*LookupString function
6574 we used just above and the locale. */
6575 setup_coding_system (coding_system, &coding);
6576 coding.src_multibyte = 0;
6577 coding.dst_multibyte = 1;
6578 /* The input is converted to events, thus we can't
6579 handle composition. Anyway, there's no XIM that
6580 gives us composition information. */
6581 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6583 SAFE_NALLOCA (coding.destination, MAX_MULTIBYTE_LENGTH,
6584 nbytes);
6585 coding.dst_bytes = MAX_MULTIBYTE_LENGTH * nbytes;
6586 coding.mode |= CODING_MODE_LAST_BLOCK;
6587 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6588 nbytes = coding.produced;
6589 nchars = coding.produced_char;
6590 copy_bufptr = coding.destination;
6593 /* Convert the input data to a sequence of
6594 character events. */
6595 for (i = 0; i < nbytes; i += len)
6597 int ch;
6598 if (nchars == nbytes)
6599 ch = copy_bufptr[i], len = 1;
6600 else
6601 ch = STRING_CHAR_AND_LENGTH (copy_bufptr + i, len);
6602 inev.ie.kind = (SINGLE_BYTE_CHAR_P (ch)
6603 ? ASCII_KEYSTROKE_EVENT
6604 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6605 inev.ie.code = ch;
6606 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6609 count += nchars;
6611 inev.ie.kind = NO_EVENT; /* Already stored above. */
6613 if (keysym == NoSymbol)
6614 break;
6617 done_keysym:
6618 #ifdef HAVE_X_I18N
6619 /* Don't dispatch this event since XtDispatchEvent calls
6620 XFilterEvent, and two calls in a row may freeze the
6621 client. */
6622 break;
6623 #else
6624 goto OTHER;
6625 #endif
6627 case KeyRelease:
6628 last_user_time = event.xkey.time;
6629 #ifdef HAVE_X_I18N
6630 /* Don't dispatch this event since XtDispatchEvent calls
6631 XFilterEvent, and two calls in a row may freeze the
6632 client. */
6633 break;
6634 #else
6635 goto OTHER;
6636 #endif
6638 case EnterNotify:
6639 last_user_time = event.xcrossing.time;
6640 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6642 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6644 if (f && x_mouse_click_focus_ignore_position)
6645 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6647 /* EnterNotify counts as mouse movement,
6648 so update things that depend on mouse position. */
6649 if (f && !f->output_data.x->hourglass_p)
6650 note_mouse_movement (f, &event.xmotion);
6651 #ifdef USE_GTK
6652 /* We may get an EnterNotify on the buttons in the toolbar. In that
6653 case we moved out of any highlighted area and need to note this. */
6654 if (!f && last_mouse_glyph_frame)
6655 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6656 #endif
6657 goto OTHER;
6659 case FocusIn:
6660 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6661 goto OTHER;
6663 case LeaveNotify:
6664 last_user_time = event.xcrossing.time;
6665 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6667 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6668 if (f)
6670 if (f == hlinfo->mouse_face_mouse_frame)
6672 /* If we move outside the frame, then we're
6673 certainly no longer on any text in the frame. */
6674 clear_mouse_face (hlinfo);
6675 hlinfo->mouse_face_mouse_frame = 0;
6678 /* Generate a nil HELP_EVENT to cancel a help-echo.
6679 Do it only if there's something to cancel.
6680 Otherwise, the startup message is cleared when
6681 the mouse leaves the frame. */
6682 if (any_help_event_p)
6683 do_help = -1;
6685 #ifdef USE_GTK
6686 /* See comment in EnterNotify above */
6687 else if (last_mouse_glyph_frame)
6688 note_mouse_movement (last_mouse_glyph_frame, &event.xmotion);
6689 #endif
6690 goto OTHER;
6692 case FocusOut:
6693 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6694 goto OTHER;
6696 case MotionNotify:
6698 last_user_time = event.xmotion.time;
6699 previous_help_echo_string = help_echo_string;
6700 help_echo_string = Qnil;
6702 if (dpyinfo->grabbed && last_mouse_frame
6703 && FRAME_LIVE_P (last_mouse_frame))
6704 f = last_mouse_frame;
6705 else
6706 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6708 if (hlinfo->mouse_face_hidden)
6710 hlinfo->mouse_face_hidden = 0;
6711 clear_mouse_face (hlinfo);
6714 #ifdef USE_GTK
6715 if (f && xg_event_is_for_scrollbar (f, &event))
6716 f = 0;
6717 #endif
6718 if (f)
6721 /* Generate SELECT_WINDOW_EVENTs when needed.
6722 Don't let popup menus influence things (bug#1261). */
6723 if (!NILP (Vmouse_autoselect_window) && !popup_activated ())
6725 Lisp_Object window;
6727 window = window_from_coordinates (f,
6728 event.xmotion.x, event.xmotion.y,
6729 0, 0);
6731 /* Window will be selected only when it is not selected now and
6732 last mouse movement event was not in it. Minibuffer window
6733 will be selected only when it is active. */
6734 if (WINDOWP (window)
6735 && !EQ (window, last_window)
6736 && !EQ (window, selected_window)
6737 /* For click-to-focus window managers
6738 create event iff we don't leave the
6739 selected frame. */
6740 && (focus_follows_mouse
6741 || (EQ (XWINDOW (window)->frame,
6742 XWINDOW (selected_window)->frame))))
6744 inev.ie.kind = SELECT_WINDOW_EVENT;
6745 inev.ie.frame_or_window = window;
6748 last_window=window;
6750 if (!note_mouse_movement (f, &event.xmotion))
6751 help_echo_string = previous_help_echo_string;
6753 else
6755 #ifndef USE_TOOLKIT_SCROLL_BARS
6756 struct scroll_bar *bar
6757 = x_window_to_scroll_bar (event.xmotion.display,
6758 event.xmotion.window);
6760 if (bar)
6761 x_scroll_bar_note_movement (bar, &event);
6762 #endif /* USE_TOOLKIT_SCROLL_BARS */
6764 /* If we move outside the frame, then we're
6765 certainly no longer on any text in the frame. */
6766 clear_mouse_face (hlinfo);
6769 /* If the contents of the global variable help_echo_string
6770 has changed, generate a HELP_EVENT. */
6771 if (!NILP (help_echo_string)
6772 || !NILP (previous_help_echo_string))
6773 do_help = 1;
6774 goto OTHER;
6777 case ConfigureNotify:
6778 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6779 #ifdef USE_GTK
6780 if (!f
6781 && (f = x_any_window_to_frame (dpyinfo, event.xconfigure.window))
6782 && event.xconfigure.window == FRAME_X_WINDOW (f))
6784 xg_frame_resized (f, event.xconfigure.width,
6785 event.xconfigure.height);
6786 f = 0;
6788 #endif
6789 if (f)
6791 #ifndef USE_X_TOOLKIT
6792 #ifndef USE_GTK
6793 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6794 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6796 /* In the toolkit version, change_frame_size
6797 is called by the code that handles resizing
6798 of the EmacsFrame widget. */
6800 /* Even if the number of character rows and columns has
6801 not changed, the font size may have changed, so we need
6802 to check the pixel dimensions as well. */
6803 if (columns != FRAME_COLS (f)
6804 || rows != FRAME_LINES (f)
6805 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6806 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6808 change_frame_size (f, rows, columns, 0, 1, 0);
6809 SET_FRAME_GARBAGED (f);
6810 cancel_mouse_face (f);
6813 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6814 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6815 #endif /* not USE_GTK */
6816 #endif
6818 #ifdef USE_GTK
6819 /* GTK creates windows but doesn't map them.
6820 Only get real positions when mapped. */
6821 if (FRAME_GTK_OUTER_WIDGET (f)
6822 && gtk_widget_get_mapped (FRAME_GTK_OUTER_WIDGET (f)))
6823 #endif
6825 x_real_positions (f, &f->left_pos, &f->top_pos);
6828 #ifdef HAVE_X_I18N
6829 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6830 xic_set_statusarea (f);
6831 #endif
6834 goto OTHER;
6836 case ButtonRelease:
6837 case ButtonPress:
6839 /* If we decide we want to generate an event to be seen
6840 by the rest of Emacs, we put it here. */
6841 int tool_bar_p = 0;
6843 memset (&compose_status, 0, sizeof (compose_status));
6844 last_mouse_glyph_frame = 0;
6845 last_user_time = event.xbutton.time;
6847 if (dpyinfo->grabbed
6848 && last_mouse_frame
6849 && FRAME_LIVE_P (last_mouse_frame))
6850 f = last_mouse_frame;
6851 else
6852 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6854 #ifdef USE_GTK
6855 if (f && xg_event_is_for_scrollbar (f, &event))
6856 f = 0;
6857 #endif
6858 if (f)
6860 /* Is this in the tool-bar? */
6861 if (WINDOWP (f->tool_bar_window)
6862 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6864 Lisp_Object window;
6865 int x = event.xbutton.x;
6866 int y = event.xbutton.y;
6868 window = window_from_coordinates (f, x, y, 0, 1);
6869 tool_bar_p = EQ (window, f->tool_bar_window);
6871 if (tool_bar_p && event.xbutton.button < 4)
6873 handle_tool_bar_click (f, x, y,
6874 event.xbutton.type == ButtonPress,
6875 x_x_to_emacs_modifiers (dpyinfo,
6876 event.xbutton.state));
6880 if (!tool_bar_p)
6881 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6882 if (! popup_activated ())
6883 #endif
6885 if (ignore_next_mouse_click_timeout)
6887 if (event.type == ButtonPress
6888 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
6890 ignore_next_mouse_click_timeout = 0;
6891 construct_mouse_click (&inev.ie, &event.xbutton, f);
6893 if (event.type == ButtonRelease)
6894 ignore_next_mouse_click_timeout = 0;
6896 else
6897 construct_mouse_click (&inev.ie, &event.xbutton, f);
6899 if (FRAME_X_EMBEDDED_P (f))
6900 xembed_send_message (f, event.xbutton.time,
6901 XEMBED_REQUEST_FOCUS, 0, 0, 0);
6903 else
6905 struct scroll_bar *bar
6906 = x_window_to_scroll_bar (event.xbutton.display,
6907 event.xbutton.window);
6909 #ifdef USE_TOOLKIT_SCROLL_BARS
6910 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
6911 scroll bars. */
6912 if (bar && event.xbutton.state & ControlMask)
6914 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6915 *finish = X_EVENT_DROP;
6917 #else /* not USE_TOOLKIT_SCROLL_BARS */
6918 if (bar)
6919 x_scroll_bar_handle_click (bar, &event, &inev.ie);
6920 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6923 if (event.type == ButtonPress)
6925 dpyinfo->grabbed |= (1 << event.xbutton.button);
6926 last_mouse_frame = f;
6928 if (!tool_bar_p)
6929 last_tool_bar_item = -1;
6931 else
6932 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
6934 /* Ignore any mouse motion that happened before this event;
6935 any subsequent mouse-movement Emacs events should reflect
6936 only motion after the ButtonPress/Release. */
6937 if (f != 0)
6938 f->mouse_moved = 0;
6940 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6941 f = x_menubar_window_to_frame (dpyinfo, &event);
6942 /* For a down-event in the menu bar,
6943 don't pass it to Xt right now.
6944 Instead, save it away
6945 and we will pass it to Xt from kbd_buffer_get_event.
6946 That way, we can run some Lisp code first. */
6947 if (! popup_activated ()
6948 #ifdef USE_GTK
6949 /* Gtk+ menus only react to the first three buttons. */
6950 && event.xbutton.button < 3
6951 #endif
6952 && f && event.type == ButtonPress
6953 /* Verify the event is really within the menu bar
6954 and not just sent to it due to grabbing. */
6955 && event.xbutton.x >= 0
6956 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
6957 && event.xbutton.y >= 0
6958 && event.xbutton.y < f->output_data.x->menubar_height
6959 && event.xbutton.same_screen)
6961 SET_SAVED_BUTTON_EVENT;
6962 XSETFRAME (last_mouse_press_frame, f);
6963 *finish = X_EVENT_DROP;
6965 else if (event.type == ButtonPress)
6967 last_mouse_press_frame = Qnil;
6968 goto OTHER;
6970 else
6971 goto OTHER;
6972 #endif /* USE_X_TOOLKIT || USE_GTK */
6974 break;
6976 case CirculateNotify:
6977 goto OTHER;
6979 case CirculateRequest:
6980 goto OTHER;
6982 case VisibilityNotify:
6983 goto OTHER;
6985 case MappingNotify:
6986 /* Someone has changed the keyboard mapping - update the
6987 local cache. */
6988 switch (event.xmapping.request)
6990 case MappingModifier:
6991 x_find_modifier_meanings (dpyinfo);
6992 /* This is meant to fall through. */
6993 case MappingKeyboard:
6994 XRefreshKeyboardMapping (&event.xmapping);
6996 goto OTHER;
6998 case DestroyNotify:
6999 xft_settings_event (dpyinfo, &event);
7000 break;
7002 default:
7003 OTHER:
7004 #ifdef USE_X_TOOLKIT
7005 BLOCK_INPUT;
7006 if (*finish != X_EVENT_DROP)
7007 XtDispatchEvent (&event);
7008 UNBLOCK_INPUT;
7009 #endif /* USE_X_TOOLKIT */
7010 break;
7013 done:
7014 if (inev.ie.kind != NO_EVENT)
7016 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7017 count++;
7020 if (do_help
7021 && !(hold_quit && hold_quit->kind != NO_EVENT))
7023 Lisp_Object frame;
7025 if (f)
7026 XSETFRAME (frame, f);
7027 else
7028 frame = Qnil;
7030 if (do_help > 0)
7032 any_help_event_p = 1;
7033 gen_help_event (help_echo_string, frame, help_echo_window,
7034 help_echo_object, help_echo_pos);
7036 else
7038 help_echo_string = Qnil;
7039 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7041 count++;
7044 SAFE_FREE ();
7045 *eventptr = event;
7046 return count;
7049 #if defined USE_GTK || defined USE_X_TOOLKIT
7051 /* Handles the XEvent EVENT on display DISPLAY.
7052 This is used for event loops outside the normal event handling,
7053 i.e. looping while a popup menu or a dialog is posted.
7055 Returns the value handle_one_xevent sets in the finish argument. */
7057 x_dispatch_event (XEvent *event, Display *display)
7059 struct x_display_info *dpyinfo;
7060 int finish = X_EVENT_NORMAL;
7062 dpyinfo = x_display_info_for_display (display);
7064 if (dpyinfo)
7065 handle_one_xevent (dpyinfo, event, &finish, 0);
7067 return finish;
7069 #endif
7072 /* Read events coming from the X server.
7073 This routine is called by the SIGIO handler.
7074 We return as soon as there are no more events to be read.
7076 We return the number of characters stored into the buffer,
7077 thus pretending to be `read' (except the characters we store
7078 in the keyboard buffer can be multibyte, so are not necessarily
7079 C chars).
7081 EXPECTED is nonzero if the caller knows input is available. */
7083 static int
7084 XTread_socket (struct terminal *terminal, int expected, struct input_event *hold_quit)
7086 int count = 0;
7087 int event_found = 0;
7089 if (interrupt_input_blocked)
7091 interrupt_input_pending = 1;
7092 #ifdef SYNC_INPUT
7093 pending_signals = 1;
7094 #endif
7095 return -1;
7098 interrupt_input_pending = 0;
7099 #ifdef SYNC_INPUT
7100 pending_signals = pending_atimers;
7101 #endif
7102 BLOCK_INPUT;
7104 /* So people can tell when we have read the available input. */
7105 input_signal_count++;
7107 ++handling_signal;
7109 /* For debugging, this gives a way to fake an I/O error. */
7110 if (terminal->display_info.x == XTread_socket_fake_io_error)
7112 XTread_socket_fake_io_error = 0;
7113 x_io_error_quitter (terminal->display_info.x->display);
7116 #ifndef USE_GTK
7117 while (XPending (terminal->display_info.x->display))
7119 int finish;
7120 XEvent event;
7122 XNextEvent (terminal->display_info.x->display, &event);
7124 #ifdef HAVE_X_I18N
7125 /* Filter events for the current X input method. */
7126 if (x_filter_event (terminal->display_info.x, &event))
7127 continue;
7128 #endif
7129 event_found = 1;
7131 count += handle_one_xevent (terminal->display_info.x,
7132 &event, &finish, hold_quit);
7134 if (finish == X_EVENT_GOTO_OUT)
7135 goto out;
7138 out:;
7140 #else /* USE_GTK */
7142 /* For GTK we must use the GTK event loop. But XEvents gets passed
7143 to our filter function above, and then to the big event switch.
7144 We use a bunch of globals to communicate with our filter function,
7145 that is kind of ugly, but it works.
7147 There is no way to do one display at the time, GTK just does events
7148 from all displays. */
7150 while (gtk_events_pending ())
7152 current_count = count;
7153 current_hold_quit = hold_quit;
7155 gtk_main_iteration ();
7157 count = current_count;
7158 current_count = -1;
7159 current_hold_quit = 0;
7161 if (current_finish == X_EVENT_GOTO_OUT)
7162 break;
7164 #endif /* USE_GTK */
7166 /* On some systems, an X bug causes Emacs to get no more events
7167 when the window is destroyed. Detect that. (1994.) */
7168 if (! event_found)
7170 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7171 One XNOOP in 100 loops will make Emacs terminate.
7172 B. Bretthauer, 1994 */
7173 x_noop_count++;
7174 if (x_noop_count >= 100)
7176 x_noop_count=0;
7178 if (next_noop_dpyinfo == 0)
7179 next_noop_dpyinfo = x_display_list;
7181 XNoOp (next_noop_dpyinfo->display);
7183 /* Each time we get here, cycle through the displays now open. */
7184 next_noop_dpyinfo = next_noop_dpyinfo->next;
7188 /* If the focus was just given to an auto-raising frame,
7189 raise it now. */
7190 /* ??? This ought to be able to handle more than one such frame. */
7191 if (pending_autoraise_frame)
7193 x_raise_frame (pending_autoraise_frame);
7194 pending_autoraise_frame = 0;
7197 --handling_signal;
7198 UNBLOCK_INPUT;
7200 return count;
7206 /***********************************************************************
7207 Text Cursor
7208 ***********************************************************************/
7210 /* Set clipping for output in glyph row ROW. W is the window in which
7211 we operate. GC is the graphics context to set clipping in.
7213 ROW may be a text row or, e.g., a mode line. Text rows must be
7214 clipped to the interior of the window dedicated to text display,
7215 mode lines must be clipped to the whole window. */
7217 static void
7218 x_clip_to_row (struct window *w, struct glyph_row *row, int area, GC gc)
7220 struct frame *f = XFRAME (WINDOW_FRAME (w));
7221 XRectangle clip_rect;
7222 int window_x, window_y, window_width;
7224 window_box (w, area, &window_x, &window_y, &window_width, 0);
7226 clip_rect.x = window_x;
7227 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7228 clip_rect.y = max (clip_rect.y, window_y);
7229 clip_rect.width = window_width;
7230 clip_rect.height = row->visible_height;
7232 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7236 /* Draw a hollow box cursor on window W in glyph row ROW. */
7238 static void
7239 x_draw_hollow_cursor (struct window *w, struct glyph_row *row)
7241 struct frame *f = XFRAME (WINDOW_FRAME (w));
7242 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7243 Display *dpy = FRAME_X_DISPLAY (f);
7244 int x, y, wd, h;
7245 XGCValues xgcv;
7246 struct glyph *cursor_glyph;
7247 GC gc;
7249 /* Get the glyph the cursor is on. If we can't tell because
7250 the current matrix is invalid or such, give up. */
7251 cursor_glyph = get_phys_cursor_glyph (w);
7252 if (cursor_glyph == NULL)
7253 return;
7255 /* Compute frame-relative coordinates for phys cursor. */
7256 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7257 wd = w->phys_cursor_width;
7259 /* The foreground of cursor_gc is typically the same as the normal
7260 background color, which can cause the cursor box to be invisible. */
7261 xgcv.foreground = f->output_data.x->cursor_pixel;
7262 if (dpyinfo->scratch_cursor_gc)
7263 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7264 else
7265 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7266 GCForeground, &xgcv);
7267 gc = dpyinfo->scratch_cursor_gc;
7269 /* Set clipping, draw the rectangle, and reset clipping again. */
7270 x_clip_to_row (w, row, TEXT_AREA, gc);
7271 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7272 XSetClipMask (dpy, gc, None);
7276 /* Draw a bar cursor on window W in glyph row ROW.
7278 Implementation note: One would like to draw a bar cursor with an
7279 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7280 Unfortunately, I didn't find a font yet that has this property set.
7281 --gerd. */
7283 static void
7284 x_draw_bar_cursor (struct window *w, struct glyph_row *row, int width, enum text_cursor_kinds kind)
7286 struct frame *f = XFRAME (w->frame);
7287 struct glyph *cursor_glyph;
7289 /* If cursor is out of bounds, don't draw garbage. This can happen
7290 in mini-buffer windows when switching between echo area glyphs
7291 and mini-buffer. */
7292 cursor_glyph = get_phys_cursor_glyph (w);
7293 if (cursor_glyph == NULL)
7294 return;
7295 #ifdef HAVE_XWIDGETS
7296 if (cursor_glyph->type == XWIDGET_GLYPH){
7297 printf("tried avoiding xwidget cursor\n");
7298 return; //experimental avoidance of cursor on xwidget
7300 #endif
7301 /* If on an image, draw like a normal cursor. That's usually better
7302 visible than drawing a bar, esp. if the image is large so that
7303 the bar might not be in the window. */
7304 if (cursor_glyph->type == IMAGE_GLYPH)
7306 struct glyph_row *r;
7307 r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7308 draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
7310 else
7312 Display *dpy = FRAME_X_DISPLAY (f);
7313 Window window = FRAME_X_WINDOW (f);
7314 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7315 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7316 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7317 XGCValues xgcv;
7319 /* If the glyph's background equals the color we normally draw
7320 the bars cursor in, the bar cursor in its normal color is
7321 invisible. Use the glyph's foreground color instead in this
7322 case, on the assumption that the glyph's colors are chosen so
7323 that the glyph is legible. */
7324 if (face->background == f->output_data.x->cursor_pixel)
7325 xgcv.background = xgcv.foreground = face->foreground;
7326 else
7327 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7328 xgcv.graphics_exposures = 0;
7330 if (gc)
7331 XChangeGC (dpy, gc, mask, &xgcv);
7332 else
7334 gc = XCreateGC (dpy, window, mask, &xgcv);
7335 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7338 x_clip_to_row (w, row, TEXT_AREA, gc);
7340 if (kind == BAR_CURSOR)
7342 int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
7344 if (width < 0)
7345 width = FRAME_CURSOR_WIDTH (f);
7346 width = min (cursor_glyph->pixel_width, width);
7348 w->phys_cursor_width = width;
7350 /* If the character under cursor is R2L, draw the bar cursor
7351 on the right of its glyph, rather than on the left. */
7352 if ((cursor_glyph->resolved_level & 1) != 0)
7353 x += cursor_glyph->pixel_width - width;
7355 XFillRectangle (dpy, window, gc, x,
7356 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7357 width, row->height);
7359 else
7361 int dummy_x, dummy_y, dummy_h;
7363 if (width < 0)
7364 width = row->height;
7366 width = min (row->height, width);
7368 get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
7369 &dummy_y, &dummy_h);
7371 XFillRectangle (dpy, window, gc,
7372 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7373 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7374 row->height - width),
7375 w->phys_cursor_width, width);
7378 XSetClipMask (dpy, gc, None);
7383 /* RIF: Define cursor CURSOR on frame F. */
7385 static void
7386 x_define_frame_cursor (struct frame *f, Cursor cursor)
7388 if (!f->pointer_invisible
7389 && f->output_data.x->current_cursor != cursor)
7390 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7391 f->output_data.x->current_cursor = cursor;
7395 /* RIF: Clear area on frame F. */
7397 static void
7398 x_clear_frame_area (struct frame *f, int x, int y, int width, int height)
7400 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7401 x, y, width, height, False);
7402 #ifdef USE_GTK
7403 /* Must queue a redraw, because scroll bars might have been cleared. */
7404 if (FRAME_GTK_WIDGET (f))
7405 gtk_widget_queue_draw (FRAME_GTK_WIDGET (f));
7406 #endif
7410 /* RIF: Draw cursor on window W. */
7412 static void
7413 x_draw_window_cursor (struct window *w, struct glyph_row *glyph_row, int x, int y, int cursor_type, int cursor_width, int on_p, int active_p)
7415 struct frame *f = XFRAME (WINDOW_FRAME (w));
7417 if (on_p)
7419 w->phys_cursor_type = cursor_type;
7420 w->phys_cursor_on_p = 1;
7422 if (glyph_row->exact_window_width_line_p
7423 && (glyph_row->reversed_p
7424 ? (w->phys_cursor.hpos < 0)
7425 : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
7427 glyph_row->cursor_in_fringe_p = 1;
7428 draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
7430 else
7432 switch (cursor_type)
7434 case HOLLOW_BOX_CURSOR:
7435 x_draw_hollow_cursor (w, glyph_row);
7436 break;
7438 case FILLED_BOX_CURSOR:
7439 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7440 break;
7442 case BAR_CURSOR:
7443 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7444 break;
7446 case HBAR_CURSOR:
7447 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7448 break;
7450 case NO_CURSOR:
7451 w->phys_cursor_width = 0;
7452 break;
7454 default:
7455 abort ();
7459 #ifdef HAVE_X_I18N
7460 if (w == XWINDOW (f->selected_window))
7461 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7462 xic_set_preeditarea (w, x, y);
7463 #endif
7466 #ifndef XFlush
7467 XFlush (FRAME_X_DISPLAY (f));
7468 #endif
7472 /* Icons. */
7474 /* Make the x-window of frame F use the gnu icon bitmap. */
7477 x_bitmap_icon (struct frame *f, Lisp_Object file)
7479 ptrdiff_t bitmap_id;
7481 if (FRAME_X_WINDOW (f) == 0)
7482 return 1;
7484 /* Free up our existing icon bitmap and mask if any. */
7485 if (f->output_data.x->icon_bitmap > 0)
7486 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7487 f->output_data.x->icon_bitmap = 0;
7489 if (STRINGP (file))
7491 #ifdef USE_GTK
7492 /* Use gtk_window_set_icon_from_file () if available,
7493 It's not restricted to bitmaps */
7494 if (xg_set_icon (f, file))
7495 return 0;
7496 #endif /* USE_GTK */
7497 bitmap_id = x_create_bitmap_from_file (f, file);
7498 x_create_bitmap_mask (f, bitmap_id);
7500 else
7502 /* Create the GNU bitmap and mask if necessary. */
7503 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7505 ptrdiff_t rc = -1;
7507 #ifdef USE_GTK
7509 if (xg_set_icon (f, xg_default_icon_file)
7510 || xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7511 return 0;
7513 #elif defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7515 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7516 if (rc != -1)
7517 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7519 #endif
7521 /* If all else fails, use the (black and white) xbm image. */
7522 if (rc == -1)
7524 rc = x_create_bitmap_from_data (f, (char *) gnu_xbm_bits,
7525 gnu_xbm_width, gnu_xbm_height);
7526 if (rc == -1)
7527 return 1;
7529 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7530 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7534 /* The first time we create the GNU bitmap and mask,
7535 this increments the ref-count one extra time.
7536 As a result, the GNU bitmap and mask are never freed.
7537 That way, we don't have to worry about allocating it again. */
7538 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7540 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7543 x_wm_set_icon_pixmap (f, bitmap_id);
7544 f->output_data.x->icon_bitmap = bitmap_id;
7546 return 0;
7550 /* Make the x-window of frame F use a rectangle with text.
7551 Use ICON_NAME as the text. */
7554 x_text_icon (struct frame *f, const char *icon_name)
7556 if (FRAME_X_WINDOW (f) == 0)
7557 return 1;
7560 XTextProperty text;
7561 text.value = (unsigned char *) icon_name;
7562 text.encoding = XA_STRING;
7563 text.format = 8;
7564 text.nitems = strlen (icon_name);
7565 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7568 if (f->output_data.x->icon_bitmap > 0)
7569 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7570 f->output_data.x->icon_bitmap = 0;
7571 x_wm_set_icon_pixmap (f, 0);
7573 return 0;
7576 #define X_ERROR_MESSAGE_SIZE 200
7578 /* If non-nil, this should be a string.
7579 It means catch X errors and store the error message in this string.
7581 The reason we use a stack is that x_catch_error/x_uncatch_error can
7582 be called from a signal handler.
7585 struct x_error_message_stack {
7586 char string[X_ERROR_MESSAGE_SIZE];
7587 Display *dpy;
7588 struct x_error_message_stack *prev;
7590 static struct x_error_message_stack *x_error_message;
7592 /* An X error handler which stores the error message in
7593 *x_error_message. This is called from x_error_handler if
7594 x_catch_errors is in effect. */
7596 static void
7597 x_error_catcher (Display *display, XErrorEvent *event)
7599 XGetErrorText (display, event->error_code,
7600 x_error_message->string,
7601 X_ERROR_MESSAGE_SIZE);
7604 /* Begin trapping X errors for display DPY. Actually we trap X errors
7605 for all displays, but DPY should be the display you are actually
7606 operating on.
7608 After calling this function, X protocol errors no longer cause
7609 Emacs to exit; instead, they are recorded in the string
7610 stored in *x_error_message.
7612 Calling x_check_errors signals an Emacs error if an X error has
7613 occurred since the last call to x_catch_errors or x_check_errors.
7615 Calling x_uncatch_errors resumes the normal error handling. */
7617 void
7618 x_catch_errors (Display *dpy)
7620 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7622 /* Make sure any errors from previous requests have been dealt with. */
7623 XSync (dpy, False);
7625 data->dpy = dpy;
7626 data->string[0] = 0;
7627 data->prev = x_error_message;
7628 x_error_message = data;
7631 /* Undo the last x_catch_errors call.
7632 DPY should be the display that was passed to x_catch_errors. */
7634 void
7635 x_uncatch_errors (void)
7637 struct x_error_message_stack *tmp;
7639 BLOCK_INPUT;
7641 /* The display may have been closed before this function is called.
7642 Check if it is still open before calling XSync. */
7643 if (x_display_info_for_display (x_error_message->dpy) != 0)
7644 XSync (x_error_message->dpy, False);
7646 tmp = x_error_message;
7647 x_error_message = x_error_message->prev;
7648 xfree (tmp);
7649 UNBLOCK_INPUT;
7652 /* If any X protocol errors have arrived since the last call to
7653 x_catch_errors or x_check_errors, signal an Emacs error using
7654 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7656 void
7657 x_check_errors (Display *dpy, const char *format)
7659 /* Make sure to catch any errors incurred so far. */
7660 XSync (dpy, False);
7662 if (x_error_message->string[0])
7664 char string[X_ERROR_MESSAGE_SIZE];
7665 memcpy (string, x_error_message->string, X_ERROR_MESSAGE_SIZE);
7666 x_uncatch_errors ();
7667 error (format, string);
7671 /* Nonzero if we had any X protocol errors
7672 since we did x_catch_errors on DPY. */
7675 x_had_errors_p (Display *dpy)
7677 /* Make sure to catch any errors incurred so far. */
7678 XSync (dpy, False);
7680 return x_error_message->string[0] != 0;
7683 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7685 void
7686 x_clear_errors (Display *dpy)
7688 x_error_message->string[0] = 0;
7691 #if 0 /* See comment in unwind_to_catch why calling this is a bad
7692 * idea. --lorentey */
7693 /* Close off all unclosed x_catch_errors calls. */
7695 void
7696 x_fully_uncatch_errors (void)
7698 while (x_error_message)
7699 x_uncatch_errors ();
7701 #endif
7703 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7706 x_catching_errors (void)
7708 return x_error_message != 0;
7711 #if 0
7712 static unsigned int x_wire_count;
7713 x_trace_wire (void)
7715 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7717 #endif /* ! 0 */
7720 /* Handle SIGPIPE, which can happen when the connection to a server
7721 simply goes away. SIGPIPE is handled by x_connection_signal.
7722 Don't need to do anything, because the write which caused the
7723 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7724 which will do the appropriate cleanup for us. */
7726 static void
7727 x_connection_signal (int signalnum) /* If we don't have an argument, */
7728 /* some compilers complain in signal calls. */
7730 #ifdef USG
7731 /* USG systems forget handlers when they are used;
7732 must reestablish each time */
7733 signal (signalnum, x_connection_signal);
7734 #endif /* USG */
7738 /************************************************************************
7739 Handling X errors
7740 ************************************************************************/
7742 /* Error message passed to x_connection_closed. */
7744 static char *error_msg;
7746 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7747 the text of an error message that lead to the connection loss. */
7749 static void
7750 x_connection_closed (Display *dpy, const char *error_message)
7752 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7753 Lisp_Object frame, tail;
7754 int idx = SPECPDL_INDEX ();
7756 error_msg = (char *) alloca (strlen (error_message) + 1);
7757 strcpy (error_msg, error_message);
7758 handling_signal = 0;
7760 /* Inhibit redisplay while frames are being deleted. */
7761 specbind (Qinhibit_redisplay, Qt);
7763 if (dpyinfo)
7765 /* Protect display from being closed when we delete the last
7766 frame on it. */
7767 dpyinfo->reference_count++;
7768 dpyinfo->terminal->reference_count++;
7771 /* First delete frames whose mini-buffers are on frames
7772 that are on the dead display. */
7773 FOR_EACH_FRAME (tail, frame)
7775 Lisp_Object minibuf_frame;
7776 minibuf_frame
7777 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7778 if (FRAME_X_P (XFRAME (frame))
7779 && FRAME_X_P (XFRAME (minibuf_frame))
7780 && ! EQ (frame, minibuf_frame)
7781 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7782 delete_frame (frame, Qnoelisp);
7785 /* Now delete all remaining frames on the dead display.
7786 We are now sure none of these is used as the mini-buffer
7787 for another frame that we need to delete. */
7788 FOR_EACH_FRAME (tail, frame)
7789 if (FRAME_X_P (XFRAME (frame))
7790 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7792 /* Set this to t so that delete_frame won't get confused
7793 trying to find a replacement. */
7794 KVAR (FRAME_KBOARD (XFRAME (frame)), Vdefault_minibuffer_frame) = Qt;
7795 delete_frame (frame, Qnoelisp);
7798 /* If DPYINFO is null, this means we didn't open the display in the
7799 first place, so don't try to close it. */
7800 if (dpyinfo)
7802 /* We can not call XtCloseDisplay here because it calls XSync.
7803 XSync inside the error handler apparently hangs Emacs. On
7804 current Xt versions, this isn't needed either. */
7805 #ifdef USE_GTK
7806 /* A long-standing GTK bug prevents proper disconnect handling
7807 (https://bugzilla.gnome.org/show_bug.cgi?id=85715). Once,
7808 the resulting Glib error message loop filled a user's disk.
7809 To avoid this, kill Emacs unconditionally on disconnect. */
7810 shut_down_emacs (0, 0, Qnil);
7811 fprintf (stderr, "%s\n\
7812 When compiled with GTK, Emacs cannot recover from X disconnects.\n\
7813 This is a GTK bug: https://bugzilla.gnome.org/show_bug.cgi?id=85715\n\
7814 For details, see etc/PROBLEMS.\n",
7815 error_msg);
7816 abort ();
7817 #endif /* USE_GTK */
7819 /* Indicate that this display is dead. */
7820 dpyinfo->display = 0;
7822 dpyinfo->reference_count--;
7823 dpyinfo->terminal->reference_count--;
7824 if (dpyinfo->reference_count != 0)
7825 /* We have just closed all frames on this display. */
7826 abort ();
7829 Lisp_Object tmp;
7830 XSETTERMINAL (tmp, dpyinfo->terminal);
7831 Fdelete_terminal (tmp, Qnoelisp);
7835 if (terminal_list == 0)
7837 fprintf (stderr, "%s\n", error_msg);
7838 Fkill_emacs (make_number (70));
7839 /* NOTREACHED */
7842 /* Ordinary stack unwind doesn't deal with these. */
7843 #ifdef SIGIO
7844 sigunblock (sigmask (SIGIO));
7845 #endif
7846 sigunblock (sigmask (SIGALRM));
7847 TOTALLY_UNBLOCK_INPUT;
7849 unbind_to (idx, Qnil);
7850 clear_waiting_for_input ();
7852 /* Tell GCC not to suggest attribute 'noreturn' for this function. */
7853 IF_LINT (if (! terminal_list) return; )
7855 /* Here, we absolutely have to use a non-local exit (e.g. signal, throw,
7856 longjmp), because returning from this function would get us back into
7857 Xlib's code which will directly call `exit'. */
7858 error ("%s", error_msg);
7861 /* We specifically use it before defining it, so that gcc doesn't inline it,
7862 otherwise gdb doesn't know how to properly put a breakpoint on it. */
7863 static void x_error_quitter (Display *, XErrorEvent *);
7865 /* This is the first-level handler for X protocol errors.
7866 It calls x_error_quitter or x_error_catcher. */
7868 static int
7869 x_error_handler (Display *display, XErrorEvent *event)
7871 if (x_error_message)
7872 x_error_catcher (display, event);
7873 else
7874 x_error_quitter (display, event);
7875 return 0;
7878 /* This is the usual handler for X protocol errors.
7879 It kills all frames on the display that we got the error for.
7880 If that was the only one, it prints an error message and kills Emacs. */
7882 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
7884 /* On older GCC versions, just putting x_error_quitter
7885 after x_error_handler prevents inlining into the former. */
7887 static void NO_INLINE
7888 x_error_quitter (Display *display, XErrorEvent *event)
7890 char buf[256], buf1[356];
7892 /* Ignore BadName errors. They can happen because of fonts
7893 or colors that are not defined. */
7895 if (event->error_code == BadName)
7896 return;
7898 /* Note that there is no real way portable across R3/R4 to get the
7899 original error handler. */
7901 XGetErrorText (display, event->error_code, buf, sizeof (buf));
7902 sprintf (buf1, "X protocol error: %s on protocol request %d",
7903 buf, event->request_code);
7904 x_connection_closed (display, buf1);
7908 /* This is the handler for X IO errors, always.
7909 It kills all frames on the display that we lost touch with.
7910 If that was the only one, it prints an error message and kills Emacs. */
7912 static int
7913 x_io_error_quitter (Display *display)
7915 char buf[256];
7917 snprintf (buf, sizeof buf, "Connection lost to X server `%s'",
7918 DisplayString (display));
7919 x_connection_closed (display, buf);
7920 return 0;
7923 /* Changing the font of the frame. */
7925 /* Give frame F the font FONT-OBJECT as its default font. The return
7926 value is FONT-OBJECT. FONTSET is an ID of the fontset for the
7927 frame. If it is negative, generate a new fontset from
7928 FONT-OBJECT. */
7930 Lisp_Object
7931 x_new_font (struct frame *f, Lisp_Object font_object, int fontset)
7933 struct font *font = XFONT_OBJECT (font_object);
7935 if (fontset < 0)
7936 fontset = fontset_from_font (font_object);
7937 FRAME_FONTSET (f) = fontset;
7938 if (FRAME_FONT (f) == font)
7939 /* This font is already set in frame F. There's nothing more to
7940 do. */
7941 return font_object;
7943 FRAME_FONT (f) = font;
7944 FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
7945 FRAME_COLUMN_WIDTH (f) = font->average_width;
7946 FRAME_SPACE_WIDTH (f) = font->space_width;
7947 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (font);
7949 compute_fringe_widths (f, 1);
7951 /* Compute the scroll bar width in character columns. */
7952 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
7954 int wid = FRAME_COLUMN_WIDTH (f);
7955 FRAME_CONFIG_SCROLL_BAR_COLS (f)
7956 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
7958 else
7960 int wid = FRAME_COLUMN_WIDTH (f);
7961 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
7964 if (FRAME_X_WINDOW (f) != 0)
7966 /* Don't change the size of a tip frame; there's no point in
7967 doing it because it's done in Fx_show_tip, and it leads to
7968 problems because the tip frame has no widget. */
7969 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
7970 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
7973 #ifdef HAVE_X_I18N
7974 if (FRAME_XIC (f)
7975 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
7977 BLOCK_INPUT;
7978 xic_set_xfontset (f, SSDATA (fontset_ascii (fontset)));
7979 UNBLOCK_INPUT;
7981 #endif
7983 return font_object;
7987 /***********************************************************************
7988 X Input Methods
7989 ***********************************************************************/
7991 #ifdef HAVE_X_I18N
7993 #ifdef HAVE_X11R6
7995 /* XIM destroy callback function, which is called whenever the
7996 connection to input method XIM dies. CLIENT_DATA contains a
7997 pointer to the x_display_info structure corresponding to XIM. */
7999 static void
8000 xim_destroy_callback (XIM xim, XPointer client_data, XPointer call_data)
8002 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8003 Lisp_Object frame, tail;
8005 BLOCK_INPUT;
8007 /* No need to call XDestroyIC.. */
8008 FOR_EACH_FRAME (tail, frame)
8010 struct frame *f = XFRAME (frame);
8011 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8013 FRAME_XIC (f) = NULL;
8014 xic_free_xfontset (f);
8018 /* No need to call XCloseIM. */
8019 dpyinfo->xim = NULL;
8020 XFree (dpyinfo->xim_styles);
8021 UNBLOCK_INPUT;
8024 #endif /* HAVE_X11R6 */
8026 #ifdef HAVE_X11R6
8027 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8028 extern char *XSetIMValues (XIM, ...);
8029 #endif
8031 /* Open the connection to the XIM server on display DPYINFO.
8032 RESOURCE_NAME is the resource name Emacs uses. */
8034 static void
8035 xim_open_dpy (struct x_display_info *dpyinfo, char *resource_name)
8037 XIM xim;
8039 #ifdef HAVE_XIM
8040 if (use_xim)
8042 if (dpyinfo->xim)
8043 XCloseIM (dpyinfo->xim);
8044 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8045 emacs_class);
8046 dpyinfo->xim = xim;
8048 if (xim)
8050 #ifdef HAVE_X11R6
8051 XIMCallback destroy;
8052 #endif
8054 /* Get supported styles and XIM values. */
8055 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8057 #ifdef HAVE_X11R6
8058 destroy.callback = xim_destroy_callback;
8059 destroy.client_data = (XPointer)dpyinfo;
8060 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8061 #endif
8065 else
8066 #endif /* HAVE_XIM */
8067 dpyinfo->xim = NULL;
8071 #ifdef HAVE_X11R6_XIM
8073 /* XIM instantiate callback function, which is called whenever an XIM
8074 server is available. DISPLAY is the display of the XIM.
8075 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8076 when the callback was registered. */
8078 static void
8079 xim_instantiate_callback (Display *display, XPointer client_data, XPointer call_data)
8081 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8082 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8084 /* We don't support multiple XIM connections. */
8085 if (dpyinfo->xim)
8086 return;
8088 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8090 /* Create XIC for the existing frames on the same display, as long
8091 as they have no XIC. */
8092 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8094 Lisp_Object tail, frame;
8096 BLOCK_INPUT;
8097 FOR_EACH_FRAME (tail, frame)
8099 struct frame *f = XFRAME (frame);
8101 if (FRAME_X_P (f)
8102 && FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8103 if (FRAME_XIC (f) == NULL)
8105 create_frame_xic (f);
8106 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8107 xic_set_statusarea (f);
8108 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8110 struct window *w = XWINDOW (f->selected_window);
8111 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8116 UNBLOCK_INPUT;
8120 #endif /* HAVE_X11R6_XIM */
8123 /* Open a connection to the XIM server on display DPYINFO.
8124 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8125 connection only at the first time. On X11R6, open the connection
8126 in the XIM instantiate callback function. */
8128 static void
8129 xim_initialize (struct x_display_info *dpyinfo, char *resource_name)
8131 dpyinfo->xim = NULL;
8132 #ifdef HAVE_XIM
8133 if (use_xim)
8135 #ifdef HAVE_X11R6_XIM
8136 struct xim_inst_t *xim_inst;
8137 ptrdiff_t len;
8139 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8140 dpyinfo->xim_callback_data = xim_inst;
8141 xim_inst->dpyinfo = dpyinfo;
8142 len = strlen (resource_name);
8143 xim_inst->resource_name = (char *) xmalloc (len + 1);
8144 memcpy (xim_inst->resource_name, resource_name, len + 1);
8145 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8146 resource_name, emacs_class,
8147 xim_instantiate_callback,
8148 /* This is XPointer in XFree86
8149 but (XPointer *) on Tru64, at
8150 least, hence the configure test. */
8151 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8152 #else /* not HAVE_X11R6_XIM */
8153 xim_open_dpy (dpyinfo, resource_name);
8154 #endif /* not HAVE_X11R6_XIM */
8156 #endif /* HAVE_XIM */
8160 /* Close the connection to the XIM server on display DPYINFO. */
8162 static void
8163 xim_close_dpy (struct x_display_info *dpyinfo)
8165 #ifdef HAVE_XIM
8166 if (use_xim)
8168 #ifdef HAVE_X11R6_XIM
8169 if (dpyinfo->display)
8170 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8171 NULL, emacs_class,
8172 xim_instantiate_callback, NULL);
8173 xfree (dpyinfo->xim_callback_data->resource_name);
8174 xfree (dpyinfo->xim_callback_data);
8175 #endif /* HAVE_X11R6_XIM */
8176 if (dpyinfo->display)
8177 XCloseIM (dpyinfo->xim);
8178 dpyinfo->xim = NULL;
8179 XFree (dpyinfo->xim_styles);
8181 #endif /* HAVE_XIM */
8184 #endif /* not HAVE_X11R6_XIM */
8188 /* Calculate the absolute position in frame F
8189 from its current recorded position values and gravity. */
8191 static void
8192 x_calc_absolute_position (struct frame *f)
8194 int flags = f->size_hint_flags;
8196 /* We have nothing to do if the current position
8197 is already for the top-left corner. */
8198 if (! ((flags & XNegative) || (flags & YNegative)))
8199 return;
8201 /* Treat negative positions as relative to the leftmost bottommost
8202 position that fits on the screen. */
8203 if (flags & XNegative)
8204 f->left_pos = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
8205 - FRAME_PIXEL_WIDTH (f) + f->left_pos;
8208 int height = FRAME_PIXEL_HEIGHT (f);
8210 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8211 /* Something is fishy here. When using Motif, starting Emacs with
8212 `-g -0-0', the frame appears too low by a few pixels.
8214 This seems to be so because initially, while Emacs is starting,
8215 the column widget's height and the frame's pixel height are
8216 different. The column widget's height is the right one. In
8217 later invocations, when Emacs is up, the frame's pixel height
8218 is right, though.
8220 It's not obvious where the initial small difference comes from.
8221 2000-12-01, gerd. */
8223 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8224 #endif
8226 if (flags & YNegative)
8227 f->top_pos = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
8228 - height + f->top_pos;
8231 /* The left_pos and top_pos
8232 are now relative to the top and left screen edges,
8233 so the flags should correspond. */
8234 f->size_hint_flags &= ~ (XNegative | YNegative);
8237 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8238 to really change the position, and 0 when calling from
8239 x_make_frame_visible (in that case, XOFF and YOFF are the current
8240 position values). It is -1 when calling from x_set_frame_parameters,
8241 which means, do adjust for borders but don't change the gravity. */
8243 void
8244 x_set_offset (struct frame *f, register int xoff, register int yoff, int change_gravity)
8246 int modified_top, modified_left;
8248 if (change_gravity > 0)
8250 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8251 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8253 f->top_pos = yoff;
8254 f->left_pos = xoff;
8255 f->size_hint_flags &= ~ (XNegative | YNegative);
8256 if (xoff < 0)
8257 f->size_hint_flags |= XNegative;
8258 if (yoff < 0)
8259 f->size_hint_flags |= YNegative;
8260 f->win_gravity = NorthWestGravity;
8262 x_calc_absolute_position (f);
8264 BLOCK_INPUT;
8265 x_wm_set_size_hint (f, (long) 0, 0);
8267 modified_left = f->left_pos;
8268 modified_top = f->top_pos;
8270 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8272 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8273 than the WM decorations. So we use the calculated offset instead
8274 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8275 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8276 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8279 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8280 modified_left, modified_top);
8282 x_sync_with_move (f, f->left_pos, f->top_pos,
8283 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8284 ? 1 : 0);
8286 /* change_gravity is non-zero when this function is called from Lisp to
8287 programmatically move a frame. In that case, we call
8288 x_check_expected_move to discover if we have a "Type A" or "Type B"
8289 window manager, and, for a "Type A" window manager, adjust the position
8290 of the frame.
8292 We call x_check_expected_move if a programmatic move occurred, and
8293 either the window manager type (A/B) is unknown or it is Type A but we
8294 need to compute the top/left offset adjustment for this frame. */
8296 if (change_gravity != 0 &&
8297 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8298 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8299 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8300 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8301 x_check_expected_move (f, modified_left, modified_top);
8303 UNBLOCK_INPUT;
8306 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8307 on the root window for frame F contains ATOMNAME.
8308 This is how a WM check shall be done according to the Window Manager
8309 Specification/Extended Window Manager Hints at
8310 http://freedesktop.org/wiki/Specifications/wm-spec. */
8312 static int
8313 wm_supports (struct frame *f, Atom want_atom)
8315 Atom actual_type;
8316 unsigned long actual_size, bytes_remaining;
8317 int i, rc, actual_format;
8318 Window wmcheck_window;
8319 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8320 Window target_window = dpyinfo->root_window;
8321 long max_len = 65536;
8322 Display *dpy = FRAME_X_DISPLAY (f);
8323 unsigned char *tmp_data = NULL;
8324 Atom target_type = XA_WINDOW;
8326 BLOCK_INPUT;
8328 x_catch_errors (dpy);
8329 rc = XGetWindowProperty (dpy, target_window,
8330 dpyinfo->Xatom_net_supporting_wm_check,
8331 0, max_len, False, target_type,
8332 &actual_type, &actual_format, &actual_size,
8333 &bytes_remaining, &tmp_data);
8335 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8337 if (tmp_data) XFree (tmp_data);
8338 x_uncatch_errors ();
8339 UNBLOCK_INPUT;
8340 return 0;
8343 wmcheck_window = *(Window *) tmp_data;
8344 XFree (tmp_data);
8346 /* Check if window exists. */
8347 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8348 x_sync (f);
8349 if (x_had_errors_p (dpy))
8351 x_uncatch_errors ();
8352 UNBLOCK_INPUT;
8353 return 0;
8356 if (dpyinfo->net_supported_window != wmcheck_window)
8358 /* Window changed, reload atoms */
8359 if (dpyinfo->net_supported_atoms != NULL)
8360 XFree (dpyinfo->net_supported_atoms);
8361 dpyinfo->net_supported_atoms = NULL;
8362 dpyinfo->nr_net_supported_atoms = 0;
8363 dpyinfo->net_supported_window = 0;
8365 target_type = XA_ATOM;
8366 tmp_data = NULL;
8367 rc = XGetWindowProperty (dpy, target_window,
8368 dpyinfo->Xatom_net_supported,
8369 0, max_len, False, target_type,
8370 &actual_type, &actual_format, &actual_size,
8371 &bytes_remaining, &tmp_data);
8373 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8375 if (tmp_data) XFree (tmp_data);
8376 x_uncatch_errors ();
8377 UNBLOCK_INPUT;
8378 return 0;
8381 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8382 dpyinfo->nr_net_supported_atoms = actual_size;
8383 dpyinfo->net_supported_window = wmcheck_window;
8386 rc = 0;
8388 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8389 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8391 x_uncatch_errors ();
8392 UNBLOCK_INPUT;
8394 return rc;
8397 static void
8398 set_wm_state (Lisp_Object frame, int add, Atom atom, Atom value)
8400 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (frame));
8402 x_send_client_event (frame, make_number (0), frame,
8403 dpyinfo->Xatom_net_wm_state,
8404 make_number (32),
8405 /* 1 = add, 0 = remove */
8406 Fcons
8407 (make_number (add ? 1 : 0),
8408 Fcons
8409 (make_fixnum_or_float (atom),
8410 value != 0
8411 ? Fcons (make_fixnum_or_float (value), Qnil)
8412 : Qnil)));
8415 void
8416 x_set_sticky (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
8418 Lisp_Object frame;
8419 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8421 XSETFRAME (frame, f);
8423 set_wm_state (frame, NILP (new_value) ? 0 : 1,
8424 dpyinfo->Xatom_net_wm_state_sticky, None);
8427 /* Return the current _NET_WM_STATE.
8428 SIZE_STATE is set to one of the FULLSCREEN_* values.
8429 STICKY is set to 1 if the sticky state is set, 0 if not. */
8431 static void
8432 get_current_wm_state (struct frame *f,
8433 Window window,
8434 int *size_state,
8435 int *sticky)
8437 Atom actual_type;
8438 unsigned long actual_size, bytes_remaining;
8439 int i, rc, actual_format;
8440 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8441 long max_len = 65536;
8442 Display *dpy = FRAME_X_DISPLAY (f);
8443 unsigned char *tmp_data = NULL;
8444 Atom target_type = XA_ATOM;
8446 *sticky = 0;
8447 *size_state = FULLSCREEN_NONE;
8449 BLOCK_INPUT;
8450 x_catch_errors (dpy);
8451 rc = XGetWindowProperty (dpy, window, dpyinfo->Xatom_net_wm_state,
8452 0, max_len, False, target_type,
8453 &actual_type, &actual_format, &actual_size,
8454 &bytes_remaining, &tmp_data);
8456 if (rc != Success || actual_type != target_type || x_had_errors_p (dpy))
8458 if (tmp_data) XFree (tmp_data);
8459 x_uncatch_errors ();
8460 UNBLOCK_INPUT;
8461 return;
8464 x_uncatch_errors ();
8466 for (i = 0; i < actual_size; ++i)
8468 Atom a = ((Atom*)tmp_data)[i];
8469 if (a == dpyinfo->Xatom_net_wm_state_maximized_horz)
8471 if (*size_state == FULLSCREEN_HEIGHT)
8472 *size_state = FULLSCREEN_MAXIMIZED;
8473 else
8474 *size_state = FULLSCREEN_WIDTH;
8476 else if (a == dpyinfo->Xatom_net_wm_state_maximized_vert)
8478 if (*size_state == FULLSCREEN_WIDTH)
8479 *size_state = FULLSCREEN_MAXIMIZED;
8480 else
8481 *size_state = FULLSCREEN_HEIGHT;
8483 else if (a == dpyinfo->Xatom_net_wm_state_fullscreen)
8484 *size_state = FULLSCREEN_BOTH;
8485 else if (a == dpyinfo->Xatom_net_wm_state_sticky)
8486 *sticky = 1;
8489 if (tmp_data) XFree (tmp_data);
8490 UNBLOCK_INPUT;
8493 /* Do fullscreen as specified in extended window manager hints */
8495 static int
8496 do_ewmh_fullscreen (struct frame *f)
8498 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8499 int have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state);
8500 int cur, dummy;
8502 get_current_wm_state (f, FRAME_OUTER_WINDOW (f), &cur, &dummy);
8504 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8505 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8506 if (!have_net_atom)
8507 have_net_atom = wm_supports (f, dpyinfo->Xatom_net_wm_state_fullscreen);
8509 if (have_net_atom && cur != f->want_fullscreen)
8511 Lisp_Object frame;
8513 XSETFRAME (frame, f);
8515 /* Keep number of calls to set_wm_state as low as possible.
8516 Some window managers, or possible Gtk+, hangs when too many
8517 are sent at once. */
8518 switch (f->want_fullscreen)
8520 case FULLSCREEN_BOTH:
8521 if (cur == FULLSCREEN_WIDTH || cur == FULLSCREEN_MAXIMIZED
8522 || cur == FULLSCREEN_HEIGHT)
8523 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_maximized_horz,
8524 dpyinfo->Xatom_net_wm_state_maximized_vert);
8525 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8526 break;
8527 case FULLSCREEN_WIDTH:
8528 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_HEIGHT
8529 || cur == FULLSCREEN_MAXIMIZED)
8530 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen,
8531 dpyinfo->Xatom_net_wm_state_maximized_vert);
8532 if (cur != FULLSCREEN_MAXIMIZED)
8533 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_horz, None);
8534 break;
8535 case FULLSCREEN_HEIGHT:
8536 if (cur == FULLSCREEN_BOTH || cur == FULLSCREEN_WIDTH
8537 || cur == FULLSCREEN_MAXIMIZED)
8538 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen,
8539 dpyinfo->Xatom_net_wm_state_maximized_horz);
8540 if (cur != FULLSCREEN_MAXIMIZED)
8541 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_vert, None);
8542 break;
8543 case FULLSCREEN_MAXIMIZED:
8544 if (cur == FULLSCREEN_BOTH)
8545 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8546 set_wm_state (frame, 1, dpyinfo->Xatom_net_wm_state_maximized_horz,
8547 dpyinfo->Xatom_net_wm_state_maximized_vert);
8548 break;
8549 case FULLSCREEN_NONE:
8550 if (cur == FULLSCREEN_BOTH)
8551 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_fullscreen, None);
8552 else
8553 set_wm_state (frame, 0, dpyinfo->Xatom_net_wm_state_maximized_horz,
8554 dpyinfo->Xatom_net_wm_state_maximized_vert);
8557 f->want_fullscreen = FULLSCREEN_NONE;
8561 return have_net_atom;
8564 static void
8565 XTfullscreen_hook (FRAME_PTR f)
8567 if (f->async_visible)
8569 BLOCK_INPUT;
8570 x_check_fullscreen (f);
8571 x_sync (f);
8572 UNBLOCK_INPUT;
8577 static void
8578 x_handle_net_wm_state (struct frame *f, XPropertyEvent *event)
8580 int value = FULLSCREEN_NONE;
8581 Lisp_Object lval;
8582 int sticky = 0;
8584 get_current_wm_state (f, event->window, &value, &sticky);
8585 lval = Qnil;
8586 switch (value)
8588 case FULLSCREEN_WIDTH:
8589 lval = Qfullwidth;
8590 break;
8591 case FULLSCREEN_HEIGHT:
8592 lval = Qfullheight;
8593 break;
8594 case FULLSCREEN_BOTH:
8595 lval = Qfullboth;
8596 break;
8597 case FULLSCREEN_MAXIMIZED:
8598 lval = Qmaximized;
8599 break;
8602 store_frame_param (f, Qfullscreen, lval);
8603 store_frame_param (f, Qsticky, sticky ? Qt : Qnil);
8606 /* Check if we need to resize the frame due to a fullscreen request.
8607 If so needed, resize the frame. */
8608 static void
8609 x_check_fullscreen (struct frame *f)
8611 if (do_ewmh_fullscreen (f))
8612 return;
8614 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
8615 return; /* Only fullscreen without WM or with EWM hints (above). */
8617 /* Setting fullscreen to nil doesn't do anything. We could save the
8618 last non-fullscreen size and restore it, but it seems like a
8619 lot of work for this unusual case (no window manager running). */
8621 if (f->want_fullscreen != FULLSCREEN_NONE)
8623 int width = FRAME_PIXEL_WIDTH (f), height = FRAME_PIXEL_HEIGHT (f);
8624 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8626 switch (f->want_fullscreen)
8628 /* No difference between these two when there is no WM */
8629 case FULLSCREEN_BOTH:
8630 case FULLSCREEN_MAXIMIZED:
8631 width = x_display_pixel_width (dpyinfo);
8632 height = x_display_pixel_height (dpyinfo);
8633 break;
8634 case FULLSCREEN_WIDTH:
8635 width = x_display_pixel_width (dpyinfo);
8636 break;
8637 case FULLSCREEN_HEIGHT:
8638 height = x_display_pixel_height (dpyinfo);
8641 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8642 width, height);
8646 /* This function is called by x_set_offset to determine whether the window
8647 manager interfered with the positioning of the frame. Type A window
8648 managers position the surrounding window manager decorations a small
8649 amount above and left of the user-supplied position. Type B window
8650 managers position the surrounding window manager decorations at the
8651 user-specified position. If we detect a Type A window manager, we
8652 compensate by moving the window right and down by the proper amount. */
8654 static void
8655 x_check_expected_move (struct frame *f, int expected_left, int expected_top)
8657 int current_left = 0, current_top = 0;
8659 /* x_real_positions returns the left and top offsets of the outermost
8660 window manager window around the frame. */
8662 x_real_positions (f, &current_left, &current_top);
8664 if (current_left != expected_left || current_top != expected_top)
8666 /* It's a "Type A" window manager. */
8668 int adjusted_left;
8669 int adjusted_top;
8671 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8672 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8673 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8675 /* Now fix the mispositioned frame's location. */
8677 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8678 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8680 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8681 adjusted_left, adjusted_top);
8683 x_sync_with_move (f, expected_left, expected_top, 0);
8685 else
8686 /* It's a "Type B" window manager. We don't have to adjust the
8687 frame's position. */
8689 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8693 /* Wait for XGetGeometry to return up-to-date position information for a
8694 recently-moved frame. Call this immediately after calling XMoveWindow.
8695 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8696 frame has been moved to, so we use a fuzzy position comparison instead
8697 of an exact comparison. */
8699 static void
8700 x_sync_with_move (struct frame *f, int left, int top, int fuzzy)
8702 int count = 0;
8704 while (count++ < 50)
8706 int current_left = 0, current_top = 0;
8708 /* In theory, this call to XSync only needs to happen once, but in
8709 practice, it doesn't seem to work, hence the need for the surrounding
8710 loop. */
8712 XSync (FRAME_X_DISPLAY (f), False);
8713 x_real_positions (f, &current_left, &current_top);
8715 if (fuzzy)
8717 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8718 pixels. */
8720 if (eabs (current_left - left) <= 10
8721 && eabs (current_top - top) <= 40)
8722 return;
8724 else if (current_left == left && current_top == top)
8725 return;
8728 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8729 will then return up-to-date position info. */
8731 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8735 /* Wait for an event on frame F matching EVENTTYPE. */
8736 void
8737 x_wait_for_event (struct frame *f, int eventtype)
8739 int level = interrupt_input_blocked;
8741 SELECT_TYPE fds;
8742 EMACS_TIME tmo, tmo_at, time_now;
8743 int fd = ConnectionNumber (FRAME_X_DISPLAY (f));
8745 pending_event_wait.f = f;
8746 pending_event_wait.eventtype = eventtype;
8748 /* Set timeout to 0.1 second. Hopefully not noticable.
8749 Maybe it should be configurable. */
8750 EMACS_SET_SECS_USECS (tmo, 0, 100000);
8751 EMACS_GET_TIME (tmo_at);
8752 EMACS_ADD_TIME (tmo_at, tmo_at, tmo);
8754 while (pending_event_wait.eventtype)
8756 interrupt_input_pending = 1;
8757 TOTALLY_UNBLOCK_INPUT;
8758 /* XTread_socket is called after unblock. */
8759 BLOCK_INPUT;
8760 interrupt_input_blocked = level;
8762 FD_ZERO (&fds);
8763 FD_SET (fd, &fds);
8765 EMACS_GET_TIME (time_now);
8766 EMACS_SUB_TIME (tmo, tmo_at, time_now);
8768 if (EMACS_TIME_NEG_P (tmo) || select (fd+1, &fds, NULL, NULL, &tmo) == 0)
8769 break; /* Timeout */
8771 pending_event_wait.f = 0;
8772 pending_event_wait.eventtype = 0;
8776 /* Change the size of frame F's X window to COLS/ROWS in the case F
8777 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8778 top-left-corner window gravity for this size change and subsequent
8779 size changes. Otherwise we leave the window gravity unchanged. */
8781 static void
8782 x_set_window_size_1 (struct frame *f, int change_gravity, int cols, int rows)
8784 int pixelwidth, pixelheight;
8786 check_frame_size (f, &rows, &cols);
8787 f->scroll_bar_actual_width
8788 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8790 : FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f));
8792 compute_fringe_widths (f, 0);
8794 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols)
8795 + FRAME_TOOLBAR_WIDTH (f);
8796 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows)
8797 + FRAME_MENUBAR_HEIGHT (f) + FRAME_TOOLBAR_HEIGHT (f);
8799 if (change_gravity) f->win_gravity = NorthWestGravity;
8800 x_wm_set_size_hint (f, (long) 0, 0);
8801 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8802 pixelwidth, pixelheight);
8805 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8806 receive in the ConfigureNotify event; if we get what we asked
8807 for, then the event won't cause the screen to become garbaged, so
8808 we have to make sure to do it here. */
8809 SET_FRAME_GARBAGED (f);
8811 /* Now, strictly speaking, we can't be sure that this is accurate,
8812 but the window manager will get around to dealing with the size
8813 change request eventually, and we'll hear how it went when the
8814 ConfigureNotify event gets here.
8816 We could just not bother storing any of this information here,
8817 and let the ConfigureNotify event set everything up, but that
8818 might be kind of confusing to the Lisp code, since size changes
8819 wouldn't be reported in the frame parameters until some random
8820 point in the future when the ConfigureNotify event arrives.
8822 We pass 1 for DELAY since we can't run Lisp code inside of
8823 a BLOCK_INPUT. */
8825 /* But the ConfigureNotify may in fact never arrive, and then this is
8826 not right if the frame is visible. Instead wait (with timeout)
8827 for the ConfigureNotify. */
8828 if (f->async_visible)
8829 x_wait_for_event (f, ConfigureNotify);
8830 else
8832 change_frame_size (f, rows, cols, 0, 1, 0);
8833 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8834 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8835 x_sync (f);
8840 /* Call this to change the size of frame F's x-window.
8841 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8842 for this size change and subsequent size changes.
8843 Otherwise we leave the window gravity unchanged. */
8845 void
8846 x_set_window_size (struct frame *f, int change_gravity, int cols, int rows)
8848 BLOCK_INPUT;
8850 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8852 int r, c;
8854 /* When the frame is maximized/fullscreen or running under for
8855 example Xmonad, x_set_window_size_1 will be a no-op.
8856 In that case, the right thing to do is extend rows/cols to
8857 the current frame size. We do that first if x_set_window_size_1
8858 turns out to not be a no-op (there is no way to know).
8859 The size will be adjusted again if the frame gets a
8860 ConfigureNotify event as a result of x_set_window_size. */
8861 int pixelh = FRAME_PIXEL_HEIGHT (f);
8862 #ifdef USE_X_TOOLKIT
8863 /* The menu bar is not part of text lines. The tool bar
8864 is however. */
8865 pixelh -= FRAME_MENUBAR_HEIGHT (f);
8866 #endif
8867 r = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, pixelh);
8868 /* Update f->scroll_bar_actual_width because it is used in
8869 FRAME_PIXEL_WIDTH_TO_TEXT_COLS. */
8870 f->scroll_bar_actual_width
8871 = FRAME_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f);
8872 c = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, FRAME_PIXEL_WIDTH (f));
8873 change_frame_size (f, r, c, 0, 1, 0);
8876 #ifdef USE_GTK
8877 if (FRAME_GTK_WIDGET (f))
8878 xg_frame_set_char_size (f, cols, rows);
8879 else
8880 x_set_window_size_1 (f, change_gravity, cols, rows);
8881 #else /* not USE_GTK */
8883 x_set_window_size_1 (f, change_gravity, cols, rows);
8885 #endif /* not USE_GTK */
8887 /* If cursor was outside the new size, mark it as off. */
8888 mark_window_cursors_off (XWINDOW (f->root_window));
8890 /* Clear out any recollection of where the mouse highlighting was,
8891 since it might be in a place that's outside the new frame size.
8892 Actually checking whether it is outside is a pain in the neck,
8893 so don't try--just let the highlighting be done afresh with new size. */
8894 cancel_mouse_face (f);
8896 UNBLOCK_INPUT;
8899 /* Mouse warping. */
8901 void
8902 x_set_mouse_position (struct frame *f, int x, int y)
8904 int pix_x, pix_y;
8906 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
8907 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
8909 if (pix_x < 0) pix_x = 0;
8910 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
8912 if (pix_y < 0) pix_y = 0;
8913 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
8915 BLOCK_INPUT;
8917 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8918 0, 0, 0, 0, pix_x, pix_y);
8919 UNBLOCK_INPUT;
8922 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
8924 void
8925 x_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
8927 BLOCK_INPUT;
8929 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
8930 0, 0, 0, 0, pix_x, pix_y);
8931 UNBLOCK_INPUT;
8934 /* Raise frame F. */
8936 void
8937 x_raise_frame (struct frame *f)
8939 BLOCK_INPUT;
8940 if (f->async_visible)
8941 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8943 XFlush (FRAME_X_DISPLAY (f));
8944 UNBLOCK_INPUT;
8947 /* Lower frame F. */
8949 static void
8950 x_lower_frame (struct frame *f)
8952 if (f->async_visible)
8954 BLOCK_INPUT;
8955 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
8956 XFlush (FRAME_X_DISPLAY (f));
8957 UNBLOCK_INPUT;
8961 /* Activate frame with Extended Window Manager Hints */
8963 void
8964 x_ewmh_activate_frame (FRAME_PTR f)
8966 /* See Window Manager Specification/Extended Window Manager Hints at
8967 http://freedesktop.org/wiki/Specifications/wm-spec */
8969 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8970 if (f->async_visible && wm_supports (f, dpyinfo->Xatom_net_active_window))
8972 Lisp_Object frame;
8973 XSETFRAME (frame, f);
8974 x_send_client_event (frame, make_number (0), frame,
8975 dpyinfo->Xatom_net_active_window,
8976 make_number (32),
8977 Fcons (make_number (1),
8978 Fcons (make_number (last_user_time),
8979 Qnil)));
8983 static void
8984 XTframe_raise_lower (FRAME_PTR f, int raise_flag)
8986 if (raise_flag)
8987 x_raise_frame (f);
8988 else
8989 x_lower_frame (f);
8992 /* XEmbed implementation. */
8994 #if defined USE_X_TOOLKIT || ! defined USE_GTK
8996 /* XEmbed implementation. */
8998 #define XEMBED_VERSION 0
9000 static void
9001 xembed_set_info (struct frame *f, enum xembed_info flags)
9003 unsigned long data[2];
9004 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9006 data[0] = XEMBED_VERSION;
9007 data[1] = flags;
9009 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9010 dpyinfo->Xatom_XEMBED_INFO, dpyinfo->Xatom_XEMBED_INFO,
9011 32, PropModeReplace, (unsigned char *) data, 2);
9013 #endif /* defined USE_X_TOOLKIT || ! defined USE_GTK */
9015 static void
9016 xembed_send_message (struct frame *f, Time t, enum xembed_message msg,
9017 long int detail, long int data1, long int data2)
9019 XEvent event;
9021 event.xclient.type = ClientMessage;
9022 event.xclient.window = FRAME_X_OUTPUT (f)->parent_desc;
9023 event.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_XEMBED;
9024 event.xclient.format = 32;
9025 event.xclient.data.l[0] = t;
9026 event.xclient.data.l[1] = msg;
9027 event.xclient.data.l[2] = detail;
9028 event.xclient.data.l[3] = data1;
9029 event.xclient.data.l[4] = data2;
9031 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_OUTPUT (f)->parent_desc,
9032 False, NoEventMask, &event);
9033 XSync (FRAME_X_DISPLAY (f), False);
9036 /* Change of visibility. */
9038 /* This tries to wait until the frame is really visible.
9039 However, if the window manager asks the user where to position
9040 the frame, this will return before the user finishes doing that.
9041 The frame will not actually be visible at that time,
9042 but it will become visible later when the window manager
9043 finishes with it. */
9045 void
9046 x_make_frame_visible (struct frame *f)
9048 Lisp_Object type;
9049 int original_top, original_left;
9050 int retry_count = 2;
9052 retry:
9054 BLOCK_INPUT;
9056 type = x_icon_type (f);
9057 if (!NILP (type))
9058 x_bitmap_icon (f, type);
9060 if (! FRAME_VISIBLE_P (f))
9062 /* We test FRAME_GARBAGED_P here to make sure we don't
9063 call x_set_offset a second time
9064 if we get to x_make_frame_visible a second time
9065 before the window gets really visible. */
9066 if (! FRAME_ICONIFIED_P (f)
9067 && ! FRAME_X_EMBEDDED_P (f)
9068 && ! f->output_data.x->asked_for_visible)
9069 x_set_offset (f, f->left_pos, f->top_pos, 0);
9071 f->output_data.x->asked_for_visible = 1;
9073 if (! EQ (Vx_no_window_manager, Qt))
9074 x_wm_set_window_state (f, NormalState);
9075 #ifdef USE_X_TOOLKIT
9076 if (FRAME_X_EMBEDDED_P (f))
9077 xembed_set_info (f, XEMBED_MAPPED);
9078 else
9080 /* This was XtPopup, but that did nothing for an iconified frame. */
9081 XtMapWidget (f->output_data.x->widget);
9083 #else /* not USE_X_TOOLKIT */
9084 #ifdef USE_GTK
9085 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9086 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9087 #else
9088 if (FRAME_X_EMBEDDED_P (f))
9089 xembed_set_info (f, XEMBED_MAPPED);
9090 else
9091 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9092 #endif /* not USE_GTK */
9093 #endif /* not USE_X_TOOLKIT */
9096 XFlush (FRAME_X_DISPLAY (f));
9098 /* Synchronize to ensure Emacs knows the frame is visible
9099 before we do anything else. We do this loop with input not blocked
9100 so that incoming events are handled. */
9102 Lisp_Object frame;
9103 int count;
9104 /* This must be before UNBLOCK_INPUT
9105 since events that arrive in response to the actions above
9106 will set it when they are handled. */
9107 int previously_visible = f->output_data.x->has_been_visible;
9109 original_left = f->left_pos;
9110 original_top = f->top_pos;
9112 /* This must come after we set COUNT. */
9113 UNBLOCK_INPUT;
9115 /* We unblock here so that arriving X events are processed. */
9117 /* Now move the window back to where it was "supposed to be".
9118 But don't do it if the gravity is negative.
9119 When the gravity is negative, this uses a position
9120 that is 3 pixels too low. Perhaps that's really the border width.
9122 Don't do this if the window has never been visible before,
9123 because the window manager may choose the position
9124 and we don't want to override it. */
9126 if (! FRAME_VISIBLE_P (f)
9127 && ! FRAME_ICONIFIED_P (f)
9128 && ! FRAME_X_EMBEDDED_P (f)
9129 && f->win_gravity == NorthWestGravity
9130 && previously_visible)
9132 Drawable rootw;
9133 int x, y;
9134 unsigned int width, height, border, depth;
9136 BLOCK_INPUT;
9138 /* On some window managers (such as FVWM) moving an existing
9139 window, even to the same place, causes the window manager
9140 to introduce an offset. This can cause the window to move
9141 to an unexpected location. Check the geometry (a little
9142 slow here) and then verify that the window is in the right
9143 place. If the window is not in the right place, move it
9144 there, and take the potential window manager hit. */
9145 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9146 &rootw, &x, &y, &width, &height, &border, &depth);
9148 if (original_left != x || original_top != y)
9149 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9150 original_left, original_top);
9152 UNBLOCK_INPUT;
9155 XSETFRAME (frame, f);
9157 /* Wait until the frame is visible. Process X events until a
9158 MapNotify event has been seen, or until we think we won't get a
9159 MapNotify at all.. */
9160 for (count = input_signal_count + 10;
9161 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9163 /* Force processing of queued events. */
9164 x_sync (f);
9166 /* Machines that do polling rather than SIGIO have been
9167 observed to go into a busy-wait here. So we'll fake an
9168 alarm signal to let the handler know that there's something
9169 to be read. We used to raise a real alarm, but it seems
9170 that the handler isn't always enabled here. This is
9171 probably a bug. */
9172 if (input_polling_used ())
9174 /* It could be confusing if a real alarm arrives while
9175 processing the fake one. Turn it off and let the
9176 handler reset it. */
9177 int old_poll_suppress_count = poll_suppress_count;
9178 poll_suppress_count = 1;
9179 poll_for_input_1 ();
9180 poll_suppress_count = old_poll_suppress_count;
9183 /* See if a MapNotify event has been processed. */
9184 FRAME_SAMPLE_VISIBILITY (f);
9187 /* 2000-09-28: In
9189 (let ((f (selected-frame)))
9190 (iconify-frame f)
9191 (raise-frame f))
9193 the frame is not raised with various window managers on
9194 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9195 unknown reason, the call to XtMapWidget is completely ignored.
9196 Mapping the widget a second time works. */
9198 if (!FRAME_VISIBLE_P (f) && --retry_count != 0)
9199 goto retry;
9203 /* Change from mapped state to withdrawn state. */
9205 /* Make the frame visible (mapped and not iconified). */
9207 void
9208 x_make_frame_invisible (struct frame *f)
9210 Window window;
9212 /* Use the frame's outermost window, not the one we normally draw on. */
9213 window = FRAME_OUTER_WINDOW (f);
9215 /* Don't keep the highlight on an invisible frame. */
9216 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9217 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9219 BLOCK_INPUT;
9221 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9222 that the current position of the window is user-specified, rather than
9223 program-specified, so that when the window is mapped again, it will be
9224 placed at the same location, without forcing the user to position it
9225 by hand again (they have already done that once for this window.) */
9226 x_wm_set_size_hint (f, (long) 0, 1);
9228 #ifdef USE_GTK
9229 if (FRAME_GTK_OUTER_WIDGET (f))
9230 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9231 else
9232 #else
9233 if (FRAME_X_EMBEDDED_P (f))
9234 xembed_set_info (f, 0);
9235 else
9236 #endif
9239 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9240 DefaultScreen (FRAME_X_DISPLAY (f))))
9242 UNBLOCK_INPUT_RESIGNAL;
9243 error ("Can't notify window manager of window withdrawal");
9247 /* We can't distinguish this from iconification
9248 just by the event that we get from the server.
9249 So we can't win using the usual strategy of letting
9250 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9251 and synchronize with the server to make sure we agree. */
9252 f->visible = 0;
9253 FRAME_ICONIFIED_P (f) = 0;
9254 f->async_visible = 0;
9255 f->async_iconified = 0;
9257 x_sync (f);
9259 UNBLOCK_INPUT;
9262 /* Change window state from mapped to iconified. */
9264 void
9265 x_iconify_frame (struct frame *f)
9267 #ifdef USE_X_TOOLKIT
9268 int result;
9269 #endif
9270 Lisp_Object type;
9272 /* Don't keep the highlight on an invisible frame. */
9273 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9274 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9276 if (f->async_iconified)
9277 return;
9279 BLOCK_INPUT;
9281 FRAME_SAMPLE_VISIBILITY (f);
9283 type = x_icon_type (f);
9284 if (!NILP (type))
9285 x_bitmap_icon (f, type);
9287 #ifdef USE_GTK
9288 if (FRAME_GTK_OUTER_WIDGET (f))
9290 if (! FRAME_VISIBLE_P (f))
9291 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9293 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9294 f->iconified = 1;
9295 f->visible = 1;
9296 f->async_iconified = 1;
9297 f->async_visible = 0;
9298 UNBLOCK_INPUT;
9299 return;
9301 #endif
9303 #ifdef USE_X_TOOLKIT
9305 if (! FRAME_VISIBLE_P (f))
9307 if (! EQ (Vx_no_window_manager, Qt))
9308 x_wm_set_window_state (f, IconicState);
9309 /* This was XtPopup, but that did nothing for an iconified frame. */
9310 XtMapWidget (f->output_data.x->widget);
9311 /* The server won't give us any event to indicate
9312 that an invisible frame was changed to an icon,
9313 so we have to record it here. */
9314 f->iconified = 1;
9315 f->visible = 1;
9316 f->async_iconified = 1;
9317 f->async_visible = 0;
9318 UNBLOCK_INPUT;
9319 return;
9322 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9323 XtWindow (f->output_data.x->widget),
9324 DefaultScreen (FRAME_X_DISPLAY (f)));
9325 UNBLOCK_INPUT;
9327 if (!result)
9328 error ("Can't notify window manager of iconification");
9330 f->async_iconified = 1;
9331 f->async_visible = 0;
9334 BLOCK_INPUT;
9335 XFlush (FRAME_X_DISPLAY (f));
9336 UNBLOCK_INPUT;
9337 #else /* not USE_X_TOOLKIT */
9339 /* Make sure the X server knows where the window should be positioned,
9340 in case the user deiconifies with the window manager. */
9341 if (! FRAME_VISIBLE_P (f)
9342 && ! FRAME_ICONIFIED_P (f)
9343 && ! FRAME_X_EMBEDDED_P (f))
9344 x_set_offset (f, f->left_pos, f->top_pos, 0);
9346 /* Since we don't know which revision of X we're running, we'll use both
9347 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9349 /* X11R4: send a ClientMessage to the window manager using the
9350 WM_CHANGE_STATE type. */
9352 XEvent msg;
9354 msg.xclient.window = FRAME_X_WINDOW (f);
9355 msg.xclient.type = ClientMessage;
9356 msg.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9357 msg.xclient.format = 32;
9358 msg.xclient.data.l[0] = IconicState;
9360 if (! XSendEvent (FRAME_X_DISPLAY (f),
9361 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9362 False,
9363 SubstructureRedirectMask | SubstructureNotifyMask,
9364 &msg))
9366 UNBLOCK_INPUT_RESIGNAL;
9367 error ("Can't notify window manager of iconification");
9371 /* X11R3: set the initial_state field of the window manager hints to
9372 IconicState. */
9373 x_wm_set_window_state (f, IconicState);
9375 if (!FRAME_VISIBLE_P (f))
9377 /* If the frame was withdrawn, before, we must map it. */
9378 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9381 f->async_iconified = 1;
9382 f->async_visible = 0;
9384 XFlush (FRAME_X_DISPLAY (f));
9385 UNBLOCK_INPUT;
9386 #endif /* not USE_X_TOOLKIT */
9390 /* Free X resources of frame F. */
9392 void
9393 x_free_frame_resources (struct frame *f)
9395 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9396 Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
9397 #ifdef USE_X_TOOLKIT
9398 Lisp_Object bar;
9399 struct scroll_bar *b;
9400 #endif
9402 BLOCK_INPUT;
9404 /* If a display connection is dead, don't try sending more
9405 commands to the X server. */
9406 if (dpyinfo->display)
9408 /* We must free faces before destroying windows because some
9409 font-driver (e.g. xft) access a window while finishing a
9410 face. */
9411 if (FRAME_FACE_CACHE (f))
9412 free_frame_faces (f);
9414 if (f->output_data.x->icon_desc)
9415 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9417 #ifdef USE_X_TOOLKIT
9418 /* Explicitly destroy the scroll bars of the frame. Without
9419 this, we get "BadDrawable" errors from the toolkit later on,
9420 presumably from expose events generated for the disappearing
9421 toolkit scroll bars. */
9422 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9424 b = XSCROLL_BAR (bar);
9425 x_scroll_bar_remove (b);
9427 #endif
9429 #ifdef HAVE_X_I18N
9430 if (FRAME_XIC (f))
9431 free_frame_xic (f);
9432 #endif
9434 #ifdef USE_X_TOOLKIT
9435 if (f->output_data.x->widget)
9437 XtDestroyWidget (f->output_data.x->widget);
9438 f->output_data.x->widget = NULL;
9440 /* Tooltips don't have widgets, only a simple X window, even if
9441 we are using a toolkit. */
9442 else if (FRAME_X_WINDOW (f))
9443 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9445 free_frame_menubar (f);
9446 #else /* !USE_X_TOOLKIT */
9448 #ifdef USE_GTK
9449 xg_free_frame_widgets (f);
9450 #endif /* USE_GTK */
9452 if (FRAME_X_WINDOW (f))
9453 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9454 #endif /* !USE_X_TOOLKIT */
9456 unload_color (f, FRAME_FOREGROUND_PIXEL (f));
9457 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
9458 unload_color (f, f->output_data.x->cursor_pixel);
9459 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9460 unload_color (f, f->output_data.x->border_pixel);
9461 unload_color (f, f->output_data.x->mouse_pixel);
9463 if (f->output_data.x->scroll_bar_background_pixel != -1)
9464 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9465 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9466 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9467 #ifdef USE_TOOLKIT_SCROLL_BARS
9468 /* Scrollbar shadow colors. */
9469 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9470 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9471 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9472 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9473 #endif /* USE_TOOLKIT_SCROLL_BARS */
9474 if (f->output_data.x->white_relief.allocated_p)
9475 unload_color (f, f->output_data.x->white_relief.pixel);
9476 if (f->output_data.x->black_relief.allocated_p)
9477 unload_color (f, f->output_data.x->black_relief.pixel);
9479 x_free_gcs (f);
9480 XFlush (FRAME_X_DISPLAY (f));
9483 xfree (f->output_data.x->saved_menu_event);
9484 xfree (f->output_data.x);
9485 f->output_data.x = NULL;
9487 if (f == dpyinfo->x_focus_frame)
9488 dpyinfo->x_focus_frame = 0;
9489 if (f == dpyinfo->x_focus_event_frame)
9490 dpyinfo->x_focus_event_frame = 0;
9491 if (f == dpyinfo->x_highlight_frame)
9492 dpyinfo->x_highlight_frame = 0;
9494 if (f == hlinfo->mouse_face_mouse_frame)
9496 hlinfo->mouse_face_beg_row
9497 = hlinfo->mouse_face_beg_col = -1;
9498 hlinfo->mouse_face_end_row
9499 = hlinfo->mouse_face_end_col = -1;
9500 hlinfo->mouse_face_window = Qnil;
9501 hlinfo->mouse_face_deferred_gc = 0;
9502 hlinfo->mouse_face_mouse_frame = 0;
9505 UNBLOCK_INPUT;
9509 /* Destroy the X window of frame F. */
9511 static void
9512 x_destroy_window (struct frame *f)
9514 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9516 /* If a display connection is dead, don't try sending more
9517 commands to the X server. */
9518 if (dpyinfo->display != 0)
9519 x_free_frame_resources (f);
9521 dpyinfo->reference_count--;
9525 /* Setting window manager hints. */
9527 /* Set the normal size hints for the window manager, for frame F.
9528 FLAGS is the flags word to use--or 0 meaning preserve the flags
9529 that the window now has.
9530 If USER_POSITION is nonzero, we set the USPosition
9531 flag (this is useful when FLAGS is 0).
9532 The GTK version is in gtkutils.c */
9534 #ifndef USE_GTK
9535 void
9536 x_wm_set_size_hint (struct frame *f, long flags, int user_position)
9538 XSizeHints size_hints;
9539 Window window = FRAME_OUTER_WINDOW (f);
9541 /* Setting PMaxSize caused various problems. */
9542 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9544 size_hints.x = f->left_pos;
9545 size_hints.y = f->top_pos;
9547 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9548 size_hints.width = FRAME_PIXEL_WIDTH (f);
9550 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9551 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9552 size_hints.max_width = x_display_pixel_width (FRAME_X_DISPLAY_INFO (f))
9553 - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9554 size_hints.max_height = x_display_pixel_height (FRAME_X_DISPLAY_INFO (f))
9555 - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9557 /* Calculate the base and minimum sizes. */
9559 int base_width, base_height;
9560 int min_rows = 0, min_cols = 0;
9562 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9563 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9565 check_frame_size (f, &min_rows, &min_cols);
9567 /* The window manager uses the base width hints to calculate the
9568 current number of rows and columns in the frame while
9569 resizing; min_width and min_height aren't useful for this
9570 purpose, since they might not give the dimensions for a
9571 zero-row, zero-column frame.
9573 We use the base_width and base_height members if we have
9574 them; otherwise, we set the min_width and min_height members
9575 to the size for a zero x zero frame. */
9577 size_hints.flags |= PBaseSize;
9578 size_hints.base_width = base_width;
9579 size_hints.base_height = base_height + FRAME_MENUBAR_HEIGHT (f);
9580 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9581 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9584 /* If we don't need the old flags, we don't need the old hint at all. */
9585 if (flags)
9587 size_hints.flags |= flags;
9588 goto no_read;
9592 XSizeHints hints; /* Sometimes I hate X Windows... */
9593 long supplied_return;
9594 int value;
9596 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9597 &supplied_return);
9599 if (flags)
9600 size_hints.flags |= flags;
9601 else
9603 if (value == 0)
9604 hints.flags = 0;
9605 if (hints.flags & PSize)
9606 size_hints.flags |= PSize;
9607 if (hints.flags & PPosition)
9608 size_hints.flags |= PPosition;
9609 if (hints.flags & USPosition)
9610 size_hints.flags |= USPosition;
9611 if (hints.flags & USSize)
9612 size_hints.flags |= USSize;
9616 no_read:
9618 #ifdef PWinGravity
9619 size_hints.win_gravity = f->win_gravity;
9620 size_hints.flags |= PWinGravity;
9622 if (user_position)
9624 size_hints.flags &= ~ PPosition;
9625 size_hints.flags |= USPosition;
9627 #endif /* PWinGravity */
9629 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9631 #endif /* not USE_GTK */
9633 /* Used for IconicState or NormalState */
9635 static void
9636 x_wm_set_window_state (struct frame *f, int state)
9638 #ifdef USE_X_TOOLKIT
9639 Arg al[1];
9641 XtSetArg (al[0], XtNinitialState, state);
9642 XtSetValues (f->output_data.x->widget, al, 1);
9643 #else /* not USE_X_TOOLKIT */
9644 Window window = FRAME_X_WINDOW (f);
9646 f->output_data.x->wm_hints.flags |= StateHint;
9647 f->output_data.x->wm_hints.initial_state = state;
9649 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9650 #endif /* not USE_X_TOOLKIT */
9653 static void
9654 x_wm_set_icon_pixmap (struct frame *f, ptrdiff_t pixmap_id)
9656 Pixmap icon_pixmap, icon_mask;
9658 #if !defined USE_X_TOOLKIT && !defined USE_GTK
9659 Window window = FRAME_OUTER_WINDOW (f);
9660 #endif
9662 if (pixmap_id > 0)
9664 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9665 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9666 icon_mask = x_bitmap_mask (f, pixmap_id);
9667 f->output_data.x->wm_hints.icon_mask = icon_mask;
9669 else
9671 /* It seems there is no way to turn off use of an icon
9672 pixmap. */
9673 return;
9677 #ifdef USE_GTK
9679 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9680 return;
9683 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9686 Arg al[1];
9687 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9688 XtSetValues (f->output_data.x->widget, al, 1);
9689 XtSetArg (al[0], XtNiconMask, icon_mask);
9690 XtSetValues (f->output_data.x->widget, al, 1);
9693 #else /* not USE_X_TOOLKIT && not USE_GTK */
9695 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9696 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9698 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9701 void
9702 x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
9704 Window window = FRAME_OUTER_WINDOW (f);
9706 f->output_data.x->wm_hints.flags |= IconPositionHint;
9707 f->output_data.x->wm_hints.icon_x = icon_x;
9708 f->output_data.x->wm_hints.icon_y = icon_y;
9710 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9714 /***********************************************************************
9715 Fonts
9716 ***********************************************************************/
9718 #if GLYPH_DEBUG
9720 /* Check that FONT is valid on frame F. It is if it can be found in F's
9721 font table. */
9723 static void
9724 x_check_font (struct frame *f, struct font *font)
9726 xassert (font != NULL && ! NILP (font->props[FONT_TYPE_INDEX]));
9727 if (font->driver->check)
9728 xassert (font->driver->check (f, font) == 0);
9731 #endif /* GLYPH_DEBUG != 0 */
9734 /***********************************************************************
9735 Initialization
9736 ***********************************************************************/
9738 #ifdef USE_X_TOOLKIT
9739 static XrmOptionDescRec emacs_options[] = {
9740 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
9741 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
9743 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
9744 XrmoptionSepArg, NULL},
9745 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
9747 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9748 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9749 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
9750 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9751 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
9752 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
9753 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
9756 /* Whether atimer for Xt timeouts is activated or not. */
9758 static int x_timeout_atimer_activated_flag;
9760 #endif /* USE_X_TOOLKIT */
9762 static int x_initialized;
9764 /* Test whether two display-name strings agree up to the dot that separates
9765 the screen number from the server number. */
9766 static int
9767 same_x_server (const char *name1, const char *name2)
9769 int seen_colon = 0;
9770 const char *system_name = SSDATA (Vsystem_name);
9771 ptrdiff_t system_name_length = SBYTES (Vsystem_name);
9772 ptrdiff_t length_until_period = 0;
9774 while (system_name[length_until_period] != 0
9775 && system_name[length_until_period] != '.')
9776 length_until_period++;
9778 /* Treat `unix' like an empty host name. */
9779 if (! strncmp (name1, "unix:", 5))
9780 name1 += 4;
9781 if (! strncmp (name2, "unix:", 5))
9782 name2 += 4;
9783 /* Treat this host's name like an empty host name. */
9784 if (! strncmp (name1, system_name, system_name_length)
9785 && name1[system_name_length] == ':')
9786 name1 += system_name_length;
9787 if (! strncmp (name2, system_name, system_name_length)
9788 && name2[system_name_length] == ':')
9789 name2 += system_name_length;
9790 /* Treat this host's domainless name like an empty host name. */
9791 if (! strncmp (name1, system_name, length_until_period)
9792 && name1[length_until_period] == ':')
9793 name1 += length_until_period;
9794 if (! strncmp (name2, system_name, length_until_period)
9795 && name2[length_until_period] == ':')
9796 name2 += length_until_period;
9798 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
9800 if (*name1 == ':')
9801 seen_colon = 1;
9802 if (seen_colon && *name1 == '.')
9803 return 1;
9805 return (seen_colon
9806 && (*name1 == '.' || *name1 == '\0')
9807 && (*name2 == '.' || *name2 == '\0'));
9810 /* Count number of set bits in mask and number of bits to shift to
9811 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
9812 to 5. */
9813 static void
9814 get_bits_and_offset (long unsigned int mask, int *bits, int *offset)
9816 int nr = 0;
9817 int off = 0;
9819 while (!(mask & 1))
9821 off++;
9822 mask >>= 1;
9825 while (mask & 1)
9827 nr++;
9828 mask >>= 1;
9831 *offset = off;
9832 *bits = nr;
9835 /* Return 1 if display DISPLAY is available for use, 0 otherwise.
9836 But don't permanently open it, just test its availability. */
9839 x_display_ok (const char *display)
9841 int dpy_ok = 1;
9842 Display *dpy;
9844 dpy = XOpenDisplay (display);
9845 if (dpy)
9846 XCloseDisplay (dpy);
9847 else
9848 dpy_ok = 0;
9849 return dpy_ok;
9852 #ifdef USE_GTK
9853 static void
9854 my_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
9855 const gchar *msg, gpointer user_data)
9857 if (!strstr (msg, "g_set_prgname"))
9858 fprintf (stderr, "%s-WARNING **: %s\n", log_domain, msg);
9860 #endif
9862 /* Open a connection to X display DISPLAY_NAME, and return
9863 the structure that describes the open display.
9864 If we cannot contact the display, return null. */
9866 struct x_display_info *
9867 x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
9869 int connection;
9870 Display *dpy;
9871 struct terminal *terminal;
9872 struct x_display_info *dpyinfo;
9873 XrmDatabase xrdb;
9874 Mouse_HLInfo *hlinfo;
9875 ptrdiff_t lim;
9877 BLOCK_INPUT;
9879 if (!x_initialized)
9881 x_initialize ();
9882 ++x_initialized;
9885 if (! x_display_ok (SSDATA (display_name)))
9886 error ("Display %s can't be opened", SSDATA (display_name));
9888 #ifdef USE_GTK
9890 #define NUM_ARGV 10
9891 int argc;
9892 char *argv[NUM_ARGV];
9893 char **argv2 = argv;
9894 guint id;
9896 if (x_initialized++ > 1)
9898 xg_display_open (SSDATA (display_name), &dpy);
9900 else
9902 static char display_opt[] = "--display";
9903 static char name_opt[] = "--name";
9905 for (argc = 0; argc < NUM_ARGV; ++argc)
9906 argv[argc] = 0;
9908 argc = 0;
9909 argv[argc++] = initial_argv[0];
9911 if (! NILP (display_name))
9913 argv[argc++] = display_opt;
9914 argv[argc++] = SSDATA (display_name);
9917 argv[argc++] = name_opt;
9918 argv[argc++] = resource_name;
9920 XSetLocaleModifiers ("");
9922 /* Emacs can only handle core input events, so make sure
9923 Gtk doesn't use Xinput or Xinput2 extensions. */
9925 static char fix_events[] = "GDK_CORE_DEVICE_EVENTS=1";
9926 putenv (fix_events);
9929 /* Work around GLib bug that outputs a faulty warning. See
9930 https://bugzilla.gnome.org/show_bug.cgi?id=563627. */
9931 id = g_log_set_handler ("GLib", G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL
9932 | G_LOG_FLAG_RECURSION, my_log_handler, NULL);
9933 #ifdef HAVE_CLUTTER
9934 gtk_clutter_init (&argc, &argv2);
9935 #else
9936 gtk_init (&argc, &argv2);
9937 #endif
9938 g_log_remove_handler ("GLib", id);
9940 /* gtk_init does set_locale. We must fix locale after calling it. */
9941 fixup_locale ();
9942 xg_initialize ();
9944 dpy = DEFAULT_GDK_DISPLAY ();
9946 /* NULL window -> events for all windows go to our function */
9947 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
9949 #if GTK_MAJOR_VERSION <= 2 && GTK_MINOR_VERSION <= 90
9950 /* Load our own gtkrc if it exists. */
9952 const char *file = "~/.emacs.d/gtkrc";
9953 Lisp_Object s, abs_file;
9955 s = make_string (file, strlen (file));
9956 abs_file = Fexpand_file_name (s, Qnil);
9958 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
9959 gtk_rc_parse (SSDATA (abs_file));
9961 #endif
9963 XSetErrorHandler (x_error_handler);
9964 XSetIOErrorHandler (x_io_error_quitter);
9967 #else /* not USE_GTK */
9968 #ifdef USE_X_TOOLKIT
9969 /* weiner@footloose.sps.mot.com reports that this causes
9970 errors with X11R5:
9971 X protocol error: BadAtom (invalid Atom parameter)
9972 on protocol request 18skiloaf.
9973 So let's not use it until R6. */
9974 #ifdef HAVE_X11XTR6
9975 XtSetLanguageProc (NULL, NULL, NULL);
9976 #endif
9979 int argc = 0;
9980 char *argv[3];
9982 argv[0] = "";
9983 argc = 1;
9984 if (xrm_option)
9986 argv[argc++] = "-xrm";
9987 argv[argc++] = xrm_option;
9989 turn_on_atimers (0);
9990 dpy = XtOpenDisplay (Xt_app_con, SSDATA (display_name),
9991 resource_name, EMACS_CLASS,
9992 emacs_options, XtNumber (emacs_options),
9993 &argc, argv);
9994 turn_on_atimers (1);
9996 #ifdef HAVE_X11XTR6
9997 /* I think this is to compensate for XtSetLanguageProc. */
9998 fixup_locale ();
9999 #endif
10002 #else /* not USE_X_TOOLKIT */
10003 XSetLocaleModifiers ("");
10004 dpy = XOpenDisplay (SSDATA (display_name));
10005 #endif /* not USE_X_TOOLKIT */
10006 #endif /* not USE_GTK*/
10008 /* Detect failure. */
10009 if (dpy == 0)
10011 UNBLOCK_INPUT;
10012 return 0;
10015 /* We have definitely succeeded. Record the new connection. */
10017 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
10018 memset (dpyinfo, 0, sizeof *dpyinfo);
10019 hlinfo = &dpyinfo->mouse_highlight;
10021 terminal = x_create_terminal (dpyinfo);
10024 struct x_display_info *share;
10025 Lisp_Object tail;
10027 for (share = x_display_list, tail = x_display_name_list; share;
10028 share = share->next, tail = XCDR (tail))
10029 if (same_x_server (SSDATA (XCAR (XCAR (tail))),
10030 SSDATA (display_name)))
10031 break;
10032 if (share)
10033 terminal->kboard = share->terminal->kboard;
10034 else
10036 terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
10037 init_kboard (terminal->kboard);
10038 KVAR (terminal->kboard, Vwindow_system) = Qx;
10040 /* Add the keyboard to the list before running Lisp code (via
10041 Qvendor_specific_keysyms below), since these are not traced
10042 via terminals but only through all_kboards. */
10043 terminal->kboard->next_kboard = all_kboards;
10044 all_kboards = terminal->kboard;
10046 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
10048 char *vendor = ServerVendor (dpy);
10050 /* Protect terminal from GC before removing it from the
10051 list of terminals. */
10052 struct gcpro gcpro1;
10053 Lisp_Object gcpro_term;
10054 XSETTERMINAL (gcpro_term, terminal);
10055 GCPRO1 (gcpro_term);
10057 /* Temporarily hide the partially initialized terminal. */
10058 terminal_list = terminal->next_terminal;
10059 UNBLOCK_INPUT;
10060 KVAR (terminal->kboard, Vsystem_key_alist)
10061 = call1 (Qvendor_specific_keysyms,
10062 vendor ? build_string (vendor) : empty_unibyte_string);
10063 BLOCK_INPUT;
10064 terminal->next_terminal = terminal_list;
10065 terminal_list = terminal;
10066 UNGCPRO;
10069 /* Don't let the initial kboard remain current longer than necessary.
10070 That would cause problems if a file loaded on startup tries to
10071 prompt in the mini-buffer. */
10072 if (current_kboard == initial_kboard)
10073 current_kboard = terminal->kboard;
10075 terminal->kboard->reference_count++;
10078 /* Put this display on the chain. */
10079 dpyinfo->next = x_display_list;
10080 x_display_list = dpyinfo;
10082 /* Put it on x_display_name_list as well, to keep them parallel. */
10083 x_display_name_list = Fcons (Fcons (display_name, Qnil),
10084 x_display_name_list);
10085 dpyinfo->name_list_element = XCAR (x_display_name_list);
10087 dpyinfo->display = dpy;
10089 /* Set the name of the terminal. */
10090 terminal->name = (char *) xmalloc (SBYTES (display_name) + 1);
10091 strncpy (terminal->name, SSDATA (display_name), SBYTES (display_name));
10092 terminal->name[SBYTES (display_name)] = 0;
10094 #if 0
10095 XSetAfterFunction (x_current_display, x_trace_wire);
10096 #endif /* ! 0 */
10098 lim = min (PTRDIFF_MAX, SIZE_MAX) - sizeof "@";
10099 if (lim - SBYTES (Vinvocation_name) < SBYTES (Vsystem_name))
10100 memory_full (SIZE_MAX);
10101 dpyinfo->x_id_name
10102 = (char *) xmalloc (SBYTES (Vinvocation_name)
10103 + SBYTES (Vsystem_name)
10104 + 2);
10105 strcat (strcat (strcpy (dpyinfo->x_id_name, SSDATA (Vinvocation_name)), "@"),
10106 SSDATA (Vsystem_name));
10108 /* Figure out which modifier bits mean what. */
10109 x_find_modifier_meanings (dpyinfo);
10111 /* Get the scroll bar cursor. */
10112 #ifdef USE_GTK
10113 /* We must create a GTK cursor, it is required for GTK widgets. */
10114 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
10115 #endif /* USE_GTK */
10117 dpyinfo->vertical_scroll_bar_cursor
10118 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
10120 xrdb = x_load_resources (dpyinfo->display, xrm_option,
10121 resource_name, EMACS_CLASS);
10122 #ifdef HAVE_XRMSETDATABASE
10123 XrmSetDatabase (dpyinfo->display, xrdb);
10124 #else
10125 dpyinfo->display->db = xrdb;
10126 #endif
10127 /* Put the rdb where we can find it in a way that works on
10128 all versions. */
10129 dpyinfo->xrdb = xrdb;
10131 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
10132 DefaultScreen (dpyinfo->display));
10133 select_visual (dpyinfo);
10134 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
10135 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
10136 dpyinfo->client_leader_window = 0;
10137 dpyinfo->grabbed = 0;
10138 dpyinfo->reference_count = 0;
10139 dpyinfo->icon_bitmap_id = -1;
10140 dpyinfo->n_fonts = 0;
10141 dpyinfo->bitmaps = 0;
10142 dpyinfo->bitmaps_size = 0;
10143 dpyinfo->bitmaps_last = 0;
10144 dpyinfo->scratch_cursor_gc = 0;
10145 hlinfo->mouse_face_mouse_frame = 0;
10146 hlinfo->mouse_face_deferred_gc = 0;
10147 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
10148 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
10149 hlinfo->mouse_face_face_id = DEFAULT_FACE_ID;
10150 hlinfo->mouse_face_window = Qnil;
10151 hlinfo->mouse_face_overlay = Qnil;
10152 hlinfo->mouse_face_mouse_x = hlinfo->mouse_face_mouse_y = 0;
10153 hlinfo->mouse_face_defer = 0;
10154 hlinfo->mouse_face_hidden = 0;
10155 dpyinfo->x_focus_frame = 0;
10156 dpyinfo->x_focus_event_frame = 0;
10157 dpyinfo->x_highlight_frame = 0;
10158 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
10160 /* See if we can construct pixel values from RGB values. */
10161 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
10162 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
10164 if (dpyinfo->visual->class == TrueColor)
10166 get_bits_and_offset (dpyinfo->visual->red_mask,
10167 &dpyinfo->red_bits, &dpyinfo->red_offset);
10168 get_bits_and_offset (dpyinfo->visual->blue_mask,
10169 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
10170 get_bits_and_offset (dpyinfo->visual->green_mask,
10171 &dpyinfo->green_bits, &dpyinfo->green_offset);
10174 /* See if a private colormap is requested. */
10175 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
10177 if (dpyinfo->visual->class == PseudoColor)
10179 Lisp_Object value;
10180 value = display_x_get_resource (dpyinfo,
10181 build_string ("privateColormap"),
10182 build_string ("PrivateColormap"),
10183 Qnil, Qnil);
10184 if (STRINGP (value)
10185 && (!strcmp (SSDATA (value), "true")
10186 || !strcmp (SSDATA (value), "on")))
10187 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
10190 else
10191 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
10192 dpyinfo->visual, AllocNone);
10194 #ifdef HAVE_XFT
10196 /* If we are using Xft, check dpi value in X resources.
10197 It is better we use it as well, since Xft will use it, as will all
10198 Gnome applications. If our real DPI is smaller or larger than the
10199 one Xft uses, our font will look smaller or larger than other
10200 for other applications, even if it is the same font name (monospace-10
10201 for example). */
10202 char *v = XGetDefault (dpyinfo->display, "Xft", "dpi");
10203 double d;
10204 if (v != NULL && sscanf (v, "%lf", &d) == 1)
10205 dpyinfo->resy = dpyinfo->resx = d;
10207 #endif
10209 if (dpyinfo->resy < 1)
10211 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
10212 double pixels = DisplayHeight (dpyinfo->display, screen_number);
10213 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
10214 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10215 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
10216 pixels = DisplayWidth (dpyinfo->display, screen_number);
10217 mm = DisplayWidthMM (dpyinfo->display, screen_number);
10218 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
10219 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
10223 const struct
10225 const char *name;
10226 Atom *atom;
10227 } atom_refs[] = {
10228 { "WM_PROTOCOLS", &dpyinfo->Xatom_wm_protocols },
10229 { "WM_TAKE_FOCUS", &dpyinfo->Xatom_wm_take_focus },
10230 { "WM_SAVE_YOURSELF", &dpyinfo->Xatom_wm_save_yourself },
10231 { "WM_DELETE_WINDOW", &dpyinfo->Xatom_wm_delete_window },
10232 { "WM_CHANGE_STATE", &dpyinfo->Xatom_wm_change_state },
10233 { "WM_CONFIGURE_DENIED", &dpyinfo->Xatom_wm_configure_denied },
10234 { "WM_MOVED", &dpyinfo->Xatom_wm_window_moved },
10235 { "WM_CLIENT_LEADER", &dpyinfo->Xatom_wm_client_leader },
10236 { "Editres", &dpyinfo->Xatom_editres },
10237 { "CLIPBOARD", &dpyinfo->Xatom_CLIPBOARD },
10238 { "TIMESTAMP", &dpyinfo->Xatom_TIMESTAMP },
10239 { "TEXT", &dpyinfo->Xatom_TEXT },
10240 { "COMPOUND_TEXT", &dpyinfo->Xatom_COMPOUND_TEXT },
10241 { "UTF8_STRING", &dpyinfo->Xatom_UTF8_STRING },
10242 { "DELETE", &dpyinfo->Xatom_DELETE },
10243 { "MULTIPLE", &dpyinfo->Xatom_MULTIPLE },
10244 { "INCR", &dpyinfo->Xatom_INCR },
10245 { "_EMACS_TMP_", &dpyinfo->Xatom_EMACS_TMP },
10246 { "TARGETS", &dpyinfo->Xatom_TARGETS },
10247 { "NULL", &dpyinfo->Xatom_NULL },
10248 { "ATOM", &dpyinfo->Xatom_ATOM },
10249 { "ATOM_PAIR", &dpyinfo->Xatom_ATOM_PAIR },
10250 { "CLIPBOARD_MANAGER", &dpyinfo->Xatom_CLIPBOARD_MANAGER },
10251 { "_XEMBED_INFO", &dpyinfo->Xatom_XEMBED_INFO },
10252 /* For properties of font. */
10253 { "PIXEL_SIZE", &dpyinfo->Xatom_PIXEL_SIZE },
10254 { "AVERAGE_WIDTH", &dpyinfo->Xatom_AVERAGE_WIDTH },
10255 { "_MULE_BASELINE_OFFSET", &dpyinfo->Xatom_MULE_BASELINE_OFFSET },
10256 { "_MULE_RELATIVE_COMPOSE", &dpyinfo->Xatom_MULE_RELATIVE_COMPOSE },
10257 { "_MULE_DEFAULT_ASCENT", &dpyinfo->Xatom_MULE_DEFAULT_ASCENT },
10258 /* Ghostscript support. */
10259 { "DONE", &dpyinfo->Xatom_DONE },
10260 { "PAGE", &dpyinfo->Xatom_PAGE },
10261 { "SCROLLBAR", &dpyinfo->Xatom_Scrollbar },
10262 { "_XEMBED", &dpyinfo->Xatom_XEMBED },
10263 /* EWMH */
10264 { "_NET_WM_STATE", &dpyinfo->Xatom_net_wm_state },
10265 { "_NET_WM_STATE_FULLSCREEN", &dpyinfo->Xatom_net_wm_state_fullscreen },
10266 { "_NET_WM_STATE_MAXIMIZED_HORZ",
10267 &dpyinfo->Xatom_net_wm_state_maximized_horz },
10268 { "_NET_WM_STATE_MAXIMIZED_VERT",
10269 &dpyinfo->Xatom_net_wm_state_maximized_vert },
10270 { "_NET_WM_STATE_STICKY", &dpyinfo->Xatom_net_wm_state_sticky },
10271 { "_NET_WM_WINDOW_TYPE", &dpyinfo->Xatom_net_window_type },
10272 { "_NET_WM_WINDOW_TYPE_TOOLTIP",
10273 &dpyinfo->Xatom_net_window_type_tooltip },
10274 { "_NET_WM_ICON_NAME", &dpyinfo->Xatom_net_wm_icon_name },
10275 { "_NET_WM_NAME", &dpyinfo->Xatom_net_wm_name },
10276 { "_NET_SUPPORTED", &dpyinfo->Xatom_net_supported },
10277 { "_NET_SUPPORTING_WM_CHECK", &dpyinfo->Xatom_net_supporting_wm_check },
10278 { "_NET_WM_WINDOW_OPACITY", &dpyinfo->Xatom_net_wm_window_opacity },
10279 { "_NET_ACTIVE_WINDOW", &dpyinfo->Xatom_net_active_window },
10280 { "_NET_FRAME_EXTENTS", &dpyinfo->Xatom_net_frame_extents },
10281 /* Session management */
10282 { "SM_CLIENT_ID", &dpyinfo->Xatom_SM_CLIENT_ID },
10283 { "_XSETTINGS_SETTINGS", &dpyinfo->Xatom_xsettings_prop },
10284 { "MANAGER", &dpyinfo->Xatom_xsettings_mgr },
10287 int i;
10288 const int atom_count = sizeof (atom_refs) / sizeof (atom_refs[0]);
10289 /* 1 for _XSETTINGS_SN */
10290 const int total_atom_count = 1 + atom_count;
10291 Atom *atoms_return = xmalloc (sizeof (Atom) * total_atom_count);
10292 char **atom_names = xmalloc (sizeof (char *) * total_atom_count);
10293 char xsettings_atom_name[64];
10295 for (i = 0; i < atom_count; i++)
10296 atom_names[i] = (char *) atom_refs[i].name;
10298 /* Build _XSETTINGS_SN atom name */
10299 snprintf (xsettings_atom_name, sizeof (xsettings_atom_name),
10300 "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo->screen));
10301 atom_names[i] = xsettings_atom_name;
10303 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
10304 False, atoms_return);
10306 for (i = 0; i < atom_count; i++)
10307 *atom_refs[i].atom = atoms_return[i];
10309 /* Manual copy of last atom */
10310 dpyinfo->Xatom_xsettings_sel = atoms_return[i];
10312 xfree (atom_names);
10313 xfree (atoms_return);
10316 dpyinfo->x_dnd_atoms_size = 8;
10317 dpyinfo->x_dnd_atoms_length = 0;
10318 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
10319 * dpyinfo->x_dnd_atoms_size);
10321 dpyinfo->net_supported_atoms = NULL;
10322 dpyinfo->nr_net_supported_atoms = 0;
10323 dpyinfo->net_supported_window = 0;
10325 connection = ConnectionNumber (dpyinfo->display);
10326 dpyinfo->connection = connection;
10329 dpyinfo->gray
10330 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
10331 gray_bitmap_bits,
10332 gray_bitmap_width, gray_bitmap_height,
10333 1, 0, 1);
10336 #ifdef HAVE_X_I18N
10337 xim_initialize (dpyinfo, resource_name);
10338 #endif
10340 xsettings_initialize (dpyinfo);
10342 /* This is only needed for distinguishing keyboard and process input. */
10343 if (connection != 0)
10344 add_keyboard_wait_descriptor (connection);
10346 #ifdef F_SETOWN
10347 fcntl (connection, F_SETOWN, getpid ());
10348 #endif /* ! defined (F_SETOWN) */
10350 #ifdef SIGIO
10351 if (interrupt_input)
10352 init_sigio (connection);
10353 #endif /* ! defined (SIGIO) */
10355 #ifdef USE_LUCID
10357 XrmValue d, fr, to;
10358 Font font;
10360 dpy = dpyinfo->display;
10361 d.addr = (XPointer)&dpy;
10362 d.size = sizeof (Display *);
10363 fr.addr = XtDefaultFont;
10364 fr.size = sizeof (XtDefaultFont);
10365 to.size = sizeof (Font *);
10366 to.addr = (XPointer)&font;
10367 x_catch_errors (dpy);
10368 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
10369 abort ();
10370 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
10371 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
10372 x_uncatch_errors ();
10374 #endif
10376 /* See if we should run in synchronous mode. This is useful
10377 for debugging X code. */
10379 Lisp_Object value;
10380 value = display_x_get_resource (dpyinfo,
10381 build_string ("synchronous"),
10382 build_string ("Synchronous"),
10383 Qnil, Qnil);
10384 if (STRINGP (value)
10385 && (!strcmp (SSDATA (value), "true")
10386 || !strcmp (SSDATA (value), "on")))
10387 XSynchronize (dpyinfo->display, True);
10391 Lisp_Object value;
10392 value = display_x_get_resource (dpyinfo,
10393 build_string ("useXIM"),
10394 build_string ("UseXIM"),
10395 Qnil, Qnil);
10396 #ifdef USE_XIM
10397 if (STRINGP (value)
10398 && (!strcmp (SSDATA (value), "false")
10399 || !strcmp (SSDATA (value), "off")))
10400 use_xim = 0;
10401 #else
10402 if (STRINGP (value)
10403 && (!strcmp (SSDATA (value), "true")
10404 || !strcmp (SSDATA (value), "on")))
10405 use_xim = 1;
10406 #endif
10409 #ifdef HAVE_X_SM
10410 /* Only do this for the very first display in the Emacs session.
10411 Ignore X session management when Emacs was first started on a
10412 tty. */
10413 if (terminal->id == 1)
10414 x_session_initialize (dpyinfo);
10415 #endif
10417 UNBLOCK_INPUT;
10419 return dpyinfo;
10422 /* Get rid of display DPYINFO, deleting all frames on it,
10423 and without sending any more commands to the X server. */
10425 static void
10426 x_delete_display (struct x_display_info *dpyinfo)
10428 struct terminal *t;
10430 /* Close all frames and delete the generic struct terminal for this
10431 X display. */
10432 for (t = terminal_list; t; t = t->next_terminal)
10433 if (t->type == output_x_window && t->display_info.x == dpyinfo)
10435 #ifdef HAVE_X_SM
10436 /* Close X session management when we close its display. */
10437 if (t->id == 1 && x_session_have_connection ())
10438 x_session_close ();
10439 #endif
10440 delete_terminal (t);
10441 break;
10444 delete_keyboard_wait_descriptor (dpyinfo->connection);
10446 /* Discard this display from x_display_name_list and x_display_list.
10447 We can't use Fdelq because that can quit. */
10448 if (! NILP (x_display_name_list)
10449 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
10450 x_display_name_list = XCDR (x_display_name_list);
10451 else
10453 Lisp_Object tail;
10455 tail = x_display_name_list;
10456 while (CONSP (tail) && CONSP (XCDR (tail)))
10458 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
10460 XSETCDR (tail, XCDR (XCDR (tail)));
10461 break;
10463 tail = XCDR (tail);
10467 if (next_noop_dpyinfo == dpyinfo)
10468 next_noop_dpyinfo = dpyinfo->next;
10470 if (x_display_list == dpyinfo)
10471 x_display_list = dpyinfo->next;
10472 else
10474 struct x_display_info *tail;
10476 for (tail = x_display_list; tail; tail = tail->next)
10477 if (tail->next == dpyinfo)
10478 tail->next = tail->next->next;
10481 xfree (dpyinfo->x_id_name);
10482 xfree (dpyinfo->x_dnd_atoms);
10483 xfree (dpyinfo->color_cells);
10484 xfree (dpyinfo);
10487 #ifdef USE_X_TOOLKIT
10489 /* Atimer callback function for TIMER. Called every 0.1s to process
10490 Xt timeouts, if needed. We must avoid calling XtAppPending as
10491 much as possible because that function does an implicit XFlush
10492 that slows us down. */
10494 static void
10495 x_process_timeouts (struct atimer *timer)
10497 BLOCK_INPUT;
10498 x_timeout_atimer_activated_flag = 0;
10499 if (toolkit_scroll_bar_interaction || popup_activated ())
10501 while (XtAppPending (Xt_app_con) & XtIMTimer)
10502 XtAppProcessEvent (Xt_app_con, XtIMTimer);
10503 /* Reactivate the atimer for next time. */
10504 x_activate_timeout_atimer ();
10506 UNBLOCK_INPUT;
10509 /* Install an asynchronous timer that processes Xt timeout events
10510 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
10511 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
10512 function whenever these variables are set. This is necessary
10513 because some widget sets use timeouts internally, for example the
10514 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
10515 processed, these widgets don't behave normally. */
10517 void
10518 x_activate_timeout_atimer (void)
10520 BLOCK_INPUT;
10521 if (!x_timeout_atimer_activated_flag)
10523 EMACS_TIME interval;
10525 EMACS_SET_SECS_USECS (interval, 0, 100000);
10526 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
10527 x_timeout_atimer_activated_flag = 1;
10529 UNBLOCK_INPUT;
10532 #endif /* USE_X_TOOLKIT */
10535 /* Set up use of X before we make the first connection. */
10537 extern frame_parm_handler x_frame_parm_handlers[];
10539 static struct redisplay_interface x_redisplay_interface =
10541 x_frame_parm_handlers,
10542 x_produce_glyphs,
10543 x_write_glyphs,
10544 x_insert_glyphs,
10545 x_clear_end_of_line,
10546 x_scroll_run,
10547 x_after_update_window_line,
10548 x_update_window_begin,
10549 x_update_window_end,
10550 x_cursor_to,
10551 x_flush,
10552 #ifdef XFlush
10553 x_flush,
10554 #else
10555 0, /* flush_display_optional */
10556 #endif
10557 x_clear_window_mouse_face,
10558 x_get_glyph_overhangs,
10559 x_fix_overlapping_area,
10560 x_draw_fringe_bitmap,
10561 0, /* define_fringe_bitmap */
10562 0, /* destroy_fringe_bitmap */
10563 x_compute_glyph_string_overhangs,
10564 x_draw_glyph_string,
10565 x_define_frame_cursor,
10566 x_clear_frame_area,
10567 x_draw_window_cursor,
10568 x_draw_vertical_window_border,
10569 x_shift_glyphs_for_insert
10573 /* This function is called when the last frame on a display is deleted. */
10574 void
10575 x_delete_terminal (struct terminal *terminal)
10577 struct x_display_info *dpyinfo = terminal->display_info.x;
10579 /* Protect against recursive calls. delete_frame in
10580 delete_terminal calls us back when it deletes our last frame. */
10581 if (!terminal->name)
10582 return;
10584 BLOCK_INPUT;
10585 #ifdef HAVE_X_I18N
10586 /* We must close our connection to the XIM server before closing the
10587 X display. */
10588 if (dpyinfo->xim)
10589 xim_close_dpy (dpyinfo);
10590 #endif
10592 /* If called from x_connection_closed, the display may already be closed
10593 and dpyinfo->display was set to 0 to indicate that. */
10594 if (dpyinfo->display)
10596 x_destroy_all_bitmaps (dpyinfo);
10597 XSetCloseDownMode (dpyinfo->display, DestroyAll);
10599 /* Whether or not XCloseDisplay destroys the associated resource
10600 database depends on the version of libX11. To avoid both
10601 crash and memory leak, we dissociate the database from the
10602 display and then destroy dpyinfo->xrdb ourselves.
10604 Unfortunately, the above strategy does not work in some
10605 situations due to a bug in newer versions of libX11: because
10606 XrmSetDatabase doesn't clear the flag XlibDisplayDfltRMDB if
10607 dpy->db is NULL, XCloseDisplay destroys the associated
10608 database whereas it has not been created by XGetDefault
10609 (Bug#21974 in freedesktop.org Bugzilla). As a workaround, we
10610 don't destroy the database here in order to avoid the crash
10611 in the above situations for now, though that may cause memory
10612 leaks in other situations. */
10613 #if 0
10614 #ifdef HAVE_XRMSETDATABASE
10615 XrmSetDatabase (dpyinfo->display, NULL);
10616 #else
10617 dpyinfo->display->db = NULL;
10618 #endif
10619 /* We used to call XrmDestroyDatabase from x_delete_display, but
10620 some older versions of libX11 crash if we call it after
10621 closing all the displays. */
10622 XrmDestroyDatabase (dpyinfo->xrdb);
10623 #endif
10625 #ifdef USE_GTK
10626 xg_display_close (dpyinfo->display);
10627 #else
10628 #ifdef USE_X_TOOLKIT
10629 XtCloseDisplay (dpyinfo->display);
10630 #else
10631 XCloseDisplay (dpyinfo->display);
10632 #endif
10633 #endif /* ! USE_GTK */
10636 /* Mark as dead. */
10637 dpyinfo->display = NULL;
10638 x_delete_display (dpyinfo);
10639 UNBLOCK_INPUT;
10642 /* Create a struct terminal, initialize it with the X11 specific
10643 functions and make DISPLAY->TERMINAL point to it. */
10645 static struct terminal *
10646 x_create_terminal (struct x_display_info *dpyinfo)
10648 struct terminal *terminal;
10650 terminal = create_terminal ();
10652 terminal->type = output_x_window;
10653 terminal->display_info.x = dpyinfo;
10654 dpyinfo->terminal = terminal;
10656 /* kboard is initialized in x_term_init. */
10658 terminal->clear_frame_hook = x_clear_frame;
10659 terminal->ins_del_lines_hook = x_ins_del_lines;
10660 terminal->delete_glyphs_hook = x_delete_glyphs;
10661 terminal->ring_bell_hook = XTring_bell;
10662 terminal->toggle_invisible_pointer_hook = XTtoggle_invisible_pointer;
10663 terminal->reset_terminal_modes_hook = XTreset_terminal_modes;
10664 terminal->set_terminal_modes_hook = XTset_terminal_modes;
10665 terminal->update_begin_hook = x_update_begin;
10666 terminal->update_end_hook = x_update_end;
10667 terminal->set_terminal_window_hook = XTset_terminal_window;
10668 terminal->read_socket_hook = XTread_socket;
10669 terminal->frame_up_to_date_hook = XTframe_up_to_date;
10670 terminal->mouse_position_hook = XTmouse_position;
10671 terminal->frame_rehighlight_hook = XTframe_rehighlight;
10672 terminal->frame_raise_lower_hook = XTframe_raise_lower;
10673 terminal->fullscreen_hook = XTfullscreen_hook;
10674 terminal->set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
10675 terminal->condemn_scroll_bars_hook = XTcondemn_scroll_bars;
10676 terminal->redeem_scroll_bar_hook = XTredeem_scroll_bar;
10677 terminal->judge_scroll_bars_hook = XTjudge_scroll_bars;
10679 terminal->delete_frame_hook = x_destroy_window;
10680 terminal->delete_terminal_hook = x_delete_terminal;
10682 terminal->rif = &x_redisplay_interface;
10683 terminal->scroll_region_ok = 1; /* We'll scroll partial frames. */
10684 terminal->char_ins_del_ok = 1;
10685 terminal->line_ins_del_ok = 1; /* We'll just blt 'em. */
10686 terminal->fast_clear_end_of_line = 1; /* X does this well. */
10687 terminal->memory_below_frame = 0; /* We don't remember what scrolls
10688 off the bottom. */
10690 return terminal;
10693 void
10694 x_initialize (void)
10696 baud_rate = 19200;
10698 x_noop_count = 0;
10699 last_tool_bar_item = -1;
10700 any_help_event_p = 0;
10701 ignore_next_mouse_click_timeout = 0;
10703 #ifdef USE_GTK
10704 current_count = -1;
10705 #endif
10707 /* Try to use interrupt input; if we can't, then start polling. */
10708 Fset_input_interrupt_mode (Qt);
10710 #ifdef USE_X_TOOLKIT
10711 XtToolkitInitialize ();
10713 Xt_app_con = XtCreateApplicationContext ();
10715 /* Register a converter from strings to pixels, which uses
10716 Emacs' color allocation infrastructure. */
10717 XtAppSetTypeConverter (Xt_app_con,
10718 XtRString, XtRPixel, cvt_string_to_pixel,
10719 cvt_string_to_pixel_args,
10720 XtNumber (cvt_string_to_pixel_args),
10721 XtCacheByDisplay, cvt_pixel_dtor);
10723 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
10724 #endif
10726 #ifdef USE_TOOLKIT_SCROLL_BARS
10727 #ifndef USE_GTK
10728 xaw3d_arrow_scroll = False;
10729 xaw3d_pick_top = True;
10730 #endif
10731 #endif
10733 pending_autoraise_frame = 0;
10734 pending_event_wait.f = 0;
10735 pending_event_wait.eventtype = 0;
10737 /* Note that there is no real way portable across R3/R4 to get the
10738 original error handler. */
10739 XSetErrorHandler (x_error_handler);
10740 XSetIOErrorHandler (x_io_error_quitter);
10742 signal (SIGPIPE, x_connection_signal);
10744 xgselect_initialize ();
10748 void
10749 syms_of_xterm (void)
10751 x_error_message = NULL;
10753 staticpro (&x_display_name_list);
10754 x_display_name_list = Qnil;
10756 staticpro (&last_mouse_scroll_bar);
10757 last_mouse_scroll_bar = Qnil;
10759 DEFSYM (Qvendor_specific_keysyms, "vendor-specific-keysyms");
10760 DEFSYM (Qlatin_1, "latin-1");
10762 staticpro (&last_mouse_press_frame);
10763 last_mouse_press_frame = Qnil;
10765 #ifdef USE_GTK
10766 xg_default_icon_file = make_pure_c_string ("icons/hicolor/scalable/apps/emacs.svg");
10767 staticpro (&xg_default_icon_file);
10769 DEFSYM (Qx_gtk_map_stock, "x-gtk-map-stock");
10770 #endif
10772 DEFVAR_BOOL ("x-use-underline-position-properties",
10773 x_use_underline_position_properties,
10774 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
10775 A value of nil means ignore them. If you encounter fonts with bogus
10776 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
10777 to 4.1, set this to nil. You can also use `underline-minimum-offset'
10778 to override the font's UNDERLINE_POSITION for small font display
10779 sizes. */);
10780 x_use_underline_position_properties = 1;
10782 DEFVAR_BOOL ("x-underline-at-descent-line",
10783 x_underline_at_descent_line,
10784 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
10785 A value of nil means to draw the underline according to the value of the
10786 variable `x-use-underline-position-properties', which is usually at the
10787 baseline level. The default value is nil. */);
10788 x_underline_at_descent_line = 0;
10790 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
10791 x_mouse_click_focus_ignore_position,
10792 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
10793 This variable is only used when the window manager requires that you
10794 click on a frame to select it (give it focus). In that case, a value
10795 of nil, means that the selected window and cursor position changes to
10796 reflect the mouse click position, while a non-nil value means that the
10797 selected window or cursor position is preserved. */);
10798 x_mouse_click_focus_ignore_position = 0;
10800 DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
10801 doc: /* Which toolkit scroll bars Emacs uses, if any.
10802 A value of nil means Emacs doesn't use toolkit scroll bars.
10803 With the X Window system, the value is a symbol describing the
10804 X toolkit. Possible values are: gtk, motif, xaw, or xaw3d.
10805 With MS Windows, the value is t. */);
10806 #ifdef USE_TOOLKIT_SCROLL_BARS
10807 #ifdef USE_MOTIF
10808 Vx_toolkit_scroll_bars = intern_c_string ("motif");
10809 #elif defined HAVE_XAW3D
10810 Vx_toolkit_scroll_bars = intern_c_string ("xaw3d");
10811 #elif USE_GTK
10812 Vx_toolkit_scroll_bars = intern_c_string ("gtk");
10813 #else
10814 Vx_toolkit_scroll_bars = intern_c_string ("xaw");
10815 #endif
10816 #else
10817 Vx_toolkit_scroll_bars = Qnil;
10818 #endif
10820 staticpro (&last_mouse_motion_frame);
10821 last_mouse_motion_frame = Qnil;
10823 Qmodifier_value = intern_c_string ("modifier-value");
10824 Qalt = intern_c_string ("alt");
10825 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
10826 Qhyper = intern_c_string ("hyper");
10827 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
10828 Qmeta = intern_c_string ("meta");
10829 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
10830 Qsuper = intern_c_string ("super");
10831 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
10833 DEFVAR_LISP ("x-alt-keysym", Vx_alt_keysym,
10834 doc: /* Which keys Emacs uses for the alt modifier.
10835 This should be one of the symbols `alt', `hyper', `meta', `super'.
10836 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
10837 is nil, which is the same as `alt'. */);
10838 Vx_alt_keysym = Qnil;
10840 DEFVAR_LISP ("x-hyper-keysym", Vx_hyper_keysym,
10841 doc: /* Which keys Emacs uses for the hyper modifier.
10842 This should be one of the symbols `alt', `hyper', `meta', `super'.
10843 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
10844 default is nil, which is the same as `hyper'. */);
10845 Vx_hyper_keysym = Qnil;
10847 DEFVAR_LISP ("x-meta-keysym", Vx_meta_keysym,
10848 doc: /* Which keys Emacs uses for the meta modifier.
10849 This should be one of the symbols `alt', `hyper', `meta', `super'.
10850 For example, `meta' means use the Meta_L and Meta_R keysyms. The
10851 default is nil, which is the same as `meta'. */);
10852 Vx_meta_keysym = Qnil;
10854 DEFVAR_LISP ("x-super-keysym", Vx_super_keysym,
10855 doc: /* Which keys Emacs uses for the super modifier.
10856 This should be one of the symbols `alt', `hyper', `meta', `super'.
10857 For example, `super' means use the Super_L and Super_R keysyms. The
10858 default is nil, which is the same as `super'. */);
10859 Vx_super_keysym = Qnil;
10861 DEFVAR_LISP ("x-keysym-table", Vx_keysym_table,
10862 doc: /* Hash table of character codes indexed by X keysym codes. */);
10863 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
10864 make_float (DEFAULT_REHASH_SIZE),
10865 make_float (DEFAULT_REHASH_THRESHOLD),
10866 Qnil, Qnil, Qnil);
10869 #endif /* HAVE_X_WINDOWS */